]>
git.saurik.com Git - apple/system_cmds.git/blob - getty.tproj/subr.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 /*static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93";*/
59 static char rcsid
[] = "$Id: subr.c,v 1.1.1.2 2000/01/11 02:10:15 wsanchez Exp $";
70 #include <sys/ioctl.h>
73 #include "pathnames.h"
76 extern struct termios tmode
, omode
;
78 static void compatflags
__P((long));
87 register struct gettystrs
*sp
;
88 register struct gettynums
*np
;
89 register struct gettyflags
*fp
;
92 dba
[0] = _PATH_GETTYTAB
;
95 if (cgetent(&buf
, dba
, name
) != 0)
98 for (sp
= gettystrs
; sp
->field
; sp
++)
99 cgetstr(buf
, sp
->field
, &sp
->value
);
100 for (np
= gettynums
; np
->field
; np
++) {
101 if (cgetnum(buf
, np
->field
, &n
) == -1)
108 for (fp
= gettyflags
; fp
->field
; fp
++) {
109 if (cgetcap(buf
, fp
->field
, ':') == NULL
)
113 fp
->value
= 1 ^ fp
->invrt
;
117 printf("name=\"%s\", buf=\"%s\"\n", name
, buf
);
118 for (sp
= gettystrs
; sp
->field
; sp
++)
119 printf("cgetstr: %s=%s\n", sp
->field
, sp
->value
);
120 for (np
= gettynums
; np
->field
; np
++)
121 printf("cgetnum: %s=%d\n", np
->field
, np
->value
);
122 for (fp
= gettyflags
; fp
->field
; fp
++)
123 printf("cgetflags: %s='%c' set='%c'\n", fp
->field
,
124 fp
->value
+ '0', fp
->set
+ '0');
132 register struct gettystrs
*sp
;
133 register struct gettynums
*np
;
134 register struct gettyflags
*fp
;
136 for (sp
= gettystrs
; sp
->field
; sp
++)
138 sp
->defalt
= sp
->value
;
139 for (np
= gettynums
; np
->field
; np
++)
141 np
->defalt
= np
->value
;
142 for (fp
= gettyflags
; fp
->field
; fp
++)
144 fp
->defalt
= fp
->value
;
146 fp
->defalt
= fp
->invrt
;
152 register struct gettystrs
*sp
;
153 register struct gettynums
*np
;
154 register struct gettyflags
*fp
;
156 for (sp
= gettystrs
; sp
->field
; sp
++)
158 sp
->value
= sp
->defalt
;
159 for (np
= gettynums
; np
->field
; np
++)
161 np
->value
= np
->defalt
;
162 for (fp
= gettyflags
; fp
->field
; fp
++)
164 fp
->value
= fp
->defalt
;
169 &ER
, &KL
, &IN
, &QU
, &XN
, &XF
, &ET
, &BK
,
170 &SU
, &DS
, &RP
, &FL
, &WE
, &LN
, 0
175 &tmode
.c_cc
[VERASE
], &tmode
.c_cc
[VKILL
], &tmode
.c_cc
[VINTR
],
176 &tmode
.c_cc
[VQUIT
], &tmode
.c_cc
[VSTART
], &tmode
.c_cc
[VSTOP
],
177 &tmode
.c_cc
[VEOF
], &tmode
.c_cc
[VEOL
], &tmode
.c_cc
[VSUSP
],
178 &tmode
.c_cc
[VDSUSP
], &tmode
.c_cc
[VREPRINT
], &tmode
.c_cc
[VDISCARD
],
179 &tmode
.c_cc
[VWERASE
], &tmode
.c_cc
[VLNEXT
], 0
188 for (i
= 0; charnames
[i
]; i
++) {
193 *charvars
[i
] = _POSIX_VDISABLE
;
201 register tcflag_t iflag
, oflag
, cflag
, lflag
;
228 if (C0set
&& I0set
&& L0set
&& O0set
) {
237 if (C1set
&& I1set
&& L1set
&& O1set
) {
246 if (C2set
&& I2set
&& L2set
&& O2set
) {
256 #define BIC(v,c) (v) &= ~(c)
257 #define BIS(v,s) (v) |= (s)
258 #define BICS(v,c,s) BIC(v,c),BIS(v,s)
260 iflag
= omode
.c_iflag
;
261 oflag
= omode
.c_oflag
;
262 cflag
= omode
.c_cflag
;
263 lflag
= omode
.c_lflag
;
266 BIC(iflag
, ISTRIP
|INPCK
|IGNPAR
);
267 BICS(cflag
, CSIZE
|PARENB
|PARODD
, CS8
);
268 } else if (OP
&& !EP
) {
269 BIS(iflag
, ISTRIP
|INPCK
|IGNPAR
);
270 BICS(cflag
, CSIZE
, PARENB
|PARODD
|CS7
);
273 } else if (EP
&& !OP
) {
274 BIS(iflag
, ISTRIP
|INPCK
|IGNPAR
);
275 BICS(cflag
, CSIZE
|PARODD
, PARENB
|CS7
);
278 } else if (AP
|| EP
&& OP
) {
279 BICS(iflag
, INPCK
|IGNPAR
, ISTRIP
);
280 BICS(cflag
, CSIZE
|PARODD
, PARENB
|CS7
);
281 } /* else, leave as is */
302 if (n
== 1) { /* read mode flags */
351 tmode
.c_iflag
= iflag
;
352 tmode
.c_oflag
= oflag
;
353 tmode
.c_cflag
= cflag
;
354 tmode
.c_lflag
= lflag
;
359 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
365 register tcflag_t iflag
, oflag
, cflag
, lflag
;
367 iflag
= (BRKINT
|ICRNL
|IMAXBEL
|IXON
|IXANY
);
368 oflag
= (OPOST
|ONLCR
|OXTABS
);
370 lflag
= (ICANON
|ISIG
|IEXTEN
);
394 lflag
&= ~(ISIG
|ICANON
|IEXTEN
);
396 iflag
|= BRKINT
|IXON
|IMAXBEL
;
397 lflag
|= ISIG
|IEXTEN
;
404 switch (flags
& ANYP
) {
418 if (flags
& (RAW
|LITOUT
|PASS8
)) {
419 cflag
&= ~(CSIZE
|PARENB
);
421 if ((flags
& (RAW
|PASS8
)) == 0)
425 if ((flags
& (RAW
|LITOUT
)) == 0)
460 if ((flags
& DECCTQ
) == 0)
464 lflag
&= ~(TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
465 lflag
|= flags
& (TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
467 if (flags
& (RAW
|LITOUT
|PASS8
)) {
468 cflag
&= ~(CSIZE
|PARENB
);
470 if ((flags
& (RAW
|PASS8
)) == 0)
474 if ((flags
& (RAW
|LITOUT
)) == 0)
485 tmode
.c_iflag
= iflag
;
486 tmode
.c_oflag
= oflag
;
487 tmode
.c_cflag
= cflag
;
488 tmode
.c_lflag
= lflag
;
494 unsigned delay
; /* delay in ms */
499 * below are random guesses, I can't be bothered checking
502 struct delayval crdelay
[] = {
511 struct delayval nldelay
[] = {
512 { 1, NL1
}, /* special, calculated */
519 struct delayval bsdelay
[] = {
524 struct delayval ffdelay
[] = {
530 struct delayval tbdelay
[] = {
533 { 3, XTABS
}, /* this is expand tabs */
543 f
= adelay(CD
, crdelay
);
544 f
|= adelay(ND
, nldelay
);
545 f
|= adelay(FD
, ffdelay
);
546 f
|= adelay(TD
, tbdelay
);
547 f
|= adelay(BD
, bsdelay
);
554 register struct delayval
*dp
;
558 while (dp
->delay
&& ms
> dp
->delay
)
570 register char *host
= HN
;
571 register char *res
= editedhost
;
593 if (res
== &editedhost
[sizeof editedhost
- 1]) {
600 strncpy(res
, host
, sizeof editedhost
- (res
- editedhost
) - 1);
603 editedhost
[sizeof editedhost
- 1] = '\0';
610 static char termbuf
[128] = "TERM=";
611 register char *p
, *q
;
621 while (q
= strchr(q
, ',')) {
633 * This speed select mechanism is written for the Develcon DATASWITCH.
634 * The Develcon sends a string of the form "B{speed}\n" at a predefined
635 * baud rate. This string indicates the user's actual speed.
636 * The routine below returns the terminal type mapped from derived speed.
642 { "B110", "std.110" },
643 { "B134", "std.134" },
644 { "B150", "std.150" },
645 { "B300", "std.300" },
646 { "B600", "std.600" },
647 { "B1200", "std.1200" },
648 { "B2400", "std.2400" },
649 { "B4800", "std.4800" },
650 { "B9600", "std.9600" },
651 { "B19200", "std.19200" },
658 char c
, baud
[20], *type
= "default";
659 register struct portselect
*ps
;
663 for (len
= 0; len
< sizeof (baud
) - 1; len
++) {
664 if (read(STDIN_FILENO
, &c
, 1) <= 0)
667 if (c
== '\n' || c
== '\r')
670 len
= 0; /* in case of leading garbage */
674 for (ps
= portspeeds
; ps
->ps_baud
; ps
++)
675 if (strcmp(ps
->ps_baud
, baud
) == 0) {
679 sleep(2); /* wait for connection to complete */
684 * This auto-baud speed select mechanism is written for the Micom 600
685 * portselector. Selection is done by looking at how the character '\r'
686 * is garbled at the different speeds.
688 #include <sys/time.h>
694 struct timeval timeout
;
695 char c
, *type
= "9600-baud";
698 ioctl(0, TIOCFLUSH
, &null
);
702 if (select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
703 (fd_set
*)NULL
, &timeout
) <= 0)
705 if (read(STDIN_FILENO
, &c
, sizeof(char)) != sizeof(char))
708 timeout
.tv_usec
= 20;
709 (void) select(32, (fd_set
*)NULL
, (fd_set
*)NULL
,
710 (fd_set
*)NULL
, &timeout
);
711 ioctl(0, TIOCFLUSH
, &null
);
714 case 0200: /* 300-baud */
718 case 0346: /* 1200-baud */
722 case 015: /* 2400-baud */
727 default: /* 4800-baud */
731 case 0377: /* 9600-baud */