* luci/libs/uvl: fix an error message in luci.uvl.read_scheme()
[project/luci.git] / libs / uvl / luasrc / uvl.lua
index 24dc3a2..aada2e6 100644 (file)
@@ -33,9 +33,8 @@ require("luci.uvl.dependencies")
 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
@@ -55,6 +54,7 @@ STRICT_LIST_TYPE           = true
 
 
 local default_schemedir = "/lib/uci/schema"
+local default_savedir = "/tmp/.uvl"
 local ERR = luci.uvl.errors
 
 
@@ -69,6 +69,7 @@ function UVL.__init__( self, schemedir )
        self.schemedir  = schemedir or default_schemedir
        self.packages   = { }
        self.beenthere  = { }
+       self.depseen    = { }
        self.uci                = luci.model.uci
        self.err                = luci.uvl.errors
        self.dep                = luci.uvl.dependencies
@@ -123,9 +124,10 @@ function UVL.validate_config( self, config, uci )
        local sc = { }
 
        self.beenthere = { }
+       self.depseen   = { }
 
        if not co:config() then
-               return false, ERR.UCILOAD(co)
+               return false, co:errors()
        end
 
        local function _uci_foreach( type, func )
@@ -186,9 +188,10 @@ function UVL.validate_section( self, config, section, uci )
        local so = co:section( section )
 
        self.beenthere = { }
+       self.depseen   = { }
 
        if not co:config() then
-               return false, ERR.UCILOAD(co)
+               return false, co:errors()
        end
 
        if so:config() then
@@ -217,8 +220,8 @@ function UVL.validate_option( self, config, section, option, uci )
        local so = co:section( section )
        local oo = so:option( option )
 
-       if not co then
-               return false, oerr:child(ERR.UCILOAD(config))
+       if not co:config() then
+               return false, co:errors()
        end
 
        if so:config() and oo:config() then
@@ -231,6 +234,8 @@ end
 
 function UVL._validate_section( self, section )
 
+       self.beenthere[section:cid()] = true
+
        if section:config() then
                if section:scheme('named') == true and
                   section:config('.anonymous') == true
@@ -240,7 +245,12 @@ function UVL._validate_section( self, section )
 
                for _, v in ipairs(section:variables()) do
                        local ok, err = self:_validate_option( v )
-                       if not ok then
+                       if not ok and (
+                               v:scheme('required') or v:scheme('type') == "enum" or (
+                                       not err:is(ERR.ERR_DEP_NOTEQUAL) and
+                                       not err:is(ERR.ERR_DEP_NOVALUE)
+                               )
+                       ) then
                                section:error(err)
                        end
                end
@@ -257,7 +267,7 @@ function UVL._validate_section( self, section )
                for k, v in pairs(section:config()) do
                        local oo = section:option(k)
                        if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
-                               section:error(ERR.OPT_NOTFOUND(oo))
+                               section:error(ERR.OPT_UNKNOWN(oo))
                        end
                end
        end
@@ -267,8 +277,14 @@ end
 
 function UVL._validate_option( self, option, nodeps )
 
+       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
@@ -280,20 +296,24 @@ function UVL._validate_option( self, option, nodeps )
                        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, { val, table.concat(
-                                                       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
@@ -301,7 +321,7 @@ function UVL._validate_option( self, option, nodeps )
                                        for i, v in ipairs(val) do
                                                if not self.datatypes[dt]( v ) then
                                                        return false, option:error(
-                                                               ERR.OPT_INVVALUE(option, {v, dt})
+                                                               ERR.OPT_INVVALUE(option, { v, dt })
                                                        )
                                                end
                                        end
@@ -331,34 +351,47 @@ 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()
-                             uci:set_confdir( luci.fs.dirname(file) )
+                               local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
 
-                       local sd = 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)
+                               if not sd then
+                                       return false, ERR.UCILOAD(so, err)
+                               end
+
+                               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
 
@@ -366,10 +399,20 @@ 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
@@ -381,7 +424,7 @@ function UVL._read_scheme_parts( self, scheme, schemes )
                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
@@ -407,12 +450,32 @@ function UVL._read_scheme_parts( self, scheme, schemes )
 
        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 )
@@ -464,10 +527,10 @@ function UVL._read_scheme_parts( self, scheme, schemes )
                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]]
@@ -535,7 +598,7 @@ function UVL._read_scheme_parts( self, scheme, schemes )
                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 )
@@ -650,6 +713,25 @@ function UVL._read_validator( self, values, validators )
                                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
@@ -677,7 +759,7 @@ function UVL._read_reference( self, values )
 
                if #ref == 2 or #ref == 3 then
                        local co = luci.uvl.config( self, ref[1] )
-                       if not co:config() then return false, ERR.UCILOAD(ref[1]) end
+                       if not co:config() then return false, co:errors() end
 
                        for k, v in pairs(co:config()) do
                                if v['.type'] == ref[2] then
@@ -742,16 +824,19 @@ function uvlitem.scheme(self, opt)
        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
@@ -765,9 +850,10 @@ function uvlitem.config(self, opt)
        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
@@ -825,6 +911,18 @@ function uvlitem.parent(self)
        end
 end
 
+function uvlitem._loadconf(self, co, c)
+       if not co then
+               local uci, err = luci.model.uci.cursor(), nil
+               co, err = uci:get_all(c)
+
+               if err then
+                       self:error(ERR.UCILOAD(self, err))
+               end
+       end
+       return co
+end
+
 
 --- Object representation of a scheme.
 -- @class      scheme
@@ -845,14 +943,9 @@ function scheme.__init__(self, scheme, co, c)
                c, co = co, nil
        end
 
-       if not co then
-               local uci = luci.model.uci.cursor()
-               co = uci:get_all(c)
-       end
-
        self.cref = { c }
        self.sref = { c }
-       self.c    = co
+       self.c    = self:_loadconf(co, c)
        self.s    = scheme
        self.t    = luci.uvl.TYPE_SCHEME
 end
@@ -917,14 +1010,9 @@ function config.__init__(self, scheme, co, c)
                c, co = co, nil
        end
 
-       if not co then
-               local uci = luci.model.uci.cursor()
-               co = uci:get_all(c)
-       end
-
        self.cref = { c }
        self.sref = { c }
-       self.c    = co
+       self.c    = self:_loadconf(co, c)
        self.s    = scheme
        self.t    = luci.uvl.TYPE_CONFIG
 end
@@ -972,7 +1060,7 @@ section = luci.util.class(uvlitem)
 function section.__init__(self, scheme, co, c, s)
        self.cref = { c, s }
        self.sref = { c, co and co[s] and co[s]['.type'] or s }
-       self.c    = co
+       self.c    = self:_loadconf(co, c)
        self.s    = scheme
        self.t    = luci.uvl.TYPE_SECTION
 end
@@ -1023,7 +1111,7 @@ option = luci.util.class(uvlitem)
 function option.__init__(self, scheme, co, c, s, o)
        self.cref = { c, s, o }
        self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
-       self.c    = co
+       self.c    = self:_loadconf(co, c)
        self.s    = scheme
        self.t    = luci.uvl.TYPE_OPTION
 end
@@ -1031,7 +1119,11 @@ 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.
@@ -1072,7 +1164,7 @@ enum = luci.util.class(option)
 function enum.__init__(self, scheme, co, c, s, o, v)
        self.cref = { c, s, o, v }
        self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
-       self.c    = co
+       self.c    = self:_loadconf(co, c)
        self.s    = scheme
        self.t    = luci.uvl.TYPE_ENUM
 end