xref: /netbsd-src/libexec/getty/subr.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: subr.c,v 1.28 2002/09/23 12:48:02 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)subr.c	8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: subr.c,v 1.28 2002/09/23 12:48:02 mycroft Exp $");
42 #endif
43 #endif /* not lint */
44 
45 /*
46  * Melbourne getty.
47  */
48 #define COMPAT_43
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 
52 #include <stdlib.h>
53 #include <string.h>
54 #include <termios.h>
55 #include <unistd.h>
56 #include <poll.h>
57 
58 #include "extern.h"
59 #include "gettytab.h"
60 #include "pathnames.h"
61 
62 extern	struct termios tmode, omode;
63 
64 static void	compatflags(long);
65 
66 /*
67  * Get a table entry.
68  */
69 void
70 gettable(char *name, char *buf)
71 {
72 	struct gettystrs *sp;
73 	struct gettynums *np;
74 	struct gettyflags *fp;
75 	long n;
76 	char *dba[2];
77 	dba[0] = _PATH_GETTYTAB;
78 	dba[1] = 0;
79 
80 	if (cgetent(&buf, dba, name) != 0)
81 		return;
82 
83 	for (sp = gettystrs; sp->field; sp++)
84 		cgetstr(buf, sp->field, &sp->value);
85 	for (np = gettynums; np->field; np++) {
86 		if (cgetnum(buf, np->field, &n) == -1)
87 			np->set = 0;
88 		else {
89 			np->set = 1;
90 			np->value = n;
91 		}
92 	}
93 	for (fp = gettyflags; fp->field; fp++) {
94 		if (cgetcap(buf, fp->field, ':') == NULL)
95 			fp->set = 0;
96 		else {
97 			fp->set = 1;
98 			fp->value = 1 ^ fp->invrt;
99 		}
100 	}
101 #ifdef DEBUG
102 	printf("name=\"%s\", buf=\"%s\"\n", name, buf);
103 	for (sp = gettystrs; sp->field; sp++)
104 		printf("cgetstr: %s=%s\n", sp->field, sp->value);
105 	for (np = gettynums; np->field; np++)
106 		printf("cgetnum: %s=%d\n", np->field, np->value);
107 	for (fp = gettyflags; fp->field; fp++)
108 		printf("cgetflags: %s='%c' set='%c'\n", fp->field,
109 		       fp->value + '0', fp->set + '0');
110 	exit(1);
111 #endif /* DEBUG */
112 }
113 
114 void
115 gendefaults(void)
116 {
117 	struct gettystrs *sp;
118 	struct gettynums *np;
119 	struct gettyflags *fp;
120 
121 	for (sp = gettystrs; sp->field; sp++)
122 		if (sp->value)
123 			sp->defalt = sp->value;
124 	for (np = gettynums; np->field; np++)
125 		if (np->set)
126 			np->defalt = np->value;
127 	for (fp = gettyflags; fp->field; fp++)
128 		if (fp->set)
129 			fp->defalt = fp->value;
130 		else
131 			fp->defalt = fp->invrt;
132 }
133 
134 void
135 setdefaults(void)
136 {
137 	struct gettystrs *sp;
138 	struct gettynums *np;
139 	struct gettyflags *fp;
140 
141 	for (sp = gettystrs; sp->field; sp++)
142 		if (!sp->value)
143 			sp->value = sp->defalt;
144 	for (np = gettynums; np->field; np++)
145 		if (!np->set)
146 			np->value = np->defalt;
147 	for (fp = gettyflags; fp->field; fp++)
148 		if (!fp->set)
149 			fp->value = fp->defalt;
150 }
151 
152 static char **
153 charnames[] = {
154 	&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
155 	&SU, &DS, &RP, &FL, &WE, &LN, 0
156 };
157 
158 static char *
159 charvars[] = {
160 	&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
161 	&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
162 	&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
163 	&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
164 	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
165 };
166 
167 void
168 setchars(void)
169 {
170 	int i;
171 	char *p;
172 
173 	for (i = 0; charnames[i]; i++) {
174 		p = *charnames[i];
175 		if (p && *p)
176 			*charvars[i] = *p;
177 		else
178 			*charvars[i] = _POSIX_VDISABLE;
179 	}
180 }
181 
182 /* Macros to clear/set/test flags. */
183 #define	SET(t, f)	(t) |= (f)
184 #define	CLR(t, f)	(t) &= ~(f)
185 #define	ISSET(t, f)	((t) & (f))
186 
187 void
188 setflags(int n)
189 {
190 	tcflag_t iflag, oflag, cflag, lflag;
191 
192 #ifdef COMPAT_43
193 	switch (n) {
194 	case 0:
195 		if (F0set) {
196 			compatflags(F0);
197 			return;
198 		}
199 		break;
200 	case 1:
201 		if (F1set) {
202 			compatflags(F1);
203 			return;
204 		}
205 		break;
206 	default:
207 		if (F2set) {
208 			compatflags(F2);
209 			return;
210 		}
211 		break;
212 	}
213 #endif
214 
215 	switch (n) {
216 	case 0:
217 		if (C0set && I0set && L0set && O0set) {
218 			tmode.c_cflag = C0;
219 			tmode.c_iflag = I0;
220 			tmode.c_lflag = L0;
221 			tmode.c_oflag = O0;
222 			return;
223 		}
224 		break;
225 	case 1:
226 		if (C1set && I1set && L1set && O1set) {
227 			tmode.c_cflag = C1;
228 			tmode.c_iflag = I1;
229 			tmode.c_lflag = L1;
230 			tmode.c_oflag = O1;
231 			return;
232 		}
233 		break;
234 	default:
235 		if (C2set && I2set && L2set && O2set) {
236 			tmode.c_cflag = C2;
237 			tmode.c_iflag = I2;
238 			tmode.c_lflag = L2;
239 			tmode.c_oflag = O2;
240 			return;
241 		}
242 		break;
243 	}
244 
245 	iflag = omode.c_iflag;
246 	oflag = omode.c_oflag;
247 	cflag = omode.c_cflag;
248 	lflag = omode.c_lflag;
249 
250 	if (NP) {
251 		CLR(cflag, CSIZE|PARENB);
252 		SET(cflag, CS8);
253 		CLR(iflag, ISTRIP|INPCK|IGNPAR);
254 	} else if (AP || EP || OP) {
255 		CLR(cflag, CSIZE);
256 		SET(cflag, CS7|PARENB);
257 		SET(iflag, ISTRIP);
258 		if (OP && !EP) {
259 			SET(iflag, INPCK|IGNPAR);
260 			SET(cflag, PARODD);
261 			if (AP)
262 				CLR(iflag, INPCK);
263 		} else if (EP && !OP) {
264 			SET(iflag, INPCK|IGNPAR);
265 			CLR(cflag, PARODD);
266 			if (AP)
267 				CLR(iflag, INPCK);
268 		} else if (AP || (EP && OP)) {
269 			CLR(iflag, INPCK|IGNPAR);
270 			CLR(cflag, PARODD);
271 		}
272 	} /* else, leave as is */
273 
274 #if 0
275 	if (UC)
276 		f |= LCASE;
277 #endif
278 
279 	if (HC)
280 		SET(cflag, HUPCL);
281 	else
282 		CLR(cflag, HUPCL);
283 
284 	if (MB)
285 		SET(cflag, MDMBUF);
286 	else
287 		CLR(cflag, MDMBUF);
288 
289 	if (NL) {
290 		SET(iflag, ICRNL);
291 		SET(oflag, ONLCR|OPOST);
292 	} else {
293 		CLR(iflag, ICRNL);
294 		CLR(oflag, ONLCR);
295 	}
296 
297 	if (!HT)
298 		SET(oflag, OXTABS|OPOST);
299 	else
300 		CLR(oflag, OXTABS);
301 
302 #ifdef XXX_DELAY
303 	SET(f, delaybits());
304 #endif
305 
306 	if (n == 1) {		/* read mode flags */
307 		if (RW) {
308 			iflag = 0;
309 			CLR(oflag, OPOST);
310 			CLR(cflag, CSIZE|PARENB);
311 			SET(cflag, CS8);
312 			lflag = 0;
313 		} else {
314 			CLR(lflag, ICANON);
315 		}
316 		goto out;
317 	}
318 
319 	if (n == 0)
320 		goto out;
321 
322 #if 0
323 	if (CB)
324 		SET(f, CRTBS);
325 #endif
326 
327 	if (CE)
328 		SET(lflag, ECHOE);
329 	else
330 		CLR(lflag, ECHOE);
331 
332 	if (CK)
333 		SET(lflag, ECHOKE);
334 	else
335 		CLR(lflag, ECHOKE);
336 
337 	if (PE)
338 		SET(lflag, ECHOPRT);
339 	else
340 		CLR(lflag, ECHOPRT);
341 
342 	if (EC)
343 		SET(lflag, ECHO);
344 	else
345 		CLR(lflag, ECHO);
346 
347 	if (XC)
348 		SET(lflag, ECHOCTL);
349 	else
350 		CLR(lflag, ECHOCTL);
351 
352 	if (DX)
353 		SET(lflag, IXANY);
354 	else
355 		CLR(lflag, IXANY);
356 
357 out:
358 	tmode.c_iflag = iflag;
359 	tmode.c_oflag = oflag;
360 	tmode.c_cflag = cflag;
361 	tmode.c_lflag = lflag;
362 }
363 
364 #ifdef COMPAT_43
365 /*
366  * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
367  */
368 void
369 compatflags(long flags)
370 {
371 	tcflag_t iflag, oflag, cflag, lflag;
372 
373 	iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
374 	oflag = OPOST|ONLCR|OXTABS;
375 	cflag = CREAD;
376 	lflag = ICANON|ISIG|IEXTEN;
377 
378 	if (ISSET(flags, TANDEM))
379 		SET(iflag, IXOFF);
380 	else
381 		CLR(iflag, IXOFF);
382 	if (ISSET(flags, ECHO))
383 		SET(lflag, ECHO);
384 	else
385 		CLR(lflag, ECHO);
386 	if (ISSET(flags, CRMOD)) {
387 		SET(iflag, ICRNL);
388 		SET(oflag, ONLCR);
389 	} else {
390 		CLR(iflag, ICRNL);
391 		CLR(oflag, ONLCR);
392 	}
393 	if (ISSET(flags, XTABS))
394 		SET(oflag, OXTABS);
395 	else
396 		CLR(oflag, OXTABS);
397 
398 
399 	if (ISSET(flags, RAW)) {
400 		iflag &= IXOFF;
401 		CLR(lflag, ISIG|ICANON|IEXTEN);
402 		CLR(cflag, PARENB);
403 	} else {
404 		SET(iflag, BRKINT|IXON|IMAXBEL);
405 		SET(lflag, ISIG|IEXTEN);
406 		if (ISSET(flags, CBREAK))
407 			CLR(lflag, ICANON);
408 		else
409 			SET(lflag, ICANON);
410 		switch (ISSET(flags, ANYP)) {
411 		case 0:
412 			CLR(cflag, PARENB);
413 			break;
414 		case ANYP:
415 			SET(cflag, PARENB);
416 			CLR(iflag, INPCK);
417 			break;
418 		case EVENP:
419 			SET(cflag, PARENB);
420 			SET(iflag, INPCK);
421 			CLR(cflag, PARODD);
422 			break;
423 		case ODDP:
424 			SET(cflag, PARENB);
425 			SET(iflag, INPCK);
426 			SET(cflag, PARODD);
427 			break;
428 		}
429 	}
430 
431 	/* Nothing we can do with CRTBS. */
432 	if (ISSET(flags, PRTERA))
433 		SET(lflag, ECHOPRT);
434 	else
435 		CLR(lflag, ECHOPRT);
436 	if (ISSET(flags, CRTERA))
437 		SET(lflag, ECHOE);
438 	else
439 		CLR(lflag, ECHOE);
440 	/* Nothing we can do with TILDE. */
441 	if (ISSET(flags, MDMBUF))
442 		SET(cflag, MDMBUF);
443 	else
444 		CLR(cflag, MDMBUF);
445 	if (ISSET(flags, NOHANG))
446 		CLR(cflag, HUPCL);
447 	else
448 		SET(cflag, HUPCL);
449 	if (ISSET(flags, CRTKIL))
450 		SET(lflag, ECHOKE);
451 	else
452 		CLR(lflag, ECHOKE);
453 	if (ISSET(flags, CTLECH))
454 		SET(lflag, ECHOCTL);
455 	else
456 		CLR(lflag, ECHOCTL);
457 	if (!ISSET(flags, DECCTQ))
458 		SET(iflag, IXANY);
459 	else
460 		CLR(iflag, IXANY);
461 	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
462 	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
463 
464 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
465 		CLR(cflag, CSIZE);
466 		SET(cflag, CS8);
467 		if (!ISSET(flags, RAW|PASS8))
468 			SET(iflag, ISTRIP);
469 		else
470 			CLR(iflag, ISTRIP);
471 		if (!ISSET(flags, RAW|LITOUT))
472 			SET(oflag, OPOST);
473 		else
474 			CLR(oflag, OPOST);
475 	} else {
476 		CLR(cflag, CSIZE);
477 		SET(cflag, CS7);
478 		SET(iflag, ISTRIP);
479 		SET(oflag, OPOST);
480 	}
481 
482 	tmode.c_iflag = iflag;
483 	tmode.c_oflag = oflag;
484 	tmode.c_cflag = cflag;
485 	tmode.c_lflag = lflag;
486 }
487 #endif
488 
489 #ifdef XXX_DELAY
490 struct delayval {
491 	unsigned	delay;		/* delay in ms */
492 	int		bits;
493 };
494 
495 /*
496  * below are random guesses, I can't be bothered checking
497  */
498 
499 struct delayval	crdelay[] = {
500 	{ 1,		CR1 },
501 	{ 2,		CR2 },
502 	{ 3,		CR3 },
503 	{ 83,		CR1 },
504 	{ 166,		CR2 },
505 	{ 0,		CR3 },
506 };
507 
508 struct delayval nldelay[] = {
509 	{ 1,		NL1 },		/* special, calculated */
510 	{ 2,		NL2 },
511 	{ 3,		NL3 },
512 	{ 100,		NL2 },
513 	{ 0,		NL3 },
514 };
515 
516 struct delayval	bsdelay[] = {
517 	{ 1,		BS1 },
518 	{ 0,		0 },
519 };
520 
521 struct delayval	ffdelay[] = {
522 	{ 1,		FF1 },
523 	{ 1750,		FF1 },
524 	{ 0,		FF1 },
525 };
526 
527 struct delayval	tbdelay[] = {
528 	{ 1,		 TAB1 },
529 	{ 2,		 TAB2 },
530 	{ 3,		XTABS },	/* this is expand tabs */
531 	{ 100,		 TAB1 },
532 	{ 0,		 TAB2 },
533 };
534 
535 int
536 delaybits(void)
537 {
538 	int f;
539 
540 	f  = adelay(CD, crdelay);
541 	f |= adelay(ND, nldelay);
542 	f |= adelay(FD, ffdelay);
543 	f |= adelay(TD, tbdelay);
544 	f |= adelay(BD, bsdelay);
545 	return (f);
546 }
547 
548 int
549 adelay(int ms, struct delayval *dp)
550 {
551 	if (ms == 0)
552 		return (0);
553 	while (dp->delay && ms > dp->delay)
554 		dp++;
555 	return (dp->bits);
556 }
557 #endif
558 
559 char	editedhost[MAXHOSTNAMELEN];
560 
561 void
562 edithost(char *pat)
563 {
564 	char *host = HN;
565 	char *res = editedhost;
566 
567 	if (!pat)
568 		pat = "";
569 	while (*pat) {
570 		switch (*pat) {
571 
572 		case '#':
573 			if (*host)
574 				host++;
575 			break;
576 
577 		case '@':
578 			if (*host)
579 				*res++ = *host++;
580 			break;
581 
582 		default:
583 			*res++ = *pat;
584 			break;
585 
586 		}
587 		if (res == &editedhost[sizeof editedhost - 1]) {
588 			*res = '\0';
589 			return;
590 		}
591 		pat++;
592 	}
593 	if (*host)
594 		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
595 	else
596 		*res = '\0';
597 	editedhost[sizeof editedhost - 1] = '\0';
598 }
599 
600 void
601 makeenv(char *env[])
602 {
603 	static char termbuf[128] = "TERM=";
604 	char *p, *q;
605 	char **ep;
606 
607 	ep = env;
608 	if (TT && *TT) {
609 		strlcat(termbuf, TT, sizeof(termbuf));
610 		*ep++ = termbuf;
611 	}
612 	if ((p = EV) != NULL) {
613 		q = p;
614 		while ((q = strchr(q, ',')) != NULL) {
615 			*q++ = '\0';
616 			*ep++ = p;
617 			p = q;
618 		}
619 		if (*p)
620 			*ep++ = p;
621 	}
622 	*ep = (char *)0;
623 }
624 
625 /*
626  * This speed select mechanism is written for the Develcon DATASWITCH.
627  * The Develcon sends a string of the form "B{speed}\n" at a predefined
628  * baud rate. This string indicates the user's actual speed.
629  * The routine below returns the terminal type mapped from derived speed.
630  */
631 struct	portselect {
632 	char	*ps_baud;
633 	char	*ps_type;
634 } portspeeds[] = {
635 	{ "B110",	"std.110" },
636 	{ "B134",	"std.134" },
637 	{ "B150",	"std.150" },
638 	{ "B300",	"std.300" },
639 	{ "B600",	"std.600" },
640 	{ "B1200",	"std.1200" },
641 	{ "B2400",	"std.2400" },
642 	{ "B4800",	"std.4800" },
643 	{ "B9600",	"std.9600" },
644 	{ "B19200",	"std.19200" },
645 	{ 0 }
646 };
647 
648 char *
649 portselector(void)
650 {
651 	char c, baud[20], *type = "default";
652 	struct portselect *ps;
653 	int len;
654 
655 	alarm(5*60);
656 	for (len = 0; len < sizeof (baud) - 1; len++) {
657 		if (read(STDIN_FILENO, &c, 1) <= 0)
658 			break;
659 		c &= 0177;
660 		if (c == '\n' || c == '\r')
661 			break;
662 		if (c == 'B')
663 			len = 0;	/* in case of leading garbage */
664 		baud[len] = c;
665 	}
666 	baud[len] = '\0';
667 	for (ps = portspeeds; ps->ps_baud; ps++)
668 		if (strcmp(ps->ps_baud, baud) == 0) {
669 			type = ps->ps_type;
670 			break;
671 		}
672 	sleep(2);	/* wait for connection to complete */
673 	return (type);
674 }
675 
676 /*
677  * This auto-baud speed select mechanism is written for the Micom 600
678  * portselector. Selection is done by looking at how the character '\r'
679  * is garbled at the different speeds.
680  */
681 #include <sys/time.h>
682 
683 char *
684 autobaud(void)
685 {
686 	struct pollfd set[1];
687 	struct timespec timeout;
688 	char c, *type = "9600-baud";
689 
690 	(void)tcflush(0, TCIOFLUSH);
691 	set[0].fd = STDIN_FILENO;
692 	set[0].events = POLLIN;
693 	if (poll(set, 1, 5000) <= 0)
694 		return (type);
695 	if (read(STDIN_FILENO, &c, 1) != 1)
696 		return (type);
697 	timeout.tv_sec = 0;
698 	timeout.tv_nsec = 20000;
699 	(void)nanosleep(&timeout, NULL);
700 	(void)tcflush(0, TCIOFLUSH);
701 	switch (c & 0377) {
702 
703 	case 0200:		/* 300-baud */
704 		type = "300-baud";
705 		break;
706 
707 	case 0346:		/* 1200-baud */
708 		type = "1200-baud";
709 		break;
710 
711 	case  015:		/* 2400-baud */
712 	case 0215:
713 		type = "2400-baud";
714 		break;
715 
716 	default:		/* 4800-baud */
717 		type = "4800-baud";
718 		break;
719 
720 	case 0377:		/* 9600-baud */
721 		type = "9600-baud";
722 		break;
723 	}
724 	return (type);
725 }
726