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
33 * @(#)from: subr.c 8.1 (Berkeley) 6/4/93
34 * $FreeBSD: src/libexec/getty/subr.c,v 1.16.2.1 2001/05/12 10:16:51 kris Exp $
35 * $DragonFly: src/libexec/getty/subr.c,v 1.3 2003/11/14 03:54:30 dillon Exp $
49 #include <sys/ioctl.h>
50 #include <sys/param.h>
55 #include "pathnames.h"
60 static void compatflags (long);
71 register struct gettystrs *sp;
72 register struct gettynums *np;
73 register struct gettyflags *fp;
80 static int firsttime = 1;
82 dba[0] = _PATH_GETTYTAB;
87 * we need to strdup() anything in the strings array
88 * initially in order to simplify things later
90 for (sp = gettystrs; sp->field; sp++)
91 if (sp->value != NULL) {
92 /* handle these ones more carefully */
93 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
96 l = strlen(sp->value) + 1;
97 if ((p = malloc(l)) != NULL) {
98 strncpy(p, sp->value, l);
102 * replace, even if NULL, else we'll
103 * have problems with free()ing static mem
110 switch (cgetent(&buf, (char **)dba, (char *)name)) {
112 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
116 msg = "%s: unknown gettytab entry '%s'";
119 msg = "%s: retrieving gettytab entry '%s': %m";
122 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
125 msg = "%s: unexpected cgetent() error for entry '%s'";
130 syslog(LOG_ERR, msg, "getty", name);
134 for (sp = gettystrs; sp->field; sp++) {
135 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
137 /* prefer existing value */
138 if (strcmp(p, sp->value) != 0)
146 } else if (l == -1) {
152 for (np = gettynums; np->field; np++) {
153 if (cgetnum(buf, (char*)np->field, &n) == -1)
161 for (fp = gettyflags; fp->field; fp++) {
162 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
166 fp->value = 1 ^ fp->invrt;
171 printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
172 for (sp = gettystrs; sp->field; sp++)
173 printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
174 for (np = gettynums; np->field; np++)
175 printf("cgetnum: %s=%d\r\n", np->field, np->value);
176 for (fp = gettyflags; fp->field; fp++)
177 printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
178 fp->value + '0', fp->set + '0');
185 register struct gettystrs *sp;
186 register struct gettynums *np;
187 register struct gettyflags *fp;
189 for (sp = gettystrs; sp->field; sp++)
191 sp->defalt = strdup(sp->value);
192 for (np = gettynums; np->field; np++)
194 np->defalt = np->value;
195 for (fp = gettyflags; fp->field; fp++)
197 fp->defalt = fp->value;
199 fp->defalt = fp->invrt;
205 register struct gettystrs *sp;
206 register struct gettynums *np;
207 register struct gettyflags *fp;
209 for (sp = gettystrs; sp->field; sp++)
211 sp->value = !sp->defalt ? sp->defalt
212 : strdup(sp->defalt);
213 for (np = gettynums; np->field; np++)
215 np->value = np->defalt;
216 for (fp = gettyflags; fp->field; fp++)
218 fp->value = fp->defalt;
223 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
224 &SU, &DS, &RP, &FL, &WE, &LN, 0
229 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
230 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
231 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
232 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
233 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
240 register const char *p;
242 for (i = 0; charnames[i]; i++) {
247 *charvars[i] = _POSIX_VDISABLE;
251 /* Macros to clear/set/test flags. */
252 #define SET(t, f) (t) |= (f)
253 #define CLR(t, f) (t) &= ~(f)
254 #define ISSET(t, f) ((t) & (f))
260 register tcflag_t iflag, oflag, cflag, lflag;
287 if (C0set && I0set && L0set && O0set) {
296 if (C1set && I1set && L1set && O1set) {
305 if (C2set && I2set && L2set && O2set) {
315 iflag = omode.c_iflag;
316 oflag = omode.c_oflag;
317 cflag = omode.c_cflag;
318 lflag = omode.c_lflag;
321 CLR(cflag, CSIZE|PARENB);
323 CLR(iflag, ISTRIP|INPCK|IGNPAR);
324 } else if (AP || EP || OP) {
326 SET(cflag, CS7|PARENB);
329 SET(iflag, INPCK|IGNPAR);
333 } else if (EP && !OP) {
334 SET(iflag, INPCK|IGNPAR);
338 } else if (AP || (EP && OP)) {
339 CLR(iflag, INPCK|IGNPAR);
342 } /* else, leave as is */
366 SET(oflag, ONLCR|OPOST);
373 SET(oflag, OXTABS|OPOST);
381 if (n == 1) { /* read mode flags */
385 CLR(cflag, CSIZE|PARENB);
433 tmode.c_iflag = iflag;
434 tmode.c_oflag = oflag;
435 tmode.c_cflag = cflag;
436 tmode.c_lflag = lflag;
441 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
447 register tcflag_t iflag, oflag, cflag, lflag;
449 iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
450 oflag = OPOST|ONLCR|OXTABS;
452 lflag = ICANON|ISIG|IEXTEN;
454 if (ISSET(flags, TANDEM))
458 if (ISSET(flags, ECHO))
462 if (ISSET(flags, CRMOD)) {
469 if (ISSET(flags, XTABS))
475 if (ISSET(flags, RAW)) {
477 CLR(lflag, ISIG|ICANON|IEXTEN);
480 SET(iflag, BRKINT|IXON|IMAXBEL);
481 SET(lflag, ISIG|IEXTEN);
482 if (ISSET(flags, CBREAK))
486 switch (ISSET(flags, ANYP)) {
507 /* Nothing we can do with CRTBS. */
508 if (ISSET(flags, PRTERA))
512 if (ISSET(flags, CRTERA))
516 /* Nothing we can do with TILDE. */
517 if (ISSET(flags, MDMBUF))
521 if (ISSET(flags, NOHANG))
525 if (ISSET(flags, CRTKIL))
529 if (ISSET(flags, CTLECH))
533 if (!ISSET(flags, DECCTQ))
537 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
538 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
540 if (ISSET(flags, RAW|LITOUT|PASS8)) {
543 if (!ISSET(flags, RAW|PASS8))
547 if (!ISSET(flags, RAW|LITOUT))
558 tmode.c_iflag = iflag;
559 tmode.c_oflag = oflag;
560 tmode.c_cflag = cflag;
561 tmode.c_lflag = lflag;
567 unsigned delay; /* delay in ms */
572 * below are random guesses, I can't be bothered checking
575 struct delayval crdelay[] = {
584 struct delayval nldelay[] = {
585 { 1, NL1 }, /* special, calculated */
592 struct delayval bsdelay[] = {
597 struct delayval ffdelay[] = {
603 struct delayval tbdelay[] = {
606 { 3, XTABS }, /* this is expand tabs */
616 f = adelay(CD, crdelay);
617 f |= adelay(ND, nldelay);
618 f |= adelay(FD, ffdelay);
619 f |= adelay(TD, tbdelay);
620 f |= adelay(BD, bsdelay);
627 register struct delayval *dp;
631 while (dp->delay && ms > dp->delay)
637 char editedhost[MAXHOSTNAMELEN];
641 register const char *pat;
643 register const char *host = HN;
644 register char *res = editedhost;
666 if (res == &editedhost[sizeof editedhost - 1]) {
673 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
676 editedhost[sizeof editedhost - 1] = '\0';
679 static struct speedtab {
697 { 19, EXTA }, /* for people who say 19.2K */
700 { 7200, EXTB }, /* alternative */
711 register struct speedtab *sp;
716 for (sp = speedtab; sp->speed; sp++)
717 if (sp->speed == val)
720 return (B300); /* default in impossible cases */
727 static char termbuf[128] = "TERM=";
728 register char *p, *q;
733 strlcat(termbuf, TT, sizeof(termbuf));
738 while ((q = strchr(q, ','))) {
750 * This speed select mechanism is written for the Develcon DATASWITCH.
751 * The Develcon sends a string of the form "B{speed}\n" at a predefined
752 * baud rate. This string indicates the user's actual speed.
753 * The routine below returns the terminal type mapped from derived speed.
759 { "B110", "std.110" },
760 { "B134", "std.134" },
761 { "B150", "std.150" },
762 { "B300", "std.300" },
763 { "B600", "std.600" },
764 { "B1200", "std.1200" },
765 { "B2400", "std.2400" },
766 { "B4800", "std.4800" },
767 { "B9600", "std.9600" },
768 { "B19200", "std.19200" },
776 const char *type = "default";
777 register struct portselect *ps;
781 for (len = 0; len < sizeof (baud) - 1; len++) {
782 if (read(STDIN_FILENO, &c, 1) <= 0)
785 if (c == '\n' || c == '\r')
788 len = 0; /* in case of leading garbage */
792 for (ps = portspeeds; ps->ps_baud; ps++)
793 if (strcmp(ps->ps_baud, baud) == 0) {
797 sleep(2); /* wait for connection to complete */
802 * This auto-baud speed select mechanism is written for the Micom 600
803 * portselector. Selection is done by looking at how the character '\r'
804 * is garbled at the different speeds.
810 struct timeval timeout;
812 const char *type = "9600-baud";
814 (void)tcflush(0, TCIOFLUSH);
818 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
819 (fd_set *)NULL, &timeout) <= 0)
821 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
824 timeout.tv_usec = 20;
825 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
826 (fd_set *)NULL, &timeout);
827 (void)tcflush(0, TCIOFLUSH);
830 case 0200: /* 300-baud */
834 case 0346: /* 1200-baud */
838 case 015: /* 2400-baud */
843 default: /* 4800-baud */
847 case 0377: /* 9600-baud */