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>
53 #include "pathnames.h"
62 gettable(const char *name, char *buf)
66 struct gettyflags *fp;
73 static int firsttime = 1;
75 dba[0] = _PATH_GETTYTAB;
80 * we need to strdup() anything in the strings array
81 * initially in order to simplify things later
83 for (sp = gettystrs; sp->field; sp++)
84 if (sp->value != NULL) {
85 /* handle these ones more carefully */
86 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
89 l = strlen(sp->value) + 1;
90 if ((p = malloc(l)) != NULL) {
91 strncpy(p, sp->value, l);
95 * replace, even if NULL, else we'll
96 * have problems with free()ing static mem
103 switch (cgetent(&buf, (char **)dba, (char *)name)) {
105 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
109 msg = "%s: unknown gettytab entry '%s'";
112 msg = "%s: retrieving gettytab entry '%s': %m";
115 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
118 msg = "%s: unexpected cgetent() error for entry '%s'";
123 syslog(LOG_ERR, msg, "getty", name);
127 for (sp = gettystrs; sp->field; sp++) {
128 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
130 /* prefer existing value */
131 if (strcmp(p, sp->value) != 0)
139 } else if (l == -1) {
145 for (np = gettynums; np->field; np++) {
146 if (cgetnum(buf, (char*)np->field, &n) == -1)
154 for (fp = gettyflags; fp->field; fp++) {
155 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
159 fp->value = 1 ^ fp->invrt;
167 struct gettystrs *sp;
168 struct gettynums *np;
169 struct gettyflags *fp;
171 for (sp = gettystrs; sp->field; sp++)
173 sp->defalt = strdup(sp->value);
174 for (np = gettynums; np->field; np++)
176 np->defalt = np->value;
177 for (fp = gettyflags; fp->field; fp++)
179 fp->defalt = fp->value;
181 fp->defalt = fp->invrt;
187 struct gettystrs *sp;
188 struct gettynums *np;
189 struct gettyflags *fp;
191 for (sp = gettystrs; sp->field; sp++)
193 sp->value = !sp->defalt ? sp->defalt
194 : strdup(sp->defalt);
195 for (np = gettynums; np->field; np++)
197 np->value = np->defalt;
198 for (fp = gettyflags; fp->field; fp++)
200 fp->value = fp->defalt;
205 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
206 &SU, &DS, &RP, &FL, &WE, &LN, 0
211 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
212 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
213 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
214 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
215 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
224 for (i = 0; charnames[i]; i++) {
229 *charvars[i] = _POSIX_VDISABLE;
233 /* Macros to clear/set/test flags. */
234 #define SET(t, f) (t) |= (f)
235 #define CLR(t, f) (t) &= ~(f)
236 #define ISSET(t, f) ((t) & (f))
241 tcflag_t iflag, oflag, cflag, lflag;
246 if (C0set && I0set && L0set && O0set) {
255 if (C1set && I1set && L1set && O1set) {
264 if (C2set && I2set && L2set && O2set) {
274 iflag = omode.c_iflag;
275 oflag = omode.c_oflag;
276 cflag = omode.c_cflag;
277 lflag = omode.c_lflag;
280 CLR(cflag, CSIZE|PARENB);
282 CLR(iflag, ISTRIP|INPCK|IGNPAR);
283 } else if (AP || EP || OP) {
285 SET(cflag, CS7|PARENB);
288 SET(iflag, INPCK|IGNPAR);
292 } else if (EP && !OP) {
293 SET(iflag, INPCK|IGNPAR);
297 } else if (AP || (EP && OP)) {
298 CLR(iflag, INPCK|IGNPAR);
301 } /* else, leave as is */
325 SET(oflag, ONLCR|OPOST);
332 SET(oflag, OXTABS|OPOST);
340 if (n == 1) { /* read mode flags */
344 CLR(cflag, CSIZE|PARENB);
392 tmode.c_iflag = iflag;
393 tmode.c_oflag = oflag;
394 tmode.c_cflag = cflag;
395 tmode.c_lflag = lflag;
401 unsigned delay; /* delay in ms */
406 * below are random guesses, I can't be bothered checking
409 struct delayval crdelay[] = {
418 struct delayval nldelay[] = {
419 { 1, NL1 }, /* special, calculated */
426 struct delayval bsdelay[] = {
431 struct delayval ffdelay[] = {
437 struct delayval tbdelay[] = {
440 { 3, XTABS }, /* this is expand tabs */
450 f = adelay(CD, crdelay);
451 f |= adelay(ND, nldelay);
452 f |= adelay(FD, ffdelay);
453 f |= adelay(TD, tbdelay);
454 f |= adelay(BD, bsdelay);
459 adelay(int ms, struct delayval *dp)
463 while (dp->delay && ms > dp->delay)
469 char editedhost[MAXHOSTNAMELEN];
472 edithost(const char *pat)
474 const char *host = HN;
475 char *res = editedhost;
497 if (res == &editedhost[sizeof editedhost - 1]) {
504 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
507 editedhost[sizeof editedhost - 1] = '\0';
510 static struct speedtab {
528 { 19, EXTA }, /* for people who say 19.2K */
531 { 7200, EXTB }, /* alternative */
546 for (sp = speedtab; sp->speed; sp++)
547 if (sp->speed == val)
550 return (B300); /* default in impossible cases */
556 static char termbuf[128] = "TERM=";
562 strlcat(termbuf, TT, sizeof(termbuf));
567 while ((q = strchr(q, ','))) {
579 * This speed select mechanism is written for the Develcon DATASWITCH.
580 * The Develcon sends a string of the form "B{speed}\n" at a predefined
581 * baud rate. This string indicates the user's actual speed.
582 * The routine below returns the terminal type mapped from derived speed.
588 { "B110", "std.110" },
589 { "B134", "std.134" },
590 { "B150", "std.150" },
591 { "B300", "std.300" },
592 { "B600", "std.600" },
593 { "B1200", "std.1200" },
594 { "B2400", "std.2400" },
595 { "B4800", "std.4800" },
596 { "B9600", "std.9600" },
597 { "B19200", "std.19200" },
605 const char *type = "default";
606 struct portselect *ps;
610 for (len = 0; len < sizeof (baud) - 1; len++) {
611 if (read(STDIN_FILENO, &c, 1) <= 0)
614 if (c == '\n' || c == '\r')
617 len = 0; /* in case of leading garbage */
621 for (ps = portspeeds; ps->ps_baud; ps++)
622 if (strcmp(ps->ps_baud, baud) == 0) {
626 sleep(2); /* wait for connection to complete */
631 * This auto-baud speed select mechanism is written for the Micom 600
632 * portselector. Selection is done by looking at how the character '\r'
633 * is garbled at the different speeds.
638 struct pollfd set[1];
639 struct timespec timeout;
641 const char *type = "9600-baud";
643 (void)tcflush(0, TCIOFLUSH);
644 set[0].fd = STDIN_FILENO;
645 set[0].events = POLLIN;
646 if (poll(set, 1, 5000) <= 0)
648 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
651 timeout.tv_nsec = 20000;
652 (void)nanosleep(&timeout, NULL);
653 (void)tcflush(0, TCIOFLUSH);
656 case 0200: /* 300-baud */
660 case 0346: /* 1200-baud */
664 case 015: /* 2400-baud */
669 default: /* 4800-baud */
673 case 0377: /* 9600-baud */