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