Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- local funcs = {}
- local _print = print
- local _term_write = term.write
- local _term_clear = term.clear
- local _term_clearLine = term.clearLine
- local _term_scroll = term.scroll
- local _term_setCursorPos = term.setCursorPos
- local _io_write = io.write
- local _io_read = io.read
- local _os_pullEvent = os.pullEvent
- local buf = {}
- local client
- function myWrite(s)
- buf[#buf+1]=s
- end
- function myRead()
- rednet.send(client,"\1read")
- local _,msg = rednet.receive()
- return msg
- end
- function myClear()
- rednet.send(client,"\1clear")
- end
- function myClearLine()
- rednet.send(client,"\1clearLine")
- end
- function myScroll()
- rednet.send(client,"\1scroll")
- end
- function mySetCursorPos(x,y)
- rednet.send(client,"\1setCursorPos"..x..","..y)
- end
- local keybuf = {}
- local charbuf = {}
- function myPullEvent(filt)
- if #keybuf ~= 0 then
- if not filt or filt=="key" then
- local k = keybuf[1]
- table.remove(keybuf,1)
- return "key",k
- end
- end
- if #charbuf ~= 0 then
- if not filt or filt=="char" then
- local c = charbuf[1]
- table.remove(charbuf,1)
- return "char",tonumber(c)
- end
- end
- return _os_pullEvent(filt)
- end
- function hookIO()
- rawset(_G,"print",myWrite)
- rawset(_G["term"],"write",myWrite)
- rawset(_G["term"],"clear",myClear)
- rawset(_G["term"],"clearLine",myClearLine)
- rawset(_G["term"],"scroll",myScroll)
- rawset(_G["term"],"setCursorPos",mySetCursorPos)
- rawset(_G["io"],"write",myWrite)
- rawset(_G["io"],"read",myRead)
- rawset(_G["os"],"pullEvent",myPullEvent)
- end
- function unhookIO()
- rawset(_G,"print",_print)
- rawset(_G["term"],"write",_term_write)
- rawset(_G["term"],"clear",_term_clear)
- rawset(_G["term"],"clearLine",_term_clearLine)
- rawset(_G["term"],"scroll",_term_scroll)
- rawset(_G["term"],"setCursorPos",_term_setCursorPos)
- rawset(_G["io"],"write",_io_write)
- rawset(_G["io"],"read",_io_read)
- rawset(_G["os"],"pullEvent",_os_pullEvent)
- end
- function funcs.exe(id,args)
- local prog = args[1]
- table.remove(args,1)
- local run =
- function()
- hookIO()
- shell.run(prog,unpack(args))
- --unhookIO()
- end
- local reader =
- function()
- while #buf~=0 do
- rednet.send(id,buf[1])
- table.remove(buf,1)
- end
- end
- client = id
- run()
- reader()
- rednet.send(id,"\1done")
- end
- function funcs.key(id,args)
- keybuf[#keybuf+1] = args[1]
- end
- function funcs.char(id,args)
- charbuf[#charbuf+1] = args[1]
- end
- function process(id,msg)
- local rest = string.gmatch(msg,"cmd:(.+)$")()
- local func,args = string.gmatch(rest,"^(.-):(.+)$")()
- local t = {}
- for v in string.gmatch(args,"[^:]+") do
- t[#t+1]=v
- end
- funcs[func](id,t)
- end
- while true do
- local id,msg = rednet.receive()
- if msg:sub(1,4) == "cmd:" then
- process(id,msg)
- end
- end
- rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment