2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93";
36 static const char rcsid[] =
43 #include <sys/ioctl.h>
44 #include <sys/param.h>
56 #include "pathnames.h"
63 gettable(const char *name, char *buf)
67 struct gettyflags *fp;
74 static int firsttime = 1;
76 dba[0] = _PATH_GETTYTAB;
81 * we need to strdup() anything in the strings array
82 * initially in order to simplify things later
84 for (sp = gettystrs; sp->field; sp++)
85 if (sp->value != NULL) {
86 /* handle these ones more carefully */
87 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
90 l = strlen(sp->value) + 1;
91 if ((p = malloc(l)) != NULL) {
92 strncpy(p, sp->value, l);
96 * replace, even if NULL, else we'll
97 * have problems with free()ing static mem
104 switch (cgetent(&buf, (char **)dba, (char *)name)) {
106 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
110 msg = "%s: unknown gettytab entry '%s'";
113 msg = "%s: retrieving gettytab entry '%s': %m";
116 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
119 msg = "%s: unexpected cgetent() error for entry '%s'";
124 syslog(LOG_ERR, msg, "getty", name);
128 for (sp = gettystrs; sp->field; sp++) {
129 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
131 /* prefer existing value */
132 if (strcmp(p, sp->value) != 0)
140 } else if (l == -1) {
146 for (np = gettynums; np->field; np++) {
147 if (cgetnum(buf, (char*)np->field, &n) == -1)
155 for (fp = gettyflags; fp->field; fp++) {
156 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
160 fp->value = 1 ^ fp->invrt;
168 struct gettystrs *sp;
169 struct gettynums *np;
170 struct gettyflags *fp;
172 for (sp = gettystrs; sp->field; sp++)
174 sp->defalt = strdup(sp->value);
175 for (np = gettynums; np->field; np++)
177 np->defalt = np->value;
178 for (fp = gettyflags; fp->field; fp++)
180 fp->defalt = fp->value;
182 fp->defalt = fp->invrt;
188 struct gettystrs *sp;
189 struct gettynums *np;
190 struct gettyflags *fp;
192 for (sp = gettystrs; sp->field; sp++)
194 sp->value = !sp->defalt ? sp->defalt
195 : strdup(sp->defalt);
196 for (np = gettynums; np->field; np++)
198 np->value = np->defalt;
199 for (fp = gettyflags; fp->field; fp++)
201 fp->value = fp->defalt;
206 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
207 &SU, &DS, &RP, &FL, &WE, &LN, 0
212 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
213 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
214 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
215 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
216 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
225 for (i = 0; charnames[i]; i++) {
230 *charvars[i] = _POSIX_VDISABLE;
234 /* Macros to clear/set/test flags. */
235 #define SET(t, f) (t) |= (f)
236 #define CLR(t, f) (t) &= ~(f)
237 #define ISSET(t, f) ((t) & (f))
242 tcflag_t iflag, oflag, cflag, lflag;
247 if (C0set && I0set && L0set && O0set) {
256 if (C1set && I1set && L1set && O1set) {
265 if (C2set && I2set && L2set && O2set) {
275 iflag = omode.c_iflag;
276 oflag = omode.c_oflag;
277 cflag = omode.c_cflag;
278 lflag = omode.c_lflag;
281 CLR(cflag, CSIZE|PARENB);
283 CLR(iflag, ISTRIP|INPCK|IGNPAR);
284 } else if (AP || EP || OP) {
286 SET(cflag, CS7|PARENB);
289 SET(iflag, INPCK|IGNPAR);
293 } else if (EP && !OP) {
294 SET(iflag, INPCK|IGNPAR);
298 } else if (AP || (EP && OP)) {
299 CLR(iflag, INPCK|IGNPAR);
302 } /* else, leave as is */
326 SET(oflag, ONLCR|OPOST);
333 SET(oflag, OXTABS|OPOST);
341 if (n == 1) { /* read mode flags */
345 CLR(cflag, CSIZE|PARENB);
393 tmode.c_iflag = iflag;
394 tmode.c_oflag = oflag;
395 tmode.c_cflag = cflag;
396 tmode.c_lflag = lflag;
402 unsigned delay; /* delay in ms */
407 * below are random guesses, I can't be bothered checking
410 struct delayval crdelay[] = {
419 struct delayval nldelay[] = {
420 { 1, NL1 }, /* special, calculated */
427 struct delayval bsdelay[] = {
432 struct delayval ffdelay[] = {
438 struct delayval tbdelay[] = {
441 { 3, XTABS }, /* this is expand tabs */
451 f = adelay(CD, crdelay);
452 f |= adelay(ND, nldelay);
453 f |= adelay(FD, ffdelay);
454 f |= adelay(TD, tbdelay);
455 f |= adelay(BD, bsdelay);
460 adelay(int ms, struct delayval *dp)
464 while (dp->delay && ms > dp->delay)
470 char editedhost[MAXHOSTNAMELEN];
473 edithost(const char *pattern)
479 if (pattern == NULL || *pattern == '\0')
481 if (regcomp(®ex, pattern, REG_EXTENDED) != 0)
484 match = calloc(regex.re_nsub + 1, sizeof(*match));
490 found = !regexec(®ex, HN, regex.re_nsub + 1, match, 0);
492 size_t subex, totalsize;
495 * We found a match. If there were no parenthesized
496 * subexpressions in the pattern, use entire matched
497 * string as ``editedhost''; otherwise use the first
498 * matched subexpression.
500 subex = !!regex.re_nsub;
501 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
502 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
503 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
510 * In case of any errors, or if the pattern did not match, pass
511 * the original hostname as is.
514 strlcpy(editedhost, HN, sizeof(editedhost));
517 static struct speedtab {
535 { 19, EXTA }, /* for people who say 19.2K */
538 { 7200, EXTB }, /* alternative */
553 for (sp = speedtab; sp->speed; sp++)
554 if (sp->speed == val)
557 return (B300); /* default in impossible cases */
563 static char termbuf[128] = "TERM=";
569 strlcat(termbuf, TT, sizeof(termbuf));
574 while ((q = strchr(q, ','))) {
586 * This speed select mechanism is written for the Develcon DATASWITCH.
587 * The Develcon sends a string of the form "B{speed}\n" at a predefined
588 * baud rate. This string indicates the user's actual speed.
589 * The routine below returns the terminal type mapped from derived speed.
595 { "B110", "std.110" },
596 { "B134", "std.134" },
597 { "B150", "std.150" },
598 { "B300", "std.300" },
599 { "B600", "std.600" },
600 { "B1200", "std.1200" },
601 { "B2400", "std.2400" },
602 { "B4800", "std.4800" },
603 { "B9600", "std.9600" },
604 { "B19200", "std.19200" },
612 const char *type = "default";
613 struct portselect *ps;
617 for (len = 0; len < sizeof (baud) - 1; len++) {
618 if (read(STDIN_FILENO, &c, 1) <= 0)
621 if (c == '\n' || c == '\r')
624 len = 0; /* in case of leading garbage */
628 for (ps = portspeeds; ps->ps_baud; ps++)
629 if (strcmp(ps->ps_baud, baud) == 0) {
633 sleep(2); /* wait for connection to complete */
638 * This auto-baud speed select mechanism is written for the Micom 600
639 * portselector. Selection is done by looking at how the character '\r'
640 * is garbled at the different speeds.
645 struct pollfd set[1];
646 struct timespec timeout;
648 const char *type = "9600-baud";
650 (void)tcflush(0, TCIOFLUSH);
651 set[0].fd = STDIN_FILENO;
652 set[0].events = POLLIN;
653 if (poll(set, 1, 5000) <= 0)
655 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
658 timeout.tv_nsec = 20000;
659 (void)nanosleep(&timeout, NULL);
660 (void)tcflush(0, TCIOFLUSH);
663 case 0200: /* 300-baud */
667 case 0346: /* 1200-baud */
671 case 015: /* 2400-baud */
676 default: /* 4800-baud */
680 case 0377: /* 9600-baud */