Home | History | Annotate | Line # | Download | only in tip
tip.c revision 1.51.8.1
      1  1.51.8.1       tls /*	$NetBSD: tip.c,v 1.51.8.1 2013/06/23 06:29:02 tls Exp $	*/
      2       1.6       jtc 
      3       1.1       cgd /*
      4       1.6       jtc  * Copyright (c) 1983, 1993
      5       1.6       jtc  *	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.24       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.15     lukem #include <sys/cdefs.h>
     33      1.47   hubertf #include <ctype.h>
     34      1.48   hubertf #include <libgen.h>
     35      1.44       jdc 
     36       1.1       cgd #ifndef lint
     37      1.49     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     38      1.49     lukem  The Regents of the University of California.  All rights reserved.");
     39       1.1       cgd #endif /* not lint */
     40       1.1       cgd 
     41       1.1       cgd #ifndef lint
     42       1.6       jtc #if 0
     43       1.6       jtc static char sccsid[] = "@(#)tip.c	8.1 (Berkeley) 6/6/93";
     44       1.6       jtc #endif
     45  1.51.8.1       tls __RCSID("$NetBSD: tip.c,v 1.51.8.1 2013/06/23 06:29:02 tls Exp $");
     46       1.1       cgd #endif /* not lint */
     47       1.1       cgd 
     48       1.1       cgd /*
     49       1.1       cgd  * tip - UNIX link to other systems
     50       1.1       cgd  *  tip [-v] [-speed] system-name
     51       1.1       cgd  * or
     52      1.44       jdc  *  cu [options] [phone-number|"dir"]
     53       1.1       cgd  */
     54       1.1       cgd #include "tip.h"
     55       1.1       cgd #include "pathnames.h"
     56       1.1       cgd 
     57      1.51     joerg __dead static void	tipusage(void);
     58      1.44       jdc 
     59      1.35     perry int	escape(void);
     60      1.35     perry int	main(int, char **);
     61      1.51     joerg __dead static void	intprompt(int);
     62      1.51     joerg __dead static void	tipin(void);
     63      1.15     lukem 
     64       1.1       cgd char	PNbuf[256];			/* This limits the size of a number */
     65       1.1       cgd 
     66      1.26  christos static char path_phones[] = _PATH_PHONES;
     67      1.26  christos 
     68      1.15     lukem int
     69      1.35     perry main(int argc, char *argv[])
     70       1.1       cgd {
     71      1.25  christos 	char *System = NULL;
     72      1.44       jdc 	int c, i;
     73      1.15     lukem 	char *p;
     74      1.25  christos 	const char *q;
     75       1.1       cgd 	char sbuf[12];
     76      1.44       jdc 	static char brbuf[16];
     77      1.18    mellon 	int fcarg;
     78       1.1       cgd 
     79       1.1       cgd 	gid = getgid();
     80       1.1       cgd 	egid = getegid();
     81       1.1       cgd 	uid = getuid();
     82       1.1       cgd 	euid = geteuid();
     83      1.15     lukem 	if (equal(basename(argv[0]), "cu")) {
     84       1.1       cgd 		cumode = 1;
     85       1.1       cgd 		cumain(argc, argv);
     86       1.1       cgd 		goto cucommon;
     87       1.1       cgd 	}
     88       1.1       cgd 
     89       1.1       cgd 	if (argc > 4) {
     90      1.44       jdc 		tipusage();
     91       1.1       cgd 	}
     92       1.1       cgd 	if (!isatty(0)) {
     93      1.46  christos 		(void)fprintf(stderr, "%s: must be interactive\n", getprogname());
     94       1.1       cgd 		exit(1);
     95       1.1       cgd 	}
     96       1.1       cgd 
     97      1.44       jdc 	while((c = getopt(argc, argv, "v0123456789")) != -1) {
     98      1.44       jdc 		switch(c) {
     99       1.1       cgd 
    100       1.1       cgd 		case 'v':
    101       1.1       cgd 			vflag++;
    102       1.1       cgd 			break;
    103       1.1       cgd 
    104       1.1       cgd 		case '0': case '1': case '2': case '3': case '4':
    105       1.1       cgd 		case '5': case '6': case '7': case '8': case '9':
    106      1.46  christos 			(void)snprintf(brbuf, sizeof(brbuf) -1, "%s%c", brbuf, c);
    107      1.44       jdc 			BR = atoi(brbuf);
    108       1.1       cgd 			break;
    109       1.1       cgd 
    110       1.1       cgd 		default:
    111      1.32       tls 			warnx("%s, unknown option", argv[1]);
    112       1.1       cgd 			break;
    113       1.1       cgd 		}
    114       1.1       cgd 	}
    115       1.1       cgd 
    116      1.44       jdc 	argc -= optind;
    117      1.44       jdc 	argv += optind;
    118      1.44       jdc 
    119      1.44       jdc 	if (argc != 1)
    120      1.44       jdc 		tipusage();
    121      1.44       jdc 	else
    122      1.44       jdc 		System = argv[0];
    123      1.44       jdc 
    124      1.44       jdc 
    125      1.25  christos 	if (System == NULL)
    126       1.1       cgd 		goto notnumber;
    127      1.25  christos 	if (isalpha((unsigned char)*System))
    128       1.1       cgd 		goto notnumber;
    129       1.1       cgd 	/*
    130       1.1       cgd 	 * System name is really a phone number...
    131       1.1       cgd 	 * Copy the number then stomp on the original (in case the number
    132       1.1       cgd 	 *	is private, we don't want 'ps' or 'w' to find it).
    133       1.1       cgd 	 */
    134      1.25  christos 	if (strlen(System) > sizeof PNbuf - 1) {
    135      1.32       tls 		errx(1, "phone number too long (max = %d bytes)",
    136      1.17       mrg 			(int)sizeof(PNbuf) - 1);
    137       1.1       cgd 	}
    138      1.27       jrf 	(void)strlcpy(PNbuf, System, sizeof(PNbuf));
    139      1.25  christos 	for (p = System; *p; p++)
    140       1.1       cgd 		*p = '\0';
    141       1.1       cgd 	PN = PNbuf;
    142      1.15     lukem 	(void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR);
    143      1.25  christos 	System = sbuf;
    144       1.1       cgd 
    145       1.1       cgd notnumber:
    146       1.1       cgd 	(void)signal(SIGINT, cleanup);
    147       1.1       cgd 	(void)signal(SIGQUIT, cleanup);
    148       1.1       cgd 	(void)signal(SIGHUP, cleanup);
    149       1.1       cgd 	(void)signal(SIGTERM, cleanup);
    150       1.1       cgd 
    151      1.25  christos 	if ((i = hunt(System)) == 0) {
    152      1.46  christos 		(void)printf("all ports busy\n");
    153       1.1       cgd 		exit(3);
    154       1.1       cgd 	}
    155       1.1       cgd 	if (i == -1) {
    156      1.36     perry 		errx(3, "link down\n");
    157       1.1       cgd 	}
    158       1.1       cgd 	setbuf(stdout, NULL);
    159       1.1       cgd 
    160       1.1       cgd 	/*
    161       1.1       cgd 	 * Kludge, their's no easy way to get the initialization
    162       1.1       cgd 	 *   in the right order, so force it here
    163       1.1       cgd 	 */
    164      1.15     lukem 	if ((PH = getenv("PHONES")) == NULL)
    165      1.26  christos 		PH = path_phones;
    166       1.1       cgd 	vinit();				/* init variables */
    167      1.31       tls 	setparity("none");			/* set the parity table */
    168       1.1       cgd 
    169       1.1       cgd 	/*
    170       1.1       cgd 	 * Hardwired connections require the
    171       1.1       cgd 	 *  line speed set before they make any transmissions
    172       1.1       cgd 	 *  (this is particularly true of things like a DF03-AC)
    173       1.1       cgd 	 */
    174      1.33       tls 	if (HW) {
    175      1.40      yamt 		if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
    176      1.36     perry 			errx(3, "bad baud rate %d",
    177      1.33       tls 			    (int)number(value(BAUDRATE)));
    178      1.33       tls 		}
    179      1.33       tls 	}
    180      1.46  christos 	if ((q = tip_connect()) != NULL) {
    181      1.36     perry 		errx(1, "\07%s\n[EOT]\n", q);
    182       1.1       cgd 	}
    183      1.33       tls 	if (!HW) {
    184      1.40      yamt 		if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
    185      1.36     perry 			errx(3, "bad baud rate %d",
    186      1.33       tls 			    (int)number(value(BAUDRATE)));
    187      1.33       tls 		}
    188      1.33       tls 	}
    189      1.35     perry 
    190      1.18    mellon 
    191      1.41       tls cucommon:
    192      1.41       tls 	/*
    193      1.41       tls 	 * From here down the code is shared with
    194      1.41       tls 	 * the "cu" version of tip.
    195      1.41       tls 	 */
    196      1.41       tls 
    197      1.19    mellon 	/*
    198      1.19    mellon 	 * Direct connections with no carrier require using O_NONBLOCK on
    199      1.19    mellon 	 * open, but we don't want to keep O_NONBLOCK after open because it
    200      1.19    mellon 	 * will cause busy waits.
    201      1.19    mellon 	 */
    202      1.19    mellon 	if (DC &&
    203      1.19    mellon 	    ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
    204      1.19    mellon 	     fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
    205      1.36     perry 		err(1, "can't clear O_NONBLOCK");
    206      1.18    mellon 	}
    207      1.35     perry 
    208      1.46  christos 	(void)tcgetattr(0, &defterm);
    209       1.8        pk 	term = defterm;
    210       1.8        pk 	term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
    211       1.8        pk 	term.c_iflag &= ~(INPCK|ICRNL);
    212       1.8        pk 	term.c_oflag &= ~OPOST;
    213       1.8        pk 	term.c_cc[VMIN] = 1;
    214       1.8        pk 	term.c_cc[VTIME] = 0;
    215       1.8        pk 	defchars = term;
    216       1.8        pk 	term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
    217      1.35     perry 		term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
    218       1.8        pk 	 	term.c_cc[VLNEXT] = _POSIX_VDISABLE;
    219       1.1       cgd 	raw();
    220       1.1       cgd 
    221      1.46  christos 	(void)pipe(attndes);
    222      1.46  christos 	(void)pipe(fildes);
    223      1.46  christos 	(void)pipe(repdes);
    224      1.15     lukem 	(void)signal(SIGALRM, alrmtimeout);
    225       1.1       cgd 
    226       1.1       cgd 	/*
    227       1.1       cgd 	 * Everything's set up now:
    228       1.1       cgd 	 *	connection established (hardwired or dialup)
    229       1.1       cgd 	 *	line conditioned (baud rate, mode, etc.)
    230       1.1       cgd 	 *	internal data structures (variables)
    231       1.1       cgd 	 * so, fork one process for local side and one for remote.
    232       1.1       cgd 	 */
    233      1.46  christos 	(void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
    234      1.15     lukem 	switch (pid = fork()) {
    235      1.15     lukem 	default:
    236       1.1       cgd 		tipin();
    237      1.15     lukem 		break;
    238      1.15     lukem 	case 0:
    239       1.1       cgd 		tipout();
    240      1.15     lukem 		break;
    241      1.15     lukem 	case -1:
    242      1.15     lukem 		err(1, "can't fork");
    243      1.15     lukem 	}
    244       1.1       cgd 	/*NOTREACHED*/
    245      1.15     lukem 	exit(0);	/* XXX: pacify gcc */
    246       1.1       cgd }
    247       1.1       cgd 
    248       1.1       cgd void
    249      1.44       jdc tipusage(void)
    250      1.44       jdc {
    251      1.46  christos 	(void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n",
    252      1.44       jdc 	    getprogname());
    253      1.46  christos 	(void)fprintf(stderr, "       %s [-v] [-speed] phone-number\n",
    254      1.44       jdc 	    getprogname());
    255      1.44       jdc 	exit(1);
    256      1.44       jdc }
    257      1.44       jdc 
    258      1.44       jdc void
    259      1.39  christos /*ARGSUSED*/
    260      1.46  christos cleanup(int dummy __unused)
    261       1.1       cgd {
    262       1.1       cgd 
    263       1.1       cgd 	if (odisc)
    264      1.46  christos 		(void)ioctl(0, TIOCSETD, &odisc);
    265       1.1       cgd 	exit(0);
    266       1.1       cgd }
    267       1.1       cgd 
    268       1.1       cgd /*
    269       1.1       cgd  * put the controlling keyboard into raw mode
    270       1.1       cgd  */
    271      1.15     lukem void
    272      1.35     perry raw(void)
    273       1.1       cgd {
    274      1.21       mrg 
    275      1.46  christos 	(void)tcsetattr(0, TCSADRAIN, &term);
    276       1.1       cgd }
    277       1.1       cgd 
    278       1.1       cgd 
    279       1.1       cgd /*
    280       1.1       cgd  * return keyboard to normal mode
    281       1.1       cgd  */
    282      1.15     lukem void
    283      1.35     perry unraw(void)
    284       1.1       cgd {
    285      1.21       mrg 
    286      1.46  christos 	(void)tcsetattr(0, TCSADRAIN, &defterm);
    287       1.1       cgd }
    288       1.1       cgd 
    289       1.1       cgd static	jmp_buf promptbuf;
    290       1.1       cgd 
    291       1.1       cgd /*
    292       1.1       cgd  * Print string ``s'', then read a string
    293       1.1       cgd  *  in from the terminal.  Handles signals & allows use of
    294       1.1       cgd  *  normal erase and kill characters.
    295       1.1       cgd  */
    296      1.15     lukem int
    297      1.45  christos prompt(const char *s, char *volatile p, size_t l)
    298       1.1       cgd {
    299      1.15     lukem 	int c;
    300      1.15     lukem 	char *b = p;
    301       1.1       cgd 	sig_t oint, oquit;
    302       1.1       cgd 
    303       1.1       cgd 	stoprompt = 0;
    304       1.1       cgd 	oint = signal(SIGINT, intprompt);
    305       1.1       cgd 	oquit = signal(SIGQUIT, SIG_IGN);
    306       1.1       cgd 	unraw();
    307      1.46  christos 	(void)printf("%s", s);
    308       1.1       cgd 	if (setjmp(promptbuf) == 0)
    309      1.21       mrg 		while ((c = getchar()) != -1 && (*p = c) != '\n' &&
    310      1.21       mrg 		    b + l > p)
    311       1.1       cgd 			p++;
    312       1.1       cgd 	*p = '\0';
    313       1.1       cgd 
    314       1.1       cgd 	raw();
    315       1.1       cgd 	(void)signal(SIGINT, oint);
    316       1.1       cgd 	(void)signal(SIGQUIT, oquit);
    317       1.1       cgd 	return (stoprompt || p == b);
    318       1.1       cgd }
    319       1.1       cgd 
    320       1.1       cgd /*
    321       1.1       cgd  * Interrupt service routine during prompting
    322       1.1       cgd  */
    323      1.51     joerg static void
    324      1.39  christos /*ARGSUSED*/
    325      1.46  christos intprompt(int dummy __unused)
    326       1.1       cgd {
    327       1.1       cgd 
    328       1.1       cgd 	(void)signal(SIGINT, SIG_IGN);
    329       1.1       cgd 	stoprompt = 1;
    330      1.46  christos 	(void)printf("\r\n");
    331       1.1       cgd 	longjmp(promptbuf, 1);
    332       1.1       cgd }
    333       1.1       cgd 
    334       1.1       cgd /*
    335       1.1       cgd  * ****TIPIN   TIPIN****
    336       1.1       cgd  */
    337      1.51     joerg static void
    338      1.35     perry tipin(void)
    339       1.1       cgd {
    340       1.1       cgd 	char gch, bol = 1;
    341       1.1       cgd 
    342       1.1       cgd 	/*
    343       1.1       cgd 	 * Kinda klugey here...
    344       1.1       cgd 	 *   check for scripting being turned on from the .tiprc file,
    345       1.1       cgd 	 *   but be careful about just using setscript(), as we may
    346       1.1       cgd 	 *   send a SIGEMT before tipout has a chance to set up catching
    347       1.1       cgd 	 *   it; so wait a second, then setscript()
    348       1.1       cgd 	 */
    349       1.1       cgd 	if (boolean(value(SCRIPT))) {
    350      1.46  christos 		(void)sleep(1);
    351       1.1       cgd 		setscript();
    352       1.1       cgd 	}
    353       1.1       cgd 
    354      1.39  christos 	for (;;) {
    355       1.8        pk 		gch = getchar()&STRIP_PAR;
    356       1.1       cgd 		if ((gch == character(value(ESCAPE))) && bol) {
    357       1.1       cgd 			if (!(gch = escape()))
    358       1.1       cgd 				continue;
    359      1.35     perry 		} else if (!cumode &&
    360      1.16     lukem 		    gch && gch == character(value(RAISECHAR))) {
    361       1.9       cgd 			setboolean(value(RAISE), !boolean(value(RAISE)));
    362       1.1       cgd 			continue;
    363       1.1       cgd 		} else if (gch == '\r') {
    364       1.1       cgd 			bol = 1;
    365      1.20   thorpej 			xpwrite(FD, &gch, 1);
    366       1.1       cgd 			if (boolean(value(HALFDUPLEX)))
    367      1.46  christos 				(void)printf("\r\n");
    368       1.1       cgd 			continue;
    369      1.16     lukem 		} else if (!cumode && gch && gch == character(value(FORCE)))
    370       1.8        pk 			gch = getchar()&STRIP_PAR;
    371       1.1       cgd 		bol = any(gch, value(EOL));
    372      1.22  christos 		if (boolean(value(RAISE)) && islower((unsigned char)gch))
    373      1.28       dsl 			gch = toupper((unsigned char)gch);
    374      1.20   thorpej 		xpwrite(FD, &gch, 1);
    375       1.1       cgd 		if (boolean(value(HALFDUPLEX)))
    376      1.46  christos 			(void)printf("%c", gch);
    377       1.1       cgd 	}
    378       1.1       cgd }
    379       1.1       cgd 
    380       1.1       cgd /*
    381       1.1       cgd  * Escape handler --
    382       1.1       cgd  *  called on recognition of ``escapec'' at the beginning of a line
    383       1.1       cgd  */
    384      1.15     lukem int
    385      1.35     perry escape(void)
    386       1.1       cgd {
    387      1.15     lukem 	char gch;
    388      1.15     lukem 	esctable_t *p;
    389       1.1       cgd 	char c = character(value(ESCAPE));
    390       1.1       cgd 
    391       1.8        pk 	gch = (getchar()&STRIP_PAR);
    392       1.1       cgd 	for (p = etable; p->e_char; p++)
    393       1.1       cgd 		if (p->e_char == gch) {
    394       1.1       cgd 			if ((p->e_flags&PRIV) && uid)
    395       1.1       cgd 				continue;
    396      1.46  christos 			(void)printf("%s", ctrl(c));
    397       1.1       cgd 			(*p->e_func)(gch);
    398       1.1       cgd 			return (0);
    399       1.1       cgd 		}
    400       1.1       cgd 	/* ESCAPE ESCAPE forces ESCAPE */
    401       1.1       cgd 	if (c != gch)
    402      1.20   thorpej 		xpwrite(FD, &c, 1);
    403       1.1       cgd 	return (gch);
    404       1.1       cgd }
    405       1.1       cgd 
    406      1.15     lukem int
    407      1.35     perry any(char c, const char *p)
    408       1.1       cgd {
    409      1.21       mrg 
    410       1.1       cgd 	while (p && *p)
    411       1.1       cgd 		if (*p++ == c)
    412       1.1       cgd 			return (1);
    413       1.1       cgd 	return (0);
    414       1.1       cgd }
    415       1.1       cgd 
    416       1.1       cgd char *
    417      1.35     perry interp(const char *s)
    418       1.1       cgd {
    419       1.1       cgd 	static char buf[256];
    420      1.25  christos 	char *p = buf, c;
    421      1.25  christos 	const char *q;
    422       1.1       cgd 
    423      1.21       mrg 	while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
    424       1.1       cgd 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
    425       1.1       cgd 			if (*q++ == c) {
    426       1.1       cgd 				*p++ = '\\'; *p++ = *q;
    427       1.1       cgd 				goto next;
    428       1.1       cgd 			}
    429       1.1       cgd 		if (c < 040) {
    430       1.1       cgd 			*p++ = '^'; *p++ = c + 'A'-1;
    431       1.1       cgd 		} else if (c == 0177) {
    432       1.1       cgd 			*p++ = '^'; *p++ = '?';
    433       1.1       cgd 		} else
    434       1.1       cgd 			*p++ = c;
    435       1.1       cgd 	next:
    436       1.1       cgd 		;
    437       1.1       cgd 	}
    438       1.1       cgd 	*p = '\0';
    439       1.1       cgd 	return (buf);
    440       1.1       cgd }
    441       1.1       cgd 
    442       1.1       cgd char *
    443      1.35     perry ctrl(char c)
    444       1.1       cgd {
    445       1.1       cgd 	static char s[3];
    446       1.1       cgd 
    447       1.1       cgd 	if (c < 040 || c == 0177) {
    448       1.1       cgd 		s[0] = '^';
    449       1.1       cgd 		s[1] = c == 0177 ? '?' : c+'A'-1;
    450       1.1       cgd 		s[2] = '\0';
    451       1.1       cgd 	} else {
    452       1.1       cgd 		s[0] = c;
    453       1.1       cgd 		s[1] = '\0';
    454       1.1       cgd 	}
    455       1.1       cgd 	return (s);
    456       1.1       cgd }
    457       1.1       cgd 
    458       1.1       cgd /*
    459       1.1       cgd  * Help command
    460       1.1       cgd  */
    461      1.15     lukem void
    462      1.35     perry help(char c)
    463       1.1       cgd {
    464      1.15     lukem 	esctable_t *p;
    465       1.1       cgd 
    466      1.46  christos 	(void)printf("%c\r\n", c);
    467       1.1       cgd 	for (p = etable; p->e_char; p++) {
    468       1.1       cgd 		if ((p->e_flags&PRIV) && uid)
    469       1.1       cgd 			continue;
    470      1.46  christos 		(void)printf("%2s", ctrl(character(value(ESCAPE))));
    471      1.46  christos 		(void)printf("%-2s %c   %s\r\n", ctrl(p->e_char),
    472       1.1       cgd 			p->e_flags&EXP ? '*': ' ', p->e_help);
    473       1.1       cgd 	}
    474       1.1       cgd }
    475       1.1       cgd 
    476       1.1       cgd /*
    477       1.1       cgd  * Set up the "remote" tty's state
    478       1.1       cgd  */
    479      1.33       tls int
    480      1.39  christos ttysetup(speed_t spd)
    481       1.1       cgd {
    482       1.8        pk 	struct termios	cntrl;
    483       1.1       cgd 
    484      1.46  christos 	(void)tcgetattr(FD, &cntrl);
    485      1.46  christos 	(void)cfsetospeed(&cntrl, spd);
    486      1.46  christos 	(void)cfsetispeed(&cntrl, spd);
    487       1.8        pk 	cntrl.c_cflag &= ~(CSIZE|PARENB);
    488       1.8        pk 	cntrl.c_cflag |= CS8;
    489      1.14    mellon 	if (DC)
    490      1.13    mellon 		cntrl.c_cflag |= CLOCAL;
    491      1.29      yamt 	if (boolean(value(HARDWAREFLOW)))
    492      1.35     perry 		cntrl.c_cflag |= CRTSCTS;
    493       1.8        pk 	cntrl.c_iflag &= ~(ISTRIP|ICRNL);
    494       1.8        pk 	cntrl.c_oflag &= ~OPOST;
    495       1.8        pk 	cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
    496       1.8        pk 	cntrl.c_cc[VMIN] = 1;
    497       1.8        pk 	cntrl.c_cc[VTIME] = 0;
    498       1.1       cgd 	if (boolean(value(TAND)))
    499  1.51.8.1       tls 		cntrl.c_iflag |= IXOFF|IXON|IXANY;
    500  1.51.8.1       tls 	else
    501  1.51.8.1       tls 		cntrl.c_iflag &= ~(IXOFF|IXON|IXANY);
    502      1.46  christos 	return tcsetattr(FD, TCSAFLUSH, &cntrl);
    503       1.1       cgd }
    504       1.1       cgd 
    505       1.1       cgd static char partab[0200];
    506       1.1       cgd 
    507       1.1       cgd /*
    508       1.1       cgd  * Do a write to the remote machine with the correct parity.
    509       1.1       cgd  * We are doing 8 bit wide output, so we just generate a character
    510       1.1       cgd  * with the right parity and output it.
    511       1.1       cgd  */
    512      1.15     lukem void
    513      1.38     perry xpwrite(int fd, char *buf, size_t n)
    514       1.1       cgd {
    515      1.50     lukem 	size_t i;
    516      1.15     lukem 	char *bp;
    517       1.1       cgd 
    518       1.1       cgd 	bp = buf;
    519       1.6       jtc 	if (bits8 == 0)
    520       1.6       jtc 		for (i = 0; i < n; i++) {
    521       1.6       jtc 			*bp = partab[(*bp) & 0177];
    522       1.6       jtc 			bp++;
    523       1.1       cgd 		}
    524       1.6       jtc 	if (write(fd, buf, n) < 0) {
    525       1.1       cgd 		if (errno == EIO)
    526       1.1       cgd 			tipabort("Lost carrier.");
    527       1.1       cgd 		/* this is questionable */
    528      1.32       tls 		warn("write");
    529       1.1       cgd 	}
    530       1.1       cgd }
    531       1.1       cgd 
    532       1.1       cgd /*
    533       1.1       cgd  * Build a parity table with appropriate high-order bit.
    534       1.1       cgd  */
    535      1.15     lukem void
    536      1.35     perry setparity(const char *defparity)
    537       1.1       cgd {
    538      1.15     lukem 	int i, flip, clr, set;
    539      1.25  christos 	const char *parity;
    540      1.26  christos 	static char *curpar;
    541       1.1       cgd 
    542      1.39  christos 	if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') {
    543      1.26  christos 		if (curpar != NULL)
    544      1.26  christos 			free(curpar);
    545      1.26  christos 		value(PARITY) = curpar = strdup(defparity);
    546      1.26  christos 	}
    547       1.1       cgd 	parity = value(PARITY);
    548       1.1       cgd 	if (equal(parity, "none")) {
    549       1.1       cgd 		bits8 = 1;
    550       1.1       cgd 		return;
    551       1.1       cgd 	}
    552       1.1       cgd 	bits8 = 0;
    553       1.1       cgd 	flip = 0;
    554       1.1       cgd 	clr = 0377;
    555       1.1       cgd 	set = 0;
    556       1.1       cgd 	if (equal(parity, "odd"))
    557       1.1       cgd 		flip = 0200;			/* reverse bit 7 */
    558       1.1       cgd 	else if (equal(parity, "zero"))
    559       1.1       cgd 		clr = 0177;			/* turn off bit 7 */
    560       1.1       cgd 	else if (equal(parity, "one"))
    561       1.1       cgd 		set = 0200;			/* turn on bit 7 */
    562       1.1       cgd 	else if (!equal(parity, "even")) {
    563      1.46  christos 		(void)fprintf(stderr, "%s: unknown parity value\r\n", parity);
    564      1.46  christos 		(void)fflush(stderr);
    565       1.1       cgd 	}
    566       1.1       cgd 	for (i = 0; i < 0200; i++)
    567      1.15     lukem 		partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
    568       1.1       cgd }
    569