-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.lua
275 lines (244 loc) · 5.45 KB
/
shell.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
cmds_shell={}
function cmds_shell.exit(ctx)
ctx:exit()
end
function cmds_shell.rehash(ctx)
shell.rehash()
return 0
end
shell={}
local function get_module(name)
if (shell.module == nil) then
shell.module={}
setmetatable(shell.module, { __mode = "v" })
end
local mod=shell.module[name]
if (not mod) then
-- print("loading",name)
mod=require(name)
package.loaded[name]=nil
shell.module[name]=mod
else
-- print(name,"already loaded")
end
return mod
end
local function proxy(name,cmd,...)
local mod=get_module(name)
return mod[cmd](...)
end
function shell.rehash()
local cmds={}
for key,value in pairs(file.list()) do
if (key:match("CMD_.*%.lua")) then
-- print(key)
local name=key:sub(1,-5)
local mod=get_module(name)
for key2,value2 in pairs(mod) do
-- print(key,":",key2)
cmds[key2]=function(...) return proxy(name,key2,...) end
end
end
end
shell.cmds=cmds
end
function shell.cmd_tables()
local list={}
-- table.insert(shell.cmds)
for key,item in pairs(_G) do
if (key:sub(0,5) == 'cmds_') then
-- print("global",key)
table.insert(list,item)
end
end
if (shell.cmds) then
table.insert(list,shell.cmds)
end
return list
end
function shell.help(ctx,tables)
local list={}
ctx.stdout:print("Following commands exist:")
for key,item in pairs(tables) do
-- print("key",key)
for key,item in pairs(item) do
-- print("key",key,type(item))
if (type(item) == "function") then
table.insert(list,key)
end
end
end
table.sort(list)
for k,v in pairs(list) do ctx.stdout:print(v) end
return 0
end
function shell.cmd_exec(ctx,tables,cmd,args)
if (cmd == 'help' or cmd == '' or cmd == nil) then
return shell.help(ctx,tables)
end
for key,item in pairs(tables) do
local f=item[cmd]
if (f) then
local status,ret=xpcall(function() return f(ctx,unpack(args)) end ,function(x) return x.."\n"..debug.traceback() end)
-- print("xpcall","status",status,"ret",ret)
if (status) then
return ret
end
ctx.stderr:print(ret)
return -2
end
end
ctx.stderr:print("Command '"..cmd.."' not found, use 'help' for help")
return -22
end
function shell.pack(...)
return { n = select("#", ...), ... }
end
function shell.cmd2(ctx,tables,cmd,...)
return shell.cmd_exec(ctx,tables,cmd,shell.pack(...))
end
function shell.cmd_line(ctx,c)
if (c:match('[A-Za-z_][0-9A-Za-z_]*=.*')) then
pcall(loadstring(c))
return 0
end
local args=shell.words(c)
local cmd=table.remove(args,1)
if (cmd == nil or cmd == "") then return end
local ret=shell.cmd_exec(ctx,shell.cmd_tables(),cmd,args)
if (ret == nil) then ret=0 end
return ret
end
function shell.response(ret)
if (ret < 0) then
return("ERR "..ret)
elseif (ret > 0) then
return("OK "..ret)
else
return("OK")
end
end
function shell.cmd(ctx,c)
ret=shell.cmd_line(ctx,c)
if (ret == nil) then
return
end
ctx.stderr:print(shell.response(ret))
end
function shell.cmd_str(c)
str_ctx={}
str_ctx.stdin=iostr:new()
str_ctx.stdout=iostr:new()
str_ctx.stderr=iostr:new()
ret=shell.cmd_line(str_ctx,c)
return ret,str_ctx.stdout.data,str_ctx.stderr.data
end
function shell.filter(ctx,from,to,tomode,filterfunc,post)
local fd1=shell.open(ctx,from,"r")
if (fd1 == nil) then return -1 end
local fd2=shell.open(ctx,to,tomode)
local ret=0
if (fd2) then
while true do
str=fd1:readline()
if (str == nil) then
break
end
str=filterfunc(str)
if (str) then
if (shell.write(ctx,to,fd2,str) == nil) then
ret=-1
break
end
end
end
if (post) then
if (shell.write(ctx,to,fd2,post) == nil) then
ret=-1
end
end
fd2:close()
end
fd1:close()
return ret
end
function shell.on(ctx,data)
if (data == "\r" or data == "\n") then
ctx.stderr:write("\r\n")
shell.cmd(ctx,ctx.cmdline)
ctx.cmdline=""
shell.prompt(ctx)
elseif (data == "\b") then
local len=ctx.cmdline:len()
if (len > 0) then
ctx.cmdline=ctx.cmdline:sub(0,len-1)
ctx.stderr:write("\b \b")
end
else
ctx.cmdline=ctx.cmdline..data
ctx.stderr:write(data)
end
end
function shell.open(ctx,name,mode)
local fd=file.open(name,mode)
if (fd == nil) then
ctx.stderr:print("Failed to open '"..name.."'")
end
return fd
end
function shell.prompt(ctx)
ctx.stderr:write("# ")
end
function shell.rename(ctx,old,new)
file.remove(new)
if (file.rename(old,new)) then
return 0
end
ctx.stderr.print("Failed to rename '"+old+"' to '"+new+"'")
return -1
end
function shell.run()
uart_ctx={}
uart_ctx.stdin=io:new{write=function(self,str) uart.write(0, str) end}
uart_ctx.cmdline=""
uart.on(0,"data", 0, function(data) uart_ctx.stdin:on(data) end, 0)
uart_ctx.stdin.on=function(self,data) shell.on(uart_ctx,data) end
uart_ctx.stdout=uart_ctx.stdin;
uart_ctx.stderr=uart_ctx.stdin;
uart_ctx.exit=function(self) uart_ctx={}; uart.on("data") end
shell.prompt(uart_ctx)
end
function shell.words(str)
local args={}
for arg in str:gmatch("%S+") do table.insert(args, arg) end
return args
end
function shell.write(ctx,file,fd,data)
local ret=fd:write(data)
if (ret == nil) then
ctx.stderr:print("Failed to write to '"+file+"'")
end
return ret
end
io={}
function io:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function io.print(self,...)
local n = select("#",...)
for i = 1,n do
local v = tostring(select(i,...))
self:write(v)
if i~=n then self:write("\t") end
end
self:write("\n")
end
iostr=io:new()
function iostr.write(self, str)
self.data=(self.data or "")..str
end
shell.rehash()
return shell