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[] =
45 #include <sys/ioctl.h>
46 #include <sys/param.h>
51 #include "pathnames.h"
60 gettable(const char *name, char *buf)
64 struct gettyflags *fp;
71 static int firsttime = 1;
73 dba[0] = _PATH_GETTYTAB;
78 * we need to strdup() anything in the strings array
79 * initially in order to simplify things later
81 for (sp = gettystrs; sp->field; sp++)
82 if (sp->value != NULL) {
83 /* handle these ones more carefully */
84 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
87 l = strlen(sp->value) + 1;
88 if ((p = malloc(l)) != NULL) {
89 strncpy(p, sp->value, l);
93 * replace, even if NULL, else we'll
94 * have problems with free()ing static mem
101 switch (cgetent(&buf, (char **)dba, (char *)name)) {
103 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
107 msg = "%s: unknown gettytab entry '%s'";
110 msg = "%s: retrieving gettytab entry '%s': %m";
113 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
116 msg = "%s: unexpected cgetent() error for entry '%s'";
121 syslog(LOG_ERR, msg, "getty", name);
125 for (sp = gettystrs; sp->field; sp++) {
126 if ((l = cgetstr(buf, (char*)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, (char*)np->field, &n) == -1)
152 for (fp = gettyflags; fp->field; fp++) {
153 if (cgetcap(buf, (char *)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 *pat)
472 const char *host = HN;
473 char *res = editedhost;
495 if (res == &editedhost[sizeof editedhost - 1]) {
502 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
505 editedhost[sizeof editedhost - 1] = '\0';
508 static struct speedtab {
526 { 19, EXTA }, /* for people who say 19.2K */
529 { 7200, EXTB }, /* alternative */
544 for (sp = speedtab; sp->speed; sp++)
545 if (sp->speed == val)
548 return (B300); /* default in impossible cases */
554 static char termbuf[128] = "TERM=";
560 strlcat(termbuf, TT, sizeof(termbuf));
565 while ((q = strchr(q, ','))) {
577 * This speed select mechanism is written for the Develcon DATASWITCH.
578 * The Develcon sends a string of the form "B{speed}\n" at a predefined
579 * baud rate. This string indicates the user's actual speed.
580 * The routine below returns the terminal type mapped from derived speed.
586 { "B110", "std.110" },
587 { "B134", "std.134" },
588 { "B150", "std.150" },
589 { "B300", "std.300" },
590 { "B600", "std.600" },
591 { "B1200", "std.1200" },
592 { "B2400", "std.2400" },
593 { "B4800", "std.4800" },
594 { "B9600", "std.9600" },
595 { "B19200", "std.19200" },
603 const char *type = "default";
604 struct portselect *ps;
608 for (len = 0; len < sizeof (baud) - 1; len++) {
609 if (read(STDIN_FILENO, &c, 1) <= 0)
612 if (c == '\n' || c == '\r')
615 len = 0; /* in case of leading garbage */
619 for (ps = portspeeds; ps->ps_baud; ps++)
620 if (strcmp(ps->ps_baud, baud) == 0) {
624 sleep(2); /* wait for connection to complete */
629 * This auto-baud speed select mechanism is written for the Micom 600
630 * portselector. Selection is done by looking at how the character '\r'
631 * is garbled at the different speeds.
637 struct timeval timeout;
639 const char *type = "9600-baud";
641 (void)tcflush(0, TCIOFLUSH);
645 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
646 (fd_set *)NULL, &timeout) <= 0)
648 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
651 timeout.tv_usec = 20;
652 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
653 (fd_set *)NULL, &timeout);
654 (void)tcflush(0, TCIOFLUSH);
657 case 0200: /* 300-baud */
661 case 0346: /* 1200-baud */
665 case 015: /* 2400-baud */
670 default: /* 4800-baud */
674 case 0377: /* 9600-baud */