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 = rawget, rawset, unpack
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
106 __index = function(self, key)
107 local t = rawget(self, coxpt[coroutine.running()]
108 or coroutine.running() or 0)
112 __newindex = function(self, key, value)
113 local c = coxpt[coroutine.running()] or coroutine.running() or 0
114 local r = rawget(self, c)
116 rawset(self, c, { [key] = value })
123 -- the current active coroutine. A thread local store is private a table object
124 -- whose values can't be accessed from outside of the running coroutine.
125 function threadlocal(tbl)
126 return setmetatable(tbl or {}, tl_meta)
131 -- Debugging routines
135 return io.stderr:write(tostring(obj) .. "\n")
138 function dumptable(t, maxdepth, i, seen)
140 seen = seen or setmetatable({}, {__mode="k"})
142 for k,v in pairs(t) do
143 perror(string.rep("\t", i) .. tostring(k) .. "\t" .. tostring(v))
144 if type(v) == "table" and (not maxdepth or i < maxdepth) then
147 dumptable(v, maxdepth, i+1, seen)
149 perror(string.rep("\t", i) .. "*** RECURSION ***")
157 -- String and data manipulation routines
160 function pcdata(value)
161 return value and tparser.pcdata(tostring(value))
164 function urlencode(value)
166 local str = tostring(value)
167 return lhttp.urlencode(str, lhttp.ENCODE_IF_NEEDED + lhttp.ENCODE_FULL)
173 function urldecode(value, decode_plus)
175 local flag = decode_plus and lhttp.DECODE_PLUS or 0
176 local str = tostring(value)
177 return lhttp.urldecode(str, lhttp.DECODE_IF_NEEDED + flag)
183 function striptags(value)
184 return value and tparser.striptags(tostring(value))
187 function shellquote(value)
188 return string.format("'%s'", string.gsub(value or "", "'", "'\\''"))
191 -- for bash, ash and similar shells single-quoted strings are taken
192 -- literally except for single quotes (which terminate the string)
193 -- (and the exception noted below for dash (-) at the start of a
194 -- command line parameter).
195 function shellsqescape(value)
197 res, _ = string.gsub(value, "'", "'\\''")
201 -- bash, ash and other similar shells interpret a dash (-) at the start
202 -- of a command-line parameters as an option indicator regardless of
203 -- whether it is inside a single-quoted string. It must be backlash
204 -- escaped to resolve this. This requires in some funky special-case
205 -- handling. It may actually be a property of the getopt function
206 -- rather than the shell proper.
207 function shellstartsqescape(value)
208 res, _ = string.gsub(value, "^\-", "\\-")
209 res, _ = string.gsub(res, "^-", "\-")
210 return shellsqescape(value)
213 -- containing the resulting substrings. The optional max parameter specifies
214 -- the number of bytes to process, regardless of the actual length of the given
215 -- string. The optional last parameter, regex, specifies whether the separator
216 -- sequence is interpreted as regular expression.
217 -- pattern as regular expression (optional, default is false)
218 function split(str, pat, max, regex)
238 local s, e = str:find(pat, c, not regex)
240 if s and max < 0 then
243 t[#t+1] = str:sub(c, s and s - 1)
245 c = e and e + 1 or #str + 1
246 until not s or max < 0
252 return (str:gsub("^%s*(.-)%s*$", "%1"))
255 function cmatch(str, pat)
257 for _ in str:gmatch(pat) do count = count + 1 end
261 -- one token per invocation, the tokens are separated by whitespace. If the
262 -- input value is a table, it is transformed into a string first. A nil value
263 -- will result in a valid interator which aborts with the first invocation.
265 if type(v) == "table" then
272 elseif type(v) == "number" or type(v) == "boolean" then
281 elseif type(v) == "userdata" or type(v) == "string" then
282 return tostring(v):gmatch("%S+")
285 return function() end
288 -- value or 0 if the unit is unknown. Upper- or lower case is irrelevant.
289 -- Recognized units are:
290 -- o "y" - one year (60*60*24*366)
291 -- o "m" - one month (60*60*24*31)
292 -- o "w" - one week (60*60*24*7)
293 -- o "d" - one day (60*60*24)
294 -- o "h" - one hour (60*60)
295 -- o "min" - one minute (60)
296 -- o "kb" - one kilobyte (1024)
297 -- o "mb" - one megabyte (1024*1024)
298 -- o "gb" - one gigabyte (1024*1024*1024)
299 -- o "kib" - one si kilobyte (1000)
300 -- o "mib" - one si megabyte (1000*1000)
301 -- o "gib" - one si gigabyte (1000*1000*1000)
302 function parse_units(ustr)
309 y = 60 * 60 * 24 * 366,
310 m = 60 * 60 * 24 * 31,
311 w = 60 * 60 * 24 * 7,
319 gb = 1024 * 1024 * 1024,
321 -- storage sizes (si)
324 gib = 1000 * 1000 * 1000
327 -- parse input string
328 for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do
330 local num = spec:gsub("[^0-9%.]+$","")
331 local spn = spec:gsub("^[0-9%.]+", "")
333 if map[spn] or map[spn:sub(1,1)] then
334 val = val + num * ( map[spn] or map[spn:sub(1,1)] )
344 -- also register functions above in the central string class for convenience
345 string.pcdata = pcdata
346 string.striptags = striptags
349 string.cmatch = cmatch
350 string.parse_units = parse_units
353 function append(src, ...)
354 for i, a in ipairs({...}) do
355 if type(a) == "table" then
356 for j, v in ipairs(a) do
366 function combine(...)
367 return append({}, ...)
370 function contains(table, value)
371 for k, v in pairs(table) do
379 -- Both table are - in fact - merged together.
380 function update(t, updates)
381 for k, v in pairs(updates) do
389 for k, _ in kspairs(t) do
396 function clone(object, deep)
399 for k, v in pairs(object) do
400 if deep and type(v) == "table" then
406 return setmetatable(copy, getmetatable(object))
411 return setmetatable({}, { __index =
413 return rawget(tbl, key)
414 or rawget(rawset(tbl, key, dtable()), key)
420 -- Serialize the contents of a table value.
421 function _serialize_table(t, seen)
422 assert(not seen[t], "Recursion detected.")
429 for k, v in pairs(t) do
430 if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then
431 k = serialize_data(k, seen)
432 v = serialize_data(v, seen)
433 data = data .. ( #data > 0 and ", " or "" ) ..
434 '[' .. k .. '] = ' .. v
441 local v = serialize_data(t[i], seen)
442 idata = idata .. ( #idata > 0 and ", " or "" ) .. v
445 return idata .. ( #data > 0 and #idata > 0 and ", " or "" ) .. data
448 -- with loadstring().
449 function serialize_data(val, seen)
450 seen = seen or setmetatable({}, {__mode="k"})
454 elseif type(val) == "number" then
456 elseif type(val) == "string" then
458 elseif type(val) == "boolean" then
459 return val and "true" or "false"
460 elseif type(val) == "function" then
461 return "loadstring(%q)" % get_bytecode(val)
462 elseif type(val) == "table" then
463 return "{ " .. _serialize_table(val, seen) .. " }"
465 return '"[unhandled data type:' .. type(val) .. ']"'
469 function restore_data(str)
470 return loadstring("return " .. str)()
475 -- Byte code manipulation routines
478 -- will be stripped before it is returned.
479 function get_bytecode(val)
482 if type(val) == "function" then
483 code = string.dump(val)
485 code = string.dump( loadstring( "return " .. serialize_data(val) ) )
488 return code -- and strip_bytecode(code)
491 -- numbers and debugging numbers will be discarded. Original version by
492 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
493 function strip_bytecode(code)
494 local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
497 subint = function(code, i, l)
500 val = val * 256 + code:byte(i + n - 1)
505 subint = function(code, i, l)
508 val = val * 256 + code:byte(i + n - 1)
514 local function strip_function(code)
515 local count, offset = subint(code, 1, size)
516 local stripped = { string.rep("\0", size) }
517 local dirty = offset + count
518 offset = offset + count + int * 2 + 4
519 offset = offset + int + subint(code, offset, int) * ins
520 count, offset = subint(code, offset, int)
523 t, offset = subint(code, offset, 1)
527 offset = offset + size + subint(code, offset, size)
529 offset = offset + num
530 elseif t == 254 or t == 9 then
531 offset = offset + lnum
534 count, offset = subint(code, offset, int)
535 stripped[#stripped+1] = code:sub(dirty, offset - 1)
537 local proto, off = strip_function(code:sub(offset, -1))
538 stripped[#stripped+1] = proto
539 offset = offset + off - 1
541 offset = offset + subint(code, offset, int) * int + int
542 count, offset = subint(code, offset, int)
544 offset = offset + subint(code, offset, size) + size + int * 2
546 count, offset = subint(code, offset, int)
548 offset = offset + subint(code, offset, size) + size
550 stripped[#stripped+1] = string.rep("\0", int * 3)
551 return table.concat(stripped), offset
554 return code:sub(1,12) .. strip_function(code:sub(13,-1))
559 -- Sorting iterator functions
562 function _sortiter( t, f )
566 for k, v in pairs(t) do
572 table.sort( keys, f )
576 if _pos <= #keys then
577 return keys[_pos], t[keys[_pos]], _pos
582 -- the provided callback function.
584 return _sortiter( t, f )
587 -- The table pairs are sorted by key.
589 return _sortiter( t )
592 -- The table pairs are sorted by value.
594 return _sortiter( t, function (a,b) return t[a] < t[b] end )
599 -- System utility functions
603 return string.byte(string.dump(function() end), 7) == 0
606 function exec(command)
607 local pp = io.popen(command)
608 local data = pp:read("*a")
614 function execi(command)
615 local pp = io.popen(command)
617 return pp and function()
618 local line = pp:read()
629 function execl(command)
630 local pp = io.popen(command)
636 if (line == nil) then break end
644 function ubus(object, method, data)
645 if not _ubus_connection then
646 _ubus_connection = _ubus.connect()
647 assert(_ubus_connection, "Unable to establish ubus connection")
650 if object and method then
651 if type(data) ~= "table" then
654 return _ubus_connection:call(object, method, data)
656 return _ubus_connection:signatures(object)
658 return _ubus_connection:objects()
662 function serialize_json(x, cb)
663 local js = json.stringify(x)
664 if type(cb) == "function" then
673 return require "nixio.fs".dirname(ldebug.__file__)
676 function checklib(fullpathexe, wantedlib)
677 local fs = require "nixio.fs"
678 local haveldd = fs.access('/usr/bin/ldd')
679 local haveexe = fs.access(fullpathexe)
680 if not haveldd or not haveexe then
683 local libs = exec(string.format("/usr/bin/ldd %s", shellquote(fullpathexe)))
687 for k, v in ipairs(split(libs)) do
688 if v:find(wantedlib) then
696 -- Coroutine safe xpcall and pcall versions modified for Luci
698 -- coxpcall 1.13 - Copyright 2005 - Kepler Project (www.keplerproject.org)
700 -- Copyright © 2005 Kepler Project.
701 -- Permission is hereby granted, free of charge, to any person obtaining a
702 -- copy of this software and associated documentation files (the "Software"),
703 -- to deal in the Software without restriction, including without limitation
704 -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
705 -- and/or sell copies of the Software, and to permit persons to whom the
706 -- Software is furnished to do so, subject to the following conditions:
708 -- The above copyright notice and this permission notice shall be
709 -- included in all copies or substantial portions of the Software.
711 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
712 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
713 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
714 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
715 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
716 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
717 -- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
719 local performResume, handleReturnValue
720 local oldpcall, oldxpcall = pcall, xpcall
722 setmetatable(coxpt, {__mode = "kv"})
724 -- Identity function for copcall
725 local function copcall_id(trace, ...)
729 -- values of either the function or the error handler
730 function coxpcall(f, err, ...)
731 local res, co = oldpcall(coroutine.create, f)
734 local newf = function() return f(unpack(params)) end
735 co = coroutine.create(newf)
737 local c = coroutine.running()
738 coxpt[co] = coxpt[c] or c or 0
740 return performResume(err, co, ...)
743 -- values of the function or the error object
744 function copcall(f, ...)
745 return coxpcall(f, copcall_id, ...)
748 -- Handle return value of protected call
749 function handleReturnValue(err, co, status, ...)
751 return false, err(debug.traceback(co, (...)), ...)
754 if coroutine.status(co) ~= 'suspended' then
758 return performResume(err, co, coroutine.yield(...))
761 -- Resume execution of protected function call
762 function performResume(err, co, ...)
763 return handleReturnValue(err, co, coroutine.resume(co, ...))