1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local io = require "io"
5 local math = require "math"
6 local table = require "table"
7 local debug = require "debug"
8 local ldebug = require "luci.debug"
9 local string = require "string"
10 local coroutine = require "coroutine"
11 local tparser = require "luci.template.parser"
12 local json = require "luci.jsonc"
14 local _ubus = require "ubus"
15 local _ubus_connection = nil
17 local getmetatable, setmetatable = getmetatable, setmetatable
18 local rawget, rawset, unpack = rawget, rawset, unpack
19 local tostring, type, assert = tostring, type, assert
20 local ipairs, pairs, next, loadstring = ipairs, pairs, next, loadstring
21 local require, pcall, xpcall = require, pcall, xpcall
22 local collectgarbage, get_memory_limit = collectgarbage, get_memory_limit
27 -- Pythonic string formatting extension
29 getmetatable("").__mod = function(a, b)
32 elseif type(b) == "table" then
33 for k, _ in pairs(b) do if type(b[k]) == "userdata" then b[k] = tostring(b[k]) end end
34 return a:format(unpack(b))
36 if type(b) == "userdata" then b = tostring(b) end
43 -- Class helper routines
46 -- Instantiates a class
47 local function _instantiate(class, ...)
48 local inst = setmetatable({}, {__index = class})
57 -- The class object can be instantiated by calling itself.
58 -- Any class functions or shared parameters can be attached to this object.
59 -- Attaching a table to the class object makes this table shared between
60 -- all instances of this class. For object parameters use the __init__ function.
61 -- Classes can inherit member functions and values from a base class.
62 -- Class can be instantiated by calling them. All parameters will be passed
63 -- to the __init__ function of this class - if such a function exists.
64 -- The __init__ function must be used to set any object parameters that are not shared
65 -- with other objects of this class. Any return values will be ignored.
67 return setmetatable({}, {
68 __call = _instantiate,
73 function instanceof(object, class)
74 local meta = getmetatable(object)
75 while meta and meta.__index do
76 if meta.__index == class then
79 meta = getmetatable(meta.__index)
86 -- Scope manipulation routines
92 __index = function(self, key)
93 local t = rawget(self, coxpt[coroutine.running()]
94 or coroutine.running() or 0)
98 __newindex = function(self, key, value)
99 local c = coxpt[coroutine.running()] or coroutine.running() or 0
100 local r = rawget(self, c)
102 rawset(self, c, { [key] = value })
109 -- the current active coroutine. A thread local store is private a table object
110 -- whose values can't be accessed from outside of the running coroutine.
111 function threadlocal(tbl)
112 return setmetatable(tbl or {}, tl_meta)
117 -- Debugging routines
121 return io.stderr:write(tostring(obj) .. "\n")
124 function dumptable(t, maxdepth, i, seen)
126 seen = seen or setmetatable({}, {__mode="k"})
128 for k,v in pairs(t) do
129 perror(string.rep("\t", i) .. tostring(k) .. "\t" .. tostring(v))
130 if type(v) == "table" and (not maxdepth or i < maxdepth) then
133 dumptable(v, maxdepth, i+1, seen)
135 perror(string.rep("\t", i) .. "*** RECURSION ***")
143 -- String and data manipulation routines
146 function pcdata(value)
147 return value and tparser.pcdata(tostring(value))
150 function striptags(value)
151 return value and tparser.striptags(tostring(value))
154 -- for bash, ash and similar shells single-quoted strings are taken
155 -- literally except for single quotes (which terminate the string)
156 -- (and the exception noted below for dash (-) at the start of a
157 -- command line parameter).
158 function shellsqescape(value)
160 res, _ = string.gsub(value, "'", "'\\''")
164 -- bash, ash and other similar shells interpret a dash (-) at the start
165 -- of a command-line parameters as an option indicator regardless of
166 -- whether it is inside a single-quoted string. It must be backlash
167 -- escaped to resolve this. This requires in some funky special-case
168 -- handling. It may actually be a property of the getopt function
169 -- rather than the shell proper.
170 function shellstartsqescape(value)
171 res, _ = string.gsub(value, "^\-", "\\-")
172 res, _ = string.gsub(res, "^-", "\-")
173 return shellsqescape(value)
176 -- containing the resulting substrings. The optional max parameter specifies
177 -- the number of bytes to process, regardless of the actual length of the given
178 -- string. The optional last parameter, regex, specifies whether the separator
179 -- sequence is interpreted as regular expression.
180 -- pattern as regular expression (optional, default is false)
181 function split(str, pat, max, regex)
201 local s, e = str:find(pat, c, not regex)
203 if s and max < 0 then
206 t[#t+1] = str:sub(c, s and s - 1)
208 c = e and e + 1 or #str + 1
209 until not s or max < 0
215 return (str:gsub("^%s*(.-)%s*$", "%1"))
218 function cmatch(str, pat)
220 for _ in str:gmatch(pat) do count = count + 1 end
224 -- one token per invocation, the tokens are separated by whitespace. If the
225 -- input value is a table, it is transformed into a string first. A nil value
226 -- will result in a valid interator which aborts with the first invocation.
228 if type(v) == "table" then
235 elseif type(v) == "number" or type(v) == "boolean" then
244 elseif type(v) == "userdata" or type(v) == "string" then
245 return tostring(v):gmatch("%S+")
248 return function() end
251 -- value or 0 if the unit is unknown. Upper- or lower case is irrelevant.
252 -- Recognized units are:
253 -- o "y" - one year (60*60*24*366)
254 -- o "m" - one month (60*60*24*31)
255 -- o "w" - one week (60*60*24*7)
256 -- o "d" - one day (60*60*24)
257 -- o "h" - one hour (60*60)
258 -- o "min" - one minute (60)
259 -- o "kb" - one kilobyte (1024)
260 -- o "mb" - one megabyte (1024*1024)
261 -- o "gb" - one gigabyte (1024*1024*1024)
262 -- o "kib" - one si kilobyte (1000)
263 -- o "mib" - one si megabyte (1000*1000)
264 -- o "gib" - one si gigabyte (1000*1000*1000)
265 function parse_units(ustr)
272 y = 60 * 60 * 24 * 366,
273 m = 60 * 60 * 24 * 31,
274 w = 60 * 60 * 24 * 7,
282 gb = 1024 * 1024 * 1024,
284 -- storage sizes (si)
287 gib = 1000 * 1000 * 1000
290 -- parse input string
291 for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do
293 local num = spec:gsub("[^0-9%.]+$","")
294 local spn = spec:gsub("^[0-9%.]+", "")
296 if map[spn] or map[spn:sub(1,1)] then
297 val = val + num * ( map[spn] or map[spn:sub(1,1)] )
307 -- also register functions above in the central string class for convenience
308 string.pcdata = pcdata
309 string.striptags = striptags
312 string.cmatch = cmatch
313 string.parse_units = parse_units
316 function append(src, ...)
317 for i, a in ipairs({...}) do
318 if type(a) == "table" then
319 for j, v in ipairs(a) do
329 function combine(...)
330 return append({}, ...)
333 function contains(table, value)
334 for k, v in pairs(table) do
342 -- Both table are - in fact - merged together.
343 function update(t, updates)
344 for k, v in pairs(updates) do
352 for k, _ in kspairs(t) do
359 function clone(object, deep)
362 for k, v in pairs(object) do
363 if deep and type(v) == "table" then
369 return setmetatable(copy, getmetatable(object))
374 return setmetatable({}, { __index =
376 return rawget(tbl, key)
377 or rawget(rawset(tbl, key, dtable()), key)
383 -- Serialize the contents of a table value.
384 function _serialize_table(t, seen)
385 assert(not seen[t], "Recursion detected.")
392 for k, v in pairs(t) do
393 if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then
394 k = serialize_data(k, seen)
395 v = serialize_data(v, seen)
396 data = data .. ( #data > 0 and ", " or "" ) ..
397 '[' .. k .. '] = ' .. v
404 local v = serialize_data(t[i], seen)
405 idata = idata .. ( #idata > 0 and ", " or "" ) .. v
408 return idata .. ( #data > 0 and #idata > 0 and ", " or "" ) .. data
411 -- with loadstring().
412 function serialize_data(val, seen)
413 seen = seen or setmetatable({}, {__mode="k"})
417 elseif type(val) == "number" then
419 elseif type(val) == "string" then
421 elseif type(val) == "boolean" then
422 return val and "true" or "false"
423 elseif type(val) == "function" then
424 return "loadstring(%q)" % get_bytecode(val)
425 elseif type(val) == "table" then
426 return "{ " .. _serialize_table(val, seen) .. " }"
428 return '"[unhandled data type:' .. type(val) .. ']"'
432 function restore_data(str)
433 return loadstring("return " .. str)()
438 -- Byte code manipulation routines
441 -- will be stripped before it is returned.
442 function get_bytecode(val)
445 if type(val) == "function" then
446 code = string.dump(val)
448 code = string.dump( loadstring( "return " .. serialize_data(val) ) )
451 return code -- and strip_bytecode(code)
454 -- numbers and debugging numbers will be discarded. Original version by
455 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
456 function strip_bytecode(code)
457 local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
460 subint = function(code, i, l)
463 val = val * 256 + code:byte(i + n - 1)
468 subint = function(code, i, l)
471 val = val * 256 + code:byte(i + n - 1)
477 local function strip_function(code)
478 local count, offset = subint(code, 1, size)
479 local stripped = { string.rep("\0", size) }
480 local dirty = offset + count
481 offset = offset + count + int * 2 + 4
482 offset = offset + int + subint(code, offset, int) * ins
483 count, offset = subint(code, offset, int)
486 t, offset = subint(code, offset, 1)
490 offset = offset + size + subint(code, offset, size)
492 offset = offset + num
493 elseif t == 254 or t == 9 then
494 offset = offset + lnum
497 count, offset = subint(code, offset, int)
498 stripped[#stripped+1] = code:sub(dirty, offset - 1)
500 local proto, off = strip_function(code:sub(offset, -1))
501 stripped[#stripped+1] = proto
502 offset = offset + off - 1
504 offset = offset + subint(code, offset, int) * int + int
505 count, offset = subint(code, offset, int)
507 offset = offset + subint(code, offset, size) + size + int * 2
509 count, offset = subint(code, offset, int)
511 offset = offset + subint(code, offset, size) + size
513 stripped[#stripped+1] = string.rep("\0", int * 3)
514 return table.concat(stripped), offset
517 return code:sub(1,12) .. strip_function(code:sub(13,-1))
522 -- Sorting iterator functions
525 function _sortiter( t, f )
529 for k, v in pairs(t) do
535 table.sort( keys, f )
539 if _pos <= #keys then
540 return keys[_pos], t[keys[_pos]], _pos
545 -- the provided callback function.
547 return _sortiter( t, f )
550 -- The table pairs are sorted by key.
552 return _sortiter( t )
555 -- The table pairs are sorted by value.
557 return _sortiter( t, function (a,b) return t[a] < t[b] end )
562 -- System utility functions
566 return string.byte(string.dump(function() end), 7) == 0
569 function exec(command)
570 local pp = io.popen(command)
571 local data = pp:read("*a")
577 function execi(command)
578 local pp = io.popen(command)
580 return pp and function()
581 local line = pp:read()
592 function execl(command)
593 local pp = io.popen(command)
599 if (line == nil) then break end
607 function ubus(object, method, data)
608 if not _ubus_connection then
609 _ubus_connection = _ubus.connect()
610 assert(_ubus_connection, "Unable to establish ubus connection")
613 if object and method then
614 if type(data) ~= "table" then
617 return _ubus_connection:call(object, method, data)
619 return _ubus_connection:signatures(object)
621 return _ubus_connection:objects()
625 function serialize_json(x, cb)
626 local js = json.stringify(x)
627 if type(cb) == "function" then
636 return require "nixio.fs".dirname(ldebug.__file__)
639 function checklib(fullpathexe, wantedlib)
640 local fs = require "nixio.fs"
641 local haveldd = fs.access('/usr/bin/ldd')
645 local libs = exec("/usr/bin/ldd " .. fullpathexe)
649 for k, v in ipairs(split(libs)) do
650 if v:find(wantedlib) then
658 -- Coroutine safe xpcall and pcall versions modified for Luci
660 -- coxpcall 1.13 - Copyright 2005 - Kepler Project (www.keplerproject.org)
662 -- Copyright © 2005 Kepler Project.
663 -- Permission is hereby granted, free of charge, to any person obtaining a
664 -- copy of this software and associated documentation files (the "Software"),
665 -- to deal in the Software without restriction, including without limitation
666 -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
667 -- and/or sell copies of the Software, and to permit persons to whom the
668 -- Software is furnished to do so, subject to the following conditions:
670 -- The above copyright notice and this permission notice shall be
671 -- included in all copies or substantial portions of the Software.
673 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
674 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
675 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
676 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
677 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
678 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
679 -- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
681 local performResume, handleReturnValue
682 local oldpcall, oldxpcall = pcall, xpcall
684 setmetatable(coxpt, {__mode = "kv"})
686 -- Identity function for copcall
687 local function copcall_id(trace, ...)
691 -- values of either the function or the error handler
692 function coxpcall(f, err, ...)
693 local res, co = oldpcall(coroutine.create, f)
696 local newf = function() return f(unpack(params)) end
697 co = coroutine.create(newf)
699 local c = coroutine.running()
700 coxpt[co] = coxpt[c] or c or 0
702 return performResume(err, co, ...)
705 -- values of the function or the error object
706 function copcall(f, ...)
707 return coxpcall(f, copcall_id, ...)
710 -- Handle return value of protected call
711 function handleReturnValue(err, co, status, ...)
713 return false, err(debug.traceback(co, (...)), ...)
716 if coroutine.status(co) ~= 'suspended' then
720 return performResume(err, co, coroutine.yield(...))
723 -- Resume execution of protected function call
724 function performResume(err, co, ...)
725 return handleReturnValue(err, co, coroutine.resume(co, ...))