aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoronefang2025-05-28 10:42:37 +1000
committeronefang2025-05-28 10:42:37 +1000
commit298d75d46ba6bd26646c9c6a51aa0c932fadeb33 (patch)
tree913519a0e25f7a6c40d81aafae14145d98434102
parentNeaten up the white space. (diff)
downloadJackOnAllDevices-master.zip
JackOnAllDevices-master.tar.gz
JackOnAllDevices-master.tar.bz2
JackOnAllDevices-master.tar.xz
Switch to using the standard polygLua.HEADmaster
-rwxr-xr-xPolygLua.lua405
-rwxr-xr-xaataaj.lua2
-rwxr-xr-xtest_.lua186
3 files changed, 1 insertions, 592 deletions
diff --git a/PolygLua.lua b/PolygLua.lua
deleted file mode 100755
index 17abd88..0000000
--- a/PolygLua.lua
+++ /dev/null
@@ -1,405 +0,0 @@
1#!/usr/bin/env luajit
2
3--[[ PolygLua. Gluing things onto Lua, making it a polyglot language.
4
5TODO - Make the parsing recursive. So the command '--fancy' could have it's own options table.
6 --fancy option0 opt1=foo 'Random string!' --somethingElse
7 When to stop and hand back? First time we see a ' -bar' or ' --foo'?
8
9TODO - Add some sort of alias mechanism for the #! thing. _.bash'echo "This is bash."':Do()
10 The user can define their own aliases in a table, with defaults for bash, sh, luajit, and maybe some others.
11 Then extend it to changing language on the fly _.bash'echo "This is bash."':luajit("print('This is Lua.')"):Do()
12
13TODO - APT also has +/- for adding and removing option values into an options entry.
14 Not sure if that is being used.
15
16]]
17
18-- Most of this _ stuff was copied from apt-panopticon.
19local _ = {}
20_.version = '0.0 crap'
21
22
23_.verbosity = 2
24local log = function(v, t, s)
25 if v <= _.verbosity then
26 if 3 <= _.verbosity then t = os.date('!%F %T') .. ' ' .. t end
27 print(t .. ': ' .. s)
28 end
29 io.flush()
30end
31
32-- This sets the global values, here and in the caller. The "_G." part isn't needed, it's just there to make things more obvious to the reader.
33_G.D = function(s) log(4, 'DEBUG ', s) end
34_G.I = function(s) log(3, 'INFO ', s) end
35_G.T = function(s) log(2, 'TIMEOUT ', s) end
36_G.W = function(s) log(1, 'WARNING ', s) end
37_G.E = function(s) log(0, 'ERROR ', s) end
38_G.C = function(s) log(-1, 'CRITICAL ', s) end
39
40
41local optionsCommon =
42{
43 help = {help = 'Print the help text.',
44 func = function(self, options, a, args, i, name)
45 for i,v in ipairs{'/usr/share/doc/' .. name, '/usr/local/share/doc/' .. name, './'} do
46 local p = v .. 'README.md'
47 local h = io.open(p, 'r')
48 if nil ~= h then
49 D('Found README file '.. p)
50 Help = h:read('*a')
51 h:close()
52 end
53 end
54
55 print(Help)
56 _.usage(args, options, true)
57 os.exit(0)
58 end
59 },
60 version = {help = 'Print the version details.',
61 func = function(self, options, a, args, i)
62 print('This is version ' ..Version .. ' of ' .. args[0])
63 os.exit(0)
64 end
65 },
66 ['-q'] = {help = 'Decrease verbosity level.',
67 func = function(self, options, a, args, i)
68 if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end
69 print('New verbosity level is ' .. _.verbosity)
70 end
71 },
72 ['-v'] = {help = 'Increase verbosity level.',
73 func = function(self, options, a, args, i)
74 if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end
75 print('New verbosity level is ' .. _.verbosity)
76 end
77 },
78}
79
80_.usage = function(args, options, all)
81 local h = ''
82 for k, v in pairs(options) do
83 if 'table' == type(v) then h = h .. k .. ' | ' end
84 end
85 for k, v in pairs(optionsCommon) do
86 if 'table' == type(v) then h = h .. k .. ' | ' end
87 end
88 print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}')
89 if true == all then
90 for k, v in pairs(options) do
91 if 'table' == type(v) then
92 if nil ~= v.help then print(k .. '\t\t' .. v.help) end
93 end
94 end
95 for k, v in pairs(optionsCommon) do
96 if 'table' == type(v) then
97 if nil ~= v.help then print(k .. '\t\t' .. v.help) end
98 end
99 end
100 end
101end
102
103_.parse = function(args, options, name)
104 local o = nil
105
106 local doIt = function(name, val, a, args, i)
107 local o = options[name]
108 if nil == o then o = optionsCommon[name] end
109 if nil ~= o then
110 if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end
111 if nil ~= o.func then o:func(options, a, args, i, name) end
112 end
113 return o
114 end
115
116 if nil ~= name then
117 for i,v in ipairs{'/etc/', '~/.', './.'} do
118 local p = v .. name .. '.conf.lua'
119 local h = io.open(p, 'r')
120 if nil ~= h then
121 D('Found configuration file '.. p)
122 h:close()
123 local ar = dofile(p)
124 for k, w in pairs(ar) do
125 if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end
126 end
127 end
128 end
129 end
130
131 if 0 ~= #args then
132 for i,a in ipairs(args) do
133 D('Argument ' .. i .. ' = ' .. a)
134 local ds = 0
135 if ('-' == a:sub(1, 1)) and ('-' ~= a:sub(2, 2)) then ds = 1 end
136 if '--' == a:sub(1, 2) then ds = 2; a = a:sub(3, -1) end
137 local s, e = a:find('=', 1, true)
138 local k , v
139 if not s then
140 e = 0
141 v = nil
142 else
143 v = a:sub(e + 1, -1)
144 end
145 k = a:sub(1, e - 1)
146 if 1 == ds then
147 for j = 2, #k do
148 o = doIt('-' .. k:sub(j, j), v, a, args, i)
149 end
150 else
151 o = doIt(k, v, a, args, i)
152 end
153 end
154 end
155
156 if nil == o then
157 _.usage(args, options)
158 os.exit(0)
159 end
160end
161
162_.runnable = function(c)
163 return ( 0 == __('which ' .. c):Do().status )
164end
165
166_.running = function(c)
167 return ( 1 ~= tonumber(__('pgrep -u $USER -cf ' .. c):Do().lines[1]) )
168end
169
170_.exists = function(f)
171 local h = io.open(f, 'r')
172 if nil == h then return false else h:close(); return true end
173end
174
175
176_.killEmAll = function(all)
177 for i,l in ipairs(all) do
178 local c = 0
179 while 0 ~= tonumber(__('pgrep -u $USER -xc ' .. l):Do().lines[1]) do
180 local s = 'TERM'
181 if c > 1 then s = 'KILL'; __('sleep ' .. c):Do() end
182 __('pkill -' .. s .. ' -u $USER -x ' .. l):log():Do()
183 c = c + 1
184 end
185 end
186end
187
188
189_.readCmd = function(cmd)
190 local result = {}
191 local output = io.popen(cmd)
192 if nil ~= output then
193 for l in output:lines() do
194 table.insert(result, l)
195 end
196 end
197 -- While this does return the same things as os.execute(), it's just as useless.
198 output:close()
199 return result
200end
201
202
203_G.__ = function(c)
204 local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''}
205 local n = 0
206
207 exe.cmd = '{ '
208 if 'table' == type(c) then
209 for i, l in ipairs(c) do
210 n = n + 1
211 exe.cmd = exe.cmd .. l .. ' ; '
212 end
213 elseif 'string' == type(c) then
214 exe.isScript = (n == 0) and ('#!' == c:sub(1,2))
215 for l in string.gmatch(c, '\n*([^\n]+)\n*') do
216 if '' ~= l then
217 if exe.isScript then
218 if '' == exe.script then
219 exe.scriptFile = os.tmpname()
220 D('Creating temporary script file at ' .. exe.scriptFile)
221 exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; '
222 -- PHP wants this to be executable.
223 __('chmod u+x ' .. exe.scriptFile)
224 end
225 exe.script = exe.script .. l .. '\n'
226 else
227 n = n + 1
228 exe.cmd = exe.cmd .. l .. ' ; '
229 end
230 end
231 end
232 end
233 if exe.isScript then
234 local a, e = io.open(exe.scriptFile, 'w')
235 if nil == a then E('Could not open ' .. exe.scriptFile .. ' - ' .. e) else
236 a:write(exe.script)
237 a:close()
238 end
239 exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; '
240 end
241 exe.cmd = exe.cmd .. ' } '
242 if 1 == n then exe.cmd = c end
243
244
245 function exe:Nice(c)
246 if nil == c then
247 self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd
248 else
249 self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' '
250 end
251 return self
252 end
253
254 function exe:timeout(c)
255 -- 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.
256 -- --kill-after means "send KILL after TERM fails".
257 if nil == c then
258 self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd
259 else
260 self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd
261 end
262 return self
263 end
264
265 function exe:log() self.logging = true return self end
266 function exe:show() self.showing = true return self end
267 -- Should be called "then" but that's a reserved word.
268 function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end
269 function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end
270 function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end
271 function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end
272 function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end
273 function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end
274
275 function exe:Do()
276 --[[ "The condition expression of a control structure can return any
277 value. Both false and nil are considered false. All values different
278 from nil and false are considered true (in particular, the number 0
279 and the empty string are also true)."
280 says the docs, I beg to differ.]]
281 if true == self.logging then D(' executing - ' .. self.cmd) end
282 --[[ Damn os.execute()
283 Lua 5.1 says it returns "a status code, which is system-dependent"
284 Lua 5.2 says it returns true/nil, "exit"/"signal", the status code.
285 I'm getting 7168 or 0. No idea what the fuck that is.
286 local ok, rslt, status = os.execute(s)
287 ]]
288 self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r')
289 -- The last line will be the command's returned status, fish that out and collect everything else in lines.
290 self.status = tonumber(self.lines[#self.lines])
291 self.lines[#self.lines] = nil
292 if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end
293
294 if (nil == self.status) then D('STATUS |' .. 'NIL' .. '| ' .. self.command)
295 elseif (137 == self.status) or (124 == self.status) then T('timeout killed ' .. self.status .. ' ' .. self.command)
296 elseif (0 ~= self.status) then D('STATUS |' .. self.status .. '| ' .. self.command)
297 end
298
299 return self
300 end
301
302 function exe:fork(after, host)
303-- The host part is from apt-panopticon, likely needed there, but makes no sense here.
304-- if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end
305 if nil == after then after = '' end
306 if '' ~= after then after = ' ; ' .. after end
307 self.cmd = '{ ' .. self.cmd .. after .. ' ; } & '
308 if true == self.logging then D(' forking - ' .. self.cmd) end
309 os.execute(self.cmd)
310 return self
311 end
312
313 function exe:forkOnce()
314 if _.running(self.command) then
315 D('Already running ' .. self.command)
316 else
317 self:fork()
318 end
319 end
320
321 return exe
322end
323
324
325
326_.who = __[[whoami]]:noErr():Do().lines[1]
327_.dir = __[[pwd]]:noErr():Do().lines[1]
328
329
330
331if (arg[0] == './PolygLua.lua') or (arg[0] == 'PolygLua.lua') then
332 local function goAway(txt)
333 local luas = __'ls -d1 /usr/share/lua/*':noErr():Do()
334 for i,l in ipairs(luas.lines) do
335 local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/PolygLua.lua'
336 if _.exists(lua) then
337 if 'root' == _.who then
338 print(txt .. ' ' .. lua)
339 __('rm ' .. lua):Do()
340 end
341 end
342 end
343 end
344
345 local options =
346 {
347 install =
348 {
349 help = 'Command to install PolygLua.lua',
350 func = function(self, options, a, args, i)
351 if 'root' ~= _.who then
352 E'Need to be root user to install.'
353 else
354 print('INSTALLING PolygLua.lua!!!')
355
356 local luas = __'ls -d1 /usr/share/lua/*':noErr():Do()
357 for i,l in ipairs(luas.lines) do
358 local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/PolygLua.lua'
359 if _.exists(lua) then
360 print(lua .. ' installed')
361 else
362 if 'root' == _.who then
363 print('Installing ' .. lua)
364 __('mkdir -p /usr/local/share/lua/' .. l:sub(16) .. ' ; ln -s ' .. _.dir .. '/PolygLua.lua ' .. lua):Do()
365 else
366 print(lua .. ' NOT installed')
367 end
368 end
369 end
370
371 end
372 end
373 },
374 uninstall =
375 {
376 help = 'Command to uninstall PolygLua.lua',
377 func = function(self, options, a, args, i)
378 if 'root' ~= _.who then
379 E'Need to be root user to uninstall.'
380 else
381 print('UNINSTALLING PolygLua.lua!!!')
382 goAway('Uninstalling')
383 end
384 end
385 },
386 purge =
387 {
388 help = 'Command to purge PolygLua.lua',
389 func = function(self, options, a, args, i)
390 if 'root' ~= _.who then
391 E'Need to be root user to purge.'
392 else
393 print('PURGING PolygLua.lua!!!')
394 goAway('Purging')
395 end
396 end
397 },
398 }
399
400 _.parse(arg, options)
401end
402
403
404
405return _
diff --git a/aataaj.lua b/aataaj.lua
index e2ac7cb..e08373b 100755
--- a/aataaj.lua
+++ b/aataaj.lua
@@ -14,7 +14,7 @@
14### END INIT INFO 14### END INIT INFO
15]] 15]]
16 16
17local _ = require 'PolygLua' 17local _ = require 'polygLua'
18Version = '0.0 crap' 18Version = '0.0 crap'
19 19
20local function goAway() 20local function goAway()
diff --git a/test_.lua b/test_.lua
deleted file mode 100755
index 9e1931a..0000000
--- a/test_.lua
+++ /dev/null
@@ -1,186 +0,0 @@
1#!/usr/bin/env luajit
2
3if type(jit) == 'table' then
4 print(arg[0] .. ' is being run by ' .. jit.version .. ' under ' .. jit.os .. ' on a ' .. jit.arch .. '. Lua version ' .. _VERSION)
5else
6 print(arg[0] .. ' is being run by Lua version ' .. _VERSION)
7end
8
9
10local _ = require 'PolygLua'
11
12
13I'Starting from the top.'
14
15Help = [[
16This is part of the blah blah blah...
17]]
18
19Version = '0.0 crap'
20
21local options =
22{
23 start = {help = 'Command to start the scanning process, for Sys V init.', value = 'blah'},
24-- restart = {start},
25-- ['force-reload'] = {start},
26 status = {help = 'Command to check the status of the scanning process, for Sys V init.',},
27 stop = {help = 'Command to stop the scanning process, for Sys V init.',},
28 JACK = {help = 'Command to start the JACK stuff, for users.',},
29 STOP = {help = 'Command to stop the JACK stuff, for users.',
30 func = function(self, options, a, args, i)
31--[=[
32 _.killEmAll{'qsynth'}
33 __[[
34 a2j_control --stop
35 sleep 2
36 a2j_control --exit
37 sleep 2
38 ]]:Do()
39 _.killEmAll{'alsa_in', 'alsa_out', 'zita-a2j', 'zita-j2a', 'aseqjoy', 'jack-plumbing'}
40 __[[
41 sleep 2
42 jack_control stop
43 sleep 2
44 jack_control exit
45 sleep 2
46 ]]:Do()
47 _.killEmAll{'jmcore', 'qjackctl'}
48 -- Catia is python, and no easy way to kill it.
49 -- Also it keeps jackdbus alive, no matter how hard you kill it.
50 __'pkill -TERM -u $USER -f catia':Do()
51 __'sleep 2':Do()
52 _.killEmAll{'jackdbus', 'a2jmidid'}
53]=]
54 end
55 },
56 install =
57 {
58 help = 'Command to install aataaj.lua',
59 func = function(self, options, a, args, i)
60 local w = __[[whoami]]:noErr():Do().lines[1]
61 local d = __[[pwd]]:noErr():Do().lines[1]
62 if 'root' ~= w then
63 E'Need to be root user to install.'
64 else
65 print('INSTALLING!!!')
66 __[[#!/bin/bash
67 echo "Installing PolygLua"
68 ./PolygLua.lua -vvvv
69 ]]:log():show():Do()
70 end
71 os.exit(0)
72 end
73 },
74}
75options['restart'] = options['start']
76options['force-reload'] = options['start']
77
78print('start = ' .. options.start.value)
79_.parse(arg, options, 'test_')
80print('start = ' .. options.start.value)
81print('stop = ' .. options.stop.value)
82
83
84
85__[[
86 echo -n "This'll print forking later. "
87 date
88 sleep 2
89]]:log():show():fork('echo -n "later is "; date')
90print('')
91
92
93local GUI = 'qjackctl'
94if _.runnable'catia' then GUI = 'catia' end
95local speaker = 'espeak'
96if _.runnable'espeak-ng' then speaker = 'espeak-ng' end
97print('' .. GUI .. ' ' .. speaker)
98print('')
99
100for i,l in ipairs(__'free -h':log():Do().lines) do
101 print(l)
102end
103print('')
104
105for i,l in ipairs(__'uname -a\nfree -h':log():Do().lines) do
106 print(l)
107end
108print('')
109
110__'uname -a':log():Then'free -h':show():Do()
111print('')
112
113__{'uname -a', 'free -h'}:log():show():Do()
114print('')
115
116
117print(__[[#!/bin/NoSuchCommand
118 echo "Should not print."
119]]:log():show():Do().status)
120print('')
121
122__[[#!/bin/bash
123 echo "G'day world from bash."
124]]:log():show():Do()
125print('')
126
127print(__[[#!/usr/bin/tcc -run
128 #include <tcclib.h>
129
130 int main()
131 {
132 printf("G'day world from C.\n");
133 return 1;
134 }
135]]:log():show():Do().status)
136print('')
137
138__[[#!/usr/bin/env luajit
139 print("G'day " .. "world " .. [=[from]=] .. ' Lua.')
140]]:log():show():Do()
141print('')
142
143__[[#!/usr/bin/perl
144 print "G'day world from perl.\n";
145]]:log():show():Do()
146print('')
147
148__[[#!/usr/bin/env php
149<?php
150 print("G'day world from php.\n");
151]]:log():show():Do()
152print('')
153
154-- Note no indent for Python, coz whitespace is significant.
155__[[#!/usr/bin/env python3
156print("G'day world from python.")
157]]:log():show():Do()
158print('')
159
160__[[#!/usr/bin/ruby
161 puts "G'day world from ruby."
162]]:log():show():Do()
163print('')
164
165
166print(__'true':log():show():Do().status)
167print(__'false':log():show():Do().status)
168print(__'exit 42':log():show():Do().status)
169print(__'return 42':log():show():Do().status)
170print(__'command NoSuchCommand':log():show():Do().status)
171
172
173--__'qpdfview':forkOnce()
174--__'qpdfview':forkOnce()
175
176print('')
177
178for i,a in ipairs(arg) do print('arg[' .. i .. '] = ' .. a) end
179print('')
180for k,a in pairs(arg) do print('arg[' .. k .. '] = ' .. a) end
181
182__'date':log():show():Do()
183-- Should get that "later" thing printed while the sleep is happening.
184__'date; sleep 5; date':log():show():Do()
185
186