xref: /csrg-svn/usr.bin/telnet/commands.c (revision 45008)
133685Sbostic /*
233685Sbostic  * Copyright (c) 1988 Regents of the University of California.
333685Sbostic  * All rights reserved.
433685Sbostic  *
542770Sbostic  * %sccs.include.redist.c%
633685Sbostic  */
733685Sbostic 
833685Sbostic #ifndef lint
9*45008Skarels static char sccsid[] = "@(#)commands.c	1.31 (Berkeley) 07/27/90";
1033685Sbostic #endif /* not lint */
1133685Sbostic 
1244361Sborman #include <sys/types.h>
1336274Sminshall #if	defined(unix)
1436274Sminshall #include <sys/file.h>
1536274Sminshall #endif	/* defined(unix) */
1632144Sminshall #include <sys/socket.h>
1732144Sminshall #include <netinet/in.h>
1838689Sborman #ifdef	CRAY
1938689Sborman #include <sys/fcntl.h>
2038810Sborman #endif	/* CRAY */
2132144Sminshall 
2232144Sminshall #include <signal.h>
2332144Sminshall #include <netdb.h>
2432144Sminshall #include <ctype.h>
25*45008Skarels #include <pwd.h>
2635298Sminshall #include <varargs.h>
2732144Sminshall 
2832144Sminshall #include <arpa/telnet.h>
2932144Sminshall 
3034305Sminshall #include "general.h"
3134305Sminshall 
3232381Sminshall #include "ring.h"
3332381Sminshall 
3432144Sminshall #include "externs.h"
3532144Sminshall #include "defines.h"
3632144Sminshall #include "types.h"
3732144Sminshall 
3844361Sborman #ifdef	SRCRT
3944361Sborman # ifndef CRAY
4044361Sborman # include <netinet/in_systm.h>
4144361Sborman #  if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
4244361Sborman #  include <machine/endian.h>
4344361Sborman #  endif /* vax */
4444361Sborman # endif /* CRAY */
4544361Sborman #include <netinet/ip.h>
4644361Sborman #endif /* SRCRT */
4738689Sborman 
4840248Sborman #if defined(CRAY) && defined(IP_TOS) && !defined(HAS_IP_TOS)
4944361Sborman # define HAS_IP_TOS
5040248Sborman #endif
5138689Sborman 
5240248Sborman 
5332144Sminshall char	*hostname;
5438689Sborman extern char *getenv();
5532144Sminshall 
5636180Sminshall #define Ambiguous(s)	((char **)s == &ambiguous)
5732144Sminshall static char *ambiguous;		/* special return value for command routines */
5844361Sborman static call();
5932144Sminshall 
6032144Sminshall typedef struct {
6132144Sminshall 	char	*name;		/* command name */
6238689Sborman 	char	*help;		/* help string (NULL for no help) */
6332144Sminshall 	int	(*handler)();	/* routine which executes command */
6432144Sminshall 	int	needconnect;	/* Do we need to be connected to execute? */
6532144Sminshall } Command;
6632144Sminshall 
6738689Sborman static char line[256];
6838689Sborman static char saveline[256];
6932144Sminshall static int margc;
7032144Sminshall static char *margv[20];
7132144Sminshall 
7232144Sminshall /*
7332144Sminshall  * Various utility routines.
7432144Sminshall  */
7532144Sminshall 
7636180Sminshall #if	!defined(BSD) || (BSD <= 43)
7736180Sminshall 
7836180Sminshall char	*h_errlist[] = {
7936180Sminshall 	"Error 0",
8036180Sminshall 	"Unknown host",				/* 1 HOST_NOT_FOUND */
8136180Sminshall 	"Host name lookup failure",		/* 2 TRY_AGAIN */
8236180Sminshall 	"Unknown server error",			/* 3 NO_RECOVERY */
8336180Sminshall 	"No address associated with name",	/* 4 NO_ADDRESS */
8436180Sminshall };
8536180Sminshall int	h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
8636180Sminshall 
8736274Sminshall int h_errno;		/* In some version of SunOS this is necessary */
8836180Sminshall 
8936180Sminshall /*
9036180Sminshall  * herror --
9136180Sminshall  *	print the error indicated by the h_errno value.
9236180Sminshall  */
9336180Sminshall herror(s)
9436180Sminshall 	char *s;
9536180Sminshall {
9636180Sminshall 	if (s && *s) {
9736180Sminshall 		fprintf(stderr, "%s: ", s);
9836180Sminshall 	}
9936180Sminshall 	if ((h_errno < 0) || (h_errno >= h_nerr)) {
10036180Sminshall 		fprintf(stderr, "Unknown error\n");
10136274Sminshall 	} else if (h_errno == 0) {
10236274Sminshall #if	defined(sun)
10336274Sminshall 		fprintf(stderr, "Host unknown\n");
10436274Sminshall #endif	/* defined(sun) */
10536180Sminshall 	} else {
10636180Sminshall 		fprintf(stderr, "%s\n", h_errlist[h_errno]);
10736180Sminshall 	}
10836180Sminshall }
10936180Sminshall #endif	/* !define(BSD) || (BSD <= 43) */
11036180Sminshall 
11132144Sminshall static void
11232144Sminshall makeargv()
11332144Sminshall {
11444361Sborman     register char *cp, *cp2, c;
11532144Sminshall     register char **argp = margv;
11632144Sminshall 
11732144Sminshall     margc = 0;
11832144Sminshall     cp = line;
11932144Sminshall     if (*cp == '!') {		/* Special case shell escape */
12038689Sborman 	strcpy(saveline, line);	/* save for shell command */
12132144Sminshall 	*argp++ = "!";		/* No room in string to get this */
12232144Sminshall 	margc++;
12332144Sminshall 	cp++;
12432144Sminshall     }
12544361Sborman     while (c = *cp) {
12644361Sborman 	register int inquote = 0;
12744361Sborman 	while (isspace(c))
12844361Sborman 	    c = *++cp;
12944361Sborman 	if (c == '\0')
13032144Sminshall 	    break;
13132144Sminshall 	*argp++ = cp;
13232144Sminshall 	margc += 1;
13344361Sborman 	for (cp2 = cp; c != '\0'; c = *++cp) {
13444361Sborman 	    if (inquote) {
13544361Sborman 		if (c == inquote) {
13644361Sborman 		    inquote = 0;
13744361Sborman 		    continue;
13844361Sborman 		}
13944361Sborman 	    } else {
14044361Sborman 		if (c == '\\') {
14144361Sborman 		    if ((c = *++cp) == '\0')
14244361Sborman 			break;
14344361Sborman 		} else if (c == '"') {
14444361Sborman 		    inquote = '"';
14544361Sborman 		    continue;
14644361Sborman 		} else if (c == '\'') {
14744361Sborman 		    inquote = '\'';
14844361Sborman 		    continue;
14944361Sborman 		} else if (isspace(c))
15044361Sborman 		    break;
15144361Sborman 	    }
15244361Sborman 	    *cp2++ = c;
15344361Sborman 	}
15444361Sborman 	*cp2 = '\0';
15544361Sborman 	if (c == '\0')
15632144Sminshall 	    break;
15744361Sborman 	cp++;
15832144Sminshall     }
15932144Sminshall     *argp++ = 0;
16032144Sminshall }
16132144Sminshall 
16232144Sminshall 
16332144Sminshall static char **
16432144Sminshall genget(name, table, next)
16532144Sminshall char	*name;		/* name to match */
16632144Sminshall char	**table;		/* name entry in table */
16732144Sminshall char	**(*next)();	/* routine to return next entry in table */
16832144Sminshall {
16932144Sminshall 	register char *p, *q;
17032144Sminshall 	register char **c, **found;
17132144Sminshall 	register int nmatches, longest;
17232144Sminshall 
17332144Sminshall 	if (name == 0) {
17432144Sminshall 	    return 0;
17532144Sminshall 	}
17632144Sminshall 	longest = 0;
17732144Sminshall 	nmatches = 0;
17832144Sminshall 	found = 0;
17932144Sminshall 	for (c = table; (p = *c) != 0; c = (*next)(c)) {
18032144Sminshall 		for (q = name;
18132144Sminshall 		    (*q == *p) || (isupper(*q) && tolower(*q) == *p); p++, q++)
18232144Sminshall 			if (*q == 0)		/* exact match? */
18332144Sminshall 				return (c);
18432144Sminshall 		if (!*q) {			/* the name was a prefix */
18532144Sminshall 			if (q - name > longest) {
18632144Sminshall 				longest = q - name;
18732144Sminshall 				nmatches = 1;
18832144Sminshall 				found = c;
18932144Sminshall 			} else if (q - name == longest)
19032144Sminshall 				nmatches++;
19132144Sminshall 		}
19232144Sminshall 	}
19332144Sminshall 	if (nmatches > 1)
19436180Sminshall 		return &ambiguous;
19532144Sminshall 	return (found);
19632144Sminshall }
19732144Sminshall 
19832144Sminshall /*
19932144Sminshall  * Make a character string into a number.
20032144Sminshall  *
20132144Sminshall  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
20232144Sminshall  */
20332144Sminshall 
20432144Sminshall static
20532144Sminshall special(s)
20632144Sminshall register char *s;
20732144Sminshall {
20832144Sminshall 	register char c;
20932144Sminshall 	char b;
21032144Sminshall 
21132144Sminshall 	switch (*s) {
21232144Sminshall 	case '^':
21332144Sminshall 		b = *++s;
21432144Sminshall 		if (b == '?') {
21532144Sminshall 		    c = b | 0x40;		/* DEL */
21632144Sminshall 		} else {
21732144Sminshall 		    c = b & 0x1f;
21832144Sminshall 		}
21932144Sminshall 		break;
22032144Sminshall 	default:
22132144Sminshall 		c = *s;
22232144Sminshall 		break;
22332144Sminshall 	}
22432144Sminshall 	return c;
22532144Sminshall }
22632144Sminshall 
22732144Sminshall /*
22832144Sminshall  * Construct a control character sequence
22932144Sminshall  * for a special character.
23032144Sminshall  */
23132144Sminshall static char *
23232144Sminshall control(c)
23340245Sborman 	register cc_t c;
23432144Sminshall {
23544361Sborman 	static char buf[5];
23632144Sminshall 
23732144Sminshall 	if (c == 0x7f)
23832144Sminshall 		return ("^?");
23940245Sborman 	if (c == (cc_t)-1) {
24032144Sminshall 		return "off";
24132144Sminshall 	}
24244361Sborman 	if (c >= 0x80) {
24344361Sborman 		buf[0] = '\\';
24444361Sborman 		buf[1] = ((c>>6)&07) + '0';
24544361Sborman 		buf[2] = ((c>>3)&07) + '0';
24644361Sborman 		buf[3] = (c&07) + '0';
24744361Sborman 		buf[4] = 0;
24844361Sborman 	} else if (c >= 0x20) {
24932144Sminshall 		buf[0] = c;
25032144Sminshall 		buf[1] = 0;
25132144Sminshall 	} else {
25232144Sminshall 		buf[0] = '^';
25332144Sminshall 		buf[1] = '@'+c;
25432144Sminshall 		buf[2] = 0;
25532144Sminshall 	}
25632144Sminshall 	return (buf);
25732144Sminshall }
25832144Sminshall 
25932144Sminshall 
26032144Sminshall 
26132144Sminshall /*
26232144Sminshall  *	The following are data structures and routines for
26332144Sminshall  *	the "send" command.
26432144Sminshall  *
26532144Sminshall  */
26632144Sminshall 
26732144Sminshall struct sendlist {
26832144Sminshall     char	*name;		/* How user refers to it (case independent) */
26932144Sminshall     char	*help;		/* Help information (0 ==> no help) */
27032144Sminshall #if	defined(NOT43)
27138689Sborman     int		(*handler)();	/* Routine to perform (for special ops) */
27232144Sminshall #else	/* defined(NOT43) */
27338689Sborman     void	(*handler)();	/* Routine to perform (for special ops) */
27432144Sminshall #endif	/* defined(NOT43) */
27538689Sborman     int		what;		/* Character to be sent (<0 ==> special) */
27632144Sminshall };
27732144Sminshall 
27832144Sminshall #define	SENDQUESTION	-1
27932144Sminshall #define	SENDESCAPE	-3
28032144Sminshall 
28132144Sminshall static struct sendlist Sendlist[] = {
28238689Sborman     { "ao",	"Send Telnet Abort output",		0,	AO },
28338689Sborman     { "ayt",	"Send Telnet 'Are You There'",		0,	AYT },
28438689Sborman     { "brk",	"Send Telnet Break",			0,	BREAK },
28538689Sborman     { "ec",	"Send Telnet Erase Character",		0,	EC },
28638689Sborman     { "el",	"Send Telnet Erase Line",		0,	EL },
28738689Sborman     { "escape",	"Send current escape character",	0,	SENDESCAPE },
28838689Sborman     { "ga",	"Send Telnet 'Go Ahead' sequence",	0,	GA },
28938689Sborman     { "ip",	"Send Telnet Interrupt Process",	0,	IP },
29038689Sborman     { "nop",	"Send Telnet 'No operation'",		0,	NOP },
29138689Sborman     { "eor",	"Send Telnet 'End of Record'",		0,	EOR },
29238689Sborman     { "abort",	"Send Telnet 'Abort Process'",		0,	ABORT },
29338689Sborman     { "susp",	"Send Telnet 'Suspend Process'",	0,	SUSP },
29438689Sborman     { "eof",	"Send Telnet End of File Character",	0,	xEOF },
29538689Sborman     { "synch",	"Perform Telnet 'Synch operation'",	dosynch, SYNCH },
29638909Sborman     { "getstatus", "Send request for STATUS",		get_status, 0 },
29738689Sborman     { "?",	"Display send options",			0,	SENDQUESTION },
29832144Sminshall     { 0 }
29932144Sminshall };
30032144Sminshall 
30132144Sminshall static struct sendlist Sendlist2[] = {		/* some synonyms */
30238689Sborman     { "break",		0, 0, BREAK },
30332144Sminshall 
30438689Sborman     { "intp",		0, 0, IP },
30538689Sborman     { "interrupt",	0, 0, IP },
30638689Sborman     { "intr",		0, 0, IP },
30732144Sminshall 
30838689Sborman     { "help",		0, 0, SENDQUESTION },
30932144Sminshall 
31038689Sborman     { 0 }
31132144Sminshall };
31232144Sminshall 
31332144Sminshall static char **
31432144Sminshall getnextsend(name)
31532144Sminshall char *name;
31632144Sminshall {
31732144Sminshall     struct sendlist *c = (struct sendlist *) name;
31832144Sminshall 
31932144Sminshall     return (char **) (c+1);
32032144Sminshall }
32132144Sminshall 
32232144Sminshall static struct sendlist *
32332144Sminshall getsend(name)
32432144Sminshall char *name;
32532144Sminshall {
32632144Sminshall     struct sendlist *sl;
32732144Sminshall 
32832144Sminshall     if ((sl = (struct sendlist *)
32932144Sminshall 			genget(name, (char **) Sendlist, getnextsend)) != 0) {
33032144Sminshall 	return sl;
33132144Sminshall     } else {
33232144Sminshall 	return (struct sendlist *)
33332144Sminshall 				genget(name, (char **) Sendlist2, getnextsend);
33432144Sminshall     }
33532144Sminshall }
33632144Sminshall 
33732144Sminshall static
33832144Sminshall sendcmd(argc, argv)
33932144Sminshall int	argc;
34032144Sminshall char	**argv;
34132144Sminshall {
34232144Sminshall     int what;		/* what we are sending this time */
34332144Sminshall     int count;		/* how many bytes we are going to need to send */
34432144Sminshall     int i;
34532144Sminshall     int question = 0;	/* was at least one argument a question */
34632144Sminshall     struct sendlist *s;	/* pointer to current command */
34732144Sminshall 
34832144Sminshall     if (argc < 2) {
34932144Sminshall 	printf("need at least one argument for 'send' command\n");
35032144Sminshall 	printf("'send ?' for help\n");
35132144Sminshall 	return 0;
35232144Sminshall     }
35332144Sminshall     /*
35432144Sminshall      * First, validate all the send arguments.
35532144Sminshall      * In addition, we see how much space we are going to need, and
35632144Sminshall      * whether or not we will be doing a "SYNCH" operation (which
35732144Sminshall      * flushes the network queue).
35832144Sminshall      */
35932144Sminshall     count = 0;
36032144Sminshall     for (i = 1; i < argc; i++) {
36132144Sminshall 	s = getsend(argv[i]);
36232144Sminshall 	if (s == 0) {
36332144Sminshall 	    printf("Unknown send argument '%s'\n'send ?' for help.\n",
36432144Sminshall 			argv[i]);
36532144Sminshall 	    return 0;
36632144Sminshall 	} else if (Ambiguous(s)) {
36732144Sminshall 	    printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
36832144Sminshall 			argv[i]);
36932144Sminshall 	    return 0;
37032144Sminshall 	}
37132144Sminshall 	switch (s->what) {
37232144Sminshall 	case SENDQUESTION:
37338689Sborman 	    question = 1;
37432144Sminshall 	    break;
37532144Sminshall 	case SENDESCAPE:
37632144Sminshall 	    count += 1;
37732144Sminshall 	    break;
37832144Sminshall 	case SYNCH:
37932144Sminshall 	    count += 2;
38032144Sminshall 	    break;
38132144Sminshall 	default:
38232144Sminshall 	    count += 2;
38332144Sminshall 	    break;
38432144Sminshall 	}
38532144Sminshall     }
38638689Sborman     if (!connected) {
38738689Sborman 	if (count)
38838689Sborman 	    printf("?Need to be connected first.\n");
38938689Sborman 	if (question) {
39038689Sborman 	    for (s = Sendlist; s->name; s++)
39138689Sborman 		if (s->help)
39238689Sborman 		    printf("%-15s %s\n", s->name, s->help);
39338689Sborman 	} else
39438689Sborman 	    printf("'send ?' for help\n");
39538689Sborman 	return !question;
39638689Sborman     }
39732144Sminshall     /* Now, do we have enough room? */
39832144Sminshall     if (NETROOM() < count) {
39932144Sminshall 	printf("There is not enough room in the buffer TO the network\n");
40032144Sminshall 	printf("to process your request.  Nothing will be done.\n");
40132144Sminshall 	printf("('send synch' will throw away most data in the network\n");
40232144Sminshall 	printf("buffer, if this might help.)\n");
40332144Sminshall 	return 0;
40432144Sminshall     }
40532144Sminshall     /* OK, they are all OK, now go through again and actually send */
40632144Sminshall     for (i = 1; i < argc; i++) {
40732144Sminshall 	if ((s = getsend(argv[i])) == 0) {
40832144Sminshall 	    fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
40944361Sborman 	    (void) quit();
41032144Sminshall 	    /*NOTREACHED*/
41132144Sminshall 	}
41238689Sborman 	if (s->handler) {
41338689Sborman 	    (*s->handler)(s);
41432144Sminshall 	} else {
41532144Sminshall 	    switch (what = s->what) {
41632144Sminshall 	    case SYNCH:
41732144Sminshall 		dosynch();
41832144Sminshall 		break;
41932144Sminshall 	    case SENDQUESTION:
42032144Sminshall 		for (s = Sendlist; s->name; s++) {
42138689Sborman 		    if (s->help)
42238689Sborman 			printf("%-15s %s\n", s->name, s->help);
42332144Sminshall 		}
42432144Sminshall 		question = 1;
42532144Sminshall 		break;
42632144Sminshall 	    case SENDESCAPE:
42732144Sminshall 		NETADD(escape);
42832144Sminshall 		break;
42932144Sminshall 	    default:
43032144Sminshall 		NET2ADD(IAC, what);
43132144Sminshall 		break;
43232144Sminshall 	    }
43332144Sminshall 	}
43432144Sminshall     }
43532144Sminshall     return !question;
43632144Sminshall }
43732144Sminshall 
43832144Sminshall /*
43932144Sminshall  * The following are the routines and data structures referred
44032144Sminshall  * to by the arguments to the "toggle" command.
44132144Sminshall  */
44232144Sminshall 
44332144Sminshall static
44432144Sminshall lclchars()
44532144Sminshall {
44632144Sminshall     donelclchars = 1;
44732144Sminshall     return 1;
44832144Sminshall }
44932144Sminshall 
45032144Sminshall static
45132144Sminshall togdebug()
45232144Sminshall {
45332144Sminshall #ifndef	NOT43
45432144Sminshall     if (net > 0 &&
45532144Sminshall 	(SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
45632144Sminshall 	    perror("setsockopt (SO_DEBUG)");
45732144Sminshall     }
45832144Sminshall #else	/* NOT43 */
45932144Sminshall     if (debug) {
46032144Sminshall 	if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
46132144Sminshall 	    perror("setsockopt (SO_DEBUG)");
46232144Sminshall     } else
46332144Sminshall 	printf("Cannot turn off socket debugging\n");
46432144Sminshall #endif	/* NOT43 */
46532144Sminshall     return 1;
46632144Sminshall }
46732144Sminshall 
46832144Sminshall 
46932144Sminshall static int
47032144Sminshall togcrlf()
47132144Sminshall {
47232144Sminshall     if (crlf) {
47332144Sminshall 	printf("Will send carriage returns as telnet <CR><LF>.\n");
47432144Sminshall     } else {
47532144Sminshall 	printf("Will send carriage returns as telnet <CR><NUL>.\n");
47632144Sminshall     }
47732144Sminshall     return 1;
47832144Sminshall }
47932144Sminshall 
48038909Sborman int binmode;
48132144Sminshall 
48232144Sminshall static int
48338689Sborman togbinary(val)
48438689Sborman int val;
48532144Sminshall {
48632144Sminshall     donebinarytoggle = 1;
48732144Sminshall 
48838909Sborman     if (val >= 0) {
48938909Sborman 	binmode = val;
49038909Sborman     } else {
49138909Sborman 	if (my_want_state_is_will(TELOPT_BINARY) &&
49238909Sborman 				my_want_state_is_do(TELOPT_BINARY)) {
49338909Sborman 	    binmode = 1;
49438909Sborman 	} else if (my_want_state_is_wont(TELOPT_BINARY) &&
49538909Sborman 				my_want_state_is_dont(TELOPT_BINARY)) {
49638909Sborman 	    binmode = 0;
49738909Sborman 	}
49838909Sborman 	val = binmode ? 0 : 1;
49938909Sborman     }
50038909Sborman 
50138909Sborman     if (val == 1) {
50238909Sborman 	if (my_want_state_is_will(TELOPT_BINARY) &&
50338909Sborman 					my_want_state_is_do(TELOPT_BINARY)) {
50438689Sborman 	    printf("Already operating in binary mode with remote host.\n");
50538909Sborman 	} else {
50638909Sborman 	    printf("Negotiating binary mode with remote host.\n");
50738909Sborman 	    tel_enter_binary(3);
50838689Sborman 	}
50938909Sborman     } else {
51038909Sborman 	if (my_want_state_is_wont(TELOPT_BINARY) &&
51138909Sborman 					my_want_state_is_dont(TELOPT_BINARY)) {
51238689Sborman 	    printf("Already in network ascii mode with remote host.\n");
51338909Sborman 	} else {
51438909Sborman 	    printf("Negotiating network ascii mode with remote host.\n");
51538909Sborman 	    tel_leave_binary(3);
51638689Sborman 	}
51732144Sminshall     }
51832144Sminshall     return 1;
51932144Sminshall }
52032144Sminshall 
52138909Sborman static int
52238909Sborman togrbinary(val)
52338909Sborman int val;
52438909Sborman {
52538909Sborman     donebinarytoggle = 1;
52632144Sminshall 
52738909Sborman     if (val == -1)
52838909Sborman 	val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
52932144Sminshall 
53038909Sborman     if (val == 1) {
53138909Sborman 	if (my_want_state_is_do(TELOPT_BINARY)) {
53238909Sborman 	    printf("Already receiving in binary mode.\n");
53338909Sborman 	} else {
53438909Sborman 	    printf("Negotiating binary mode on input.\n");
53538909Sborman 	    tel_enter_binary(1);
53638909Sborman 	}
53738909Sborman     } else {
53838909Sborman 	if (my_want_state_is_dont(TELOPT_BINARY)) {
53938909Sborman 	    printf("Already receiving in network ascii mode.\n");
54038909Sborman 	} else {
54138909Sborman 	    printf("Negotiating network ascii mode on input.\n");
54238909Sborman 	    tel_leave_binary(1);
54338909Sborman 	}
54438909Sborman     }
54538909Sborman     return 1;
54638909Sborman }
54738909Sborman 
54838909Sborman static int
54938909Sborman togxbinary(val)
55038909Sborman int val;
55138909Sborman {
55238909Sborman     donebinarytoggle = 1;
55338909Sborman 
55438909Sborman     if (val == -1)
55538909Sborman 	val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
55638909Sborman 
55738909Sborman     if (val == 1) {
55838909Sborman 	if (my_want_state_is_will(TELOPT_BINARY)) {
55938909Sborman 	    printf("Already transmitting in binary mode.\n");
56038909Sborman 	} else {
56138909Sborman 	    printf("Negotiating binary mode on output.\n");
56238909Sborman 	    tel_enter_binary(2);
56338909Sborman 	}
56438909Sborman     } else {
56538909Sborman 	if (my_want_state_is_wont(TELOPT_BINARY)) {
56638909Sborman 	    printf("Already transmitting in network ascii mode.\n");
56738909Sborman 	} else {
56838909Sborman 	    printf("Negotiating network ascii mode on output.\n");
56938909Sborman 	    tel_leave_binary(2);
57038909Sborman 	}
57138909Sborman     }
57238909Sborman     return 1;
57338909Sborman }
57438909Sborman 
57538909Sborman 
57632144Sminshall extern int togglehelp();
57738689Sborman extern int slc_check();
57832144Sminshall 
57932144Sminshall struct togglelist {
58032144Sminshall     char	*name;		/* name of toggle */
58132144Sminshall     char	*help;		/* help message */
58232144Sminshall     int		(*handler)();	/* routine to do actual setting */
58332144Sminshall     int		*variable;
58432144Sminshall     char	*actionexplanation;
58532144Sminshall };
58632144Sminshall 
58732144Sminshall static struct togglelist Togglelist[] = {
58832144Sminshall     { "autoflush",
58938689Sborman 	"flushing of output when sending interrupt characters",
59032144Sminshall 	    0,
59138689Sborman 		&autoflush,
59238689Sborman 		    "flush output when sending interrupt characters" },
59332144Sminshall     { "autosynch",
59438689Sborman 	"automatic sending of interrupt characters in urgent mode",
59532144Sminshall 	    0,
59638689Sborman 		&autosynch,
59738689Sborman 		    "send interrupt characters in urgent mode" },
59832144Sminshall     { "binary",
59938689Sborman 	"sending and receiving of binary data",
60032144Sminshall 	    togbinary,
60138689Sborman 		0,
60238689Sborman 		    0 },
60338909Sborman     { "inbinary",
60438909Sborman 	"receiving of binary data",
60538909Sborman 	    togrbinary,
60638909Sborman 		0,
60738909Sborman 		    0 },
60838909Sborman     { "outbinary",
60938909Sborman 	"sending of binary data",
61038909Sborman 	    togxbinary,
61138909Sborman 		0,
61238909Sborman 		    0 },
61332144Sminshall     { "crlf",
61438689Sborman 	"sending carriage returns as telnet <CR><LF>",
61532144Sminshall 	    togcrlf,
61638689Sborman 		&crlf,
61738689Sborman 		    0 },
61832144Sminshall     { "crmod",
61938689Sborman 	"mapping of received carriage returns",
62032144Sminshall 	    0,
62138689Sborman 		&crmod,
62238689Sborman 		    "map carriage return on output" },
62332144Sminshall     { "localchars",
62438689Sborman 	"local recognition of certain control characters",
62532144Sminshall 	    lclchars,
62638689Sborman 		&localchars,
62738689Sborman 		    "recognize certain control characters" },
62843317Skfall #ifdef	KERBEROS
62943317Skfall     { "kerberos",
63043317Skfall 	"toggle use of Kerberos authentication",
63143317Skfall 	    0,
63243317Skfall 		&kerberized,
63343317Skfall 		    "use Kerberos authentication" },
63443317Skfall #endif
63538689Sborman     { " ", "", 0 },		/* empty line */
63638208Sminshall #if	defined(unix) && defined(TN3270)
63738920Sminshall     { "apitrace",
63838920Sminshall 	"(debugging) toggle tracing of API transactions",
63938920Sminshall 	    0,
64038920Sminshall 		&apitrace,
64138920Sminshall 		    "trace API transactions" },
64238208Sminshall     { "cursesdata",
64338208Sminshall 	"(debugging) toggle printing of hexadecimal curses data",
64438208Sminshall 	    0,
64538689Sborman 		&cursesdata,
64638689Sborman 		    "print hexadecimal representation of curses data" },
64738208Sminshall #endif	/* defined(unix) && defined(TN3270) */
64832144Sminshall     { "debug",
64938689Sborman 	"debugging",
65032144Sminshall 	    togdebug,
65138689Sborman 		&debug,
65238689Sborman 		    "turn on socket level debugging" },
65332144Sminshall     { "netdata",
65438689Sborman 	"printing of hexadecimal network data (debugging)",
65532144Sminshall 	    0,
65638689Sborman 		&netdata,
65738689Sborman 		    "print hexadecimal representation of network traffic" },
65838689Sborman     { "prettydump",
65938689Sborman 	"output of \"netdata\" to user readable format (debugging)",
66038689Sborman 	    0,
66138689Sborman 		&prettydump,
66238689Sborman 		    "print user readable output for \"netdata\"" },
66332144Sminshall     { "options",
66438689Sborman 	"viewing of options processing (debugging)",
66532144Sminshall 	    0,
66638689Sborman 		&showoptions,
66738689Sborman 		    "show option processing" },
66838208Sminshall #if	defined(unix)
66938208Sminshall     { "termdata",
67038208Sminshall 	"(debugging) toggle printing of hexadecimal terminal data",
67138208Sminshall 	    0,
67238689Sborman 		&termdata,
67338689Sborman 		    "print hexadecimal representation of terminal traffic" },
67438208Sminshall #endif	/* defined(unix) */
67532144Sminshall     { "?",
67638689Sborman 	0,
67738689Sborman 	    togglehelp },
67832144Sminshall     { "help",
67938689Sborman 	0,
68038689Sborman 	    togglehelp },
68132144Sminshall     { 0 }
68232144Sminshall };
68332144Sminshall 
68432144Sminshall static
68532144Sminshall togglehelp()
68632144Sminshall {
68732144Sminshall     struct togglelist *c;
68832144Sminshall 
68932144Sminshall     for (c = Togglelist; c->name; c++) {
69038689Sborman 	if (c->help) {
69138689Sborman 	    if (*c->help)
69238689Sborman 		printf("%-15s toggle %s\n", c->name, c->help);
69338689Sborman 	    else
69438689Sborman 		printf("\n");
69532144Sminshall 	}
69632144Sminshall     }
69738689Sborman     printf("\n");
69838689Sborman     printf("%-15s %s\n", "?", "display help information");
69932144Sminshall     return 0;
70032144Sminshall }
70132144Sminshall 
70238689Sborman static
70338689Sborman settogglehelp(set)
70438689Sborman int set;
70538689Sborman {
70638689Sborman     struct togglelist *c;
70738689Sborman 
70838689Sborman     for (c = Togglelist; c->name; c++) {
70938689Sborman 	if (c->help) {
71038689Sborman 	    if (*c->help)
71138689Sborman 		printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
71238689Sborman 						c->help);
71338689Sborman 	    else
71438689Sborman 		printf("\n");
71538689Sborman 	}
71638689Sborman     }
71738689Sborman }
71838689Sborman 
71932144Sminshall static char **
72032144Sminshall getnexttoggle(name)
72132144Sminshall char *name;
72232144Sminshall {
72332144Sminshall     struct togglelist *c = (struct togglelist *) name;
72432144Sminshall 
72532144Sminshall     return (char **) (c+1);
72632144Sminshall }
72732144Sminshall 
72832144Sminshall static struct togglelist *
72932144Sminshall gettoggle(name)
73032144Sminshall char *name;
73132144Sminshall {
73232144Sminshall     return (struct togglelist *)
73332144Sminshall 			genget(name, (char **) Togglelist, getnexttoggle);
73432144Sminshall }
73532144Sminshall 
73632144Sminshall static
73732144Sminshall toggle(argc, argv)
73832144Sminshall int	argc;
73932144Sminshall char	*argv[];
74032144Sminshall {
74132144Sminshall     int retval = 1;
74232144Sminshall     char *name;
74332144Sminshall     struct togglelist *c;
74432144Sminshall 
74532144Sminshall     if (argc < 2) {
74632144Sminshall 	fprintf(stderr,
74732144Sminshall 	    "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
74832144Sminshall 	return 0;
74932144Sminshall     }
75032144Sminshall     argc--;
75132144Sminshall     argv++;
75232144Sminshall     while (argc--) {
75332144Sminshall 	name = *argv++;
75432144Sminshall 	c = gettoggle(name);
75532144Sminshall 	if (Ambiguous(c)) {
75632144Sminshall 	    fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
75732144Sminshall 					name);
75832144Sminshall 	    return 0;
75932144Sminshall 	} else if (c == 0) {
76032144Sminshall 	    fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
76132144Sminshall 					name);
76232144Sminshall 	    return 0;
76332144Sminshall 	} else {
76432144Sminshall 	    if (c->variable) {
76532144Sminshall 		*c->variable = !*c->variable;		/* invert it */
76632144Sminshall 		if (c->actionexplanation) {
76732144Sminshall 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
76832144Sminshall 							c->actionexplanation);
76932144Sminshall 		}
77032144Sminshall 	    }
77132144Sminshall 	    if (c->handler) {
77238689Sborman 		retval &= (*c->handler)(-1);
77332144Sminshall 	    }
77432144Sminshall 	}
77532144Sminshall     }
77632144Sminshall     return retval;
77732144Sminshall }
77832144Sminshall 
77932144Sminshall /*
78032144Sminshall  * The following perform the "set" command.
78132144Sminshall  */
78232144Sminshall 
78338689Sborman #ifdef	USE_TERMIO
78438689Sborman struct termio new_tc = { 0 };
78538689Sborman #endif
78638689Sborman 
78732144Sminshall struct setlist {
78832144Sminshall     char *name;				/* name */
78932144Sminshall     char *help;				/* help information */
79038689Sborman     void (*handler)();
79140245Sborman     cc_t *charp;			/* where it is located at */
79232144Sminshall };
79332144Sminshall 
79432144Sminshall static struct setlist Setlist[] = {
79544361Sborman #ifdef	KLUDGELINEMODE
79638689Sborman     { "echo", 	"character to toggle local echoing on/off", 0, &echoc },
79744361Sborman #endif
79838689Sborman     { "escape",	"character to escape back to telnet command mode", 0, &escape },
79940245Sborman     { "tracefile", "file to write trace intormation to", SetNetTrace, (cc_t *)NetTraceFile},
80032144Sminshall     { " ", "" },
80138689Sborman     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
80238689Sborman     { "flushoutput", "character to cause an Abort Oubput", 0, termFlushCharp },
80338689Sborman     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
80438689Sborman     { "quit",	"character to cause an Abort process", 0, termQuitCharp },
80538689Sborman     { "eof",	"character to cause an EOF ", 0, termEofCharp },
80638689Sborman     { " ", "" },
80738689Sborman     { " ", "The following are for local editing in linemode", 0, 0 },
80838689Sborman     { "erase",	"character to use to erase a character", 0, termEraseCharp },
80938689Sborman     { "kill",	"character to use to erase a line", 0, termKillCharp },
81038689Sborman     { "lnext",	"character to use for literal next", 0, termLiteralNextCharp },
81144361Sborman     { "susp",	"character to cause a Suspend Process", 0, termSuspCharp },
81238689Sborman     { "reprint", "character to use for line reprint", 0, termRprntCharp },
81338689Sborman     { "worderase", "character to use to erase a word", 0, termWerasCharp },
81438689Sborman     { "start",	"character to use for XON", 0, termStartCharp },
81544361Sborman     { "stop",	"character to use for XOFF", 0, termStopCharp },
81644361Sborman     { "forw1",	"alternate end of line character", 0, termForw1Charp },
81744361Sborman     { "forw2",	"alternate end of line character", 0, termForw2Charp },
81832144Sminshall     { 0 }
81932144Sminshall };
82032144Sminshall 
82138689Sborman #ifdef	CRAY
82238689Sborman /* Work around compiler bug */
82338689Sborman _setlist_init()
82438689Sborman {
82544361Sborman #ifndef	KLUDGELINEMODE
82644361Sborman #define	N 4
82744361Sborman #else
82844361Sborman #define	N 5
82944361Sborman #endif
83044361Sborman 	Setlist[N+0].charp = &termFlushChar;
83144361Sborman 	Setlist[N+1].charp = &termIntChar;
83244361Sborman 	Setlist[N+2].charp = &termQuitChar;
83344361Sborman 	Setlist[N+3].charp = &termEofChar;
83444361Sborman 	Setlist[N+6].charp = &termEraseChar;
83544361Sborman 	Setlist[N+7].charp = &termKillChar;
83644361Sborman 	Setlist[N+8].charp = &termLiteralNextChar;
83744361Sborman 	Setlist[N+9].charp = &termSuspChar;
83844361Sborman 	Setlist[N+10].charp = &termRprntChar;
83944361Sborman 	Setlist[N+11].charp = &termWerasChar;
84044361Sborman 	Setlist[N+12].charp = &termStartChar;
84144361Sborman 	Setlist[N+13].charp = &termStopChar;
84244361Sborman 	Setlist[N+14].charp = &termForw1Char;
84344361Sborman 	Setlist[N+15].charp = &termForw2Char;
84444361Sborman #undef	N
84538689Sborman }
84638810Sborman #endif	/* CRAY */
84738689Sborman 
84832144Sminshall static char **
84932144Sminshall getnextset(name)
85032144Sminshall char *name;
85132144Sminshall {
85232144Sminshall     struct setlist *c = (struct setlist *)name;
85332144Sminshall 
85432144Sminshall     return (char **) (c+1);
85532144Sminshall }
85632144Sminshall 
85732144Sminshall static struct setlist *
85832144Sminshall getset(name)
85932144Sminshall char *name;
86032144Sminshall {
86132144Sminshall     return (struct setlist *) genget(name, (char **) Setlist, getnextset);
86232144Sminshall }
86332144Sminshall 
86444361Sborman set_escape_char(s)
86544361Sborman char *s;
86644361Sborman {
86744361Sborman 	escape = (s && *s) ? special(s) : -1;
86844361Sborman 	printf("escape character is '%s'.\n", control(escape));
86944361Sborman }
87044361Sborman 
87132144Sminshall static
87232144Sminshall setcmd(argc, argv)
87332144Sminshall int	argc;
87432144Sminshall char	*argv[];
87532144Sminshall {
87632144Sminshall     int value;
87732144Sminshall     struct setlist *ct;
87838689Sborman     struct togglelist *c;
87932144Sminshall 
88038689Sborman     if (argc < 2 || argc > 3) {
88138689Sborman 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
88232144Sminshall 	return 0;
88332144Sminshall     }
88438689Sborman     if ((argc == 2) &&
88538689Sborman 		((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help")))) {
88638689Sborman 	for (ct = Setlist; ct->name; ct++)
88738689Sborman 	    printf("%-15s %s\n", ct->name, ct->help);
88838689Sborman 	printf("\n");
88938689Sborman 	settogglehelp(1);
89038689Sborman 	printf("%-15s %s\n", "?", "display help information");
89138689Sborman 	return 0;
89238689Sborman     }
89332144Sminshall 
89432144Sminshall     ct = getset(argv[1]);
89532144Sminshall     if (ct == 0) {
89638689Sborman 	c = gettoggle(argv[1]);
89738689Sborman 	if (c == 0) {
89838689Sborman 	    fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
89932144Sminshall 			argv[1]);
90038689Sborman 	    return 0;
90138689Sborman 	} else if (Ambiguous(c)) {
90238689Sborman 	    fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
90338689Sborman 			argv[1]);
90438689Sborman 	    return 0;
90538689Sborman 	}
90638689Sborman 	if (c->variable) {
90738689Sborman 	    if ((argc == 2) || (strcmp("on", argv[2]) == 0))
90838689Sborman 		*c->variable = 1;
90938689Sborman 	    else if (strcmp("off", argv[2]) == 0)
91038689Sborman 		*c->variable = 0;
91138689Sborman 	    else {
91238689Sborman 		printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
91338689Sborman 		return 0;
91438689Sborman 	    }
91538689Sborman 	    if (c->actionexplanation) {
91638689Sborman 		printf("%s %s.\n", *c->variable? "Will" : "Won't",
91738689Sborman 							c->actionexplanation);
91838689Sborman 	    }
91938689Sborman 	}
92038689Sborman 	if (c->handler)
92138689Sborman 	    (*c->handler)(1);
92238689Sborman     } else if (argc != 3) {
92338689Sborman 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
92432144Sminshall 	return 0;
92532144Sminshall     } else if (Ambiguous(ct)) {
92632144Sminshall 	fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
92732144Sminshall 			argv[1]);
92832144Sminshall 	return 0;
92938689Sborman     } else if (ct->handler) {
93038689Sborman 	(*ct->handler)(argv[2]);
93144361Sborman 	printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
93232144Sminshall     } else {
93332144Sminshall 	if (strcmp("off", argv[2])) {
93432144Sminshall 	    value = special(argv[2]);
93532144Sminshall 	} else {
93632144Sminshall 	    value = -1;
93732144Sminshall 	}
93840245Sborman 	*(ct->charp) = (cc_t)value;
93932144Sminshall 	printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
94032144Sminshall     }
94138689Sborman     slc_check();
94232144Sminshall     return 1;
94332144Sminshall }
94438689Sborman 
94538689Sborman static
94638689Sborman unsetcmd(argc, argv)
94738689Sborman int	argc;
94838689Sborman char	*argv[];
94938689Sborman {
95038689Sborman     struct setlist *ct;
95138689Sborman     struct togglelist *c;
95238689Sborman     register char *name;
95338689Sborman 
95438689Sborman     if (argc < 2) {
95538689Sborman 	fprintf(stderr,
95638689Sborman 	    "Need an argument to 'unset' command.  'unset ?' for help.\n");
95738689Sborman 	return 0;
95838689Sborman     }
95938689Sborman     if ((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help"))) {
96038689Sborman 	for (ct = Setlist; ct->name; ct++)
96138689Sborman 	    printf("%-15s %s\n", ct->name, ct->help);
96238689Sborman 	printf("\n");
96338689Sborman 	settogglehelp(0);
96438689Sborman 	printf("%-15s %s\n", "?", "display help information");
96538689Sborman 	return 0;
96638689Sborman     }
96738689Sborman 
96838689Sborman     argc--;
96938689Sborman     argv++;
97038689Sborman     while (argc--) {
97138689Sborman 	name = *argv++;
97238689Sborman 	ct = getset(name);
97338689Sborman 	if (ct == 0) {
97438689Sborman 	    c = gettoggle(name);
97538689Sborman 	    if (c == 0) {
97638689Sborman 		fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
97738689Sborman 			name);
97838689Sborman 		return 0;
97938689Sborman 	    } else if (Ambiguous(c)) {
98038689Sborman 		fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
98138689Sborman 			name);
98238689Sborman 		return 0;
98338689Sborman 	    }
98438689Sborman 	    if (c->variable) {
98538689Sborman 		*c->variable = 0;
98638689Sborman 		if (c->actionexplanation) {
98738689Sborman 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
98838689Sborman 							c->actionexplanation);
98938689Sborman 		}
99038689Sborman 	    }
99138689Sborman 	    if (c->handler)
99238689Sborman 		(*c->handler)(0);
99338689Sborman 	} else if (Ambiguous(ct)) {
99438689Sborman 	    fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
99538689Sborman 			name);
99638689Sborman 	    return 0;
99738689Sborman 	} else if (ct->handler) {
99838689Sborman 	    (*ct->handler)(0);
99944361Sborman 	    printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
100038689Sborman 	} else {
100138689Sborman 	    *(ct->charp) = -1;
100238689Sborman 	    printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
100338689Sborman 	}
100438689Sborman     }
100538689Sborman     return 1;
100638689Sborman }
100732144Sminshall 
100832144Sminshall /*
100932144Sminshall  * The following are the data structures and routines for the
101032144Sminshall  * 'mode' command.
101132144Sminshall  */
101238689Sborman #ifdef	KLUDGELINEMODE
101338689Sborman extern int kludgelinemode;
101444361Sborman 
101544361Sborman dokludgemode()
101644361Sborman {
101744361Sborman     kludgelinemode = 1;
101844361Sborman     send_wont(TELOPT_LINEMODE, 1);
101944361Sborman     send_dont(TELOPT_SGA, 1);
102044361Sborman     send_dont(TELOPT_ECHO, 1);
102144361Sborman }
102238689Sborman #endif
102332144Sminshall 
102432144Sminshall static
102532144Sminshall dolinemode()
102632144Sminshall {
102738689Sborman #ifdef	KLUDGELINEMODE
102838689Sborman     if (kludgelinemode)
102938689Sborman 	send_dont(TELOPT_SGA, 1);
103038689Sborman #endif
103138689Sborman     send_will(TELOPT_LINEMODE, 1);
103238689Sborman     send_dont(TELOPT_ECHO, 1);
103332144Sminshall     return 1;
103432144Sminshall }
103532144Sminshall 
103632144Sminshall static
103732144Sminshall docharmode()
103832144Sminshall {
103938689Sborman #ifdef	KLUDGELINEMODE
104038689Sborman     if (kludgelinemode)
104138689Sborman 	send_do(TELOPT_SGA, 1);
104238689Sborman     else
104338689Sborman #endif
104438689Sborman     send_wont(TELOPT_LINEMODE, 1);
104538689Sborman     send_do(TELOPT_ECHO, 1);
104638689Sborman     return 1;
104738689Sborman }
104838689Sborman 
104938689Sborman setmode(bit)
105038689Sborman {
105138689Sborman     return dolmmode(bit, 1);
105238689Sborman }
105338689Sborman 
105438689Sborman clearmode(bit)
105538689Sborman {
105638689Sborman     return dolmmode(bit, 0);
105738689Sborman }
105838689Sborman 
105938689Sborman dolmmode(bit, on)
106038689Sborman int bit, on;
106138689Sborman {
106238689Sborman     char c;
106338689Sborman     extern int linemode;
106438689Sborman 
106538689Sborman     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
106638689Sborman 	printf("?Need to have LINEMODE option enabled first.\n");
106738689Sborman 	printf("'mode ?' for help.\n");
106838689Sborman 	return 0;
106932144Sminshall     }
107038689Sborman 
107138689Sborman     if (on)
107238689Sborman 	c = (linemode | bit);
107338689Sborman     else
107438689Sborman 	c = (linemode & ~bit);
107538689Sborman     lm_mode(&c, 1, 1);
107632144Sminshall     return 1;
107732144Sminshall }
107832144Sminshall 
107938689Sborman struct modelist {
108038689Sborman 	char	*name;		/* command name */
108138689Sborman 	char	*help;		/* help string */
108238689Sborman 	int	(*handler)();	/* routine which executes command */
108338689Sborman 	int	needconnect;	/* Do we need to be connected to execute? */
108438689Sborman 	int	arg1;
108538689Sborman };
108638689Sborman 
108738689Sborman extern int modehelp();
108838689Sborman 
108938689Sborman static struct modelist ModeList[] = {
109038689Sborman     { "character", "Disable LINEMODE option",	docharmode, 1 },
109139529Sborman #ifdef	KLUDGELINEMODE
109239529Sborman     { "",	"(or disable obsolete line-by-line mode)", 0 },
109338689Sborman #endif
109438689Sborman     { "line",	"Enable LINEMODE option",	dolinemode, 1 },
109539529Sborman #ifdef	KLUDGELINEMODE
109639529Sborman     { "",	"(or enable obsolete line-by-line mode)", 0 },
109738689Sborman #endif
109838689Sborman     { "", "", 0 },
109938689Sborman     { "",	"These require the LINEMODE option to be enabled", 0 },
110038689Sborman     { "isig",	"Enable signal trapping",	setmode, 1, MODE_TRAPSIG },
110138689Sborman     { "+isig",	0,				setmode, 1, MODE_TRAPSIG },
110238689Sborman     { "-isig",	"Disable signal trapping",	clearmode, 1, MODE_TRAPSIG },
110338689Sborman     { "edit",	"Enable character editing",	setmode, 1, MODE_EDIT },
110438689Sborman     { "+edit",	0,				setmode, 1, MODE_EDIT },
110538689Sborman     { "-edit",	"Disable character editing",	clearmode, 1, MODE_EDIT },
110644361Sborman     { "softtabs", "Enable tab expansion",	setmode, 1, MODE_SOFT_TAB },
110744361Sborman     { "+softtabs", 0,				setmode, 1, MODE_SOFT_TAB },
110844361Sborman     { "-softtabs", "Disable character editing",	clearmode, 1, MODE_SOFT_TAB },
110944361Sborman     { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
111044361Sborman     { "+litecho", 0,				setmode, 1, MODE_LIT_ECHO },
111144361Sborman     { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
111238689Sborman     { "help",	0,				modehelp, 0 },
111344361Sborman #ifdef	KLUDGELINEMODE
111444361Sborman     { "kludgeline", 0,				dokludgemode, 1 },
111544361Sborman #endif
111644361Sborman     { "", "", 0 },
111738689Sborman     { "?",	"Print help information",	modehelp, 0 },
111832144Sminshall     { 0 },
111932144Sminshall };
112032144Sminshall 
112132144Sminshall static char **
112232144Sminshall getnextmode(name)
112332144Sminshall char *name;
112432144Sminshall {
112538689Sborman     return (char **) (((struct modelist *)name)+1);
112632144Sminshall }
112732144Sminshall 
112838689Sborman static struct modelist *
112932144Sminshall getmodecmd(name)
113032144Sminshall char *name;
113132144Sminshall {
113238689Sborman     return (struct modelist *) genget(name, (char **) ModeList, getnextmode);
113332144Sminshall }
113432144Sminshall 
113538689Sborman modehelp()
113638689Sborman {
113738689Sborman     struct modelist *mt;
113838689Sborman 
113938689Sborman     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
114038689Sborman     for (mt = ModeList; mt->name; mt++) {
114138689Sborman 	if (mt->help) {
114238689Sborman 	    if (*mt->help)
114338689Sborman 		printf("%-15s %s\n", mt->name, mt->help);
114438689Sborman 	    else
114538689Sborman 		printf("\n");
114638689Sborman 	}
114738689Sborman     }
114838689Sborman     return 0;
114938689Sborman }
115038689Sborman 
115132144Sminshall static
115232144Sminshall modecmd(argc, argv)
115332144Sminshall int	argc;
115432144Sminshall char	*argv[];
115532144Sminshall {
115638689Sborman     struct modelist *mt;
115732144Sminshall 
115838689Sborman     if (argc != 2) {
115938689Sborman 	printf("'mode' command requires an argument\n");
116038689Sborman 	printf("'mode ?' for help.\n");
116138689Sborman     } else if ((mt = getmodecmd(argv[1])) == 0) {
116232144Sminshall 	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
116332144Sminshall     } else if (Ambiguous(mt)) {
116432144Sminshall 	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
116538689Sborman     } else if (mt->needconnect && !connected) {
116638689Sborman 	printf("?Need to be connected first.\n");
116738689Sborman 	printf("'mode ?' for help.\n");
116838689Sborman     } else if (mt->handler) {
116938689Sborman 	return (*mt->handler)(mt->arg1);
117032144Sminshall     }
117138689Sborman     return 0;
117232144Sminshall }
117332144Sminshall 
117432144Sminshall /*
117532144Sminshall  * The following data structures and routines implement the
117632144Sminshall  * "display" command.
117732144Sminshall  */
117832144Sminshall 
117932144Sminshall static
118032144Sminshall display(argc, argv)
118132144Sminshall int	argc;
118232144Sminshall char	*argv[];
118332144Sminshall {
118432144Sminshall #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
118532144Sminshall 			    if (*tl->variable) { \
118632144Sminshall 				printf("will"); \
118732144Sminshall 			    } else { \
118832144Sminshall 				printf("won't"); \
118932144Sminshall 			    } \
119032144Sminshall 			    printf(" %s.\n", tl->actionexplanation); \
119132144Sminshall 			}
119232144Sminshall 
119332144Sminshall #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
119438689Sborman 			if (sl->handler == 0) \
119538689Sborman 			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
119638689Sborman 			else \
119744361Sborman 			    printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
119832144Sminshall 		    }
119932144Sminshall 
120032144Sminshall     struct togglelist *tl;
120132144Sminshall     struct setlist *sl;
120232144Sminshall 
120332144Sminshall     if (argc == 1) {
120432144Sminshall 	for (tl = Togglelist; tl->name; tl++) {
120532144Sminshall 	    dotog(tl);
120632144Sminshall 	}
120732144Sminshall 	printf("\n");
120832144Sminshall 	for (sl = Setlist; sl->name; sl++) {
120932144Sminshall 	    doset(sl);
121032144Sminshall 	}
121132144Sminshall     } else {
121232144Sminshall 	int i;
121332144Sminshall 
121432144Sminshall 	for (i = 1; i < argc; i++) {
121532144Sminshall 	    sl = getset(argv[i]);
121632144Sminshall 	    tl = gettoggle(argv[i]);
121732144Sminshall 	    if (Ambiguous(sl) || Ambiguous(tl)) {
121832144Sminshall 		printf("?Ambiguous argument '%s'.\n", argv[i]);
121932144Sminshall 		return 0;
122032144Sminshall 	    } else if (!sl && !tl) {
122132144Sminshall 		printf("?Unknown argument '%s'.\n", argv[i]);
122232144Sminshall 		return 0;
122332144Sminshall 	    } else {
122432144Sminshall 		if (tl) {
122532144Sminshall 		    dotog(tl);
122632144Sminshall 		}
122732144Sminshall 		if (sl) {
122832144Sminshall 		    doset(sl);
122932144Sminshall 		}
123032144Sminshall 	    }
123132144Sminshall 	}
123232144Sminshall     }
123338689Sborman /*@*/optionstatus();
123432144Sminshall     return 1;
123532144Sminshall #undef	doset
123632144Sminshall #undef	dotog
123732144Sminshall }
123832144Sminshall 
123932144Sminshall /*
124032144Sminshall  * The following are the data structures, and many of the routines,
124132144Sminshall  * relating to command processing.
124232144Sminshall  */
124332144Sminshall 
124432144Sminshall /*
124532144Sminshall  * Set the escape character.
124632144Sminshall  */
124732144Sminshall static
124832144Sminshall setescape(argc, argv)
124932144Sminshall 	int argc;
125032144Sminshall 	char *argv[];
125132144Sminshall {
125232144Sminshall 	register char *arg;
125332144Sminshall 	char buf[50];
125432144Sminshall 
125532144Sminshall 	printf(
125632144Sminshall 	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",
125732144Sminshall 				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");
125832144Sminshall 	if (argc > 2)
125932144Sminshall 		arg = argv[1];
126032144Sminshall 	else {
126132144Sminshall 		printf("new escape character: ");
126234849Sminshall 		(void) gets(buf);
126332144Sminshall 		arg = buf;
126432144Sminshall 	}
126532144Sminshall 	if (arg[0] != '\0')
126632144Sminshall 		escape = arg[0];
126732144Sminshall 	if (!In3270) {
126832144Sminshall 		printf("Escape character is '%s'.\n", control(escape));
126932144Sminshall 	}
127034849Sminshall 	(void) fflush(stdout);
127132144Sminshall 	return 1;
127232144Sminshall }
127332144Sminshall 
127432144Sminshall /*VARARGS*/
127532144Sminshall static
127632144Sminshall togcrmod()
127732144Sminshall {
127832144Sminshall     crmod = !crmod;
127932144Sminshall     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
128032144Sminshall     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
128134849Sminshall     (void) fflush(stdout);
128232144Sminshall     return 1;
128332144Sminshall }
128432144Sminshall 
128532144Sminshall /*VARARGS*/
128632144Sminshall suspend()
128732144Sminshall {
128838689Sborman #ifdef	SIGTSTP
128937219Sminshall     setcommandmode();
129037219Sminshall     {
129144361Sborman 	long oldrows, oldcols, newrows, newcols, err;
129237219Sminshall 
129344361Sborman 	err = TerminalWindowSize(&oldrows, &oldcols);
129434849Sminshall 	(void) kill(0, SIGTSTP);
129544361Sborman 	err += TerminalWindowSize(&newrows, &newcols);
129644361Sborman 	if (connected && !err &&
129744361Sborman 	    ((oldrows != newrows) || (oldcols != newcols))) {
129837219Sminshall 		sendnaws();
129937219Sminshall 	}
130037219Sminshall     }
130137219Sminshall     /* reget parameters in case they were changed */
130237219Sminshall     TerminalSaveState();
130338689Sborman     setconnmode(0);
130438689Sborman #else
130538689Sborman     printf("Suspend is not supported.  Try the '!' command instead\n");
130638689Sborman #endif
130737219Sminshall     return 1;
130832144Sminshall }
130932144Sminshall 
131038689Sborman #if	!defined(TN3270)
131144361Sborman /*ARGSUSED*/
131238689Sborman shell(argc, argv)
131338689Sborman int argc;
131438689Sborman char *argv[];
131538689Sborman {
131638689Sborman     extern char *rindex();
131738689Sborman 
131838689Sborman     setcommandmode();
131938689Sborman     switch(vfork()) {
132038689Sborman     case -1:
132138689Sborman 	perror("Fork failed\n");
132238689Sborman 	break;
132338689Sborman 
132438689Sborman     case 0:
132538689Sborman 	{
132638689Sborman 	    /*
132738689Sborman 	     * Fire up the shell in the child.
132838689Sborman 	     */
132938689Sborman 	    register char *shell, *shellname;
133038689Sborman 
133138689Sborman 	    shell = getenv("SHELL");
133238689Sborman 	    if (shell == NULL)
133338689Sborman 		shell = "/bin/sh";
133438689Sborman 	    if ((shellname = rindex(shell, '/')) == 0)
133538689Sborman 		shellname = shell;
133638689Sborman 	    else
133738689Sborman 		shellname++;
133838689Sborman 	    if (argc > 1)
133938689Sborman 		execl(shell, shellname, "-c", &saveline[1], 0);
134038689Sborman 	    else
134138689Sborman 		execl(shell, shellname, 0);
134238689Sborman 	    perror("Execl");
134338689Sborman 	    _exit(1);
134438689Sborman 	}
134538689Sborman     default:
134644361Sborman 	    (void)wait((int *)0);	/* Wait for the shell to complete */
134738689Sborman     }
134838689Sborman }
134938689Sborman #endif	/* !defined(TN3270) */
135038689Sborman 
135132144Sminshall /*VARARGS*/
135232144Sminshall static
135332144Sminshall bye(argc, argv)
135432144Sminshall int	argc;		/* Number of arguments */
135532144Sminshall char	*argv[];	/* arguments */
135632144Sminshall {
135732144Sminshall     if (connected) {
135834849Sminshall 	(void) shutdown(net, 2);
135932144Sminshall 	printf("Connection closed.\n");
136034849Sminshall 	(void) NetClose(net);
136132144Sminshall 	connected = 0;
136232144Sminshall 	/* reset options */
136332144Sminshall 	tninit();
136432144Sminshall #if	defined(TN3270)
136532144Sminshall 	SetIn3270();		/* Get out of 3270 mode */
136632144Sminshall #endif	/* defined(TN3270) */
136732144Sminshall     }
136832144Sminshall     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
136932144Sminshall 	longjmp(toplevel, 1);
137032144Sminshall 	/* NOTREACHED */
137132144Sminshall     }
137232144Sminshall     return 1;			/* Keep lint, etc., happy */
137332144Sminshall }
137432144Sminshall 
137532144Sminshall /*VARARGS*/
137632144Sminshall quit()
137732144Sminshall {
137832144Sminshall 	(void) call(bye, "bye", "fromquit", 0);
137932144Sminshall 	Exit(0);
138044361Sborman 	/*NOTREACHED*/
138132144Sminshall }
138238689Sborman 
138338689Sborman /*
138438689Sborman  * The SLC command.
138538689Sborman  */
138632144Sminshall 
138738689Sborman struct slclist {
138838689Sborman 	char	*name;
138938689Sborman 	char	*help;
139038689Sborman 	int	(*handler)();
139138689Sborman 	int	arg;
139238689Sborman };
139338689Sborman 
139438689Sborman extern int slc_help();
139538689Sborman extern int slc_mode_export(), slc_mode_import(), slcstate();
139638689Sborman 
139738689Sborman struct slclist SlcList[] = {
139838689Sborman     { "export",	"Use local special character definitions",
139938689Sborman 						slc_mode_export,	0 },
140038689Sborman     { "import",	"Use remote special character definitions",
140138689Sborman 						slc_mode_import,	1 },
140238689Sborman     { "check",	"Verify remote special character definitions",
140338689Sborman 						slc_mode_import,	0 },
140438689Sborman     { "help",	0,				slc_help,		0 },
140538689Sborman     { "?",	"Print help information",	slc_help,		0 },
140638689Sborman     { 0 },
140738689Sborman };
140838689Sborman 
140938689Sborman static
141038689Sborman slc_help()
141138689Sborman {
141238689Sborman     struct slclist *c;
141338689Sborman 
141438689Sborman     for (c = SlcList; c->name; c++) {
141538689Sborman 	if (c->help) {
141638689Sborman 	    if (*c->help)
141738689Sborman 		printf("%-15s %s\n", c->name, c->help);
141838689Sborman 	    else
141938689Sborman 		printf("\n");
142038689Sborman 	}
142138689Sborman     }
142238689Sborman }
142338689Sborman 
142438689Sborman static char **
142538689Sborman getnextslc(name)
142638689Sborman char *name;
142738689Sborman {
142838689Sborman     return (char **)(((struct slclist *)name)+1);
142938689Sborman }
143038689Sborman 
143138689Sborman static struct slclist *
143238689Sborman getslc(name)
143338689Sborman char *name;
143438689Sborman {
143538689Sborman     return (struct slclist *)genget(name, (char **) SlcList, getnextslc);
143638689Sborman }
143738689Sborman 
143838689Sborman static
143938689Sborman slccmd(argc, argv)
144038689Sborman int	argc;
144138689Sborman char	*argv[];
144238689Sborman {
144338689Sborman     struct slclist *c;
144438689Sborman 
144538689Sborman     if (argc != 2) {
144638689Sborman 	fprintf(stderr,
144738689Sborman 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
144838689Sborman 	return 0;
144938689Sborman     }
145038689Sborman     c = getslc(argv[1]);
145138689Sborman     if (c == 0) {
145238689Sborman         fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
145338689Sborman     				argv[1]);
145438689Sborman         return 0;
145538689Sborman     }
145638689Sborman     if (Ambiguous(c)) {
145738689Sborman         fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
145838689Sborman     				argv[1]);
145938689Sborman         return 0;
146038689Sborman     }
146138689Sborman     (*c->handler)(c->arg);
146238689Sborman     slcstate();
146338689Sborman     return 1;
146438689Sborman }
146544361Sborman 
146644361Sborman /*
146744361Sborman  * The ENVIRON command.
146844361Sborman  */
146938689Sborman 
147044361Sborman struct envlist {
147144361Sborman 	char	*name;
147244361Sborman 	char	*help;
147344361Sborman 	int	(*handler)();
147444361Sborman 	int	narg;
147544361Sborman };
147644361Sborman 
147744361Sborman extern struct env_lst *env_define();
147844361Sborman extern int env_undefine();
147944361Sborman extern int env_export(), env_unexport();
148044361Sborman extern int env_list(), env_help();
148144361Sborman 
148244361Sborman struct envlist EnvList[] = {
148344361Sborman     { "define",	"Define an environment variable",
148444361Sborman 						(int (*)())env_define,	2 },
148544361Sborman     { "undefine", "Undefine an environment variable",
148644361Sborman 						env_undefine,	1 },
148744361Sborman     { "export",	"Mark an environment variable for automatic export",
148844361Sborman 						env_export,	1 },
148944361Sborman     { "unexport", "Dont mark an environment variable for automatic export",
149044361Sborman 						env_unexport,	1 },
149144361Sborman     { "list",	"List the current environment variables",
149244361Sborman 						env_list,	0 },
149344361Sborman     { "help",	0,				env_help,		0 },
149444361Sborman     { "?",	"Print help information",	env_help,		0 },
149544361Sborman     { 0 },
149644361Sborman };
149744361Sborman 
149844361Sborman static
149944361Sborman env_help()
150044361Sborman {
150144361Sborman     struct envlist *c;
150244361Sborman 
150344361Sborman     for (c = EnvList; c->name; c++) {
150444361Sborman 	if (c->help) {
150544361Sborman 	    if (*c->help)
150644361Sborman 		printf("%-15s %s\n", c->name, c->help);
150744361Sborman 	    else
150844361Sborman 		printf("\n");
150944361Sborman 	}
151044361Sborman     }
151144361Sborman }
151244361Sborman 
151344361Sborman static char **
151444361Sborman getnextenv(name)
151544361Sborman char *name;
151644361Sborman {
151744361Sborman     return (char **)(((struct envlist *)name)+1);
151844361Sborman }
151944361Sborman 
152044361Sborman static struct envlist *
152144361Sborman getenvcmd(name)
152244361Sborman char *name;
152344361Sborman {
152444361Sborman     return (struct envlist *)genget(name, (char **) EnvList, getnextenv);
152544361Sborman }
152644361Sborman 
152744361Sborman env_cmd(argc, argv)
152844361Sborman int	argc;
152944361Sborman char	*argv[];
153044361Sborman {
153144361Sborman     struct envlist *c;
153244361Sborman 
153344361Sborman     if (argc < 2) {
153444361Sborman 	fprintf(stderr,
153544361Sborman 	    "Need an argument to 'environ' command.  'environ ?' for help.\n");
153644361Sborman 	return 0;
153744361Sborman     }
153844361Sborman     c = getenvcmd(argv[1]);
153944361Sborman     if (c == 0) {
154044361Sborman         fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
154144361Sborman     				argv[1]);
154244361Sborman         return 0;
154344361Sborman     }
154444361Sborman     if (Ambiguous(c)) {
154544361Sborman         fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
154644361Sborman     				argv[1]);
154744361Sborman         return 0;
154844361Sborman     }
154944361Sborman     if (c->narg + 2 != argc) {
155044361Sborman 	fprintf(stderr,
155144361Sborman 	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
155244361Sborman 		c->narg < argc + 2 ? "only " : "",
155344361Sborman 		c->narg, c->narg == 1 ? "" : "s", c->name);
155444361Sborman 	return 0;
155544361Sborman     }
155644361Sborman     (void)(*c->handler)(argv[2], argv[3]);
155744361Sborman     return 1;
155844361Sborman }
155944361Sborman 
156044361Sborman struct env_lst {
156144361Sborman 	struct env_lst *next;	/* pointer to next structure */
156244361Sborman 	struct env_lst *prev;	/* pointer to next structure */
156344361Sborman 	char *var;		/* pointer to variable name */
156444361Sborman 	char *value;		/* pointer to varialbe value */
156544361Sborman 	int export;		/* 1 -> export with default list of variables */
156644361Sborman };
156744361Sborman 
156844361Sborman struct env_lst envlisthead;
156944361Sborman 
157044361Sborman struct env_lst *
157144361Sborman env_find(var)
157244361Sborman {
157344361Sborman 	register struct env_lst *ep;
157444361Sborman 
157544361Sborman 	for (ep = envlisthead.next; ep; ep = ep->next) {
157644361Sborman 		if (strcmp(ep->var, var) == 0)
157744361Sborman 			return(ep);
157844361Sborman 	}
157944361Sborman 	return(NULL);
158044361Sborman }
158144361Sborman 
158244361Sborman env_init()
158344361Sborman {
158444361Sborman 	extern char **environ, *index();
158544361Sborman 	register char **epp, *cp;
158644361Sborman 	register struct env_lst *ep;
158744361Sborman 
158844361Sborman 	for (epp = environ; *epp; epp++) {
158944361Sborman 		if (cp = index(*epp, '=')) {
159044361Sborman 			*cp = '\0';
159144361Sborman 			ep = env_define(*epp, cp+1);
159244361Sborman 			ep->export = 0;
159344361Sborman 			*cp = '=';
159444361Sborman 		}
159544361Sborman 	}
159644361Sborman 	/*
159744361Sborman 	 * Special case for DISPLAY variable.  If it is ":0.0" or
159844361Sborman 	 * "unix:0.0", we have to get rid of "unix" and insert our
159944361Sborman 	 * hostname.
160044361Sborman 	 */
160144361Sborman 	if ((ep = env_find("DISPLAY")) &&
160244361Sborman 	    ((*ep->value == ':') || (strncmp(ep->value, "unix:", 5) == 0))) {
160344361Sborman 		char hbuf[256+1];
160444361Sborman 		char *cp2 = index(ep->value, ':');
160544361Sborman 
160644361Sborman 		gethostname(hbuf, 256);
160744361Sborman 		hbuf[256] = '\0';
160844361Sborman 		cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
160944361Sborman 		sprintf(cp, "%s%s", hbuf, cp2);
161044361Sborman 		free(ep->value);
161144361Sborman 		ep->value = cp;
161244361Sborman 	}
1613*45008Skarels #ifdef notdef
161444361Sborman 	/*
161544361Sborman 	 * If USER is not defined, but LOGNAME is, then add
161644361Sborman 	 * USER with the value from LOGNAME.
161744361Sborman 	 */
161844361Sborman 	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME")))
161944361Sborman 		env_define("USER", ep->value);
162044361Sborman 	env_export("USER");
1621*45008Skarels #endif
162244361Sborman 	env_export("DISPLAY");
162344361Sborman 	env_export("PRINTER");
162444361Sborman }
162544361Sborman 
162644361Sborman struct env_lst *
162744361Sborman env_define(var, value)
162844361Sborman char *var, *value;
162944361Sborman {
163044361Sborman 	register struct env_lst *ep;
163144361Sborman 	extern char *savestr();
163244361Sborman 
163344361Sborman 	if (ep = env_find(var)) {
163444361Sborman 		if (ep->var)
163544361Sborman 			free(ep->var);
163644361Sborman 		if (ep->value)
163744361Sborman 			free(ep->value);
163844361Sborman 	} else {
163944361Sborman 		ep = (struct env_lst *)malloc(sizeof(struct env_lst));
164044361Sborman 		ep->export = 1;
164144361Sborman 		ep->next = envlisthead.next;
164244361Sborman 		envlisthead.next = ep;
164344361Sborman 		ep->prev = &envlisthead;
164444361Sborman 		if (ep->next)
164544361Sborman 			ep->next->prev = ep;
164644361Sborman 	}
164744361Sborman 	ep->var = savestr(var);
164844361Sborman 	ep->value = savestr(value);
164944361Sborman 	return(ep);
165044361Sborman }
165144361Sborman 
165244361Sborman env_undefine(var)
165344361Sborman char *var;
165444361Sborman {
165544361Sborman 	register struct env_lst *ep;
165644361Sborman 
165744361Sborman 	if (ep = env_find(var)) {
165844361Sborman 		ep->prev->next = ep->next;
165944361Sborman 		ep->next->prev = ep->prev;
166044361Sborman 		if (ep->var)
166144361Sborman 			free(ep->var);
166244361Sborman 		if (ep->value)
166344361Sborman 			free(ep->value);
166444361Sborman 		free(ep);
166544361Sborman 	}
166644361Sborman }
166744361Sborman 
166844361Sborman env_export(var)
166944361Sborman char *var;
167044361Sborman {
167144361Sborman 	register struct env_lst *ep;
167244361Sborman 
167344361Sborman 	if (ep = env_find(var))
167444361Sborman 		ep->export = 1;
167544361Sborman }
167644361Sborman 
167744361Sborman env_unexport(var)
167844361Sborman char *var;
167944361Sborman {
168044361Sborman 	register struct env_lst *ep;
168144361Sborman 
168244361Sborman 	if (ep = env_find(var))
168344361Sborman 		ep->export = 0;
168444361Sborman }
168544361Sborman 
168644361Sborman env_list()
168744361Sborman {
168844361Sborman 	register struct env_lst *ep;
168944361Sborman 
169044361Sborman 	for (ep = envlisthead.next; ep; ep = ep->next) {
169144361Sborman 		printf("%c %-20s %s\n", ep->export ? '*' : ' ',
169244361Sborman 					ep->var, ep->value);
169344361Sborman 	}
169444361Sborman }
169544361Sborman 
169644361Sborman char *
169744361Sborman env_default(init)
169844361Sborman {
169944361Sborman 	static struct env_lst *nep = NULL;
170044361Sborman 
170144361Sborman 	if (init) {
170244361Sborman 		nep = &envlisthead;
170344361Sborman 		return;
170444361Sborman 	}
170544361Sborman 	if (nep) {
170644361Sborman 		while (nep = nep->next) {
170744361Sborman 			if (nep->export)
170844361Sborman 				return(nep->var);
170944361Sborman 		}
171044361Sborman 	}
171144361Sborman 	return(NULL);
171244361Sborman }
171344361Sborman 
171444361Sborman char *
171544361Sborman env_getvalue(var)
171644361Sborman char *var;
171744361Sborman {
171844361Sborman 	register struct env_lst *ep;
171944361Sborman 
172044361Sborman 	if (ep = env_find(var))
172144361Sborman 		return(ep->value);
172244361Sborman 	return(NULL);
172344361Sborman }
172444361Sborman 
172544361Sborman char *
172644361Sborman savestr(s)
172744361Sborman register char *s;
172844361Sborman {
172944361Sborman 	register char *ret;
173044361Sborman 	if (ret = (char *)malloc(strlen(s)+1))
173144361Sborman 		strcpy(ret, s);
173244361Sborman 	return(ret);
173344361Sborman }
173444361Sborman 
173536274Sminshall #if	defined(unix)
173644361Sborman #ifdef notdef
173732144Sminshall /*
173836274Sminshall  * Some information about our file descriptors.
173936274Sminshall  */
174036274Sminshall 
174136274Sminshall char *
174236274Sminshall decodeflags(mask)
174336274Sminshall int mask;
174436274Sminshall {
174536274Sminshall     static char buffer[100];
174636274Sminshall #define do(m,s) \
174736274Sminshall 	if (mask&(m)) { \
174836274Sminshall 	    strcat(buffer, (s)); \
174936274Sminshall 	}
175036274Sminshall 
175136274Sminshall     buffer[0] = 0;			/* Terminate it */
175236274Sminshall 
175336274Sminshall #ifdef FREAD
175436274Sminshall     do(FREAD, " FREAD");
175536274Sminshall #endif
175636274Sminshall #ifdef FWRITE
175736274Sminshall     do(FWRITE, " FWRITE");
175836274Sminshall #endif
175936274Sminshall #ifdef F_DUPFP
176036274Sminshall     do(F_DUPFD, " F_DUPFD");
176136274Sminshall #endif
176236274Sminshall #ifdef FNDELAY
176336274Sminshall     do(FNDELAY, " FNDELAY");
176436274Sminshall #endif
176536274Sminshall #ifdef FAPPEND
176636274Sminshall     do(FAPPEND, " FAPPEND");
176736274Sminshall #endif
176836274Sminshall #ifdef FMARK
176936274Sminshall     do(FMARK, " FMARK");
177036274Sminshall #endif
177136274Sminshall #ifdef FDEFER
177236274Sminshall     do(FDEFER, " FDEFER");
177336274Sminshall #endif
177436274Sminshall #ifdef FASYNC
177536274Sminshall     do(FASYNC, " FASYNC");
177636274Sminshall #endif
177736274Sminshall #ifdef FSHLOCK
177836274Sminshall     do(FSHLOCK, " FSHLOCK");
177936274Sminshall #endif
178036274Sminshall #ifdef FEXLOCK
178136274Sminshall     do(FEXLOCK, " FEXLOCK");
178236274Sminshall #endif
178336274Sminshall #ifdef FCREAT
178436274Sminshall     do(FCREAT, " FCREAT");
178536274Sminshall #endif
178636274Sminshall #ifdef FTRUNC
178736274Sminshall     do(FTRUNC, " FTRUNC");
178836274Sminshall #endif
178936274Sminshall #ifdef FEXCL
179036274Sminshall     do(FEXCL, " FEXCL");
179136274Sminshall #endif
179236274Sminshall 
179336274Sminshall     return buffer;
179436274Sminshall }
179536274Sminshall #undef do
179644361Sborman #endif	/* notdef */
179736274Sminshall 
179844361Sborman #if defined(TN3270)
179936274Sminshall static void
180036274Sminshall filestuff(fd)
180136274Sminshall int fd;
180236274Sminshall {
180336274Sminshall     int res;
180436274Sminshall 
180538689Sborman #ifdef	F_GETOWN
180638689Sborman     setconnmode(0);
180736274Sminshall     res = fcntl(fd, F_GETOWN, 0);
180836274Sminshall     setcommandmode();
180936274Sminshall 
181036274Sminshall     if (res == -1) {
181136274Sminshall 	perror("fcntl");
181236274Sminshall 	return;
181336274Sminshall     }
181436274Sminshall     printf("\tOwner is %d.\n", res);
181538689Sborman #endif
181636274Sminshall 
181738689Sborman     setconnmode(0);
181836274Sminshall     res = fcntl(fd, F_GETFL, 0);
181936274Sminshall     setcommandmode();
182036274Sminshall 
182136274Sminshall     if (res == -1) {
182236274Sminshall 	perror("fcntl");
182336274Sminshall 	return;
182436274Sminshall     }
182536274Sminshall     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
182636274Sminshall }
182744361Sborman #endif /* defined(TN3270) */
182836274Sminshall 
182936274Sminshall 
183036274Sminshall #endif	/* defined(unix) */
183136274Sminshall 
183236274Sminshall /*
183332144Sminshall  * Print status about the connection.
183432144Sminshall  */
183534849Sminshall /*ARGSUSED*/
183632144Sminshall static
183732144Sminshall status(argc, argv)
183832144Sminshall int	argc;
183932144Sminshall char	*argv[];
184032144Sminshall {
184132144Sminshall     if (connected) {
184232144Sminshall 	printf("Connected to %s.\n", hostname);
184336242Sminshall 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
184438689Sborman 	    int mode = getconnmode();
184538689Sborman 
184638689Sborman 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
184738689Sborman 		printf("Operating with LINEMODE option\n");
184838689Sborman 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
184938689Sborman 		printf("%s catching of signals\n",
185038689Sborman 					(mode&MODE_TRAPSIG) ? "Local" : "No");
185138689Sborman 		slcstate();
185238689Sborman #ifdef	KLUDGELINEMODE
185339529Sborman 	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
185438689Sborman 		printf("Operating in obsolete linemode\n");
185538689Sborman #endif
185638689Sborman 	    } else {
185738689Sborman 		printf("Operating in single character mode\n");
185838689Sborman 		if (localchars)
185938689Sborman 		    printf("Catching signals locally\n");
186032144Sminshall 	    }
186138689Sborman 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
186238689Sborman 	    if (my_want_state_is_will(TELOPT_LFLOW))
186338689Sborman 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
186432144Sminshall 	}
186532144Sminshall     } else {
186632144Sminshall 	printf("No connection.\n");
186732144Sminshall     }
186832144Sminshall #   if !defined(TN3270)
186932144Sminshall     printf("Escape character is '%s'.\n", control(escape));
187034849Sminshall     (void) fflush(stdout);
187132144Sminshall #   else /* !defined(TN3270) */
187232144Sminshall     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
187332144Sminshall 	printf("Escape character is '%s'.\n", control(escape));
187432144Sminshall     }
187532144Sminshall #   if defined(unix)
187636242Sminshall     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
187736242Sminshall 	printf("SIGIO received %d time%s.\n",
187836242Sminshall 				sigiocount, (sigiocount == 1)? "":"s");
187936274Sminshall 	if (In3270) {
188036274Sminshall 	    printf("Process ID %d, process group %d.\n",
188136274Sminshall 					    getpid(), getpgrp(getpid()));
188236274Sminshall 	    printf("Terminal input:\n");
188336274Sminshall 	    filestuff(tin);
188436274Sminshall 	    printf("Terminal output:\n");
188536274Sminshall 	    filestuff(tout);
188636274Sminshall 	    printf("Network socket:\n");
188736274Sminshall 	    filestuff(net);
188836274Sminshall 	}
188936242Sminshall     }
189032144Sminshall     if (In3270 && transcom) {
189132144Sminshall        printf("Transparent mode command is '%s'.\n", transcom);
189232144Sminshall     }
189332144Sminshall #   endif /* defined(unix) */
189434849Sminshall     (void) fflush(stdout);
189532144Sminshall     if (In3270) {
189632144Sminshall 	return 0;
189732144Sminshall     }
189832144Sminshall #   endif /* defined(TN3270) */
189932144Sminshall     return 1;
190032144Sminshall }
190132144Sminshall 
190232144Sminshall 
190344361Sborman #if	defined(NEED_GETTOS)
190444361Sborman struct tosent {
190544361Sborman 	char	*t_name;	/* name */
190644361Sborman 	char	**t_aliases;	/* alias list */
190744361Sborman 	char	*t_proto;	/* protocol */
190844361Sborman 	int	t_tos;		/* Type Of Service bits */
190944361Sborman };
191044361Sborman 
191144361Sborman struct tosent *
191244361Sborman gettosbyname(name, proto)
191344361Sborman char *name, *proto;
191444361Sborman {
191544361Sborman 	static struct tosent te;
191644361Sborman 	static char *aliasp = 0;
191744361Sborman 
191844361Sborman 	te.t_name = name;
191944361Sborman 	te.t_aliases = &aliasp;
192044361Sborman 	te.t_proto = proto;
192144361Sborman 	te.t_tos = 020; /* Low Delay bit */
192244361Sborman 	return(&te);
192344361Sborman }
192444361Sborman #endif
192544361Sborman 
1926*45008Skarels extern	int autologin;
1927*45008Skarels 
192832144Sminshall int
192932144Sminshall tn(argc, argv)
193032144Sminshall 	int argc;
193132144Sminshall 	char *argv[];
193232144Sminshall {
193332144Sminshall     register struct hostent *host = 0;
193432144Sminshall     struct sockaddr_in sin;
193532144Sminshall     struct servent *sp = 0;
193632144Sminshall     static char	hnamebuf[32];
193738689Sborman     unsigned long temp, inet_addr();
193837219Sminshall     extern char *inet_ntoa();
193938689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
194038689Sborman     char *srp = 0, *strrchr();
194138689Sborman     unsigned long sourceroute(), srlen;
194238689Sborman #endif
194344361Sborman #if defined(HAS_IP_TOS) || defined(NEED_GETTOS)
194440245Sborman     struct tosent *tp;
194544361Sborman #endif /* defined(HAS_IP_TOS) || defined(NEED_GETTOS) */
194644361Sborman     char *cmd, *hostp = 0, *portp = 0, *user = 0;
194732144Sminshall 
194832144Sminshall 
194932144Sminshall     if (connected) {
195032144Sminshall 	printf("?Already connected to %s\n", hostname);
195132144Sminshall 	return 0;
195232144Sminshall     }
195332144Sminshall     if (argc < 2) {
195432144Sminshall 	(void) strcpy(line, "Connect ");
195532144Sminshall 	printf("(to) ");
195634849Sminshall 	(void) gets(&line[strlen(line)]);
195732144Sminshall 	makeargv();
195832144Sminshall 	argc = margc;
195932144Sminshall 	argv = margv;
196032144Sminshall     }
196144361Sborman     cmd = *argv;
196244361Sborman     --argc; ++argv;
196344361Sborman     while (argc) {
196444361Sborman 	if (strcmp(*argv, "-l") == 0) {
196544361Sborman 	    --argc; ++argv;
196644361Sborman 	    if (argc == 0)
196744361Sborman 		goto usage;
196844361Sborman 	    user = *argv++;
196944361Sborman 	    --argc;
197044361Sborman 	    continue;
197144361Sborman 	}
197244361Sborman 	if (hostp == 0) {
197344361Sborman 	    hostp = *argv++;
197444361Sborman 	    --argc;
197544361Sborman 	    continue;
197644361Sborman 	}
197744361Sborman 	if (portp == 0) {
197844361Sborman 	    portp = *argv++;
197944361Sborman 	    --argc;
198044361Sborman 	    continue;
198144361Sborman 	}
198244361Sborman     usage:
198344361Sborman 	printf("usage: %s [-l user] host-name [port]\n", cmd);
198432144Sminshall 	return 0;
198532144Sminshall     }
198638689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
198744361Sborman     if (hostp[0] == '@' || hostp[0] == '!') {
198844361Sborman 	if ((hostname = strrchr(hostp, ':')) == NULL)
198944361Sborman 	    hostname = strrchr(hostp, '@');
199038689Sborman 	hostname++;
199138689Sborman 	srp = 0;
199244361Sborman 	temp = sourceroute(hostp, &srp, &srlen);
199338689Sborman 	if (temp == 0) {
199438689Sborman 	    herror(srp);
199538689Sborman 	    return 0;
199638689Sborman 	} else if (temp == -1) {
199744361Sborman 	    printf("Bad source route option: %s\n", hostp);
199838689Sborman 	    return 0;
199938689Sborman 	} else {
200038689Sborman 	    sin.sin_addr.s_addr = temp;
200138689Sborman 	    sin.sin_family = AF_INET;
200238689Sborman 	}
200332144Sminshall     } else {
200438689Sborman #endif
200544361Sborman 	temp = inet_addr(hostp);
200638689Sborman 	if (temp != (unsigned long) -1) {
200738689Sborman 	    sin.sin_addr.s_addr = temp;
200838689Sborman 	    sin.sin_family = AF_INET;
200944361Sborman 	    (void) strcpy(hnamebuf, hostp);
201038689Sborman 	    hostname = hnamebuf;
201138689Sborman 	} else {
201244361Sborman 	    host = gethostbyname(hostp);
201338689Sborman 	    if (host) {
201438689Sborman 		sin.sin_family = host->h_addrtype;
201532144Sminshall #if	defined(h_addr)		/* In 4.3, this is a #define */
201638689Sborman 		memcpy((caddr_t)&sin.sin_addr,
201732144Sminshall 				host->h_addr_list[0], host->h_length);
201832144Sminshall #else	/* defined(h_addr) */
201938689Sborman 		memcpy((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
202032144Sminshall #endif	/* defined(h_addr) */
202138689Sborman 		hostname = host->h_name;
202238689Sborman 	    } else {
202344361Sborman 		herror(hostp);
202438689Sborman 		return 0;
202538689Sborman 	    }
202632144Sminshall 	}
202738689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
202832144Sminshall     }
202938689Sborman #endif
203044361Sborman     if (portp) {
203144361Sborman 	if (*portp == '-') {
203244361Sborman 	    portp++;
203338689Sborman 	    telnetport = 1;
203438689Sborman 	} else
203538689Sborman 	    telnetport = 0;
203644361Sborman 	sin.sin_port = atoi(portp);
203732144Sminshall 	if (sin.sin_port == 0) {
203844361Sborman 	    sp = getservbyname(portp, "tcp");
203932144Sminshall 	    if (sp)
204032144Sminshall 		sin.sin_port = sp->s_port;
204132144Sminshall 	    else {
204244361Sborman 		printf("%s: bad port number\n", portp);
204332144Sminshall 		return 0;
204432144Sminshall 	    }
204532144Sminshall 	} else {
204634849Sminshall #if	!defined(htons)
204734849Sminshall 	    u_short htons();
204834849Sminshall #endif	/* !defined(htons) */
204932144Sminshall 	    sin.sin_port = htons(sin.sin_port);
205032144Sminshall 	}
205132144Sminshall     } else {
205232144Sminshall 	if (sp == 0) {
205332144Sminshall 	    sp = getservbyname("telnet", "tcp");
205432144Sminshall 	    if (sp == 0) {
205534849Sminshall 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
205632144Sminshall 		return 0;
205732144Sminshall 	    }
205832144Sminshall 	    sin.sin_port = sp->s_port;
205932144Sminshall 	}
206032144Sminshall 	telnetport = 1;
206132144Sminshall     }
206237219Sminshall     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
206332144Sminshall     do {
206432144Sminshall 	net = socket(AF_INET, SOCK_STREAM, 0);
206532144Sminshall 	if (net < 0) {
206632144Sminshall 	    perror("telnet: socket");
206732144Sminshall 	    return 0;
206832144Sminshall 	}
206938689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
207038689Sborman 	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
207138689Sborman 		perror("setsockopt (IP_OPTIONS)");
207238689Sborman #endif
207344361Sborman #if	defined(HAS_IP_TOS) || defined(NEED_GETTOS)
207444361Sborman 	if ((tp = gettosbyname("telnet", "tcp")) &&
207544361Sborman 	    (setsockopt(net, IPPROTO_IP, IP_TOS, &tp->t_tos, sizeof(int)) < 0))
207640245Sborman 		perror("telnet: setsockopt TOS (ignored)");
207744361Sborman #endif	/* defined(HAS_IP_TOS) || defined(NEED_GETTOS) */
207840245Sborman 
207932144Sminshall 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
208032144Sminshall 		perror("setsockopt (SO_DEBUG)");
208132144Sminshall 	}
208232144Sminshall 
208332144Sminshall 	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
208432144Sminshall #if	defined(h_addr)		/* In 4.3, this is a #define */
208532144Sminshall 	    if (host && host->h_addr_list[1]) {
208632144Sminshall 		int oerrno = errno;
208732144Sminshall 
208832144Sminshall 		fprintf(stderr, "telnet: connect to address %s: ",
208932144Sminshall 						inet_ntoa(sin.sin_addr));
209032144Sminshall 		errno = oerrno;
209132144Sminshall 		perror((char *)0);
209232144Sminshall 		host->h_addr_list++;
209332144Sminshall 		memcpy((caddr_t)&sin.sin_addr,
209432144Sminshall 			host->h_addr_list[0], host->h_length);
209532144Sminshall 		(void) NetClose(net);
209632144Sminshall 		continue;
209732144Sminshall 	    }
209832144Sminshall #endif	/* defined(h_addr) */
209932144Sminshall 	    perror("telnet: Unable to connect to remote host");
210032144Sminshall 	    return 0;
210137219Sminshall 	}
210232144Sminshall 	connected++;
210332144Sminshall     } while (connected == 0);
210444361Sborman     cmdrc(hostp, hostname);
2105*45008Skarels     if (autologin && user == NULL) {
2106*45008Skarels 	struct passwd *pw;
2107*45008Skarels 	uid_t uid = getuid();
2108*45008Skarels 
2109*45008Skarels 	user = getlogin();
2110*45008Skarels 	if (user == NULL ||
2111*45008Skarels 	    (pw = getpwnam(user)) && pw->pw_uid != uid)
2112*45008Skarels 		if (pw = getpwuid(uid))
2113*45008Skarels 			user = pw->pw_name;
2114*45008Skarels 		else
2115*45008Skarels 			user = NULL;
2116*45008Skarels     }
2117*45008Skarels     if (user) {
211844361Sborman 	env_define("USER", user);
2119*45008Skarels 	env_export("USER");
2120*45008Skarels     }
212134849Sminshall     (void) call(status, "status", "notmuch", 0);
212232144Sminshall     if (setjmp(peerdied) == 0)
212332144Sminshall 	telnet();
212434849Sminshall     (void) NetClose(net);
212532381Sminshall     ExitString("Connection closed by foreign host.\n",1);
212632144Sminshall     /*NOTREACHED*/
212732144Sminshall }
212832144Sminshall 
212932144Sminshall 
213032144Sminshall #define HELPINDENT (sizeof ("connect"))
213132144Sminshall 
213232144Sminshall static char
213332144Sminshall 	openhelp[] =	"connect to a site",
213432144Sminshall 	closehelp[] =	"close current connection",
213532144Sminshall 	quithelp[] =	"exit telnet",
213632144Sminshall 	statushelp[] =	"print status information",
213732144Sminshall 	helphelp[] =	"print help information",
213832144Sminshall 	sendhelp[] =	"transmit special characters ('send ?' for more)",
213932144Sminshall 	sethelp[] = 	"set operating parameters ('set ?' for more)",
214038689Sborman 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
214132144Sminshall 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
214238689Sborman 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
214332144Sminshall 	displayhelp[] =	"display operating parameters",
214432144Sminshall #if	defined(TN3270) && defined(unix)
214532144Sminshall 	transcomhelp[] = "specify Unix command for transparent mode pipe",
214632144Sminshall #endif	/* defined(TN3270) && defined(unix) */
214732144Sminshall #if	defined(unix)
214832144Sminshall 	zhelp[] =	"suspend telnet",
214943317Skfall #endif	/* defined(unix) */
215032144Sminshall 	shellhelp[] =	"invoke a subshell",
215144361Sborman 	envhelp[] =	"change environment variables ('environ ?' for more)",
215244361Sborman 	modestring[] = "try to enter line or character mode ('mode ?' for more)";
215332144Sminshall 
215432144Sminshall extern int	help(), shell();
215532144Sminshall 
215632144Sminshall static Command cmdtab[] = {
215738689Sborman 	{ "close",	closehelp,	bye,		1 },
215838689Sborman 	{ "display",	displayhelp,	display,	0 },
215938689Sborman 	{ "mode",	modestring,	modecmd,	0 },
216038689Sborman 	{ "open",	openhelp,	tn,		0 },
216138689Sborman 	{ "quit",	quithelp,	quit,		0 },
216238689Sborman 	{ "send",	sendhelp,	sendcmd,	0 },
216338689Sborman 	{ "set",	sethelp,	setcmd,		0 },
216438689Sborman 	{ "unset",	unsethelp,	unsetcmd,	0 },
216538689Sborman 	{ "status",	statushelp,	status,		0 },
216638689Sborman 	{ "toggle",	togglestring,	toggle,		0 },
216738689Sborman 	{ "slc",	slchelp,	slccmd,		0 },
216832144Sminshall #if	defined(TN3270) && defined(unix)
216938689Sborman 	{ "transcom",	transcomhelp,	settranscom,	0 },
217032144Sminshall #endif	/* defined(TN3270) && defined(unix) */
217132144Sminshall #if	defined(unix)
217238689Sborman 	{ "z",		zhelp,		suspend,	0 },
217332144Sminshall #endif	/* defined(unix) */
217432144Sminshall #if	defined(TN3270)
217538689Sborman 	{ "!",		shellhelp,	shell,		1 },
217638689Sborman #else
217738689Sborman 	{ "!",		shellhelp,	shell,		0 },
217838689Sborman #endif
217944361Sborman 	{ "environ",	envhelp,	env_cmd,	0 },
218038689Sborman 	{ "?",		helphelp,	help,		0 },
218132144Sminshall 	0
218232144Sminshall };
218332144Sminshall 
218432144Sminshall static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
218532144Sminshall static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
218632144Sminshall 
218732144Sminshall static Command cmdtab2[] = {
218838689Sborman 	{ "help",	0,		help,		0 },
218938689Sborman 	{ "escape",	escapehelp,	setescape,	0 },
219038689Sborman 	{ "crmod",	crmodhelp,	togcrmod,	0 },
219132144Sminshall 	0
219232144Sminshall };
219332144Sminshall 
219435298Sminshall 
219532144Sminshall /*
219632144Sminshall  * Call routine with argc, argv set from args (terminated by 0).
219732144Sminshall  */
219835298Sminshall 
219935417Sminshall /*VARARGS1*/
220032144Sminshall static
220135298Sminshall call(va_alist)
220235298Sminshall va_dcl
220332144Sminshall {
220435298Sminshall     va_list ap;
220535298Sminshall     typedef int (*intrtn_t)();
220635298Sminshall     intrtn_t routine;
220735298Sminshall     char *args[100];
220835298Sminshall     int argno = 0;
220935298Sminshall 
221035298Sminshall     va_start(ap);
221135298Sminshall     routine = (va_arg(ap, intrtn_t));
221235495Sminshall     while ((args[argno++] = va_arg(ap, char *)) != 0) {
221335298Sminshall 	;
221435495Sminshall     }
221535298Sminshall     va_end(ap);
221635495Sminshall     return (*routine)(argno-1, args);
221732144Sminshall }
221832144Sminshall 
221935298Sminshall 
222032144Sminshall static char **
222132144Sminshall getnextcmd(name)
222232144Sminshall char *name;
222332144Sminshall {
222432144Sminshall     Command *c = (Command *) name;
222532144Sminshall 
222632144Sminshall     return (char **) (c+1);
222732144Sminshall }
222832144Sminshall 
222932144Sminshall static Command *
223032144Sminshall getcmd(name)
223132144Sminshall char *name;
223232144Sminshall {
223332144Sminshall     Command *cm;
223432144Sminshall 
223532144Sminshall     if ((cm = (Command *) genget(name, (char **) cmdtab, getnextcmd)) != 0) {
223632144Sminshall 	return cm;
223732144Sminshall     } else {
223832144Sminshall 	return (Command *) genget(name, (char **) cmdtab2, getnextcmd);
223932144Sminshall     }
224032144Sminshall }
224132144Sminshall 
224232144Sminshall void
224338689Sborman command(top, tbuf, cnt)
224432144Sminshall 	int top;
224538689Sborman 	char *tbuf;
224638689Sborman 	int cnt;
224732144Sminshall {
224832144Sminshall     register Command *c;
224932144Sminshall 
225032144Sminshall     setcommandmode();
225132144Sminshall     if (!top) {
225232144Sminshall 	putchar('\n');
225337219Sminshall #if	defined(unix)
225432144Sminshall     } else {
225544361Sborman 	(void) signal(SIGINT, SIG_DFL);
225644361Sborman 	(void) signal(SIGQUIT, SIG_DFL);
225732144Sminshall #endif	/* defined(unix) */
225832144Sminshall     }
225932144Sminshall     for (;;) {
226032144Sminshall 	printf("%s> ", prompt);
226138689Sborman 	if (tbuf) {
226238689Sborman 	    register char *cp;
226338689Sborman 	    cp = line;
226438689Sborman 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
226538689Sborman 		cnt--;
226638689Sborman 	    tbuf = 0;
226738689Sborman 	    if (cp == line || *--cp != '\n' || cp == line)
226838689Sborman 		goto getline;
226938689Sborman 	    *cp = '\0';
227038689Sborman 	    printf("%s\n", line);
227138689Sborman 	} else {
227238689Sborman 	getline:
227338689Sborman 	    if (gets(line) == NULL) {
227444361Sborman 		if (feof(stdin) || ferror(stdin)) {
227544361Sborman 		    (void) quit();
227644361Sborman 		    /*NOTREACHED*/
227744361Sborman 		}
227838689Sborman 		break;
227938689Sborman 	    }
228032144Sminshall 	}
228132144Sminshall 	if (line[0] == 0)
228232144Sminshall 	    break;
228332144Sminshall 	makeargv();
228437219Sminshall 	if (margv[0] == 0) {
228537219Sminshall 	    break;
228637219Sminshall 	}
228732144Sminshall 	c = getcmd(margv[0]);
228832144Sminshall 	if (Ambiguous(c)) {
228932144Sminshall 	    printf("?Ambiguous command\n");
229032144Sminshall 	    continue;
229132144Sminshall 	}
229232144Sminshall 	if (c == 0) {
229332144Sminshall 	    printf("?Invalid command\n");
229432144Sminshall 	    continue;
229532144Sminshall 	}
229632144Sminshall 	if (c->needconnect && !connected) {
229732144Sminshall 	    printf("?Need to be connected first.\n");
229832144Sminshall 	    continue;
229932144Sminshall 	}
230032144Sminshall 	if ((*c->handler)(margc, margv)) {
230132144Sminshall 	    break;
230232144Sminshall 	}
230332144Sminshall     }
230432144Sminshall     if (!top) {
230532144Sminshall 	if (!connected) {
230632144Sminshall 	    longjmp(toplevel, 1);
230732144Sminshall 	    /*NOTREACHED*/
230832144Sminshall 	}
230932144Sminshall #if	defined(TN3270)
231032144Sminshall 	if (shell_active == 0) {
231138689Sborman 	    setconnmode(0);
231232144Sminshall 	}
231332144Sminshall #else	/* defined(TN3270) */
231438689Sborman 	setconnmode(0);
231532144Sminshall #endif	/* defined(TN3270) */
231632144Sminshall     }
231732144Sminshall }
231832144Sminshall 
231932144Sminshall /*
232032144Sminshall  * Help command.
232132144Sminshall  */
232232144Sminshall static
232332144Sminshall help(argc, argv)
232432144Sminshall 	int argc;
232532144Sminshall 	char *argv[];
232632144Sminshall {
232732144Sminshall 	register Command *c;
232832144Sminshall 
232932144Sminshall 	if (argc == 1) {
233032144Sminshall 		printf("Commands may be abbreviated.  Commands are:\n\n");
233132144Sminshall 		for (c = cmdtab; c->name; c++)
233238689Sborman 			if (c->help) {
233332144Sminshall 				printf("%-*s\t%s\n", HELPINDENT, c->name,
233432144Sminshall 								    c->help);
233532144Sminshall 			}
233632144Sminshall 		return 0;
233732144Sminshall 	}
233832144Sminshall 	while (--argc > 0) {
233932144Sminshall 		register char *arg;
234032144Sminshall 		arg = *++argv;
234132144Sminshall 		c = getcmd(arg);
234232144Sminshall 		if (Ambiguous(c))
234332144Sminshall 			printf("?Ambiguous help command %s\n", arg);
234432144Sminshall 		else if (c == (Command *)0)
234532144Sminshall 			printf("?Invalid help command %s\n", arg);
234632144Sminshall 		else
234732144Sminshall 			printf("%s\n", c->help);
234832144Sminshall 	}
234932144Sminshall 	return 0;
235032144Sminshall }
235138689Sborman 
235238689Sborman static char *rcname = 0;
235338689Sborman static char rcbuf[128];
235438689Sborman 
235538689Sborman cmdrc(m1, m2)
235638689Sborman 	char *m1, *m2;
235738689Sborman {
235838689Sborman     register Command *c;
235938689Sborman     FILE *rcfile;
236038689Sborman     int gotmachine = 0;
236138689Sborman     int l1 = strlen(m1);
236238689Sborman     int l2 = strlen(m2);
236338689Sborman     char m1save[64];
236438689Sborman 
236538689Sborman     strcpy(m1save, m1);
236638689Sborman     m1 = m1save;
236738689Sborman 
236838689Sborman     if (rcname == 0) {
236938689Sborman 	rcname = getenv("HOME");
237038689Sborman 	if (rcname)
237138689Sborman 	    strcpy(rcbuf, rcname);
237238689Sborman 	else
237338689Sborman 	    rcbuf[0] = '\0';
237438689Sborman 	strcat(rcbuf, "/.telnetrc");
237538689Sborman 	rcname = rcbuf;
237638689Sborman     }
237738689Sborman 
237838689Sborman     if ((rcfile = fopen(rcname, "r")) == 0) {
237938689Sborman 	return;
238038689Sborman     }
238138689Sborman 
238238689Sborman     for (;;) {
238338689Sborman 	if (fgets(line, sizeof(line), rcfile) == NULL)
238438689Sborman 	    break;
238538689Sborman 	if (line[0] == 0)
238638689Sborman 	    break;
238738689Sborman 	if (line[0] == '#')
238838689Sborman 	    continue;
238938689Sborman 	if (gotmachine == 0) {
239038689Sborman 	    if (isspace(line[0]))
239138689Sborman 		continue;
239238689Sborman 	    if (strncasecmp(line, m1, l1) == 0)
239338689Sborman 		strncpy(line, &line[l1], sizeof(line) - l1);
239438689Sborman 	    else if (strncasecmp(line, m2, l2) == 0)
239538689Sborman 		strncpy(line, &line[l2], sizeof(line) - l2);
239638689Sborman 	    else
239738689Sborman 		continue;
239838689Sborman 	    gotmachine = 1;
239938689Sborman 	} else {
240038689Sborman 	    if (!isspace(line[0])) {
240138689Sborman 		gotmachine = 0;
240238689Sborman 		continue;
240338689Sborman 	    }
240438689Sborman 	}
240538689Sborman 	makeargv();
240638689Sborman 	if (margv[0] == 0)
240738689Sborman 	    continue;
240838689Sborman 	c = getcmd(margv[0]);
240938689Sborman 	if (Ambiguous(c)) {
241038689Sborman 	    printf("?Ambiguous command: %s\n", margv[0]);
241138689Sborman 	    continue;
241238689Sborman 	}
241338689Sborman 	if (c == 0) {
241438689Sborman 	    printf("?Invalid command: %s\n", margv[0]);
241538689Sborman 	    continue;
241638689Sborman 	}
241738689Sborman 	/*
241838689Sborman 	 * This should never happen...
241938689Sborman 	 */
242038689Sborman 	if (c->needconnect && !connected) {
242138689Sborman 	    printf("?Need to be connected first for %s.\n", margv[0]);
242238689Sborman 	    continue;
242338689Sborman 	}
242438689Sborman 	(*c->handler)(margc, margv);
242538689Sborman     }
242638689Sborman     fclose(rcfile);
242738689Sborman }
242838689Sborman 
242938689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
243038689Sborman 
243138689Sborman /*
243238689Sborman  * Source route is handed in as
243338689Sborman  *	[!]@hop1@hop2...[@|:]dst
243438689Sborman  * If the leading ! is present, it is a
243538689Sborman  * strict source route, otherwise it is
243638689Sborman  * assmed to be a loose source route.
243738689Sborman  *
243838689Sborman  * We fill in the source route option as
243938689Sborman  *	hop1,hop2,hop3...dest
244038689Sborman  * and return a pointer to hop1, which will
244138689Sborman  * be the address to connect() to.
244238689Sborman  *
244338689Sborman  * Arguments:
244438689Sborman  *	arg:	pointer to route list to decipher
244538689Sborman  *
244638689Sborman  *	cpp: 	If *cpp is not equal to NULL, this is a
244738689Sborman  *		pointer to a pointer to a character array
244838689Sborman  *		that should be filled in with the option.
244938689Sborman  *
245038689Sborman  *	lenp:	pointer to an integer that contains the
245138689Sborman  *		length of *cpp if *cpp != NULL.
245238689Sborman  *
245338689Sborman  * Return values:
245438689Sborman  *
245538689Sborman  *	Returns the address of the host to connect to.  If the
245638689Sborman  *	return value is -1, there was a syntax error in the
245738689Sborman  *	option, either unknown characters, or too many hosts.
245838689Sborman  *	If the return value is 0, one of the hostnames in the
245938689Sborman  *	path is unknown, and *cpp is set to point to the bad
246038689Sborman  *	hostname.
246138689Sborman  *
246238689Sborman  *	*cpp:	If *cpp was equal to NULL, it will be filled
246338689Sborman  *		in with a pointer to our static area that has
246438689Sborman  *		the option filled in.  This will be 32bit aligned.
246538689Sborman  *
246638689Sborman  *	*lenp:	This will be filled in with how long the option
246738689Sborman  *		pointed to by *cpp is.
246838689Sborman  *
246938689Sborman  */
247038689Sborman unsigned long
247138689Sborman sourceroute(arg, cpp, lenp)
247238689Sborman char	*arg;
247338689Sborman char	**cpp;
247438689Sborman int	*lenp;
247538689Sborman {
247638689Sborman 	static char lsr[44];
247738689Sborman 	char *cp, *cp2, *lsrp, *lsrep, *index();
247838689Sborman 	register int tmp;
247938689Sborman 	struct in_addr sin_addr;
248038689Sborman 	register struct hostent *host = 0;
248138689Sborman 	register char c;
248238689Sborman 
248338689Sborman 	/*
248438689Sborman 	 * Verify the arguments, and make sure we have
248538689Sborman 	 * at least 7 bytes for the option.
248638689Sborman 	 */
248738689Sborman 	if (cpp == NULL || lenp == NULL)
248838689Sborman 		return((unsigned long)-1);
248938689Sborman 	if (*cpp != NULL && *lenp < 7)
249038689Sborman 		return((unsigned long)-1);
249138689Sborman 	/*
249238689Sborman 	 * Decide whether we have a buffer passed to us,
249338689Sborman 	 * or if we need to use our own static buffer.
249438689Sborman 	 */
249538689Sborman 	if (*cpp) {
249638689Sborman 		lsrp = *cpp;
249738689Sborman 		lsrep = lsrp + *lenp;
249838689Sborman 	} else {
249938689Sborman 		*cpp = lsrp = lsr;
250038689Sborman 		lsrep = lsrp + 44;
250138689Sborman 	}
250238689Sborman 
250338689Sborman 	cp = arg;
250438689Sborman 
250538689Sborman 	/*
250638689Sborman 	 * Next, decide whether we have a loose source
250738689Sborman 	 * route or a strict source route, and fill in
250838689Sborman 	 * the begining of the option.
250938689Sborman 	 */
251038689Sborman 	if (*cp == '!') {
251138689Sborman 		cp++;
251238689Sborman 		*lsrp++ = IPOPT_SSRR;
251338689Sborman 	} else
251438689Sborman 		*lsrp++ = IPOPT_LSRR;
251538689Sborman 
251638689Sborman 	if (*cp != '@')
251738689Sborman 		return((unsigned long)-1);
251838689Sborman 
251938689Sborman 	lsrp++;		/* skip over length, we'll fill it in later */
252038689Sborman 	*lsrp++ = 4;
252138689Sborman 
252238689Sborman 	cp++;
252338689Sborman 
252438689Sborman 	sin_addr.s_addr = 0;
252538689Sborman 
252638689Sborman 	for (c = 0;;) {
252738689Sborman 		if (c == ':')
252838689Sborman 			cp2 = 0;
252938689Sborman 		else for (cp2 = cp; c = *cp2; cp2++) {
253038689Sborman 			if (c == ',') {
253138689Sborman 				*cp2++ = '\0';
253238689Sborman 				if (*cp2 == '@')
253338689Sborman 					cp2++;
253438689Sborman 			} else if (c == '@') {
253538689Sborman 				*cp2++ = '\0';
253638689Sborman 			} else if (c == ':') {
253738689Sborman 				*cp2++ = '\0';
253838689Sborman 			} else
253938689Sborman 				continue;
254038689Sborman 			break;
254138689Sborman 		}
254238689Sborman 		if (!c)
254338689Sborman 			cp2 = 0;
254438689Sborman 
254538689Sborman 		if ((tmp = inet_addr(cp)) != -1) {
254638689Sborman 			sin_addr.s_addr = tmp;
254738689Sborman 		} else if (host = gethostbyname(cp)) {
254838689Sborman #if	defined(h_addr)
254938689Sborman 			memcpy((caddr_t)&sin_addr,
255038689Sborman 				host->h_addr_list[0], host->h_length);
255138689Sborman #else
255238689Sborman 			memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length);
255338689Sborman #endif
255438689Sborman 		} else {
255538689Sborman 			*cpp = cp;
255638689Sborman 			return(0);
255738689Sborman 		}
255838689Sborman 		memcpy(lsrp, (char *)&sin_addr, 4);
255938689Sborman 		lsrp += 4;
256038689Sborman 		if (cp2)
256138689Sborman 			cp = cp2;
256238689Sborman 		else
256338689Sborman 			break;
256438689Sborman 		/*
256538689Sborman 		 * Check to make sure there is space for next address
256638689Sborman 		 */
256738689Sborman 		if (lsrp + 4 > lsrep)
256838689Sborman 			return((unsigned long)-1);
256938689Sborman 	}
257038689Sborman 	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
257138689Sborman 		*cpp = 0;
257238689Sborman 		*lenp = 0;
257338689Sborman 		return((unsigned long)-1);
257438689Sborman 	}
257538689Sborman 	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */
257638689Sborman 	*lenp = lsrp - *cpp;
257738689Sborman 	return(sin_addr.s_addr);
257838689Sborman }
257938689Sborman #endif
258038689Sborman 
258138909Sborman #if	defined(NOSTRNCASECMP)
258238689Sborman strncasecmp(p1, p2, len)
258338689Sborman register char *p1, *p2;
258438689Sborman int len;
258538689Sborman {
258638689Sborman     while (len--) {
258738689Sborman 	if (tolower(*p1) != tolower(*p2))
258838689Sborman 	   return(tolower(*p1) - tolower(*p2));
258938689Sborman 	if (*p1 == '\0')
259038689Sborman 	    return(0);
259138689Sborman 	p1++, p2++;
259238689Sborman     }
259338689Sborman     return(0);
259438689Sborman }
259538689Sborman #endif
2596