* Major CBI improvements
[project/luci.git] / src / ffluci / model / uci.lua
1 --[[
2 FFLuCI - UCI wrapper library
3
4 Description:
5 Wrapper for the /sbin/uci application, syntax of implemented functions
6 is comparable to the syntax of the uci application
7
8 Any return value of false or nil can be interpreted as an error
9
10
11 ToDo: Reimplement in Lua
12
13 FileId:
14 $Id$
15
16 License:
17 Copyright 2008 Steven Barth <steven@midlink.org>
18
19 Licensed under the Apache License, Version 2.0 (the "License");
20 you may not use this file except in compliance with the License.
21 You may obtain a copy of the License at 
22
23         http://www.apache.org/licenses/LICENSE-2.0 
24
25 Unless required by applicable law or agreed to in writing, software
26 distributed under the License is distributed on an "AS IS" BASIS,
27 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 See the License for the specific language governing permissions and
29 limitations under the License.
30
31 ]]--
32 module("ffluci.model.uci", package.seeall)
33 require("ffluci.util")
34 require("ffluci.fs")
35
36 -- The OS uci command
37 ucicmd = "uci"
38
39 -- Session class
40 Session = ffluci.util.class()
41
42 -- Session constructor
43 function Session.__init__(self, path, uci)
44         uci = uci or ucicmd
45         if path then
46                 self.ucicmd = uci .. " -P " .. path 
47         else
48                 self.ucicmd = uci
49         end
50 end
51
52 -- The default Session
53 local default = Session()
54
55 -- Wrapper for "uci add"
56 function Session.add(self, config, section_type)
57         return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
58 end
59
60 function add(...)
61         return default:add(...)
62 end
63
64
65 -- Wrapper for "uci changes"
66 function Session.changes(self, config)
67         return self:_uci("changes " .. _path(config))
68 end
69
70 function changes(...)
71         return default:changes(...)
72 end
73
74
75 -- Wrapper for "uci commit"
76 function Session.commit(self, config)
77         return self:_uci2("commit " .. _path(config))
78 end
79
80 function commit(...)
81         return default:commit(...)
82 end
83
84
85 -- Wrapper for "uci del"
86 function Session.del(self, config, section, option)
87         return self:_uci2("del " .. _path(config, section, option))
88 end
89
90 function del(...)
91         return default:del(...)
92 end
93
94
95 -- Wrapper for "uci get"
96 function Session.get(self, config, section, option)
97         return self:_uci("get " .. _path(config, section, option))
98 end
99
100 function get(...)
101         return default:get(...)
102 end
103
104
105 -- Wrapper for "uci revert"
106 function Session.revert(self, config)
107         return self:_uci2("revert " .. _path(config))
108 end
109
110 function revert(...)
111         return default:revert(...)
112 end
113
114
115 -- Wrapper for "uci show"
116 function Session.show(self, config)
117         return self:_uci3("show " .. _path(config))
118 end
119
120 function show(...)
121         return default:show(...)
122 end
123
124
125 -- Wrapper for "uci set"
126 function Session.set(self, config, section, option, value)
127         return self:_uci2("set " .. _path(config, section, option, value))
128 end
129
130 function set(...)
131         return default:set(...)
132 end
133
134
135 -- Internal functions --
136
137 function Session._uci(self, cmd)
138         local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
139         
140         if res:len() == 0 then
141                 return nil
142         else
143                 return res:sub(1, res:len()-1)
144         end     
145 end
146
147 function Session._uci2(self, cmd)
148         local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
149         
150         if res:len() > 0 then
151                 return false, res
152         else
153                 return true
154         end     
155 end
156
157 function Session._uci3(self, cmd)
158         local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd)
159         if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
160                 return nil, res[1]
161         end
162
163         table = {}
164         
165         for k,line in pairs(res) do
166                 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
167                 if c then
168                         table[c] = table[c] or {}
169                         table[c][s] = {}
170                         table[c][s][".type"] = t
171                 end
172         
173                 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
174                 if c then
175                         table[c][s][o] = v
176                 end
177         end
178         
179         return table
180 end
181
182 -- Build path (config.section.option=value) and prevent command injection
183 function _path(...)
184         local result = ""
185         
186         -- Not using ipairs because it is not reliable in case of nil arguments
187         arg.n = nil
188         for k,v in pairs(arg) do
189                 if v then
190                         v = tostring(v)
191                         if k == 1 then
192                                 result = "'" .. v:gsub("['.]", "") .. "'"
193                         elseif k < 4 then
194                                 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
195                         elseif k == 4 then
196                                 result = result .. "='" .. v:gsub("'", "") .. "'"
197                         end
198                 end
199         end
200         return result
201 end