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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
/* Copyright (c) 1993
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Copyright (c) 1987 Oliver Laumann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
****************************************************************
*/
/*
* NOTICE: tty.c is automatically generated from tty.sh
* Do not change anything here. If you then change tty.sh.
*/
#include "rcs.h"
RCS_ID("$Id: tty.sh,v 1.13 1994/05/31 12:33:17 mlschroe Exp $ FAU")
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#ifndef sgi
# include <sys/file.h>
#endif
#if !defined(sun) || defined(SUNOS3)
# include <sys/ioctl.h> /* collosions with termios.h */
#else
# ifndef TIOCEXCL
# include <sys/ttold.h> /* needed for TIOCEXCL */
# endif
#endif
#ifdef ISC
# include <sys/tty.h>
# include <sys/sioctl.h>
# include <sys/pty.h>
#endif
#include "config.h"
#include "screen.h"
#include "extern.h"
extern struct display *display, *displays;
extern int iflag;
/* Frank Schulz (fschulz@pyramid.com):
* I have no idea why VSTART is not defined and my fix is probably not
* the cleanest, but it works.
*/
#if !defined(VSTART) && defined(_VSTART)
#define VSTART _VSTART
#endif
#if !defined(VSTOP) && defined(_VSTOP)
#define VSTOP _VSTOP
#endif
static sigret_t
SigAlrmDummy SIGDEFARG
{
debug("SigAlrmDummy()\n");
SIGRETURN;
}
/*
* Carefully open a charcter device. Not used to open ttys.
*/
int
OpenTTY(line)
char *line;
{
int f;
sigret_t (*sigalrm)__P(SIGPROTOARG);
sigalrm = signal(SIGALRM, SigAlrmDummy);
alarm(2);
/* this open only succeeds, if real uid is allowed */
if ((f = secopen(line, O_RDWR | O_NONBLOCK, 0)) == -1)
{
Msg(errno, "Cannot open line '%s' for R/W", line);
alarm(0);
signal(SIGALRM, sigalrm);
return -1;
}
#ifdef I_POP
debug("OpenTTY I_POP\n");
while (ioctl(f, I_POP, (char *)0) >= 0)
;
#endif
/*
* We come here exclusively. This is to stop all kermit and cu type things
* accessing the same tty line.
* Perhaps we should better create a lock in some /usr/spool/locks directory?
*/
#ifdef TIOCEXCL
errno = 0;
ioctl(f, TIOCEXCL, (char *) 0);
debug3("%d %d %d\n", getuid(), geteuid(), getpid());
debug2("%s TIOCEXCL errno %d\n", line, errno);
#endif /* TIOCEXCL */
/*
* We create a sane tty mode. We do not copy things from the display tty
*/
#if WE_REALLY_WANT_TO_COPY_THE_TTY_MODE
if (display)
{
debug1("OpenTTY: using mode of display for %s\n", line);
SetTTY(f, &D_NewMode);
#ifdef DEBUG
DebugTTY(&D_NewMode);
#endif
}
else
#endif
{
struct mode Mode;
InitTTY(&Mode, TTY_FLAG_PLAIN);
#ifdef DEBUG
DebugTTY(&Mode);
#endif
SetTTY(f, &Mode);
}
brktty(f);
alarm(0);
signal(SIGALRM, sigalrm);
debug2("'%s' CONNECT fd=%d.\n", line, f);
return f;
}
/*
* Tty mode handling
*/
#if defined(TERMIO) || defined(POSIX)
int intrc, origintrc = VDISABLE; /* display? */
#else
int intrc, origintrc = -1; /* display? */
#endif
static startc, stopc; /* display? */
void
InitTTY(m, ttyflag)
struct mode *m;
int ttyflag;
{
bzero((char *)m, sizeof(*m));
#ifdef POSIX
/* struct termios tio
* defaults, as seen on SunOS 4.1.3
*/
debug1("InitTTY: POSIX: termios defaults a la SunOS 4.1.3 (%d)\n", ttyflag);
#if defined(BRKINT)
m->tio.c_iflag |= BRKINT;
#endif /* BRKINT */
#if defined(IGNPAR)
m->tio.c_iflag |= IGNPAR;
#endif /* IGNPAR */
#if defined(ISTRIP)
m->tio.c_iflag |= ISTRIP;
#endif /* ISTRIP */
#if defined(IXON)
m->tio.c_iflag |= IXON;
#endif /* IXON */
#if defined(IMAXBEL)
m->tio.c_iflag |= IMAXBEL;
#endif /* IMAXBEL */
if (!ttyflag) /* may not even be good for ptys.. */
{
#if defined(ICRNL)
m->tio.c_iflag |= ICRNL;
#endif /* ICRNL */
#if defined(ONLCR)
m->tio.c_oflag |= ONLCR;
#endif /* ONLCR */
#if defined(TAB3)
m->tio.c_oflag |= TAB3;
#endif /* TAB3 */
#if defined(PARENB)
m->tio.c_cflag |= PARENB;
#endif /* PARENB */
}
#if defined(OPOST)
m->tio.c_oflag |= OPOST;
#endif /* OPOST */
#if defined(B9600)
m->tio.c_cflag |= B9600;
#endif /* B9600 */
#if defined(CS8)
m->tio.c_cflag |= CS8;
#endif /* CS8 */
#if defined(CREAD)
m->tio.c_cflag |= CREAD;
#endif /* CREAD */
#if defined(IBSHIFT) && defined(B9600)
m->tio.c_cflag |= B9600 << IBSHIFT;
#endif /* IBSHIFT) && defined(B9600 */
/* IF{CLOCAL} m->tio.c_cflag |= CLOCAL; */
#if defined(ECHOCTL)
m->tio.c_lflag |= ECHOCTL;
#endif /* ECHOCTL */
#if defined(ECHOKE)
m->tio.c_lflag |= ECHOKE;
#endif /* ECHOKE */
if (!ttyflag)
{
#if defined(ISIG)
m->tio.c_lflag |= ISIG;
#endif /* ISIG */
#if defined(ICANON)
m->tio.c_lflag |= ICANON;
#endif /* ICANON */
#if defined(ECHO)
m->tio.c_lflag |= ECHO;
#endif /* ECHO */
}
#if defined(ECHOE)
m->tio.c_lflag |= ECHOE;
#endif /* ECHOE */
#if defined(ECHOK)
m->tio.c_lflag |= ECHOK;
#endif /* ECHOK */
#if defined(IEXTEN)
m->tio.c_lflag |= IEXTEN;
#endif /* IEXTEN */
#if defined(VINTR) && VINTR < MAXCC
m->tio.c_cc[VINTR] = Ctrl('C');
#endif /* VINTR */
#if defined(VQUIT) && VQUIT < MAXCC
m->tio.c_cc[VQUIT] = Ctrl('\\');
#endif /* VQUIT */
#if defined(VERASE) && VERASE < MAXCC
m->tio.c_cc[VERASE] = 0x7f; /* DEL */
#endif /* VERASE */
#if defined(VKILL) && VKILL < MAXCC
m->tio.c_cc[VKILL] = Ctrl('H');
#endif /* VKILL */
#if defined(VEOF) && VEOF < MAXCC
m->tio.c_cc[VEOF] = Ctrl('D');
#endif /* VEOF */
#if defined(VEOL) && VEOL < MAXCC
m->tio.c_cc[VEOL] = 0000;
#endif /* VEOL */
#if defined(VEOL2) && VEOL2 < MAXCC
m->tio.c_cc[VEOL2] = 0000;
#endif /* VEOL2 */
#if defined(VSWTCH) && VSWTCH < MAXCC
m->tio.c_cc[VSWTCH] = 0000;
#endif /* VSWTCH */
#if defined(VSTART) && VSTART < MAXCC
m->tio.c_cc[VSTART] = Ctrl('Q');
#endif /* VSTART */
#if defined(VSTOP) && VSTOP < MAXCC
m->tio.c_cc[VSTOP] = Ctrl('S');
#endif /* VSTOP */
#if defined(VSUSP) && VSUSP < MAXCC
m->tio.c_cc[VSUSP] = Ctrl('Z');
#endif /* VSUSP */
#if defined(VDSUSP) && VDSUSP < MAXCC
m->tio.c_cc[VDSUSP] = Ctrl('Y');
#endif /* VDSUSP */
#if defined(VREPRINT) && VREPRINT < MAXCC
m->tio.c_cc[VREPRINT] = Ctrl('R');
#endif /* VREPRINT */
#if defined(VDISCARD) && VDISCARD < MAXCC
m->tio.c_cc[VDISCARD] = Ctrl('O');
#endif /* VDISCARD */
#if defined(VWERASE) && VWERASE < MAXCC
m->tio.c_cc[VWERASE] = Ctrl('W');
#endif /* VWERASE */
#if defined(VLNEXT) && VLNEXT < MAXCC
m->tio.c_cc[VLNEXT] = Ctrl('V');
#endif /* VLNEXT */
#if defined(VSTATUS) && VSTATUS < MAXCC
m->tio.c_cc[VSTATUS] = Ctrl('T');
#endif /* VSTATUS */
if (ttyflag)
{
m->tio.c_cc[VMIN] = 1;
m->tio.c_cc[VTIME] = 0;
}
# ifdef hpux
m->m_ltchars.t_suspc = Ctrl('Z');
m->m_ltchars.t_dsuspc = Ctrl('Y');
m->m_ltchars.t_rprntc = Ctrl('R');
m->m_ltchars.t_flushc = Ctrl('O');
m->m_ltchars.t_werasc = Ctrl('W');
m->m_ltchars.t_lnextc = Ctrl('V');
# endif /* hpux */
#else /* POSIX */
# ifdef TERMIO
debug1("InitTTY: nonPOSIX, struct termio a la Motorola SYSV68 (%d)\n", ttyflag);
/* struct termio tio
* defaults, as seen on Mototola SYSV68:
* input: 7bit, CR->NL, ^S/^Q flow control
* output: POSTprocessing: NL->NL-CR, Tabs to spaces
* control: 9600baud, 8bit CSIZE, enable input
* local: enable signals, erase/kill processing, echo on.
*/
#if defined(ISTRIP)
m->tio.c_iflag |= ISTRIP;
#endif /* ISTRIP */
#if defined(IXON)
m->tio.c_iflag |= IXON;
#endif /* IXON */
#if defined(OPOST)
m->tio.c_oflag |= OPOST;
#endif /* OPOST */
if (!ttyflag) /* may not even be good for ptys.. */
{
#if defined(ICRNL)
m->tio.c_iflag |= ICRNL;
#endif /* ICRNL */
#if defined(ONLCR)
m->tio.c_oflag |= ONLCR;
#endif /* ONLCR */
#if defined(TAB3)
m->tio.c_oflag |= TAB3;
#endif /* TAB3 */
}
#if defined(B9600)
m->tio.c_cflag = B9600;
#endif /* B9600 */
#if defined(CS8)
m->tio.c_cflag |= CS8;
#endif /* CS8 */
#if defined(CREAD)
m->tio.c_cflag |= CREAD;
#endif /* CREAD */
if (!ttyflag)
{
#if defined(ISIG)
m->tio.c_lflag |= ISIG;
#endif /* ISIG */
#if defined(ICANON)
m->tio.c_lflag |= ICANON;
#endif /* ICANON */
#if defined(ECHO)
m->tio.c_lflag |= ECHO;
#endif /* ECHO */
}
#if defined(ECHOE)
m->tio.c_lflag |= ECHOE;
#endif /* ECHOE */
#if defined(ECHOK)
m->tio.c_lflag |= ECHOK;
#endif /* ECHOK */
#if defined(VINTR) && VINTR < MAXCC
m->tio.c_cc[VINTR] = Ctrl('C');
#endif /* VINTR */
#if defined(VQUIT) && VQUIT < MAXCC
m->tio.c_cc[VQUIT] = Ctrl('\\');
#endif /* VQUIT */
#if defined(VERASE) && VERASE < MAXCC
m->tio.c_cc[VERASE] = 0177; /* DEL */
#endif /* VERASE */
#if defined(VKILL) && VKILL < MAXCC
m->tio.c_cc[VKILL] = Ctrl('H');
#endif /* VKILL */
#if defined(VEOF) && VEOF < MAXCC
m->tio.c_cc[VEOF] = Ctrl('D');
#endif /* VEOF */
#if defined(VEOL) && VEOL < MAXCC
m->tio.c_cc[VEOL] = 0377;
#endif /* VEOL */
#if defined(VEOL2) && VEOL2 < MAXCC
m->tio.c_cc[VEOL2] = 0377;
#endif /* VEOL2 */
#if defined(VSWTCH) && VSWTCH < MAXCC
m->tio.c_cc[VSWTCH] = 0000;
#endif /* VSWTCH */
if (ttyflag)
{
m->tio.c_cc[VMIN] = 1;
m->tio.c_cc[VTIME] = 0;
}
# else /* TERMIO */
debug1("InitTTY: BSD: defaults a la SunOS 4.1.3 (%d)\n", ttyflag);
m->m_ttyb.sg_ispeed = B9600;
m->m_ttyb.sg_ospeed = B9600;
m->m_ttyb.sg_erase = 0177; /*DEL */
m->m_ttyb.sg_kill = Ctrl('H');
if (!ttyflag)
m->m_ttyb.sg_flags = CRMOD | ECHO
#if defined(ANYP)
| ANYP
#endif /* ANYP */
;
else
m->m_ttyb.sg_flags = CBREAK
#if defined(ANYP)
| ANYP
#endif /* ANYP */
;
m->m_tchars.t_intrc = Ctrl('C');
m->m_tchars.t_quitc = Ctrl('\\');
m->m_tchars.t_startc = Ctrl('Q');
m->m_tchars.t_stopc = Ctrl('S');
m->m_tchars.t_eofc = Ctrl('D');
m->m_tchars.t_brkc = -1;
m->m_ltchars.t_suspc = Ctrl('Z');
m->m_ltchars.t_dsuspc = Ctrl('Y');
m->m_ltchars.t_rprntc = Ctrl('R');
m->m_ltchars.t_flushc = Ctrl('O');
m->m_ltchars.t_werasc = Ctrl('W');
m->m_ltchars.t_lnextc = Ctrl('V');
#if defined(NTTYDISC)
m->m_ldisc = NTTYDISC;
#endif /* NTTYDISC */
m->m_lmode = 0
#if defined(LDECCTQ)
| LDECCTQ
#endif /* LDECCTQ */
#if defined(LCTLECH)
| LCTLECH
#endif /* LCTLECH */
#if defined(LPASS8)
| LPASS8
#endif /* LPASS8 */
#if defined(LCRTKIL)
| LCRTKIL
#endif /* LCRTKIL */
#if defined(LCRTERA)
| LCRTERA
#endif /* LCRTERA */
#if defined(LCRTBS)
| LCRTBS
#endif /* LCRTBS */
;
# endif /* TERMIO */
#endif /* POSIX */
#if defined(KANJI) && defined(TIOCKSET) && defined(KM_ASCII) && defined(KM_SYSSJIS)
m->m_jtchars.t_ascii = 'J';
m->m_jtchars.t_kanji = 'B';
m->m_knjmode = KM_ASCII | KM_SYSSJIS;
#endif
}
void
SetTTY(fd, mp)
int fd;
struct mode *mp;
{
errno = 0;
#ifdef POSIX
tcsetattr(fd, TCSADRAIN, &mp->tio);
# ifdef hpux
ioctl(fd, TIOCSLTC, (char *)&mp->m_ltchars);
# endif
#else
# ifdef TERMIO
ioctl(fd, TCSETAW, (char *)&mp->tio);
# ifdef CYTERMIO
if (mp->tio.c_line == 3)
{
ioctl(fd, LDSETMAPKEY, (char *)&mp->m_mapkey);
ioctl(fd, LDSETMAPSCREEN, (char *)&mp->m_mapscreen);
ioctl(fd, LDSETBACKSPACE, (char *)&mp->m_backspace);
}
# endif
# else
/* ioctl(fd, TIOCSETP, (char *)&mp->m_ttyb); */
ioctl(fd, TIOCSETC, (char *)&mp->m_tchars);
ioctl(fd, TIOCLSET, (char *)&mp->m_lmode);
ioctl(fd, TIOCSETD, (char *)&mp->m_ldisc);
ioctl(fd, TIOCSETP, (char *)&mp->m_ttyb);
ioctl(fd, TIOCSLTC, (char *)&mp->m_ltchars); /* moved here for apollo. jw */
# endif
#endif
#if defined(KANJI) && defined(TIOCKSET) && defined(KM_ASCII) && defined(KM_SYSSJIS)
ioctl(fd, TIOCKSETC, &mp->m_jtchars);
ioctl(fd, TIOCKSET, &mp->m_knjmode);
#endif
if (errno)
Msg(errno, "SetTTY (fd %d): ioctl failed", fd);
}
void
GetTTY(fd, mp)
int fd;
struct mode *mp;
{
errno = 0;
#ifdef POSIX
tcgetattr(fd, &mp->tio);
# ifdef hpux
ioctl(fd, TIOCGLTC, (char *)&mp->m_ltchars);
# endif
#else
# ifdef TERMIO
ioctl(fd, TCGETA, (char *)&mp->tio);
# ifdef CYTERMIO
if (mp->tio.c_line == 3)
{
ioctl(fd, LDGETMAPKEY, (char *)&mp->m_mapkey);
ioctl(fd, LDGETMAPSCREEN, (char *)&mp->m_mapscreen);
ioctl(fd, LDGETBACKSPACE, (char *)&mp->m_backspace);
}
else
{
mp->m_mapkey = NOMAPKEY;
mp->m_mapscreen = NOMAPSCREEN;
mp->m_backspace = '\b';
}
# endif
# else
ioctl(fd, TIOCGETP, (char *)&mp->m_ttyb);
ioctl(fd, TIOCGETC, (char *)&mp->m_tchars);
ioctl(fd, TIOCGLTC, (char *)&mp->m_ltchars);
ioctl(fd, TIOCLGET, (char *)&mp->m_lmode);
ioctl(fd, TIOCGETD, (char *)&mp->m_ldisc);
# endif
#endif
#if defined(KANJI) && defined(TIOCKSET) && defined(KM_ASCII) && defined(KM_SYSSJIS)
ioctl(fd, TIOCKGETC, &mp->m_jtchars);
ioctl(fd, TIOCKGET, &mp->m_knjmode);
#endif
if (errno)
Msg(errno, "GetTTY (fd %d): ioctl failed", fd);
}
void
SetMode(op, np)
struct mode *op, *np;
{
*np = *op;
#if defined(TERMIO) || defined(POSIX)
# ifdef CYTERMIO
np->m_mapkey = NOMAPKEY;
np->m_mapscreen = NOMAPSCREEN;
np->tio.c_line = 0;
# endif
#if defined(ICRNL)
np->tio.c_iflag &= ~ICRNL;
#endif /* ICRNL */
#if defined(ONLCR)
np->tio.c_oflag &= ~ONLCR;
#endif /* ONLCR */
np->tio.c_lflag &= ~(ICANON | ECHO);
/*
* From Andrew Myers (andru@tonic.lcs.mit.edu)
*/
#if defined(IEXTEN)
np->tio.c_lflag &= ~IEXTEN;
#endif /* IEXTEN */
/*
* Unfortunately, the master process never will get SIGINT if the real
* terminal is different from the one on which it was originaly started
* (process group membership has not been restored or the new tty could not
* be made controlling again). In my solution, it is the attacher who
* receives SIGINT (because it is always correctly associated with the real
* tty) and forwards it to the master [kill(MasterPid, SIGINT)].
* Marc Boucher (marc@CAM.ORG)
*/
if (iflag)
np->tio.c_lflag |= ISIG;
else
np->tio.c_lflag &= ~ISIG;
/*
* careful, careful catche monkey..
* never set VMIN and VTIME to zero, if you want blocking io.
*/
np->tio.c_cc[VMIN] = 1;
np->tio.c_cc[VTIME] = 0;
#if defined(VSTART) && VSTART < MAXCC
startc = op->tio.c_cc[VSTART];
#endif /* VSTART */
#if defined(VSTOP) && VSTOP < MAXCC
stopc = op->tio.c_cc[VSTOP];
#endif /* VSTOP */
if (iflag)
origintrc = intrc = op->tio.c_cc[VINTR];
else
{
origintrc = op->tio.c_cc[VINTR];
intrc = np->tio.c_cc[VINTR] = VDISABLE;
}
np->tio.c_cc[VQUIT] = VDISABLE;
if (D_flow == 0)
{
np->tio.c_cc[VINTR] = VDISABLE;
#if defined(VSTART) && VSTART < MAXCC
np->tio.c_cc[VSTART] = VDISABLE;
#endif /* VSTART */
#if defined(VSTOP) && VSTOP < MAXCC
np->tio.c_cc[VSTOP] = VDISABLE;
#endif /* VSTOP */
np->tio.c_iflag &= ~IXON;
}
#if defined(VDISCARD) && VDISCARD < MAXCC
np->tio.c_cc[VDISCARD] = VDISABLE;
#endif /* VDISCARD */
#if defined(VLNEXT) && VLNEXT < MAXCC
np->tio.c_cc[VLNEXT] = VDISABLE;
#endif /* VLNEXT */
#if defined(VSTATUS) && VSTATUS < MAXCC
np->tio.c_cc[VSTATUS] = VDISABLE;
#endif /* VSTATUS */
#if defined(VSUSP) && VSUSP < MAXCC
np->tio.c_cc[VSUSP] = VDISABLE;
#endif /* VSUSP */
#if defined(VERASE) && VERASE < MAXCC
np->tio.c_cc[VERASE] = VDISABLE;
#endif /* VERASE */
#if defined(VKILL) && VKILL < MAXCC
np->tio.c_cc[VKILL] = VDISABLE;
#endif /* VKILL */
# ifdef hpux
np->m_ltchars.t_suspc = VDISABLE;
np->m_ltchars.t_dsuspc = VDISABLE;
np->m_ltchars.t_rprntc = VDISABLE;
np->m_ltchars.t_flushc = VDISABLE;
np->m_ltchars.t_werasc = VDISABLE;
np->m_ltchars.t_lnextc = VDISABLE;
# else /* hpux */
#if defined(VDSUSP) && VDSUSP < MAXCC
np->tio.c_cc[VDSUSP] = VDISABLE;
#endif /* VDSUSP */
#if defined(VREPRINT) && VREPRINT < MAXCC
np->tio.c_cc[VREPRINT] = VDISABLE;
#endif /* VREPRINT */
#if defined(VWERASE) && VWERASE < MAXCC
np->tio.c_cc[VWERASE] = VDISABLE;
#endif /* VWERASE */
# endif /* hpux */
#else /* TERMIO || POSIX */
startc = op->m_tchars.t_startc;
stopc = op->m_tchars.t_stopc;
if (iflag)
origintrc = intrc = op->m_tchars.t_intrc;
else
{
origintrc = op->m_tchars.t_intrc;
intrc = np->m_tchars.t_intrc = -1;
}
np->m_ttyb.sg_flags &= ~(CRMOD | ECHO);
np->m_ttyb.sg_flags |= CBREAK;
# if defined(CYRILL) && defined(CSTYLE) && defined(CS_8BITS)
np->m_ttyb.sg_flags &= ~CSTYLE;
np->m_ttyb.sg_flags |= CS_8BITS;
# endif
np->m_tchars.t_quitc = -1;
if (D_flow == 0)
{
np->m_tchars.t_intrc = -1;
np->m_tchars.t_startc = -1;
np->m_tchars.t_stopc = -1;
}
np->m_ltchars.t_suspc = -1;
np->m_ltchars.t_dsuspc = -1;
np->m_ltchars.t_flushc = -1;
np->m_ltchars.t_lnextc = -1;
#endif /* defined(TERMIO) || defined(POSIX) */
}
void
SetFlow(on)
int on;
{
ASSERT(display);
if (D_flow == on)
return;
#if defined(TERMIO) || defined(POSIX)
if (on)
{
D_NewMode.tio.c_cc[VINTR] = intrc;
#if defined(VSTART) && VSTART < MAXCC
D_NewMode.tio.c_cc[VSTART] = startc;
#endif /* VSTART */
#if defined(VSTOP) && VSTOP < MAXCC
D_NewMode.tio.c_cc[VSTOP] = stopc;
#endif /* VSTOP */
D_NewMode.tio.c_iflag |= IXON;
}
else
{
D_NewMode.tio.c_cc[VINTR] = VDISABLE;
#if defined(VSTART) && VSTART < MAXCC
D_NewMode.tio.c_cc[VSTART] = VDISABLE;
#endif /* VSTART */
#if defined(VSTOP) && VSTOP < MAXCC
D_NewMode.tio.c_cc[VSTOP] = VDISABLE;
#endif /* VSTOP */
D_NewMode.tio.c_iflag &= ~IXON;
}
# ifdef POSIX
if (tcsetattr(D_userfd, TCSANOW, &D_NewMode.tio))
# else
if (ioctl(D_userfd, TCSETAW, (char *)&D_NewMode.tio) != 0)
# endif
debug1("SetFlow: ioctl errno %d\n", errno);
#else /* POSIX || TERMIO */
if (on)
{
D_NewMode.m_tchars.t_intrc = intrc;
D_NewMode.m_tchars.t_startc = startc;
D_NewMode.m_tchars.t_stopc = stopc;
}
else
{
D_NewMode.m_tchars.t_intrc = -1;
D_NewMode.m_tchars.t_startc = -1;
D_NewMode.m_tchars.t_stopc = -1;
}
if (ioctl(D_userfd, TIOCSETC, (char *)&D_NewMode.m_tchars) != 0)
debug1("SetFlow: ioctl errno %d\n", errno);
#endif /* POSIX || TERMIO */
D_flow = on;
}
/*
* Job control handling
*
* Somehow the ultrix session handling is broken, so use
* the bsdish variant.
*/
/*ARGSUSED*/
void
brktty(fd)
int fd;
{
#if defined(POSIX) && !defined(ultrix)
setsid(); /* will break terminal affiliation */
# if defined(BSD) && defined(TIOCSCTTY)
ioctl(fd, TIOCSCTTY, (char *)0);
# endif /* BSD && TIOCSCTTY */
#else /* POSIX */
# ifdef SYSV
setpgrp(); /* will break terminal affiliation */
# else /* SYSV */
# ifdef BSDJOBS
int devtty;
if ((devtty = open("/dev/tty", O_RDWR | O_NONBLOCK)) >= 0)
{
if (ioctl(devtty, TIOCNOTTY, (char *)0))
debug2("brktty: ioctl(devtty=%d, TIOCNOTTY, 0) = %d\n", devtty, errno);
close(devtty);
}
# endif /* BSDJOBS */
# endif /* SYSV */
#endif /* POSIX */
}
int
fgtty(fd)
int fd;
{
#ifdef BSDJOBS
int mypid;
mypid = getpid();
/* The next lines should be obsolete. Can anybody check if they
* are really needed on the BSD platforms?
*/
# if defined(__osf__) || (BSD >= 199103) || defined(ISC)
setsid(); /* should be already done */
# ifdef TIOCSCTTY
ioctl(fd, TIOCSCTTY, (char *)0);
# endif
# endif
# ifdef POSIX
if (tcsetpgrp(fd, mypid))
{
debug1("fgtty: tcsetpgrp: %d\n", errno);
return -1;
}
# else /* POSIX */
if (ioctl(fd, TIOCSPGRP, (char *)&mypid) != 0)
debug1("fgtty: TIOSETPGRP: %d\n", errno);
# ifndef SYSV /* Already done in brktty():setpgrp() */
if (setpgrp(fd, mypid))
debug1("fgtty: setpgrp: %d\n", errno);
# endif
# endif /* POSIX */
#endif /* BSDJOBS */
return 0;
}
/*
* Send a break for n * 0.25 seconds. Tty must be PLAIN.
*/
void SendBreak(wp, n, closeopen)
struct win *wp;
int n, closeopen;
{
if ((wp->w_t.flags & TTY_FLAG_PLAIN) == 0)
return;
debug3("break(%d, %d) fd %d\n", n, closeopen, wp->w_ptyfd);
#ifdef POSIX
(void) tcflush(wp->w_ptyfd, TCIOFLUSH);
#else
# ifdef TIOCFLUSH
(void) ioctl(wp->w_ptyfd, TIOCFLUSH, (char *)0);
# endif /* TIOCFLUSH */
#endif /* POSIX */
if (closeopen)
{
close(wp->w_ptyfd);
sleep((n + 3) / 4);
if ((wp->w_ptyfd = OpenTTY(wp->w_tty)) < 1)
{
Msg(0, "Ouch, cannot reopen line %s, please try harder", wp->w_tty);
return;
}
(void) fcntl(wp->w_ptyfd, F_SETFL, FNBLOCK);
}
else
{
#ifdef POSIX
debug("tcsendbreak\n");
if (tcsendbreak(wp->w_ptyfd, n) < 0)
{
Msg(errno, "cannot send BREAK");
return;
}
#else
if (!n)
n++;
# ifdef TCSBRK
debug("TCSBRK\n");
{
int i;
for (i = 0; i < n; i++)
if (ioctl(wp->w_ptyfd, TCSBRK, (char *)0) < 0)
{
Msg(errno, "Cannot send BREAK");
return;
}
}
# else /* TCSBRK */
# if defined(TIOCSBRK) && defined(TIOCCBRK)
debug("TIOCSBRK TIOCCBRK\n");
if (ioctl(wp->w_ptyfd, TIOCSBRK, (char *)0) < 0)
{
Msg(errno, "Can't send BREAK");
return;
}
sleep((n + 3) / 4);
if (ioctl(wp->w_ptyfd, TIOCCBRK, (char *)0) < 0)
{
Msg(errno, "BREAK stuck!!! -- HELP!");
return;
}
# else /* TIOCSBRK && TIOCCBRK */
Msg(0, "Break not simulated yet");
return;
# endif /* TIOCSBRK && TIOCCBRK */
# endif /* TCSBRK */
#endif /* POSIX */
debug(" broken\n");
}
}
/*
* Console grabbing
*/
/*ARGSUSED*/
int
TtyGrabConsole(fd, on, rc_name)
int fd, on;
char *rc_name;
{
#ifdef TIOCCONS
char *slave;
int sfd = -1, ret = 0;
struct display *d;
if (!on)
{
if ((fd = OpenPTY(&slave)) < 0)
{
Msg(errno, "%s: could not open detach pty master", rc_name);
return -1;
}
if ((sfd = open(slave, O_RDWR)) < 0)
{
Msg(errno, "%s: could not open detach pty slave", rc_name);
close(fd);
return -1;
}
}
else
{
if (displays == 0)
{
Msg(0, "I need a display");
return -1;
}
for (d = displays; d; d = d->d_next)
if (strcmp(d->d_usertty, "/dev/console") == 0)
break;
if (d)
{
Msg(0, "too dangerous - screen is running on /dev/console");
return -1;
}
}
if (UserContext() == 1)
UserReturn(ioctl(fd, TIOCCONS, (char *)&on));
ret = UserStatus();
if (ret)
Msg(errno, "%s: ioctl TIOCCONS failed", rc_name);
if (!on)
{
close(sfd);
close(fd);
}
return ret;
#else /* TIOCCONS */
Msg(0, "%s: no TIOCCONS on this machine", rc_name);
return -1;
#endif /* TIOCCONS */
}
/*
* Write out the mode struct in a readable form
*/
#ifdef DEBUG
void
DebugTTY(m)
struct mode *m;
{
int i;
#ifdef POSIX
debug("struct termios tio:\n");
debug1("c_iflag = %#x\n", (unsigned int)m->tio.c_iflag);
debug1("c_oflag = %#x\n", (unsigned int)m->tio.c_oflag);
debug1("c_cflag = %#x\n", (unsigned int)m->tio.c_cflag);
debug1("c_lflag = %#x\n", (unsigned int)m->tio.c_lflag);
for (i = 0; i < sizeof(m->tio.c_cc)/sizeof(*m->tio.c_cc); i++)
{
debug2("c_cc[%d] = %#x\n", i, m->tio.c_cc[i]);
}
# ifdef hpux
debug1("suspc = %#02x\n", m->m_ltchars.t_suspc);
debug1("dsuspc = %#02x\n", m->m_ltchars.t_dsuspc);
debug1("rprntc = %#02x\n", m->m_ltchars.t_rprntc);
debug1("flushc = %#02x\n", m->m_ltchars.t_flushc);
debug1("werasc = %#02x\n", m->m_ltchars.t_werasc);
debug1("lnextc = %#02x\n", m->m_ltchars.t_lnextc);
# endif /* hpux */
#else /* POSIX */
# ifdef TERMIO
debug("struct termio tio:\n");
debug1("c_iflag = %04o\n", m->tio.c_iflag);
debug1("c_oflag = %04o\n", m->tio.c_oflag);
debug1("c_cflag = %04o\n", m->tio.c_cflag);
debug1("c_lflag = %04o\n", m->tio.c_lflag);
for (i = 0; i < sizeof(m->tio.c_cc)/sizeof(*m->tio.c_cc); i++)
{
debug2("c_cc[%d] = %04o\n", i, m->tio.c_cc[i]);
}
# else /* TERMIO */
debug1("sg_ispeed = %d\n", m->m_ttyb.sg_ispeed);
debug1("sg_ospeed = %d\n", m->m_ttyb.sg_ospeed);
debug1("sg_erase = %#02x\n", m->m_ttyb.sg_erase);
debug1("sg_kill = %#02x\n", m->m_ttyb.sg_kill);
debug1("sg_flags = %#04x\n", (unsigned short)m->m_ttyb.sg_flags);
debug1("intrc = %#02x\n", m->m_tchars.t_intrc);
debug1("quitc = %#02x\n", m->m_tchars.t_quitc);
debug1("startc = %#02x\n", m->m_tchars.t_startc);
debug1("stopc = %#02x\n", m->m_tchars.t_stopc);
debug1("eofc = %#02x\n", m->m_tchars.t_eofc);
debug1("brkc = %#02x\n", m->m_tchars.t_brkc);
debug1("suspc = %#02x\n", m->m_ltchars.t_suspc);
debug1("dsuspc = %#02x\n", m->m_ltchars.t_dsuspc);
debug1("rprntc = %#02x\n", m->m_ltchars.t_rprntc);
debug1("flushc = %#02x\n", m->m_ltchars.t_flushc);
debug1("werasc = %#02x\n", m->m_ltchars.t_werasc);
debug1("lnextc = %#02x\n", m->m_ltchars.t_lnextc);
debug1("ldisc = %d\n", m->m_ldisc);
debug1("lmode = %#x\n", m->m_lmode);
# endif /* TERMIO */
#endif /* POSIX */
}
#endif /* DEBUG */
|