-- tinyftp.lua -- standalone version -- by Edrx - version: 2005-12-26 -- Tenho que ser capaz de testar isso com LUA_INIT="". -- setenv -- (let ((process-environment `("LUA_INIT=" . ,process-environment))) -- (find-luasocket20file "src/") -- (find-luasocket20file "src/ftp.lua") -- (find-luasocket20file "src/ftp.lua" "function override(t)") -- (find-eevfile "eev-dev.el" "defun ee-expand") ee_expand = function (path) path = string.gsub(path, "^~$", "$HOME/", 1) path = string.gsub(path, "^~/", "$HOME/", 1) path = string.gsub(path, "^%$(%w+)", os.getenv, 1) return path end readfile = function (fname) local f = assert(io.open(fname, "rb")) local bigstr = f:read("*a") f:close() return bigstr end writefile = function (fname, bigstr) local f = assert(io.open(fname, "w+b")) f:write(bigstr) f:close() end fname_nondir = function (str) local _, _, nondir = string.find(str, "([^/]*)$") return nondir end isdir = function (str) return fname_nondir(str) == "" end isftp = function (str) return (string.find(str, "ftp://")) end normalize_fname = normalize_fname or function (str) str = ee_expand(str) str = gsub(str, "\\", "/") str = gsub(str, "^mach1:", "ftp://user1:passwd1@machine1/") return str end -- 2005-12-12 -- tinyftp and his lower buddies -- These functions support three kinds of "filenames": "ftp://...", -- "-" (for stdin/stdout), and "other" (local files). -- All ftp transfers are done in binary mode. -- For simplicity, connections are never reused. tinyftpwrite = function (fname, stuff) fname = normalize_fname(fname) if fname == "-" then io.write(stuff); return "stdout" elseif isftp(fname) then ftp.put(fname..";type=i", stuff); return "ftp" else writefile(fname, stuff) return "file" end end tinyftpread = function (fname) fname = normalize_fname(fname) if fname == "-" then return io.read("*a"), "stdout" elseif isftp(fname) then return ftp.get(fname..";type=i"), "ftp" else return readfile(fname), "file" end end tinyftpls = function (fname) fname = normalize_fname(fname) local t = {} local p = url.parse(fname) p.command = "list" p.sink = ltn12.sink.table(t) local r, e = ftp.get(p) t = tounix(table.concat(t)) t = string.gsub(t, "(\nd[^\n]*)", "%1/") -- add slashes to dirs return r and t, e end -- (find-alides "edrx/") -- (find-alides "src/sync_FOBA_bcorp/") tinyftpcopy = function (arg1, arg2) arg1 = normalize_fname(arg1) arg2 = normalize_fname(arg2) if isdir(arg2) then args2 = arg2 .. fname_nondir(arg1) end local stuff, inputtype = tinyftpread(arg1) local outputtype = tinyftpwrite(arg2, stuff) return format("%s -> %d bytes -> %s", inputtype, strlen(stuff), outputtype) end tinyftp = function (arg1, arg2) if (arg2 or "") == "" then -- if we only got one argument if isdir(normalize_fname(arg1)) -- if it is an ftp directory then print((tinyftpftpls(arg1))) -- then list it else io.write(tinyftpread(arg1)) -- otherwise cat it end else -- if we got two arguments if arg2 == "-" -- if the target is stdout then tinyftpcopy(arg1, arg2) -- then just cat the input else print(tinyftpcopy(arg1, arg2)) -- otherwise print "src -> nbytes -> tgt" end end end -- Missing: support for "file:///" urls