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