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