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