* luci/libs/uvl:
[project/luci.git] / libs / uvl / luasrc / uvl.lua
1 --[[
2
3 UCI Validation Layer - Main Library
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17
18 --- UVL - UCI Validation Layer
19 -- @class       module
20 -- @cstyle      instance
21
22 module( "luci.uvl", package.seeall )
23
24 require("luci.fs")
25 require("luci.util")
26 require("luci.model.uci")
27 require("luci.uvl.errors")
28 require("luci.uvl.datatypes")
29 require("luci.uvl.validation")
30 require("luci.uvl.dependencies")
31
32
33 TYPE_SCHEME   = 0x00
34 TYPE_CONFIG   = 0x01
35 TYPE_SECTION  = 0x02
36 TYPE_OPTION   = 0x03
37 TYPE_ENUM     = 0x04
38
39 --- Boolean; default true;
40 -- treat sections found in config but not in scheme as error
41 STRICT_UNKNOWN_SECTIONS    = true
42
43 --- Boolean; default true;
44 -- treat options found in config but not in scheme as error
45 STRICT_UNKNOWN_OPTIONS     = true
46
47 --- Boolean; default true;
48 -- treat failed external validators as error
49 STRICT_EXTERNAL_VALIDATORS = true
50
51 --- Boolean; default true;
52 -- treat list values stored as options like errors
53 STRICT_LIST_TYPE           = true
54
55
56 local default_schemedir = "/lib/uci/schema"
57 local default_savedir = "/tmp/.uvl"
58 local ERR = luci.uvl.errors
59
60
61 --- Object constructor
62 -- @class                       function
63 -- @name                        UVL
64 -- @param schemedir     Path to the scheme directory (optional)
65 -- @return                      Instance object
66 UVL = luci.util.class()
67
68 function UVL.__init__( self, schemedir )
69         self.schemedir  = schemedir or default_schemedir
70         self.packages   = { }
71         self.beenthere  = { }
72         self.depseen    = { }
73         self.uci                = luci.model.uci
74         self.err                = luci.uvl.errors
75         self.dep                = luci.uvl.dependencies
76         self.datatypes  = luci.uvl.datatypes
77 end
78
79
80 --- Parse given scheme and return the scheme tree.
81 -- @param scheme        Name of the scheme to parse
82 -- @return                      Table containing the parsed scheme or nil on error
83 -- @return                      String containing the reason for errors (if any)
84 function UVL.get_scheme( self, scheme )
85         if not self.packages[scheme] then
86                 local ok, err = self:read_scheme( scheme )
87                 if not ok then
88                         return nil, err
89                 end
90         end
91         return self.packages[scheme], nil
92 end
93
94 --- Validate given configuration, section or option.
95 -- @param config        Name of the configuration to validate
96 -- @param section       Name of the section to validate (optional)
97 -- @param option        Name of the option to validate (optional)
98 -- @return                      Boolean indicating whether the given config validates
99 -- @return                      String containing the reason for errors (if any)
100 function UVL.validate( self, config, section, option )
101         if config and section and option then
102                 return self:validate_option( config, section, option )
103         elseif config and section then
104                 return self:validate_section( config, section )
105         elseif config then
106                 return self:validate_config( config )
107         end
108 end
109
110 --- Validate given configuration.
111 -- @param config        Name of the configuration to validate
112 -- @return                      Boolean indicating whether the given config validates
113 -- @return                      String containing the reason for errors (if any)
114 function UVL.validate_config( self, config, uci )
115
116         if not self.packages[config] then
117                 local ok, err = self:read_scheme(config)
118                 if not ok then
119                         return false, err
120                 end
121         end
122
123         local co = luci.uvl.config( self, uci or config, uci and config )
124         local sc = { }
125
126         self.beenthere = { }
127         self.depseen   = { }
128
129         if not co:config() then
130                 return false, co:errors()
131         end
132
133         local function _uci_foreach( type, func )
134                 for k, v in pairs(co:config()) do
135                         if v['.type'] == type then
136                                 sc[type] = sc[type] + 1
137                                 local ok, err = func( k, v )
138                                 if not ok then co:error(err) end
139                         end
140                 end
141         end
142
143         for k, v in pairs( self.packages[config].sections ) do
144                 sc[k] = 0
145                 _uci_foreach( k,
146                         function(s)
147                                 return self:_validate_section( co:section(s) )
148                         end
149                 )
150         end
151
152         if STRICT_UNKNOWN_SECTIONS then
153                 for k, v in pairs(co:config()) do
154                         local so = co:section(k)
155                         if not self.beenthere[so:cid()] then
156                                 co:error(ERR.SECT_UNKNOWN(so))
157                         end
158                 end
159         end
160
161         for _, k in ipairs(luci.util.keys(sc)) do
162                 local so = co:section(k)
163                 if so:scheme('required') and sc[k] == 0 then
164                         co:error(ERR.SECT_REQUIRED(so))
165                 elseif so:scheme('unique') and sc[k] > 1 then
166                         co:error(ERR.SECT_UNIQUE(so))
167                 end
168         end
169
170         return co:ok(), co:errors()
171 end
172
173 --- Validate given config section.
174 -- @param config        Name of the configuration to validate
175 -- @param section       Name of the section to validate
176 -- @return                      Boolean indicating whether the given config validates
177 -- @return                      String containing the reason for errors (if any)
178 function UVL.validate_section( self, config, section, uci )
179
180         if not self.packages[config] then
181                 local ok, err = self:read_scheme( config )
182                 if not ok then
183                         return false, err
184                 end
185         end
186
187         local co = luci.uvl.config( self, uci or config, uci and config )
188         local so = co:section( section )
189
190         self.beenthere = { }
191         self.depseen   = { }
192
193         if not co:config() then
194                 return false, co:errors()
195         end
196
197         if so:config() then
198                 return self:_validate_section( so )
199         else
200                 return false, ERR.SECT_NOTFOUND(so)
201         end
202 end
203
204 --- Validate given config option.
205 -- @param config        Name of the configuration to validate
206 -- @param section       Name of the section to validate
207 -- @param option        Name of the option to validate
208 -- @return                      Boolean indicating whether the given config validates
209 -- @return                      String containing the reason for errors (if any)
210 function UVL.validate_option( self, config, section, option, uci )
211
212         if not self.packages[config] then
213                 local ok, err = self:read_scheme( config )
214                 if not ok then
215                         return false, err
216                 end
217         end
218
219         local co = luci.uvl.config( self, uci or config, uci and config )
220         local so = co:section( section )
221         local oo = so:option( option )
222
223         if not co:config() then
224                 return false, co:errors()
225         end
226
227         if so:config() and oo:config() then
228                 return self:_validate_option( oo )
229         else
230                 return false, ERR.OPT_NOTFOUND(oo)
231         end
232 end
233
234
235 function UVL._validate_section( self, section )
236
237         self.beenthere[section:cid()] = true
238
239         if section:config() then
240                 if section:scheme('named') == true and
241                    section:config('.anonymous') == true
242                 then
243                         return false, ERR.SECT_NAMED(section)
244                 end
245
246                 for _, v in ipairs(section:variables()) do
247                         local ok, err = self:_validate_option( v )
248                         if not ok and (
249                                 v:scheme('required') or v:scheme('type') == "enum" or (
250                                         not err:is(ERR.ERR_DEP_NOTEQUAL) and
251                                         not err:is(ERR.ERR_DEP_NOVALUE)
252                                 )
253                         ) then
254                                 section:error(err)
255                         end
256                 end
257
258                 local ok, err = luci.uvl.dependencies.check( self, section )
259                 if not ok then
260                         section:error(err)
261                 end
262         else
263                 return false, ERR.SECT_NOTFOUND(section)
264         end
265
266         if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
267                 for k, v in pairs(section:config()) do
268                         local oo = section:option(k)
269                         if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
270                                 section:error(ERR.OPT_UNKNOWN(oo))
271                         end
272                 end
273         end
274
275         return section:ok(), section:errors()
276 end
277
278 function UVL._validate_option( self, option, nodeps )
279
280         self.beenthere[option:cid()] = true
281
282         if not option:scheme() and not option:parent():scheme('dynamic') then
283                 return false, option:error(ERR.OPT_UNKNOWN(option))
284
285         elseif option:scheme() then
286                 if option:scheme('required') and not option:value() then
287                         return false, option:error(ERR.OPT_REQUIRED(option))
288
289                 elseif option:value() then
290                         local val = option:value()
291
292                         if option:scheme('type') == "reference" or
293                            option:scheme('type') == "enum"
294                         then
295                                 if not option:scheme('values') or
296                                    not option:scheme('values')[val]
297                                 then
298                                         return false, option:error( ERR.OPT_BADVALUE(
299                                                 option, luci.util.serialize_data(
300                                                         luci.util.keys(option:scheme('values') or {})
301                                                 )
302                                         ) )
303                                 end
304                         elseif option:scheme('type') == "list" then
305                                 if type(val) ~= "table" and STRICT_LIST_TYPE then
306                                         return false, option:error(ERR.OPT_NOTLIST(option))
307                                 end
308                         end
309
310                         if option:scheme('datatype') then
311                                 local dt = option:scheme('datatype')
312
313                                 if self.datatypes[dt] then
314                                         val = ( type(val) == "table" and val or { val } )
315                                         for i, v in ipairs(val) do
316                                                 if not self.datatypes[dt]( v ) then
317                                                         return false, option:error(
318                                                                 ERR.OPT_INVVALUE(option, dt)
319                                                         )
320                                                 end
321                                         end
322                                 else
323                                         return false, option:error(ERR.OPT_DATATYPE(option, dt))
324                                 end
325                         end
326                 end
327
328                 if not nodeps then
329                         local ok, err = luci.uvl.dependencies.check( self, option )
330                         if not ok then
331                                 option:error(err)
332                         end
333                 end
334
335                 local ok, err = luci.uvl.validation.check( self, option )
336                 if not ok and STRICT_EXTERNAL_VALIDATORS then
337                         return false, option:error(err)
338                 end
339         end
340
341         return option:ok(), option:errors()
342 end
343
344 --- Find all parts of given scheme and construct validation tree.
345 -- This is normally done on demand, so you don't have to call this function
346 -- by yourself.
347 -- @param scheme        Name of the scheme to parse
348 function UVL.read_scheme( self, scheme )
349
350         local so = luci.uvl.scheme( self, scheme )
351
352         local schemes = { }
353         local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
354
355         if files then
356                 for i, file in ipairs( files ) do
357                         if not luci.fs.access(file) then
358                                 return so:error(ERR.SME_READ(so,file))
359                         end
360
361                         local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
362
363                         local sd, err = uci:get_all( luci.fs.basename(file) )
364
365                         if not sd then
366                                 return false, ERR.UCILOAD(so, err)
367                         end
368
369                         table.insert( schemes, sd )
370                 end
371
372                 return self:_read_scheme_parts( so, schemes )
373         else
374                 return false, so:error(ERR.SME_FIND(so, self.schemedir))
375         end
376 end
377
378 -- Process all given parts and construct validation tree
379 function UVL._read_scheme_parts( self, scheme, schemes )
380
381         -- helper function to check for required fields
382         local function _req( t, n, c, r )
383                 for i, v in ipairs(r) do
384                         if not c[v] then
385                                 local p, o = scheme:sid(), nil
386
387                                 if t == TYPE_SECTION then
388                                         o = section( scheme, nil, p, n )
389                                 elseif t == TYPE_OPTION then
390                                         o = option( scheme, nil, p, '(nil)', n )
391                                 elseif t == TYPE_ENUM then
392                                         o = enum( scheme, nil, p, '(nil)', '(nil)', n )
393                                 end
394
395                                 return false, ERR.SME_REQFLD(o,v)
396                         end
397                 end
398                 return true
399         end
400
401         -- helper function to validate references
402         local function _ref( c, t )
403                 local k, n
404                 if c == TYPE_SECTION then
405                         k = "package"
406                         n = 1
407                 elseif c == TYPE_OPTION then
408                         k = "section"
409                         n = 2
410                 elseif c == TYPE_ENUM then
411                         k = "variable"
412                         n = 3
413                 end
414
415                 local r = luci.util.split( t[k], "." )
416                 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
417
418                 if #r ~= n then
419                         return false, ERR.SME_BADREF(scheme, k)
420                 end
421
422                 return r
423         end
424
425         -- helper function to read bools
426         local function _bool( v )
427                 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
428         end
429
430
431         local ok, err
432
433         -- Step 0: get package meta information
434         for i, conf in ipairs( schemes ) do
435                 for k, v in pairs( conf ) do
436                         if v['.type'] == 'package' then
437                                 self.packages[scheme:sid()] =
438                                         self.packages[scheme:sid()] or {
439                                                 ["name"]      = scheme:sid();
440                                                 ["sections"]  = { };
441                                                 ["variables"] = { };
442                                         }
443
444                                 for k, v2 in pairs(v) do
445                                         if k == "title" or k == "description" then
446                                                 self.packages[scheme:sid()][k] = v2
447                                         end
448                                 end
449                         end
450                 end
451         end
452
453         -- Step 1: get all sections
454         for i, conf in ipairs( schemes ) do
455                 for k, v in pairs( conf ) do
456                         if v['.type'] == 'section' then
457
458                                 ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
459                                 if err then return false, scheme:error(err) end
460
461                                 local r, err = _ref( TYPE_SECTION, v )
462                                 if err then return false, scheme:error(err) end
463
464                                 self.packages[r[1]] =
465                                         self.packages[r[1]] or {
466                                                 ["name"]      = r[1];
467                                                 ["sections"]  = { };
468                                                 ["variables"] = { };
469                                         }
470
471                                 local p = self.packages[r[1]]
472                                           p.sections[v.name]  = p.sections[v.name]  or { }
473                                           p.variables[v.name] = p.variables[v.name] or { }
474
475                                 local s  = p.sections[v.name]
476                                 local so = scheme:section(v.name)
477
478                                 for k, v2 in pairs(v) do
479                                         if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
480                                                 if k == "depends" then
481                                                         s.depends = self:_read_dependency( v2, s.depends )
482                                                         if not s.depends then
483                                                                 return false, scheme:error(
484                                                                         ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
485                                                                 )
486                                                         end
487                                                 elseif k == "dynamic" or k == "unique" or
488                                                        k == "required" or k == "named"
489                                                 then
490                                                         s[k] = _bool(v2)
491                                                 else
492                                                         s[k] = v2
493                                                 end
494                                         end
495                                 end
496
497                                 s.dynamic  = s.dynamic  or false
498                                 s.unique   = s.unique   or false
499                                 s.required = s.required or false
500                                 s.named    = s.named    or false
501                         end
502                 end
503         end
504
505         -- Step 2: get all variables
506         for i, conf in ipairs( schemes ) do
507                 for k, v in pairs( conf ) do
508                         if v['.type'] == "variable" then
509
510                                 ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
511                                 if err then return false, scheme:error(err) end
512
513                                 local r, err = _ref( TYPE_OPTION, v )
514                                 if err then return false, scheme:error(err) end
515
516                                 local p = self.packages[r[1]]
517                                 if not p then
518                                         return false, scheme:error(
519                                                 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
520                                         )
521                                 end
522
523                                 local s = p.variables[r[2]]
524                                 if not s then
525                                         return false, scheme:error(
526                                                 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
527                                         )
528                                 end
529
530                                 s[v.name] = s[v.name] or { }
531
532                                 local t  = s[v.name]
533                                 local so = scheme:section(r[2])
534                                 local to = so:option(v.name)
535
536                                 for k, v2 in pairs(v) do
537                                         if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
538                                                 if k == "depends" then
539                                                         t.depends = self:_read_dependency( v2, t.depends )
540                                                         if not t.depends then
541                                                                 return false, scheme:error(so:error(
542                                                                         ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
543                                                                 ))
544                                                         end
545                                                 elseif k == "validator" then
546                                                         t.validators = self:_read_validator( v2, t.validators )
547                                                         if not t.validators then
548                                                                 return false, scheme:error(so:error(
549                                                                         ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
550                                                                 ))
551                                                         end
552                                                 elseif k == "valueof" then
553                                                         local values, err = self:_read_reference( v2 )
554                                                         if err then
555                                                                 return false, scheme:error(so:error(
556                                                                         ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
557                                                                 ))
558                                                         end
559                                                         t.type   = "reference"
560                                                         t.values = values
561                                                 elseif k == "required" then
562                                                         t[k] = _bool(v2)
563                                                 else
564                                                         t[k] = t[k] or v2
565                                                 end
566                                         end
567                                 end
568
569                                 t.type     = t.type     or "variable"
570                                 t.datatype = t.datatype or "string"
571                                 t.required = t.required or false
572                         end
573                 end
574         end
575
576         -- Step 3: get all enums
577         for i, conf in ipairs( schemes ) do
578                 for k, v in pairs( conf ) do
579                         if v['.type'] == "enum" then
580
581                                 ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
582                                 if err then return false, scheme:error(err) end
583
584                                 local r, err = _ref( TYPE_ENUM, v )
585                                 if err then return false, scheme:error(err) end
586
587                                 local p = self.packages[r[1]]
588                                 if not p then
589                                         return false, scheme:error(
590                                                 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
591                                         )
592                                 end
593
594                                 local s = p.variables[r[2]]
595                                 if not s then
596                                         return false, scheme:error(
597                                                 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
598                                         )
599                                 end
600
601                                 local t = s[r[3]]
602                                 if not t then
603                                         return false, scheme:error(
604                                                 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
605                                         )
606                                 end
607
608
609                                 local so = scheme:section(r[2])
610                                 local oo = so:option(r[3])
611                                 local eo = oo:enum(v.value)
612
613                                 if t.type ~= "enum" then
614                                         return false, scheme:error(ERR.SME_EBADTYPE(eo))
615                                 end
616
617                                 if not t.values then
618                                         t.values = { [v.value] = v.title or v.value }
619                                 else
620                                         t.values[v.value] = v.title or v.value
621                                 end
622
623                                 if not t.enum_depends then
624                                         t.enum_depends = { }
625                                 end
626
627                                 if v.default then
628                                         if t.default then
629                                                 return false, scheme:error(ERR.SME_EBADDEF(eo))
630                                         end
631                                         t.default = v.value
632                                 end
633
634                                 if v.depends then
635                                         t.enum_depends[v.value] = self:_read_dependency(
636                                                 v.depends, t.enum_depends[v.value]
637                                         )
638
639                                         if not t.enum_depends[v.value] then
640                                                 return false, scheme:error(so:error(oo:error(
641                                                         ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
642                                                 )))
643                                         end
644                                 end
645                         end
646                 end
647         end
648
649         return self
650 end
651
652 -- Read a dependency specification
653 function UVL._read_dependency( self, values, deps )
654         local expr = "%$?[a-zA-Z0-9_]+"
655         if values then
656                 values = ( type(values) == "table" and values or { values } )
657                 for _, value in ipairs(values) do
658                         local parts     = luci.util.split( value, "%s*,%s*", nil, true )
659                         local condition = { }
660                         for i, val in ipairs(parts) do
661                                 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
662
663                                 if k and (
664                                         k:match("^"..expr.."%."..expr.."%."..expr.."$") or
665                                         k:match("^"..expr.."%."..expr.."$") or
666                                         k:match("^"..expr.."$")
667                                 ) then
668                                         condition[k] = v or true
669                                 else
670                                         return nil
671                                 end
672                         end
673
674                         if not deps then
675                                 deps = { condition }
676                         else
677                                 table.insert( deps, condition )
678                         end
679                 end
680         end
681
682         return deps
683 end
684
685 -- Read a validator specification
686 function UVL._read_validator( self, values, validators )
687         if values then
688                 values = ( type(values) == "table" and values or { values } )
689                 for _, value in ipairs(values) do
690                         local validator
691
692                         if value:match("^exec:") then
693                                 validator = value:gsub("^exec:","")
694                         elseif value:match("^lua:") then
695                                 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
696                         elseif value:match("^regexp:") then
697                                 local pattern = value:gsub("^regexp:","")
698                                 validator = function( type, dtype, pack, sect, optn, ... )
699                                         local values = { ... }
700                                         for _, v in ipairs(values) do
701                                                 local ok, match =
702                                                         luci.util.copcall( string.match, v, pattern )
703
704                                                 if not ok then
705                                                         return false, match
706                                                 elseif not match then
707                                                         return false,
708                                                                 'Value "%s" does not match pattern "%s"' % {
709                                                                         v, pattern
710                                                                 }
711                                                 end
712                                         end
713                                         return true
714                                 end
715                         end
716
717                         if validator then
718                                 if not validators then
719                                         validators = { validator }
720                                 else
721                                         table.insert( validators, validator )
722                                 end
723                         else
724                                 return nil
725                         end
726                 end
727
728                 return validators
729         end
730 end
731
732 -- Read a reference specification (XXX: We should validate external configs too...)
733 function UVL._read_reference( self, values )
734         local val = { }
735         values = ( type(values) == "table" and values or { values } )
736
737         for _, value in ipairs(values) do
738                 local ref = luci.util.split(value, ".")
739
740                 if #ref == 2 or #ref == 3 then
741                         local co = luci.uvl.config( self, ref[1] )
742                         if not co:config() then return false, co:errors() end
743
744                         for k, v in pairs(co:config()) do
745                                 if v['.type'] == ref[2] then
746                                         if #ref == 2 then
747                                                 if v['.anonymous'] == true then
748                                                         return false, ERR.SME_INVREF('', value)
749                                                 end
750                                                 val[k] = k      -- XXX: title/description would be nice
751                                         elseif v[ref[3]] then
752                                                 val[v[ref[3]]] = v[ref[3]]  -- XXX: dito
753                                         end
754                                 end
755                         end
756                 else
757                         return false, ERR.SME_BADREF('', value)
758                 end
759         end
760
761         return val, nil
762 end
763
764 -- Resolve given path
765 function UVL._resolve_function( self, value )
766         local path = luci.util.split(value, ".")
767
768         for i=1, #path-1 do
769                 local stat, mod = luci.util.copcall(
770                         require, table.concat(path, ".", 1, i)
771                 )
772
773                 if stat and mod then
774                         for j=i+1, #path-1 do
775                                 if not type(mod) == "table" then
776                                         break
777                                 end
778                                 mod = mod[path[j]]
779                                 if not mod then
780                                         break
781                                 end
782                         end
783                         mod = type(mod) == "table" and mod[path[#path]] or nil
784                         if type(mod) == "function" then
785                                 return mod
786                         end
787                 end
788         end
789 end
790
791
792 --- Object representation of an uvl item - base class.
793 uvlitem = luci.util.class()
794
795 function uvlitem.cid(self)
796         return table.concat( self.cref, '.' )
797 end
798
799 function uvlitem.sid(self)
800         return table.concat( self.sref, '.' )
801 end
802
803 function uvlitem.scheme(self, opt)
804         local s
805
806         if #self.sref == 4 or #self.sref == 3 then
807                 s = self.s and self.s.packages
808                 s = s      and s[self.sref[1]]
809                 s = s      and s.variables
810                 s = s      and s[self.sref[2]]
811                 s = s      and s[self.sref[3]]
812         elseif #self.sref == 2 then
813                 s = self.s and self.s.packages
814                 s = s      and s[self.sref[1]]
815                 s = s      and s.sections
816                 s = s      and s[self.sref[2]]
817         else
818                 s = self.s and self.s.packages
819                 s = s      and s[self.sref[1]]
820         end
821
822         if s and opt then
823                 return s[opt]
824         elseif s then
825                 return s
826         end
827 end
828
829 function uvlitem.config(self, opt)
830         local c
831
832         if #self.cref == 4 or #self.cref == 3 then
833                 c = self.c and self.c[self.cref[2]] or nil
834                 c = c      and c[self.cref[3]]      or nil
835         elseif #self.cref == 2 then
836                 c = self.c and self.c[self.cref[2]] or nil
837         else
838                 c = self.c
839         end
840
841         if c and opt then
842                 return c[opt]
843         elseif c then
844                 return c
845         end
846 end
847
848 function uvlitem.title(self)
849         return self:scheme() and self:scheme('title') or
850                 self.cref[3] or self.cref[2] or self.cref[1]
851 end
852
853 function uvlitem.type(self)
854         if self.t == luci.uvl.TYPE_CONFIG then
855                 return 'config'
856         elseif self.t == luci.uvl.TYPE_SECTION then
857                 return 'section'
858         elseif self.t == luci.uvl.TYPE_OPTION then
859                 return 'option'
860         elseif self.t == luci.uvl.TYPE_ENUM then
861                 return 'enum'
862         end
863 end
864
865 function uvlitem.error(self, ...)
866         if not self.e then
867                 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
868                 self.e = errconst[#self.cref]( self )
869         end
870
871         return self.e:child( ... )
872 end
873
874 function uvlitem.errors(self)
875         return self.e
876 end
877
878 function uvlitem.ok(self)
879         return not self:errors()
880 end
881
882 function uvlitem.parent(self)
883         if self.p then
884                 return self.p
885         elseif #self.cref == 3 or #self.cref == 4 then
886                 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
887         elseif #self.cref == 2 then
888                 return luci.uvl.config( self.s, self.c, self.cref[1] )
889         else
890                 return nil
891         end
892 end
893
894 function uvlitem._loadconf(self, co, c)
895         if not co then
896                 local uci, err = luci.model.uci.cursor(), nil
897                 co, err = uci:get_all(c)
898
899                 if err then
900                         self:error(ERR.UCILOAD(self, err))
901                 end
902         end
903         return co
904 end
905
906
907 --- Object representation of a scheme.
908 -- @class       scheme
909 -- @cstyle      instance
910 -- @name        luci.uvl.scheme
911
912 --- Scheme instance constructor.
913 -- @class                       function
914 -- @name                        scheme
915 -- @param scheme        Scheme instance
916 -- @param co            Configuration data
917 -- @param c                     Configuration name
918 -- @return                      Config instance
919 scheme = luci.util.class(uvlitem)
920
921 function scheme.__init__(self, scheme, co, c)
922         if not c then
923                 c, co = co, nil
924         end
925
926         self.cref = { c }
927         self.sref = { c }
928         self.c    = self:_loadconf(co, c)
929         self.s    = scheme
930         self.t    = luci.uvl.TYPE_SCHEME
931 end
932
933 --- Add an error to scheme.
934 -- @return      Scheme error context
935 function scheme.error(self, ...)
936         if not self.e then self.e = ERR.SCHEME( self ) end
937         return self.e:child( ... )
938 end
939
940 --- Get an associated config object.
941 -- @return      Config instance
942 function scheme.config(self)
943         local co = luci.uvl.config( self.s, self.cref[1] )
944               co.p = self
945
946         return co
947 end
948
949 --- Get all section objects associated with this scheme.
950 -- @return      Table containing all associated luci.uvl.section instances
951 function scheme.sections(self)
952         local v = { }
953         if self.s.packages[self.sref[1]].sections then
954                 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
955                         table.insert( v, luci.uvl.option(
956                                 self.s, self.c, self.cref[1], self.cref[2], o
957                         ) )
958                 end
959         end
960         return v
961 end
962
963 --- Get an associated section object.
964 -- @param s     Section to select
965 -- @return      Section instance
966 function scheme.section(self, s)
967         local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
968               so.p = self
969
970         return so
971 end
972
973
974 --- Object representation of a config.
975 -- @class       config
976 -- @cstyle      instance
977 -- @name        luci.uvl.config
978
979 --- Config instance constructor.
980 -- @class                       function
981 -- @name                        config
982 -- @param scheme        Scheme instance
983 -- @param co            Configuration data
984 -- @param c                     Configuration name
985 -- @return                      Config instance
986 config = luci.util.class(uvlitem)
987
988 function config.__init__(self, scheme, co, c)
989         if not c then
990                 c, co = co, nil
991         end
992
993         self.cref = { c }
994         self.sref = { c }
995         self.c    = self:_loadconf(co, c)
996         self.s    = scheme
997         self.t    = luci.uvl.TYPE_CONFIG
998 end
999
1000 --- Get all section objects associated with this config.
1001 -- @return      Table containing all associated luci.uvl.section instances
1002 function config.sections(self)
1003         local v = { }
1004         if self.s.packages[self.sref[1]].sections then
1005                 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1006                         table.insert( v, luci.uvl.option(
1007                                 self.s, self.c, self.cref[1], self.cref[2], o
1008                         ) )
1009                 end
1010         end
1011         return v
1012 end
1013
1014 --- Get an associated section object.
1015 -- @param s     Section to select
1016 -- @return      Section instance
1017 function config.section(self, s)
1018         local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
1019               so.p = self
1020
1021         return so
1022 end
1023
1024
1025 --- Object representation of a scheme/config section.
1026 -- @class       module
1027 -- @cstyle      instance
1028 -- @name        luci.uvl.section
1029
1030 --- Section instance constructor.
1031 -- @class                       function
1032 -- @name                        section
1033 -- @param scheme        Scheme instance
1034 -- @param co            Configuration data
1035 -- @param c                     Configuration name
1036 -- @param s                     Section name
1037 -- @return                      Section instance
1038 section = luci.util.class(uvlitem)
1039
1040 function section.__init__(self, scheme, co, c, s)
1041         self.cref = { c, s }
1042         self.sref = { c, co and co[s] and co[s]['.type'] or s }
1043         self.c    = self:_loadconf(co, c)
1044         self.s    = scheme
1045         self.t    = luci.uvl.TYPE_SECTION
1046 end
1047
1048 --- Get all option objects associated with this section.
1049 -- @return      Table containing all associated luci.uvl.option instances
1050 function section.variables(self)
1051         local v = { }
1052         if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1053                 for o, _ in pairs(
1054                         self.s.packages[self.sref[1]].variables[self.sref[2]]
1055                 ) do
1056                         table.insert( v, luci.uvl.option(
1057                                 self.s, self.c, self.cref[1], self.cref[2], o
1058                         ) )
1059                 end
1060         end
1061         return v
1062 end
1063
1064 --- Get an associated option object.
1065 -- @param o     Option to select
1066 -- @return      Option instance
1067 function section.option(self, o)
1068         local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1069               oo.p = self
1070
1071         return oo
1072 end
1073
1074
1075 --- Object representation of a scheme/config option.
1076 -- @class       module
1077 -- @cstyle      instance
1078 -- @name        luci.uvl.option
1079
1080 --- Section instance constructor.
1081 -- @class                       function
1082 -- @name                        option
1083 -- @param scheme        Scheme instance
1084 -- @param co            Configuration data
1085 -- @param c                     Configuration name
1086 -- @param s                     Section name
1087 -- @param o                     Option name
1088 -- @return                      Option instance
1089 option = luci.util.class(uvlitem)
1090
1091 function option.__init__(self, scheme, co, c, s, o)
1092         self.cref = { c, s, o }
1093         self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1094         self.c    = self:_loadconf(co, c)
1095         self.s    = scheme
1096         self.t    = luci.uvl.TYPE_OPTION
1097 end
1098
1099 --- Get the value of this option.
1100 -- @return      The associated configuration value
1101 function option.value(self)
1102         return self:config()
1103 end
1104
1105 --- Get the associated section information in scheme.
1106 -- @return      Table containing the scheme properties
1107 function option.section(self)
1108         return self.s.packages[self.sref[1]].sections[self.sref[2]]
1109 end
1110
1111 --- Construct an enum object instance from given or default value.
1112 -- @param v     Value to select
1113 -- @return      Enum instance for selected value
1114 function option.enum(self, val)
1115         return enum(
1116                 self.s, self.c,
1117                 self.cref[1], self.cref[2], self.cref[3],
1118                 val or self:value()
1119         )
1120 end
1121
1122
1123 --- Object representation of a enum value.
1124 -- @class       module
1125 -- @cstyle      instance
1126 -- @name        luci.uvl.enum
1127
1128 --- Section instance constructor.
1129 -- @class                       function
1130 -- @name                        enum
1131 -- @param scheme        Scheme instance
1132 -- @param co            Configuration data
1133 -- @param c                     Configuration name
1134 -- @param s                     Section name
1135 -- @param o                     Enum name
1136 -- @param v                     Enum value
1137 -- @return                      Enum value instance
1138 enum = luci.util.class(option)
1139
1140 function enum.__init__(self, scheme, co, c, s, o, v)
1141         self.cref = { c, s, o, v }
1142         self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1143         self.c    = self:_loadconf(co, c)
1144         self.s    = scheme
1145         self.t    = luci.uvl.TYPE_ENUM
1146 end