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