subr.c revision 31331
1139823Simp/*
211150Swollman * Copyright (c) 1983, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2911150Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
32172467Ssilby */
33172467Ssilby
34172467Ssilby#ifndef lint
35125680Sbms#if 0
3654263Sshinstatic char sccsid[] = "@(#)from: subr.c	8.1 (Berkeley) 6/4/93";
3729514Sjoerg#endif
3829514Sjoergstatic const char rcsid[] =
391541Srgrimes	"$Id$";
401541Srgrimes#endif /* not lint */
4150673Sjlemon
4212172Sphk/*
4312172Sphk * Melbourne getty.
441541Srgrimes */
451541Srgrimes#define COMPAT_43
46164033Srwatson#ifdef DEBUG
4748758Sgreen#include <stdio.h>
481541Srgrimes#endif
491541Srgrimes#include <stdlib.h>
501541Srgrimes#include <string.h>
5175619Skris#include <termios.h>
5234923Sbde#include <unistd.h>
5392760Sjeff#include <sys/ioctl.h>
541541Srgrimes#include <sys/param.h>
551541Srgrimes#include <sys/time.h>
561541Srgrimes#include <syslog.h>
57196019Srwatson
581541Srgrimes#include "gettytab.h"
591541Srgrimes#include "pathnames.h"
60221250Sbz#include "extern.h"
611541Srgrimes
62221250Sbz
631541Srgrimes#ifdef COMPAT_43
64221250Sbzstatic void	compatflags __P((long));
65221250Sbz#endif
6655679Sshin
6755679Sshin/*
6855679Sshin * Get a table entry.
6955679Sshin */
70148385Sumevoid
71122922Sandregettable(name, buf)
7255679Sshin	const char *name;
731541Srgrimes	char *buf;
741541Srgrimes{
751541Srgrimes	register struct gettystrs *sp;
761541Srgrimes	register struct gettynums *np;
771541Srgrimes	register struct gettyflags *fp;
7855679Sshin	long n;
7955679Sshin	int l;
8055679Sshin	char *p;
811541Srgrimes	char *msg = NULL;
826283Swollman	const char *dba[2];
836283Swollman
846283Swollman	static int firsttime = 1;
85221250Sbz
8655679Sshin	dba[0] = _PATH_GETTYTAB;
87221250Sbz	dba[1] = 0;
881541Srgrimes
8958698Sjlemon	if (firsttime) {
9058698Sjlemon		/*
91163606Srwatson		 * we need to strdup() anything in the strings array
92163606Srwatson		 * initially in order to simplify things later
93215701Sdim		 */
94207369Sbz		for (sp = gettystrs; sp->field; sp++)
95157478Sglebius			if (sp->value != NULL) {
96162767Ssilby				/* handle these ones more carefully */
97169608Sandre				if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
98169608Sandre					l = 2;
99169608Sandre				else
100169608Sandre					l = strlen(sp->value) + 1;
101169608Sandre				if ((p = malloc(l)) != NULL) {
102169608Sandre					strncpy(p, sp->value, l);
103215701Sdim					p[l-1] = '\0';
104195727Srwatson				}
105195699Srwatson				/*
106169608Sandre				 * replace, even if NULL, else we'll
107169608Sandre				 * have problems with free()ing static mem
108169608Sandre				 */
109162031Sglebius				sp->value = p;
110162767Ssilby			}
111162767Ssilby		firsttime = 0;
112162767Ssilby	}
113162767Ssilby
114162767Ssilby	switch (cgetent(&buf, (char **)dba, (char *)name)) {
115162768Smaxim	case 1:
116162768Smaxim		msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
117162768Smaxim	case 0:
118181803Sbz		break;
119181803Sbz	case -1:
120162767Ssilby		msg = "%s: unknown gettytab entry '%s'";
121181803Sbz		break;
122162767Ssilby	case -2:
123162767Ssilby		msg = "%s: retrieving gettytab entry '%s': %m";
124162767Ssilby		break;
125162767Ssilby	case -3:
126162767Ssilby		msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
127162031Sglebius		break;
128162031Sglebius	default:
129162031Sglebius		msg = "%s: unexpected cgetent() error for entry '%s'";
130157478Sglebius		break;
131162031Sglebius	}
132162767Ssilby
133162031Sglebius	if (msg != NULL) {
134162031Sglebius		syslog(LOG_ERR, msg, "getty", name);
135170289Sdwmalone		return;
136162767Ssilby	}
137162767Ssilby
138162031Sglebius	for (sp = gettystrs; sp->field; sp++) {
139190787Szec		if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
140162767Ssilby			if (sp->value) {
141162031Sglebius				/* prefer existing value */
142162031Sglebius				if (strcmp(p, sp->value) != 0)
143169541Sandre					free(sp->value);
144162031Sglebius				else {
145162031Sglebius					free(p);
146162031Sglebius					p = sp->value;
147162031Sglebius				}
148207369Sbz			}
149207369Sbz			sp->value = p;
150195699Srwatson		} else if (l == -1) {
151195699Srwatson			free(sp->value);
152167721Sandre			sp->value = NULL;
153162151Sglebius		}
154169541Sandre	}
155169541Sandre
156157927Sps	for (np = gettynums; np->field; np++) {
157157927Sps		if (cgetnum(buf, (char*)np->field, &n) == -1)
158162031Sglebius			np->set = 0;
159190787Szec		else {
160157927Sps			np->set = 1;
161157927Sps			np->value = n;
1621541Srgrimes		}
163169541Sandre	}
1641541Srgrimes
165169454Srwatson	for (fp = gettyflags; fp->field; fp++) {
166190787Szec		if (cgetcap(buf, (char *)fp->field, ':') == NULL)
167162031Sglebius			fp->set = 0;
168157478Sglebius		else {
169157478Sglebius			fp->set = 1;
170190787Szec			fp->value = 1 ^ fp->invrt;
171162031Sglebius		}
172190787Szec	}
173181803Sbz
1741541Srgrimes#ifdef DEBUG
1751541Srgrimes	printf("name=\"%s\", buf=\"%s\"\r\n", name, buf);
176193731Szec	for (sp = gettystrs; sp->field; sp++)
177193731Szec		printf("cgetstr: %s=%s\r\n", sp->field, sp->value);
178193731Szec	for (np = gettynums; np->field; np++)
179193731Szec		printf("cgetnum: %s=%d\r\n", np->field, np->value);
180193731Szec	for (fp = gettyflags; fp->field; fp++)
181193731Szec		printf("cgetflags: %s='%c' set='%c'\r\n", fp->field,
182193731Szec		       fp->value + '0', fp->set + '0');
183193731Szec#endif /* DEBUG */
184193731Szec}
185193731Szec
186204838Sbzvoid
187204838Sbzgendefaults()
188193731Szec{
189193731Szec	register struct gettystrs *sp;
190193731Szec	register struct gettynums *np;
1911541Srgrimes	register struct gettyflags *fp;
192111145Sjlemon
193138410Srwatson	for (sp = gettystrs; sp->field; sp++)
194111145Sjlemon		if (sp->value)
195111145Sjlemon			sp->defalt = strdup(sp->value);
196111145Sjlemon	for (np = gettynums; np->field; np++)
197157431Srwatson		if (np->set)
198111145Sjlemon			np->defalt = np->value;
199111145Sjlemon	for (fp = gettyflags; fp->field; fp++)
200162151Sglebius		if (fp->set)
201162111Sru			fp->defalt = fp->value;
202111145Sjlemon		else
203221250Sbz			fp->defalt = fp->invrt;
204221250Sbz}
205221250Sbz
206111145Sjlemonvoid
207181803Sbzsetdefaults()
208178285Srwatson{
209138020Srwatson	register struct gettystrs *sp;
210221250Sbz	register struct gettynums *np;
211221250Sbz	register struct gettyflags *fp;
212221250Sbz
213221250Sbz	for (sp = gettystrs; sp->field; sp++)
214221250Sbz		if (!sp->value)
215221250Sbz			sp->value = !sp->defalt ? sp->defalt
216221250Sbz						: strdup(sp->defalt);
217221250Sbz	for (np = gettynums; np->field; np++)
218221250Sbz		if (!np->set)
219221250Sbz			np->value = np->defalt;
220221250Sbz	for (fp = gettyflags; fp->field; fp++)
221221250Sbz		if (!fp->set)
222221250Sbz			fp->value = fp->defalt;
223221250Sbz}
224221250Sbz
225221250Sbzstatic char **
226221250Sbzcharnames[] = {
227221250Sbz	&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
228162151Sglebius	&SU, &DS, &RP, &FL, &WE, &LN, 0
229162151Sglebius};
230190787Szec
231112009Sjlemonstatic char *
232169608Sandrecharvars[] = {
233112009Sjlemon	&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
234157376Srwatson	&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
235157376Srwatson	&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
236178285Srwatson	&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
237112009Sjlemon	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
238112009Sjlemon};
239112009Sjlemon
240111145Sjlemonvoid
241111145Sjlemonsetchars()
242111145Sjlemon{
243111145Sjlemon	register int i;
244111145Sjlemon	register const char *p;
245229700Sjhb
246229665Sjhb	for (i = 0; charnames[i]; i++) {
247229665Sjhb		p = *charnames[i];
248229665Sjhb		if (p && *p)
249111145Sjlemon			*charvars[i] = *p;
250111145Sjlemon		else
251111145Sjlemon			*charvars[i] = _POSIX_VDISABLE;
252111145Sjlemon	}
253133874Srwatson}
254169477Sandre
255111145Sjlemon/* Macros to clear/set/test flags. */
256169477Sandre#define	SET(t, f)	(t) |= (f)
257169477Sandre#define	CLR(t, f)	(t) &= ~(f)
258111145Sjlemon#define	ISSET(t, f)	((t) & (f))
259169477Sandre
260169477Sandrevoid
261111145Sjlemonsetflags(n)
262111145Sjlemon	int n;
263111145Sjlemon{
264121850Ssilby	register tcflag_t iflag, oflag, cflag, lflag;
265121884Ssilby
266111145Sjlemon#ifdef COMPAT_43
267112009Sjlemon	switch (n) {
268111145Sjlemon	case 0:
269111145Sjlemon		if (F0set) {
270111145Sjlemon			compatflags(F0);
271111145Sjlemon			return;
272111145Sjlemon		}
273111145Sjlemon		break;
274137139Sandre	case 1:
275157376Srwatson		if (F1set) {
276157376Srwatson			compatflags(F1);
277157376Srwatson			return;
278157376Srwatson		}
279157376Srwatson		break;
280160925Srwatson	default:
281160925Srwatson		if (F2set) {
282160925Srwatson			compatflags(F2);
283157376Srwatson			return;
284111145Sjlemon		}
285111145Sjlemon		break;
286160925Srwatson	}
287169347Srwatson#endif
288130387Srwatson
289111145Sjlemon	switch (n) {
290157376Srwatson	case 0:
291114794Srwatson		if (C0set && I0set && L0set && O0set) {
292126351Srwatson			tmode.c_cflag = C0;
293157432Srwatson			tmode.c_iflag = I0;
294189848Srwatson			tmode.c_lflag = L0;
295169608Sandre			tmode.c_oflag = O0;
296157376Srwatson			return;
297157376Srwatson		}
298157376Srwatson		break;
299157376Srwatson	case 1:
300157376Srwatson		if (C1set && I1set && L1set && O1set) {
301189848Srwatson			tmode.c_cflag = C1;
302157376Srwatson			tmode.c_iflag = I1;
303157376Srwatson			tmode.c_lflag = L1;
304189848Srwatson			tmode.c_oflag = O1;
305178285Srwatson			return;
306157376Srwatson		}
307157376Srwatson		break;
308157376Srwatson	default:
309157376Srwatson		if (C2set && I2set && L2set && O2set) {
310157376Srwatson			tmode.c_cflag = C2;
311178285Srwatson			tmode.c_iflag = I2;
312111145Sjlemon			tmode.c_lflag = L2;
313111145Sjlemon			tmode.c_oflag = O2;
314162064Sglebius			return;
315121850Ssilby		}
316121884Ssilby		break;
317121884Ssilby	}
318121884Ssilby
319121884Ssilby	iflag = omode.c_iflag;
320121884Ssilby	oflag = omode.c_oflag;
321121884Ssilby	cflag = omode.c_cflag;
322121884Ssilby	lflag = omode.c_lflag;
323121884Ssilby
324121884Ssilby	if (NP) {
325121884Ssilby		CLR(cflag, CSIZE|PARENB);
326121850Ssilby		SET(cflag, CS8);
327121850Ssilby		CLR(iflag, ISTRIP|INPCK|IGNPAR);
328121850Ssilby	} else if (AP || EP || OP) {
329121850Ssilby		CLR(cflag, CSIZE);
330121850Ssilby		SET(cflag, CS7|PARENB);
331121850Ssilby		SET(iflag, ISTRIP);
332121850Ssilby		if (OP && !EP) {
333121884Ssilby			SET(iflag, INPCK|IGNPAR);
334121884Ssilby			SET(cflag, PARODD);
335121850Ssilby			if (AP)
336181803Sbz				CLR(iflag, INPCK);
337121884Ssilby		} else if (EP && !OP) {
338121884Ssilby			SET(iflag, INPCK|IGNPAR);
339133874Srwatson			CLR(cflag, PARODD);
340121884Ssilby			if (AP)
341139222Srwatson				CLR(iflag, INPCK);
342121850Ssilby		} else if (AP || (EP && OP)) {
343139222Srwatson			CLR(iflag, INPCK|IGNPAR);
344121850Ssilby			CLR(cflag, PARODD);
345162064Sglebius		}
346121850Ssilby	} /* else, leave as is */
347169608Sandre
348169608Sandre#if 0
349169608Sandre	if (UC)
350169608Sandre		f |= LCASE;
351169608Sandre#endif
352169608Sandre
353169608Sandre	if (HC)
354169608Sandre		SET(cflag, HUPCL);
355169608Sandre	else
356169608Sandre		CLR(cflag, HUPCL);
357169608Sandre
358169608Sandre	if (MB)
359169608Sandre		SET(cflag, MDMBUF);
360181803Sbz	else
361178285Srwatson		CLR(cflag, MDMBUF);
362169608Sandre
363169608Sandre	if (HW)
364169608Sandre		SET(cflag, CRTSCTS);
365169608Sandre	else
366169608Sandre		CLR(cflag, CRTSCTS);
367169608Sandre
368169608Sandre	if (NL) {
369169608Sandre		SET(iflag, ICRNL);
370169608Sandre		SET(oflag, ONLCR|OPOST);
371169608Sandre	} else {
372169608Sandre		CLR(iflag, ICRNL);
373169608Sandre		CLR(oflag, ONLCR);
374169608Sandre	}
375169608Sandre
376169608Sandre	if (!HT)
377169608Sandre		SET(oflag, OXTABS|OPOST);
378169608Sandre	else
379169608Sandre		CLR(oflag, OXTABS);
380169608Sandre
381169608Sandre#ifdef XXX_DELAY
382169608Sandre	SET(f, delaybits());
383169608Sandre#endif
384169608Sandre
385169608Sandre	if (n == 1) {		/* read mode flags */
386169608Sandre		if (RW) {
387169608Sandre			iflag = 0;
388169608Sandre			CLR(oflag, OPOST);
389169608Sandre			CLR(cflag, CSIZE|PARENB);
390169608Sandre			SET(cflag, CS8);
391169608Sandre			lflag = 0;
392169608Sandre		} else {
393169608Sandre			CLR(lflag, ICANON);
394169608Sandre		}
395169608Sandre		goto out;
396169608Sandre	}
397169608Sandre
398169608Sandre	if (n == 0)
399169608Sandre		goto out;
400169608Sandre
401169608Sandre#if 0
402169608Sandre	if (CB)
403169608Sandre		SET(f, CRTBS);
404169608Sandre#endif
405169608Sandre
406169608Sandre	if (CE)
407169608Sandre		SET(lflag, ECHOE);
408169608Sandre	else
409169608Sandre		CLR(lflag, ECHOE);
410169608Sandre
411169608Sandre	if (CK)
412169608Sandre		SET(lflag, ECHOKE);
413169608Sandre	else
414169608Sandre		CLR(lflag, ECHOKE);
415169608Sandre
416169608Sandre	if (PE)
417218909Sbrucec		SET(lflag, ECHOPRT);
418169608Sandre	else
419169608Sandre		CLR(lflag, ECHOPRT);
420169608Sandre
421169608Sandre	if (EC)
422169608Sandre		SET(lflag, ECHO);
423169608Sandre	else
424169608Sandre		CLR(lflag, ECHO);
425169608Sandre
426169608Sandre	if (XC)
427169608Sandre		SET(lflag, ECHOCTL);
428169608Sandre	else
429169608Sandre		CLR(lflag, ECHOCTL);
430169608Sandre
431169608Sandre	if (DX)
432169608Sandre		SET(lflag, IXANY);
433169608Sandre	else
434169608Sandre		CLR(lflag, IXANY);
435169608Sandre
436169608Sandreout:
437169608Sandre	tmode.c_iflag = iflag;
438178285Srwatson	tmode.c_oflag = oflag;
439169608Sandre	tmode.c_cflag = cflag;
440169608Sandre	tmode.c_lflag = lflag;
441169608Sandre}
442169608Sandre
443157376Srwatson#ifdef COMPAT_43
444112009Sjlemon/*
445111145Sjlemon * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
446157376Srwatson */
447111145Sjlemonvoid
448111145Sjlemoncompatflags(flags)
449157376Srwatsonregister long flags;
450157386Srwatson{
451157376Srwatson	register tcflag_t iflag, oflag, cflag, lflag;
452160549Srwatson
453160549Srwatson	iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
454157386Srwatson	oflag = OPOST|ONLCR|OXTABS;
455160549Srwatson	cflag = CREAD;
456160549Srwatson	lflag = ICANON|ISIG|IEXTEN;
457157376Srwatson
458111145Sjlemon	if (ISSET(flags, TANDEM))
459189848Srwatson		SET(iflag, IXOFF);
460157432Srwatson	else
461181803Sbz		CLR(iflag, IXOFF);
462178285Srwatson	if (ISSET(flags, ECHO))
463138020Srwatson		SET(lflag, ECHO);
464111145Sjlemon	else
465169608Sandre		CLR(lflag, ECHO);
466111145Sjlemon	if (ISSET(flags, CRMOD)) {
467158009Srwatson		SET(iflag, ICRNL);
468157376Srwatson		SET(oflag, ONLCR);
469157376Srwatson	} else {
470157386Srwatson		CLR(iflag, ICRNL);
471160549Srwatson		CLR(oflag, ONLCR);
472160549Srwatson	}
473160549Srwatson	if (ISSET(flags, XTABS))
474160549Srwatson		SET(oflag, OXTABS);
475160549Srwatson	else
476160549Srwatson		CLR(oflag, OXTABS);
477189848Srwatson
478189848Srwatson
479178285Srwatson	if (ISSET(flags, RAW)) {
480157386Srwatson		iflag &= IXOFF;
481157386Srwatson		CLR(lflag, ISIG|ICANON|IEXTEN);
482157386Srwatson		CLR(cflag, PARENB);
483157386Srwatson	} else {
484157386Srwatson		SET(iflag, BRKINT|IXON|IMAXBEL);
485157386Srwatson		SET(lflag, ISIG|IEXTEN);
486157376Srwatson		if (ISSET(flags, CBREAK))
487157386Srwatson			CLR(lflag, ICANON);
488157386Srwatson		else
489157386Srwatson			SET(lflag, ICANON);
490157386Srwatson		switch (ISSET(flags, ANYP)) {
491157386Srwatson		case 0:
492178285Srwatson			CLR(cflag, PARENB);
493157376Srwatson			break;
494185370Sbz		case ANYP:
495185370Sbz			SET(cflag, PARENB);
496190948Srwatson			CLR(iflag, INPCK);
497126002Spjd			break;
498126002Spjd		case EVENP:
499112009Sjlemon			SET(cflag, PARENB);
500157376Srwatson			SET(iflag, INPCK);
501190787Szec			CLR(cflag, PARODD);
502111145Sjlemon			break;
503111145Sjlemon		case ODDP:
504111145Sjlemon			SET(cflag, PARENB);
505126351Srwatson			SET(iflag, INPCK);
506111145Sjlemon			SET(cflag, PARODD);
507111145Sjlemon			break;
508221250Sbz		}
509221250Sbz	}
510221250Sbz
511111145Sjlemon	/* Nothing we can do with CRTBS. */
512221250Sbz	if (ISSET(flags, PRTERA))
513111145Sjlemon		SET(lflag, ECHOPRT);
514221250Sbz	else
515111145Sjlemon		CLR(lflag, ECHOPRT);
516221250Sbz	if (ISSET(flags, CRTERA))
517168845Sandre		SET(lflag, ECHOE);
518111145Sjlemon	else
519111145Sjlemon		CLR(lflag, ECHOE);
520186222Sbz	/* Nothing we can do with TILDE. */
521111145Sjlemon	if (ISSET(flags, MDMBUF))
522111145Sjlemon		SET(cflag, MDMBUF);
523178285Srwatson	else
524138020Srwatson		CLR(cflag, MDMBUF);
525151967Sandre	if (ISSET(flags, NOHANG))
526111145Sjlemon		CLR(cflag, HUPCL);
527111145Sjlemon	else
528111145Sjlemon		SET(cflag, HUPCL);
529111145Sjlemon	if (ISSET(flags, CRTKIL))
530114794Srwatson		SET(lflag, ECHOKE);
531172930Srwatson	else
532114794Srwatson		CLR(lflag, ECHOKE);
533114794Srwatson	if (ISSET(flags, CTLECH))
534111153Sjlemon		SET(lflag, ECHOCTL);
535111145Sjlemon	else
536111145Sjlemon		CLR(lflag, ECHOCTL);
537111145Sjlemon	if (!ISSET(flags, DECCTQ))
538111145Sjlemon		SET(iflag, IXANY);
539111145Sjlemon	else
540221250Sbz		CLR(iflag, IXANY);
541111153Sjlemon	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
542221250Sbz	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
543221250Sbz
544221250Sbz	if (ISSET(flags, RAW|LITOUT|PASS8)) {
545221250Sbz		CLR(cflag, CSIZE);
546111153Sjlemon		SET(cflag, CS8);
547111145Sjlemon		if (!ISSET(flags, RAW|PASS8))
548111145Sjlemon			SET(iflag, ISTRIP);
549111145Sjlemon		else
550111145Sjlemon			CLR(iflag, ISTRIP);
551111145Sjlemon		if (!ISSET(flags, RAW|LITOUT))
552221250Sbz			SET(oflag, OPOST);
553168845Sandre		else
554133874Srwatson			CLR(oflag, OPOST);
555133874Srwatson	} else {
556111145Sjlemon		CLR(cflag, CSIZE);
557111145Sjlemon		SET(cflag, CS7);
558133874Srwatson		SET(iflag, ISTRIP);
559111145Sjlemon		SET(oflag, OPOST);
560168845Sandre	}
561231767Sbz
562168845Sandre	tmode.c_iflag = iflag;
563133874Srwatson	tmode.c_oflag = oflag;
564168845Sandre	tmode.c_cflag = cflag;
565111145Sjlemon	tmode.c_lflag = lflag;
566111145Sjlemon}
567111145Sjlemon#endif
568111145Sjlemon
569111145Sjlemon#ifdef XXX_DELAY
570111145Sjlemonstruct delayval {
571111145Sjlemon	unsigned	delay;		/* delay in ms */
572111145Sjlemon	int		bits;
573111145Sjlemon};
574111145Sjlemon
575111145Sjlemon/*
576111145Sjlemon * below are random guesses, I can't be bothered checking
577235961Sbz */
578235961Sbz
579111153Sjlemonstruct delayval	crdelay[] = {
580111145Sjlemon	{ 1,		CR1 },
581235961Sbz	{ 2,		CR2 },
582235961Sbz	{ 3,		CR3 },
583122922Sandre	{ 83,		CR1 },
584122922Sandre	{ 166,		CR2 },
585111145Sjlemon	{ 0,		CR3 },
586221250Sbz};
587111153Sjlemon
588221250Sbzstruct delayval nldelay[] = {
589221250Sbz	{ 1,		NL1 },		/* special, calculated */
590221250Sbz	{ 2,		NL2 },
591221250Sbz	{ 3,		NL3 },
592111153Sjlemon	{ 100,		NL2 },
593111145Sjlemon	{ 0,		NL3 },
594133874Srwatson};
595111145Sjlemon
596181803Sbzstruct delayval	bsdelay[] = {
597124248Sandre	{ 1,		BS1 },
598122922Sandre	{ 0,		0 },
599134793Sjmg};
600134793Sjmg
601111145Sjlemonstruct delayval	ffdelay[] = {
602221250Sbz	{ 1,		FF1 },
603111145Sjlemon	{ 1750,		FF1 },
604190948Srwatson	{ 0,		FF1 },
605111145Sjlemon};
606190948Srwatson
607190948Srwatsonstruct delayval	tbdelay[] = {
608111145Sjlemon	{ 1,		TAB1 },
609111145Sjlemon	{ 2,		TAB2 },
610169608Sandre	{ 3,		XTABS },	/* this is expand tabs */
611169608Sandre	{ 100,		TAB1 },
612169608Sandre	{ 0,		TAB2 },
613169608Sandre};
614169608Sandre
615181803Sbzint
616178285Srwatsondelaybits()
617169608Sandre{
618181803Sbz	register int f;
619169608Sandre
620181803Sbz	f  = adelay(CD, crdelay);
621169608Sandre	f |= adelay(ND, nldelay);
622169608Sandre	f |= adelay(FD, ffdelay);
623169608Sandre	f |= adelay(TD, tbdelay);
624169608Sandre	f |= adelay(BD, bsdelay);
625169608Sandre	return (f);
626169608Sandre}
627181803Sbz
628181803Sbzint
629169608Sandreadelay(ms, dp)
630169608Sandre	register ms;
631169608Sandre	register struct delayval *dp;
632169608Sandre{
633169608Sandre	if (ms == 0)
634169608Sandre		return (0);
635169608Sandre	while (dp->delay && ms > dp->delay)
636181803Sbz		dp++;
637169608Sandre	return (dp->bits);
638181803Sbz}
639196410Speter#endif
640169608Sandre
641178285Srwatsonchar	editedhost[MAXHOSTNAMELEN];
642169608Sandre
643169608Sandrevoid
644169608Sandreedithost(pat)
645169608Sandre	register const char *pat;
646169608Sandre{
647169608Sandre	register const char *host = HN;
648	register char *res = editedhost;
649
650	if (!pat)
651		pat = "";
652	while (*pat) {
653		switch (*pat) {
654
655		case '#':
656			if (*host)
657				host++;
658			break;
659
660		case '@':
661			if (*host)
662				*res++ = *host++;
663			break;
664
665		default:
666			*res++ = *pat;
667			break;
668
669		}
670		if (res == &editedhost[sizeof editedhost - 1]) {
671			*res = '\0';
672			return;
673		}
674		pat++;
675	}
676	if (*host)
677		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
678	else
679		*res = '\0';
680	editedhost[sizeof editedhost - 1] = '\0';
681}
682
683static struct speedtab {
684	int	speed;
685	int	uxname;
686} speedtab[] = {
687	{ 50,	B50 },
688	{ 75,	B75 },
689	{ 110,	B110 },
690	{ 134,	B134 },
691	{ 150,	B150 },
692	{ 200,	B200 },
693	{ 300,	B300 },
694	{ 600,	B600 },
695	{ 1200,	B1200 },
696	{ 1800,	B1800 },
697	{ 2400,	B2400 },
698	{ 4800,	B4800 },
699	{ 9600,	B9600 },
700	{ 19200, EXTA },
701	{ 19,	EXTA },		/* for people who say 19.2K */
702	{ 38400, EXTB },
703	{ 38,	EXTB },
704	{ 7200,	EXTB },		/* alternative */
705	{ 57600, B57600 },
706	{ 115200, B115200 },
707	{ 0 }
708};
709
710int
711speed(val)
712	int val;
713{
714	register struct speedtab *sp;
715
716	if (val <= B115200)
717		return (val);
718
719	for (sp = speedtab; sp->speed; sp++)
720		if (sp->speed == val)
721			return (sp->uxname);
722
723	return (B300);		/* default in impossible cases */
724}
725
726void
727makeenv(env)
728	char *env[];
729{
730	static char termbuf[128] = "TERM=";
731	register char *p, *q;
732	register char **ep;
733
734	ep = env;
735	if (TT && *TT) {
736		strcat(termbuf, TT);
737		*ep++ = termbuf;
738	}
739	if ((p = EV)) {
740		q = p;
741		while ((q = strchr(q, ','))) {
742			*q++ = '\0';
743			*ep++ = p;
744			p = q;
745		}
746		if (*p)
747			*ep++ = p;
748	}
749	*ep = (char *)0;
750}
751
752/*
753 * This speed select mechanism is written for the Develcon DATASWITCH.
754 * The Develcon sends a string of the form "B{speed}\n" at a predefined
755 * baud rate. This string indicates the user's actual speed.
756 * The routine below returns the terminal type mapped from derived speed.
757 */
758struct	portselect {
759	const char	*ps_baud;
760	const char	*ps_type;
761} portspeeds[] = {
762	{ "B110",	"std.110" },
763	{ "B134",	"std.134" },
764	{ "B150",	"std.150" },
765	{ "B300",	"std.300" },
766	{ "B600",	"std.600" },
767	{ "B1200",	"std.1200" },
768	{ "B2400",	"std.2400" },
769	{ "B4800",	"std.4800" },
770	{ "B9600",	"std.9600" },
771	{ "B19200",	"std.19200" },
772	{ 0 }
773};
774
775const char *
776portselector()
777{
778	char c, baud[20];
779	const char *type = "default";
780	register struct portselect *ps;
781	int len;
782
783	alarm(5*60);
784	for (len = 0; len < sizeof (baud) - 1; len++) {
785		if (read(STDIN_FILENO, &c, 1) <= 0)
786			break;
787		c &= 0177;
788		if (c == '\n' || c == '\r')
789			break;
790		if (c == 'B')
791			len = 0;	/* in case of leading garbage */
792		baud[len] = c;
793	}
794	baud[len] = '\0';
795	for (ps = portspeeds; ps->ps_baud; ps++)
796		if (strcmp(ps->ps_baud, baud) == 0) {
797			type = ps->ps_type;
798			break;
799		}
800	sleep(2);	/* wait for connection to complete */
801	return (type);
802}
803
804/*
805 * This auto-baud speed select mechanism is written for the Micom 600
806 * portselector. Selection is done by looking at how the character '\r'
807 * is garbled at the different speeds.
808 */
809const char *
810autobaud()
811{
812	int rfds;
813	struct timeval timeout;
814	char c;
815	const char *type = "9600-baud";
816
817	(void)tcflush(0, TCIOFLUSH);
818	rfds = 1 << 0;
819	timeout.tv_sec = 5;
820	timeout.tv_usec = 0;
821	if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
822	    (fd_set *)NULL, &timeout) <= 0)
823		return (type);
824	if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
825		return (type);
826	timeout.tv_sec = 0;
827	timeout.tv_usec = 20;
828	(void) select(32, (fd_set *)NULL, (fd_set *)NULL,
829	    (fd_set *)NULL, &timeout);
830	(void)tcflush(0, TCIOFLUSH);
831	switch (c & 0377) {
832
833	case 0200:		/* 300-baud */
834		type = "300-baud";
835		break;
836
837	case 0346:		/* 1200-baud */
838		type = "1200-baud";
839		break;
840
841	case  015:		/* 2400-baud */
842	case 0215:
843		type = "2400-baud";
844		break;
845
846	default:		/* 4800-baud */
847		type = "4800-baud";
848		break;
849
850	case 0377:		/* 9600-baud */
851		type = "9600-baud";
852		break;
853	}
854	return (type);
855}
856