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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93";
38 static const char rcsid[] =
52 #include <sys/ioctl.h>
53 #include <sys/param.h>
58 #include "pathnames.h"
67 gettable(const char *name, char *buf)
71 struct gettyflags *fp;
78 static int firsttime = 1;
80 dba[0] = _PATH_GETTYTAB;
85 * we need to strdup() anything in the strings array
86 * initially in order to simplify things later
88 for (sp = gettystrs; sp->field; sp++)
89 if (sp->value != NULL) {
90 /* handle these ones more carefully */
91 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
94 l = strlen(sp->value) + 1;
95 if ((p = malloc(l)) != NULL) {
96 strncpy(p, sp->value, l);
100 * replace, even if NULL, else we'll
101 * have problems with free()ing static mem
108 switch (cgetent(&buf, (char **)dba, (char *)name)) {
110 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
114 msg = "%s: unknown gettytab entry '%s'";
117 msg = "%s: retrieving gettytab entry '%s': %m";
120 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
123 msg = "%s: unexpected cgetent() error for entry '%s'";
128 syslog(LOG_ERR, msg, "getty", name);
132 for (sp = gettystrs; sp->field; sp++) {
133 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
135 /* prefer existing value */
136 if (strcmp(p, sp->value) != 0)
144 } else if (l == -1) {
150 for (np = gettynums; np->field; np++) {
151 if (cgetnum(buf, (char*)np->field, &n) == -1)
159 for (fp = gettyflags; fp->field; fp++) {
160 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
164 fp->value = 1 ^ fp->invrt;
169 printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
170 for (sp = gettystrs; sp->field; sp++)
171 printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
172 for (np = gettynums; np->field; np++)
173 printf("cgetnum: %s=%d\r\n", np->field, np->value);
174 for (fp = gettyflags; fp->field; fp++)
175 printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
176 fp->value + '0', fp->set + '0');
183 struct gettystrs *sp;
184 struct gettynums *np;
185 struct gettyflags *fp;
187 for (sp = gettystrs; sp->field; sp++)
189 sp->defalt = strdup(sp->value);
190 for (np = gettynums; np->field; np++)
192 np->defalt = np->value;
193 for (fp = gettyflags; fp->field; fp++)
195 fp->defalt = fp->value;
197 fp->defalt = fp->invrt;
203 struct gettystrs *sp;
204 struct gettynums *np;
205 struct gettyflags *fp;
207 for (sp = gettystrs; sp->field; sp++)
209 sp->value = !sp->defalt ? sp->defalt
210 : strdup(sp->defalt);
211 for (np = gettynums; np->field; np++)
213 np->value = np->defalt;
214 for (fp = gettyflags; fp->field; fp++)
216 fp->value = fp->defalt;
221 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
222 &SU, &DS, &RP, &FL, &WE, &LN, 0
227 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
228 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
229 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
230 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
231 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
240 for (i = 0; charnames[i]; i++) {
245 *charvars[i] = _POSIX_VDISABLE;
249 /* Macros to clear/set/test flags. */
250 #define SET(t, f) (t) |= (f)
251 #define CLR(t, f) (t) &= ~(f)
252 #define ISSET(t, f) ((t) & (f))
257 tcflag_t iflag, oflag, cflag, lflag;
262 if (C0set && I0set && L0set && O0set) {
271 if (C1set && I1set && L1set && O1set) {
280 if (C2set && I2set && L2set && O2set) {
290 iflag = omode.c_iflag;
291 oflag = omode.c_oflag;
292 cflag = omode.c_cflag;
293 lflag = omode.c_lflag;
296 CLR(cflag, CSIZE|PARENB);
298 CLR(iflag, ISTRIP|INPCK|IGNPAR);
299 } else if (AP || EP || OP) {
301 SET(cflag, CS7|PARENB);
304 SET(iflag, INPCK|IGNPAR);
308 } else if (EP && !OP) {
309 SET(iflag, INPCK|IGNPAR);
313 } else if (AP || (EP && OP)) {
314 CLR(iflag, INPCK|IGNPAR);
317 } /* else, leave as is */
341 SET(oflag, ONLCR|OPOST);
348 SET(oflag, OXTABS|OPOST);
356 if (n == 1) { /* read mode flags */
360 CLR(cflag, CSIZE|PARENB);
408 tmode.c_iflag = iflag;
409 tmode.c_oflag = oflag;
410 tmode.c_cflag = cflag;
411 tmode.c_lflag = lflag;
417 unsigned delay; /* delay in ms */
422 * below are random guesses, I can't be bothered checking
425 struct delayval crdelay[] = {
434 struct delayval nldelay[] = {
435 { 1, NL1 }, /* special, calculated */
442 struct delayval bsdelay[] = {
447 struct delayval ffdelay[] = {
453 struct delayval tbdelay[] = {
456 { 3, XTABS }, /* this is expand tabs */
466 f = adelay(CD, crdelay);
467 f |= adelay(ND, nldelay);
468 f |= adelay(FD, ffdelay);
469 f |= adelay(TD, tbdelay);
470 f |= adelay(BD, bsdelay);
475 adelay(int ms, struct delayval *dp)
479 while (dp->delay && ms > dp->delay)
485 char editedhost[MAXHOSTNAMELEN];
488 edithost(const char *pat)
490 const char *host = HN;
491 char *res = editedhost;
513 if (res == &editedhost[sizeof editedhost - 1]) {
520 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
523 editedhost[sizeof editedhost - 1] = '\0';
526 static struct speedtab {
544 { 19, EXTA }, /* for people who say 19.2K */
547 { 7200, EXTB }, /* alternative */
562 for (sp = speedtab; sp->speed; sp++)
563 if (sp->speed == val)
566 return (B300); /* default in impossible cases */
572 static char termbuf[128] = "TERM=";
578 strlcat(termbuf, TT, sizeof(termbuf));
583 while ((q = strchr(q, ','))) {
595 * This speed select mechanism is written for the Develcon DATASWITCH.
596 * The Develcon sends a string of the form "B{speed}\n" at a predefined
597 * baud rate. This string indicates the user's actual speed.
598 * The routine below returns the terminal type mapped from derived speed.
604 { "B110", "std.110" },
605 { "B134", "std.134" },
606 { "B150", "std.150" },
607 { "B300", "std.300" },
608 { "B600", "std.600" },
609 { "B1200", "std.1200" },
610 { "B2400", "std.2400" },
611 { "B4800", "std.4800" },
612 { "B9600", "std.9600" },
613 { "B19200", "std.19200" },
621 const char *type = "default";
622 struct portselect *ps;
626 for (len = 0; len < sizeof (baud) - 1; len++) {
627 if (read(STDIN_FILENO, &c, 1) <= 0)
630 if (c == '\n' || c == '\r')
633 len = 0; /* in case of leading garbage */
637 for (ps = portspeeds; ps->ps_baud; ps++)
638 if (strcmp(ps->ps_baud, baud) == 0) {
642 sleep(2); /* wait for connection to complete */
647 * This auto-baud speed select mechanism is written for the Micom 600
648 * portselector. Selection is done by looking at how the character '\r'
649 * is garbled at the different speeds.
655 struct timeval timeout;
657 const char *type = "9600-baud";
659 (void)tcflush(0, TCIOFLUSH);
663 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
664 (fd_set *)NULL, &timeout) <= 0)
666 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
669 timeout.tv_usec = 20;
670 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
671 (fd_set *)NULL, &timeout);
672 (void)tcflush(0, TCIOFLUSH);
675 case 0200: /* 300-baud */
679 case 0346: /* 1200-baud */
683 case 015: /* 2400-baud */
688 default: /* 4800-baud */
692 case 0377: /* 9600-baud */