-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrje80.c
More file actions
2464 lines (2298 loc) · 64.9 KB
/
rje80.c
File metadata and controls
2464 lines (2298 loc) · 64.9 KB
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
// A program to emulate an IBM 2780/3780 RJE station over a simulated
// bisync line using the BSC protocol as implemented by the Hercules/370 2703
// device. This program will not work with "real" bisync hardware.
#include <stdio.h>
#include <stdlib.h>
#if defined (_WIN32) // Windows
#include <winsock2.h>
#include <conio.h>
#include <io.h>
#include <signal.h>
#include <windows.h>
#else // Linux
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <netdb.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#endif
#include <errno.h>
// Prototypes
int do_char(unsigned char c);
int do_input(int count);
int execute();
int nexttoken();
int gettoken(int upper);
int InitSockets();
int CloseSockets();
int connecthost();
int send_file(char *command);
int get_buffer();
unsigned char read_byte();
int read_poll(int sec, int usec);
int read_data(int sec, int usec, int mode);
int clear_input_buffer();
int clear_input_record();
int write_record();
int printer_function(unsigned char func);
int write_buffer();
int send_ack(char ack);
int ttyinit();
int ttyclose();
int ttygets(char *str);
int ttyread(unsigned char *buf);
int ttychar(char c);
int ttystr(char *msg);
int rjesleep(int t);
char *translate_to_ebcdic (unsigned char *str);
char *translate_to_ascii (unsigned char *str);
// Global control items
// Status values for overall status flag
#define NOLINK -1 /* not connected, not yet trying */
#define INITIAL_WAIT 0 /* Connected waiting for signon command */
#define IDLE 1 /* Connected, signed on, idle */
#define SENDING 5 /* Data being sent to host */
#define RECEIVING 6 /* data being received from host */
#define SHUTDOWN 9 /* emulator shutdown */
int status = NOLINK; /* Emulator overall status */
int transparent = 0; /* 1 when in transparent mode */
int debugit = 0; /* debug flag, set by -d on command */
char inethost[128]; /* Internet host (from command line) */
int inetport; /* Internet port (ditto) */
char macro[8192]; /* The macro buffer */
int macro_size = 0; /* Size of macro in biffer */
int macro_ctr = 0; /* chars in macro buffer */
char command[128]; /* A command to the Emulator */
char token[64]; /* A token from the command */
int comctr; /* used by the parser */
int comlen = 0; /* Length of command */
int prompt = 0; /* Prompt flag */
int pollflag = 2; /* Polling control */
int pollctr = 0;
unsigned char lastack = 0; /* To flip between ACK0 and ACK1 */
char reader[80]; /* Reader filename */
int reader_recl = 80; /* record length */
int reader_fmt = 0; /* 0=ascii 1=ebcdic */
char print[80]; /* Print filename */
int print_recl = 132; /* max printer width */
char htabs[256]; /* Horizontal tabs storage */
char punch[80]; /* Punch filename */
int punch_recl = 80; /* Punch recl (used for ebcdic only) */
int punch_fmt = 0; /* 0=ascii 1=ebcdic */
int punchform = 0; /* Punch format 0=ascii 1=ebcdic */
int readform = 0; /* Reader format 0=ascii 1=ebcdic */
int device_select = 0; /* 0 = printer 1 = punch */
int print_open = 0; /* need to open = 1 */
int punch_open = 0;
FILE *printfd; /* FDs for files */
FILE *punchfd;
FILE *readerfd;
FILE *tracefd;
FILE *rcfd;
char tracefile[80];
char opt_user[32]; /* Username */
int opt_poll = 1; /* Poll when idle TODO: was 0*/
int opt_trn = 1; /* Transparency on all writes TODO: was 0*/
int opt_pause = 0; /* -1 = every FF, 0 = none, > 0 = pause */
int opt_copy = 1; /* 1 = display printer output, 0 = no disp */
int opt_os = 2; /* 0 = generic TODO: was 0*/
/* 1 = VM/370 RSCS */
/* 2 = JES2 */
/* 3 = JES3 */
/* 4 = DOS/VS */
/* 5 = RES (VS1) */
/* 6 = OS/360 */
// TTY-related data
unsigned char ttybuf[1];
#if defined (_WIN32)
#else
struct termios cmdtty, runtty;
#endif
// Socket-related stuff
int sockfd; /* The socket itself */
char hname[80]; /* given host name */
int host_ip;
int inport; /* our input port */
int outport; /* the hosts port */
// Communications buffers
unsigned char phybuffer[8192]; /* Raw socket data */
int phy_ctr = 0; /* Size in physical buffer */
unsigned char line_in[1024]; /* Input data from line */
int line_in_ctr = 0; /* How many are stacked up */
unsigned char record_in[1024]; /* Record data */
int record_ctr = 0; /* How many bytes in record */
unsigned char line_out[1024]; /* Output data to be sent */
int line_out_size = 0; /* How many to actually send */
char signon[80]; /* we build this */
// BSC control characters
unsigned char SOH = 0x01;
unsigned char STX = 0x02;
unsigned char ETX = 0x03;
unsigned char DLE = 0x10;
unsigned char DC1 = 0x11;
unsigned char DC2 = 0x12;
unsigned char NL = 0x15;
unsigned char EM = 0x19;
unsigned char IGS = 0x1d;
unsigned char IRS = 0x1e;
unsigned char ITB = 0x1f;
unsigned char ETB = 0x26;
unsigned char ESC = 0x27;
unsigned char ENQ = 0x2d;
unsigned char SYN = 0x32;
unsigned char EOT = 0x37;
unsigned char NAK = 0x3d;
unsigned char DC3 = 0x5d;
unsigned char ACK1 = 0x61;
unsigned char WABT = 0x6b;
unsigned char ACK0 = 0x70;
unsigned char EPAD = 0xff;
unsigned char SPAD = 0xaa;
unsigned char RVI = 0x7c;
// Translation tables (from Hercules)
static unsigned char
ascii_to_ebcdic[] = {
"\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F"
"\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x1A\x27\x22\x1D\x35\x1F"
"\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61"
"\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F"
"\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6"
"\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D"
"\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96"
"\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x6A\xD0\xA1\x07"
"\x68\xDC\x51\x42\x43\x44\x47\x48\x52\x53\x54\x57\x56\x58\x63\x67"
"\x71\x9C\x9E\xCB\xCC\xCD\xDB\xDD\xDF\xEC\xFC\xB0\xB1\xB2\xB3\xB4"
"\x45\x55\xCE\xDE\x49\x69\x04\x06\xAB\x08\xBA\xB8\xB7\xAA\x8A\x8B"
"\x09\x0A\x14\xBB\x15\xB5\xB6\x17\x1B\xB9\x1C\x1E\xBC\x20\xBE\xBF"
"\x21\x23\x24\x28\x29\x2A\x2B\x2C\x30\x31\xCA\x33\x34\x36\x38\xCF"
"\x39\x3A\x3B\x3E\x41\x46\x4A\x4F\x59\x62\xDA\x64\x65\x66\x70\x72"
"\x73\xE1\x74\x75\x76\x77\x78\x80\x8C\x8D\x8E\xEB\x8F\xED\xEE\xEF"
"\x90\x9A\x9B\x9D\x9F\xA0\xAC\xAE\xAF\xFD\xFE\xFB\x3F\xEA\xFA\xFF"
};
static unsigned char
ebcdic_to_ascii[] = {
"\x00\x01\x02\x03\xA6\x09\xA7\x7F\xA9\xB0\xB1\x0B\x0C\x0D\x0E\x0F"
"\x10\x11\x12\x13\xB2\x0A\x08\xB7\x18\x19\x1A\xB8\xBA\x1D\xBB\x1F"
"\xBD\xC0\x1C\xC1\xC2\x0A\x17\x1B\xC3\xC4\xC5\xC6\xC7\x05\x06\x07"
"\xC8\xC9\x16\xCB\xCC\x1E\xCD\x04\xCE\xD0\xD1\xD2\x14\x15\xD3\xFC"
"\x20\xD4\x83\x84\x85\xA0\xD5\x86\x87\xA4\xD6\x2E\x3C\x28\x2B\xD7"
"\x26\x82\x88\x89\x8A\xA1\x8C\x8B\x8D\xD8\x21\x24\x2A\x29\x3B\x5E"
"\x2D\x2F\xD9\x8E\xDB\xDC\xDD\x8F\x80\xA5\x7C\x2C\x25\x5F\x3E\x3F"
"\xDE\x90\xDF\xE0\xE2\xE3\xE4\xE5\xE6\x60\x3A\x23\x40\x27\x3D\x22"
"\xE7\x61\x62\x63\x64\x65\x66\x67\x68\x69\xAE\xAF\xE8\xE9\xEA\xEC"
"\xF0\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\xF1\xF2\x91\xF3\x92\xF4"
"\xF5\x7E\x73\x74\x75\x76\x77\x78\x79\x7A\xAD\xA8\xF6\x5B\xF7\xF8"
"\x9B\x9C\x9D\x9E\x9F\xB5\xB6\xAC\xAB\xB9\xAA\xB3\xBC\x5D\xBE\xBF"
"\x7B\x41\x42\x43\x44\x45\x46\x47\x48\x49\xCA\x93\x94\x95\xA2\xCF"
"\x7D\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\xDA\x96\x81\x97\xA3\x98"
"\x5C\xE1\x53\x54\x55\x56\x57\x58\x59\x5A\xFD\xEB\x99\xED\xEE\xEF"
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\xFE\xFB\x9A\xF9\xFA\xFF"
};
// Mainline code - - start here.
main(int argc, char *argv[])
{
int stat, i, rstat;
int tries = 0;
int startar = 1;
int gotdle = 0;
int gotstx = 0;
unsigned char buf[1], ch, sho[16];
strcpy(inethost, "");
strcpy(opt_user, "");
strcpy(tracefile, "");
if (argc > 1) {
if (strcmp(argv[1], "-d") == 0) {
printf("We are in debug mode.\n");
debugit = 1;
startar = 2;
}
if (argc >= startar) {
strcpy(inethost, argv[startar]);
startar++;
}
if (argc >= startar) {
inetport=atoi(argv[startar]);
outport = inetport;
}
}
ttyinit();
ttystr("\r\nRJE80 IBM 3780 Emulator Version 0.29");
strcpy(print, ""); /* default output files to display */
strcpy(punch, "punch.txt");
if (InitSockets() == -1) {
status = SHUTDOWN;
}
if (strlen(inethost) > 0 &&
status != SHUTDOWN) { /* host given in command line ? */
connecthost(); /* YEAH */
}
// See if there's an rje80.rc file, if so, read it and stuff the
// characters in it into the macro buffer
if ((rcfd = fopen("rje80.rc", "r")) != NULL) {
macro_size = 0;
while (1) {
if (feof(rcfd))
break;
macro[macro_size] = fgetc(rcfd);
macro_size++;
}
macro_ctr = 0;
fclose(rcfd);
}
// This is the main loop. It cycles continuously, looking for
// events to process. Events such as a character from the local
// keyboard, data arriving on the communications socket, or
// a timer expiring.
while (status != SHUTDOWN) {
if (!prompt) { /* Need a new prompt? */
for (i=0; i < 128; i++) command[i] = 0;
ttychar('\n');
ttychar('\r');
ttychar(')');
ttychar(' ');
prompt = 1;
comlen = 0;
}
stat = ttyread(buf);
if (stat) {
do_char(buf[0]);
}
if (status == IDLE) {
if (read_poll(0, 10000) == 1) { /* Have data? */
read_data(3, 0, 0); /* Yes -- go get it */
while (ch = read_byte()) { /* Process data */
if (ch == ENQ) { /* he wants to send us something */
prompt = 1;
pollflag = 0;
status = RECEIVING;
clear_input_record();
lastack = ACK0;
send_ack(ACK0);
continue;
}
if (ch == EPAD || ch == SYN) { /* fillers - ignore */
continue;
}
if (ch == DLE) { /* Probably poll response */
pollflag = 1;
continue;
}
if (ch == ACK0 || ch == ACK1) { /* it is a poll response */
pollflag = 2; /* set up to poll again */
continue;
}
// WE don't know what it is ...
// Ignore it for now ...
if (debugit == 1) {
ttystr("\r\nOdd data found: ");
sprintf(sho, "%2x", ch);
ttystr(sho);
ttystr("\r\n");
}
}
} else { // no data .. so, we consider polling
if (pollflag == 2 && pollctr > 50) {
if (opt_poll) {
line_out[0] = line_out[1] = line_out[2] = DLE;
line_out[3] = ACK0;
line_out_size = 4;
write_buffer();
}
pollctr = 0;
} else {
pollctr++;
}
}
}
if (status == RECEIVING) {
if (line_in_ctr < 1)
rstat = read_data(3, 0, 1);
if (rstat == -1) {
ttystr("\r\nRJE127S The line has disconnected.\r\n");
status = IDLE;
continue;
}
ch = read_byte();
if (ch != 0) {
if (transparent) { // Handle chars in transparent mode */
if (gotdle == 1 && ch == EOT) { /* End of file */
if (device_select == 0 && strlen(print) != 0) {
ttystr("EOT\r\n");
prompt = 0;
}
if (device_select == 1 && strlen(punch) != 0) {
ttystr("EOT\r\n");
prompt = 0;
}
send_ack(0);
status = IDLE;
pollflag = 2;
pollctr = 0;
prompt = 0;
gotdle = gotstx = 0;
continue;
}
if (gotdle == 1 && ch == STX) { /* Start of text */
if (gotdle)
transparent = 1;
gotstx = 1;
gotdle = 0;
continue;
}
if (gotdle == 0 && ch == DLE) { /* DLE what follows is special */
gotdle = 1;
gotstx = 0;
continue;
}
if (gotdle == 1 && ch == DC1) { /* DC1: Select printer if after an STX */
if (gotstx == 1) {
device_select = 0;
if (strlen(print) != 0) {
ttystr("\r\nRJE001I Receiving print data...");
} else {
ttystr("\r\n");
}
}
gotdle = gotstx = 0;
continue;
}
if (gotdle == 1 && ch == DC2) { /* DC2: Select punch if after an STX */
if (gotstx == 1) {
device_select = 1;
ttystr("\r\nRJE001I Receiving punch data...");
}
continue;
}
if (gotdle == 1 && ch == ETB && record_ctr > 0) { // DOS bug, some recs have only ETB
write_record();
}
if (gotdle == 1 && ch == ETX) { /* End of block -- */
send_ack(0); /* Acknowledge */
gotdle = gotstx = 0;
transparent = 0; /* transparent off */
continue;
}
if (gotdle == 1 && ch == ETB) { /* End of block -- */
send_ack(0); /* Acknowledge */
gotdle = gotstx = 0;
continue;
}
if (gotdle == 1 && (ch == IRS ||
ch == NL)) { /* End of record - write */
write_record();
gotdle = gotstx = 0;
continue;
}
if (gotdle == 1 && ch == ENQ) {
send_ack(0); /* Acknowledge */
gotdle = gotstx = 0;
continue;
}
record_in[record_ctr] = ch; /* data - save it */
record_ctr++;
gotdle = gotstx = 0;
} else {
if (ch == EOT) { /* End of file */
if (device_select == 0 && strlen(print) != 0) {
ttystr("EOT\r\n");
prompt = 0;
}
if (device_select == 1 && strlen(punch) != 0) {
ttystr("EOT\r\n");
prompt = 0;
}
send_ack(0);
status = IDLE;
pollflag = 2;
pollctr = 0;
prompt = 0;
gotdle = gotstx = 0;
continue;
}
if (ch == STX) { /* Start of text */
if (gotdle)
transparent = 1;
gotstx = 1;
gotdle = 0;
continue;
}
if (ch == DLE) { /* DLE what follows is special */
gotdle = 1;
gotstx = 0;
continue;
}
if (ch == DC1) { /* DC1: Select printer if after an STX */
if (gotstx == 1) {
device_select = 0;
if (strlen(print) != 0) {
ttystr("\r\nRJE001I Receiving print data...");
} else {
ttystr("\r\n");
}
}
gotdle = gotstx = 0;
continue;
}
if (ch == DC2) { /* DC2: Select punch if after an STX */
if (gotstx == 1) {
device_select = 1;
ttystr("\r\nRJE001I Receiving punch data...");
}
continue;
}
if (ch == ETB && record_ctr > 0) { // DOS bug, some recs have only ETB
write_record();
}
if (ch == ETX || /* End of block -- */
ch == ETB) {
send_ack(0); /* Acknowledge */
gotdle = gotstx = 0;
continue;
}
if (ch == IRS ||
ch == NL) { /* End of record - write */
write_record();
gotdle = gotstx = 0;
continue;
}
if (ch == ENQ) {
send_ack(0); /* Acknowledge */
gotdle = gotstx = 0;
continue;
}
// WHAT DO TO WITH ALL THE OTHER CONTROL CHARS?
// store'em, thats what ... until we know better ...
// NOTE: thats what we want to do with ESC sequences
// ...we handle them when we output the record
record_in[record_ctr] = ch; /* data - save it */
record_ctr++;
gotdle = gotstx = 0;
}
}
}
rjesleep(10); /* Allow local CPU some cycles */
}
if (strlen(tracefile) > 0)
fclose(tracefd);
CloseSockets();
ttyclose();
printf("Goodbye...\n");
}
// A character typed - store it, or execute the command
int do_char(unsigned char c)
{
if (c == 8) { /* Backspace */
if (comlen > 0) {
comlen--;
command[comlen] = ' ';
ttystr("\b \b");
}
return (0);
}
if (c == '\n' || c == '\r') { /* Line terminator */
execute();
return (0);
}
if (comlen > 128) {
ttystr("\n\rRJE002W Command too long, sorry, only enter, del, or esc allowed\r\n");
return (0);
}
command[comlen] = c; /* Just a char - store it */
comlen++;
ttychar(c);
return (0);
}
// This function executes commands from the user
int execute()
{
char passw[32];
char reclen[32];
char shell[128];
char cmd[128];
char cmdline[128];
int i, rc, retry, savep;
prompt = 0;
if (comlen < 1)
return (0);
comctr = 0;
while ((command[comctr] == ' ' || command[comctr] < 0)
&& comctr < comlen) {
comctr++;
}
if (comctr >= comlen)
return (0);
gettoken(1);
if (strcmp(token, "OPEN") == 0 || strcmp(token, "START") == 0 ||
strcmp(token, "OPE") == 0 ||
strcmp(token, "OP") == 0 ||
strcmp(token, "O") == 0) {
if (nexttoken() == 1) {
ttystr("\r\nRJE121A The hostname is missing, try again\r\n");
return (0);
}
if (status > INITIAL_WAIT) {
ttystr("\n\rRJE122A Connection is already open. CLOSE will close it.\r\n");
return (0);
}
gettoken(0);
strcpy(inethost, token);
if (nexttoken() == 1) {
ttystr("\r\nRJE123A Remote host port is missing, try again.\r\n");
return (0);
}
gettoken(0);
inetport=atoi(token);
if (nexttoken() == 0) {
ttystr("\r\nRJE124W Extra data after the port number is ignored\r\n");
}
connecthost();
return (0);
}
if (strcmp(token, "SHELL") == 0 || strcmp(token, "SH") == 0 ||
strcmp(token, "!") == 0) {
ttystr("\r\n\r\n");
fflush(stdout); /* flush stdout */
#if defined (_WIN32)
system ("command.com");
#else
tcsetattr (0, TCSAFLUSH, &cmdtty);
system ("bash");
tcsetattr (0, TCSAFLUSH, &runtty);
#endif
return (0);
}
if (strcmp(token, "SIGNON") == 0 ||
strcmp(token, "SIGNO") == 0 ||
strcmp(token, "SIGN") == 0 ||
strcmp(token, "SIG") == 0 ||
strcmp(token, "SI") == 0) {
if (status < INITIAL_WAIT) {
ttystr("\r\nRJE125A You need a connection first. Use OPEN.\r\n");
return (0);
}
if (nexttoken() == 1) {
ttystr("\r\nRJE126A The user id is missing, try again\r\n");
return (0);
}
gettoken(0);
if (strcmp(token, "*") == 0) {
ttystr("\r\nRJE300I Signon bypassed.");
status = IDLE;
pollflag = 2;
pollctr = 0;
return (0);
}
ttystr("\r\n");
// Flush out anything that might be in the pipe
while (1) {
rc = read_poll(0, 5000);
if (rc == 0) break;
rc = read_data(3, 0, 0);
}
// Send the initial ENQ and see if the host gives us an ACK0
retry = 0;
while (retry < 3) {
line_out[0] = line_out[1] = line_out[2] = SYN;
line_out[3] = ENQ;
line_out_size = 4;
write_buffer();
rc = read_data(3, 0, 0);
if (rc != 0)
break;
retry++;
}
if (rc == -1) {
ttystr("\r\nRJE127S The line has disconnected.\r\n");
return (0);
}
if (rc == 0) {
ttystr("\r\nRJE128S The host did not respond to our initial greeting.\r\n");
return (0);
}
if (line_in[0] == NAK || (line_in[0] == DLE && line_in[1] == NAK)) {
ttystr("\r\nRJE129S The host says (with a NAK) it's not ready.\r\n");
return (0);
}
if (line_in[0] != DLE || line_in[1] != ACK0) {
return (0);
}
// Ok, great, it's talking to us.
// Build the SIGNON card
line_out[0] = SYN;
line_out[1] = SYN;
line_out[2] = STX;
line_out[3] = 0;
line_out_size = 3;
switch (opt_os) {
case 1: // VM/370
strcpy(signon, "SIGNON ");
break;
case 4: // DOS/VS
strcpy(signon, "* .. SIGNON ");
break;
case 6: // OS/360
strcpy(signon, ".. RJSTART ");
break;
default: // All others
strcpy(signon, "SIGNON ");
break;
}
strcat(signon, token);
if (nexttoken() != 1) {
gettoken(0);
strcpy(passw, token);
} else {
strcpy(passw, "");
}
switch (opt_os) {
case 1: // VM/370
strcat(signon, " ");
strcat(signon, "3780 B512 P120 TRSY PCHY");
if (strlen(passw) > 0) {
strcat(signon, " PWD=");
strcat(signon, passw);
}
break;
case 4: // DOS/VS
strcat(signon, " ");
strcat(signon, passw);
break;
case 6: // OS/360
strcat(signon, ",BRDCST=YES");
break;
default: // All others
strcat(signon, " ");
strcat(signon, passw);
break;
}
//TODO: remove
strcpy(signon, "/*SIGNON REMOTE001");
translate_to_ebcdic(signon);
strcat(line_out, signon);
line_out_size += strlen(signon);
line_out[line_out_size] = ETX;
line_out_size++;
write_buffer();
rc = read_data(10, 0, 0);
if (rc == 0) {
ttystr("\r\nRJE131S The host did not respond to the signon record.\r\n");
return (0);
}
if (line_in[0] == NAK || (line_in[0] == DLE && line_in[1] == NAK)) {
ttystr("\r\nRJE132S The host says (with a NAK) it didn't like the signon.\r\n");
return (0);
}
if (line_in[0] != DLE || line_in[1] != ACK1) {
ttystr("\r\nRJE133S The host said something odd. Use TRACE.\r\n");
return (0);
}
// Cool. The host said it got our signon.
// Let's send it an EOT so it'll know we're done
line_out[0] = line_out[1] = SYN;
line_out[2] = EOT;
line_out_size = 3;
write_buffer();
// Note: EOT does not expect a reply
status = IDLE;
pollflag = 2;
pollctr = 0;
return (0);
}
if (strcmp(token, "STATUS") == 0 ||
strcmp(token, "STATU") == 0 ||
strcmp(token, "STAT") == 0 ||
strcmp(token, "STA") == 0 ||
strcmp(token, "ST") == 0) {
if (status == NOLINK) {
ttystr("\r\nRJE134I You have not connected to the host. Use OPEN.");
}
if (status == INITIAL_WAIT) {
ttystr("\r\nRJE135I Connected but not signed on. Use SIGNON.");
}
if (status == IDLE) {
ttystr("\r\nRJE136I Link is signed on but currently idle.");
}
if (status == SENDING) {
ttystr("\r\nRJE137I Sending a file to the host.");
}
if (status == RECEIVING) {
ttystr("\r\nRJE138I Receiving data from the host.");
}
ttystr("\r\nRJE139I Received print data will be ");
if (strlen(print) > 0) {
ttystr("stored in ");
ttystr(print);
} else {
if (opt_copy == 1) {
ttystr("displayed onscreen only");
} else {
ttystr("discarded.");
}
}
ttystr("\r\nRJE140I Received punch data will be ");
if (strlen(punch) > 0) {
ttystr("stored in ");
ttystr(punch);
ttystr(" in ");
if (punch_fmt) {
ttystr("EBCDIC, recl=");
sprintf(reclen,"%d",punch_recl);
ttystr(reclen);
} else {
ttystr("ASCII");
}
} else {
ttystr("discarded");
}
return (0);
}
if (strcmp(token, "TRACE") == 0 ||
strcmp(token, "TRAC") == 0 ||
strcmp(token, "TRA") == 0 ||
strcmp(token, "TR") == 0) {
if (nexttoken() == 1) {
strcpy(token, "");
} else {
gettoken(0);
}
if (strlen(token) > 0) {
ttystr("\r\nRJE035I Data trace will be recorded in ");
ttystr(token);
} else {
ttystr("\r\nRJE036I Data trace will no longer be recorded");
if (strlen(tracefile) > 0)
fclose(tracefd);
}
strcpy(tracefile, token);
if (strlen(tracefile) > 0)
tracefd = fopen(tracefile, "w");
return (0);
}
if (strcmp(token, "PRINT") == 0 ||
strcmp(token, "PRIN") == 0 ||
strcmp(token, "PRI") == 0 ||
strcmp(token, "PR") == 0) {
if (nexttoken() == 1) {
strcpy(token, "");
} else {
gettoken(0);
}
strcpy(print, token);
ttystr("\r\nRJE141I Received print data will be ");
if (strlen(print) > 0) {
ttystr("stored in ");
ttystr(print);
if (print_open == 1)
fclose(printfd);
print_open = 0;
} else {
if (opt_copy == 1) {
ttystr("displayed onscreen only");
} else {
ttystr("discarded.");
}
}
return (0);
}
if (strcmp(token, "PUNCH") == 0 ||
strcmp(token, "PUNC") == 0 ||
strcmp(token, "PUN") == 0 ||
strcmp(token, "PU") == 0) {
if (nexttoken() == 1) {
strcpy(token, "");
} else {
gettoken(0);
}
strcpy(punch, token);
if (nexttoken() != 1) {
gettoken(1);
savep = punch_fmt;
punch_fmt = -1;
if (strcmp(token, "ASCII") == 0) {
punch_fmt = 0;
}
if (strcmp(token, "EBCDIC") == 0) {
punch_fmt = 1;
}
if (punch_fmt == -1) {
ttystr("\r\nRJE142A Invalid option following punch filename");
punch_fmt = savep;
return (0);
}
if (nexttoken() != 1) {
gettoken(0);
savep = punch_recl;
punch_recl=atoi(token);
if (punch_recl < 80 || punch_recl > 512) {
ttystr("\r\nRJE143A Invalid punch record length");
punch_recl = savep;
return (0);
}
}
}
ttystr("\r\nRJE144I Received punch data will be ");
if (strlen(punch) > 0) {
ttystr("stored in ");
ttystr(punch);
if (punch_open == 1)
fclose(punchfd);
punch_open = 0;
ttystr(" in ");
if (punch_fmt) {
ttystr("EBCDIC, recl=");
sprintf(reclen,"%d",punch_recl);
ttystr(reclen);
} else {
ttystr("ASCII");
}
} else {
ttystr("discarded");
}
return (0);
}
if (strcmp(token, "SET") == 0) {
if (nexttoken() == 1) {
ttystr("\r\nRJE145I Host OS is: ");
switch (opt_os) {
case 0:
ttystr("Generic RJE");
break;
case 1:
ttystr("VM/370 R6 RSCS V1.");
break;
case 2:
ttystr("MVS JES2");
break;
case 3:
ttystr("MVS JES3");
break;
case 4:
ttystr("DOS[/VS] POWER[/VS].");
break;
case 5:
ttystr("OS/VS1 RES.");
break;
case 6:
ttystr("OS/360 RJE");
break;
}
ttystr("\r\nRJE146I Route to this user id: ");
if (strlen(opt_user) > 0) {
ttystr(opt_user);
} else {
ttystr("(none)");
}
ttystr("\r\nRJE147I Display printer output: ");
if (opt_copy == 1) {
ttystr("ON");
} else {
ttystr("OFF");
}
ttystr("\r\nRJE148I Poll when idle: ");
if (opt_poll == 1) {
ttystr("ON");
} else {
ttystr("OFF");
}
ttystr("\r\nRJE301I openrent send: ");
if (opt_trn == 1) {
ttystr("ON");
} else {
ttystr("OFF");
}
// ttystr("\r\nRJE148I Pause printer display: ");
// switch (opt_pause) {
// case -1:
// ttystr("on form feed.");
// break;
// case 0:
// ttystr("never.");
// break;
// default:
// sprintf(wstr, "every %d lines.", opt_pause);
// ttystr(wstr);
// break;
// }
} else {
gettoken(1);
if (strcmp(token, "NOCOPY") == 0) {
opt_copy = 0;
return (0);
}
if (strcmp(token, "COPY") == 0) {
opt_copy = 1;
return (0);
}
if (strcmp(token, "NOPOLL") == 0) {
opt_poll = 0;
return (0);
}
if (strcmp(token, "POLL") == 0) {
opt_poll = 1;
return (0);
}