Home | History | Annotate | Line # | Download | only in getty
subr.c revision 1.32
      1  1.32  christos /*	$NetBSD: subr.c,v 1.32 2006/11/16 04:15:13 christos 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.32  christos __RCSID("$NetBSD: subr.c,v 1.32 2006/11/16 04:15:13 christos 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.30  christos 	const 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.31  christos 	&SU, &DS, &RP, &FL, &WE, &LN, &ST, &B2, 0
    152   1.1       cgd };
    153   1.1       cgd 
    154  1.32  christos static cc_t *
    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.31  christos 	&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], &tmode.c_cc[VSTATUS],
    161  1.31  christos 	&tmode.c_cc[VEOL2], 0
    162   1.1       cgd };
    163   1.1       cgd 
    164   1.9        pk void
    165  1.25   thorpej setchars(void)
    166   1.1       cgd {
    167  1.23       mrg 	int i;
    168  1.23       mrg 	char *p;
    169   1.1       cgd 
    170   1.1       cgd 	for (i = 0; charnames[i]; i++) {
    171   1.1       cgd 		p = *charnames[i];
    172   1.1       cgd 		if (p && *p)
    173   1.1       cgd 			*charvars[i] = *p;
    174   1.1       cgd 		else
    175  1.12   mycroft 			*charvars[i] = _POSIX_VDISABLE;
    176   1.1       cgd 	}
    177   1.1       cgd }
    178   1.1       cgd 
    179  1.17   mycroft /* Macros to clear/set/test flags. */
    180  1.17   mycroft #define	SET(t, f)	(t) |= (f)
    181  1.17   mycroft #define	CLR(t, f)	(t) &= ~(f)
    182  1.17   mycroft #define	ISSET(t, f)	((t) & (f))
    183  1.17   mycroft 
    184   1.7        pk void
    185  1.25   thorpej setflags(int n)
    186   1.1       cgd {
    187  1.23       mrg 	tcflag_t iflag, oflag, cflag, lflag;
    188   1.1       cgd 
    189   1.7        pk #ifdef COMPAT_43
    190   1.1       cgd 	switch (n) {
    191   1.1       cgd 	case 0:
    192   1.7        pk 		if (F0set) {
    193   1.7        pk 			compatflags(F0);
    194   1.7        pk 			return;
    195   1.7        pk 		}
    196   1.1       cgd 		break;
    197   1.1       cgd 	case 1:
    198   1.7        pk 		if (F1set) {
    199   1.7        pk 			compatflags(F1);
    200   1.7        pk 			return;
    201   1.7        pk 		}
    202   1.1       cgd 		break;
    203   1.1       cgd 	default:
    204   1.7        pk 		if (F2set) {
    205   1.7        pk 			compatflags(F2);
    206   1.7        pk 			return;
    207   1.7        pk 		}
    208   1.1       cgd 		break;
    209   1.1       cgd 	}
    210   1.7        pk #endif
    211   1.1       cgd 
    212   1.7        pk 	switch (n) {
    213   1.7        pk 	case 0:
    214   1.7        pk 		if (C0set && I0set && L0set && O0set) {
    215   1.7        pk 			tmode.c_cflag = C0;
    216   1.7        pk 			tmode.c_iflag = I0;
    217   1.7        pk 			tmode.c_lflag = L0;
    218   1.7        pk 			tmode.c_oflag = O0;
    219   1.7        pk 			return;
    220   1.7        pk 		}
    221   1.7        pk 		break;
    222   1.7        pk 	case 1:
    223   1.7        pk 		if (C1set && I1set && L1set && O1set) {
    224   1.7        pk 			tmode.c_cflag = C1;
    225   1.7        pk 			tmode.c_iflag = I1;
    226   1.7        pk 			tmode.c_lflag = L1;
    227   1.7        pk 			tmode.c_oflag = O1;
    228   1.7        pk 			return;
    229   1.7        pk 		}
    230   1.7        pk 		break;
    231   1.7        pk 	default:
    232   1.7        pk 		if (C2set && I2set && L2set && O2set) {
    233   1.7        pk 			tmode.c_cflag = C2;
    234   1.7        pk 			tmode.c_iflag = I2;
    235   1.7        pk 			tmode.c_lflag = L2;
    236   1.7        pk 			tmode.c_oflag = O2;
    237   1.7        pk 			return;
    238   1.7        pk 		}
    239   1.7        pk 		break;
    240   1.7        pk 	}
    241   1.1       cgd 
    242   1.9        pk 	iflag = omode.c_iflag;
    243  1.16   mycroft 	oflag = omode.c_oflag;
    244   1.9        pk 	cflag = omode.c_cflag;
    245   1.9        pk 	lflag = omode.c_lflag;
    246   1.7        pk 
    247   1.7        pk 	if (NP) {
    248  1.17   mycroft 		CLR(cflag, CSIZE|PARENB);
    249  1.17   mycroft 		SET(cflag, CS8);
    250  1.17   mycroft 		CLR(iflag, ISTRIP|INPCK|IGNPAR);
    251  1.17   mycroft 	} else if (AP || EP || OP) {
    252  1.17   mycroft 		CLR(cflag, CSIZE);
    253  1.17   mycroft 		SET(cflag, CS7|PARENB);
    254  1.17   mycroft 		SET(iflag, ISTRIP);
    255  1.17   mycroft 		if (OP && !EP) {
    256  1.17   mycroft 			SET(iflag, INPCK|IGNPAR);
    257  1.17   mycroft 			SET(cflag, PARODD);
    258  1.17   mycroft 			if (AP)
    259  1.17   mycroft 				CLR(iflag, INPCK);
    260  1.17   mycroft 		} else if (EP && !OP) {
    261  1.17   mycroft 			SET(iflag, INPCK|IGNPAR);
    262  1.17   mycroft 			CLR(cflag, PARODD);
    263  1.17   mycroft 			if (AP)
    264  1.17   mycroft 				CLR(iflag, INPCK);
    265  1.22     mikel 		} else if (AP || (EP && OP)) {
    266  1.17   mycroft 			CLR(iflag, INPCK|IGNPAR);
    267  1.17   mycroft 			CLR(cflag, PARODD);
    268  1.17   mycroft 		}
    269   1.9        pk 	} /* else, leave as is */
    270   1.1       cgd 
    271   1.7        pk #if 0
    272   1.1       cgd 	if (UC)
    273   1.1       cgd 		f |= LCASE;
    274   1.7        pk #endif
    275   1.1       cgd 
    276   1.7        pk 	if (HC)
    277  1.17   mycroft 		SET(cflag, HUPCL);
    278   1.9        pk 	else
    279  1.17   mycroft 		CLR(cflag, HUPCL);
    280   1.7        pk 
    281  1.13        pk 	if (MB)
    282  1.17   mycroft 		SET(cflag, MDMBUF);
    283  1.13        pk 	else
    284  1.17   mycroft 		CLR(cflag, MDMBUF);
    285  1.13        pk 
    286   1.7        pk 	if (NL) {
    287  1.17   mycroft 		SET(iflag, ICRNL);
    288  1.17   mycroft 		SET(oflag, ONLCR|OPOST);
    289  1.15   mycroft 	} else {
    290  1.17   mycroft 		CLR(iflag, ICRNL);
    291  1.17   mycroft 		CLR(oflag, ONLCR);
    292   1.7        pk 	}
    293   1.1       cgd 
    294  1.16   mycroft 	if (!HT)
    295  1.17   mycroft 		SET(oflag, OXTABS|OPOST);
    296  1.16   mycroft 	else
    297  1.17   mycroft 		CLR(oflag, OXTABS);
    298  1.16   mycroft 
    299   1.7        pk #ifdef XXX_DELAY
    300  1.17   mycroft 	SET(f, delaybits());
    301   1.7        pk #endif
    302   1.1       cgd 
    303   1.1       cgd 	if (n == 1) {		/* read mode flags */
    304   1.7        pk 		if (RW) {
    305   1.7        pk 			iflag = 0;
    306  1.17   mycroft 			CLR(oflag, OPOST);
    307  1.17   mycroft 			CLR(cflag, CSIZE|PARENB);
    308  1.17   mycroft 			SET(cflag, CS8);
    309   1.7        pk 			lflag = 0;
    310   1.7        pk 		} else {
    311  1.17   mycroft 			CLR(lflag, ICANON);
    312   1.7        pk 		}
    313   1.7        pk 		goto out;
    314   1.1       cgd 	}
    315   1.1       cgd 
    316   1.1       cgd 	if (n == 0)
    317   1.7        pk 		goto out;
    318   1.1       cgd 
    319   1.7        pk #if 0
    320   1.1       cgd 	if (CB)
    321  1.17   mycroft 		SET(f, CRTBS);
    322   1.7        pk #endif
    323   1.1       cgd 
    324   1.1       cgd 	if (CE)
    325  1.17   mycroft 		SET(lflag, ECHOE);
    326  1.16   mycroft 	else
    327  1.17   mycroft 		CLR(lflag, ECHOE);
    328   1.1       cgd 
    329   1.1       cgd 	if (CK)
    330  1.17   mycroft 		SET(lflag, ECHOKE);
    331  1.16   mycroft 	else
    332  1.17   mycroft 		CLR(lflag, ECHOKE);
    333   1.1       cgd 
    334   1.1       cgd 	if (PE)
    335  1.17   mycroft 		SET(lflag, ECHOPRT);
    336  1.16   mycroft 	else
    337  1.17   mycroft 		CLR(lflag, ECHOPRT);
    338   1.1       cgd 
    339   1.1       cgd 	if (EC)
    340  1.17   mycroft 		SET(lflag, ECHO);
    341  1.16   mycroft 	else
    342  1.17   mycroft 		CLR(lflag, ECHO);
    343   1.1       cgd 
    344   1.1       cgd 	if (XC)
    345  1.17   mycroft 		SET(lflag, ECHOCTL);
    346  1.16   mycroft 	else
    347  1.17   mycroft 		CLR(lflag, ECHOCTL);
    348   1.1       cgd 
    349   1.1       cgd 	if (DX)
    350  1.17   mycroft 		SET(lflag, IXANY);
    351  1.16   mycroft 	else
    352  1.17   mycroft 		CLR(lflag, IXANY);
    353   1.1       cgd 
    354   1.7        pk out:
    355   1.7        pk 	tmode.c_iflag = iflag;
    356   1.7        pk 	tmode.c_oflag = oflag;
    357   1.7        pk 	tmode.c_cflag = cflag;
    358   1.7        pk 	tmode.c_lflag = lflag;
    359   1.7        pk }
    360   1.7        pk 
    361   1.7        pk #ifdef COMPAT_43
    362   1.7        pk /*
    363   1.7        pk  * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
    364   1.7        pk  */
    365   1.7        pk void
    366  1.25   thorpej compatflags(long flags)
    367   1.7        pk {
    368  1.23       mrg 	tcflag_t iflag, oflag, cflag, lflag;
    369   1.7        pk 
    370  1.17   mycroft 	iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
    371  1.17   mycroft 	oflag = OPOST|ONLCR|OXTABS;
    372  1.17   mycroft 	cflag = CREAD;
    373  1.17   mycroft 	lflag = ICANON|ISIG|IEXTEN;
    374  1.17   mycroft 
    375  1.17   mycroft 	if (ISSET(flags, TANDEM))
    376  1.17   mycroft 		SET(iflag, IXOFF);
    377  1.17   mycroft 	else
    378  1.17   mycroft 		CLR(iflag, IXOFF);
    379  1.17   mycroft 	if (ISSET(flags, ECHO))
    380  1.17   mycroft 		SET(lflag, ECHO);
    381  1.17   mycroft 	else
    382  1.17   mycroft 		CLR(lflag, ECHO);
    383  1.17   mycroft 	if (ISSET(flags, CRMOD)) {
    384  1.17   mycroft 		SET(iflag, ICRNL);
    385  1.17   mycroft 		SET(oflag, ONLCR);
    386   1.8        pk 	} else {
    387  1.17   mycroft 		CLR(iflag, ICRNL);
    388  1.17   mycroft 		CLR(oflag, ONLCR);
    389   1.8        pk 	}
    390  1.17   mycroft 	if (ISSET(flags, XTABS))
    391  1.17   mycroft 		SET(oflag, OXTABS);
    392   1.8        pk 	else
    393  1.17   mycroft 		CLR(oflag, OXTABS);
    394   1.8        pk 
    395  1.17   mycroft 
    396  1.17   mycroft 	if (ISSET(flags, RAW)) {
    397   1.8        pk 		iflag &= IXOFF;
    398  1.17   mycroft 		CLR(lflag, ISIG|ICANON|IEXTEN);
    399  1.18   mycroft 		CLR(cflag, PARENB);
    400   1.7        pk 	} else {
    401  1.17   mycroft 		SET(iflag, BRKINT|IXON|IMAXBEL);
    402  1.17   mycroft 		SET(lflag, ISIG|IEXTEN);
    403  1.17   mycroft 		if (ISSET(flags, CBREAK))
    404  1.17   mycroft 			CLR(lflag, ICANON);
    405   1.7        pk 		else
    406  1.17   mycroft 			SET(lflag, ICANON);
    407  1.18   mycroft 		switch (ISSET(flags, ANYP)) {
    408  1.18   mycroft 		case 0:
    409  1.18   mycroft 			CLR(cflag, PARENB);
    410  1.18   mycroft 			break;
    411  1.18   mycroft 		case ANYP:
    412  1.18   mycroft 			SET(cflag, PARENB);
    413  1.18   mycroft 			CLR(iflag, INPCK);
    414  1.18   mycroft 			break;
    415  1.18   mycroft 		case EVENP:
    416  1.18   mycroft 			SET(cflag, PARENB);
    417  1.18   mycroft 			SET(iflag, INPCK);
    418  1.18   mycroft 			CLR(cflag, PARODD);
    419  1.18   mycroft 			break;
    420  1.18   mycroft 		case ODDP:
    421  1.18   mycroft 			SET(cflag, PARENB);
    422  1.18   mycroft 			SET(iflag, INPCK);
    423  1.18   mycroft 			SET(cflag, PARODD);
    424  1.18   mycroft 			break;
    425  1.18   mycroft 		}
    426   1.8        pk 	}
    427   1.8        pk 
    428  1.17   mycroft 	/* Nothing we can do with CRTBS. */
    429  1.17   mycroft 	if (ISSET(flags, PRTERA))
    430  1.17   mycroft 		SET(lflag, ECHOPRT);
    431  1.17   mycroft 	else
    432  1.17   mycroft 		CLR(lflag, ECHOPRT);
    433  1.17   mycroft 	if (ISSET(flags, CRTERA))
    434  1.17   mycroft 		SET(lflag, ECHOE);
    435  1.17   mycroft 	else
    436  1.17   mycroft 		CLR(lflag, ECHOE);
    437  1.17   mycroft 	/* Nothing we can do with TILDE. */
    438  1.17   mycroft 	if (ISSET(flags, MDMBUF))
    439  1.17   mycroft 		SET(cflag, MDMBUF);
    440  1.17   mycroft 	else
    441  1.17   mycroft 		CLR(cflag, MDMBUF);
    442  1.17   mycroft 	if (ISSET(flags, NOHANG))
    443  1.17   mycroft 		CLR(cflag, HUPCL);
    444  1.17   mycroft 	else
    445  1.17   mycroft 		SET(cflag, HUPCL);
    446  1.17   mycroft 	if (ISSET(flags, CRTKIL))
    447  1.17   mycroft 		SET(lflag, ECHOKE);
    448  1.17   mycroft 	else
    449  1.17   mycroft 		CLR(lflag, ECHOKE);
    450  1.17   mycroft 	if (ISSET(flags, CTLECH))
    451  1.17   mycroft 		SET(lflag, ECHOCTL);
    452  1.17   mycroft 	else
    453  1.17   mycroft 		CLR(lflag, ECHOCTL);
    454  1.17   mycroft 	if (!ISSET(flags, DECCTQ))
    455  1.17   mycroft 		SET(iflag, IXANY);
    456  1.17   mycroft 	else
    457  1.17   mycroft 		CLR(iflag, IXANY);
    458  1.17   mycroft 	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
    459  1.17   mycroft 	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
    460  1.17   mycroft 
    461  1.17   mycroft 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    462  1.18   mycroft 		CLR(cflag, CSIZE);
    463  1.17   mycroft 		SET(cflag, CS8);
    464  1.17   mycroft 		if (!ISSET(flags, RAW|PASS8))
    465  1.17   mycroft 			SET(iflag, ISTRIP);
    466   1.8        pk 		else
    467  1.17   mycroft 			CLR(iflag, ISTRIP);
    468  1.17   mycroft 		if (!ISSET(flags, RAW|LITOUT))
    469  1.17   mycroft 			SET(oflag, OPOST);
    470   1.8        pk 		else
    471  1.17   mycroft 			CLR(oflag, OPOST);
    472   1.8        pk 	} else {
    473  1.17   mycroft 		CLR(cflag, CSIZE);
    474  1.18   mycroft 		SET(cflag, CS7);
    475  1.17   mycroft 		SET(iflag, ISTRIP);
    476  1.17   mycroft 		SET(oflag, OPOST);
    477   1.7        pk 	}
    478   1.7        pk 
    479   1.7        pk 	tmode.c_iflag = iflag;
    480   1.7        pk 	tmode.c_oflag = oflag;
    481   1.7        pk 	tmode.c_cflag = cflag;
    482   1.7        pk 	tmode.c_lflag = lflag;
    483   1.1       cgd }
    484   1.7        pk #endif
    485   1.1       cgd 
    486   1.7        pk #ifdef XXX_DELAY
    487   1.1       cgd struct delayval {
    488   1.1       cgd 	unsigned	delay;		/* delay in ms */
    489   1.1       cgd 	int		bits;
    490   1.1       cgd };
    491   1.1       cgd 
    492   1.1       cgd /*
    493   1.1       cgd  * below are random guesses, I can't be bothered checking
    494   1.1       cgd  */
    495   1.1       cgd 
    496   1.1       cgd struct delayval	crdelay[] = {
    497   1.9        pk 	{ 1,		CR1 },
    498   1.9        pk 	{ 2,		CR2 },
    499   1.9        pk 	{ 3,		CR3 },
    500   1.9        pk 	{ 83,		CR1 },
    501   1.9        pk 	{ 166,		CR2 },
    502   1.9        pk 	{ 0,		CR3 },
    503   1.1       cgd };
    504   1.1       cgd 
    505   1.1       cgd struct delayval nldelay[] = {
    506   1.9        pk 	{ 1,		NL1 },		/* special, calculated */
    507   1.9        pk 	{ 2,		NL2 },
    508   1.9        pk 	{ 3,		NL3 },
    509   1.9        pk 	{ 100,		NL2 },
    510   1.9        pk 	{ 0,		NL3 },
    511   1.1       cgd };
    512   1.1       cgd 
    513   1.1       cgd struct delayval	bsdelay[] = {
    514   1.9        pk 	{ 1,		BS1 },
    515   1.9        pk 	{ 0,		0 },
    516   1.1       cgd };
    517   1.1       cgd 
    518   1.1       cgd struct delayval	ffdelay[] = {
    519   1.9        pk 	{ 1,		FF1 },
    520   1.9        pk 	{ 1750,		FF1 },
    521   1.9        pk 	{ 0,		FF1 },
    522   1.1       cgd };
    523   1.1       cgd 
    524   1.1       cgd struct delayval	tbdelay[] = {
    525   1.9        pk 	{ 1,		 TAB1 },
    526   1.9        pk 	{ 2,		 TAB2 },
    527   1.9        pk 	{ 3,		XTABS },	/* this is expand tabs */
    528   1.9        pk 	{ 100,		 TAB1 },
    529   1.9        pk 	{ 0,		 TAB2 },
    530   1.1       cgd };
    531   1.1       cgd 
    532   1.9        pk int
    533  1.25   thorpej delaybits(void)
    534   1.1       cgd {
    535  1.23       mrg 	int f;
    536   1.1       cgd 
    537   1.1       cgd 	f  = adelay(CD, crdelay);
    538   1.1       cgd 	f |= adelay(ND, nldelay);
    539   1.1       cgd 	f |= adelay(FD, ffdelay);
    540   1.1       cgd 	f |= adelay(TD, tbdelay);
    541   1.1       cgd 	f |= adelay(BD, bsdelay);
    542   1.1       cgd 	return (f);
    543   1.1       cgd }
    544   1.1       cgd 
    545   1.9        pk int
    546  1.25   thorpej adelay(int ms, struct delayval *dp)
    547   1.1       cgd {
    548   1.1       cgd 	if (ms == 0)
    549   1.1       cgd 		return (0);
    550   1.1       cgd 	while (dp->delay && ms > dp->delay)
    551   1.1       cgd 		dp++;
    552   1.1       cgd 	return (dp->bits);
    553   1.1       cgd }
    554   1.7        pk #endif
    555   1.1       cgd 
    556  1.20   thorpej char	editedhost[MAXHOSTNAMELEN];
    557   1.1       cgd 
    558   1.9        pk void
    559  1.25   thorpej edithost(char *pat)
    560   1.1       cgd {
    561  1.23       mrg 	char *host = HN;
    562  1.23       mrg 	char *res = editedhost;
    563   1.1       cgd 
    564   1.1       cgd 	if (!pat)
    565   1.1       cgd 		pat = "";
    566   1.1       cgd 	while (*pat) {
    567   1.1       cgd 		switch (*pat) {
    568   1.1       cgd 
    569   1.1       cgd 		case '#':
    570   1.1       cgd 			if (*host)
    571   1.1       cgd 				host++;
    572   1.1       cgd 			break;
    573   1.1       cgd 
    574   1.1       cgd 		case '@':
    575   1.1       cgd 			if (*host)
    576   1.1       cgd 				*res++ = *host++;
    577   1.1       cgd 			break;
    578   1.1       cgd 
    579   1.1       cgd 		default:
    580   1.1       cgd 			*res++ = *pat;
    581   1.1       cgd 			break;
    582   1.1       cgd 
    583   1.1       cgd 		}
    584   1.1       cgd 		if (res == &editedhost[sizeof editedhost - 1]) {
    585   1.1       cgd 			*res = '\0';
    586   1.1       cgd 			return;
    587   1.1       cgd 		}
    588   1.1       cgd 		pat++;
    589   1.1       cgd 	}
    590   1.1       cgd 	if (*host)
    591   1.1       cgd 		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
    592   1.1       cgd 	else
    593   1.1       cgd 		*res = '\0';
    594   1.1       cgd 	editedhost[sizeof editedhost - 1] = '\0';
    595   1.1       cgd }
    596   1.1       cgd 
    597   1.9        pk void
    598  1.25   thorpej makeenv(char *env[])
    599   1.1       cgd {
    600   1.1       cgd 	static char termbuf[128] = "TERM=";
    601  1.23       mrg 	char *p, *q;
    602  1.23       mrg 	char **ep;
    603   1.1       cgd 
    604   1.1       cgd 	ep = env;
    605   1.1       cgd 	if (TT && *TT) {
    606  1.24       mjl 		strlcat(termbuf, TT, sizeof(termbuf));
    607   1.1       cgd 		*ep++ = termbuf;
    608   1.1       cgd 	}
    609  1.22     mikel 	if ((p = EV) != NULL) {
    610   1.1       cgd 		q = p;
    611  1.22     mikel 		while ((q = strchr(q, ',')) != NULL) {
    612   1.1       cgd 			*q++ = '\0';
    613   1.1       cgd 			*ep++ = p;
    614   1.1       cgd 			p = q;
    615   1.1       cgd 		}
    616   1.1       cgd 		if (*p)
    617   1.1       cgd 			*ep++ = p;
    618   1.1       cgd 	}
    619   1.1       cgd 	*ep = (char *)0;
    620   1.1       cgd }
    621   1.1       cgd 
    622   1.1       cgd /*
    623   1.1       cgd  * This speed select mechanism is written for the Develcon DATASWITCH.
    624   1.1       cgd  * The Develcon sends a string of the form "B{speed}\n" at a predefined
    625   1.1       cgd  * baud rate. This string indicates the user's actual speed.
    626   1.1       cgd  * The routine below returns the terminal type mapped from derived speed.
    627   1.1       cgd  */
    628   1.1       cgd struct	portselect {
    629   1.1       cgd 	char	*ps_baud;
    630   1.1       cgd 	char	*ps_type;
    631   1.1       cgd } portspeeds[] = {
    632   1.1       cgd 	{ "B110",	"std.110" },
    633   1.1       cgd 	{ "B134",	"std.134" },
    634   1.1       cgd 	{ "B150",	"std.150" },
    635   1.1       cgd 	{ "B300",	"std.300" },
    636   1.1       cgd 	{ "B600",	"std.600" },
    637   1.1       cgd 	{ "B1200",	"std.1200" },
    638   1.1       cgd 	{ "B2400",	"std.2400" },
    639   1.1       cgd 	{ "B4800",	"std.4800" },
    640   1.1       cgd 	{ "B9600",	"std.9600" },
    641   1.1       cgd 	{ "B19200",	"std.19200" },
    642   1.1       cgd 	{ 0 }
    643   1.1       cgd };
    644   1.1       cgd 
    645   1.1       cgd char *
    646  1.25   thorpej portselector(void)
    647   1.1       cgd {
    648   1.1       cgd 	char c, baud[20], *type = "default";
    649  1.23       mrg 	struct portselect *ps;
    650   1.1       cgd 	int len;
    651   1.1       cgd 
    652   1.1       cgd 	alarm(5*60);
    653   1.1       cgd 	for (len = 0; len < sizeof (baud) - 1; len++) {
    654   1.1       cgd 		if (read(STDIN_FILENO, &c, 1) <= 0)
    655   1.1       cgd 			break;
    656   1.1       cgd 		c &= 0177;
    657   1.1       cgd 		if (c == '\n' || c == '\r')
    658   1.1       cgd 			break;
    659   1.1       cgd 		if (c == 'B')
    660   1.1       cgd 			len = 0;	/* in case of leading garbage */
    661   1.1       cgd 		baud[len] = c;
    662   1.1       cgd 	}
    663   1.1       cgd 	baud[len] = '\0';
    664   1.1       cgd 	for (ps = portspeeds; ps->ps_baud; ps++)
    665   1.1       cgd 		if (strcmp(ps->ps_baud, baud) == 0) {
    666   1.1       cgd 			type = ps->ps_type;
    667   1.1       cgd 			break;
    668   1.1       cgd 		}
    669   1.1       cgd 	sleep(2);	/* wait for connection to complete */
    670   1.1       cgd 	return (type);
    671   1.1       cgd }
    672   1.1       cgd 
    673   1.1       cgd /*
    674   1.1       cgd  * This auto-baud speed select mechanism is written for the Micom 600
    675   1.1       cgd  * portselector. Selection is done by looking at how the character '\r'
    676   1.1       cgd  * is garbled at the different speeds.
    677   1.1       cgd  */
    678   1.1       cgd #include <sys/time.h>
    679   1.1       cgd 
    680   1.1       cgd char *
    681  1.25   thorpej autobaud(void)
    682   1.1       cgd {
    683  1.26   mycroft 	struct pollfd set[1];
    684  1.26   mycroft 	struct timespec timeout;
    685   1.1       cgd 	char c, *type = "9600-baud";
    686   1.1       cgd 
    687  1.14        pk 	(void)tcflush(0, TCIOFLUSH);
    688  1.26   mycroft 	set[0].fd = STDIN_FILENO;
    689  1.26   mycroft 	set[0].events = POLLIN;
    690  1.26   mycroft 	if (poll(set, 1, 5000) <= 0)
    691   1.1       cgd 		return (type);
    692  1.24       mjl 	if (read(STDIN_FILENO, &c, 1) != 1)
    693   1.1       cgd 		return (type);
    694   1.1       cgd 	timeout.tv_sec = 0;
    695  1.26   mycroft 	timeout.tv_nsec = 20000;
    696  1.26   mycroft 	(void)nanosleep(&timeout, NULL);
    697  1.14        pk 	(void)tcflush(0, TCIOFLUSH);
    698   1.1       cgd 	switch (c & 0377) {
    699   1.1       cgd 
    700   1.1       cgd 	case 0200:		/* 300-baud */
    701   1.1       cgd 		type = "300-baud";
    702   1.1       cgd 		break;
    703   1.1       cgd 
    704   1.1       cgd 	case 0346:		/* 1200-baud */
    705   1.1       cgd 		type = "1200-baud";
    706   1.1       cgd 		break;
    707   1.1       cgd 
    708   1.1       cgd 	case  015:		/* 2400-baud */
    709   1.1       cgd 	case 0215:
    710   1.1       cgd 		type = "2400-baud";
    711   1.1       cgd 		break;
    712   1.1       cgd 
    713   1.1       cgd 	default:		/* 4800-baud */
    714   1.1       cgd 		type = "4800-baud";
    715   1.1       cgd 		break;
    716   1.1       cgd 
    717   1.1       cgd 	case 0377:		/* 9600-baud */
    718   1.1       cgd 		type = "9600-baud";
    719   1.1       cgd 		break;
    720   1.1       cgd 	}
    721   1.1       cgd 	return (type);
    722   1.1       cgd }
    723