diff options
author | dvs1 | 2024-10-21 19:49:44 +1000 |
---|---|---|
committer | dvs1 | 2024-10-21 19:49:44 +1000 |
commit | 64439629c587a5f4a0395f4fc680d4d38d0c614d (patch) | |
tree | 8a252ffa4fdebfa291b55e1c24fe4dacb7aaa826 /.aataaj_JACK.lua | |
parent | A few clean ups. (diff) | |
download | JackOnAllDevices-64439629c587a5f4a0395f4fc680d4d38d0c614d.zip JackOnAllDevices-64439629c587a5f4a0395f4fc680d4d38d0c614d.tar.gz JackOnAllDevices-64439629c587a5f4a0395f4fc680d4d38d0c614d.tar.bz2 JackOnAllDevices-64439629c587a5f4a0395f4fc680d4d38d0c614d.tar.xz |
Convert example user script to new exe stuff.
Diffstat (limited to '.aataaj_JACK.lua')
-rwxr-xr-x | .aataaj_JACK.lua | 138 |
1 files changed, 10 insertions, 128 deletions
diff --git a/.aataaj_JACK.lua b/.aataaj_JACK.lua index b8b32a2..21e7938 100755 --- a/.aataaj_JACK.lua +++ b/.aataaj_JACK.lua | |||
@@ -2,131 +2,13 @@ | |||
2 | 2 | ||
3 | -- An example users .aataaj_JACK.lua script that just starts qsynth. | 3 | -- An example users .aataaj_JACK.lua script that just starts qsynth. |
4 | 4 | ||
5 | 5 | local _ = require '_' | |
6 | -- Most of this APT stuff was copied from apt-panopticon. | 6 | local D = _.D |
7 | local APT = {} | 7 | local I = _.I |
8 | 8 | local T = _.T | |
9 | APT.readCmd = function(cmd) | 9 | local W = _.W |
10 | local result = {} | 10 | local E = _.E |
11 | local output = io.popen(cmd) | 11 | local C = _.C |
12 | if nil ~= output then | 12 | local __ = _._ |
13 | for l in output:lines() do | 13 | |
14 | table.insert(result, l) | 14 | if _.exists('qsynth') then __('qsynth'):fork() end |
15 | end | ||
16 | end | ||
17 | return result | ||
18 | end | ||
19 | |||
20 | |||
21 | APT.exe = function(c) | ||
22 | local exe = {status = 0, result = '', lines = {}, log = true, cmd = c .. ' ', command = c} | ||
23 | |||
24 | function exe:log() | ||
25 | self.log = true | ||
26 | return self | ||
27 | end | ||
28 | function exe:Nice(c) | ||
29 | if nil == c then | ||
30 | self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd | ||
31 | else | ||
32 | self.cmd = self.cmd .. 'ionice -c3 nice -n 19 ' .. c .. ' ' | ||
33 | end | ||
34 | return self | ||
35 | end | ||
36 | function exe:timeout(c) | ||
37 | -- 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. | ||
38 | -- --kill-after means "send KILL after TERM fails. | ||
39 | if nil == c then | ||
40 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd | ||
41 | else | ||
42 | self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd | ||
43 | end | ||
44 | return self | ||
45 | end | ||
46 | function exe:also(c) | ||
47 | if nil == c then c = '' else c = ' ' .. c end | ||
48 | self.cmd = self.cmd .. ';' .. c .. ' ' | ||
49 | return self | ||
50 | end | ||
51 | function exe:And(c) | ||
52 | if nil == c then c = '' else c = ' ' .. c end | ||
53 | self.cmd = self.cmd .. '&&' .. c .. ' ' | ||
54 | return self | ||
55 | end | ||
56 | function exe:Or(c) | ||
57 | if nil == c then c = '' end | ||
58 | self.cmd = self.cmd .. '|| ' .. c .. ' ' | ||
59 | return self | ||
60 | end | ||
61 | function exe:noErr() | ||
62 | self.cmd = self.cmd .. '2>/dev/null ' | ||
63 | return self | ||
64 | end | ||
65 | function exe:wait(w) | ||
66 | self.cmd = self.cmd .. '&& touch ' .. w .. ' ' | ||
67 | return self | ||
68 | end | ||
69 | function exe:Do() | ||
70 | --[[ "The condition expression of a control structure can return any | ||
71 | value. Both false and nil are considered false. All values different | ||
72 | from nil and false are considered true (in particular, the number 0 | ||
73 | and the empty string are also true)." | ||
74 | says the docs, I beg to differ.]] | ||
75 | if true == self.log then D(" executing - <code>" .. self.cmd .. "</code>") end | ||
76 | --[[ Damn os.execute() | ||
77 | Lua 5.1 says it returns "a status code, which is system-dependent" | ||
78 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. | ||
79 | I'm getting 7168 or 0. No idea what the fuck that is. | ||
80 | local ok, rslt, status = os.execute(s) | ||
81 | ]] | ||
82 | local f = APT.readCmd(self.cmd, 'r') | ||
83 | -- The last line will be the command's returned status, collect everything else in result. | ||
84 | self.status = '' -- Otherwise the result starts with 0. | ||
85 | self.result = '\n' | ||
86 | self.lines = f | ||
87 | for i,l in ipairs(f) do | ||
88 | self.result = self.result .. l .. "\n" | ||
89 | end | ||
90 | f = APT.readCmd('echo "$?"', 'r') | ||
91 | for i,l in ipairs(f) do | ||
92 | self.status = tonumber(l) | ||
93 | if (137 == self.status) or (124 == self.status) then | ||
94 | print("timeout killed " .. self.status .. ' ' .. self.command) | ||
95 | E("timeout killed " .. self.status .. ' ' .. self.command) | ||
96 | elseif (0 ~= self.status) then | ||
97 | print("status |" .. self.status .. '| ' .. self.command) | ||
98 | E("status |" .. self.status .. '| ' .. self.command) | ||
99 | end | ||
100 | end | ||
101 | return self | ||
102 | end | ||
103 | function exe:fork(host) | ||
104 | 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 | ||
105 | self.cmd = '{ ' .. self.cmd .. '; } &' | ||
106 | if true == self.log then D(" forking - <code>" .. self.cmd .. "</code>") end | ||
107 | os.execute(self.cmd) | ||
108 | return self | ||
109 | end | ||
110 | return exe | ||
111 | end | ||
112 | |||
113 | APT.exists = function(c) | ||
114 | if 0 == APT.exe('which ' .. c):Do().status then return true end | ||
115 | return false | ||
116 | end | ||
117 | |||
118 | APT.killEmAll = function(all) | ||
119 | for i,l in ipairs(all) do | ||
120 | local c = 0 | ||
121 | while 0 ~= tonumber(APT.exe("pgrep -u $USER -xc " .. l):Do().result) do | ||
122 | local s = 'TERM' | ||
123 | if c > 0 then s = 'KILL'; APT.exe("sleep " .. c):Do() end | ||
124 | print( "pkill -" .. s .. " -u $USER -x " .. l) | ||
125 | APT.exe("pkill -" .. s .. " -u $USER -x " .. l):Do() | ||
126 | c = c + 1 | ||
127 | end | ||
128 | end | ||
129 | end | ||
130 | |||
131 | |||
132 | if APT.exists('qsynth') then APT.exe('qsynth'):fork() end | ||