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