From aba1131c59e54a7b2baf048b3f7d372e14268e6b Mon Sep 17 00:00:00 2001 From: onefang Date: Thu, 5 Feb 2026 03:43:04 +1000 Subject: The actual script. --- LICENCE | 31 ++++ polygLua.lua | 518 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 549 insertions(+) create mode 100644 LICENCE create mode 100755 polygLua.lua diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..d4cba67 --- /dev/null +++ b/LICENCE @@ -0,0 +1,31 @@ +Copyright notice for polygLua + +Copyright (C) 2025 David Walter Seikel AKA onefang + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Freedom -1: the author specifically grants themselves the freedom to not +be infected by the viral licence clauses of any code this source code +"links" to. It's my code, I choose my licence terms, no one else does. diff --git a/polygLua.lua b/polygLua.lua new file mode 100755 index 0000000..008011e --- /dev/null +++ b/polygLua.lua @@ -0,0 +1,518 @@ +#!/usr/bin/env luajit + +--- polygLua, gluing things onto Lua, making it a polyglot language. +-- I can write Lua in any language. B-) +-- Also includes some other little useful bits I commonly use, most of which is used in support of its main function. +-- @module polygLua +-- @alias _ + +local _ = {} +--- version number and string +_.version = '0.0 crap' + + +_.verbosity = 4 +local log = function( v, -- verbosity + t, -- level + s -- message + ) + if nil == s then s = 'nil' end + if v <= _.verbosity then + if 3 <= _.verbosity then t = os.date('!%F %T') .. ' ' .. t end + if 4 == v then t = '' else t = t .. ': ' end + if 3 <= v then + io.stdout:write(t .. s .. '\n') + io.stdout:flush() + else + io.stderr:write(t .. s .. '\n') + io.stderr:flush() + end + end +end +-- This sets the global values, here and in the caller. +--- log DEBUG level string +D = function(s) log(5, 'DEBUG ', s) end +--- log PRINT level string +P = function(s) log(4, 'PRINT ', s) end +--- log INFO level string +I = function(s) log(3, 'INFO ', s) end +--- log TIMEOUT level string +T = function(s) log(2, 'TIMEOUT ', s) end +--- log WARNING level string +W = function(s) log(1, 'WARNING ', s) end +--- log ERROR level string +E = function(s) log(0, 'ERROR ', s) end +--- log CRITICAL level string +C = function(s) log(-1, 'CRITICAL ', s) end + +--- common options +local optionsCommon = +{ + help = {help = 'Print the help text.', + func = function(self, options, a, args, i, name) + for i,v in ipairs{'/usr/share/doc/' .. name, '/usr/local/share/doc/' .. name, './'} do + local p = v .. 'README.md' + local h = io.open(p, 'r') + if nil ~= h then + D('Found README file '.. p) + Help = h:read('*a') -- NOTE Lua 5.3 doesn't use the *, but ignores it if it's there, earlier versions need it. + h:close() + end + end + + P(Help) + _.usage(args, options, true) + os.exit(0) + end + }, + version = {help = 'Print the version details.', + func = function(self, options, a, args, i) + P('This is version ' ..Version .. ' of ' .. args[0]) + os.exit(0) + end + }, + ['-q'] = {help = 'Decrease verbosity level.', + func = function(self, options, a, args, i) + if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end + I('New verbosity level is ' .. _.verbosity) + end + }, + ['-v'] = {help = 'Increase verbosity level.', + func = function(self, options, a, args, i) + if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end + I('New verbosity level is ' .. _.verbosity) + end + }, + ['--maximum-verbosity'] = {help = 'Increase verbosity level to maximum.', + func = function(self, options, a, args, i) + _.verbosity = 4 + I('New verbosity level is ' .. _.verbosity) + end + }, +} + +--- print usage info +_.usage = function( args, -- command line arguments that where passed to the script, including the name + options, -- describes all the command line options + all -- print the lot + ) + local h = '' + for k, v in pairs(options) do + if 'table' == type(v) then h = h .. k .. ' | ' end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then h = h .. k .. ' | ' end + end + P('Usage: ' .. args[0] .. ' {' .. h:sub(1, -2) .. '}') + if true == all then + for k, v in pairs(options) do + if 'table' == type(v) then + if nil ~= v.help then P(k .. '\t\t' .. v.help) end + end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then + if nil ~= v.help then P(k .. '\t\t' .. v.help) end + end + end + end +end + +--- parse command line options +_.parse = function( args, -- command line arguments that where passed to the script + options, -- describes all the command line options + name -- name to use for finding config file + ) + local o = nil + + local doIt = function(name, val, a, args, i) + local o = options[name] + if nil == o then o = optionsCommon[name] end + if nil ~= o then + if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end + if nil ~= o.func then o:func(options, a, args, i, name) end + end + return o + end + + if nil ~= name then + for i,v in ipairs{'/etc/', '~/.', './.'} do + local p = v .. name .. '.conf.lua' + local h = io.open(p, 'r') + if nil ~= h then + D('Found configuration file '.. p) + h:close() + local ar = dofile(p) + for k, w in pairs(ar) do + if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end + end + end + end + end + + if (0 == #args) and (nil ~= options['']) then table.insert(args, '') end + if 0 ~= #args then + for i,a in ipairs(args) do + D('Argument ' .. i .. ' = ' .. a) + local ds = 0 + if ('-' == a:sub(1, 1)) and ('-' ~= a:sub(2, 2)) then ds = 1 end + if '--' == a:sub(1, 2) then ds = 2; a = a:sub(3, -1) end + local s, e = a:find('=', 1, true) + local k , v + if not s then + e = 0 + v = nil + else + v = a:sub(e + 1, -1) + end + k = a:sub(1, e - 1) + if 1 == ds then + for j = 2, #k do + o = doIt('-' .. k:sub(j, j), v, a, args, i) + end + else + o = doIt(k, v, a, args, i) + end + end + end + + if nil == o then + _.usage(args, options) + os.exit(0) + end +end + + +--- run a shell command, return the output in a table +_.readCmd = function(cmd) + local result = {} + --[[ + io.popen gives us a read file handle for 'r' and a write one for 'w' + os.execute gives us some sort of status code, plus other things in other Lua versions, but no in or out. + ]] + local output = io.popen(cmd, 'r') + if nil ~= output then + for l in output:lines() do + table.insert(result, l) + end + end + output:close() + return result +end + + + +--- funky executable wrapper, might even call it a class +-- @alias exe +__ = function(c -- main command, or commands in a multiline string, or list of commands in a table, or a #! script + ) + local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} + local n = 0 + + exe.cmd = '{ ' + if 'table' == type(c) then + for i, l in ipairs(c) do + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end + elseif 'string' == type(c) then + exe.isScript = (n == 0) and ('#!' == c:sub(1,2)) + for l in c:gmatch('\n*([^\n]+)\n*') do + if '' ~= l then + if exe.isScript then + if '' == exe.script then + exe.scriptFile = os.tmpname() + D('Creating temporary script file at ' .. exe.scriptFile) + exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' + -- PHP wants this to be executable. + __('chmod u+x ' .. exe.scriptFile) + end + exe.script = exe.script .. l .. '\n' + else + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end + end + end + end + if exe.isScript then + local a, e = io.open(exe.scriptFile, 'w') + if nil == a then E('Could not open ' .. exe.scriptFile .. ' - ' .. e) else + a:write(exe.script) + a:close() + end +-- exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' + end + exe.cmd = exe.cmd .. ' } ' + if 1 == n then exe.cmd = c end + + + --- run this command under ionice and nice + function exe:Nice(c) + if nil == c then + self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd + else + self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' + end + return self + end + + --- run this command under timeout + function exe:timeout(c) + if nil == c then + -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well. + -- --kill-after means "send KILL after TERM fails". + self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd + else + self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd + end + return self + end + + --- enable logging the command line + function exe:log() self.logging = true return self end + --- enable showing the command output + function exe:show() self.showing = true return self end + --- run this command after the last one runs + function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end + --- run this command after the last one runs, if it worked + function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end + --- run this command after the last one runs, if it failed + function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end + --- discard stderr + function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end + --- discard stdout + function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end + --- if the command worked, touch the w file, which is being waited on elsewhere, maybe + function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end + + --- actually run the command. + function exe:Do() + --[[ "The condition expression of a control structure can return any + value. Both false and nil are considered false. All values different + from nil and false are considered true (in particular, the number 0 + and the empty string are also true)." + says the docs, I beg to differ.]] + if true == self.logging then D(' executing - ' .. self.cmd) end + self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') + -- The last line will be the command's returned status, fish that out and collect everything else in lines. + self.status = tonumber(self.lines[#self.lines]) + self.lines[#self.lines] = nil + if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end + + if (nil == self.status) then D('STATUS |' .. 'NIL' .. '| ' .. self.command) + elseif (137 == self.status) or (124 == self.status) then T('timeout killed ' .. self.status .. ' ' .. self.command) + elseif (0 ~= self.status) then D('STATUS |' .. self.status .. '| ' .. self.command) + end + + if nil ~= exe.scriptFile then os.execute('rm ' .. exe.scriptFile) end + + return self + end + +-- TODO - currently after is a string that is run after the command. Could be a Lua function to call. + + --- fork the command + function exe:fork(after, host) + if nil == after then after = '' end + if '' ~= after then after = ' ; ' .. after end +-- The host part is from apt-panopticon, likely needed there, but makes no sense here. +-- if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. self.cmd:gsub(, '"', "'") .. '"; fi' end + self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' + if true == self.logging then D(' forking - ' .. self.cmd) end + os.execute(self.cmd) + return self + end + + --- fork the command, unless it's already running + function exe:forkOnce() + if _.running(self.command) then + D('Already running ' .. self.command) + else + self:fork() + end + end + + return exe +end + + + +--- dereference an array +-- A simple table.subtable = subtable wont work, you end up with a reference so that changes to the later get applied to the former. +-- On the other hand, this isn't going deep, only the top layer if there is sub tables. +_.derefiTable = function(t) + local argh = {} + for l, y in ipairs(t) do if (l ~= y.name) then table.insert(argh, y) end end + return argh +end +--- dereference a table +_.derefTable = function(t) + local argh = {} + for l, y in pairs(t) do argh[l] = y end + return argh +end + +--- Does this file exist? +_.exists = function(f) + local h = io.open(f, 'r') + if nil == h then return false else h:close(); return true end +end +--- Is this command runnable? +_.runnable = function(c) return ( 0 == __('which ' .. c):Do().status ) end +--- Is this command running? +_.running = function(c) return ( 1 ~= tonumber(__('pgrep -u $USER -cf ' .. c):Do().lines[1]) ) end +--- pkill all +_.killEmAll = function(all -- table of command names to pkill + ) + for i,l in ipairs(all) do + local c = 0 + while 0 ~= tonumber(__('pgrep -u $USER -xc ' .. l):Do().lines[1]) do + local s = 'TERM' + if c > 1 then s = 'KILL'; __('sleep ' .. c):Do() end + __('pkill -' .. s .. ' -u $USER -x ' .. l):log():Do() + c = c + 1 + end + end +end + +--- execute whoami and return the result +_.who = __[[whoami]]:noErr():Do().lines[1] +--- execute pwd and return the result +_.dir = __[[pwd]]:noErr():Do().lines[1] + +--- write a string to a file +_.string2file = function(s, f) + local a, e = io.open(f, 'w') + if nil == a then E('Could not open ' .. f .. ' - ' .. e) else + a:write(s) + a:close() + end +end + +--- dump a table to a pretty string +_.table2string = function (table, -- table to dump + name, -- name of table + space -- Optional, used internally for sub tables. + ) + if nil == space then space = '' end + local r = space + if '' == space then r = r .. name .. ' =\n' else r = r .. '[' .. name .. '] =\n' end + r = r .. space .. '{\n' .. _.table2stringSub(table, space .. ' ') .. space .. '}' + if '' ~= space then r = r .. ',' end + return r .. '\n' +end +_.table2stringSub = function (table, space) + local r = '' + for k, v in pairs(table) do + if type(k) == 'string' then k = "'" .. k .. "'" end + if type(v) == 'table' then r = r .. _.table2string(v, k, space) + elseif type(v) == 'string' then + local bq, eq = "'", "'" + if nil ~= v:find(bq, 1, true) then + bq, eq = '[=[', ']=]' + end + if nil ~= v:find(bq, 1, true) then + bq, eq = '[==[', ']==]' + if nil ~= v:find(bq, 1, true) then + bq, eq = '[===[', ']===]' + mbq, meq = '%[%[', '%]%]' + while (nil ~= v:match(mbq)) or (nil ~= v:match(meq)) do + bq = '[' .. '=' .. bq:sub(2, -1) + eq = ']' .. '=' .. eq:sub(2, -1) + mbq = '%[' .. '=' .. bq:sub(3, -1) + meq = '%]' .. '=' .. eq:sub(3, -1) + end + end + end + r = r .. space .. '[' .. k .. '] = ' .. bq .. v .. eq .. ';\n' + elseif type(v) == 'function' then r = r .. space .. '[' .. k .. '] = function ();\n' + elseif type(v) == 'userdata' then r = r .. space .. 'userdata ' .. '[' .. k .. '];\n' + elseif type(v) == 'boolean' then + if (v) then r = r .. space .. '[' .. k .. '] = true;\n' + else r = r .. space .. '[' .. k .. '] = false;\n' + end + else r = r .. space .. '[' .. k .. '] = ' .. v .. ';\n' + end + end + return r +end + + + +-- Deal with being called directly. +if (arg[0] == './polygLua.lua') or (arg[0] == 'polygLua.lua') then + local function goAway(txt) + local luas = __'ls -d1 /usr/share/lua/*':noErr():Do() + for i,l in ipairs(luas.lines) do + local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/polygLua.lua' + if _.exists(lua) then + if 'root' == _.who then + I(txt .. ' ' .. lua) + __('rm ' .. lua):Do() + end + end + end + end + + local options = + { + install = + { + help = 'Command to install polygLua.lua', + func = function(self, options, a, args, i) + if 'root' ~= _.who then + E'Need to be root user to install.' + else + I'INSTALLING polygLua.lua!!!' + + local luas = __'ls -d1 /usr/share/lua/*':noErr():Do() + for i,l in ipairs(luas.lines) do + local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/polygLua.lua' + if _.exists(lua) then + P(lua .. ' installed') + else + if 'root' == _.who then + P('Installing ' .. lua) + __('mkdir -p /usr/local/share/lua/' .. l:sub(16) .. ' ; ln -s ' .. _.dir .. '/polygLua.lua ' .. lua):Do() + else + P(lua .. ' NOT installed') + end + end + end + + end + end + }, + uninstall = + { + help = 'Command to uninstall polygLua.lua', + func = function(self, options, a, args, i) + if 'root' ~= _.who then + E'Need to be root user to uninstall.' + else + P'UNINSTALLING polygLua.lua!!!' + goAway('Uninstalling') + end + end + }, + purge = + { + help = 'Command to purge polygLua.lua', + func = function(self, options, a, args, i) + if 'root' ~= _.who then + E'Need to be root user to purge.' + else + P'PURGING polygLua.lua!!!' + goAway('Purging') + end + end + }, + } + + _.parse(arg, options) +end + + + +return _ -- cgit v1.1