2 FFLuCI - Utility library
5 Several common useful Lua functions
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
27 module("ffluci.util", package.seeall)
30 -- Lua OO class support emulation
33 local metatable = {__index = clsobj}
37 setmetatable(inst, metatable)
42 setmetatable(clsobj, {__index = base})
49 -- Checks whether a table has an object "value" in it
50 function contains(table, value)
51 for k,v in pairs(table) do
60 -- Dumps a table to stdout (useful for testing and debugging)
61 function dumptable(t, i)
63 for k,v in pairs(t) do
64 print(string.rep("\t", i) .. k, v)
65 if type(v) == "table" then
72 -- Escapes all occurences of c in s
75 return s:gsub(c, "\\" .. c)
79 -- Runs "command" and returns its output
80 function exec(command)
81 local pp = io.popen(command)
82 local data = pp:read("*a")
88 -- Runs "command" and returns its output as a array of lines
89 function execl(command)
90 local pp = io.popen(command)
96 if (line == nil) then break end
97 table.insert(data, line)
104 -- Populate obj in the scope of f as key
105 function extfenv(f, key, obj)
106 local scope = getfenv(f)
112 -- Updates the scope of f with "extscope"
113 function updfenv(f, extscope)
114 local scope = getfenv(f)
115 for k, v in pairs(extscope) do
121 -- Returns the filename of the calling script
123 return debug.getinfo(2, 'S').source:sub(2)