-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.lua
More file actions
119 lines (99 loc) · 2.66 KB
/
shell.lua
File metadata and controls
119 lines (99 loc) · 2.66 KB
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
local syscall = require "_syscall"
local expect = require "cc.expect"
local os = require "craftos.os"
local shell = {}
local path = syscall.getenv().PATH or "/bin:/sbin:/usr/bin"
local aliases = {}
local completion = {}
function shell.dir()
return syscall.getcwd()
end
function shell.setDir(dir)
expect(1, dir, "string")
return syscall.chdir(dir)
end
function shell.path()
return path
end
function shell.setPath(newPath)
expect(1, newPath, "string")
path = newPath
end
function shell.getRunningProgram()
return syscall.getname()
end
function shell.resolve(path)
expect(1, path, "string")
if path:sub(1, 1) == "/" then return path end
return syscall.combine(syscall.getcwd(), path)
end
function shell.run(...)
local s = ""
for _, v in ipairs{...} do s = s .. (s == "" and "" or " ") .. v end
local t = {}
for w in s:gmatch "%S+" do t[#t+1] = w end
return shell.execute(table.unpack(t))
end
function shell.execute(...)
local env = setmetatable({shell = shell}, {__index = _ENV})
local fn, err = loadfile(path, nil, env)
-- printError?
if not fn then return false end
local ok, res = pcall(fn, ...)
return ok
end
function shell.aliases()
local retval = {}
for k, v in pairs(aliases) do retval[k] = v end
return retval
end
function shell.setAlias(command, program)
expect(1, command, "string")
expect(2, program, "string")
aliases[command] = program
end
function shell.clearAlias(command)
expect(1, command, "string")
aliases[command] = nil
end
function shell.getCompletionInfo()
return completion
end
function shell.setCompletionFunction(program, fn)
end
function shell.complete(line)
return {}
end
function shell.completeProgram(program)
return {}
end
function shell.resolveProgram(command)
for p in path:gmatch "[^:]+" do
local ok, l = pcall(syscall.list, p)
if ok then
for name in ipairs(l) do
if command == name or command .. ".lua" == name then
return syscall.combine(p, name)
end
end
end
end
return nil
end
function shell.programs(include_hidden)
expect(1, include_hidden, "boolean", "nil")
local retval = {}
for p in path:gmatch "[^:]+" do
local ok, l = pcall(syscall.list, p)
if ok then
for name in ipairs(l) do
if syscall.stat(syscall.combine(p, name)).type ~= "directory" and
(include_hidden or not name:match "^%.") then
retval[#retval+1] = name:gsub("%.lua$", "")
end
end
end
end
return retval
end
return shell