2 FFLuCI - UCI wrapper library
5 Wrapper for the /sbin/uci application, syntax of implemented functions
6 is comparable to the syntax of the uci application
8 Any return value of false or nil can be interpreted as an error
11 ToDo: Reimplement in Lua
17 Copyright 2008 Steven Barth <steven@midlink.org>
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
23 http://www.apache.org/licenses/LICENSE-2.0
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.
32 module("ffluci.model.uci", package.seeall)
33 require("ffluci.util")
40 Session = ffluci.util.class()
42 -- Session constructor
43 function Session.__init__(self, path, uci)
46 self.ucicmd = uci .. " -P " .. path
52 -- The default Session
53 local default = Session()
55 -- Wrapper for "uci add"
56 function Session.add(self, config, section_type)
57 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
61 return default:add(...)
65 -- Wrapper for "uci changes"
66 function Session.changes(self, config)
67 return self:_uci("changes " .. _path(config))
71 return default:changes(...)
75 -- Wrapper for "uci commit"
76 function Session.commit(self, config)
77 return self:_uci2("commit " .. _path(config))
81 return default:commit(...)
85 -- Wrapper for "uci del"
86 function Session.del(self, config, section, option)
87 return self:_uci2("del " .. _path(config, section, option))
91 return default:del(...)
95 -- Wrapper for "uci get"
96 function Session.get(self, config, section, option)
97 return self:_uci("get " .. _path(config, section, option))
101 return default:get(...)
105 -- Wrapper for "uci revert"
106 function Session.revert(self, config)
107 return self:_uci2("revert " .. _path(config))
111 return default:revert(...)
115 -- Wrapper for "uci show"
116 function Session.show(self, config)
117 return self:_uci3("show " .. _path(config))
121 return default:show(...)
125 -- Wrapper for "uci set"
126 function Session.set(self, config, section, option, value)
127 return self:_uci2("set " .. _path(config, section, option, value))
131 return default:set(...)
135 -- Internal functions --
137 function Session._uci(self, cmd)
138 local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
140 if res:len() == 0 then
143 return res:sub(1, res:len()-1)
147 function Session._uci2(self, cmd)
148 local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
150 if res:len() > 0 then
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
165 for k,line in pairs(res) do
166 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
168 table[c] = table[c] or {}
170 table[c][s][".type"] = t
173 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
182 -- Build path (config.section.option=value) and prevent command injection
186 -- Not using ipairs because it is not reliable in case of nil arguments
188 for k,v in pairs(arg) do
192 result = "'" .. v:gsub("['.]", "") .. "'"
194 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
196 result = result .. "='" .. v:gsub("'", "") .. "'"