* Rewrote ffluci.http, ffluci.model.uci
[project/luci.git] / core / src / ffluci / model / uci.lua
index 75a898acbd73a6c90665dd74ffe5b48f5db99556..511c97433db97d976c1d10a061b3658503ac6404 100644 (file)
@@ -1,14 +1,8 @@
 --[[
-FFLuCI - UCI wrapper library
+FFLuCI - UCI mpdel
 
 Description:
-Wrapper for the /sbin/uci application, syntax of implemented functions
-is comparable to the syntax of the uci application
-
-Any return value of false or nil can be interpreted as an error
-
-
-ToDo: Reimplement in Lua
+Generalized UCI model
 
 FileId:
 $Id$
@@ -30,24 +24,12 @@ limitations under the License.
 
 ]]--
 module("ffluci.model.uci", package.seeall)
-require("ffluci.util")
-require("ffluci.fs")
-require("ffluci.sys")
-
--- The OS uci command
-ucicmd = "uci"
-
--- Session class
-Session = ffluci.util.class()
-
--- Session constructor
-function Session.__init__(self, path, uci)
-       uci = uci or ucicmd
-       if path then
-               self.ucicmd = uci .. " -P " .. path 
-       else
-               self.ucicmd = uci
-       end
+
+-- Test whether to load libuci-Wrapper or /sbin/uci-Wrapper
+if pcall(require, "uci") then
+       Session = require("ffluci.model.uci.libuci").Session
+else
+       Session = require("ffluci.model.uci.wrapper").Session
 end
 
 -- The default Session
@@ -61,156 +43,48 @@ end
 
 
 -- Wrapper for "uci add"
-function Session.add(self, config, section_type)
-       return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
-end
-
 function add(...)
        return default:add(...)
 end
 
 
 -- Wrapper for "uci changes"
-function Session.changes(self, config)
-       return self:_uci("changes " .. _path(config))
-end
-
 function changes(...)
        return default:changes(...)
 end
 
 
 -- Wrapper for "uci commit"
-function Session.commit(self, config)
-       return self:_uci2("commit " .. _path(config))
-end
-
 function commit(...)
        return default:commit(...)
 end
 
 
 -- Wrapper for "uci del"
-function Session.del(self, config, section, option)
-       return self:_uci2("del " .. _path(config, section, option))
-end
-
 function del(...)
        return default:del(...)
 end
 
 
 -- Wrapper for "uci get"
-function Session.get(self, config, section, option)
-       return self:_uci("get " .. _path(config, section, option))
-end
-
 function get(...)
        return default:get(...)
 end
 
 
 -- Wrapper for "uci revert"
-function Session.revert(self, config)
-       return self:_uci2("revert " .. _path(config))
-end
-
 function revert(...)
        return default:revert(...)
 end
 
 
 -- Wrapper for "uci show"
-function Session.show(self, config, ...)
-       return self:_uci3("show " .. _path(config), ...)
-end
-
-function show(...)
-       return default:show(...)
+function sections(...)
+       return default:sections(...)
 end
 
 
 -- Wrapper for "uci set"
-function Session.set(self, config, section, option, value)
-       return self:_uci2("set " .. _path(config, section, option, value))
-end
-
 function set(...)
        return default:set(...)
-end
-
-
--- Internal functions --
-
-function Session._uci(self, cmd)
-       local res = ffluci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
-       
-       if res:len() == 0 then
-               return nil
-       else
-               return res:sub(1, res:len()-1)
-       end     
-end
-
-function Session._uci2(self, cmd)
-       local res = ffluci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
-       
-       if res:len() > 0 then
-               return false, res
-       else
-               return true
-       end     
-end
-
-function Session._uci3(self, cmd, raw)
-       local res = ffluci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd)
-       if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then
-               return nil, res[1]
-       end
-       
-       if raw then
-               return table.concat(res, "\n")
-       end
-
-       tbl = {}
-
-       for k,line in pairs(res) do
-               c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
-               if c then
-                       tbl[c] = tbl[c] or {}
-                       tbl[c][".order"] = tbl[c][".order"] or {}
-                       
-                       tbl[c][s] = {}
-                       table.insert(tbl[c][".order"], s)
-                       tbl[c][s][".type"] = t
-               end
-       
-               c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
-               if c then
-                       tbl[c][s][o] = v
-               end
-       end
-       
-       return tbl
-end
-
--- Build path (config.section.option=value) and prevent command injection
-function _path(...)
-       local result = ""
-       
-       -- Not using ipairs because it is not reliable in case of nil arguments
-       arg.n = nil
-       for k,v in pairs(arg) do
-               if v then
-                       v = tostring(v)
-                       if k == 1 then
-                               result = "'" .. v:gsub("['.]", "") .. "'"
-                       elseif k < 4 then
-                               result = result .. ".'" .. v:gsub("['.]", "") .. "'"
-                       elseif k == 4 then
-                               result = result .. "='" .. v:gsub("'", "") .. "'"
-                       end
-               end
-       end
-       return result
 end
\ No newline at end of file