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()
56 -- Wrapper for "uci add"
57 function Session.add(self, config, section_type)
58 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
62 return default:add(...)
66 -- Wrapper for "uci changes"
67 function Session.changes(self, config)
68 return self:_uci("changes " .. _path(config))
72 return default:changes(...)
76 -- Wrapper for "uci commit"
77 function Session.commit(self, config)
78 return self:_uci2("commit " .. _path(config))
82 return default:commit(...)
86 -- Wrapper for "uci del"
87 function Session.del(self, config, section, option)
88 return self:_uci2("del " .. _path(config, section, option))
92 return default:del(...)
96 -- Wrapper for "uci get"
97 function Session.get(self, config, section, option)
98 return self:_uci("get " .. _path(config, section, option))
102 return default:get(...)
106 -- Wrapper for "uci revert"
107 function Session.revert(self, config)
108 return self:_uci2("revert " .. _path(config))
112 return default:revert(...)
116 -- Wrapper for "uci show"
117 function Session.show(self, config)
118 return self:_uci3("show " .. _path(config))
122 return default:show(...)
126 -- Wrapper for "uci set"
127 function Session.set(self, config, section, option, value)
128 return self:_uci2("set " .. _path(config, section, option, value))
132 return default:set(...)
136 -- Internal functions --
138 function Session._uci(self, cmd)
139 local res = ffluci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
141 if res:len() == 0 then
144 return res:sub(1, res:len()-1)
148 function Session._uci2(self, cmd)
149 local res = ffluci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
151 if res:len() > 0 then
158 function Session._uci3(self, cmd)
159 local res = ffluci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd)
160 if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then
166 for k,line in pairs(res) do
167 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
169 table[c] = table[c] or {}
171 table[c][s][".type"] = t
174 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
183 -- Build path (config.section.option=value) and prevent command injection
187 -- Not using ipairs because it is not reliable in case of nil arguments
189 for k,v in pairs(arg) do
193 result = "'" .. v:gsub("['.]", "") .. "'"
195 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
197 result = result .. "='" .. v:gsub("'", "") .. "'"