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
14 Copyright 2008 Steven Barth <steven@midlink.org>
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
20 http://www.apache.org/licenses/LICENSE-2.0
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
29 module("ffluci.model.uci", package.seeall)
30 require("ffluci.util")
37 Session = ffluci.util.class()
39 -- Session constructor
40 function Session.__init__(self, path, uci)
43 self.ucicmd = uci .. " -P " .. path
49 -- The default Session
50 local default = Session()
52 -- Wrapper for "uci add"
53 function Session.add(self, config, section_type)
54 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
58 return default:add(...)
62 -- Wrapper for "uci changes"
63 function Session.changes(self, config)
64 return self:_uci3("changes " .. _path(config))
68 return default:change(...)
72 -- Wrapper for "uci commit"
73 function Session.commit(self, config)
74 return self:_uci2("commit " .. _path(config))
78 return default:commit(...)
82 -- Wrapper for "uci del"
83 function Session.del(self, config, section, option)
84 return self:_uci2("del " .. _path(config, section, option))
88 return default:del(...)
92 -- Wrapper for "uci get"
93 function Session.get(self, config, section, option)
94 return self:_uci("get " .. _path(config, section, option))
98 return default:get(...)
102 -- Wrapper for "uci revert"
103 function Session.revert(self, config)
104 return self:_uci2("revert " .. _path(config))
108 return self:revert(...)
112 -- Wrapper for "uci show"
113 function Session.show(self, config)
114 return self:_uci3("show " .. _path(config))
118 return default:show(...)
122 -- Wrapper for "uci set"
123 function Session.set(self, config, section, option, value)
124 return self:_uci2("set " .. _path(config, section, option, value))
128 return default:set(...)
132 -- Internal functions --
134 function Session._uci(self, cmd)
135 local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
137 if res:len() == 0 then
140 return res:sub(1, res:len()-1)
144 function Session._uci2(self, cmd)
145 local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
147 if res:len() > 0 then
154 function Session._uci3(self, cmd)
155 local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd)
156 if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
162 for k,line in pairs(res) do
163 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
165 table[c] = table[c] or {}
167 table[c][s][".type"] = t
170 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
179 -- Build path (config.section.option=value) and prevent command injection
183 -- Not using ipairs because it is not reliable in case of nil arguments
185 for k,v in pairs(arg) do
189 result = "'" .. v:gsub("['.]", "") .. "'"
191 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
193 result = result .. "='" .. v:gsub("'", "") .. "'"