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[] =
48 #include <sys/ioctl.h>
49 #include <sys/param.h>
54 #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;
165 printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
166 for (sp = gettystrs; sp->field; sp++)
167 printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
168 for (np = gettynums; np->field; np++)
169 printf("cgetnum: %s=%d\r\n", np->field, np->value);
170 for (fp = gettyflags; fp->field; fp++)
171 printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
172 fp->value + '0', fp->set + '0');
179 struct gettystrs *sp;
180 struct gettynums *np;
181 struct gettyflags *fp;
183 for (sp = gettystrs; sp->field; sp++)
185 sp->defalt = strdup(sp->value);
186 for (np = gettynums; np->field; np++)
188 np->defalt = np->value;
189 for (fp = gettyflags; fp->field; fp++)
191 fp->defalt = fp->value;
193 fp->defalt = fp->invrt;
199 struct gettystrs *sp;
200 struct gettynums *np;
201 struct gettyflags *fp;
203 for (sp = gettystrs; sp->field; sp++)
205 sp->value = !sp->defalt ? sp->defalt
206 : strdup(sp->defalt);
207 for (np = gettynums; np->field; np++)
209 np->value = np->defalt;
210 for (fp = gettyflags; fp->field; fp++)
212 fp->value = fp->defalt;
217 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
218 &SU, &DS, &RP, &FL, &WE, &LN, 0
223 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
224 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
225 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
226 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
227 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
236 for (i = 0; charnames[i]; i++) {
241 *charvars[i] = _POSIX_VDISABLE;
245 /* Macros to clear/set/test flags. */
246 #define SET(t, f) (t) |= (f)
247 #define CLR(t, f) (t) &= ~(f)
248 #define ISSET(t, f) ((t) & (f))
253 tcflag_t iflag, oflag, cflag, lflag;
258 if (C0set && I0set && L0set && O0set) {
267 if (C1set && I1set && L1set && O1set) {
276 if (C2set && I2set && L2set && O2set) {
286 iflag = omode.c_iflag;
287 oflag = omode.c_oflag;
288 cflag = omode.c_cflag;
289 lflag = omode.c_lflag;
292 CLR(cflag, CSIZE|PARENB);
294 CLR(iflag, ISTRIP|INPCK|IGNPAR);
295 } else if (AP || EP || OP) {
297 SET(cflag, CS7|PARENB);
300 SET(iflag, INPCK|IGNPAR);
304 } else if (EP && !OP) {
305 SET(iflag, INPCK|IGNPAR);
309 } else if (AP || (EP && OP)) {
310 CLR(iflag, INPCK|IGNPAR);
313 } /* else, leave as is */
337 SET(oflag, ONLCR|OPOST);
344 SET(oflag, OXTABS|OPOST);
352 if (n == 1) { /* read mode flags */
356 CLR(cflag, CSIZE|PARENB);
404 tmode.c_iflag = iflag;
405 tmode.c_oflag = oflag;
406 tmode.c_cflag = cflag;
407 tmode.c_lflag = lflag;
413 unsigned delay; /* delay in ms */
418 * below are random guesses, I can't be bothered checking
421 struct delayval crdelay[] = {
430 struct delayval nldelay[] = {
431 { 1, NL1 }, /* special, calculated */
438 struct delayval bsdelay[] = {
443 struct delayval ffdelay[] = {
449 struct delayval tbdelay[] = {
452 { 3, XTABS }, /* this is expand tabs */
462 f = adelay(CD, crdelay);
463 f |= adelay(ND, nldelay);
464 f |= adelay(FD, ffdelay);
465 f |= adelay(TD, tbdelay);
466 f |= adelay(BD, bsdelay);
471 adelay(int ms, struct delayval *dp)
475 while (dp->delay && ms > dp->delay)
481 char editedhost[MAXHOSTNAMELEN];
484 edithost(const char *pat)
486 const char *host = HN;
487 char *res = editedhost;
509 if (res == &editedhost[sizeof editedhost - 1]) {
516 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
519 editedhost[sizeof editedhost - 1] = '\0';
522 static struct speedtab {
540 { 19, EXTA }, /* for people who say 19.2K */
543 { 7200, EXTB }, /* alternative */
558 for (sp = speedtab; sp->speed; sp++)
559 if (sp->speed == val)
562 return (B300); /* default in impossible cases */
568 static char termbuf[128] = "TERM=";
574 strlcat(termbuf, TT, sizeof(termbuf));
579 while ((q = strchr(q, ','))) {
591 * This speed select mechanism is written for the Develcon DATASWITCH.
592 * The Develcon sends a string of the form "B{speed}\n" at a predefined
593 * baud rate. This string indicates the user's actual speed.
594 * The routine below returns the terminal type mapped from derived speed.
600 { "B110", "std.110" },
601 { "B134", "std.134" },
602 { "B150", "std.150" },
603 { "B300", "std.300" },
604 { "B600", "std.600" },
605 { "B1200", "std.1200" },
606 { "B2400", "std.2400" },
607 { "B4800", "std.4800" },
608 { "B9600", "std.9600" },
609 { "B19200", "std.19200" },
617 const char *type = "default";
618 struct portselect *ps;
622 for (len = 0; len < sizeof (baud) - 1; len++) {
623 if (read(STDIN_FILENO, &c, 1) <= 0)
626 if (c == '\n' || c == '\r')
629 len = 0; /* in case of leading garbage */
633 for (ps = portspeeds; ps->ps_baud; ps++)
634 if (strcmp(ps->ps_baud, baud) == 0) {
638 sleep(2); /* wait for connection to complete */
643 * This auto-baud speed select mechanism is written for the Micom 600
644 * portselector. Selection is done by looking at how the character '\r'
645 * is garbled at the different speeds.
651 struct timeval timeout;
653 const char *type = "9600-baud";
655 (void)tcflush(0, TCIOFLUSH);
659 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
660 (fd_set *)NULL, &timeout) <= 0)
662 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
665 timeout.tv_usec = 20;
666 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
667 (fd_set *)NULL, &timeout);
668 (void)tcflush(0, TCIOFLUSH);
671 case 0200: /* 300-baud */
675 case 0346: /* 1200-baud */
679 case 015: /* 2400-baud */
684 default: /* 4800-baud */
688 case 0377: /* 9600-baud */