xref: /original-bsd/usr.bin/telnet/commands.c (revision 24dc12b6)
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)commands.c	8.2 (Berkeley) 12/15/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 #ifdef	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	/* ENCRYPTION */
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 #ifdef	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	/* ENCRYPTION */
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 #ifdef	ENCRYPTION
1274     EncryptStatus();
1275 #endif	/* ENCRYPTION */
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 #else	/* !defined(TN3270) */
1408 extern int shell();
1409 #endif	/* !defined(TN3270) */
1410 
1411     /*VARARGS*/
1412     static
1413 bye(argc, argv)
1414     int  argc;		/* Number of arguments */
1415     char *argv[];	/* arguments */
1416 {
1417     extern int resettermname;
1418 
1419     if (connected) {
1420 	(void) shutdown(net, 2);
1421 	printf("Connection closed.\n");
1422 	(void) NetClose(net);
1423 	connected = 0;
1424 	resettermname = 1;
1425 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
1426 	auth_encrypt_connect(connected);
1427 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
1428 	/* reset options */
1429 	tninit();
1430 #if	defined(TN3270)
1431 	SetIn3270();		/* Get out of 3270 mode */
1432 #endif	/* defined(TN3270) */
1433     }
1434     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1435 	longjmp(toplevel, 1);
1436 	/* NOTREACHED */
1437     }
1438     return 1;			/* Keep lint, etc., happy */
1439 }
1440 
1441 /*VARARGS*/
1442 quit()
1443 {
1444 	(void) call(bye, "bye", "fromquit", 0);
1445 	Exit(0);
1446 	/*NOTREACHED*/
1447 }
1448 
1449 /*VARARGS*/
1450 	int
1451 logout()
1452 {
1453 	send_do(TELOPT_LOGOUT, 1);
1454 	(void) netflush();
1455 	return 1;
1456 }
1457 
1458 
1459 /*
1460  * The SLC command.
1461  */
1462 
1463 struct slclist {
1464 	char	*name;
1465 	char	*help;
1466 	void	(*handler)();
1467 	int	arg;
1468 };
1469 
1470 static void slc_help();
1471 
1472 struct slclist SlcList[] = {
1473     { "export",	"Use local special character definitions",
1474 						slc_mode_export,	0 },
1475     { "import",	"Use remote special character definitions",
1476 						slc_mode_import,	1 },
1477     { "check",	"Verify remote special character definitions",
1478 						slc_mode_import,	0 },
1479     { "help",	0,				slc_help,		0 },
1480     { "?",	"Print help information",	slc_help,		0 },
1481     { 0 },
1482 };
1483 
1484     static void
1485 slc_help()
1486 {
1487     struct slclist *c;
1488 
1489     for (c = SlcList; c->name; c++) {
1490 	if (c->help) {
1491 	    if (*c->help)
1492 		printf("%-15s %s\n", c->name, c->help);
1493 	    else
1494 		printf("\n");
1495 	}
1496     }
1497 }
1498 
1499     static struct slclist *
1500 getslc(name)
1501     char *name;
1502 {
1503     return (struct slclist *)
1504 		genget(name, (char **) SlcList, sizeof(struct slclist));
1505 }
1506 
1507     static
1508 slccmd(argc, argv)
1509     int  argc;
1510     char *argv[];
1511 {
1512     struct slclist *c;
1513 
1514     if (argc != 2) {
1515 	fprintf(stderr,
1516 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
1517 	return 0;
1518     }
1519     c = getslc(argv[1]);
1520     if (c == 0) {
1521         fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1522     				argv[1]);
1523         return 0;
1524     }
1525     if (Ambiguous(c)) {
1526         fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1527     				argv[1]);
1528         return 0;
1529     }
1530     (*c->handler)(c->arg);
1531     slcstate();
1532     return 1;
1533 }
1534 
1535 /*
1536  * The ENVIRON command.
1537  */
1538 
1539 struct envlist {
1540 	char	*name;
1541 	char	*help;
1542 	void	(*handler)();
1543 	int	narg;
1544 };
1545 
1546 extern struct env_lst *
1547 	env_define P((unsigned char *, unsigned char *));
1548 extern void
1549 	env_undefine P((unsigned char *)),
1550 	env_export P((unsigned char *)),
1551 	env_unexport P((unsigned char *)),
1552 	env_send P((unsigned char *)),
1553 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1554 	env_varval P((unsigned char *)),
1555 #endif
1556 	env_list P((void));
1557 static void
1558 	env_help P((void));
1559 
1560 struct envlist EnvList[] = {
1561     { "define",	"Define an environment variable",
1562 						(void (*)())env_define,	2 },
1563     { "undefine", "Undefine an environment variable",
1564 						env_undefine,	1 },
1565     { "export",	"Mark an environment variable for automatic export",
1566 						env_export,	1 },
1567     { "unexport", "Don't mark an environment variable for automatic export",
1568 						env_unexport,	1 },
1569     { "send",	"Send an environment variable", env_send,	1 },
1570     { "list",	"List the current environment variables",
1571 						env_list,	0 },
1572 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1573     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1574 						env_varval,    1 },
1575 #endif
1576     { "help",	0,				env_help,		0 },
1577     { "?",	"Print help information",	env_help,		0 },
1578     { 0 },
1579 };
1580 
1581     static void
1582 env_help()
1583 {
1584     struct envlist *c;
1585 
1586     for (c = EnvList; c->name; c++) {
1587 	if (c->help) {
1588 	    if (*c->help)
1589 		printf("%-15s %s\n", c->name, c->help);
1590 	    else
1591 		printf("\n");
1592 	}
1593     }
1594 }
1595 
1596     static struct envlist *
1597 getenvcmd(name)
1598     char *name;
1599 {
1600     return (struct envlist *)
1601 		genget(name, (char **) EnvList, sizeof(struct envlist));
1602 }
1603 
1604 env_cmd(argc, argv)
1605     int  argc;
1606     char *argv[];
1607 {
1608     struct envlist *c;
1609 
1610     if (argc < 2) {
1611 	fprintf(stderr,
1612 	    "Need an argument to 'environ' command.  'environ ?' for help.\n");
1613 	return 0;
1614     }
1615     c = getenvcmd(argv[1]);
1616     if (c == 0) {
1617         fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1618     				argv[1]);
1619         return 0;
1620     }
1621     if (Ambiguous(c)) {
1622         fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1623     				argv[1]);
1624         return 0;
1625     }
1626     if (c->narg + 2 != argc) {
1627 	fprintf(stderr,
1628 	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
1629 		c->narg < argc + 2 ? "only " : "",
1630 		c->narg, c->narg == 1 ? "" : "s", c->name);
1631 	return 0;
1632     }
1633     (*c->handler)(argv[2], argv[3]);
1634     return 1;
1635 }
1636 
1637 struct env_lst {
1638 	struct env_lst *next;	/* pointer to next structure */
1639 	struct env_lst *prev;	/* pointer to previous structure */
1640 	unsigned char *var;	/* pointer to variable name */
1641 	unsigned char *value;	/* pointer to variable value */
1642 	int export;		/* 1 -> export with default list of variables */
1643 	int welldefined;	/* A well defined variable */
1644 };
1645 
1646 struct env_lst envlisthead;
1647 
1648 	struct env_lst *
1649 env_find(var)
1650 	unsigned char *var;
1651 {
1652 	register struct env_lst *ep;
1653 
1654 	for (ep = envlisthead.next; ep; ep = ep->next) {
1655 		if (strcmp((char *)ep->var, (char *)var) == 0)
1656 			return(ep);
1657 	}
1658 	return(NULL);
1659 }
1660 
1661 	void
1662 env_init()
1663 {
1664 	extern char **environ;
1665 	register char **epp, *cp;
1666 	register struct env_lst *ep;
1667 	extern char *index();
1668 
1669 	for (epp = environ; *epp; epp++) {
1670 		if (cp = index(*epp, '=')) {
1671 			*cp = '\0';
1672 			ep = env_define((unsigned char *)*epp,
1673 					(unsigned char *)cp+1);
1674 			ep->export = 0;
1675 			*cp = '=';
1676 		}
1677 	}
1678 	/*
1679 	 * Special case for DISPLAY variable.  If it is ":0.0" or
1680 	 * "unix:0.0", we have to get rid of "unix" and insert our
1681 	 * hostname.
1682 	 */
1683 	if ((ep = env_find("DISPLAY"))
1684 	    && ((*ep->value == ':')
1685 	        || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1686 		char hbuf[256+1];
1687 		char *cp2 = index((char *)ep->value, ':');
1688 
1689 		gethostname(hbuf, 256);
1690 		hbuf[256] = '\0';
1691 		cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1692 		sprintf((char *)cp, "%s%s", hbuf, cp2);
1693 		free(ep->value);
1694 		ep->value = (unsigned char *)cp;
1695 	}
1696 	/*
1697 	 * If USER is not defined, but LOGNAME is, then add
1698 	 * USER with the value from LOGNAME.  By default, we
1699 	 * don't export the USER variable.
1700 	 */
1701 	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1702 		env_define((unsigned char *)"USER", ep->value);
1703 		env_unexport((unsigned char *)"USER");
1704 	}
1705 	env_export((unsigned char *)"DISPLAY");
1706 	env_export((unsigned char *)"PRINTER");
1707 }
1708 
1709 	struct env_lst *
1710 env_define(var, value)
1711 	unsigned char *var, *value;
1712 {
1713 	register struct env_lst *ep;
1714 
1715 	if (ep = env_find(var)) {
1716 		if (ep->var)
1717 			free(ep->var);
1718 		if (ep->value)
1719 			free(ep->value);
1720 	} else {
1721 		ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1722 		ep->next = envlisthead.next;
1723 		envlisthead.next = ep;
1724 		ep->prev = &envlisthead;
1725 		if (ep->next)
1726 			ep->next->prev = ep;
1727 	}
1728 	ep->welldefined = opt_welldefined(var);
1729 	ep->export = 1;
1730 	ep->var = (unsigned char *)strdup((char *)var);
1731 	ep->value = (unsigned char *)strdup((char *)value);
1732 	return(ep);
1733 }
1734 
1735 	void
1736 env_undefine(var)
1737 	unsigned char *var;
1738 {
1739 	register struct env_lst *ep;
1740 
1741 	if (ep = env_find(var)) {
1742 		ep->prev->next = ep->next;
1743 		if (ep->next)
1744 			ep->next->prev = ep->prev;
1745 		if (ep->var)
1746 			free(ep->var);
1747 		if (ep->value)
1748 			free(ep->value);
1749 		free(ep);
1750 	}
1751 }
1752 
1753 	void
1754 env_export(var)
1755 	unsigned char *var;
1756 {
1757 	register struct env_lst *ep;
1758 
1759 	if (ep = env_find(var))
1760 		ep->export = 1;
1761 }
1762 
1763 	void
1764 env_unexport(var)
1765 	unsigned char *var;
1766 {
1767 	register struct env_lst *ep;
1768 
1769 	if (ep = env_find(var))
1770 		ep->export = 0;
1771 }
1772 
1773 	void
1774 env_send(var)
1775 	unsigned char *var;
1776 {
1777 	register struct env_lst *ep;
1778 
1779         if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1780 #ifdef	OLD_ENVIRON
1781 	    && my_state_is_wont(TELOPT_OLD_ENVIRON)
1782 #endif
1783 		) {
1784 		fprintf(stderr,
1785 		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1786 									var);
1787 		return;
1788 	}
1789 	ep = env_find(var);
1790 	if (ep == 0) {
1791 		fprintf(stderr, "Cannot send '%s': variable not defined\n",
1792 									var);
1793 		return;
1794 	}
1795 	env_opt_start_info();
1796 	env_opt_add(ep->var);
1797 	env_opt_end(0);
1798 }
1799 
1800 	void
1801 env_list()
1802 {
1803 	register struct env_lst *ep;
1804 
1805 	for (ep = envlisthead.next; ep; ep = ep->next) {
1806 		printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1807 					ep->var, ep->value);
1808 	}
1809 }
1810 
1811 	unsigned char *
1812 env_default(init, welldefined)
1813 	int init;
1814 {
1815 	static struct env_lst *nep = NULL;
1816 
1817 	if (init) {
1818 		nep = &envlisthead;
1819 		return;
1820 	}
1821 	if (nep) {
1822 		while (nep = nep->next) {
1823 			if (nep->export && (nep->welldefined == welldefined))
1824 				return(nep->var);
1825 		}
1826 	}
1827 	return(NULL);
1828 }
1829 
1830 	unsigned char *
1831 env_getvalue(var)
1832 	unsigned char *var;
1833 {
1834 	register struct env_lst *ep;
1835 
1836 	if (ep = env_find(var))
1837 		return(ep->value);
1838 	return(NULL);
1839 }
1840 
1841 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1842 	void
1843 env_varval(what)
1844 	unsigned char *what;
1845 {
1846 	extern int old_env_var, old_env_value, env_auto;
1847 	int len = strlen((char *)what);
1848 
1849 	if (len == 0)
1850 		goto unknown;
1851 
1852 	if (strncasecmp((char *)what, "status", len) == 0) {
1853 		if (env_auto)
1854 			printf("%s%s", "VAR and VALUE are/will be ",
1855 					"determined automatically\n");
1856 		if (old_env_var == OLD_ENV_VAR)
1857 			printf("VAR and VALUE set to correct definitions\n");
1858 		else
1859 			printf("VAR and VALUE definitions are reversed\n");
1860 	} else if (strncasecmp((char *)what, "auto", len) == 0) {
1861 		env_auto = 1;
1862 		old_env_var = OLD_ENV_VALUE;
1863 		old_env_value = OLD_ENV_VAR;
1864 	} else if (strncasecmp((char *)what, "right", len) == 0) {
1865 		env_auto = 0;
1866 		old_env_var = OLD_ENV_VAR;
1867 		old_env_value = OLD_ENV_VALUE;
1868 	} else if (strncasecmp((char *)what, "wrong", len) == 0) {
1869 		env_auto = 0;
1870 		old_env_var = OLD_ENV_VALUE;
1871 		old_env_value = OLD_ENV_VAR;
1872 	} else {
1873 unknown:
1874 		printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1875 	}
1876 }
1877 #endif
1878 
1879 #if	defined(AUTHENTICATION)
1880 /*
1881  * The AUTHENTICATE command.
1882  */
1883 
1884 struct authlist {
1885 	char	*name;
1886 	char	*help;
1887 	int	(*handler)();
1888 	int	narg;
1889 };
1890 
1891 extern int
1892 	auth_enable P((int)),
1893 	auth_disable P((int)),
1894 	auth_status P((void));
1895 static int
1896 	auth_help P((void));
1897 
1898 struct authlist AuthList[] = {
1899     { "status",	"Display current status of authentication information",
1900 						auth_status,	0 },
1901     { "disable", "Disable an authentication type ('auth disable ?' for more)",
1902 						auth_disable,	1 },
1903     { "enable", "Enable an authentication type ('auth enable ?' for more)",
1904 						auth_enable,	1 },
1905     { "help",	0,				auth_help,		0 },
1906     { "?",	"Print help information",	auth_help,		0 },
1907     { 0 },
1908 };
1909 
1910     static int
1911 auth_help()
1912 {
1913     struct authlist *c;
1914 
1915     for (c = AuthList; c->name; c++) {
1916 	if (c->help) {
1917 	    if (*c->help)
1918 		printf("%-15s %s\n", c->name, c->help);
1919 	    else
1920 		printf("\n");
1921 	}
1922     }
1923     return 0;
1924 }
1925 
1926 auth_cmd(argc, argv)
1927     int  argc;
1928     char *argv[];
1929 {
1930     struct authlist *c;
1931 
1932     c = (struct authlist *)
1933 		genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1934     if (c == 0) {
1935         fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1936     				argv[1]);
1937         return 0;
1938     }
1939     if (Ambiguous(c)) {
1940         fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1941     				argv[1]);
1942         return 0;
1943     }
1944     if (c->narg + 2 != argc) {
1945 	fprintf(stderr,
1946 	    "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
1947 		c->narg < argc + 2 ? "only " : "",
1948 		c->narg, c->narg == 1 ? "" : "s", c->name);
1949 	return 0;
1950     }
1951     return((*c->handler)(argv[2], argv[3]));
1952 }
1953 #endif
1954 
1955 #ifdef	ENCRYPTION
1956 /*
1957  * The ENCRYPT command.
1958  */
1959 
1960 struct encryptlist {
1961 	char	*name;
1962 	char	*help;
1963 	int	(*handler)();
1964 	int	needconnect;
1965 	int	minarg;
1966 	int	maxarg;
1967 };
1968 
1969 extern int
1970 	EncryptEnable P((char *, char *)),
1971 	EncryptDisable P((char *, char *)),
1972 	EncryptType P((char *, char *)),
1973 	EncryptStart P((char *)),
1974 	EncryptStartInput P((void)),
1975 	EncryptStartOutput P((void)),
1976 	EncryptStop P((char *)),
1977 	EncryptStopInput P((void)),
1978 	EncryptStopOutput P((void)),
1979 	EncryptStatus P((void));
1980 static int
1981 	EncryptHelp P((void));
1982 
1983 struct encryptlist EncryptList[] = {
1984     { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1985 						EncryptEnable, 1, 1, 2 },
1986     { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1987 						EncryptDisable, 0, 1, 2 },
1988     { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1989 						EncryptType, 0, 1, 1 },
1990     { "start", "Start encryption. ('encrypt start ?' for more)",
1991 						EncryptStart, 1, 0, 1 },
1992     { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1993 						EncryptStop, 1, 0, 1 },
1994     { "input", "Start encrypting the input stream",
1995 						EncryptStartInput, 1, 0, 0 },
1996     { "-input", "Stop encrypting the input stream",
1997 						EncryptStopInput, 1, 0, 0 },
1998     { "output", "Start encrypting the output stream",
1999 						EncryptStartOutput, 1, 0, 0 },
2000     { "-output", "Stop encrypting the output stream",
2001 						EncryptStopOutput, 1, 0, 0 },
2002 
2003     { "status",	"Display current status of authentication information",
2004 						EncryptStatus,	0, 0, 0 },
2005     { "help",	0,				EncryptHelp,	0, 0, 0 },
2006     { "?",	"Print help information",	EncryptHelp,	0, 0, 0 },
2007     { 0 },
2008 };
2009 
2010     static int
2011 EncryptHelp()
2012 {
2013     struct encryptlist *c;
2014 
2015     for (c = EncryptList; c->name; c++) {
2016 	if (c->help) {
2017 	    if (*c->help)
2018 		printf("%-15s %s\n", c->name, c->help);
2019 	    else
2020 		printf("\n");
2021 	}
2022     }
2023     return 0;
2024 }
2025 
2026 encrypt_cmd(argc, argv)
2027     int  argc;
2028     char *argv[];
2029 {
2030     struct encryptlist *c;
2031 
2032     c = (struct encryptlist *)
2033 		genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2034     if (c == 0) {
2035         fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2036     				argv[1]);
2037         return 0;
2038     }
2039     if (Ambiguous(c)) {
2040         fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2041     				argv[1]);
2042         return 0;
2043     }
2044     argc -= 2;
2045     if (argc < c->minarg || argc > c->maxarg) {
2046 	if (c->minarg == c->maxarg) {
2047 	    fprintf(stderr, "Need %s%d argument%s ",
2048 		c->minarg < argc ? "only " : "", c->minarg,
2049 		c->minarg == 1 ? "" : "s");
2050 	} else {
2051 	    fprintf(stderr, "Need %s%d-%d arguments ",
2052 		c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2053 	}
2054 	fprintf(stderr, "to 'encrypt %s' command.  'encrypt ?' for help.\n",
2055 		c->name);
2056 	return 0;
2057     }
2058     if (c->needconnect && !connected) {
2059 	if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2060 	    printf("?Need to be connected first.\n");
2061 	    return 0;
2062 	}
2063     }
2064     return ((*c->handler)(argc > 0 ? argv[2] : 0,
2065 			argc > 1 ? argv[3] : 0,
2066 			argc > 2 ? argv[4] : 0));
2067 }
2068 #endif	/* ENCRYPTION */
2069 
2070 #if	defined(unix) && defined(TN3270)
2071     static void
2072 filestuff(fd)
2073     int fd;
2074 {
2075     int res;
2076 
2077 #ifdef	F_GETOWN
2078     setconnmode(0);
2079     res = fcntl(fd, F_GETOWN, 0);
2080     setcommandmode();
2081 
2082     if (res == -1) {
2083 	perror("fcntl");
2084 	return;
2085     }
2086     printf("\tOwner is %d.\n", res);
2087 #endif
2088 
2089     setconnmode(0);
2090     res = fcntl(fd, F_GETFL, 0);
2091     setcommandmode();
2092 
2093     if (res == -1) {
2094 	perror("fcntl");
2095 	return;
2096     }
2097 #ifdef notdef
2098     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2099 #endif
2100 }
2101 #endif /* defined(unix) && defined(TN3270) */
2102 
2103 /*
2104  * Print status about the connection.
2105  */
2106     /*ARGSUSED*/
2107     static
2108 status(argc, argv)
2109     int	 argc;
2110     char *argv[];
2111 {
2112     if (connected) {
2113 	printf("Connected to %s.\n", hostname);
2114 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2115 	    int mode = getconnmode();
2116 
2117 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
2118 		printf("Operating with LINEMODE option\n");
2119 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2120 		printf("%s catching of signals\n",
2121 					(mode&MODE_TRAPSIG) ? "Local" : "No");
2122 		slcstate();
2123 #ifdef	KLUDGELINEMODE
2124 	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2125 		printf("Operating in obsolete linemode\n");
2126 #endif
2127 	    } else {
2128 		printf("Operating in single character mode\n");
2129 		if (localchars)
2130 		    printf("Catching signals locally\n");
2131 	    }
2132 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2133 	    if (my_want_state_is_will(TELOPT_LFLOW))
2134 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2135 #ifdef	ENCRYPTION
2136 	    encrypt_display();
2137 #endif	/* ENCRYPTION */
2138 	}
2139     } else {
2140 	printf("No connection.\n");
2141     }
2142 #   if !defined(TN3270)
2143     printf("Escape character is '%s'.\n", control(escape));
2144     (void) fflush(stdout);
2145 #   else /* !defined(TN3270) */
2146     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2147 	printf("Escape character is '%s'.\n", control(escape));
2148     }
2149 #   if defined(unix)
2150     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2151 	printf("SIGIO received %d time%s.\n",
2152 				sigiocount, (sigiocount == 1)? "":"s");
2153 	if (In3270) {
2154 	    printf("Process ID %d, process group %d.\n",
2155 					    getpid(), getpgrp(getpid()));
2156 	    printf("Terminal input:\n");
2157 	    filestuff(tin);
2158 	    printf("Terminal output:\n");
2159 	    filestuff(tout);
2160 	    printf("Network socket:\n");
2161 	    filestuff(net);
2162 	}
2163     }
2164     if (In3270 && transcom) {
2165        printf("Transparent mode command is '%s'.\n", transcom);
2166     }
2167 #   endif /* defined(unix) */
2168     (void) fflush(stdout);
2169     if (In3270) {
2170 	return 0;
2171     }
2172 #   endif /* defined(TN3270) */
2173     return 1;
2174 }
2175 
2176 #ifdef	SIGINFO
2177 /*
2178  * Function that gets called when SIGINFO is received.
2179  */
2180 ayt_status()
2181 {
2182     (void) call(status, "status", "notmuch", 0);
2183 }
2184 #endif
2185 
2186 unsigned long inet_addr();
2187 
2188     int
2189 tn(argc, argv)
2190     int argc;
2191     char *argv[];
2192 {
2193     register struct hostent *host = 0;
2194     struct sockaddr_in sin;
2195     struct servent *sp = 0;
2196     unsigned long temp;
2197     extern char *inet_ntoa();
2198 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2199     char *srp = 0, *strrchr();
2200     unsigned long sourceroute(), srlen;
2201 #endif
2202     char *cmd, *hostp = 0, *portp = 0, *user = 0;
2203 
2204     /* clear the socket address prior to use */
2205     bzero((char *)&sin, sizeof(sin));
2206 
2207     if (connected) {
2208 	printf("?Already connected to %s\n", hostname);
2209 	setuid(getuid());
2210 	return 0;
2211     }
2212     if (argc < 2) {
2213 	(void) strcpy(line, "open ");
2214 	printf("(to) ");
2215 	(void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2216 	makeargv();
2217 	argc = margc;
2218 	argv = margv;
2219     }
2220     cmd = *argv;
2221     --argc; ++argv;
2222     while (argc) {
2223 	if (isprefix(*argv, "help") || isprefix(*argv, "?"))
2224 	    goto usage;
2225 	if (strcmp(*argv, "-l") == 0) {
2226 	    --argc; ++argv;
2227 	    if (argc == 0)
2228 		goto usage;
2229 	    user = *argv++;
2230 	    --argc;
2231 	    continue;
2232 	}
2233 	if (strcmp(*argv, "-a") == 0) {
2234 	    --argc; ++argv;
2235 	    autologin = 1;
2236 	    continue;
2237 	}
2238 	if (hostp == 0) {
2239 	    hostp = *argv++;
2240 	    --argc;
2241 	    continue;
2242 	}
2243 	if (portp == 0) {
2244 	    portp = *argv++;
2245 	    --argc;
2246 	    continue;
2247 	}
2248     usage:
2249 	printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2250 	setuid(getuid());
2251 	return 0;
2252     }
2253     if (hostp == 0)
2254 	goto usage;
2255 
2256 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2257     if (hostp[0] == '@' || hostp[0] == '!') {
2258 	if ((hostname = strrchr(hostp, ':')) == NULL)
2259 	    hostname = strrchr(hostp, '@');
2260 	hostname++;
2261 	srp = 0;
2262 	temp = sourceroute(hostp, &srp, &srlen);
2263 	if (temp == 0) {
2264 	    herror(srp);
2265 	    setuid(getuid());
2266 	    return 0;
2267 	} else if (temp == -1) {
2268 	    printf("Bad source route option: %s\n", hostp);
2269 	    setuid(getuid());
2270 	    return 0;
2271 	} else {
2272 	    sin.sin_addr.s_addr = temp;
2273 	    sin.sin_family = AF_INET;
2274 	}
2275     } else {
2276 #endif
2277 	temp = inet_addr(hostp);
2278 	if (temp != (unsigned long) -1) {
2279 	    sin.sin_addr.s_addr = temp;
2280 	    sin.sin_family = AF_INET;
2281 	    (void) strcpy(_hostname, hostp);
2282 	    hostname = _hostname;
2283 	} else {
2284 	    host = gethostbyname(hostp);
2285 	    if (host) {
2286 		sin.sin_family = host->h_addrtype;
2287 #if	defined(h_addr)		/* In 4.3, this is a #define */
2288 		memcpy((caddr_t)&sin.sin_addr,
2289 				host->h_addr_list[0], host->h_length);
2290 #else	/* defined(h_addr) */
2291 		memcpy((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2292 #endif	/* defined(h_addr) */
2293 		strncpy(_hostname, host->h_name, sizeof(_hostname));
2294 		_hostname[sizeof(_hostname)-1] = '\0';
2295 		hostname = _hostname;
2296 	    } else {
2297 		herror(hostp);
2298 	        setuid(getuid());
2299 		return 0;
2300 	    }
2301 	}
2302 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2303     }
2304 #endif
2305     if (portp) {
2306 	if (*portp == '-') {
2307 	    portp++;
2308 	    telnetport = 1;
2309 	} else
2310 	    telnetport = 0;
2311 	sin.sin_port = atoi(portp);
2312 	if (sin.sin_port == 0) {
2313 	    sp = getservbyname(portp, "tcp");
2314 	    if (sp)
2315 		sin.sin_port = sp->s_port;
2316 	    else {
2317 		printf("%s: bad port number\n", portp);
2318 	        setuid(getuid());
2319 		return 0;
2320 	    }
2321 	} else {
2322 #if	!defined(htons)
2323 	    u_short htons P((unsigned short));
2324 #endif	/* !defined(htons) */
2325 	    sin.sin_port = htons(sin.sin_port);
2326 	}
2327     } else {
2328 	if (sp == 0) {
2329 	    sp = getservbyname("telnet", "tcp");
2330 	    if (sp == 0) {
2331 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2332 	        setuid(getuid());
2333 		return 0;
2334 	    }
2335 	    sin.sin_port = sp->s_port;
2336 	}
2337 	telnetport = 1;
2338     }
2339     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2340     do {
2341 	net = socket(AF_INET, SOCK_STREAM, 0);
2342 	setuid(getuid());
2343 	if (net < 0) {
2344 	    perror("telnet: socket");
2345 	    return 0;
2346 	}
2347 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2348 	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2349 		perror("setsockopt (IP_OPTIONS)");
2350 #endif
2351 #if	defined(IPPROTO_IP) && defined(IP_TOS)
2352 	{
2353 # if	defined(HAS_GETTOS)
2354 	    struct tosent *tp;
2355 	    if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2356 		tos = tp->t_tos;
2357 # endif
2358 	    if (tos < 0)
2359 		tos = 020;	/* Low Delay bit */
2360 	    if (tos
2361 		&& (setsockopt(net, IPPROTO_IP, IP_TOS,
2362 		    (char *)&tos, sizeof(int)) < 0)
2363 		&& (errno != ENOPROTOOPT))
2364 		    perror("telnet: setsockopt (IP_TOS) (ignored)");
2365 	}
2366 #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
2367 
2368 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2369 		perror("setsockopt (SO_DEBUG)");
2370 	}
2371 
2372 	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2373 #if	defined(h_addr)		/* In 4.3, this is a #define */
2374 	    if (host && host->h_addr_list[1]) {
2375 		int oerrno = errno;
2376 
2377 		fprintf(stderr, "telnet: connect to address %s: ",
2378 						inet_ntoa(sin.sin_addr));
2379 		errno = oerrno;
2380 		perror((char *)0);
2381 		host->h_addr_list++;
2382 		memcpy((caddr_t)&sin.sin_addr,
2383 			host->h_addr_list[0], host->h_length);
2384 		(void) NetClose(net);
2385 		continue;
2386 	    }
2387 #endif	/* defined(h_addr) */
2388 	    perror("telnet: Unable to connect to remote host");
2389 	    return 0;
2390 	}
2391 	connected++;
2392 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
2393 	auth_encrypt_connect(connected);
2394 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2395     } while (connected == 0);
2396     cmdrc(hostp, hostname);
2397     if (autologin && user == NULL) {
2398 	struct passwd *pw;
2399 
2400 	user = getenv("USER");
2401 	if (user == NULL ||
2402 	    (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
2403 		if (pw = getpwuid(getuid()))
2404 			user = pw->pw_name;
2405 		else
2406 			user = NULL;
2407 	}
2408     }
2409     if (user) {
2410 	env_define((unsigned char *)"USER", (unsigned char *)user);
2411 	env_export((unsigned char *)"USER");
2412     }
2413     (void) call(status, "status", "notmuch", 0);
2414     if (setjmp(peerdied) == 0)
2415 	telnet(user);
2416     (void) NetClose(net);
2417     ExitString("Connection closed by foreign host.\n",1);
2418     /*NOTREACHED*/
2419 }
2420 
2421 #define HELPINDENT (sizeof ("connect"))
2422 
2423 static char
2424 	openhelp[] =	"connect to a site",
2425 	closehelp[] =	"close current connection",
2426 	logouthelp[] =	"forcibly logout remote user and close the connection",
2427 	quithelp[] =	"exit telnet",
2428 	statushelp[] =	"print status information",
2429 	helphelp[] =	"print help information",
2430 	sendhelp[] =	"transmit special characters ('send ?' for more)",
2431 	sethelp[] = 	"set operating parameters ('set ?' for more)",
2432 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
2433 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2434 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
2435 	displayhelp[] =	"display operating parameters",
2436 #if	defined(TN3270) && defined(unix)
2437 	transcomhelp[] = "specify Unix command for transparent mode pipe",
2438 #endif	/* defined(TN3270) && defined(unix) */
2439 #if	defined(AUTHENTICATION)
2440 	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",
2441 #endif
2442 #ifdef	ENCRYPTION
2443 	encrypthelp[] =	"turn on (off) encryption ('encrypt ?' for more)",
2444 #endif	/* ENCRYPTION */
2445 #if	defined(unix)
2446 	zhelp[] =	"suspend telnet",
2447 #endif	/* defined(unix) */
2448 	shellhelp[] =	"invoke a subshell",
2449 	envhelp[] =	"change environment variables ('environ ?' for more)",
2450 	modestring[] = "try to enter line or character mode ('mode ?' for more)";
2451 
2452 static int	help();
2453 
2454 static Command cmdtab[] = {
2455 	{ "close",	closehelp,	bye,		1 },
2456 	{ "logout",	logouthelp,	logout,		1 },
2457 	{ "display",	displayhelp,	display,	0 },
2458 	{ "mode",	modestring,	modecmd,	0 },
2459 	{ "open",	openhelp,	tn,		0 },
2460 	{ "quit",	quithelp,	quit,		0 },
2461 	{ "send",	sendhelp,	sendcmd,	0 },
2462 	{ "set",	sethelp,	setcmd,		0 },
2463 	{ "unset",	unsethelp,	unsetcmd,	0 },
2464 	{ "status",	statushelp,	status,		0 },
2465 	{ "toggle",	togglestring,	toggle,		0 },
2466 	{ "slc",	slchelp,	slccmd,		0 },
2467 #if	defined(TN3270) && defined(unix)
2468 	{ "transcom",	transcomhelp,	settranscom,	0 },
2469 #endif	/* defined(TN3270) && defined(unix) */
2470 #if	defined(AUTHENTICATION)
2471 	{ "auth",	authhelp,	auth_cmd,	0 },
2472 #endif
2473 #ifdef	ENCRYPTION
2474 	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },
2475 #endif	/* ENCRYPTION */
2476 #if	defined(unix)
2477 	{ "z",		zhelp,		suspend,	0 },
2478 #endif	/* defined(unix) */
2479 #if	defined(TN3270)
2480 	{ "!",		shellhelp,	shell,		1 },
2481 #else
2482 	{ "!",		shellhelp,	shell,		0 },
2483 #endif
2484 	{ "environ",	envhelp,	env_cmd,	0 },
2485 	{ "?",		helphelp,	help,		0 },
2486 	0
2487 };
2488 
2489 static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
2490 static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
2491 
2492 static Command cmdtab2[] = {
2493 	{ "help",	0,		help,		0 },
2494 	{ "escape",	escapehelp,	setescape,	0 },
2495 	{ "crmod",	crmodhelp,	togcrmod,	0 },
2496 	0
2497 };
2498 
2499 
2500 /*
2501  * Call routine with argc, argv set from args (terminated by 0).
2502  */
2503 
2504     /*VARARGS1*/
2505     static
2506 call(va_alist)
2507     va_dcl
2508 {
2509     va_list ap;
2510     typedef int (*intrtn_t)();
2511     intrtn_t routine;
2512     char *args[100];
2513     int argno = 0;
2514 
2515     va_start(ap);
2516     routine = (va_arg(ap, intrtn_t));
2517     while ((args[argno++] = va_arg(ap, char *)) != 0) {
2518 	;
2519     }
2520     va_end(ap);
2521     return (*routine)(argno-1, args);
2522 }
2523 
2524 
2525     static Command *
2526 getcmd(name)
2527     char *name;
2528 {
2529     Command *cm;
2530 
2531     if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
2532 	return cm;
2533     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2534 }
2535 
2536     void
2537 command(top, tbuf, cnt)
2538     int top;
2539     char *tbuf;
2540     int cnt;
2541 {
2542     register Command *c;
2543 
2544     setcommandmode();
2545     if (!top) {
2546 	putchar('\n');
2547 #if	defined(unix)
2548     } else {
2549 	(void) signal(SIGINT, SIG_DFL);
2550 	(void) signal(SIGQUIT, SIG_DFL);
2551 #endif	/* defined(unix) */
2552     }
2553     for (;;) {
2554 	if (rlogin == _POSIX_VDISABLE)
2555 		printf("%s> ", prompt);
2556 	if (tbuf) {
2557 	    register char *cp;
2558 	    cp = line;
2559 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2560 		cnt--;
2561 	    tbuf = 0;
2562 	    if (cp == line || *--cp != '\n' || cp == line)
2563 		goto getline;
2564 	    *cp = '\0';
2565 	    if (rlogin == _POSIX_VDISABLE)
2566 	        printf("%s\n", line);
2567 	} else {
2568 	getline:
2569 	    if (rlogin != _POSIX_VDISABLE)
2570 		printf("%s> ", prompt);
2571 	    if (fgets(line, sizeof(line), stdin) == NULL) {
2572 		if (feof(stdin) || ferror(stdin)) {
2573 		    (void) quit();
2574 		    /*NOTREACHED*/
2575 		}
2576 		break;
2577 	    }
2578 	}
2579 	if (line[0] == 0)
2580 	    break;
2581 	makeargv();
2582 	if (margv[0] == 0) {
2583 	    break;
2584 	}
2585 	c = getcmd(margv[0]);
2586 	if (Ambiguous(c)) {
2587 	    printf("?Ambiguous command\n");
2588 	    continue;
2589 	}
2590 	if (c == 0) {
2591 	    printf("?Invalid command\n");
2592 	    continue;
2593 	}
2594 	if (c->needconnect && !connected) {
2595 	    printf("?Need to be connected first.\n");
2596 	    continue;
2597 	}
2598 	if ((*c->handler)(margc, margv)) {
2599 	    break;
2600 	}
2601     }
2602     if (!top) {
2603 	if (!connected) {
2604 	    longjmp(toplevel, 1);
2605 	    /*NOTREACHED*/
2606 	}
2607 #if	defined(TN3270)
2608 	if (shell_active == 0) {
2609 	    setconnmode(0);
2610 	}
2611 #else	/* defined(TN3270) */
2612 	setconnmode(0);
2613 #endif	/* defined(TN3270) */
2614     }
2615 }
2616 
2617 /*
2618  * Help command.
2619  */
2620 	static
2621 help(argc, argv)
2622 	int argc;
2623 	char *argv[];
2624 {
2625 	register Command *c;
2626 
2627 	if (argc == 1) {
2628 		printf("Commands may be abbreviated.  Commands are:\n\n");
2629 		for (c = cmdtab; c->name; c++)
2630 			if (c->help) {
2631 				printf("%-*s\t%s\n", HELPINDENT, c->name,
2632 								    c->help);
2633 			}
2634 		return 0;
2635 	}
2636 	while (--argc > 0) {
2637 		register char *arg;
2638 		arg = *++argv;
2639 		c = getcmd(arg);
2640 		if (Ambiguous(c))
2641 			printf("?Ambiguous help command %s\n", arg);
2642 		else if (c == (Command *)0)
2643 			printf("?Invalid help command %s\n", arg);
2644 		else
2645 			printf("%s\n", c->help);
2646 	}
2647 	return 0;
2648 }
2649 
2650 static char *rcname = 0;
2651 static char rcbuf[128];
2652 
2653 cmdrc(m1, m2)
2654 	char *m1, *m2;
2655 {
2656     register Command *c;
2657     FILE *rcfile;
2658     int gotmachine = 0;
2659     int l1 = strlen(m1);
2660     int l2 = strlen(m2);
2661     char m1save[64];
2662 
2663     if (skiprc)
2664 	return;
2665 
2666     strcpy(m1save, m1);
2667     m1 = m1save;
2668 
2669     if (rcname == 0) {
2670 	rcname = getenv("HOME");
2671 	if (rcname)
2672 	    strcpy(rcbuf, rcname);
2673 	else
2674 	    rcbuf[0] = '\0';
2675 	strcat(rcbuf, "/.telnetrc");
2676 	rcname = rcbuf;
2677     }
2678 
2679     if ((rcfile = fopen(rcname, "r")) == 0) {
2680 	return;
2681     }
2682 
2683     for (;;) {
2684 	if (fgets(line, sizeof(line), rcfile) == NULL)
2685 	    break;
2686 	if (line[0] == 0)
2687 	    break;
2688 	if (line[0] == '#')
2689 	    continue;
2690 	if (gotmachine) {
2691 	    if (!isspace(line[0]))
2692 		gotmachine = 0;
2693 	}
2694 	if (gotmachine == 0) {
2695 	    if (isspace(line[0]))
2696 		continue;
2697 	    if (strncasecmp(line, m1, l1) == 0)
2698 		strncpy(line, &line[l1], sizeof(line) - l1);
2699 	    else if (strncasecmp(line, m2, l2) == 0)
2700 		strncpy(line, &line[l2], sizeof(line) - l2);
2701 	    else if (strncasecmp(line, "DEFAULT", 7) == 0)
2702 		strncpy(line, &line[7], sizeof(line) - 7);
2703 	    else
2704 		continue;
2705 	    if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2706 		continue;
2707 	    gotmachine = 1;
2708 	}
2709 	makeargv();
2710 	if (margv[0] == 0)
2711 	    continue;
2712 	c = getcmd(margv[0]);
2713 	if (Ambiguous(c)) {
2714 	    printf("?Ambiguous command: %s\n", margv[0]);
2715 	    continue;
2716 	}
2717 	if (c == 0) {
2718 	    printf("?Invalid command: %s\n", margv[0]);
2719 	    continue;
2720 	}
2721 	/*
2722 	 * This should never happen...
2723 	 */
2724 	if (c->needconnect && !connected) {
2725 	    printf("?Need to be connected first for %s.\n", margv[0]);
2726 	    continue;
2727 	}
2728 	(*c->handler)(margc, margv);
2729     }
2730     fclose(rcfile);
2731 }
2732 
2733 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2734 
2735 /*
2736  * Source route is handed in as
2737  *	[!]@hop1@hop2...[@|:]dst
2738  * If the leading ! is present, it is a
2739  * strict source route, otherwise it is
2740  * assmed to be a loose source route.
2741  *
2742  * We fill in the source route option as
2743  *	hop1,hop2,hop3...dest
2744  * and return a pointer to hop1, which will
2745  * be the address to connect() to.
2746  *
2747  * Arguments:
2748  *	arg:	pointer to route list to decipher
2749  *
2750  *	cpp: 	If *cpp is not equal to NULL, this is a
2751  *		pointer to a pointer to a character array
2752  *		that should be filled in with the option.
2753  *
2754  *	lenp:	pointer to an integer that contains the
2755  *		length of *cpp if *cpp != NULL.
2756  *
2757  * Return values:
2758  *
2759  *	Returns the address of the host to connect to.  If the
2760  *	return value is -1, there was a syntax error in the
2761  *	option, either unknown characters, or too many hosts.
2762  *	If the return value is 0, one of the hostnames in the
2763  *	path is unknown, and *cpp is set to point to the bad
2764  *	hostname.
2765  *
2766  *	*cpp:	If *cpp was equal to NULL, it will be filled
2767  *		in with a pointer to our static area that has
2768  *		the option filled in.  This will be 32bit aligned.
2769  *
2770  *	*lenp:	This will be filled in with how long the option
2771  *		pointed to by *cpp is.
2772  *
2773  */
2774 	unsigned long
2775 sourceroute(arg, cpp, lenp)
2776 	char	*arg;
2777 	char	**cpp;
2778 	int	*lenp;
2779 {
2780 	static char lsr[44];
2781 #ifdef	sysV88
2782 	static IOPTN ipopt;
2783 #endif
2784 	char *cp, *cp2, *lsrp, *lsrep;
2785 	register int tmp;
2786 	struct in_addr sin_addr;
2787 	register struct hostent *host = 0;
2788 	register char c;
2789 
2790 	/*
2791 	 * Verify the arguments, and make sure we have
2792 	 * at least 7 bytes for the option.
2793 	 */
2794 	if (cpp == NULL || lenp == NULL)
2795 		return((unsigned long)-1);
2796 	if (*cpp != NULL && *lenp < 7)
2797 		return((unsigned long)-1);
2798 	/*
2799 	 * Decide whether we have a buffer passed to us,
2800 	 * or if we need to use our own static buffer.
2801 	 */
2802 	if (*cpp) {
2803 		lsrp = *cpp;
2804 		lsrep = lsrp + *lenp;
2805 	} else {
2806 		*cpp = lsrp = lsr;
2807 		lsrep = lsrp + 44;
2808 	}
2809 
2810 	cp = arg;
2811 
2812 	/*
2813 	 * Next, decide whether we have a loose source
2814 	 * route or a strict source route, and fill in
2815 	 * the begining of the option.
2816 	 */
2817 #ifndef	sysV88
2818 	if (*cp == '!') {
2819 		cp++;
2820 		*lsrp++ = IPOPT_SSRR;
2821 	} else
2822 		*lsrp++ = IPOPT_LSRR;
2823 #else
2824 	if (*cp == '!') {
2825 		cp++;
2826 		ipopt.io_type = IPOPT_SSRR;
2827 	} else
2828 		ipopt.io_type = IPOPT_LSRR;
2829 #endif
2830 
2831 	if (*cp != '@')
2832 		return((unsigned long)-1);
2833 
2834 #ifndef	sysV88
2835 	lsrp++;		/* skip over length, we'll fill it in later */
2836 	*lsrp++ = 4;
2837 #endif
2838 
2839 	cp++;
2840 
2841 	sin_addr.s_addr = 0;
2842 
2843 	for (c = 0;;) {
2844 		if (c == ':')
2845 			cp2 = 0;
2846 		else for (cp2 = cp; c = *cp2; cp2++) {
2847 			if (c == ',') {
2848 				*cp2++ = '\0';
2849 				if (*cp2 == '@')
2850 					cp2++;
2851 			} else if (c == '@') {
2852 				*cp2++ = '\0';
2853 			} else if (c == ':') {
2854 				*cp2++ = '\0';
2855 			} else
2856 				continue;
2857 			break;
2858 		}
2859 		if (!c)
2860 			cp2 = 0;
2861 
2862 		if ((tmp = inet_addr(cp)) != -1) {
2863 			sin_addr.s_addr = tmp;
2864 		} else if (host = gethostbyname(cp)) {
2865 #if	defined(h_addr)
2866 			memcpy((caddr_t)&sin_addr,
2867 				host->h_addr_list[0], host->h_length);
2868 #else
2869 			memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length);
2870 #endif
2871 		} else {
2872 			*cpp = cp;
2873 			return(0);
2874 		}
2875 		memcpy(lsrp, (char *)&sin_addr, 4);
2876 		lsrp += 4;
2877 		if (cp2)
2878 			cp = cp2;
2879 		else
2880 			break;
2881 		/*
2882 		 * Check to make sure there is space for next address
2883 		 */
2884 		if (lsrp + 4 > lsrep)
2885 			return((unsigned long)-1);
2886 	}
2887 #ifndef	sysV88
2888 	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2889 		*cpp = 0;
2890 		*lenp = 0;
2891 		return((unsigned long)-1);
2892 	}
2893 	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2894 	*lenp = lsrp - *cpp;
2895 #else
2896 	ipopt.io_len = lsrp - *cpp;
2897 	if (ipopt.io_len <= 5) {		/* Is 3 better ? */
2898 		*cpp = 0;
2899 		*lenp = 0;
2900 		return((unsigned long)-1);
2901 	}
2902 	*lenp = sizeof(ipopt);
2903 	*cpp = (char *) &ipopt;
2904 #endif
2905 	return(sin_addr.s_addr);
2906 }
2907 #endif
2908