1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#!/usr/bin/lua
--[[
UCI Validation Layer - Command Line Utility
(c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
(c) 2008 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
]]--
require("luci.uvl")
require("luci.util")
function getopt( arg, options )
options = options or ""
local tab = {}
local args = {}
for k, v in ipairs(arg) do
if v:sub(1, 2) == "--" then
local x = v:find( "=", 1, true )
if x then
tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 )
else
tab[ v:sub( 3 ) ] = true
end
elseif v:sub( 1, 1 ) == "-" then
local y = 2
local l = #v
local jopt
while ( y <= l ) do
jopt = v:sub( y, y )
if options:find( jopt, 1, true ) then
if y < l then
tab[ jopt ] = v:sub( y+1 )
y = l
else
tab[ jopt ] = arg[ k + 1 ]
arg[ k + 1 ] = ""
end
else
tab[ jopt ] = true
end
y = y + 1
end
elseif #v > 0 then
table.insert(args, v)
end
end
return tab, args
end
function genspec(conf)
require("luci.model.uci")
require("luci.uvl.datatypes")
local uci = luci.model.uci.cursor()
local ok, err = uci:load(conf)
if not ok then
print("Can not load config:", err)
os.exit(1)
else
local function _guess_datatype(v)
if type(v) == "table" then v = v[1] end
for _, type in ipairs({
"boolean", "integer", "float", "ip4addr", "ip6addr",
"macaddr", "directory", "file"
}) do
if luci.uvl.datatypes[type](v) then
return type
end
end
return "string"
end
local co = uci:get_all(conf)
local ct = { }
local ca = { }
local so = { }
local to = { }
-- count section types
for _, section in pairs(co) do
ct[section['.type']] = ( ct[section['.type']] or 0 ) + 1
ca[section['.type']] = section['.anonymous']
so[section['.type']] = so[section['.type']] or { }
to[section['.type']] = to[section['.type']] or { }
for option, value in pairs(section) do
if option:sub(1,1) ~= "." then
so[section['.type']][option] = _guess_datatype(value)
to[section['.type']][option] = ( type(value) == "table" and "list" or "variable" )
end
end
end
-- package name
print( "package %s" % conf )
-- write section schemes
for type, count in luci.util.kspairs(ct) do
print( "\nconfig section" )
print( "\toption name '%s'" % type )
print( "\toption title 'Section %s'" % type )
print( "\toption package '%s'"% conf )
print( "\toption named %s" % ( ca[type] and 'false' or 'true' ) )
print( "\toption unique %s" % ( ct[type] > 1 and 'false' or ( ca[type] and 'false' or 'true' ) ) )
print( "\toption dynamic false" )
print( "\toption required false" )
-- write option schemes
for opt, val in luci.util.kspairs(so[type]) do
print( "\nconfig variable" )
print( "\toption name '%s'" % opt )
print( "\toption title 'Option %s'" % opt )
print( "\toption section '%s.%s'" %{ conf, type } )
print( "\toption datatype '%s'" % so[type][opt] )
if to[type][opt] ~= "variable" then
print( "\toption type '%s'" % to[type][opt] )
end
end
print("")
end
end
end
local options, arguments = getopt( arg )
if #arguments ~= 2 or options.help then
print([=[
uvl - UCI Validation Layer
$Id$
(c) 2008 Jo-Philipp Wich, Steven Barth
Usage:
uvl --help
uvl [--silent] [--schemedir=DIR]
[--no-strict-sections] [--no-strict-options] [--no-strict-validators]
[--no-strict-lists] {verify|genspec} config[.section[.option]]
Options:
--help
Display this help message.
--silent
Don't produce any output.
--schemedir=DIR
Use DIR as scheme directory.
--no-strict-sections
Don't treat sections found in config but not in scheme as error.
--no-strict-options
Don't treat options found in config but not in scheme as error.
--no-strict-validators
Don't invalidate config if an external validator fails.
--no-strict-lists
Don't invalidate lists that are stored options.
Actions:
verify
Validate given configuration, section or option.
genspec
Generate a scheme skeleton from given configuration.
]=])
os.exit(255)
elseif arguments[1] == "verify" then
luci.uvl.STRICT_UNKNOWN_SECTIONS =
( not options['no-strict-sections'] and true or false )
luci.uvl.STRICT_UNKNOWN_OPTIONS =
( not options['no-strict-options'] and true or false )
luci.uvl.STRICT_EXTERNAL_VALIDATORS =
( not options['no-strict-validators'] and true or false )
luci.uvl.STRICT_LIST_TYPE =
( not options['no-strict-lists'] and true or false )
local uvl = luci.uvl.UVL(
type(options.schemedir) == "string" and options.schemedir or nil
)
local cso = luci.util.split( arguments[2], "." )
local ok, err = uvl:validate( unpack(cso) )
if ok then
if not options.silent then
print( string.format(
'%s "%s" validates fine!',
( #cso == 1 and "Config" or
( #cso == 2 and "Section" or "Option" ) ),
table.concat(cso, ".")
) )
end
os.exit( 0 )
else
if not options.silent then print( err and err:string() or "Unknown error" ) end
os.exit( 1 )
end
else
genspec( arguments[2] )
end
|