summaryrefslogtreecommitdiffstats
path: root/tty.sh
blob: d408720eadbb5c53b051e51c8c51479d0a2afdef (plain)
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
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
#! /bin/sh
# sh tty.sh tty.c
# This inserts all the needed #ifdefs for IF{} statements
# and generates tty.c

#
# Stupid cpp on A/UX barfs on ``#if defined(FOO) && FOO < 17'' when 
# FOO is undefined. Reported by Robert C. Tindall (rtindall@uidaho.edu)
#
rm -f $1
sed -e '1,26d' \
-e 's%^IF{\([^}]*\)}\(.*\)%#if defined(\1)\
\2\
#endif /* \1 */%' \
-e 's%^IFN{\([^}]*\)}\(.*\)%#if !defined(\1)\
\2\
#endif /* \1 */%' \
-e 's%^XIF{\([^}]*\)}\(.*\)%#if defined(\1)\
#if (\1 < MAXCC)\
\2\
#endif \
#endif /* \1 */%' \
 < $0 > $1
chmod -w $1
exit 0

/* Copyright (c) 2008, 2009
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 *      Micah Cowan (micah@cowan.name)
 *      Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
 *      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 3, 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, see
 * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */

/*
 * NOTICE: tty.c is automatically generated from tty.sh
 * Do not change anything here. If you then change tty.sh.
 */

#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/stat.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 __hpux
# include <sys/modem.h>
#endif
#include <limits.h>

#ifdef ISC
# include <sys/tty.h>
# include <sys/sioctl.h>
# include <sys/pty.h>
#endif

#include "config.h"
#ifdef HAVE_STROPTS_H
#include <sys/stropts.h>	/* for I_POP */
#endif

#include "screen.h"
#include "extern.h"

#if !defined(TIOCCONS) && defined(sun) && defined(SVR4)
# include <sys/strredir.h>
#endif

extern struct display *display, *displays;
extern int iflag;
#if (!defined(TIOCCONS) && defined(SRIOCSREDIR)) || defined(linux)
extern struct win *console_window;
static void consredir_readev_fn __P((struct event *, char *));
#endif

int separate_sids = 1;

static void DoSendBreak __P((int, int, int));
static sigret_t SigAlrmDummy __P(SIGPROTOARG);


/* 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

#ifndef O_NOCTTY
# define O_NOCTTY 0
#endif

#ifndef TTYVMIN
# define TTYVMIN 1
#endif
#ifndef TTYVTIME
#define TTYVTIME 0
#endif


static sigret_t
SigAlrmDummy SIGDEFARG
{
  debug("SigAlrmDummy()\n");
  SIGRETURN;
}

/*
 *  Carefully open a charcter device. Not used to open display ttys.
 *  The second parameter is parsed for a few stty style options.
 */

int OpenTTY(char *line, char *opt)
{
  int f;
  struct mode Mode;
  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 | O_NOCTTY, 0)) == -1) {
    if (errno == EINTR)
      Msg(0, "Cannot open line '%s' for R/W: open() blocked, aborted.", line);
    else
      Msg(errno, "Cannot open line '%s' for R/W", line);

    alarm(0);
    signal(SIGALRM, sigalrm);
    return -1;
  }

  if (!isatty(f)) {
    Msg(0, "'%s' is not a tty", line);
    alarm(0);
    signal(SIGALRM, sigalrm);
    close(f);
    return -1;
  }

#if defined(I_POP) && defined(POP_TTYMODULES)
  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;
 if (ioctl(f, TIOCEXCL, (char *) 0) < 0)
   Msg(errno, "%s: ioctl TIOCEXCL failed", line);
 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);
    Mode = D_NewMode;
  }
  else
#endif
    InitTTY(&Mode, W_TYPE_PLAIN);
  
  SttyMode(&Mode, opt);
#ifdef DEBUG
  DebugTTY(&Mode);
#endif
  SetTTY(f, &Mode);

#if defined(linux) && defined(TIOCMSET)
  {
    int mcs = 0;
    ioctl(f, TIOCMGET, &mcs);
    mcs |= TIOCM_RTS;
    ioctl(f, TIOCMSET, &mcs);
  }
#endif

  brktty(f);
  alarm(0);
  signal(SIGALRM, sigalrm);
  debug2("'%s' CONNECT fd=%d.\n", line, f);
  return f;
}


/*
 *  Tty mode handling
 */

void InitTTY(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 based on SunOS 4.1.3, but better (%d)\n", ttyflag);
IF{BRKINT}	m->tio.c_iflag |= BRKINT;
IF{IGNPAR}	m->tio.c_iflag |= IGNPAR;
/* IF{ISTRIP}	m->tio.c_iflag |= ISTRIP;  may be needed, let's try. jw. */
IF{IXON}	m->tio.c_iflag |= IXON;
/* IF{IMAXBEL}	m->tio.c_iflag |= IMAXBEL; sorry, this one is ridiculus. jw */

  if (!ttyflag)	/* may not even be good for ptys.. */
    {
IF{ICRNL}	m->tio.c_iflag |= ICRNL;
IF{ONLCR}	m->tio.c_oflag |= ONLCR; 
IF{TAB3}	m->tio.c_oflag |= TAB3; 
IF{OXTABS}      m->tio.c_oflag |= OXTABS;
/* IF{PARENB}	m->tio.c_cflag |= PARENB;	nah! jw. */
IF{OPOST}	m->tio.c_oflag |= OPOST;
    }


/*
 * Or-ing the speed into c_cflags is dangerous.
 * It breaks on bsdi, where c_ispeed and c_ospeed are extra longs.
 *
 * IF{B9600}    m->tio.c_cflag |= B9600;
 * IF{IBSHIFT) && defined(B9600}        m->tio.c_cflag |= B9600 << IBSHIFT;
 *
 * We hope that we have the posix calls to do it right:
 * If these are not available you might try the above.
 */
IF{B9600}       cfsetospeed(&m->tio, B9600);
IF{B9600}       cfsetispeed(&m->tio, B9600);

IF{CS8} 	m->tio.c_cflag |= CS8;
IF{CREAD}	m->tio.c_cflag |= CREAD;
IF{CLOCAL}	m->tio.c_cflag |= CLOCAL;

IF{ECHOCTL}	m->tio.c_lflag |= ECHOCTL;
IF{ECHOKE}	m->tio.c_lflag |= ECHOKE;

  if (!ttyflag)
    {
IF{ISIG}	m->tio.c_lflag |= ISIG;
IF{ICANON}	m->tio.c_lflag |= ICANON;
IF{ECHO}	m->tio.c_lflag |= ECHO;
    }
IF{ECHOE}	m->tio.c_lflag |= ECHOE;
IF{ECHOK}	m->tio.c_lflag |= ECHOK;
IF{IEXTEN}	m->tio.c_lflag |= IEXTEN;

XIF{VINTR}	m->tio.c_cc[VINTR]    = Ctrl('C');
XIF{VQUIT}	m->tio.c_cc[VQUIT]    = Ctrl('\\');
XIF{VERASE}	m->tio.c_cc[VERASE]   = 0x7f; /* DEL */
XIF{VKILL}	m->tio.c_cc[VKILL]    = Ctrl('U');
XIF{VEOF}	m->tio.c_cc[VEOF]     = Ctrl('D');
XIF{VEOL}	m->tio.c_cc[VEOL]     = VDISABLE;
XIF{VEOL2}	m->tio.c_cc[VEOL2]    = VDISABLE;
XIF{VSWTCH}	m->tio.c_cc[VSWTCH]   = VDISABLE;
XIF{VSTART}	m->tio.c_cc[VSTART]   = Ctrl('Q');
XIF{VSTOP}	m->tio.c_cc[VSTOP]    = Ctrl('S');
XIF{VSUSP}	m->tio.c_cc[VSUSP]    = Ctrl('Z');
XIF{VDSUSP}	m->tio.c_cc[VDSUSP]   = Ctrl('Y');
XIF{VREPRINT}	m->tio.c_cc[VREPRINT] = Ctrl('R');
XIF{VDISCARD}	m->tio.c_cc[VDISCARD] = Ctrl('O');
XIF{VWERASE}	m->tio.c_cc[VWERASE]  = Ctrl('W');
XIF{VLNEXT}	m->tio.c_cc[VLNEXT]   = Ctrl('V');
XIF{VSTATUS}	m->tio.c_cc[VSTATUS]  = Ctrl('T');

  if (ttyflag)
    {
      m->tio.c_cc[VMIN] = TTYVMIN;
      m->tio.c_cc[VTIME] = TTYVTIME;
    }

# ifdef HPUX_LTCHARS_HACK
  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_LTCHARS_HACK */

#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{ISTRIP}	m->tio.c_iflag |= ISTRIP;
IF{IXON}	m->tio.c_iflag |= IXON;

  if (!ttyflag)	/* may not even be good for ptys.. */
    {
IF{OPOST}	m->tio.c_oflag |= OPOST;
IF{ICRNL}	m->tio.c_iflag |= ICRNL;
IF{ONLCR}	m->tio.c_oflag |= ONLCR;
IF{TAB3}	m->tio.c_oflag |= TAB3;
    }

#ifdef __bsdi__
		)-: cannot handle BSDI without POSIX
#else
IF{B9600}	m->tio.c_cflag  = B9600;
#endif
IF{CS8} 	m->tio.c_cflag |= CS8;
IF{CREAD}	m->tio.c_cflag |= CREAD;

  if (!ttyflag)
    {
IF{ISIG}	m->tio.c_lflag |= ISIG;
IF{ICANON}	m->tio.c_lflag |= ICANON;
IF{ECHO}	m->tio.c_lflag |= ECHO;
    }
IF{ECHOE}	m->tio.c_lflag |= ECHOE;
IF{ECHOK}	m->tio.c_lflag |= ECHOK;

XIF{VINTR}	m->tio.c_cc[VINTR]  = Ctrl('C');
XIF{VQUIT}	m->tio.c_cc[VQUIT]  = Ctrl('\\');
XIF{VERASE}	m->tio.c_cc[VERASE] = 0177; /* DEL */
XIF{VKILL}	m->tio.c_cc[VKILL]  = Ctrl('U');
XIF{VEOF}	m->tio.c_cc[VEOF]   = Ctrl('D');
XIF{VEOL}	m->tio.c_cc[VEOL]   = VDISABLE;
XIF{VEOL2}	m->tio.c_cc[VEOL2]  = VDISABLE;
XIF{VSWTCH}	m->tio.c_cc[VSWTCH] = VDISABLE;

  if (ttyflag) {
    m->tio.c_cc[VMIN] = TTYVMIN;
    m->tio.c_cc[VTIME] = TTYVTIME;
  }

# 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('U');
  if (!ttyflag)
    m->m_ttyb.sg_flags = CRMOD | ECHO
IF{ANYP}	| ANYP
    ;
  else
    m->m_ttyb.sg_flags = CBREAK
IF{ANYP}	| 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{NTTYDISC}	m->m_ldisc = NTTYDISC;

  m->m_lmode = 0
IF{LDECCTQ}	| LDECCTQ
IF{LCTLECH}	| LCTLECH
IF{LPASS8}	| LPASS8
IF{LCRTKIL}	| LCRTKIL
IF{LCRTERA}	| LCRTERA
IF{LCRTBS}	| LCRTBS
  ;
# endif /* TERMIO */
#endif /* POSIX */

#if defined(ENCODINGS) && defined(TIOCKSET)
  m->m_jtchars.t_ascii = 'J';
  m->m_jtchars.t_kanji = 'B';
  m->m_knjmode = KM_ASCII | KM_SYSSJIS;
#endif
}

void SetTTY(int fd, struct mode *mp)
{
  errno = 0;
#ifdef POSIX
  tcsetattr(fd, TCSADRAIN, &mp->tio);
# ifdef HPUX_LTCHARS_HACK
  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(ENCODINGS) && defined(TIOCKSET)
  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(int fd, struct mode *mp)
{
  errno = 0;
#ifdef POSIX
  tcgetattr(fd, &mp->tio);
# ifdef HPUX_LTCHARS_HACK
  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(ENCODINGS) && defined(TIOCKSET)
  ioctl(fd, TIOCKGETC, &mp->m_jtchars);
  ioctl(fd, TIOCKGET, &mp->m_knjmode);
#endif
  if (errno)
    Msg(errno, "GetTTY (fd %d): ioctl failed", fd);
}

/*
 * needs interrupt = iflag and flow = d->d_flow
 */
void SetMode(struct mode *op, struct mode *np, int flow, int interrupt)
{
  *np = *op;

  ASSERT(display);
#if defined(TERMIO) || defined(POSIX)
# ifdef CYTERMIO
  np->m_mapkey = NOMAPKEY;
  np->m_mapscreen = NOMAPSCREEN;
  np->tio.c_line = 0;
# endif
IF{ICRNL}  np->tio.c_iflag &= ~ICRNL;
IF{ISTRIP}  np->tio.c_iflag &= ~ISTRIP;
IF{ONLCR}  np->tio.c_oflag &= ~ONLCR;
  np->tio.c_lflag &= ~(ICANON | ECHO);
  /*
   * From Andrew Myers (andru@tonic.lcs.mit.edu)
   * to avoid ^V^V-Problem on OSF1
   */
IF{IEXTEN}  np->tio.c_lflag &= ~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 (interrupt)
    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.
   *
   * We may want to do a VMIN > 0, VTIME > 0 read on the ptys too, to 
   * reduce interrupt frequency.  But then we would not know how to 
   * handle read returning 0. jw.
   */
  np->tio.c_cc[VMIN] = 1;
  np->tio.c_cc[VTIME] = 0;
  if (!interrupt || !flow)
    np->tio.c_cc[VINTR] = VDISABLE;
  np->tio.c_cc[VQUIT] = VDISABLE;
  if (flow == 0)
    {
XIF{VSTART}	np->tio.c_cc[VSTART] = VDISABLE;
XIF{VSTOP}	np->tio.c_cc[VSTOP] = VDISABLE;
      np->tio.c_iflag &= ~IXON;
    }
XIF{VDISCARD}	np->tio.c_cc[VDISCARD] = VDISABLE;
XIF{VLNEXT}	np->tio.c_cc[VLNEXT] = VDISABLE;
XIF{VSTATUS}	np->tio.c_cc[VSTATUS] = VDISABLE;
XIF{VSUSP}	np->tio.c_cc[VSUSP] = VDISABLE;
 /* Set VERASE to DEL, rather than VDISABLE, to avoid libvte
    "autodetect" issues. */
XIF{VERASE}	np->tio.c_cc[VERASE] = 0x7f;
XIF{VKILL}	np->tio.c_cc[VKILL] = VDISABLE;
# ifdef HPUX_LTCHARS_HACK
  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_LTCHARS_HACK */
XIF{VDSUSP}	np->tio.c_cc[VDSUSP] = VDISABLE;
XIF{VREPRINT}	np->tio.c_cc[VREPRINT] = VDISABLE;
XIF{VWERASE}	np->tio.c_cc[VWERASE] = VDISABLE;
# endif /* HPUX_LTCHARS_HACK */
#else /* TERMIO || POSIX */
  if (!interrupt || !flow)
    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 (flow == 0)
    {
      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) */
}

/* operates on display */
void SetFlow(int on)
{
  ASSERT(display);
  if (D_flow == on)
    return;
#if defined(TERMIO) || defined(POSIX)
  if (on)
    {
      D_NewMode.tio.c_cc[VINTR] = iflag ? D_OldMode.tio.c_cc[VINTR] : VDISABLE;
XIF{VSTART}	D_NewMode.tio.c_cc[VSTART] = D_OldMode.tio.c_cc[VSTART];
XIF{VSTOP}	D_NewMode.tio.c_cc[VSTOP] = D_OldMode.tio.c_cc[VSTOP];
      D_NewMode.tio.c_iflag |= D_OldMode.tio.c_iflag & IXON;
    }
  else
    {
      D_NewMode.tio.c_cc[VINTR] = VDISABLE;
XIF{VSTART}	D_NewMode.tio.c_cc[VSTART] = VDISABLE;
XIF{VSTOP}	D_NewMode.tio.c_cc[VSTOP] = VDISABLE;
      D_NewMode.tio.c_iflag &= ~IXON;
    }
# ifdef POSIX
#  ifdef TCOON
  if (!on)
    tcflow(D_userfd, TCOON);
#  endif
  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 = iflag ? D_OldMode.m_tchars.t_intrc : -1;
    D_NewMode.m_tchars.t_startc = D_OldMode.m_tchars.t_startc;
    D_NewMode.m_tchars.t_stopc = D_OldMode.m_tchars.t_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;
}

/* parse commands from opt and modify m */
int SttyMode(struct mode *m, char *opt)
{
  static const char sep[] = " \t:;,";

  if (!opt)
    return 0;

  while (*opt) {
    while (index(sep, *opt)) opt++;
      if (*opt >= '0' && *opt <= '9') {
        if (SetBaud(m, atoi(opt), atoi(opt)))
	      return -1;
      }

      else if (!strncmp("cs7", opt, 3)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_cflag &= ~CSIZE;
	  m->tio.c_cflag |= CS7;
#else
	  m->m_lmode &= ~LPASS8;
#endif
      }

      else if (!strncmp("cs8", opt, 3)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_cflag &= ~CSIZE;
	  m->tio.c_cflag |= CS8;
#else
	  m->m_lmode |= LPASS8;
#endif
      }

      else if (!strncmp("istrip", opt, 6)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag |= ISTRIP;
#else
	  m->m_lmode &= ~LPASS8;
#endif
      }

      else if (!strncmp("-istrip", opt, 7)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag &= ~ISTRIP;
#else
	  m->m_lmode |= LPASS8;
#endif
      }

      else if (!strncmp("ixon", opt, 4)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag |= IXON;
#else
	  debug("SttyMode: no ixon in old bsd land.\n");
#endif
      }

      else if (!strncmp("-ixon", opt, 5)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag &= ~IXON;
#else
	  debug("SttyMode: no -ixon in old bsd land.\n");
#endif
      }

      else if (!strncmp("ixoff", opt, 5)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag |= IXOFF;
#else
	  m->m_ttyb.sg_flags |= TANDEM;
#endif
      }

      else if (!strncmp("-ixoff", opt, 6)) {
#if defined(POSIX) || defined(TERMIO)
	  m->tio.c_iflag &= ~IXOFF;
#else
	  m->m_ttyb.sg_flags &= ~TANDEM;
#endif
      }

      else if (!strncmp("crtscts", opt, 7)) {
#if (defined(POSIX) || defined(TERMIO)) && defined(CRTSCTS)
	  m->tio.c_cflag |= CRTSCTS;
#endif
      }

      else if (!strncmp("-crtscts", opt, 8)) {
#if (defined(POSIX) || defined(TERMIO)) && defined(CRTSCTS)
	  m->tio.c_cflag &= ~CRTSCTS;
#endif
      }

      else
        return -1;

      while (*opt && !index(sep, *opt)) opt++;
    }
  return 0;
}

/*
 *  Job control handling
 *
 *  Somehow the ultrix session handling is broken, so use
 *  the bsdish variant.
 */

/*ARGSUSED*/
void brktty(int fd)
{
#if defined(POSIX) && !defined(ultrix)
  if (separate_sids)
    setsid();		/* will break terminal affiliation */
  /* GNU added for Hurd systems 2001-10-10 */
# if defined(BSD) && defined(TIOCSCTTY) && !defined(__GNU__)
  ioctl(fd, TIOCSCTTY, (char *)0);
# endif /* BSD && TIOCSCTTY */
#else /* POSIX */
# ifdef SYSV
  if (separate_sids)
    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(int fd)
{
#ifdef BSDJOBS
  int mypid;
  mypid = getpid();

  /*
   * Under BSD we have to set the controlling terminal again explicitly.
   */
# if (defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__GNU__) || defined(__OpenBSD__)) && defined(TIOCSCTTY)
  ioctl(fd, TIOCSCTTY, (char *)0);
# endif

# ifdef POSIX
  if (separate_sids)
    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 (separate_sids)
    if (setpgrp(fd, mypid))
      debug1("fgtty: setpgrp: %d\n", errno);
#  endif
# endif /* POSIX */
#endif /* BSDJOBS */
  return 0;
}

/* 
 * The alm boards on our sparc center 1000 have a lousy driver.
 * We cannot generate long breaks unless we use the most ugly form
 * of ioctls. jw.
 */
#ifdef POSIX
int breaktype = 2;
#else /* POSIX */
# ifdef TCSBRK
int breaktype = 1;
# else
int breaktype = 0;
# endif
#endif /* POSIX */

#if defined(sun) && !defined(SVR4)
# define HAVE_SUPER_TCSENDBREAK
#endif

/*
 * type:
 *  0:	TIOCSBRK / TIOCCBRK
 *  1:	TCSBRK
 *  2:	tcsendbreak()
 * n: approximate duration in 1/4 seconds.
 */
static void DoSendBreak(int fd, int n, int type)
{
  switch (type) {
    case 2:	/* tcsendbreak() =============================== */
#ifdef POSIX
# ifdef HAVE_SUPER_TCSENDBREAK
      /* There is one rare case that I have tested, where tcsendbreak works
       * really great: this was an alm driver that came with SunOS 4.1.3
       * If you have this one, define the above symbol.
       * here we can use the second parameter to specify the duration.
       */
      debug2("tcsendbreak(fd=%d, %d)\n", fd, n);
      if (tcsendbreak(fd, n) < 0)
        Msg(errno, "cannot send BREAK (tcsendbreak)");
# else
      /* 
       * here we hope, that multiple calls to tcsendbreak() can
       * be concatenated to form a long break, as we do not know 
       * what exact interpretation the second parameter has:
       *
       * - sunos 4: duration in quarter seconds
       * - sunos 5: 0 a short break, nonzero a tcdrain()
       * - hpux, irix: ignored
       * - mot88: duration in milliseconds
       * - aix: duration in milliseconds, but 0 is 25 milliseconds.
       */
      debug2("%d * tcsendbreak(fd=%d, 0)\n", n, fd);
	  {
	    int i;

	    if (!n)
	      n++;
	    for (i = 0; i < n; i++)
	      if (tcsendbreak(fd, 0) < 0) {
		    Msg(errno, "cannot send BREAK (tcsendbreak SVR4)");
		    return;
	      }
	  }
# endif
#else /* POSIX */
      Msg(0, "tcsendbreak() not available, change breaktype");
#endif /* POSIX */
      break;

    case 1:	/* TCSBRK ======================================= */
#ifdef TCSBRK
      if (!n)
        n++;
      /*
       * Here too, we assume that short breaks can be concatenated to 
       * perform long breaks. But for SOLARIS, this is not true, of course.
       */
      debug2("%d * TCSBRK fd=%d\n", n, fd);
	  {
	    int i;

	    for (i = 0; i < n; i++)
	      if (ioctl(fd, TCSBRK, (char *)0) < 0) {
		    Msg(errno, "Cannot send BREAK (TCSBRK)");
		    return;
	      }
	  }
#else /* TCSBRK */
      Msg(0, "TCSBRK not available, change breaktype");
#endif /* TCSBRK */
      break;

    case 0:	/* TIOCSBRK / TIOCCBRK ========================== */
#if defined(TIOCSBRK) && defined(TIOCCBRK)
      /*
       * This is very rude. Screen actively celebrates the break.
       * But it may be the only save way to issue long breaks.
       */
      debug("TIOCSBRK TIOCCBRK\n");
      if (ioctl(fd, TIOCSBRK, (char *)0) < 0) {
	    Msg(errno, "Can't send BREAK (TIOCSBRK)");
	    return;
	  }
      sleep1000(n ? n * 250 : 250);
      if (ioctl(fd, TIOCCBRK, (char *)0) < 0) {
	    Msg(errno, "BREAK stuck!!! -- HELP! (TIOCCBRK)");
	    return;
	  }
#else /* TIOCSBRK && TIOCCBRK */
      Msg(0, "TIOCSBRK/CBRK not available, change breaktype");
#endif /* TIOCSBRK && TIOCCBRK */
      break;

    default:	/* unknown ========================== */
      Msg(0, "Internal SendBreak error: method %d unknown", type);
    }
}

/* 
 * Send a break for n * 0.25 seconds. Tty must be PLAIN.
 * The longest possible break allowed here is 15 seconds.
 */

void SendBreak(struct win *wp, int n, int closeopen)
{
  sigret_t (*sigalrm)__P(SIGPROTOARG);

#ifdef BUILTIN_TELNET
  if (wp->w_type == W_TYPE_TELNET) {
    TelBreak(wp);
    return;
  }
#endif
  if (wp->w_type != W_TYPE_PLAIN)
    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);
    sleep1000(n ? n * 250 : 250);
    if ((wp->w_ptyfd = OpenTTY(wp->w_tty, wp->w_cmdargs[1])) < 1) {
	  Msg(0, "Ouch, cannot reopen line %s, please try harder", wp->w_tty);
	  return;
	}
    (void) fcntl(wp->w_ptyfd, F_SETFL, FNBLOCK);
  }
  else {
    sigalrm = signal(SIGALRM, SigAlrmDummy);
    alarm(15);

    DoSendBreak(wp->w_ptyfd, n, breaktype);

    alarm(0);
    signal(SIGALRM, sigalrm);
  }
  debug("            broken.\n");
}

/*
 *  Console grabbing
 */

#if (!defined(TIOCCONS) && defined(SRIOCSREDIR)) || defined(linux)

static struct event consredir_ev;
static int consredirfd[2] = {-1, -1};

static void consredir_readev_fn(struct event *ev, char *data)
{
  char *p, *n, buf[256];
  int l;

  if (!console_window || (l = read(consredirfd[0], buf, sizeof(buf))) <= 0) {
    close(consredirfd[0]);
    close(consredirfd[1]);
    consredirfd[0] = consredirfd[1] = -1;
    evdeq(ev);
    return;
  }

  for (p = n = buf; l > 0; n++, l--)
    if (*n == '\n') {
      if (n > p)
        WriteString(console_window, p, n - p);
      WriteString(console_window, "\r\n", 2);
      p = n + 1;
    }

  if (n > p)
    WriteString(console_window, p, n - p);
}

#endif

/*ARGSUSED*/
int TtyGrabConsole(int fd, int on, char *rc_name)
{
#if defined(TIOCCONS) && !defined(linux)
  struct display *d;
  int ret = 0;
  int sfd = -1;

  if (on < 0)
    return 0;		/* pty close will ungrab */

  if (on) {
    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 (!on) {
    char *slave;
    if ((fd = OpenPTY(&slave)) < 0) {
      Msg(errno, "%s: could not open detach pty master", rc_name);
      return -1;
	}

    if ((sfd = open(slave, O_RDWR | O_NOCTTY)) < 0) {
      Msg(errno, "%s: could not open detach pty slave", rc_name);
      close(fd);
      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
# if defined(SRIOCSREDIR) || defined(linux)
  struct display *d;
#  ifdef SRIOCSREDIR
  int cfd;
#  else
  struct mode new1, new2;
  char *slave;
#  endif

  if (on > 0) {
    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 (consredirfd[0] >= 0) {
    evdeq(&consredir_ev);
    close(consredirfd[0]);
    close(consredirfd[1]);
    consredirfd[0] = consredirfd[1] = -1;
  }
  if (on <= 0)
    return 0;

#  ifdef SRIOCSREDIR
  if ((cfd = secopen("/dev/console", O_RDWR|O_NOCTTY, 0)) == -1) {
    Msg(errno, "/dev/console");
    return -1;
  }

  if (pipe(consredirfd)) {
    Msg(errno, "pipe");
    close(cfd);
    consredirfd[0] = consredirfd[1] = -1;
    return -1;
  }
 
  if (ioctl(cfd, SRIOCSREDIR, consredirfd[1])) {
    Msg(errno, "SRIOCSREDIR ioctl");
    close(cfd);
    close(consredirfd[0]);
    close(consredirfd[1]);
    consredirfd[0] = consredirfd[1] = -1;
    return -1;
  }

  close(cfd);
#  else
  /* special linux workaround for a too restrictive kernel */
  if ((consredirfd[0] = OpenPTY(&slave)) < 0) {
    Msg(errno, "%s: could not open detach pty master", rc_name);
    return -1;
  }
  if ((consredirfd[1] = open(slave, O_RDWR | O_NOCTTY)) < 0) {
    Msg(errno, "%s: could not open detach pty slave", rc_name);
    close(consredirfd[0]);
    return -1;
  }
  InitTTY(&new1, 0);
  SetMode(&new1, &new2, 0, 0);
  SetTTY(consredirfd[1], &new2);

  if (UserContext() == 1)
    UserReturn(ioctl(consredirfd[1], TIOCCONS, (char *)&on));
  if (UserStatus()) {
    Msg(errno, "%s: ioctl TIOCCONS failed", rc_name);
    close(consredirfd[0]);
    close(consredirfd[1]);
    return -1;
  }
#  endif

  consredir_ev.fd = consredirfd[0];
  consredir_ev.type = EV_READ;
  consredir_ev.handler = consredir_readev_fn;
  evenq(&consredir_ev);
  return 0;

# else
  if (on > 0)
    Msg(0, "%s: don't know how to grab the console", rc_name);
  return -1;
# endif
#endif
}

/*
 * Read modem control lines of a physical tty and write them to buf
 * in a readable format.
 * Will not write more than 256 characters to buf.
 * Returns buf;
 */
char * TtyGetModemStatus(int fd, char *buf)
{
  char *p = buf;
#ifdef TIOCGSOFTCAR
  unsigned int softcar;
#endif
#if defined(TIOCMGET) || defined(TIOCMODG)
  unsigned int mflags;
#else
# ifdef MCGETA
  /* this is yet another interface, found on hpux. grrr */
  mflag mflags;
IF{MDTR}#  define TIOCM_DTR MDTR
IF{MRTS}#  define TIOCM_RTS MRTS
IF{MDSR}#  define TIOCM_DSR MDSR
IF{MDCD}#  define TIOCM_CAR MDCD
IF{MRI}#  define TIOCM_RNG MRI
IF{MCTS}#  define TIOCM_CTS MCTS
# endif
#endif

#if defined(CLOCAL) || defined(CRTSCTS)
  struct mode mtio;	/* screen.h */
#endif

#if defined(CRTSCTS) || defined(TIOCM_CTS)
  int rtscts;
#endif
  int clocal;

#if defined(CLOCAL) || defined(CRTSCTS)
  GetTTY(fd, &mtio);
#endif
  clocal = 0;
#ifdef CLOCAL
  if (mtio.tio.c_cflag & CLOCAL) {
    clocal = 1;
    *p++ = '{';
  }
#endif

#ifdef TIOCM_CTS
# ifdef CRTSCTS
  if (!(mtio.tio.c_cflag & CRTSCTS))
    rtscts = 0;
  else
# endif /* CRTSCTS */
    rtscts = 1;
#endif /* TIOCM_CTS */

#ifdef TIOCGSOFTCAR
  if (ioctl(fd, TIOCGSOFTCAR, (char *)&softcar) < 0)
    softcar = 0;
#endif

#if defined(TIOCMGET) || defined(TIOCMODG) || defined(MCGETA)
# ifdef TIOCMGET
  if (ioctl(fd, TIOCMGET, (char *)&mflags) < 0)
# else
#  ifdef TIOCMODG
  if (ioctl(fd, TIOCMODG, (char *)&mflags) < 0)
#  else
  if (ioctl(fd, MCGETA, &mflags) < 0)
#  endif
# endif
    {
#ifdef TIOCGSOFTCAR
      sprintf(p, "NO-TTY? %s", softcar ? "(CD)" : "CD");
#else
      sprintf(p, "NO-TTY?");
#endif
      p += strlen(p);
    }
  else
    {
      char *s;
# ifdef FANCY_MODEM
#  ifdef TIOCM_LE
      if (!(mflags & TIOCM_LE))
        for (s = "!LE "; *s; *p++ = *s++);
#  endif
# endif /* FANCY_MODEM */

# ifdef TIOCM_RTS
      s = "!RTS ";
      if (mflags & TIOCM_RTS)
        s++;
      while (*s) *p++ = *s++;
# endif

# ifdef TIOCM_CTS
      s = "!CTS "; 
      if (!rtscts) {
        *p++ = '(';
        s = "!CTS) "; 
      }
      if (mflags & TIOCM_CTS)
        s++;
      while (*s) *p++ = *s++;
# endif

# ifdef TIOCM_DTR
      s = "!DTR ";
      if (mflags & TIOCM_DTR)
        s++;
      while (*s) *p++ = *s++;
# endif

# ifdef TIOCM_DSR
      s = "!DSR ";
      if (mflags & TIOCM_DSR)
        s++;
      while (*s) *p++ = *s++;
# endif

# if defined(TIOCM_CD) || defined(TIOCM_CAR)
      s = "!CD "; 
#  ifdef TIOCGSOFTCAR
      if (softcar) {
        *p++ = '(';
        s = "!CD) ";
      }
#  endif

#  ifdef TIOCM_CD
      if (mflags & TIOCM_CD)
        s++;
#  else
      if (mflags & TIOCM_CAR)
        s++;
#  endif
      while (*s) *p++ = *s++;
# endif


# if defined(TIOCM_RI) || defined(TIOCM_RNG)
#  ifdef TIOCM_RI
      if (mflags & TIOCM_RI)
#  else
      if (mflags & TIOCM_RNG)
#  endif
        for (s = "RI "; *s; *p++ = *s++);
# endif


# ifdef FANCY_MODEM
#  ifdef TIOCM_ST
      s = "!ST ";
      if (mflags & TIOCM_ST)
        s++;
      while (*s) *p++ = *s++;
#  endif

#  ifdef TIOCM_SR
      s = "!SR ";
      if (mflags & TIOCM_SR)
        s++;
      while (*s) *p++ = *s++;
#  endif

# endif /* FANCY_MODEM */
      if (p > buf && p[-1] == ' ')
        p--;
      *p = '\0';
  }
#else
# ifdef TIOCGSOFTCAR
  sprintf(p, " %s", softcar ? "(CD)", "CD");
  p += strlen(p);
# endif
#endif
  if (clocal)
    *p++ = '}';
  *p = '\0';
  return buf;
}

/*
 * Old bsd-ish machines may not have any of the baudrate B... symbols.
 * We hope to detect them here, so that the btable[] below always has
 * many entries.
 */
#ifndef POSIX
# ifndef TERMIO
#  if !defined(B9600) && !defined(B2400) && !defined(B1200) && !defined(B300)
IFN{B0}#define		B0	0
IFN{B50}#define  	B50	1
IFN{B75}#define  	B75	2
IFN{B110}#define 	B110	3
IFN{B134}#define 	B134	4
IFN{B150}#define 	B150	5
IFN{B200}#define 	B200	6
IFN{B300}#define 	B300	7
IFN{B600}#define 	B600	8
IFN{B1200}#define	B1200	9
IFN{B1800}#define	B1800	10
IFN{B2400}#define	B2400	11
IFN{B4800}#define	B4800	12
IFN{B9600}#define	B9600	13
IFN{EXTA}#define	EXTA	14
IFN{EXTB}#define	EXTB	15
#  endif
# endif
#endif

/*
 * On hpux, idx and sym will be different. 
 * Rumor has it that, we need idx in D_dospeed to make tputs
 * padding correct. 
 * Frequently used entries come first.
 */
static struct baud_values btable[] =
{
IF{B4000000}	{	33,	4000000,	B4000000},
IF{B3500000}	{	32,	3500000,	B3500000},
IF{B3000000}	{	31,	3000000,	B3000000},
IF{B2500000}	{	30,	2500000,	B2500000},
IF{B2000000}	{	29,	2000000,	B2000000},
IF{B1500000}	{	28,	1500000,	B1500000},
IF{B1152000}	{	27,	1152000,	B1152000},
IF{B1000000}	{	26,	1000000,	B1000000},
IF{B921600}	{	25,	921600,		B921600	},
IF{B576000}	{	24,	576000,		B576000	},
IF{B500000}	{	23,	500000,		B500000	},
IF{B460800}	{	22,	460800,		B460800	},
IF{B230400}	{	21,	230400,		B230400	},
IF{B115200}	{	20,	115200,		B115200	},
IF{B57600}	{	19,	57600,		B57600	},
IF{EXTB}	{	18,	38400,		EXTB	},
IF{B38400}	{	18,	38400,		B38400	},
IF{EXTA}	{	17,	19200,		EXTA	},
IF{B19200}	{	17,	19200,		B19200	},
IF{B9600}	{	16,	9600,		B9600	},
IF{B7200}	{	15,	7200,		B7200	},
IF{B4800}	{	14,	4800,		B4800	},
IF{B3600}	{	13,	3600,		B3600	},
IF{B2400}	{	12,	2400,		B2400	},
IF{B1800}	{	11,	1800,		B1800	},
IF{B1200}	{	10,	1200,		B1200	},
IF{B900} 	{	9,	900,		B900	},
IF{B600} 	{	8,	600,		B600	},
IF{B300} 	{	7,	300, 		B300	},
IF{B200} 	{	6,	200, 		B200	},
IF{B150} 	{	5,	150,		B150	},
IF{B134} 	{	4,	134,		B134	},
IF{B110} 	{	3,	110,		B110	},
IF{B75}  	{	2,	75,		B75	},
IF{B50}  	{	1,	50,		B50	},
IF{B0}   	{	0,	0,		B0	},
		{	-1,	-1,		-1	}
};

/*
 * baud may either be a bits-per-second value or a symbolic
 * value as returned by cfget?speed() 
 */
struct baud_values *lookup_baud(int baud)
{
  struct baud_values *p;

  for (p = btable; p->idx >= 0; p++)
    if (baud == p->bps || baud == p->sym)
      return p;
  return NULL;
}

/*
 * change the baud rate in a mode structure.
 * ibaud and obaud are given in bit/second, or at your option as
 * termio B... symbols as defined in e.g. suns sys/ttydev.h
 * -1 means do not change.
 */
int SetBaud(struct mode *m, int ibaud, int obaud)
{
  struct baud_values *ip, *op;

  if ((!(ip = lookup_baud(ibaud)) && ibaud != -1) || (!(op = lookup_baud(obaud)) && obaud != -1))
    return -1;

#ifdef POSIX
  if (ip)
    cfsetispeed(&m->tio, ip->sym);
  if (op)
    cfsetospeed(&m->tio, op->sym);
#else /* POSIX */

# ifdef TERMIO
  if (ip) {
#  ifdef IBSHIFT
    m->tio.c_cflag &= ~(CBAUD << IBSHIFT);
    m->tio.c_cflag |= (ip->sym & CBAUD) << IBSHIFT;
#  else /* IBSHIFT */
    if (ibaud != obaud)
      return -1;
#  endif /* IBSHIFT */
  }

  if (op) {
    m->tio.c_cflag &= ~CBAUD;
    m->tio.c_cflag |= op->sym & CBAUD;
  }
# else /* TERMIO */
  if (ip)
    m->m_ttyb.sg_ispeed = ip->idx;
  if (op)
    m->m_ttyb.sg_ospeed = op->idx;
# endif /* TERMIO */
#endif /* POSIX */
  return 0;
}


int CheckTtyname (char *tty)
{
  struct stat st;
  char realbuf[PATH_MAX];
  const char *real;
  int rc;

  real = realpath(tty, realbuf);
  if (!real)
    return -1;
  realbuf[sizeof(realbuf)-1]='\0';

  if (lstat(real, &st) || !S_ISCHR(st.st_mode) || (st.st_nlink > 1 && strncmp(real, "/dev", 4)))
    rc = -1;
  else
    rc = 0;

  return rc;
}

/*
 *  Write out the mode struct in a readable form
 */

#ifdef DEBUG
void DebugTTY(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);
  debug1("cfgetospeed() = %d\n", (int)cfgetospeed(&m->tio));
  debug1("cfgetispeed() = %d\n", (int)cfgetispeed(&m->tio));

  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_LTCHARS_HACK
  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_LTCHARS_HACK */

#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 */