diff options
author | Ted Trask <ttrask01@yahoo.com> | 2013-10-17 19:03:48 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2013-10-17 19:03:48 +0000 |
commit | d60a739470dc0d46f5306033b17c1dd27ddc37fc (patch) | |
tree | b59f28794de88f78edf2a4b3c381d5aed2a32550 | |
parent | 75117b54d8a43b94b7a6ff797cce06af1647a7aa (diff) | |
download | acf-lib-d60a739470dc0d46f5306033b17c1dd27ddc37fc.tar.bz2 acf-lib-d60a739470dc0d46f5306033b17c1dd27ddc37fc.tar.xz |
Remove all calls to 'module' in preparation for move to Lua 5.2
Use mymodule parameter for module definition.
This was also helpful in revealing places where the code relied on the global environment.
-rw-r--r-- | apk.lua | 12 | ||||
-rw-r--r-- | date.lua | 78 | ||||
-rw-r--r-- | db.lua | 26 | ||||
-rw-r--r-- | format.lua | 54 | ||||
-rw-r--r-- | fs.lua | 82 | ||||
-rw-r--r-- | html.lua | 72 | ||||
-rw-r--r-- | processinfo.lua | 21 | ||||
-rw-r--r-- | validator.lua | 28 |
8 files changed, 194 insertions, 179 deletions
@@ -1,9 +1,9 @@ -- apk library -module (..., package.seeall) +local mymodule = {} -subprocess = require("subprocess") +mymodule.subprocess = require("subprocess") -delete = function(package) +mymodule.delete = function(package) local success = false local code, cmdresult = subprocess.call_capture({"apk", "del", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^OK") then @@ -14,7 +14,7 @@ delete = function(package) return success, cmdresult end -install = function(package) +mymodule.install = function(package) local success = true local code, cmdresult = subprocess.call_capture({"apk", "add", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^ERROR") then @@ -23,10 +23,12 @@ install = function(package) return success, cmdresult end -version = function(package) +mymodule.version = function(package) local code, cmdresult = subprocess.call_capture({"apk", "info", "-ve", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^%s*$") then cmdresult = nil end return cmdresult end + +return mymodule @@ -1,6 +1,6 @@ --date and time functions -module(..., package.seeall) +local mymodule = {} posix = require("posix") format = require("acf.format") @@ -9,9 +9,9 @@ fs = require("acf.fs") --global for date formating see below for more information --Mon Nov 26 19:56:10 UTC 2007 looks like most systems use this --print(os.date(date.format)) -formats = "%a %b %d %X %Z %Y" +mymodule.formats = "%a %b %d %X %Z %Y" -months ={ {"January","Jan"}, +mymodule.months ={ {"January","Jan"}, {"February", "Feb"}, {"March","Mar"}, {"April", "Apr"}, @@ -25,7 +25,7 @@ months ={ {"January","Jan"}, {"December","Dec"} } -revmonths = {["january"] = 1, ["jan"] = 1, +mymodule.revmonths = {["january"] = 1, ["jan"] = 1, ["february"] = 2, ["feb"] = 2, ["march"] = 3, ["mar"] = 3, ["april"] = 4, ["apr"] = 4, @@ -39,7 +39,7 @@ revmonths = {["january"] = 1, ["jan"] = 1, ["december"] = 12, ["dec"] = 12 } -dow = { {"Sunday","Sun"}, +mymodule.dow = { {"Sunday","Sun"}, {"Monday","Mon"}, {"Tuesday","Tue"}, {"Wednesday","Wed"}, @@ -48,7 +48,7 @@ dow = { {"Sunday","Sun"}, {"Saturday","Sat"} } -revdow = { ["sunday"] = 1, ["sun"] = 2, +mymodule.revdow = { ["sunday"] = 1, ["sun"] = 2, ["monday"] = 2, ["mon"] = 2, ["tuesday"] = 3, ["tue"] = 3, ["wednesday"] = 4, ["wed"] = 4, @@ -62,7 +62,7 @@ revdow = { ["sunday"] = 1, ["sun"] = 2, --this list is not full. May need some more added. No Africa or Asia --Abrr TZ,Real Offset, FullName, Location, What would be put in /etc/TZ(busybox needed offset) -timezones = { +mymodule.timezones = { {"A","+1","Alpha Time Zone","Military","Alpha-1"}, {"ACDT","+10:30","Australian Central Daylight Time","Australia","ACDT-10:30"}, @@ -184,7 +184,7 @@ timezones = { --t = { {year=2007,month=1,day=2,hour=2}, {year=2006,month=1,day=5} } --will return a table sorted by oldest <-> newest --to grab the largest and smallest a,b=g[1],g[table.maxn(g)] -function date_to_seconds (t) +function mymodule.date_to_seconds (t) g = {} count = table.maxn(t) for i = 1,count do @@ -198,11 +198,11 @@ end --format can be changed. This seems to be standard, dow,mon,dom,time,zone,year -- seems like %z- +0000 time zone format and %Z- 3 letter timezone undocumented or new -function seconds_to_date (t) +function mymodule.seconds_to_date (t) g = {} count = table.maxn(t) for i = 1,count do - g[#g+1] = os.date(formats,t[i]) + g[#g+1] = os.date(mymodule.formats,t[i]) end return g @@ -211,11 +211,11 @@ end --Wed Nov 28 14:01:23 UTC 2007 --os.date(date.formats) put into a table --year,month,day,hour,min,sec,isdst- may need a dst table to set this automatically -function string_to_table (str) - if str == nil then str = os.date(formats) end +function mymodule.string_to_table (str) + if str == nil then str = os.date(mymodule.formats) end g = {} temp = format.string_to_table(str,"%s") - month = abr_month_num(temp[2]) + month = mymodule.abr_month_num(temp[2]) g["month"] = month day = temp[3] g["day"] = day @@ -239,20 +239,20 @@ end --gives a table back with hour,min,month,sec,day,year to display something like --you have 10 years, 14 hours, 10 days to renew you certificate -- in secs - year, day, hour,min,sec -t_time = { field_names = {"years","days","hours","minutes","seconds"}, +mymodule.t_time = { field_names = {"years","days","hours","minutes","seconds"}, 31556926,86400,3600,60,1 } -function date_diff (d1, d2) +function mymodule.date_diff (d1, d2) g = {} if d2 == nil then d2 = os.time() end --first sum of seconds sum = math.abs(os.difftime(d1,d2)) --going to go through and get it smaller with each pass through the table - for a,b in ipairs(t_time) do + for a,b in ipairs(mymodule.t_time) do print(sum) hold = math.modf(sum/b) - g[t_time.field_names[a]] = hold + g[mymodule.t_time.field_names[a]] = hold sum = (sum - (hold*b)) end @@ -261,43 +261,43 @@ end --give a search number and return the month name -function num_month_name (search) - return months[search][1] +function mymodule.num_month_name (search) + return mymodule.months[search][1] end --give a search number and return the month abr -function num_month_name_abr (search) - return months[search][2] +function mymodule.num_month_name_abr (search) + return mymodule.months[search][2] end -function name_month_num (search) - return revmonths[string.lower(search)] +function mymodule.name_month_num (search) + return mymodule.revmonths[string.lower(search)] end -function abr_month_num (search) - return revmonths[string.lower(search)] +function mymodule.abr_month_num (search) + return mymodule.revmonths[string.lower(search)] end -function num_dow_name (search) - return dow[search][1] +function mymodule.num_dow_name (search) + return mymodule.dow[search][1] end -function num_dow_name_abr (search) - return dow[search][2] +function mymodule.num_dow_name_abr (search) + return mymodule.dow[search][2] end -function name_dow_num (search) - return revdow[string.lower(search)] +function mymodule.name_dow_num (search) + return mymodule.revdow[string.lower(search)] end -function abr_dow_num (search) - return revdow[string.lower(search)] +function mymodule.abr_dow_num (search) + return mymodule.revdow[string.lower(search)] end --tell me what TimeZone my system is set to -function what_tz () +function mymodule.what_tz () f = fs.read_file_as_array("/etc/TZ") or {} local tz = f[1] return tz @@ -305,15 +305,15 @@ end --change the timezone my system is set to -function change_tz ( tz ) +function mymodule.change_tz ( tz ) --give us something like CET-1, this is busy box offset need to fix. tz = string.gsub(tz, "%+", "%%+") tz = string.gsub(tz, "%-", "%%-") tz = "^" .. tz .. "$" result = {} - for a=1,table.maxn(date.timezones) do - c = string.match(date.timezones[a][5], tz) + for a=1,table.maxn(mymodule.timezones) do + c = string.match(mymodule.timezones[a][5], tz) if c ~= nil then result[#result +1] = c end end @@ -324,5 +324,7 @@ function change_tz ( tz ) mess = "Too many matches." end - return mess,date.what_tz() + return mess,mymodule.what_tz() end + +return mymodule @@ -1,4 +1,4 @@ -module(..., package.seeall) +local mymodule = {} subprocess = require("subprocess") @@ -24,10 +24,10 @@ end export.databaseconnect = function(dbobject) if not dbobject.con then -- create environment object - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then luasql = require("luasql.postgres") dbobject.env = assert (luasql.postgres()) - elseif dbobject.engine == engine.sqlite3 then + elseif dbobject.engine == mymodule.engine.sqlite3 then luasql = require("luasql.sqlite3") dbobject.env = assert (luasql.sqlite3()) else @@ -64,9 +64,9 @@ export.runsqlcommand = function(dbobject, sql, transaction) if not res and err then -- Catch the error to see if it's caused by lack of table local table - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then table = string.match(err, "relation \"(%S+)\" does not exist") - elseif dbobject.engine == engine.sqlite3 then + elseif dbobject.engine == mymodule.engine.sqlite3 then table = string.match(err, "LuaSQL: no such table: (%S+)") end if table and dbobject.table_creation_scripts[table] then @@ -105,9 +105,9 @@ export.getselectresponse = function(dbobject, sql, transaction) if not res and err then -- Catch the error to see if it's caused by lack of table local table - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then table = string.match(err, "relation \"(%S+)\" does not exist") - elseif dbobject.engine == engine.sqlite3 then + elseif dbobject.engine == mymodule.engine.sqlite3 then table = string.match(err, "LuaSQL: no such table: (%S+)") end if table and dbobject.table_creation_scripts[table] then @@ -125,7 +125,7 @@ end export.listtables = function(dbobject) local result = {} - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then local tab = dbobject.getselectresponse("SELECT tablename FROM pg_tables WHERE tablename !~* 'pg_*' ORDER BY tablename ASC") for i,t in ipairs(tab) do result[#result+1] = t.tablename @@ -139,7 +139,7 @@ end export.listcolumns = function(dbobject, table) local result = {} - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then local col = dbobject.getselectresponse("SELECT a.attname AS field FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = '"..dbobject.escape(table).."' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum") for i,c in ipairs(col) do result[#result+1] = c.field @@ -150,7 +150,7 @@ end export.listdatabases = function(dbobject) local result = {} - if dbobject.engine == engine.postgresql then + if dbobject.engine == mymodule.engine.postgresql then local cmd = {"psql", "-U", "postgres", "-lt"} if dbobject.host then cmd[#cmd+1] = "-h" @@ -178,15 +178,17 @@ end -- ################################################################################ -- PUBLIC FUNCTIONS / DEFINITIONS -engine = { +mymodule.engine = { ["postgresql"] = 1, ["sqlite3"] = 2, } -create = function(engine, database, user, password, host, port) +mymodule.create = function(engine, database, user, password, host, port) local dbobject = {engine=engine, database=database, user=user, password=password, host=host, port=port} for n,f in pairs(export) do dbobject[n] = function(...) return f(dbobject, ...) end end return dbobject end + +return mymodule @@ -3,30 +3,30 @@ try to keep non input specific ]]-- -module (..., package.seeall) +local mymodule = {} -- find all return characters and removes them, may get this from a browser -- that is why didn't do file specific -function dostounix ( str ) +function mymodule.dostounix ( str ) local data = string.gsub(str, "\r", "") return data end -- Escape Lua magic characters -function escapemagiccharacters ( str ) +function mymodule.escapemagiccharacters ( str ) return (string.gsub(str or "", "[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")) end -- Escape shell special characters -function escapespecialcharacters ( str ) +function mymodule.escapespecialcharacters ( str ) return (string.gsub(str or "", "[~`#%$&%*%(%)\\|%[%]{};\'\"<>/\n\r]", "\\%1")) end -- search and remove all blank and commented lines from a string or table of lines -- returns a table to iterate over without the blank or commented lines -function parse_lines ( input, comment ) +function mymodule.parse_lines ( input, comment ) local lines = {} comment = comment or "#" @@ -53,7 +53,7 @@ end -- parse the lines for words, looking for quotes and removing comments -- returns a table with an array of words for each line -function parse_linesandwords ( input, comment ) +function mymodule.parse_linesandwords ( input, comment ) local lines = {} local linenum = 0 comment = comment or "#" @@ -96,9 +96,9 @@ end -- returns a table with label value pairs -function parse_configfile( input, comment ) +function mymodule.parse_configfile( input, comment ) local config = {} - local lines = parse_linesandwords(input, comment) + local lines = mymodule.parse_linesandwords(input, comment) for i,linetable in ipairs(lines) do config[linetable[1]] = table.concat(linetable, " ", 2) or "" @@ -109,7 +109,7 @@ end -- search and replace through a table -- string is easy string.gsub(string, find, replace) -function search_replace (input, find, replace) +function mymodule.search_replace (input, find, replace) local lines = {} for i,line in ipairs(input) do lines[#lines + 1] = string.gsub(line, find, replace) @@ -124,7 +124,7 @@ end -- say want all the _OPTS from a file format.search_for_lines (fs.read_file("/etc/conf.d/cron"), "OPT") -- if want to avoid commented lines, call parse_lines first -function search_for_lines (input, find) +function mymodule.search_for_lines (input, find) local lines = {} function findfn(line) @@ -147,7 +147,7 @@ function search_for_lines (input, find) end --string format function to capitalize the beginging of each word. -function cap_begin_word ( str ) +function mymodule.cap_begin_word ( str ) --first need to do the first word local data = string.gsub(str, "^%l", string.upper) --word is any space cause no <> regex @@ -161,7 +161,7 @@ end -- This code comes from http://lua-users.org/wiki/SplitJoin -- example: format.string_to_table( "Anna, Bob, Charlie,Dolores", ",%s*") -function string_to_table ( text, delimiter) +function mymodule.string_to_table ( text, delimiter) local list = {} if text then -- this would result in endless loops @@ -190,7 +190,7 @@ end -- Takes a str and expands any ${...} constructs with the Lua variable -- ex: a="foo"; print(expand_bash_syntax_vars("a=${a}) - > "a=foo" -expand_bash_syntax_vars = function (str) +mymodule.expand_bash_syntax_vars = function (str) local deref = function (f) local v = getfenv(3) -- get the upstream global env for w in string.gfind(f, "[%w_]+") do @@ -202,7 +202,7 @@ expand_bash_syntax_vars = function (str) for w in string.gmatch (str, "${[^}]*}" ) do local rvar = string.sub(w,3,-2) local rval = ( deref(rvar) or "nil" ) - str = string.gsub (str, w, escapespecialcharacters(rval)) + str = string.gsub (str, w, mymodule.escapespecialcharacters(rval)) end return (str) @@ -211,7 +211,7 @@ end -- Removes the linenum line from str and replaces it with line. -- Do nothing if doesn't exist -- Set line to nil to remove the line -function replace_line(str, linenum, line) +function mymodule.replace_line(str, linenum, line) -- Split the str to remove the line local startchar, endchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*\n?()") if startchar and endchar then @@ -227,7 +227,7 @@ function replace_line(str, linenum, line) end -- Inserts the line into the str after the linenum (or at the end) -function insert_line(str, linenum, line) +function mymodule.insert_line(str, linenum, line) -- Split the str to remove the line local startchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum) .. "()") local lines = {} @@ -244,7 +244,7 @@ function insert_line(str, linenum, line) return str end -function get_line(str, linenum) +function mymodule.get_line(str, linenum) -- Split the str to remove the line local startchar, endchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*()") local line @@ -255,7 +255,7 @@ function get_line(str, linenum) end -- Search the option string for separate options (-x or --xyz) and put them in a table -function opts_to_table ( optstring, filter ) +function mymodule.opts_to_table ( optstring, filter ) local optsparams if optstring then local optstr = optstring .. " " @@ -271,7 +271,7 @@ function opts_to_table ( optstring, filter ) end -- Go through an options table and create the option string -function table_to_opts ( optsparams ) +function mymodule.table_to_opts ( optsparams ) local optstring = {} for opt,val in pairs(optsparams) do optstring[#optstring + 1] = opt @@ -296,7 +296,7 @@ end -- Try not to touch anything but the value we're interested in (although will combine multi-line into one) -- If the search_section is not found, we'll add it at the end of the string -- If the search_name is not found, we'll add it at the end of the section -function update_ini_file (file, search_section, search_name, value) +function mymodule.update_ini_file (file, search_section, search_name, value) if not file or not search_name or search_name == "" then return file, false end @@ -368,7 +368,7 @@ end -- Parse string for name=value pairs, returned in a table -- If search_section is defined, only report values in matching section -- If search_name is defined, only report matching name (possibly in multiple sections) -function parse_ini_file (file, search_section, search_name) +function mymodule.parse_ini_file (file, search_section, search_name) if not file or file == "" then return nil end @@ -420,7 +420,7 @@ function parse_ini_file (file, search_section, search_name) return opts end -function get_ini_section (file, search_section) +function mymodule.get_ini_section (file, search_section) if not file then return nil end @@ -441,7 +441,7 @@ function get_ini_section (file, search_section) return table.concat(sectionlines, "\n") end -function set_ini_section (file, search_section, section_content) +function mymodule.set_ini_section (file, search_section, section_content) if not file then return file, false end @@ -488,12 +488,12 @@ end -- the file parameter can be a string or structure returned by parse_ini_file -- beginning and ending quotes are removed -- returns value or "" if not found -function get_ini_entry (file, section, value) +function mymodule.get_ini_entry (file, section, value) local opts = file if not file or not value then return nil elseif type(file) == "string" then - opts = parse_ini_file(file) + opts = mymodule.parse_ini_file(file) end section = section or "" local result @@ -510,10 +510,12 @@ function get_ini_entry (file, section, value) end while string.find(result, "%$[%w_]+") do local sub = string.match(result, "%$[%w_]+") - result = string.gsub(result, escapemagiccharacters(sub), get_ini_entry(opts, section, sub)) + result = string.gsub(result, mymodule.escapemagiccharacters(sub), mymodule.get_ini_entry(opts, section, sub)) end if string.find(result, '^"') and string.find(result, '"$') then result = string.sub(result, 2, -2) end return result end + +return mymodule @@ -5,43 +5,43 @@ MM edited to use "posix" ]]-- -module (..., package.seeall) +local mymodule = {} posix = require("posix") format = require("acf.format") -- generic wrapper funcs -function is_dir ( pathstr ) +function mymodule.is_dir ( pathstr ) return posix.stat ( pathstr or "", "type" ) == "directory" end -function is_file ( pathstr ) +function mymodule.is_file ( pathstr ) return posix.stat ( pathstr or "", "type" ) == "regular" end -function is_link ( pathstr ) +function mymodule.is_link ( pathstr ) return posix.stat ( pathstr or "", "type" ) == "link" end -- Creates a directory if it doesn't exist, including the parent dirs -function create_directory ( path ) +function mymodule.create_directory ( path ) local pos = string.find(path, "/") while pos do posix.mkdir(string.sub(path, 1, pos)) pos = string.find(path, "/", pos+1) end posix.mkdir(path) - return is_dir(path) + return mymodule.is_dir(path) end -- Deletes a directory along with its contents -function remove_directory ( path ) - if is_dir(path) then +function mymodule.remove_directory ( path ) + if mymodule.is_dir(path) then for d in posix.files(path) do if (d == ".") or (d == "..") then -- ignore - elseif is_dir(path .. "/" .. d) then - remove_directory(path .. "/" ..d) + elseif mymodule.is_dir(path .. "/" .. d) then + mymodule.remove_directory(path .. "/" ..d) else os.remove(path .. "/" ..d) end @@ -53,16 +53,16 @@ function remove_directory ( path ) end -- Creates a blank file (and the directory if necessary) -function create_file ( path ) +function mymodule.create_file ( path ) path = path or "" - if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end + if not posix.stat(posix.dirname(path)) then mymodule.create_directory(posix.dirname(path)) end local f = io.open(path, "w") if f then f:close() end - return is_file(path) + return mymodule.is_file(path) end -- Copies the permissions and ownership of one file to another (if they exist and are the same type) -function copy_properties(source, dest) +function mymodule.copy_properties(source, dest) if posix.stat(source or "", "type") == posix.stat(dest or "", "type") then local stats = posix.stat(source) posix.chmod(dest, stats.mode) @@ -75,39 +75,39 @@ end -- Copies a file to a directory or new filename (creating the directory if necessary) -- fails if new file is already a directory (this is different than cp function) -- if newpath ends in "/", will treat as a directory -function copy_file(oldpath, newpath) +function mymodule.copy_file(oldpath, newpath) local use_dir = string.find(newpath or "", "/%s*$") - if not is_file(oldpath) or not newpath or newpath == "" or (not use_dir and is_dir(newpath)) or (use_dir and is_dir(newpath .. posix.basename(oldpath))) then + if not mymodule.is_file(oldpath) or not newpath or newpath == "" or (not use_dir and mymodule.is_dir(newpath)) or (use_dir and mymodule.is_dir(newpath .. posix.basename(oldpath))) then return false end if use_dir then newpath = newpath .. posix.basename(oldpath) end - if not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end + if not posix.stat(posix.dirname(newpath)) then mymodule.create_directory(posix.dirname(newpath)) end local old = io.open(oldpath, "r") local new = io.open(newpath, "w") new:write(old:read("*a")) new:close() old:close() - copy_properties(oldpath, newpath) - return is_file(newpath) + mymodule.copy_properties(oldpath, newpath) + return mymodule.is_file(newpath) end -- Moves a file to a directory or new filename (creating the directory if necessary) -- fails if new file is already a directory (this is different than mv function) -- if newpath ends in "/", will treat as a directory -function move_file(oldpath, newpath) +function mymodule.move_file(oldpath, newpath) local use_dir = string.find(newpath or "", "/%s*$") - if not is_file(oldpath) or not newpath or newpath == "" or (not use_dir and is_dir(newpath)) or (use_dir and is_dir(newpath .. posix.basename(oldpath))) then + if not mymodule.is_file(oldpath) or not newpath or newpath == "" or (not use_dir and mymodule.is_dir(newpath)) or (use_dir and mymodule.is_dir(newpath .. posix.basename(oldpath))) then return false end if use_dir then newpath = newpath .. posix.basename(oldpath) end - if not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end + if not posix.stat(posix.dirname(newpath)) then mymodule.create_directory(posix.dirname(newpath)) end local status, errstr, errno = os.rename(oldpath, newpath) -- errno 18 means Invalid cross-device link if status or errno ~= 18 then -- successful move or failure due to something else return (status ~= nil), errstr, errno else - status = copy_file(oldpath, newpath) + status = mymodule.copy_file(oldpath, newpath) if status then os.remove(oldpath) end @@ -116,7 +116,7 @@ function move_file(oldpath, newpath) end -- Returns the contents of a file as a string -function read_file ( path ) +function mymodule.read_file ( path ) local file = io.open(path or "") if ( file ) then local f = file:read("*a") @@ -129,7 +129,7 @@ end -- Returns an array with the contents of a file, -- or nil and the error message -function read_file_as_array ( path ) +function mymodule.read_file_as_array ( path ) local file, error = io.open(path or "") if ( file == nil ) then return nil, error @@ -144,9 +144,9 @@ function read_file_as_array ( path ) end -- write a string to a file, will replace file contents -function write_file ( path, str ) +function mymodule.write_file ( path, str ) path = path or "" - if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end + if not posix.stat(posix.dirname(path)) then mymodule.create_directory(posix.dirname(path)) end local file = io.open(path, "w") --append a newline char to EOF str = string.gsub(str or "", "\n*$", "\n") @@ -158,39 +158,39 @@ end -- this could do more than a line. This will append -- fs.write_line_file ("filename", "Line1 \nLines2 \nLines3") -function write_line_file ( path, str ) +function mymodule.write_line_file ( path, str ) path = path or "" - if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end + if not posix.stat(posix.dirname(path)) then mymodule.create_directory(posix.dirname(path)) end local file = io.open(path) if ( file) then local c = file:read("*a") or "" file:close() - write_file(path, c .. (str or "")) + mymodule.write_file(path, c .. (str or "")) end end -- returns an array of files under "where" that match "what" (a Lua pattern) -function find_files_as_array ( what, where, follow, t ) +function mymodule.find_files_as_array ( what, where, follow, t ) where = where or posix.getcwd() what = what or ".*" t = t or {} local link - if follow and is_link(where) then + if follow and mymodule.is_link(where) then link = posix.readlink(where) if link and not string.find(link, "^/") then link = posix.dirname(where).."/"..link end end - if is_dir(where) or (link and is_dir(link)) then + if mymodule.is_dir(where) or (link and mymodule.is_dir(link)) then for d in posix.files ( where ) do if (d == ".") or ( d == "..") then -- do nothing - elseif is_dir ( where .. "/" .. d ) then - find_files_as_array (what, where .. "/" .. d, follow, t ) - elseif follow and is_link ( where .. "/" .. d ) then - find_files_as_array (what, where .. "/" .. d, follow, t ) + elseif mymodule.is_dir ( where .. "/" .. d ) then + mymodule.find_files_as_array (what, where .. "/" .. d, follow, t ) + elseif follow and mymodule.is_link ( where .. "/" .. d ) then + mymodule.find_files_as_array (what, where .. "/" .. d, follow, t ) elseif (string.match (d, "^" .. what .. "$" )) then table.insert (t, ( string.gsub ( where .. "/" .. d, "/+", "/" ) ) ) end @@ -204,8 +204,8 @@ end -- iterator function for finding dir entries matching (what) (a Lua pattern) -- starting at where, or currentdir if not specified. -function find ( what, where, follow ) - local t = find_files_as_array ( what, where, follow ) +function mymodule.find ( what, where, follow ) + local t = mymodule.find_files_as_array ( what, where, follow ) local idx = 0 return function () idx = idx + 1 @@ -214,7 +214,7 @@ function find ( what, where, follow ) end -- This function does almost the same as posix.stat, but instead it writes the output human readable. -function stat ( path ) +function mymodule.stat ( path ) local filedetails = posix.stat(path or "") if (filedetails) then filedetails["ctime"]=os.date("%c", filedetails["ctime"]) @@ -232,3 +232,5 @@ function stat ( path ) end return filedetails end + +return mymodule @@ -3,14 +3,14 @@ Copyright (C) 2007 Nathan Angelacos Licensed under the terms of GPL2 ]]-- -module (..., package.seeall) +local mymodule = {} --[[ Cookie functions ]]------------------------------------------------------ -cookie={} +mymodule.cookie={} -- Set a cookie - returns a string suitable for setting a cookie -- if the value is the boolean "false", then set the cookie to expire -cookie.set = function ( name, value, path ) +mymodule.cookie.set = function ( name, value, path ) local expires = "" if name == nil then return ("") @@ -22,20 +22,20 @@ cookie.set = function ( name, value, path ) if path == nil then path = "/" end - return (string.format('Set-Cookie: %s=%s; path=%s; %s\n', html_escape(tostring(name)), - html_escape(tostring(value)), html_escape(path), html_escape(expires))) + return (string.format('Set-Cookie: %s=%s; path=%s; %s\n', mymodule.html_escape(tostring(name)), + mymodule.html_escape(tostring(value)), mymodule.html_escape(path), mymodule.html_escape(expires))) end -- wrapper function to clear a cookie -cookie.unset = function ( name, path) - return cookie.set (name, false, path) +mymodule.cookie.unset = function ( name, path) + return mymodule.cookie.set (name, false, path) end -- escape unsafe html characters -function html_escape (text ) +function mymodule.html_escape (text ) text = text or "" local str = string.gsub (text, "&", "&" ) str = string.gsub (str, "<", "<" ) @@ -57,7 +57,7 @@ local nv_pair = function ( name, value) if ( value == nil ) then return ( "" ) else - return (string.format (' %s="%s" ', html_escape(name) , html_escape(value) )) + return (string.format (' %s="%s" ', mymodule.html_escape(name) , mymodule.html_escape(value) )) end end @@ -89,7 +89,7 @@ local generic_input = function ( field_type, v ) return nil end - local str = string.format ( '<input class="%s %s" type="%s" ', html_escape(v.class), html_escape(field_type), html_escape(field_type) ) + local str = string.format ( '<input class="%s %s" type="%s" ', mymodule.html_escape(v.class), mymodule.html_escape(field_type), mymodule.html_escape(field_type) ) for i,k in ipairs ( { "name", "size", "checked", "maxlength", @@ -111,13 +111,13 @@ end --[[ Form functions ]]------------------------------------------------------ -- These expect something like a cfe to work (see mvc.lua) -form = {} -form.text = function ( v ) +mymodule.form = {} +mymodule.form.text = function ( v ) return generic_input ( "text", v ) end -form.longtext = function ( v ) +mymodule.form.longtext = function ( v ) local str = "<textarea" for i,k in ipairs ( { "name", "rows", "cols", @@ -127,33 +127,33 @@ form.longtext = function ( v ) str = str .. nv_pair ( k, v[k] ) end str = str .. nv_pair (nil, v.disabled) - return ( str .. ">" .. html_escape(v.value) .. "</textarea>" ) + return ( str .. ">" .. mymodule.html_escape(v.value) .. "</textarea>" ) end -function form.password ( v ) +function mymodule.form.password ( v ) return generic_input ( "password", v ) end -function form.hidden ( v ) +function mymodule.form.hidden ( v ) return generic_input ( "hidden", v ) end -function form.submit ( v ) +function mymodule.form.submit ( v ) return generic_input ( "submit", v ) end -function form.action (v) +function mymodule.form.action (v) return generic_input ("submit", v) end -function form.file ( v ) +function mymodule.form.file ( v ) return generic_input ( "file", v ) end -function form.image ( v ) +function mymodule.form.image ( v ) return generic_input ( "image", v ) end @@ -161,7 +161,7 @@ end -- v.value is the selected item (or an array if multiple) -- v.option is an array of valid options (or an array of value, label) -- NOTE use of value and values (plural) -function form.select ( v ) +function mymodule.form.select ( v ) if ( v.name == nil ) then return nil end @@ -201,22 +201,22 @@ function form.select ( v ) str = str .. " selected" reverseval[val] = nil end - str = str .. nv_pair("value", val) .. ">" .. html_escape(label) .. "</option>" + str = str .. nv_pair("value", val) .. ">" .. mymodule.html_escape(label) .. "</option>" end for val in pairs(reverseval) do - str = str .. '<option selected value="' .. html_escape(val) ..'">[' .. html_escape(val) .. ']</option>' + str = str .. '<option selected value="' .. mymodule.html_escape(val) ..'">[' .. mymodule.html_escape(val) .. ']</option>' end str = str .. "</select>" return (str) end -function form.checkbox ( v ) +function mymodule.form.checkbox ( v ) return generic_input ( "checkbox", v ) end -- NOTE: VALUE of a form is a table containing the form elements ... -function form.start ( v) +function mymodule.form.start ( v) if ( v.action == nil ) then return nil end @@ -224,30 +224,30 @@ function form.start ( v) local method = v.method or "get" return ( string.format ( '<form %s%s%s>', - nv_pair ( "class", html_escape(v.class) ), - nv_pair ( "method", html_escape(v.method) ), - nv_pair ( "action", html_escape(v.action) ) + nv_pair ( "class", mymodule.html_escape(v.class) ), + nv_pair ( "method", mymodule.html_escape(v.method) ), + nv_pair ( "action", mymodule.html_escape(v.action) ) ) ) end -function form.stop ( ) +function mymodule.form.stop ( ) return ("</form>") end -- For "h1, h2, p," etc -- WARNING - Text is printed verbatim - you may want to --- wrap the text in html_escape -function entity (tag, text, class, id) +-- wrap the text in mymodule.html_escape +function mymodule.entity (tag, text, class, id) return ( string.format ( "<%s%s%s>%s</%s>", - html_escape(tag), + mymodule.html_escape(tag), nv_pair ("class", class), - nv_pair("id", id), html_escape(text), html_escape(tag)) + nv_pair("id", id), mymodule.html_escape(text), mymodule.html_escape(tag)) ) end -function link ( v ) +function mymodule.link ( v ) if ( v.value == nil ) then return nil end @@ -256,5 +256,7 @@ function link ( v ) str = str .. nv_pair ( k, v[k] ) end - return ( "<a " .. str .. ">" .. html_escape(v.label) .. "</a>" ) + return ( "<a " .. str .. ">" .. mymodule.html_escape(v.label) .. "</a>" ) end + +return mymodule diff --git a/processinfo.lua b/processinfo.lua index 2a099c3..0a2013d 100644 --- a/processinfo.lua +++ b/processinfo.lua @@ -1,5 +1,5 @@ -module(..., package.seeall) +local mymodule = {} posix = require("posix") fs = require("acf.fs") @@ -7,7 +7,7 @@ format = require("acf.format") apk = require("acf.apk") subprocess = require("subprocess") -function package_version(packagename) +function mymodule.package_version(packagename) local result = apk.version(packagename) local errtxt if not result then @@ -16,7 +16,7 @@ function package_version(packagename) return result,errtxt end -function process_autostart(servicename) +function mymodule.process_autostart(servicename) local result local errtxt = "Not programmed to autostart" local code, cmdresult = subprocess.call_capture({"rc-update", "show"}) @@ -37,7 +37,7 @@ function process_autostart(servicename) return result,errtxt end -function read_initrunlevels() +function mymodule.read_initrunlevels() local config = {} local code, cmdresult = subprocess.call_capture({"rc-update", "show", "-v"}) for line in string.gmatch(cmdresult, "([^\n]*)\n?") do @@ -55,7 +55,7 @@ function read_initrunlevels() return config end -function add_runlevels(servicename, runlevels) +function mymodule.add_runlevels(servicename, runlevels) local cmdresult,cmderrors if not servicename then cmderrors = "Invalid service name" @@ -78,7 +78,7 @@ function add_runlevels(servicename, runlevels) return cmdresult,cmderrors end -function delete_runlevels(servicename, runlevels) +function mymodule.delete_runlevels(servicename, runlevels) local cmdresult,cmderrors if not servicename then cmderrors = "Invalid service name" @@ -101,7 +101,7 @@ function delete_runlevels(servicename, runlevels) return cmdresult,cmderrors end -function daemoncontrol (process, action) +function mymodule.daemoncontrol (process, action) local cmdresult = "" local cmderrors if not process then @@ -120,14 +120,14 @@ function daemoncontrol (process, action) return cmdresult,cmderrors end -function daemon_actions (process) +function mymodule.daemon_actions (process) local actions = {"status", "start", "stop", "restart", "describe", "zap"} local reverse = {} for i,a in ipairs(actions) do reverse[a] = i end local description - local res, err = daemoncontrol(process, "describe") + local res, err = mymodule.daemoncontrol(process, "describe") if err then return nil, err else @@ -220,7 +220,7 @@ local function has_pidfile(name) return pid end -function pidof(name) +function mymodule.pidof(name) local pids = {has_pidfile(name)} local i, j @@ -236,3 +236,4 @@ function pidof(name) return pids end +return mymodule diff --git a/validator.lua b/validator.lua index 3146a6b..4a7adb7 100644 --- a/validator.lua +++ b/validator.lua @@ -1,26 +1,26 @@ -------------------------------------------------- -- Validation Functions for Alpine Linux' Webconf -------------------------------------------------- -module (..., package.seeall) +local mymodule = {} format = require("acf.format") -function is_string ( str ) +function mymodule.is_string ( str ) return (type(str) == "string") end -function is_boolean ( str ) +function mymodule.is_boolean ( str ) return (type(str) == "boolean") end -function is_number ( str ) +function mymodule.is_number ( str ) return (type(str) == "number") end -- -- This function validates an ipv4 address. -- -function is_ipv4(ipv4) +function mymodule.is_ipv4(ipv4) local retval = false; local nums = {}; local iplen = string.len(ipv4); @@ -62,7 +62,7 @@ end -- -- This function validates a partial ipv4 address. -- -function is_partial_ipv4(ipv4) +function mymodule.is_partial_ipv4(ipv4) local retval = false; local nums = {}; @@ -89,7 +89,7 @@ function is_partial_ipv4(ipv4) return true end -function is_mac(mac) +function mymodule.is_mac(mac) local tmpmac = string.upper(mac) @@ -137,7 +137,7 @@ end -- consists of number-chars between 0..9 only -- and eventually a leading '-' -- -function is_integer(numstr) +function mymodule.is_integer(numstr) -- ^ beginning of string -- -? one or zero of the char '-' -- %d+ one or more digits @@ -151,8 +151,8 @@ end -- consists of number-chars between 0..9 only -- and if it is within a given range. -- -function is_integer_in_range(numstr, min, max) - return is_integer(numstr) +function mymodule.is_integer_in_range(numstr, min, max) + return mymodule.is_integer(numstr) and tonumber(numstr) >= min and tonumber(numstr) <= max @@ -162,13 +162,15 @@ end -- This function checks if the given number is an integer -- and wheter it is between 1 .. 65535 -- -function is_port(numstr) - return is_integer_in_range(numstr, 1, 65535) +function mymodule.is_port(numstr) + return mymodule.is_integer_in_range(numstr, 1, 65535) end -function is_valid_filename ( path, restriction ) +function mymodule.is_valid_filename ( path, restriction ) if not (path) or ((restriction) and (string.find (path, "^" .. format.escapemagiccharacters(restriction) ) == nil or string.find (path, "/", #restriction+2) )) then return false end return true end + +return mymodule |