xref: /illumos-gate/usr/src/cmd/tip/tip.c (revision d9c3e05c)
17c478bd9Sstevel@tonic-gate /*
2de81e71eSTim Marsland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
58d489c7aSmuffin 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate /*
137c478bd9Sstevel@tonic-gate  * tip - UNIX link to other systems
147c478bd9Sstevel@tonic-gate  *  tip [-v] [-speed] system-name
157c478bd9Sstevel@tonic-gate  * or
167c478bd9Sstevel@tonic-gate  *  cu phone-number [-s speed] [-l line] [-a acu]
177c478bd9Sstevel@tonic-gate  */
187c478bd9Sstevel@tonic-gate #include "tip.h"
197c478bd9Sstevel@tonic-gate #include <sys/wait.h>
207c478bd9Sstevel@tonic-gate #include <locale.h>
217c478bd9Sstevel@tonic-gate 
221e094e1bSToomas Soome int	vflag;
231e094e1bSToomas Soome int	cumode;
241e094e1bSToomas Soome FILE	*phfd;
251e094e1bSToomas Soome 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Baud rate mapping table
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate int bauds[] = {
307c478bd9Sstevel@tonic-gate 	0, 50, 75, 110, 134, 150, 200, 300, 600,
317c478bd9Sstevel@tonic-gate 	1200, 1800, 2400, 4800, 9600, 19200, 38400,
32de81e71eSTim Marsland 	57600, 76800, 115200, 153600, 230400, 307200,
33*d9c3e05cSJoshua M. Clulow 	460800, 921600, 1000000, 1152000, 1500000,
34*d9c3e05cSJoshua M. Clulow 	2000000, 2500000, 3000000, 3500000, 4000000, -1
357c478bd9Sstevel@tonic-gate };
367c478bd9Sstevel@tonic-gate 
378d489c7aSmuffin extern void	tipout(void)	__NORETURN;
388d489c7aSmuffin extern void	timeout(void);
398d489c7aSmuffin extern esctable_t	etable[];
408d489c7aSmuffin extern unsigned char	evenpartab[];
418d489c7aSmuffin 
428d489c7aSmuffin void	intprompt(void);
438d489c7aSmuffin void	deadkid(void);
448d489c7aSmuffin void	cleanup(void);
458d489c7aSmuffin void	tipin(void)	__NORETURN;
468d489c7aSmuffin unsigned char	escape(void);
478d489c7aSmuffin char	*sname(char *);
488d489c7aSmuffin char	PNbuf[256];		/* This limits the size of a number */
497c478bd9Sstevel@tonic-gate int	noparity = 0;
507c478bd9Sstevel@tonic-gate 
518d489c7aSmuffin int
main(int argc,char * argv[])528d489c7aSmuffin main(int argc, char *argv[])
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	char *system = NOSTR;
558d489c7aSmuffin 	int i;
568d489c7aSmuffin 	char *p;
5794e1761eSsn 	char sbuf[15];
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	gid = getgid();
607c478bd9Sstevel@tonic-gate 	egid = getegid();
617c478bd9Sstevel@tonic-gate 	uid = getuid();
627c478bd9Sstevel@tonic-gate 	euid = geteuid();
637c478bd9Sstevel@tonic-gate 	if (equal(sname(argv[0]), "cu")) {
647c478bd9Sstevel@tonic-gate 		cumode = 1;
657c478bd9Sstevel@tonic-gate 		cumain(argc, argv);
667c478bd9Sstevel@tonic-gate 		goto cucommon;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (argc > 4) {
708d489c7aSmuffin 		(void) fprintf(stderr,
718d489c7aSmuffin 		    "usage: tip [-v] [-speed] [system-name]\n");
728d489c7aSmuffin 		return (1);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if (!isatty(0)) {
758d489c7aSmuffin 		(void) fprintf(stderr, "tip: must be interactive\n");
768d489c7aSmuffin 		return (1);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	for (; argc > 1; argv++, argc--) {
807c478bd9Sstevel@tonic-gate 		if (argv[1][0] != '-')
817c478bd9Sstevel@tonic-gate 			system = argv[1];
827c478bd9Sstevel@tonic-gate 		else switch (argv[1][1]) {
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		case 'v':
857c478bd9Sstevel@tonic-gate 			vflag++;
867c478bd9Sstevel@tonic-gate 			break;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
897c478bd9Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
907c478bd9Sstevel@tonic-gate 			BR = atoi(&argv[1][1]);
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		default:
948d489c7aSmuffin 			(void) fprintf(stderr, "tip: %s, unknown option\n",
958d489c7aSmuffin 			    argv[1]);
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_CTYPE, "");
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (system == NOSTR)
1037c478bd9Sstevel@tonic-gate 		goto notnumber;
1047c478bd9Sstevel@tonic-gate 	for (p = system; *p; p++)
1057c478bd9Sstevel@tonic-gate 		if (isalpha(*p))
1067c478bd9Sstevel@tonic-gate 			goto notnumber;
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * System name is really a phone number...
1097c478bd9Sstevel@tonic-gate 	 * Copy the number then stomp on the original (in case the number
1107c478bd9Sstevel@tonic-gate 	 *	is private, we don't want 'ps' or 'w' to find it).
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	if (strlen(system) > sizeof (PNbuf) - 1) {
1138d489c7aSmuffin 		(void) fprintf(stderr,
1148d489c7aSmuffin 		    "tip: phone number too long (max = %d bytes)\n",
1157c478bd9Sstevel@tonic-gate 		    sizeof (PNbuf) - 1);
1168d489c7aSmuffin 		return (1);
1177c478bd9Sstevel@tonic-gate 	}
1188d489c7aSmuffin 	(void) strncpy(PNbuf, system, sizeof (PNbuf) - 1);
1197c478bd9Sstevel@tonic-gate 	for (p = system; *p; p++)
1207c478bd9Sstevel@tonic-gate 		*p = '\0';
1217c478bd9Sstevel@tonic-gate 	PN = PNbuf;
12294e1761eSsn 	(void) snprintf(sbuf, sizeof (sbuf), "tip%d", BR);
1237c478bd9Sstevel@tonic-gate 	system = sbuf;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate notnumber:
1268d489c7aSmuffin 	(void) signal(SIGINT, (sig_handler_t)cleanup);
1278d489c7aSmuffin 	(void) signal(SIGQUIT, (sig_handler_t)cleanup);
1288d489c7aSmuffin 	(void) signal(SIGHUP, (sig_handler_t)cleanup);
1298d489c7aSmuffin 	(void) signal(SIGTERM, (sig_handler_t)cleanup);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((i = hunt(system)) == 0) {
1328d489c7aSmuffin 		(void) printf("all ports busy\n");
1338d489c7aSmuffin 		return (3);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (i == -1) {
1368d489c7aSmuffin 		(void) printf("link down\n");
1377c478bd9Sstevel@tonic-gate 		delock(uucplock);
1388d489c7aSmuffin 		return (3);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
1417c478bd9Sstevel@tonic-gate 	loginit();
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * Now that we have the logfile and the ACU open
1457c478bd9Sstevel@tonic-gate 	 *  return to the real uid and gid.  These things will
1467c478bd9Sstevel@tonic-gate 	 *  be closed on exit.  The saved-setuid uid and gid
1477c478bd9Sstevel@tonic-gate 	 *  allows us to get the original setuid permissions back
1487c478bd9Sstevel@tonic-gate 	 *  for removing the uucp lock.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	userperm();
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * Kludge, there's no easy way to get the initialization
1547c478bd9Sstevel@tonic-gate 	 *   in the right order, so force it here.
1557c478bd9Sstevel@tonic-gate 	 * Do the open here, before we change back to real uid.
1567c478bd9Sstevel@tonic-gate 	 * We will check whether the open succeeded later, when
1577c478bd9Sstevel@tonic-gate 	 * (and if) we actually go to use the file.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if ((PH = getenv("PHONES")) == NOSTR) {
1607c478bd9Sstevel@tonic-gate 		myperm();
1617c478bd9Sstevel@tonic-gate 		PH = "/etc/phones";
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	phfd = fopen(PH, "r");
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	userperm();
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	vinit();				/* init variables */
1687c478bd9Sstevel@tonic-gate 	setparity("none");			/* set the parity table */
16982aa8bddSToomas Soome 	if ((i = speed(number(value(BAUDRATE)))) == 0) {
1708d489c7aSmuffin 		(void) printf("tip: bad baud rate %d\n",
1718d489c7aSmuffin 		    number(value(BAUDRATE)));
1727c478bd9Sstevel@tonic-gate 		myperm();
1737c478bd9Sstevel@tonic-gate 		delock(uucplock);
1748d489c7aSmuffin 		return (3);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Hardwired connections require the
1807c478bd9Sstevel@tonic-gate 	 *  line speed set before they make any transmissions
1817c478bd9Sstevel@tonic-gate 	 *  (this is particularly true of things like a DF03-AC)
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	if (HW)
1847c478bd9Sstevel@tonic-gate 		ttysetup(i);
1857c478bd9Sstevel@tonic-gate 	if (p = connect()) {
1868d489c7aSmuffin 		(void) printf("\07%s\n[EOT]\n", p);
1877c478bd9Sstevel@tonic-gate 		myperm();
1887c478bd9Sstevel@tonic-gate 		delock(uucplock);
1898d489c7aSmuffin 		return (1);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * Always setup the tty again here in case hardware flow
1947c478bd9Sstevel@tonic-gate 	 *  control was selected, which can only be set after the
1957c478bd9Sstevel@tonic-gate 	 *  connection is made, or in case this is not a hardwired
1967c478bd9Sstevel@tonic-gate 	 *  modem (rare these days) that likewise can only be setup
1977c478bd9Sstevel@tonic-gate 	 *  after the connection is made.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	ttysetup(i);
2007c478bd9Sstevel@tonic-gate cucommon:
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * From here down the code is shared with
2037c478bd9Sstevel@tonic-gate 	 * the "cu" version of tip.
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 
2068d489c7aSmuffin 	(void) ioctl(0, TCGETS, (char *)&defarg);
2077c478bd9Sstevel@tonic-gate 	arg = defarg;
2087c478bd9Sstevel@tonic-gate 	/* turn off input processing */
2097c478bd9Sstevel@tonic-gate 	arg.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
2107c478bd9Sstevel@tonic-gate 	arg.c_cc[VMIN] = 1;
2117c478bd9Sstevel@tonic-gate 	arg.c_cc[VTIME] = 0;
2127c478bd9Sstevel@tonic-gate 	arg.c_iflag &= ~(INPCK|IXON|IXOFF|ICRNL);
2137c478bd9Sstevel@tonic-gate 	arg.c_oflag = 0;		/* turn off all output processing */
2147c478bd9Sstevel@tonic-gate 	/* handle tandem mode in case was set in remote file */
2157c478bd9Sstevel@tonic-gate 	if (boolean(value(TAND)))
2167c478bd9Sstevel@tonic-gate 		tandem("on");
2177c478bd9Sstevel@tonic-gate 	else
2187c478bd9Sstevel@tonic-gate 		tandem("off");
2197c478bd9Sstevel@tonic-gate 	raw();
2207c478bd9Sstevel@tonic-gate 
2218d489c7aSmuffin 	(void) pipe(fildes); (void) pipe(repdes);
2228d489c7aSmuffin 	(void) signal(SIGALRM, (sig_handler_t)timeout);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Everything's set up now:
2267c478bd9Sstevel@tonic-gate 	 *	connection established (hardwired or dialup)
2277c478bd9Sstevel@tonic-gate 	 *	line conditioned (baud rate, mode, etc.)
2287c478bd9Sstevel@tonic-gate 	 *	internal data structures (variables)
2297c478bd9Sstevel@tonic-gate 	 * so, fork one process for local side and one for remote.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (CM != NOSTR) {
2328d489c7aSmuffin 		(void) sleep(2);	/* let line settle */
2338d489c7aSmuffin 		parwrite(FD, (unsigned char *)CM, strlen(CM));
2347c478bd9Sstevel@tonic-gate 	}
2358d489c7aSmuffin 	(void) printf(cumode ? "Connected\r\n" : "\07connected\r\n");
2368d489c7aSmuffin 	(void) signal(SIGCHLD, (sig_handler_t)deadkid);
2377c478bd9Sstevel@tonic-gate 	if (pid = fork())
2387c478bd9Sstevel@tonic-gate 		tipin();
2397c478bd9Sstevel@tonic-gate 	else
2407c478bd9Sstevel@tonic-gate 		tipout();
2417c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate void
deadkid(void)2458d489c7aSmuffin deadkid(void)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (pid >= 0 && waitpid(pid, NULL, WNOHANG) == pid)
2498d489c7aSmuffin 		tip_abort("Connection Closed");
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate void
cleanup(void)2538d489c7aSmuffin cleanup(void)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (uid != getuid()) {
2577c478bd9Sstevel@tonic-gate 		myperm();
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	delock(uucplock);
2607c478bd9Sstevel@tonic-gate 	exit(0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * put the controlling keyboard into raw mode
2657c478bd9Sstevel@tonic-gate  */
2668d489c7aSmuffin void
raw(void)2678d489c7aSmuffin raw(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 
2708d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * return keyboard to normal mode
2767c478bd9Sstevel@tonic-gate  */
2778d489c7aSmuffin void
unraw(void)2788d489c7aSmuffin unraw(void)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 
2818d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&defarg);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * switch to using invoking user's permissions
2867c478bd9Sstevel@tonic-gate  */
2878d489c7aSmuffin void
userperm(void)2888d489c7aSmuffin userperm(void)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 
2918d489c7aSmuffin 	(void) setegid(gid);
2928d489c7aSmuffin 	(void) seteuid(uid);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * switch to using my special (setuid) permissions
2977c478bd9Sstevel@tonic-gate  */
2988d489c7aSmuffin void
myperm(void)2998d489c7aSmuffin myperm(void)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 
3028d489c7aSmuffin 	(void) setegid(egid);
3038d489c7aSmuffin 	(void) seteuid(euid);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static	sigjmp_buf promptbuf;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Print string ``s'', then read a string
3107c478bd9Sstevel@tonic-gate  *  in from the terminal.  Handles signals & allows use of
3117c478bd9Sstevel@tonic-gate  *  normal erase and kill characters.
3127c478bd9Sstevel@tonic-gate  */
3138d489c7aSmuffin int
prompt(char * s,char * p,size_t len)3148d489c7aSmuffin prompt(char *s, char *p, size_t len)
3157c478bd9Sstevel@tonic-gate {
3168d489c7aSmuffin 	char *b = p;
3178d489c7aSmuffin 	int c;
3188d489c7aSmuffin 	sig_handler_t	ointr, oquit;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	stoprompt = 0;
3218d489c7aSmuffin 	ointr = signal(SIGINT, (sig_handler_t)intprompt);
3227c478bd9Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
3237c478bd9Sstevel@tonic-gate 	unraw();
3248d489c7aSmuffin 	(void) printf("%s", s);
3257c478bd9Sstevel@tonic-gate 	if (sigsetjmp(promptbuf, 1) == 0)
3267c478bd9Sstevel@tonic-gate 		while (p < b + len - 1 &&
3277c478bd9Sstevel@tonic-gate 		    ((c = getchar()) != EOF) && (c != '\n'))
3287c478bd9Sstevel@tonic-gate 			*p++ = c;
3297c478bd9Sstevel@tonic-gate 	*p = '\0';
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	raw();
3328d489c7aSmuffin 	(void) signal(SIGINT, ointr);
3338d489c7aSmuffin 	(void) signal(SIGQUIT, oquit);
3347c478bd9Sstevel@tonic-gate 	return (stoprompt || p == b);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Interrupt service routine during prompting
3397c478bd9Sstevel@tonic-gate  */
3407c478bd9Sstevel@tonic-gate void
intprompt(void)3418d489c7aSmuffin intprompt(void)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 
3448d489c7aSmuffin 	(void) signal(SIGINT, SIG_IGN);
3458d489c7aSmuffin 	(void) signal(SIGQUIT, SIG_IGN);
3467c478bd9Sstevel@tonic-gate 	stoprompt = 1;
3478d489c7aSmuffin 	(void) printf("\r\n");
3487c478bd9Sstevel@tonic-gate 	siglongjmp(promptbuf, 1);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * ****TIPIN   TIPIN****
3537c478bd9Sstevel@tonic-gate  */
3548d489c7aSmuffin void
tipin(void)3558d489c7aSmuffin tipin(void)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	unsigned char gch, c;
3587c478bd9Sstevel@tonic-gate 	int bol = 1;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * Kinda klugey here...
3627c478bd9Sstevel@tonic-gate 	 *   check for scripting being turned on from the .tiprc file,
3637c478bd9Sstevel@tonic-gate 	 *   but be careful about just using setscript(), as we may
3647c478bd9Sstevel@tonic-gate 	 *   send a SIGEMT before tipout has a chance to set up catching
3657c478bd9Sstevel@tonic-gate 	 *   it; so wait a second, then setscript()
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (boolean(value(SCRIPT))) {
3688d489c7aSmuffin 		(void) sleep(1);
3697c478bd9Sstevel@tonic-gate 		setscript();
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	for (;;) {
3737c478bd9Sstevel@tonic-gate 		gch = getchar()&0377;
3747c478bd9Sstevel@tonic-gate 		if ((gch == character(value(ESCAPE))) && bol) {
3757c478bd9Sstevel@tonic-gate 			if (!(gch = escape()))
3767c478bd9Sstevel@tonic-gate 				continue;
3777c478bd9Sstevel@tonic-gate 		} else if (!cumode && gch == character(value(RAISECHAR))) {
3787c478bd9Sstevel@tonic-gate 			boolean(value(RAISE)) = !boolean(value(RAISE));
3797c478bd9Sstevel@tonic-gate 			continue;
3807c478bd9Sstevel@tonic-gate 		} else if (gch == '\r') {
3817c478bd9Sstevel@tonic-gate 			bol = 1;
3827c478bd9Sstevel@tonic-gate 			parwrite(FD, &gch, 1);
3837c478bd9Sstevel@tonic-gate 			if (boolean(value(HALFDUPLEX)))
3848d489c7aSmuffin 				(void) printf("\r\n");
3857c478bd9Sstevel@tonic-gate 			continue;
3867c478bd9Sstevel@tonic-gate 		} else if (!cumode && gch == character(value(FORCE)))
3877c478bd9Sstevel@tonic-gate 			gch = getchar()&0377;
3887c478bd9Sstevel@tonic-gate 		bol = any(gch, value(EOL));
3897c478bd9Sstevel@tonic-gate 		if (boolean(value(RAISE)) && islower(gch))
3907c478bd9Sstevel@tonic-gate 			gch = toupper(gch);
3917c478bd9Sstevel@tonic-gate 		c = gch;
3927c478bd9Sstevel@tonic-gate 		parwrite(FD, &gch, 1);
3937c478bd9Sstevel@tonic-gate 		if (boolean(value(HALFDUPLEX)))
3948d489c7aSmuffin 			(void) putchar(c);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * Escape handler --
4007c478bd9Sstevel@tonic-gate  *  called on recognition of ``escapec'' at the beginning of a line
4017c478bd9Sstevel@tonic-gate  */
4028d489c7aSmuffin unsigned char
escape(void)4038d489c7aSmuffin escape(void)
4047c478bd9Sstevel@tonic-gate {
4058d489c7aSmuffin 	unsigned char gch;
4068d489c7aSmuffin 	esctable_t *p;
4077c478bd9Sstevel@tonic-gate 	char c = character(value(ESCAPE));
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	gch = (getchar()&0377);
4107c478bd9Sstevel@tonic-gate 	for (p = etable; p->e_char; p++)
4117c478bd9Sstevel@tonic-gate 		if (p->e_char == gch) {
4127c478bd9Sstevel@tonic-gate 			if ((p->e_flags&PRIV) && uid)
4137c478bd9Sstevel@tonic-gate 				continue;
4148d489c7aSmuffin 			(void) printf("%s", ctrl(c));
4157c478bd9Sstevel@tonic-gate 			(*p->e_func)(gch);
4167c478bd9Sstevel@tonic-gate 			return (0);
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	/* ESCAPE ESCAPE forces ESCAPE */
4197c478bd9Sstevel@tonic-gate 	if (c != gch)
4208d489c7aSmuffin 		parwrite(FD, (unsigned char *)&c, 1);
4217c478bd9Sstevel@tonic-gate 	return (gch);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4248d489c7aSmuffin int
speed(int n)4258d489c7aSmuffin speed(int n)
4267c478bd9Sstevel@tonic-gate {
4278d489c7aSmuffin 	int *p;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	for (p = bauds; *p != -1;  p++)
4307c478bd9Sstevel@tonic-gate 		if (*p == n)
4317c478bd9Sstevel@tonic-gate 			return (p - bauds);
4328d489c7aSmuffin 	return (0);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4358d489c7aSmuffin int
any(char c,char * p)4368d489c7aSmuffin any(char c, char *p)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	while (p && *p)
4397c478bd9Sstevel@tonic-gate 		if (*p++ == c)
4407c478bd9Sstevel@tonic-gate 			return (1);
4417c478bd9Sstevel@tonic-gate 	return (0);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate char *
interp(char * s)4458d489c7aSmuffin interp(char *s)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	static char buf[256];
4488d489c7aSmuffin 	char *p = buf, c, *q;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	while (c = *s++) {
4517c478bd9Sstevel@tonic-gate 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
4527c478bd9Sstevel@tonic-gate 			if (*q++ == c) {
4537c478bd9Sstevel@tonic-gate 				*p++ = '\\'; *p++ = *q;
4547c478bd9Sstevel@tonic-gate 				goto next;
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 		if (c < 040) {
4577c478bd9Sstevel@tonic-gate 			*p++ = '^'; *p++ = c + 'A'-1;
4587c478bd9Sstevel@tonic-gate 		} else if (c == 0177) {
4597c478bd9Sstevel@tonic-gate 			*p++ = '^'; *p++ = '?';
4607c478bd9Sstevel@tonic-gate 		} else
4617c478bd9Sstevel@tonic-gate 			*p++ = c;
4627c478bd9Sstevel@tonic-gate 	next:
4637c478bd9Sstevel@tonic-gate 		;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	*p = '\0';
4667c478bd9Sstevel@tonic-gate 	return (buf);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate char *
ctrl(char c)4708d489c7aSmuffin ctrl(char c)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	static char s[3];
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (c < 040 || c == 0177) {
4757c478bd9Sstevel@tonic-gate 		s[0] = '^';
4767c478bd9Sstevel@tonic-gate 		s[1] = c == 0177 ? '?' : c+'A'-1;
4777c478bd9Sstevel@tonic-gate 		s[2] = '\0';
4787c478bd9Sstevel@tonic-gate 	} else {
4797c478bd9Sstevel@tonic-gate 		s[0] = c;
4807c478bd9Sstevel@tonic-gate 		s[1] = '\0';
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	return (s);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  * Help command
4877c478bd9Sstevel@tonic-gate  */
4888d489c7aSmuffin void
help(int c)4898d489c7aSmuffin help(int c)
4907c478bd9Sstevel@tonic-gate {
4918d489c7aSmuffin 	esctable_t *p;
4927c478bd9Sstevel@tonic-gate 
4938d489c7aSmuffin 	(void) printf("%c\r\n", c);
4947c478bd9Sstevel@tonic-gate 	for (p = etable; p->e_char; p++) {
4957c478bd9Sstevel@tonic-gate 		if ((p->e_flags&PRIV) && uid)
4967c478bd9Sstevel@tonic-gate 			continue;
4978d489c7aSmuffin 		(void) printf("%2s", ctrl(character(value(ESCAPE))));
4988d489c7aSmuffin 		(void) printf("%-2s %c   %s\r\n", ctrl(p->e_char),
4998d489c7aSmuffin 		    p->e_flags&EXP ? '*': ' ', p->e_help);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Set up the "remote" tty's state
5057c478bd9Sstevel@tonic-gate  */
5068d489c7aSmuffin void
ttysetup(int speed)5078d489c7aSmuffin ttysetup(int speed)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct termios buf;
5107c478bd9Sstevel@tonic-gate 	char *loc;
5117c478bd9Sstevel@tonic-gate 
5128d489c7aSmuffin 	(void) ioctl(FD, TCGETS, (char *)&buf);
5137c478bd9Sstevel@tonic-gate 	buf.c_cflag &= (CREAD|HUPCL|CLOCAL|CRTSCTS|CRTSXOFF);
5147c478bd9Sstevel@tonic-gate 	buf.c_cflag |= CS8;
5158d489c7aSmuffin 	(void) cfsetospeed(&buf, speed);
5167c478bd9Sstevel@tonic-gate 	if (boolean(value(HARDWAREFLOW))) {
5177c478bd9Sstevel@tonic-gate 		int i = TIOCM_CAR;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * Only set hardware flow control if carrier is up,
5217c478bd9Sstevel@tonic-gate 		 * because some devices require both CD and RTS to
5227c478bd9Sstevel@tonic-gate 		 * be up before sending.
5237c478bd9Sstevel@tonic-gate 		 */
5248d489c7aSmuffin 		(void) ioctl(FD, TIOCMGET, &i);
5257c478bd9Sstevel@tonic-gate 		if (i & TIOCM_CAR)
5267c478bd9Sstevel@tonic-gate 			buf.c_cflag |= (CRTSCTS|CRTSXOFF);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * Careful to only penalize the 8-bit users here on the
5317c478bd9Sstevel@tonic-gate 	 * incoming tty port.  The default 7-bit users will
5327c478bd9Sstevel@tonic-gate 	 * still get the parity bit from the other side's login
5337c478bd9Sstevel@tonic-gate 	 * process (which happens to be the default for sun tip
5347c478bd9Sstevel@tonic-gate 	 * configurations).
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	loc = setlocale(LC_CTYPE, NULL);
5377c478bd9Sstevel@tonic-gate 	if (noparity && loc != 0 && strcmp(loc, "C") != 0)
5387c478bd9Sstevel@tonic-gate 		buf.c_iflag = 0;
5397c478bd9Sstevel@tonic-gate 	else
5407c478bd9Sstevel@tonic-gate 		buf.c_iflag = ISTRIP;
5417c478bd9Sstevel@tonic-gate 	buf.c_oflag = 0;
5427c478bd9Sstevel@tonic-gate 	buf.c_lflag = 0;
5437c478bd9Sstevel@tonic-gate 	buf.c_cc[VMIN] = 1;
5447c478bd9Sstevel@tonic-gate 	buf.c_cc[VTIME] = 0;
5458d489c7aSmuffin 	(void) ioctl(FD, TCSETSF, (char *)&buf);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * Return "simple" name from a file name,
5507c478bd9Sstevel@tonic-gate  * strip leading directories.
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate char *
sname(char * s)5538d489c7aSmuffin sname(char *s)
5547c478bd9Sstevel@tonic-gate {
5558d489c7aSmuffin 	char *p = s;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	while (*s)
5587c478bd9Sstevel@tonic-gate 		if (*s++ == '/')
5597c478bd9Sstevel@tonic-gate 			p = s;
5607c478bd9Sstevel@tonic-gate 	return (p);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static char partab[0400];
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Do a write to the remote machine with the correct parity.
5677c478bd9Sstevel@tonic-gate  * We are doing 8 bit wide output, so we just generate a character
5687c478bd9Sstevel@tonic-gate  * with the right parity and output it.
5697c478bd9Sstevel@tonic-gate  */
5708d489c7aSmuffin void
parwrite(int fd,unsigned char * buf,int n)5718d489c7aSmuffin parwrite(int fd, unsigned char *buf, int n)
5727c478bd9Sstevel@tonic-gate {
5738d489c7aSmuffin 	int i;
5748d489c7aSmuffin 	unsigned char *bp;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	bp = buf;
5777c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
5787c478bd9Sstevel@tonic-gate 		*bp = partab[(*bp)&0377];
5797c478bd9Sstevel@tonic-gate 		bp++;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	if (write(fd, buf, n) < 0) {
5827c478bd9Sstevel@tonic-gate 		if (errno == EIO || errno == ENXIO)
5838d489c7aSmuffin 			tip_abort("Lost carrier.");
5847c478bd9Sstevel@tonic-gate 		/* this is questionable */
5857c478bd9Sstevel@tonic-gate 		perror("write");
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * Build a parity table with appropriate high-order bit.
5917c478bd9Sstevel@tonic-gate  */
5928d489c7aSmuffin void
setparity(char * defparity)5938d489c7aSmuffin setparity(char *defparity)
5947c478bd9Sstevel@tonic-gate {
5958d489c7aSmuffin 	int i;
5967c478bd9Sstevel@tonic-gate 	char *parity;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (value(PARITY) == NOSTR)
5997c478bd9Sstevel@tonic-gate 		value(PARITY) = defparity;
6007c478bd9Sstevel@tonic-gate 	parity = value(PARITY);
6017c478bd9Sstevel@tonic-gate 	for (i = 0; i < 0400; i++)
6027c478bd9Sstevel@tonic-gate 		partab[i] = evenpartab[i];
6038d489c7aSmuffin 	if (equal(parity, "even")) {
6048d489c7aSmuffin 		/* EMPTY */
6058d489c7aSmuffin 	} else if (equal(parity, "odd")) {
6067c478bd9Sstevel@tonic-gate 		for (i = 0; i < 0400; i++)
6077c478bd9Sstevel@tonic-gate 			partab[i] ^= 0200;	/* reverse bit 7 */
6087c478bd9Sstevel@tonic-gate 	} else if (equal(parity, "none")) {
6097c478bd9Sstevel@tonic-gate 		/* Do nothing so we can pass thru 8-bit chars */
6107c478bd9Sstevel@tonic-gate 		noparity = 1;
6117c478bd9Sstevel@tonic-gate 		for (i = 0; i < 0400; i++)
6127c478bd9Sstevel@tonic-gate 			partab[i] = i;
6137c478bd9Sstevel@tonic-gate 	} else if (equal(parity, "zero")) {
6147c478bd9Sstevel@tonic-gate 		for (i = 0; i < 0400; i++)
6157c478bd9Sstevel@tonic-gate 			partab[i] &= ~0200;	/* turn off bit 7 */
6167c478bd9Sstevel@tonic-gate 	} else if (equal(parity, "one")) {
6177c478bd9Sstevel@tonic-gate 		for (i = 0; i < 0400; i++)
6187c478bd9Sstevel@tonic-gate 			partab[i] |= 0200;	/* turn on bit 7 */
6197c478bd9Sstevel@tonic-gate 	} else {
6208d489c7aSmuffin 		(void) fprintf(stderr, "%s: unknown parity value\n", PA);
6218d489c7aSmuffin 		(void) fflush(stderr);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate }
624