-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckucns.c
2745 lines (2519 loc) · 77.1 KB
/
ckucns.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
#include "ckcsym.h"
char *connv = "CONNECT Command for UNIX:select(), 10.0.143, 15 Apr 2023";
/* C K U C N S -- Terminal connection to remote system, for UNIX */
/*
Author: Frank da Cruz <[email protected]>,
Columbia University Academic Information Systems, New York City.
Copyright (C) 1985, 2023,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
*/
/*
This version of the UNIX CONNECT module uses select(), which is required for
Kerberos encryption. Thus it can be used only on UNIX systems that support
select() on both TCP/IP and serial connections. A separate module that uses
a completely portable fork() structure can be used on systems where select()
is not available or does not work as required.
*/
#include "ckcdeb.h" /* Common things first */
#ifndef NOLOCAL
#ifdef OSF13
#ifdef CK_ANSIC
#ifdef _NO_PROTO
#undef _NO_PROTO
#endif /* _NO_PROTO */
#endif /* CK_ANSIC */
#endif /* OSF13 */
#include <errno.h> /* Error numbers */
#ifndef NOTIMEH
#include <time.h> /* For FD_blah */
#ifdef SYSTIMEH /* (IRIX 5.3) */
#include <sys/time.h>
#endif /* SYSTIMEH */
#endif /* NOTIMEH */
#ifdef BSD42HACK /* Why is this necessary? */
#ifndef DCLTIMEVAL
#define DCLTIMEVAL
#endif /* DCLTIMEVAL */
#endif /* BSD42HACK */
/* Kermit-specific includes */
#include "ckcasc.h" /* ASCII characters */
#include "ckcker.h" /* Kermit things */
#include "ckucmd.h" /* For xxesc() prototype */
#include "ckcnet.h" /* Network symbols */
#include "ckuusr.h"
#ifndef NOCSETS
#include "ckcxla.h" /* Character set translation */
#endif /* NOCSETS */
#ifdef BEBOX
#include <kernel/OS.h>
#include <socket.h>
#include <stdio.h>
#endif /* BEBOX */
#include <signal.h> /* Signals */
/* All the following is for select()... */
#ifdef CKTIDLE /* Timeouts only for SET TERM IDLE */
#ifndef DCLTIMEVAL
#ifdef UNIXWARE
#ifndef UW7
#define DCLTIMEVAL
#endif /* UW7 */
#endif /* UNIXWARE */
#endif /* DCLTIMEVAL */
#ifdef DCLTIMEVAL /* Declare timeval ourselves */
struct timeval {
long tv_sec;
long tv_usec;
};
#else /* !DCLTIMEVAL */
#ifndef NOSYSTIMEBH
#ifdef SYSTIMEBH
#include <sys/timeb.h>
#endif /* SYSTIMEBH */
#endif /* NOSYSTIMEBH */
#endif /* DCLTIMEVAL */
#endif /* CKTIDLE */
#ifndef SCO_OSR504
#ifdef SELECT_H
#include <sys/select.h>
#endif /* SELECT_H */
#endif /* SCO_OSR504 */
#ifndef FD_SETSIZE
#ifdef CK_FORWARD_X
#define FD_SETSIZE 256
#else
#define FD_SETSIZE 32
#endif /* CK_FORWARD_X */
#endif /* FD_SETSIZE */
#ifdef HPUX
#ifndef HPUX10
#ifndef HPUX1100
/* The three interior args to select() are (int *) rather than (fd_set *) */
#ifndef INTSELECT
#define INTSELECT
#endif /* INTSELECT */
#endif /* HPUX1100 */
#endif /* HPUX10 */
#endif /* HPUX */
#ifdef CK_AUTHENTICATION
#include "ckuath.h" /* fdc 2021-12-17 */
#endif /* CK_AUTHENTICATION */
#include "ckcfnp.h" /* Prototypes (must be last) */
/* Internal function prototypes */
#ifdef NEWFTP
#endif /* NEWFTP */
_PROTOTYP( VOID ttflux, (void) );
_PROTOTYP( VOID doesc, (char) );
_PROTOTYP( int hconne, (void) );
#ifndef NOSHOW
_PROTOTYP( VOID shomdm, (void) );
#endif /* NOSHOW */
_PROTOTYP( static int kbget, (void) );
_PROTOTYP( static int ckcputf, (void) );
/* External variables */
extern struct ck_p ptab[];
extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, sosi, tnlm,
xitsta, what, ttyfd, ttpipe, quiet, backgrd, pflag, tt_crd, tt_lfd,
tn_nlm, ttfdflg,
tt_escape, justone, carrier, ttpty, hwparity;
#ifndef NODIAL
extern int dialmhu, dialsta;
#endif /* NODIAL */
#ifdef CKLEARN
extern FILE * learnfp;
extern int learning;
static ULONG learnt1;
static char learnbuf[LEARNBUFSIZ] = { NUL, NUL };
static int learnbc = 0;
static int learnbp = 0;
static int learnst = 0;
#endif /* CKLEARN */
extern long speed;
extern char ttname[], sesfil[], myhost[], *ccntab[];
#ifdef TNCODE
extern int tn_b_nlm, tn_rem_echo;
#endif /* TNCODE */
#ifdef CK_TRIGGER
extern char * tt_trigger[], * triggerval;
#endif /* CK_TRIGGER */
#ifdef CKTIDLE
extern int tt_idlelimit, tt_idleact;
extern char * tt_idlestr;
static int idlelimit = 0;
#endif /* CKTIDLE */
extern int cx_status; /* CONNECT status code */
extern int nopush;
#ifdef CK_APC
extern int apcactive; /* Application Program Command (APC) */
extern int apcstatus; /* items ... */
static int apclength = 0;
#ifdef DCMDBUF
extern char *apcbuf;
#else
extern char apcbuf[];
#endif /* DCMDBUF */
static int apcbuflen = APCBUFLEN - 2;
extern int protocol;
#endif /* CK_APC */
#ifndef NOXFER
extern int autodl; /* Auto download */
#endif /* NOXFER */
#ifdef CK_AUTODL
extern CHAR ksbuf[];
extern CHAR stchr;
extern int kstartactive;
#endif /* CK_AUTODL */
#ifdef CK_ENCRYPTION
extern int me_auth;
#endif /* CK_ENCRYPTION */
#ifdef CK_XYZ
#ifdef XYZ_INTERNAL
static int zmdlok = 1; /* Zmodem autodownloads available */
#else
static int zmdlok = 0; /* Depends on external protocol def */
#endif /* XYZ_INTERNAL */
#else
static int zmdlok = 0; /* Not available at all */
#endif /* CK_XYZ */
#ifndef NOSETKEY /* Keyboard mapping */
extern KEY *keymap; /* Single-character key map */
extern MACRO *macrotab; /* Key macro pointer table */
static MACRO kmptr = NULL; /* Pointer to current key macro */
#endif /* NOSETKEY */
/* Global variables local to this module */
static int
active = 0,
quitnow = 0, /* <esc-char>Q was typed */
dohangup = 0, /* <esc-char>H was typed */
inshift = 0, /* SO/SI shift states */
outshift = 0;
static char ecbuf[10], *ecbp; /* Escape char buffer & pointer */
#ifdef CK_SMALL
#define IBUFL 1536 /* Input buffer length */
#else
#define IBUFL 4096
#endif /* CK_SMALL */
static int obc = 0; /* Output buffer count */
#ifndef OXOS
#define OBUFL 1024 /* Output buffer length */
#else
#define OBUFL IBUFL
#endif /* OXOS */
#ifdef BIGBUFOK
#define TMPLEN 4096 /* Temporary message buffer length */
#else
#define TMPLEN 200
#endif /* BIGBUFOK */
#ifdef DYNAMIC
static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
#else
static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
#endif /* DYNAMIC */
#ifdef TNCODE
static char tnopt[4];
#endif /* TNCODE */
#ifdef DYNAMIC
static char *ibp; /* Input buffer pointer */
#else
static char *ibp = ibuf; /* Input buffer pointer */
#endif /*DYNAMIC */
static int ibc = 0; /* Input buffer count */
#ifdef DYNAMIC
static char *obp; /* Output buffer pointer */
#else
static char *obp = obuf; /* Output buffer pointer */
#endif /* DYNAMIC */
/* Character-set items */
static int unicode = 0;
#ifndef NOCSETS
#ifdef CK_ANSIC /* ANSI C prototypes... */
extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
static CHAR (*sxo)(CHAR); /* Local translation functions */
static CHAR (*rxo)(CHAR); /* for output (sending) terminal chars */
static CHAR (*sxi)(CHAR); /* and for input (receiving) terminal chars. */
static CHAR (*rxi)(CHAR);
#else /* Not ANSI C... */
extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(); /* Character set */
extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(); /* translation functions. */
static CHAR (*sxo)(); /* Local translation functions */
static CHAR (*rxo)(); /* for output (sending) terminal chars */
static CHAR (*sxi)(); /* and for input (receiving) terminal chars. */
static CHAR (*rxi)();
#endif /* CK_ANSIC */
extern int language; /* Current language. */
static int langsv; /* For remembering language setting. */
extern struct csinfo fcsinfo[]; /* File character set info. */
extern int tcsr, tcsl; /* Terminal character sets, remote & local. */
static int tcs; /* Intermediate ("transfer") character set. */
static int tcssize = 0; /* Size of tcs */
#ifdef UNICODE /* UTF-8 support */
#ifdef CK_ANSIC
extern int (*xl_ufc[MAXFCSETS+1])(USHORT); /* Unicode to FCS */
extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
#else
extern int (*xl_ufc[MAXFCSETS+1])();
extern USHORT (*xl_fcu[MAXFCSETS+1])();
extern int (*xuf)();
extern USHORT (*xfu)();
#endif /* CK_ANSIC */
#endif /* UNICODE */
#endif /* NOCSETS */
static int printing = 0;
/*
We do not need to parse and recognize escape sequences if we are being built
without character-set support AND without APC support.
*/
#ifdef NOESCSEQ
#ifdef XPRINT
#undef XPRINT
#endif /* XPRINT */
#else /* NOESCSEQ not defined from outside */
#ifdef NOCSETS /* No character sets */
#ifndef CK_APC /* No APC */
#ifndef XPRINT /* No transparent printing */
#define NOESCSEQ /* So no escape sequence recognizer */
#endif /* XPRINT */
#endif /* CK_APC */
#endif /* NOCSETS */
#endif /* NOESCSEQ */
/* inesc[] and oldesc[] made global 2010/03/01 for INPUT command */
static int escseq = 0; /* 1 = Recognizer is active */
/* static */ int inesc[2] = { 0, 0 }; /* State of sequence recognizer */
/* static */ int oldesc[2] = { -1, -1 }; /* Previous state of recognizer */
#ifdef NOESCSEQ
#define ES_NORMAL 0 /* Normal, not in an escape sequence */
#define chkaes(x,y) 0
#else
/*
As of C-Kermit 5A(178), the CONNECT command skips past ANSI escape sequences
to avoid translating the characters within them. This allows the CONNECT
command to work correctly with a host that uses a 7-bit ISO 646 national
character set, in which characters like '[' would normally be converted to
accented letters, ruining the terminal's interpretation (and generation)
of escape sequences.
As of 5A(190), the CONNECT command responds to APC escape sequences
(ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
program was built with CK_APC defined.
Non-ANSI/ISO-compliant escape sequences are not handled. */
/* States for the escape-sequence recognizer. */
#define ES_NORMAL 0 /* Normal, not in an escape sequence */
#define ES_GOTESC 1 /* Current character is ESC */
#define ES_ESCSEQ 2 /* Inside an escape sequence */
#define ES_GOTCSI 3 /* Inside a control sequence */
#define ES_STRING 4 /* Inside DCS,OSC,PM, or APC string */
#define ES_TERMIN 5 /* 1st char of string terminator */
/*
ANSI escape sequence handling. Only the 7-bit form is treated, because
translation is not a problem in the 8-bit environment, in which all GL
characters are ASCII and no translation takes place. So we don't check
for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
Here is the ANSI sequence recognizer state table, followed by the code
that implements it.
Definitions:
CAN = Cancel 01/08 Ctrl-X
SUB = Substitute 01/10 Ctrl-Z
DCS = Device Control Sequence 01/11 05/00 ESC P
CSI = Control Sequence Introducer 01/11 05/11 ESC [
ST = String Terminator 01/11 05/12 ESC \
OSC = Operating System Command 01/11 05/13 ESC ]
PM = Privacy Message 01/11 05/14 ESC ^
APC = Application Program Command 01/11 05/15 ESC _
ANSI escape sequence recognizer:
State Input New State ; Commentary
NORMAL (start) ; Start in NORMAL state
(any) CAN NORMAL ; ^X cancels
(any) SUB NORMAL ; ^Z cancels
NORMAL ESC GOTESC ; Begin escape sequence
NORMAL other ; NORMAL control or graphic character
GOTESC ESC ; Start again
GOTESC [ GOTCSI ; CSI
GOTESC P STRING ; DCS introducer, consume through ST
GOTESC ] STRING ; OSC introducer, consume through ST
GOTESC ^ STRING ; PM introducer, consume through ST
GOTESC _ STRING ; APC introducer, consume through ST
GOTESC 0..~ NORMAL ; 03/00 through 17/14 = Final character
GOTESC other ESCSEQ ; Intermediate or ignored control character
ESCSEQ ESC GOTESC ; Start again
ESCSEQ 0..~ NORMAL ; 03/00 through 17/14 = Final character
ESCSEQ other ; Intermediate or ignored control character
GOTCSI ESC GOTESC ; Start again
GOTCSI @..~ NORMAL ; 04/00 through 17/14 = Final character
GOTCSI other ; Intermediate char or ignored control char
STRING ESC TERMIN ; Maybe have ST
STRING other ; Consume all else
TERMIN \ NORMAL ; End of string
TERMIN other STRING ; Still in string
*/
#ifdef XPRINT /* Transparent print support */
/*
We can't just print each byte as it comes in because then the printer-off
sequence would be sent to the printer. Thus we have to buffer up escape
sequences and print them only when they are complete AND we know they are
not the printer-off sequence. All printing is done via zsoutx(ZMFILE,s,n).
This allows for strings that contain NULs. Don't mix calls to zsoutx() with
calls to zchout(), or the output will be scrambled. Also note that when
printing a saved-up escape sequence, we never print its final character
because that will be printed in the mainline code, upon return from
chkaes(). Note that the printer-on sequence is passed to the screen; this
is unavoidable, since we don't know what it is until after we get to the
end, and for screen display purposes we can't buffer up escape sequences
for numerous reasons. Therefore we also must output the printer-off
sequence, otherwise a real terminal or emulator will be stuck in print mode.
*/
extern int tt_print;
#define ESCBUFLEN 63
static char escbuf[ESCBUFLEN+1] = { NUL, NUL };
static int escbufc = 0;
static int dontprint = 0;
VOID
printon() { /* Turn printing on */
int x, pp;
char * p;
extern int printpipe, noprinter;
extern char * printername;
if (noprinter) {
debug(F110,"PRINTER ON NOPRINTER","",0);
return;
}
p = printername;
pp = printpipe;
if (!p) p = "";
if (!*p) {
#ifdef ANYBSD
p = "lpr";
#else
p = "lp";
#endif /* ANYBSD */
pp = 1;
debug(F110,"PRINTER DEFAULT",p,0);
}
debug(F111,"PRINTER ON",p,pp);
if (pp) { /* Printing to pipe */
x = zxcmd(ZMFILE,p);
} else { /* Append to file */
struct filinfo xx;
xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
xx.typ = 0; xx.dsp = XYFZ_A; xx.os_specific = "";
xx.lblopts = 0;
x = zopeno(ZMFILE,p,NULL,&xx);
}
debug(F101,"PRINTER OPEN","",x);
printing = 1;
}
VOID
printoff() { /* Turn printing off */
int x;
extern int noprinter;
if (noprinter) {
printing = 0;
debug(F100,"PRINTER OFF NOPRINTER","",0);
return;
}
debug(F100,"PRINTER OFF","",0);
if (printing) {
x = zclose(ZMFILE);
debug(F101,"PRINTER CLOSE","",x);
printing = 0;
}
}
#endif /* XPRINT */
/*
C H K A E S -- Check ANSI Escape Sequence.
Call with EACH character in input stream.
src = 0 means c is incoming from remote; 1 = char from keyboard.
Sets global inesc[src] variable according to escape sequence state.
Returns 0 normally, 1 if an APC sequence is to be executed.
Handles transparent printing internally.
*/
int
#ifdef CK_ANSIC
chkaes(char c, int src)
#else
chkaes(c,src) char c; int src;
#endif /* CK_ANSIC */
/* chkaes */ {
debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
debug(F101,"chkaes c","",c);
if (src < 0 || src > 1) /* Don't allow bad args. */
return(0);
oldesc[src] = inesc[src]; /* Remember previous state */
#ifdef XPRINT
if (inesc[src] && !src) { /* Save up escape seq for printing */
if (!c) return(0); /* Ignore NULs */
if (escbufc < ESCBUFLEN) {
escbuf[escbufc++] = c;
escbuf[escbufc] = NUL;
debug(F111,"ESCBUF 1",escbuf,escbufc);
} else { /* Buffer overrun */
if (printing && escbufc) /* Print what's there so far */
zsoutx(ZMFILE,escbuf,escbufc);
escbufc = 1; /* clear it out */
escbuf[0] = c; /* and start off fresh buffer */
escbuf[1] = NUL; /* with this character. */
}
}
#endif /* XPRINT */
if (c == CAN || c == SUB) { /* CAN and SUB cancel any sequence */
#ifdef XPRINT
if (!src) {
if (printing && escbufc > 1)
zsoutx(ZMFILE,escbuf,escbufc-1);
escbufc = 0; /* Clear buffer */
escbuf[0] = NUL;
}
#endif /* XPRINT */
inesc[src] = ES_NORMAL;
} else /* Otherwise */
switch (inesc[src]) { /* enter state switcher */
case ES_NORMAL: /* NORMAL state */
if (c == ESC) { /* Got an ESC */
inesc[src] = ES_GOTESC; /* Change state to GOTESC */
#ifdef XPRINT
if (!src) {
escbufc = 1; /* Clear escape sequence buffer */
escbuf[0] = c; /* and deposit the ESC */
escbuf[1] = NUL;
debug(F111,"ESCBUF 2",escbuf,escbufc);
}
#endif /* XPRINT */
}
break; /* Otherwise stay in NORMAL state */
case ES_GOTESC: /* GOTESC state - prev char was ESC*/
if (c == '[') { /* Left bracket after ESC is CSI */
inesc[src] = ES_GOTCSI; /* Change to GOTCSI state */
} else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
inesc[src] = ES_STRING; /* Switch to STRING-absorption state */
#ifdef XPRINT
debug(F111,"ESCBUF STRING",escbuf,escbufc);
#endif /* XPRINT */
#ifdef CK_APC
/* If APC not disabled */
if (!src && c == '_' && (apcstatus & APC_ON)) {
debug(F100,"CONNECT APC begin","",0);
apcactive = APC_REMOTE; /* Set APC-Active flag */
apclength = 0; /* and reset APC buffer pointer */
}
#endif /* CK_APC */
} else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
inesc[src] = ES_NORMAL; /* Back to normal */
#ifdef XPRINT
if (!src) {
if (printing && escbufc > 1) {
/* Dump esc seq buf to printer */
zsoutx(ZMFILE,escbuf,escbufc-1);
debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
}
escbufc = 0; /* Clear parameter buffer */
escbuf[0] = NUL;
}
#endif /* XPRINT */
} else if (c != ESC) { /* ESC in an escape sequence... */
inesc[src] = ES_ESCSEQ; /* starts a new escape sequence */
}
break; /* Intermediate or ignored ctrl char */
case ES_ESCSEQ: /* ESCSEQ -- in an escape sequence */
if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
inesc[src] = ES_NORMAL; /* Return to NORMAL state. */
#ifdef XPRINT
if (!src) {
if (printing && escbufc > 1) {
zsoutx(ZMFILE,escbuf,escbufc-1);
debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
}
escbufc = 0; /* Clear escseq buffer */
escbuf[0] = NUL;
}
#endif /* XPRINT */
} else if (c == ESC) { /* ESC ... */
inesc[src] = ES_GOTESC; /* starts a new escape sequence */
}
break; /* Intermediate or ignored ctrl char */
case ES_GOTCSI: /* GOTCSI -- In a control sequence */
if (c > 077 && c < 0177) { /* Final character '@' thru '~' */
#ifdef XPRINT
if (!src && tt_print) { /* Printer enabled? */
if (c == 'i') { /* Final char is "i"? */
char * p = (char *) (escbuf + escbufc - 4);
if (!strncmp(p, "\033[5i", 4)) { /* Turn printer on */
printon();
} else if (!strncmp(p, "\033[4i", 4)) { /* Or off... */
int i;
printoff(); /* Turn off printer. */
dontprint = 1;
for (i = 0; i < escbufc; i++) /* And output the */
ckcputc(escbuf[i]); /* sequence. */
} else if (printing && escbufc > 1) {
zsoutx(ZMFILE,escbuf,escbufc-1);
debug(F011,"ESCBUF PRINT 3",escbuf,escbufc);
}
} else if (printing && escbufc > 1) {
zsoutx(ZMFILE,escbuf,escbufc-1);
debug(F111,"ESCBUF PRINT 4",escbuf,escbufc);
}
}
if (!src) {
escbufc = 0; /* Clear esc sequence buffer */
escbuf[0] = NUL;
}
#endif /* XPRINT */
inesc[src] = ES_NORMAL; /* Return to NORMAL. */
} else if (c == ESC) { /* ESC ... */
inesc[src] = ES_GOTESC; /* starts over. */
}
break;
case ES_STRING: /* Inside a string */
if (c == ESC) /* ESC may be 1st char of terminator */
inesc[src] = ES_TERMIN; /* Go see. */
#ifdef CK_APC
else if (apcactive) { /* If in APC */
if (apclength < apcbuflen) { /* and there is room... */
apcbuf[apclength++] = c; /* deposit this character. */
} else { /* Buffer overrun */
apcactive = 0; /* Discard what we got */
apclength = 0; /* and go back to normal */
apcbuf[0] = 0; /* Not pretty, but what else */
inesc[src] = ES_NORMAL; /* can we do? (ST might not come) */
}
}
#endif /* CK_APC */
break; /* Absorb all other characters. */
case ES_TERMIN: /* Maybe a string terminator */
if (c == '\\') { /* which must be backslash */
inesc[src] = ES_NORMAL; /* If so, back to NORMAL */
#ifdef XPRINT
if (!src) {
if (printing && escbufc > 1) { /* If printing... */
/* Print esc seq buffer */
zsoutx(ZMFILE,escbuf,escbufc-1);
debug(F111,"ESCBUF PRINT 5",escbuf,escbufc);
}
escbufc = 0; /* Clear escseq buffer */
escbuf[0] = NUL;
}
#endif /* XPRINT */
#ifdef CK_APC
if (!src && apcactive) { /* If it was an APC string, */
debug(F101,"CONNECT APC terminated","",c);
apcbuf[apclength] = NUL; /* terminate it and then ... */
return(1);
}
#endif /* CK_APC */
} else { /* It's not a backslash so... */
inesc[src] = ES_STRING; /* back to string absorption. */
#ifdef CK_APC
if (apcactive) { /* In APC string */
if (apclength+1 < apcbuflen) { /* If enough room */
apcbuf[apclength++] = ESC; /* deposit the Esc */
apcbuf[apclength++] = c; /* and this character too. */
} else { /* Buffer overrun */
apcactive = 0;
apclength = 0;
apcbuf[0] = 0;
inesc[src] = ES_NORMAL;
}
}
#endif /* CK_APC */
}
} /* switch() */
debug(F111,"chkaes exit inesc",ckitoa(src),inesc[src]);
return(0);
}
#endif /* NOESCSEQ */
VOID
#ifdef CK_ANSIC
LOGCHAR(char c)
#else
LOGCHAR(c) char c;
#endif /* CK_ANSIC */
/* LOGCHAR */ { /* Log character c to session log */
/* but skip over escape sequences if session log is text */
if (escseq) {
if ((sessft == XYFT_T) && (debses == 0) &&
(inesc[0] != ES_NORMAL || oldesc[0] != ES_NORMAL))
return;
}
logchar(c);
}
/* C K C P U T C -- C-Kermit CONNECT Put Character to Screen */
/*
Output is buffered to avoid slow screen writes on fast connections.
*/
static int
ckcputf() { /* Dump the console output buffer */
int x = 0;
if (obc > 0) /* If we have any characters, */
x = conxo(obc,obuf); /* dump them, */
obp = obuf; /* reset the pointer */
obc = 0; /* and the counter. */
return(x); /* Return conxo's return code */
}
/*
NOTE: This is probably the right place for character-set translation,
rather than down below in the mainline code. ckcputc() would act like
xpnbyte() in ckcfns.c, and ckcgetc() would act like xgnbyte(). This
would shield the rest of the code from all the complexities of many-to-one
and one-to-many conversions, and would allow handling of Kanji and other
CJK sets along with UTF-8 and the rest.
*/
int
#ifdef CK_ANSIC
ckcputc( int c )
#else
ckcputc(c) int c;
#endif /* CK_ANSIC */
{
int x;
*obp++ = c & 0xff; /* Deposit the character */
obc++; /* Count it */
if (ibc == 0 || /* If input buffer about empty */
obc == OBUFL) { /* or output buffer full */
debug(F101,"CONNECT CKCPUTC obc","",obc);
x = conxo(obc,obuf); /* dump the buffer, */
obp = obuf; /* reset the pointer */
obc = 0; /* and the counter. */
return(x); /* Return conxo's return code */
} else return(0);
}
/* C K C G E T C -- C-Kermit CONNECT Get Character */
/*
Buffered read from communication device.
Returns the next character, refilling the buffer if necessary.
On error, returns ttinc's return code (see ttinc() description).
Dummy argument for compatible calling conventions with ttinc()
so a pointer to this function can be passed to tn_doop().
*/
int
#ifdef CK_ANSIC
ckcgetc( int dummy )
#else
ckcgetc(dummy) int dummy;
#endif /* CK_ANSIC */
{
int c, n;
#ifdef CK_SSL
extern int ssl_active_flag, tls_active_flag;
#endif /* CK_SSL */
#ifdef CK_ENCRYPTION
/* No buffering for possibly encrypted connections */
if (network && IS_TELNET() && TELOPT_ME(TELOPT_AUTHENTICATION))
return(ttinc(0));
#endif /* CK_ENCRYPTION */
#ifdef CK_SSL
if (ssl_active_flag || tls_active_flag)
return(ttinc(0));
#endif /* CK_SSL */
if (ibc < 1) { /* Need to refill buffer? */
ibc = 0; /* Yes, reset count */
ibp = ibuf; /* and buffer pointer */
c = ttinc(0); /* Read one character, blocking */
if (c < 0) { /* If error, return error code */
return(c);
} else { /* Otherwise, got one character */
*ibp++ = c; /* Advance buffer pointer */
ibc++; /* and count. */
}
if ((n = ttchk()) > 0) { /* Any more waiting? */
if (n > (IBUFL - ibc)) /* Get them all at once. */
n = IBUFL - ibc; /* Don't overflow buffer */
if ((n = ttxin(n,(CHAR *)ibp)) > 0) {
ibc += n; /* Advance counter */
}
} else if (n < 0) { /* Error? */
return(n); /* Return the error code */
}
ibp = ibuf; /* Point to beginning of buffer */
}
c = *ibp++ & 0xff; /* Get next character from buffer */
ibc--; /* Reduce buffer count */
/* debug(F000,"CKCGETC","",c); */
return(c); /* Return the character */
}
/*
Keyboard handling, buffered for speed, which is needed when C-Kermit is
in CONNECT mode between two other computers that are transferring data.
*/
static char *kbp; /* Keyboard input buffer pointer */
static int kbc; /* Keyboard input buffer count */
#ifdef CK_SMALL /* Keyboard input buffer length */
#define KBUFL 32 /* Small for PDP-11 UNIX */
#else
#define KBUFL 257 /* Regular kernel size for others */
#endif /* CK_SMALL */
#ifdef DYNAMIC
static char *kbuf = NULL;
#else
static char kbuf[KBUFL];
#endif /* DYNAMIC */
/* Macro for reading keystrokes. */
#define CONGKS() (((--kbc)>=0) ? ((int)(*kbp++) & 0377) : kbget())
/*
Note that we call read() directly here, normally a no-no, but in this case
we know it's UNIX and we're only doing what coninc(0) would have done,
except we're reading a block of characters rather than just one. There is,
at present, no conxin() analog to ttxin() for chunk reads, and instituting
one would only add function-call overhead as it would only be a wrapper for
a read() call anyway.
Another note: We stick in this read() till the user types something.
But we know they already did, since select() said so. Therefore something
would need to be mighty wrong before we get stuck here.
*/
static int /* Keyboard buffer filler */
kbget() {
#ifdef EINTR
int tries = 10; /* If read() is interrupted, */
int ok = 0;
while (tries-- > 0) { /* try a few times... */
#endif /* EINTR */
kbc = conchk(); /* How many chars waiting? */
debug(F101,"kbget kbc","",kbc);
if (kbc < 1)
kbc = 1; /* If none or dunno, wait for one. */
else if (kbc > KBUFL) /* If too many, */
kbc = KBUFL; /* only read this many. */
if ((kbc = read(0, kbuf, kbc)) < 1) { /* Now read it/them. */
debug(F101,"CONNECT kbget errno","",errno); /* Got an error. */
#ifdef EINTR
if (errno == EINTR) /* Interrupted system call. */
continue; /* Try again, up to limit. */
else /* Something else. */
#endif /* EINTR */
return(-1); /* Pass along read() error. */
}
#ifdef EINTR
else { ok = 1; break; }
}
if (!ok) return(-1);
#endif /* EINTR */
kbp = kbuf; /* Adjust buffer pointer, */
kbc--; /* count, */
return((int)(*kbp++) & 0377); /* and return first character. */
}
#ifdef BEBOX
/*
* CreateSocketPair --
*
* This procedure creates a connected socket pair
*
* Results:
* 0 if OK, the error if not OK.
*
* Side effects:
* None
*/
int
socketpair(int *pair) {
int servsock;
int val;
struct sockaddr_in serv_addr, cli_addr;
extern char myipaddr[];
debug(F110,"socketpair",myipaddr,0);
if (myipaddr[0] == 0)
getlocalipaddr();
servsock = socket(AF_INET, SOCK_STREAM, 0);
if (servsock == 0) {
return h_errno;
}
debug(F111,"socketpair","socket",servsock);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(0);
val = sizeof(serv_addr);
if (bind(servsock, (struct sockaddr *) &serv_addr, val) < 0) {
closesocket(servsock);
return h_errno;
}
debug(F111,"socketpair","bind",0);
listen(servsock, 1);
debug(F111,"socketpair","listen",0);
if (getsockname(servsock, (struct sockaddr *) &serv_addr, &val) < 0) {
closesocket(servsock);
return h_errno;
}
debug(F111,"socketpair","getsockname",0);
pair[0] = socket(AF_INET, SOCK_STREAM, 0);
if (pair[0] == 0) {
closesocket(servsock);
return h_errno;
}
debug(F111,"socketpair","socket",pair[0]);
memset(&cli_addr, 0, sizeof(cli_addr));
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = inet_addr(myipaddr[0]?myipaddr:"127.0.0.1");
cli_addr.sin_port = serv_addr.sin_port;
if (connect(pair[0],(struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
closesocket(pair[0]);
closesocket(servsock);
return h_errno;
}
debug(F111,"socketpair","connect",0);
pair[1] = accept(servsock, (struct sockaddr *) &serv_addr, &val);
if (pair[1] == 0) {
closesocket(pair[0]);
closesocket(servsock);
return h_errno;
}
debug(F111,"socketpair","accept",pair[1]);
closesocket(servsock);
debug(F111,"socketpair","closesocket",0);
return 0;
}
long
kbdread(void * param) {
int sock = (int) param;
char ch;
int rc = 0;
debug(F111,"kbdread","sock",sock);
while (rc >= 0) {
rc = read(fileno(stdin), &ch, 1); /* Read a character. */
if (rc > 0) {
rc = send(sock,&ch,1,0);
/* debug(F000,"kbdread","send()",ch); */
printf("\r\ngot: %c rc = %d\r\n",ch,rc);