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.

143 lines
3.3 KiB

local component = require("component")
local event = require("event")
local fs = require("filesystem")
local io = require("io")
local shell = require("shell")
local term = require("term")
local m = component.proxy(component.list("modem")())
args = {...}
function doCommand(server, port, cwd, scwd)
term.write(">> ")
input = io.read()
if input == "exit" or input == "bye" or input == "quit" then
print("Bye")
return
end
t = split(input)
if t[1] == "clear" then
shell.execute("clear")
elseif t[1] == "lls" then
local ls = {}
if t[2] == nil then
ls = fs.list(cwd)
else
ls = fs.list(cwd.."/"..t[2])
end
for _,v in ipairs(ls) do
print(v)
end
elseif t[1] == "ls" then
if t[2] == nil then
m.send(server, port, t[1].." "..scwd)
else
m.send(server, port, t[1].." "..scwd.."/"..t[2])
end
_, _, from, port, _, msg = event.pull("modem_message")
for _, v in ipairs(msg) do
print(v)
end
elseif t[1] == "lcd" then
if t[2] == nil then
cwd = "/"
else
if t[2]:gmatch(".") == "/" then
cwd = t[2]
--Didn't handle .. operator
else
if cwd == "/" then
cwd = cwd..t[2]
else
cwd = cwd.."/"..t[2]
end
end
end
elseif t[1] == "cd" then
if t[2] == nil then
scwd = "/"
else
if t[2]:gmatch(".") == "/" then
scwd = t[2]
--Didn't handle .. operator
else
if scwd == "/" then
scwd = scwd..t[2]
else
scwd = scwd.."/"..t[2]
end
end
end
elseif t[1] == "put" then
if t[2] == nil then
print("Please supply a file to put")
else
ls = fs.list(cwd)
--Bad search algorithm
isFile = false
for _,v in ipairs(ls) do
if v == t[2] then
isFile = true
end
end
if isFile then
if cwd == "/" then
file = cwd..t[2]
else
file = cwd.."/"..t[2]
end
f = fs.open(file, "r")
m.send(server, port, t[1].." "..file.." "..f.readAll())
_, _, from, port, _, msg = event.pull("modem_message")
print(msg)
else
print("File not found")
end
end
elseif t[1] == "pull" then
if t[2] == nil then
print("Please supply a file to pull")
else
if scwd == "/" then
file = scwd..t[2]
else
file = scwd.."/"..t[2]
end
m.send(server, port, t[1].." "..file)
_, _, from, port, _, msg = event.pull("modem_message")
if msg == "ERROR" then
print("File not found")
else
print(msg)
_, _, from, port, _, msg = event.pull("modem_message")
f = io.open(file, "w")
f:write(msg)
f:close()
end
end
end
doCommand(m, id, from, cwd, scwd)
end
if m == nil then
print("No modems found")
return
end
local port = 21
if args[1] ~= nil then
port = tonumber(args[1])
end
local server = nil
if args[2] == nil then
f = io.open("addr", "r")
server = f:read()
f:close()
else
server = args[2]
end
print("FTP CLIENT")
doCommand(server, port, shell.getPath(), "/")