2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)from: subr.c 8.1 (Berkeley) 6/4/93
34 * $FreeBSD: src/libexec/getty/subr.c,v 1.16.2.1 2001/05/12 10:16:51 kris Exp $
35 * $DragonFly: src/libexec/getty/subr.c,v 1.4 2004/03/26 00:30:12 cpressey Exp $
49 #include <sys/ioctl.h>
50 #include <sys/param.h>
55 #include "pathnames.h"
60 static void compatflags (long);
67 gettable(const char *name
, char *buf
)
71 struct gettyflags
*fp
;
78 static int firsttime
= 1;
80 dba
[0] = _PATH_GETTYTAB
;
85 * we need to strdup() anything in the strings array
86 * initially in order to simplify things later
88 for (sp
= gettystrs
; sp
->field
; sp
++)
89 if (sp
->value
!= NULL
) {
90 /* handle these ones more carefully */
91 if (sp
>= &gettystrs
[4] && sp
<= &gettystrs
[6])
94 l
= strlen(sp
->value
) + 1;
95 if ((p
= malloc(l
)) != NULL
) {
96 strncpy(p
, sp
->value
, l
);
100 * replace, even if NULL, else we'll
101 * have problems with free()ing static mem
108 switch (cgetent(&buf
, (char **)dba
, (char *)name
)) {
110 msg
= "%s: couldn't resolve 'tc=' in gettytab '%s'";
114 msg
= "%s: unknown gettytab entry '%s'";
117 msg
= "%s: retrieving gettytab entry '%s': %m";
120 msg
= "%s: recursive 'tc=' reference gettytab entry '%s'";
123 msg
= "%s: unexpected cgetent() error for entry '%s'";
128 syslog(LOG_ERR
, msg
, "getty", name
);
132 for (sp
= gettystrs
; sp
->field
; sp
++) {
133 if ((l
= cgetstr(buf
, (char*)sp
->field
, &p
)) >= 0) {
135 /* prefer existing value */
136 if (strcmp(p
, sp
->value
) != 0)
144 } else if (l
== -1) {
150 for (np
= gettynums
; np
->field
; np
++) {
151 if (cgetnum(buf
, (char*)np
->field
, &n
) == -1)
159 for (fp
= gettyflags
; fp
->field
; fp
++) {
160 if (cgetcap(buf
, (char *)fp
->field
, ':') == NULL
)
164 fp
->value
= 1 ^ fp
->invrt
;
169 printf("name=\"%s\", buf=\"%s\"\r\n", name
, buf
);
170 for (sp
= gettystrs
; sp
->field
; sp
++)
171 printf("cgetstr: %s=%s\r\n", sp
->field
, sp
->value
);
172 for (np
= gettynums
; np
->field
; np
++)
173 printf("cgetnum: %s=%d\r\n", np
->field
, np
->value
);
174 for (fp
= gettyflags
; fp
->field
; fp
++)
175 printf("cgetflags: %s='%c' set='%c'\r\n", fp
->field
,
176 fp
->value
+ '0', fp
->set
+ '0');
183 struct gettystrs
*sp
;
184 struct gettynums
*np
;
185 struct gettyflags
*fp
;
187 for (sp
= gettystrs
; sp
->field
; sp
++)
189 sp
->defalt
= strdup(sp
->value
);
190 for (np
= gettynums
; np
->field
; np
++)
192 np
->defalt
= np
->value
;
193 for (fp
= gettyflags
; fp
->field
; fp
++)
195 fp
->defalt
= fp
->value
;
197 fp
->defalt
= fp
->invrt
;
203 struct gettystrs
*sp
;
204 struct gettynums
*np
;
205 struct gettyflags
*fp
;
207 for (sp
= gettystrs
; sp
->field
; sp
++)
209 sp
->value
= !sp
->defalt
? sp
->defalt
210 : strdup(sp
->defalt
);
211 for (np
= gettynums
; np
->field
; np
++)
213 np
->value
= np
->defalt
;
214 for (fp
= gettyflags
; fp
->field
; fp
++)
216 fp
->value
= fp
->defalt
;
221 &ER
, &KL
, &IN
, &QU
, &XN
, &XF
, &ET
, &BK
,
222 &SU
, &DS
, &RP
, &FL
, &WE
, &LN
, 0
227 &tmode
.c_cc
[VERASE
], &tmode
.c_cc
[VKILL
], &tmode
.c_cc
[VINTR
],
228 &tmode
.c_cc
[VQUIT
], &tmode
.c_cc
[VSTART
], &tmode
.c_cc
[VSTOP
],
229 &tmode
.c_cc
[VEOF
], &tmode
.c_cc
[VEOL
], &tmode
.c_cc
[VSUSP
],
230 &tmode
.c_cc
[VDSUSP
], &tmode
.c_cc
[VREPRINT
], &tmode
.c_cc
[VDISCARD
],
231 &tmode
.c_cc
[VWERASE
], &tmode
.c_cc
[VLNEXT
], 0
240 for (i
= 0; charnames
[i
]; i
++) {
245 *charvars
[i
] = _POSIX_VDISABLE
;
249 /* Macros to clear/set/test flags. */
250 #define SET(t, f) (t) |= (f)
251 #define CLR(t, f) (t) &= ~(f)
252 #define ISSET(t, f) ((t) & (f))
257 tcflag_t iflag
, oflag
, cflag
, lflag
;
284 if (C0set
&& I0set
&& L0set
&& O0set
) {
293 if (C1set
&& I1set
&& L1set
&& O1set
) {
302 if (C2set
&& I2set
&& L2set
&& O2set
) {
312 iflag
= omode
.c_iflag
;
313 oflag
= omode
.c_oflag
;
314 cflag
= omode
.c_cflag
;
315 lflag
= omode
.c_lflag
;
318 CLR(cflag
, CSIZE
|PARENB
);
320 CLR(iflag
, ISTRIP
|INPCK
|IGNPAR
);
321 } else if (AP
|| EP
|| OP
) {
323 SET(cflag
, CS7
|PARENB
);
326 SET(iflag
, INPCK
|IGNPAR
);
330 } else if (EP
&& !OP
) {
331 SET(iflag
, INPCK
|IGNPAR
);
335 } else if (AP
|| (EP
&& OP
)) {
336 CLR(iflag
, INPCK
|IGNPAR
);
339 } /* else, leave as is */
363 SET(oflag
, ONLCR
|OPOST
);
370 SET(oflag
, OXTABS
|OPOST
);
378 if (n
== 1) { /* read mode flags */
382 CLR(cflag
, CSIZE
|PARENB
);
430 tmode
.c_iflag
= iflag
;
431 tmode
.c_oflag
= oflag
;
432 tmode
.c_cflag
= cflag
;
433 tmode
.c_lflag
= lflag
;
438 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
441 compatflags(long flags
)
443 tcflag_t iflag
, oflag
, cflag
, lflag
;
445 iflag
= BRKINT
|ICRNL
|IMAXBEL
|IXON
|IXANY
;
446 oflag
= OPOST
|ONLCR
|OXTABS
;
448 lflag
= ICANON
|ISIG
|IEXTEN
;
450 if (ISSET(flags
, TANDEM
))
454 if (ISSET(flags
, ECHO
))
458 if (ISSET(flags
, CRMOD
)) {
465 if (ISSET(flags
, XTABS
))
471 if (ISSET(flags
, RAW
)) {
473 CLR(lflag
, ISIG
|ICANON
|IEXTEN
);
476 SET(iflag
, BRKINT
|IXON
|IMAXBEL
);
477 SET(lflag
, ISIG
|IEXTEN
);
478 if (ISSET(flags
, CBREAK
))
482 switch (ISSET(flags
, ANYP
)) {
503 /* Nothing we can do with CRTBS. */
504 if (ISSET(flags
, PRTERA
))
508 if (ISSET(flags
, CRTERA
))
512 /* Nothing we can do with TILDE. */
513 if (ISSET(flags
, MDMBUF
))
517 if (ISSET(flags
, NOHANG
))
521 if (ISSET(flags
, CRTKIL
))
525 if (ISSET(flags
, CTLECH
))
529 if (!ISSET(flags
, DECCTQ
))
533 CLR(lflag
, TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
534 SET(lflag
, ISSET(flags
, TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
));
536 if (ISSET(flags
, RAW
|LITOUT
|PASS8
)) {
539 if (!ISSET(flags
, RAW
|PASS8
))
543 if (!ISSET(flags
, RAW
|LITOUT
))
554 tmode
.c_iflag
= iflag
;
555 tmode
.c_oflag
= oflag
;
556 tmode
.c_cflag
= cflag
;
557 tmode
.c_lflag
= lflag
;
563 unsigned delay
; /* delay in ms */
568 * below are random guesses, I can't be bothered checking
571 struct delayval crdelay
[] = {
580 struct delayval nldelay
[] = {
581 { 1, NL1
}, /* special, calculated */
588 struct delayval bsdelay
[] = {
593 struct delayval ffdelay
[] = {
599 struct delayval tbdelay
[] = {
602 { 3, XTABS
}, /* this is expand tabs */
612 f
= adelay(CD
, crdelay
);
613 f
|= adelay(ND
, nldelay
);
614 f
|= adelay(FD
, ffdelay
);
615 f
|= adelay(TD
, tbdelay
);
616 f
|= adelay(BD
, bsdelay
);
621 adelay(int ms
, struct delayval
*dp
)
625 while (dp
->delay
&& ms
> dp
->delay
)
631 char editedhost
[MAXHOSTNAMELEN
];
634 edithost(const char *pat
)
636 const char *host
= HN
;
637 char *res
= editedhost
;
659 if (res
== &editedhost
[sizeof editedhost
- 1]) {
666 strncpy(res
, host
, sizeof editedhost
- (res
- editedhost
) - 1);
669 editedhost
[sizeof editedhost
- 1] = '\0';
672 static struct speedtab
{
690 { 19, EXTA
}, /* for people who say 19.2K */
693 { 7200, EXTB
}, /* alternative */
708 for (sp
= speedtab
; sp
->speed
; sp
++)
709 if (sp
->speed
== val
)
712 return (B300
); /* default in impossible cases */
718 static char termbuf
[128] = "TERM=";
724 strlcat(termbuf
, TT
, sizeof(termbuf
));
729 while ((q
= strchr(q
, ','))) {
741 * This speed select mechanism is written for the Develcon DATASWITCH.
742 * The Develcon sends a string of the form "B{speed}\n" at a predefined
743 * baud rate. This string indicates the user's actual speed.
744 * The routine below returns the terminal type mapped from derived speed.
750 { "B110", "std.110" },
751 { "B134", "std.134" },
752 { "B150", "std.150" },
753 { "B300", "std.300" },
754 { "B600", "std.600" },
755 { "B1200", "std.1200" },
756 { "B2400", "std.2400" },
757 { "B4800", "std.4800" },
758 { "B9600", "std.9600" },
759 { "B19200", "std.19200" },
767 const char *type
= "default";
768 struct portselect
*ps
;
772 for (len
= 0; len
< sizeof (baud
) - 1; len
++) {
773 if (read(STDIN_FILENO
, &c
, 1) <= 0)
776 if (c
== '\n' || c
== '\r')
779 len
= 0; /* in case of leading garbage */
783 for (ps
= portspeeds
; ps
->ps_baud
; ps
++)
784 if (strcmp(ps
->ps_baud
, baud
) == 0) {
788 sleep(2); /* wait for connection to complete */
793 * This auto-baud speed select mechanism is written for the Micom 600
794 * portselector. Selection is done by looking at how the character '\r'
795 * is garbled at the different speeds.
801 struct timeval timeout
;
803 const char *type
= "9600-baud";
805 (void)tcflush(0, TCIOFLUSH
);
809 if (select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
810 (fd_set
*)NULL
, &timeout
) <= 0)
812 if (read(STDIN_FILENO
, &c
, sizeof(char)) != sizeof(char))
815 timeout
.tv_usec
= 20;
816 (void) select(32, (fd_set
*)NULL
, (fd_set
*)NULL
,
817 (fd_set
*)NULL
, &timeout
);
818 (void)tcflush(0, TCIOFLUSH
);
821 case 0200: /* 300-baud */
825 case 0346: /* 1200-baud */
829 case 015: /* 2400-baud */
834 default: /* 4800-baud */
838 case 0377: /* 9600-baud */