FTP.Mod 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. MODULE FTP;
  2. IMPORT
  3. FTPClient, Commands, Streams, Files, Texts, TextUtilities;
  4. CONST
  5. BufSize = 16*1024; (* internal buffer size, used for file transfer *)
  6. LocalFileNotFound = -2;
  7. VAR ftp : FTPClient.FTPClient;
  8. PROCEDURE PutFile(ftp : FTPClient.FTPClient; local, remote : ARRAY OF CHAR; VAR res : WORD);
  9. VAR buf: ARRAY BufSize OF CHAR; len: LONGINT;
  10. f : Files.File; r : Files.Reader;
  11. w : Streams.Writer;
  12. BEGIN
  13. f := Files.Old(local);
  14. IF f = NIL THEN res := LocalFileNotFound; RETURN END;
  15. Files.OpenReader(r, f, 0);
  16. ftp.OpenPut(remote, w, res);
  17. IF res = 0 THEN
  18. REPEAT
  19. r.Bytes(buf, 0, BufSize, len); w.Bytes(buf, 0, len);
  20. UNTIL r.res # 0;
  21. w.Update;
  22. ftp.ClosePut(res);
  23. END;
  24. END PutFile;
  25. PROCEDURE PutText(ftp : FTPClient.FTPClient; local, remote : ARRAY OF CHAR; VAR res : WORD);
  26. VAR w : Streams.Writer;
  27. text: Texts.Text;
  28. r: Texts.TextReader;
  29. ch: Texts.Char32;
  30. i: LONGINT;
  31. BEGIN
  32. NEW(text);
  33. TextUtilities.LoadOberonText(text, local, res);
  34. IF res # 0 THEN res:= LocalFileNotFound; RETURN END;
  35. text.AcquireRead;
  36. NEW(r, text);
  37. ftp.OpenPut(remote, w, res);
  38. IF res = 0 THEN
  39. FOR i := 0 TO text.GetLength() - 1 DO
  40. r.ReadCh(ch);
  41. IF (ch >= 0) & (ch < 128) THEN w.Char(CHR(ch)) END;
  42. END;
  43. w.Update;
  44. ftp.ClosePut(res)
  45. END;
  46. text.ReleaseRead
  47. END PutText;
  48. PROCEDURE GetFile(ftp : FTPClient.FTPClient; remote, local : ARRAY OF CHAR; VAR res : WORD);
  49. VAR buf: ARRAY BufSize OF CHAR; len: LONGINT;
  50. f : Files.File; w : Files.Writer;
  51. r : Streams.Reader;
  52. BEGIN
  53. f := Files.New(local);
  54. Files.OpenWriter(w, f, 0);
  55. ftp.OpenGet(remote, r, res);
  56. IF res = 0 THEN
  57. REPEAT
  58. r.Bytes(buf, 0, BufSize, len); w.Bytes(buf, 0, len);
  59. UNTIL r.res # 0;
  60. w.Update;
  61. Files.Register(f);
  62. ftp.CloseGet(res)
  63. END;
  64. END GetFile;
  65. PROCEDURE Open*(context : Commands.Context);
  66. VAR
  67. host, user, password : ARRAY 256 OF CHAR;
  68. res : WORD;
  69. BEGIN
  70. IF ftp # NIL THEN
  71. context.out.String("Already open"); context.out.Ln;
  72. RETURN;
  73. END;
  74. context.arg.SkipWhitespace; context.arg.String(host);
  75. context.arg.SkipWhitespace; context.arg.String(user);
  76. context.arg.SkipWhitespace; context.arg.String(password);
  77. context.out.String("host = "); context.out.String(host); context.out.Ln;
  78. context.out.String("user = "); context.out.String(user); context.out.Ln;
  79. context.out.String("password = "); context.out.String(password); context.out.Ln;
  80. NEW(ftp);
  81. ftp.Open(host, user, password, 21, res);
  82. context.out.String(ftp.msg);
  83. IF res = 0 THEN
  84. context.out.String("Connected"); context.out.Ln;
  85. ELSE
  86. ftp := NIL;
  87. context.out.String("Connecting failed"); context.out.Ln;
  88. END;
  89. END Open;
  90. PROCEDURE PutFiles*(context : Commands.Context);
  91. VAR
  92. local, path, remote : ARRAY 256 OF CHAR;
  93. tok : ARRAY 8 OF CHAR;
  94. res : WORD;
  95. BEGIN
  96. IF ftp = NIL THEN
  97. context.out.String("not connected"); context.out.Ln;
  98. RETURN;
  99. END;
  100. REPEAT
  101. context.arg.SkipWhitespace; context.arg.String(local);
  102. Files.SplitPath(local, path, remote);
  103. context.arg.SkipWhitespace;
  104. IF context.arg.Peek() = "=" THEN
  105. context.arg.Token(tok);
  106. IF tok # "=>" THEN
  107. context.out.String("=> expected");
  108. RETURN;
  109. END;
  110. context.arg.SkipWhitespace; context.arg.String(remote)
  111. END;
  112. IF (local # "") & (remote # "") THEN
  113. PutFile(ftp, local, remote, res);
  114. IF res = 0 THEN context.out.String(local); context.out.String(" copied to "); context.out.String(remote); context.out.Ln
  115. ELSIF res = LocalFileNotFound THEN context.out.String("Local file "); context.out.String(local); context.out.String(" not found "); context.out.Ln
  116. ELSE context.out.String("upload failed on remote file "); context.out.String(remote); context.out.Ln
  117. END;
  118. END
  119. UNTIL context.arg.res # 0;
  120. END PutFiles;
  121. PROCEDURE PutTexts*(context : Commands.Context);
  122. VAR
  123. local, path, remote : ARRAY 256 OF CHAR;
  124. tok : ARRAY 8 OF CHAR;
  125. res : WORD;
  126. BEGIN
  127. IF ftp = NIL THEN
  128. context.out.String("not connected"); context.out.Ln;
  129. RETURN;
  130. END;
  131. REPEAT
  132. context.arg.SkipWhitespace; context.arg.String(local);
  133. Files.SplitPath(local, path, remote);
  134. context.arg.SkipWhitespace;
  135. IF context.arg.Peek() = "=" THEN
  136. context.arg.Token(tok);
  137. IF tok # "=>" THEN
  138. context.out.String("=> expected");
  139. RETURN;
  140. END;
  141. context.arg.SkipWhitespace; context.arg.String(remote);
  142. END;
  143. IF (local # "") & (remote # "") THEN
  144. PutText(ftp, local, remote, res);
  145. IF res = 0 THEN context.out.String(local); context.out.String(" copied to "); context.out.String(remote); context.out.Ln
  146. ELSIF res = LocalFileNotFound THEN context.out.String("Local file "); context.out.String(local); context.out.String(" not found "); context.out.Ln
  147. ELSE context.out.String("upload failed on remote file "); context.out.String(remote); context.out.Ln
  148. END;
  149. END
  150. UNTIL context.arg.res # 0;
  151. END PutTexts;
  152. PROCEDURE GetFiles*(context : Commands.Context);
  153. VAR
  154. local, remote : ARRAY 256 OF CHAR;
  155. tok : ARRAY 8 OF CHAR;
  156. res : WORD;
  157. BEGIN
  158. IF ftp = NIL THEN
  159. context.out.String("not connected"); context.out.Ln;
  160. RETURN;
  161. END;
  162. REPEAT
  163. context.arg.SkipWhitespace; context.arg.String(remote);
  164. COPY(remote, local);
  165. context.arg.SkipWhitespace;
  166. IF context.arg.Peek() = "=" THEN
  167. context.arg.Token(tok);
  168. IF tok # "=>" THEN
  169. context.out.String("=> expected");
  170. RETURN;
  171. END;
  172. context.arg.SkipWhitespace; context.arg.String(local);
  173. END;
  174. IF (local # "") & (remote # "") THEN
  175. GetFile(ftp, remote, local, res);
  176. IF res = 0 THEN context.out.String(remote); context.out.String(" downloaded to "); context.out.String(local); context.out.Ln
  177. ELSE context.out.String("download failed on remote file "); context.out.String(remote); context.out.Ln
  178. END;
  179. END
  180. UNTIL context.arg.res # 0;
  181. END GetFiles;
  182. PROCEDURE MakeDir*(context : Commands.Context);
  183. VAR
  184. path : ARRAY 256 OF CHAR;
  185. res : WORD;
  186. BEGIN
  187. IF ftp = NIL THEN
  188. context.out.String("not open"); context.out.Ln;
  189. RETURN;
  190. END;
  191. context.arg.String(path);
  192. ftp.MakeDir(path, res);
  193. IF res = 0 THEN context.out.String("Directory created."); context.out.Ln
  194. ELSE context.out.String("Failed creating directory."); context.out.Ln
  195. END;
  196. END MakeDir;
  197. PROCEDURE ChangeDir*(context : Commands.Context);
  198. VAR
  199. path : ARRAY 256 OF CHAR;
  200. res : WORD;
  201. BEGIN
  202. IF ftp = NIL THEN
  203. context.out.String("not open"); context.out.Ln;
  204. RETURN;
  205. END;
  206. context.arg.String(path);
  207. ftp.ChangeDir(path, res);
  208. IF res = 0 THEN context.out.String("Directory changed."); context.out.Ln
  209. ELSE context.out.String("Failed changing directory."); context.out.Ln
  210. END;
  211. ftp.GetCurrentDir(path, res);
  212. IF res = 0 THEN context.out.String("New remote dir is : "); context.out.String(path); context.out.Ln END;
  213. END ChangeDir;
  214. PROCEDURE Directory*(context : Commands.Context);
  215. VAR i : LONGINT;
  216. BEGIN
  217. IF ftp = NIL THEN
  218. context.out.String("not open"); context.out.Ln;
  219. RETURN;
  220. END;
  221. ftp.EnumerateDir("");
  222. FOR i := 0 TO ftp.nofEntries-1 DO
  223. context.out.String(ftp.listing[i].full); context.out.Ln;
  224. END;
  225. END Directory;
  226. PROCEDURE Close*(context : Commands.Context);
  227. VAR res : WORD;
  228. BEGIN
  229. IF ftp = NIL THEN
  230. context.out.String("not connected"); context.out.Ln;
  231. RETURN;
  232. END;
  233. ftp.Close(res);
  234. context.out.String("closed."); context.out.String(ftp.msg); context.out.Ln;
  235. ftp := NIL;
  236. END Close;
  237. END FTP.
  238. FTP.Open www.ocp.inf.ethz.ch "ocp" "download" ~
  239. FTP.Directory ~
  240. FTP.ChangeDir <directoryname> ~
  241. FTP.PutFiles <list of filenames> ~
  242. FTP.GetFiles <list of filenames> ~
  243. FTP.Close ~
  244. SystemTools.Free FTP FTPClient ~