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
32 static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93";
34 static const char rcsid[] =
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
54 #include "pathnames.h"
61 gettable(const char *name, char *buf)
65 struct gettyflags *fp;
72 static int firsttime = 1;
74 dba[0] = _PATH_GETTYTAB;
79 * we need to strdup() anything in the strings array
80 * initially in order to simplify things later
82 for (sp = gettystrs; sp->field; sp++)
83 if (sp->value != NULL) {
84 /* handle these ones more carefully */
85 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
88 l = strlen(sp->value) + 1;
89 if ((p = malloc(l)) != NULL) {
90 strncpy(p, sp->value, l);
94 * replace, even if NULL, else we'll
95 * have problems with free()ing static mem
102 switch (cgetent(&buf, (char **)dba, (char *)name)) {
104 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
108 msg = "%s: unknown gettytab entry '%s'";
111 msg = "%s: retrieving gettytab entry '%s': %m";
114 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
117 msg = "%s: unexpected cgetent() error for entry '%s'";
122 syslog(LOG_ERR, msg, "getty", name);
126 for (sp = gettystrs; sp->field; sp++) {
127 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
129 /* prefer existing value */
130 if (strcmp(p, sp->value) != 0)
138 } else if (l == -1) {
144 for (np = gettynums; np->field; np++) {
145 if (cgetnum(buf, (char*)np->field, &n) == -1)
153 for (fp = gettyflags; fp->field; fp++) {
154 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
158 fp->value = 1 ^ fp->invrt;
166 struct gettystrs *sp;
167 struct gettynums *np;
168 struct gettyflags *fp;
170 for (sp = gettystrs; sp->field; sp++)
172 sp->defalt = strdup(sp->value);
173 for (np = gettynums; np->field; np++)
175 np->defalt = np->value;
176 for (fp = gettyflags; fp->field; fp++)
178 fp->defalt = fp->value;
180 fp->defalt = fp->invrt;
186 struct gettystrs *sp;
187 struct gettynums *np;
188 struct gettyflags *fp;
190 for (sp = gettystrs; sp->field; sp++)
192 sp->value = !sp->defalt ? sp->defalt
193 : strdup(sp->defalt);
194 for (np = gettynums; np->field; np++)
196 np->value = np->defalt;
197 for (fp = gettyflags; fp->field; fp++)
199 fp->value = fp->defalt;
204 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
205 &SU, &DS, &RP, &FL, &WE, &LN, 0
210 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
211 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
212 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
213 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
214 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
223 for (i = 0; charnames[i]; i++) {
228 *charvars[i] = _POSIX_VDISABLE;
232 /* Macros to clear/set/test flags. */
233 #define SET(t, f) (t) |= (f)
234 #define CLR(t, f) (t) &= ~(f)
235 #define ISSET(t, f) ((t) & (f))
240 tcflag_t iflag, oflag, cflag, lflag;
245 if (C0set && I0set && L0set && O0set) {
254 if (C1set && I1set && L1set && O1set) {
263 if (C2set && I2set && L2set && O2set) {
273 iflag = omode.c_iflag;
274 oflag = omode.c_oflag;
275 cflag = omode.c_cflag;
276 lflag = omode.c_lflag;
279 CLR(cflag, CSIZE|PARENB);
281 CLR(iflag, ISTRIP|INPCK|IGNPAR);
282 } else if (AP || EP || OP) {
284 SET(cflag, CS7|PARENB);
287 SET(iflag, INPCK|IGNPAR);
291 } else if (EP && !OP) {
292 SET(iflag, INPCK|IGNPAR);
296 } else if (AP || (EP && OP)) {
297 CLR(iflag, INPCK|IGNPAR);
300 } /* else, leave as is */
324 SET(oflag, ONLCR|OPOST);
331 SET(oflag, OXTABS|OPOST);
339 if (n == 1) { /* read mode flags */
343 CLR(cflag, CSIZE|PARENB);
391 tmode.c_iflag = iflag;
392 tmode.c_oflag = oflag;
393 tmode.c_cflag = cflag;
394 tmode.c_lflag = lflag;
400 unsigned delay; /* delay in ms */
405 * below are random guesses, I can't be bothered checking
408 struct delayval crdelay[] = {
417 struct delayval nldelay[] = {
418 { 1, NL1 }, /* special, calculated */
425 struct delayval bsdelay[] = {
430 struct delayval ffdelay[] = {
436 struct delayval tbdelay[] = {
439 { 3, XTABS }, /* this is expand tabs */
449 f = adelay(CD, crdelay);
450 f |= adelay(ND, nldelay);
451 f |= adelay(FD, ffdelay);
452 f |= adelay(TD, tbdelay);
453 f |= adelay(BD, bsdelay);
458 adelay(int ms, struct delayval *dp)
462 while (dp->delay && ms > dp->delay)
468 char editedhost[MAXHOSTNAMELEN];
471 edithost(const char *pattern)
477 if (pattern == NULL || *pattern == '\0')
479 if (regcomp(®ex, pattern, REG_EXTENDED) != 0)
482 match = calloc(regex.re_nsub + 1, sizeof(*match));
488 found = !regexec(®ex, HN, regex.re_nsub + 1, match, 0);
490 size_t subex, totalsize;
493 * We found a match. If there were no parenthesized
494 * subexpressions in the pattern, use entire matched
495 * string as ``editedhost''; otherwise use the first
496 * matched subexpression.
498 subex = !!regex.re_nsub;
499 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
500 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
501 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
508 * In case of any errors, or if the pattern did not match, pass
509 * the original hostname as is.
512 strlcpy(editedhost, HN, sizeof(editedhost));
515 static struct speedtab {
533 { 19, EXTA }, /* for people who say 19.2K */
536 { 7200, EXTB }, /* alternative */
551 for (sp = speedtab; sp->speed; sp++)
552 if (sp->speed == val)
555 return (B300); /* default in impossible cases */
561 static char termbuf[128] = "TERM=";
567 strlcat(termbuf, TT, sizeof(termbuf));
572 while ((q = strchr(q, ','))) {
584 * This speed select mechanism is written for the Develcon DATASWITCH.
585 * The Develcon sends a string of the form "B{speed}\n" at a predefined
586 * baud rate. This string indicates the user's actual speed.
587 * The routine below returns the terminal type mapped from derived speed.
593 { "B110", "std.110" },
594 { "B134", "std.134" },
595 { "B150", "std.150" },
596 { "B300", "std.300" },
597 { "B600", "std.600" },
598 { "B1200", "std.1200" },
599 { "B2400", "std.2400" },
600 { "B4800", "std.4800" },
601 { "B9600", "std.9600" },
602 { "B19200", "std.19200" },
610 const char *type = "default";
611 struct portselect *ps;
615 for (len = 0; len < sizeof (baud) - 1; len++) {
616 if (read(STDIN_FILENO, &c, 1) <= 0)
619 if (c == '\n' || c == '\r')
622 len = 0; /* in case of leading garbage */
626 for (ps = portspeeds; ps->ps_baud; ps++)
627 if (strcmp(ps->ps_baud, baud) == 0) {
631 sleep(2); /* wait for connection to complete */
636 * This auto-baud speed select mechanism is written for the Micom 600
637 * portselector. Selection is done by looking at how the character '\r'
638 * is garbled at the different speeds.
643 struct pollfd set[1];
644 struct timespec timeout;
646 const char *type = "9600-baud";
648 (void)tcflush(0, TCIOFLUSH);
649 set[0].fd = STDIN_FILENO;
650 set[0].events = POLLIN;
651 if (poll(set, 1, 5000) <= 0)
653 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
656 timeout.tv_nsec = 20000;
657 (void)nanosleep(&timeout, NULL);
658 (void)tcflush(0, TCIOFLUSH);
661 case 0200: /* 300-baud */
665 case 0346: /* 1200-baud */
669 case 015: /* 2400-baud */
674 default: /* 4800-baud */
678 case 0377: /* 9600-baud */