17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1990, 1993
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  *
33740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <netdb.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <pwd.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <arpa/telnet.h>
517c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "general.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include "ring.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "externs.h"
587c478bd9Sstevel@tonic-gate #include "defines.h"
597c478bd9Sstevel@tonic-gate #include "types.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate extern	char *telnet_krb5_realm;
627c478bd9Sstevel@tonic-gate extern	void krb5_profile_get_options(char *, char *,
637c478bd9Sstevel@tonic-gate 		profile_options_boolean*);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <k5-int.h>
667c478bd9Sstevel@tonic-gate #include <profile/prof_int.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate profile_options_boolean config_file_options[] = {
697c478bd9Sstevel@tonic-gate 	{ "forwardable", &forwardable_flag, 0},
707c478bd9Sstevel@tonic-gate 	{ "forward", &forward_flag, 0},
717c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
727c478bd9Sstevel@tonic-gate 	{ "autologin", &autologin, 0 },
737c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0}
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Number of maximum IPv4 gateways user can specify. This number is limited by
807c478bd9Sstevel@tonic-gate  * the maximum size of the IPv4 options in the IPv4 header.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate #define	MAX_GATEWAY	8
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Number of maximum IPv6 gateways user can specify. This number is limited by
857c478bd9Sstevel@tonic-gate  * the maximum header extension length of the IPv6 routing header.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate #define	MAX_GATEWAY6	127
887c478bd9Sstevel@tonic-gate #define	MAXMAX_GATEWAY	MAX(MAX_GATEWAY, MAX_GATEWAY6)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Depending on the address resolutions of the target and gateways,
927c478bd9Sstevel@tonic-gate  * we determine which addresses of the target we'll try connecting to.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate #define	ALL_ADDRS	0	/* try all addrs of target */
957c478bd9Sstevel@tonic-gate #define	ONLY_V4		1	/* try only IPv4 addrs of target */
967c478bd9Sstevel@tonic-gate #define	ONLY_V6		2	/* try only IPv6 addrs of target */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #if defined(USE_TOS)
997c478bd9Sstevel@tonic-gate int tos = -1;
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate char	*hostname;
1037c478bd9Sstevel@tonic-gate static char _hostname[MAXHOSTNAMELEN];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int send_tncmd(void (*func)(), char *, char *);
1067c478bd9Sstevel@tonic-gate static void call(int n_ptrs, ...);
1077c478bd9Sstevel@tonic-gate static int cmdrc(char *, char *);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate typedef struct {
1107c478bd9Sstevel@tonic-gate 	char	*name;		/* command name */
1117c478bd9Sstevel@tonic-gate 	char	*help;		/* help string (NULL for no help) */
1127c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* routine which executes command */
1137c478bd9Sstevel@tonic-gate 	int	needconnect;	/* Do we need to be connected to execute? */
1147c478bd9Sstevel@tonic-gate } Command;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * storage for IPv6 and/or IPv4 addresses of gateways
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate struct gateway {
1207c478bd9Sstevel@tonic-gate 	struct in6_addr	gw_addr6;
1217c478bd9Sstevel@tonic-gate 	struct in_addr	gw_addr;
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * IPv4 source routing option.
1267c478bd9Sstevel@tonic-gate  * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
1277c478bd9Sstevel@tonic-gate  * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
1287c478bd9Sstevel@tonic-gate  * If it were defined as "struct in_addr ipsr_addrs[1]", "ipsr_ptr" would be
1297c478bd9Sstevel@tonic-gate  * followed by one byte of padding to avoid misaligned struct in_addr.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate struct ip_sourceroute {
1327c478bd9Sstevel@tonic-gate 	uint8_t ipsr_code;
1337c478bd9Sstevel@tonic-gate 	uint8_t ipsr_len;
1347c478bd9Sstevel@tonic-gate 	uint8_t ipsr_ptr;
1357c478bd9Sstevel@tonic-gate 	/* up to 9 IPv4 addresses */
1367c478bd9Sstevel@tonic-gate 	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static char *line = NULL;
1407c478bd9Sstevel@tonic-gate static unsigned linesize = 0;
1417c478bd9Sstevel@tonic-gate static int margc;
1427c478bd9Sstevel@tonic-gate static char **margv = NULL;
1437c478bd9Sstevel@tonic-gate static unsigned margvlen = 0;
1447c478bd9Sstevel@tonic-gate static int doing_rc = 0;   /* .telnetrc file is being read and processed */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static void
Close(int * fd)1477c478bd9Sstevel@tonic-gate Close(int *fd)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	if (*fd != -1) {
1507c478bd9Sstevel@tonic-gate 		(void) close(*fd);
1517c478bd9Sstevel@tonic-gate 		*fd = -1;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static void
Free(char ** p)1567c478bd9Sstevel@tonic-gate Free(char **p)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	if (*p != NULL) {
1597c478bd9Sstevel@tonic-gate 		free(*p);
1607c478bd9Sstevel@tonic-gate 		*p = NULL;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static void
FreeHostnameList(char * list[])1657c478bd9Sstevel@tonic-gate FreeHostnameList(char *list[])
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	unsigned i;
1687c478bd9Sstevel@tonic-gate 	for (i = 0; i <= MAXMAX_GATEWAY && list[i] != NULL; i++)
1697c478bd9Sstevel@tonic-gate 		Free(&list[i]);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	MARGV_CHUNK_SIZE 8
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static void
set_argv(str)1757c478bd9Sstevel@tonic-gate set_argv(str)
1767c478bd9Sstevel@tonic-gate char *str;
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	if (margc == margvlen) {
1797c478bd9Sstevel@tonic-gate 		char **newmargv;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		margvlen += MARGV_CHUNK_SIZE;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		if ((newmargv = realloc(margv, margvlen * sizeof (char *)))
1847c478bd9Sstevel@tonic-gate 			== NULL)
1857c478bd9Sstevel@tonic-gate 			ExitString("telnet: no space for arguments",
1867c478bd9Sstevel@tonic-gate 				EXIT_FAILURE);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		margv = newmargv;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	margv[margc] = str;
1927c478bd9Sstevel@tonic-gate 	if (str != NULL)
1937c478bd9Sstevel@tonic-gate 		margc++;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static void
makeargv()1977c478bd9Sstevel@tonic-gate makeargv()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	char *cp, *cp2, c;
2007c478bd9Sstevel@tonic-gate 	boolean_t shellcmd = B_FALSE;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	margc = 0;
2037c478bd9Sstevel@tonic-gate 	cp = line;
2047c478bd9Sstevel@tonic-gate 	if (*cp == '!') {		/* Special case shell escape */
2057c478bd9Sstevel@tonic-gate 		set_argv("!");		/* No room in string to get this */
2067c478bd9Sstevel@tonic-gate 		cp++;
2077c478bd9Sstevel@tonic-gate 		shellcmd = B_TRUE;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	while ((c = *cp) != '\0') {
2107c478bd9Sstevel@tonic-gate 		register int inquote = 0;
2117c478bd9Sstevel@tonic-gate 		while (isspace(c))
2127c478bd9Sstevel@tonic-gate 			c = *++cp;
2137c478bd9Sstevel@tonic-gate 		if (c == '\0')
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		set_argv(cp);
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * For the shell escape, put the rest of the line, less
2187c478bd9Sstevel@tonic-gate 		 * leading space, into a single argument, breaking out from
2197c478bd9Sstevel@tonic-gate 		 * the loop to prevent the rest of the line being split up
2207c478bd9Sstevel@tonic-gate 		 * into smaller arguments.
2217c478bd9Sstevel@tonic-gate 		 */
2227c478bd9Sstevel@tonic-gate 		if (shellcmd)
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		for (cp2 = cp; c != '\0'; c = *++cp) {
2257c478bd9Sstevel@tonic-gate 			if (inquote) {
2267c478bd9Sstevel@tonic-gate 				if (c == inquote) {
2277c478bd9Sstevel@tonic-gate 					inquote = 0;
2287c478bd9Sstevel@tonic-gate 					continue;
2297c478bd9Sstevel@tonic-gate 				}
2307c478bd9Sstevel@tonic-gate 			} else {
2317c478bd9Sstevel@tonic-gate 				if (c == '\\') {
2327c478bd9Sstevel@tonic-gate 					if ((c = *++cp) == '\0')
2337c478bd9Sstevel@tonic-gate 						break;
2347c478bd9Sstevel@tonic-gate 				} else if (c == '"') {
2357c478bd9Sstevel@tonic-gate 					inquote = '"';
2367c478bd9Sstevel@tonic-gate 					continue;
2377c478bd9Sstevel@tonic-gate 				} else if (c == '\'') {
2387c478bd9Sstevel@tonic-gate 					inquote = '\'';
2397c478bd9Sstevel@tonic-gate 					continue;
2407c478bd9Sstevel@tonic-gate 				} else if (isspace(c))
2417c478bd9Sstevel@tonic-gate 					break;
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 			*cp2++ = c;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		*cp2 = '\0';
2467c478bd9Sstevel@tonic-gate 		if (c == '\0')
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		cp++;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	set_argv((char *)NULL);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Make a character string into a number.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
259740638c8Sbw 	static int
special(s)2607c478bd9Sstevel@tonic-gate special(s)
2617c478bd9Sstevel@tonic-gate 	register char *s;
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	register char c;
2647c478bd9Sstevel@tonic-gate 	char b;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	switch (*s) {
2677c478bd9Sstevel@tonic-gate 	case '^':
2687c478bd9Sstevel@tonic-gate 		b = *++s;
2697c478bd9Sstevel@tonic-gate 		if (b == '?') {
2707c478bd9Sstevel@tonic-gate 		    c = b | 0x40;		/* DEL */
2717c478bd9Sstevel@tonic-gate 		} else {
2727c478bd9Sstevel@tonic-gate 		    c = b & 0x1f;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		break;
2757c478bd9Sstevel@tonic-gate 	default:
2767c478bd9Sstevel@tonic-gate 		c = *s;
2777c478bd9Sstevel@tonic-gate 		break;
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	return (c);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Construct a control character sequence
2847c478bd9Sstevel@tonic-gate  * for a special character.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate 	static char *
control(c)2877c478bd9Sstevel@tonic-gate control(c)
2887c478bd9Sstevel@tonic-gate 	register cc_t c;
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	static char buf[5];
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * The only way I could get the Sun 3.5 compiler
2937c478bd9Sstevel@tonic-gate 	 * to shut up about
2947c478bd9Sstevel@tonic-gate 	 *	if ((unsigned int)c >= 0x80)
2957c478bd9Sstevel@tonic-gate 	 * was to assign "c" to an unsigned int variable...
2967c478bd9Sstevel@tonic-gate 	 * Arggg....
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	register unsigned int uic = (unsigned int)c;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (uic == 0x7f)
3017c478bd9Sstevel@tonic-gate 		return ("^?");
3027c478bd9Sstevel@tonic-gate 	if (c == (cc_t)_POSIX_VDISABLE) {
3037c478bd9Sstevel@tonic-gate 		return ("off");
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	if (uic >= 0x80) {
3067c478bd9Sstevel@tonic-gate 		buf[0] = '\\';
3077c478bd9Sstevel@tonic-gate 		buf[1] = ((c>>6)&07) + '0';
3087c478bd9Sstevel@tonic-gate 		buf[2] = ((c>>3)&07) + '0';
3097c478bd9Sstevel@tonic-gate 		buf[3] = (c&07) + '0';
3107c478bd9Sstevel@tonic-gate 		buf[4] = 0;
3117c478bd9Sstevel@tonic-gate 	} else if (uic >= 0x20) {
3127c478bd9Sstevel@tonic-gate 		buf[0] = c;
3137c478bd9Sstevel@tonic-gate 		buf[1] = 0;
3147c478bd9Sstevel@tonic-gate 	} else {
3157c478bd9Sstevel@tonic-gate 		buf[0] = '^';
3167c478bd9Sstevel@tonic-gate 		buf[1] = '@'+c;
3177c478bd9Sstevel@tonic-gate 		buf[2] = 0;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 	return (buf);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Same as control() except that its only used for escape handling, which uses
3247c478bd9Sstevel@tonic-gate  * _POSIX_VDISABLE differently and is aided by the use of the state variable
3257c478bd9Sstevel@tonic-gate  * escape_valid.
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate 	static char *
esc_control(c)3287c478bd9Sstevel@tonic-gate esc_control(c)
3297c478bd9Sstevel@tonic-gate 	register cc_t c;
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	static char buf[5];
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * The only way I could get the Sun 3.5 compiler
3347c478bd9Sstevel@tonic-gate 	 * to shut up about
3357c478bd9Sstevel@tonic-gate 	 *	if ((unsigned int)c >= 0x80)
3367c478bd9Sstevel@tonic-gate 	 * was to assign "c" to an unsigned int variable...
3377c478bd9Sstevel@tonic-gate 	 * Arggg....
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	register unsigned int uic = (unsigned int)c;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (escape_valid == B_FALSE)
3427c478bd9Sstevel@tonic-gate 		return ("off");
3437c478bd9Sstevel@tonic-gate 	if (uic == 0x7f)
3447c478bd9Sstevel@tonic-gate 		return ("^?");
3457c478bd9Sstevel@tonic-gate 	if (uic >= 0x80) {
3467c478bd9Sstevel@tonic-gate 		buf[0] = '\\';
3477c478bd9Sstevel@tonic-gate 		buf[1] = ((c>>6)&07) + '0';
3487c478bd9Sstevel@tonic-gate 		buf[2] = ((c>>3)&07) + '0';
3497c478bd9Sstevel@tonic-gate 		buf[3] = (c&07) + '0';
3507c478bd9Sstevel@tonic-gate 		buf[4] = 0;
3517c478bd9Sstevel@tonic-gate 	} else if (uic >= 0x20) {
3527c478bd9Sstevel@tonic-gate 		buf[0] = c;
3537c478bd9Sstevel@tonic-gate 		buf[1] = 0;
3547c478bd9Sstevel@tonic-gate 	} else {
3557c478bd9Sstevel@tonic-gate 		buf[0] = '^';
3567c478bd9Sstevel@tonic-gate 		buf[1] = '@'+c;
3577c478bd9Sstevel@tonic-gate 		buf[2] = 0;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	return (buf);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  *	The following are data structures and routines for
3647c478bd9Sstevel@tonic-gate  *	the "send" command.
3657c478bd9Sstevel@tonic-gate  *
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate struct sendlist {
3697c478bd9Sstevel@tonic-gate 	char	*name;		/* How user refers to it (case independent) */
3707c478bd9Sstevel@tonic-gate 	char	*help;		/* Help information (0 ==> no help) */
3717c478bd9Sstevel@tonic-gate 	int	needconnect;	/* Need to be connected */
3727c478bd9Sstevel@tonic-gate 	int	narg;		/* Number of arguments */
3737c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* Routine to perform (for special ops) */
3747c478bd9Sstevel@tonic-gate 	int	nbyte;		/* Number of bytes to send this command */
3757c478bd9Sstevel@tonic-gate 	int	what;		/* Character to be sent (<0 ==> special) */
3767c478bd9Sstevel@tonic-gate };
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static int send_esc(void);
3807c478bd9Sstevel@tonic-gate static int send_help(void);
3817c478bd9Sstevel@tonic-gate static int send_docmd(char *);
3827c478bd9Sstevel@tonic-gate static int send_dontcmd(char *);
3837c478bd9Sstevel@tonic-gate static int send_willcmd(char *);
3847c478bd9Sstevel@tonic-gate static int send_wontcmd(char *);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate static struct sendlist Sendlist[] = {
3877c478bd9Sstevel@tonic-gate 	{ "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },
3887c478bd9Sstevel@tonic-gate 	{ "ayt", "Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },
3897c478bd9Sstevel@tonic-gate 	{ "b", 0,					1, 0, 0, 2, BREAK },
3907c478bd9Sstevel@tonic-gate 	{ "br", 0,					1, 0, 0, 2, BREAK },
3917c478bd9Sstevel@tonic-gate 	{ "break", 0,					1, 0, 0, 2, BREAK },
3927c478bd9Sstevel@tonic-gate 	{ "brk", "Send Telnet Break",			1, 0, 0, 2, BREAK },
3937c478bd9Sstevel@tonic-gate 	{ "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },
3947c478bd9Sstevel@tonic-gate 	{ "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },
3957c478bd9Sstevel@tonic-gate 	{ "escape", "Send current escape character",	1, 0, send_esc, 1, 0 },
3967c478bd9Sstevel@tonic-gate 	{ "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },
3977c478bd9Sstevel@tonic-gate 	{ "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },
3987c478bd9Sstevel@tonic-gate 	{ "intp", 0,					1, 0, 0, 2, IP },
3997c478bd9Sstevel@tonic-gate 	{ "interrupt", 0,				1, 0, 0, 2, IP },
4007c478bd9Sstevel@tonic-gate 	{ "intr",	0,				1, 0, 0, 2, IP },
4017c478bd9Sstevel@tonic-gate 	{ "nop", "Send Telnet 'No operation'",		1, 0, 0, 2, NOP },
4027c478bd9Sstevel@tonic-gate 	{ "eor", "Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },
4037c478bd9Sstevel@tonic-gate 	{ "abort", "Send Telnet 'Abort Process'",	1, 0, 0, 2, ABORT },
4047c478bd9Sstevel@tonic-gate 	{ "susp", "Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },
4057c478bd9Sstevel@tonic-gate 	{ "eof", "Send Telnet End of File Character",	1, 0, 0, 2, xEOF },
4067c478bd9Sstevel@tonic-gate 	{ "synch", "Perform Telnet 'Synch operation'",	1, 0, dosynch, 2, 0 },
4077c478bd9Sstevel@tonic-gate 	{ "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
4087c478bd9Sstevel@tonic-gate 	{ "?",	"Display send options",			0, 0, send_help, 0, 0 },
4097c478bd9Sstevel@tonic-gate 	{ "help",	0,				0, 0, send_help, 0, 0 },
4107c478bd9Sstevel@tonic-gate 	{ "do",	0,				0, 1, send_docmd, 3, 0 },
4117c478bd9Sstevel@tonic-gate 	{ "dont", 0,				0, 1, send_dontcmd, 3, 0 },
4127c478bd9Sstevel@tonic-gate 	{ "will", 0,				0, 1, send_willcmd, 3, 0 },
4137c478bd9Sstevel@tonic-gate 	{ "wont", 0,				0, 1, send_wontcmd, 3, 0 },
4147c478bd9Sstevel@tonic-gate 	{ 0 }
4157c478bd9Sstevel@tonic-gate };
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate #define	GETSEND(name) ((struct sendlist *)genget(name, (char **)Sendlist, \
4187c478bd9Sstevel@tonic-gate 				sizeof (struct sendlist)))
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static int
sendcmd(argc,argv)4217c478bd9Sstevel@tonic-gate sendcmd(argc, argv)
4227c478bd9Sstevel@tonic-gate 	int  argc;
4237c478bd9Sstevel@tonic-gate 	char **argv;
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	int count;	/* how many bytes we are going to need to send */
4267c478bd9Sstevel@tonic-gate 	int i;
4277c478bd9Sstevel@tonic-gate 	struct sendlist *s;	/* pointer to current command */
4287c478bd9Sstevel@tonic-gate 	int success = 0;
4297c478bd9Sstevel@tonic-gate 	int needconnect = 0;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (argc < 2) {
4327c478bd9Sstevel@tonic-gate 		(void) printf(
4337c478bd9Sstevel@tonic-gate 		    "need at least one argument for 'send' command\n");
4347c478bd9Sstevel@tonic-gate 		(void) printf("'send ?' for help\n");
4357c478bd9Sstevel@tonic-gate 		return (0);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * First, validate all the send arguments.
4397c478bd9Sstevel@tonic-gate 	 * In addition, we see how much space we are going to need, and
4407c478bd9Sstevel@tonic-gate 	 * whether or not we will be doing a "SYNCH" operation (which
4417c478bd9Sstevel@tonic-gate 	 * flushes the network queue).
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	count = 0;
4447c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
4457c478bd9Sstevel@tonic-gate 		s = GETSEND(argv[i]);
4467c478bd9Sstevel@tonic-gate 		if (s == 0) {
4477c478bd9Sstevel@tonic-gate 			(void) printf("Unknown send argument '%s'\n'send ?' "
4487c478bd9Sstevel@tonic-gate 			    "for help.\n", argv[i]);
4497c478bd9Sstevel@tonic-gate 			return (0);
4507c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(s)) {
4517c478bd9Sstevel@tonic-gate 			(void) printf("Ambiguous send argument '%s'\n'send ?' "
4527c478bd9Sstevel@tonic-gate 			    "for help.\n", argv[i]);
4537c478bd9Sstevel@tonic-gate 			return (0);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		if (i + s->narg >= argc) {
4567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4577c478bd9Sstevel@tonic-gate 			    "Need %d argument%s to 'send %s' "
4587c478bd9Sstevel@tonic-gate 			    "command.  'send %s ?' for help.\n",
4597c478bd9Sstevel@tonic-gate 			    s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
4607c478bd9Sstevel@tonic-gate 			return (0);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		count += s->nbyte;
4637c478bd9Sstevel@tonic-gate 		if (s->handler == send_help) {
4647c478bd9Sstevel@tonic-gate 			(void) send_help();
4657c478bd9Sstevel@tonic-gate 			return (0);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		i += s->narg;
4697c478bd9Sstevel@tonic-gate 		needconnect += s->needconnect;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	if (!connected && needconnect) {
4727c478bd9Sstevel@tonic-gate 		(void) printf("?Need to be connected first.\n");
4737c478bd9Sstevel@tonic-gate 		(void) printf("'send ?' for help\n");
4747c478bd9Sstevel@tonic-gate 		return (0);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	/* Now, do we have enough room? */
4777c478bd9Sstevel@tonic-gate 	if (NETROOM() < count) {
4787c478bd9Sstevel@tonic-gate 		(void) printf("There is not enough room in the buffer "
4797c478bd9Sstevel@tonic-gate 		    "TO the network\n");
4807c478bd9Sstevel@tonic-gate 		(void) printf(
4817c478bd9Sstevel@tonic-gate 		    "to process your request.  Nothing will be done.\n");
4827c478bd9Sstevel@tonic-gate 		(void) printf("('send synch' will throw away most "
4837c478bd9Sstevel@tonic-gate 		    "data in the network\n");
4847c478bd9Sstevel@tonic-gate 		(void) printf("buffer, if this might help.)\n");
4857c478bd9Sstevel@tonic-gate 		return (0);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	/* OK, they are all OK, now go through again and actually send */
4887c478bd9Sstevel@tonic-gate 	count = 0;
4897c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
4907c478bd9Sstevel@tonic-gate 		if ((s = GETSEND(argv[i])) == 0) {
4917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4927c478bd9Sstevel@tonic-gate 			    "Telnet 'send' error - argument disappeared!\n");
4937c478bd9Sstevel@tonic-gate 			(void) quit();
4947c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		if (s->handler) {
4977c478bd9Sstevel@tonic-gate 			count++;
4987c478bd9Sstevel@tonic-gate 			success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
4997c478bd9Sstevel@tonic-gate 			    (s->narg > 1) ? argv[i+2] : 0);
5007c478bd9Sstevel@tonic-gate 			i += s->narg;
5017c478bd9Sstevel@tonic-gate 		} else {
5027c478bd9Sstevel@tonic-gate 			NET2ADD(IAC, s->what);
5037c478bd9Sstevel@tonic-gate 			printoption("SENT", IAC, s->what);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	return (count == success);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
send_esc()5107c478bd9Sstevel@tonic-gate send_esc()
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	NETADD(escape);
5137c478bd9Sstevel@tonic-gate 	return (1);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static int
send_docmd(name)5177c478bd9Sstevel@tonic-gate send_docmd(name)
5187c478bd9Sstevel@tonic-gate 	char *name;
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_do, "do", name));
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static int
send_dontcmd(name)5247c478bd9Sstevel@tonic-gate send_dontcmd(name)
5257c478bd9Sstevel@tonic-gate 	char *name;
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_dont, "dont", name));
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate static int
send_willcmd(name)5317c478bd9Sstevel@tonic-gate send_willcmd(name)
5327c478bd9Sstevel@tonic-gate 	char *name;
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_will, "will", name));
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int
send_wontcmd(name)5387c478bd9Sstevel@tonic-gate send_wontcmd(name)
5397c478bd9Sstevel@tonic-gate 	char *name;
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	return (send_tncmd(send_wont, "wont", name));
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate int
5457c478bd9Sstevel@tonic-gate send_tncmd(func, cmd, name)
5467c478bd9Sstevel@tonic-gate 	void	(*func)();
5477c478bd9Sstevel@tonic-gate 	char	*cmd, *name;
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	char **cpp;
5507c478bd9Sstevel@tonic-gate 	extern char *telopts[];
5517c478bd9Sstevel@tonic-gate 	register int val = 0;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (isprefix(name, "help") || isprefix(name, "?")) {
5547c478bd9Sstevel@tonic-gate 		register int col, len;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		(void) printf("Usage: send %s <value|option>\n", cmd);
5577c478bd9Sstevel@tonic-gate 		(void) printf("\"value\" must be from 0 to 255\n");
5587c478bd9Sstevel@tonic-gate 		(void) printf("Valid options are:\n\t");
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		col = 8;
5617c478bd9Sstevel@tonic-gate 		for (cpp = telopts; *cpp; cpp++) {
5627c478bd9Sstevel@tonic-gate 			len = strlen(*cpp) + 3;
5637c478bd9Sstevel@tonic-gate 			if (col + len > 65) {
5647c478bd9Sstevel@tonic-gate 				(void) printf("\n\t");
5657c478bd9Sstevel@tonic-gate 				col = 8;
5667c478bd9Sstevel@tonic-gate 			}
5677c478bd9Sstevel@tonic-gate 			(void) printf(" \"%s\"", *cpp);
5687c478bd9Sstevel@tonic-gate 			col += len;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5717c478bd9Sstevel@tonic-gate 		return (0);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	cpp = (char **)genget(name, telopts, sizeof (char *));
5747c478bd9Sstevel@tonic-gate 	if (Ambiguous(cpp)) {
5757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5767c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('send %s ?' for help).\n",
5777c478bd9Sstevel@tonic-gate 		    name, cmd);
5787c478bd9Sstevel@tonic-gate 		return (0);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	if (cpp) {
5817c478bd9Sstevel@tonic-gate 		val = cpp - telopts;
5827c478bd9Sstevel@tonic-gate 	} else {
5837c478bd9Sstevel@tonic-gate 		register char *cp = name;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9') {
5867c478bd9Sstevel@tonic-gate 			val *= 10;
5877c478bd9Sstevel@tonic-gate 			val += *cp - '0';
5887c478bd9Sstevel@tonic-gate 			cp++;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		if (*cp != 0) {
5917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5927c478bd9Sstevel@tonic-gate 			    "'%s': unknown argument ('send %s ?' for help).\n",
5937c478bd9Sstevel@tonic-gate 			    name, cmd);
5947c478bd9Sstevel@tonic-gate 			return (0);
5957c478bd9Sstevel@tonic-gate 		} else if (val < 0 || val > 255) {
5967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5977c478bd9Sstevel@tonic-gate 			    "'%s': bad value ('send %s ?' for help).\n",
5987c478bd9Sstevel@tonic-gate 			    name, cmd);
5997c478bd9Sstevel@tonic-gate 			return (0);
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	if (!connected) {
6037c478bd9Sstevel@tonic-gate 		(void) printf("?Need to be connected first.\n");
6047c478bd9Sstevel@tonic-gate 		return (0);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	(*func)(val, 1);
6077c478bd9Sstevel@tonic-gate 	return (1);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate static int
send_help()6117c478bd9Sstevel@tonic-gate send_help()
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	struct sendlist *s;	/* pointer to current command */
6147c478bd9Sstevel@tonic-gate 	for (s = Sendlist; s->name; s++) {
6157c478bd9Sstevel@tonic-gate 		if (s->help)
6167c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", s->name, s->help);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	return (0);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * The following are the routines and data structures referred
6237c478bd9Sstevel@tonic-gate  * to by the arguments to the "toggle" command.
6247c478bd9Sstevel@tonic-gate  */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate static int
lclchars()6277c478bd9Sstevel@tonic-gate lclchars()
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	donelclchars = 1;
6307c478bd9Sstevel@tonic-gate 	return (1);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate static int
togdebug()6347c478bd9Sstevel@tonic-gate togdebug()
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	if (net > 0 &&
6377c478bd9Sstevel@tonic-gate 	    (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
6387c478bd9Sstevel@tonic-gate 		perror("setsockopt (SO_DEBUG)");
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	return (1);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static int
togcrlf()6457c478bd9Sstevel@tonic-gate togcrlf()
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	if (crlf) {
6487c478bd9Sstevel@tonic-gate 		(void) printf(
6497c478bd9Sstevel@tonic-gate 		    "Will send carriage returns as telnet <CR><LF>.\n");
6507c478bd9Sstevel@tonic-gate 	} else {
6517c478bd9Sstevel@tonic-gate 		(void) printf(
6527c478bd9Sstevel@tonic-gate 		    "Will send carriage returns as telnet <CR><NUL>.\n");
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	return (1);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static int binmode;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate static int
togbinary(val)6607c478bd9Sstevel@tonic-gate togbinary(val)
6617c478bd9Sstevel@tonic-gate 	int val;
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (val >= 0) {
6667c478bd9Sstevel@tonic-gate 		binmode = val;
6677c478bd9Sstevel@tonic-gate 	} else {
6687c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY) &&
6697c478bd9Sstevel@tonic-gate 		    my_want_state_is_do(TELOPT_BINARY)) {
6707c478bd9Sstevel@tonic-gate 			binmode = 1;
6717c478bd9Sstevel@tonic-gate 		} else if (my_want_state_is_wont(TELOPT_BINARY) &&
6727c478bd9Sstevel@tonic-gate 		    my_want_state_is_dont(TELOPT_BINARY)) {
6737c478bd9Sstevel@tonic-gate 			binmode = 0;
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 		val = binmode ? 0 : 1;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (val == 1) {
6797c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY) &&
6807c478bd9Sstevel@tonic-gate 		    my_want_state_is_do(TELOPT_BINARY)) {
6817c478bd9Sstevel@tonic-gate 			(void) printf("Already operating in binary mode "
6827c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6837c478bd9Sstevel@tonic-gate 		} else {
6847c478bd9Sstevel@tonic-gate 			(void) printf(
6857c478bd9Sstevel@tonic-gate 			    "Negotiating binary mode with remote host.\n");
6867c478bd9Sstevel@tonic-gate 			tel_enter_binary(3);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 	} else {
6897c478bd9Sstevel@tonic-gate 		if (my_want_state_is_wont(TELOPT_BINARY) &&
6907c478bd9Sstevel@tonic-gate 		    my_want_state_is_dont(TELOPT_BINARY)) {
6917c478bd9Sstevel@tonic-gate 			(void) printf("Already in network ascii mode "
6927c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6937c478bd9Sstevel@tonic-gate 		} else {
6947c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating network ascii mode "
6957c478bd9Sstevel@tonic-gate 			    "with remote host.\n");
6967c478bd9Sstevel@tonic-gate 			tel_leave_binary(3);
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	return (1);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate static int
togrbinary(val)7037c478bd9Sstevel@tonic-gate togrbinary(val)
7047c478bd9Sstevel@tonic-gate 	int val;
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (val == -1)
7097c478bd9Sstevel@tonic-gate 		val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (val == 1) {
7127c478bd9Sstevel@tonic-gate 		if (my_want_state_is_do(TELOPT_BINARY)) {
7137c478bd9Sstevel@tonic-gate 			(void) printf("Already receiving in binary mode.\n");
7147c478bd9Sstevel@tonic-gate 		} else {
7157c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating binary mode on input.\n");
7167c478bd9Sstevel@tonic-gate 			tel_enter_binary(1);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	} else {
7197c478bd9Sstevel@tonic-gate 		if (my_want_state_is_dont(TELOPT_BINARY)) {
7207c478bd9Sstevel@tonic-gate 			(void) printf(
7217c478bd9Sstevel@tonic-gate 			    "Already receiving in network ascii mode.\n");
7227c478bd9Sstevel@tonic-gate 		} else {
7237c478bd9Sstevel@tonic-gate 			(void) printf(
7247c478bd9Sstevel@tonic-gate 			    "Negotiating network ascii mode on input.\n");
7257c478bd9Sstevel@tonic-gate 			    tel_leave_binary(1);
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	return (1);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static int
togxbinary(val)7327c478bd9Sstevel@tonic-gate togxbinary(val)
7337c478bd9Sstevel@tonic-gate 	int val;
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	donebinarytoggle = 1;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (val == -1)
7387c478bd9Sstevel@tonic-gate 		val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (val == 1) {
7417c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_BINARY)) {
7427c478bd9Sstevel@tonic-gate 			(void) printf("Already transmitting in binary mode.\n");
7437c478bd9Sstevel@tonic-gate 		} else {
7447c478bd9Sstevel@tonic-gate 			(void) printf("Negotiating binary mode on output.\n");
7457c478bd9Sstevel@tonic-gate 			tel_enter_binary(2);
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 	} else {
7487c478bd9Sstevel@tonic-gate 		if (my_want_state_is_wont(TELOPT_BINARY)) {
7497c478bd9Sstevel@tonic-gate 			(void) printf(
7507c478bd9Sstevel@tonic-gate 			    "Already transmitting in network ascii mode.\n");
7517c478bd9Sstevel@tonic-gate 		} else {
7527c478bd9Sstevel@tonic-gate 			(void) printf(
7537c478bd9Sstevel@tonic-gate 			    "Negotiating network ascii mode on output.\n");
7547c478bd9Sstevel@tonic-gate 			tel_leave_binary(2);
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	return (1);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate static int togglehelp(void);
7627c478bd9Sstevel@tonic-gate extern int auth_togdebug(int);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate struct togglelist {
7657c478bd9Sstevel@tonic-gate 	char	*name;		/* name of toggle */
7667c478bd9Sstevel@tonic-gate 	char	*help;		/* help message */
7677c478bd9Sstevel@tonic-gate 	int	(*handler)();	/* routine to do actual setting */
7687c478bd9Sstevel@tonic-gate 	int	*variable;
7697c478bd9Sstevel@tonic-gate 	char	*actionexplanation;
7707c478bd9Sstevel@tonic-gate };
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate static struct togglelist Togglelist[] = {
7737c478bd9Sstevel@tonic-gate 	{ "autoflush",
7747c478bd9Sstevel@tonic-gate 	"flushing of output when sending interrupt characters",
7757c478bd9Sstevel@tonic-gate 	    0,
7767c478bd9Sstevel@tonic-gate 		&autoflush,
7777c478bd9Sstevel@tonic-gate 		    "flush output when sending interrupt characters" },
7787c478bd9Sstevel@tonic-gate 	{ "autosynch",
7797c478bd9Sstevel@tonic-gate 	"automatic sending of interrupt characters in urgent mode",
7807c478bd9Sstevel@tonic-gate 	    0,
7817c478bd9Sstevel@tonic-gate 		&autosynch,
7827c478bd9Sstevel@tonic-gate 		    "send interrupt characters in urgent mode" },
7837c478bd9Sstevel@tonic-gate 	{ "autologin",
7847c478bd9Sstevel@tonic-gate 	"automatic sending of login and/or authentication info",
7857c478bd9Sstevel@tonic-gate 	    0,
7867c478bd9Sstevel@tonic-gate 		&autologin,
7877c478bd9Sstevel@tonic-gate 		    "send login name and/or authentication information" },
7887c478bd9Sstevel@tonic-gate 	{ "authdebug",
7897c478bd9Sstevel@tonic-gate 	"authentication debugging",
7907c478bd9Sstevel@tonic-gate 	    auth_togdebug,
7917c478bd9Sstevel@tonic-gate 		0,
7927c478bd9Sstevel@tonic-gate 		    "print authentication debugging information" },
7937c478bd9Sstevel@tonic-gate 	{ "autoencrypt",
7947c478bd9Sstevel@tonic-gate 	"automatic encryption of data stream",
7957c478bd9Sstevel@tonic-gate 	    EncryptAutoEnc,
7967c478bd9Sstevel@tonic-gate 		0,
7977c478bd9Sstevel@tonic-gate 		    "automatically encrypt output" },
7987c478bd9Sstevel@tonic-gate 	{ "autodecrypt",
7997c478bd9Sstevel@tonic-gate 	"automatic decryption of data stream",
8007c478bd9Sstevel@tonic-gate 	    EncryptAutoDec,
8017c478bd9Sstevel@tonic-gate 		0,
8027c478bd9Sstevel@tonic-gate 		    "automatically decrypt input" },
8037c478bd9Sstevel@tonic-gate 	{ "verbose_encrypt",
8047c478bd9Sstevel@tonic-gate 	"verbose encryption output",
8057c478bd9Sstevel@tonic-gate 	    EncryptVerbose,
8067c478bd9Sstevel@tonic-gate 		0,
8077c478bd9Sstevel@tonic-gate 		    "print verbose encryption output" },
8087c478bd9Sstevel@tonic-gate 	{ "encdebug",
8097c478bd9Sstevel@tonic-gate 	"encryption debugging",
8107c478bd9Sstevel@tonic-gate 	    EncryptDebug,
8117c478bd9Sstevel@tonic-gate 		0,
8127c478bd9Sstevel@tonic-gate 		    "print encryption debugging information" },
8137c478bd9Sstevel@tonic-gate 	{ "skiprc",
8147c478bd9Sstevel@tonic-gate 	"don't read ~/.telnetrc file",
8157c478bd9Sstevel@tonic-gate 	    0,
8167c478bd9Sstevel@tonic-gate 		&skiprc,
8177c478bd9Sstevel@tonic-gate 		    "skip reading of ~/.telnetrc file" },
8187c478bd9Sstevel@tonic-gate 	{ "binary",
8197c478bd9Sstevel@tonic-gate 	"sending and receiving of binary data",
8207c478bd9Sstevel@tonic-gate 	    togbinary,
8217c478bd9Sstevel@tonic-gate 		0,
8227c478bd9Sstevel@tonic-gate 		    0 },
8237c478bd9Sstevel@tonic-gate 	{ "inbinary",
8247c478bd9Sstevel@tonic-gate 	"receiving of binary data",
8257c478bd9Sstevel@tonic-gate 	    togrbinary,
8267c478bd9Sstevel@tonic-gate 		0,
8277c478bd9Sstevel@tonic-gate 		    0 },
8287c478bd9Sstevel@tonic-gate 	{ "outbinary",
8297c478bd9Sstevel@tonic-gate 	"sending of binary data",
8307c478bd9Sstevel@tonic-gate 	    togxbinary,
8317c478bd9Sstevel@tonic-gate 		0,
8327c478bd9Sstevel@tonic-gate 		    0 },
8337c478bd9Sstevel@tonic-gate 	{ "crlf",
8347c478bd9Sstevel@tonic-gate 	"sending carriage returns as telnet <CR><LF>",
8357c478bd9Sstevel@tonic-gate 	    togcrlf,
8367c478bd9Sstevel@tonic-gate 		&crlf,
8377c478bd9Sstevel@tonic-gate 		    0 },
8387c478bd9Sstevel@tonic-gate 	{ "crmod",
8397c478bd9Sstevel@tonic-gate 	"mapping of received carriage returns",
8407c478bd9Sstevel@tonic-gate 	    0,
8417c478bd9Sstevel@tonic-gate 		&crmod,
8427c478bd9Sstevel@tonic-gate 		    "map carriage return on output" },
8437c478bd9Sstevel@tonic-gate 	{ "localchars",
8447c478bd9Sstevel@tonic-gate 	"local recognition of certain control characters",
8457c478bd9Sstevel@tonic-gate 	    lclchars,
8467c478bd9Sstevel@tonic-gate 		&localchars,
8477c478bd9Sstevel@tonic-gate 		    "recognize certain control characters" },
8487c478bd9Sstevel@tonic-gate 	{ " ", "", 0 },		/* empty line */
8497c478bd9Sstevel@tonic-gate 	{ "debug",
8507c478bd9Sstevel@tonic-gate 	"debugging",
8517c478bd9Sstevel@tonic-gate 	    togdebug,
8527c478bd9Sstevel@tonic-gate 		&debug,
8537c478bd9Sstevel@tonic-gate 		    "turn on socket level debugging" },
8547c478bd9Sstevel@tonic-gate 	{ "netdata",
8557c478bd9Sstevel@tonic-gate 	"printing of hexadecimal network data (debugging)",
8567c478bd9Sstevel@tonic-gate 	    0,
8577c478bd9Sstevel@tonic-gate 		&netdata,
8587c478bd9Sstevel@tonic-gate 		    "print hexadecimal representation of network traffic" },
8597c478bd9Sstevel@tonic-gate 	{ "prettydump",
8607c478bd9Sstevel@tonic-gate 	"output of \"netdata\" to user readable format (debugging)",
8617c478bd9Sstevel@tonic-gate 	    0,
8627c478bd9Sstevel@tonic-gate 		&prettydump,
8637c478bd9Sstevel@tonic-gate 		    "print user readable output for \"netdata\"" },
8647c478bd9Sstevel@tonic-gate 	{ "options",
8657c478bd9Sstevel@tonic-gate 	"viewing of options processing (debugging)",
8667c478bd9Sstevel@tonic-gate 	    0,
8677c478bd9Sstevel@tonic-gate 		&showoptions,
8687c478bd9Sstevel@tonic-gate 		    "show option processing" },
8697c478bd9Sstevel@tonic-gate 	{ "termdata",
8707c478bd9Sstevel@tonic-gate 	"(debugging) toggle printing of hexadecimal terminal data",
8717c478bd9Sstevel@tonic-gate 	    0,
8727c478bd9Sstevel@tonic-gate 		&termdata,
8737c478bd9Sstevel@tonic-gate 		    "print hexadecimal representation of terminal traffic" },
8747c478bd9Sstevel@tonic-gate 	{ "?",
8757c478bd9Sstevel@tonic-gate 	0,
8767c478bd9Sstevel@tonic-gate 	    togglehelp },
8777c478bd9Sstevel@tonic-gate 	{ "help",
8787c478bd9Sstevel@tonic-gate 	0,
8797c478bd9Sstevel@tonic-gate 	    togglehelp },
8807c478bd9Sstevel@tonic-gate 	{ 0 }
8817c478bd9Sstevel@tonic-gate };
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate static int
togglehelp()8847c478bd9Sstevel@tonic-gate togglehelp()
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	struct togglelist *c;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	for (c = Togglelist; c->name; c++) {
8897c478bd9Sstevel@tonic-gate 		if (c->help) {
8907c478bd9Sstevel@tonic-gate 			if (*c->help)
8917c478bd9Sstevel@tonic-gate 				(void) printf(
8927c478bd9Sstevel@tonic-gate 					"%-15s toggle %s\n", c->name, c->help);
8937c478bd9Sstevel@tonic-gate 			else
8947c478bd9Sstevel@tonic-gate 				(void) printf("\n");
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	(void) printf("\n");
8987c478bd9Sstevel@tonic-gate 	(void) printf("%-15s %s\n", "?", "display help information");
8997c478bd9Sstevel@tonic-gate 	return (0);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate static void
settogglehelp(set)9037c478bd9Sstevel@tonic-gate settogglehelp(set)
9047c478bd9Sstevel@tonic-gate 	int set;
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	struct togglelist *c;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	for (c = Togglelist; c->name; c++) {
9097c478bd9Sstevel@tonic-gate 		if (c->help) {
9107c478bd9Sstevel@tonic-gate 			if (*c->help)
9117c478bd9Sstevel@tonic-gate 				(void) printf("%-15s %s %s\n", c->name,
9127c478bd9Sstevel@tonic-gate 				    set ? "enable" : "disable", c->help);
9137c478bd9Sstevel@tonic-gate 			else
9147c478bd9Sstevel@tonic-gate 				(void) printf("\n");
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate #define	GETTOGGLE(name) (struct togglelist *) \
9207c478bd9Sstevel@tonic-gate 		genget(name, (char **)Togglelist, sizeof (struct togglelist))
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate static int
toggle(argc,argv)9237c478bd9Sstevel@tonic-gate toggle(argc, argv)
9247c478bd9Sstevel@tonic-gate 	int  argc;
9257c478bd9Sstevel@tonic-gate 	char *argv[];
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	int retval = 1;
9287c478bd9Sstevel@tonic-gate 	char *name;
9297c478bd9Sstevel@tonic-gate 	struct togglelist *c;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (argc < 2) {
9327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9337c478bd9Sstevel@tonic-gate 		    "Need an argument to 'toggle' command.  "
9347c478bd9Sstevel@tonic-gate 		    "'toggle ?' for help.\n");
9357c478bd9Sstevel@tonic-gate 		return (0);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 	argc--;
9387c478bd9Sstevel@tonic-gate 	argv++;
9397c478bd9Sstevel@tonic-gate 	while (argc--) {
9407c478bd9Sstevel@tonic-gate 		name = *argv++;
9417c478bd9Sstevel@tonic-gate 		c = GETTOGGLE(name);
9427c478bd9Sstevel@tonic-gate 		if (Ambiguous(c)) {
9437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
9447c478bd9Sstevel@tonic-gate 			    "('toggle ?' for help).\n", name);
9457c478bd9Sstevel@tonic-gate 			return (0);
9467c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
9477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': unknown argument "
9487c478bd9Sstevel@tonic-gate 			    "('toggle ?' for help).\n", name);
9497c478bd9Sstevel@tonic-gate 			return (0);
9507c478bd9Sstevel@tonic-gate 		} else {
9517c478bd9Sstevel@tonic-gate 			if (c->variable) {
9527c478bd9Sstevel@tonic-gate 				*c->variable = !*c->variable;	/* invert it */
9537c478bd9Sstevel@tonic-gate 				if (c->actionexplanation) {
9547c478bd9Sstevel@tonic-gate 			(void) printf("%s %s.\n",
9557c478bd9Sstevel@tonic-gate 				*c->variable ? "Will" : "Won't",
9567c478bd9Sstevel@tonic-gate 							c->actionexplanation);
9577c478bd9Sstevel@tonic-gate 				}
9587c478bd9Sstevel@tonic-gate 			}
9597c478bd9Sstevel@tonic-gate 			if (c->handler) {
9607c478bd9Sstevel@tonic-gate 				retval &= (*c->handler)(-1);
9617c478bd9Sstevel@tonic-gate 			}
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	return (retval);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate  * The following perform the "set" command.
9697c478bd9Sstevel@tonic-gate  */
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate #ifdef	USE_TERMIO
9727c478bd9Sstevel@tonic-gate struct termio new_tc = { 0 };
9737c478bd9Sstevel@tonic-gate #endif
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate struct setlist {
9767c478bd9Sstevel@tonic-gate 	char *name;		/* name */
9777c478bd9Sstevel@tonic-gate 	char *help;		/* help information */
9787c478bd9Sstevel@tonic-gate 	void (*handler)();
9797c478bd9Sstevel@tonic-gate 	cc_t *charp;		/* where it is located at */
9807c478bd9Sstevel@tonic-gate };
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate static struct setlist Setlist[] = {
9837c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
9847c478bd9Sstevel@tonic-gate 	{ "echo",  "character to toggle local echoing on/off", 0, &echoc },
9857c478bd9Sstevel@tonic-gate #endif
9867c478bd9Sstevel@tonic-gate 	{ "escape", "character to escape back to telnet command mode", 0,
9877c478bd9Sstevel@tonic-gate 	    &escape },
9887c478bd9Sstevel@tonic-gate 	{ "rlogin", "rlogin escape character", 0, &rlogin },
9897c478bd9Sstevel@tonic-gate 	{ "tracefile", "file to write trace information to", SetNetTrace,
9907c478bd9Sstevel@tonic-gate 	    (cc_t *)NetTraceFile},
9917c478bd9Sstevel@tonic-gate 	{ " ", "" },
9927c478bd9Sstevel@tonic-gate 	{ " ", "The following need 'localchars' to be toggled true", 0, 0 },
9937c478bd9Sstevel@tonic-gate 	{ "flushoutput", "character to cause an Abort Output", 0,
9947c478bd9Sstevel@tonic-gate 	    termFlushCharp },
9957c478bd9Sstevel@tonic-gate 	{ "interrupt", "character to cause an Interrupt Process", 0,
9967c478bd9Sstevel@tonic-gate 	    termIntCharp },
9977c478bd9Sstevel@tonic-gate 	{ "quit", "character to cause an Abort process", 0, termQuitCharp },
9987c478bd9Sstevel@tonic-gate 	{ "eof", "character to cause an EOF ", 0, termEofCharp },
9997c478bd9Sstevel@tonic-gate 	{ " ", "" },
10007c478bd9Sstevel@tonic-gate 	{ " ", "The following are for local editing in linemode", 0, 0 },
10017c478bd9Sstevel@tonic-gate 	{ "erase", "character to use to erase a character", 0, termEraseCharp },
10027c478bd9Sstevel@tonic-gate 	{ "kill", "character to use to erase a line", 0, termKillCharp },
10037c478bd9Sstevel@tonic-gate 	{ "lnext", "character to use for literal next", 0,
10047c478bd9Sstevel@tonic-gate 	    termLiteralNextCharp },
10057c478bd9Sstevel@tonic-gate 	{ "susp", "character to cause a Suspend Process", 0, termSuspCharp },
10067c478bd9Sstevel@tonic-gate 	{ "reprint", "character to use for line reprint", 0, termRprntCharp },
10077c478bd9Sstevel@tonic-gate 	{ "worderase", "character to use to erase a word", 0, termWerasCharp },
10087c478bd9Sstevel@tonic-gate 	{ "start",	"character to use for XON", 0, termStartCharp },
10097c478bd9Sstevel@tonic-gate 	{ "stop",	"character to use for XOFF", 0, termStopCharp },
10107c478bd9Sstevel@tonic-gate 	{ "forw1",	"alternate end of line character", 0, termForw1Charp },
10117c478bd9Sstevel@tonic-gate 	{ "forw2",	"alternate end of line character", 0, termForw2Charp },
10127c478bd9Sstevel@tonic-gate 	{ "ayt",	"alternate AYT character", 0, termAytCharp },
10137c478bd9Sstevel@tonic-gate 	{ 0 }
10147c478bd9Sstevel@tonic-gate };
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate static struct setlist *
getset(name)10177c478bd9Sstevel@tonic-gate getset(name)
10187c478bd9Sstevel@tonic-gate     char *name;
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	return ((struct setlist *)
10217c478bd9Sstevel@tonic-gate 	    genget(name, (char **)Setlist, sizeof (struct setlist)));
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate     void
set_escape_char(s)10257c478bd9Sstevel@tonic-gate set_escape_char(s)
10267c478bd9Sstevel@tonic-gate     char *s;
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	if (rlogin != _POSIX_VDISABLE) {
10297c478bd9Sstevel@tonic-gate 		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
10307c478bd9Sstevel@tonic-gate 		(void) printf("Telnet rlogin escape character is '%s'.\n",
10317c478bd9Sstevel@tonic-gate 					control(rlogin));
10327c478bd9Sstevel@tonic-gate 	} else {
10337c478bd9Sstevel@tonic-gate 		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
10347c478bd9Sstevel@tonic-gate 		(void) printf("Telnet escape character is '%s'.\n",
10357c478bd9Sstevel@tonic-gate 		    esc_control(escape));
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate static int
setcmd(argc,argv)10407c478bd9Sstevel@tonic-gate setcmd(argc, argv)
10417c478bd9Sstevel@tonic-gate 	int  argc;
10427c478bd9Sstevel@tonic-gate 	char *argv[];
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	int value;
10457c478bd9Sstevel@tonic-gate 	struct setlist *ct;
10467c478bd9Sstevel@tonic-gate 	struct togglelist *c;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (argc < 2 || argc > 3) {
10497c478bd9Sstevel@tonic-gate 		(void) printf(
10507c478bd9Sstevel@tonic-gate 			"Format is 'set Name Value'\n'set ?' for help.\n");
10517c478bd9Sstevel@tonic-gate 		return (0);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 	if ((argc == 2) &&
10547c478bd9Sstevel@tonic-gate 	    (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
10557c478bd9Sstevel@tonic-gate 		for (ct = Setlist; ct->name; ct++)
10567c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", ct->name, ct->help);
10577c478bd9Sstevel@tonic-gate 		(void) printf("\n");
10587c478bd9Sstevel@tonic-gate 		settogglehelp(1);
10597c478bd9Sstevel@tonic-gate 		(void) printf("%-15s %s\n", "?", "display help information");
10607c478bd9Sstevel@tonic-gate 		return (0);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	ct = getset(argv[1]);
10647c478bd9Sstevel@tonic-gate 	if (ct == 0) {
10657c478bd9Sstevel@tonic-gate 		c = GETTOGGLE(argv[1]);
10667c478bd9Sstevel@tonic-gate 		if (c == 0) {
10677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': unknown argument "
10687c478bd9Sstevel@tonic-gate 			    "('set ?' for help).\n", argv[1]);
10697c478bd9Sstevel@tonic-gate 			return (0);
10707c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(c)) {
10717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
10727c478bd9Sstevel@tonic-gate 			    "('set ?' for help).\n", argv[1]);
10737c478bd9Sstevel@tonic-gate 			return (0);
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 		if (c->variable) {
10767c478bd9Sstevel@tonic-gate 			if ((argc == 2) || (strcmp("on", argv[2]) == 0))
10777c478bd9Sstevel@tonic-gate 				*c->variable = 1;
10787c478bd9Sstevel@tonic-gate 			else if (strcmp("off", argv[2]) == 0)
10797c478bd9Sstevel@tonic-gate 				*c->variable = 0;
10807c478bd9Sstevel@tonic-gate 			else {
10817c478bd9Sstevel@tonic-gate 				(void) printf(
10827c478bd9Sstevel@tonic-gate 				    "Format is 'set togglename [on|off]'\n"
10837c478bd9Sstevel@tonic-gate 				    "'set ?' for help.\n");
10847c478bd9Sstevel@tonic-gate 				return (0);
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 			if (c->actionexplanation) {
10877c478bd9Sstevel@tonic-gate 				(void) printf("%s %s.\n",
10887c478bd9Sstevel@tonic-gate 				    *c->variable? "Will" : "Won't",
10897c478bd9Sstevel@tonic-gate 				    c->actionexplanation);
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 		if (c->handler)
10937c478bd9Sstevel@tonic-gate 			(*c->handler)(1);
10947c478bd9Sstevel@tonic-gate 	} else if (argc != 3) {
10957c478bd9Sstevel@tonic-gate 		(void) printf(
10967c478bd9Sstevel@tonic-gate 			"Format is 'set Name Value'\n'set ?' for help.\n");
10977c478bd9Sstevel@tonic-gate 		return (0);
10987c478bd9Sstevel@tonic-gate 	} else if (Ambiguous(ct)) {
10997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11007c478bd9Sstevel@tonic-gate 		    "'%s': ambiguous argument ('set ?' for help).\n", argv[1]);
11017c478bd9Sstevel@tonic-gate 		return (0);
11027c478bd9Sstevel@tonic-gate 	} else if (ct->handler) {
11037c478bd9Sstevel@tonic-gate 		(*ct->handler)(argv[2]);
11047c478bd9Sstevel@tonic-gate 		(void) printf(
11057c478bd9Sstevel@tonic-gate 		    "%s set to \"%s\".\n", ct->name, (char *)ct->charp);
11067c478bd9Sstevel@tonic-gate 	} else {
11077c478bd9Sstevel@tonic-gate 		if (strcmp("off", argv[2])) {
11087c478bd9Sstevel@tonic-gate 			value = special(argv[2]);
11097c478bd9Sstevel@tonic-gate 		} else {
11107c478bd9Sstevel@tonic-gate 			value = _POSIX_VDISABLE;
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		*(ct->charp) = (cc_t)value;
11137c478bd9Sstevel@tonic-gate 		(void) printf("%s character is '%s'.\n", ct->name,
11147c478bd9Sstevel@tonic-gate 		    control(*(ct->charp)));
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	slc_check();
11177c478bd9Sstevel@tonic-gate 	return (1);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate static int
unsetcmd(argc,argv)11217c478bd9Sstevel@tonic-gate unsetcmd(argc, argv)
11227c478bd9Sstevel@tonic-gate 	int  argc;
11237c478bd9Sstevel@tonic-gate 	char *argv[];
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	struct setlist *ct;
11267c478bd9Sstevel@tonic-gate 	struct togglelist *c;
11277c478bd9Sstevel@tonic-gate 	register char *name;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (argc < 2) {
11307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Need an argument to 'unset' command.  "
11317c478bd9Sstevel@tonic-gate 		    "'unset ?' for help.\n");
11327c478bd9Sstevel@tonic-gate 		return (0);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
11357c478bd9Sstevel@tonic-gate 		for (ct = Setlist; ct->name; ct++)
11367c478bd9Sstevel@tonic-gate 			(void) printf("%-15s %s\n", ct->name, ct->help);
11377c478bd9Sstevel@tonic-gate 		(void) printf("\n");
11387c478bd9Sstevel@tonic-gate 		settogglehelp(0);
11397c478bd9Sstevel@tonic-gate 		(void) printf("%-15s %s\n", "?", "display help information");
11407c478bd9Sstevel@tonic-gate 		return (0);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	argc--;
11447c478bd9Sstevel@tonic-gate 	argv++;
11457c478bd9Sstevel@tonic-gate 	while (argc--) {
11467c478bd9Sstevel@tonic-gate 		name = *argv++;
11477c478bd9Sstevel@tonic-gate 		ct = getset(name);
11487c478bd9Sstevel@tonic-gate 		if (ct == 0) {
11497c478bd9Sstevel@tonic-gate 			c = GETTOGGLE(name);
11507c478bd9Sstevel@tonic-gate 			if (c == 0) {
11517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s': unknown argument "
11527c478bd9Sstevel@tonic-gate 				    "('unset ?' for help).\n", name);
11537c478bd9Sstevel@tonic-gate 				return (0);
11547c478bd9Sstevel@tonic-gate 			} else if (Ambiguous(c)) {
11557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11567c478bd9Sstevel@tonic-gate 				    "'%s': ambiguous argument "
11577c478bd9Sstevel@tonic-gate 				    "('unset ?' for help).\n", name);
11587c478bd9Sstevel@tonic-gate 				return (0);
11597c478bd9Sstevel@tonic-gate 			}
11607c478bd9Sstevel@tonic-gate 			if (c->variable) {
11617c478bd9Sstevel@tonic-gate 				*c->variable = 0;
11627c478bd9Sstevel@tonic-gate 				if (c->actionexplanation) {
11637c478bd9Sstevel@tonic-gate 					(void) printf("%s %s.\n",
11647c478bd9Sstevel@tonic-gate 					    *c->variable? "Will" : "Won't",
11657c478bd9Sstevel@tonic-gate 					    c->actionexplanation);
11667c478bd9Sstevel@tonic-gate 				}
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 			if (c->handler)
11697c478bd9Sstevel@tonic-gate 				(*c->handler)(0);
11707c478bd9Sstevel@tonic-gate 		} else if (Ambiguous(ct)) {
11717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "'%s': ambiguous argument "
11727c478bd9Sstevel@tonic-gate 			    "('unset ?' for help).\n", name);
11737c478bd9Sstevel@tonic-gate 			return (0);
11747c478bd9Sstevel@tonic-gate 		} else if (ct->handler) {
11757c478bd9Sstevel@tonic-gate 			(*ct->handler)(0);
11767c478bd9Sstevel@tonic-gate 			(void) printf("%s reset to \"%s\".\n", ct->name,
11777c478bd9Sstevel@tonic-gate 			    (char *)ct->charp);
11787c478bd9Sstevel@tonic-gate 		} else {
11797c478bd9Sstevel@tonic-gate 			*(ct->charp) = _POSIX_VDISABLE;
11807c478bd9Sstevel@tonic-gate 			(void) printf("%s character is '%s'.\n", ct->name,
11817c478bd9Sstevel@tonic-gate 			    control(*(ct->charp)));
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	return (1);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * The following are the data structures and routines for the
11897c478bd9Sstevel@tonic-gate  * 'mode' command.
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate extern int reqd_linemode;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
11947c478bd9Sstevel@tonic-gate extern int kludgelinemode;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static int
dokludgemode()11977c478bd9Sstevel@tonic-gate dokludgemode()
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	kludgelinemode = 1;
12007c478bd9Sstevel@tonic-gate 	send_wont(TELOPT_LINEMODE, 1);
12017c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_SGA, 1);
12027c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_ECHO, 1);
12037c478bd9Sstevel@tonic-gate 	/*
12047c478bd9Sstevel@tonic-gate 	 * If processing the .telnetrc file, keep track of linemode and/or
12057c478bd9Sstevel@tonic-gate 	 * kludgelinemode requests which are processed before initial option
12067c478bd9Sstevel@tonic-gate 	 * negotiations occur.
12077c478bd9Sstevel@tonic-gate 	 */
12087c478bd9Sstevel@tonic-gate 	if (doing_rc)
12097c478bd9Sstevel@tonic-gate 		reqd_linemode = 1;
12107c478bd9Sstevel@tonic-gate 	return (1);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate #endif
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate static int
dolinemode()12157c478bd9Sstevel@tonic-gate dolinemode()
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12187c478bd9Sstevel@tonic-gate 	if (kludgelinemode)
12197c478bd9Sstevel@tonic-gate 		send_dont(TELOPT_SGA, 1);
12207c478bd9Sstevel@tonic-gate #endif
12217c478bd9Sstevel@tonic-gate 	send_will(TELOPT_LINEMODE, 1);
12227c478bd9Sstevel@tonic-gate 	send_dont(TELOPT_ECHO, 1);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	/*
12257c478bd9Sstevel@tonic-gate 	 * If processing the .telnetrc file, keep track of linemode and/or
12267c478bd9Sstevel@tonic-gate 	 * kludgelinemode requests which are processed before initial option
12277c478bd9Sstevel@tonic-gate 	 * negotiations occur.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 	if (doing_rc)
12307c478bd9Sstevel@tonic-gate 		reqd_linemode = 1;
12317c478bd9Sstevel@tonic-gate 	return (1);
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate static int
docharmode()12357c478bd9Sstevel@tonic-gate docharmode()
12367c478bd9Sstevel@tonic-gate {
12377c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
12387c478bd9Sstevel@tonic-gate 	if (kludgelinemode)
12397c478bd9Sstevel@tonic-gate 		send_do(TELOPT_SGA, 1);
12407c478bd9Sstevel@tonic-gate 	else
12417c478bd9Sstevel@tonic-gate #endif
12427c478bd9Sstevel@tonic-gate 		send_wont(TELOPT_LINEMODE, 1);
12437c478bd9Sstevel@tonic-gate 	send_do(TELOPT_ECHO, 1);
12447c478bd9Sstevel@tonic-gate 	reqd_linemode = 0;
12457c478bd9Sstevel@tonic-gate 	return (1);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate