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 strncpy(p, sp->value, l);
97 * replace, even if NULL, else we'll
98 * have problems with free()ing static mem
105 switch (cgetent(&buf, dba, name)) {
107 syslog(LOG_ERR, "getty: couldn't resolve 'tc=' in gettytab '%s'", name);
112 syslog(LOG_ERR, "getty: unknown gettytab entry '%s'", name);
115 syslog(LOG_ERR, "getty: retrieving gettytab entry '%s': %m", name);
118 syslog(LOG_ERR, "getty: recursive 'tc=' reference gettytab entry '%s'", name);
121 syslog(LOG_ERR, "getty: unexpected cgetent() error for entry '%s'", name);
125 for (sp = gettystrs; sp->field; sp++) {
126 if ((l = cgetstr(buf, sp->field, &p)) >= 0) {
128 /* prefer existing value */
129 if (strcmp(p, sp->value) != 0)
137 } else if (l == -1) {
143 for (np = gettynums; np->field; np++) {
144 if (cgetnum(buf, np->field, &n) == -1)
152 for (fp = gettyflags; fp->field; fp++) {
153 if (cgetcap(buf, fp->field, ':') == NULL)
157 fp->value = 1 ^ fp->invrt;
165 struct gettystrs *sp;
166 struct gettynums *np;
167 struct gettyflags *fp;
169 for (sp = gettystrs; sp->field; sp++)
171 sp->defalt = strdup(sp->value);
172 for (np = gettynums; np->field; np++)
174 np->defalt = np->value;
175 for (fp = gettyflags; fp->field; fp++)
177 fp->defalt = fp->value;
179 fp->defalt = fp->invrt;
185 struct gettystrs *sp;
186 struct gettynums *np;
187 struct gettyflags *fp;
189 for (sp = gettystrs; sp->field; sp++)
191 sp->value = !sp->defalt ? sp->defalt
192 : strdup(sp->defalt);
193 for (np = gettynums; np->field; np++)
195 np->value = np->defalt;
196 for (fp = gettyflags; fp->field; fp++)
198 fp->value = fp->defalt;
203 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
204 &SU, &DS, &RP, &FL, &WE, &LN, 0
209 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
210 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
211 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
212 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
213 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
222 for (i = 0; charnames[i]; i++) {
227 *charvars[i] = _POSIX_VDISABLE;
231 /* Macros to clear/set/test flags. */
232 #define SET(t, f) (t) |= (f)
233 #define CLR(t, f) (t) &= ~(f)
234 #define ISSET(t, f) ((t) & (f))
239 tcflag_t iflag, oflag, cflag, lflag;
244 if (C0set && I0set && L0set && O0set) {
253 if (C1set && I1set && L1set && O1set) {
262 if (C2set && I2set && L2set && O2set) {
272 iflag = omode.c_iflag;
273 oflag = omode.c_oflag;
274 cflag = omode.c_cflag;
275 lflag = omode.c_lflag;
278 CLR(cflag, CSIZE|PARENB);
280 CLR(iflag, ISTRIP|INPCK|IGNPAR);
281 } else if (AP || EP || OP) {
283 SET(cflag, CS7|PARENB);
286 SET(iflag, INPCK|IGNPAR);
290 } else if (EP && !OP) {
291 SET(iflag, INPCK|IGNPAR);
295 } else if (AP || (EP && OP)) {
296 CLR(iflag, INPCK|IGNPAR);
299 } /* else, leave as is */
323 SET(oflag, ONLCR|OPOST);
330 SET(oflag, OXTABS|OPOST);
338 if (n == 1) { /* read mode flags */
342 CLR(cflag, CSIZE|PARENB);
390 tmode.c_iflag = iflag;
391 tmode.c_oflag = oflag;
392 tmode.c_cflag = cflag;
393 tmode.c_lflag = lflag;
399 unsigned delay; /* delay in ms */
404 * below are random guesses, I can't be bothered checking
407 struct delayval crdelay[] = {
416 struct delayval nldelay[] = {
417 { 1, NL1 }, /* special, calculated */
424 struct delayval bsdelay[] = {
429 struct delayval ffdelay[] = {
435 struct delayval tbdelay[] = {
438 { 3, XTABS }, /* this is expand tabs */
448 f = adelay(CD, crdelay);
449 f |= adelay(ND, nldelay);
450 f |= adelay(FD, ffdelay);
451 f |= adelay(TD, tbdelay);
452 f |= adelay(BD, bsdelay);
457 adelay(int ms, struct delayval *dp)
461 while (dp->delay && ms > dp->delay)
467 char editedhost[MAXHOSTNAMELEN];
470 edithost(const char *pattern)
476 if (pattern == NULL || *pattern == '\0')
478 if (regcomp(®ex, pattern, REG_EXTENDED) != 0)
481 match = calloc(regex.re_nsub + 1, sizeof(*match));
487 found = !regexec(®ex, HN, regex.re_nsub + 1, match, 0);
489 size_t subex, totalsize;
492 * We found a match. If there were no parenthesized
493 * subexpressions in the pattern, use entire matched
494 * string as ``editedhost''; otherwise use the first
495 * matched subexpression.
497 subex = !!regex.re_nsub;
498 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
499 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
500 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
507 * In case of any errors, or if the pattern did not match, pass
508 * the original hostname as is.
511 strlcpy(editedhost, HN, sizeof(editedhost));
514 static struct speedtab {
532 { 19, EXTA }, /* for people who say 19.2K */
535 { 7200, EXTB }, /* alternative */
550 for (sp = speedtab; sp->speed; sp++)
551 if (sp->speed == val)
554 return (B300); /* default in impossible cases */
560 static char termbuf[128] = "TERM=";
566 strlcat(termbuf, TT, sizeof(termbuf));
571 while ((q = strchr(q, ','))) {
583 * This speed select mechanism is written for the Develcon DATASWITCH.
584 * The Develcon sends a string of the form "B{speed}\n" at a predefined
585 * baud rate. This string indicates the user's actual speed.
586 * The routine below returns the terminal type mapped from derived speed.
588 static struct portselect {
592 { "B110", "std.110" },
593 { "B134", "std.134" },
594 { "B150", "std.150" },
595 { "B300", "std.300" },
596 { "B600", "std.600" },
597 { "B1200", "std.1200" },
598 { "B2400", "std.2400" },
599 { "B4800", "std.4800" },
600 { "B9600", "std.9600" },
601 { "B19200", "std.19200" },
609 const char *type = "default";
610 struct portselect *ps;
614 for (len = 0; len < sizeof (baud) - 1; len++) {
615 if (read(STDIN_FILENO, &c, 1) <= 0)
618 if (c == '\n' || c == '\r')
621 len = 0; /* in case of leading garbage */
625 for (ps = portspeeds; ps->ps_baud; ps++)
626 if (strcmp(ps->ps_baud, baud) == 0) {
630 sleep(2); /* wait for connection to complete */
635 * This auto-baud speed select mechanism is written for the Micom 600
636 * portselector. Selection is done by looking at how the character '\r'
637 * is garbled at the different speeds.
642 struct pollfd set[1];
643 struct timespec timeout;
645 const char *type = "9600-baud";
647 (void)tcflush(0, TCIOFLUSH);
648 set[0].fd = STDIN_FILENO;
649 set[0].events = POLLIN;
650 if (poll(set, 1, 5000) <= 0)
652 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
655 timeout.tv_nsec = 20000;
656 (void)nanosleep(&timeout, NULL);
657 (void)tcflush(0, TCIOFLUSH);
660 case 0200: /* 300-baud */
664 case 0346: /* 1200-baud */
668 case 015: /* 2400-baud */
673 default: /* 4800-baud */
677 case 0377: /* 9600-baud */