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")
41 Session = ffluci.util.class()
43 -- Session constructor
44 function Session.__init__(self, path, uci)
47 self.ucicmd = uci .. " -P " .. path
53 -- The default Session
54 local default = Session()
55 local state = Session("/var/state")
58 function StateSession()
63 -- Wrapper for "uci add"
64 function Session.add(self, config, section_type)
65 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
69 return default:add(...)
73 -- Wrapper for "uci changes"
74 function Session.changes(self, config)
75 return self:_uci("changes " .. _path(config))
79 return default:changes(...)
83 -- Wrapper for "uci commit"
84 function Session.commit(self, config)
85 return self:_uci2("commit " .. _path(config))
89 return default:commit(...)
93 -- Wrapper for "uci del"
94 function Session.del(self, config, section, option)
95 return self:_uci2("del " .. _path(config, section, option))
99 return default:del(...)
103 -- Wrapper for "uci get"
104 function Session.get(self, config, section, option)
105 return self:_uci("get " .. _path(config, section, option))
109 return default:get(...)
113 -- Wrapper for "uci revert"
114 function Session.revert(self, config)
115 return self:_uci2("revert " .. _path(config))
119 return default:revert(...)
123 -- Wrapper for "uci show"
124 function Session.show(self, config, ...)
125 return self:_uci3("show " .. _path(config), ...)
129 return default:show(...)
133 -- Wrapper for "uci set"
134 function Session.set(self, config, section, option, value)
135 return self:_uci2("set " .. _path(config, section, option, value))
139 return default:set(...)
143 -- Internal functions --
145 function Session._uci(self, cmd)
146 local res = ffluci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
148 if res:len() == 0 then
151 return res:sub(1, res:len()-1)
155 function Session._uci2(self, cmd)
156 local res = ffluci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
158 if res:len() > 0 then
165 function Session._uci3(self, cmd, raw)
166 local res = ffluci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd)
167 if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then
172 return table.concat(res, "\n")
177 for k,line in pairs(res) do
178 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
180 tbl[c] = tbl[c] or {}
181 tbl[c][".order"] = tbl[c][".order"] or {}
184 table.insert(tbl[c][".order"], s)
185 tbl[c][s][".type"] = t
188 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
197 -- Build path (config.section.option=value) and prevent command injection
201 -- Not using ipairs because it is not reliable in case of nil arguments
203 for k,v in pairs(arg) do
207 result = "'" .. v:gsub("['.]", "") .. "'"
209 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
211 result = result .. "='" .. v:gsub("'", "") .. "'"