From: Steven Barth Date: Fri, 6 Jun 2008 20:45:33 +0000 (+0000) Subject: * Removed High-Level UCI-API due to Lua compiler bugs X-Git-Tag: 0.8.0~870 X-Git-Url: http://git.openwrt.org/?p=project%2Fluci.git;a=commitdiff_plain;h=cd0fb5e44e5cb538d93024d02a14a02c00752641 * Removed High-Level UCI-API due to Lua compiler bugs --- diff --git a/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua b/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua index 216230fe15..6c3ad8c026 100644 --- a/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua +++ b/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua @@ -22,176 +22,173 @@ end function configure_freifunk() local ip = luci.http.formvalue("ip") local uci = luci.model.uci - local cfg = uci.config - + local cfg = { + wireless = uci.load("wireless"), + luci_fw = uci.load("luci_fw"), + luci_splash = uci.load("luci_splash"), + olsr = uci.load("olsr") + } -- Configure FF-Interface uci.delete("network", "ff") uci.delete("network", "ffdhcp") - cfg.network.ff = "interface" - cfg.network.ff.type = "bridge" - cfg.network.ff.proto = "static" - cfg.network.ff.ipaddr = ip - cfg.network.ff.netmask = cfg.freifunk.community.mask - cfg.network.ff.dns = cfg.freifunk.community.dns + uci.section("network", "interface", "ff", { + type = "bridge", + proto = "static", + ipaddr = ip, + netmask = uci.get("freifunk", "community", "mask"), + dns = uci.get("freifunk", "community", "dns") + }) -- Reset Routing - uci.foreach("luci_fw", "routing", + uci.delete_all("luci_fw", "routing", function (section) - if section.iface == "ff" or section.oface == "ff" then - uci.delete("luci_fw", section[".name"]) - end + return (section.iface == "ff" or section.oface == "ff") end) if cfg.luci_fw then - cfg.luci_fw[""] = "routing" - cfg.luci_fw[""].iface = "ff" - cfg.luci_fw[""].oface = "ff" - cfg.luci_fw[""].fwd = "1" + uci.section("luci_fw", "routing", nil, { + iface = "ff", + oface = "ff", + fwd = "1" + }) end -- Routing from Internal local iface = luci.http.formvalue("frominternal") if iface and iface ~= "" then - uci.foreach("luci_fw", "routing", + uci.delete_all("luci_fw", "routing", function (section) - if section.iface == iface and section.oface == "ff" then - uci.delete("luci_fw", section[".name"]) - end + return (section.iface == iface and section.oface == "ff") end) if cfg.luci_fw then - cfg.luci_fw[""] = "routing" - cfg.luci_fw[""].iface = iface - cfg.luci_fw[""].oface = "ff" - cfg.luci_fw[""].fwd = "1" - cfg.luci_fw[""].nat = "1" + uci.section("luci_fw", "routing", nil, { + iface = iface, + oface = "ff", + fwd = "1", + nat = "1" + }) end end - + -- Routing to External local iface = luci.http.formvalue("toexternal") if iface and iface ~= "" then - uci.foreach("luci_fw", "routing", + uci.delete_all("luci_fw", "routing", function (section) - if section.oface == iface and section.iface == "ff" then - uci.delete("luci_fw", section[".name"]) - end + return (section.oface == iface and section.iface == "ff") end) if cfg.luci_fw then - cfg.luci_fw[""] = "routing" - cfg.luci_fw[""].oface = iface - cfg.luci_fw[""].iface = "ff" - cfg.luci_fw[""].fwd = "1" - cfg.luci_fw[""].nat = "1" + uci.section("luci_fw", "routing", nil, { + oface = iface, + iface = "ff", + fwd = "1", + nat = "1" + }) end end -- Configure DHCP if luci.http.formvalue("dhcp") then - local dhcpnet = cfg.freifunk.community.dhcp:match("^([0-9]+)") + local dhcpnet = uci.get("freifunk", "community", "dhcp"):match("^([0-9]+)") local dhcpip = ip:gsub("^[0-9]+", dhcpnet) - cfg.network.ffdhcp = "interface" - cfg.network.ffdhcp.proto = "static" - cfg.network.ffdhcp.ifname = "br-ff:dhcp" - cfg.network.ffdhcp.ipaddr = dhcpip - cfg.network.ffdhcp.netmask = cfg.freifunk.community.dhcpmask + uci.section("network", "interface", "ffdhcp", { + proto = "static", + ifname = "br-ff:dhcp", + ipaddr = dhcpip, + netmask = uci.get("freifunk", "community", "dhcpmask") + }) - uci.foreach("dhcp", "dhcp", - function (section) - if section.interface == "ffdhcp" then - uci.delete("dhcp", section[".name"]) - end - end) + uci.delete_all("dhcp", "dhcp", + function (section) + return (section.interface == "ffdhcp") + end) local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4 - - cfg.dhcp[""] = "dhcp" - cfg.dhcp[""].interface = "ffdhcp" - cfg.dhcp[""].start = dhcpbeg - cfg.dhcp[""].limit = (dhcpbeg < 252) and 3 or 2 - cfg.dhcp[""].leasetime = "30m" - - - uci.foreach("luci_splash", "iface", + uci.section("dhcp", "dhcp", nil, { + interface = "ffdhcp", + start = dhcpbeg, + limit = ((dhcpbeg < 252) and 3 or 2), + leasetime = "30m" + }) + + + uci.delete_all("luci_splash", "iface", function (section) - if section.network == "ffdhcp" then - uci.delete("luci_splash", section[".name"]) - end + return (section.network == "ffdhcp") end) - + if cfg.luci_splash then - cfg.luci_splash[""] = "iface" - cfg.luci_splash[""].network = "ffdhcp" + uci.section("luci_splash", "iface", nil, { + network = "ffdhcp" + }) end - uci.foreach("luci_fw", "routing", + uci.delete_all("luci_fw", "routing", function (section) - if section.iface == "ffdhcp" or section.oface == "ffdhcp" then - uci.delete("luci_fw", section[".name"]) - end + return (section.iface == "ffdhcp" or section.oface == "ffdhcp") end) - if cfg.luci_fw then - cfg.luci_fw[""] = "routing" - cfg.luci_fw[""].iface = "ffdhcp" - cfg.luci_fw[""].oface = "ff" - cfg.luci_fw[""].nat = "1" + if cfg.luci_fw then + uci.section("luci_fw", "routing", nil, { + iface = "ffdhcp", + oface = "ff", + nat = "1" + }) local iface = luci.http.formvalue("toexternal") if iface and iface ~= "" then - cfg.luci_fw[""] = "routing" - cfg.luci_fw[""].iface = "ffdhcp" - cfg.luci_fw[""].oface = iface - cfg.luci_fw[""].nat = "1" + uci.section("luci_fw", "routing", nil, { + iface = "ffdhcp", + oface = iface, + nat = "1" + }) end end end - + -- Configure OLSR if luci.http.formvalue("olsr") and cfg.olsr then - uci.foreach("olsr", "Interface", - function (section) - uci.delete("olsr", section[".name"]) - end) - - uci.foreach("olsr", "LoadPlugin", - function (section) - uci.delete("olsr", section[".name"]) - end) + uci.delete_all("olsr", "Interface") + uci.delete_all("olsr", "LoadPlugin") if luci.http.formvalue("shareinet") then - cfg.olsr.dyn_gw = "LoadPlugin" - cfg.olsr.dyn_gw.Library = "olsrd_dyn_gw.so.0.4" + uci.section("olsr", "LoadPlugin", "dyn_gw", { + Library = "olsrd_dyn_gw.so.0.4" + }) end - cfg.olsr.nameservice = "LoadPlugin" - cfg.olsr.nameservice.Library = "olsrd_nameservice.so.0.3" - cfg.olsr.nameservice.name = ip:gsub("%.", "-") - cfg.olsr.nameservice.hosts_file = "/var/etc/hosts" - cfg.olsr.nameservice.suffix = ".olsr" - cfg.olsr.nameservice.latlon_infile = "/tmp/latlon.txt" + uci.section("olsr", "LoadPlugin", "nameservice", { + Library = "olsrd_nameservice.so.0.3", + name = ip:gsub("%.", "-"), + hosts_file = "/var/etc/hosts", + suffix = ".olsr", + latlon_infile = "/tmp/latlon.txt" + }) - cfg.olsr.txtinfo = "LoadPlugin" - cfg.olsr.txtinfo.Library = "olsrd_txtinfo.so.0.1" - cfg.olsr.txtinfo.Accept = "127.0.0.1" + uci.section("olsr", "LoadPlugin", "txtinfo", { + Library = "olsrd_txtinfo.so.0.1", + Accept = "127.0.0.1" + }) - cfg.olsr[""] = "Interface" - cfg.olsr[""].Interface = "ff" - cfg.olsr[""].HelloInterval = "6.0" - cfg.olsr[""].HelloValidityTime = "108.0" - cfg.olsr[""].TcInterval = "4.0" - cfg.olsr[""].TcValidityTime = "324.0" - cfg.olsr[""].MidInterval = "18.0" - cfg.olsr[""].MidValidityTime = "324.0" - cfg.olsr[""].HnaInterval = "18.0" - cfg.olsr[""].HnaValidityTime = "108.0" + uci.section("olsr", "Interface", nil, { + Interface = "ff", + HelloInterval = "6.0", + HelloValidityTime = "108.0", + TcInterval = "4.0", + TcValidityTime = "324.0", + MidInterval = "18.0", + MidValidityTime = "324.0", + HnaInterval = "18.0", + HnaValidityTime = "108.0" + }) end - + -- Configure Wifi if cfg.wireless then uci.foreach("wireless", "wifi-device", @@ -199,32 +196,34 @@ function configure_freifunk() local device = section[".name"] if luci.http.formvalue("wifi."..iface) then - uci.foreach("wireless", "wifi-iface", + uci.delete_all("wireless", "wifi-iface", function (section) - if section.device == device then - uci.delete("wireless", section[".name"]) - end + return (section.device == device) end) end - cfg.wireless[device].disabled = "0" - cfg.wireless[device].mode = "11g" - cfg.wireless[device].txantenna = "1" - cfg.wireless[device].rxantenna = "1" - cfg.wireless[device].channel = cfg.freifunk.community.channel + uci.tset("wireless", "device", { + disabled = "0", + mode = "11g", + txantenna = "1", + rxantenna = "1", + channel = uci.get("freifunk", "community", "channel") + }) - cfg.wireless[""] = "wifi-iface" - cfg.wireless[""].device = iface - cfg.wireless[""].network = "ff" - cfg.wireless[""].mode = "adhoc" - cfg.wireless[""].ssid = cfg.freifunk.community.essid - cfg.wireless[""].bssid = cfg.freifunk.community.bssid - cfg.wireless[""].txpower = 13 + uci.section("wireless", "wifi-iface", nil, { + device = iface, + network = "ff", + mode = "adhoc", + ssid = uci.get("freifunk", "community", "essid"), + bssid = uci.get("freifunk", "community", "bssid"), + txpower = 13 + }) end) end - + -- Save UCI uci.save() + luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes")) end \ No newline at end of file diff --git a/applications/luci-splash/root/usr/sbin/luci-splash b/applications/luci-splash/root/usr/sbin/luci-splash index 347bdcc65e..fc3a0c8da5 100644 --- a/applications/luci-splash/root/usr/sbin/luci-splash +++ b/applications/luci-splash/root/usr/sbin/luci-splash @@ -7,7 +7,6 @@ require("luci.model.uci") -- Init state session luci.model.uci.set_savedir(luci.model.uci.savedir_state) local uci = luci.model.uci -local cfg = uci.config function main(argv) @@ -61,9 +60,10 @@ end -- Add a lease to state and invoke add_rule function add_lease(mac) - cfg.luci_splash[""] = "lease" - cfg.luci_splash[""].mac = mac - cfg.luci_splash[""].start = os.time() + uci.section("luci_splash", "lease", nil, { + mac = mac, + start = os.time() + }) add_rule(mac) uci.save() @@ -148,9 +148,10 @@ function sync() local leases = uci.get_all("luci_splash") -- Convert leasetime to seconds - local leasetime = tonumber(cfg.luci_splash.general.leasetime) * 3600 + local leasetime = tonumber(uci.get("luci_splash", "general", "leasetime")) * 3600 -- Clean state file + uci.load("luci_splash") uci.revert("luci_splash") @@ -162,9 +163,10 @@ function sync() remove_rule(v.mac) else -- Rewrite state - cfg.luci_splash[""] = "lease" - cfg.luci_splash[""].mac = v.mac - cfg.luci_splash[""].start = v.start + uci.section("luci_splash", "lease", nil, { + mac = v.mac, + start = v.start + }) written[v.mac:lower()] = 1 end end diff --git a/libs/uci/luasrc/model/uci.lua b/libs/uci/luasrc/model/uci.lua index 59a9a2c27f..bf7ac93156 100644 --- a/libs/uci/luasrc/model/uci.lua +++ b/libs/uci/luasrc/model/uci.lua @@ -25,92 +25,52 @@ limitations under the License. ]]-- local uci = require("uci") local util = require("luci.util") -local setmetatable = setmetatable -local rawget = rawget -local rawset = rawset -local error = error -local tostring = tostring +local setmetatable, rawget, rawset = setmetatable, rawget, rawset +local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end) -local configs_mt = {} -local sections_mt = {} -local options_mt = {} - savedir_default = "/tmp/.uci" confdir_default = "/etc/config" savedir_state = "/var/state" -config = {} -setmetatable(config, configs_mt) - --- Level 1 (configs) -function configs_mt.__index(self, key) - local node = rawget(self, key) - if not node then - if not uci.load(key) then - return nil - end - node = {} - node[".name"] = key - setmetatable(node, sections_mt) - rawset(self, key, node) +function delete_all(config, type, comparator) + local del = {} + + foreach(config, type, + function (section) + if not comparator or comparator(section) then + table.insert(del, section[".name"]) + end + end) + + for i, j in ipairs(del) do + uci.delete("config", j) end - return node -end -function configs_mt.__newindex() - error("invalid operation") end - --- Level 2 (sections) -function sections_mt.__index(self, key) - local node = rawget(self, key) - if not node then - node = {} - node[".conf"] = self[".name"] - node[".name"] = key - node[".type"] = uci.get(self[".name"], key) - setmetatable(node, options_mt) - rawset(self, key, node) - end - return node -end -function sections_mt.__newindex(self, key, value) - if not value then - if uci.delete(self[".name"], key) then - rawset(self, key, nil) - else - error("unable to delete section") - end - elseif key == "" then - key = uci.add(self[".name"], tostring(value)) - if key then - rawset(self, "", self[key]) - else - error("unable to create section") - end +function section(config, type, name, values) + local stat = true + if name then + stat = set(config, name, type) else - if not uci.set(self[".name"], key, value) then - error("unable to create section") - end + name = add(config, type) + stat = name and true end + + if stat and values then + stat = tset(config, name, values) + end + + return stat and name end - --- Level 3 (options) -function options_mt.__index(self, key) - return uci.get(self[".conf"], self[".name"], key) -end -function options_mt.__newindex(self, key, value) - if not value then - if not uci.delete(self[".conf"], self[".name"], key) then - error("unable to delete option") - end - else - if not uci.set(self[".conf"], self[".name"], key, tostring(value)) then - error("unable to write option") +function tset(config, section, values) + local stat = true + for k, v in pairs(values) do + if k:sub(1, 1) ~= "." then + stat = stat and set(config, section, k, v) end end end diff --git a/themes/fledermaus/luasrc/view/themes/fledermaus/header.htm b/themes/fledermaus/luasrc/view/themes/fledermaus/header.htm index 1b7c875328..0556bef860 100644 --- a/themes/fledermaus/luasrc/view/themes/fledermaus/header.htm +++ b/themes/fledermaus/luasrc/view/themes/fledermaus/header.htm @@ -120,9 +120,11 @@ end <% if "admin" == request[1] then local ucic = 0 - for n, s in pairs(require("luci.model.uci").changes()) do - for no, o in pairs(s) do - ucic = ucic + 1; + for i, j in pairs(require("luci.model.uci").changes()) do + for k, l in pairs(j) do + for m, n in pairs(l) do + ucic = ucic + 1; + end end end %> diff --git a/themes/openwrt.org/luasrc/view/themes/openwrt.org/header.htm b/themes/openwrt.org/luasrc/view/themes/openwrt.org/header.htm index 5c354d5b49..8672b381a5 100644 --- a/themes/openwrt.org/luasrc/view/themes/openwrt.org/header.htm +++ b/themes/openwrt.org/luasrc/view/themes/openwrt.org/header.htm @@ -120,9 +120,11 @@ end <% if "admin" == request[1] then local ucic = 0 - for n, s in pairs(require("luci.model.uci").changes()) do - for no, o in pairs(s) do - ucic = ucic + 1; + for i, j in pairs(require("luci.model.uci").changes()) do + for k, l in pairs(j) do + for m, n in pairs(l) do + ucic = ucic + 1; + end end end %>