xref: /csrg-svn/usr.bin/telnet/telnet.c (revision 13997)
111758Ssam #ifndef lint
2*13997Ssam static char sccsid[] = "@(#)telnet.c	4.23 (Berkeley) 07/19/83";
311758Ssam #endif
411758Ssam 
56000Sroot /*
66000Sroot  * User telnet program.
76000Sroot  */
89217Ssam #include <sys/types.h>
99217Ssam #include <sys/socket.h>
109972Ssam #include <sys/ioctl.h>
119217Ssam 
129217Ssam #include <netinet/in.h>
139217Ssam 
1412212Ssam #define	TELOPTS
1512212Ssam #include <arpa/telnet.h>
1612212Ssam 
176000Sroot #include <stdio.h>
186000Sroot #include <ctype.h>
196000Sroot #include <errno.h>
206000Sroot #include <signal.h>
216000Sroot #include <setjmp.h>
228345Ssam #include <netdb.h>
239217Ssam 
246000Sroot #define	strip(x)	((x)&0177)
256000Sroot 
266000Sroot char	ttyobuf[BUFSIZ], *tfrontp = ttyobuf, *tbackp = ttyobuf;
278378Ssam char	netobuf[BUFSIZ], *nfrontp = netobuf, *nbackp = netobuf;
286000Sroot 
296000Sroot char	hisopts[256];
306000Sroot char	myopts[256];
316000Sroot 
326000Sroot char	doopt[] = { IAC, DO, '%', 'c', 0 };
336000Sroot char	dont[] = { IAC, DONT, '%', 'c', 0 };
346000Sroot char	will[] = { IAC, WILL, '%', 'c', 0 };
356000Sroot char	wont[] = { IAC, WONT, '%', 'c', 0 };
366000Sroot 
376000Sroot int	connected;
386000Sroot int	net;
399972Ssam int	showoptions = 0;
407377Sfeldman int	options;
4110339Ssam int	debug = 0;
429972Ssam int	crmod = 0;
436000Sroot char	*prompt;
449972Ssam char	escape = CTRL(]);
456000Sroot 
466000Sroot char	line[200];
476000Sroot int	margc;
486000Sroot char	*margv[20];
496000Sroot 
506000Sroot jmp_buf	toplevel;
516000Sroot jmp_buf	peerdied;
526000Sroot 
536000Sroot extern	int errno;
546000Sroot 
556000Sroot int	tn(), quit(), suspend(), bye(), help();
566024Ssam int	setescape(), status(), toggle(), setoptions();
5710603Ssam int	setcrmod(), setdebug();
586000Sroot 
598378Ssam #define HELPINDENT (sizeof ("connect"))
606000Sroot 
616000Sroot struct cmd {
6211758Ssam 	char	*name;		/* command name */
6311758Ssam 	char	*help;		/* help string */
6411758Ssam 	int	(*handler)();	/* routine which executes command */
656000Sroot };
666000Sroot 
6711758Ssam char	openhelp[] =	"connect to a site";
6811758Ssam char	closehelp[] =	"close current connection";
6911758Ssam char	quithelp[] =	"exit telnet";
7011758Ssam char	zhelp[] =	"suspend telnet";
7111758Ssam char	debughelp[] =	"toggle debugging";
7211758Ssam char	escapehelp[] =	"set escape character";
7311758Ssam char	statushelp[] =	"print status information";
7411758Ssam char	helphelp[] =	"print help information";
7511758Ssam char	optionshelp[] =	"toggle viewing of options processing";
7611758Ssam char	crmodhelp[] =	"toggle mapping of received carriage returns";
776000Sroot 
786000Sroot struct cmd cmdtab[] = {
7911758Ssam 	{ "open",	openhelp,	tn },
8011758Ssam 	{ "close",	closehelp,	bye },
8111758Ssam 	{ "quit",	quithelp,	quit },
826000Sroot 	{ "z",		zhelp,		suspend },
8311758Ssam 	{ "escape",	escapehelp,	setescape },
8411758Ssam 	{ "status",	statushelp,	status },
8511758Ssam 	{ "options",	optionshelp,	setoptions },
8611758Ssam 	{ "crmod",	crmodhelp,	setcrmod },
8711758Ssam 	{ "debug",	debughelp,	setdebug },
8811758Ssam 	{ "?",		helphelp,	help },
896000Sroot 	0
906000Sroot };
916000Sroot 
929972Ssam struct sockaddr_in sin;
936000Sroot 
946000Sroot int	intr(), deadpeer();
956000Sroot char	*control();
966000Sroot struct	cmd *getcmd();
978345Ssam struct	servent *sp;
986000Sroot 
9913076Ssam struct	tchars otc;
10013076Ssam struct	ltchars oltc;
10113076Ssam struct	sgttyb ottyb;
1028378Ssam 
1036000Sroot main(argc, argv)
1046000Sroot 	int argc;
1056000Sroot 	char *argv[];
1066000Sroot {
1078345Ssam 	sp = getservbyname("telnet", "tcp");
1088345Ssam 	if (sp == 0) {
1098345Ssam 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
1108345Ssam 		exit(1);
1118345Ssam 	}
11213076Ssam 	ioctl(0, TIOCGETP, (char *)&ottyb);
11313076Ssam 	ioctl(0, TIOCGETC, (char *)&otc);
11413076Ssam 	ioctl(0, TIOCGLTC, (char *)&oltc);
1156000Sroot 	setbuf(stdin, 0);
1166000Sroot 	setbuf(stdout, 0);
1176000Sroot 	prompt = argv[0];
1187377Sfeldman 	if (argc > 1 && !strcmp(argv[1], "-d"))
1197377Sfeldman 		options = SO_DEBUG, argv++, argc--;
1206000Sroot 	if (argc != 1) {
1216000Sroot 		if (setjmp(toplevel) != 0)
1226000Sroot 			exit(0);
1236000Sroot 		tn(argc, argv);
1246000Sroot 	}
1256000Sroot 	setjmp(toplevel);
1266000Sroot 	for (;;)
1276000Sroot 		command(1);
1286000Sroot }
1296000Sroot 
1308377Ssam char	*hostname;
1318377Ssam char	hnamebuf[32];
1326000Sroot 
1336000Sroot tn(argc, argv)
1346000Sroot 	int argc;
1356000Sroot 	char *argv[];
1366000Sroot {
1376000Sroot 	register int c;
1388377Ssam 	register struct hostent *host;
1396000Sroot 
1406000Sroot 	if (connected) {
1418377Ssam 		printf("?Already connected to %s\n", hostname);
1426000Sroot 		return;
1436000Sroot 	}
1446000Sroot 	if (argc < 2) {
1456000Sroot 		strcpy(line, "Connect ");
1466000Sroot 		printf("(to) ");
1476000Sroot 		gets(&line[strlen(line)]);
1486000Sroot 		makeargv();
1496000Sroot 		argc = margc;
1506000Sroot 		argv = margv;
1516000Sroot 	}
1526000Sroot 	if (argc > 3) {
1536000Sroot 		printf("usage: %s host-name [port]\n", argv[0]);
1546000Sroot 		return;
1556000Sroot 	}
1568345Ssam 	host = gethostbyname(argv[1]);
1578377Ssam 	if (host) {
1589217Ssam 		sin.sin_family = host->h_addrtype;
1599217Ssam 		bcopy(host->h_addr, (caddr_t)&sin.sin_addr, host->h_length);
1608377Ssam 		hostname = host->h_name;
1618377Ssam 	} else {
1629217Ssam 		sin.sin_family = AF_INET;
1638377Ssam 		sin.sin_addr.s_addr = inet_addr(argv[1]);
1648377Ssam 		if (sin.sin_addr.s_addr == -1) {
1658377Ssam 			printf("%s: unknown host\n", argv[1]);
1668377Ssam 			return;
1678377Ssam 		}
1688377Ssam 		strcpy(hnamebuf, argv[1]);
1698377Ssam 		hostname = hnamebuf;
1706000Sroot 	}
1718345Ssam 	sin.sin_port = sp->s_port;
1726024Ssam 	if (argc == 3) {
1736024Ssam 		sin.sin_port = atoi(argv[2]);
1746024Ssam 		if (sin.sin_port < 0) {
1756024Ssam 			printf("%s: bad port number\n", argv[2]);
1766024Ssam 			return;
1776024Ssam 		}
1789968Ssam 		sin.sin_port = htons(sin.sin_port);
1796024Ssam 	}
1809287Ssam 	net = socket(AF_INET, SOCK_STREAM, 0, 0);
1819217Ssam 	if (net < 0) {
1829217Ssam 		perror("telnet: socket");
1836000Sroot 		return;
1846000Sroot 	}
18511758Ssam 	if (debug && setsockopt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
18611758Ssam 		perror("setsockopt (SO_DEBUG)");
18712989Ssam 	signal(SIGINT, intr);
18812989Ssam 	signal(SIGPIPE, deadpeer);
1896000Sroot 	printf("Trying...\n");
1909217Ssam 	if (connect(net, (caddr_t)&sin, sizeof (sin), 0) < 0) {
1919217Ssam 		perror("telnet: connect");
19212989Ssam 		signal(SIGINT, SIG_DFL);
1936000Sroot 		return;
1946000Sroot 	}
1956000Sroot 	connected++;
1966000Sroot 	call(status, "status", 0);
1976000Sroot 	if (setjmp(peerdied) == 0)
1986000Sroot 		telnet(net);
1996000Sroot 	fprintf(stderr, "Connection closed by foreign host.\n");
2006000Sroot 	exit(1);
2016000Sroot }
2026000Sroot 
2036000Sroot /*
2046000Sroot  * Print status about the connection.
2056000Sroot  */
2066000Sroot /*VARARGS*/
2076000Sroot status()
2086000Sroot {
2096000Sroot 	if (connected)
2108377Ssam 		printf("Connected to %s.\n", hostname);
2116000Sroot 	else
2126000Sroot 		printf("No connection.\n");
2136000Sroot 	printf("Escape character is '%s'.\n", control(escape));
2149972Ssam 	fflush(stdout);
2156000Sroot }
2166000Sroot 
2176000Sroot makeargv()
2186000Sroot {
2196000Sroot 	register char *cp;
2206000Sroot 	register char **argp = margv;
2216000Sroot 
2226000Sroot 	margc = 0;
2236000Sroot 	for (cp = line; *cp;) {
2246000Sroot 		while (isspace(*cp))
2256000Sroot 			cp++;
2266000Sroot 		if (*cp == '\0')
2276000Sroot 			break;
2286000Sroot 		*argp++ = cp;
2296000Sroot 		margc += 1;
2306000Sroot 		while (*cp != '\0' && !isspace(*cp))
2316000Sroot 			cp++;
2326000Sroot 		if (*cp == '\0')
2336000Sroot 			break;
2346000Sroot 		*cp++ = '\0';
2356000Sroot 	}
2366000Sroot 	*argp++ = 0;
2376000Sroot }
2386000Sroot 
2396000Sroot /*VARARGS*/
2406000Sroot suspend()
2416000Sroot {
2426000Sroot 	register int save;
2436000Sroot 
2446000Sroot 	save = mode(0);
2458378Ssam 	kill(0, SIGTSTP);
2468378Ssam 	/* reget parameters in case they were changed */
24713076Ssam 	ioctl(0, TIOCGETP, (char *)&ottyb);
24813076Ssam 	ioctl(0, TIOCGETC, (char *)&otc);
24913076Ssam 	ioctl(0, TIOCGLTC, (char *)&oltc);
2508378Ssam 	(void) mode(save);
2516000Sroot }
2526000Sroot 
2536000Sroot /*VARARGS*/
2546000Sroot bye()
2556000Sroot {
2568378Ssam 	register char *op;
2576000Sroot 
2588378Ssam 	(void) mode(0);
2596000Sroot 	if (connected) {
26011758Ssam 		shutdown(net, 2);
2616000Sroot 		printf("Connection closed.\n");
2626000Sroot 		close(net);
2636000Sroot 		connected = 0;
2648378Ssam 		/* reset his options */
2658378Ssam 		for (op = hisopts; op < &hisopts[256]; op++)
2668378Ssam 			*op = 0;
2676000Sroot 	}
2686000Sroot }
2696000Sroot 
2706000Sroot /*VARARGS*/
2716000Sroot quit()
2726000Sroot {
2736000Sroot 	call(bye, "bye", 0);
2746000Sroot 	exit(0);
2756000Sroot }
2766000Sroot 
2776000Sroot /*
2786000Sroot  * Help command.
2796000Sroot  */
2806000Sroot help(argc, argv)
2816000Sroot 	int argc;
2826000Sroot 	char *argv[];
2836000Sroot {
2846000Sroot 	register struct cmd *c;
2856000Sroot 
2866000Sroot 	if (argc == 1) {
2876000Sroot 		printf("Commands may be abbreviated.  Commands are:\n\n");
2886000Sroot 		for (c = cmdtab; c->name; c++)
2896000Sroot 			printf("%-*s\t%s\n", HELPINDENT, c->name, c->help);
2906000Sroot 		return;
2916000Sroot 	}
2926000Sroot 	while (--argc > 0) {
2936000Sroot 		register char *arg;
2946000Sroot 		arg = *++argv;
2956000Sroot 		c = getcmd(arg);
2966000Sroot 		if (c == (struct cmd *)-1)
2976000Sroot 			printf("?Ambiguous help command %s\n", arg);
2986000Sroot 		else if (c == (struct cmd *)0)
2996000Sroot 			printf("?Invalid help command %s\n", arg);
3006000Sroot 		else
3016000Sroot 			printf("%s\n", c->help);
3026000Sroot 	}
3036000Sroot }
3046000Sroot 
3056000Sroot /*
3066000Sroot  * Call routine with argc, argv set from args (terminated by 0).
3076000Sroot  * VARARGS2
3086000Sroot  */
3096000Sroot call(routine, args)
3106000Sroot 	int (*routine)();
3116000Sroot 	int args;
3126000Sroot {
3136000Sroot 	register int *argp;
3146000Sroot 	register int argc;
3156000Sroot 
3166000Sroot 	for (argc = 0, argp = &args; *argp++ != 0; argc++)
3176000Sroot 		;
3186000Sroot 	(*routine)(argc, &args);
3196000Sroot }
3206000Sroot 
32113076Ssam struct	tchars notc =	{ -1, -1, -1, -1, -1, -1 };
32213076Ssam struct	ltchars noltc =	{ -1, -1, -1, -1, -1, -1 };
3239972Ssam 
3246000Sroot mode(f)
3256000Sroot 	register int f;
3266000Sroot {
3278378Ssam 	static int prevmode = 0;
32813076Ssam 	struct tchars *tc;
32913076Ssam 	struct ltchars *ltc;
33013076Ssam 	struct sgttyb sb;
33113076Ssam 	int onoff, old;
3326000Sroot 
3338378Ssam 	if (prevmode == f)
3348378Ssam 		return (f);
3358378Ssam 	old = prevmode;
3368378Ssam 	prevmode = f;
33713076Ssam 	sb = ottyb;
3386000Sroot 	switch (f) {
3398378Ssam 
3406000Sroot 	case 0:
3416000Sroot 		onoff = 0;
3429972Ssam 		tc = &otc;
34313076Ssam 		ltc = &oltc;
3446000Sroot 		break;
3456000Sroot 
3466000Sroot 	case 1:
3476000Sroot 	case 2:
34813076Ssam 		sb.sg_flags |= CBREAK;
3498378Ssam 		if (f == 1)
35013076Ssam 			sb.sg_flags &= ~(ECHO|CRMOD);
3518378Ssam 		else
35213076Ssam 			sb.sg_flags |= ECHO|CRMOD;
35313076Ssam 		sb.sg_erase = sb.sg_kill = -1;
3549972Ssam 		tc = &notc;
35513076Ssam 		ltc = &noltc;
3566000Sroot 		onoff = 1;
3579972Ssam 		break;
3589972Ssam 
3599972Ssam 	default:
3609972Ssam 		return;
3616000Sroot 	}
36213076Ssam 	ioctl(fileno(stdin), TIOCSLTC, (char *)ltc);
36313076Ssam 	ioctl(fileno(stdin), TIOCSETC, (char *)tc);
36413076Ssam 	ioctl(fileno(stdin), TIOCSETP, (char *)&sb);
3656000Sroot 	ioctl(fileno(stdin), FIONBIO, &onoff);
3666000Sroot 	ioctl(fileno(stdout), FIONBIO, &onoff);
3676000Sroot 	return (old);
3686000Sroot }
3696000Sroot 
3706000Sroot char	sibuf[BUFSIZ], *sbp;
3716000Sroot char	tibuf[BUFSIZ], *tbp;
3726000Sroot int	scc, tcc;
3736000Sroot 
3746000Sroot /*
3756000Sroot  * Select from tty and network...
3766000Sroot  */
3776000Sroot telnet(s)
3786000Sroot 	int s;
3796000Sroot {
3806000Sroot 	register int c;
3816000Sroot 	int tin = fileno(stdin), tout = fileno(stdout);
3826000Sroot 	int on = 1;
3836000Sroot 
3848378Ssam 	(void) mode(2);
3856000Sroot 	ioctl(s, FIONBIO, &on);
3866000Sroot 	for (;;) {
3876000Sroot 		int ibits = 0, obits = 0;
3886000Sroot 
3896000Sroot 		if (nfrontp - nbackp)
3906000Sroot 			obits |= (1 << s);
3916000Sroot 		else
3926000Sroot 			ibits |= (1 << tin);
3936000Sroot 		if (tfrontp - tbackp)
3946000Sroot 			obits |= (1 << tout);
3956000Sroot 		else
3966000Sroot 			ibits |= (1 << s);
3976000Sroot 		if (scc < 0 && tcc < 0)
3986000Sroot 			break;
3999217Ssam 		select(16, &ibits, &obits, 0, 0);
4006000Sroot 		if (ibits == 0 && obits == 0) {
4016000Sroot 			sleep(5);
4026000Sroot 			continue;
4036000Sroot 		}
4046000Sroot 
4056000Sroot 		/*
4066000Sroot 		 * Something to read from the network...
4076000Sroot 		 */
4086000Sroot 		if (ibits & (1 << s)) {
4098378Ssam 			scc = read(s, sibuf, sizeof (sibuf));
4106000Sroot 			if (scc < 0 && errno == EWOULDBLOCK)
4116000Sroot 				scc = 0;
4126000Sroot 			else {
4136000Sroot 				if (scc <= 0)
4146000Sroot 					break;
4156000Sroot 				sbp = sibuf;
4166000Sroot 			}
4176000Sroot 		}
4186000Sroot 
4196000Sroot 		/*
4206000Sroot 		 * Something to read from the tty...
4216000Sroot 		 */
4226000Sroot 		if (ibits & (1 << tin)) {
4238378Ssam 			tcc = read(tin, tibuf, sizeof (tibuf));
4246000Sroot 			if (tcc < 0 && errno == EWOULDBLOCK)
4256000Sroot 				tcc = 0;
4266000Sroot 			else {
4276000Sroot 				if (tcc <= 0)
4286000Sroot 					break;
4296000Sroot 				tbp = tibuf;
4306000Sroot 			}
4316000Sroot 		}
4326000Sroot 
4336000Sroot 		while (tcc > 0) {
4346000Sroot 			register int c;
4356000Sroot 
4366000Sroot 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
4376000Sroot 				break;
4386000Sroot 			c = *tbp++ & 0377, tcc--;
4396000Sroot 			if (strip(c) == escape) {
4406000Sroot 				command(0);
4416000Sroot 				tcc = 0;
4426000Sroot 				break;
4436000Sroot 			}
44411758Ssam 			if (c == IAC)
44511758Ssam 				*nfrontp++ = c;
4466000Sroot 			*nfrontp++ = c;
4476000Sroot 		}
4486000Sroot 		if ((obits & (1 << s)) && (nfrontp - nbackp) > 0)
4496000Sroot 			netflush(s);
4506000Sroot 		if (scc > 0)
4516000Sroot 			telrcv();
4526000Sroot 		if ((obits & (1 << tout)) && (tfrontp - tbackp) > 0)
4536000Sroot 			ttyflush(tout);
4546000Sroot 	}
4558378Ssam 	(void) mode(0);
4566000Sroot }
4576000Sroot 
4586000Sroot command(top)
4596000Sroot 	int top;
4606000Sroot {
4616000Sroot 	register struct cmd *c;
4626000Sroot 	int oldmode, wasopen;
4636000Sroot 
4646000Sroot 	oldmode = mode(0);
4656000Sroot 	if (!top)
4666000Sroot 		putchar('\n');
4676000Sroot 	else
46812989Ssam 		signal(SIGINT, SIG_DFL);
4696000Sroot 	for (;;) {
4706000Sroot 		printf("%s> ", prompt);
471*13997Ssam 		if (gets(line) == 0) {
472*13997Ssam 			if (feof(stdin)) {
473*13997Ssam 				clearerr(stdin);
474*13997Ssam 				putchar('\n');
475*13997Ssam 			}
4766000Sroot 			break;
477*13997Ssam 		}
4786000Sroot 		if (line[0] == 0)
4796000Sroot 			break;
4806000Sroot 		makeargv();
4816000Sroot 		c = getcmd(margv[0]);
4826000Sroot 		if (c == (struct cmd *)-1) {
4836000Sroot 			printf("?Ambiguous command\n");
4846000Sroot 			continue;
4856000Sroot 		}
4866000Sroot 		if (c == 0) {
4876000Sroot 			printf("?Invalid command\n");
4886000Sroot 			continue;
4896000Sroot 		}
4906000Sroot 		(*c->handler)(margc, margv);
4916000Sroot 		if (c->handler != help)
4926000Sroot 			break;
4936000Sroot 	}
4946000Sroot 	if (!top) {
4956000Sroot 		if (!connected)
4966000Sroot 			longjmp(toplevel, 1);
4978378Ssam 		(void) mode(oldmode);
4986000Sroot 	}
4996000Sroot }
5006000Sroot 
5016000Sroot /*
5026000Sroot  * Telnet receiver states for fsm
5036000Sroot  */
5046000Sroot #define	TS_DATA		0
5056000Sroot #define	TS_IAC		1
5066000Sroot #define	TS_WILL		2
5076000Sroot #define	TS_WONT		3
5086000Sroot #define	TS_DO		4
5096000Sroot #define	TS_DONT		5
5106000Sroot 
5116000Sroot telrcv()
5126000Sroot {
5136000Sroot 	register int c;
5146000Sroot 	static int state = TS_DATA;
5156000Sroot 
5166000Sroot 	while (scc > 0) {
5176000Sroot 		c = *sbp++ & 0377, scc--;
5186000Sroot 		switch (state) {
5196000Sroot 
5206000Sroot 		case TS_DATA:
5219972Ssam 			if (c == IAC) {
5226000Sroot 				state = TS_IAC;
5239972Ssam 				continue;
5249972Ssam 			}
5259972Ssam 			*tfrontp++ = c;
5269972Ssam 			/*
5279972Ssam 			 * This hack is needed since we can't set
5289972Ssam 			 * CRMOD on output only.  Machines like MULTICS
5299972Ssam 			 * like to send \r without \n; since we must
5309972Ssam 			 * turn off CRMOD to get proper input, the mapping
5319972Ssam 			 * is done here (sigh).
5329972Ssam 			 */
5339972Ssam 			if (c == '\r' && crmod)
5349972Ssam 				*tfrontp++ = '\n';
5356000Sroot 			continue;
5366000Sroot 
5376000Sroot 		case TS_IAC:
5386000Sroot 			switch (c) {
5396000Sroot 
5406000Sroot 			case WILL:
5416000Sroot 				state = TS_WILL;
5426000Sroot 				continue;
5436000Sroot 
5446000Sroot 			case WONT:
5456000Sroot 				state = TS_WONT;
5466000Sroot 				continue;
5476000Sroot 
5486000Sroot 			case DO:
5496000Sroot 				state = TS_DO;
5506000Sroot 				continue;
5516000Sroot 
5526000Sroot 			case DONT:
5536000Sroot 				state = TS_DONT;
5546000Sroot 				continue;
5556000Sroot 
5566000Sroot 			case DM:
5576000Sroot 				ioctl(fileno(stdout), TIOCFLUSH, 0);
5586000Sroot 				break;
5596000Sroot 
5606000Sroot 			case NOP:
5616000Sroot 			case GA:
5626000Sroot 				break;
5636000Sroot 
5646000Sroot 			default:
5656000Sroot 				break;
5666000Sroot 			}
5676000Sroot 			state = TS_DATA;
5686000Sroot 			continue;
5696000Sroot 
5706000Sroot 		case TS_WILL:
5718345Ssam 			printoption("RCVD", will, c, !hisopts[c]);
5726000Sroot 			if (!hisopts[c])
5736000Sroot 				willoption(c);
5746000Sroot 			state = TS_DATA;
5756000Sroot 			continue;
5766000Sroot 
5776000Sroot 		case TS_WONT:
5788345Ssam 			printoption("RCVD", wont, c, hisopts[c]);
5796000Sroot 			if (hisopts[c])
5806000Sroot 				wontoption(c);
5816000Sroot 			state = TS_DATA;
5826000Sroot 			continue;
5836000Sroot 
5846000Sroot 		case TS_DO:
5858345Ssam 			printoption("RCVD", doopt, c, !myopts[c]);
5866000Sroot 			if (!myopts[c])
5876000Sroot 				dooption(c);
5886000Sroot 			state = TS_DATA;
5896000Sroot 			continue;
5906000Sroot 
5916000Sroot 		case TS_DONT:
5928345Ssam 			printoption("RCVD", dont, c, myopts[c]);
5936000Sroot 			if (myopts[c]) {
5946000Sroot 				myopts[c] = 0;
5956000Sroot 				sprintf(nfrontp, wont, c);
5968378Ssam 				nfrontp += sizeof (wont) - 2;
5978345Ssam 				printoption("SENT", wont, c);
5986000Sroot 			}
5996000Sroot 			state = TS_DATA;
6006000Sroot 			continue;
6016000Sroot 		}
6026000Sroot 	}
6036000Sroot }
6046000Sroot 
6056000Sroot willoption(option)
6066000Sroot 	int option;
6076000Sroot {
6086000Sroot 	char *fmt;
6096000Sroot 
6106000Sroot 	switch (option) {
6116000Sroot 
6126000Sroot 	case TELOPT_ECHO:
6138378Ssam 		(void) mode(1);
6146000Sroot 
6156000Sroot 	case TELOPT_SGA:
6166000Sroot 		hisopts[option] = 1;
6176000Sroot 		fmt = doopt;
6186000Sroot 		break;
6196000Sroot 
6206000Sroot 	case TELOPT_TM:
6216000Sroot 		fmt = dont;
6226000Sroot 		break;
6236000Sroot 
6246000Sroot 	default:
6256000Sroot 		fmt = dont;
6266000Sroot 		break;
6276000Sroot 	}
6286024Ssam 	sprintf(nfrontp, fmt, option);
6298378Ssam 	nfrontp += sizeof (dont) - 2;
6308345Ssam 	printoption("SENT", fmt, option);
6316000Sroot }
6326000Sroot 
6336000Sroot wontoption(option)
6346000Sroot 	int option;
6356000Sroot {
6366000Sroot 	char *fmt;
6376000Sroot 
6386000Sroot 	switch (option) {
6396000Sroot 
6406000Sroot 	case TELOPT_ECHO:
6418378Ssam 		(void) mode(2);
6426000Sroot 
6436000Sroot 	case TELOPT_SGA:
6446000Sroot 		hisopts[option] = 0;
6456000Sroot 		fmt = dont;
6466000Sroot 		break;
6476000Sroot 
6486000Sroot 	default:
6496000Sroot 		fmt = dont;
6506000Sroot 	}
6516000Sroot 	sprintf(nfrontp, fmt, option);
6528378Ssam 	nfrontp += sizeof (doopt) - 2;
6538345Ssam 	printoption("SENT", fmt, option);
6546000Sroot }
6556000Sroot 
6566000Sroot dooption(option)
6576000Sroot 	int option;
6586000Sroot {
6596000Sroot 	char *fmt;
6606000Sroot 
6616000Sroot 	switch (option) {
6626000Sroot 
6636000Sroot 	case TELOPT_TM:
6646000Sroot 		fmt = wont;
6656000Sroot 		break;
6666000Sroot 
66713231Ssam 	case TELOPT_ECHO:
66813231Ssam 		(void) mode(2);
66913231Ssam 		fmt = will;
67013231Ssam 		hisopts[option] = 0;
67113231Ssam 		break;
67213231Ssam 
6736000Sroot 	case TELOPT_SGA:
6746000Sroot 		fmt = will;
6756000Sroot 		break;
6766000Sroot 
6776000Sroot 	default:
6786000Sroot 		fmt = wont;
6796000Sroot 		break;
6806000Sroot 	}
6816000Sroot 	sprintf(nfrontp, fmt, option);
6828378Ssam 	nfrontp += sizeof (doopt) - 2;
6838345Ssam 	printoption("SENT", fmt, option);
6846000Sroot }
6856000Sroot 
6866000Sroot /*
6876000Sroot  * Set the escape character.
6886000Sroot  */
6896000Sroot setescape(argc, argv)
6906000Sroot 	int argc;
6916000Sroot 	char *argv[];
6926000Sroot {
6936000Sroot 	register char *arg;
6946000Sroot 	char buf[50];
6956000Sroot 
6966000Sroot 	if (argc > 2)
6976000Sroot 		arg = argv[1];
6986000Sroot 	else {
6996000Sroot 		printf("new escape character: ");
7006000Sroot 		gets(buf);
7016000Sroot 		arg = buf;
7026000Sroot 	}
7036000Sroot 	if (arg[0] != '\0')
7046000Sroot 		escape = arg[0];
7056000Sroot 	printf("Escape character is '%s'.\n", control(escape));
7069972Ssam 	fflush(stdout);
7076000Sroot }
7086000Sroot 
7096024Ssam /*VARARGS*/
7106024Ssam setoptions()
7116024Ssam {
7129972Ssam 
7136024Ssam 	showoptions = !showoptions;
7146024Ssam 	printf("%s show option processing.\n", showoptions ? "Will" : "Wont");
7159972Ssam 	fflush(stdout);
7166024Ssam }
7176024Ssam 
7189972Ssam /*VARARGS*/
7199972Ssam setcrmod()
7209972Ssam {
7219972Ssam 
7229972Ssam 	crmod = !crmod;
7239972Ssam 	printf("%s map carriage return on output.\n", crmod ? "Will" : "Wont");
7249972Ssam 	fflush(stdout);
7259972Ssam }
7269972Ssam 
72710339Ssam /*VARARGS*/
72810339Ssam setdebug()
72910339Ssam {
73010339Ssam 
73110339Ssam 	debug = !debug;
73210339Ssam 	printf("%s turn on socket level debugging.\n",
73310339Ssam 		debug ? "Will" : "Wont");
73410339Ssam 	fflush(stdout);
73511758Ssam 	if (debug && setsockopt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
73611758Ssam 		perror("setsockopt (SO_DEBUG)");
73710339Ssam }
73810339Ssam 
7396000Sroot /*
7406000Sroot  * Construct a control character sequence
7416000Sroot  * for a special character.
7426000Sroot  */
7436000Sroot char *
7446000Sroot control(c)
7456000Sroot 	register int c;
7466000Sroot {
7476000Sroot 	static char buf[3];
7486000Sroot 
7496000Sroot 	if (c == 0177)
7506000Sroot 		return ("^?");
7516000Sroot 	if (c >= 040) {
7526000Sroot 		buf[0] = c;
7536000Sroot 		buf[1] = 0;
7546000Sroot 	} else {
7556000Sroot 		buf[0] = '^';
7566000Sroot 		buf[1] = '@'+c;
7576000Sroot 		buf[2] = 0;
7586000Sroot 	}
7596000Sroot 	return (buf);
7606000Sroot }
7616000Sroot 
7626000Sroot struct cmd *
7636000Sroot getcmd(name)
7646000Sroot 	register char *name;
7656000Sroot {
7666000Sroot 	register char *p, *q;
7676000Sroot 	register struct cmd *c, *found;
7686000Sroot 	register int nmatches, longest;
7696000Sroot 
7706000Sroot 	longest = 0;
7716000Sroot 	nmatches = 0;
7726000Sroot 	found = 0;
7736000Sroot 	for (c = cmdtab; p = c->name; c++) {
7746000Sroot 		for (q = name; *q == *p++; q++)
7756000Sroot 			if (*q == 0)		/* exact match? */
7766000Sroot 				return (c);
7776000Sroot 		if (!*q) {			/* the name was a prefix */
7786000Sroot 			if (q - name > longest) {
7796000Sroot 				longest = q - name;
7806000Sroot 				nmatches = 1;
7816000Sroot 				found = c;
7826000Sroot 			} else if (q - name == longest)
7836000Sroot 				nmatches++;
7846000Sroot 		}
7856000Sroot 	}
7866000Sroot 	if (nmatches > 1)
7876000Sroot 		return ((struct cmd *)-1);
7886000Sroot 	return (found);
7896000Sroot }
7906000Sroot 
7916000Sroot deadpeer()
7926000Sroot {
7938378Ssam 	(void) mode(0);
7946000Sroot 	longjmp(peerdied, -1);
7956000Sroot }
7966000Sroot 
7976000Sroot intr()
7986000Sroot {
7998378Ssam 	(void) mode(0);
8006000Sroot 	longjmp(toplevel, -1);
8016000Sroot }
8026000Sroot 
8036000Sroot ttyflush(fd)
8046000Sroot {
8056000Sroot 	int n;
8066000Sroot 
8076000Sroot 	if ((n = tfrontp - tbackp) > 0)
8086000Sroot 		n = write(fd, tbackp, n);
8098345Ssam 	if (n < 0)
8108345Ssam 		return;
8116000Sroot 	tbackp += n;
8126000Sroot 	if (tbackp == tfrontp)
8136000Sroot 		tbackp = tfrontp = ttyobuf;
8146000Sroot }
8156000Sroot 
8166000Sroot netflush(fd)
8176000Sroot {
8186000Sroot 	int n;
8196000Sroot 
8206000Sroot 	if ((n = nfrontp - nbackp) > 0)
8216000Sroot 		n = write(fd, nbackp, n);
8226501Ssam 	if (n < 0) {
8236501Ssam 		if (errno != ENOBUFS && errno != EWOULDBLOCK) {
8248378Ssam 			(void) mode(0);
8258377Ssam 			perror(hostname);
8266501Ssam 			close(fd);
8276501Ssam 			longjmp(peerdied, -1);
8286501Ssam 			/*NOTREACHED*/
8296501Ssam 		}
8306000Sroot 		n = 0;
8316501Ssam 	}
8326000Sroot 	nbackp += n;
8336000Sroot 	if (nbackp == nfrontp)
8346000Sroot 		nbackp = nfrontp = netobuf;
8356000Sroot }
8366024Ssam 
8376293Sroot /*VARARGS*/
8386293Sroot printoption(direction, fmt, option, what)
8396024Ssam 	char *direction, *fmt;
8406293Sroot 	int option, what;
8416024Ssam {
8428345Ssam 	if (!showoptions)
8438345Ssam 		return;
8446024Ssam 	printf("%s ", direction);
8456024Ssam 	if (fmt == doopt)
8466024Ssam 		fmt = "do";
8476024Ssam 	else if (fmt == dont)
8486024Ssam 		fmt = "dont";
8496024Ssam 	else if (fmt == will)
8506024Ssam 		fmt = "will";
8516024Ssam 	else if (fmt == wont)
8526024Ssam 		fmt = "wont";
8536024Ssam 	else
8546024Ssam 		fmt = "???";
8556024Ssam 	if (option < TELOPT_SUPDUP)
8566293Sroot 		printf("%s %s", fmt, telopts[option]);
8576024Ssam 	else
8586293Sroot 		printf("%s %d", fmt, option);
8596293Sroot 	if (*direction == '<') {
8606293Sroot 		printf("\r\n");
8616293Sroot 		return;
8626293Sroot 	}
8636293Sroot 	printf(" (%s)\r\n", what ? "reply" : "don't reply");
8646024Ssam }
865