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