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[] =
39 "$FreeBSD: src/libexec/getty/subr.c,v 1.16.2.1 2001/05/12 10:16:51 kris Exp $";
53 #include <sys/ioctl.h>
54 #include <sys/param.h>
59 #include "pathnames.h"
64 static void compatflags __P((long));
75 register struct gettystrs *sp;
76 register struct gettynums *np;
77 register struct gettyflags *fp;
84 static int firsttime = 1;
86 dba[0] = _PATH_GETTYTAB;
91 * we need to strdup() anything in the strings array
92 * initially in order to simplify things later
94 for (sp = gettystrs; sp->field; sp++)
95 if (sp->value != NULL) {
96 /* handle these ones more carefully */
97 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
100 l = strlen(sp->value) + 1;
101 if ((p = malloc(l)) != NULL) {
102 strncpy(p, sp->value, l);
106 * replace, even if NULL, else we'll
107 * have problems with free()ing static mem
114 switch (cgetent(&buf, (char **)dba, (char *)name)) {
116 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
120 msg = "%s: unknown gettytab entry '%s'";
123 msg = "%s: retrieving gettytab entry '%s': %m";
126 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
129 msg = "%s: unexpected cgetent() error for entry '%s'";
134 syslog(LOG_ERR, msg, "getty", name);
138 for (sp = gettystrs; sp->field; sp++) {
139 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
141 /* prefer existing value */
142 if (strcmp(p, sp->value) != 0)
150 } else if (l == -1) {
156 for (np = gettynums; np->field; np++) {
157 if (cgetnum(buf, (char*)np->field, &n) == -1)
165 for (fp = gettyflags; fp->field; fp++) {
166 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
170 fp->value = 1 ^ fp->invrt;
175 printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
176 for (sp = gettystrs; sp->field; sp++)
177 printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
178 for (np = gettynums; np->field; np++)
179 printf("cgetnum: %s=%d\r\n", np->field, np->value);
180 for (fp = gettyflags; fp->field; fp++)
181 printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
182 fp->value + '0', fp->set + '0');
189 register struct gettystrs *sp;
190 register struct gettynums *np;
191 register struct gettyflags *fp;
193 for (sp = gettystrs; sp->field; sp++)
195 sp->defalt = strdup(sp->value);
196 for (np = gettynums; np->field; np++)
198 np->defalt = np->value;
199 for (fp = gettyflags; fp->field; fp++)
201 fp->defalt = fp->value;
203 fp->defalt = fp->invrt;
209 register struct gettystrs *sp;
210 register struct gettynums *np;
211 register struct gettyflags *fp;
213 for (sp = gettystrs; sp->field; sp++)
215 sp->value = !sp->defalt ? sp->defalt
216 : strdup(sp->defalt);
217 for (np = gettynums; np->field; np++)
219 np->value = np->defalt;
220 for (fp = gettyflags; fp->field; fp++)
222 fp->value = fp->defalt;
227 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
228 &SU, &DS, &RP, &FL, &WE, &LN, 0
233 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
234 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
235 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
236 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
237 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
244 register const char *p;
246 for (i = 0; charnames[i]; i++) {
251 *charvars[i] = _POSIX_VDISABLE;
255 /* Macros to clear/set/test flags. */
256 #define SET(t, f) (t) |= (f)
257 #define CLR(t, f) (t) &= ~(f)
258 #define ISSET(t, f) ((t) & (f))
264 register tcflag_t iflag, oflag, cflag, lflag;
291 if (C0set && I0set && L0set && O0set) {
300 if (C1set && I1set && L1set && O1set) {
309 if (C2set && I2set && L2set && O2set) {
319 iflag = omode.c_iflag;
320 oflag = omode.c_oflag;
321 cflag = omode.c_cflag;
322 lflag = omode.c_lflag;
325 CLR(cflag, CSIZE|PARENB);
327 CLR(iflag, ISTRIP|INPCK|IGNPAR);
328 } else if (AP || EP || OP) {
330 SET(cflag, CS7|PARENB);
333 SET(iflag, INPCK|IGNPAR);
337 } else if (EP && !OP) {
338 SET(iflag, INPCK|IGNPAR);
342 } else if (AP || (EP && OP)) {
343 CLR(iflag, INPCK|IGNPAR);
346 } /* else, leave as is */
370 SET(oflag, ONLCR|OPOST);
377 SET(oflag, OXTABS|OPOST);
385 if (n == 1) { /* read mode flags */
389 CLR(cflag, CSIZE|PARENB);
437 tmode.c_iflag = iflag;
438 tmode.c_oflag = oflag;
439 tmode.c_cflag = cflag;
440 tmode.c_lflag = lflag;
445 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
451 register tcflag_t iflag, oflag, cflag, lflag;
453 iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
454 oflag = OPOST|ONLCR|OXTABS;
456 lflag = ICANON|ISIG|IEXTEN;
458 if (ISSET(flags, TANDEM))
462 if (ISSET(flags, ECHO))
466 if (ISSET(flags, CRMOD)) {
473 if (ISSET(flags, XTABS))
479 if (ISSET(flags, RAW)) {
481 CLR(lflag, ISIG|ICANON|IEXTEN);
484 SET(iflag, BRKINT|IXON|IMAXBEL);
485 SET(lflag, ISIG|IEXTEN);
486 if (ISSET(flags, CBREAK))
490 switch (ISSET(flags, ANYP)) {
511 /* Nothing we can do with CRTBS. */
512 if (ISSET(flags, PRTERA))
516 if (ISSET(flags, CRTERA))
520 /* Nothing we can do with TILDE. */
521 if (ISSET(flags, MDMBUF))
525 if (ISSET(flags, NOHANG))
529 if (ISSET(flags, CRTKIL))
533 if (ISSET(flags, CTLECH))
537 if (!ISSET(flags, DECCTQ))
541 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
542 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
544 if (ISSET(flags, RAW|LITOUT|PASS8)) {
547 if (!ISSET(flags, RAW|PASS8))
551 if (!ISSET(flags, RAW|LITOUT))
562 tmode.c_iflag = iflag;
563 tmode.c_oflag = oflag;
564 tmode.c_cflag = cflag;
565 tmode.c_lflag = lflag;
571 unsigned delay; /* delay in ms */
576 * below are random guesses, I can't be bothered checking
579 struct delayval crdelay[] = {
588 struct delayval nldelay[] = {
589 { 1, NL1 }, /* special, calculated */
596 struct delayval bsdelay[] = {
601 struct delayval ffdelay[] = {
607 struct delayval tbdelay[] = {
610 { 3, XTABS }, /* this is expand tabs */
620 f = adelay(CD, crdelay);
621 f |= adelay(ND, nldelay);
622 f |= adelay(FD, ffdelay);
623 f |= adelay(TD, tbdelay);
624 f |= adelay(BD, bsdelay);
631 register struct delayval *dp;
635 while (dp->delay && ms > dp->delay)
641 char editedhost[MAXHOSTNAMELEN];
645 register const char *pat;
647 register const char *host = HN;
648 register char *res = editedhost;
670 if (res == &editedhost[sizeof editedhost - 1]) {
677 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
680 editedhost[sizeof editedhost - 1] = '\0';
683 static struct speedtab {
701 { 19, EXTA }, /* for people who say 19.2K */
704 { 7200, EXTB }, /* alternative */
715 register struct speedtab *sp;
720 for (sp = speedtab; sp->speed; sp++)
721 if (sp->speed == val)
724 return (B300); /* default in impossible cases */
731 static char termbuf[128] = "TERM=";
732 register char *p, *q;
737 strlcat(termbuf, TT, sizeof(termbuf));
742 while ((q = strchr(q, ','))) {
754 * This speed select mechanism is written for the Develcon DATASWITCH.
755 * The Develcon sends a string of the form "B{speed}\n" at a predefined
756 * baud rate. This string indicates the user's actual speed.
757 * The routine below returns the terminal type mapped from derived speed.
763 { "B110", "std.110" },
764 { "B134", "std.134" },
765 { "B150", "std.150" },
766 { "B300", "std.300" },
767 { "B600", "std.600" },
768 { "B1200", "std.1200" },
769 { "B2400", "std.2400" },
770 { "B4800", "std.4800" },
771 { "B9600", "std.9600" },
772 { "B19200", "std.19200" },
780 const char *type = "default";
781 register struct portselect *ps;
785 for (len = 0; len < sizeof (baud) - 1; len++) {
786 if (read(STDIN_FILENO, &c, 1) <= 0)
789 if (c == '\n' || c == '\r')
792 len = 0; /* in case of leading garbage */
796 for (ps = portspeeds; ps->ps_baud; ps++)
797 if (strcmp(ps->ps_baud, baud) == 0) {
801 sleep(2); /* wait for connection to complete */
806 * This auto-baud speed select mechanism is written for the Micom 600
807 * portselector. Selection is done by looking at how the character '\r'
808 * is garbled at the different speeds.
814 struct timeval timeout;
816 const char *type = "9600-baud";
818 (void)tcflush(0, TCIOFLUSH);
822 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
823 (fd_set *)NULL, &timeout) <= 0)
825 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
828 timeout.tv_usec = 20;
829 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
830 (fd_set *)NULL, &timeout);
831 (void)tcflush(0, TCIOFLUSH);
834 case 0200: /* 300-baud */
838 case 0346: /* 1200-baud */
842 case 015: /* 2400-baud */
847 default: /* 4800-baud */
851 case 0377: /* 9600-baud */