Added module for system abstraction
[project/luci.git] / src / ffluci / util.lua
1 --[[
2 FFLuCI - Utility library
3
4 Description:
5 Several common useful Lua functions
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
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.
24
25 ]]--
26
27 module("ffluci.util", package.seeall)
28
29
30 -- Lua OO class support emulation
31 function class(base)
32         local clsobj = {}
33         local metatable = {__index = clsobj}
34         
35     function clsobj.new()
36         local inst = {}
37         setmetatable(inst, metatable)
38         return inst
39     end 
40         
41         if base then
42                 setmetatable(clsobj, {__index = base})
43         end
44         
45         return clsobj
46 end
47
48
49 -- Checks whether a table has an object "value" in it
50 function contains(table, value)
51         for k,v in pairs(table) do
52                 if value == v then
53                         return true
54                 end
55         end
56         return false
57 end
58
59
60 -- Dumps a table to stdout (useful for testing and debugging)
61 function dumptable(t, i)
62         i = i or 0
63         for k,v in pairs(t) do
64                 print(string.rep("\t", i) .. k, v)
65                 if type(v) == "table" then
66                         dumptable(v, i+1)
67                 end
68         end
69 end
70
71
72 -- Escapes all occurences of c in s
73 function escape(s, c)
74         c = c or "\\"
75         return s:gsub(c, "\\" .. c)
76 end
77
78
79 -- Runs "command" and returns its output
80 function exec(command)
81         local pp   = io.popen(command)
82         local data = pp:read("*a")
83         pp:close()
84         
85         return data
86 end
87
88 -- Runs "command" and returns its output as a array of lines
89 function execl(command)
90         local pp   = io.popen(command)  
91         local line = ""
92         local data = {}
93         
94         while true do
95                 line = pp:read()
96                 if (line == nil) then break end
97                 table.insert(data, line)
98         end 
99         pp:close()      
100         
101         return data
102 end
103
104 -- Populate obj in the scope of f as key 
105 function extfenv(f, key, obj)
106         local scope = getfenv(f)
107         scope[key] = obj
108         setfenv(f, scope)
109 end
110
111
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
116                 scope[k] = v
117         end
118         setfenv(f, scope)
119 end
120
121 -- Returns the filename of the calling script
122 function __file__()
123         return debug.getinfo(2, 'S').source:sub(2)
124 end