TYPE_SCHEME = 0x00
TYPE_CONFIG = 0x01
TYPE_SECTION = 0x02
-TYPE_VARIABLE = 0x03
-TYPE_OPTION = 0x04
-TYPE_ENUM = 0x05
+TYPE_OPTION = 0x03
+TYPE_ENUM = 0x04
--- Boolean; default true;
-- treat sections found in config but not in scheme as error
self.beenthere[option:cid()] = true
if not option:scheme() and not option:parent():scheme('dynamic') then
- return false, option:error(ERR.OPT_UNKNOWN(option))
+ if STRICT_UNKNOWN_OPTIONS then
+ return false, option:error(ERR.OPT_UNKNOWN(option))
+ else
+ return true
+ end
elseif option:scheme() then
if option:scheme('required') and not option:value() then
if option:scheme('type') == "reference" or
option:scheme('type') == "enum"
then
- if not option:scheme('values') or
- not option:scheme('values')[val]
- then
- return false, option:error( ERR.OPT_BADVALUE(
- option, luci.util.serialize_data(
- luci.util.keys(option:scheme('values') or {})
- )
- ) )
+ local scheme_values = option:scheme('values') or { }
+ local config_values = ( type(val) == "table" and val or { val } )
+ for _, v in ipairs(config_values) do
+ if not scheme_values[v] then
+ return false, option:error( ERR.OPT_BADVALUE(
+ option, { v, luci.util.serialize_data(
+ luci.util.keys(scheme_values)
+ ) }
+ ) )
+ end
end
elseif option:scheme('type') == "list" then
if type(val) ~= "table" and STRICT_LIST_TYPE then
return false, option:error(ERR.OPT_NOTLIST(option))
end
- elseif option:scheme('datatype') then
+ end
+
+ if option:scheme('datatype') then
local dt = option:scheme('datatype')
if self.datatypes[dt] then
for i, v in ipairs(val) do
if not self.datatypes[dt]( v ) then
return false, option:error(
- ERR.OPT_INVVALUE(option, dt)
+ ERR.OPT_INVVALUE(option, { v, dt })
)
end
end
-- This is normally done on demand, so you don't have to call this function
-- by yourself.
-- @param scheme Name of the scheme to parse
-function UVL.read_scheme( self, scheme )
+-- @param alias Create an alias for the loaded scheme
+function UVL.read_scheme( self, scheme, alias )
local so = luci.uvl.scheme( self, scheme )
+ local bc = "%s/bytecode/%s.lua" %{ self.schemedir, scheme }
- local schemes = { }
- local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
+ if not luci.fs.access(bc) then
+ local schemes = { }
+ local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
- if files then
- for i, file in ipairs( files ) do
- if not luci.fs.access(file) then
- return so:error(ERR.SME_READ(so,file))
- end
+ if files then
+ for i, file in ipairs( files ) do
+ if not luci.fs.access(file) then
+ return false, so:error(ERR.SME_READ(so,file))
+ end
+
+ local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
- local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
+ local sd, err = uci:get_all( luci.fs.basename(file) )
- local sd, err = uci:get_all( luci.fs.basename(file) )
+ if not sd then
+ return false, ERR.UCILOAD(so, err)
+ end
- if not sd then
- return false, ERR.UCILOAD(so, err)
+ table.insert( schemes, sd )
end
- table.insert( schemes, sd )
+ local ok, err = self:_read_scheme_parts( so, schemes )
+ if ok and alias then self.packages[alias] = self.packages[scheme] end
+ return ok, err
+ else
+ return false, so:error(ERR.SME_FIND(so, self.schemedir))
end
-
- return self:_read_scheme_parts( so, schemes )
else
- return false, so:error(ERR.SME_FIND(so, self.schemedir))
+ local sc = loadfile(bc)
+ if sc then
+ self.packages[scheme] = sc()
+ return true
+ else
+ return false, so:error(ERR.SME_READ(so,bc))
+ end
end
end
function UVL._read_scheme_parts( self, scheme, schemes )
-- helper function to check for required fields
- local function _req( c, t, r )
+ local function _req( t, n, c, r )
for i, v in ipairs(r) do
- if not t[v] then
- return false, ERR.SME_REQFLD({c,t}, v)
+ if not c[v] then
+ local p, o = scheme:sid(), nil
+
+ if t == TYPE_SECTION then
+ o = section( scheme, nil, p, n )
+ elseif t == TYPE_OPTION then
+ o = option( scheme, nil, p, '(nil)', n )
+ elseif t == TYPE_ENUM then
+ o = enum( scheme, nil, p, '(nil)', '(nil)', n )
+ end
+
+ return false, ERR.SME_REQFLD(o,v)
end
end
return true
if c == TYPE_SECTION then
k = "package"
n = 1
- elseif c == TYPE_VARIABLE then
+ elseif c == TYPE_OPTION then
k = "section"
n = 2
elseif c == TYPE_ENUM then
local ok, err
+ -- Step 0: get package meta information
+ for i, conf in ipairs( schemes ) do
+ for k, v in pairs( conf ) do
+ if v['.type'] == 'package' then
+ self.packages[scheme:sid()] =
+ self.packages[scheme:sid()] or {
+ ["name"] = scheme:sid();
+ ["sections"] = { };
+ ["variables"] = { };
+ }
+
+ for k, v2 in pairs(v) do
+ if k == "title" or k == "description" then
+ self.packages[scheme:sid()][k] = v2
+ end
+ end
+ end
+ end
+ end
+
-- Step 1: get all sections
for i, conf in ipairs( schemes ) do
for k, v in pairs( conf ) do
if v['.type'] == 'section' then
- ok, err = _req( TYPE_SECTION, v, { "name", "package" } )
+ ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
if err then return false, scheme:error(err) end
local r, err = _ref( TYPE_SECTION, v )
for k, v in pairs( conf ) do
if v['.type'] == "variable" then
- ok, err = _req( TYPE_VARIABLE, v, { "name", "section" } )
+ ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
if err then return false, scheme:error(err) end
- local r, err = _ref( TYPE_VARIABLE, v )
+ local r, err = _ref( TYPE_OPTION, v )
if err then return false, scheme:error(err) end
local p = self.packages[r[1]]
for k, v in pairs( conf ) do
if v['.type'] == "enum" then
- ok, err = _req( TYPE_ENUM, v, { "value", "variable" } )
+ ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
if err then return false, scheme:error(err) end
local r, err = _ref( TYPE_ENUM, v )
validator = value:gsub("^exec:","")
elseif value:match("^lua:") then
validator = self:_resolve_function( (value:gsub("^lua:","") ) )
+ elseif value:match("^regexp:") then
+ local pattern = value:gsub("^regexp:","")
+ validator = function( type, dtype, pack, sect, optn, ... )
+ local values = { ... }
+ for _, v in ipairs(values) do
+ local ok, match =
+ luci.util.copcall( string.match, v, pattern )
+
+ if not ok then
+ return false, match
+ elseif not match then
+ return false,
+ 'Value "%s" does not match pattern "%s"' % {
+ v, pattern
+ }
+ end
+ end
+ return true
+ end
end
if validator then
local s
if #self.sref == 4 or #self.sref == 3 then
- s = self.s
- .packages[self.sref[1]]
- .variables[self.sref[2]][self.sref[3]]
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
+ s = s and s.variables
+ s = s and s[self.sref[2]]
+ s = s and s[self.sref[3]]
elseif #self.sref == 2 then
- s = self.s
- .packages[self.sref[1]]
- .sections[self.sref[2]]
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
+ s = s and s.sections
+ s = s and s[self.sref[2]]
else
- s = self.s
- .packages[self.sref[1]]
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
end
if s and opt then
local c
if #self.cref == 4 or #self.cref == 3 then
- c = self.c[self.cref[2]][self.cref[3]]
+ c = self.c and self.c[self.cref[2]] or nil
+ c = c and c[self.cref[3]] or nil
elseif #self.cref == 2 then
- c = self.c[self.cref[2]]
+ c = self.c and self.c[self.cref[2]] or nil
else
c = self.c
end
--- Get the value of this option.
-- @return The associated configuration value
function option.value(self)
- return self:config()
+ local v = self:config()
+ if v and self:scheme('multival') then
+ v = luci.util.split( v, "%s+", nil, true )
+ end
+ return v
end
--- Get the associated section information in scheme.