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))
410 -- Serialize the contents of a table value.
411 function _serialize_table(t, seen)
412 assert(not seen[t], "Recursion detected.")
419 for k, v in pairs(t) do
420 if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then
421 k = serialize_data(k, seen)
422 v = serialize_data(v, seen)
423 data = data .. ( #data > 0 and ", " or "" ) ..
424 '[' .. k .. '] = ' .. v
431 local v = serialize_data(t[i], seen)
432 idata = idata .. ( #idata > 0 and ", " or "" ) .. v
435 return idata .. ( #data > 0 and #idata > 0 and ", " or "" ) .. data
438 -- with loadstring().
439 function serialize_data(val, seen)
440 seen = seen or setmetatable({}, {__mode="k"})
444 elseif type(val) == "number" then
446 elseif type(val) == "string" then
448 elseif type(val) == "boolean" then
449 return val and "true" or "false"
450 elseif type(val) == "function" then
451 return "loadstring(%q)" % get_bytecode(val)
452 elseif type(val) == "table" then
453 return "{ " .. _serialize_table(val, seen) .. " }"
455 return '"[unhandled data type:' .. type(val) .. ']"'
459 function restore_data(str)
460 return loadstring("return " .. str)()
465 -- Byte code manipulation routines
468 -- will be stripped before it is returned.
469 function get_bytecode(val)
472 if type(val) == "function" then
473 code = string.dump(val)
475 code = string.dump( loadstring( "return " .. serialize_data(val) ) )
478 return code -- and strip_bytecode(code)
481 -- numbers and debugging numbers will be discarded. Original version by
482 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
483 function strip_bytecode(code)
484 local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
487 subint = function(code, i, l)
490 val = val * 256 + code:byte(i + n - 1)
495 subint = function(code, i, l)
498 val = val * 256 + code:byte(i + n - 1)
504 local function strip_function(code)
505 local count, offset = subint(code, 1, size)
506 local stripped = { string.rep("\0", size) }
507 local dirty = offset + count
508 offset = offset + count + int * 2 + 4
509 offset = offset + int + subint(code, offset, int) * ins
510 count, offset = subint(code, offset, int)
513 t, offset = subint(code, offset, 1)
517 offset = offset + size + subint(code, offset, size)
519 offset = offset + num
520 elseif t == 254 or t == 9 then
521 offset = offset + lnum
524 count, offset = subint(code, offset, int)
525 stripped[#stripped+1] = code:sub(dirty, offset - 1)
527 local proto, off = strip_function(code:sub(offset, -1))
528 stripped[#stripped+1] = proto
529 offset = offset + off - 1
531 offset = offset + subint(code, offset, int) * int + int
532 count, offset = subint(code, offset, int)
534 offset = offset + subint(code, offset, size) + size + int * 2
536 count, offset = subint(code, offset, int)
538 offset = offset + subint(code, offset, size) + size
540 stripped[#stripped+1] = string.rep("\0", int * 3)
541 return table.concat(stripped), offset
544 return code:sub(1,12) .. strip_function(code:sub(13,-1))
549 -- Sorting iterator functions
552 function _sortiter( t, f )
556 for k, v in pairs(t) do
562 table.sort( keys, f )
566 if _pos <= #keys then
567 return keys[_pos], t[keys[_pos]], _pos
572 -- the provided callback function.
574 return _sortiter( t, f )
577 -- The table pairs are sorted by key.
579 return _sortiter( t )
582 -- The table pairs are sorted by value.
584 return _sortiter( t, function (a,b) return t[a] < t[b] end )
589 -- System utility functions
593 return string.byte(string.dump(function() end), 7) == 0
596 function exec(command)
597 local pp = io.popen(command)
598 local data = pp:read("*a")
604 function execi(command)
605 local pp = io.popen(command)
607 return pp and function()
608 local line = pp:read()
619 function execl(command)
620 local pp = io.popen(command)
626 if (line == nil) then break end
648 function ubus(object, method, data)
649 if not _ubus_connection then
650 _ubus_connection = _ubus.connect()
651 assert(_ubus_connection, "Unable to establish ubus connection")
654 if object and method then
655 if type(data) ~= "table" then
658 local rv, err = _ubus_connection:call(object, method, data)
659 return rv, err, ubus_codes[err]
661 return _ubus_connection:signatures(object)
663 return _ubus_connection:objects()
667 function serialize_json(x, cb)
668 local js = json.stringify(x)
669 if type(cb) == "function" then
678 return require "nixio.fs".dirname(ldebug.__file__)
681 function checklib(fullpathexe, wantedlib)
682 local fs = require "nixio.fs"
683 local haveldd = fs.access('/usr/bin/ldd')
684 local haveexe = fs.access(fullpathexe)
685 if not haveldd or not haveexe then
688 local libs = exec(string.format("/usr/bin/ldd %s", shellquote(fullpathexe)))
692 for k, v in ipairs(split(libs)) do
693 if v:find(wantedlib) then
701 -- Coroutine safe xpcall and pcall versions modified for Luci
703 -- coxpcall 1.13 - Copyright 2005 - Kepler Project (www.keplerproject.org)
705 -- Copyright © 2005 Kepler Project.
706 -- Permission is hereby granted, free of charge, to any person obtaining a
707 -- copy of this software and associated documentation files (the "Software"),
708 -- to deal in the Software without restriction, including without limitation
709 -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
710 -- and/or sell copies of the Software, and to permit persons to whom the
711 -- Software is furnished to do so, subject to the following conditions:
713 -- The above copyright notice and this permission notice shall be
714 -- included in all copies or substantial portions of the Software.
716 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
717 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
718 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
719 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
720 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
721 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
722 -- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
724 local performResume, handleReturnValue
725 local oldpcall, oldxpcall = pcall, xpcall
727 setmetatable(coxpt, {__mode = "kv"})
729 -- Identity function for copcall
730 local function copcall_id(trace, ...)
734 -- values of either the function or the error handler
735 function coxpcall(f, err, ...)
736 local res, co = oldpcall(coroutine.create, f)
739 local newf = function() return f(unpack(params)) end
740 co = coroutine.create(newf)
742 local c = coroutine.running()
743 coxpt[co] = coxpt[c] or c or 0
745 return performResume(err, co, ...)
748 -- values of the function or the error object
749 function copcall(f, ...)
750 return coxpcall(f, copcall_id, ...)
753 -- Handle return value of protected call
754 function handleReturnValue(err, co, status, ...)
756 return false, err(debug.traceback(co, (...)), ...)
759 if coroutine.status(co) ~= 'suspended' then
763 return performResume(err, co, coroutine.yield(...))
766 -- Resume execution of protected function call
767 function performResume(err, co, ...)
768 return handleReturnValue(err, co, coroutine.resume(co, ...))