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 $
48 #include <sys/ioctl.h>
49 #include <sys/param.h>
54 #include "pathnames.h"
59 static void compatflags (long);
66 gettable(const char *name, char *buf)
70 struct gettyflags *fp;
77 static int firsttime = 1;
79 dba[0] = _PATH_GETTYTAB;
84 * we need to strdup() anything in the strings array
85 * initially in order to simplify things later
87 for (sp = gettystrs; sp->field; sp++)
88 if (sp->value != NULL) {
89 /* handle these ones more carefully */
90 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
93 l = strlen(sp->value) + 1;
94 if ((p = malloc(l)) != NULL) {
95 strncpy(p, sp->value, l);
99 * replace, even if NULL, else we'll
100 * have problems with free()ing static mem
107 switch (cgetent(&buf, (char **)dba, (char *)name)) {
109 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
113 msg = "%s: unknown gettytab entry '%s'";
116 msg = "%s: retrieving gettytab entry '%s': %m";
119 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
122 msg = "%s: unexpected cgetent() error for entry '%s'";
127 syslog(LOG_ERR, msg, "getty", name);
131 for (sp = gettystrs; sp->field; sp++) {
132 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
134 /* prefer existing value */
135 if (strcmp(p, sp->value) != 0)
143 } else if (l == -1) {
149 for (np = gettynums; np->field; np++) {
150 if (cgetnum(buf, (char*)np->field, &n) == -1)
158 for (fp = gettyflags; fp->field; fp++) {
159 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
163 fp->value = 1 ^ fp->invrt;
168 printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
169 for (sp = gettystrs; sp->field; sp++)
170 printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
171 for (np = gettynums; np->field; np++)
172 printf("cgetnum: %s=%d\r\n", np->field, np->value);
173 for (fp = gettyflags; fp->field; fp++)
174 printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
175 fp->value + '0', fp->set + '0');
182 struct gettystrs *sp;
183 struct gettynums *np;
184 struct gettyflags *fp;
186 for (sp = gettystrs; sp->field; sp++)
188 sp->defalt = strdup(sp->value);
189 for (np = gettynums; np->field; np++)
191 np->defalt = np->value;
192 for (fp = gettyflags; fp->field; fp++)
194 fp->defalt = fp->value;
196 fp->defalt = fp->invrt;
202 struct gettystrs *sp;
203 struct gettynums *np;
204 struct gettyflags *fp;
206 for (sp = gettystrs; sp->field; sp++)
208 sp->value = !sp->defalt ? sp->defalt
209 : strdup(sp->defalt);
210 for (np = gettynums; np->field; np++)
212 np->value = np->defalt;
213 for (fp = gettyflags; fp->field; fp++)
215 fp->value = fp->defalt;
220 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
221 &SU, &DS, &RP, &FL, &WE, &LN, 0
226 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
227 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
228 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
229 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
230 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
239 for (i = 0; charnames[i]; i++) {
244 *charvars[i] = _POSIX_VDISABLE;
248 /* Macros to clear/set/test flags. */
249 #define SET(t, f) (t) |= (f)
250 #define CLR(t, f) (t) &= ~(f)
251 #define ISSET(t, f) ((t) & (f))
256 tcflag_t iflag, oflag, cflag, lflag;
283 if (C0set && I0set && L0set && O0set) {
292 if (C1set && I1set && L1set && O1set) {
301 if (C2set && I2set && L2set && O2set) {
311 iflag = omode.c_iflag;
312 oflag = omode.c_oflag;
313 cflag = omode.c_cflag;
314 lflag = omode.c_lflag;
317 CLR(cflag, CSIZE|PARENB);
319 CLR(iflag, ISTRIP|INPCK|IGNPAR);
320 } else if (AP || EP || OP) {
322 SET(cflag, CS7|PARENB);
325 SET(iflag, INPCK|IGNPAR);
329 } else if (EP && !OP) {
330 SET(iflag, INPCK|IGNPAR);
334 } else if (AP || (EP && OP)) {
335 CLR(iflag, INPCK|IGNPAR);
338 } /* else, leave as is */
362 SET(oflag, ONLCR|OPOST);
369 SET(oflag, OXTABS|OPOST);
377 if (n == 1) { /* read mode flags */
381 CLR(cflag, CSIZE|PARENB);
429 tmode.c_iflag = iflag;
430 tmode.c_oflag = oflag;
431 tmode.c_cflag = cflag;
432 tmode.c_lflag = lflag;
437 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
440 compatflags(long flags)
442 tcflag_t iflag, oflag, cflag, lflag;
444 iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
445 oflag = OPOST|ONLCR|OXTABS;
447 lflag = ICANON|ISIG|IEXTEN;
449 if (ISSET(flags, TANDEM))
453 if (ISSET(flags, ECHO))
457 if (ISSET(flags, CRMOD)) {
464 if (ISSET(flags, XTABS))
470 if (ISSET(flags, RAW)) {
472 CLR(lflag, ISIG|ICANON|IEXTEN);
475 SET(iflag, BRKINT|IXON|IMAXBEL);
476 SET(lflag, ISIG|IEXTEN);
477 if (ISSET(flags, CBREAK))
481 switch (ISSET(flags, ANYP)) {
502 /* Nothing we can do with CRTBS. */
503 if (ISSET(flags, PRTERA))
507 if (ISSET(flags, CRTERA))
511 /* Nothing we can do with TILDE. */
512 if (ISSET(flags, MDMBUF))
516 if (ISSET(flags, NOHANG))
520 if (ISSET(flags, CRTKIL))
524 if (ISSET(flags, CTLECH))
528 if (!ISSET(flags, DECCTQ))
532 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
533 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
535 if (ISSET(flags, RAW|LITOUT|PASS8)) {
538 if (!ISSET(flags, RAW|PASS8))
542 if (!ISSET(flags, RAW|LITOUT))
553 tmode.c_iflag = iflag;
554 tmode.c_oflag = oflag;
555 tmode.c_cflag = cflag;
556 tmode.c_lflag = lflag;
562 unsigned delay; /* delay in ms */
567 * below are random guesses, I can't be bothered checking
570 struct delayval crdelay[] = {
579 struct delayval nldelay[] = {
580 { 1, NL1 }, /* special, calculated */
587 struct delayval bsdelay[] = {
592 struct delayval ffdelay[] = {
598 struct delayval tbdelay[] = {
601 { 3, XTABS }, /* this is expand tabs */
611 f = adelay(CD, crdelay);
612 f |= adelay(ND, nldelay);
613 f |= adelay(FD, ffdelay);
614 f |= adelay(TD, tbdelay);
615 f |= adelay(BD, bsdelay);
620 adelay(int ms, struct delayval *dp)
624 while (dp->delay && ms > dp->delay)
630 char editedhost[MAXHOSTNAMELEN];
633 edithost(const char *pat)
635 const char *host = HN;
636 char *res = editedhost;
658 if (res == &editedhost[sizeof editedhost - 1]) {
665 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
668 editedhost[sizeof editedhost - 1] = '\0';
671 static struct speedtab {
689 { 19, EXTA }, /* for people who say 19.2K */
692 { 7200, EXTB }, /* alternative */
707 for (sp = speedtab; sp->speed; sp++)
708 if (sp->speed == val)
711 return (B300); /* default in impossible cases */
717 static char termbuf[128] = "TERM=";
723 strlcat(termbuf, TT, sizeof(termbuf));
728 while ((q = strchr(q, ','))) {
740 * This speed select mechanism is written for the Develcon DATASWITCH.
741 * The Develcon sends a string of the form "B{speed}\n" at a predefined
742 * baud rate. This string indicates the user's actual speed.
743 * The routine below returns the terminal type mapped from derived speed.
749 { "B110", "std.110" },
750 { "B134", "std.134" },
751 { "B150", "std.150" },
752 { "B300", "std.300" },
753 { "B600", "std.600" },
754 { "B1200", "std.1200" },
755 { "B2400", "std.2400" },
756 { "B4800", "std.4800" },
757 { "B9600", "std.9600" },
758 { "B19200", "std.19200" },
766 const char *type = "default";
767 struct portselect *ps;
771 for (len = 0; len < sizeof (baud) - 1; len++) {
772 if (read(STDIN_FILENO, &c, 1) <= 0)
775 if (c == '\n' || c == '\r')
778 len = 0; /* in case of leading garbage */
782 for (ps = portspeeds; ps->ps_baud; ps++)
783 if (strcmp(ps->ps_baud, baud) == 0) {
787 sleep(2); /* wait for connection to complete */
792 * This auto-baud speed select mechanism is written for the Micom 600
793 * portselector. Selection is done by looking at how the character '\r'
794 * is garbled at the different speeds.
800 struct timeval timeout;
802 const char *type = "9600-baud";
804 (void)tcflush(0, TCIOFLUSH);
808 if (select(32, (fd_set *)&rfds, NULL, NULL, &timeout) <= 0)
810 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
813 timeout.tv_usec = 20;
814 (void) select(32, NULL, NULL, NULL, &timeout);
815 (void)tcflush(0, TCIOFLUSH);
818 case 0200: /* 300-baud */
822 case 0346: /* 1200-baud */
826 case 015: /* 2400-baud */
831 default: /* 4800-baud */
835 case 0377: /* 9600-baud */