forked from PDP-10/supdup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supdupd.c
1505 lines (1352 loc) · 37.8 KB
/
supdupd.c
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
/* SUPDUP Server
*
* Written Jan. 84 by David Bridgham. The organization and some of
* the code was taken from the telnet server written by Berkeley for 4.2.
*/
/* Hacked by Mly to clarify ITS ITP July 1987
* Hacked by Mly 29-Aug-87 to nuke stupid auto_right_margin lossage
* Hacked by Mly 2-Sep-87 to send params as "4;" and "146;" rather than
* "\004" and "\222" to avoid stupid un*x 8-bit-and-control-d non-transparency
* Hacked by [email protected] 25-Jan-88 to use winning 4.3BSD
* -p flag to /bin/login and to do TIOCSWINSZ stuff.
* #ifdef TTYLOC around ttyloc-hacking parts
*/
/* The magic number #o176 which appears in this file is the difference
* between the real Supdup TDxxx codes and the kludge codes which appear
* in the termcap/terminfo descriptions (which are needed because bloody
* un*x deals incompetently with chars with the #o200 bit on
* (as well as with #o004. Cretins.)
* Thus, for example, we have %TDMV0 = #o217, with "cm=\177\021%d;%d;:"
* in the termcap entry; (- #o 217 #o021) => #o176
*/
/* #define TERMINFO 1 */ /* Define if want terminfo support. */
/* there should be a TERMCAP too */
#define _XOPEN_SOURCE 500 /* for unlockpt and ptsname */
#define _DEFAULT_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <unistd.h>
#include <termios.h>
/* #include <arpa/telnet.h> */
#include "supdup.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <sgtty.h>
#include <netdb.h>
#ifdef TERMINFO
# include <term.h>
# undef CUR
# define CUR
#endif /* TERMINFO */
#ifndef BANNER
#define BANNER "MIT Artificial Intelligence Laboratory 4.2 BSD UNIX (%s)\207%s"
#endif
#ifndef SBANNER
#define SBANNER "%s SUPDUP from %s"
#endif
/* These variables are set at initial connection time */
char ttyopt[6];
#define TOERS (ttyopt[0] & 4) /* Terminal can erase */
#define TOMVB (ttyopt[0] & 1) /* can move backwards */
#define TOOVR (ttyopt[1] & 010) /* Over printing */
#define TOMVU (ttyopt[1] & 4) /* can move up */
#define TOFCI (ttyopt[2] & 010) /* Terminal can transmit full 12-bit MIT ascii */
#define TOLID (ttyopt[2] & 2) /* Line insert/delete */
#define TOCID (ttyopt[2] & 1) /* Character insert/delete */
#define TOSA1 (ttyopt[1] & 020) /* send SAIL characters direct */
#define TOMOR (ttyopt[1] & 2) /* Do more processing */
#define TOROL (ttyopt[1] & 1) /* Do scrolling */
#define TPPRN (ttyopt[4] & 2) /* Swap parens and brackets (ignored)*/
short ttyrol; /* How much the terminal scrolls by */
/* * I/O data buffers, pointers, and counters. */
unsigned char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
unsigned char ptyobuf[BUFSIZ], *pfrontp = ptyobuf, *pbackp = ptyobuf;
unsigned char netibuf[BUFSIZ], *netip = netibuf;
unsigned char netobuf[BUFSIZ], *nfrontp = netobuf, *nbackp = netobuf;
int pcc, ncc;
/* Filedesc for stream connected to local un*x pty. */
int pty;
/* Filedesc for stream connected to user (via internet) */
int net;
#ifdef DAEMON
static struct sockaddr_in sin = { AF_INET };
#endif
void reapchild (void);
void doit (int f, struct sockaddr_in *who);
void fatalmsg (int f, char *msg);
void fatalperror (int f, char *msg, int errn);
void sup_options (int net);
void supdup (int f, int p);
void change_lflag (int on, int off);
void ptyflush (void);
void netflush (void);
void supxmit (void);
void suprcv (void);
void do_crlf (void);
void cleanup (void);
void rmut (void);
char pty_name[FILENAME_MAX];
#ifdef TTYLOC
char ttyloc[64];
#endif /* TTYLOC */
extern char **environ;
/* Current cursor position */
int currcol, currline;
#ifndef TERMINFO /* terminfo defines these itself */
/* Number of columns, lines of virtual terminal */
int columns, lines;
#endif
#ifndef DEBUG
#define DPRINTF(a, b, c) {}
#else /* DEBUG */
#define DPRINTF(a, b, c) \
if (debug_output) \
{ fprintf (debug_output, (a), (b), (c)); fflush (debug_output); }
FILE *debug_output = 0;
void
open_debug_output (void)
{
debug_output = fopen ("/supdupd.debug", "w");
if (debug_output < 0)
{
fprintf (stderr, "Couldn't open \"/supdupd.debug\"\n");
debug_output = 0;
}
DPRINTF ("Starting up supdupd...\n",0,0);
}
#endif /* DEBUG */
#if 0
/* this routine is used for debugging only */
void echo_char (unsigned char c)
{
static int count = 0;
if (count++ > 16)
{
printf ("\n");
count = 0;
}
if (c >= '\177') printf ("\\%o ", c);
else if (c < '\041') printf ("\\%o ", c);
else printf ("%c ", c);
fflush (stdout);
}
#endif /* 0 */
#ifdef DAEMON
int main (int argc, char *argv[])
{
int s, pid, options;
struct servent *sp;
#ifdef DEBUG
open_debug_output ();
#endif /* DEBUG */
sp = getservbyname ("supdup", "tcp");
if (sp == 0)
{
fprintf (stderr, "supdupd: tcp/supdup: unknown service\n");
exit (1);
}
sin.sin_port = sp->s_port;
argc--, argv++;
if (argc > 0 && !strcmp (*argv, "-d"))
{
options |= SO_DEBUG;
argc--, argv++;
}
if (argc > 0)
{
sin.sin_port = atoi (*argv);
if (sin.sin_port <= 0)
{
fprintf (stderr, "supdupd: %s: bad port #\n", *argv);
exit (1);
}
sin.sin_port = htons ((u_short)sin.sin_port);
}
#ifndef DEBUG
if (fork ())
exit (0);
for (s = 0; s < 10; s++)
(void) close (s);
(void) open ("/", 0);
(void) dup2 (0, 1);
(void) dup2 (0, 2);
{
int tt = open ("/dev/tty", 2);
if (tt > 0)
{
ioctl (tt, TIOCNOTTY, 0);
close (tt);
}
}
#endif /* DEBUG */
again:
s = socket (AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
perror ("supdupd: socket");;
sleep (5);
goto again;
}
if (options & SO_DEBUG)
if (setsockopt (s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
perror ("telnetd: setsockopt (SO_DEBUG)");
if (setsockopt (s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
perror ("supdupd: setsockopt (SO_KEEPALIVE)");
while (bind (s, (caddr_t) &sin, sizeof (sin)) < 0)
{
perror ("supdupd: bind");
sleep (5);
}
signal (SIGCHLD, reapchild);
listen (s, 10);
for (;;)
{
struct sockaddr_in from;
int s2, fromlen = sizeof (from);
s2 = accept (s, (caddr_t)&from, &fromlen);
if (s2 < 0)
{
if (errno == EINTR)
continue;
perror ("supdupd: accept");
sleep (1);
continue;
}
if ((pid = fork ()) < 0)
printf ("Out of processes\n");
else if (pid == 0)
{
signal (SIGCHLD, SIG_DFL);
doit (s2, &from);
}
close (s2);
}
/*NOTREACHED*/
}
#else /* not DAEMON */
int main (int argc, char *argv[])
{
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
#ifdef DEBUG
open_debug_output ();
#endif /* DEBUG */
fromlen = sizeof (from);
if (getpeername (0, (struct sockaddr *)&from, &fromlen) < 0)
{
fprintf (stderr, "%s: ", argv[0]);
perror ("getpeername");
_exit (1);
}
/* MIT */
#ifdef KEEPALIVE
if (setsockopt (0, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
{
fprintf (stderr, "%s: ", argv[0]);
perror ("setsockopt (SO_KEEPALIVE)");
}
#endif /* KEEPALIVE */
/* MIT */
doit (0, &from);
}
#endif /* DAEMON */
void reapchild (void)
{
int status;
while (wait3 (&status, WNOHANG, 0) > 0)
;
}
/* #ifdef TERMCAP */
char termcap[1024];
/* #endif // TERMCAP */
#ifdef TERMINFO
char terminfo[64];
#endif /* TERMINFO */
char *envinit[] =
{
"TERM=supdup",
/* #ifdef TERMCAP */
termcap,
/* #endif // TERMCAP */
#ifdef TERMINFO
terminfo,
#endif /* TERMINFO */
0
};
char *host;
#ifdef TIOCSWINSZ
struct winsize ws;
#endif
/*
* Get a pty, scan input lines.
*/
void doit (int f, struct sockaddr_in *who)
{
char *cp, *ntoa ();
int i, p, t;
struct termios b;
struct hostent *hp;
p = open ("/dev/ptmx", O_RDWR, 0);
if (p < 0)
fatalmsg (f, "Couldn't get a pty");
if (grantpt(p) < 0)
fatalperror (f, "grantpt", errno);
if (unlockpt(p) < 0)
fatalperror (f, "unlockpt", errno);
if ((cp = ptsname(p)) == NULL)
fatalperror (f, "ptsname", errno);
dup2 (f, 0);
t = open ("/dev/tty", O_RDWR, 0);
if (t >= 0)
{
ioctl (t, TIOCNOTTY, 0);
close (t);
}
t = open (cp, O_RDWR, 0);
if (t < 0)
fatalperror (f, cp, errno);
tcgetattr (t, &b);
/* MIT */
cfsetispeed(&b, B9600);
cfsetospeed(&b, B9600);
/* MIT */
b.c_oflag |= XTABS;
tcsetattr (t, 0, &b);
tcgetattr (p, &b);
b.c_lflag &= ~ECHO;
tcsetattr (p, 0, &b);
sup_options (f);
#ifdef TIOCSWINSZ
ioctl (p, TIOCSWINSZ, &ws);
#endif /* TIOCSWINSZ */
hp = gethostbyaddr (&who->sin_addr, sizeof (struct in_addr),
who->sin_family);
if (hp)
host = hp->h_name;
else
host = ntoa (who->sin_addr);
if ((i = fork ()) < 0)
fatalperror (f, "fork", errno);
if (i)
supdup (f, p);
close (f);
close (p);
dup2 (t, 0);
dup2 (t, 1);
dup2 (t, 2);
close (t);
#ifdef BSD4_3
/* Winning -p switch passes through environment */
execle ("/bin/login", "login", "-p", "-h", host, (char *)0, envinit);
#else
#ifdef MIT
/* Local /bin/login at ai.mit.edu machines hacked to pass TERM and TERMCAP */
execle ("/bin/login", "login", "-h", host, (char *)0, envinit);
fatalperror (2, "/bin/login", errno);
#else
/* Can't use /bin/login as that nukes TERM and TERMCAP. Cretins! */
/* For now, be a cretin and use /bin/login anyway. */
/* execle ("/usr/etc/supdup-login", "login", "-h", host, (char *)0, envinit);
fatalperror (2, "/usr/etc/supdup-login", errno); */
execle ("/bin/login", "login", (char *)0, envinit);
#endif /* MIT */
#endif /* not BSD4_3 */
/*NOTREACHED*/
}
void fatalmsg (int f, char *msg)
{
char buf[BUFSIZ];
(void) sprintf (buf, "supdupd: %s.\r\n", msg);
(void) write (f, buf, strlen (buf));
exit (1);
}
void fatalperror (int f, char *msg, int errn)
{
char buf[BUFSIZ];
(void) sprintf (buf, "%s: %s", msg, strerror(errn));
fatalmsg (f, buf);
}
static void sig_handler_cleanup(int sig) { cleanup(); }
/*
* Main loop. Select from pty and network, and
* hand data to supdup receiver finite state machine.
*/
void supdup (int f, int p)
{
int on = 1;
char hostname[256];
net = f, pty = p;
ioctl (f, FIONBIO, &on);
ioctl (p, FIONBIO, &on);
signal (SIGTSTP, SIG_IGN);
signal (SIGCHLD, sig_handler_cleanup);
change_lflag (ECHO, 0);
/*
* Print supdup banner.
*/
gethostname (hostname, sizeof (hostname));
nfrontp += sprintf ((char *) nfrontp, SBANNER, hostname, host);
*nfrontp++ = TDNOP;
netflush ();
sleep (2);
/*
* Show banner that getty never gave.
*/
*nfrontp++ = TDCLR;
nfrontp += sprintf ((char *)nfrontp, BANNER, hostname, "");
currline = 1;
currcol = 0;
for (;;)
{
fd_set ibits, obits;
int maxfd = p > f ? p : f;
FD_ZERO(&ibits);
FD_ZERO(&obits);
/*
* Never look for input if there's still
* stuff in the corresponding output buffer
*/
if ((nfrontp - nbackp) || pcc > 0)
FD_SET(f, &obits);
else
FD_SET(p, &ibits);
if ((pfrontp - pbackp) || ncc > 0)
FD_SET(p, &obits);
else
FD_SET(f, &ibits);
if (ncc < 0 && pcc < 0)
break;
select (maxfd + 1, &ibits, &obits, NULL, NULL);
/*
* Something to read from the network...
*/
if (FD_ISSET(f, &ibits))
{
ncc = read (f, netibuf, BUFSIZ);
if (ncc < 0 && errno == EWOULDBLOCK)
ncc = 0;
else
{
if (ncc <= 0)
break;
netip = netibuf;
}
}
/*
* Something to read from the pty...
*/
if (FD_ISSET(p, &ibits))
{
pcc = read (p, ptyibuf, BUFSIZ);
if (pcc < 0 && errno == EWOULDBLOCK)
pcc = 0;
else
{
if (pcc <= 0)
break;
ptyip = ptyibuf;
}
}
if (pcc > 0)
supxmit ();
if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
netflush ();
if (ncc > 0)
suprcv ();
if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
ptyflush ();
}
cleanup ();
}
/* State for xmit fsm */
#define XS_DATA 0 /* base state */
#define XS_ESCAPE 1 /* supdup commands are escaped in TERMCAP */
#define XS_MV0_V 2 /* getting vertical position for TDMV0 */
#define XS_MV0_H 4 /* getting horizontal position for TDMV0 */
#define XS_CRLF 6 /* got \r looking for \n */
#define XS_LFCR 7 /* got \n looking for \r */
#if (TDILP < 8) || (TDDLP < 8) || (TDICP < 8) || (TDDCP < 8)
you lose!
#endif
#define XS_ILINE TDILP /* waiting for number of lines to insert */
#define XS_DLINE TDDLP /* waiting for number of lines to delete */
#define XS_ICHAR TDICP /* waiting for number of chars to insert */
#define XS_DCHAR TDDCP /* waiting for number of chars to delete */
/* Because of Un*x Brain Death(tm)
* characters with the 200 bit don't go through the terminal driver reliably
* and \004 won't go through a un*x at all pty (what sort of bozos wrote
* this sh*t?)
*/
void supxmit (void)
{
static int state = XS_DATA;
register int c;
static int piece_o_state, piece_o_state_v;
while (pcc > 0)
{
if ((&netobuf[BUFSIZ] - nfrontp) < 4)
/* Caller will flush pending output */
return;
c = *ptyip++ & 0377; pcc--;
switch (state)
{
case XS_DATA:
switch (c)
{
case ITP_ESCAPE:
*nfrontp++ = c;
*nfrontp++ = c;
break;
case KLUDGE_ESCAPE:
state = XS_ESCAPE;
break;
case '\007':
*nfrontp++ = TDBEL;
break;
case '\t':
currcol = (currcol + 8) & ~7;
if (currcol >= columns)
currcol = columns;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
break;
case '\b':
currcol--;
if (currcol < 0)
currcol = 0;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
break;
case '\r':
DPRINTF ("\n<<Got a return>>",0,0);
state = XS_CRLF;
break;
case '\n':
DPRINTF ("\n<<Got a newline>>",0,0);
state = XS_LFCR;
break;
case '\0':
/* throw away nulls. Something is being
* a twit and padding even though it
* is not wanted! */
/* (Of course, \0 is really a printable MIT Ascii character) */
break;
default:
if (currcol < columns)
{
currcol++;
*nfrontp++ = c;
}
else
{
/* unread-char and do newline */
ptyip--; pcc++;
do_crlf ();
}
break;
}
break;
case XS_ESCAPE:
c += 0176;
switch (c)
{
case TDCRL:
state = XS_DATA;
do_crlf ();
break;
case TDFS:
currcol++;
state = XS_DATA;
*nfrontp++ = c;
break;
case TDMV0:
state = XS_MV0_V;
piece_o_state = 0;
piece_o_state_v = 0;
break;
case TDCLR:
DPRINTF ("\n<<In TDCLR handler>>",0,0);
currcol = 0; currline = 0;
state = XS_DATA;
*nfrontp++ = c;
break;
case TDLF:
/* this is not in the SUPDUP spec */
/* Move down one row vertically */
state = XS_DATA;
if (currline < (lines - 1))
currline++;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
break;
case TDUP:
state = XS_DATA;
if (currline > 0) currline--;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
break;
case TDILP:
case TDDLP:
case TDICP:
case TDDCP:
state = c;
piece_o_state = 0;
break;
case KLUDGE_ESCAPE:
/* Really shouldn't get two */
/* of these but it's happening */
break;
default:
state = XS_DATA;
*nfrontp++ = c;
break;
}
break;
/* The newline algorithm is as follows:
* A \r causes the cursor to be sent to the
* beginning of the line unless there is a \n
* immediately following on the input.
* If the next character is a \n then a
* %TDCRL is sent.
* A lone \n causes the cursor to be moved
* straight down one line wrapping if necessary
* or doing a %TDCRL if at the bottom of the screen
* and it is a scrolling terminal.
*
* The CR and NL handling is way too complicated...
*/
case XS_LFCR:
{
DPRINTF ("\n<<In XS_LFCR handler>>",0,0);
if (c == '\r')
do_crlf ();
else
{
/* Got a \012 followed by something other than a \015 */
/* Treat the \012 as `move down down one row */
if (currline < (lines - 1))
{
*nfrontp++ = TDMV0;
*nfrontp++ = ++currline;
*nfrontp++ = currcol;
}
else if (TOROL)
{
/* this is not the right thing, but
* what else can I do? */
currcol = 0;
*nfrontp++ = TDCRL;
}
else
{
currline = 0;
*nfrontp++ = TDMV0;
*nfrontp++ = 0;
*nfrontp++ = currcol;
*nfrontp++ = TDEOL;
}
ptyip--; pcc++; /* unread-char */
DPRINTF ("\n<<LFCR: unreading %c>>", *ptyip,0);
}
state = XS_DATA;
break;
}
case XS_CRLF:
{
DPRINTF ("\n<<In XS_CRLF handler>>",0,0);
if (c == '\n')
do_crlf ();
else
{
/* \013 followed by something other than \010 */
/* Treat the \010 as a request to move to start of row */
currcol = 0;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = 0;
ptyip--; pcc++; /* unread-char */
DPRINTF ("\n<<CRLF: unreading %c>>", *ptyip,0);
}
state = XS_DATA;
break;
}
case XS_MV0_V:
case XS_MV0_H:
case TDILP:
case TDDLP:
case TDICP:
case TDDCP:
{
DPRINTF ("\n<<arg:%d+%c>>", piece_o_state, c);
if (c >= '0' && c <= '9')
{
piece_o_state = piece_o_state * 10 + c - '0';
if (piece_o_state >= lines && piece_o_state >= columns)
{
state = XS_DATA;
break;
}
}
else if (c != ';')
{
state = XS_DATA;
break;
}
else switch (state)
{
case XS_MV0_V:
piece_o_state_v = piece_o_state;
piece_o_state = 0;
state = XS_MV0_H;
break;
case XS_MV0_H:
currline = piece_o_state_v;
currcol = piece_o_state;
state = XS_DATA;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
break;
default: /* TDILP, TDDLP, RDICP, TDDCP */
while (pcc > 4 && (ptyip[0] & 0377) == KLUDGE_ESCAPE &&
(ptyip[1] & 0377) == state - 0176 &&
(ptyip[2] & 0377) == '1' &&
(ptyip[3] & 0377) == ';')
/* Merge successive single insert/delete-line/char requests
(pander to clients who don't understand TERMCAP
AL/DL (and less-importantly IC/DC) capabilities) */
{
piece_o_state++;
pcc -= 4;
ptyip += 4;
}
*nfrontp++ = state;
*nfrontp++ = piece_o_state;
if (state == TDILP || state == TDDLP)
{
/* Just to be on the safe side */
currcol = 0;
*nfrontp++ = TDMV0;
*nfrontp++ = currline;
*nfrontp++ = currcol;
}
state = XS_DATA;
break;
}
DPRINTF ("\n<<now:%d,%d>>", piece_o_state_v, piece_o_state);
break;
}
default:
state = XS_DATA;
break;
}
}
}
void do_crlf (void)
{
currcol = 0;
if (currline < (lines - 1))
{
*nfrontp++ = TDCRL;
currline++;
}
else if (TOROL)
*nfrontp++ = TDCRL;
else
{
currline = 0;
*nfrontp++ = TDMV0;
*nfrontp++ = 0;
*nfrontp++ = 0;
*nfrontp++ = TDEOL;
}
}
/*
* State for recv fsm
*/
/* base state */
#define RS_DATA 0
/* recieved ITP_ESCAPE (034) Waiting for `m' */
#define RS_ITP_ESCAPE 1
/* received ITP_ESCAPE, `m'>#o100
Waiting for `n'.
Char will be (+ (* (- `m' #o100) #o200) `n') */
#define RS_BUCKY 2
/* Recived ITP_ESCAPE, ITP_FLOW_CONTROL_INCREASE
Ignore next char, since un*x can't hack real, winning, flow control */
#define RS_FLOW_CONTROL_INCREASE 3
/* Recived ITP_ESCAPE, ITP_CURSORPOS
Waiting for `row' */
#define RS_CURSORPOS_1 4
/* Recived ITP_ESCAPE, ITP_CURSORPOS, `row'
Waiting for `column' */
#define RS_CURSORPOS_2 5
/* received SUPDUP_ESCAPE (#o300)
Waiting for `m' */
#define RS_SUPDUP_ESCAPE 6
/* received SUPDUP_ESCAPE, SUPDUP_LOCATION.
Now receiving 000-terminated
location string */
#define RS_LOCATION 7
void suprcv (void)
{
register int c;
static int state = RS_DATA;
static int piece_o_state = 69;
while ((ncc > 0) &&
/* Caller must flush pending output */
((&ptyobuf[BUFSIZ] - pfrontp) > 3))
{
c = *netip++ & 0377; ncc--;
switch (state)
{
case RS_DATA:
{
if (c == SUPDUP_ESCAPE)
state = RS_SUPDUP_ESCAPE;
else if (c == ITP_ESCAPE)
state = RS_ITP_ESCAPE;
else
*pfrontp++ = c;
break;
}
case RS_ITP_ESCAPE:
{
if (c & 0100)
{
piece_o_state = c;
state = RS_BUCKY;
}
else switch (c)
{
case ITP_ESCAPE:
*pfrontp++ = c;
state = RS_DATA;
break;
case ITP_PIATY:
/* %piaty --- user's screen is munged.
Cretinous unix provides no way to use this.
Conceivably we could send through a c-L character (barf!)
*/
case ITP_STOP_OUTPUT:
case ITP_RESTART_OUTPUT:
case ITP_FLOW_CONTROL_START:
case ITP_FLOW_CONTROL_END:
/* ignore it */
state = RS_DATA;
break;
case ITP_FLOW_CONTROL_INCREASE:
state = RS_FLOW_CONTROL_INCREASE;
break;
case ITP_CURSORPOS:
/* read cursorpos -- v then h */
state = RS_CURSORPOS_1;
break;
default: /* huh?! */
state = RS_DATA;
break;
}
break;
}
case RS_BUCKY:
{
register int bucky;
bucky = ITP_CHAR (piece_o_state, c);
/* unix supdup server uses 0237 (CTL_PREFIX) as a control escape.
* c-a 001
* m-a 341
* c-m-a 201
* c-1 237 061
* m-1 261
* c-m-1 237 261
* c-m-_ 237 237
*/
switch (bucky)
{
case SUPDUP_HELP_KEY:
c = 'h' & 037; /* hack for Help key */
break;
case SUPDUP_ESCAPE_KEY:
c = '\033';
break;
case SUPDUP_SUSPEND_KEY:
c = 'z' & 037;
break;
case SUPDUP_CLEAR_KEY:
c = -1;
break;
default:
c = bucky & 0177;
if (bucky & ITP_TOP)
c &= 037; /* This is pretty random -- Mly */
if (bucky & ITP_CTL)
{
if ((c >= 'a' && c <= 'z') ||