forked from mkottman/AndroLua
-
Notifications
You must be signed in to change notification settings - Fork 10
/
alshell
166 lines (148 loc) · 3.75 KB
/
alshell
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
#!/usr/bin/env lua
require 'socket'
local has_linenoise, linenoise = pcall(require,'linenoise')
local has_posix, posix = pcall(require,'posix')
if has_linenoise then
function readline(prompt)
local line, err = linenoise.linenoise(prompt)
if not line then return nil end
linenoise.historyadd(line)
return line
end
else
function readline(prompt)
io.write(prompt)
return io.read()
end
end
interp = {}
local macros = {}
function interp.macro (def)
macros[def.name] = def
end
local function expand_macro (m,a)
if m.arg then
if not a then
a = m.last_arg
if not a then
return print 'you need to pass a parameter to this macro'
end
else
m.last_arg = a
end
else
a = '' -- keep format happy
end
for i,line in ipairs(m) do
local cmd = line:format(a)
print('! '..cmd)
interp.process_line(cmd)
end
end
local f,err = io.open 'defs.lua'
if f then
f:close()
dofile 'defs.lua'
end
local t,c2,err
local addr = arg[1] or 'localhost'
local c = socket.connect(addr,3333)
print('connected to '..addr..':3333')
local req = arg[2] or 'yes'
c:send (req..'\n')
if req == 'yes' then
if not has_posix then
print 'you need luaposix for accessing log socket'
else
c:receive()
c2,err = socket.connect(addr,3334)
if err then
return print('cannot connect to secondary socket',err)
end
t = posix.fork()
if t == 0 then -- run function as child
while true do
local res = c2:receive()
res = res:gsub('\001','\n')
io.write(res)
end
posix._exit(0)
end
end
end
local log = io.open('log.txt','a')
local function prep_for_send (s)
return (s:gsub('\n','\001'))
end
function readfile(file)
local f,err = io.open(file)
if not f then return nil,err end
local contents = f:read '*a'
f:close()
return contents
end
function eval(line)
c:send(line..'\n')
local res = c:receive()
return (res or '?'):gsub('\001','\n')
end
print 'Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio'
local init,err = readfile 'init.lua'
if init then
print 'loading init.lua'
eval(prep_for_send(init))
end
function interp.process_line (line)
log:write(line,'\n')
local cmd,file = line:match '^%.(%S+)(.*)$'
if cmd then
file = file:gsub('^%s*','')
file = #file > 0 and file or nil
if macros[cmd] then
expand_macro(macros[cmd],file)
return
elseif file then -- either .l (load) or .m (upload module)
local mod,kind = file,'run'
if cmd == 'm' then -- given in Lua module form
file = mod:gsub('%.','/')..'.lua'
kind = 'mod'
end
line,err = readfile(file)
if err then
return print(err)
end
if kind == 'mod' then
local ok,err = loadstring(line)
if not ok then return print(err) end
end
line = prep_for_send(line)
line = '--'..kind..':'..mod..'\001'..line
else
return print 'unknown command'
end
else
-- = EXPR becomes print(EXPR)
local expr = line:match '^%s*=%s*(.+)$'
if expr then
line = 'print('..expr..')'
end
end
if line then
local res = eval(line)
log:write(res,'\n')
io.write(res)
else
print(err)
end
end
local line = readline '> '
while line do
interp.process_line(line)
line = readline '> '
end
log:close()
c:close()
if c2 then
c2:close()
if t then posix.kill(t) end
end