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"
13 local lhttp = require "lucihttp"
15 local _ubus = require "ubus"
16 local _ubus_connection = nil
18 local getmetatable, setmetatable = getmetatable, setmetatable
19 local rawget, rawset, unpack, select = rawget, rawset, unpack, select
20 local tostring, type, assert, error = tostring, type, assert, error
21 local ipairs, pairs, next, loadstring = ipairs, pairs, next, loadstring
22 local require, pcall, xpcall = require, pcall, xpcall
23 local collectgarbage, get_memory_limit = collectgarbage, get_memory_limit
28 -- Pythonic string formatting extension
30 getmetatable("").__mod = function(a, b)
35 elseif type(b) == "table" then
37 for k, _ in pairs(b) do if type(b[k]) == "userdata" then b[k] = tostring(b[k]) end end
39 ok, res = pcall(a.format, a, unpack(b))
45 if type(b) == "userdata" then b = tostring(b) end
47 ok, res = pcall(a.format, a, b)
57 -- Class helper routines
60 -- Instantiates a class
61 local function _instantiate(class, ...)
62 local inst = setmetatable({}, {__index = class})
71 -- The class object can be instantiated by calling itself.
72 -- Any class functions or shared parameters can be attached to this object.
73 -- Attaching a table to the class object makes this table shared between
74 -- all instances of this class. For object parameters use the __init__ function.
75 -- Classes can inherit member functions and values from a base class.
76 -- Class can be instantiated by calling them. All parameters will be passed
77 -- to the __init__ function of this class - if such a function exists.
78 -- The __init__ function must be used to set any object parameters that are not shared
79 -- with other objects of this class. Any return values will be ignored.
81 return setmetatable({}, {
82 __call = _instantiate,
87 function instanceof(object, class)
88 local meta = getmetatable(object)
89 while meta and meta.__index do
90 if meta.__index == class then
93 meta = getmetatable(meta.__index)
100 -- Scope manipulation routines
103 coxpt = setmetatable({}, { __mode = "kv" })
108 __index = function(self, key)
109 local t = rawget(self, coxpt[coroutine.running()]
110 or coroutine.running() or 0)
114 __newindex = function(self, key, value)
115 local c = coxpt[coroutine.running()] or coroutine.running() or 0
116 local r = rawget(self, c)
118 rawset(self, c, { [key] = value })
125 -- the current active coroutine. A thread local store is private a table object
126 -- whose values can't be accessed from outside of the running coroutine.
127 function threadlocal(tbl)
128 return setmetatable(tbl or {}, tl_meta)
133 -- Debugging routines
137 return io.stderr:write(tostring(obj) .. "\n")
140 function dumptable(t, maxdepth, i, seen)
142 seen = seen or setmetatable({}, {__mode="k"})
144 for k,v in pairs(t) do
145 perror(string.rep("\t", i) .. tostring(k) .. "\t" .. tostring(v))
146 if type(v) == "table" and (not maxdepth or i < maxdepth) then
149 dumptable(v, maxdepth, i+1, seen)
151 perror(string.rep("\t", i) .. "*** RECURSION ***")
159 -- String and data manipulation routines
162 function pcdata(value)
163 return value and tparser.pcdata(tostring(value))
166 function urlencode(value)
168 local str = tostring(value)
169 return lhttp.urlencode(str, lhttp.ENCODE_IF_NEEDED + lhttp.ENCODE_FULL)
175 function urldecode(value, decode_plus)
177 local flag = decode_plus and lhttp.DECODE_PLUS or 0
178 local str = tostring(value)
179 return lhttp.urldecode(str, lhttp.DECODE_IF_NEEDED + flag)
185 function striptags(value)
186 return value and tparser.striptags(tostring(value))
189 function shellquote(value)
190 return string.format("'%s'", string.gsub(value or "", "'", "'\\''"))
193 -- for bash, ash and similar shells single-quoted strings are taken
194 -- literally except for single quotes (which terminate the string)
195 -- (and the exception noted below for dash (-) at the start of a
196 -- command line parameter).
197 function shellsqescape(value)
199 res, _ = string.gsub(value, "'", "'\\''")
203 -- bash, ash and other similar shells interpret a dash (-) at the start
204 -- of a command-line parameters as an option indicator regardless of
205 -- whether it is inside a single-quoted string. It must be backlash
206 -- escaped to resolve this. This requires in some funky special-case
207 -- handling. It may actually be a property of the getopt function
208 -- rather than the shell proper.
209 function shellstartsqescape(value)
210 res, _ = string.gsub(value, "^%-", "\\-")
211 return shellsqescape(res)
214 -- containing the resulting substrings. The optional max parameter specifies
215 -- the number of bytes to process, regardless of the actual length of the given
216 -- string. The optional last parameter, regex, specifies whether the separator
217 -- sequence is interpreted as regular expression.
218 -- pattern as regular expression (optional, default is false)
219 function split(str, pat, max, regex)
239 local s, e = str:find(pat, c, not regex)
241 if s and max < 0 then
244 t[#t+1] = str:sub(c, s and s - 1)
246 c = e and e + 1 or #str + 1
247 until not s or max < 0
253 return (str:gsub("^%s*(.-)%s*$", "%1"))
256 function cmatch(str, pat)
258 for _ in str:gmatch(pat) do count = count + 1 end
262 -- one token per invocation, the tokens are separated by whitespace. If the
263 -- input value is a table, it is transformed into a string first. A nil value
264 -- will result in a valid iterator which aborts with the first invocation.
266 if type(v) == "table" then
273 elseif type(v) == "number" or type(v) == "boolean" then
282 elseif type(v) == "userdata" or type(v) == "string" then
283 return tostring(v):gmatch("%S+")
286 return function() end
289 -- value or 0 if the unit is unknown. Upper- or lower case is irrelevant.
290 -- Recognized units are:
291 -- o "y" - one year (60*60*24*366)
292 -- o "m" - one month (60*60*24*31)
293 -- o "w" - one week (60*60*24*7)
294 -- o "d" - one day (60*60*24)
295 -- o "h" - one hour (60*60)
296 -- o "min" - one minute (60)
297 -- o "kb" - one kilobyte (1024)
298 -- o "mb" - one megabyte (1024*1024)
299 -- o "gb" - one gigabyte (1024*1024*1024)
300 -- o "kib" - one si kilobyte (1000)
301 -- o "mib" - one si megabyte (1000*1000)
302 -- o "gib" - one si gigabyte (1000*1000*1000)
303 function parse_units(ustr)
310 y = 60 * 60 * 24 * 366,
311 m = 60 * 60 * 24 * 31,
312 w = 60 * 60 * 24 * 7,
320 gb = 1024 * 1024 * 1024,
322 -- storage sizes (si)
325 gib = 1000 * 1000 * 1000
328 -- parse input string
329 for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do
331 local num = spec:gsub("[^0-9%.]+$","")
332 local spn = spec:gsub("^[0-9%.]+", "")
334 if map[spn] or map[spn:sub(1,1)] then
335 val = val + num * ( map[spn] or map[spn:sub(1,1)] )
345 -- also register functions above in the central string class for convenience
346 string.pcdata = pcdata
347 string.striptags = striptags
350 string.cmatch = cmatch
351 string.parse_units = parse_units
354 function append(src, ...)
355 for i, a in ipairs({...}) do
356 if type(a) == "table" then
357 for j, v in ipairs(a) do
367 function combine(...)
368 return append({}, ...)
371 function contains(table, value)
372 for k, v in pairs(table) do
380 -- Both table are - in fact - merged together.
381 function update(t, updates)
382 for k, v in pairs(updates) do
390 for k, _ in kspairs(t) do
397 function clone(object, deep)
400 for k, v in pairs(object) do
401 if deep and type(v) == "table" then
407 return setmetatable(copy, getmetatable(object))
411 -- Serialize the contents of a table value.
412 function _serialize_table(t, seen)
413 assert(not seen[t], "Recursion detected.")
420 for k, v in pairs(t) do
421 if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then
422 k = serialize_data(k, seen)
423 v = serialize_data(v, seen)
424 data = data .. ( #data > 0 and ", " or "" ) ..
425 '[' .. k .. '] = ' .. v
432 local v = serialize_data(t[i], seen)
433 idata = idata .. ( #idata > 0 and ", " or "" ) .. v
436 return idata .. ( #data > 0 and #idata > 0 and ", " or "" ) .. data
439 -- with loadstring().
440 function serialize_data(val, seen)
441 seen = seen or setmetatable({}, {__mode="k"})
445 elseif type(val) == "number" then
447 elseif type(val) == "string" then
449 elseif type(val) == "boolean" then
450 return val and "true" or "false"
451 elseif type(val) == "function" then
452 return "loadstring(%q)" % get_bytecode(val)
453 elseif type(val) == "table" then
454 return "{ " .. _serialize_table(val, seen) .. " }"
456 return '"[unhandled data type:' .. type(val) .. ']"'
460 function restore_data(str)
461 return loadstring("return " .. str)()
466 -- Byte code manipulation routines
469 -- will be stripped before it is returned.
470 function get_bytecode(val)
473 if type(val) == "function" then
474 code = string.dump(val)
476 code = string.dump( loadstring( "return " .. serialize_data(val) ) )
479 return code -- and strip_bytecode(code)
482 -- numbers and debugging numbers will be discarded. Original version by
483 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
484 function strip_bytecode(code)
485 local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
488 subint = function(code, i, l)
491 val = val * 256 + code:byte(i + n - 1)
496 subint = function(code, i, l)
499 val = val * 256 + code:byte(i + n - 1)
505 local function strip_function(code)
506 local count, offset = subint(code, 1, size)
507 local stripped = { string.rep("\0", size) }
508 local dirty = offset + count
509 offset = offset + count + int * 2 + 4
510 offset = offset + int + subint(code, offset, int) * ins
511 count, offset = subint(code, offset, int)
514 t, offset = subint(code, offset, 1)
518 offset = offset + size + subint(code, offset, size)
520 offset = offset + num
521 elseif t == 254 or t == 9 then
522 offset = offset + lnum
525 count, offset = subint(code, offset, int)
526 stripped[#stripped+1] = code:sub(dirty, offset - 1)
528 local proto, off = strip_function(code:sub(offset, -1))
529 stripped[#stripped+1] = proto
530 offset = offset + off - 1
532 offset = offset + subint(code, offset, int) * int + int
533 count, offset = subint(code, offset, int)
535 offset = offset + subint(code, offset, size) + size + int * 2
537 count, offset = subint(code, offset, int)
539 offset = offset + subint(code, offset, size) + size
541 stripped[#stripped+1] = string.rep("\0", int * 3)
542 return table.concat(stripped), offset
545 return code:sub(1,12) .. strip_function(code:sub(13,-1))
550 -- Sorting iterator functions
553 function _sortiter( t, f )
557 for k, v in pairs(t) do
563 table.sort( keys, f )
567 if _pos <= #keys then
568 return keys[_pos], t[keys[_pos]], _pos
573 -- the provided callback function.
575 return _sortiter( t, f )
578 -- The table pairs are sorted by key.
580 return _sortiter( t )
583 -- The table pairs are sorted by value.
585 return _sortiter( t, function (a,b) return t[a] < t[b] end )
590 -- System utility functions
594 return string.byte(string.dump(function() end), 7) == 0
597 function exec(command)
598 local pp = io.popen(command)
599 local data = pp:read("*a")
605 function execi(command)
606 local pp = io.popen(command)
608 return pp and function()
609 local line = pp:read()
620 function execl(command)
621 local pp = io.popen(command)
627 if (line == nil) then break end
649 local function ubus_return(...)
650 if select('#', ...) == 2 then
651 local rv, err = select(1, ...), select(2, ...)
652 if rv == nil and type(err) == "number" then
653 return nil, err, ubus_codes[err]
660 function ubus(object, method, data)
661 if not _ubus_connection then
662 _ubus_connection = _ubus.connect()
663 assert(_ubus_connection, "Unable to establish ubus connection")
666 if object and method then
667 if type(data) ~= "table" then
670 return ubus_return(_ubus_connection:call(object, method, data))
672 return _ubus_connection:signatures(object)
674 return _ubus_connection:objects()
678 function serialize_json(x, cb)
679 local js = json.stringify(x)
680 if type(cb) == "function" then
689 return require "nixio.fs".dirname(ldebug.__file__)
692 function checklib(fullpathexe, wantedlib)
693 local fs = require "nixio.fs"
694 local haveldd = fs.access('/usr/bin/ldd')
695 local haveexe = fs.access(fullpathexe)
696 if not haveldd or not haveexe then
699 local libs = exec(string.format("/usr/bin/ldd %s", shellquote(fullpathexe)))
703 for k, v in ipairs(split(libs)) do
704 if v:find(wantedlib) then
711 -------------------------------------------------------------------------------
712 -- Coroutine safe xpcall and pcall versions
714 -- Encapsulates the protected calls with a coroutine based loop, so errors can
715 -- be dealed without the usual Lua 5.x pcall/xpcall issues with coroutines
716 -- yielding inside the call to pcall or xpcall.
718 -- Authors: Roberto Ierusalimschy and Andre Carregal
719 -- Contributors: Thomas Harning Jr., Ignacio BurgueƱo, Fabio Mascarenhas
721 -- Copyright 2005 - Kepler Project
723 -- $Id: coxpcall.lua,v 1.13 2008/05/19 19:20:02 mascarenhas Exp $
724 -------------------------------------------------------------------------------
726 -------------------------------------------------------------------------------
727 -- Implements xpcall with coroutines
728 -------------------------------------------------------------------------------
729 local coromap = setmetatable({}, { __mode = "k" })
731 local function handleReturnValue(err, co, status, ...)
733 return false, err(debug.traceback(co, (...)), ...)
735 if coroutine.status(co) == 'suspended' then
736 return performResume(err, co, coroutine.yield(...))
742 function performResume(err, co, ...)
743 return handleReturnValue(err, co, coroutine.resume(co, ...))
746 local function id(trace, ...)
750 function coxpcall(f, err, ...)
751 local current = coroutine.running()
756 if select("#", ...) > 0 then
757 local oldf, params = f, { ... }
758 f = function() return oldf(unpack(params)) end
760 return xpcall(f, err)
763 local res, co = pcall(coroutine.create, f)
765 local newf = function(...) return f(...) end
766 co = coroutine.create(newf)
768 coromap[co] = current
769 coxpt[co] = coxpt[current] or current or 0
770 return performResume(err, co, ...)
774 function copcall(f, ...)
775 return coxpcall(f, id, ...)