diff options
author | Steven Barth <steven@midlink.org> | 2008-05-27 12:23:39 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-05-27 12:23:39 +0000 |
commit | d35a620e9f5665a94967f4bd02c93581a1dd7e00 (patch) | |
tree | cbe7a9b9fe71e40d850c649361c87608d56ea48b /libs/core/luasrc | |
parent | 5df565faba5eb0c26a7ce0e2d6e9092bf71cb1e8 (diff) |
commit 4f6198094cf4134179d1f9c9fa8f79759a27c87e
Author: Felix Fietkau <nbd@openwrt.org>
Date: Tue May 27 13:56:12 2008 +0200
rename src/ to luasrc/
Diffstat (limited to 'libs/core/luasrc')
-rw-r--r-- | libs/core/luasrc/bits.lua | 542 | ||||
-rw-r--r-- | libs/core/luasrc/debug.lua | 2 | ||||
-rw-r--r-- | libs/core/luasrc/fs.lua | 129 | ||||
-rw-r--r-- | libs/core/luasrc/init.lua | 29 | ||||
-rw-r--r-- | libs/core/luasrc/model/ipkg.lua | 140 | ||||
-rw-r--r-- | libs/core/luasrc/model/uci.lua | 92 | ||||
-rw-r--r-- | libs/core/luasrc/model/uci/libuci.lua | 193 | ||||
-rw-r--r-- | libs/core/luasrc/model/uci/wrapper.lua | 171 | ||||
-rw-r--r-- | libs/core/luasrc/sys.lua | 371 | ||||
-rw-r--r-- | libs/core/luasrc/sys/iptparser.lua | 245 | ||||
-rw-r--r-- | libs/core/luasrc/util.lua | 215 |
11 files changed, 2129 insertions, 0 deletions
diff --git a/libs/core/luasrc/bits.lua b/libs/core/luasrc/bits.lua new file mode 100644 index 0000000000..13b4c3066a --- /dev/null +++ b/libs/core/luasrc/bits.lua @@ -0,0 +1,542 @@ +--[[ +/* + * Copyright (c) 2007 Tim Kelly/Dialectronics + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT + * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +--]] + +--[[ +/* + * Copyright (c) 2007 Tim Kelly/Dialectronics + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT + * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +--]] + +module("luci.bits", package.seeall); + +local hex2bin = { + ["0"] = "0000", + ["1"] = "0001", + ["2"] = "0010", + ["3"] = "0011", + ["4"] = "0100", + ["5"] = "0101", + ["6"] = "0110", + ["7"] = "0111", + ["8"] = "1000", + ["9"] = "1001", + ["a"] = "1010", + ["b"] = "1011", + ["c"] = "1100", + ["d"] = "1101", + ["e"] = "1110", + ["f"] = "1111" + } + + + +local bin2hex = { + ["0000"] = "0", + ["0001"] = "1", + ["0010"] = "2", + ["0011"] = "3", + ["0100"] = "4", + ["0101"] = "5", + ["0110"] = "6", + ["0111"] = "7", + ["1000"] = "8", + ["1001"] = "9", + ["1010"] = "A", + ["1011"] = "B", + ["1100"] = "C", + ["1101"] = "D", + ["1110"] = "E", + ["1111"] = "F" + } + +--[[ +local dec2hex = { + ["0"] = "0", + ["1"] = "1", + ["2"] = "2", + ["3"] = "3", + ["4"] = "4", + ["5"] = "5", + ["6"] = "6", + ["7"] = "7", + ["8"] = "8", + ["9"] = "9", + ["10"] = "A", + ["11"] = "B", + ["12"] = "C", + ["13"] = "D", + ["14"] = "E", + ["15"] = "F" + } +--]] + + +-- These functions are big-endian and take up to 32 bits + +-- Hex2Bin +-- Bin2Hex +-- Hex2Dec +-- Dec2Hex +-- Bin2Dec +-- Dec2Bin + + +function Hex2Bin(s) + +-- s -> hexadecimal string + +local ret = "" +local i = 0 + + + for i in string.gfind(s, ".") do + i = string.lower(i) + + ret = ret..hex2bin[i] + + end + + return ret +end + + +function Bin2Hex(s) + +-- s -> binary string + +local l = 0 +local h = "" +local b = "" +local rem + +l = string.len(s) +rem = l % 4 +l = l-1 +h = "" + + -- need to prepend zeros to eliminate mod 4 + if (rem > 0) then + s = string.rep("0", 4 - rem)..s + end + + for i = 1, l, 4 do + b = string.sub(s, i, i+3) + h = h..bin2hex[b] + end + + return h + +end + + +function Bin2Dec(s) + +-- s -> binary string + +local num = 0 +local ex = string.len(s) - 1 +local l = 0 + + l = ex + 1 + for i = 1, l do + b = string.sub(s, i, i) + if b == "1" then + num = num + 2^ex + end + ex = ex - 1 + end + + return string.format("%u", num) + +end + + + +function Dec2Bin(s, num) + +-- s -> Base10 string +-- num -> string length to extend to + +local n + + if (num == nil) then + n = 0 + else + n = num + end + + s = string.format("%x", s) + + s = Hex2Bin(s) + + while string.len(s) < n do + s = "0"..s + end + + return s + +end + + + + +function Hex2Dec(s) + +-- s -> hexadecimal string + +local s = Hex2Bin(s) + + return Bin2Dec(s) + +end + + + +function Dec2Hex(s) + +-- s -> Base10 string + + s = string.format("%x", s) + + return s + +end + + + + +-- These functions are big-endian and will extend to 32 bits + +-- BMAnd +-- BMNAnd +-- BMOr +-- BMXOr +-- BMNot + + +function BMAnd(v, m) + +-- v -> hex string to be masked +-- m -> hex string mask + +-- s -> hex string as masked + +-- bv -> binary string of v +-- bm -> binary string mask + +local bv = Hex2Bin(v) +local bm = Hex2Bin(m) + +local i = 0 +local s = "" + + while (string.len(bv) < 32) do + bv = "0000"..bv + end + + while (string.len(bm) < 32) do + bm = "0000"..bm + end + + + for i = 1, 32 do + cv = string.sub(bv, i, i) + cm = string.sub(bm, i, i) + if cv == cm then + if cv == "1" then + s = s.."1" + else + s = s.."0" + end + else + s = s.."0" + + end + end + + return Bin2Hex(s) + +end + + +function BMNAnd(v, m) + +-- v -> hex string to be masked +-- m -> hex string mask + +-- s -> hex string as masked + +-- bv -> binary string of v +-- bm -> binary string mask + +local bv = Hex2Bin(v) +local bm = Hex2Bin(m) + +local i = 0 +local s = "" + + while (string.len(bv) < 32) do + bv = "0000"..bv + end + + while (string.len(bm) < 32) do + bm = "0000"..bm + end + + + for i = 1, 32 do + cv = string.sub(bv, i, i) + cm = string.sub(bm, i, i) + if cv == cm then + if cv == "1" then + s = s.."0" + else + s = s.."1" + end + else + s = s.."1" + + end + end + + return Bin2Hex(s) + +end + + + +function BMOr(v, m) + +-- v -> hex string to be masked +-- m -> hex string mask + +-- s -> hex string as masked + +-- bv -> binary string of v +-- bm -> binary string mask + +local bv = Hex2Bin(v) +local bm = Hex2Bin(m) + +local i = 0 +local s = "" + + while (string.len(bv) < 32) do + bv = "0000"..bv + end + + while (string.len(bm) < 32) do + bm = "0000"..bm + end + + + for i = 1, 32 do + cv = string.sub(bv, i, i) + cm = string.sub(bm, i, i) + if cv == "1" then + s = s.."1" + elseif cm == "1" then + s = s.."1" + else + s = s.."0" + end + end + + return Bin2Hex(s) + +end + +function BMXOr(v, m) + +-- v -> hex string to be masked +-- m -> hex string mask + +-- s -> hex string as masked + +-- bv -> binary string of v +-- bm -> binary string mask + +local bv = Hex2Bin(v) +local bm = Hex2Bin(m) + +local i = 0 +local s = "" + + while (string.len(bv) < 32) do + bv = "0000"..bv + end + + while (string.len(bm) < 32) do + bm = "0000"..bm + end + + + for i = 1, 32 do + cv = string.sub(bv, i, i) + cm = string.sub(bm, i, i) + if cv == "1" then + if cm == "0" then + s = s.."1" + else + s = s.."0" + end + elseif cm == "1" then + if cv == "0" then + s = s.."1" + else + s = s.."0" + end + else + -- cv and cm == "0" + s = s.."0" + end + end + + return Bin2Hex(s) + +end + + +function BMNot(v, m) + +-- v -> hex string to be masked +-- m -> hex string mask + +-- s -> hex string as masked + +-- bv -> binary string of v +-- bm -> binary string mask + +local bv = Hex2Bin(v) +local bm = Hex2Bin(m) + +local i = 0 +local s = "" + + while (string.len(bv) < 32) do + bv = "0000"..bv + end + + while (string.len(bm) < 32) do + bm = "0000"..bm + end + + + for i = 1, 32 do + cv = string.sub(bv, i, i) + cm = string.sub(bm, i, i) + if cm == "1" then + if cv == "1" then + -- turn off + s = s.."0" + else + -- turn on + s = s.."1" + end + else + -- leave untouched + s = s..cv + + end + end + + return Bin2Hex(s) + +end + + +-- these functions shift right and left, adding zeros to lost or gained bits +-- returned values are 32 bits long + +-- BShRight(v, nb) +-- BShLeft(v, nb) + + +function BShRight(v, nb) + +-- v -> hexstring value to be shifted +-- nb -> number of bits to shift to the right + +-- s -> binary string of v + +local s = Hex2Bin(v) + + while (string.len(s) < 32) do + s = "0000"..s + end + + s = string.sub(s, 1, 32 - nb) + + while (string.len(s) < 32) do + s = "0"..s + end + + return Bin2Hex(s) + +end + +function BShLeft(v, nb) + +-- v -> hexstring value to be shifted +-- nb -> number of bits to shift to the right + +-- s -> binary string of v + +local s = Hex2Bin(v) + + while (string.len(s) < 32) do + s = "0000"..s + end + + s = string.sub(s, nb + 1, 32) + + while (string.len(s) < 32) do + s = s.."0" + end + + return Bin2Hex(s) + +end
\ No newline at end of file diff --git a/libs/core/luasrc/debug.lua b/libs/core/luasrc/debug.lua new file mode 100644 index 0000000000..a56400f343 --- /dev/null +++ b/libs/core/luasrc/debug.lua @@ -0,0 +1,2 @@ +module("luci.debug", package.seeall) +__file__ = debug.getinfo(1, 'S').source:sub(2)
\ No newline at end of file diff --git a/libs/core/luasrc/fs.lua b/libs/core/luasrc/fs.lua new file mode 100644 index 0000000000..5c1f2a051b --- /dev/null +++ b/libs/core/luasrc/fs.lua @@ -0,0 +1,129 @@ +--[[ +LuCI - Filesystem tools + +Description: +A module offering often needed filesystem manipulation functions + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- + +module("luci.fs", package.seeall) + +require("posix") + +-- Glob +glob = posix.glob + +-- Checks whether a file exists +function isfile(filename) + local fp = io.open(filename, "r") + if fp then fp:close() end + return fp ~= nil +end + +-- Returns the content of file +function readfile(filename) + local fp, err = io.open(filename) + + if fp == nil then + return nil, err + end + + local data = fp:read("*a") + fp:close() + return data +end + +-- Writes given data to a file +function writefile(filename, data) + local fp, err = io.open(filename, "w") + + if fp == nil then + return nil, err + end + + fp:write(data) + fp:close() + + return true +end + +-- Returns the file modification date/time of "path" +function mtime(path) + return posix.stat(path, "mtime") +end + +-- basename wrapper +basename = posix.basename + +-- dirname wrapper +dirname = posix.dirname + +-- dir wrapper +dir = posix.dir + +-- wrapper for posix.mkdir +function mkdir(path, recursive) + if recursive then + local base = "." + + if path:sub(1,1) == "/" then + base = "" + path = path:gsub("^/+","") + end + + for elem in path:gmatch("([^/]+)/*") do + base = base .. "/" .. elem + + local stat = posix.stat( base ) + + if not stat then + local stat, errmsg, errno = posix.mkdir( base ) + + if type(stat) ~= "number" or stat ~= 0 then + return stat, errmsg, errno + end + else + if stat.type ~= "directory" then + return nil, base .. ": File exists", 17 + end + end + end + + return 0 + else + return posix.mkdir( path ) + end +end + +-- Alias for posix.rmdir +rmdir = posix.rmdir + +-- Alias for posix.stat +stat = posix.stat + +-- Alias for posix.chmod +chmod = posix.chmod + +-- Alias for posix.link +link = posix.link + +-- Alias for posix.unlink +unlink = posix.unlink diff --git a/libs/core/luasrc/init.lua b/libs/core/luasrc/init.lua new file mode 100644 index 0000000000..ce52d0aad6 --- /dev/null +++ b/libs/core/luasrc/init.lua @@ -0,0 +1,29 @@ +--[[ +LuCI - Lua Configuration Interface + +Description: +Main class + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- +module("luci", package.seeall) + +__version__ = "0.5" +__appname__ = "LuCI" diff --git a/libs/core/luasrc/model/ipkg.lua b/libs/core/luasrc/model/ipkg.lua new file mode 100644 index 0000000000..e95a2620a0 --- /dev/null +++ b/libs/core/luasrc/model/ipkg.lua @@ -0,0 +1,140 @@ +--[[ +LuCI - IPKG wrapper library + +Description: +Wrapper for the ipkg Package manager + +Any return value of false or nil can be interpreted as an error + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- +module("luci.model.ipkg", package.seeall) +require("luci.sys") +require("luci.util") + +ipkg = "ipkg" + +-- Returns repository information +function info(pkg) + return _lookup("info", pkg) +end + +-- Returns a table with status information +function status(pkg) + return _lookup("status", pkg) +end + +-- Installs packages +function install(...) + return _action("install", ...) +end + +-- Returns whether a package is installed +function installed(pkg, ...) + local p = status(...)[pkg] + return (p and p.Status and p.Status.installed) +end + +-- Removes packages +function remove(...) + return _action("remove", ...) +end + +-- Updates package lists +function update() + return _action("update") +end + +-- Upgrades installed packages +function upgrade() + return _action("upgrade") +end + + +-- Internal action function +function _action(cmd, ...) + local pkg = "" + arg.n = nil + for k, v in pairs(arg) do + pkg = pkg .. " '" .. v:gsub("'", "") .. "'" + end + + local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1" + local r = os.execute(c) + return (r == 0), r +end + +-- Internal lookup function +function _lookup(act, pkg) + local cmd = ipkg .. " " .. act + if pkg then + cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'" + end + + return _parselist(luci.sys.exec(cmd .. " 2>/dev/null")) +end + +-- Internal parser function +function _parselist(rawdata) + if type(rawdata) ~= "string" then + error("IPKG: Invalid rawdata given") + end + + rawdata = luci.util.split(rawdata) + local data = {} + local c = {} + local l = nil + + for k, line in pairs(rawdata) do + if line:sub(1, 1) ~= " " then + local split = luci.util.split(line, ":", 1) + local key = nil + local val = nil + + if split[1] then + key = luci.util.trim(split[1]) + end + + if split[2] then + val = luci.util.trim(split[2]) + end + + if key and val then + if key == "Package" then + c = {Package = val} + data[val] = c + elseif key == "Status" then + c.Status = {} + for i, j in pairs(luci.util.split(val, " ")) do + c.Status[j] = true + end + else + c[key] = val + end + l = key + end + else + -- Multi-line field + c[l] = c[l] .. "\n" .. line:sub(2) + end + end + + return data +end
\ No newline at end of file diff --git a/libs/core/luasrc/model/uci.lua b/libs/core/luasrc/model/uci.lua new file mode 100644 index 0000000000..39354bed19 --- /dev/null +++ b/libs/core/luasrc/model/uci.lua @@ -0,0 +1,92 @@ +--[[ +LuCI - UCI mpdel + +Description: +Generalized UCI model + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- +module("luci.model.uci", package.seeall) + +-- Default savedir +savedir = "/tmp/.uci" + +-- Test whether to load libuci-Wrapper or /sbin/uci-Wrapper +if pcall(require, "uci") then + Session = require("luci.model.uci.libuci").Session +else + Session = require("luci.model.uci.wrapper").Session +end + +-- The default Session +local default = Session() +local state = Session("/var/state") + +-- The state Session +function StateSession() + return state +end + + +-- Wrapper for "uci add" +function add(...) + return default:add(...) +end + + +-- Wrapper for "uci changes" +function changes(...) + return default:changes(...) +end + +-- Wrapper for "uci commit" +function commit(...) + return default:commit(...) +end + + +-- Wrapper for "uci del" +function del(...) + return default:del(...) +end + + +-- Wrapper for "uci get" +function get(...) + return default:get(...) +end + + +-- Wrapper for "uci revert" +function revert(...) + return default:revert(...) +end + + +-- Wrapper for "uci show" +function sections(...) + return default:sections(...) +end + + +-- Wrapper for "uci set" +function set(...) + return default:set(...) +end
\ No newline at end of file diff --git a/libs/core/luasrc/model/uci/libuci.lua b/libs/core/luasrc/model/uci/libuci.lua new file mode 100644 index 0000000000..9a1112500e --- /dev/null +++ b/libs/core/luasrc/model/uci/libuci.lua @@ -0,0 +1,193 @@ +--[[ +LuCI - UCI libuci wrapper + +Description: +Wrapper for the libuci Lua bindings + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- + +module("luci.model.uci.libuci", package.seeall) + +require("uci") +require("luci.util") +require("luci.sys") + +-- Session class +Session = luci.util.class() + +-- Session constructor +function Session.__init__(self, savedir) + self.ucicmd = savedir and "uci -P " .. savedir or "uci" + self.savedir = savedir or luci.model.uci.savedir +end + +function Session.add(self, config, section_type) + return self:_uci("add " .. _path(config) .. " " .. _path(section_type)) +end + +function Session.changes(self, config) + return self:_uci("changes " .. _path(config)) +end + +function Session.commit(self, config) + self:t_load(config) + return self:t_commit(config) +end + +function Session.del(self, config, section, option) + return self:_uci2("del " .. _path(config, section, option)) +end + +function Session.get(self, config, section, option) + self:t_load(config) + return self:t_get(config, section, option) +end + +function Session.revert(self, config) + self:t_load(config) + return self:t_revert(config) +end + +function Session.sections(self, config) + self:t_load(config) + return self:t_sections(config) +end + +function Session.set(self, config, section, option, value) + self:t_load(config) + return self:t_set(config, section, option, value) and self:t_save(config) +end + +function Session.synchronize(self) + return uci.set_savedir(self.savedir) +end + + +-- UCI-Transactions + +function Session.t_load(self, config) + return self:synchronize() and uci.load(config) +end + +function Session.t_save(self, config) + return uci.save(config) +end + +function Session.t_add(self, config, type) + self:t_save(config) + local r = self:add(config, type) + self:t_load(config) + return r +end + +function Session.t_commit(self, config) + return uci.commit(config) +end + +function Session.t_del(self, config, section, option) + self:t_save(config) + local r = self:del(config, section, option) + self:t_load(config) + return r +end + +function Session.t_get(self, config, section, option) + if option then + return uci.get(config, section, option) + else + return uci.get(config, section) + end +end + +function Session.t_revert(self, config) + return uci.revert(config) +end + +function Session.t_sections(self, config) + local raw = uci.get_all(config) + if not raw then + return nil + end + + local s = {} + local o = {} + + for i, sec in ipairs(raw) do + table.insert(o, sec.name) + + s[sec.name] = sec.options + s[sec.name][".type"] = sec.type + end + + return s, o +end + +function Session.t_set(self, config, section, option, value) + if option then + return uci.set(config.."."..section.."."..option.."="..value) + else + return uci.set(config.."."..section.."="..value) + end +end + +-- Internal functions -- + + +function Session._uci(self, cmd) + local res = luci.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 = luci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd) + + if res:len() > 0 then + return false, res + else + return true + end +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 diff --git a/libs/core/luasrc/model/uci/wrapper.lua b/libs/core/luasrc/model/uci/wrapper.lua new file mode 100644 index 0000000000..e063b272c8 --- /dev/null +++ b/libs/core/luasrc/model/uci/wrapper.lua @@ -0,0 +1,171 @@ +--[[ +LuCI - UCI wrapper library + +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 + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- + +module("luci.model.uci.wrapper", package.seeall) + +require("luci.util") +require("luci.sys") + +-- Session class +Session = luci.util.class() + +-- Session constructor +function Session.__init__(self, savedir) + self.ucicmd = savedir and "uci -P " .. savedir or "uci" +end + +function Session.add(self, config, section_type) + return self:_uci("add " .. _path(config) .. " " .. _path(section_type)) +end + +function Session.changes(self, config) + return self:_uci("changes " .. _path(config)) +end + +function Session.commit(self, config) + return self:_uci2("commit " .. _path(config)) +end + +function Session.del(self, config, section, option) + return self:_uci2("del " .. _path(config, section, option)) +end + +function Session.get(self, config, section, option) + return self:_uci("get " .. _path(config, section, option)) +end + +function Session.revert(self, config) + return self:_uci2("revert " .. _path(config)) +end + +function Session.sections(self, config) + if not config then + return nil + end + + local r1, r2 = self:_uci3("show " .. _path(config)) + if type(r1) == "table" then + return r1, r2 + else + return nil, r2 + end +end + +function Session.set(self, config, section, option, value) + return self:_uci2("set " .. _path(config, section, option, value)) +end + +function Session.synchronize(self) end + +-- Dummy transaction functions + +function Session.t_load(self) end +function Session.t_save(self) end + +Session.t_add = Session.add +Session.t_commit = Session.commit +Session.t_del = Session.del +Session.t_get = Session.get +Session.t_revert = Session.revert +Session.t_sections = Session.sections +Session.t_set = Session.set + + + + + +-- Internal functions -- + + +function Session._uci(self, cmd) + local res = luci.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 = luci.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) + local res = luci.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 + + local tbl = {} + local ord = {} + + for k,line in pairs(res) do + c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$") + if c then + tbl[s] = {} + table.insert(ord, s) + tbl[s][".type"] = t + end + + c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$") + if c then + tbl[s][o] = v + end + end + + return tbl, ord +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 diff --git a/libs/core/luasrc/sys.lua b/libs/core/luasrc/sys.lua new file mode 100644 index 0000000000..0399d0e5f0 --- /dev/null +++ b/libs/core/luasrc/sys.lua @@ -0,0 +1,371 @@ +--[[ +LuCI - System library + +Description: +Utilities for interaction with the Linux system + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- + +module("luci.sys", package.seeall) +require("posix") +require("luci.bits") +require("luci.util") +require("luci.fs") + +-- Returns whether a system is bigendian +function bigendian() + local fp = io.open("/bin/sh") + fp:seek("set", 5) + local be = (fp:read(1):byte() ~= 1) + fp:close() + return be +end + +-- Runs "command" and returns its output +function exec(command) + local pp = io.popen(command) + local data = pp:read("*a") + pp:close() + + return data +end + +-- Runs "command" and returns its output as a array of lines +function execl(command) + local pp = io.popen(command) + local line = "" + local data = {} + + while true do + line = pp:read() + if (line == nil) then break end + table.insert(data, line) + end + pp:close() + + return data +end + +-- Uses "luci-flash" to flash a new image file to the system +function flash(image, kpattern) + local cmd = "luci-flash " + if kpattern then + cmd = cmd .. "-k '" .. kpattern:gsub("'", "") .. "' " + end + cmd = cmd .. "'" .. image:gsub("'", "") .. "' >/dev/null 2>&1" + + return os.execute(cmd) +end + +-- Returns the hostname +function hostname() + return io.lines("/proc/sys/kernel/hostname")() +end + +-- Returns the contents of a documented referred by an URL +function httpget(url) + return exec("wget -qO- '"..url:gsub("'", "").."'") +end + +-- Returns the FFLuci-Basedir +function libpath() + return luci.fs.dirname(require("luci.debug").__file__) +end + +-- Returns the load average +function loadavg() + local loadavg = io.lines("/proc/loadavg")() + return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$") +end + +-- Reboots the system +function reboot() + return os.execute("reboot >/dev/null 2>&1") +end + +-- Returns the system type, cpu name, and installed physical memory +function sysinfo() + local c1 = "cat /proc/cpuinfo|grep system\\ typ|cut -d: -f2 2>/dev/null" + local c2 = "uname -m 2>/dev/null" + local c3 = "cat /proc/cpuinfo|grep model\\ name|cut -d: -f2 2>/dev/null" + local c4 = "cat /proc/cpuinfo|grep cpu\\ model|cut -d: -f2 2>/dev/null" + local c5 = "cat /proc/meminfo|grep MemTotal|cut -d: -f2 2>/dev/null" + + local s = luci.util.trim(exec(c1)) + local m = "" + local r = "" + + if s == "" then + s = luci.util.trim(exec(c2)) + m = luci.util.trim(exec(c3)) + else + m = luci.util.trim(exec(c4)) + end + + r = luci.util.trim(exec(c5)) + + return s, m, r +end + +-- Reads the syslog +function syslog() + return exec("logread") +end + + +group = {} +group.getgroup = posix.getgroup + +net = {} +-- Returns the ARP-Table +function net.arptable() + return _parse_delimited_table(io.lines("/proc/net/arp"), "%s%s+") +end + +-- Returns whether an IP-Adress belongs to a certain net +function net.belongs(ip, ipnet, prefix) + return (net.ip4bin(ip):sub(1, prefix) == net.ip4bin(ipnet):sub(1, prefix)) +end + +-- Detect the default route +function net.defaultroute() + local routes = net.routes() + local route = nil + + for i, r in pairs(luci.sys.net.routes()) do + if r.Destination == "00000000" and (not route or route.Metric > r.Metric) then + route = r + end + end + + return route +end + +-- Returns all available network interfaces +function net.devices() + local devices = {} + for line in io.lines("/proc/net/dev") do + table.insert(devices, line:match(" *(.-):")) + end + return devices +end + +-- Returns the MAC-Address belonging to the given IP-Address +function net.ip4mac(ip) + local mac = nil + + for i, l in ipairs(net.arptable()) do + if l["IP address"] == ip then + mac = l["HW address"] + end + end + + return mac +end + +-- Returns the prefix to a given netmask +function net.mask4prefix(mask) + local bin = net.ip4bin(mask) + + if not bin then + return nil + end + + return #luci.util.split(bin, "1")-1 +end + +-- Returns the kernel routing table +function net.routes() + return _parse_delimited_table(io.lines("/proc/net/route")) +end + +-- Returns the numeric IP to a given hexstring +function net.hexip4(hex, be) + if #hex ~= 8 then + return nil + end + + be = be or bigendian() + + local hexdec = luci.bits.Hex2Dec + + local ip = "" + if be then + ip = ip .. tostring(hexdec(hex:sub(1,2))) .. "." + ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "." + ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "." + ip = ip .. tostring(hexdec(hex:sub(7,8))) + else + ip = ip .. tostring(hexdec(hex:sub(7,8))) .. "." + ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "." + ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "." + ip = ip .. tostring(hexdec(hex:sub(1,2))) + end + + return ip +end + +-- Returns the binary IP to a given IP +function net.ip4bin(ip) + local parts = luci.util.split(ip, '.') + if #parts ~= 4 then + return nil + end + + local decbin = luci.bits.Dec2Bin + + local bin = "" + bin = bin .. decbin(parts[1], 8) + bin = bin .. decbin(parts[2], 8) + bin = bin .. decbin(parts[3], 8) + bin = bin .. decbin(parts[4], 8) + + return bin +end + +-- Tests whether a host is pingable +function net.pingtest(host) + return os.execute("ping -c1 '"..host:gsub("'", '').."' >/dev/null 2>&1") +end + + +process = {} +process.info = posix.getpid + +-- Sets the gid of a process +function process.setgroup(pid, gid) + return posix.setpid("g", pid, gid) +end + +-- Sets the uid of a process +function process.setuser(pid, uid) + return posix.setpid("u", pid, uid) +end + +user = {} +-- returns user information to a given uid +user.getuser = posix.getpasswd + +-- Changes the user password of given user +function user.setpasswd(user, pwd) + if pwd then + pwd = pwd:gsub("'", "") + end + + if user then + user = user:gsub("'", "") + end + + local cmd = "(echo '"..pwd.."';sleep 1;echo '"..pwd.."')|" + cmd = cmd .. "passwd '"..user.."' >/dev/null 2>&1" + return os.execute(cmd) +end + + +wifi = {} + +function wifi.getiwconfig() + local cnt = exec("/usr/sbin/iwconfig 2>/dev/null") + local iwc = {} + + for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do + local k = l:match("^(.-) ") + l = l:gsub("^(.-) +", "", 1) + if k then + iwc[k] = _parse_mixed_record(l) + end + end + + return iwc +end + +function wifi.iwscan() + local cnt = exec("iwlist scan 2>/dev/null") + local iws = {} + + for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do + local k = l:match("^(.-) ") + l = l:gsub("^[^\n]+", "", 1) + l = luci.util.trim(l) + if k then + iws[k] = {} + for j, c in pairs(luci.util.split(l, "\n Cell")) do + c = c:gsub("^(.-)- ", "", 1) + c = luci.util.split(c, "\n", 7) + c = table.concat(c, "\n", 1) + table.insert(iws[k], _parse_mixed_record(c)) + end + end + end + + return iws +end + + +-- Internal functions + +function _parse_delimited_table(iter, delimiter) + delimiter = delimiter or "%s+" + + local data = {} + local trim = luci.util.trim + local split = luci.util.split + + local keys = split(trim(iter()), delimiter, nil, true) + for i, j in pairs(keys) do + keys[i] = trim(keys[i]) + end + + for line in iter do + local row = {} + line = trim(line) + if #line > 0 then + for i, j in pairs(split(line, delimiter, nil, true)) do + if keys[i] then + row[keys[i]] = j + end + end + end + table.insert(data, row) + end + + return data +end + +function _parse_mixed_record(cnt) + local data = {} + + for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n")) do + for j, f in pairs(luci.util.split(luci.util.trim(l), " ")) do + local k, x, v = f:match('([^%s][^:=]+) *([:=]*) *"*([^\n"]*)"*') + + if k then + if x == "" then + table.insert(data, k) + else + data[k] = v + end + end + end + end + + return data +end
\ No newline at end of file diff --git a/libs/core/luasrc/sys/iptparser.lua b/libs/core/luasrc/sys/iptparser.lua new file mode 100644 index 0000000000..6450c30729 --- /dev/null +++ b/libs/core/luasrc/sys/iptparser.lua @@ -0,0 +1,245 @@ +--[[ +LuCI - Iptables parser and query library + +Copyright 2008 Jo-Philipp Wich <freifunk@wwsnet.net> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +$Id$ + +]]-- + +module("luci.sys.iptparser", package.seeall) +require("luci.sys") +require("luci.util") + + +IptParser = luci.util.class() + +--[[ +IptParser.__init__( ... ) + +The class constructor, initializes the internal lookup table. +]]-- + +function IptParser.__init__( self, ... ) + self._rules = { } + self._chain = nil + self:_parse_rules() +end + + +--[[ +IptParser.find( args ) + +Find all firewall rules that match the given criteria. Expects a table with search criteria as only argument. +If args is nil or an empty table then all rules will be returned. + +The following keys in the args table are recognized: + + - table Match rules that are located within the given table + - chain Match rules that are located within the given chain + - target Match rules with the given target + - protocol Match rules that match the given protocol, rules with protocol "all" are always matched + - source Match rules with the given source, rules with source "0.0.0.0/0" are always matched + - destination Match rules with the given destination, rules with destination "0.0.0.0/0" are always matched + - inputif Match rules with the given input interface, rules with input interface "*" (=all) are always matched + - outputif Match rules with the given output interface, rules with output interface "*" (=all) are always matched + - flags Match rules that match the given flags, current supported values are "-f" (--fragment) and "!f" (! --fragment) + - options Match rules containing all given options + +The return value is a list of tables representing the matched rules. +Each rule table contains the following fields: + + - index The index number of the rule + - table The table where the rule is located, can be one of "filter", "nat" or "mangle" + - chain The chain where the rule is located, e.g. "INPUT" or "postrouting_wan" + - target The rule target, e.g. "REJECT" or "DROP" + - protocol The matching protocols, e.g. "all" or "tcp" + - flags Special rule options ("--", "-f" or "!f") + - inputif Input interface of the rule, e.g. "eth0.0" or "*" for all interfaces + - outputif Output interface of the rule, e.g. "eth0.0" or "*" for all interfaces + - source The source ip range, e.g. "0.0.0.0/0" + - destination The destination ip range, e.g. "0.0.0.0/0" + - options A list of specific options of the rule, e.g. { "reject-with", "tcp-reset" } + - packets The number of packets matched by the rule + - bytes The number of total bytes matched by the rule + +Example: + +ip = luci.sys.iptparser.IptParser() +result = ip.find( { + target="REJECT", + protocol="tcp", + options={ "reject-with", "tcp-reset" } +} ) + +This will match all rules with target "-j REJECT", protocol "-p tcp" (or "-p all") and the option "--reject-with tcp-reset". + +]]-- + +function IptParser.find( self, args ) + + local args = args or { } + local rv = { } + + for i, rule in ipairs(self._rules) do + local match = true + + -- match table + if not ( not args.table or args.table == rule.table ) then + match = false + end + + -- match chain + if not ( match == true and ( not args.chain or args.chain == rule.chain ) ) then + match = false + end + + -- match target + if not ( match == true and ( not args.target or args.target == rule.target ) ) then + match = false + end + + -- match protocol + if not ( match == true and ( not args.protocol or rule.protocol == "all" or args.protocol == rule.protocol ) ) then + match = false + end + + -- match source (XXX: implement ipcalc stuff so that 192.168.1.0/24 matches 0.0.0.0/0 etc.) + if not ( match == true and ( not args.source or rule.source == "0.0.0.0/0" or rule.source == args.source ) ) then + match = false + end + + -- match destination (XXX: implement ipcalc stuff so that 192.168.1.0/24 matches 0.0.0.0/0 etc.) + if not ( match == true and ( not args.destination or rule.destination == "0.0.0.0/0" or rule.destination == args.destination ) ) then + match = false + end + + -- match input interface + if not ( match == true and ( not args.inputif or rule.inputif == "*" or args.inputif == rule.inputif ) ) then + match = false + end + + -- match output interface + if not ( match == true and ( not args.outputif or rule.outputif == "*" or args.outputif == rule.outputif ) ) then + match = false + end + + -- match flags (the "opt" column) + if not ( match == true and ( not args.flags or rule.flags == args.flags ) ) then + match = false + end + + -- match specific options + if not ( match == true and ( not args.options or self:_match_options( rule.options, args.options ) ) ) then + match = false + end + + + -- insert match + if match == true then + table.insert( rv, rule ) + end + end + + return rv +end + + +--[[ +IptParser.resync() + +Rebuild the internal lookup table, for example when rules have changed through external commands. +]]-- + +function IptParser.resync( self ) + self._rules = { } + self._chain = nil + self:_parse_rules() +end + + +--[[ +IptParser._parse_rules() + +[internal] Parse iptables output from all tables. +]]-- + +function IptParser._parse_rules( self ) + + for i, tbl in ipairs({ "filter", "nat", "mangle" }) do + + for i, rule in ipairs(luci.sys.execl("iptables -t " .. tbl .. " --line-numbers -nxvL")) do + + if rule:find( "Chain " ) == 1 then + + self._chain = rule:gsub("Chain ([^%s]*) .*", "%1") + + else + if rule:find("%d") == 1 then + + local rule_parts = luci.util.split( rule, "%s+", nil, true ) + local rule_details = { } + + rule_details["table"] = tbl + rule_details["chain"] = self._chain + rule_details["index"] = tonumber(rule_parts[1]) + rule_details["packets"] = tonumber(rule_parts[2]) + rule_details["bytes"] = tonumber(rule_parts[3]) + rule_details["target"] = rule_parts[4] + rule_details["protocol"] = rule_parts[5] + rule_details["flags"] = rule_parts[6] + rule_details["inputif"] = rule_parts[7] + rule_details["outputif"] = rule_parts[8] + rule_details["source"] = rule_parts[9] + rule_details["destination"] = rule_parts[10] + rule_details["options"] = { } + + for i = 11, #rule_parts - 1 do + rule_details["options"][i-10] = rule_parts[i] + end + + table.insert( self._rules, rule_details ) + end + end + end + end + + self._chain = nil +end + + +--[[ +IptParser._match_options( optlist1, optlist2 ) + +[internal] Return true if optlist1 contains all elements of optlist2. Return false in all other cases. +]]-- + +function IptParser._match_options( self, o1, o2 ) + + -- construct a hashtable of first options list to speed up lookups + local oh = { } + for i, opt in ipairs( o1 ) do oh[opt] = true end + + -- iterate over second options list + -- each string in o2 must be also present in o1 + -- if o2 contains a string which is not found in o1 then return false + for i, opt in ipairs( o2 ) do + if not oh[opt] then + return false + end + end + + return true +end diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua new file mode 100644 index 0000000000..0559fff6f8 --- /dev/null +++ b/libs/core/luasrc/util.lua @@ -0,0 +1,215 @@ +--[[ +LuCI - Utility library + +Description: +Several common useful Lua functions + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +]]-- + +module("luci.util", package.seeall) + + +-- Lua simplified Python-style OO class support emulation +function class(base) + local class = {} + + local create = function(class, ...) + local inst = {} + setmetatable(inst, {__index = class}) + + if inst.__init__ then + local stat, err = pcall(inst.__init__, inst, ...) + if not stat then + error(err) + end + end + + return inst + end + + local classmeta = {__call = create} + + if base then + classmeta.__index = base + end + + setmetatable(class, classmeta) + return class +end + + +-- Clones an object (deep on-demand) +function clone(object, deep) + local copy = {} + + for k, v in pairs(object) do + if deep and type(v) == "table" then + v = clone(v, deep) + end + copy[k] = v + end + + setmetatable(copy, getmetatable(object)) + + return copy +end + + +-- Combines two or more numerically indexed tables into one +function combine(...) + local result = {} + for i, a in ipairs(arg) do + for j, v in ipairs(a) do + table.insert(result, v) + end + end + return result +end + + +-- Checks whether a table has an object "value" in it +function contains(table, value) + for k,v in pairs(table) do + if value == v then + return true + end + end + return false +end + + +-- Dumps a table to stdout (useful for testing and debugging) +function dumptable(t, i) + i = i or 0 + for k,v in pairs(t) do + print(string.rep("\t", i) .. k, v) + if type(v) == "table" then + dumptable(v, i+1) + end + end +end + + +-- Escapes all occurences of c in s +function escape(s, c) + c = c or "\\" + return s:gsub(c, "\\" .. c) +end + + +-- Populate obj in the scope of f as key +function extfenv(f, key, obj) + local scope = getfenv(f) + scope[key] = obj +end + + +-- Checks whether an object is an instanceof class +function instanceof(object, class) + local meta = getmetatable(object) + while meta and meta.__index do + if meta.__index == class then + return true + end + meta = getmetatable(meta.__index) + end + return false +end + + +-- Creates valid XML PCDATA from a string +function pcdata(value) + value = value:gsub("&", "&") + value = value:gsub('"', """) + value = value:gsub("'", "'") + value = value:gsub("<", "<") + return value:gsub(">", ">") +end + + +-- Resets the scope of f doing a shallow copy of its scope into a new table +function resfenv(f) + setfenv(f, clone(getfenv(f))) +end + + +-- Splits a string into an array +function split(str, pat, max, regex) + pat = pat or "\n" + max = max or #str + + local t = {} + local c = 1 + + if #str == 0 then + return {""} + end + + if #pat == 0 then + return nil + end + + if max == 0 then + return str + end + + repeat + local s, e = str:find(pat, c, not regex) + table.insert(t, str:sub(c, s and s - 1)) + max = max - 1 + c = e and e + 1 or #str + 1 + until not s or max < 0 + + return t +end + +-- Removes whitespace from beginning and end of a string +function trim(str) + local s = str:gsub("^%s*(.-)%s*$", "%1") + return s +end + +-- Updates given table with new values +function update(t, updates) + for k, v in pairs(updates) do + t[k] = v + end +end + + +-- Updates the scope of f with "extscope" +function updfenv(f, extscope) + update(getfenv(f), extscope) +end + + +-- Validates a variable +function validate(value, cast_number, cast_int) + if cast_number or cast_int then + value = tonumber(value) + end + + if cast_int and value and not(value % 1 == 0) then + value = nil + end + + return value +end
\ No newline at end of file |