forked from zserge/luash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sh.lua
547 lines (487 loc) · 14.1 KB
/
sh.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
local posix = require("posix")
--
-- We'll be overwriding the lua `tostring` function, so keep a reference to the
-- original lua version here:
---
local _lua_tostring = tostring
--
-- Create a Table with stack functions, used by popd/pushd based on:
-- http://lua-users.org/wiki/SimpleStack
--
local Stack = {}
function Stack:Create()
-- stack table
local t = {}
-- entry table
t._et = {}
-- push a value on to the stack
function t:push(...)
if ... then
local targs = {...}
-- add values
for _,v in ipairs(targs) do
table.insert(self._et, v)
end
end
end
-- pop a value from the stack
function t:pop(num)
-- get num values from stack
num = num or 1
-- return table
local entries = {}
-- get values into entries
for _ = 1, num do
-- get last entry
if #self._et ~= 0 then
table.insert(entries, self._et[#self._et])
-- remove last value
table.remove(self._et)
else
break
end
end
-- return unpacked entries
return table.unpack(entries)
end
-- get entries
function t:getn()
return #self._et
end
-- list values
function t:list()
for i,v in pairs(self._et) do
print(i, v)
end
end
return t
end
---@class sh.lua : sh.Shell
local M = {}
M.version = "Automatic Shell Bindings for Lua / LuaSH 1.2.0"
--
-- Simple popen3() implementation
--
local function popen3(path, ...)
local r1, w1 = posix.pipe()
local r2, w2 = posix.pipe()
local r3, w3 = posix.pipe()
assert((r1 ~= nil or r2 ~= nil or r3 ~= nil), "pipe() failed")
local pid, _ = posix.fork()
assert(pid ~= nil, "fork() failed")
if pid == 0 then
posix.close(w1)
posix.close(r2)
posix.close(r3)
posix.dup2(r1, posix.fileno(io.stdin))
posix.dup2(w2, posix.fileno(io.stdout))
posix.dup2(w3, posix.fileno(io.stderr))
posix.close(r1)
posix.close(w2)
posix.close(w3)
local ret, _, _ = posix.execp(path, table.unpack({...}))
assert(ret ~= nil, "execp() failed")
posix._exit(1)
return
end
posix.close(r1)
posix.close(w2)
posix.close(w3)
return pid, w1, r2, r3
end
--
-- Async posix.read function. Yields:
-- {poll status, pipe ended, pipe data, poll code}
-- * poll status: 0 (timeout), 1 (ready), nil (failure)
-- * pipe ended: false (pipe has more data), true (pipe has no more data)
-- * pipe data: data chunk
-- * poll code: full return code from posix.rpoll
--
local function read_async(p, bufsize, timeout)
while true do
local poll_code = {posix.rpoll(p, timeout)}
if poll_code[1] == 0 then
-- timeout => pipe not ready
coroutine.yield(0, nil, nil, poll_code)
elseif poll_code[1] == 1 then
-- pipe ready => read data
local buf = posix.read(p, bufsize)
local ended = (buf == nil or #buf == 0)
coroutine.yield(1, ended, buf, poll_code)
-- stop if pipe has ended ended
if ended then break end
else
-- poll failed
coroutine.yield(nil, nil, nil, poll_code)
break
end
end
end
--
-- Pipe input into cmd + optional arguments and wait for completion and then
-- return status code, stdout and stderr from cmd.
--
local function pipe_simple(input, cmd, ...)
--
-- Launch child process
--
local pid, w, r, e = popen3(cmd, table.unpack({...}))
assert(pid ~= nil, "pipe_simple() unable to popen3()")
--
-- Write to popen3's stdin, important to close it as some (most?) proccess
-- block until the stdin pipe is closed
--
posix.write(w, input)
posix.close(w)
local bufsize = 4096
local timeout = 100
--
-- Read popen3's stdout and stderr simultanously via Posix file handle
--
local stdout = {}
local stderr = {}
local stdout_ended = false
local stderr_ended = false
local read_stdout = coroutine.create(function () read_async(r, bufsize, timeout) end)
local read_stderr = coroutine.create(function () read_async(e, bufsize, timeout) end)
while true do
if not stdout_ended then
local _, pstatus, ended, buf, _ = coroutine.resume(read_stdout)
if pstatus == 1 then
stdout_ended = ended
if not stdout_ended then
stdout[#stdout + 1] = buf
end
end
end
if not stderr_ended then
local _, pstatus, ended, buf, _ = coroutine.resume(read_stderr)
if pstatus == 1 then
stderr_ended = ended
if not stderr_ended then
stderr[#stderr + 1] = buf
end
end
end
if stdout_ended and stderr_ended then break end
end
--
-- Clean-up child (no zombies) and get return status
--
local _, wait_cause, wait_status = posix.wait(pid)
posix.close(r)
posix.close(e)
return wait_status, wait_cause, table.concat(stdout), table.concat(stderr)
end
--
-- converts key and it's argument to "-k" or "-k=v" or just ""
--
local function arg(k, a)
if not a then return k end
if type(a) == "string" and #a > 0 then return k .. "=\'" .. a .. "\'" end
if type(a) == "number" then return k .. "=" .. _lua_tostring(a) end
if type(a) == "boolean" and a == true then return k end
error("invalid argument type: " .. type(a), a)
end
--
-- converts nested tables into a flat list of arguments and concatenated input
--
local function flatten(t)
local result = {
args = {}, input = "", __stdout = "", __stderr = "",
__exitcode = nil, __signal = nil
}
local function f(t)
local keys = {}
for k = 1, #t do
keys[k] = true
local v = t[k]
if type(v) == "table" then
f(v)
else
table.insert(result.args, _lua_tostring(v))
end
end
for k, v in pairs(t) do
if k == "__input" then
result.input = result.input .. v
elseif k == "__stdout" then
result.__stdout = result.__stdout .. v
elseif k == "__stderr" then
result.__stderr = result.__stderr .. v
elseif k == "__exitcode" then
result.__exitcode = v
elseif k == "__signal" then
result.__signal = v
elseif not keys[k] and k:sub(1, 1) ~= "_" then
local key = '-' .. k
if #k > 1 then key = "-" .. key end
table.insert(result.args, arg(key, v))
end
end
end
f(t)
return result
end
--
-- return a string representation of a shell command output
--
local function strip(str)
-- capture repeated charaters (.-) startign with the first non-space ^%s,
-- and not captuing any trailing spaces %s*
return str:match("^%s*(.-)%s*$")
end
local function tostring(self)
-- return trimmed command output as a string
local out = strip(self.__stdout)
local err = strip(self.__stderr)
if #err == 0 then
return out
end
-- if there is an error, print the output and error string
return "O: " .. out .. "\nE: " .. err .. "\n" .. self.__exitcode
end
---the concatenation (..) operator must be overloaded so you don't have to keep calling `tostring`
local function concat(self, rhs)
local out, err = self, ""
if type(out) ~= "string" then out, err = strip(self.__stdout), strip(self.__stderr) end
if #err ~= 0 then out = "O: " .. out .. "\nE: " .. err .. "\n" .. self.__exitcode end
--Errors when type(rhs) == "string" for some reason
return out..(type(rhs) == "string" and rhs or tostring(rhs))
end
--
-- Configurable flag that will raise errors and/or halt program on error
--
M.__raise_errors = true
--
-- returns a function that executes the command with given args and returns its
-- output, exit status etc
--
---@param cmd sh.CommandName | string
---@param ... string
---@return fun(...: string | sh.ReturnType): sh.ReturnType
local function command(cmd, ...)
local prearg = {...}
return function(...)
local args = flatten({...})
local all_args = {}
for _, x in pairs(prearg) do
table.insert(all_args, _lua_tostring(x))
end
for _, x in pairs(args.args) do
table.insert(all_args, _lua_tostring(x))
end
local status, cause, stdout, stderr = pipe_simple(
args.input, cmd, table.unpack(all_args)
)
if M.__raise_errors and status ~= 0 then
error(stderr)
end
local t = {
__input = stdout, -- set input = output for pipelines
__stdout = stdout,
__stderr = stderr,
__exitcode = cause == "exited" and status or 127,
__signal = cause == "killed" and status or 0,
}
local mt = {
__index = function(self, k, ...)
return M[k]
end,
__tostring = tostring,
__concat = concat
}
return setmetatable(t, mt)
end
end
--
-- get global metatable
--
local mt = getmetatable(_G)
if mt == nil then
mt = {}
setmetatable(_G, mt)
end
--
-- String comparison functions: strcmp (returns true only if two strings are
-- identical), prefcmp (returns true only if the second string starts with the
-- first string).
--
local function strcmp(a, b)
return a == b
end
local function prefcmp(a, b)
return a == b:sub(1, #a)
end
local function list_contains(v, t, comp)
for _, kv in pairs(v) do
if comp(kv, t) then
return true
end
end
return false
end
--
-- Define patterns that the __index function should ignore
--
M.__index_ignore_prefix = {"_G", "_PROMPT"}
M.__index_ignore_exact = {}
M.__index_ignore_function = {"cd", "pushd", "popd", "stdout", "stderr", "print"}
--
-- set hook for undefined variables
--
---Adds the shell functions into the global table
local function install()
mt.__index = function(t, cmd)
if list_contains(M.__index_ignore_prefix, cmd, prefcmp) then
return rawget(t, cmd)
end
if list_contains(M.__index_ignore_exact, cmd, strcmp) then
return rawget(t, cmd)
end
if list_contains(M.__index_ignore_function, cmd, strcmp) then
return M.FUNCTIONS[cmd]
end
return command(cmd)
end
end
--
-- manually defined functions
--
M.FUNCTIONS = {}
local function cd(...)
local args = flatten({...})
local dir = args.args[1]
local pt = posix.chdir(dir)
local t = {
__input = args.input, -- set input = output from previous pipelines
__stdout = args.__stdout,
__stderr = args.__stderr,
__exitcode = args.__exitcode,
__signal = args.__signal
}
if pt == nil then
t.__stderr = "cd: The directory \'" .. dir .. "\' does not exist"
t.__exitcode = 1
end
local mt = {
__index = function(self, k, ...)
return M[k]
end,
__tostring = tostring,
__concat = concat
}
return setmetatable(t, mt)
end
local function stdout(t)
return t.__stdout
end
local function stderr(t)
return t.__stderr
end
M.PUSHD_STACK = Stack:Create()
local function pushd(...)
local args = flatten({...})
local dir = args.args[1]
local old_dir = strip(stdout(M.pwd()))
local pt = posix.chdir(dir)
if pt ~= nil then
M.PUSHD_STACK:push(old_dir)
end
local t = {
__input = args.input, -- set input = output from previous pipelines
__stdout = args.__stdout,
__stderr = args.__stderr,
__exitcode = args.__exitcode,
__signal = args.__signal
}
if pt == nil then
t.__stderr = "pushd: The directory \'" .. dir .. "\' does not exist"
t.__exitcode = 1
end
local mt = {
__index = function(self, k, ...)
return M[k]
end,
__tostring = tostring,
__concat = concat
}
return setmetatable(t, mt)
end
local function popd(...)
local args = flatten({...})
local ndir = M.PUSHD_STACK:getn()
local dir = M.PUSHD_STACK:pop(1)
local pt
if ndir > 0 then
pt = posix.chdir(dir)
else
pt = nil
dir = "EMPTY"
end
local t = {
__input = args.input, -- set input = output from previous pipelines
__stdout = args.__stdout,
__stderr = args.__stderr,
__exitcode = args.__exitcode,
__signal = args.__signal
}
if pt == nil then
t.__stderr = "popd: The directory \'" .. dir .. "\' does not exist"
t.__exitcode = 1
end
local mt = {
__index = function(self, k, ...)
return M[k]
end,
__tostring = tostring,
__concat = concat
}
return setmetatable(t, mt)
end
local _lua_print = print
local function print(...)
local args = flatten({...})
_lua_print(tostring(args))
local t = {
__input = args.input, -- set input = output from previous pipelines
__stdout = args.__stdout,
__stderr = args.__stderr,
__exitcode = args.__exitcode,
__signal = args.__signal
}
local mt = {
__index = function(self, k, ...)
return M[k]
end,
__tostring = tostring,
__concat = concat
}
return setmetatable(t, mt)
end
M.FUNCTIONS.cd = cd
M.FUNCTIONS.stdout = stdout
M.FUNCTIONS.stderr = stderr
M.FUNCTIONS.pushd = pushd
M.FUNCTIONS.popd = popd
M.FUNCTIONS.print = print
--
-- export command() and install() functions
--
M.command = command
M.install = install
--
-- allow to call sh to run shell commands
--
setmetatable(M, {
__call = function(_, cmd, ...)
return command(cmd, ...)
end,
__index = function(t, cmd)
if list_contains(M.__index_ignore_function, cmd, strcmp) then
return M.FUNCTIONS[cmd]
end
return command(cmd)
end
})
return M