xref: /netbsd-src/libexec/getty/subr.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: subr.c,v 1.20 1996/09/27 02:15:35 thorpej 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)subr.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: subr.c,v 1.20 1996/09/27 02:15:35 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43 
44 /*
45  * Melbourne getty.
46  */
47 #define COMPAT_43
48 #include <sys/param.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <termios.h>
53 #include <sys/ioctl.h>
54 
55 #include "gettytab.h"
56 #include "pathnames.h"
57 #include "extern.h"
58 
59 extern	struct termios tmode, omode;
60 
61 static void	compatflags __P((long));
62 
63 /*
64  * Get a table entry.
65  */
66 void
67 gettable(name, buf)
68 	char *name, *buf;
69 {
70 	register struct gettystrs *sp;
71 	register struct gettynums *np;
72 	register struct gettyflags *fp;
73 	long n;
74 	char *dba[2];
75 	dba[0] = _PATH_GETTYTAB;
76 	dba[1] = 0;
77 
78 	if (cgetent(&buf, dba, name) != 0)
79 		return;
80 
81 	for (sp = gettystrs; sp->field; sp++)
82 		cgetstr(buf, sp->field, &sp->value);
83 	for (np = gettynums; np->field; np++) {
84 		if (cgetnum(buf, np->field, &n) == -1)
85 			np->set = 0;
86 		else {
87 			np->set = 1;
88 			np->value = n;
89 		}
90 	}
91 	for (fp = gettyflags; fp->field; fp++) {
92 		if (cgetcap(buf, fp->field, ':') == NULL)
93 			fp->set = 0;
94 		else {
95 			fp->set = 1;
96 			fp->value = 1 ^ fp->invrt;
97 		}
98 	}
99 #ifdef DEBUG
100 	printf("name=\"%s\", buf=\"%s\"\n", name, buf);
101 	for (sp = gettystrs; sp->field; sp++)
102 		printf("cgetstr: %s=%s\n", sp->field, sp->value);
103 	for (np = gettynums; np->field; np++)
104 		printf("cgetnum: %s=%d\n", np->field, np->value);
105 	for (fp = gettyflags; fp->field; fp++)
106 		printf("cgetflags: %s='%c' set='%c'\n", fp->field,
107 		       fp->value + '0', fp->set + '0');
108 	exit(1);
109 #endif /* DEBUG */
110 }
111 
112 void
113 gendefaults()
114 {
115 	register struct gettystrs *sp;
116 	register struct gettynums *np;
117 	register struct gettyflags *fp;
118 
119 	for (sp = gettystrs; sp->field; sp++)
120 		if (sp->value)
121 			sp->defalt = sp->value;
122 	for (np = gettynums; np->field; np++)
123 		if (np->set)
124 			np->defalt = np->value;
125 	for (fp = gettyflags; fp->field; fp++)
126 		if (fp->set)
127 			fp->defalt = fp->value;
128 		else
129 			fp->defalt = fp->invrt;
130 }
131 
132 void
133 setdefaults()
134 {
135 	register struct gettystrs *sp;
136 	register struct gettynums *np;
137 	register struct gettyflags *fp;
138 
139 	for (sp = gettystrs; sp->field; sp++)
140 		if (!sp->value)
141 			sp->value = sp->defalt;
142 	for (np = gettynums; np->field; np++)
143 		if (!np->set)
144 			np->value = np->defalt;
145 	for (fp = gettyflags; fp->field; fp++)
146 		if (!fp->set)
147 			fp->value = fp->defalt;
148 }
149 
150 static char **
151 charnames[] = {
152 	&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
153 	&SU, &DS, &RP, &FL, &WE, &LN, 0
154 };
155 
156 static char *
157 charvars[] = {
158 	&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
159 	&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
160 	&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
161 	&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
162 	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
163 };
164 
165 void
166 setchars()
167 {
168 	register int i;
169 	register char *p;
170 
171 	for (i = 0; charnames[i]; i++) {
172 		p = *charnames[i];
173 		if (p && *p)
174 			*charvars[i] = *p;
175 		else
176 			*charvars[i] = _POSIX_VDISABLE;
177 	}
178 }
179 
180 /* Macros to clear/set/test flags. */
181 #define	SET(t, f)	(t) |= (f)
182 #define	CLR(t, f)	(t) &= ~(f)
183 #define	ISSET(t, f)	((t) & (f))
184 
185 void
186 setflags(n)
187 	int n;
188 {
189 	register tcflag_t iflag, oflag, cflag, lflag;
190 
191 #ifdef COMPAT_43
192 	switch (n) {
193 	case 0:
194 		if (F0set) {
195 			compatflags(F0);
196 			return;
197 		}
198 		break;
199 	case 1:
200 		if (F1set) {
201 			compatflags(F1);
202 			return;
203 		}
204 		break;
205 	default:
206 		if (F2set) {
207 			compatflags(F2);
208 			return;
209 		}
210 		break;
211 	}
212 #endif
213 
214 	switch (n) {
215 	case 0:
216 		if (C0set && I0set && L0set && O0set) {
217 			tmode.c_cflag = C0;
218 			tmode.c_iflag = I0;
219 			tmode.c_lflag = L0;
220 			tmode.c_oflag = O0;
221 			return;
222 		}
223 		break;
224 	case 1:
225 		if (C1set && I1set && L1set && O1set) {
226 			tmode.c_cflag = C1;
227 			tmode.c_iflag = I1;
228 			tmode.c_lflag = L1;
229 			tmode.c_oflag = O1;
230 			return;
231 		}
232 		break;
233 	default:
234 		if (C2set && I2set && L2set && O2set) {
235 			tmode.c_cflag = C2;
236 			tmode.c_iflag = I2;
237 			tmode.c_lflag = L2;
238 			tmode.c_oflag = O2;
239 			return;
240 		}
241 		break;
242 	}
243 
244 	iflag = omode.c_iflag;
245 	oflag = omode.c_oflag;
246 	cflag = omode.c_cflag;
247 	lflag = omode.c_lflag;
248 
249 	if (NP) {
250 		CLR(cflag, CSIZE|PARENB);
251 		SET(cflag, CS8);
252 		CLR(iflag, ISTRIP|INPCK|IGNPAR);
253 	} else if (AP || EP || OP) {
254 		CLR(cflag, CSIZE);
255 		SET(cflag, CS7|PARENB);
256 		SET(iflag, ISTRIP);
257 		if (OP && !EP) {
258 			SET(iflag, INPCK|IGNPAR);
259 			SET(cflag, PARODD);
260 			if (AP)
261 				CLR(iflag, INPCK);
262 		} else if (EP && !OP) {
263 			SET(iflag, INPCK|IGNPAR);
264 			CLR(cflag, PARODD);
265 			if (AP)
266 				CLR(iflag, INPCK);
267 		} else if (AP || EP && OP) {
268 			CLR(iflag, INPCK|IGNPAR);
269 			CLR(cflag, PARODD);
270 		}
271 	} /* else, leave as is */
272 
273 #if 0
274 	if (UC)
275 		f |= LCASE;
276 #endif
277 
278 	if (HC)
279 		SET(cflag, HUPCL);
280 	else
281 		CLR(cflag, HUPCL);
282 
283 	if (MB)
284 		SET(cflag, MDMBUF);
285 	else
286 		CLR(cflag, MDMBUF);
287 
288 	if (NL) {
289 		SET(iflag, ICRNL);
290 		SET(oflag, ONLCR|OPOST);
291 	} else {
292 		CLR(iflag, ICRNL);
293 		CLR(oflag, ONLCR);
294 	}
295 
296 	if (!HT)
297 		SET(oflag, OXTABS|OPOST);
298 	else
299 		CLR(oflag, OXTABS);
300 
301 #ifdef XXX_DELAY
302 	SET(f, delaybits());
303 #endif
304 
305 	if (n == 1) {		/* read mode flags */
306 		if (RW) {
307 			iflag = 0;
308 			CLR(oflag, OPOST);
309 			CLR(cflag, CSIZE|PARENB);
310 			SET(cflag, CS8);
311 			lflag = 0;
312 		} else {
313 			CLR(lflag, ICANON);
314 		}
315 		goto out;
316 	}
317 
318 	if (n == 0)
319 		goto out;
320 
321 #if 0
322 	if (CB)
323 		SET(f, CRTBS);
324 #endif
325 
326 	if (CE)
327 		SET(lflag, ECHOE);
328 	else
329 		CLR(lflag, ECHOE);
330 
331 	if (CK)
332 		SET(lflag, ECHOKE);
333 	else
334 		CLR(lflag, ECHOKE);
335 
336 	if (PE)
337 		SET(lflag, ECHOPRT);
338 	else
339 		CLR(lflag, ECHOPRT);
340 
341 	if (EC)
342 		SET(lflag, ECHO);
343 	else
344 		CLR(lflag, ECHO);
345 
346 	if (XC)
347 		SET(lflag, ECHOCTL);
348 	else
349 		CLR(lflag, ECHOCTL);
350 
351 	if (DX)
352 		SET(lflag, IXANY);
353 	else
354 		CLR(lflag, IXANY);
355 
356 out:
357 	tmode.c_iflag = iflag;
358 	tmode.c_oflag = oflag;
359 	tmode.c_cflag = cflag;
360 	tmode.c_lflag = lflag;
361 }
362 
363 #ifdef COMPAT_43
364 /*
365  * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
366  */
367 void
368 compatflags(flags)
369 register long flags;
370 {
371 	register 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()
537 {
538 	register 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(ms, dp)
550 	register ms;
551 	register struct delayval *dp;
552 {
553 	if (ms == 0)
554 		return (0);
555 	while (dp->delay && ms > dp->delay)
556 		dp++;
557 	return (dp->bits);
558 }
559 #endif
560 
561 char	editedhost[MAXHOSTNAMELEN];
562 
563 void
564 edithost(pat)
565 	register char *pat;
566 {
567 	register char *host = HN;
568 	register char *res = editedhost;
569 
570 	if (!pat)
571 		pat = "";
572 	while (*pat) {
573 		switch (*pat) {
574 
575 		case '#':
576 			if (*host)
577 				host++;
578 			break;
579 
580 		case '@':
581 			if (*host)
582 				*res++ = *host++;
583 			break;
584 
585 		default:
586 			*res++ = *pat;
587 			break;
588 
589 		}
590 		if (res == &editedhost[sizeof editedhost - 1]) {
591 			*res = '\0';
592 			return;
593 		}
594 		pat++;
595 	}
596 	if (*host)
597 		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
598 	else
599 		*res = '\0';
600 	editedhost[sizeof editedhost - 1] = '\0';
601 }
602 
603 void
604 makeenv(env)
605 	char *env[];
606 {
607 	static char termbuf[128] = "TERM=";
608 	register char *p, *q;
609 	register char **ep;
610 
611 	ep = env;
612 	if (TT && *TT) {
613 		strcat(termbuf, TT);
614 		*ep++ = termbuf;
615 	}
616 	if (p = EV) {
617 		q = p;
618 		while (q = strchr(q, ',')) {
619 			*q++ = '\0';
620 			*ep++ = p;
621 			p = q;
622 		}
623 		if (*p)
624 			*ep++ = p;
625 	}
626 	*ep = (char *)0;
627 }
628 
629 /*
630  * This speed select mechanism is written for the Develcon DATASWITCH.
631  * The Develcon sends a string of the form "B{speed}\n" at a predefined
632  * baud rate. This string indicates the user's actual speed.
633  * The routine below returns the terminal type mapped from derived speed.
634  */
635 struct	portselect {
636 	char	*ps_baud;
637 	char	*ps_type;
638 } portspeeds[] = {
639 	{ "B110",	"std.110" },
640 	{ "B134",	"std.134" },
641 	{ "B150",	"std.150" },
642 	{ "B300",	"std.300" },
643 	{ "B600",	"std.600" },
644 	{ "B1200",	"std.1200" },
645 	{ "B2400",	"std.2400" },
646 	{ "B4800",	"std.4800" },
647 	{ "B9600",	"std.9600" },
648 	{ "B19200",	"std.19200" },
649 	{ 0 }
650 };
651 
652 char *
653 portselector()
654 {
655 	char c, baud[20], *type = "default";
656 	register struct portselect *ps;
657 	int len;
658 
659 	alarm(5*60);
660 	for (len = 0; len < sizeof (baud) - 1; len++) {
661 		if (read(STDIN_FILENO, &c, 1) <= 0)
662 			break;
663 		c &= 0177;
664 		if (c == '\n' || c == '\r')
665 			break;
666 		if (c == 'B')
667 			len = 0;	/* in case of leading garbage */
668 		baud[len] = c;
669 	}
670 	baud[len] = '\0';
671 	for (ps = portspeeds; ps->ps_baud; ps++)
672 		if (strcmp(ps->ps_baud, baud) == 0) {
673 			type = ps->ps_type;
674 			break;
675 		}
676 	sleep(2);	/* wait for connection to complete */
677 	return (type);
678 }
679 
680 /*
681  * This auto-baud speed select mechanism is written for the Micom 600
682  * portselector. Selection is done by looking at how the character '\r'
683  * is garbled at the different speeds.
684  */
685 #include <sys/time.h>
686 
687 char *
688 autobaud()
689 {
690 	int rfds;
691 	struct timeval timeout;
692 	char c, *type = "9600-baud";
693 
694 	(void)tcflush(0, TCIOFLUSH);
695 	rfds = 1 << 0;
696 	timeout.tv_sec = 5;
697 	timeout.tv_usec = 0;
698 	if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
699 	    (fd_set *)NULL, &timeout) <= 0)
700 		return (type);
701 	if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
702 		return (type);
703 	timeout.tv_sec = 0;
704 	timeout.tv_usec = 20;
705 	(void) select(32, (fd_set *)NULL, (fd_set *)NULL,
706 	    (fd_set *)NULL, &timeout);
707 	(void)tcflush(0, TCIOFLUSH);
708 	switch (c & 0377) {
709 
710 	case 0200:		/* 300-baud */
711 		type = "300-baud";
712 		break;
713 
714 	case 0346:		/* 1200-baud */
715 		type = "1200-baud";
716 		break;
717 
718 	case  015:		/* 2400-baud */
719 	case 0215:
720 		type = "2400-baud";
721 		break;
722 
723 	default:		/* 4800-baud */
724 		type = "4800-baud";
725 		break;
726 
727 	case 0377:		/* 9600-baud */
728 		type = "9600-baud";
729 		break;
730 	}
731 	return (type);
732 }
733