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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)from: subr.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/libexec/getty/subr.c,v 1.16.2.1 2001/05/12 10:16:51 kris Exp $
44 #include <sys/ioctl.h>
45 #include <sys/param.h>
50 #include "pathnames.h"
55 static void compatflags (long);
62 gettable(const char *name
, char *buf
)
66 struct gettyflags
*fp
;
73 static int firsttime
= 1;
75 dba
[0] = _PATH_GETTYTAB
;
80 * we need to strdup() anything in the strings array
81 * initially in order to simplify things later
83 for (sp
= gettystrs
; sp
->field
; sp
++)
84 if (sp
->value
!= NULL
) {
85 /* handle these ones more carefully */
86 if (sp
>= &gettystrs
[4] && sp
<= &gettystrs
[6])
89 l
= strlen(sp
->value
) + 1;
90 if ((p
= malloc(l
)) != NULL
) {
91 strncpy(p
, sp
->value
, l
);
95 * replace, even if NULL, else we'll
96 * have problems with free()ing static mem
103 switch (cgetent(&buf
, (char **)dba
, (char *)name
)) {
105 msg
= "%s: couldn't resolve 'tc=' in gettytab '%s'";
109 msg
= "%s: unknown gettytab entry '%s'";
112 msg
= "%s: retrieving gettytab entry '%s': %m";
115 msg
= "%s: recursive 'tc=' reference gettytab entry '%s'";
118 msg
= "%s: unexpected cgetent() error for entry '%s'";
123 syslog(LOG_ERR
, msg
, "getty", name
);
127 for (sp
= gettystrs
; sp
->field
; sp
++) {
128 if ((l
= cgetstr(buf
, (char*)sp
->field
, &p
)) >= 0) {
130 /* prefer existing value */
131 if (strcmp(p
, sp
->value
) != 0)
139 } else if (l
== -1) {
145 for (np
= gettynums
; np
->field
; np
++) {
146 if (cgetnum(buf
, (char*)np
->field
, &n
) == -1)
154 for (fp
= gettyflags
; fp
->field
; fp
++) {
155 if (cgetcap(buf
, (char *)fp
->field
, ':') == NULL
)
159 fp
->value
= 1 ^ fp
->invrt
;
164 printf("name=\"%s\", buf=\"%s\"\r\n", name
, buf
);
165 for (sp
= gettystrs
; sp
->field
; sp
++)
166 printf("cgetstr: %s=%s\r\n", sp
->field
, sp
->value
);
167 for (np
= gettynums
; np
->field
; np
++)
168 printf("cgetnum: %s=%d\r\n", np
->field
, np
->value
);
169 for (fp
= gettyflags
; fp
->field
; fp
++)
170 printf("cgetflags: %s='%c' set='%c'\r\n", fp
->field
,
171 fp
->value
+ '0', fp
->set
+ '0');
178 struct gettystrs
*sp
;
179 struct gettynums
*np
;
180 struct gettyflags
*fp
;
182 for (sp
= gettystrs
; sp
->field
; sp
++)
184 sp
->defalt
= strdup(sp
->value
);
185 for (np
= gettynums
; np
->field
; np
++)
187 np
->defalt
= np
->value
;
188 for (fp
= gettyflags
; fp
->field
; fp
++)
190 fp
->defalt
= fp
->value
;
192 fp
->defalt
= fp
->invrt
;
198 struct gettystrs
*sp
;
199 struct gettynums
*np
;
200 struct gettyflags
*fp
;
202 for (sp
= gettystrs
; sp
->field
; sp
++)
204 sp
->value
= !sp
->defalt
? sp
->defalt
205 : strdup(sp
->defalt
);
206 for (np
= gettynums
; np
->field
; np
++)
208 np
->value
= np
->defalt
;
209 for (fp
= gettyflags
; fp
->field
; fp
++)
211 fp
->value
= fp
->defalt
;
216 &ER
, &KL
, &IN
, &QU
, &XN
, &XF
, &ET
, &BK
,
217 &SU
, &DS
, &RP
, &FL
, &WE
, &LN
, 0
222 &tmode
.c_cc
[VERASE
], &tmode
.c_cc
[VKILL
], &tmode
.c_cc
[VINTR
],
223 &tmode
.c_cc
[VQUIT
], &tmode
.c_cc
[VSTART
], &tmode
.c_cc
[VSTOP
],
224 &tmode
.c_cc
[VEOF
], &tmode
.c_cc
[VEOL
], &tmode
.c_cc
[VSUSP
],
225 &tmode
.c_cc
[VDSUSP
], &tmode
.c_cc
[VREPRINT
], &tmode
.c_cc
[VDISCARD
],
226 &tmode
.c_cc
[VWERASE
], &tmode
.c_cc
[VLNEXT
], 0
235 for (i
= 0; charnames
[i
]; i
++) {
240 *charvars
[i
] = _POSIX_VDISABLE
;
244 /* Macros to clear/set/test flags. */
245 #define SET(t, f) (t) |= (f)
246 #define CLR(t, f) (t) &= ~(f)
247 #define ISSET(t, f) ((t) & (f))
252 tcflag_t iflag
, oflag
, cflag
, lflag
;
279 if (C0set
&& I0set
&& L0set
&& O0set
) {
288 if (C1set
&& I1set
&& L1set
&& O1set
) {
297 if (C2set
&& I2set
&& L2set
&& O2set
) {
307 iflag
= omode
.c_iflag
;
308 oflag
= omode
.c_oflag
;
309 cflag
= omode
.c_cflag
;
310 lflag
= omode
.c_lflag
;
313 CLR(cflag
, CSIZE
|PARENB
);
315 CLR(iflag
, ISTRIP
|INPCK
|IGNPAR
);
316 } else if (AP
|| EP
|| OP
) {
318 SET(cflag
, CS7
|PARENB
);
321 SET(iflag
, INPCK
|IGNPAR
);
325 } else if (EP
&& !OP
) {
326 SET(iflag
, INPCK
|IGNPAR
);
330 } else if (AP
|| (EP
&& OP
)) {
331 CLR(iflag
, INPCK
|IGNPAR
);
334 } /* else, leave as is */
358 SET(oflag
, ONLCR
|OPOST
);
365 SET(oflag
, OXTABS
|OPOST
);
373 if (n
== 1) { /* read mode flags */
377 CLR(cflag
, CSIZE
|PARENB
);
425 tmode
.c_iflag
= iflag
;
426 tmode
.c_oflag
= oflag
;
427 tmode
.c_cflag
= cflag
;
428 tmode
.c_lflag
= lflag
;
433 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
436 compatflags(long flags
)
438 tcflag_t iflag
, oflag
, cflag
, lflag
;
440 iflag
= BRKINT
|ICRNL
|IMAXBEL
|IXON
|IXANY
;
441 oflag
= OPOST
|ONLCR
|OXTABS
;
443 lflag
= ICANON
|ISIG
|IEXTEN
;
445 if (ISSET(flags
, TANDEM
))
449 if (ISSET(flags
, ECHO
))
453 if (ISSET(flags
, CRMOD
)) {
460 if (ISSET(flags
, XTABS
))
466 if (ISSET(flags
, RAW
)) {
468 CLR(lflag
, ISIG
|ICANON
|IEXTEN
);
471 SET(iflag
, BRKINT
|IXON
|IMAXBEL
);
472 SET(lflag
, ISIG
|IEXTEN
);
473 if (ISSET(flags
, CBREAK
))
477 switch (ISSET(flags
, ANYP
)) {
498 /* Nothing we can do with CRTBS. */
499 if (ISSET(flags
, PRTERA
))
503 if (ISSET(flags
, CRTERA
))
507 /* Nothing we can do with TILDE. */
508 if (ISSET(flags
, MDMBUF
))
512 if (ISSET(flags
, NOHANG
))
516 if (ISSET(flags
, CRTKIL
))
520 if (ISSET(flags
, CTLECH
))
524 if (!ISSET(flags
, DECCTQ
))
528 CLR(lflag
, TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
529 SET(lflag
, ISSET(flags
, TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
));
531 if (ISSET(flags
, RAW
|LITOUT
|PASS8
)) {
534 if (!ISSET(flags
, RAW
|PASS8
))
538 if (!ISSET(flags
, RAW
|LITOUT
))
549 tmode
.c_iflag
= iflag
;
550 tmode
.c_oflag
= oflag
;
551 tmode
.c_cflag
= cflag
;
552 tmode
.c_lflag
= lflag
;
558 unsigned delay
; /* delay in ms */
563 * below are random guesses, I can't be bothered checking
566 struct delayval crdelay
[] = {
575 struct delayval nldelay
[] = {
576 { 1, NL1
}, /* special, calculated */
583 struct delayval bsdelay
[] = {
588 struct delayval ffdelay
[] = {
594 struct delayval tbdelay
[] = {
597 { 3, XTABS
}, /* this is expand tabs */
607 f
= adelay(CD
, crdelay
);
608 f
|= adelay(ND
, nldelay
);
609 f
|= adelay(FD
, ffdelay
);
610 f
|= adelay(TD
, tbdelay
);
611 f
|= adelay(BD
, bsdelay
);
616 adelay(int ms
, struct delayval
*dp
)
620 while (dp
->delay
&& ms
> dp
->delay
)
626 char editedhost
[MAXHOSTNAMELEN
];
629 edithost(const char *pat
)
631 const char *host
= HN
;
632 char *res
= editedhost
;
654 if (res
== &editedhost
[sizeof editedhost
- 1]) {
661 strncpy(res
, host
, sizeof editedhost
- (res
- editedhost
) - 1);
664 editedhost
[sizeof editedhost
- 1] = '\0';
667 static struct speedtab
{
685 { 19, EXTA
}, /* for people who say 19.2K */
688 { 7200, EXTB
}, /* alternative */
703 for (sp
= speedtab
; sp
->speed
; sp
++)
704 if (sp
->speed
== val
)
707 return (B300
); /* default in impossible cases */
713 static char termbuf
[128] = "TERM=";
719 strlcat(termbuf
, TT
, sizeof(termbuf
));
724 while ((q
= strchr(q
, ','))) {
736 * This speed select mechanism is written for the Develcon DATASWITCH.
737 * The Develcon sends a string of the form "B{speed}\n" at a predefined
738 * baud rate. This string indicates the user's actual speed.
739 * The routine below returns the terminal type mapped from derived speed.
745 { "B110", "std.110" },
746 { "B134", "std.134" },
747 { "B150", "std.150" },
748 { "B300", "std.300" },
749 { "B600", "std.600" },
750 { "B1200", "std.1200" },
751 { "B2400", "std.2400" },
752 { "B4800", "std.4800" },
753 { "B9600", "std.9600" },
754 { "B19200", "std.19200" },
762 const char *type
= "default";
763 struct portselect
*ps
;
767 for (len
= 0; len
< sizeof (baud
) - 1; len
++) {
768 if (read(STDIN_FILENO
, &c
, 1) <= 0)
771 if (c
== '\n' || c
== '\r')
774 len
= 0; /* in case of leading garbage */
778 for (ps
= portspeeds
; ps
->ps_baud
; ps
++)
779 if (strcmp(ps
->ps_baud
, baud
) == 0) {
783 sleep(2); /* wait for connection to complete */
788 * This auto-baud speed select mechanism is written for the Micom 600
789 * portselector. Selection is done by looking at how the character '\r'
790 * is garbled at the different speeds.
796 struct timeval timeout
;
798 const char *type
= "9600-baud";
800 (void)tcflush(0, TCIOFLUSH
);
804 if (select(32, (fd_set
*)&rfds
, NULL
, NULL
, &timeout
) <= 0)
806 if (read(STDIN_FILENO
, &c
, sizeof(char)) != sizeof(char))
809 timeout
.tv_usec
= 20;
810 (void) select(32, NULL
, NULL
, NULL
, &timeout
);
811 (void)tcflush(0, TCIOFLUSH
);
814 case 0200: /* 300-baud */
818 case 0346: /* 1200-baud */
822 case 015: /* 2400-baud */
827 default: /* 4800-baud */
831 case 0377: /* 9600-baud */