Backport: Overall CBI-UVL ineraction fixes
[project/luci.git] / contrib / package / olsrd-luci / files / lib / config / olsr.lua
1 #!/usr/bin/lua
2
3 --[[
4
5 OLSRd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
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.fs")
19 require("luci.util")
20 require("luci.model.uci")
21
22 local uci = luci.model.uci.cursor()
23 local conf = uci:get_all("olsr")
24
25 local function _value(val)
26 if val:match("^[0-9%. \t]+$") or val == "yes" or val == "no" then
27 return val
28 else
29 return string.format( '"%s"', val )
30 end
31 end
32
33 local function _section(sect,sval,parstr)
34 local rv = ""
35 local pad = ""
36
37 if sval then
38 if sval == "Interface" then
39 rv = string.format( 'Interface "%s"\n{\n', uci:get("network",conf[sect][sval],"ifname") )
40 else
41 rv = string.format( '%s "%s"\n{\n', conf[sect][".type"], conf[sect][sval] )
42 end
43 pad = "\t"
44 end
45
46 for k, v in luci.util.spairs(conf[sect]) do
47 if k:sub(1,1) ~= '.' and k ~= sval then
48 if parstr then
49 rv = rv .. string.format(
50 '%s%s "%s"\t"%s"\n',
51 pad, parstr,
52 k:gsub( "_", "-" ), -- XXX: find a better solution for this
53 v
54 )
55 else
56 rv = rv .. string.format(
57 '%s%s\t%s\n',
58 pad, k, _value(v)
59 )
60 end
61 end
62 end
63
64 if sval then
65 rv = rv .. "}\n"
66 end
67
68 return rv
69 end
70
71 local function _hna(sval)
72 local rv = string.format( "%s\n{\n", sval )
73
74 for k, v in luci.util.spairs(conf) do
75 if conf[k][".type"] == sval and conf[k].NetAddr and conf[k].Prefix then
76 rv = rv .. string.format(
77 "\t%s\t%s\n",
78 conf[k].NetAddr,
79 conf[k].Prefix
80 )
81 end
82 end
83
84 return rv .. "}\n"
85 end
86
87 local function _ipc(sval)
88 local rv = string.format( "%s\n{\n", sval )
89
90 for k, v in luci.util.spairs(conf[sval]) do
91 if k:sub(1,1) ~= "." then
92 local vals = luci.util.split(v, "%s+", nil, true)
93
94 if k == "Net" then
95 for i = 1,#vals,2 do
96 rv = rv .. string.format(
97 "\tNet\t%s\t%s\n",
98 vals[i], vals[i+1]
99 )
100 end
101 elseif k == "Host" then
102 for i, v in ipairs(vals) do
103 rv = rv .. string.format(
104 "\t%s\t%s\n",
105 k, vals[i]
106 )
107 end
108 else
109 rv = rv .. string.format(
110 "\t%s\t%s\n",
111 k, v
112 )
113 end
114 end
115 end
116
117 return rv .. "}\n"
118 end
119
120
121 -- general config section
122 print( _section("general") )
123
124 -- plugin config sections
125 for k, v in luci.util.spairs(conf) do
126 if conf[k][".type"] == "LoadPlugin" then
127 if v.Library and luci.fs.access("/usr/lib/"..v.Library) then
128 print( _section( k, "Library", "PlParam" ) )
129 end
130 end
131 end
132
133 -- interface config sections
134 for k, v in luci.util.spairs(conf) do
135 if conf[k][".type"] == "Interface" then
136 print( _section( k, "Interface" ) )
137 end
138 end
139
140 -- write Hna4, Hna6 sections
141 print( _hna("Hna4") )
142 print( _hna("Hna6") )
143
144 -- write IpcConnect section
145 print( _ipc("IpcConnect") )