OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

2 years ago
  1. local shell = require("shell")
  2. local internet = require("internet")
  3. local fs = require("filesystem")
  4. local args = {...}
  5. -- Split string based on delimiter
  6. function split(s, sep)
  7. if sep == nil then
  8. sep = "%s"
  9. end
  10. local t = {}
  11. for str in string.gmatch(s, "([^"..sep.."]+)") do
  12. table.insert(t, str)
  13. end
  14. return t
  15. end
  16. -- Slice table
  17. function slice(tbl, first, last, step)
  18. local sliced = {}
  19. for i = first or 1, last or #tbl, step or 1 do
  20. sliced[#sliced+1] = tbl[i]
  21. end
  22. return sliced
  23. end
  24. -- Error checking
  25. if args[1] == nil then
  26. print("URL must exist")
  27. return
  28. end
  29. args[1] = "http://mc.bashed.rocks:13699/"..args[1]
  30. -- Get local filename
  31. local fileName = ""
  32. if args[2] == nil then
  33. local t = split(args[1], "/")
  34. fileName = t[#t]
  35. else
  36. fileName = args[2]
  37. end
  38. local newArgs = slice(args, 3, #args-2)
  39. -- Sandbox so we don't write to the screen
  40. sandbox_env = {
  41. assert = assert,
  42. require = require,
  43. pairs = pairs,
  44. pcall = pcall,
  45. tostring = tostring,
  46. string = string,
  47. io = {write = function(...) end, read = io.read, open = io.open, stderr = io.stderr}
  48. }
  49. pcall(fs.remove, fileName)
  50. shell.execute("wget -f "..args[1].." "..fileName, sandbox_env)
  51. -- Run downloaded file
  52. local c = fileName
  53. for _, i in ipairs(newArgs) do c = c.." "..i end
  54. shell.execute(c)