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;
71 static char path_gettytab[PATH_MAX];
74 static int firsttime = 1;
76 strlcpy(path_gettytab, _PATH_GETTYTAB, sizeof(path_gettytab));
77 dba[0] = path_gettytab;
82 * we need to strdup() anything in the strings array
83 * initially in order to simplify things later
85 for (sp = gettystrs; sp->field; sp++)
86 if (sp->value != NULL) {
87 /* handle these ones more carefully */
88 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
91 l = strlen(sp->value) + 1;
92 if ((p = malloc(l)) != NULL)
93 strlcpy(p, sp->value, l);
95 * replace, even if NULL, else we'll
96 * have problems with free()ing static mem
103 switch (cgetent(&buf, dba, name)) {
105 syslog(LOG_ERR, "getty: couldn't resolve 'tc=' in gettytab '%s'", name);
110 syslog(LOG_ERR, "getty: unknown gettytab entry '%s'", name);
113 syslog(LOG_ERR, "getty: retrieving gettytab entry '%s': %m", name);
116 syslog(LOG_ERR, "getty: recursive 'tc=' reference gettytab entry '%s'", name);
119 syslog(LOG_ERR, "getty: unexpected cgetent() error for entry '%s'", name);
123 for (sp = gettystrs; sp->field; sp++) {
124 if ((l = cgetstr(buf, sp->field, &p)) >= 0) {
126 /* prefer existing value */
127 if (strcmp(p, sp->value) != 0)
135 } else if (l == -1) {
141 for (np = gettynums; np->field; np++) {
142 if (cgetnum(buf, np->field, &n) == -1)
150 for (fp = gettyflags; fp->field; fp++) {
151 if (cgetcap(buf, fp->field, ':') == NULL)
155 fp->value = 1 ^ fp->invrt;
163 struct gettystrs *sp;
164 struct gettynums *np;
165 struct gettyflags *fp;
167 for (sp = gettystrs; sp->field; sp++)
169 sp->defalt = strdup(sp->value);
170 for (np = gettynums; np->field; np++)
172 np->defalt = np->value;
173 for (fp = gettyflags; fp->field; fp++)
175 fp->defalt = fp->value;
177 fp->defalt = fp->invrt;
183 struct gettystrs *sp;
184 struct gettynums *np;
185 struct gettyflags *fp;
187 for (sp = gettystrs; sp->field; sp++)
189 sp->value = !sp->defalt ? sp->defalt
190 : strdup(sp->defalt);
191 for (np = gettynums; np->field; np++)
193 np->value = np->defalt;
194 for (fp = gettyflags; fp->field; fp++)
196 fp->value = fp->defalt;
201 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
202 &SU, &DS, &RP, &FL, &WE, &LN, 0
207 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
208 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
209 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
210 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
211 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
220 for (i = 0; charnames[i]; i++) {
225 *charvars[i] = _POSIX_VDISABLE;
229 /* Macros to clear/set/test flags. */
230 #define SET(t, f) (t) |= (f)
231 #define CLR(t, f) (t) &= ~(f)
232 #define ISSET(t, f) ((t) & (f))
237 tcflag_t iflag, oflag, cflag, lflag;
242 if (C0set && I0set && L0set && O0set) {
251 if (C1set && I1set && L1set && O1set) {
260 if (C2set && I2set && L2set && O2set) {
270 iflag = omode.c_iflag;
271 oflag = omode.c_oflag;
272 cflag = omode.c_cflag;
273 lflag = omode.c_lflag;
276 CLR(cflag, CSIZE|PARENB);
278 CLR(iflag, ISTRIP|INPCK|IGNPAR);
279 } else if (AP || EP || OP) {
281 SET(cflag, CS7|PARENB);
284 SET(iflag, INPCK|IGNPAR);
288 } else if (EP && !OP) {
289 SET(iflag, INPCK|IGNPAR);
293 } else if (AP || (EP && OP)) {
294 CLR(iflag, INPCK|IGNPAR);
297 } /* else, leave as is */
321 SET(oflag, ONLCR|OPOST);
328 SET(oflag, OXTABS|OPOST);
336 if (n == 1) { /* read mode flags */
340 CLR(cflag, CSIZE|PARENB);
388 tmode.c_iflag = iflag;
389 tmode.c_oflag = oflag;
390 tmode.c_cflag = cflag;
391 tmode.c_lflag = lflag;
397 unsigned delay; /* delay in ms */
402 * below are random guesses, I can't be bothered checking
405 struct delayval crdelay[] = {
414 struct delayval nldelay[] = {
415 { 1, NL1 }, /* special, calculated */
422 struct delayval bsdelay[] = {
427 struct delayval ffdelay[] = {
433 struct delayval tbdelay[] = {
436 { 3, XTABS }, /* this is expand tabs */
446 f = adelay(CD, crdelay);
447 f |= adelay(ND, nldelay);
448 f |= adelay(FD, ffdelay);
449 f |= adelay(TD, tbdelay);
450 f |= adelay(BD, bsdelay);
455 adelay(int ms, struct delayval *dp)
459 while (dp->delay && ms > dp->delay)
465 char editedhost[MAXHOSTNAMELEN];
468 edithost(const char *pattern)
474 if (pattern == NULL || *pattern == '\0')
476 if (regcomp(®ex, pattern, REG_EXTENDED) != 0)
479 match = calloc(regex.re_nsub + 1, sizeof(*match));
485 found = !regexec(®ex, HN, regex.re_nsub + 1, match, 0);
487 size_t subex, totalsize;
490 * We found a match. If there were no parenthesized
491 * subexpressions in the pattern, use entire matched
492 * string as ``editedhost''; otherwise use the first
493 * matched subexpression.
495 subex = !!regex.re_nsub;
496 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
497 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
498 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
505 * In case of any errors, or if the pattern did not match, pass
506 * the original hostname as is.
509 strlcpy(editedhost, HN, sizeof(editedhost));
512 static struct speedtab {
530 { 19, EXTA }, /* for people who say 19.2K */
533 { 7200, EXTB }, /* alternative */
548 for (sp = speedtab; sp->speed; sp++)
549 if (sp->speed == val)
552 return (B300); /* default in impossible cases */
558 static char termbuf[128] = "TERM=";
564 strlcat(termbuf, TT, sizeof(termbuf));
569 while ((q = strchr(q, ','))) {
581 * This speed select mechanism is written for the Develcon DATASWITCH.
582 * The Develcon sends a string of the form "B{speed}\n" at a predefined
583 * baud rate. This string indicates the user's actual speed.
584 * The routine below returns the terminal type mapped from derived speed.
586 static struct portselect {
590 { "B110", "std.110" },
591 { "B134", "std.134" },
592 { "B150", "std.150" },
593 { "B300", "std.300" },
594 { "B600", "std.600" },
595 { "B1200", "std.1200" },
596 { "B2400", "std.2400" },
597 { "B4800", "std.4800" },
598 { "B9600", "std.9600" },
599 { "B19200", "std.19200" },
607 const char *type = "default";
608 struct portselect *ps;
612 for (len = 0; len < sizeof (baud) - 1; len++) {
613 if (read(STDIN_FILENO, &c, 1) <= 0)
616 if (c == '\n' || c == '\r')
619 len = 0; /* in case of leading garbage */
623 for (ps = portspeeds; ps->ps_baud; ps++)
624 if (strcmp(ps->ps_baud, baud) == 0) {
628 sleep(2); /* wait for connection to complete */
633 * This auto-baud speed select mechanism is written for the Micom 600
634 * portselector. Selection is done by looking at how the character '\r'
635 * is garbled at the different speeds.
640 struct pollfd set[1];
641 struct timespec timeout;
643 const char *type = "9600-baud";
645 (void)tcflush(0, TCIOFLUSH);
646 set[0].fd = STDIN_FILENO;
647 set[0].events = POLLIN;
648 if (poll(set, 1, 5000) <= 0)
650 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
653 timeout.tv_nsec = 20000;
654 (void)nanosleep(&timeout, NULL);
655 (void)tcflush(0, TCIOFLUSH);
658 case 0200: /* 300-baud */
662 case 0346: /* 1200-baud */
666 case 015: /* 2400-baud */
671 default: /* 4800-baud */
675 case 0377: /* 9600-baud */