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