OC-PsychOS/exec/nsh.lua

27 lines
734 B
Lua

tA = {...}
local h,p = tA[1], tonumber(tA[2]) or 23
local function nshcw(h,p)
while true do
local sI = os.getenv("sI")
local ev = {event.pull()}
if ev[1] == "net_msg" and ev[2] == h and ev[3] == p and ev[4]:sub(1,1) == "d" then
write(ev[4]:sub(2))
elseif ev[1] == "net_msg" and ev[2] == h and ev[3] == p and ev[4]:sub(1,1) == "\27" then
break
elseif ev[1] == "key" and ev[2] == sI then
net.send(h,p,string.char(ev[3],ev[4]))
end
end
end
net.send(h,p,"initnsh2")
local bt = computer.uptime()
local tp,src,port,msg
repeat
tp, src, port, msg = event.pull()
until (tp == "net_msg" and src == h and port == p) or computer.uptime() > bt+16
if tp == "net_msg" then
nshcw(h,tonumber(msg))
else
print("timeout.")
end