xref: /original-bsd/usr.bin/telnet/commands.c (revision 49a3a6ff)
1 /*
2  * Copyright (c) 1988, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)commands.c	5.10 (Berkeley) 04/05/93";
10 #endif /* not lint */
11 
12 #if	defined(unix)
13 #include <sys/param.h>
14 #if	defined(CRAY) || defined(sysV88)
15 #include <sys/types.h>
16 #endif
17 #include <sys/file.h>
18 #else
19 #include <sys/types.h>
20 #endif	/* defined(unix) */
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #ifdef	CRAY
24 #include <fcntl.h>
25 #endif	/* CRAY */
26 
27 #include <signal.h>
28 #include <netdb.h>
29 #include <ctype.h>
30 #include <pwd.h>
31 #include <varargs.h>
32 #include <errno.h>
33 
34 #include <arpa/telnet.h>
35 
36 #include "general.h"
37 
38 #include "ring.h"
39 
40 #include "externs.h"
41 #include "defines.h"
42 #include "types.h"
43 
44 #if !defined(CRAY) && !defined(sysV88)
45 #include <netinet/in_systm.h>
46 # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
47 # include <machine/endian.h>
48 # endif /* vax */
49 #endif /* !defined(CRAY) && !defined(sysV88) */
50 #include <netinet/ip.h>
51 
52 
53 #ifndef       MAXHOSTNAMELEN
54 #define       MAXHOSTNAMELEN 64
55 #endif        MAXHOSTNAMELEN
56 
57 #if	defined(IPPROTO_IP) && defined(IP_TOS)
58 int tos = -1;
59 #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
60 
61 char	*hostname;
62 static char _hostname[MAXHOSTNAMELEN];
63 
64 extern char *getenv();
65 
66 extern int isprefix();
67 extern char **genget();
68 extern int Ambiguous();
69 
70 static call();
71 
72 typedef struct {
73 	char	*name;		/* command name */
74 	char	*help;		/* help string (NULL for no help) */
75 	int	(*handler)();	/* routine which executes command */
76 	int	needconnect;	/* Do we need to be connected to execute? */
77 } Command;
78 
79 static char line[256];
80 static char saveline[256];
81 static int margc;
82 static char *margv[20];
83 
84     static void
85 makeargv()
86 {
87     register char *cp, *cp2, c;
88     register char **argp = margv;
89 
90     margc = 0;
91     cp = line;
92     if (*cp == '!') {		/* Special case shell escape */
93 	strcpy(saveline, line);	/* save for shell command */
94 	*argp++ = "!";		/* No room in string to get this */
95 	margc++;
96 	cp++;
97     }
98     while (c = *cp) {
99 	register int inquote = 0;
100 	while (isspace(c))
101 	    c = *++cp;
102 	if (c == '\0')
103 	    break;
104 	*argp++ = cp;
105 	margc += 1;
106 	for (cp2 = cp; c != '\0'; c = *++cp) {
107 	    if (inquote) {
108 		if (c == inquote) {
109 		    inquote = 0;
110 		    continue;
111 		}
112 	    } else {
113 		if (c == '\\') {
114 		    if ((c = *++cp) == '\0')
115 			break;
116 		} else if (c == '"') {
117 		    inquote = '"';
118 		    continue;
119 		} else if (c == '\'') {
120 		    inquote = '\'';
121 		    continue;
122 		} else if (isspace(c))
123 		    break;
124 	    }
125 	    *cp2++ = c;
126 	}
127 	*cp2 = '\0';
128 	if (c == '\0')
129 	    break;
130 	cp++;
131     }
132     *argp++ = 0;
133 }
134 
135 /*
136  * Make a character string into a number.
137  *
138  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
139  */
140 
141 	static
142 special(s)
143 	register char *s;
144 {
145 	register char c;
146 	char b;
147 
148 	switch (*s) {
149 	case '^':
150 		b = *++s;
151 		if (b == '?') {
152 		    c = b | 0x40;		/* DEL */
153 		} else {
154 		    c = b & 0x1f;
155 		}
156 		break;
157 	default:
158 		c = *s;
159 		break;
160 	}
161 	return c;
162 }
163 
164 /*
165  * Construct a control character sequence
166  * for a special character.
167  */
168 	static char *
169 control(c)
170 	register cc_t c;
171 {
172 	static char buf[5];
173 	/*
174 	 * The only way I could get the Sun 3.5 compiler
175 	 * to shut up about
176 	 *	if ((unsigned int)c >= 0x80)
177 	 * was to assign "c" to an unsigned int variable...
178 	 * Arggg....
179 	 */
180 	register unsigned int uic = (unsigned int)c;
181 
182 	if (uic == 0x7f)
183 		return ("^?");
184 	if (c == (cc_t)_POSIX_VDISABLE) {
185 		return "off";
186 	}
187 	if (uic >= 0x80) {
188 		buf[0] = '\\';
189 		buf[1] = ((c>>6)&07) + '0';
190 		buf[2] = ((c>>3)&07) + '0';
191 		buf[3] = (c&07) + '0';
192 		buf[4] = 0;
193 	} else if (uic >= 0x20) {
194 		buf[0] = c;
195 		buf[1] = 0;
196 	} else {
197 		buf[0] = '^';
198 		buf[1] = '@'+c;
199 		buf[2] = 0;
200 	}
201 	return (buf);
202 }
203 
204 
205 
206 /*
207  *	The following are data structures and routines for
208  *	the "send" command.
209  *
210  */
211 
212 struct sendlist {
213     char	*name;		/* How user refers to it (case independent) */
214     char	*help;		/* Help information (0 ==> no help) */
215     int		needconnect;	/* Need to be connected */
216     int		narg;		/* Number of arguments */
217     int		(*handler)();	/* Routine to perform (for special ops) */
218     int		nbyte;		/* Number of bytes to send this command */
219     int		what;		/* Character to be sent (<0 ==> special) */
220 };
221 
222 
223 static int
224 	send_esc P((void)),
225 	send_help P((void)),
226 	send_docmd P((char *)),
227 	send_dontcmd P((char *)),
228 	send_willcmd P((char *)),
229 	send_wontcmd P((char *));
230 
231 static struct sendlist Sendlist[] = {
232     { "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },
233     { "ayt",	"Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },
234     { "brk",	"Send Telnet Break",			1, 0, 0, 2, BREAK },
235     { "break",	0,					1, 0, 0, 2, BREAK },
236     { "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },
237     { "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },
238     { "escape",	"Send current escape character",	1, 0, send_esc, 1, 0 },
239     { "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },
240     { "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },
241     { "intp",	0,					1, 0, 0, 2, IP },
242     { "interrupt", 0,					1, 0, 0, 2, IP },
243     { "intr",	0,					1, 0, 0, 2, IP },
244     { "nop",	"Send Telnet 'No operation'",		1, 0, 0, 2, NOP },
245     { "eor",	"Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },
246     { "abort",	"Send Telnet 'Abort Process'",		1, 0, 0, 2, ABORT },
247     { "susp",	"Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },
248     { "eof",	"Send Telnet End of File Character",	1, 0, 0, 2, xEOF },
249     { "synch",	"Perform Telnet 'Synch operation'",	1, 0, dosynch, 2, 0 },
250     { "getstatus", "Send request for STATUS",		1, 0, get_status, 6, 0 },
251     { "?",	"Display send options",			0, 0, send_help, 0, 0 },
252     { "help",	0,					0, 0, send_help, 0, 0 },
253     { "do",	0,					0, 1, send_docmd, 3, 0 },
254     { "dont",	0,					0, 1, send_dontcmd, 3, 0 },
255     { "will",	0,					0, 1, send_willcmd, 3, 0 },
256     { "wont",	0,					0, 1, send_wontcmd, 3, 0 },
257     { 0 }
258 };
259 
260 #define	GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
261 				sizeof(struct sendlist)))
262 
263     static int
264 sendcmd(argc, argv)
265     int  argc;
266     char **argv;
267 {
268     int count;		/* how many bytes we are going to need to send */
269     int i;
270     int question = 0;	/* was at least one argument a question */
271     struct sendlist *s;	/* pointer to current command */
272     int success = 0;
273     int needconnect = 0;
274 
275     if (argc < 2) {
276 	printf("need at least one argument for 'send' command\n");
277 	printf("'send ?' for help\n");
278 	return 0;
279     }
280     /*
281      * First, validate all the send arguments.
282      * In addition, we see how much space we are going to need, and
283      * whether or not we will be doing a "SYNCH" operation (which
284      * flushes the network queue).
285      */
286     count = 0;
287     for (i = 1; i < argc; i++) {
288 	s = GETSEND(argv[i]);
289 	if (s == 0) {
290 	    printf("Unknown send argument '%s'\n'send ?' for help.\n",
291 			argv[i]);
292 	    return 0;
293 	} else if (Ambiguous(s)) {
294 	    printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
295 			argv[i]);
296 	    return 0;
297 	}
298 	if (i + s->narg >= argc) {
299 	    fprintf(stderr,
300 	    "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\n",
301 		s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
302 	    return 0;
303 	}
304 	count += s->nbyte;
305 	if (s->handler == send_help) {
306 	    send_help();
307 	    return 0;
308 	}
309 
310 	i += s->narg;
311 	needconnect += s->needconnect;
312     }
313     if (!connected && needconnect) {
314 	printf("?Need to be connected first.\n");
315 	printf("'send ?' for help\n");
316 	return 0;
317     }
318     /* Now, do we have enough room? */
319     if (NETROOM() < count) {
320 	printf("There is not enough room in the buffer TO the network\n");
321 	printf("to process your request.  Nothing will be done.\n");
322 	printf("('send synch' will throw away most data in the network\n");
323 	printf("buffer, if this might help.)\n");
324 	return 0;
325     }
326     /* OK, they are all OK, now go through again and actually send */
327     count = 0;
328     for (i = 1; i < argc; i++) {
329 	if ((s = GETSEND(argv[i])) == 0) {
330 	    fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
331 	    (void) quit();
332 	    /*NOTREACHED*/
333 	}
334 	if (s->handler) {
335 	    count++;
336 	    success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
337 				  (s->narg > 1) ? argv[i+2] : 0);
338 	    i += s->narg;
339 	} else {
340 	    NET2ADD(IAC, s->what);
341 	    printoption("SENT", IAC, s->what);
342 	}
343     }
344     return (count == success);
345 }
346 
347     static int
348 send_esc()
349 {
350     NETADD(escape);
351     return 1;
352 }
353 
354     static int
355 send_docmd(name)
356     char *name;
357 {
358     return(send_tncmd(send_do, "do", name));
359 }
360 
361     static int
362 send_dontcmd(name)
363     char *name;
364 {
365     return(send_tncmd(send_dont, "dont", name));
366 }
367     static int
368 send_willcmd(name)
369     char *name;
370 {
371     return(send_tncmd(send_will, "will", name));
372 }
373     static int
374 send_wontcmd(name)
375     char *name;
376 {
377     return(send_tncmd(send_wont, "wont", name));
378 }
379 
380     int
381 send_tncmd(func, cmd, name)
382     void	(*func)();
383     char	*cmd, *name;
384 {
385     char **cpp;
386     extern char *telopts[];
387     register int val = 0;
388 
389     if (isprefix(name, "help") || isprefix(name, "?")) {
390 	register int col, len;
391 
392 	printf("Usage: send %s <value|option>\n", cmd);
393 	printf("\"value\" must be from 0 to 255\n");
394 	printf("Valid options are:\n\t");
395 
396 	col = 8;
397 	for (cpp = telopts; *cpp; cpp++) {
398 	    len = strlen(*cpp) + 3;
399 	    if (col + len > 65) {
400 		printf("\n\t");
401 		col = 8;
402 	    }
403 	    printf(" \"%s\"", *cpp);
404 	    col += len;
405 	}
406 	printf("\n");
407 	return 0;
408     }
409     cpp = (char **)genget(name, telopts, sizeof(char *));
410     if (Ambiguous(cpp)) {
411 	fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
412 					name, cmd);
413 	return 0;
414     }
415     if (cpp) {
416 	val = cpp - telopts;
417     } else {
418 	register char *cp = name;
419 
420 	while (*cp >= '0' && *cp <= '9') {
421 	    val *= 10;
422 	    val += *cp - '0';
423 	    cp++;
424 	}
425 	if (*cp != 0) {
426 	    fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
427 					name, cmd);
428 	    return 0;
429 	} else if (val < 0 || val > 255) {
430 	    fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
431 					name, cmd);
432 	    return 0;
433 	}
434     }
435     if (!connected) {
436 	printf("?Need to be connected first.\n");
437 	return 0;
438     }
439     (*func)(val, 1);
440     return 1;
441 }
442 
443     static int
444 send_help()
445 {
446     struct sendlist *s;	/* pointer to current command */
447     for (s = Sendlist; s->name; s++) {
448 	if (s->help)
449 	    printf("%-15s %s\n", s->name, s->help);
450     }
451     return(0);
452 }
453 
454 /*
455  * The following are the routines and data structures referred
456  * to by the arguments to the "toggle" command.
457  */
458 
459     static int
460 lclchars()
461 {
462     donelclchars = 1;
463     return 1;
464 }
465 
466     static int
467 togdebug()
468 {
469 #ifndef	NOT43
470     if (net > 0 &&
471 	(SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
472 	    perror("setsockopt (SO_DEBUG)");
473     }
474 #else	/* NOT43 */
475     if (debug) {
476 	if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
477 	    perror("setsockopt (SO_DEBUG)");
478     } else
479 	printf("Cannot turn off socket debugging\n");
480 #endif	/* NOT43 */
481     return 1;
482 }
483 
484 
485     static int
486 togcrlf()
487 {
488     if (crlf) {
489 	printf("Will send carriage returns as telnet <CR><LF>.\n");
490     } else {
491 	printf("Will send carriage returns as telnet <CR><NUL>.\n");
492     }
493     return 1;
494 }
495 
496 int binmode;
497 
498     static int
499 togbinary(val)
500     int val;
501 {
502     donebinarytoggle = 1;
503 
504     if (val >= 0) {
505 	binmode = val;
506     } else {
507 	if (my_want_state_is_will(TELOPT_BINARY) &&
508 				my_want_state_is_do(TELOPT_BINARY)) {
509 	    binmode = 1;
510 	} else if (my_want_state_is_wont(TELOPT_BINARY) &&
511 				my_want_state_is_dont(TELOPT_BINARY)) {
512 	    binmode = 0;
513 	}
514 	val = binmode ? 0 : 1;
515     }
516 
517     if (val == 1) {
518 	if (my_want_state_is_will(TELOPT_BINARY) &&
519 					my_want_state_is_do(TELOPT_BINARY)) {
520 	    printf("Already operating in binary mode with remote host.\n");
521 	} else {
522 	    printf("Negotiating binary mode with remote host.\n");
523 	    tel_enter_binary(3);
524 	}
525     } else {
526 	if (my_want_state_is_wont(TELOPT_BINARY) &&
527 					my_want_state_is_dont(TELOPT_BINARY)) {
528 	    printf("Already in network ascii mode with remote host.\n");
529 	} else {
530 	    printf("Negotiating network ascii mode with remote host.\n");
531 	    tel_leave_binary(3);
532 	}
533     }
534     return 1;
535 }
536 
537     static int
538 togrbinary(val)
539     int val;
540 {
541     donebinarytoggle = 1;
542 
543     if (val == -1)
544 	val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
545 
546     if (val == 1) {
547 	if (my_want_state_is_do(TELOPT_BINARY)) {
548 	    printf("Already receiving in binary mode.\n");
549 	} else {
550 	    printf("Negotiating binary mode on input.\n");
551 	    tel_enter_binary(1);
552 	}
553     } else {
554 	if (my_want_state_is_dont(TELOPT_BINARY)) {
555 	    printf("Already receiving in network ascii mode.\n");
556 	} else {
557 	    printf("Negotiating network ascii mode on input.\n");
558 	    tel_leave_binary(1);
559 	}
560     }
561     return 1;
562 }
563 
564     static int
565 togxbinary(val)
566     int val;
567 {
568     donebinarytoggle = 1;
569 
570     if (val == -1)
571 	val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
572 
573     if (val == 1) {
574 	if (my_want_state_is_will(TELOPT_BINARY)) {
575 	    printf("Already transmitting in binary mode.\n");
576 	} else {
577 	    printf("Negotiating binary mode on output.\n");
578 	    tel_enter_binary(2);
579 	}
580     } else {
581 	if (my_want_state_is_wont(TELOPT_BINARY)) {
582 	    printf("Already transmitting in network ascii mode.\n");
583 	} else {
584 	    printf("Negotiating network ascii mode on output.\n");
585 	    tel_leave_binary(2);
586 	}
587     }
588     return 1;
589 }
590 
591 
592 static int togglehelp P((void));
593 #if	defined(AUTHENTICATION)
594 extern int auth_togdebug P((int));
595 #endif
596 #if	defined(ENCRYPTION)
597 extern int EncryptAutoEnc P((int));
598 extern int EncryptAutoDec P((int));
599 extern int EncryptDebug P((int));
600 extern int EncryptVerbose P((int));
601 #endif
602 
603 struct togglelist {
604     char	*name;		/* name of toggle */
605     char	*help;		/* help message */
606     int		(*handler)();	/* routine to do actual setting */
607     int		*variable;
608     char	*actionexplanation;
609 };
610 
611 static struct togglelist Togglelist[] = {
612     { "autoflush",
613 	"flushing of output when sending interrupt characters",
614 	    0,
615 		&autoflush,
616 		    "flush output when sending interrupt characters" },
617     { "autosynch",
618 	"automatic sending of interrupt characters in urgent mode",
619 	    0,
620 		&autosynch,
621 		    "send interrupt characters in urgent mode" },
622 #if	defined(AUTHENTICATION)
623     { "autologin",
624 	"automatic sending of login and/or authentication info",
625 	    0,
626 		&autologin,
627 		    "send login name and/or authentication information" },
628     { "authdebug",
629 	"Toggle authentication debugging",
630 	    auth_togdebug,
631 		0,
632 		     "print authentication debugging information" },
633 #endif
634 #if	defined(ENCRYPTION)
635     { "autoencrypt",
636 	"automatic encryption of data stream",
637 	    EncryptAutoEnc,
638 		0,
639 		    "automatically encrypt output" },
640     { "autodecrypt",
641 	"automatic decryption of data stream",
642 	    EncryptAutoDec,
643 		0,
644 		    "automatically decrypt input" },
645     { "verbose_encrypt",
646 	"Toggle verbose encryption output",
647 	    EncryptVerbose,
648 		0,
649 		    "print verbose encryption output" },
650     { "encdebug",
651 	"Toggle encryption debugging",
652 	    EncryptDebug,
653 		0,
654 		    "print encryption debugging information" },
655 #endif
656     { "skiprc",
657 	"don't read ~/.telnetrc file",
658 	    0,
659 		&skiprc,
660 		    "skip reading of ~/.telnetrc file" },
661     { "binary",
662 	"sending and receiving of binary data",
663 	    togbinary,
664 		0,
665 		    0 },
666     { "inbinary",
667 	"receiving of binary data",
668 	    togrbinary,
669 		0,
670 		    0 },
671     { "outbinary",
672 	"sending of binary data",
673 	    togxbinary,
674 		0,
675 		    0 },
676     { "crlf",
677 	"sending carriage returns as telnet <CR><LF>",
678 	    togcrlf,
679 		&crlf,
680 		    0 },
681     { "crmod",
682 	"mapping of received carriage returns",
683 	    0,
684 		&crmod,
685 		    "map carriage return on output" },
686     { "localchars",
687 	"local recognition of certain control characters",
688 	    lclchars,
689 		&localchars,
690 		    "recognize certain control characters" },
691     { " ", "", 0 },		/* empty line */
692 #if	defined(unix) && defined(TN3270)
693     { "apitrace",
694 	"(debugging) toggle tracing of API transactions",
695 	    0,
696 		&apitrace,
697 		    "trace API transactions" },
698     { "cursesdata",
699 	"(debugging) toggle printing of hexadecimal curses data",
700 	    0,
701 		&cursesdata,
702 		    "print hexadecimal representation of curses data" },
703 #endif	/* defined(unix) && defined(TN3270) */
704     { "debug",
705 	"debugging",
706 	    togdebug,
707 		&debug,
708 		    "turn on socket level debugging" },
709     { "netdata",
710 	"printing of hexadecimal network data (debugging)",
711 	    0,
712 		&netdata,
713 		    "print hexadecimal representation of network traffic" },
714     { "prettydump",
715 	"output of \"netdata\" to user readable format (debugging)",
716 	    0,
717 		&prettydump,
718 		    "print user readable output for \"netdata\"" },
719     { "options",
720 	"viewing of options processing (debugging)",
721 	    0,
722 		&showoptions,
723 		    "show option processing" },
724 #if	defined(unix)
725     { "termdata",
726 	"(debugging) toggle printing of hexadecimal terminal data",
727 	    0,
728 		&termdata,
729 		    "print hexadecimal representation of terminal traffic" },
730 #endif	/* defined(unix) */
731     { "?",
732 	0,
733 	    togglehelp },
734     { "help",
735 	0,
736 	    togglehelp },
737     { 0 }
738 };
739 
740     static int
741 togglehelp()
742 {
743     struct togglelist *c;
744 
745     for (c = Togglelist; c->name; c++) {
746 	if (c->help) {
747 	    if (*c->help)
748 		printf("%-15s toggle %s\n", c->name, c->help);
749 	    else
750 		printf("\n");
751 	}
752     }
753     printf("\n");
754     printf("%-15s %s\n", "?", "display help information");
755     return 0;
756 }
757 
758     static void
759 settogglehelp(set)
760     int set;
761 {
762     struct togglelist *c;
763 
764     for (c = Togglelist; c->name; c++) {
765 	if (c->help) {
766 	    if (*c->help)
767 		printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
768 						c->help);
769 	    else
770 		printf("\n");
771 	}
772     }
773 }
774 
775 #define	GETTOGGLE(name) (struct togglelist *) \
776 		genget(name, (char **) Togglelist, sizeof(struct togglelist))
777 
778     static int
779 toggle(argc, argv)
780     int  argc;
781     char *argv[];
782 {
783     int retval = 1;
784     char *name;
785     struct togglelist *c;
786 
787     if (argc < 2) {
788 	fprintf(stderr,
789 	    "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
790 	return 0;
791     }
792     argc--;
793     argv++;
794     while (argc--) {
795 	name = *argv++;
796 	c = GETTOGGLE(name);
797 	if (Ambiguous(c)) {
798 	    fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
799 					name);
800 	    return 0;
801 	} else if (c == 0) {
802 	    fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
803 					name);
804 	    return 0;
805 	} else {
806 	    if (c->variable) {
807 		*c->variable = !*c->variable;		/* invert it */
808 		if (c->actionexplanation) {
809 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
810 							c->actionexplanation);
811 		}
812 	    }
813 	    if (c->handler) {
814 		retval &= (*c->handler)(-1);
815 	    }
816 	}
817     }
818     return retval;
819 }
820 
821 /*
822  * The following perform the "set" command.
823  */
824 
825 #ifdef	USE_TERMIO
826 struct termio new_tc = { 0 };
827 #endif
828 
829 struct setlist {
830     char *name;				/* name */
831     char *help;				/* help information */
832     void (*handler)();
833     cc_t *charp;			/* where it is located at */
834 };
835 
836 static struct setlist Setlist[] = {
837 #ifdef	KLUDGELINEMODE
838     { "echo", 	"character to toggle local echoing on/off", 0, &echoc },
839 #endif
840     { "escape",	"character to escape back to telnet command mode", 0, &escape },
841     { "rlogin", "rlogin escape character", 0, &rlogin },
842     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
843     { " ", "" },
844     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
845     { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
846     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
847     { "quit",	"character to cause an Abort process", 0, termQuitCharp },
848     { "eof",	"character to cause an EOF ", 0, termEofCharp },
849     { " ", "" },
850     { " ", "The following are for local editing in linemode", 0, 0 },
851     { "erase",	"character to use to erase a character", 0, termEraseCharp },
852     { "kill",	"character to use to erase a line", 0, termKillCharp },
853     { "lnext",	"character to use for literal next", 0, termLiteralNextCharp },
854     { "susp",	"character to cause a Suspend Process", 0, termSuspCharp },
855     { "reprint", "character to use for line reprint", 0, termRprntCharp },
856     { "worderase", "character to use to erase a word", 0, termWerasCharp },
857     { "start",	"character to use for XON", 0, termStartCharp },
858     { "stop",	"character to use for XOFF", 0, termStopCharp },
859     { "forw1",	"alternate end of line character", 0, termForw1Charp },
860     { "forw2",	"alternate end of line character", 0, termForw2Charp },
861     { "ayt",	"alternate AYT character", 0, termAytCharp },
862     { 0 }
863 };
864 
865 #if	defined(CRAY) && !defined(__STDC__)
866 /* Work around compiler bug in pcc 4.1.5 */
867     void
868 _setlist_init()
869 {
870 #ifndef	KLUDGELINEMODE
871 #define	N 5
872 #else
873 #define	N 6
874 #endif
875 	Setlist[N+0].charp = &termFlushChar;
876 	Setlist[N+1].charp = &termIntChar;
877 	Setlist[N+2].charp = &termQuitChar;
878 	Setlist[N+3].charp = &termEofChar;
879 	Setlist[N+6].charp = &termEraseChar;
880 	Setlist[N+7].charp = &termKillChar;
881 	Setlist[N+8].charp = &termLiteralNextChar;
882 	Setlist[N+9].charp = &termSuspChar;
883 	Setlist[N+10].charp = &termRprntChar;
884 	Setlist[N+11].charp = &termWerasChar;
885 	Setlist[N+12].charp = &termStartChar;
886 	Setlist[N+13].charp = &termStopChar;
887 	Setlist[N+14].charp = &termForw1Char;
888 	Setlist[N+15].charp = &termForw2Char;
889 	Setlist[N+16].charp = &termAytChar;
890 #undef	N
891 }
892 #endif	/* defined(CRAY) && !defined(__STDC__) */
893 
894     static struct setlist *
895 getset(name)
896     char *name;
897 {
898     return (struct setlist *)
899 		genget(name, (char **) Setlist, sizeof(struct setlist));
900 }
901 
902     void
903 set_escape_char(s)
904     char *s;
905 {
906 	if (rlogin != _POSIX_VDISABLE) {
907 		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
908 		printf("Telnet rlogin escape character is '%s'.\n",
909 					control(rlogin));
910 	} else {
911 		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
912 		printf("Telnet escape character is '%s'.\n", control(escape));
913 	}
914 }
915 
916     static int
917 setcmd(argc, argv)
918     int  argc;
919     char *argv[];
920 {
921     int value;
922     struct setlist *ct;
923     struct togglelist *c;
924 
925     if (argc < 2 || argc > 3) {
926 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
927 	return 0;
928     }
929     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
930 	for (ct = Setlist; ct->name; ct++)
931 	    printf("%-15s %s\n", ct->name, ct->help);
932 	printf("\n");
933 	settogglehelp(1);
934 	printf("%-15s %s\n", "?", "display help information");
935 	return 0;
936     }
937 
938     ct = getset(argv[1]);
939     if (ct == 0) {
940 	c = GETTOGGLE(argv[1]);
941 	if (c == 0) {
942 	    fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
943 			argv[1]);
944 	    return 0;
945 	} else if (Ambiguous(c)) {
946 	    fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
947 			argv[1]);
948 	    return 0;
949 	}
950 	if (c->variable) {
951 	    if ((argc == 2) || (strcmp("on", argv[2]) == 0))
952 		*c->variable = 1;
953 	    else if (strcmp("off", argv[2]) == 0)
954 		*c->variable = 0;
955 	    else {
956 		printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
957 		return 0;
958 	    }
959 	    if (c->actionexplanation) {
960 		printf("%s %s.\n", *c->variable? "Will" : "Won't",
961 							c->actionexplanation);
962 	    }
963 	}
964 	if (c->handler)
965 	    (*c->handler)(1);
966     } else if (argc != 3) {
967 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
968 	return 0;
969     } else if (Ambiguous(ct)) {
970 	fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
971 			argv[1]);
972 	return 0;
973     } else if (ct->handler) {
974 	(*ct->handler)(argv[2]);
975 	printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
976     } else {
977 	if (strcmp("off", argv[2])) {
978 	    value = special(argv[2]);
979 	} else {
980 	    value = _POSIX_VDISABLE;
981 	}
982 	*(ct->charp) = (cc_t)value;
983 	printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
984     }
985     slc_check();
986     return 1;
987 }
988 
989     static int
990 unsetcmd(argc, argv)
991     int  argc;
992     char *argv[];
993 {
994     struct setlist *ct;
995     struct togglelist *c;
996     register char *name;
997 
998     if (argc < 2) {
999 	fprintf(stderr,
1000 	    "Need an argument to 'unset' command.  'unset ?' for help.\n");
1001 	return 0;
1002     }
1003     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1004 	for (ct = Setlist; ct->name; ct++)
1005 	    printf("%-15s %s\n", ct->name, ct->help);
1006 	printf("\n");
1007 	settogglehelp(0);
1008 	printf("%-15s %s\n", "?", "display help information");
1009 	return 0;
1010     }
1011 
1012     argc--;
1013     argv++;
1014     while (argc--) {
1015 	name = *argv++;
1016 	ct = getset(name);
1017 	if (ct == 0) {
1018 	    c = GETTOGGLE(name);
1019 	    if (c == 0) {
1020 		fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1021 			name);
1022 		return 0;
1023 	    } else if (Ambiguous(c)) {
1024 		fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1025 			name);
1026 		return 0;
1027 	    }
1028 	    if (c->variable) {
1029 		*c->variable = 0;
1030 		if (c->actionexplanation) {
1031 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
1032 							c->actionexplanation);
1033 		}
1034 	    }
1035 	    if (c->handler)
1036 		(*c->handler)(0);
1037 	} else if (Ambiguous(ct)) {
1038 	    fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1039 			name);
1040 	    return 0;
1041 	} else if (ct->handler) {
1042 	    (*ct->handler)(0);
1043 	    printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1044 	} else {
1045 	    *(ct->charp) = _POSIX_VDISABLE;
1046 	    printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1047 	}
1048     }
1049     return 1;
1050 }
1051 
1052 /*
1053  * The following are the data structures and routines for the
1054  * 'mode' command.
1055  */
1056 #ifdef	KLUDGELINEMODE
1057 extern int kludgelinemode;
1058 
1059     static int
1060 dokludgemode()
1061 {
1062     kludgelinemode = 1;
1063     send_wont(TELOPT_LINEMODE, 1);
1064     send_dont(TELOPT_SGA, 1);
1065     send_dont(TELOPT_ECHO, 1);
1066 }
1067 #endif
1068 
1069     static int
1070 dolinemode()
1071 {
1072 #ifdef	KLUDGELINEMODE
1073     if (kludgelinemode)
1074 	send_dont(TELOPT_SGA, 1);
1075 #endif
1076     send_will(TELOPT_LINEMODE, 1);
1077     send_dont(TELOPT_ECHO, 1);
1078     return 1;
1079 }
1080 
1081     static int
1082 docharmode()
1083 {
1084 #ifdef	KLUDGELINEMODE
1085     if (kludgelinemode)
1086 	send_do(TELOPT_SGA, 1);
1087     else
1088 #endif
1089     send_wont(TELOPT_LINEMODE, 1);
1090     send_do(TELOPT_ECHO, 1);
1091     return 1;
1092 }
1093 
1094     static int
1095 dolmmode(bit, on)
1096     int bit, on;
1097 {
1098     unsigned char c;
1099     extern int linemode;
1100 
1101     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1102 	printf("?Need to have LINEMODE option enabled first.\n");
1103 	printf("'mode ?' for help.\n");
1104 	return 0;
1105     }
1106 
1107     if (on)
1108 	c = (linemode | bit);
1109     else
1110 	c = (linemode & ~bit);
1111     lm_mode(&c, 1, 1);
1112     return 1;
1113 }
1114 
1115     int
1116 setmode(bit)
1117 {
1118     return dolmmode(bit, 1);
1119 }
1120 
1121     int
1122 clearmode(bit)
1123 {
1124     return dolmmode(bit, 0);
1125 }
1126 
1127 struct modelist {
1128 	char	*name;		/* command name */
1129 	char	*help;		/* help string */
1130 	int	(*handler)();	/* routine which executes command */
1131 	int	needconnect;	/* Do we need to be connected to execute? */
1132 	int	arg1;
1133 };
1134 
1135 extern int modehelp();
1136 
1137 static struct modelist ModeList[] = {
1138     { "character", "Disable LINEMODE option",	docharmode, 1 },
1139 #ifdef	KLUDGELINEMODE
1140     { "",	"(or disable obsolete line-by-line mode)", 0 },
1141 #endif
1142     { "line",	"Enable LINEMODE option",	dolinemode, 1 },
1143 #ifdef	KLUDGELINEMODE
1144     { "",	"(or enable obsolete line-by-line mode)", 0 },
1145 #endif
1146     { "", "", 0 },
1147     { "",	"These require the LINEMODE option to be enabled", 0 },
1148     { "isig",	"Enable signal trapping",	setmode, 1, MODE_TRAPSIG },
1149     { "+isig",	0,				setmode, 1, MODE_TRAPSIG },
1150     { "-isig",	"Disable signal trapping",	clearmode, 1, MODE_TRAPSIG },
1151     { "edit",	"Enable character editing",	setmode, 1, MODE_EDIT },
1152     { "+edit",	0,				setmode, 1, MODE_EDIT },
1153     { "-edit",	"Disable character editing",	clearmode, 1, MODE_EDIT },
1154     { "softtabs", "Enable tab expansion",	setmode, 1, MODE_SOFT_TAB },
1155     { "+softtabs", 0,				setmode, 1, MODE_SOFT_TAB },
1156     { "-softtabs", "Disable character editing",	clearmode, 1, MODE_SOFT_TAB },
1157     { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
1158     { "+litecho", 0,				setmode, 1, MODE_LIT_ECHO },
1159     { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1160     { "help",	0,				modehelp, 0 },
1161 #ifdef	KLUDGELINEMODE
1162     { "kludgeline", 0,				dokludgemode, 1 },
1163 #endif
1164     { "", "", 0 },
1165     { "?",	"Print help information",	modehelp, 0 },
1166     { 0 },
1167 };
1168 
1169 
1170     int
1171 modehelp()
1172 {
1173     struct modelist *mt;
1174 
1175     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
1176     for (mt = ModeList; mt->name; mt++) {
1177 	if (mt->help) {
1178 	    if (*mt->help)
1179 		printf("%-15s %s\n", mt->name, mt->help);
1180 	    else
1181 		printf("\n");
1182 	}
1183     }
1184     return 0;
1185 }
1186 
1187 #define	GETMODECMD(name) (struct modelist *) \
1188 		genget(name, (char **) ModeList, sizeof(struct modelist))
1189 
1190     static int
1191 modecmd(argc, argv)
1192     int  argc;
1193     char *argv[];
1194 {
1195     struct modelist *mt;
1196 
1197     if (argc != 2) {
1198 	printf("'mode' command requires an argument\n");
1199 	printf("'mode ?' for help.\n");
1200     } else if ((mt = GETMODECMD(argv[1])) == 0) {
1201 	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1202     } else if (Ambiguous(mt)) {
1203 	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1204     } else if (mt->needconnect && !connected) {
1205 	printf("?Need to be connected first.\n");
1206 	printf("'mode ?' for help.\n");
1207     } else if (mt->handler) {
1208 	return (*mt->handler)(mt->arg1);
1209     }
1210     return 0;
1211 }
1212 
1213 /*
1214  * The following data structures and routines implement the
1215  * "display" command.
1216  */
1217 
1218     static int
1219 display(argc, argv)
1220     int  argc;
1221     char *argv[];
1222 {
1223     struct togglelist *tl;
1224     struct setlist *sl;
1225 
1226 #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
1227 			    if (*tl->variable) { \
1228 				printf("will"); \
1229 			    } else { \
1230 				printf("won't"); \
1231 			    } \
1232 			    printf(" %s.\n", tl->actionexplanation); \
1233 			}
1234 
1235 #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
1236 			if (sl->handler == 0) \
1237 			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1238 			else \
1239 			    printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1240 		    }
1241 
1242     if (argc == 1) {
1243 	for (tl = Togglelist; tl->name; tl++) {
1244 	    dotog(tl);
1245 	}
1246 	printf("\n");
1247 	for (sl = Setlist; sl->name; sl++) {
1248 	    doset(sl);
1249 	}
1250     } else {
1251 	int i;
1252 
1253 	for (i = 1; i < argc; i++) {
1254 	    sl = getset(argv[i]);
1255 	    tl = GETTOGGLE(argv[i]);
1256 	    if (Ambiguous(sl) || Ambiguous(tl)) {
1257 		printf("?Ambiguous argument '%s'.\n", argv[i]);
1258 		return 0;
1259 	    } else if (!sl && !tl) {
1260 		printf("?Unknown argument '%s'.\n", argv[i]);
1261 		return 0;
1262 	    } else {
1263 		if (tl) {
1264 		    dotog(tl);
1265 		}
1266 		if (sl) {
1267 		    doset(sl);
1268 		}
1269 	    }
1270 	}
1271     }
1272 /*@*/optionstatus();
1273 #if	defined(ENCRYPTION)
1274     EncryptStatus();
1275 #endif
1276     return 1;
1277 #undef	doset
1278 #undef	dotog
1279 }
1280 
1281 /*
1282  * The following are the data structures, and many of the routines,
1283  * relating to command processing.
1284  */
1285 
1286 /*
1287  * Set the escape character.
1288  */
1289 	static int
1290 setescape(argc, argv)
1291 	int argc;
1292 	char *argv[];
1293 {
1294 	register char *arg;
1295 	char buf[50];
1296 
1297 	printf(
1298 	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1299 				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1300 	if (argc > 2)
1301 		arg = argv[1];
1302 	else {
1303 		printf("new escape character: ");
1304 		(void) fgets(buf, sizeof(buf), stdin);
1305 		arg = buf;
1306 	}
1307 	if (arg[0] != '\0')
1308 		escape = arg[0];
1309 	if (!In3270) {
1310 		printf("Escape character is '%s'.\n", control(escape));
1311 	}
1312 	(void) fflush(stdout);
1313 	return 1;
1314 }
1315 
1316     /*VARARGS*/
1317     static int
1318 togcrmod()
1319 {
1320     crmod = !crmod;
1321     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1322     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1323     (void) fflush(stdout);
1324     return 1;
1325 }
1326 
1327     /*VARARGS*/
1328     int
1329 suspend()
1330 {
1331 #ifdef	SIGTSTP
1332     setcommandmode();
1333     {
1334 	long oldrows, oldcols, newrows, newcols, err;
1335 
1336 	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1337 	(void) kill(0, SIGTSTP);
1338 	/*
1339 	 * If we didn't get the window size before the SUSPEND, but we
1340 	 * can get them now (???), then send the NAWS to make sure that
1341 	 * we are set up for the right window size.
1342 	 */
1343 	if (TerminalWindowSize(&newrows, &newcols) && connected &&
1344 	    (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1345 		sendnaws();
1346 	}
1347     }
1348     /* reget parameters in case they were changed */
1349     TerminalSaveState();
1350     setconnmode(0);
1351 #else
1352     printf("Suspend is not supported.  Try the '!' command instead\n");
1353 #endif
1354     return 1;
1355 }
1356 
1357 #if	!defined(TN3270)
1358     /*ARGSUSED*/
1359     int
1360 shell(argc, argv)
1361     int argc;
1362     char *argv[];
1363 {
1364     long oldrows, oldcols, newrows, newcols, err;
1365 
1366     setcommandmode();
1367 
1368     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1369     switch(vfork()) {
1370     case -1:
1371 	perror("Fork failed\n");
1372 	break;
1373 
1374     case 0:
1375 	{
1376 	    /*
1377 	     * Fire up the shell in the child.
1378 	     */
1379 	    register char *shellp, *shellname;
1380 	    extern char *rindex();
1381 
1382 	    shellp = getenv("SHELL");
1383 	    if (shellp == NULL)
1384 		shellp = "/bin/sh";
1385 	    if ((shellname = rindex(shellp, '/')) == 0)
1386 		shellname = shellp;
1387 	    else
1388 		shellname++;
1389 	    if (argc > 1)
1390 		execl(shellp, shellname, "-c", &saveline[1], 0);
1391 	    else
1392 		execl(shellp, shellname, 0);
1393 	    perror("Execl");
1394 	    _exit(1);
1395 	}
1396     default:
1397 	    (void)wait((int *)0);	/* Wait for the shell to complete */
1398 
1399 	    if (TerminalWindowSize(&newrows, &newcols) && connected &&
1400 		(err || ((oldrows != newrows) || (oldcols != newcols)))) {
1401 		    sendnaws();
1402 	    }
1403 	    break;
1404     }
1405     return 1;
1406 }
1407 #endif	/* !defined(TN3270) */
1408 
1409     /*VARARGS*/
1410     static
1411 bye(argc, argv)
1412     int  argc;		/* Number of arguments */
1413     char *argv[];	/* arguments */
1414 {
1415     extern int resettermname;
1416 
1417     if (connected) {
1418 	(void) shutdown(net, 2);
1419 	printf("Connection closed.\n");
1420 	(void) NetClose(net);
1421 	connected = 0;
1422 	resettermname = 1;
1423 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
1424 	auth_encrypt_connect(connected);
1425 #endif
1426 	/* reset options */
1427 	tninit();
1428 #if	defined(TN3270)
1429 	SetIn3270();		/* Get out of 3270 mode */
1430 #endif	/* defined(TN3270) */
1431     }
1432     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1433 	longjmp(toplevel, 1);
1434 	/* NOTREACHED */
1435     }
1436     return 1;			/* Keep lint, etc., happy */
1437 }
1438 
1439 /*VARARGS*/
1440 quit()
1441 {
1442 	(void) call(bye, "bye", "fromquit", 0);
1443 	Exit(0);
1444 	/*NOTREACHED*/
1445 }
1446 
1447 /*VARARGS*/
1448 	int
1449 logout()
1450 {
1451 	send_do(TELOPT_LOGOUT, 1);
1452 	(void) netflush();
1453 	return 1;
1454 }
1455 
1456 
1457 /*
1458  * The SLC command.
1459  */
1460 
1461 struct slclist {
1462 	char	*name;
1463 	char	*help;
1464 	void	(*handler)();
1465 	int	arg;
1466 };
1467 
1468 static void slc_help();
1469 
1470 struct slclist SlcList[] = {
1471     { "export",	"Use local special character definitions",
1472 						slc_mode_export,	0 },
1473     { "import",	"Use remote special character definitions",
1474 						slc_mode_import,	1 },
1475     { "check",	"Verify remote special character definitions",
1476 						slc_mode_import,	0 },
1477     { "help",	0,				slc_help,		0 },
1478     { "?",	"Print help information",	slc_help,		0 },
1479     { 0 },
1480 };
1481 
1482     static void
1483 slc_help()
1484 {
1485     struct slclist *c;
1486 
1487     for (c = SlcList; c->name; c++) {
1488 	if (c->help) {
1489 	    if (*c->help)
1490 		printf("%-15s %s\n", c->name, c->help);
1491 	    else
1492 		printf("\n");
1493 	}
1494     }
1495 }
1496 
1497     static struct slclist *
1498 getslc(name)
1499     char *name;
1500 {
1501     return (struct slclist *)
1502 		genget(name, (char **) SlcList, sizeof(struct slclist));
1503 }
1504 
1505     static
1506 slccmd(argc, argv)
1507     int  argc;
1508     char *argv[];
1509 {
1510     struct slclist *c;
1511 
1512     if (argc != 2) {
1513 	fprintf(stderr,
1514 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
1515 	return 0;
1516     }
1517     c = getslc(argv[1]);
1518     if (c == 0) {
1519         fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1520     				argv[1]);
1521         return 0;
1522     }
1523     if (Ambiguous(c)) {
1524         fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1525     				argv[1]);
1526         return 0;
1527     }
1528     (*c->handler)(c->arg);
1529     slcstate();
1530     return 1;
1531 }
1532 
1533 /*
1534  * The ENVIRON command.
1535  */
1536 
1537 struct envlist {
1538 	char	*name;
1539 	char	*help;
1540 	void	(*handler)();
1541 	int	narg;
1542 };
1543 
1544 extern struct env_lst *
1545 	env_define P((unsigned char *, unsigned char *));
1546 extern void
1547 	env_undefine P((unsigned char *)),
1548 	env_export P((unsigned char *)),
1549 	env_unexport P((unsigned char *)),
1550 	env_send P((unsigned char *)),
1551 #ifdef	ENV_HACK
1552 	env_varval P((unsigned char *)),
1553 #endif
1554 	env_list P((void));
1555 static void
1556 	env_help P((void));
1557 
1558 struct envlist EnvList[] = {
1559     { "define",	"Define an environment variable",
1560 						(void (*)())env_define,	2 },
1561     { "undefine", "Undefine an environment variable",
1562 						env_undefine,	1 },
1563     { "export",	"Mark an environment variable for automatic export",
1564 						env_export,	1 },
1565     { "unexport", "Don't mark an environment variable for automatic export",
1566 						env_unexport,	1 },
1567     { "send",	"Send an environment variable", env_send,	1 },
1568     { "list",	"List the current environment variables",
1569 						env_list,	0 },
1570 #ifdef	ENV_HACK
1571     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1572 						env_varval,    1 },
1573 #endif
1574     { "help",	0,				env_help,		0 },
1575     { "?",	"Print help information",	env_help,		0 },
1576     { 0 },
1577 };
1578 
1579     static void
1580 env_help()
1581 {
1582     struct envlist *c;
1583 
1584     for (c = EnvList; c->name; c++) {
1585 	if (c->help) {
1586 	    if (*c->help)
1587 		printf("%-15s %s\n", c->name, c->help);
1588 	    else
1589 		printf("\n");
1590 	}
1591     }
1592 }
1593 
1594     static struct envlist *
1595 getenvcmd(name)
1596     char *name;
1597 {
1598     return (struct envlist *)
1599 		genget(name, (char **) EnvList, sizeof(struct envlist));
1600 }
1601 
1602 env_cmd(argc, argv)
1603     int  argc;
1604     char *argv[];
1605 {
1606     struct envlist *c;
1607 
1608     if (argc < 2) {
1609 	fprintf(stderr,
1610 	    "Need an argument to 'environ' command.  'environ ?' for help.\n");
1611 	return 0;
1612     }
1613     c = getenvcmd(argv[1]);
1614     if (c == 0) {
1615         fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1616     				argv[1]);
1617         return 0;
1618     }
1619     if (Ambiguous(c)) {
1620         fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1621     				argv[1]);
1622         return 0;
1623     }
1624     if (c->narg + 2 != argc) {
1625 	fprintf(stderr,
1626 	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
1627 		c->narg < argc + 2 ? "only " : "",
1628 		c->narg, c->narg == 1 ? "" : "s", c->name);
1629 	return 0;
1630     }
1631     (*c->handler)(argv[2], argv[3]);
1632     return 1;
1633 }
1634 
1635 struct env_lst {
1636 	struct env_lst *next;	/* pointer to next structure */
1637 	struct env_lst *prev;	/* pointer to previous structure */
1638 	unsigned char *var;	/* pointer to variable name */
1639 	unsigned char *value;	/* pointer to variable value */
1640 	int export;		/* 1 -> export with default list of variables */
1641 	int welldefined;	/* A well defined variable */
1642 };
1643 
1644 struct env_lst envlisthead;
1645 
1646 	struct env_lst *
1647 env_find(var)
1648 	unsigned char *var;
1649 {
1650 	register struct env_lst *ep;
1651 
1652 	for (ep = envlisthead.next; ep; ep = ep->next) {
1653 		if (strcmp((char *)ep->var, (char *)var) == 0)
1654 			return(ep);
1655 	}
1656 	return(NULL);
1657 }
1658 
1659 	void
1660 env_init()
1661 {
1662 	extern char **environ;
1663 	register char **epp, *cp;
1664 	register struct env_lst *ep;
1665 	extern char *index();
1666 
1667 	for (epp = environ; *epp; epp++) {
1668 		if (cp = index(*epp, '=')) {
1669 			*cp = '\0';
1670 			ep = env_define((unsigned char *)*epp,
1671 					(unsigned char *)cp+1);
1672 			ep->export = 0;
1673 			*cp = '=';
1674 		}
1675 	}
1676 	/*
1677 	 * Special case for DISPLAY variable.  If it is ":0.0" or
1678 	 * "unix:0.0", we have to get rid of "unix" and insert our
1679 	 * hostname.
1680 	 */
1681 	if ((ep = env_find("DISPLAY"))
1682 	    && ((*ep->value == ':')
1683 	        || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1684 		char hbuf[256+1];
1685 		char *cp2 = index((char *)ep->value, ':');
1686 
1687 		gethostname(hbuf, 256);
1688 		hbuf[256] = '\0';
1689 		cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1690 		sprintf((char *)cp, "%s%s", hbuf, cp2);
1691 		free(ep->value);
1692 		ep->value = (unsigned char *)cp;
1693 	}
1694 	/*
1695 	 * If USER is not defined, but LOGNAME is, then add
1696 	 * USER with the value from LOGNAME.  By default, we
1697 	 * don't export the USER variable.
1698 	 */
1699 	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1700 		env_define((unsigned char *)"USER", ep->value);
1701 		env_unexport((unsigned char *)"USER");
1702 	}
1703 	env_export((unsigned char *)"DISPLAY");
1704 	env_export((unsigned char *)"PRINTER");
1705 }
1706 
1707 	struct env_lst *
1708 env_define(var, value)
1709 	unsigned char *var, *value;
1710 {
1711 	register struct env_lst *ep;
1712 
1713 	if (ep = env_find(var)) {
1714 		if (ep->var)
1715 			free(ep->var);
1716 		if (ep->value)
1717 			free(ep->value);
1718 	} else {
1719 		ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1720 		ep->next = envlisthead.next;
1721 		envlisthead.next = ep;
1722 		ep->prev = &envlisthead;
1723 		if (ep->next)
1724 			ep->next->prev = ep;
1725 	}
1726 	ep->welldefined = opt_welldefined(var);
1727 	ep->export = 1;
1728 	ep->var = (unsigned char *)strdup((char *)var);
1729 	ep->value = (unsigned char *)strdup((char *)value);
1730 	return(ep);
1731 }
1732 
1733 	void
1734 env_undefine(var)
1735 	unsigned char *var;
1736 {
1737 	register struct env_lst *ep;
1738 
1739 	if (ep = env_find(var)) {
1740 		ep->prev->next = ep->next;
1741 		if (ep->next)
1742 			ep->next->prev = ep->prev;
1743 		if (ep->var)
1744 			free(ep->var);
1745 		if (ep->value)
1746 			free(ep->value);
1747 		free(ep);
1748 	}
1749 }
1750 
1751 	void
1752 env_export(var)
1753 	unsigned char *var;
1754 {
1755 	register struct env_lst *ep;
1756 
1757 	if (ep = env_find(var))
1758 		ep->export = 1;
1759 }
1760 
1761 	void
1762 env_unexport(var)
1763 	unsigned char *var;
1764 {
1765 	register struct env_lst *ep;
1766 
1767 	if (ep = env_find(var))
1768 		ep->export = 0;
1769 }
1770 
1771 	void
1772 env_send(var)
1773 	unsigned char *var;
1774 {
1775 	register struct env_lst *ep;
1776 
1777         if (my_state_is_wont(TELOPT_ENVIRON)) {
1778 		fprintf(stderr,
1779 		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1780 									var);
1781 		return;
1782 	}
1783 	ep = env_find(var);
1784 	if (ep == 0) {
1785 		fprintf(stderr, "Cannot send '%s': variable not defined\n",
1786 									var);
1787 		return;
1788 	}
1789 	env_opt_start_info();
1790 	env_opt_add(ep->var);
1791 	env_opt_end(0);
1792 }
1793 
1794 	void
1795 env_list()
1796 {
1797 	register struct env_lst *ep;
1798 
1799 	for (ep = envlisthead.next; ep; ep = ep->next) {
1800 		printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1801 					ep->var, ep->value);
1802 	}
1803 }
1804 
1805 	unsigned char *
1806 env_default(init, welldefined)
1807 	int init;
1808 {
1809 	static struct env_lst *nep = NULL;
1810 
1811 	if (init) {
1812 		nep = &envlisthead;
1813 		return;
1814 	}
1815 	if (nep) {
1816 		while (nep = nep->next) {
1817 			if (nep->export && (nep->welldefined == welldefined))
1818 				return(nep->var);
1819 		}
1820 	}
1821 	return(NULL);
1822 }
1823 
1824 	unsigned char *
1825 env_getvalue(var)
1826 	unsigned char *var;
1827 {
1828 	register struct env_lst *ep;
1829 
1830 	if (ep = env_find(var))
1831 		return(ep->value);
1832 	return(NULL);
1833 }
1834 
1835 #ifdef	ENV_HACK
1836 	void
1837 env_varval(what)
1838 	unsigned char *what;
1839 {
1840 	extern int env_var, env_value, env_auto;
1841 	int len = strlen(what);
1842 
1843 	if (len == 0)
1844 		goto unknown;
1845 
1846 	if (strncasecmp(what, "status", len) == 0) {
1847 		if (env_auto)
1848 			printf("%s%s", "VAR and VALUE are/will be ",
1849 					"determined automatically\n");
1850 		if (env_var == ENV_VAR)
1851 			printf("VAR and VALUE set to correct definitions\n");
1852 		else
1853 			printf("VAR and VALUE definitions are reversed\n");
1854 	} else if (strncasecmp(what, "auto", len) == 0) {
1855 		env_auto = 1;
1856 		env_var = ENV_VALUE;
1857 		env_value = ENV_VAR;
1858 	} else if (strncasecmp(what, "right", len) == 0) {
1859 		env_auto = 0;
1860 		env_var = ENV_VAR;
1861 		env_value = ENV_VALUE;
1862 	} else if (strncasecmp(what, "wrong", len) == 0) {
1863 		env_auto = 0;
1864 		env_var = ENV_VALUE;
1865 		env_value = ENV_VAR;
1866 	} else {
1867 unknown:
1868 		printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1869 	}
1870 }
1871 #endif
1872 
1873 #if	defined(AUTHENTICATION)
1874 /*
1875  * The AUTHENTICATE command.
1876  */
1877 
1878 struct authlist {
1879 	char	*name;
1880 	char	*help;
1881 	int	(*handler)();
1882 	int	narg;
1883 };
1884 
1885 extern int
1886 	auth_enable P((int)),
1887 	auth_disable P((int)),
1888 	auth_status P((void));
1889 static int
1890 	auth_help P((void));
1891 
1892 struct authlist AuthList[] = {
1893     { "status",	"Display current status of authentication information",
1894 						auth_status,	0 },
1895     { "disable", "Disable an authentication type ('auth disable ?' for more)",
1896 						auth_disable,	1 },
1897     { "enable", "Enable an authentication type ('auth enable ?' for more)",
1898 						auth_enable,	1 },
1899     { "help",	0,				auth_help,		0 },
1900     { "?",	"Print help information",	auth_help,		0 },
1901     { 0 },
1902 };
1903 
1904     static int
1905 auth_help()
1906 {
1907     struct authlist *c;
1908 
1909     for (c = AuthList; c->name; c++) {
1910 	if (c->help) {
1911 	    if (*c->help)
1912 		printf("%-15s %s\n", c->name, c->help);
1913 	    else
1914 		printf("\n");
1915 	}
1916     }
1917     return 0;
1918 }
1919 
1920 auth_cmd(argc, argv)
1921     int  argc;
1922     char *argv[];
1923 {
1924     struct authlist *c;
1925 
1926     c = (struct authlist *)
1927 		genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1928     if (c == 0) {
1929         fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1930     				argv[1]);
1931         return 0;
1932     }
1933     if (Ambiguous(c)) {
1934         fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1935     				argv[1]);
1936         return 0;
1937     }
1938     if (c->narg + 2 != argc) {
1939 	fprintf(stderr,
1940 	    "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
1941 		c->narg < argc + 2 ? "only " : "",
1942 		c->narg, c->narg == 1 ? "" : "s", c->name);
1943 	return 0;
1944     }
1945     return((*c->handler)(argv[2], argv[3]));
1946 }
1947 #endif
1948 
1949 #if	defined(ENCRYPTION)
1950 /*
1951  * The ENCRYPT command.
1952  */
1953 
1954 struct encryptlist {
1955 	char	*name;
1956 	char	*help;
1957 	int	(*handler)();
1958 	int	needconnect;
1959 	int	minarg;
1960 	int	maxarg;
1961 };
1962 
1963 extern int
1964 	EncryptEnable P((char *, char *)),
1965 	EncryptDisable P((char *, char *)),
1966 	EncryptType P((char *, char *)),
1967 	EncryptStart P((char *)),
1968 	EncryptStartInput P((void)),
1969 	EncryptStartOutput P((void)),
1970 	EncryptStop P((char *)),
1971 	EncryptStopInput P((void)),
1972 	EncryptStopOutput P((void)),
1973 	EncryptStatus P((void));
1974 static int
1975 	EncryptHelp P((void));
1976 
1977 struct encryptlist EncryptList[] = {
1978     { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1979 						EncryptEnable, 1, 1, 2 },
1980     { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1981 						EncryptDisable, 0, 1, 2 },
1982     { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1983 						EncryptType, 0, 1, 1 },
1984     { "start", "Start encryption. ('encrypt start ?' for more)",
1985 						EncryptStart, 1, 0, 1 },
1986     { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1987 						EncryptStop, 1, 0, 1 },
1988     { "input", "Start encrypting the input stream",
1989 						EncryptStartInput, 1, 0, 0 },
1990     { "-input", "Stop encrypting the input stream",
1991 						EncryptStopInput, 1, 0, 0 },
1992     { "output", "Start encrypting the output stream",
1993 						EncryptStartOutput, 1, 0, 0 },
1994     { "-output", "Stop encrypting the output stream",
1995 						EncryptStopOutput, 1, 0, 0 },
1996 
1997     { "status",	"Display current status of authentication information",
1998 						EncryptStatus,	0, 0, 0 },
1999     { "help",	0,				EncryptHelp,	0, 0, 0 },
2000     { "?",	"Print help information",	EncryptHelp,	0, 0, 0 },
2001     { 0 },
2002 };
2003 
2004     static int
2005 EncryptHelp()
2006 {
2007     struct encryptlist *c;
2008 
2009     for (c = EncryptList; c->name; c++) {
2010 	if (c->help) {
2011 	    if (*c->help)
2012 		printf("%-15s %s\n", c->name, c->help);
2013 	    else
2014 		printf("\n");
2015 	}
2016     }
2017     return 0;
2018 }
2019 
2020 encrypt_cmd(argc, argv)
2021     int  argc;
2022     char *argv[];
2023 {
2024     struct encryptlist *c;
2025 
2026     c = (struct encryptlist *)
2027 		genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2028     if (c == 0) {
2029         fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2030     				argv[1]);
2031         return 0;
2032     }
2033     if (Ambiguous(c)) {
2034         fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2035     				argv[1]);
2036         return 0;
2037     }
2038     argc -= 2;
2039     if (argc < c->minarg || argc > c->maxarg) {
2040 	if (c->minarg == c->maxarg) {
2041 	    fprintf(stderr, "Need %s%d argument%s ",
2042 		c->minarg < argc ? "only " : "", c->minarg,
2043 		c->minarg == 1 ? "" : "s");
2044 	} else {
2045 	    fprintf(stderr, "Need %s%d-%d arguments ",
2046 		c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2047 	}
2048 	fprintf(stderr, "to 'encrypt %s' command.  'encrypt ?' for help.\n",
2049 		c->name);
2050 	return 0;
2051     }
2052     if (c->needconnect && !connected) {
2053 	if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2054 	    printf("?Need to be connected first.\n");
2055 	    return 0;
2056 	}
2057     }
2058     return ((*c->handler)(argc > 0 ? argv[2] : 0,
2059 			argc > 1 ? argv[3] : 0,
2060 			argc > 2 ? argv[4] : 0));
2061 }
2062 #endif
2063 
2064 #if	defined(unix) && defined(TN3270)
2065     static void
2066 filestuff(fd)
2067     int fd;
2068 {
2069     int res;
2070 
2071 #ifdef	F_GETOWN
2072     setconnmode(0);
2073     res = fcntl(fd, F_GETOWN, 0);
2074     setcommandmode();
2075 
2076     if (res == -1) {
2077 	perror("fcntl");
2078 	return;
2079     }
2080     printf("\tOwner is %d.\n", res);
2081 #endif
2082 
2083     setconnmode(0);
2084     res = fcntl(fd, F_GETFL, 0);
2085     setcommandmode();
2086 
2087     if (res == -1) {
2088 	perror("fcntl");
2089 	return;
2090     }
2091     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2092 }
2093 #endif /* defined(unix) && defined(TN3270) */
2094 
2095 /*
2096  * Print status about the connection.
2097  */
2098     /*ARGSUSED*/
2099     static
2100 status(argc, argv)
2101     int	 argc;
2102     char *argv[];
2103 {
2104     if (connected) {
2105 	printf("Connected to %s.\n", hostname);
2106 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2107 	    int mode = getconnmode();
2108 
2109 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
2110 		printf("Operating with LINEMODE option\n");
2111 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2112 		printf("%s catching of signals\n",
2113 					(mode&MODE_TRAPSIG) ? "Local" : "No");
2114 		slcstate();
2115 #ifdef	KLUDGELINEMODE
2116 	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2117 		printf("Operating in obsolete linemode\n");
2118 #endif
2119 	    } else {
2120 		printf("Operating in single character mode\n");
2121 		if (localchars)
2122 		    printf("Catching signals locally\n");
2123 	    }
2124 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2125 	    if (my_want_state_is_will(TELOPT_LFLOW))
2126 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2127 #if	defined(ENCRYPTION)
2128 	    encrypt_display();
2129 #endif
2130 	}
2131     } else {
2132 	printf("No connection.\n");
2133     }
2134 #   if !defined(TN3270)
2135     printf("Escape character is '%s'.\n", control(escape));
2136     (void) fflush(stdout);
2137 #   else /* !defined(TN3270) */
2138     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2139 	printf("Escape character is '%s'.\n", control(escape));
2140     }
2141 #   if defined(unix)
2142     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2143 	printf("SIGIO received %d time%s.\n",
2144 				sigiocount, (sigiocount == 1)? "":"s");
2145 	if (In3270) {
2146 	    printf("Process ID %d, process group %d.\n",
2147 					    getpid(), getpgrp(getpid()));
2148 	    printf("Terminal input:\n");
2149 	    filestuff(tin);
2150 	    printf("Terminal output:\n");
2151 	    filestuff(tout);
2152 	    printf("Network socket:\n");
2153 	    filestuff(net);
2154 	}
2155     }
2156     if (In3270 && transcom) {
2157        printf("Transparent mode command is '%s'.\n", transcom);
2158     }
2159 #   endif /* defined(unix) */
2160     (void) fflush(stdout);
2161     if (In3270) {
2162 	return 0;
2163     }
2164 #   endif /* defined(TN3270) */
2165     return 1;
2166 }
2167 
2168 #ifdef	SIGINFO
2169 /*
2170  * Function that gets called when SIGINFO is received.
2171  */
2172 ayt_status()
2173 {
2174     (void) call(status, "status", "notmuch", 0);
2175 }
2176 #endif
2177 
2178 unsigned long inet_addr();
2179 
2180     int
2181 tn(argc, argv)
2182     int argc;
2183     char *argv[];
2184 {
2185     register struct hostent *host = 0;
2186     struct sockaddr_in sin;
2187     struct servent *sp = 0;
2188     unsigned long temp;
2189     extern char *inet_ntoa();
2190 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2191     char *srp = 0, *strrchr();
2192     unsigned long sourceroute(), srlen;
2193 #endif
2194     char *cmd, *hostp = 0, *portp = 0, *user = 0;
2195 
2196     /* clear the socket address prior to use */
2197     bzero((char *)&sin, sizeof(sin));
2198 
2199     if (connected) {
2200 	printf("?Already connected to %s\n", hostname);
2201 	setuid(getuid());
2202 	return 0;
2203     }
2204     if (argc < 2) {
2205 	(void) strcpy(line, "open ");
2206 	printf("(to) ");
2207 	(void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2208 	makeargv();
2209 	argc = margc;
2210 	argv = margv;
2211     }
2212     cmd = *argv;
2213     --argc; ++argv;
2214     while (argc) {
2215 	if (isprefix(*argv, "help") || isprefix(*argv, "?"))
2216 	    goto usage;
2217 	if (strcmp(*argv, "-l") == 0) {
2218 	    --argc; ++argv;
2219 	    if (argc == 0)
2220 		goto usage;
2221 	    user = *argv++;
2222 	    --argc;
2223 	    continue;
2224 	}
2225 	if (strcmp(*argv, "-a") == 0) {
2226 	    --argc; ++argv;
2227 	    autologin = 1;
2228 	    continue;
2229 	}
2230 	if (hostp == 0) {
2231 	    hostp = *argv++;
2232 	    --argc;
2233 	    continue;
2234 	}
2235 	if (portp == 0) {
2236 	    portp = *argv++;
2237 	    --argc;
2238 	    continue;
2239 	}
2240     usage:
2241 	printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2242 	setuid(getuid());
2243 	return 0;
2244     }
2245     if (hostp == 0)
2246 	goto usage;
2247 
2248 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2249     if (hostp[0] == '@' || hostp[0] == '!') {
2250 	if ((hostname = strrchr(hostp, ':')) == NULL)
2251 	    hostname = strrchr(hostp, '@');
2252 	hostname++;
2253 	srp = 0;
2254 	temp = sourceroute(hostp, &srp, &srlen);
2255 	if (temp == 0) {
2256 	    herror(srp);
2257 	    setuid(getuid());
2258 	    return 0;
2259 	} else if (temp == -1) {
2260 	    printf("Bad source route option: %s\n", hostp);
2261 	    setuid(getuid());
2262 	    return 0;
2263 	} else {
2264 	    sin.sin_addr.s_addr = temp;
2265 	    sin.sin_family = AF_INET;
2266 	}
2267     } else {
2268 #endif
2269 	temp = inet_addr(hostp);
2270 	if (temp != (unsigned long) -1) {
2271 	    sin.sin_addr.s_addr = temp;
2272 	    sin.sin_family = AF_INET;
2273 	    (void) strcpy(_hostname, hostp);
2274 	    hostname = _hostname;
2275 	} else {
2276 	    host = gethostbyname(hostp);
2277 	    if (host) {
2278 		sin.sin_family = host->h_addrtype;
2279 #if	defined(h_addr)		/* In 4.3, this is a #define */
2280 		memcpy((caddr_t)&sin.sin_addr,
2281 				host->h_addr_list[0], host->h_length);
2282 #else	/* defined(h_addr) */
2283 		memcpy((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2284 #endif	/* defined(h_addr) */
2285 		strncpy(_hostname, host->h_name, sizeof(_hostname));
2286 		_hostname[sizeof(_hostname)-1] = '\0';
2287 		hostname = _hostname;
2288 	    } else {
2289 		herror(hostp);
2290 	        setuid(getuid());
2291 		return 0;
2292 	    }
2293 	}
2294 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2295     }
2296 #endif
2297     if (portp) {
2298 	if (*portp == '-') {
2299 	    portp++;
2300 	    telnetport = 1;
2301 	} else
2302 	    telnetport = 0;
2303 	sin.sin_port = atoi(portp);
2304 	if (sin.sin_port == 0) {
2305 	    sp = getservbyname(portp, "tcp");
2306 	    if (sp)
2307 		sin.sin_port = sp->s_port;
2308 	    else {
2309 		printf("%s: bad port number\n", portp);
2310 	        setuid(getuid());
2311 		return 0;
2312 	    }
2313 	} else {
2314 #if	!defined(htons)
2315 	    u_short htons();
2316 #endif	/* !defined(htons) */
2317 	    sin.sin_port = htons(sin.sin_port);
2318 	}
2319     } else {
2320 	if (sp == 0) {
2321 	    sp = getservbyname("telnet", "tcp");
2322 	    if (sp == 0) {
2323 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2324 	        setuid(getuid());
2325 		return 0;
2326 	    }
2327 	    sin.sin_port = sp->s_port;
2328 	}
2329 	telnetport = 1;
2330     }
2331     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2332     do {
2333 	net = socket(AF_INET, SOCK_STREAM, 0);
2334 	setuid(getuid());
2335 	if (net < 0) {
2336 	    perror("telnet: socket");
2337 	    return 0;
2338 	}
2339 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2340 	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2341 		perror("setsockopt (IP_OPTIONS)");
2342 #endif
2343 #if	defined(IPPROTO_IP) && defined(IP_TOS)
2344 	{
2345 # if	defined(HAS_GETTOS)
2346 	    struct tosent *tp;
2347 	    if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2348 		tos = tp->t_tos;
2349 # endif
2350 	    if (tos < 0)
2351 		tos = 020;	/* Low Delay bit */
2352 	    if (tos
2353 		&& (setsockopt(net, IPPROTO_IP, IP_TOS,
2354 		    (char *)&tos, sizeof(int)) < 0)
2355 		&& (errno != ENOPROTOOPT))
2356 		    perror("telnet: setsockopt (IP_TOS) (ignored)");
2357 	}
2358 #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
2359 
2360 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2361 		perror("setsockopt (SO_DEBUG)");
2362 	}
2363 
2364 	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2365 #if	defined(h_addr)		/* In 4.3, this is a #define */
2366 	    if (host && host->h_addr_list[1]) {
2367 		int oerrno = errno;
2368 
2369 		fprintf(stderr, "telnet: connect to address %s: ",
2370 						inet_ntoa(sin.sin_addr));
2371 		errno = oerrno;
2372 		perror((char *)0);
2373 		host->h_addr_list++;
2374 		memcpy((caddr_t)&sin.sin_addr,
2375 			host->h_addr_list[0], host->h_length);
2376 		(void) NetClose(net);
2377 		continue;
2378 	    }
2379 #endif	/* defined(h_addr) */
2380 	    perror("telnet: Unable to connect to remote host");
2381 	    return 0;
2382 	}
2383 	connected++;
2384 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
2385 	auth_encrypt_connect(connected);
2386 #endif
2387     } while (connected == 0);
2388     cmdrc(hostp, hostname);
2389     if (autologin && user == NULL) {
2390 	struct passwd *pw;
2391 
2392 	user = getenv("USER");
2393 	if (user == NULL ||
2394 	    (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
2395 		if (pw = getpwuid(getuid()))
2396 			user = pw->pw_name;
2397 		else
2398 			user = NULL;
2399 	}
2400     }
2401     if (user) {
2402 	env_define((unsigned char *)"USER", (unsigned char *)user);
2403 	env_export((unsigned char *)"USER");
2404     }
2405     (void) call(status, "status", "notmuch", 0);
2406     if (setjmp(peerdied) == 0)
2407 	telnet(user);
2408     (void) NetClose(net);
2409     ExitString("Connection closed by foreign host.\n",1);
2410     /*NOTREACHED*/
2411 }
2412 
2413 #define HELPINDENT (sizeof ("connect"))
2414 
2415 static char
2416 	openhelp[] =	"connect to a site",
2417 	closehelp[] =	"close current connection",
2418 	logouthelp[] =	"forcibly logout remote user and close the connection",
2419 	quithelp[] =	"exit telnet",
2420 	statushelp[] =	"print status information",
2421 	helphelp[] =	"print help information",
2422 	sendhelp[] =	"transmit special characters ('send ?' for more)",
2423 	sethelp[] = 	"set operating parameters ('set ?' for more)",
2424 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
2425 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2426 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
2427 	displayhelp[] =	"display operating parameters",
2428 #if	defined(TN3270) && defined(unix)
2429 	transcomhelp[] = "specify Unix command for transparent mode pipe",
2430 #endif	/* defined(TN3270) && defined(unix) */
2431 #if	defined(AUTHENTICATION)
2432 	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",
2433 #endif
2434 #if	defined(ENCRYPTION)
2435 	encrypthelp[] =	"turn on (off) encryption ('encrypt ?' for more)",
2436 #endif
2437 #if	defined(unix)
2438 	zhelp[] =	"suspend telnet",
2439 #endif	/* defined(unix) */
2440 	shellhelp[] =	"invoke a subshell",
2441 	envhelp[] =	"change environment variables ('environ ?' for more)",
2442 	modestring[] = "try to enter line or character mode ('mode ?' for more)";
2443 
2444 static int	help();
2445 
2446 static Command cmdtab[] = {
2447 	{ "close",	closehelp,	bye,		1 },
2448 	{ "logout",	logouthelp,	logout,		1 },
2449 	{ "display",	displayhelp,	display,	0 },
2450 	{ "mode",	modestring,	modecmd,	0 },
2451 	{ "open",	openhelp,	tn,		0 },
2452 	{ "quit",	quithelp,	quit,		0 },
2453 	{ "send",	sendhelp,	sendcmd,	0 },
2454 	{ "set",	sethelp,	setcmd,		0 },
2455 	{ "unset",	unsethelp,	unsetcmd,	0 },
2456 	{ "status",	statushelp,	status,		0 },
2457 	{ "toggle",	togglestring,	toggle,		0 },
2458 	{ "slc",	slchelp,	slccmd,		0 },
2459 #if	defined(TN3270) && defined(unix)
2460 	{ "transcom",	transcomhelp,	settranscom,	0 },
2461 #endif	/* defined(TN3270) && defined(unix) */
2462 #if	defined(AUTHENTICATION)
2463 	{ "auth",	authhelp,	auth_cmd,	0 },
2464 #endif
2465 #if	defined(ENCRYPTION)
2466 	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },
2467 #endif
2468 #if	defined(unix)
2469 	{ "z",		zhelp,		suspend,	0 },
2470 #endif	/* defined(unix) */
2471 #if	defined(TN3270)
2472 	{ "!",		shellhelp,	shell,		1 },
2473 #else
2474 	{ "!",		shellhelp,	shell,		0 },
2475 #endif
2476 	{ "environ",	envhelp,	env_cmd,	0 },
2477 	{ "?",		helphelp,	help,		0 },
2478 	0
2479 };
2480 
2481 static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
2482 static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
2483 
2484 static Command cmdtab2[] = {
2485 	{ "help",	0,		help,		0 },
2486 	{ "escape",	escapehelp,	setescape,	0 },
2487 	{ "crmod",	crmodhelp,	togcrmod,	0 },
2488 	0
2489 };
2490 
2491 
2492 /*
2493  * Call routine with argc, argv set from args (terminated by 0).
2494  */
2495 
2496     /*VARARGS1*/
2497     static
2498 call(va_alist)
2499     va_dcl
2500 {
2501     va_list ap;
2502     typedef int (*intrtn_t)();
2503     intrtn_t routine;
2504     char *args[100];
2505     int argno = 0;
2506 
2507     va_start(ap);
2508     routine = (va_arg(ap, intrtn_t));
2509     while ((args[argno++] = va_arg(ap, char *)) != 0) {
2510 	;
2511     }
2512     va_end(ap);
2513     return (*routine)(argno-1, args);
2514 }
2515 
2516 
2517     static Command *
2518 getcmd(name)
2519     char *name;
2520 {
2521     Command *cm;
2522 
2523     if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
2524 	return cm;
2525     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2526 }
2527 
2528     void
2529 command(top, tbuf, cnt)
2530     int top;
2531     char *tbuf;
2532     int cnt;
2533 {
2534     register Command *c;
2535 
2536     setcommandmode();
2537     if (!top) {
2538 	putchar('\n');
2539 #if	defined(unix)
2540     } else {
2541 	(void) signal(SIGINT, SIG_DFL);
2542 	(void) signal(SIGQUIT, SIG_DFL);
2543 #endif	/* defined(unix) */
2544     }
2545     for (;;) {
2546 	if (rlogin == _POSIX_VDISABLE)
2547 		printf("%s> ", prompt);
2548 	if (tbuf) {
2549 	    register char *cp;
2550 	    cp = line;
2551 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2552 		cnt--;
2553 	    tbuf = 0;
2554 	    if (cp == line || *--cp != '\n' || cp == line)
2555 		goto getline;
2556 	    *cp = '\0';
2557 	    if (rlogin == _POSIX_VDISABLE)
2558 	        printf("%s\n", line);
2559 	} else {
2560 	getline:
2561 	    if (rlogin != _POSIX_VDISABLE)
2562 		printf("%s> ", prompt);
2563 	    if (fgets(line, sizeof(line), stdin) == NULL) {
2564 		if (feof(stdin) || ferror(stdin)) {
2565 		    (void) quit();
2566 		    /*NOTREACHED*/
2567 		}
2568 		break;
2569 	    }
2570 	}
2571 	if (line[0] == 0)
2572 	    break;
2573 	makeargv();
2574 	if (margv[0] == 0) {
2575 	    break;
2576 	}
2577 	c = getcmd(margv[0]);
2578 	if (Ambiguous(c)) {
2579 	    printf("?Ambiguous command\n");
2580 	    continue;
2581 	}
2582 	if (c == 0) {
2583 	    printf("?Invalid command\n");
2584 	    continue;
2585 	}
2586 	if (c->needconnect && !connected) {
2587 	    printf("?Need to be connected first.\n");
2588 	    continue;
2589 	}
2590 	if ((*c->handler)(margc, margv)) {
2591 	    break;
2592 	}
2593     }
2594     if (!top) {
2595 	if (!connected) {
2596 	    longjmp(toplevel, 1);
2597 	    /*NOTREACHED*/
2598 	}
2599 #if	defined(TN3270)
2600 	if (shell_active == 0) {
2601 	    setconnmode(0);
2602 	}
2603 #else	/* defined(TN3270) */
2604 	setconnmode(0);
2605 #endif	/* defined(TN3270) */
2606     }
2607 }
2608 
2609 /*
2610  * Help command.
2611  */
2612 	static
2613 help(argc, argv)
2614 	int argc;
2615 	char *argv[];
2616 {
2617 	register Command *c;
2618 
2619 	if (argc == 1) {
2620 		printf("Commands may be abbreviated.  Commands are:\n\n");
2621 		for (c = cmdtab; c->name; c++)
2622 			if (c->help) {
2623 				printf("%-*s\t%s\n", HELPINDENT, c->name,
2624 								    c->help);
2625 			}
2626 		return 0;
2627 	}
2628 	while (--argc > 0) {
2629 		register char *arg;
2630 		arg = *++argv;
2631 		c = getcmd(arg);
2632 		if (Ambiguous(c))
2633 			printf("?Ambiguous help command %s\n", arg);
2634 		else if (c == (Command *)0)
2635 			printf("?Invalid help command %s\n", arg);
2636 		else
2637 			printf("%s\n", c->help);
2638 	}
2639 	return 0;
2640 }
2641 
2642 static char *rcname = 0;
2643 static char rcbuf[128];
2644 
2645 cmdrc(m1, m2)
2646 	char *m1, *m2;
2647 {
2648     register Command *c;
2649     FILE *rcfile;
2650     int gotmachine = 0;
2651     int l1 = strlen(m1);
2652     int l2 = strlen(m2);
2653     char m1save[64];
2654 
2655     if (skiprc)
2656 	return;
2657 
2658     strcpy(m1save, m1);
2659     m1 = m1save;
2660 
2661     if (rcname == 0) {
2662 	rcname = getenv("HOME");
2663 	if (rcname)
2664 	    strcpy(rcbuf, rcname);
2665 	else
2666 	    rcbuf[0] = '\0';
2667 	strcat(rcbuf, "/.telnetrc");
2668 	rcname = rcbuf;
2669     }
2670 
2671     if ((rcfile = fopen(rcname, "r")) == 0) {
2672 	return;
2673     }
2674 
2675     for (;;) {
2676 	if (fgets(line, sizeof(line), rcfile) == NULL)
2677 	    break;
2678 	if (line[0] == 0)
2679 	    break;
2680 	if (line[0] == '#')
2681 	    continue;
2682 	if (gotmachine) {
2683 	    if (!isspace(line[0]))
2684 		gotmachine = 0;
2685 	}
2686 	if (gotmachine == 0) {
2687 	    if (isspace(line[0]))
2688 		continue;
2689 	    if (strncasecmp(line, m1, l1) == 0)
2690 		strncpy(line, &line[l1], sizeof(line) - l1);
2691 	    else if (strncasecmp(line, m2, l2) == 0)
2692 		strncpy(line, &line[l2], sizeof(line) - l2);
2693 	    else if (strncasecmp(line, "DEFAULT", 7) == 0)
2694 		strncpy(line, &line[7], sizeof(line) - 7);
2695 	    else
2696 		continue;
2697 	    if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2698 		continue;
2699 	    gotmachine = 1;
2700 	}
2701 	makeargv();
2702 	if (margv[0] == 0)
2703 	    continue;
2704 	c = getcmd(margv[0]);
2705 	if (Ambiguous(c)) {
2706 	    printf("?Ambiguous command: %s\n", margv[0]);
2707 	    continue;
2708 	}
2709 	if (c == 0) {
2710 	    printf("?Invalid command: %s\n", margv[0]);
2711 	    continue;
2712 	}
2713 	/*
2714 	 * This should never happen...
2715 	 */
2716 	if (c->needconnect && !connected) {
2717 	    printf("?Need to be connected first for %s.\n", margv[0]);
2718 	    continue;
2719 	}
2720 	(*c->handler)(margc, margv);
2721     }
2722     fclose(rcfile);
2723 }
2724 
2725 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2726 
2727 /*
2728  * Source route is handed in as
2729  *	[!]@hop1@hop2...[@|:]dst
2730  * If the leading ! is present, it is a
2731  * strict source route, otherwise it is
2732  * assmed to be a loose source route.
2733  *
2734  * We fill in the source route option as
2735  *	hop1,hop2,hop3...dest
2736  * and return a pointer to hop1, which will
2737  * be the address to connect() to.
2738  *
2739  * Arguments:
2740  *	arg:	pointer to route list to decipher
2741  *
2742  *	cpp: 	If *cpp is not equal to NULL, this is a
2743  *		pointer to a pointer to a character array
2744  *		that should be filled in with the option.
2745  *
2746  *	lenp:	pointer to an integer that contains the
2747  *		length of *cpp if *cpp != NULL.
2748  *
2749  * Return values:
2750  *
2751  *	Returns the address of the host to connect to.  If the
2752  *	return value is -1, there was a syntax error in the
2753  *	option, either unknown characters, or too many hosts.
2754  *	If the return value is 0, one of the hostnames in the
2755  *	path is unknown, and *cpp is set to point to the bad
2756  *	hostname.
2757  *
2758  *	*cpp:	If *cpp was equal to NULL, it will be filled
2759  *		in with a pointer to our static area that has
2760  *		the option filled in.  This will be 32bit aligned.
2761  *
2762  *	*lenp:	This will be filled in with how long the option
2763  *		pointed to by *cpp is.
2764  *
2765  */
2766 	unsigned long
2767 sourceroute(arg, cpp, lenp)
2768 	char	*arg;
2769 	char	**cpp;
2770 	int	*lenp;
2771 {
2772 	static char lsr[44];
2773 #ifdef	sysV88
2774 	static IOPTN ipopt;
2775 #endif
2776 	char *cp, *cp2, *lsrp, *lsrep;
2777 	register int tmp;
2778 	struct in_addr sin_addr;
2779 	register struct hostent *host = 0;
2780 	register char c;
2781 
2782 	/*
2783 	 * Verify the arguments, and make sure we have
2784 	 * at least 7 bytes for the option.
2785 	 */
2786 	if (cpp == NULL || lenp == NULL)
2787 		return((unsigned long)-1);
2788 	if (*cpp != NULL && *lenp < 7)
2789 		return((unsigned long)-1);
2790 	/*
2791 	 * Decide whether we have a buffer passed to us,
2792 	 * or if we need to use our own static buffer.
2793 	 */
2794 	if (*cpp) {
2795 		lsrp = *cpp;
2796 		lsrep = lsrp + *lenp;
2797 	} else {
2798 		*cpp = lsrp = lsr;
2799 		lsrep = lsrp + 44;
2800 	}
2801 
2802 	cp = arg;
2803 
2804 	/*
2805 	 * Next, decide whether we have a loose source
2806 	 * route or a strict source route, and fill in
2807 	 * the begining of the option.
2808 	 */
2809 #ifndef	sysV88
2810 	if (*cp == '!') {
2811 		cp++;
2812 		*lsrp++ = IPOPT_SSRR;
2813 	} else
2814 		*lsrp++ = IPOPT_LSRR;
2815 #else
2816 	if (*cp == '!') {
2817 		cp++;
2818 		ipopt.io_type = IPOPT_SSRR;
2819 	} else
2820 		ipopt.io_type = IPOPT_LSRR;
2821 #endif
2822 
2823 	if (*cp != '@')
2824 		return((unsigned long)-1);
2825 
2826 #ifndef	sysV88
2827 	lsrp++;		/* skip over length, we'll fill it in later */
2828 	*lsrp++ = 4;
2829 #endif
2830 
2831 	cp++;
2832 
2833 	sin_addr.s_addr = 0;
2834 
2835 	for (c = 0;;) {
2836 		if (c == ':')
2837 			cp2 = 0;
2838 		else for (cp2 = cp; c = *cp2; cp2++) {
2839 			if (c == ',') {
2840 				*cp2++ = '\0';
2841 				if (*cp2 == '@')
2842 					cp2++;
2843 			} else if (c == '@') {
2844 				*cp2++ = '\0';
2845 			} else if (c == ':') {
2846 				*cp2++ = '\0';
2847 			} else
2848 				continue;
2849 			break;
2850 		}
2851 		if (!c)
2852 			cp2 = 0;
2853 
2854 		if ((tmp = inet_addr(cp)) != -1) {
2855 			sin_addr.s_addr = tmp;
2856 		} else if (host = gethostbyname(cp)) {
2857 #if	defined(h_addr)
2858 			memcpy((caddr_t)&sin_addr,
2859 				host->h_addr_list[0], host->h_length);
2860 #else
2861 			memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length);
2862 #endif
2863 		} else {
2864 			*cpp = cp;
2865 			return(0);
2866 		}
2867 		memcpy(lsrp, (char *)&sin_addr, 4);
2868 		lsrp += 4;
2869 		if (cp2)
2870 			cp = cp2;
2871 		else
2872 			break;
2873 		/*
2874 		 * Check to make sure there is space for next address
2875 		 */
2876 		if (lsrp + 4 > lsrep)
2877 			return((unsigned long)-1);
2878 	}
2879 #ifndef	sysV88
2880 	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2881 		*cpp = 0;
2882 		*lenp = 0;
2883 		return((unsigned long)-1);
2884 	}
2885 	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2886 	*lenp = lsrp - *cpp;
2887 #else
2888 	ipopt.io_len = lsrp - *cpp;
2889 	if (ipopt.io_len <= 5) {		/* Is 3 better ? */
2890 		*cpp = 0;
2891 		*lenp = 0;
2892 		return((unsigned long)-1);
2893 	}
2894 	*lenp = sizeof(ipopt);
2895 	*cpp = (char *) &ipopt;
2896 #endif
2897 	return(sin_addr.s_addr);
2898 }
2899 #endif
2900