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