Stary2001

Untitled

Jan 5th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. local funcs = {}
  4.  
  5. local _print = print
  6.  
  7. local _term_write = term.write
  8. local _term_clear = term.clear
  9. local _term_clearLine = term.clearLine
  10. local _term_scroll = term.scroll
  11. local _term_setCursorPos = term.setCursorPos
  12.  
  13. local _io_write = io.write
  14. local _io_read = io.read
  15.  
  16. local buf = {}
  17.  
  18. local client
  19.  
  20. function myWrite(s)
  21. buf[#buf+1]=s
  22. end
  23.  
  24. function myRead()
  25. rednet.send(client,"\1read")
  26.  
  27. local _,msg = rednet.receive()
  28.  
  29. return msg
  30. end
  31.  
  32. function myClear()
  33. rednet.send(client,"\1clear")
  34. end
  35.  
  36. function myClearLine()
  37. rednet.send(client,"\1clearLine")
  38. end
  39.  
  40. function myScroll()
  41. rednet.send(client,"\1scroll")
  42. end
  43.  
  44. function mySetCursorPos(x,y)
  45. rednet.send(client,"\1setCursorPos"..x..","..y)
  46. end
  47.  
  48. function hookIO()
  49. rawset(_G,"print",myWrite)
  50.  
  51. rawset(_G["term"],"write",myWrite)
  52. rawset(_G["term"],"clear",myClear)
  53. rawset(_G["term"],"clearLine",myClearLine)
  54. rawset(_G["term"],"scroll",myScroll)
  55. rawset(_G["term"],"setCursorPos",mySetCursorPos)
  56.  
  57. rawset(_G["io"],"write",myWrite)
  58. rawset(_G["io"],"read",myRead)
  59.  
  60. end
  61.  
  62. function unhookOutput()
  63. rawset(_G,"print",_print)
  64.  
  65. rawset(_G["term"],"write",_term_write)
  66. rawset(_G["term"],"clear",_term_clear)
  67. rawset(_G["term"],"clearLine",_term_clearLine)
  68. rawset(_G["term"],"scroll",_term_scroll)
  69. rawset(_G["term"],"setCursorPos",_term_setCursorPos)
  70.  
  71. rawset(_G["io"],"write",_io_write)
  72. rawset(_G["io"],"read",_io_read)
  73. end
  74.  
  75. function funcs.exe(id,args)
  76. local t = {}
  77. if #args~=2 then
  78. for i=2,#args do
  79. t[i-1]=args[i]
  80. end
  81. else
  82. t[1]=args[2]
  83. end
  84.  
  85. local run =
  86. function()
  87. shell.run(args[1],unpack(t))
  88. end
  89.  
  90. local reader =
  91. function()
  92. while #buf~=0 do
  93. rednet.send(id,buf[1])
  94. table.remove(buf,1)
  95. end
  96. end
  97.  
  98. client = id
  99. hookIO()
  100. run()
  101. unhookIO()
  102. reader()
  103. rednet.send(id,"\1done")
  104. end
  105.  
  106. function process(id,msg)
  107. local rest = string.gmatch(msg,"cmd:(.+)$")()
  108.  
  109. local func,args = string.gmatch(rest,"^(.-):(.+)$")()
  110. local t = {}
  111. for v in string.gmatch(args,"[^:]+") do
  112. t[#t+1]=v
  113. end
  114.  
  115. funcs[func](id,t)
  116. end
  117.  
  118. while true do
  119. local id,msg = rednet.receive()
  120. if msg:sub(1,4) == "cmd:" then
  121. process(id,msg)
  122. end
  123. end
  124.  
  125. rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment