* luci/libs: set svn property on uvl executable
[project/luci.git] / libs / uvl / root / usr / bin / uvl
1 #!/usr/bin/lua
2 --[[
3
4 UCI Validation Layer - Command Line Utility
5 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 (c) 2008 Steven Barth <steven@midlink.org>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12         http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15
16 ]]--
17
18 require("luci.uvl")
19 require("luci.util")
20
21
22 function getopt( arg, options )
23         options = options or ""
24         local tab = {}
25         local args = {}
26         for k, v in ipairs(arg) do
27                 if v:sub(1, 2) == "--" then
28                         local x = v:find( "=", 1, true )
29                         if x then
30                                 tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 )
31                         else
32                             tab[ v:sub( 3 ) ] = true
33                         end
34                 elseif v:sub( 1, 1 ) == "-" then
35                         local y = 2
36                         local l = #v
37                         local jopt
38                         while ( y <= l ) do
39                                 jopt = v:sub( y, y )
40                                 if options:find( jopt, 1, true ) then
41                                         if y < l then
42                                                 tab[ jopt ] = v:sub( y+1 )
43                                                 y = l
44                                         else
45                                                 tab[ jopt ] = arg[ k + 1 ]
46                                                 arg[ k + 1 ] = ""
47                                         end
48                                 else
49                                         tab[ jopt ] = true
50                                 end
51                                 y = y + 1
52                         end
53             elseif #v > 0 then
54                 table.insert(args, v)
55             end
56         end
57         return tab, args
58 end
59
60 local options, arguments = getopt( arg )
61
62 if #arguments == 0 or options.help then
63         print([=[
64
65 uvl - UCI Validation Layer
66 $Id$
67 (c) 2008 Jo-Philipp Wich, Steven Barth
68
69 Usage:
70         uvl --help
71         uvl [--silent] [--schemedir=DIR]
72                 [--no-strict-sections] [--no-strict-options]
73                 [--no-strict-validators] config[.section[.option]]
74
75 Options:
76         --help
77                 Display this help message.
78
79         --silent
80                 Don't produce any output.
81
82         --schemedir=DIR
83                 Use DIR as scheme directory.
84
85         --no-strict-sections
86                 Don't treat sections found in config but not in scheme as error.
87
88         --no-strict-options
89                 Don't treat options found in config but not in scheme as error.
90
91         --no-strict-validators
92                 Don't invalidate config if an external validator fails.
93         ]=])
94         os.exit(255)
95 else
96         luci.uvl.STRICT_UNKNOWN_SECTIONS =
97                 ( options['no-strict-sections'] and false or true )
98         luci.uvl.STRICT_UNKNOWN_OPTIONS =
99                 ( options['no-strict-options'] and false or true )
100         luci.uvl.STRICT_EXTERNAL_VALIDATORS =
101                 ( options['no-strict-validators'] and false or true )
102
103         local uvl = luci.uvl.UVL(
104                 type(options.schemedir) == "string" and options.schemedir or nil
105         )
106
107         local cso = luci.util.split( arguments[1], "." )
108         local ok, err = uvl:validate( unpack(cso) )
109
110         if ok then
111                 if not options.silent then
112                         print( string.format(
113                                 '%s "%s" validates fine!',
114                                         ( #cso == 1 and "Config" or
115                                                 ( #cso == 2 and "Section" or "Option" ) ),
116                                         table.concat(cso, ".")
117                         ) )
118                 end
119                 os.exit( 0 )
120         else
121                 if not options.silent then print( err ) end
122                 os.exit( 1 )
123         end
124 end