diff options
Diffstat (limited to 'PolygLua.lua')
-rwxr-xr-x | PolygLua.lua | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/PolygLua.lua b/PolygLua.lua index ed14fc5..ee775eb 100755 --- a/PolygLua.lua +++ b/PolygLua.lua | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | --[[ PolygLua. Gluing things onto Lua, making it a polyglot language. | 3 | --[[ PolygLua. Gluing things onto Lua, making it a polyglot language. |
4 | 4 | ||
5 | TODO - Make the parsing recursive. So the command "--fancy" could have it's own options table. | 5 | TODO - Make the parsing recursive. So the command '--fancy' could have it's own options table. |
6 | --fancy option0 opt1=foo 'Random string!' --somethingElse | 6 | --fancy option0 opt1=foo 'Random string!' --somethingElse |
7 | When to stop and hand back? First time we see a ' -bar' or ' --foo'? | 7 | When to stop and hand back? First time we see a ' -bar' or ' --foo'? |
8 | 8 | ||
@@ -23,8 +23,8 @@ _.version = '0.0 crap' | |||
23 | _.verbosity = 2 | 23 | _.verbosity = 2 |
24 | local log = function(v, t, s) | 24 | local log = function(v, t, s) |
25 | if v <= _.verbosity then | 25 | if v <= _.verbosity then |
26 | if 3 <= _.verbosity then t = os.date('!%F %T') .. " " .. t end | 26 | if 3 <= _.verbosity then t = os.date('!%F %T') .. ' ' .. t end |
27 | print(t .. ": " .. s) | 27 | print(t .. ': ' .. s) |
28 | end | 28 | end |
29 | io.flush() | 29 | io.flush() |
30 | end | 30 | end |
@@ -106,7 +106,7 @@ _.parse = function(args, options, confFile) | |||
106 | if nil ~= confFile then | 106 | if nil ~= confFile then |
107 | for i,v in ipairs{'/etc/', '~/.', './.'} do | 107 | for i,v in ipairs{'/etc/', '~/.', './.'} do |
108 | local p = v .. confFile .. '.conf.lua' | 108 | local p = v .. confFile .. '.conf.lua' |
109 | local h, e = io.open(p, "r") | 109 | local h, e = io.open(p, 'r') |
110 | if nil ~= h then | 110 | if nil ~= h then |
111 | D('Found configuration file '.. p) | 111 | D('Found configuration file '.. p) |
112 | h:close() | 112 | h:close() |
@@ -124,7 +124,7 @@ _.parse = function(args, options, confFile) | |||
124 | local ds = 0 | 124 | local ds = 0 |
125 | if ('-' == a:sub(1, 1)) and ('-' ~= a:sub(2, 2)) then ds = 1 end | 125 | if ('-' == a:sub(1, 1)) and ('-' ~= a:sub(2, 2)) then ds = 1 end |
126 | if '--' == a:sub(1, 2) then ds = 2; a = a:sub(3, -1) end | 126 | if '--' == a:sub(1, 2) then ds = 2; a = a:sub(3, -1) end |
127 | local s, e = a:find("=") | 127 | local s, e = a:find('=') |
128 | local k , v | 128 | local k , v |
129 | if nil == s then | 129 | if nil == s then |
130 | e = 0 | 130 | e = 0 |
@@ -154,11 +154,11 @@ _.runnable = function(c) | |||
154 | end | 154 | end |
155 | 155 | ||
156 | _.running = function(c) | 156 | _.running = function(c) |
157 | return ( 1 ~= tonumber(__("pgrep -u $USER -cf " .. c):Do().lines[1]) ) | 157 | return ( 1 ~= tonumber(__('pgrep -u $USER -cf ' .. c):Do().lines[1]) ) |
158 | end | 158 | end |
159 | 159 | ||
160 | _.exists = function(f) | 160 | _.exists = function(f) |
161 | local h, e = io.open(f, "r") | 161 | local h, e = io.open(f, 'r') |
162 | if nil == h then return false else h:close(); return true end | 162 | if nil == h then return false else h:close(); return true end |
163 | end | 163 | end |
164 | 164 | ||
@@ -166,10 +166,10 @@ end | |||
166 | _.killEmAll = function(all) | 166 | _.killEmAll = function(all) |
167 | for i,l in ipairs(all) do | 167 | for i,l in ipairs(all) do |
168 | local c = 0 | 168 | local c = 0 |
169 | while 0 ~= tonumber(__("pgrep -u $USER -xc " .. l):Do().lines[1]) do | 169 | while 0 ~= tonumber(__('pgrep -u $USER -xc ' .. l):Do().lines[1]) do |
170 | local s = 'TERM' | 170 | local s = 'TERM' |
171 | if c > 1 then s = 'KILL'; __("sleep " .. c):Do() end | 171 | if c > 1 then s = 'KILL'; __('sleep ' .. c):Do() end |
172 | __("pkill -" .. s .. " -u $USER -x " .. l):log():Do() | 172 | __('pkill -' .. s .. ' -u $USER -x ' .. l):log():Do() |
173 | c = c + 1 | 173 | c = c + 1 |
174 | end | 174 | end |
175 | end | 175 | end |
@@ -202,7 +202,7 @@ __ = function(c) | |||
202 | end | 202 | end |
203 | elseif 'string' == type(c) then | 203 | elseif 'string' == type(c) then |
204 | exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) | 204 | exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) |
205 | for l in string.gmatch(c, "\n*([^\n]+)\n*") do | 205 | for l in string.gmatch(c, '\n*([^\n]+)\n*') do |
206 | if '' ~= l then | 206 | if '' ~= l then |
207 | if exe.isScript then | 207 | if exe.isScript then |
208 | if '' == exe.script then | 208 | if '' == exe.script then |
@@ -219,8 +219,8 @@ __ = function(c) | |||
219 | end | 219 | end |
220 | end | 220 | end |
221 | if exe.isScript then | 221 | if exe.isScript then |
222 | local a, e = io.open(exe.scriptFile, "w") | 222 | local a, e = io.open(exe.scriptFile, 'w') |
223 | if nil == a then E("Could not open " .. exe.scriptFile) else | 223 | if nil == a then E('Could not open ' .. exe.scriptFile) else |
224 | a:write(exe.script) | 224 | a:write(exe.script) |
225 | a:close() | 225 | a:close() |
226 | end | 226 | end |
@@ -241,7 +241,7 @@ __ = function(c) | |||
241 | 241 | ||
242 | function exe:timeout(c) | 242 | function exe:timeout(c) |
243 | -- 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. | 243 | -- 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. |
244 | -- --kill-after means "send KILL after TERM fails. | 244 | -- --kill-after means "send KILL after TERM fails". |
245 | if nil == c then | 245 | if nil == c then |
246 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd | 246 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd |
247 | else | 247 | else |
@@ -266,7 +266,7 @@ __ = function(c) | |||
266 | from nil and false are considered true (in particular, the number 0 | 266 | from nil and false are considered true (in particular, the number 0 |
267 | and the empty string are also true)." | 267 | and the empty string are also true)." |
268 | says the docs, I beg to differ.]] | 268 | says the docs, I beg to differ.]] |
269 | if true == self.logging then D(" executing - " .. self.cmd) end | 269 | if true == self.logging then D(' executing - ' .. self.cmd) end |
270 | --[[ Damn os.execute() | 270 | --[[ Damn os.execute() |
271 | Lua 5.1 says it returns "a status code, which is system-dependent" | 271 | Lua 5.1 says it returns "a status code, which is system-dependent" |
272 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. | 272 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. |
@@ -279,9 +279,9 @@ __ = function(c) | |||
279 | self.lines[#self.lines] = nil | 279 | self.lines[#self.lines] = nil |
280 | if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end | 280 | if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end |
281 | 281 | ||
282 | if (nil == self.status) then D("STATUS |" .. "NIL" .. '| ' .. self.command) | 282 | if (nil == self.status) then D('STATUS |' .. 'NIL' .. '| ' .. self.command) |
283 | elseif (137 == self.status) or (124 == self.status) then T("timeout killed " .. self.status .. ' ' .. self.command) | 283 | elseif (137 == self.status) or (124 == self.status) then T('timeout killed ' .. self.status .. ' ' .. self.command) |
284 | elseif (0 ~= self.status) then D("STATUS |" .. self.status .. '| ' .. self.command) | 284 | elseif (0 ~= self.status) then D('STATUS |' .. self.status .. '| ' .. self.command) |
285 | end | 285 | end |
286 | 286 | ||
287 | return self | 287 | return self |
@@ -293,7 +293,7 @@ __ = function(c) | |||
293 | if nil == after then after = '' end | 293 | if nil == after then after = '' end |
294 | if '' ~= after then after = ' ; ' .. after end | 294 | if '' ~= after then after = ' ; ' .. after end |
295 | self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' | 295 | self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' |
296 | if true == self.logging then D(" forking - " .. self.cmd) end | 296 | if true == self.logging then D(' forking - ' .. self.cmd) end |
297 | os.execute(self.cmd) | 297 | os.execute(self.cmd) |
298 | return self | 298 | return self |
299 | end | 299 | end |