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 get"
83 function Session.get(self, config, section, option)
84 return self:_uci("get " .. _path(config, section, option))
88 return default:get(...)
92 -- Wrapper for "uci revert"
93 function Session.revert(self, config)
94 return self:_uci2("revert " .. _path(config))
98 return self:revert(...)
102 -- Wrapper for "uci show"
103 function Session.show(self, config)
104 return self:_uci3("show " .. _path(config))
108 return default:show(...)
112 -- Wrapper for "uci set"
113 function Session.set(self, config, section, option, value)
114 return self:_uci2("set " .. _path(config, section, option, value))
118 return default:set(...)
122 -- Internal functions --
124 function Session._uci(self, cmd)
125 local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
127 if res:len() == 0 then
130 return res:sub(1, res:len()-1)
134 function Session._uci2(self, cmd)
135 local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
137 if res:len() > 0 then
144 function Session._uci3(self, cmd)
145 local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd)
146 if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
152 for k,line in pairs(res) do
153 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
155 table[c] = table[c] or {}
157 table[c][s][".type"] = t
160 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
169 -- Build path (config.section.option=value) and prevent command injection
173 -- Not using ipairs because it is not reliable in case of nil arguments
175 for k,v in pairs(arg) do
177 result = "'" .. v:gsub("['.]", "") .. "'"
179 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
181 result = result .. "='" .. v:gsub("'", "") .. "'"