Home | History | Annotate | Line # | Download | only in tip
tip.c revision 1.23
      1  1.23        is /*	$NetBSD: tip.c,v 1.23 2000/10/11 14:46:19 is 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.15     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.15     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     39  1.15     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.6       jtc #if 0
     44   1.6       jtc static char sccsid[] = "@(#)tip.c	8.1 (Berkeley) 6/6/93";
     45   1.6       jtc #endif
     46  1.23        is __RCSID("$NetBSD: tip.c,v 1.23 2000/10/11 14:46:19 is Exp $");
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * tip - UNIX link to other systems
     51   1.1       cgd  *  tip [-v] [-speed] system-name
     52   1.1       cgd  * or
     53   1.1       cgd  *  cu phone-number [-s speed] [-l line] [-a acu]
     54   1.1       cgd  */
     55   1.1       cgd #include "tip.h"
     56   1.1       cgd #include "pathnames.h"
     57   1.1       cgd 
     58   1.1       cgd /*
     59   1.1       cgd  * Baud rate mapping table
     60   1.1       cgd  */
     61   1.8        pk int rates[] = {
     62   1.1       cgd 	0, 50, 75, 110, 134, 150, 200, 300, 600,
     63   1.2       cgd 	1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
     64   1.1       cgd };
     65   1.1       cgd 
     66  1.15     lukem int	escape __P((void));
     67  1.15     lukem int	main __P((int, char **));
     68  1.15     lukem void	intprompt __P((int));
     69  1.15     lukem void	tipin __P((void));
     70  1.15     lukem 
     71   1.1       cgd char	PNbuf[256];			/* This limits the size of a number */
     72   1.1       cgd 
     73  1.15     lukem int
     74   1.1       cgd main(argc, argv)
     75  1.15     lukem 	int argc;
     76   1.1       cgd 	char *argv[];
     77   1.1       cgd {
     78  1.15     lukem 	char *system = NULL;
     79  1.15     lukem 	int i;
     80  1.15     lukem 	char *p;
     81   1.1       cgd 	char sbuf[12];
     82  1.18    mellon 	int fcarg;
     83   1.1       cgd 
     84   1.1       cgd 	gid = getgid();
     85   1.1       cgd 	egid = getegid();
     86   1.1       cgd 	uid = getuid();
     87   1.1       cgd 	euid = geteuid();
     88  1.15     lukem 	if (equal(basename(argv[0]), "cu")) {
     89   1.1       cgd 		cumode = 1;
     90   1.1       cgd 		cumain(argc, argv);
     91   1.1       cgd 		goto cucommon;
     92   1.1       cgd 	}
     93   1.1       cgd 
     94   1.1       cgd 	if (argc > 4) {
     95   1.1       cgd 		fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
     96   1.1       cgd 		exit(1);
     97   1.1       cgd 	}
     98   1.1       cgd 	if (!isatty(0)) {
     99   1.1       cgd 		fprintf(stderr, "tip: must be interactive\n");
    100   1.1       cgd 		exit(1);
    101   1.1       cgd 	}
    102   1.1       cgd 
    103   1.1       cgd 	for (; argc > 1; argv++, argc--) {
    104   1.1       cgd 		if (argv[1][0] != '-')
    105   1.1       cgd 			system = argv[1];
    106   1.1       cgd 		else switch (argv[1][1]) {
    107   1.1       cgd 
    108   1.1       cgd 		case 'v':
    109   1.1       cgd 			vflag++;
    110   1.1       cgd 			break;
    111   1.1       cgd 
    112   1.1       cgd 		case '0': case '1': case '2': case '3': case '4':
    113   1.1       cgd 		case '5': case '6': case '7': case '8': case '9':
    114   1.1       cgd 			BR = atoi(&argv[1][1]);
    115   1.1       cgd 			break;
    116   1.1       cgd 
    117   1.1       cgd 		default:
    118   1.1       cgd 			fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
    119   1.1       cgd 			break;
    120   1.1       cgd 		}
    121   1.1       cgd 	}
    122   1.1       cgd 
    123  1.15     lukem 	if (system == NULL)
    124   1.1       cgd 		goto notnumber;
    125  1.22  christos 	if (isalpha((unsigned char)*system))
    126   1.1       cgd 		goto notnumber;
    127   1.1       cgd 	/*
    128   1.1       cgd 	 * System name is really a phone number...
    129   1.1       cgd 	 * Copy the number then stomp on the original (in case the number
    130   1.1       cgd 	 *	is private, we don't want 'ps' or 'w' to find it).
    131   1.1       cgd 	 */
    132   1.1       cgd 	if (strlen(system) > sizeof PNbuf - 1) {
    133   1.1       cgd 		fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
    134  1.17       mrg 			(int)sizeof(PNbuf) - 1);
    135   1.1       cgd 		exit(1);
    136   1.1       cgd 	}
    137  1.21       mrg 	strncpy(PNbuf, system, sizeof PNbuf - 1);
    138  1.21       mrg 	PNbuf[sizeof PNbuf - 1] = '\0';
    139   1.1       cgd 	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.1       cgd 	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.1       cgd 	if ((i = hunt(system)) == 0) {
    152   1.1       cgd 		printf("all ports busy\n");
    153   1.1       cgd 		exit(3);
    154   1.1       cgd 	}
    155   1.1       cgd 	if (i == -1) {
    156   1.1       cgd 		printf("link down\n");
    157   1.1       cgd 		(void)uu_unlock(uucplock);
    158   1.1       cgd 		exit(3);
    159   1.1       cgd 	}
    160   1.1       cgd 	setbuf(stdout, NULL);
    161   1.1       cgd 	loginit();
    162   1.1       cgd 
    163   1.1       cgd 	/*
    164   1.3    andrew 	 * Now that we have the logfile and the ACU open
    165   1.3    andrew 	 *  return to the real uid and gid.  These things will
    166   1.3    andrew 	 *  be closed on exit.  Swap real and effective uid's
    167   1.3    andrew 	 *  so we can get the original permissions back
    168   1.3    andrew 	 *  for removing the uucp lock.
    169   1.3    andrew 	 */
    170   1.3    andrew 	user_uid();
    171   1.3    andrew 
    172   1.3    andrew 	/*
    173   1.1       cgd 	 * Kludge, their's no easy way to get the initialization
    174   1.1       cgd 	 *   in the right order, so force it here
    175   1.1       cgd 	 */
    176  1.15     lukem 	if ((PH = getenv("PHONES")) == NULL)
    177   1.1       cgd 		PH = _PATH_PHONES;
    178   1.1       cgd 	vinit();				/* init variables */
    179   1.1       cgd 	setparity("even");			/* set the parity table */
    180   1.8        pk 	if ((i = speed(number(value(BAUDRATE)))) == 0) {
    181  1.15     lukem 		printf("tip: bad baud rate %d\n", (int)number(value(BAUDRATE)));
    182   1.3    andrew 		daemon_uid();
    183   1.1       cgd 		(void)uu_unlock(uucplock);
    184   1.1       cgd 		exit(3);
    185   1.1       cgd 	}
    186   1.1       cgd 
    187   1.1       cgd 	/*
    188   1.1       cgd 	 * Hardwired connections require the
    189   1.1       cgd 	 *  line speed set before they make any transmissions
    190   1.1       cgd 	 *  (this is particularly true of things like a DF03-AC)
    191   1.1       cgd 	 */
    192   1.1       cgd 	if (HW)
    193   1.1       cgd 		ttysetup(i);
    194  1.15     lukem 	if ((p = connect()) != NULL) {
    195   1.1       cgd 		printf("\07%s\n[EOT]\n", p);
    196   1.1       cgd 		daemon_uid();
    197   1.1       cgd 		(void)uu_unlock(uucplock);
    198   1.1       cgd 		exit(1);
    199   1.1       cgd 	}
    200   1.1       cgd 	if (!HW)
    201   1.1       cgd 		ttysetup(i);
    202  1.18    mellon 
    203  1.19    mellon 	/*
    204  1.19    mellon 	 * Direct connections with no carrier require using O_NONBLOCK on
    205  1.19    mellon 	 * open, but we don't want to keep O_NONBLOCK after open because it
    206  1.19    mellon 	 * will cause busy waits.
    207  1.19    mellon 	 */
    208  1.19    mellon 	if (DC &&
    209  1.19    mellon 	    ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
    210  1.19    mellon 	     fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
    211  1.18    mellon 		printf("tip: can't clear O_NONBLOCK: %s", strerror (errno));
    212  1.18    mellon 		daemon_uid();
    213  1.21       mrg 		(void)uu_unlock(uucplock);
    214  1.18    mellon 		exit(1);
    215  1.18    mellon 	}
    216  1.18    mellon 
    217   1.1       cgd cucommon:
    218   1.1       cgd 	/*
    219   1.1       cgd 	 * From here down the code is shared with
    220   1.1       cgd 	 * the "cu" version of tip.
    221   1.1       cgd 	 */
    222   1.1       cgd 
    223   1.8        pk 	tcgetattr(0, &defterm);
    224   1.8        pk 	term = defterm;
    225   1.8        pk 	term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
    226   1.8        pk 	term.c_iflag &= ~(INPCK|ICRNL);
    227   1.8        pk 	term.c_oflag &= ~OPOST;
    228   1.8        pk 	term.c_cc[VMIN] = 1;
    229   1.8        pk 	term.c_cc[VTIME] = 0;
    230   1.8        pk 	defchars = term;
    231   1.8        pk 	term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
    232   1.8        pk 		term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
    233   1.8        pk 	 	term.c_cc[VLNEXT] = _POSIX_VDISABLE;
    234   1.1       cgd 	raw();
    235   1.1       cgd 
    236   1.1       cgd 	pipe(fildes); pipe(repdes);
    237  1.15     lukem 	(void)signal(SIGALRM, alrmtimeout);
    238   1.1       cgd 
    239   1.1       cgd 	/*
    240   1.1       cgd 	 * Everything's set up now:
    241   1.1       cgd 	 *	connection established (hardwired or dialup)
    242   1.1       cgd 	 *	line conditioned (baud rate, mode, etc.)
    243   1.1       cgd 	 *	internal data structures (variables)
    244   1.1       cgd 	 * so, fork one process for local side and one for remote.
    245   1.1       cgd 	 */
    246  1.23        is 	printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
    247  1.15     lukem 	switch (pid = fork()) {
    248  1.15     lukem 	default:
    249   1.1       cgd 		tipin();
    250  1.15     lukem 		break;
    251  1.15     lukem 	case 0:
    252   1.1       cgd 		tipout();
    253  1.15     lukem 		break;
    254  1.15     lukem 	case -1:
    255  1.15     lukem 		err(1, "can't fork");
    256  1.15     lukem 	}
    257   1.1       cgd 	/*NOTREACHED*/
    258  1.15     lukem 	exit(0);	/* XXX: pacify gcc */
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd void
    262  1.15     lukem cleanup(dummy)
    263  1.15     lukem 	int dummy;
    264   1.1       cgd {
    265   1.1       cgd 
    266   1.1       cgd 	daemon_uid();
    267   1.1       cgd 	(void)uu_unlock(uucplock);
    268   1.1       cgd 	if (odisc)
    269   1.1       cgd 		ioctl(0, TIOCSETD, (char *)&odisc);
    270   1.1       cgd 	exit(0);
    271   1.1       cgd }
    272   1.1       cgd 
    273   1.1       cgd /*
    274   1.1       cgd  * Muck with user ID's.  We are setuid to the owner of the lock
    275   1.1       cgd  * directory when we start.  user_uid() reverses real and effective
    276   1.1       cgd  * ID's after startup, to run with the user's permissions.
    277   1.1       cgd  * daemon_uid() switches back to the privileged uid for unlocking.
    278   1.1       cgd  * Finally, to avoid running a shell with the wrong real uid,
    279   1.1       cgd  * shell_uid() sets real and effective uid's to the user's real ID.
    280   1.1       cgd  */
    281   1.1       cgd static int uidswapped;
    282   1.1       cgd 
    283  1.15     lukem void
    284   1.1       cgd user_uid()
    285   1.1       cgd {
    286  1.21       mrg 
    287   1.1       cgd 	if (uidswapped == 0) {
    288   1.5        pk 		seteuid(uid);
    289   1.1       cgd 		uidswapped = 1;
    290   1.1       cgd 	}
    291   1.1       cgd }
    292   1.1       cgd 
    293  1.15     lukem void
    294   1.1       cgd daemon_uid()
    295   1.1       cgd {
    296   1.1       cgd 
    297   1.1       cgd 	if (uidswapped) {
    298   1.5        pk 		seteuid(euid);
    299   1.1       cgd 		uidswapped = 0;
    300   1.1       cgd 	}
    301   1.1       cgd }
    302   1.1       cgd 
    303  1.15     lukem void
    304   1.1       cgd shell_uid()
    305   1.1       cgd {
    306  1.21       mrg 
    307   1.6       jtc 	seteuid(uid);
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.1       cgd /*
    311   1.1       cgd  * put the controlling keyboard into raw mode
    312   1.1       cgd  */
    313  1.15     lukem void
    314   1.1       cgd raw()
    315   1.1       cgd {
    316  1.21       mrg 
    317   1.8        pk 	tcsetattr(0, TCSADRAIN, &term);
    318   1.1       cgd }
    319   1.1       cgd 
    320   1.1       cgd 
    321   1.1       cgd /*
    322   1.1       cgd  * return keyboard to normal mode
    323   1.1       cgd  */
    324  1.15     lukem void
    325   1.1       cgd unraw()
    326   1.1       cgd {
    327  1.21       mrg 
    328   1.8        pk 	tcsetattr(0, TCSADRAIN, &defterm);
    329   1.1       cgd }
    330   1.1       cgd 
    331   1.1       cgd static	jmp_buf promptbuf;
    332   1.1       cgd 
    333   1.1       cgd /*
    334   1.1       cgd  * Print string ``s'', then read a string
    335   1.1       cgd  *  in from the terminal.  Handles signals & allows use of
    336   1.1       cgd  *  normal erase and kill characters.
    337   1.1       cgd  */
    338  1.15     lukem int
    339  1.21       mrg prompt(s, p, l)
    340   1.1       cgd 	char *s;
    341  1.15     lukem 	char *p;
    342  1.21       mrg 	size_t l;
    343   1.1       cgd {
    344  1.15     lukem 	int c;
    345  1.15     lukem 	char *b = p;
    346   1.1       cgd 	sig_t oint, oquit;
    347   1.1       cgd 
    348  1.15     lukem #if __GNUC__		/* XXX: pacify gcc */
    349  1.15     lukem 	(void)&p;
    350  1.15     lukem #endif
    351  1.15     lukem 
    352   1.1       cgd 	stoprompt = 0;
    353   1.1       cgd 	oint = signal(SIGINT, intprompt);
    354   1.1       cgd 	oquit = signal(SIGQUIT, SIG_IGN);
    355   1.1       cgd 	unraw();
    356   1.1       cgd 	printf("%s", s);
    357   1.1       cgd 	if (setjmp(promptbuf) == 0)
    358  1.21       mrg 		while ((c = getchar()) != -1 && (*p = c) != '\n' &&
    359  1.21       mrg 		    b + l > p)
    360   1.1       cgd 			p++;
    361   1.1       cgd 	*p = '\0';
    362   1.1       cgd 
    363   1.1       cgd 	raw();
    364   1.1       cgd 	(void)signal(SIGINT, oint);
    365   1.1       cgd 	(void)signal(SIGQUIT, oquit);
    366   1.1       cgd 	return (stoprompt || p == b);
    367   1.1       cgd }
    368   1.1       cgd 
    369   1.1       cgd /*
    370   1.1       cgd  * Interrupt service routine during prompting
    371   1.1       cgd  */
    372   1.1       cgd void
    373  1.15     lukem intprompt(dummy)
    374  1.15     lukem 	int dummy;
    375   1.1       cgd {
    376   1.1       cgd 
    377   1.1       cgd 	(void)signal(SIGINT, SIG_IGN);
    378   1.1       cgd 	stoprompt = 1;
    379   1.1       cgd 	printf("\r\n");
    380   1.1       cgd 	longjmp(promptbuf, 1);
    381   1.1       cgd }
    382   1.1       cgd 
    383   1.1       cgd /*
    384   1.1       cgd  * ****TIPIN   TIPIN****
    385   1.1       cgd  */
    386  1.15     lukem void
    387   1.1       cgd tipin()
    388   1.1       cgd {
    389   1.1       cgd 	char gch, bol = 1;
    390   1.1       cgd 
    391   1.1       cgd 	/*
    392   1.1       cgd 	 * Kinda klugey here...
    393   1.1       cgd 	 *   check for scripting being turned on from the .tiprc file,
    394   1.1       cgd 	 *   but be careful about just using setscript(), as we may
    395   1.1       cgd 	 *   send a SIGEMT before tipout has a chance to set up catching
    396   1.1       cgd 	 *   it; so wait a second, then setscript()
    397   1.1       cgd 	 */
    398   1.1       cgd 	if (boolean(value(SCRIPT))) {
    399   1.1       cgd 		sleep(1);
    400   1.1       cgd 		setscript();
    401   1.1       cgd 	}
    402   1.1       cgd 
    403   1.1       cgd 	while (1) {
    404   1.8        pk 		gch = getchar()&STRIP_PAR;
    405   1.1       cgd 		if ((gch == character(value(ESCAPE))) && bol) {
    406   1.1       cgd 			if (!(gch = escape()))
    407   1.1       cgd 				continue;
    408  1.16     lukem 		} else if (!cumode &&
    409  1.16     lukem 		    gch && gch == character(value(RAISECHAR))) {
    410   1.9       cgd 			setboolean(value(RAISE), !boolean(value(RAISE)));
    411   1.1       cgd 			continue;
    412   1.1       cgd 		} else if (gch == '\r') {
    413   1.1       cgd 			bol = 1;
    414  1.20   thorpej 			xpwrite(FD, &gch, 1);
    415   1.1       cgd 			if (boolean(value(HALFDUPLEX)))
    416   1.1       cgd 				printf("\r\n");
    417   1.1       cgd 			continue;
    418  1.16     lukem 		} else if (!cumode && gch && gch == character(value(FORCE)))
    419   1.8        pk 			gch = getchar()&STRIP_PAR;
    420   1.1       cgd 		bol = any(gch, value(EOL));
    421  1.22  christos 		if (boolean(value(RAISE)) && islower((unsigned char)gch))
    422   1.1       cgd 			gch = toupper(gch);
    423  1.20   thorpej 		xpwrite(FD, &gch, 1);
    424   1.1       cgd 		if (boolean(value(HALFDUPLEX)))
    425   1.1       cgd 			printf("%c", gch);
    426   1.1       cgd 	}
    427   1.1       cgd }
    428   1.1       cgd 
    429   1.1       cgd /*
    430   1.1       cgd  * Escape handler --
    431   1.1       cgd  *  called on recognition of ``escapec'' at the beginning of a line
    432   1.1       cgd  */
    433  1.15     lukem int
    434   1.1       cgd escape()
    435   1.1       cgd {
    436  1.15     lukem 	char gch;
    437  1.15     lukem 	esctable_t *p;
    438   1.1       cgd 	char c = character(value(ESCAPE));
    439   1.1       cgd 
    440   1.8        pk 	gch = (getchar()&STRIP_PAR);
    441   1.1       cgd 	for (p = etable; p->e_char; p++)
    442   1.1       cgd 		if (p->e_char == gch) {
    443   1.1       cgd 			if ((p->e_flags&PRIV) && uid)
    444   1.1       cgd 				continue;
    445   1.1       cgd 			printf("%s", ctrl(c));
    446   1.1       cgd 			(*p->e_func)(gch);
    447   1.1       cgd 			return (0);
    448   1.1       cgd 		}
    449   1.1       cgd 	/* ESCAPE ESCAPE forces ESCAPE */
    450   1.1       cgd 	if (c != gch)
    451  1.20   thorpej 		xpwrite(FD, &c, 1);
    452   1.1       cgd 	return (gch);
    453   1.1       cgd }
    454   1.1       cgd 
    455  1.15     lukem int
    456   1.1       cgd speed(n)
    457   1.1       cgd 	int n;
    458   1.1       cgd {
    459  1.15     lukem 	int *p;
    460   1.1       cgd 
    461   1.8        pk 	for (p = rates; *p != -1;  p++)
    462   1.1       cgd 		if (*p == n)
    463   1.8        pk 			return n;
    464   1.8        pk 	return 0;
    465   1.1       cgd }
    466   1.1       cgd 
    467  1.15     lukem int
    468   1.1       cgd any(c, p)
    469  1.15     lukem 	char c, *p;
    470   1.1       cgd {
    471  1.21       mrg 
    472   1.1       cgd 	while (p && *p)
    473   1.1       cgd 		if (*p++ == c)
    474   1.1       cgd 			return (1);
    475   1.1       cgd 	return (0);
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.1       cgd char *
    479   1.1       cgd interp(s)
    480  1.15     lukem 	char *s;
    481   1.1       cgd {
    482   1.1       cgd 	static char buf[256];
    483  1.15     lukem 	char *p = buf, c, *q;
    484   1.1       cgd 
    485  1.21       mrg 	while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
    486   1.1       cgd 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
    487   1.1       cgd 			if (*q++ == c) {
    488   1.1       cgd 				*p++ = '\\'; *p++ = *q;
    489   1.1       cgd 				goto next;
    490   1.1       cgd 			}
    491   1.1       cgd 		if (c < 040) {
    492   1.1       cgd 			*p++ = '^'; *p++ = c + 'A'-1;
    493   1.1       cgd 		} else if (c == 0177) {
    494   1.1       cgd 			*p++ = '^'; *p++ = '?';
    495   1.1       cgd 		} else
    496   1.1       cgd 			*p++ = c;
    497   1.1       cgd 	next:
    498   1.1       cgd 		;
    499   1.1       cgd 	}
    500   1.1       cgd 	*p = '\0';
    501   1.1       cgd 	return (buf);
    502   1.1       cgd }
    503   1.1       cgd 
    504   1.1       cgd char *
    505   1.1       cgd ctrl(c)
    506   1.1       cgd 	char c;
    507   1.1       cgd {
    508   1.1       cgd 	static char s[3];
    509   1.1       cgd 
    510   1.1       cgd 	if (c < 040 || c == 0177) {
    511   1.1       cgd 		s[0] = '^';
    512   1.1       cgd 		s[1] = c == 0177 ? '?' : c+'A'-1;
    513   1.1       cgd 		s[2] = '\0';
    514   1.1       cgd 	} else {
    515   1.1       cgd 		s[0] = c;
    516   1.1       cgd 		s[1] = '\0';
    517   1.1       cgd 	}
    518   1.1       cgd 	return (s);
    519   1.1       cgd }
    520   1.1       cgd 
    521   1.1       cgd /*
    522   1.1       cgd  * Help command
    523   1.1       cgd  */
    524  1.15     lukem void
    525   1.1       cgd help(c)
    526   1.1       cgd 	char c;
    527   1.1       cgd {
    528  1.15     lukem 	esctable_t *p;
    529   1.1       cgd 
    530   1.1       cgd 	printf("%c\r\n", c);
    531   1.1       cgd 	for (p = etable; p->e_char; p++) {
    532   1.1       cgd 		if ((p->e_flags&PRIV) && uid)
    533   1.1       cgd 			continue;
    534   1.1       cgd 		printf("%2s", ctrl(character(value(ESCAPE))));
    535   1.1       cgd 		printf("%-2s %c   %s\r\n", ctrl(p->e_char),
    536   1.1       cgd 			p->e_flags&EXP ? '*': ' ', p->e_help);
    537   1.1       cgd 	}
    538   1.1       cgd }
    539   1.1       cgd 
    540   1.1       cgd /*
    541   1.1       cgd  * Set up the "remote" tty's state
    542   1.1       cgd  */
    543  1.15     lukem void
    544   1.1       cgd ttysetup(speed)
    545   1.1       cgd 	int speed;
    546   1.1       cgd {
    547   1.8        pk 	struct termios	cntrl;
    548   1.1       cgd 
    549   1.8        pk 	tcgetattr(FD, &cntrl);
    550   1.8        pk 	cfsetospeed(&cntrl, speed);
    551   1.8        pk 	cfsetispeed(&cntrl, speed);
    552   1.8        pk 	cntrl.c_cflag &= ~(CSIZE|PARENB);
    553   1.8        pk 	cntrl.c_cflag |= CS8;
    554  1.14    mellon 	if (DC)
    555  1.13    mellon 		cntrl.c_cflag |= CLOCAL;
    556   1.8        pk 	cntrl.c_iflag &= ~(ISTRIP|ICRNL);
    557   1.8        pk 	cntrl.c_oflag &= ~OPOST;
    558   1.8        pk 	cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
    559   1.8        pk 	cntrl.c_cc[VMIN] = 1;
    560   1.8        pk 	cntrl.c_cc[VTIME] = 0;
    561   1.1       cgd 	if (boolean(value(TAND)))
    562   1.8        pk 		cntrl.c_iflag |= IXOFF;
    563   1.8        pk 	tcsetattr(FD, TCSAFLUSH, &cntrl);
    564   1.1       cgd }
    565   1.1       cgd 
    566   1.1       cgd static char partab[0200];
    567   1.1       cgd 
    568   1.1       cgd /*
    569   1.1       cgd  * Do a write to the remote machine with the correct parity.
    570   1.1       cgd  * We are doing 8 bit wide output, so we just generate a character
    571   1.1       cgd  * with the right parity and output it.
    572   1.1       cgd  */
    573  1.15     lukem void
    574  1.20   thorpej xpwrite(fd, buf, n)
    575   1.1       cgd 	int fd;
    576   1.1       cgd 	char *buf;
    577  1.15     lukem 	int n;
    578   1.1       cgd {
    579  1.15     lukem 	int i;
    580  1.15     lukem 	char *bp;
    581   1.1       cgd 
    582   1.1       cgd 	bp = buf;
    583   1.6       jtc 	if (bits8 == 0)
    584   1.6       jtc 		for (i = 0; i < n; i++) {
    585   1.6       jtc 			*bp = partab[(*bp) & 0177];
    586   1.6       jtc 			bp++;
    587   1.1       cgd 		}
    588   1.6       jtc 	if (write(fd, buf, n) < 0) {
    589   1.1       cgd 		if (errno == EIO)
    590   1.1       cgd 			tipabort("Lost carrier.");
    591   1.1       cgd 		/* this is questionable */
    592   1.1       cgd 		perror("write");
    593   1.1       cgd 	}
    594   1.1       cgd }
    595   1.1       cgd 
    596   1.1       cgd /*
    597   1.1       cgd  * Build a parity table with appropriate high-order bit.
    598   1.1       cgd  */
    599  1.15     lukem void
    600   1.1       cgd setparity(defparity)
    601   1.1       cgd 	char *defparity;
    602   1.1       cgd {
    603  1.15     lukem 	int i, flip, clr, set;
    604   1.1       cgd 	char *parity;
    605   1.1       cgd 
    606  1.16     lukem 	if (value(PARITY) == NULL || (value(PARITY))[0] == '\0')
    607   1.1       cgd 		value(PARITY) = defparity;
    608   1.1       cgd 	parity = value(PARITY);
    609   1.1       cgd 	if (equal(parity, "none")) {
    610   1.1       cgd 		bits8 = 1;
    611   1.1       cgd 		return;
    612   1.1       cgd 	}
    613   1.1       cgd 	bits8 = 0;
    614   1.1       cgd 	flip = 0;
    615   1.1       cgd 	clr = 0377;
    616   1.1       cgd 	set = 0;
    617   1.1       cgd 	if (equal(parity, "odd"))
    618   1.1       cgd 		flip = 0200;			/* reverse bit 7 */
    619   1.1       cgd 	else if (equal(parity, "zero"))
    620   1.1       cgd 		clr = 0177;			/* turn off bit 7 */
    621   1.1       cgd 	else if (equal(parity, "one"))
    622   1.1       cgd 		set = 0200;			/* turn on bit 7 */
    623   1.1       cgd 	else if (!equal(parity, "even")) {
    624   1.1       cgd 		(void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
    625   1.1       cgd 		(void) fflush(stderr);
    626   1.1       cgd 	}
    627   1.1       cgd 	for (i = 0; i < 0200; i++)
    628  1.15     lukem 		partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
    629   1.1       cgd }
    630