xref: /csrg-svn/usr.bin/telnet/commands.c (revision 38920)
133685Sbostic /*
233685Sbostic  * Copyright (c) 1988 Regents of the University of California.
333685Sbostic  * All rights reserved.
433685Sbostic  *
533685Sbostic  * Redistribution and use in source and binary forms are permitted
634898Sbostic  * provided that the above copyright notice and this paragraph are
734898Sbostic  * duplicated in all such forms and that any documentation,
834898Sbostic  * advertising materials, and other materials related to such
934898Sbostic  * distribution and use acknowledge that the software was developed
1034898Sbostic  * by the University of California, Berkeley.  The name of the
1134898Sbostic  * University may not be used to endorse or promote products derived
1234898Sbostic  * from this software without specific prior written permission.
1334898Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434898Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534898Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633685Sbostic  */
1733685Sbostic 
1833685Sbostic #ifndef lint
19*38920Sminshall static char sccsid[] = "@(#)commands.c	1.22 (Berkeley) 09/02/89";
2033685Sbostic #endif /* not lint */
2133685Sbostic 
2232144Sminshall #include <sys/types.h>
2336274Sminshall #if	defined(unix)
2436274Sminshall #include <sys/file.h>
2536274Sminshall #endif	/* defined(unix) */
2632144Sminshall #include <sys/socket.h>
2732144Sminshall #include <netinet/in.h>
2838689Sborman #ifdef	CRAY
2938689Sborman #include <sys/fcntl.h>
3038810Sborman #endif	/* CRAY */
3132144Sminshall 
3232144Sminshall #include <signal.h>
3332144Sminshall #include <netdb.h>
3432144Sminshall #include <ctype.h>
3535298Sminshall #include <varargs.h>
3632144Sminshall 
3732144Sminshall #include <arpa/telnet.h>
3832144Sminshall 
3934305Sminshall #include "general.h"
4034305Sminshall 
4132381Sminshall #include "ring.h"
4232381Sminshall 
4332144Sminshall #include "externs.h"
4432144Sminshall #include "defines.h"
4532144Sminshall #include "types.h"
4632144Sminshall 
4738689Sborman #ifdef	SRCRT
4838810Sborman # ifndef CRAY
4938810Sborman # include <netinet/in_systm.h>
5038810Sborman #  ifndef sun
5138810Sborman #  include <machine/endian.h>
5238810Sborman #  endif /* sun */
5338810Sborman # endif /* CRAY */
5438689Sborman #include <netinet/ip.h>
5538810Sborman #endif /* SRCRT */
5638689Sborman 
5738689Sborman 
5832144Sminshall char	*hostname;
5938689Sborman extern char *getenv();
6032144Sminshall 
6136180Sminshall #define Ambiguous(s)	((char **)s == &ambiguous)
6232144Sminshall static char *ambiguous;		/* special return value for command routines */
6332144Sminshall 
6432144Sminshall typedef struct {
6532144Sminshall 	char	*name;		/* command name */
6638689Sborman 	char	*help;		/* help string (NULL for no help) */
6732144Sminshall 	int	(*handler)();	/* routine which executes command */
6832144Sminshall 	int	needconnect;	/* Do we need to be connected to execute? */
6932144Sminshall } Command;
7032144Sminshall 
7138689Sborman static char line[256];
7238689Sborman static char saveline[256];
7332144Sminshall static int margc;
7432144Sminshall static char *margv[20];
7532144Sminshall 
7632144Sminshall /*
7732144Sminshall  * Various utility routines.
7832144Sminshall  */
7932144Sminshall 
8036180Sminshall #if	!defined(BSD) || (BSD <= 43)
8136180Sminshall 
8236180Sminshall char	*h_errlist[] = {
8336180Sminshall 	"Error 0",
8436180Sminshall 	"Unknown host",				/* 1 HOST_NOT_FOUND */
8536180Sminshall 	"Host name lookup failure",		/* 2 TRY_AGAIN */
8636180Sminshall 	"Unknown server error",			/* 3 NO_RECOVERY */
8736180Sminshall 	"No address associated with name",	/* 4 NO_ADDRESS */
8836180Sminshall };
8936180Sminshall int	h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
9036180Sminshall 
9136274Sminshall int h_errno;		/* In some version of SunOS this is necessary */
9236180Sminshall 
9336180Sminshall /*
9436180Sminshall  * herror --
9536180Sminshall  *	print the error indicated by the h_errno value.
9636180Sminshall  */
9736180Sminshall herror(s)
9836180Sminshall 	char *s;
9936180Sminshall {
10036180Sminshall 	if (s && *s) {
10136180Sminshall 		fprintf(stderr, "%s: ", s);
10236180Sminshall 	}
10336180Sminshall 	if ((h_errno < 0) || (h_errno >= h_nerr)) {
10436180Sminshall 		fprintf(stderr, "Unknown error\n");
10536274Sminshall 	} else if (h_errno == 0) {
10636274Sminshall #if	defined(sun)
10736274Sminshall 		fprintf(stderr, "Host unknown\n");
10836274Sminshall #endif	/* defined(sun) */
10936180Sminshall 	} else {
11036180Sminshall 		fprintf(stderr, "%s\n", h_errlist[h_errno]);
11136180Sminshall 	}
11236180Sminshall }
11336180Sminshall #endif	/* !define(BSD) || (BSD <= 43) */
11436180Sminshall 
11532144Sminshall static void
11632144Sminshall makeargv()
11732144Sminshall {
11832144Sminshall     register char *cp;
11932144Sminshall     register char **argp = margv;
12032144Sminshall 
12132144Sminshall     margc = 0;
12232144Sminshall     cp = line;
12332144Sminshall     if (*cp == '!') {		/* Special case shell escape */
12438689Sborman 	strcpy(saveline, line);	/* save for shell command */
12532144Sminshall 	*argp++ = "!";		/* No room in string to get this */
12632144Sminshall 	margc++;
12732144Sminshall 	cp++;
12832144Sminshall     }
12932144Sminshall     while (*cp) {
13032144Sminshall 	while (isspace(*cp))
13132144Sminshall 	    cp++;
13232144Sminshall 	if (*cp == '\0')
13332144Sminshall 	    break;
13432144Sminshall 	*argp++ = cp;
13532144Sminshall 	margc += 1;
13632144Sminshall 	while (*cp != '\0' && !isspace(*cp))
13732144Sminshall 	    cp++;
13832144Sminshall 	if (*cp == '\0')
13932144Sminshall 	    break;
14032144Sminshall 	*cp++ = '\0';
14132144Sminshall     }
14232144Sminshall     *argp++ = 0;
14332144Sminshall }
14432144Sminshall 
14532144Sminshall 
14632144Sminshall static char **
14732144Sminshall genget(name, table, next)
14832144Sminshall char	*name;		/* name to match */
14932144Sminshall char	**table;		/* name entry in table */
15032144Sminshall char	**(*next)();	/* routine to return next entry in table */
15132144Sminshall {
15232144Sminshall 	register char *p, *q;
15332144Sminshall 	register char **c, **found;
15432144Sminshall 	register int nmatches, longest;
15532144Sminshall 
15632144Sminshall 	if (name == 0) {
15732144Sminshall 	    return 0;
15832144Sminshall 	}
15932144Sminshall 	longest = 0;
16032144Sminshall 	nmatches = 0;
16132144Sminshall 	found = 0;
16232144Sminshall 	for (c = table; (p = *c) != 0; c = (*next)(c)) {
16332144Sminshall 		for (q = name;
16432144Sminshall 		    (*q == *p) || (isupper(*q) && tolower(*q) == *p); p++, q++)
16532144Sminshall 			if (*q == 0)		/* exact match? */
16632144Sminshall 				return (c);
16732144Sminshall 		if (!*q) {			/* the name was a prefix */
16832144Sminshall 			if (q - name > longest) {
16932144Sminshall 				longest = q - name;
17032144Sminshall 				nmatches = 1;
17132144Sminshall 				found = c;
17232144Sminshall 			} else if (q - name == longest)
17332144Sminshall 				nmatches++;
17432144Sminshall 		}
17532144Sminshall 	}
17632144Sminshall 	if (nmatches > 1)
17736180Sminshall 		return &ambiguous;
17832144Sminshall 	return (found);
17932144Sminshall }
18032144Sminshall 
18132144Sminshall /*
18232144Sminshall  * Make a character string into a number.
18332144Sminshall  *
18432144Sminshall  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
18532144Sminshall  */
18632144Sminshall 
18732144Sminshall static
18832144Sminshall special(s)
18932144Sminshall register char *s;
19032144Sminshall {
19132144Sminshall 	register char c;
19232144Sminshall 	char b;
19332144Sminshall 
19432144Sminshall 	switch (*s) {
19532144Sminshall 	case '^':
19632144Sminshall 		b = *++s;
19732144Sminshall 		if (b == '?') {
19832144Sminshall 		    c = b | 0x40;		/* DEL */
19932144Sminshall 		} else {
20032144Sminshall 		    c = b & 0x1f;
20132144Sminshall 		}
20232144Sminshall 		break;
20332144Sminshall 	default:
20432144Sminshall 		c = *s;
20532144Sminshall 		break;
20632144Sminshall 	}
20732144Sminshall 	return c;
20832144Sminshall }
20932144Sminshall 
21032144Sminshall /*
21132144Sminshall  * Construct a control character sequence
21232144Sminshall  * for a special character.
21332144Sminshall  */
21432144Sminshall static char *
21532144Sminshall control(c)
21632144Sminshall 	register int c;
21732144Sminshall {
21832144Sminshall 	static char buf[3];
21932144Sminshall 
22032144Sminshall 	if (c == 0x7f)
22132144Sminshall 		return ("^?");
22232144Sminshall 	if (c == '\377') {
22332144Sminshall 		return "off";
22432144Sminshall 	}
22532144Sminshall 	if (c >= 0x20) {
22632144Sminshall 		buf[0] = c;
22732144Sminshall 		buf[1] = 0;
22832144Sminshall 	} else {
22932144Sminshall 		buf[0] = '^';
23032144Sminshall 		buf[1] = '@'+c;
23132144Sminshall 		buf[2] = 0;
23232144Sminshall 	}
23332144Sminshall 	return (buf);
23432144Sminshall }
23532144Sminshall 
23632144Sminshall 
23732144Sminshall 
23832144Sminshall /*
23932144Sminshall  *	The following are data structures and routines for
24032144Sminshall  *	the "send" command.
24132144Sminshall  *
24232144Sminshall  */
24332144Sminshall 
24432144Sminshall struct sendlist {
24532144Sminshall     char	*name;		/* How user refers to it (case independent) */
24632144Sminshall     char	*help;		/* Help information (0 ==> no help) */
24732144Sminshall #if	defined(NOT43)
24838689Sborman     int		(*handler)();	/* Routine to perform (for special ops) */
24932144Sminshall #else	/* defined(NOT43) */
25038689Sborman     void	(*handler)();	/* Routine to perform (for special ops) */
25132144Sminshall #endif	/* defined(NOT43) */
25238689Sborman     int		what;		/* Character to be sent (<0 ==> special) */
25332144Sminshall };
25432144Sminshall 
25532144Sminshall #define	SENDQUESTION	-1
25632144Sminshall #define	SENDESCAPE	-3
25732144Sminshall 
25832144Sminshall static struct sendlist Sendlist[] = {
25938689Sborman     { "ao",	"Send Telnet Abort output",		0,	AO },
26038689Sborman     { "ayt",	"Send Telnet 'Are You There'",		0,	AYT },
26138689Sborman     { "brk",	"Send Telnet Break",			0,	BREAK },
26238689Sborman     { "ec",	"Send Telnet Erase Character",		0,	EC },
26338689Sborman     { "el",	"Send Telnet Erase Line",		0,	EL },
26438689Sborman     { "escape",	"Send current escape character",	0,	SENDESCAPE },
26538689Sborman     { "ga",	"Send Telnet 'Go Ahead' sequence",	0,	GA },
26638689Sborman     { "ip",	"Send Telnet Interrupt Process",	0,	IP },
26738689Sborman     { "nop",	"Send Telnet 'No operation'",		0,	NOP },
26838689Sborman     { "eor",	"Send Telnet 'End of Record'",		0,	EOR },
26938689Sborman     { "abort",	"Send Telnet 'Abort Process'",		0,	ABORT },
27038689Sborman     { "susp",	"Send Telnet 'Suspend Process'",	0,	SUSP },
27138689Sborman     { "eof",	"Send Telnet End of File Character",	0,	xEOF },
27238689Sborman     { "synch",	"Perform Telnet 'Synch operation'",	dosynch, SYNCH },
27338909Sborman     { "getstatus", "Send request for STATUS",		get_status, 0 },
27438689Sborman     { "?",	"Display send options",			0,	SENDQUESTION },
27532144Sminshall     { 0 }
27632144Sminshall };
27732144Sminshall 
27832144Sminshall static struct sendlist Sendlist2[] = {		/* some synonyms */
27938689Sborman     { "break",		0, 0, BREAK },
28032144Sminshall 
28138689Sborman     { "intp",		0, 0, IP },
28238689Sborman     { "interrupt",	0, 0, IP },
28338689Sborman     { "intr",		0, 0, IP },
28432144Sminshall 
28538689Sborman     { "help",		0, 0, SENDQUESTION },
28632144Sminshall 
28738689Sborman     { 0 }
28832144Sminshall };
28932144Sminshall 
29032144Sminshall static char **
29132144Sminshall getnextsend(name)
29232144Sminshall char *name;
29332144Sminshall {
29432144Sminshall     struct sendlist *c = (struct sendlist *) name;
29532144Sminshall 
29632144Sminshall     return (char **) (c+1);
29732144Sminshall }
29832144Sminshall 
29932144Sminshall static struct sendlist *
30032144Sminshall getsend(name)
30132144Sminshall char *name;
30232144Sminshall {
30332144Sminshall     struct sendlist *sl;
30432144Sminshall 
30532144Sminshall     if ((sl = (struct sendlist *)
30632144Sminshall 			genget(name, (char **) Sendlist, getnextsend)) != 0) {
30732144Sminshall 	return sl;
30832144Sminshall     } else {
30932144Sminshall 	return (struct sendlist *)
31032144Sminshall 				genget(name, (char **) Sendlist2, getnextsend);
31132144Sminshall     }
31232144Sminshall }
31332144Sminshall 
31432144Sminshall static
31532144Sminshall sendcmd(argc, argv)
31632144Sminshall int	argc;
31732144Sminshall char	**argv;
31832144Sminshall {
31932144Sminshall     int what;		/* what we are sending this time */
32032144Sminshall     int count;		/* how many bytes we are going to need to send */
32132144Sminshall     int i;
32232144Sminshall     int question = 0;	/* was at least one argument a question */
32332144Sminshall     struct sendlist *s;	/* pointer to current command */
32432144Sminshall 
32532144Sminshall     if (argc < 2) {
32632144Sminshall 	printf("need at least one argument for 'send' command\n");
32732144Sminshall 	printf("'send ?' for help\n");
32832144Sminshall 	return 0;
32932144Sminshall     }
33032144Sminshall     /*
33132144Sminshall      * First, validate all the send arguments.
33232144Sminshall      * In addition, we see how much space we are going to need, and
33332144Sminshall      * whether or not we will be doing a "SYNCH" operation (which
33432144Sminshall      * flushes the network queue).
33532144Sminshall      */
33632144Sminshall     count = 0;
33732144Sminshall     for (i = 1; i < argc; i++) {
33832144Sminshall 	s = getsend(argv[i]);
33932144Sminshall 	if (s == 0) {
34032144Sminshall 	    printf("Unknown send argument '%s'\n'send ?' for help.\n",
34132144Sminshall 			argv[i]);
34232144Sminshall 	    return 0;
34332144Sminshall 	} else if (Ambiguous(s)) {
34432144Sminshall 	    printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
34532144Sminshall 			argv[i]);
34632144Sminshall 	    return 0;
34732144Sminshall 	}
34832144Sminshall 	switch (s->what) {
34932144Sminshall 	case SENDQUESTION:
35038689Sborman 	    question = 1;
35132144Sminshall 	    break;
35232144Sminshall 	case SENDESCAPE:
35332144Sminshall 	    count += 1;
35432144Sminshall 	    break;
35532144Sminshall 	case SYNCH:
35632144Sminshall 	    count += 2;
35732144Sminshall 	    break;
35832144Sminshall 	default:
35932144Sminshall 	    count += 2;
36032144Sminshall 	    break;
36132144Sminshall 	}
36232144Sminshall     }
36338689Sborman     if (!connected) {
36438689Sborman 	if (count)
36538689Sborman 	    printf("?Need to be connected first.\n");
36638689Sborman 	if (question) {
36738689Sborman 	    for (s = Sendlist; s->name; s++)
36838689Sborman 		if (s->help)
36938689Sborman 		    printf("%-15s %s\n", s->name, s->help);
37038689Sborman 	} else
37138689Sborman 	    printf("'send ?' for help\n");
37238689Sborman 	return !question;
37338689Sborman     }
37432144Sminshall     /* Now, do we have enough room? */
37532144Sminshall     if (NETROOM() < count) {
37632144Sminshall 	printf("There is not enough room in the buffer TO the network\n");
37732144Sminshall 	printf("to process your request.  Nothing will be done.\n");
37832144Sminshall 	printf("('send synch' will throw away most data in the network\n");
37932144Sminshall 	printf("buffer, if this might help.)\n");
38032144Sminshall 	return 0;
38132144Sminshall     }
38232144Sminshall     /* OK, they are all OK, now go through again and actually send */
38332144Sminshall     for (i = 1; i < argc; i++) {
38432144Sminshall 	if ((s = getsend(argv[i])) == 0) {
38532144Sminshall 	    fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
38632144Sminshall 	    quit();
38732144Sminshall 	    /*NOTREACHED*/
38832144Sminshall 	}
38938689Sborman 	if (s->handler) {
39038689Sborman 	    (*s->handler)(s);
39132144Sminshall 	} else {
39232144Sminshall 	    switch (what = s->what) {
39332144Sminshall 	    case SYNCH:
39432144Sminshall 		dosynch();
39532144Sminshall 		break;
39632144Sminshall 	    case SENDQUESTION:
39732144Sminshall 		for (s = Sendlist; s->name; s++) {
39838689Sborman 		    if (s->help)
39938689Sborman 			printf("%-15s %s\n", s->name, s->help);
40032144Sminshall 		}
40132144Sminshall 		question = 1;
40232144Sminshall 		break;
40332144Sminshall 	    case SENDESCAPE:
40432144Sminshall 		NETADD(escape);
40532144Sminshall 		break;
40632144Sminshall 	    default:
40732144Sminshall 		NET2ADD(IAC, what);
40832144Sminshall 		break;
40932144Sminshall 	    }
41032144Sminshall 	}
41132144Sminshall     }
41232144Sminshall     return !question;
41332144Sminshall }
41432144Sminshall 
41532144Sminshall /*
41632144Sminshall  * The following are the routines and data structures referred
41732144Sminshall  * to by the arguments to the "toggle" command.
41832144Sminshall  */
41932144Sminshall 
42032144Sminshall static
42132144Sminshall lclchars()
42232144Sminshall {
42332144Sminshall     donelclchars = 1;
42432144Sminshall     return 1;
42532144Sminshall }
42632144Sminshall 
42732144Sminshall static
42832144Sminshall togdebug()
42932144Sminshall {
43032144Sminshall #ifndef	NOT43
43132144Sminshall     if (net > 0 &&
43232144Sminshall 	(SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
43332144Sminshall 	    perror("setsockopt (SO_DEBUG)");
43432144Sminshall     }
43532144Sminshall #else	/* NOT43 */
43632144Sminshall     if (debug) {
43732144Sminshall 	if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
43832144Sminshall 	    perror("setsockopt (SO_DEBUG)");
43932144Sminshall     } else
44032144Sminshall 	printf("Cannot turn off socket debugging\n");
44132144Sminshall #endif	/* NOT43 */
44232144Sminshall     return 1;
44332144Sminshall }
44432144Sminshall 
44532144Sminshall 
44632144Sminshall static int
44732144Sminshall togcrlf()
44832144Sminshall {
44932144Sminshall     if (crlf) {
45032144Sminshall 	printf("Will send carriage returns as telnet <CR><LF>.\n");
45132144Sminshall     } else {
45232144Sminshall 	printf("Will send carriage returns as telnet <CR><NUL>.\n");
45332144Sminshall     }
45432144Sminshall     return 1;
45532144Sminshall }
45632144Sminshall 
45738909Sborman int binmode;
45832144Sminshall 
45932144Sminshall static int
46038689Sborman togbinary(val)
46138689Sborman int val;
46232144Sminshall {
46332144Sminshall     donebinarytoggle = 1;
46432144Sminshall 
46538909Sborman     if (val >= 0) {
46638909Sborman 	binmode = val;
46738909Sborman     } else {
46838909Sborman 	if (my_want_state_is_will(TELOPT_BINARY) &&
46938909Sborman 				my_want_state_is_do(TELOPT_BINARY)) {
47038909Sborman 	    binmode = 1;
47138909Sborman 	} else if (my_want_state_is_wont(TELOPT_BINARY) &&
47238909Sborman 				my_want_state_is_dont(TELOPT_BINARY)) {
47338909Sborman 	    binmode = 0;
47438909Sborman 	}
47538909Sborman 	val = binmode ? 0 : 1;
47638909Sborman     }
47738909Sborman 
47838909Sborman     if (val == 1) {
47938909Sborman 	if (my_want_state_is_will(TELOPT_BINARY) &&
48038909Sborman 					my_want_state_is_do(TELOPT_BINARY)) {
48138689Sborman 	    printf("Already operating in binary mode with remote host.\n");
48238909Sborman 	} else {
48338909Sborman 	    printf("Negotiating binary mode with remote host.\n");
48438909Sborman 	    tel_enter_binary(3);
48538689Sborman 	}
48638909Sborman     } else {
48738909Sborman 	if (my_want_state_is_wont(TELOPT_BINARY) &&
48838909Sborman 					my_want_state_is_dont(TELOPT_BINARY)) {
48938689Sborman 	    printf("Already in network ascii mode with remote host.\n");
49038909Sborman 	} else {
49138909Sborman 	    printf("Negotiating network ascii mode with remote host.\n");
49238909Sborman 	    tel_leave_binary(3);
49338689Sborman 	}
49432144Sminshall     }
49532144Sminshall     return 1;
49632144Sminshall }
49732144Sminshall 
49838909Sborman static int
49938909Sborman togrbinary(val)
50038909Sborman int val;
50138909Sborman {
50238909Sborman     donebinarytoggle = 1;
50332144Sminshall 
50438909Sborman     if (val == -1)
50538909Sborman 	val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
50632144Sminshall 
50738909Sborman     if (val == 1) {
50838909Sborman 	if (my_want_state_is_do(TELOPT_BINARY)) {
50938909Sborman 	    printf("Already receiving in binary mode.\n");
51038909Sborman 	} else {
51138909Sborman 	    printf("Negotiating binary mode on input.\n");
51238909Sborman 	    tel_enter_binary(1);
51338909Sborman 	}
51438909Sborman     } else {
51538909Sborman 	if (my_want_state_is_dont(TELOPT_BINARY)) {
51638909Sborman 	    printf("Already receiving in network ascii mode.\n");
51738909Sborman 	} else {
51838909Sborman 	    printf("Negotiating network ascii mode on input.\n");
51938909Sborman 	    tel_leave_binary(1);
52038909Sborman 	}
52138909Sborman     }
52238909Sborman     return 1;
52338909Sborman }
52438909Sborman 
52538909Sborman static int
52638909Sborman togxbinary(val)
52738909Sborman int val;
52838909Sborman {
52938909Sborman     donebinarytoggle = 1;
53038909Sborman 
53138909Sborman     if (val == -1)
53238909Sborman 	val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
53338909Sborman 
53438909Sborman     if (val == 1) {
53538909Sborman 	if (my_want_state_is_will(TELOPT_BINARY)) {
53638909Sborman 	    printf("Already transmitting in binary mode.\n");
53738909Sborman 	} else {
53838909Sborman 	    printf("Negotiating binary mode on output.\n");
53938909Sborman 	    tel_enter_binary(2);
54038909Sborman 	}
54138909Sborman     } else {
54238909Sborman 	if (my_want_state_is_wont(TELOPT_BINARY)) {
54338909Sborman 	    printf("Already transmitting in network ascii mode.\n");
54438909Sborman 	} else {
54538909Sborman 	    printf("Negotiating network ascii mode on output.\n");
54638909Sborman 	    tel_leave_binary(2);
54738909Sborman 	}
54838909Sborman     }
54938909Sborman     return 1;
55038909Sborman }
55138909Sborman 
55238909Sborman 
55332144Sminshall extern int togglehelp();
55438689Sborman extern int slc_check();
55532144Sminshall 
55632144Sminshall struct togglelist {
55732144Sminshall     char	*name;		/* name of toggle */
55832144Sminshall     char	*help;		/* help message */
55932144Sminshall     int		(*handler)();	/* routine to do actual setting */
56032144Sminshall     int		*variable;
56132144Sminshall     char	*actionexplanation;
56232144Sminshall };
56332144Sminshall 
56432144Sminshall static struct togglelist Togglelist[] = {
56532144Sminshall     { "autoflush",
56638689Sborman 	"flushing of output when sending interrupt characters",
56732144Sminshall 	    0,
56838689Sborman 		&autoflush,
56938689Sborman 		    "flush output when sending interrupt characters" },
57032144Sminshall     { "autosynch",
57138689Sborman 	"automatic sending of interrupt characters in urgent mode",
57232144Sminshall 	    0,
57338689Sborman 		&autosynch,
57438689Sborman 		    "send interrupt characters in urgent mode" },
57532144Sminshall     { "binary",
57638689Sborman 	"sending and receiving of binary data",
57732144Sminshall 	    togbinary,
57838689Sborman 		0,
57938689Sborman 		    0 },
58038909Sborman     { "inbinary",
58138909Sborman 	"receiving of binary data",
58238909Sborman 	    togrbinary,
58338909Sborman 		0,
58438909Sborman 		    0 },
58538909Sborman     { "outbinary",
58638909Sborman 	"sending of binary data",
58738909Sborman 	    togxbinary,
58838909Sborman 		0,
58938909Sborman 		    0 },
59032144Sminshall     { "crlf",
59138689Sborman 	"sending carriage returns as telnet <CR><LF>",
59232144Sminshall 	    togcrlf,
59338689Sborman 		&crlf,
59438689Sborman 		    0 },
59532144Sminshall     { "crmod",
59638689Sborman 	"mapping of received carriage returns",
59732144Sminshall 	    0,
59838689Sborman 		&crmod,
59938689Sborman 		    "map carriage return on output" },
60032144Sminshall     { "localchars",
60138689Sborman 	"local recognition of certain control characters",
60232144Sminshall 	    lclchars,
60338689Sborman 		&localchars,
60438689Sborman 		    "recognize certain control characters" },
60538689Sborman     { " ", "", 0 },		/* empty line */
60638208Sminshall #if	defined(unix) && defined(TN3270)
607*38920Sminshall     { "apitrace",
608*38920Sminshall 	"(debugging) toggle tracing of API transactions",
609*38920Sminshall 	    0,
610*38920Sminshall 		&apitrace,
611*38920Sminshall 		    "trace API transactions" },
61238208Sminshall     { "cursesdata",
61338208Sminshall 	"(debugging) toggle printing of hexadecimal curses data",
61438208Sminshall 	    0,
61538689Sborman 		&cursesdata,
61638689Sborman 		    "print hexadecimal representation of curses data" },
61738208Sminshall #endif	/* defined(unix) && defined(TN3270) */
61832144Sminshall     { "debug",
61938689Sborman 	"debugging",
62032144Sminshall 	    togdebug,
62138689Sborman 		&debug,
62238689Sborman 		    "turn on socket level debugging" },
62332144Sminshall     { "netdata",
62438689Sborman 	"printing of hexadecimal network data (debugging)",
62532144Sminshall 	    0,
62638689Sborman 		&netdata,
62738689Sborman 		    "print hexadecimal representation of network traffic" },
62838689Sborman     { "prettydump",
62938689Sborman 	"output of \"netdata\" to user readable format (debugging)",
63038689Sborman 	    0,
63138689Sborman 		&prettydump,
63238689Sborman 		    "print user readable output for \"netdata\"" },
63332144Sminshall     { "options",
63438689Sborman 	"viewing of options processing (debugging)",
63532144Sminshall 	    0,
63638689Sborman 		&showoptions,
63738689Sborman 		    "show option processing" },
63838208Sminshall #if	defined(unix)
63938208Sminshall     { "termdata",
64038208Sminshall 	"(debugging) toggle printing of hexadecimal terminal data",
64138208Sminshall 	    0,
64238689Sborman 		&termdata,
64338689Sborman 		    "print hexadecimal representation of terminal traffic" },
64438208Sminshall #endif	/* defined(unix) */
64532144Sminshall     { "?",
64638689Sborman 	0,
64738689Sborman 	    togglehelp },
64832144Sminshall     { "help",
64938689Sborman 	0,
65038689Sborman 	    togglehelp },
65132144Sminshall     { 0 }
65232144Sminshall };
65332144Sminshall 
65432144Sminshall static
65532144Sminshall togglehelp()
65632144Sminshall {
65732144Sminshall     struct togglelist *c;
65832144Sminshall 
65932144Sminshall     for (c = Togglelist; c->name; c++) {
66038689Sborman 	if (c->help) {
66138689Sborman 	    if (*c->help)
66238689Sborman 		printf("%-15s toggle %s\n", c->name, c->help);
66338689Sborman 	    else
66438689Sborman 		printf("\n");
66532144Sminshall 	}
66632144Sminshall     }
66738689Sborman     printf("\n");
66838689Sborman     printf("%-15s %s\n", "?", "display help information");
66932144Sminshall     return 0;
67032144Sminshall }
67132144Sminshall 
67238689Sborman static
67338689Sborman settogglehelp(set)
67438689Sborman int set;
67538689Sborman {
67638689Sborman     struct togglelist *c;
67738689Sborman 
67838689Sborman     for (c = Togglelist; c->name; c++) {
67938689Sborman 	if (c->help) {
68038689Sborman 	    if (*c->help)
68138689Sborman 		printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
68238689Sborman 						c->help);
68338689Sborman 	    else
68438689Sborman 		printf("\n");
68538689Sborman 	}
68638689Sborman     }
68738689Sborman }
68838689Sborman 
68932144Sminshall static char **
69032144Sminshall getnexttoggle(name)
69132144Sminshall char *name;
69232144Sminshall {
69332144Sminshall     struct togglelist *c = (struct togglelist *) name;
69432144Sminshall 
69532144Sminshall     return (char **) (c+1);
69632144Sminshall }
69732144Sminshall 
69832144Sminshall static struct togglelist *
69932144Sminshall gettoggle(name)
70032144Sminshall char *name;
70132144Sminshall {
70232144Sminshall     return (struct togglelist *)
70332144Sminshall 			genget(name, (char **) Togglelist, getnexttoggle);
70432144Sminshall }
70532144Sminshall 
70632144Sminshall static
70732144Sminshall toggle(argc, argv)
70832144Sminshall int	argc;
70932144Sminshall char	*argv[];
71032144Sminshall {
71132144Sminshall     int retval = 1;
71232144Sminshall     char *name;
71332144Sminshall     struct togglelist *c;
71432144Sminshall 
71532144Sminshall     if (argc < 2) {
71632144Sminshall 	fprintf(stderr,
71732144Sminshall 	    "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
71832144Sminshall 	return 0;
71932144Sminshall     }
72032144Sminshall     argc--;
72132144Sminshall     argv++;
72232144Sminshall     while (argc--) {
72332144Sminshall 	name = *argv++;
72432144Sminshall 	c = gettoggle(name);
72532144Sminshall 	if (Ambiguous(c)) {
72632144Sminshall 	    fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
72732144Sminshall 					name);
72832144Sminshall 	    return 0;
72932144Sminshall 	} else if (c == 0) {
73032144Sminshall 	    fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
73132144Sminshall 					name);
73232144Sminshall 	    return 0;
73332144Sminshall 	} else {
73432144Sminshall 	    if (c->variable) {
73532144Sminshall 		*c->variable = !*c->variable;		/* invert it */
73632144Sminshall 		if (c->actionexplanation) {
73732144Sminshall 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
73832144Sminshall 							c->actionexplanation);
73932144Sminshall 		}
74032144Sminshall 	    }
74132144Sminshall 	    if (c->handler) {
74238689Sborman 		retval &= (*c->handler)(-1);
74332144Sminshall 	    }
74432144Sminshall 	}
74532144Sminshall     }
74632144Sminshall     return retval;
74732144Sminshall }
74832144Sminshall 
74932144Sminshall /*
75032144Sminshall  * The following perform the "set" command.
75132144Sminshall  */
75232144Sminshall 
75338689Sborman #ifdef	USE_TERMIO
75438689Sborman struct termio new_tc = { 0 };
75538689Sborman #endif
75638689Sborman 
75732144Sminshall struct setlist {
75832144Sminshall     char *name;				/* name */
75932144Sminshall     char *help;				/* help information */
76038689Sborman     void (*handler)();
76132144Sminshall     char *charp;			/* where it is located at */
76232144Sminshall };
76332144Sminshall 
76432144Sminshall static struct setlist Setlist[] = {
76538689Sborman     { "echo", 	"character to toggle local echoing on/off", 0, &echoc },
76638689Sborman     { "escape",	"character to escape back to telnet command mode", 0, &escape },
76738689Sborman     { "tracefile", "file to write trace intormation to", SetNetTrace, NetTraceFile},
76832144Sminshall     { " ", "" },
76938689Sborman     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
77038689Sborman #ifndef	CRAY
77138689Sborman     { "flushoutput", "character to cause an Abort Oubput", 0, termFlushCharp },
77238689Sborman #endif
77338689Sborman     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
77438689Sborman     { "quit",	"character to cause an Abort process", 0, termQuitCharp },
77538689Sborman     { "eof",	"character to cause an EOF ", 0, termEofCharp },
77638689Sborman     { " ", "" },
77738689Sborman     { " ", "The following are for local editing in linemode", 0, 0 },
77838689Sborman     { "erase",	"character to use to erase a character", 0, termEraseCharp },
77938689Sborman     { "kill",	"character to use to erase a line", 0, termKillCharp },
78038689Sborman #ifndef	CRAY
78138689Sborman     { "lnext",	"character to use for literal next", 0, termLiteralNextCharp },
78238689Sborman     { "susp",	"character to cuase a Suspend Process", 0, termSuspCharp },
78338689Sborman     { "reprint", "character to use for line reprint", 0, termRprntCharp },
78438689Sborman     { "worderase", "character to use to erase a word", 0, termWerasCharp },
78538689Sborman     { "start",	"character to use for XON", 0, termStartCharp },
78638689Sborman     { "stop",	"character to sue for XOFF", 0, termStopCharp },
78738689Sborman #endif
78832144Sminshall     { 0 }
78932144Sminshall };
79032144Sminshall 
79138689Sborman #ifdef	CRAY
79238689Sborman /* Work around compiler bug */
79338689Sborman _setlist_init()
79438689Sborman {
79538689Sborman 	Setlist[6].charp = &termIntChar;
79638689Sborman 	Setlist[7].charp = &termQuitChar;
79738689Sborman 	Setlist[8].charp = &termEofChar;
79838689Sborman 	Setlist[11].charp = &termEraseChar;
79938689Sborman 	Setlist[12].charp = &termKillChar;
80038689Sborman }
80138810Sborman #endif	/* CRAY */
80238689Sborman 
80332144Sminshall static char **
80432144Sminshall getnextset(name)
80532144Sminshall char *name;
80632144Sminshall {
80732144Sminshall     struct setlist *c = (struct setlist *)name;
80832144Sminshall 
80932144Sminshall     return (char **) (c+1);
81032144Sminshall }
81132144Sminshall 
81232144Sminshall static struct setlist *
81332144Sminshall getset(name)
81432144Sminshall char *name;
81532144Sminshall {
81632144Sminshall     return (struct setlist *) genget(name, (char **) Setlist, getnextset);
81732144Sminshall }
81832144Sminshall 
81932144Sminshall static
82032144Sminshall setcmd(argc, argv)
82132144Sminshall int	argc;
82232144Sminshall char	*argv[];
82332144Sminshall {
82432144Sminshall     int value;
82532144Sminshall     struct setlist *ct;
82638689Sborman     struct togglelist *c;
82732144Sminshall 
82838689Sborman     if (argc < 2 || argc > 3) {
82938689Sborman 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
83032144Sminshall 	return 0;
83132144Sminshall     }
83238689Sborman     if ((argc == 2) &&
83338689Sborman 		((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help")))) {
83438689Sborman 	for (ct = Setlist; ct->name; ct++)
83538689Sborman 	    printf("%-15s %s\n", ct->name, ct->help);
83638689Sborman 	printf("\n");
83738689Sborman 	settogglehelp(1);
83838689Sborman 	printf("%-15s %s\n", "?", "display help information");
83938689Sborman 	return 0;
84038689Sborman     }
84132144Sminshall 
84232144Sminshall     ct = getset(argv[1]);
84332144Sminshall     if (ct == 0) {
84438689Sborman 	c = gettoggle(argv[1]);
84538689Sborman 	if (c == 0) {
84638689Sborman 	    fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
84732144Sminshall 			argv[1]);
84838689Sborman 	    return 0;
84938689Sborman 	} else if (Ambiguous(c)) {
85038689Sborman 	    fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
85138689Sborman 			argv[1]);
85238689Sborman 	    return 0;
85338689Sborman 	}
85438689Sborman 	if (c->variable) {
85538689Sborman 	    if ((argc == 2) || (strcmp("on", argv[2]) == 0))
85638689Sborman 		*c->variable = 1;
85738689Sborman 	    else if (strcmp("off", argv[2]) == 0)
85838689Sborman 		*c->variable = 0;
85938689Sborman 	    else {
86038689Sborman 		printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
86138689Sborman 		return 0;
86238689Sborman 	    }
86338689Sborman 	    if (c->actionexplanation) {
86438689Sborman 		printf("%s %s.\n", *c->variable? "Will" : "Won't",
86538689Sborman 							c->actionexplanation);
86638689Sborman 	    }
86738689Sborman 	}
86838689Sborman 	if (c->handler)
86938689Sborman 	    (*c->handler)(1);
87038689Sborman     } else if (argc != 3) {
87138689Sborman 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
87232144Sminshall 	return 0;
87332144Sminshall     } else if (Ambiguous(ct)) {
87432144Sminshall 	fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
87532144Sminshall 			argv[1]);
87632144Sminshall 	return 0;
87738689Sborman     } else if (ct->handler) {
87838689Sborman 	(*ct->handler)(argv[2]);
87938689Sborman 	printf("%s set to \"%s\".\n", ct->name, ct->charp);
88032144Sminshall     } else {
88132144Sminshall 	if (strcmp("off", argv[2])) {
88232144Sminshall 	    value = special(argv[2]);
88332144Sminshall 	} else {
88432144Sminshall 	    value = -1;
88532144Sminshall 	}
88632144Sminshall 	*(ct->charp) = value;
88732144Sminshall 	printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
88832144Sminshall     }
88938689Sborman     slc_check();
89032144Sminshall     return 1;
89132144Sminshall }
89238689Sborman 
89338689Sborman static
89438689Sborman unsetcmd(argc, argv)
89538689Sborman int	argc;
89638689Sborman char	*argv[];
89738689Sborman {
89838689Sborman     int value;
89938689Sborman     struct setlist *ct;
90038689Sborman     struct togglelist *c;
90138689Sborman     register char *name;
90238689Sborman 
90338689Sborman     if (argc < 2) {
90438689Sborman 	fprintf(stderr,
90538689Sborman 	    "Need an argument to 'unset' command.  'unset ?' for help.\n");
90638689Sborman 	return 0;
90738689Sborman     }
90838689Sborman     if ((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help"))) {
90938689Sborman 	for (ct = Setlist; ct->name; ct++)
91038689Sborman 	    printf("%-15s %s\n", ct->name, ct->help);
91138689Sborman 	printf("\n");
91238689Sborman 	settogglehelp(0);
91338689Sborman 	printf("%-15s %s\n", "?", "display help information");
91438689Sborman 	return 0;
91538689Sborman     }
91638689Sborman 
91738689Sborman     argc--;
91838689Sborman     argv++;
91938689Sborman     while (argc--) {
92038689Sborman 	name = *argv++;
92138689Sborman 	ct = getset(name);
92238689Sborman 	if (ct == 0) {
92338689Sborman 	    c = gettoggle(name);
92438689Sborman 	    if (c == 0) {
92538689Sborman 		fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
92638689Sborman 			name);
92738689Sborman 		return 0;
92838689Sborman 	    } else if (Ambiguous(c)) {
92938689Sborman 		fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
93038689Sborman 			name);
93138689Sborman 		return 0;
93238689Sborman 	    }
93338689Sborman 	    if (c->variable) {
93438689Sborman 		*c->variable = 0;
93538689Sborman 		if (c->actionexplanation) {
93638689Sborman 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
93738689Sborman 							c->actionexplanation);
93838689Sborman 		}
93938689Sborman 	    }
94038689Sborman 	    if (c->handler)
94138689Sborman 		(*c->handler)(0);
94238689Sborman 	} else if (Ambiguous(ct)) {
94338689Sborman 	    fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
94438689Sborman 			name);
94538689Sborman 	    return 0;
94638689Sborman 	} else if (ct->handler) {
94738689Sborman 	    (*ct->handler)(0);
94838689Sborman 	    printf("%s reset to \"%s\".\n", ct->name, ct->charp);
94938689Sborman 	} else {
95038689Sborman 	    value = -1;
95138689Sborman 	    *(ct->charp) = -1;
95238689Sborman 	    printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
95338689Sborman 	}
95438689Sborman     }
95538689Sborman     return 1;
95638689Sborman }
95732144Sminshall 
95832144Sminshall /*
95932144Sminshall  * The following are the data structures and routines for the
96032144Sminshall  * 'mode' command.
96132144Sminshall  */
96238689Sborman #ifdef	KLUDGELINEMODE
96338689Sborman extern int kludgelinemode;
96438689Sborman #endif
96532144Sminshall 
96632144Sminshall static
96732144Sminshall dolinemode()
96832144Sminshall {
96938689Sborman #ifdef	KLUDGELINEMODE
97038689Sborman     if (kludgelinemode)
97138689Sborman 	send_dont(TELOPT_SGA, 1);
97238689Sborman #endif
97338689Sborman     send_will(TELOPT_LINEMODE, 1);
97438689Sborman     send_dont(TELOPT_ECHO, 1);
97532144Sminshall     return 1;
97632144Sminshall }
97732144Sminshall 
97832144Sminshall static
97932144Sminshall docharmode()
98032144Sminshall {
98138689Sborman #ifdef	KLUDGELINEMODE
98238689Sborman     if (kludgelinemode)
98338689Sborman 	send_do(TELOPT_SGA, 1);
98438689Sborman     else
98538689Sborman #endif
98638689Sborman     send_wont(TELOPT_LINEMODE, 1);
98738689Sborman     send_do(TELOPT_ECHO, 1);
98838689Sborman     return 1;
98938689Sborman }
99038689Sborman 
99138689Sborman setmode(bit)
99238689Sborman {
99338689Sborman     return dolmmode(bit, 1);
99438689Sborman }
99538689Sborman 
99638689Sborman clearmode(bit)
99738689Sborman {
99838689Sborman     return dolmmode(bit, 0);
99938689Sborman }
100038689Sborman 
100138689Sborman dolmmode(bit, on)
100238689Sborman int bit, on;
100338689Sborman {
100438689Sborman     char c;
100538689Sborman     extern int linemode;
100638689Sborman 
100738689Sborman     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
100838689Sborman 	printf("?Need to have LINEMODE option enabled first.\n");
100938689Sborman 	printf("'mode ?' for help.\n");
101038689Sborman 	return 0;
101132144Sminshall     }
101238689Sborman 
101338689Sborman     if (on)
101438689Sborman 	c = (linemode | bit);
101538689Sborman     else
101638689Sborman 	c = (linemode & ~bit);
101738689Sborman     lm_mode(&c, 1, 1);
101832144Sminshall     return 1;
101932144Sminshall }
102032144Sminshall 
102138689Sborman struct modelist {
102238689Sborman 	char	*name;		/* command name */
102338689Sborman 	char	*help;		/* help string */
102438689Sborman 	int	(*handler)();	/* routine which executes command */
102538689Sborman 	int	needconnect;	/* Do we need to be connected to execute? */
102638689Sborman 	int	arg1;
102738689Sborman };
102838689Sborman 
102938689Sborman extern int modehelp();
103038689Sborman 
103138689Sborman static struct modelist ModeList[] = {
103238689Sborman     { "character", "Disable LINEMODE option",	docharmode, 1 },
103338689Sborman #ifdef	KLUDEGLINEMODE
103438689Sborman     { "",	"(or disable obsolete line-by-line mode)", 0 };
103538689Sborman #endif
103638689Sborman     { "line",	"Enable LINEMODE option",	dolinemode, 1 },
103738689Sborman #ifdef	KLUDEGLINEMODE
103838689Sborman     { "",	"(or enable obsolete line-by-line mode)", 0 };
103938689Sborman #endif
104038689Sborman     { "", "", 0 },
104138689Sborman     { "",	"These require the LINEMODE option to be enabled", 0 },
104238689Sborman     { "isig",	"Enable signal trapping",	setmode, 1, MODE_TRAPSIG },
104338689Sborman     { "+isig",	0,				setmode, 1, MODE_TRAPSIG },
104438689Sborman     { "-isig",	"Disable signal trapping",	clearmode, 1, MODE_TRAPSIG },
104538689Sborman     { "edit",	"Enable character editing",	setmode, 1, MODE_EDIT },
104638689Sborman     { "+edit",	0,				setmode, 1, MODE_EDIT },
104738689Sborman     { "-edit",	"Disable character editing",	clearmode, 1, MODE_EDIT },
104838689Sborman     { "help",	0,				modehelp, 0 },
104938689Sborman     { "?",	"Print help information",	modehelp, 0 },
105032144Sminshall     { 0 },
105132144Sminshall };
105232144Sminshall 
105332144Sminshall static char **
105432144Sminshall getnextmode(name)
105532144Sminshall char *name;
105632144Sminshall {
105738689Sborman     return (char **) (((struct modelist *)name)+1);
105832144Sminshall }
105932144Sminshall 
106038689Sborman static struct modelist *
106132144Sminshall getmodecmd(name)
106232144Sminshall char *name;
106332144Sminshall {
106438689Sborman     return (struct modelist *) genget(name, (char **) ModeList, getnextmode);
106532144Sminshall }
106632144Sminshall 
106738689Sborman modehelp()
106838689Sborman {
106938689Sborman     struct modelist *mt;
107038689Sborman 
107138689Sborman     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
107238689Sborman     for (mt = ModeList; mt->name; mt++) {
107338689Sborman 	if (mt->help) {
107438689Sborman 	    if (*mt->help)
107538689Sborman 		printf("%-15s %s\n", mt->name, mt->help);
107638689Sborman 	    else
107738689Sborman 		printf("\n");
107838689Sborman 	}
107938689Sborman     }
108038689Sborman     return 0;
108138689Sborman }
108238689Sborman 
108332144Sminshall static
108432144Sminshall modecmd(argc, argv)
108532144Sminshall int	argc;
108632144Sminshall char	*argv[];
108732144Sminshall {
108838689Sborman     struct modelist *mt;
108932144Sminshall 
109038689Sborman     if (argc != 2) {
109138689Sborman 	printf("'mode' command requires an argument\n");
109238689Sborman 	printf("'mode ?' for help.\n");
109338689Sborman     } else if ((mt = getmodecmd(argv[1])) == 0) {
109432144Sminshall 	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
109532144Sminshall     } else if (Ambiguous(mt)) {
109632144Sminshall 	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
109738689Sborman     } else if (mt->needconnect && !connected) {
109838689Sborman 	printf("?Need to be connected first.\n");
109938689Sborman 	printf("'mode ?' for help.\n");
110038689Sborman     } else if (mt->handler) {
110138689Sborman 	return (*mt->handler)(mt->arg1);
110232144Sminshall     }
110338689Sborman     return 0;
110432144Sminshall }
110532144Sminshall 
110632144Sminshall /*
110732144Sminshall  * The following data structures and routines implement the
110832144Sminshall  * "display" command.
110932144Sminshall  */
111032144Sminshall 
111132144Sminshall static
111232144Sminshall display(argc, argv)
111332144Sminshall int	argc;
111432144Sminshall char	*argv[];
111532144Sminshall {
111632144Sminshall #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
111732144Sminshall 			    if (*tl->variable) { \
111832144Sminshall 				printf("will"); \
111932144Sminshall 			    } else { \
112032144Sminshall 				printf("won't"); \
112132144Sminshall 			    } \
112232144Sminshall 			    printf(" %s.\n", tl->actionexplanation); \
112332144Sminshall 			}
112432144Sminshall 
112532144Sminshall #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
112638689Sborman 			if (sl->handler == 0) \
112738689Sborman 			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
112838689Sborman 			else \
112938689Sborman 			    printf("%-15s \"%s\"\n", sl->name, sl->charp); \
113032144Sminshall 		    }
113132144Sminshall 
113232144Sminshall     struct togglelist *tl;
113332144Sminshall     struct setlist *sl;
113432144Sminshall 
113532144Sminshall     if (argc == 1) {
113632144Sminshall 	for (tl = Togglelist; tl->name; tl++) {
113732144Sminshall 	    dotog(tl);
113832144Sminshall 	}
113932144Sminshall 	printf("\n");
114032144Sminshall 	for (sl = Setlist; sl->name; sl++) {
114132144Sminshall 	    doset(sl);
114232144Sminshall 	}
114332144Sminshall     } else {
114432144Sminshall 	int i;
114532144Sminshall 
114632144Sminshall 	for (i = 1; i < argc; i++) {
114732144Sminshall 	    sl = getset(argv[i]);
114832144Sminshall 	    tl = gettoggle(argv[i]);
114932144Sminshall 	    if (Ambiguous(sl) || Ambiguous(tl)) {
115032144Sminshall 		printf("?Ambiguous argument '%s'.\n", argv[i]);
115132144Sminshall 		return 0;
115232144Sminshall 	    } else if (!sl && !tl) {
115332144Sminshall 		printf("?Unknown argument '%s'.\n", argv[i]);
115432144Sminshall 		return 0;
115532144Sminshall 	    } else {
115632144Sminshall 		if (tl) {
115732144Sminshall 		    dotog(tl);
115832144Sminshall 		}
115932144Sminshall 		if (sl) {
116032144Sminshall 		    doset(sl);
116132144Sminshall 		}
116232144Sminshall 	    }
116332144Sminshall 	}
116432144Sminshall     }
116538689Sborman /*@*/optionstatus();
116632144Sminshall     return 1;
116732144Sminshall #undef	doset
116832144Sminshall #undef	dotog
116932144Sminshall }
117032144Sminshall 
117132144Sminshall /*
117232144Sminshall  * The following are the data structures, and many of the routines,
117332144Sminshall  * relating to command processing.
117432144Sminshall  */
117532144Sminshall 
117632144Sminshall /*
117732144Sminshall  * Set the escape character.
117832144Sminshall  */
117932144Sminshall static
118032144Sminshall setescape(argc, argv)
118132144Sminshall 	int argc;
118232144Sminshall 	char *argv[];
118332144Sminshall {
118432144Sminshall 	register char *arg;
118532144Sminshall 	char buf[50];
118632144Sminshall 
118732144Sminshall 	printf(
118832144Sminshall 	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",
118932144Sminshall 				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");
119032144Sminshall 	if (argc > 2)
119132144Sminshall 		arg = argv[1];
119232144Sminshall 	else {
119332144Sminshall 		printf("new escape character: ");
119434849Sminshall 		(void) gets(buf);
119532144Sminshall 		arg = buf;
119632144Sminshall 	}
119732144Sminshall 	if (arg[0] != '\0')
119832144Sminshall 		escape = arg[0];
119932144Sminshall 	if (!In3270) {
120032144Sminshall 		printf("Escape character is '%s'.\n", control(escape));
120132144Sminshall 	}
120234849Sminshall 	(void) fflush(stdout);
120332144Sminshall 	return 1;
120432144Sminshall }
120532144Sminshall 
120632144Sminshall /*VARARGS*/
120732144Sminshall static
120832144Sminshall togcrmod()
120932144Sminshall {
121032144Sminshall     crmod = !crmod;
121132144Sminshall     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
121232144Sminshall     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
121334849Sminshall     (void) fflush(stdout);
121432144Sminshall     return 1;
121532144Sminshall }
121632144Sminshall 
121732144Sminshall /*VARARGS*/
121832144Sminshall suspend()
121932144Sminshall {
122038689Sborman #ifdef	SIGTSTP
122137219Sminshall     setcommandmode();
122237219Sminshall     {
122337219Sminshall 	long oldrows, oldcols, newrows, newcols;
122437219Sminshall 
122537219Sminshall 	TerminalWindowSize(&oldrows, &oldcols);
122634849Sminshall 	(void) kill(0, SIGTSTP);
122737219Sminshall 	TerminalWindowSize(&newrows, &newcols);
122837219Sminshall 	if ((oldrows != newrows) || (oldcols != newcols)) {
122937219Sminshall 	    if (connected) {
123037219Sminshall 		sendnaws();
123137219Sminshall 	    }
123237219Sminshall 	}
123337219Sminshall     }
123437219Sminshall     /* reget parameters in case they were changed */
123537219Sminshall     TerminalSaveState();
123638689Sborman     setconnmode(0);
123738689Sborman #else
123838689Sborman     printf("Suspend is not supported.  Try the '!' command instead\n");
123938689Sborman #endif
124037219Sminshall     return 1;
124132144Sminshall }
124232144Sminshall 
124338689Sborman #if	!defined(TN3270)
124438689Sborman #ifdef	CRAY
124538689Sborman #define	vfork	fork
124638689Sborman #endif
124738689Sborman shell(argc, argv)
124838689Sborman int argc;
124938689Sborman char *argv[];
125038689Sborman {
125138689Sborman     extern char *rindex();
125238689Sborman     char cmdbuf[256];
125338689Sborman 
125438689Sborman     setcommandmode();
125538689Sborman     switch(vfork()) {
125638689Sborman     case -1:
125738689Sborman 	perror("Fork failed\n");
125838689Sborman 	break;
125938689Sborman 
126038689Sborman     case 0:
126138689Sborman 	{
126238689Sborman 	    /*
126338689Sborman 	     * Fire up the shell in the child.
126438689Sborman 	     */
126538689Sborman 	    register char *shell, *shellname;
126638689Sborman 
126738689Sborman 	    shell = getenv("SHELL");
126838689Sborman 	    if (shell == NULL)
126938689Sborman 		shell = "/bin/sh";
127038689Sborman 	    if ((shellname = rindex(shell, '/')) == 0)
127138689Sborman 		shellname = shell;
127238689Sborman 	    else
127338689Sborman 		shellname++;
127438689Sborman 	    if (argc > 1)
127538689Sborman 		execl(shell, shellname, "-c", &saveline[1], 0);
127638689Sborman 	    else
127738689Sborman 		execl(shell, shellname, 0);
127838689Sborman 	    perror("Execl");
127938689Sborman 	    _exit(1);
128038689Sborman 	}
128138689Sborman     default:
128238689Sborman 	    wait((int *)0);	/* Wait for the shell to complete */
128338689Sborman     }
128438689Sborman }
128538689Sborman #endif	/* !defined(TN3270) */
128638689Sborman 
128732144Sminshall /*VARARGS*/
128832144Sminshall static
128932144Sminshall bye(argc, argv)
129032144Sminshall int	argc;		/* Number of arguments */
129132144Sminshall char	*argv[];	/* arguments */
129232144Sminshall {
129332144Sminshall     if (connected) {
129434849Sminshall 	(void) shutdown(net, 2);
129532144Sminshall 	printf("Connection closed.\n");
129634849Sminshall 	(void) NetClose(net);
129732144Sminshall 	connected = 0;
129832144Sminshall 	/* reset options */
129932144Sminshall 	tninit();
130032144Sminshall #if	defined(TN3270)
130132144Sminshall 	SetIn3270();		/* Get out of 3270 mode */
130232144Sminshall #endif	/* defined(TN3270) */
130332144Sminshall     }
130432144Sminshall     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
130532144Sminshall 	longjmp(toplevel, 1);
130632144Sminshall 	/* NOTREACHED */
130732144Sminshall     }
130832144Sminshall     return 1;			/* Keep lint, etc., happy */
130932144Sminshall }
131032144Sminshall 
131132144Sminshall /*VARARGS*/
131232144Sminshall quit()
131332144Sminshall {
131432144Sminshall 	(void) call(bye, "bye", "fromquit", 0);
131532144Sminshall 	Exit(0);
131632144Sminshall 	return 1;			/* just to keep lint happy */
131732144Sminshall }
131838689Sborman 
131938689Sborman /*
132038689Sborman  * The SLC command.
132138689Sborman  */
132232144Sminshall 
132338689Sborman struct slclist {
132438689Sborman 	char	*name;
132538689Sborman 	char	*help;
132638689Sborman 	int	(*handler)();
132738689Sborman 	int	arg;
132838689Sborman };
132938689Sborman 
133038689Sborman extern int slc_help();
133138689Sborman extern int slc_mode_export(), slc_mode_import(), slcstate();
133238689Sborman 
133338689Sborman struct slclist SlcList[] = {
133438689Sborman     { "export",	"Use local special character definitions",
133538689Sborman 						slc_mode_export,	0 },
133638689Sborman     { "import",	"Use remote special character definitions",
133738689Sborman 						slc_mode_import,	1 },
133838689Sborman     { "check",	"Verify remote special character definitions",
133938689Sborman 						slc_mode_import,	0 },
134038689Sborman     { "help",	0,				slc_help,		0 },
134138689Sborman     { "?",	"Print help information",	slc_help,		0 },
134238689Sborman     { 0 },
134338689Sborman };
134438689Sborman 
134538689Sborman static
134638689Sborman slc_help()
134738689Sborman {
134838689Sborman     struct slclist *c;
134938689Sborman 
135038689Sborman     for (c = SlcList; c->name; c++) {
135138689Sborman 	if (c->help) {
135238689Sborman 	    if (*c->help)
135338689Sborman 		printf("%-15s %s\n", c->name, c->help);
135438689Sborman 	    else
135538689Sborman 		printf("\n");
135638689Sborman 	}
135738689Sborman     }
135838689Sborman }
135938689Sborman 
136038689Sborman static char **
136138689Sborman getnextslc(name)
136238689Sborman char *name;
136338689Sborman {
136438689Sborman     return (char **)(((struct slclist *)name)+1);
136538689Sborman }
136638689Sborman 
136738689Sborman static struct slclist *
136838689Sborman getslc(name)
136938689Sborman char *name;
137038689Sborman {
137138689Sborman     return (struct slclist *)genget(name, (char **) SlcList, getnextslc);
137238689Sborman }
137338689Sborman 
137438689Sborman static
137538689Sborman slccmd(argc, argv)
137638689Sborman int	argc;
137738689Sborman char	*argv[];
137838689Sborman {
137938689Sborman     struct slclist *c;
138038689Sborman 
138138689Sborman     if (argc != 2) {
138238689Sborman 	fprintf(stderr,
138338689Sborman 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
138438689Sborman 	return 0;
138538689Sborman     }
138638689Sborman     c = getslc(argv[1]);
138738689Sborman     if (c == 0) {
138838689Sborman         fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
138938689Sborman     				argv[1]);
139038689Sborman         return 0;
139138689Sborman     }
139238689Sborman     if (Ambiguous(c)) {
139338689Sborman         fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
139438689Sborman     				argv[1]);
139538689Sborman         return 0;
139638689Sborman     }
139738689Sborman     (*c->handler)(c->arg);
139838689Sborman     slcstate();
139938689Sborman     return 1;
140038689Sborman }
140138689Sborman 
140236274Sminshall #if	defined(unix)
140332144Sminshall /*
140436274Sminshall  * Some information about our file descriptors.
140536274Sminshall  */
140636274Sminshall 
140736274Sminshall char *
140836274Sminshall decodeflags(mask)
140936274Sminshall int mask;
141036274Sminshall {
141136274Sminshall     static char buffer[100];
141236274Sminshall #define do(m,s) \
141336274Sminshall 	if (mask&(m)) { \
141436274Sminshall 	    strcat(buffer, (s)); \
141536274Sminshall 	}
141636274Sminshall 
141736274Sminshall     buffer[0] = 0;			/* Terminate it */
141836274Sminshall 
141936274Sminshall #ifdef FREAD
142036274Sminshall     do(FREAD, " FREAD");
142136274Sminshall #endif
142236274Sminshall #ifdef FWRITE
142336274Sminshall     do(FWRITE, " FWRITE");
142436274Sminshall #endif
142536274Sminshall #ifdef F_DUPFP
142636274Sminshall     do(F_DUPFD, " F_DUPFD");
142736274Sminshall #endif
142836274Sminshall #ifdef FNDELAY
142936274Sminshall     do(FNDELAY, " FNDELAY");
143036274Sminshall #endif
143136274Sminshall #ifdef FAPPEND
143236274Sminshall     do(FAPPEND, " FAPPEND");
143336274Sminshall #endif
143436274Sminshall #ifdef FMARK
143536274Sminshall     do(FMARK, " FMARK");
143636274Sminshall #endif
143736274Sminshall #ifdef FDEFER
143836274Sminshall     do(FDEFER, " FDEFER");
143936274Sminshall #endif
144036274Sminshall #ifdef FASYNC
144136274Sminshall     do(FASYNC, " FASYNC");
144236274Sminshall #endif
144336274Sminshall #ifdef FSHLOCK
144436274Sminshall     do(FSHLOCK, " FSHLOCK");
144536274Sminshall #endif
144636274Sminshall #ifdef FEXLOCK
144736274Sminshall     do(FEXLOCK, " FEXLOCK");
144836274Sminshall #endif
144936274Sminshall #ifdef FCREAT
145036274Sminshall     do(FCREAT, " FCREAT");
145136274Sminshall #endif
145236274Sminshall #ifdef FTRUNC
145336274Sminshall     do(FTRUNC, " FTRUNC");
145436274Sminshall #endif
145536274Sminshall #ifdef FEXCL
145636274Sminshall     do(FEXCL, " FEXCL");
145736274Sminshall #endif
145836274Sminshall 
145936274Sminshall     return buffer;
146036274Sminshall }
146136274Sminshall #undef do
146236274Sminshall 
146336274Sminshall static void
146436274Sminshall filestuff(fd)
146536274Sminshall int fd;
146636274Sminshall {
146736274Sminshall     int res;
146836274Sminshall 
146938689Sborman #ifdef	F_GETOWN
147038689Sborman     setconnmode(0);
147136274Sminshall     res = fcntl(fd, F_GETOWN, 0);
147236274Sminshall     setcommandmode();
147336274Sminshall 
147436274Sminshall     if (res == -1) {
147536274Sminshall 	perror("fcntl");
147636274Sminshall 	return;
147736274Sminshall     }
147836274Sminshall     printf("\tOwner is %d.\n", res);
147938689Sborman #endif
148036274Sminshall 
148138689Sborman     setconnmode(0);
148236274Sminshall     res = fcntl(fd, F_GETFL, 0);
148336274Sminshall     setcommandmode();
148436274Sminshall 
148536274Sminshall     if (res == -1) {
148636274Sminshall 	perror("fcntl");
148736274Sminshall 	return;
148836274Sminshall     }
148936274Sminshall     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
149036274Sminshall }
149136274Sminshall 
149236274Sminshall 
149336274Sminshall #endif	/* defined(unix) */
149436274Sminshall 
149536274Sminshall /*
149632144Sminshall  * Print status about the connection.
149732144Sminshall  */
149834849Sminshall /*ARGSUSED*/
149932144Sminshall static
150032144Sminshall status(argc, argv)
150132144Sminshall int	argc;
150232144Sminshall char	*argv[];
150332144Sminshall {
150432144Sminshall     if (connected) {
150532144Sminshall 	printf("Connected to %s.\n", hostname);
150636242Sminshall 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
150738689Sborman 	    int mode = getconnmode();
150838689Sborman 
150938689Sborman 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
151038689Sborman 		printf("Operating with LINEMODE option\n");
151138689Sborman 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
151238689Sborman 		printf("%s catching of signals\n",
151338689Sborman 					(mode&MODE_TRAPSIG) ? "Local" : "No");
151438689Sborman 		slcstate();
151538689Sborman #ifdef	KLUDGELINEMODE
151638689Sborman 	    } else if (kludgelinemode && my_want_state_is_wont(TELOPT_SGA)) {
151738689Sborman 		printf("Operating in obsolete linemode\n");
151838689Sborman #endif
151938689Sborman 	    } else {
152038689Sborman 		printf("Operating in single character mode\n");
152138689Sborman 		if (localchars)
152238689Sborman 		    printf("Catching signals locally\n");
152332144Sminshall 	    }
152438689Sborman 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
152538689Sborman 	    if (my_want_state_is_will(TELOPT_LFLOW))
152638689Sborman 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
152732144Sminshall 	}
152832144Sminshall     } else {
152932144Sminshall 	printf("No connection.\n");
153032144Sminshall     }
153132144Sminshall #   if !defined(TN3270)
153232144Sminshall     printf("Escape character is '%s'.\n", control(escape));
153334849Sminshall     (void) fflush(stdout);
153432144Sminshall #   else /* !defined(TN3270) */
153532144Sminshall     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
153632144Sminshall 	printf("Escape character is '%s'.\n", control(escape));
153732144Sminshall     }
153832144Sminshall #   if defined(unix)
153936242Sminshall     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
154036242Sminshall 	printf("SIGIO received %d time%s.\n",
154136242Sminshall 				sigiocount, (sigiocount == 1)? "":"s");
154236274Sminshall 	if (In3270) {
154336274Sminshall 	    printf("Process ID %d, process group %d.\n",
154436274Sminshall 					    getpid(), getpgrp(getpid()));
154536274Sminshall 	    printf("Terminal input:\n");
154636274Sminshall 	    filestuff(tin);
154736274Sminshall 	    printf("Terminal output:\n");
154836274Sminshall 	    filestuff(tout);
154936274Sminshall 	    printf("Network socket:\n");
155036274Sminshall 	    filestuff(net);
155136274Sminshall 	}
155236242Sminshall     }
155332144Sminshall     if (In3270 && transcom) {
155432144Sminshall        printf("Transparent mode command is '%s'.\n", transcom);
155532144Sminshall     }
155632144Sminshall #   endif /* defined(unix) */
155734849Sminshall     (void) fflush(stdout);
155832144Sminshall     if (In3270) {
155932144Sminshall 	return 0;
156032144Sminshall     }
156132144Sminshall #   endif /* defined(TN3270) */
156232144Sminshall     return 1;
156332144Sminshall }
156432144Sminshall 
156532144Sminshall 
156632144Sminshall 
156732144Sminshall int
156832144Sminshall tn(argc, argv)
156932144Sminshall 	int argc;
157032144Sminshall 	char *argv[];
157132144Sminshall {
157232144Sminshall     register struct hostent *host = 0;
157332144Sminshall     struct sockaddr_in sin;
157432144Sminshall     struct servent *sp = 0;
157532144Sminshall     static char	hnamebuf[32];
157638689Sborman     unsigned long temp, inet_addr();
157737219Sminshall     extern char *inet_ntoa();
157838689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
157938689Sborman     char *srp = 0, *strrchr();
158038689Sborman     unsigned long sourceroute(), srlen;
158138689Sborman #endif
158232144Sminshall 
158332144Sminshall 
158432144Sminshall #if defined(MSDOS)
158532144Sminshall     char *cp;
158632144Sminshall #endif	/* defined(MSDOS) */
158732144Sminshall 
158832144Sminshall     if (connected) {
158932144Sminshall 	printf("?Already connected to %s\n", hostname);
159032144Sminshall 	return 0;
159132144Sminshall     }
159232144Sminshall     if (argc < 2) {
159332144Sminshall 	(void) strcpy(line, "Connect ");
159432144Sminshall 	printf("(to) ");
159534849Sminshall 	(void) gets(&line[strlen(line)]);
159632144Sminshall 	makeargv();
159732144Sminshall 	argc = margc;
159832144Sminshall 	argv = margv;
159932144Sminshall     }
160032144Sminshall     if ((argc < 2) || (argc > 3)) {
160132144Sminshall 	printf("usage: %s host-name [port]\n", argv[0]);
160232144Sminshall 	return 0;
160332144Sminshall     }
160432144Sminshall #if	defined(MSDOS)
160532144Sminshall     for (cp = argv[1]; *cp; cp++) {
160632144Sminshall 	if (isupper(*cp)) {
160732144Sminshall 	    *cp = tolower(*cp);
160832144Sminshall 	}
160932144Sminshall     }
161032144Sminshall #endif	/* defined(MSDOS) */
161138689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
161238689Sborman     if (argv[1][0] == '@' || argv[1][0] == '!') {
161338689Sborman 	if ((hostname = strrchr(argv[1], ':')) == NULL)
161438689Sborman 	    hostname = strrchr(argv[1], '@');
161538689Sborman 	hostname++;
161638689Sborman 	srp = 0;
161738689Sborman 	temp = sourceroute(argv[1], &srp, &srlen);
161838689Sborman 	if (temp == 0) {
161938689Sborman 	    herror(srp);
162038689Sborman 	    return 0;
162138689Sborman 	} else if (temp == -1) {
162238689Sborman 	    printf("Bad source route option: %s\n", argv[1]);
162338689Sborman 	    return 0;
162438689Sborman 	} else {
162538689Sborman 	    sin.sin_addr.s_addr = temp;
162638689Sborman 	    sin.sin_family = AF_INET;
162738689Sborman 	}
162832144Sminshall     } else {
162938689Sborman #endif
163038689Sborman 	temp = inet_addr(argv[1]);
163138689Sborman 	if (temp != (unsigned long) -1) {
163238689Sborman 	    sin.sin_addr.s_addr = temp;
163338689Sborman 	    sin.sin_family = AF_INET;
163438689Sborman 	    (void) strcpy(hnamebuf, argv[1]);
163538689Sborman 	    hostname = hnamebuf;
163638689Sborman 	} else {
163738689Sborman 	    host = gethostbyname(argv[1]);
163838689Sborman 	    if (host) {
163938689Sborman 		sin.sin_family = host->h_addrtype;
164032144Sminshall #if	defined(h_addr)		/* In 4.3, this is a #define */
164138689Sborman 		memcpy((caddr_t)&sin.sin_addr,
164232144Sminshall 				host->h_addr_list[0], host->h_length);
164332144Sminshall #else	/* defined(h_addr) */
164438689Sborman 		memcpy((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
164532144Sminshall #endif	/* defined(h_addr) */
164638689Sborman 		hostname = host->h_name;
164738689Sborman 	    } else {
164838689Sborman 		herror(argv[1]);
164938689Sborman 		return 0;
165038689Sborman 	    }
165132144Sminshall 	}
165238689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
165332144Sminshall     }
165438689Sborman #endif
165532144Sminshall     if (argc == 3) {
165638689Sborman 	int tmp;
165738689Sborman 
165838689Sborman 	if (*argv[2] == '-') {
165938689Sborman 	    argv[2]++;
166038689Sborman 	    telnetport = 1;
166138689Sborman 	} else
166238689Sborman 	    telnetport = 0;
166332144Sminshall 	sin.sin_port = atoi(argv[2]);
166432144Sminshall 	if (sin.sin_port == 0) {
166532144Sminshall 	    sp = getservbyname(argv[2], "tcp");
166632144Sminshall 	    if (sp)
166732144Sminshall 		sin.sin_port = sp->s_port;
166832144Sminshall 	    else {
166932144Sminshall 		printf("%s: bad port number\n", argv[2]);
167032144Sminshall 		return 0;
167132144Sminshall 	    }
167232144Sminshall 	} else {
167334849Sminshall #if	!defined(htons)
167434849Sminshall 	    u_short htons();
167534849Sminshall #endif	/* !defined(htons) */
167632144Sminshall 	    sin.sin_port = htons(sin.sin_port);
167732144Sminshall 	}
167832144Sminshall     } else {
167932144Sminshall 	if (sp == 0) {
168032144Sminshall 	    sp = getservbyname("telnet", "tcp");
168132144Sminshall 	    if (sp == 0) {
168234849Sminshall 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
168332144Sminshall 		return 0;
168432144Sminshall 	    }
168532144Sminshall 	    sin.sin_port = sp->s_port;
168632144Sminshall 	}
168732144Sminshall 	telnetport = 1;
168832144Sminshall     }
168937219Sminshall     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
169032144Sminshall     do {
169132144Sminshall 	net = socket(AF_INET, SOCK_STREAM, 0);
169232144Sminshall 	if (net < 0) {
169332144Sminshall 	    perror("telnet: socket");
169432144Sminshall 	    return 0;
169532144Sminshall 	}
169638689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
169738689Sborman 	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
169838689Sborman 		perror("setsockopt (IP_OPTIONS)");
169938689Sborman #endif
170032144Sminshall 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
170132144Sminshall 		perror("setsockopt (SO_DEBUG)");
170232144Sminshall 	}
170332144Sminshall 
170432144Sminshall 	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
170532144Sminshall #if	defined(h_addr)		/* In 4.3, this is a #define */
170632144Sminshall 	    if (host && host->h_addr_list[1]) {
170732144Sminshall 		int oerrno = errno;
170832144Sminshall 
170932144Sminshall 		fprintf(stderr, "telnet: connect to address %s: ",
171032144Sminshall 						inet_ntoa(sin.sin_addr));
171132144Sminshall 		errno = oerrno;
171232144Sminshall 		perror((char *)0);
171332144Sminshall 		host->h_addr_list++;
171432144Sminshall 		memcpy((caddr_t)&sin.sin_addr,
171532144Sminshall 			host->h_addr_list[0], host->h_length);
171632144Sminshall 		(void) NetClose(net);
171732144Sminshall 		continue;
171832144Sminshall 	    }
171932144Sminshall #endif	/* defined(h_addr) */
172032144Sminshall 	    perror("telnet: Unable to connect to remote host");
172132144Sminshall 	    return 0;
172237219Sminshall 	}
172332144Sminshall 	connected++;
172432144Sminshall     } while (connected == 0);
172538689Sborman     cmdrc(argv[1], hostname);
172634849Sminshall     (void) call(status, "status", "notmuch", 0);
172732144Sminshall     if (setjmp(peerdied) == 0)
172832144Sminshall 	telnet();
172934849Sminshall     (void) NetClose(net);
173032381Sminshall     ExitString("Connection closed by foreign host.\n",1);
173132144Sminshall     /*NOTREACHED*/
173232144Sminshall }
173332144Sminshall 
173432144Sminshall 
173532144Sminshall #define HELPINDENT (sizeof ("connect"))
173632144Sminshall 
173732144Sminshall static char
173832144Sminshall 	openhelp[] =	"connect to a site",
173932144Sminshall 	closehelp[] =	"close current connection",
174032144Sminshall 	quithelp[] =	"exit telnet",
174132144Sminshall 	statushelp[] =	"print status information",
174232144Sminshall 	helphelp[] =	"print help information",
174332144Sminshall 	sendhelp[] =	"transmit special characters ('send ?' for more)",
174432144Sminshall 	sethelp[] = 	"set operating parameters ('set ?' for more)",
174538689Sborman 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
174632144Sminshall 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
174738689Sborman 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
174832144Sminshall 	displayhelp[] =	"display operating parameters",
174932144Sminshall #if	defined(TN3270) && defined(unix)
175032144Sminshall 	transcomhelp[] = "specify Unix command for transparent mode pipe",
175132144Sminshall #endif	/* defined(TN3270) && defined(unix) */
175232144Sminshall #if	defined(unix)
175332144Sminshall 	zhelp[] =	"suspend telnet",
175432144Sminshall #endif	/* defined(unix */
175532144Sminshall 	shellhelp[] =	"invoke a subshell",
175638689Sborman 	modestring[] = "try to enter line-by-line or character-at-a-time mode";
175732144Sminshall 
175832144Sminshall extern int	help(), shell();
175932144Sminshall 
176032144Sminshall static Command cmdtab[] = {
176138689Sborman 	{ "close",	closehelp,	bye,		1 },
176238689Sborman 	{ "display",	displayhelp,	display,	0 },
176338689Sborman 	{ "mode",	modestring,	modecmd,	0 },
176438689Sborman 	{ "open",	openhelp,	tn,		0 },
176538689Sborman 	{ "quit",	quithelp,	quit,		0 },
176638689Sborman 	{ "send",	sendhelp,	sendcmd,	0 },
176738689Sborman 	{ "set",	sethelp,	setcmd,		0 },
176838689Sborman 	{ "unset",	unsethelp,	unsetcmd,	0 },
176938689Sborman 	{ "status",	statushelp,	status,		0 },
177038689Sborman 	{ "toggle",	togglestring,	toggle,		0 },
177138689Sborman 	{ "slc",	slchelp,	slccmd,		0 },
177232144Sminshall #if	defined(TN3270) && defined(unix)
177338689Sborman 	{ "transcom",	transcomhelp,	settranscom,	0 },
177432144Sminshall #endif	/* defined(TN3270) && defined(unix) */
177532144Sminshall #if	defined(unix)
177638689Sborman 	{ "z",		zhelp,		suspend,	0 },
177732144Sminshall #endif	/* defined(unix) */
177832144Sminshall #if	defined(TN3270)
177938689Sborman 	{ "!",		shellhelp,	shell,		1 },
178038689Sborman #else
178138689Sborman 	{ "!",		shellhelp,	shell,		0 },
178238689Sborman #endif
178338689Sborman 	{ "?",		helphelp,	help,		0 },
178432144Sminshall 	0
178532144Sminshall };
178632144Sminshall 
178732144Sminshall static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
178832144Sminshall static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
178932144Sminshall 
179032144Sminshall static Command cmdtab2[] = {
179138689Sborman 	{ "help",	0,		help,		0 },
179238689Sborman 	{ "escape",	escapehelp,	setescape,	0 },
179338689Sborman 	{ "crmod",	crmodhelp,	togcrmod,	0 },
179432144Sminshall 	0
179532144Sminshall };
179632144Sminshall 
179735298Sminshall 
179832144Sminshall /*
179932144Sminshall  * Call routine with argc, argv set from args (terminated by 0).
180032144Sminshall  */
180135298Sminshall 
180235417Sminshall /*VARARGS1*/
180332144Sminshall static
180435298Sminshall call(va_alist)
180535298Sminshall va_dcl
180632144Sminshall {
180735298Sminshall     va_list ap;
180835298Sminshall     typedef int (*intrtn_t)();
180935298Sminshall     intrtn_t routine;
181035298Sminshall     char *args[100];
181135298Sminshall     int argno = 0;
181235298Sminshall 
181335298Sminshall     va_start(ap);
181435298Sminshall     routine = (va_arg(ap, intrtn_t));
181535495Sminshall     while ((args[argno++] = va_arg(ap, char *)) != 0) {
181635298Sminshall 	;
181735495Sminshall     }
181835298Sminshall     va_end(ap);
181935495Sminshall     return (*routine)(argno-1, args);
182032144Sminshall }
182132144Sminshall 
182235298Sminshall 
182332144Sminshall static char **
182432144Sminshall getnextcmd(name)
182532144Sminshall char *name;
182632144Sminshall {
182732144Sminshall     Command *c = (Command *) name;
182832144Sminshall 
182932144Sminshall     return (char **) (c+1);
183032144Sminshall }
183132144Sminshall 
183232144Sminshall static Command *
183332144Sminshall getcmd(name)
183432144Sminshall char *name;
183532144Sminshall {
183632144Sminshall     Command *cm;
183732144Sminshall 
183832144Sminshall     if ((cm = (Command *) genget(name, (char **) cmdtab, getnextcmd)) != 0) {
183932144Sminshall 	return cm;
184032144Sminshall     } else {
184132144Sminshall 	return (Command *) genget(name, (char **) cmdtab2, getnextcmd);
184232144Sminshall     }
184332144Sminshall }
184432144Sminshall 
184532144Sminshall void
184638689Sborman command(top, tbuf, cnt)
184732144Sminshall 	int top;
184838689Sborman 	char *tbuf;
184938689Sborman 	int cnt;
185032144Sminshall {
185132144Sminshall     register Command *c;
185232144Sminshall 
185332144Sminshall     setcommandmode();
185432144Sminshall     if (!top) {
185532144Sminshall 	putchar('\n');
185637219Sminshall #if	defined(unix)
185732144Sminshall     } else {
185832144Sminshall 	signal(SIGINT, SIG_DFL);
185932144Sminshall 	signal(SIGQUIT, SIG_DFL);
186032144Sminshall #endif	/* defined(unix) */
186132144Sminshall     }
186232144Sminshall     for (;;) {
186332144Sminshall 	printf("%s> ", prompt);
186438689Sborman 	if (tbuf) {
186538689Sborman 	    register char *cp;
186638689Sborman 	    cp = line;
186738689Sborman 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
186838689Sborman 		cnt--;
186938689Sborman 	    tbuf = 0;
187038689Sborman 	    if (cp == line || *--cp != '\n' || cp == line)
187138689Sborman 		goto getline;
187238689Sborman 	    *cp = '\0';
187338689Sborman 	    printf("%s\n", line);
187438689Sborman 	} else {
187538689Sborman 	getline:
187638689Sborman 	    if (gets(line) == NULL) {
187738689Sborman 		if (feof(stdin) || ferror(stdin))
187838689Sborman 		    quit();
187938689Sborman 		break;
188038689Sborman 	    }
188132144Sminshall 	}
188232144Sminshall 	if (line[0] == 0)
188332144Sminshall 	    break;
188432144Sminshall 	makeargv();
188537219Sminshall 	if (margv[0] == 0) {
188637219Sminshall 	    break;
188737219Sminshall 	}
188832144Sminshall 	c = getcmd(margv[0]);
188932144Sminshall 	if (Ambiguous(c)) {
189032144Sminshall 	    printf("?Ambiguous command\n");
189132144Sminshall 	    continue;
189232144Sminshall 	}
189332144Sminshall 	if (c == 0) {
189432144Sminshall 	    printf("?Invalid command\n");
189532144Sminshall 	    continue;
189632144Sminshall 	}
189732144Sminshall 	if (c->needconnect && !connected) {
189832144Sminshall 	    printf("?Need to be connected first.\n");
189932144Sminshall 	    continue;
190032144Sminshall 	}
190132144Sminshall 	if ((*c->handler)(margc, margv)) {
190232144Sminshall 	    break;
190332144Sminshall 	}
190432144Sminshall     }
190532144Sminshall     if (!top) {
190632144Sminshall 	if (!connected) {
190732144Sminshall 	    longjmp(toplevel, 1);
190832144Sminshall 	    /*NOTREACHED*/
190932144Sminshall 	}
191032144Sminshall #if	defined(TN3270)
191132144Sminshall 	if (shell_active == 0) {
191238689Sborman 	    setconnmode(0);
191332144Sminshall 	}
191432144Sminshall #else	/* defined(TN3270) */
191538689Sborman 	setconnmode(0);
191632144Sminshall #endif	/* defined(TN3270) */
191732144Sminshall     }
191832144Sminshall }
191932144Sminshall 
192032144Sminshall /*
192132144Sminshall  * Help command.
192232144Sminshall  */
192332144Sminshall static
192432144Sminshall help(argc, argv)
192532144Sminshall 	int argc;
192632144Sminshall 	char *argv[];
192732144Sminshall {
192832144Sminshall 	register Command *c;
192932144Sminshall 
193032144Sminshall 	if (argc == 1) {
193132144Sminshall 		printf("Commands may be abbreviated.  Commands are:\n\n");
193232144Sminshall 		for (c = cmdtab; c->name; c++)
193338689Sborman 			if (c->help) {
193432144Sminshall 				printf("%-*s\t%s\n", HELPINDENT, c->name,
193532144Sminshall 								    c->help);
193632144Sminshall 			}
193732144Sminshall 		return 0;
193832144Sminshall 	}
193932144Sminshall 	while (--argc > 0) {
194032144Sminshall 		register char *arg;
194132144Sminshall 		arg = *++argv;
194232144Sminshall 		c = getcmd(arg);
194332144Sminshall 		if (Ambiguous(c))
194432144Sminshall 			printf("?Ambiguous help command %s\n", arg);
194532144Sminshall 		else if (c == (Command *)0)
194632144Sminshall 			printf("?Invalid help command %s\n", arg);
194732144Sminshall 		else
194832144Sminshall 			printf("%s\n", c->help);
194932144Sminshall 	}
195032144Sminshall 	return 0;
195132144Sminshall }
195238689Sborman 
195338689Sborman static char *rcname = 0;
195438689Sborman static char rcbuf[128];
195538689Sborman 
195638689Sborman cmdrc(m1, m2)
195738689Sborman 	char *m1, *m2;
195838689Sborman {
195938689Sborman     register Command *c;
196038689Sborman     FILE *rcfile;
196138689Sborman     int gotmachine = 0;
196238689Sborman     int l1 = strlen(m1);
196338689Sborman     int l2 = strlen(m2);
196438689Sborman     char m1save[64];
196538689Sborman 
196638689Sborman     strcpy(m1save, m1);
196738689Sborman     m1 = m1save;
196838689Sborman 
196938689Sborman     if (rcname == 0) {
197038689Sborman 	rcname = getenv("HOME");
197138689Sborman 	if (rcname)
197238689Sborman 	    strcpy(rcbuf, rcname);
197338689Sborman 	else
197438689Sborman 	    rcbuf[0] = '\0';
197538689Sborman 	strcat(rcbuf, "/.telnetrc");
197638689Sborman 	rcname = rcbuf;
197738689Sborman     }
197838689Sborman 
197938689Sborman     if ((rcfile = fopen(rcname, "r")) == 0) {
198038689Sborman 	return;
198138689Sborman     }
198238689Sborman 
198338689Sborman     for (;;) {
198438689Sborman 	if (fgets(line, sizeof(line), rcfile) == NULL)
198538689Sborman 	    break;
198638689Sborman 	if (line[0] == 0)
198738689Sborman 	    break;
198838689Sborman 	if (line[0] == '#')
198938689Sborman 	    continue;
199038689Sborman 	if (gotmachine == 0) {
199138689Sborman 	    if (isspace(line[0]))
199238689Sborman 		continue;
199338689Sborman 	    if (strncasecmp(line, m1, l1) == 0)
199438689Sborman 		strncpy(line, &line[l1], sizeof(line) - l1);
199538689Sborman 	    else if (strncasecmp(line, m2, l2) == 0)
199638689Sborman 		strncpy(line, &line[l2], sizeof(line) - l2);
199738689Sborman 	    else
199838689Sborman 		continue;
199938689Sborman 	    gotmachine = 1;
200038689Sborman 	} else {
200138689Sborman 	    if (!isspace(line[0])) {
200238689Sborman 		gotmachine = 0;
200338689Sborman 		continue;
200438689Sborman 	    }
200538689Sborman 	}
200638689Sborman 	makeargv();
200738689Sborman 	if (margv[0] == 0)
200838689Sborman 	    continue;
200938689Sborman 	c = getcmd(margv[0]);
201038689Sborman 	if (Ambiguous(c)) {
201138689Sborman 	    printf("?Ambiguous command: %s\n", margv[0]);
201238689Sborman 	    continue;
201338689Sborman 	}
201438689Sborman 	if (c == 0) {
201538689Sborman 	    printf("?Invalid command: %s\n", margv[0]);
201638689Sborman 	    continue;
201738689Sborman 	}
201838689Sborman 	/*
201938689Sborman 	 * This should never happen...
202038689Sborman 	 */
202138689Sborman 	if (c->needconnect && !connected) {
202238689Sborman 	    printf("?Need to be connected first for %s.\n", margv[0]);
202338689Sborman 	    continue;
202438689Sborman 	}
202538689Sborman 	(*c->handler)(margc, margv);
202638689Sborman     }
202738689Sborman     fclose(rcfile);
202838689Sborman }
202938689Sborman 
203038689Sborman #if	defined(SRCRT) && defined(IPPROTO_IP)
203138689Sborman 
203238689Sborman /*
203338689Sborman  * Source route is handed in as
203438689Sborman  *	[!]@hop1@hop2...[@|:]dst
203538689Sborman  * If the leading ! is present, it is a
203638689Sborman  * strict source route, otherwise it is
203738689Sborman  * assmed to be a loose source route.
203838689Sborman  *
203938689Sborman  * We fill in the source route option as
204038689Sborman  *	hop1,hop2,hop3...dest
204138689Sborman  * and return a pointer to hop1, which will
204238689Sborman  * be the address to connect() to.
204338689Sborman  *
204438689Sborman  * Arguments:
204538689Sborman  *	arg:	pointer to route list to decipher
204638689Sborman  *
204738689Sborman  *	cpp: 	If *cpp is not equal to NULL, this is a
204838689Sborman  *		pointer to a pointer to a character array
204938689Sborman  *		that should be filled in with the option.
205038689Sborman  *
205138689Sborman  *	lenp:	pointer to an integer that contains the
205238689Sborman  *		length of *cpp if *cpp != NULL.
205338689Sborman  *
205438689Sborman  * Return values:
205538689Sborman  *
205638689Sborman  *	Returns the address of the host to connect to.  If the
205738689Sborman  *	return value is -1, there was a syntax error in the
205838689Sborman  *	option, either unknown characters, or too many hosts.
205938689Sborman  *	If the return value is 0, one of the hostnames in the
206038689Sborman  *	path is unknown, and *cpp is set to point to the bad
206138689Sborman  *	hostname.
206238689Sborman  *
206338689Sborman  *	*cpp:	If *cpp was equal to NULL, it will be filled
206438689Sborman  *		in with a pointer to our static area that has
206538689Sborman  *		the option filled in.  This will be 32bit aligned.
206638689Sborman  *
206738689Sborman  *	*lenp:	This will be filled in with how long the option
206838689Sborman  *		pointed to by *cpp is.
206938689Sborman  *
207038689Sborman  */
207138689Sborman unsigned long
207238689Sborman sourceroute(arg, cpp, lenp)
207338689Sborman char	*arg;
207438689Sborman char	**cpp;
207538689Sborman int	*lenp;
207638689Sborman {
207738689Sborman 	static char lsr[44];
207838689Sborman 	char *cp, *cp2, *lsrp, *lsrep, *index();
207938689Sborman 	register int tmp;
208038689Sborman 	struct in_addr sin_addr;
208138689Sborman 	register struct hostent *host = 0;
208238689Sborman 	register char c;
208338689Sborman 
208438689Sborman 	/*
208538689Sborman 	 * Verify the arguments, and make sure we have
208638689Sborman 	 * at least 7 bytes for the option.
208738689Sborman 	 */
208838689Sborman 	if (cpp == NULL || lenp == NULL)
208938689Sborman 		return((unsigned long)-1);
209038689Sborman 	if (*cpp != NULL && *lenp < 7)
209138689Sborman 		return((unsigned long)-1);
209238689Sborman 	/*
209338689Sborman 	 * Decide whether we have a buffer passed to us,
209438689Sborman 	 * or if we need to use our own static buffer.
209538689Sborman 	 */
209638689Sborman 	if (*cpp) {
209738689Sborman 		lsrp = *cpp;
209838689Sborman 		lsrep = lsrp + *lenp;
209938689Sborman 	} else {
210038689Sborman 		*cpp = lsrp = lsr;
210138689Sborman 		lsrep = lsrp + 44;
210238689Sborman 	}
210338689Sborman 
210438689Sborman 	cp = arg;
210538689Sborman 
210638689Sborman 	/*
210738689Sborman 	 * Next, decide whether we have a loose source
210838689Sborman 	 * route or a strict source route, and fill in
210938689Sborman 	 * the begining of the option.
211038689Sborman 	 */
211138689Sborman 	if (*cp == '!') {
211238689Sborman 		cp++;
211338689Sborman 		*lsrp++ = IPOPT_SSRR;
211438689Sborman 	} else
211538689Sborman 		*lsrp++ = IPOPT_LSRR;
211638689Sborman 
211738689Sborman 	if (*cp != '@')
211838689Sborman 		return((unsigned long)-1);
211938689Sborman 
212038689Sborman 	lsrp++;		/* skip over length, we'll fill it in later */
212138689Sborman 	*lsrp++ = 4;
212238689Sborman 
212338689Sborman 	cp++;
212438689Sborman 
212538689Sborman 	sin_addr.s_addr = 0;
212638689Sborman 
212738689Sborman 	for (c = 0;;) {
212838689Sborman 		if (c == ':')
212938689Sborman 			cp2 = 0;
213038689Sborman 		else for (cp2 = cp; c = *cp2; cp2++) {
213138689Sborman 			if (c == ',') {
213238689Sborman 				*cp2++ = '\0';
213338689Sborman 				if (*cp2 == '@')
213438689Sborman 					cp2++;
213538689Sborman 			} else if (c == '@') {
213638689Sborman 				*cp2++ = '\0';
213738689Sborman 			} else if (c == ':') {
213838689Sborman 				*cp2++ = '\0';
213938689Sborman 			} else
214038689Sborman 				continue;
214138689Sborman 			break;
214238689Sborman 		}
214338689Sborman 		if (!c)
214438689Sborman 			cp2 = 0;
214538689Sborman 
214638689Sborman 		if ((tmp = inet_addr(cp)) != -1) {
214738689Sborman 			sin_addr.s_addr = tmp;
214838689Sborman 		} else if (host = gethostbyname(cp)) {
214938689Sborman #if	defined(h_addr)
215038689Sborman 			memcpy((caddr_t)&sin_addr,
215138689Sborman 				host->h_addr_list[0], host->h_length);
215238689Sborman #else
215338689Sborman 			memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length);
215438689Sborman #endif
215538689Sborman 		} else {
215638689Sborman 			*cpp = cp;
215738689Sborman 			return(0);
215838689Sborman 		}
215938689Sborman 		memcpy(lsrp, (char *)&sin_addr, 4);
216038689Sborman 		lsrp += 4;
216138689Sborman 		if (cp2)
216238689Sborman 			cp = cp2;
216338689Sborman 		else
216438689Sborman 			break;
216538689Sborman 		/*
216638689Sborman 		 * Check to make sure there is space for next address
216738689Sborman 		 */
216838689Sborman 		if (lsrp + 4 > lsrep)
216938689Sborman 			return((unsigned long)-1);
217038689Sborman 	}
217138689Sborman 	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
217238689Sborman 		*cpp = 0;
217338689Sborman 		*lenp = 0;
217438689Sborman 		return((unsigned long)-1);
217538689Sborman 	}
217638689Sborman 	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */
217738689Sborman 	*lenp = lsrp - *cpp;
217838689Sborman 	return(sin_addr.s_addr);
217938689Sborman }
218038689Sborman #endif
218138689Sborman 
218238909Sborman #if	defined(NOSTRNCASECMP)
218338689Sborman strncasecmp(p1, p2, len)
218438689Sborman register char *p1, *p2;
218538689Sborman int len;
218638689Sborman {
218738689Sborman     while (len--) {
218838689Sborman 	if (tolower(*p1) != tolower(*p2))
218938689Sborman 	   return(tolower(*p1) - tolower(*p2));
219038689Sborman 	if (*p1 == '\0')
219138689Sborman 	    return(0);
219238689Sborman 	p1++, p2++;
219338689Sborman     }
219438689Sborman     return(0);
219538689Sborman }
219638689Sborman #endif
2197