forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFTP.Mod
1279 lines (1217 loc) · 29.7 KB
/
FTP.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;type=A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE FTP; (** portable *) (* EJZ, *)
IMPORT Files, Strings, Input, Display, Fonts, Texts, Oberon, NetSystem;
(** A simple single session FTP Tool using commands. Useful for transfering many files to or from the
same server. *)
CONST
MaxLine = 1024; BufLen = MaxLine; MaxTry = 1000;
Tab = 9X; Esc = 01BX; BreakChar = Esc;
Done = 0; NotReady = 1; NotConnected = 2; WrongUser = 3; WrongPassword = 4; TimedOut = 5; LocFileNotFound = 6;
Interrupted = 7; Disconnected = 8; Failed = MAX(INTEGER);
MinDataPort = 1100; MaxDataPort = 1500;
Unknown = -1; UNIX = 0; VMS = 1;
DefConPort = 21;
TYPE
Session = POINTER TO SessionDesc;
SessionDesc = RECORD
C: NetSystem.Connection;
dataC: NetSystem.Connection;
reply: ARRAY MaxLine OF CHAR;
portIPAddress: ARRAY 64 OF CHAR;
dataIP: NetSystem.IPAdr;
dataPort, status, system, res: INTEGER;
ack: BOOLEAN
END;
EnumProc = PROCEDURE (entry: ARRAY OF CHAR);
VAR
S: Session;
W: Texts.Writer;
log: Texts.Text;
line: ARRAY MaxLine OF CHAR;
buffer: ARRAY BufLen OF CHAR;
timeOut: LONGINT;
dataPort, col: INTEGER;
PROCEDURE Connected(C: NetSystem.Connection; mode: INTEGER): BOOLEAN;
VAR state: INTEGER;
BEGIN
state := NetSystem.State(C);
RETURN state IN {mode, NetSystem.inout}
END Connected;
PROCEDURE Disconnect(VAR C: NetSystem.Connection);
BEGIN
IF C # NIL THEN
NetSystem.CloseConnection(C)
END;
C := NIL
END Disconnect;
PROCEDURE Connect(VAR C: NetSystem.Connection; port: INTEGER; host: ARRAY OF CHAR): BOOLEAN;
VAR
adr: NetSystem.IPAdr;
res: INTEGER;
BEGIN
NetSystem.GetIP(host, adr);
IF adr = NetSystem.anyIP THEN
C := NIL; RETURN FALSE
END;
NetSystem.OpenConnection(C, NetSystem.anyport, adr, port, res);
IF res # NetSystem.done THEN
C := NIL
END;
RETURN res = NetSystem.done
END Connect;
PROCEDURE UserBreak(): BOOLEAN;
VAR ch: CHAR;
BEGIN
IF Input.Available() > 0 THEN
Input.Read(ch);
IF ch = BreakChar THEN
Texts.WriteString(W, "interrupted");
Texts.WriteLn(W);
Texts.Append(Oberon.Log, W.buf);
RETURN TRUE
END
END;
RETURN FALSE
END UserBreak;
PROCEDURE ReadResponse(S: Session; VAR sline: ARRAY OF CHAR);
VAR
time, i, j, cpos: LONGINT;
code: ARRAY 8 OF CHAR;
line: ARRAY MaxLine OF CHAR;
BEGIN
IF ~Connected(S.C, NetSystem.in) THEN
sline := "Connection closed by server.";
S.reply := sline;
S.status := 0; S.res := Disconnected;
RETURN
END;
time := NetSystem.Available(S.C);
NetSystem.ReadString(S.C, line);
IF log # NIL THEN
Texts.WriteString(W, line);
Texts.WriteLn(W); Texts.Append(log, W.buf)
END;
Strings.StrToInt(line, time); S.status := SHORT(time);
Strings.IntToStr(time, code);
cpos := 0;
WHILE code[cpos] # 0X DO
INC(cpos)
END;
i := cpos+1; j := 0;
WHILE line[i] # 0X DO
sline[j] := line[i];
INC(j); INC(i)
END;
sline[j] := 0X;
time := Input.Time();
IF line[cpos] = "-" THEN
LOOP
IF NetSystem.Available(S.C) > 0 THEN
line[cpos] := 0X;
NetSystem.ReadString(S.C, line);
IF log # NIL THEN
Texts.WriteString(W, line);
Texts.WriteLn(W); Texts.Append(log, W.buf)
END;
IF line[cpos] # "-" THEN
line[cpos] := 0X;
IF line = code THEN
EXIT
END
END;
time := Input.Time()
ELSIF (Input.Time()-time) >= timeOut THEN
S.res := TimedOut;
RETURN
ELSIF UserBreak() THEN
S.res := Interrupted;
RETURN
END
END
END;
S.ack := TRUE
END ReadResponse;
PROCEDURE SendString(C: NetSystem.Connection; str: ARRAY OF CHAR);
VAR i: LONGINT;
BEGIN
i := 0;
WHILE str[i] # 0X DO
INC(i)
END;
NetSystem.WriteBytes(C, 0, i, str)
END SendString;
PROCEDURE SendLine(C: NetSystem.Connection; VAR str: ARRAY OF CHAR);
BEGIN
SendString(C, str);
NetSystem.WriteBytes(C, 0, 2, Strings.CRLF)
END SendLine;
PROCEDURE SendCmd(S: Session; str: ARRAY OF CHAR);
BEGIN
IF ~S.ack THEN
ReadResponse(S, line)
ELSE
S.ack := FALSE
END;
SendLine(S.C, str)
END SendCmd;
PROCEDURE CloseS(S: Session);
BEGIN
S.ack := TRUE;
SendCmd(S, "QUIT"); ReadResponse(S, S.reply);
Disconnect(S.dataC); Disconnect(S.C);
S.res := Done
END CloseS;
PROCEDURE Close2(S: Session);
BEGIN
S.ack := TRUE;
SendCmd(S, "QUIT");
Disconnect(S.dataC); Disconnect(S.C)
END Close2;
PROCEDURE QuerySystem(S: Session);
VAR pos: LONGINT;
BEGIN
S.system := UNIX;
SendCmd(S, "SYST"); ReadResponse(S, line);
IF (S.status >= 200) & (S.status < 300) THEN
pos := 0;
Strings.Search("VMS", line, pos);
IF pos >= 0 THEN
S.system := VMS
END
END
END QuerySystem;
PROCEDURE QueryString(key: ARRAY OF CHAR; VAR s: ARRAY OF CHAR): BOOLEAN;
VAR S: Texts.Scanner; lKey: ARRAY 32 OF CHAR;
BEGIN
lKey := "NetSystem."; Strings.Append(lKey, key);
Oberon.OpenScanner(S, lKey);
IF S.class IN {Texts.Name, Texts.String} THEN
s := S.s
ELSE
s := ""
END;
RETURN s # ""
END QueryString;
PROCEDURE GetLogin(VAR host, usr, passw: ARRAY OF CHAR);
BEGIN
IF (usr = "ftp") OR (usr = "anonymous") OR (usr = "") THEN
IF ~QueryString("EMail", passw) OR (passw[0] = "<") THEN
passw := "[email protected]"
END;
IF usr = "" THEN
usr := "anonymous"
END
ELSIF passw = "" THEN
NetSystem.GetPassword("ftp", host, usr, passw)
END
END GetLogin;
PROCEDURE OpenS(server, user, passwd: ARRAY OF CHAR; port: INTEGER; VAR S: Session);
BEGIN
GetLogin(server, user, passwd);
NEW(S); S.dataC := NIL; S.dataPort := -1;
IF NetSystem.hostIP = NetSystem.anyIP THEN
S.C := NIL;
S.reply := "invalid NetSystem.hostIP";
S.res := Failed;
RETURN
END;
S.system := Unknown;
S.reply := "connecting failed";
S.portIPAddress := "";
S.ack := TRUE;
IF (user = "") OR (passwd = "") THEN
S.res := Failed;
S.reply := "no password or username specified";
RETURN
END;
IF Connect(S.C, port, server) THEN
ReadResponse(S, S.reply);
IF (S.status >= 200) & (S.status < 300) THEN
line := "USER "; Strings.Append(line, user);
SendCmd(S, line); ReadResponse(S, line);
IF (S.status = 330) OR (S.status = 331) THEN
line := "PASS "; Strings.Append(line, passwd);
SendCmd(S, line); ReadResponse(S, line);
IF (S.status = 230) OR (S.status= 330) THEN
S.res := Done
ELSE
S.res := WrongPassword; S.reply := line;
Close2(S)
END
ELSIF S.status # 230 THEN
S.res := WrongUser; S.reply := line;
Close2(S)
ELSE
S.res := Done
END;
IF S.res # Done THEN
NetSystem.DelPassword("ftp", user, server)
END
ELSE
S.res := NotReady;
Close2(S)
END
ELSE
S.res := NotConnected
END;
IF S.res = Done THEN
SendCmd(S, "TYPE I");
ReadResponse(S, line);
IF S.status # 200 THEN
(* should not happen *)
END;
QuerySystem(S);
S.res := Done
END
END OpenS;
PROCEDURE ChangeDirS(S: Session; newDir: ARRAY OF CHAR);
BEGIN
S.reply := "CWD ";
Strings.Append(S.reply, newDir);
SendCmd(S, S.reply);
ReadResponse(S, S.reply);
IF S.status = 250 THEN
S.res := Done
ELSE
S.res := Failed
END
END ChangeDirS;
PROCEDURE SetDataPort(S: Session);
VAR str: ARRAY 4 OF CHAR; p0, p1: LONGINT; i, j, k: INTEGER; done: BOOLEAN;
BEGIN
SendCmd(S, "PASV"); ReadResponse(S, line);
IF (S.status >= 200) & (S.status < 300) THEN
S.res := Interrupted; i := 0;
WHILE (line[i] # 0X) & ~Strings.IsDigit(line[i]) DO INC(i) END;
j := 0; k := 0;
WHILE (line[i] # 0X) & (k < 4) DO
IF line[i] # "," THEN
S.portIPAddress[j] := line[i]
ELSE
S.portIPAddress[j] := "."; INC(k)
END;
INC(i); INC(j)
END;
IF (j <= 0) & (k < 4) THEN RETURN END;
S.portIPAddress[j-1] := 0X;
NetSystem.ToHost(S.portIPAddress, S.dataIP, done);
IF ~done THEN RETURN END;
WHILE (line[i] # 0X) & ((line[i] <= " ") OR (line[i] = ",")) DO INC(i) END;
Strings.StrToIntPos(line, p0, i);
WHILE (line[i] # 0X) & ((line[i] <= " ") OR (line[i] = ",")) DO INC(i) END;
Strings.StrToIntPos(line, p1, i);
S.dataPort := SHORT(256*p0+p1);
S.res := Done
ELSE
S.dataIP := NetSystem.anyIP;
S.dataPort := dataPort;
REPEAT
IF S.dataPort >= MaxDataPort THEN
S.dataPort := MinDataPort
END;
INC(S.dataPort);
(* not 100% safe *)
NetSystem.OpenConnection(S.dataC, S.dataPort, NetSystem.anyIP, NetSystem.anyport, S. res)
UNTIL (S.res = NetSystem.done) OR UserBreak();
IF S.res = NetSystem.done THEN
dataPort := S.dataPort; S.res := Failed;
NetSystem.ToNum(NetSystem.hostIP, S.portIPAddress);
i := 0;
WHILE S.portIPAddress[i] # 0X DO
IF S.portIPAddress[i] = "." THEN
S.portIPAddress[i] := ","
END;
INC(i)
END;
Strings.AppendCh(S.portIPAddress, ",");
Strings.IntToStr(S.dataPort DIV 256, str);
Strings.Append(S.portIPAddress, str);
Strings.AppendCh(S.portIPAddress, ",");
Strings.IntToStr(S.dataPort MOD 256, str);
Strings.Append(S.portIPAddress, str);
line := "PORT "; Strings.Append(line, S.portIPAddress);
SendCmd(S, line)
ELSE
Disconnect(S.dataC); S.dataC := NIL;
S.reply := "Interrupted"; S.res := Interrupted
END
END
END SetDataPort;
PROCEDURE WaitDataCon(S: Session): NetSystem.Connection;
VAR
time: LONGINT;
C1: NetSystem.Connection;
BEGIN
IF S.dataIP = NetSystem.anyIP THEN
time := Input.Time();
REPEAT
UNTIL NetSystem.Requested(S.dataC) OR ((Input.Time()-time) > timeOut) OR UserBreak();
IF NetSystem.Requested(S.dataC) THEN
NetSystem.Accept(S.dataC, C1, S.res); Disconnect(S.dataC);
IF S.res = NetSystem.done THEN
S.res := Done;
RETURN C1
ELSE
S.res := Failed
END
ELSIF (Input.Time()-time) > timeOut THEN
S.res := TimedOut
ELSE
S.res := Interrupted
END;
Disconnect(S.dataC)
ELSE
NetSystem.OpenConnection(C1, NetSystem.anyport, S.dataIP, S.dataPort, S.res);
IF S.res = Done THEN RETURN C1 END
END;
RETURN NIL
END WaitDataCon;
PROCEDURE EnumDir(S: Session; enum: EnumProc);
VAR
C: NetSystem.Connection;
len: LONGINT;
BEGIN
S.reply := ""; SetDataPort(S);
IF S.res = Interrupted THEN RETURN END;
IF S.dataIP = NetSystem.anyIP THEN
ReadResponse(S, line)
ELSE
C := WaitDataCon(S);
IF S.res = Done THEN S.status := 200 END
END;
IF S.status = 200 THEN
IF S.system = VMS THEN
SendCmd(S, "NLST")
ELSE
SendCmd(S, "LIST")
END;
ReadResponse(S, S.reply);
IF (S.status = 150) OR (S.status = 250) THEN
IF S.dataIP = NetSystem.anyIP THEN C := WaitDataCon(S) END;
IF S.res = Done THEN
S.res := Done;
len := NetSystem.Available(C);
WHILE ((len > 0) OR Connected(C, NetSystem.in)) & ~UserBreak() DO
IF len > 0 THEN
NetSystem.ReadString(C, line);
enum(line)
END;
len := NetSystem.Available(C)
END;
Disconnect(C)
END;
ReadResponse(S, S.reply)
ELSE
S.res := Failed
END
END;
IF S.dataC # NIL THEN Disconnect(S.dataC) END
END EnumDir;
PROCEDURE GetCurDir(S: Session; VAR curdir: ARRAY OF CHAR);
VAR i, j: INTEGER;
BEGIN
SendCmd(S, "PWD");
ReadResponse(S, S.reply);
IF S.status = 257 THEN
IF S.system = VMS THEN
curdir := S.reply;
i := 0;
WHILE curdir[i] > " " DO
INC(i)
END;
curdir[i] := 0X
ELSE
i := 0;
WHILE (S.reply[i] # 0X) & (S.reply[i] # 22X) DO
INC(i)
END;
j := 0;
IF S.reply[i] = 22X THEN
INC(i);
WHILE (S.reply[i] # 0X) & (S.reply[i] # 22X) DO
curdir[j] := S.reply[i];
INC(j); INC(i)
END
END;
curdir[j] := 0X
END;
S.res := Done
ELSE
curdir := "";
S.res := Failed
END
END GetCurDir;
PROCEDURE MakeDirS(S: Session; newDir: ARRAY OF CHAR);
BEGIN
S.reply := "MKD ";
Strings.Append(S.reply, newDir);
SendCmd(S, S.reply);
ReadResponse(S, S.reply);
IF S.status = 257 THEN
S.res := Done
ELSE
S.res := Failed
END
END MakeDirS;
PROCEDURE RmDirS(S: Session; dir: ARRAY OF CHAR);
BEGIN
S.reply := "RMD ";
Strings.Append(S.reply, dir);
SendCmd(S, S.reply);
ReadResponse(S, S.reply);
IF S.status = 250 THEN
S.res := Done
ELSE
S.res := Failed
END
END RmDirS;
PROCEDURE DeleteFile(S: Session; remName: ARRAY OF CHAR);
BEGIN
S.reply := "DELE ";
Strings.Append(S.reply, remName);
SendCmd(S, S.reply);
ReadResponse(S, S.reply);
IF S.status = 250 THEN
S.res := Done
ELSE
S.res := Failed
END
END DeleteFile;
PROCEDURE ReadData(C: NetSystem.Connection; VAR R: Files.Rider);
VAR len, rlen, try: LONGINT;
BEGIN
try := 0;
len := NetSystem.Available(C);
WHILE (len > 0) OR Connected(C, NetSystem.in) DO
IF UserBreak() THEN
RETURN
END;
IF len > BufLen THEN
rlen := BufLen
ELSE
rlen := len
END;
NetSystem.ReadBytes(C, 0, rlen, buffer);
Files.WriteBytes(R, buffer, rlen);
DEC(len, rlen);
IF len <= 0 THEN
len := NetSystem.Available(C);
IF len <= 0 THEN
INC(try);
IF try > MaxTry THEN
RETURN
END
ELSE
try := 0
END
END
END
END ReadData;
PROCEDURE GetF(S: Session; remName: ARRAY OF CHAR; VAR R: Files.Rider);
VAR C: NetSystem.Connection;
BEGIN
S.reply := ""; SetDataPort(S);
IF S.res = Interrupted THEN RETURN END;
IF S.dataIP = NetSystem.anyIP THEN
ReadResponse(S, line)
ELSE
C := WaitDataCon(S);
IF S.res = Done THEN S.status := 200 END
END;
IF S.status = 200 THEN
line := "RETR ";
Strings.Append(line, remName);
SendCmd(S, line);
ReadResponse(S, line);
S.reply := line;
IF (S.status = 150) OR (S.status = 250) THEN
IF S.dataIP = NetSystem.anyIP THEN C := WaitDataCon(S) END;
IF S.res = Done THEN
ReadData(C, R);
Disconnect(C)
END;
ReadResponse(S, S.reply);
IF S.res = Interrupted THEN
ReadResponse(S, line)
END
ELSE
S.res := Failed
END
END;
IF S.dataC # NIL THEN Disconnect(S.dataC) END
END GetF;
PROCEDURE GetFile(S: Session; remName, locName: ARRAY OF CHAR);
VAR
F: Files.File;
R: Files.Rider;
BEGIN
F := Files.New(locName);
Files.Set(R, F, 0);
GetF(S, remName, R);
Files.Register(F)
END GetFile;
PROCEDURE WriteData(C: NetSystem.Connection; VAR R: Files.Rider);
BEGIN
Files.ReadBytes(R, buffer, BufLen);
WHILE ~R.eof DO
NetSystem.WriteBytes(C, 0, BufLen, buffer);
Files.ReadBytes(R, buffer, BufLen)
END;
IF R.res > 0 THEN
NetSystem.WriteBytes(C, 0, BufLen-R.res, buffer)
END
END WriteData;
PROCEDURE PutFile(S: Session; remName, locName: ARRAY OF CHAR);
VAR
F: Files.File;
R: Files.Rider;
C: NetSystem.Connection;
BEGIN
S.reply := "";
F := Files.Old(locName);
IF F # NIL THEN
SetDataPort(S);
IF S.res = Interrupted THEN RETURN END;
IF S.dataIP = NetSystem.anyIP THEN
ReadResponse(S, line)
ELSE
C := WaitDataCon(S);
IF S.res = Done THEN S.status := 200 END
END;
IF S.status = 200 THEN
line := "STOR ";
Strings.Append(line, remName);
SendCmd(S, line);
ReadResponse(S, S.reply);
IF (S.status = 150) OR (S.status = 250) THEN
IF S.dataIP = NetSystem.anyIP THEN C := WaitDataCon(S) END;
IF S.res = Done THEN
Files.Set(R, F, 0);
WriteData(C, R);
Disconnect(C)
END;
ReadResponse(S, S.reply)
ELSE
S.res := Failed
END
END
ELSE
S.reply := locName;
Strings.Append(S.reply, " not found");
S.res := LocFileNotFound
END;
IF S.dataC # NIL THEN Disconnect(S.dataC) END
END PutFile;
PROCEDURE ReadText(C: NetSystem.Connection; VAR W: Texts.Writer);
VAR
len, rlen, i: LONGINT;
ch: CHAR;
BEGIN
len := NetSystem.Available(C);
WHILE (len > 0) OR Connected(C, NetSystem.in) DO
IF len > (BufLen-2) THEN
rlen := BufLen-2
ELSE
rlen := len
END;
NetSystem.ReadBytes(C, 0, rlen, buffer);
i := 0;
WHILE i < rlen DO
ch := buffer[i];
IF ch = Strings.CR THEN
(* ignore CR *)
ELSIF ch = Strings.LF THEN
Texts.WriteLn(W)
ELSE
ch := Strings.ISOToOberon[ORD(ch)];
Texts.Write(W, ch)
END;
INC(i)
END;
DEC(len, rlen);
IF len <= 0 THEN
len := NetSystem.Available(C)
END
END
END ReadText;
PROCEDURE GetText(S: Session; remName: ARRAY OF CHAR; VAR W: Texts.Writer);
VAR C: NetSystem.Connection;
BEGIN
S.reply := "";
SendCmd(S, "TYPE A");
ReadResponse(S, line);
SetDataPort(S);
IF S.res = Interrupted THEN SendCmd(S, "TYPE I"); ReadResponse(S, line); RETURN END;
IF S.dataIP = NetSystem.anyIP THEN
ReadResponse(S, line)
ELSE
C := WaitDataCon(S);
IF S.res = Done THEN S.status := 200 END
END;
IF S.status = 200 THEN
line := "RETR ";
Strings.Append(line, remName);
SendCmd(S, line);
ReadResponse(S, line);
S.reply := line;
IF (S.status = 150) OR (S.status = 250) THEN
IF S.dataIP = NetSystem.anyIP THEN C := WaitDataCon(S) END;
IF S.res = Done THEN
ReadText(C, W);
Disconnect(C)
END;
ReadResponse(S, S.reply);
IF S.res = Interrupted THEN
ReadResponse(S, line)
END
ELSE
S.res := Failed
END
END;
IF S.dataC # NIL THEN Disconnect(S.dataC) END;
SendCmd(S, "TYPE I");
ReadResponse(S, line)
END GetText;
PROCEDURE WriteText(C: NetSystem.Connection; T: Texts.Text);
VAR
R: Texts.Reader;
ch: CHAR;
BEGIN
Texts.OpenReader(R, T, 0);
Texts.Read(R, ch);
WHILE ~R.eot DO
IF R.lib IS Fonts.Font THEN
IF ch = Strings.CR THEN
NetSystem.WriteBytes(C, 0, 2, Strings.CRLF)
ELSIF ch # Strings.LF THEN
ch := Strings.OberonToISO[ORD(ch)];
NetSystem.Write(C, ch)
END
END;
Texts.Read(R, ch)
END
END WriteText;
PROCEDURE PutText(S: Session; remName: ARRAY OF CHAR; text: Texts.Text);
VAR C: NetSystem.Connection;
BEGIN
S.reply := "";
SendCmd(S, "TYPE A");
ReadResponse(S, line);
IF (S.status < 200) OR (S.status >= 300) THEN
RETURN
END;
SetDataPort(S);
IF S.res = Interrupted THEN SendCmd(S, "TYPE I"); ReadResponse(S, line); RETURN END;
IF S.dataIP = NetSystem.anyIP THEN
ReadResponse(S, line)
ELSE
C := WaitDataCon(S);
IF S.res = Done THEN S.status := 200 END
END;
IF S.status = 200 THEN
line := "STOR ";
Strings.Append(line, remName);
SendCmd(S, line);
ReadResponse(S, S.reply);
IF (S.status = 150) OR (S.status = 250) THEN
IF S.dataIP = NetSystem.anyIP THEN C := WaitDataCon(S) END;
IF S.res = Done THEN
WriteText(C, text);
Disconnect(C)
END;
ReadResponse(S, S.reply)
ELSE
S.res := Failed
END
END;
IF S.dataC # NIL THEN Disconnect(S.dataC) END;
SendCmd(S, "TYPE I");
ReadResponse(S, line)
END PutText;
PROCEDURE ShowRes();
BEGIN
Texts.WriteString(W, S.reply);
Texts.WriteLn(W);
Texts.Append(log, W.buf)
END ShowRes;
PROCEDURE OpenScanner(VAR S: Texts.Scanner);
VAR
beg, end, time: LONGINT;
text: Texts.Text;
BEGIN
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(S);
IF (S.class = Texts.Char) & (S.c = "^") THEN
time := -1;
text := NIL;
Oberon.GetSelection(text, beg, end, time);
IF (text # NIL) & (time >= 0) THEN
Texts.OpenScanner(S, text, beg);
Texts.Scan(S)
END
END
END OpenScanner;
PROCEDURE SplitFTPAdr(VAR url, host, path, user, passwd: ARRAY OF CHAR; VAR type: CHAR; VAR port: INTEGER): BOOLEAN;
VAR i, j, l: LONGINT;
PROCEDURE Blanks();
BEGIN
WHILE (url[i] # 0X) & (url[i] <= " ") DO
INC(i)
END
END Blanks;
BEGIN
type := 0X; port := DefConPort;
user := ""; passwd := "";
i := 0; Blanks();
(* look ahead for @ *)
j := i;
WHILE (url[j] # 0X) & (url[j] # "@") & (url[j] # "/") DO
INC(j)
END;
IF url[j] = "@" THEN
(* get user *)
l := LEN(user)-1; j := 0;
WHILE (url[i] # 0X) & (url[i] # ":") & (url[i] # "@") DO
IF (j < l) THEN
user[j] := url[i]; INC(j)
END;
INC(i)
END;
user[j] := 0X; DEC(j);
WHILE (j >= 0) & (user[j] <= " ") DO
user[j] := 0X; DEC(j)
END;
IF url[i] = ":" THEN
(* get password *)
l := LEN(passwd);
INC(i); Blanks(); j := 0;
WHILE (url[i] # 0X) & (url[i] # "@") DO
IF j < l THEN
passwd[j] := url[i]; INC(j)
END;
INC(i)
END;
passwd[j] := 0X; DEC(j);
WHILE (j >= 0) & (passwd[j] <= " ") DO
passwd[j] := 0X; DEC(j)
END
END;
INC(i); Blanks()
END;
(* get host *)
l := LEN(host); j := 0;
WHILE (url[i] # 0X) & (url[i] # ":") & (url[i] # "/") DO
IF j < l THEN
host[j] := url[i]; INC(j)
END;
INC(i)
END;
host[j] := 0X; DEC(j);
WHILE (j >= 0) & (host[j] <= " ") DO
host[j] := 0X; DEC(j)
END;
IF url[i] = ":" THEN
port := 0; INC(i);
WHILE (url[i] # "/") & (url[i] # 0X) DO
IF Strings.IsDigit(url[i]) THEN
port := port*10+ORD(url[i])-ORD("0")
END;
INC(i)
END;
IF port <= 0 THEN
port := DefConPort
END
END;
(* get path *)
l := LEN(path); j := 0;
IF url[i] # 0X THEN
path[j] := url[i]; INC(j); INC(i);
IF url[i] = "~" THEN
j := 0
END
END;
WHILE (url[i] # 0X) & (url[i] # ";") DO
IF j < l THEN
path[j] := url[i]; INC(j)
END;
INC(i)
END;
path[j] := 0X; DEC(j);
WHILE (j >= 0) & (path[j] <= " ") DO
path[j] := 0X; DEC(j)
END;
IF url[i] = ";" THEN
INC(i); Blanks();
IF CAP(url[i]) # "T" THEN
type := CAP(url[i])
ELSE
WHILE (url[i] # 0X) & (url[i] # "=") DO
INC(i)
END;
IF url[i] = "=" THEN
INC(i); Blanks();
type := CAP(url[i])
ELSE
type := "T"
END
END
END;
RETURN (host # "") & (port > 0)
END SplitFTPAdr;
(** FTP.Open (server | "^")
Open an ftp connection to server using username and password set with FTP.SetUser. *)
PROCEDURE Open*;
VAR
Sc: Texts.Scanner;
host, path, user, passwd: ARRAY 64 OF CHAR;
port: INTEGER;
type: CHAR;
BEGIN
IF S = NIL THEN
OpenScanner(Sc);
IF Sc.class IN {Texts.Name, Texts.String} THEN
IF SplitFTPAdr(Sc.s, host, path, user, passwd, type, port) THEN
OpenS(host, user, passwd, port, S);
ShowRes();
IF S.res # Done THEN
S := NIL
END
END
END
ELSE
Texts.WriteString(W, "already connected");
Texts.WriteLn(W);
Texts.Append(log, W.buf)
END
END Open;
PROCEDURE Con(): BOOLEAN;
BEGIN
IF S = NIL THEN
Texts.WriteString(W, "not connected");
Texts.WriteLn(W);
Texts.Append(log, W.buf);
RETURN FALSE
ELSE
RETURN TRUE
END
END Con;
(** FTP.Close
Close an previously opened FTP connection. *)
PROCEDURE Close*;
BEGIN
IF Con() THEN
CloseS(S);
ShowRes();
IF S.res = Done THEN
S := NIL
END
END
END Close;
(** FTP.ChangeDir (newdir | "^")
Change the current directory on the FTP server to newdir. *)
PROCEDURE ChangeDir*;
VAR Sc: Texts.Scanner;
BEGIN
IF Con() THEN
OpenScanner(Sc);
IF Sc.class IN {Texts.Name, Texts.String} THEN
ChangeDirS(S, Sc.s);
ShowRes()
END
END
END ChangeDir;
PROCEDURE *ShowEntry(entry: ARRAY OF CHAR);
BEGIN
Texts.WriteString(W, entry);
Texts.WriteLn(W);
Texts.Append(log, W.buf)
END ShowEntry;
(** FTP.Dir
List the contents of the current directory on the FTP server. *)
PROCEDURE Dir*;
BEGIN
IF Con() THEN
EnumDir(S, ShowEntry);
ShowRes()
END
END Dir;
PROCEDURE *ShowCompactEntry(entry: ARRAY OF CHAR);
VAR i: INTEGER;
BEGIN
i := 0;