X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi.lua;h=ef45a89563e38a1921c765407da412063bb46295;hp=86658d70209bf0fbc79c108b5791519cbc4dec7d;hb=a4996261eaa0b2057967e5eb855005a3403a6cb0;hpb=4fb5f1aca642eb94442d37d02eb81185f1f1ef03 diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua index 86658d702..ef45a8956 100644 --- a/libs/web/luasrc/cbi.lua +++ b/libs/web/luasrc/cbi.lua @@ -102,7 +102,7 @@ function load(cbimap, ...) for _, field in ipairs(map.upload_fields) do uploads[ field.config .. '.' .. - field.section.sectiontype .. '.' .. + (field.section.sectiontype or '1') .. '.' .. field.option ] = true end @@ -124,8 +124,8 @@ function load(cbimap, ...) )() if c and s and o then - local t = uci:get( c, s ) - if t and uploads[c.."."..t.."."..o] then + local t = uci:get( c, s ) or s + if uploads[c.."."..t.."."..o] then local path = upldir .. field.name fd = io.open(path, "w") if fd then @@ -152,6 +152,78 @@ function load(cbimap, ...) return maps end +-- +-- Compile a datatype specification into a parse tree for evaluation later on +-- +local cdt_cache = { } + +function compile_datatype(code) + local i + local pos = 0 + local esc = false + local depth = 0 + local stack = { } + + for i = 1, #code+1 do + local byte = code:byte(i) or 44 + if esc then + esc = false + elseif byte == 92 then + esc = true + elseif byte == 40 or byte == 44 then + if depth <= 0 then + if pos < i then + local label = code:sub(pos, i-1) + :gsub("\\(.)", "%1") + :gsub("^%s+", "") + :gsub("%s+$", "") + + if #label > 0 and tonumber(label) then + stack[#stack+1] = tonumber(label) + elseif label:match("^'.*'$") or label:match('^".*"$') then + stack[#stack+1] = label:gsub("[\"'](.*)[\"']", "%1") + elseif type(datatypes[label]) == "function" then + stack[#stack+1] = datatypes[label] + stack[#stack+1] = { } + else + error("Datatype error, bad token %q" % label) + end + end + pos = i + 1 + end + depth = depth + (byte == 40 and 1 or 0) + elseif byte == 41 then + depth = depth - 1 + if depth <= 0 then + if type(stack[#stack-1]) ~= "function" then + error("Datatype error, argument list follows non-function") + end + stack[#stack] = compile_datatype(code:sub(pos, i-1)) + pos = i + 1 + end + end + end + + return stack +end + +function verify_datatype(dt, value) + if dt and #dt > 0 then + if not cdt_cache[dt] then + local c = compile_datatype(dt) + if c and type(c[1]) == "function" then + cdt_cache[dt] = c + else + error("Datatype error, not a function expression") + end + end + if cdt_cache[dt] then + return cdt_cache[dt][1](value, unpack(cdt_cache[dt][2])) + end + end + return true +end + -- Node pseudo abstract class Node = class() @@ -1325,29 +1397,8 @@ end function AbstractValue.render(self, s, scope) if not self.optional or self.section:has_tabs() or self:cfgvalue(s) or self:formcreated(s) then scope = scope or {} - scope.section = s - scope.cbid = self:cbid(s) - scope.striptags = luci.util.striptags - scope.pcdata = luci.util.pcdata - - scope.ifattr = function(cond,key,val) - if cond then - return string.format( - ' %s="%s"', tostring(key), - luci.util.pcdata(tostring( val - or scope[key] - or (type(self[key]) ~= "function" and self[key]) - or "" )) - ) - else - return '' - end - end - - scope.attr = function(...) - return scope.ifattr( true, ... ) - end - + scope.section = s + scope.cbid = self:cbid(s) Node.render(self, scope) end end @@ -1377,31 +1428,17 @@ end -- Validate the form value function AbstractValue.validate(self, value) if self.datatype and value then - local args = { } - local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)") - - if dt and ar then - local a - for a in ar:gmatch("[^%s,]+") do - args[#args+1] = a - end - else - dt = self.datatype - end - - if dt and datatypes[dt] then - if type(value) == "table" then - local v - for _, v in ipairs(value) do - if v and #v > 0 and not datatypes[dt](v, unpack(args)) then - return nil - end - end - else - if not datatypes[dt](value, unpack(args)) then + if type(value) == "table" then + local v + for _, v in ipairs(value) do + if v and #v > 0 and not verify_datatype(self.datatype, v) then return nil end end + else + if not verify_datatype(self.datatype, value) then + return nil + end end end