Advertisement
Guest User

oc netcat fixed

a guest
Jul 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. local shell = require("shell")
  2. local fs = require("filesystem")
  3. local component = require("component")
  4. local event = require("event")
  5.  
  6. if not component.modem then
  7. error("network card required")
  8. end
  9.  
  10. local modem = component.modem
  11.  
  12. local args, options = shell.parse(...)
  13. if #args < 2 then
  14. error("usage: netcat [-r: receive] <port> <file [use '-' for stdin / recv end stdout]>")
  15. end
  16.  
  17. local port = tonumber(args[1])
  18.  
  19. if not port then
  20. error("invalid port")
  21. end
  22. local file = args[2]
  23.  
  24. if options.r then
  25. modem.open(port)
  26.  
  27. local input_method, input_param = "write", require("tty").getViewport()
  28.  
  29. --for i = 1, #args do
  30. local arg = shell.resolve(args[2])
  31. if fs.isDirectory(arg) then
  32. io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
  33. os.exit(1)
  34. else
  35. local file, reason
  36. if args[2] == "-" then
  37. file, reason = io.stdout, "missing stdout"
  38. input_method, input_param = "write", false
  39. else
  40. file, reason = fs.open(arg, "w")
  41. end
  42. if not file then
  43. io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
  44. os.exit(1)
  45. else
  46. while true do
  47. local _, _, _, _, _, msg = event.pull("modem_message")
  48. if msg ~= "EOF" then
  49. file[input_method](file, msg)
  50. else
  51. print("file received")
  52. break
  53. end
  54. end
  55. file:close()
  56. end
  57. end
  58. --end
  59. else
  60. local input_method, input_param = "read", require("tty").getViewport()
  61.  
  62. --for i = 1, #args do
  63. local arg = shell.resolve(args[2])
  64. if fs.isDirectory(arg) then
  65. io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
  66. os.exit(1)
  67. else
  68. local file, reason
  69. if args[2] == "-" then
  70. file, reason = io.stdin, "missing stdin"
  71. input_method, input_param = "readLine", false
  72. else
  73. file, reason = fs.open(arg)
  74. end
  75. if not file then
  76. io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
  77. os.exit(1)
  78. else
  79. repeat
  80. local chunk = file[input_method](file, input_param)
  81. if chunk then
  82. --io.write(chunk)
  83. modem.broadcast(port, chunk)
  84. end
  85. until not chunk
  86. modem.broadcast(port, "EOF")
  87. print("file sent")
  88. file:close()
  89. end
  90. end
  91. --end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement