libs/uvl: fix some memory wasting
[project/luci.git] / libs / uvl / luasrc / uvl.lua
index 9cdb994..3b5b854 100644 (file)
@@ -8,7 +8,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
-        http://www.apache.org/licenses/LICENSE-2.0
+               http://www.apache.org/licenses/LICENSE-2.0
 
 $Id$
 
@@ -19,15 +19,16 @@ $Id$
 -- @class      module
 -- @cstyle     instance
 
-local fs = require "luci.fs"
+local fs = require "nixio.fs"
 local uci = require "luci.model.uci"
 local util = require "luci.util"
+local nutil = require "nixio.util"
 local table = require "table"
 local string = require "string"
 
 local require, pcall, ipairs, pairs = require, pcall, ipairs, pairs
 local type, error, tonumber, tostring = type, error, tonumber, tostring
-local unpack, loadfile = unpack, loadfile
+local unpack, loadfile, collectgarbage = unpack, loadfile, collectgarbage
 
 module "luci.uvl"
 
@@ -42,6 +43,10 @@ local TYPE_SECTION  = 0x02
 local TYPE_OPTION   = 0x03
 local TYPE_ENUM     = 0x04
 
+local PAT_EXPR1                = "^%$?[%w_]+$"
+local PAT_EXPR2                = "^%$?[%w_]+%.%$?[%w_]+$"
+local PAT_EXPR3                = "^%$?[%w_]+%.%$?[%w_]+%.%$?[%w_]+$"
+
 --- Boolean; default true;
 -- treat sections found in config but not in scheme as error
 STRICT_UNKNOWN_SECTIONS    = true
@@ -67,11 +72,13 @@ local default_savedir = "/tmp/.uvl"
 -- @class                      function
 -- @name                       UVL
 -- @param schemedir    Path to the scheme directory (optional)
+-- @param configdir    Override config directory (optional)
 -- @return                     Instance object
 UVL = util.class()
 
-function UVL.__init__( self, schemedir )
+function UVL.__init__( self, schemedir, configdir )
        self.schemedir  = schemedir or default_schemedir
+       self.configdir  = configdir
        self.packages   = { }
        self.beenthere  = { }
        self.depseen    = { }
@@ -271,7 +278,7 @@ function UVL._validate_section( self, section )
        if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
                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
+                       if k:byte(1) == 46 and not self.beenthere[oo:cid()] then
                                section:error(ERR.OPT_UNKNOWN(oo))
                        end
                end
@@ -292,6 +299,22 @@ function UVL._validate_option( self, option, nodeps )
                end
 
        elseif option:scheme() then
+               if not nodeps then
+                       local ok, err = dependencies.check( self, option )
+                       if not ok then
+                               if not err:is_all(
+                                       ERR.ERR_OPT_REQUIRED,
+                                       ERR.ERR_DEP_NOTEQUAL,
+                                       ERR.ERR_DEP_NOVALUE
+                               ) then
+                                       option:error(err)
+                                       return false, option:errors()
+                               else
+                                       return true
+                               end
+                       end
+               end
+
                if option:scheme('required') and not option:value() then
                        return false, option:error(ERR.OPT_REQUIRED(option))
 
@@ -334,12 +357,34 @@ function UVL._validate_option( self, option, nodeps )
                                        return false, option:error(ERR.OPT_DATATYPE(option, dt))
                                end
                        end
-               end
 
-               if not nodeps then
-                       local ok, err = dependencies.check( self, option )
-                       if not ok then
-                               option:error(err)
+                       val = ( type(val) == "table" and val or { val } )
+                       for _, v in ipairs(val) do
+                               if option:scheme('minlength') then
+                                       if #v < option:scheme('minlength') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               if option:scheme('maxlength') then
+                                       if #v > option:scheme('maxlength') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               local w = tonumber(v)
+
+                               if option:scheme('minimum') then
+                                       if not w or w < option:scheme('minimum') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               if option:scheme('maximum') then
+                                       if not w or w > option:scheme('maximum') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
                        end
                end
 
@@ -363,11 +408,11 @@ function UVL.read_scheme( self, shm, alias )
        local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
 
        if not fs.access(bc) then
-               local files = fs.glob(self.schemedir .. '/*/' .. shm)
+               local files = nutil.consume((fs.glob(self.schemedir .. '/*/' .. shm)))
 
-               if files then
+               if #files > 0 then
                        local ok, err
-                       for i, file in ipairs( files ) do
+                       for _, file in ipairs(files) do
                                if not fs.access(file) then
                                        return false, so:error(ERR.SME_READ(so,file))
                                end
@@ -501,7 +546,7 @@ function UVL._parse_section(self, scheme, k, v)
        local so = scheme:section(v.name)
 
        for k, v2 in pairs(v) do
-               if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
+               if k ~= "name" and k ~= "package" and k:byte(1) == 46 then
                        if k == "depends" then
                                s.depends = self:_read_dependency( v2, s.depends )
                                if not s.depends then
@@ -525,8 +570,7 @@ function UVL._parse_section(self, scheme, k, v)
        s.named    = s.named    or false
 end
 
-
-       -- Step 2: get all variables
+-- Step 2: get all variables
 function UVL._parse_var(self, scheme, k, v)
        local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
        if err then error(scheme:error(err)) end
@@ -555,7 +599,7 @@ function UVL._parse_var(self, scheme, k, v)
        local to = so:option(v.name)
 
        for k, v2 in pairs(v) do
-               if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
+               if k ~= "name" and k ~= "section" and k:byte(1) == 46 then
                        if k == "depends" then
                                t.depends = self:_read_dependency( v2, t.depends )
                                if not t.depends then
@@ -582,6 +626,10 @@ function UVL._parse_var(self, scheme, k, v)
                                t.valueof = type(v2) == "table" and v2 or {v2}
                        elseif k == "required" then
                                t[k] = _bool(v2)
+                       elseif k == "minlength" or k == "maxlength" or
+                   k == "minimum" or k == "maximum"
+            then
+                               t[k] = tonumber(v2)
                        else
                                t[k] = t[k] or v2
                        end
@@ -633,8 +681,10 @@ function UVL._parse_enum(self, scheme, k, v)
 
        if not t.values then
                t.values = { [v.value] = v.title or v.value }
+               t.valuelist = { {value = v.value, title = v.title} }
        else
                t.values[v.value] = v.title or v.value
+               t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
        end
 
        if not t.enum_depends then
@@ -663,20 +713,18 @@ end
 
 -- Read a dependency specification
 function UVL._read_dependency( self, values, deps )
-       local expr = "%$?[a-zA-Z0-9_]+"
+       local expr = "%$?[%w_]+"
        if values then
                values = ( type(values) == "table" and values or { values } )
                for _, value in ipairs(values) do
                        local condition = { }
-                       for val in value:gmatch("[^%s,]+") do
-                               local k, v = val:match("([^%s=]+)%s*=*%s*([^%s]*)")
+                       for val in value:gmatch("[^,]+") do
+                               local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
 
                                if k and (
-                                       k:match("^"..expr.."%."..expr.."%."..expr.."$") or
-                                       k:match("^"..expr.."%."..expr.."$") or
-                                       k:match("^"..expr.."$")
+                                       k:match(PAT_EXPR1) or k:match(PAT_EXPR2) or k:match(PAT_EXPR3)
                                ) then
-                                       condition[k] = v or true
+                                       condition[k] = (e == '=') and v or true
                                else
                                        return nil
                                end
@@ -706,8 +754,8 @@ function UVL._read_validator( self, values, validators )
                                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 = { ... }
+                               validator = function( type, dtype, pack, sect, optn, arg1, arg2, arg3, arg4, arg5 )
+                                       local values = { arg1, arg2, arg3, arg4, arg5 }
                                        for _, v in ipairs(values) do
                                                local ok, match =
                                                        pcall( string.match, v, pattern )
@@ -811,7 +859,7 @@ function uvlitem.cid(self)
                local c = self.c
                if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
                        r[2] = '@' .. c[r[2]]['.type'] ..
-                              '[' .. tostring(c[r[2]]['.index']) .. ']'
+                                  '[' .. tostring(c[r[2]]['.index']) .. ']'
                end
                return table.concat( r, '.' )
        end
@@ -848,7 +896,7 @@ function uvlitem.config(self, opt)
                if #self.cref >= 3 then
                        c = c and c[self.cref[3]] or nil
                end
-       end     
+       end
 
        if c and opt then
                return c[opt]
@@ -874,13 +922,13 @@ function uvlitem.type(self)
        end
 end
 
-function uvlitem.error(self, ...)
+function uvlitem.error(self, arg1, arg2, arg3, arg4, arg5)
        if not self.e then
                local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
                self.e = errconst[#self.cref]( self )
        end
 
-       return self.e:child( ... )
+       return self.e:child( arg1, arg2, arg3, arg4, arg5 )
 end
 
 function uvlitem.errors(self)
@@ -903,16 +951,16 @@ function uvlitem.parent(self)
        end
 end
 
-function uvlitem._loadconf(self, co, c)
+function uvlitem._loadconf(self, co, c, configdir)
        co = co or self._configcache
        if not co then
                local err
-               co, err = uci.cursor():get_all(c)
+               co, err = uci.cursor(configdir):get_all(c)
 
                if err then
                        self:error(ERR.UCILOAD(self, err))
                end
-               
+
                self._configcache = co
        end
        return co
@@ -940,16 +988,16 @@ function scheme.__init__(self, scheme, co, c)
 
        self.cref = { c }
        self.sref = { c }
-       self.c    = self:_loadconf(co, c)
+       self.c    = self:_loadconf(co, c, scheme.configdir)
        self.s    = scheme
        self.t    = TYPE_SCHEME
 end
 
 --- Add an error to scheme.
 -- @return     Scheme error context
-function scheme.error(self, ...)
+function scheme.error(self, arg1, arg2, arg3, arg4, arg5)
        if not self.e then self.e = ERR.SCHEME( self ) end
-       return self.e:child( ... )
+       return self.e:child( arg1, arg2, arg3, arg4, arg5 )
 end
 
 --- Get an associated config object.
@@ -1004,10 +1052,9 @@ function config.__init__(self, scheme, co, c)
        if not c then
                c, co = co, nil
        end
-
        self.cref = { c }
        self.sref = { c }
-       self.c    = self:_loadconf(co, c)
+       self.c    = self:_loadconf(co, c, scheme.configdir)
        self.s    = scheme
        self.t    = TYPE_CONFIG
 end
@@ -1055,7 +1102,7 @@ section = 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    = self:_loadconf(co, c)
+       self.c    = self:_loadconf(co, c, scheme.configdir)
        self.s    = scheme
        self.t    = TYPE_SECTION
 end
@@ -1106,7 +1153,7 @@ option = 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    = self:_loadconf(co, c)
+       self.c    = self:_loadconf(co, c, scheme.configdir)
        self.s    = scheme
        self.t    = TYPE_OPTION
 end
@@ -1159,7 +1206,7 @@ enum = 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    = self:_loadconf(co, c)
+       self.c    = self:_loadconf(co, c, scheme.configdir)
        self.s    = scheme
        self.t    = TYPE_ENUM
 end