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")
34 -- Wrapper for "uci add"
35 function add(config, section_type)
36 return _uci("add " .. _path(config) .. " " .. _path(section_type))
40 -- Wrapper for "uci changes"
41 function changes(config)
42 return _uci3("changes " .. _path(config))
46 -- Wrapper for "uci commit"
47 function commit(config)
48 return _uci2("commit " .. _path(config))
52 -- Wrapper for "uci get"
53 function get(config, section, option)
54 return _uci("get " .. _path(config, section, option))
58 -- Wrapper for "uci revert"
59 function revert(config)
60 return _uci2("revert " .. _path(config))
64 -- Wrapper for "uci show"
66 return _uci3("show " .. _path(config))
70 -- Wrapper for "uci set"
71 function set(config, section, option, value)
72 return _uci2("set " .. _path(config, section, option, value))
76 -- Internal functions --
79 local res = ffluci.util.exec(ucicmd .. " 2>/dev/null " .. cmd)
81 if res:len() == 0 then
84 return res:sub(1, res:len()-1)
89 local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd)
99 local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd, true)
100 if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
106 for k,line in pairs(res) do
107 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
109 table[c] = table[c] or {}
111 table[c][s][".type"] = t
114 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
123 -- Build path (config.section.option=value) and prevent command injection
127 -- Not using ipairs because it is not reliable in case of nil arguments
129 for k,v in pairs(arg) do
131 result = "'" .. v:gsub("['.]", "") .. "'"
133 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
135 result = result .. "='" .. v:gsub("'", "") .. "'"