xref: /freebsd/libexec/getty/subr.c (revision d748864d2c912637ecc3d55874df9af6ff5c2c8f)
1ea022d16SRodney W. Grimes /*
2cae66988SJoerg Wunsch  * Copyright (c) 1983, 1993
3cae66988SJoerg Wunsch  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
34ea022d16SRodney W. Grimes #ifndef lint
35d748864dSPhilippe Charnier #if 0
36d748864dSPhilippe Charnier static char sccsid[] = "@(#)from: subr.c	8.1 (Berkeley) 6/4/93";
37d748864dSPhilippe Charnier #endif
38d748864dSPhilippe Charnier static const char rcsid[] =
39d748864dSPhilippe Charnier 	"$Id$";
40ea022d16SRodney W. Grimes #endif /* not lint */
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes /*
43ea022d16SRodney W. Grimes  * Melbourne getty.
44ea022d16SRodney W. Grimes  */
45cae66988SJoerg Wunsch #define COMPAT_43
46cae66988SJoerg Wunsch #ifdef DEBUG
47cae66988SJoerg Wunsch #include <stdio.h>
48cae66988SJoerg Wunsch #endif
49d748864dSPhilippe Charnier #include <stdlib.h>
50d748864dSPhilippe Charnier #include <string.h>
51d748864dSPhilippe Charnier #include <termios.h>
52d748864dSPhilippe Charnier #include <unistd.h>
53d748864dSPhilippe Charnier #include <sys/ioctl.h>
54d748864dSPhilippe Charnier #include <sys/param.h>
55d748864dSPhilippe Charnier #include <sys/time.h>
56d748864dSPhilippe Charnier #include <syslog.h>
57ea022d16SRodney W. Grimes 
58cae66988SJoerg Wunsch #include "gettytab.h"
59cae66988SJoerg Wunsch #include "pathnames.h"
60cae66988SJoerg Wunsch #include "extern.h"
61cae66988SJoerg Wunsch 
62cae66988SJoerg Wunsch 
63cae66988SJoerg Wunsch #ifdef COMPAT_43
64cae66988SJoerg Wunsch static void	compatflags __P((long));
65cae66988SJoerg Wunsch #endif
66ea022d16SRodney W. Grimes 
67ea022d16SRodney W. Grimes /*
68ea022d16SRodney W. Grimes  * Get a table entry.
69ea022d16SRodney W. Grimes  */
70cae66988SJoerg Wunsch void
71cae66988SJoerg Wunsch gettable(name, buf)
72cae66988SJoerg Wunsch 	const char *name;
73cae66988SJoerg Wunsch 	char *buf;
74ea022d16SRodney W. Grimes {
75ea022d16SRodney W. Grimes 	register struct gettystrs *sp;
76ea022d16SRodney W. Grimes 	register struct gettynums *np;
77ea022d16SRodney W. Grimes 	register struct gettyflags *fp;
78cae66988SJoerg Wunsch 	long n;
7904a59e67SDavid Nugent 	int l;
8004a59e67SDavid Nugent 	char *p;
8104a59e67SDavid Nugent 	char *msg = NULL;
82cae66988SJoerg Wunsch 	const char *dba[2];
8304a59e67SDavid Nugent 
8404a59e67SDavid Nugent 	static int firsttime = 1;
8504a59e67SDavid Nugent 
86cae66988SJoerg Wunsch 	dba[0] = _PATH_GETTYTAB;
87cae66988SJoerg Wunsch 	dba[1] = 0;
88ea022d16SRodney W. Grimes 
8904a59e67SDavid Nugent 	if (firsttime) {
9004a59e67SDavid Nugent 		/*
9104a59e67SDavid Nugent 		 * we need to strdup() anything in the strings array
9204a59e67SDavid Nugent 		 * initially in order to simplify things later
9304a59e67SDavid Nugent 		 */
94ea022d16SRodney W. Grimes 		for (sp = gettystrs; sp->field; sp++)
9504a59e67SDavid Nugent 			if (sp->value != NULL) {
9604a59e67SDavid Nugent 				/* handle these ones more carefully */
9704a59e67SDavid Nugent 				if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
9804a59e67SDavid Nugent 					l = 2;
9904a59e67SDavid Nugent 				else
10004a59e67SDavid Nugent 					l = strlen(sp->value) + 1;
10104a59e67SDavid Nugent 				if ((p = malloc(l)) != NULL) {
10204a59e67SDavid Nugent 					strncpy(p, sp->value, l);
10304a59e67SDavid Nugent 					p[l-1] = '\0';
10404a59e67SDavid Nugent 				}
10504a59e67SDavid Nugent 				/*
10604a59e67SDavid Nugent 				 * replace, even if NULL, else we'll
10704a59e67SDavid Nugent 				 * have problems with free()ing static mem
10804a59e67SDavid Nugent 				 */
10904a59e67SDavid Nugent 				sp->value = p;
11004a59e67SDavid Nugent 			}
11104a59e67SDavid Nugent 		firsttime = 0;
11204a59e67SDavid Nugent 	}
11304a59e67SDavid Nugent 
11404a59e67SDavid Nugent 	switch (cgetent(&buf, (char **)dba, (char *)name)) {
11504a59e67SDavid Nugent 	case 1:
11604a59e67SDavid Nugent 		msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
11704a59e67SDavid Nugent 	case 0:
11804a59e67SDavid Nugent 		break;
11904a59e67SDavid Nugent 	case -1:
12004a59e67SDavid Nugent 		msg = "%s: unknown gettytab entry '%s'";
12104a59e67SDavid Nugent 		break;
12204a59e67SDavid Nugent 	case -2:
12304a59e67SDavid Nugent 		msg = "%s: retrieving gettytab entry '%s': %m";
12404a59e67SDavid Nugent 		break;
12504a59e67SDavid Nugent 	case -3:
12604a59e67SDavid Nugent 		msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
12704a59e67SDavid Nugent 		break;
12804a59e67SDavid Nugent 	default:
12904a59e67SDavid Nugent 		msg = "%s: unexpected cgetent() error for entry '%s'";
13004a59e67SDavid Nugent 		break;
13104a59e67SDavid Nugent 	}
13204a59e67SDavid Nugent 
13304a59e67SDavid Nugent 	if (msg != NULL) {
13404a59e67SDavid Nugent 		syslog(LOG_ERR, msg, "getty", name);
13504a59e67SDavid Nugent 		return;
13604a59e67SDavid Nugent 	}
13704a59e67SDavid Nugent 
13804a59e67SDavid Nugent 	for (sp = gettystrs; sp->field; sp++) {
1391cc15828SDavid Nugent 		if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
14004a59e67SDavid Nugent 			if (sp->value) {
14104a59e67SDavid Nugent 				/* prefer existing value */
14204a59e67SDavid Nugent 				if (strcmp(p, sp->value) != 0)
14304a59e67SDavid Nugent 					free(sp->value);
14404a59e67SDavid Nugent 				else {
14504a59e67SDavid Nugent 					free(p);
14604a59e67SDavid Nugent 					p = sp->value;
14704a59e67SDavid Nugent 				}
14804a59e67SDavid Nugent 			}
14904a59e67SDavid Nugent 			sp->value = p;
15004a59e67SDavid Nugent 		} else if (l == -1) {
15104a59e67SDavid Nugent 			free(sp->value);
15204a59e67SDavid Nugent 			sp->value = NULL;
15304a59e67SDavid Nugent 		}
15404a59e67SDavid Nugent 	}
15504a59e67SDavid Nugent 
156ea022d16SRodney W. Grimes 	for (np = gettynums; np->field; np++) {
15704a59e67SDavid Nugent 		if (cgetnum(buf, (char*)np->field, &n) == -1)
158ea022d16SRodney W. Grimes 			np->set = 0;
159ea022d16SRodney W. Grimes 		else {
160ea022d16SRodney W. Grimes 			np->set = 1;
161ea022d16SRodney W. Grimes 			np->value = n;
162ea022d16SRodney W. Grimes 		}
163ea022d16SRodney W. Grimes 	}
16404a59e67SDavid Nugent 
165ea022d16SRodney W. Grimes 	for (fp = gettyflags; fp->field; fp++) {
16604a59e67SDavid Nugent 		if (cgetcap(buf, (char *)fp->field, ':') == NULL)
167ea022d16SRodney W. Grimes 			fp->set = 0;
168ea022d16SRodney W. Grimes 		else {
169ea022d16SRodney W. Grimes 			fp->set = 1;
170cae66988SJoerg Wunsch 			fp->value = 1 ^ fp->invrt;
171ea022d16SRodney W. Grimes 		}
172ea022d16SRodney W. Grimes 	}
17304a59e67SDavid Nugent 
174cae66988SJoerg Wunsch #ifdef DEBUG
175cae66988SJoerg Wunsch 	printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
176cae66988SJoerg Wunsch 	for (sp = gettystrs; sp->field; sp++)
1771cc15828SDavid Nugent 		printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
178cae66988SJoerg Wunsch 	for (np = gettynums; np->field; np++)
179cae66988SJoerg Wunsch 		printf("cgetnum: %s=%d\r\n", np->field, np->value);
180cae66988SJoerg Wunsch 	for (fp = gettyflags; fp->field; fp++)
181cae66988SJoerg Wunsch 		printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
182cae66988SJoerg Wunsch 		       fp->value + '0', fp->set + '0');
183cae66988SJoerg Wunsch #endif /* DEBUG */
184ea022d16SRodney W. Grimes }
185ea022d16SRodney W. Grimes 
186cae66988SJoerg Wunsch void
187ea022d16SRodney W. Grimes gendefaults()
188ea022d16SRodney W. Grimes {
189ea022d16SRodney W. Grimes 	register struct gettystrs *sp;
190ea022d16SRodney W. Grimes 	register struct gettynums *np;
191ea022d16SRodney W. Grimes 	register struct gettyflags *fp;
192ea022d16SRodney W. Grimes 
193ea022d16SRodney W. Grimes 	for (sp = gettystrs; sp->field; sp++)
194ea022d16SRodney W. Grimes 		if (sp->value)
19504a59e67SDavid Nugent 			sp->defalt = strdup(sp->value);
196ea022d16SRodney W. Grimes 	for (np = gettynums; np->field; np++)
197ea022d16SRodney W. Grimes 		if (np->set)
198ea022d16SRodney W. Grimes 			np->defalt = np->value;
199ea022d16SRodney W. Grimes 	for (fp = gettyflags; fp->field; fp++)
200ea022d16SRodney W. Grimes 		if (fp->set)
201ea022d16SRodney W. Grimes 			fp->defalt = fp->value;
202ea022d16SRodney W. Grimes 		else
203ea022d16SRodney W. Grimes 			fp->defalt = fp->invrt;
204ea022d16SRodney W. Grimes }
205ea022d16SRodney W. Grimes 
206cae66988SJoerg Wunsch void
207ea022d16SRodney W. Grimes setdefaults()
208ea022d16SRodney W. Grimes {
209ea022d16SRodney W. Grimes 	register struct gettystrs *sp;
210ea022d16SRodney W. Grimes 	register struct gettynums *np;
211ea022d16SRodney W. Grimes 	register struct gettyflags *fp;
212ea022d16SRodney W. Grimes 
213ea022d16SRodney W. Grimes 	for (sp = gettystrs; sp->field; sp++)
214ea022d16SRodney W. Grimes 		if (!sp->value)
21504a59e67SDavid Nugent 			sp->value = !sp->defalt ? sp->defalt
21604a59e67SDavid Nugent 						: strdup(sp->defalt);
217ea022d16SRodney W. Grimes 	for (np = gettynums; np->field; np++)
218ea022d16SRodney W. Grimes 		if (!np->set)
219ea022d16SRodney W. Grimes 			np->value = np->defalt;
220ea022d16SRodney W. Grimes 	for (fp = gettyflags; fp->field; fp++)
221ea022d16SRodney W. Grimes 		if (!fp->set)
222ea022d16SRodney W. Grimes 			fp->value = fp->defalt;
223ea022d16SRodney W. Grimes }
224ea022d16SRodney W. Grimes 
225ea022d16SRodney W. Grimes static char **
226ea022d16SRodney W. Grimes charnames[] = {
227ea022d16SRodney W. Grimes 	&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
228ea022d16SRodney W. Grimes 	&SU, &DS, &RP, &FL, &WE, &LN, 0
229ea022d16SRodney W. Grimes };
230ea022d16SRodney W. Grimes 
231ea022d16SRodney W. Grimes static char *
232ea022d16SRodney W. Grimes charvars[] = {
233cae66988SJoerg Wunsch 	&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
234cae66988SJoerg Wunsch 	&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
235cae66988SJoerg Wunsch 	&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
236cae66988SJoerg Wunsch 	&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
237cae66988SJoerg Wunsch 	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
238ea022d16SRodney W. Grimes };
239ea022d16SRodney W. Grimes 
240cae66988SJoerg Wunsch void
241ea022d16SRodney W. Grimes setchars()
242ea022d16SRodney W. Grimes {
243ea022d16SRodney W. Grimes 	register int i;
244cae66988SJoerg Wunsch 	register const char *p;
245ea022d16SRodney W. Grimes 
246ea022d16SRodney W. Grimes 	for (i = 0; charnames[i]; i++) {
247ea022d16SRodney W. Grimes 		p = *charnames[i];
248ea022d16SRodney W. Grimes 		if (p && *p)
249ea022d16SRodney W. Grimes 			*charvars[i] = *p;
250ea022d16SRodney W. Grimes 		else
251cae66988SJoerg Wunsch 			*charvars[i] = _POSIX_VDISABLE;
252ea022d16SRodney W. Grimes 	}
253ea022d16SRodney W. Grimes }
254ea022d16SRodney W. Grimes 
255cae66988SJoerg Wunsch /* Macros to clear/set/test flags. */
256cae66988SJoerg Wunsch #define	SET(t, f)	(t) |= (f)
257cae66988SJoerg Wunsch #define	CLR(t, f)	(t) &= ~(f)
258cae66988SJoerg Wunsch #define	ISSET(t, f)	((t) & (f))
259cae66988SJoerg Wunsch 
260cae66988SJoerg Wunsch void
261ea022d16SRodney W. Grimes setflags(n)
262cae66988SJoerg Wunsch 	int n;
263ea022d16SRodney W. Grimes {
264cae66988SJoerg Wunsch 	register tcflag_t iflag, oflag, cflag, lflag;
265cae66988SJoerg Wunsch 
266cae66988SJoerg Wunsch #ifdef COMPAT_43
267cae66988SJoerg Wunsch 	switch (n) {
268cae66988SJoerg Wunsch 	case 0:
269cae66988SJoerg Wunsch 		if (F0set) {
270cae66988SJoerg Wunsch 			compatflags(F0);
271cae66988SJoerg Wunsch 			return;
272cae66988SJoerg Wunsch 		}
273cae66988SJoerg Wunsch 		break;
274cae66988SJoerg Wunsch 	case 1:
275cae66988SJoerg Wunsch 		if (F1set) {
276cae66988SJoerg Wunsch 			compatflags(F1);
277cae66988SJoerg Wunsch 			return;
278cae66988SJoerg Wunsch 		}
279cae66988SJoerg Wunsch 		break;
280cae66988SJoerg Wunsch 	default:
281cae66988SJoerg Wunsch 		if (F2set) {
282cae66988SJoerg Wunsch 			compatflags(F2);
283cae66988SJoerg Wunsch 			return;
284cae66988SJoerg Wunsch 		}
285cae66988SJoerg Wunsch 		break;
286cae66988SJoerg Wunsch 	}
287cae66988SJoerg Wunsch #endif
288ea022d16SRodney W. Grimes 
289ea022d16SRodney W. Grimes 	switch (n) {
290ea022d16SRodney W. Grimes 	case 0:
291cae66988SJoerg Wunsch 		if (C0set && I0set && L0set && O0set) {
292cae66988SJoerg Wunsch 			tmode.c_cflag = C0;
293cae66988SJoerg Wunsch 			tmode.c_iflag = I0;
294cae66988SJoerg Wunsch 			tmode.c_lflag = L0;
295cae66988SJoerg Wunsch 			tmode.c_oflag = O0;
296cae66988SJoerg Wunsch 			return;
297cae66988SJoerg Wunsch 		}
298ea022d16SRodney W. Grimes 		break;
299ea022d16SRodney W. Grimes 	case 1:
300cae66988SJoerg Wunsch 		if (C1set && I1set && L1set && O1set) {
301cae66988SJoerg Wunsch 			tmode.c_cflag = C1;
302cae66988SJoerg Wunsch 			tmode.c_iflag = I1;
303cae66988SJoerg Wunsch 			tmode.c_lflag = L1;
304cae66988SJoerg Wunsch 			tmode.c_oflag = O1;
305cae66988SJoerg Wunsch 			return;
306cae66988SJoerg Wunsch 		}
307ea022d16SRodney W. Grimes 		break;
308ea022d16SRodney W. Grimes 	default:
309cae66988SJoerg Wunsch 		if (C2set && I2set && L2set && O2set) {
310cae66988SJoerg Wunsch 			tmode.c_cflag = C2;
311cae66988SJoerg Wunsch 			tmode.c_iflag = I2;
312cae66988SJoerg Wunsch 			tmode.c_lflag = L2;
313cae66988SJoerg Wunsch 			tmode.c_oflag = O2;
314cae66988SJoerg Wunsch 			return;
315cae66988SJoerg Wunsch 		}
316ea022d16SRodney W. Grimes 		break;
317ea022d16SRodney W. Grimes 	}
318ea022d16SRodney W. Grimes 
319cae66988SJoerg Wunsch 	iflag = omode.c_iflag;
320cae66988SJoerg Wunsch 	oflag = omode.c_oflag;
321cae66988SJoerg Wunsch 	cflag = omode.c_cflag;
322cae66988SJoerg Wunsch 	lflag = omode.c_lflag;
323ea022d16SRodney W. Grimes 
324cae66988SJoerg Wunsch 	if (NP) {
325cae66988SJoerg Wunsch 		CLR(cflag, CSIZE|PARENB);
326cae66988SJoerg Wunsch 		SET(cflag, CS8);
327cae66988SJoerg Wunsch 		CLR(iflag, ISTRIP|INPCK|IGNPAR);
328cae66988SJoerg Wunsch 	} else if (AP || EP || OP) {
329cae66988SJoerg Wunsch 		CLR(cflag, CSIZE);
330cae66988SJoerg Wunsch 		SET(cflag, CS7|PARENB);
331cae66988SJoerg Wunsch 		SET(iflag, ISTRIP);
332cae66988SJoerg Wunsch 		if (OP && !EP) {
333cae66988SJoerg Wunsch 			SET(iflag, INPCK|IGNPAR);
334cae66988SJoerg Wunsch 			SET(cflag, PARODD);
335ea022d16SRodney W. Grimes 			if (AP)
336cae66988SJoerg Wunsch 				CLR(iflag, INPCK);
337cae66988SJoerg Wunsch 		} else if (EP && !OP) {
338cae66988SJoerg Wunsch 			SET(iflag, INPCK|IGNPAR);
339cae66988SJoerg Wunsch 			CLR(cflag, PARODD);
340cae66988SJoerg Wunsch 			if (AP)
341cae66988SJoerg Wunsch 				CLR(iflag, INPCK);
342cae66988SJoerg Wunsch 		} else if (AP || (EP && OP)) {
343cae66988SJoerg Wunsch 			CLR(iflag, INPCK|IGNPAR);
344cae66988SJoerg Wunsch 			CLR(cflag, PARODD);
345cae66988SJoerg Wunsch 		}
346cae66988SJoerg Wunsch 	} /* else, leave as is */
347ea022d16SRodney W. Grimes 
348cae66988SJoerg Wunsch #if 0
349ea022d16SRodney W. Grimes 	if (UC)
350ea022d16SRodney W. Grimes 		f |= LCASE;
351cae66988SJoerg Wunsch #endif
352ea022d16SRodney W. Grimes 
353cae66988SJoerg Wunsch 	if (HC)
354cae66988SJoerg Wunsch 		SET(cflag, HUPCL);
355ea022d16SRodney W. Grimes 	else
356cae66988SJoerg Wunsch 		CLR(cflag, HUPCL);
357cae66988SJoerg Wunsch 
358cae66988SJoerg Wunsch 	if (MB)
359cae66988SJoerg Wunsch 		SET(cflag, MDMBUF);
360cae66988SJoerg Wunsch 	else
361cae66988SJoerg Wunsch 		CLR(cflag, MDMBUF);
362cae66988SJoerg Wunsch 
363fe552114SDavid Nugent 	if (HW)
364fe552114SDavid Nugent 		SET(cflag, CRTSCTS);
365fe552114SDavid Nugent 	else
366fe552114SDavid Nugent 		CLR(cflag, CRTSCTS);
367fe552114SDavid Nugent 
368cae66988SJoerg Wunsch 	if (NL) {
369cae66988SJoerg Wunsch 		SET(iflag, ICRNL);
370cae66988SJoerg Wunsch 		SET(oflag, ONLCR|OPOST);
371cae66988SJoerg Wunsch 	} else {
372cae66988SJoerg Wunsch 		CLR(iflag, ICRNL);
373cae66988SJoerg Wunsch 		CLR(oflag, ONLCR);
374ea022d16SRodney W. Grimes 	}
375ea022d16SRodney W. Grimes 
376ea022d16SRodney W. Grimes 	if (!HT)
377cae66988SJoerg Wunsch 		SET(oflag, OXTABS|OPOST);
378cae66988SJoerg Wunsch 	else
379cae66988SJoerg Wunsch 		CLR(oflag, OXTABS);
380ea022d16SRodney W. Grimes 
381cae66988SJoerg Wunsch #ifdef XXX_DELAY
382cae66988SJoerg Wunsch 	SET(f, delaybits());
383cae66988SJoerg Wunsch #endif
384ea022d16SRodney W. Grimes 
385cae66988SJoerg Wunsch 	if (n == 1) {		/* read mode flags */
386cae66988SJoerg Wunsch 		if (RW) {
387cae66988SJoerg Wunsch 			iflag = 0;
388cae66988SJoerg Wunsch 			CLR(oflag, OPOST);
389cae66988SJoerg Wunsch 			CLR(cflag, CSIZE|PARENB);
390cae66988SJoerg Wunsch 			SET(cflag, CS8);
391cae66988SJoerg Wunsch 			lflag = 0;
392cae66988SJoerg Wunsch 		} else {
393cae66988SJoerg Wunsch 			CLR(lflag, ICANON);
394cae66988SJoerg Wunsch 		}
395cae66988SJoerg Wunsch 		goto out;
396ea022d16SRodney W. Grimes 	}
397ea022d16SRodney W. Grimes 
398cae66988SJoerg Wunsch 	if (n == 0)
399cae66988SJoerg Wunsch 		goto out;
400cae66988SJoerg Wunsch 
401cae66988SJoerg Wunsch #if 0
402cae66988SJoerg Wunsch 	if (CB)
403cae66988SJoerg Wunsch 		SET(f, CRTBS);
404cae66988SJoerg Wunsch #endif
405cae66988SJoerg Wunsch 
406cae66988SJoerg Wunsch 	if (CE)
407cae66988SJoerg Wunsch 		SET(lflag, ECHOE);
408cae66988SJoerg Wunsch 	else
409cae66988SJoerg Wunsch 		CLR(lflag, ECHOE);
410cae66988SJoerg Wunsch 
411cae66988SJoerg Wunsch 	if (CK)
412cae66988SJoerg Wunsch 		SET(lflag, ECHOKE);
413cae66988SJoerg Wunsch 	else
414cae66988SJoerg Wunsch 		CLR(lflag, ECHOKE);
415cae66988SJoerg Wunsch 
416cae66988SJoerg Wunsch 	if (PE)
417cae66988SJoerg Wunsch 		SET(lflag, ECHOPRT);
418cae66988SJoerg Wunsch 	else
419cae66988SJoerg Wunsch 		CLR(lflag, ECHOPRT);
420cae66988SJoerg Wunsch 
421cae66988SJoerg Wunsch 	if (EC)
422cae66988SJoerg Wunsch 		SET(lflag, ECHO);
423cae66988SJoerg Wunsch 	else
424cae66988SJoerg Wunsch 		CLR(lflag, ECHO);
425cae66988SJoerg Wunsch 
426cae66988SJoerg Wunsch 	if (XC)
427cae66988SJoerg Wunsch 		SET(lflag, ECHOCTL);
428cae66988SJoerg Wunsch 	else
429cae66988SJoerg Wunsch 		CLR(lflag, ECHOCTL);
430cae66988SJoerg Wunsch 
431cae66988SJoerg Wunsch 	if (DX)
432cae66988SJoerg Wunsch 		SET(lflag, IXANY);
433cae66988SJoerg Wunsch 	else
434cae66988SJoerg Wunsch 		CLR(lflag, IXANY);
435cae66988SJoerg Wunsch 
436cae66988SJoerg Wunsch out:
437cae66988SJoerg Wunsch 	tmode.c_iflag = iflag;
438cae66988SJoerg Wunsch 	tmode.c_oflag = oflag;
439cae66988SJoerg Wunsch 	tmode.c_cflag = cflag;
440cae66988SJoerg Wunsch 	tmode.c_lflag = lflag;
441cae66988SJoerg Wunsch }
442cae66988SJoerg Wunsch 
443cae66988SJoerg Wunsch #ifdef COMPAT_43
444cae66988SJoerg Wunsch /*
445cae66988SJoerg Wunsch  * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
446cae66988SJoerg Wunsch  */
447cae66988SJoerg Wunsch void
448cae66988SJoerg Wunsch compatflags(flags)
449cae66988SJoerg Wunsch register long flags;
450cae66988SJoerg Wunsch {
451cae66988SJoerg Wunsch 	register tcflag_t iflag, oflag, cflag, lflag;
452cae66988SJoerg Wunsch 
453cae66988SJoerg Wunsch 	iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
454cae66988SJoerg Wunsch 	oflag = OPOST|ONLCR|OXTABS;
455cae66988SJoerg Wunsch 	cflag = CREAD;
456cae66988SJoerg Wunsch 	lflag = ICANON|ISIG|IEXTEN;
457cae66988SJoerg Wunsch 
458cae66988SJoerg Wunsch 	if (ISSET(flags, TANDEM))
459cae66988SJoerg Wunsch 		SET(iflag, IXOFF);
460cae66988SJoerg Wunsch 	else
461cae66988SJoerg Wunsch 		CLR(iflag, IXOFF);
462cae66988SJoerg Wunsch 	if (ISSET(flags, ECHO))
463cae66988SJoerg Wunsch 		SET(lflag, ECHO);
464cae66988SJoerg Wunsch 	else
465cae66988SJoerg Wunsch 		CLR(lflag, ECHO);
466cae66988SJoerg Wunsch 	if (ISSET(flags, CRMOD)) {
467cae66988SJoerg Wunsch 		SET(iflag, ICRNL);
468cae66988SJoerg Wunsch 		SET(oflag, ONLCR);
469cae66988SJoerg Wunsch 	} else {
470cae66988SJoerg Wunsch 		CLR(iflag, ICRNL);
471cae66988SJoerg Wunsch 		CLR(oflag, ONLCR);
472cae66988SJoerg Wunsch 	}
473cae66988SJoerg Wunsch 	if (ISSET(flags, XTABS))
474cae66988SJoerg Wunsch 		SET(oflag, OXTABS);
475cae66988SJoerg Wunsch 	else
476cae66988SJoerg Wunsch 		CLR(oflag, OXTABS);
477cae66988SJoerg Wunsch 
478cae66988SJoerg Wunsch 
479cae66988SJoerg Wunsch 	if (ISSET(flags, RAW)) {
480cae66988SJoerg Wunsch 		iflag &= IXOFF;
481cae66988SJoerg Wunsch 		CLR(lflag, ISIG|ICANON|IEXTEN);
482cae66988SJoerg Wunsch 		CLR(cflag, PARENB);
483cae66988SJoerg Wunsch 	} else {
484cae66988SJoerg Wunsch 		SET(iflag, BRKINT|IXON|IMAXBEL);
485cae66988SJoerg Wunsch 		SET(lflag, ISIG|IEXTEN);
486cae66988SJoerg Wunsch 		if (ISSET(flags, CBREAK))
487cae66988SJoerg Wunsch 			CLR(lflag, ICANON);
488cae66988SJoerg Wunsch 		else
489cae66988SJoerg Wunsch 			SET(lflag, ICANON);
490cae66988SJoerg Wunsch 		switch (ISSET(flags, ANYP)) {
491cae66988SJoerg Wunsch 		case 0:
492cae66988SJoerg Wunsch 			CLR(cflag, PARENB);
493cae66988SJoerg Wunsch 			break;
494cae66988SJoerg Wunsch 		case ANYP:
495cae66988SJoerg Wunsch 			SET(cflag, PARENB);
496cae66988SJoerg Wunsch 			CLR(iflag, INPCK);
497cae66988SJoerg Wunsch 			break;
498cae66988SJoerg Wunsch 		case EVENP:
499cae66988SJoerg Wunsch 			SET(cflag, PARENB);
500cae66988SJoerg Wunsch 			SET(iflag, INPCK);
501cae66988SJoerg Wunsch 			CLR(cflag, PARODD);
502cae66988SJoerg Wunsch 			break;
503cae66988SJoerg Wunsch 		case ODDP:
504cae66988SJoerg Wunsch 			SET(cflag, PARENB);
505cae66988SJoerg Wunsch 			SET(iflag, INPCK);
506cae66988SJoerg Wunsch 			SET(cflag, PARODD);
507cae66988SJoerg Wunsch 			break;
508cae66988SJoerg Wunsch 		}
509cae66988SJoerg Wunsch 	}
510cae66988SJoerg Wunsch 
511cae66988SJoerg Wunsch 	/* Nothing we can do with CRTBS. */
512cae66988SJoerg Wunsch 	if (ISSET(flags, PRTERA))
513cae66988SJoerg Wunsch 		SET(lflag, ECHOPRT);
514cae66988SJoerg Wunsch 	else
515cae66988SJoerg Wunsch 		CLR(lflag, ECHOPRT);
516cae66988SJoerg Wunsch 	if (ISSET(flags, CRTERA))
517cae66988SJoerg Wunsch 		SET(lflag, ECHOE);
518cae66988SJoerg Wunsch 	else
519cae66988SJoerg Wunsch 		CLR(lflag, ECHOE);
520cae66988SJoerg Wunsch 	/* Nothing we can do with TILDE. */
521cae66988SJoerg Wunsch 	if (ISSET(flags, MDMBUF))
522cae66988SJoerg Wunsch 		SET(cflag, MDMBUF);
523cae66988SJoerg Wunsch 	else
524cae66988SJoerg Wunsch 		CLR(cflag, MDMBUF);
525cae66988SJoerg Wunsch 	if (ISSET(flags, NOHANG))
526cae66988SJoerg Wunsch 		CLR(cflag, HUPCL);
527cae66988SJoerg Wunsch 	else
528cae66988SJoerg Wunsch 		SET(cflag, HUPCL);
529cae66988SJoerg Wunsch 	if (ISSET(flags, CRTKIL))
530cae66988SJoerg Wunsch 		SET(lflag, ECHOKE);
531cae66988SJoerg Wunsch 	else
532cae66988SJoerg Wunsch 		CLR(lflag, ECHOKE);
533cae66988SJoerg Wunsch 	if (ISSET(flags, CTLECH))
534cae66988SJoerg Wunsch 		SET(lflag, ECHOCTL);
535cae66988SJoerg Wunsch 	else
536cae66988SJoerg Wunsch 		CLR(lflag, ECHOCTL);
537cae66988SJoerg Wunsch 	if (!ISSET(flags, DECCTQ))
538cae66988SJoerg Wunsch 		SET(iflag, IXANY);
539cae66988SJoerg Wunsch 	else
540cae66988SJoerg Wunsch 		CLR(iflag, IXANY);
541cae66988SJoerg Wunsch 	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
542cae66988SJoerg Wunsch 	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
543cae66988SJoerg Wunsch 
544cae66988SJoerg Wunsch 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
545cae66988SJoerg Wunsch 		CLR(cflag, CSIZE);
546cae66988SJoerg Wunsch 		SET(cflag, CS8);
547cae66988SJoerg Wunsch 		if (!ISSET(flags, RAW|PASS8))
548cae66988SJoerg Wunsch 			SET(iflag, ISTRIP);
549cae66988SJoerg Wunsch 		else
550cae66988SJoerg Wunsch 			CLR(iflag, ISTRIP);
551cae66988SJoerg Wunsch 		if (!ISSET(flags, RAW|LITOUT))
552cae66988SJoerg Wunsch 			SET(oflag, OPOST);
553cae66988SJoerg Wunsch 		else
554cae66988SJoerg Wunsch 			CLR(oflag, OPOST);
555cae66988SJoerg Wunsch 	} else {
556cae66988SJoerg Wunsch 		CLR(cflag, CSIZE);
557cae66988SJoerg Wunsch 		SET(cflag, CS7);
558cae66988SJoerg Wunsch 		SET(iflag, ISTRIP);
559cae66988SJoerg Wunsch 		SET(oflag, OPOST);
560cae66988SJoerg Wunsch 	}
561cae66988SJoerg Wunsch 
562cae66988SJoerg Wunsch 	tmode.c_iflag = iflag;
563cae66988SJoerg Wunsch 	tmode.c_oflag = oflag;
564cae66988SJoerg Wunsch 	tmode.c_cflag = cflag;
565cae66988SJoerg Wunsch 	tmode.c_lflag = lflag;
566cae66988SJoerg Wunsch }
567cae66988SJoerg Wunsch #endif
568cae66988SJoerg Wunsch 
569cae66988SJoerg Wunsch #ifdef XXX_DELAY
570ea022d16SRodney W. Grimes struct delayval {
571ea022d16SRodney W. Grimes 	unsigned	delay;		/* delay in ms */
572ea022d16SRodney W. Grimes 	int		bits;
573ea022d16SRodney W. Grimes };
574ea022d16SRodney W. Grimes 
575ea022d16SRodney W. Grimes /*
576ea022d16SRodney W. Grimes  * below are random guesses, I can't be bothered checking
577ea022d16SRodney W. Grimes  */
578ea022d16SRodney W. Grimes 
579ea022d16SRodney W. Grimes struct delayval	crdelay[] = {
580cae66988SJoerg Wunsch 	{ 1,		CR1 },
581cae66988SJoerg Wunsch 	{ 2,		CR2 },
582cae66988SJoerg Wunsch 	{ 3,		CR3 },
583cae66988SJoerg Wunsch 	{ 83,		CR1 },
584cae66988SJoerg Wunsch 	{ 166,		CR2 },
585cae66988SJoerg Wunsch 	{ 0,		CR3 },
586ea022d16SRodney W. Grimes };
587ea022d16SRodney W. Grimes 
588ea022d16SRodney W. Grimes struct delayval nldelay[] = {
589cae66988SJoerg Wunsch 	{ 1,		NL1 },		/* special, calculated */
590cae66988SJoerg Wunsch 	{ 2,		NL2 },
591cae66988SJoerg Wunsch 	{ 3,		NL3 },
592cae66988SJoerg Wunsch 	{ 100,		NL2 },
593cae66988SJoerg Wunsch 	{ 0,		NL3 },
594ea022d16SRodney W. Grimes };
595ea022d16SRodney W. Grimes 
596ea022d16SRodney W. Grimes struct delayval	bsdelay[] = {
597cae66988SJoerg Wunsch 	{ 1,		BS1 },
598cae66988SJoerg Wunsch 	{ 0,		0 },
599ea022d16SRodney W. Grimes };
600ea022d16SRodney W. Grimes 
601ea022d16SRodney W. Grimes struct delayval	ffdelay[] = {
602cae66988SJoerg Wunsch 	{ 1,		FF1 },
603cae66988SJoerg Wunsch 	{ 1750,		FF1 },
604cae66988SJoerg Wunsch 	{ 0,		FF1 },
605ea022d16SRodney W. Grimes };
606ea022d16SRodney W. Grimes 
607ea022d16SRodney W. Grimes struct delayval	tbdelay[] = {
608cae66988SJoerg Wunsch 	{ 1,		TAB1 },
609cae66988SJoerg Wunsch 	{ 2,		TAB2 },
610cae66988SJoerg Wunsch 	{ 3,		XTABS },	/* this is expand tabs */
611cae66988SJoerg Wunsch 	{ 100,		TAB1 },
612cae66988SJoerg Wunsch 	{ 0,		TAB2 },
613ea022d16SRodney W. Grimes };
614ea022d16SRodney W. Grimes 
615cae66988SJoerg Wunsch int
616ea022d16SRodney W. Grimes delaybits()
617ea022d16SRodney W. Grimes {
618cae66988SJoerg Wunsch 	register int f;
619ea022d16SRodney W. Grimes 
620ea022d16SRodney W. Grimes 	f  = adelay(CD, crdelay);
621ea022d16SRodney W. Grimes 	f |= adelay(ND, nldelay);
622ea022d16SRodney W. Grimes 	f |= adelay(FD, ffdelay);
623ea022d16SRodney W. Grimes 	f |= adelay(TD, tbdelay);
624ea022d16SRodney W. Grimes 	f |= adelay(BD, bsdelay);
625ea022d16SRodney W. Grimes 	return (f);
626ea022d16SRodney W. Grimes }
627ea022d16SRodney W. Grimes 
628cae66988SJoerg Wunsch int
629ea022d16SRodney W. Grimes adelay(ms, dp)
630ea022d16SRodney W. Grimes 	register ms;
631ea022d16SRodney W. Grimes 	register struct delayval *dp;
632ea022d16SRodney W. Grimes {
633ea022d16SRodney W. Grimes 	if (ms == 0)
634ea022d16SRodney W. Grimes 		return (0);
635ea022d16SRodney W. Grimes 	while (dp->delay && ms > dp->delay)
636ea022d16SRodney W. Grimes 		dp++;
637ea022d16SRodney W. Grimes 	return (dp->bits);
638ea022d16SRodney W. Grimes }
639cae66988SJoerg Wunsch #endif
640ea022d16SRodney W. Grimes 
641c568fce9SAndrey A. Chernov char	editedhost[MAXHOSTNAMELEN];
642ea022d16SRodney W. Grimes 
643cae66988SJoerg Wunsch void
644ea022d16SRodney W. Grimes edithost(pat)
645cae66988SJoerg Wunsch 	register const char *pat;
646ea022d16SRodney W. Grimes {
647cae66988SJoerg Wunsch 	register const char *host = HN;
648ea022d16SRodney W. Grimes 	register char *res = editedhost;
649ea022d16SRodney W. Grimes 
650ea022d16SRodney W. Grimes 	if (!pat)
651ea022d16SRodney W. Grimes 		pat = "";
652ea022d16SRodney W. Grimes 	while (*pat) {
653ea022d16SRodney W. Grimes 		switch (*pat) {
654ea022d16SRodney W. Grimes 
655ea022d16SRodney W. Grimes 		case '#':
656ea022d16SRodney W. Grimes 			if (*host)
657ea022d16SRodney W. Grimes 				host++;
658ea022d16SRodney W. Grimes 			break;
659ea022d16SRodney W. Grimes 
660ea022d16SRodney W. Grimes 		case '@':
661ea022d16SRodney W. Grimes 			if (*host)
662ea022d16SRodney W. Grimes 				*res++ = *host++;
663ea022d16SRodney W. Grimes 			break;
664ea022d16SRodney W. Grimes 
665ea022d16SRodney W. Grimes 		default:
666ea022d16SRodney W. Grimes 			*res++ = *pat;
667ea022d16SRodney W. Grimes 			break;
668ea022d16SRodney W. Grimes 
669ea022d16SRodney W. Grimes 		}
670ea022d16SRodney W. Grimes 		if (res == &editedhost[sizeof editedhost - 1]) {
671ea022d16SRodney W. Grimes 			*res = '\0';
672ea022d16SRodney W. Grimes 			return;
673ea022d16SRodney W. Grimes 		}
674ea022d16SRodney W. Grimes 		pat++;
675ea022d16SRodney W. Grimes 	}
676ea022d16SRodney W. Grimes 	if (*host)
677ea022d16SRodney W. Grimes 		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
678ea022d16SRodney W. Grimes 	else
679ea022d16SRodney W. Grimes 		*res = '\0';
680ea022d16SRodney W. Grimes 	editedhost[sizeof editedhost - 1] = '\0';
681ea022d16SRodney W. Grimes }
682ea022d16SRodney W. Grimes 
683cae66988SJoerg Wunsch static struct speedtab {
684ea022d16SRodney W. Grimes 	int	speed;
685ea022d16SRodney W. Grimes 	int	uxname;
686ea022d16SRodney W. Grimes } speedtab[] = {
687cae66988SJoerg Wunsch 	{ 50,	B50 },
688cae66988SJoerg Wunsch 	{ 75,	B75 },
689cae66988SJoerg Wunsch 	{ 110,	B110 },
690cae66988SJoerg Wunsch 	{ 134,	B134 },
691cae66988SJoerg Wunsch 	{ 150,	B150 },
692cae66988SJoerg Wunsch 	{ 200,	B200 },
693cae66988SJoerg Wunsch 	{ 300,	B300 },
694cae66988SJoerg Wunsch 	{ 600,	B600 },
695cae66988SJoerg Wunsch 	{ 1200,	B1200 },
696cae66988SJoerg Wunsch 	{ 1800,	B1800 },
697cae66988SJoerg Wunsch 	{ 2400,	B2400 },
698cae66988SJoerg Wunsch 	{ 4800,	B4800 },
699cae66988SJoerg Wunsch 	{ 9600,	B9600 },
700cae66988SJoerg Wunsch 	{ 19200, EXTA },
701cae66988SJoerg Wunsch 	{ 19,	EXTA },		/* for people who say 19.2K */
702cae66988SJoerg Wunsch 	{ 38400, EXTB },
703cae66988SJoerg Wunsch 	{ 38,	EXTB },
704cae66988SJoerg Wunsch 	{ 7200,	EXTB },		/* alternative */
705cae66988SJoerg Wunsch 	{ 57600, B57600 },
706cae66988SJoerg Wunsch 	{ 115200, B115200 },
707cae66988SJoerg Wunsch 	{ 0 }
708ea022d16SRodney W. Grimes };
709ea022d16SRodney W. Grimes 
710cae66988SJoerg Wunsch int
711ea022d16SRodney W. Grimes speed(val)
712cae66988SJoerg Wunsch 	int val;
713ea022d16SRodney W. Grimes {
714ea022d16SRodney W. Grimes 	register struct speedtab *sp;
715ea022d16SRodney W. Grimes 
7169aa70e27SAndrey A. Chernov 	if (val <= B115200)
717ea022d16SRodney W. Grimes 		return (val);
718ea022d16SRodney W. Grimes 
719ea022d16SRodney W. Grimes 	for (sp = speedtab; sp->speed; sp++)
720ea022d16SRodney W. Grimes 		if (sp->speed == val)
721ea022d16SRodney W. Grimes 			return (sp->uxname);
722ea022d16SRodney W. Grimes 
723ea022d16SRodney W. Grimes 	return (B300);		/* default in impossible cases */
724ea022d16SRodney W. Grimes }
725ea022d16SRodney W. Grimes 
726cae66988SJoerg Wunsch void
727ea022d16SRodney W. Grimes makeenv(env)
728ea022d16SRodney W. Grimes 	char *env[];
729ea022d16SRodney W. Grimes {
730ea022d16SRodney W. Grimes 	static char termbuf[128] = "TERM=";
731ea022d16SRodney W. Grimes 	register char *p, *q;
732ea022d16SRodney W. Grimes 	register char **ep;
733ea022d16SRodney W. Grimes 
734ea022d16SRodney W. Grimes 	ep = env;
735ea022d16SRodney W. Grimes 	if (TT && *TT) {
736ea022d16SRodney W. Grimes 		strcat(termbuf, TT);
737ea022d16SRodney W. Grimes 		*ep++ = termbuf;
738ea022d16SRodney W. Grimes 	}
739cae66988SJoerg Wunsch 	if ((p = EV)) {
740ea022d16SRodney W. Grimes 		q = p;
741cae66988SJoerg Wunsch 		while ((q = strchr(q, ','))) {
742ea022d16SRodney W. Grimes 			*q++ = '\0';
743ea022d16SRodney W. Grimes 			*ep++ = p;
744ea022d16SRodney W. Grimes 			p = q;
745ea022d16SRodney W. Grimes 		}
746ea022d16SRodney W. Grimes 		if (*p)
747ea022d16SRodney W. Grimes 			*ep++ = p;
748ea022d16SRodney W. Grimes 	}
749ea022d16SRodney W. Grimes 	*ep = (char *)0;
750ea022d16SRodney W. Grimes }
751ea022d16SRodney W. Grimes 
752ea022d16SRodney W. Grimes /*
753ea022d16SRodney W. Grimes  * This speed select mechanism is written for the Develcon DATASWITCH.
754ea022d16SRodney W. Grimes  * The Develcon sends a string of the form "B{speed}\n" at a predefined
755ea022d16SRodney W. Grimes  * baud rate. This string indicates the user's actual speed.
756ea022d16SRodney W. Grimes  * The routine below returns the terminal type mapped from derived speed.
757ea022d16SRodney W. Grimes  */
758ea022d16SRodney W. Grimes struct	portselect {
759cae66988SJoerg Wunsch 	const char	*ps_baud;
760cae66988SJoerg Wunsch 	const char	*ps_type;
761ea022d16SRodney W. Grimes } portspeeds[] = {
762ea022d16SRodney W. Grimes 	{ "B110",	"std.110" },
763ea022d16SRodney W. Grimes 	{ "B134",	"std.134" },
764ea022d16SRodney W. Grimes 	{ "B150",	"std.150" },
765ea022d16SRodney W. Grimes 	{ "B300",	"std.300" },
766ea022d16SRodney W. Grimes 	{ "B600",	"std.600" },
767ea022d16SRodney W. Grimes 	{ "B1200",	"std.1200" },
768ea022d16SRodney W. Grimes 	{ "B2400",	"std.2400" },
769ea022d16SRodney W. Grimes 	{ "B4800",	"std.4800" },
770ea022d16SRodney W. Grimes 	{ "B9600",	"std.9600" },
771ea022d16SRodney W. Grimes 	{ "B19200",	"std.19200" },
772ea022d16SRodney W. Grimes 	{ 0 }
773ea022d16SRodney W. Grimes };
774ea022d16SRodney W. Grimes 
775cae66988SJoerg Wunsch const char *
776ea022d16SRodney W. Grimes portselector()
777ea022d16SRodney W. Grimes {
778cae66988SJoerg Wunsch 	char c, baud[20];
779cae66988SJoerg Wunsch 	const char *type = "default";
780ea022d16SRodney W. Grimes 	register struct portselect *ps;
781ea022d16SRodney W. Grimes 	int len;
782ea022d16SRodney W. Grimes 
783ea022d16SRodney W. Grimes 	alarm(5*60);
784ea022d16SRodney W. Grimes 	for (len = 0; len < sizeof (baud) - 1; len++) {
785ea022d16SRodney W. Grimes 		if (read(STDIN_FILENO, &c, 1) <= 0)
786ea022d16SRodney W. Grimes 			break;
787ea022d16SRodney W. Grimes 		c &= 0177;
788ea022d16SRodney W. Grimes 		if (c == '\n' || c == '\r')
789ea022d16SRodney W. Grimes 			break;
790ea022d16SRodney W. Grimes 		if (c == 'B')
791ea022d16SRodney W. Grimes 			len = 0;	/* in case of leading garbage */
792ea022d16SRodney W. Grimes 		baud[len] = c;
793ea022d16SRodney W. Grimes 	}
794ea022d16SRodney W. Grimes 	baud[len] = '\0';
795ea022d16SRodney W. Grimes 	for (ps = portspeeds; ps->ps_baud; ps++)
796ea022d16SRodney W. Grimes 		if (strcmp(ps->ps_baud, baud) == 0) {
797ea022d16SRodney W. Grimes 			type = ps->ps_type;
798ea022d16SRodney W. Grimes 			break;
799ea022d16SRodney W. Grimes 		}
800ea022d16SRodney W. Grimes 	sleep(2);	/* wait for connection to complete */
801ea022d16SRodney W. Grimes 	return (type);
802ea022d16SRodney W. Grimes }
803ea022d16SRodney W. Grimes 
804ea022d16SRodney W. Grimes /*
805ea022d16SRodney W. Grimes  * This auto-baud speed select mechanism is written for the Micom 600
806ea022d16SRodney W. Grimes  * portselector. Selection is done by looking at how the character '\r'
807ea022d16SRodney W. Grimes  * is garbled at the different speeds.
808ea022d16SRodney W. Grimes  */
809cae66988SJoerg Wunsch const char *
810ea022d16SRodney W. Grimes autobaud()
811ea022d16SRodney W. Grimes {
812ea022d16SRodney W. Grimes 	int rfds;
813ea022d16SRodney W. Grimes 	struct timeval timeout;
814cae66988SJoerg Wunsch 	char c;
815cae66988SJoerg Wunsch 	const char *type = "9600-baud";
816ea022d16SRodney W. Grimes 
817cae66988SJoerg Wunsch 	(void)tcflush(0, TCIOFLUSH);
818ea022d16SRodney W. Grimes 	rfds = 1 << 0;
819ea022d16SRodney W. Grimes 	timeout.tv_sec = 5;
820ea022d16SRodney W. Grimes 	timeout.tv_usec = 0;
821ea022d16SRodney W. Grimes 	if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
822ea022d16SRodney W. Grimes 	    (fd_set *)NULL, &timeout) <= 0)
823ea022d16SRodney W. Grimes 		return (type);
824ea022d16SRodney W. Grimes 	if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
825ea022d16SRodney W. Grimes 		return (type);
826ea022d16SRodney W. Grimes 	timeout.tv_sec = 0;
827ea022d16SRodney W. Grimes 	timeout.tv_usec = 20;
828ea022d16SRodney W. Grimes 	(void) select(32, (fd_set *)NULL, (fd_set *)NULL,
829ea022d16SRodney W. Grimes 	    (fd_set *)NULL, &timeout);
830cae66988SJoerg Wunsch 	(void)tcflush(0, TCIOFLUSH);
831ea022d16SRodney W. Grimes 	switch (c & 0377) {
832ea022d16SRodney W. Grimes 
833ea022d16SRodney W. Grimes 	case 0200:		/* 300-baud */
834ea022d16SRodney W. Grimes 		type = "300-baud";
835ea022d16SRodney W. Grimes 		break;
836ea022d16SRodney W. Grimes 
837ea022d16SRodney W. Grimes 	case 0346:		/* 1200-baud */
838ea022d16SRodney W. Grimes 		type = "1200-baud";
839ea022d16SRodney W. Grimes 		break;
840ea022d16SRodney W. Grimes 
841ea022d16SRodney W. Grimes 	case  015:		/* 2400-baud */
842ea022d16SRodney W. Grimes 	case 0215:
843ea022d16SRodney W. Grimes 		type = "2400-baud";
844ea022d16SRodney W. Grimes 		break;
845ea022d16SRodney W. Grimes 
846ea022d16SRodney W. Grimes 	default:		/* 4800-baud */
847ea022d16SRodney W. Grimes 		type = "4800-baud";
848ea022d16SRodney W. Grimes 		break;
849ea022d16SRodney W. Grimes 
850ea022d16SRodney W. Grimes 	case 0377:		/* 9600-baud */
851ea022d16SRodney W. Grimes 		type = "9600-baud";
852ea022d16SRodney W. Grimes 		break;
853ea022d16SRodney W. Grimes 	}
854ea022d16SRodney W. Grimes 	return (type);
855ea022d16SRodney W. Grimes }
856