5 Several common useful Lua functions
11 Copyright 2008 Steven Barth <steven@midlink.org>
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
17 http://www.apache.org/licenses/LICENSE-2.0
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.
27 -- LuCI utility functions.
28 module("luci.util", package.seeall)
31 -- Class helper routines
34 --- Creates a Class object (Python-style object model)
35 -- Creates a new class object which can be instantiated by calling itself.
36 -- Any class functions or shared parameters can be attached to this object.
37 -- Attaching a table to the class object makes this table shared between
38 -- all instances of this class. For object parameters use the __init__ function.
39 -- Classes can inherit member functions and values from a base class.
40 -- Class can be instantiated by calling them. All parameters will be passed
41 -- to the __init__ function of this class - if such a function exists.
42 -- The __init__ function must be used to set any object parameters that are not shared
43 -- with other objects of this class. Any return values will be ignored.
44 -- @param base The base class to inherit from (optional)
45 -- @return A class object
51 local create = function(class, ...)
53 setmetatable(inst, {__index = class})
56 local stat, err = copcall(inst.__init__, inst, ...)
65 local classmeta = {__call = create}
68 classmeta.__index = base
71 setmetatable(class, classmeta)
75 --- Test whether the given object is an instance of the given class.
76 -- @param object Object instance
77 -- @param class Class object to test against
78 -- @return Boolean indicating wheather the object is an instance
81 function instanceof(object, class)
82 local meta = getmetatable(object)
83 while meta and meta.__index do
84 if meta.__index == class then
87 meta = getmetatable(meta.__index)
94 -- Scope manipulation routines
97 --- Replace a function scope with a shallow copy of itself.
98 -- This is useful if you want to get rid of several unwanted side effects
99 -- while changing the scope of a certain Lua function.
100 -- @param f Lua function
102 setfenv(f, clone(getfenv(f)))
105 --- Store given object associated with given key in the scope of a function.
106 -- @param f Lua function
107 -- @param key String value containg the key of the object to store
108 -- @param obj Object to store in the scope
109 -- @return Always nil
112 function extfenv(f, key, obj)
113 local scope = getfenv(f)
117 --- Extend the scope of a function with the contents of a table
118 -- @param f Lua function
119 -- @param key String value containg the key of the object to store
120 -- @param obj Object to store in the scope
121 -- @return Always nil
124 function updfenv(f, extscope)
125 update(getfenv(f), extscope)
128 --- Create a new or get an already existing thread local store associated with
129 -- the current active coroutine. A thread local store is private a table object
130 -- whose values can't be accessed from outside of the running coroutine.
131 -- @return Table value representing the corresponding thread local store
132 function threadlocal()
135 local function get(self, key)
136 local c = coroutine.running()
137 local thread = coxpt[c] or c or 0
138 if not rawget(self, thread) then
141 return rawget(self, thread)[key]
144 local function set(self, key, value)
145 local c = coroutine.running()
146 local thread = coxpt[c] or c or 0
147 if not rawget(self, thread) then
148 rawset(self, thread, {})
150 rawget(self, thread)[key] = value
153 setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"})
160 -- Debugging routines
163 --- Write given object to stderr.
164 -- @param obj Value to write to stderr
165 -- @return Boolean indicating wheather the write operation was successful
167 return io.stderr:write(tostring(obj) .. "\n")
170 --- Recursively dumps a table to stdout, useful for testing and debugging.
171 -- @param t Table value to dump
172 -- @param i Number of tabs to prepend to each line
173 -- @return Always nil
174 function dumptable(t, i)
176 for k,v in pairs(t) do
177 print(string.rep("\t", i) .. tostring(k), tostring(v))
178 if type(v) == "table" then
186 -- String and data manipulation routines
189 --- Escapes all occurences of the given character in given string.
190 -- @param s String value containing unescaped characters
191 -- @param c String value with character to escape (optional, defaults to "\")
192 -- @return String value with each occurence of character escaped with "\"
193 function escape(s, c)
195 return s:gsub(c, "\\" .. c)
198 --- Create valid XML PCDATA from given string.
199 -- @param value String value containing the data to escape
200 -- @return String value containing the escaped data
201 function pcdata(value)
202 value = value:gsub("&", "&")
203 value = value:gsub('"', """)
204 value = value:gsub("'", "'")
205 value = value:gsub("<", "<")
206 return value:gsub(">", ">")
209 --- Splits given string on a defined seperator sequence and return a table
210 -- containing the resulting substrings. The optional max parameter specifies
211 -- the number of bytes to process, regardless of the actual length of the given
212 -- string. The optional last parameter, regex, sepcifies wheather the separator
213 -- sequence is interpreted as regular expression.
214 -- @param str String value containing the data to split up
215 -- @param pat String with separator pattern (optional, defaults to "\n")
216 -- @param max Num of bytes to process (optional, default is string length)
217 -- @param regexp Boolean indicating wheather to interprete the separator
218 -- pattern as regular expression (optional, default is false)
219 -- @return Table containing the resulting substrings
220 function split(str, pat, max, regex)
240 local s, e = str:find(pat, c, not regex)
242 if s and max < 0 then
243 table.insert(t, str:sub(c))
245 table.insert(t, str:sub(c, s and s - 1))
247 c = e and e + 1 or #str + 1
248 until not s or max < 0
253 --- Remove leading and trailing whitespace from given string value.
254 -- @param str String value containing whitespace padded data
255 -- @return String value with leading and trailing space removed
257 local s = str:gsub("^%s*(.-)%s*$", "%1")
261 --- Parse certain units from the given string and return the canonical integer
262 -- value or 0 if the unit is unknown. Upper- or lowercase is irrelevant.
263 -- Recognized units are:
264 -- o "y" - one year (60*60*24*366)
265 -- o "m" - one month (60*60*24*31)
266 -- o "w" - one week (60*60*24*7)
267 -- o "d" - one day (60*60*24)
268 -- o "h" - one hour (60*60)
269 -- o "min" - one minute (60)
270 -- o "kb" - one kilobyte (1024)
271 -- o "mb" - one megabyte (1024*1024)
272 -- o "gb" - one gigabyte (1024*1024*1024)
273 -- o "kib" - one si kilobyte (1000)
274 -- o "mib" - one si megabyte (1000*1000)
275 -- o "gib" - one si gigabyte (1000*1000*1000)
276 -- @param ustr String containing a numerical value with trailing unit
277 -- @return Number containing the canonical value
278 function parse_units(ustr)
285 y = 60 * 60 * 24 * 366,
286 m = 60 * 60 * 24 * 31,
287 w = 60 * 60 * 24 * 7,
295 gb = 1024 * 1024 * 1024,
297 -- storage sizes (si)
300 gib = 1000 * 1000 * 1000
303 -- parse input string
304 for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do
306 local num = spec:gsub("[^0-9%.]+$","")
307 local spn = spec:gsub("^[0-9%.]+", "")
309 if map[spn] or map[spn:sub(1,1)] then
310 val = val + num * ( map[spn] or map[spn:sub(1,1)] )
320 --- Combines two or more numerically indexed tables into one.
321 -- @param tbl1 Table value to combine
322 -- @param tbl2 Table value to combine
323 -- @param tblN More values to combine
324 -- @return Table value containing all values of given tables
325 function combine(...)
327 for i, a in ipairs(arg) do
328 for j, v in ipairs(a) do
329 table.insert(result, v)
335 --- Checks whether the given table contains the given value.
336 -- @param table Table value
337 -- @param value Value to search within the given table
338 -- @return Boolean indicating wheather the given value occurs within table
339 function contains(table, value)
340 for k, v in pairs(table) do
348 --- Update values in given table with the values from the second given table.
349 -- Both table are - in fact - merged together.
350 -- @param t Table which should be updated
351 -- @param updates Table containing the values to update
352 -- @return Always nil
353 function update(t, updates)
354 for k, v in pairs(updates) do
359 --- Clones the given object and return it's copy.
360 -- @param object Table value to clone
361 -- @param deep Boolean indicating wheather to do recursive cloning
362 -- @return Cloned table value
363 function clone(object, deep)
366 for k, v in pairs(object) do
367 if deep and type(v) == "table" then
373 setmetatable(copy, getmetatable(object))
380 -- Byte code manipulation routines
383 --- Return the current runtime bytecode of the given function. The byte code
384 -- will be stripped before it is returned.
385 -- @param f Function value to return as bytecode
386 -- @return String value containing the bytecode of the given function
387 function get_bytecode(f)
388 local d = string.dump(f)
389 return d and strip_bytecode(d)
392 --- Strips unnescessary lua bytecode from given string. Information like line
393 -- numbers and debugging numbers will be discarded. Original version by
394 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
395 -- @param code String value containing the original lua byte code
396 -- @return String value containing the stripped lua byte code
397 function strip_bytecode(code)
398 local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
401 subint = function(code, i, l)
404 val = val * 256 + code:byte(i + n - 1)
409 subint = function(code, i, l)
412 val = val * 256 + code:byte(i + n - 1)
419 strip_function = function(code)
420 local count, offset = subint(code, 1, size)
421 local stripped, dirty = string.rep("\0", size), offset + count
422 offset = offset + count + int * 2 + 4
423 offset = offset + int + subint(code, offset, int) * ins
424 count, offset = subint(code, offset, int)
427 t, offset = subint(code, offset, 1)
431 offset = offset + size + subint(code, offset, size)
433 offset = offset + num
434 elseif t == 254 or t == 9 then
435 offset = offset + lnum
438 count, offset = subint(code, offset, int)
439 stripped = stripped .. code:sub(dirty, offset - 1)
441 local proto, off = strip_function(code:sub(offset, -1))
442 stripped, offset = stripped .. proto, offset + off - 1
444 offset = offset + subint(code, offset, int) * int + int
445 count, offset = subint(code, offset, int)
447 offset = offset + subint(code, offset, size) + size + int * 2
449 count, offset = subint(code, offset, int)
451 offset = offset + subint(code, offset, size) + size
453 stripped = stripped .. string.rep("\0", int * 3)
454 return stripped, offset
457 return code:sub(1,12) .. strip_function(code:sub(13,-1))
462 -- Sorting iterator functions
465 function _sortiter( t, f )
468 for k, v in pairs(t) do
469 table.insert( keys, k )
473 local _len = table.getn( keys )
475 table.sort( keys, f )
480 return keys[_pos], t[keys[_pos]]
485 --- Return a key, value iterator which returns the values sorted according to
486 -- the provided callback function.
487 -- @param t The table to iterate
488 -- @param f A callback function to decide the order of elements
489 -- @return Function value containing the corresponding iterator
491 return _sortiter( t, f )
494 --- Return a key, value iterator for the given table.
495 -- The table pairs are sorted by key.
496 -- @param t The table to iterate
497 -- @return Function value containing the corresponding iterator
499 return _sortiter( t )
502 --- Return a key, value iterator for the given table.
503 -- The table pairs are sorted by value.
504 -- @param t The table to iterate
505 -- @return Function value containing the corresponding iterator
507 return _sortiter( t, function (a,b) return t[a] < t[b] end )
512 -- Coroutine safe xpcall and pcall versions modified for Luci
514 -- coxpcall 1.13 - Copyright 2005 - Kepler Project (www.keplerproject.org)
517 local performResume, handleReturnValue
518 local oldpcall, oldxpcall = pcall, xpcall
520 setmetatable(coxpt, {__mode = "kv"})
522 --- Identity function for copcall
523 local function copcall_id(trace, ...)
527 --- This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function
528 -- @param f Lua function to be called protected
529 -- @param err Custom error handler
530 -- @param ... Parameters passed to the function
531 -- @return A boolean whether the function call succeeded and the return
532 -- values of either the function or the error handler
533 function coxpcall(f, err, ...)
534 local res, co = oldpcall(coroutine.create, f)
537 local newf = function() return f(unpack(params)) end
538 co = coroutine.create(newf)
540 local c = coroutine.running()
541 coxpt[co] = coxpt[c] or c or 0
543 return performResume(err, co, ...)
546 --- This is a coroutine-safe drop-in replacement for Lua's "pcall"-function
547 -- @param f Lua function to be called protected
548 -- @param ... Parameters passed to the function
549 -- @return A boolean whether the function call succeeded and the returns
550 -- values of the function or the error object
551 function copcall(f, ...)
552 return coxpcall(f, copcall_id, ...)
555 --- Handle return value of protected call
556 function handleReturnValue(err, co, status, ...)
558 return false, err(debug.traceback(co, (...)), ...)
560 if coroutine.status(co) == 'suspended' then
561 return performResume(err, co, coroutine.yield(...))
567 --- Resume execution of protected function call
568 function performResume(err, co, ...)
569 return handleReturnValue(err, co, coroutine.resume(co, ...))