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