xref: /netbsd-src/usr.bin/telnet/commands.c (revision 7c7c171d130af9949261bc7dce2150a03c3d239c)
1 /*	$NetBSD: commands.c,v 1.20 1998/03/30 02:30:08 mrg 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.20 1998/03/30 02:30:08 mrg 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((void));
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(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(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     { "authdebug",
699 	"Toggle authentication debugging",
700 	    auth_togdebug,
701 		0,
702 		     "print authentication debugging information" },
703 #endif
704     { "skiprc",
705 	"don't read ~/.telnetrc file",
706 	    0,
707 		&skiprc,
708 		    "skip reading of ~/.telnetrc file" },
709     { "binary",
710 	"sending and receiving of binary data",
711 	    togbinary,
712 		0,
713 		    0 },
714     { "inbinary",
715 	"receiving of binary data",
716 	    togrbinary,
717 		0,
718 		    0 },
719     { "outbinary",
720 	"sending of binary data",
721 	    togxbinary,
722 		0,
723 		    0 },
724     { "crlf",
725 	"sending carriage returns as telnet <CR><LF>",
726 	   togcrlf,
727 		&crlf,
728 		    0 },
729     { "crmod",
730 	"mapping of received carriage returns",
731 	    0,
732 		&crmod,
733 		    "map carriage return on output" },
734     { "localchars",
735 	"local recognition of certain control characters",
736 	    lclchars,
737 		&localchars,
738 		    "recognize certain control characters" },
739     { " ", "", 0 },		/* empty line */
740 #if	defined(unix) && defined(TN3270)
741     { "apitrace",
742 	"(debugging) toggle tracing of API transactions",
743 	    0,
744 		&apitrace,
745 		    "trace API transactions" },
746     { "cursesdata",
747 	"(debugging) toggle printing of hexadecimal curses data",
748 	    0,
749 		&cursesdata,
750 		    "print hexadecimal representation of curses data" },
751 #endif	/* defined(unix) && defined(TN3270) */
752     { "debug",
753 	"debugging",
754 	    togdebug,
755 		&debug,
756 		    "turn on socket level debugging" },
757     { "netdata",
758 	"printing of hexadecimal network data (debugging)",
759 	    0,
760 		&netdata,
761 		    "print hexadecimal representation of network traffic" },
762     { "prettydump",
763 	"output of \"netdata\" to user readable format (debugging)",
764 	    0,
765 		&prettydump,
766 		    "print user readable output for \"netdata\"" },
767     { "options",
768 	"viewing of options processing (debugging)",
769 	    0,
770 		&showoptions,
771 		    "show option processing" },
772 #if	defined(unix)
773     { "termdata",
774 	"(debugging) toggle printing of hexadecimal terminal data",
775 	    0,
776 		&termdata,
777 		    "print hexadecimal representation of terminal traffic" },
778 #endif	/* defined(unix) */
779     { "?",
780 	0,
781 	    togglehelp },
782     { "help",
783 	0,
784 	    togglehelp },
785     { 0 }
786 };
787 
788     static int
789 togglehelp(n)
790     int n;
791 {
792     struct togglelist *c;
793 
794     for (c = Togglelist; c->name; c++) {
795 	if (c->help) {
796 	    if (*c->help)
797 		printf("%-15s toggle %s\n", c->name, c->help);
798 	    else
799 		printf("\n");
800 	}
801     }
802     printf("\n");
803     printf("%-15s %s\n", "?", "display help information");
804     return 0;
805 }
806 
807     static void
808 settogglehelp(set)
809     int set;
810 {
811     struct togglelist *c;
812 
813     for (c = Togglelist; c->name; c++) {
814 	if (c->help) {
815 	    if (*c->help)
816 		printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
817 						c->help);
818 	    else
819 		printf("\n");
820 	}
821     }
822 }
823 
824 #define	GETTOGGLE(name) (struct togglelist *) \
825 		genget(name, (char **) Togglelist, sizeof(struct togglelist))
826 
827     static int
828 toggle(argc, argv)
829     int  argc;
830     char *argv[];
831 {
832     int retval = 1;
833     char *name;
834     struct togglelist *c;
835 
836     if (argc < 2) {
837 	fprintf(stderr,
838 	    "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
839 	return 0;
840     }
841     argc--;
842     argv++;
843     while (argc--) {
844 	name = *argv++;
845 	c = GETTOGGLE(name);
846 	if (Ambiguous(c)) {
847 	    fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
848 					name);
849 	    return 0;
850 	} else if (c == 0) {
851 	    fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
852 					name);
853 	    return 0;
854 	} else {
855 	    if (c->variable) {
856 		*c->variable = !*c->variable;		/* invert it */
857 		if (c->actionexplanation) {
858 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
859 							c->actionexplanation);
860 		}
861 	    }
862 	    if (c->handler) {
863 		retval &= (*c->handler)(-1);
864 	    }
865 	}
866     }
867     return retval;
868 }
869 
870 /*
871  * The following perform the "set" command.
872  */
873 
874 #ifdef	USE_TERMIO
875 struct termio new_tc = { 0 };
876 #endif
877 
878 struct setlist {
879     char *name;				/* name */
880     char *help;				/* help information */
881     void (*handler) P((char *));
882     cc_t *charp;			/* where it is located at */
883 };
884 
885 static struct setlist Setlist[] = {
886 #ifdef	KLUDGELINEMODE
887     { "echo", 	"character to toggle local echoing on/off", 0, &echoc },
888 #endif
889     { "escape",	"character to escape back to telnet command mode", 0, &escape },
890     { "rlogin", "rlogin escape character", 0, &rlogin },
891     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
892     { " ", "" },
893     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
894     { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
895     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
896     { "quit",	"character to cause an Abort process", 0, termQuitCharp },
897     { "eof",	"character to cause an EOF ", 0, termEofCharp },
898     { " ", "" },
899     { " ", "The following are for local editing in linemode", 0, 0 },
900     { "erase",	"character to use to erase a character", 0, termEraseCharp },
901     { "kill",	"character to use to erase a line", 0, termKillCharp },
902     { "lnext",	"character to use for literal next", 0, termLiteralNextCharp },
903     { "susp",	"character to cause a Suspend Process", 0, termSuspCharp },
904     { "reprint", "character to use for line reprint", 0, termRprntCharp },
905     { "worderase", "character to use to erase a word", 0, termWerasCharp },
906     { "start",	"character to use for XON", 0, termStartCharp },
907     { "stop",	"character to use for XOFF", 0, termStopCharp },
908     { "forw1",	"alternate end of line character", 0, termForw1Charp },
909     { "forw2",	"alternate end of line character", 0, termForw2Charp },
910     { "ayt",	"alternate AYT character", 0, termAytCharp },
911     { 0 }
912 };
913 
914 #if	defined(CRAY) && !defined(__STDC__)
915 /* Work around compiler bug in pcc 4.1.5 */
916     void
917 _setlist_init()
918 {
919 #ifndef	KLUDGELINEMODE
920 #define	N 5
921 #else
922 #define	N 6
923 #endif
924 	Setlist[N+0].charp = &termFlushChar;
925 	Setlist[N+1].charp = &termIntChar;
926 	Setlist[N+2].charp = &termQuitChar;
927 	Setlist[N+3].charp = &termEofChar;
928 	Setlist[N+6].charp = &termEraseChar;
929 	Setlist[N+7].charp = &termKillChar;
930 	Setlist[N+8].charp = &termLiteralNextChar;
931 	Setlist[N+9].charp = &termSuspChar;
932 	Setlist[N+10].charp = &termRprntChar;
933 	Setlist[N+11].charp = &termWerasChar;
934 	Setlist[N+12].charp = &termStartChar;
935 	Setlist[N+13].charp = &termStopChar;
936 	Setlist[N+14].charp = &termForw1Char;
937 	Setlist[N+15].charp = &termForw2Char;
938 	Setlist[N+16].charp = &termAytChar;
939 #undef	N
940 }
941 #endif	/* defined(CRAY) && !defined(__STDC__) */
942 
943     static struct setlist *
944 getset(name)
945     char *name;
946 {
947     return (struct setlist *)
948 		genget(name, (char **) Setlist, sizeof(struct setlist));
949 }
950 
951     void
952 set_escape_char(s)
953     char *s;
954 {
955 	if (rlogin != _POSIX_VDISABLE) {
956 		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
957 		printf("Telnet rlogin escape character is '%s'.\n",
958 					control(rlogin));
959 	} else {
960 		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
961 		printf("Telnet escape character is '%s'.\n", control(escape));
962 	}
963 }
964 
965     static int
966 setcmd(argc, argv)
967     int  argc;
968     char *argv[];
969 {
970     int value;
971     struct setlist *ct;
972     struct togglelist *c;
973 
974     if (argc < 2 || argc > 3) {
975 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
976 	return 0;
977     }
978     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
979 	for (ct = Setlist; ct->name; ct++)
980 	    printf("%-15s %s\n", ct->name, ct->help);
981 	printf("\n");
982 	settogglehelp(1);
983 	printf("%-15s %s\n", "?", "display help information");
984 	return 0;
985     }
986 
987     ct = getset(argv[1]);
988     if (ct == 0) {
989 	c = GETTOGGLE(argv[1]);
990 	if (c == 0) {
991 	    fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
992 			argv[1]);
993 	    return 0;
994 	} else if (Ambiguous(c)) {
995 	    fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
996 			argv[1]);
997 	    return 0;
998 	}
999 	if (c->variable) {
1000 	    if ((argc == 2) || (strcmp("on", argv[2]) == 0))
1001 		*c->variable = 1;
1002 	    else if (strcmp("off", argv[2]) == 0)
1003 		*c->variable = 0;
1004 	    else {
1005 		printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
1006 		return 0;
1007 	    }
1008 	    if (c->actionexplanation) {
1009 		printf("%s %s.\n", *c->variable? "Will" : "Won't",
1010 							c->actionexplanation);
1011 	    }
1012 	}
1013 	if (c->handler)
1014 	    (*c->handler)(1);
1015     } else if (argc != 3) {
1016 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
1017 	return 0;
1018     } else if (Ambiguous(ct)) {
1019 	fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1020 			argv[1]);
1021 	return 0;
1022     } else if (ct->handler) {
1023 	(*ct->handler)(argv[2]);
1024 	printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
1025     } else {
1026 	if (strcmp("off", argv[2])) {
1027 	    value = special(argv[2]);
1028 	} else {
1029 	    value = _POSIX_VDISABLE;
1030 	}
1031 	*(ct->charp) = (cc_t)value;
1032 	printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1033     }
1034     slc_check();
1035     return 1;
1036 }
1037 
1038     static int
1039 unsetcmd(argc, argv)
1040     int  argc;
1041     char *argv[];
1042 {
1043     struct setlist *ct;
1044     struct togglelist *c;
1045     register char *name;
1046 
1047     if (argc < 2) {
1048 	fprintf(stderr,
1049 	    "Need an argument to 'unset' command.  'unset ?' for help.\n");
1050 	return 0;
1051     }
1052     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1053 	for (ct = Setlist; ct->name; ct++)
1054 	    printf("%-15s %s\n", ct->name, ct->help);
1055 	printf("\n");
1056 	settogglehelp(0);
1057 	printf("%-15s %s\n", "?", "display help information");
1058 	return 0;
1059     }
1060 
1061     argc--;
1062     argv++;
1063     while (argc--) {
1064 	name = *argv++;
1065 	ct = getset(name);
1066 	if (ct == 0) {
1067 	    c = GETTOGGLE(name);
1068 	    if (c == 0) {
1069 		fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1070 			name);
1071 		return 0;
1072 	    } else if (Ambiguous(c)) {
1073 		fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1074 			name);
1075 		return 0;
1076 	    }
1077 	    if (c->variable) {
1078 		*c->variable = 0;
1079 		if (c->actionexplanation) {
1080 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
1081 							c->actionexplanation);
1082 		}
1083 	    }
1084 	    if (c->handler)
1085 		(*c->handler)(0);
1086 	} else if (Ambiguous(ct)) {
1087 	    fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1088 			name);
1089 	    return 0;
1090 	} else if (ct->handler) {
1091 	    (*ct->handler)(0);
1092 	    printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1093 	} else {
1094 	    *(ct->charp) = _POSIX_VDISABLE;
1095 	    printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1096 	}
1097     }
1098     return 1;
1099 }
1100 
1101 /*
1102  * The following are the data structures and routines for the
1103  * 'mode' command.
1104  */
1105 #ifdef	KLUDGELINEMODE
1106 extern int kludgelinemode;
1107 
1108     static int
1109 dokludgemode(n)
1110     int n;
1111 {
1112     kludgelinemode = 1;
1113     send_wont(TELOPT_LINEMODE, 1);
1114     send_dont(TELOPT_SGA, 1);
1115     send_dont(TELOPT_ECHO, 1);
1116     return 1;
1117 }
1118 #endif
1119 
1120     static int
1121 dolinemode(n)
1122     int n;
1123 {
1124 #ifdef	KLUDGELINEMODE
1125     if (kludgelinemode)
1126 	send_dont(TELOPT_SGA, 1);
1127 #endif
1128     send_will(TELOPT_LINEMODE, 1);
1129     send_dont(TELOPT_ECHO, 1);
1130     return 1;
1131 }
1132 
1133     static int
1134 docharmode(n)
1135     int n;
1136 {
1137 #ifdef	KLUDGELINEMODE
1138     if (kludgelinemode)
1139 	send_do(TELOPT_SGA, 1);
1140     else
1141 #endif
1142     send_wont(TELOPT_LINEMODE, 1);
1143     send_do(TELOPT_ECHO, 1);
1144     return 1;
1145 }
1146 
1147     static int
1148 dolmmode(bit, on)
1149     int bit, on;
1150 {
1151     unsigned char c;
1152     extern int linemode;
1153 
1154     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1155 	printf("?Need to have LINEMODE option enabled first.\n");
1156 	printf("'mode ?' for help.\n");
1157 	return 0;
1158     }
1159 
1160     if (on)
1161 	c = (linemode | bit);
1162     else
1163 	c = (linemode & ~bit);
1164     lm_mode(&c, 1, 1);
1165     return 1;
1166 }
1167 
1168     int
1169 set_mode(bit)
1170     int bit;
1171 {
1172     return dolmmode(bit, 1);
1173 }
1174 
1175     int
1176 clear_mode(bit)
1177     int bit;
1178 {
1179     return dolmmode(bit, 0);
1180 }
1181 
1182 struct modelist {
1183 	char	*name;		/* command name */
1184 	char	*help;		/* help string */
1185 	int	(*handler)	/* routine which executes command */
1186 			P((int));
1187 	int	needconnect;	/* Do we need to be connected to execute? */
1188 	int	arg1;
1189 };
1190 
1191 static struct modelist ModeList[] = {
1192     { "character", "Disable LINEMODE option",	docharmode, 1 },
1193 #ifdef	KLUDGELINEMODE
1194     { "",	"(or disable obsolete line-by-line mode)", 0 },
1195 #endif
1196     { "line",	"Enable LINEMODE option",	dolinemode, 1 },
1197 #ifdef	KLUDGELINEMODE
1198     { "",	"(or enable obsolete line-by-line mode)", 0 },
1199 #endif
1200     { "", "", 0 },
1201     { "",	"These require the LINEMODE option to be enabled", 0 },
1202     { "isig",	"Enable signal trapping",	set_mode, 1, MODE_TRAPSIG },
1203     { "+isig",	0,				set_mode, 1, MODE_TRAPSIG },
1204     { "-isig",	"Disable signal trapping",	clear_mode, 1, MODE_TRAPSIG },
1205     { "edit",	"Enable character editing",	set_mode, 1, MODE_EDIT },
1206     { "+edit",	0,				set_mode, 1, MODE_EDIT },
1207     { "-edit",	"Disable character editing",	clear_mode, 1, MODE_EDIT },
1208     { "softtabs", "Enable tab expansion",	set_mode, 1, MODE_SOFT_TAB },
1209     { "+softtabs", 0,				set_mode, 1, MODE_SOFT_TAB },
1210     { "-softtabs", "Disable character editing",	clear_mode, 1, MODE_SOFT_TAB },
1211     { "litecho", "Enable literal character echo", set_mode, 1, MODE_LIT_ECHO },
1212     { "+litecho", 0,				set_mode, 1, MODE_LIT_ECHO },
1213     { "-litecho", "Disable literal character echo", clear_mode, 1, MODE_LIT_ECHO },
1214     { "help",	0,				modehelp, 0 },
1215 #ifdef	KLUDGELINEMODE
1216     { "kludgeline", 0,				dokludgemode, 1 },
1217 #endif
1218     { "", "", 0 },
1219     { "?",	"Print help information",	modehelp, 0 },
1220     { 0 },
1221 };
1222 
1223 
1224     int
1225 modehelp(n)
1226     int n;
1227 {
1228     struct modelist *mt;
1229 
1230     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
1231     for (mt = ModeList; mt->name; mt++) {
1232 	if (mt->help) {
1233 	    if (*mt->help)
1234 		printf("%-15s %s\n", mt->name, mt->help);
1235 	    else
1236 		printf("\n");
1237 	}
1238     }
1239     return 0;
1240 }
1241 
1242 #define	GETMODECMD(name) (struct modelist *) \
1243 		genget(name, (char **) ModeList, sizeof(struct modelist))
1244 
1245     static int
1246 modecmd(argc, argv)
1247     int  argc;
1248     char *argv[];
1249 {
1250     struct modelist *mt;
1251 
1252     if (argc != 2) {
1253 	printf("'mode' command requires an argument\n");
1254 	printf("'mode ?' for help.\n");
1255     } else if ((mt = GETMODECMD(argv[1])) == 0) {
1256 	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1257     } else if (Ambiguous(mt)) {
1258 	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1259     } else if (mt->needconnect && !connected) {
1260 	printf("?Need to be connected first.\n");
1261 	printf("'mode ?' for help.\n");
1262     } else if (mt->handler) {
1263 	return (*mt->handler)(mt->arg1);
1264     }
1265     return 0;
1266 }
1267 
1268 /*
1269  * The following data structures and routines implement the
1270  * "display" command.
1271  */
1272 
1273     static int
1274 display(argc, argv)
1275     int  argc;
1276     char *argv[];
1277 {
1278     struct togglelist *tl;
1279     struct setlist *sl;
1280 
1281 #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
1282 			    if (*tl->variable) { \
1283 				printf("will"); \
1284 			    } else { \
1285 				printf("won't"); \
1286 			    } \
1287 			    printf(" %s.\n", tl->actionexplanation); \
1288 			}
1289 
1290 #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
1291 			if (sl->handler == 0) \
1292 			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1293 			else \
1294 			    printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1295 		    }
1296 
1297     if (argc == 1) {
1298 	for (tl = Togglelist; tl->name; tl++) {
1299 	    dotog(tl);
1300 	}
1301 	printf("\n");
1302 	for (sl = Setlist; sl->name; sl++) {
1303 	    doset(sl);
1304 	}
1305     } else {
1306 	int i;
1307 
1308 	for (i = 1; i < argc; i++) {
1309 	    sl = getset(argv[i]);
1310 	    tl = GETTOGGLE(argv[i]);
1311 	    if (Ambiguous(sl) || Ambiguous(tl)) {
1312 		printf("?Ambiguous argument '%s'.\n", argv[i]);
1313 		return 0;
1314 	    } else if (!sl && !tl) {
1315 		printf("?Unknown argument '%s'.\n", argv[i]);
1316 		return 0;
1317 	    } else {
1318 		if (tl) {
1319 		    dotog(tl);
1320 		}
1321 		if (sl) {
1322 		    doset(sl);
1323 		}
1324 	    }
1325 	}
1326     }
1327 /*@*/optionstatus();
1328     return 1;
1329 #undef	doset
1330 #undef	dotog
1331 }
1332 
1333 /*
1334  * The following are the data structures, and many of the routines,
1335  * relating to command processing.
1336  */
1337 
1338 /*
1339  * Set the escape character.
1340  */
1341 	static int
1342 setescape(argc, argv)
1343 	int argc;
1344 	char *argv[];
1345 {
1346 	register char *arg;
1347 	char buf[50];
1348 
1349 	printf(
1350 	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1351 				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1352 	if (argc > 2)
1353 		arg = argv[1];
1354 	else {
1355 		printf("new escape character: ");
1356 		(void) fgets(buf, sizeof(buf), stdin);
1357 		arg = buf;
1358 	}
1359 	if (arg[0] != '\0')
1360 		escape = arg[0];
1361 	if (!In3270) {
1362 		printf("Escape character is '%s'.\n", control(escape));
1363 	}
1364 	(void) fflush(stdout);
1365 	return 1;
1366 }
1367 
1368     /*VARARGS*/
1369     static int
1370 togcrmod(argc, argv)
1371     int argc;
1372     char *argv[];
1373 {
1374     crmod = !crmod;
1375     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1376     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1377     (void) fflush(stdout);
1378     return 1;
1379 }
1380 
1381     /*VARARGS*/
1382     int
1383 suspend(argc, argv)
1384     int argc;
1385     char *argv[];
1386 {
1387 #ifdef	SIGTSTP
1388     setcommandmode();
1389     {
1390 	long oldrows, oldcols, newrows, newcols, err;
1391 
1392 	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1393 	(void) kill(0, SIGTSTP);
1394 	/*
1395 	 * If we didn't get the window size before the SUSPEND, but we
1396 	 * can get them now (?), then send the NAWS to make sure that
1397 	 * we are set up for the right window size.
1398 	 */
1399 	if (TerminalWindowSize(&newrows, &newcols) && connected &&
1400 	    (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1401 		sendnaws();
1402 	}
1403     }
1404     /* reget parameters in case they were changed */
1405     TerminalSaveState();
1406     setconnmode(0);
1407 #else
1408     printf("Suspend is not supported.  Try the '!' command instead\n");
1409 #endif
1410     return 1;
1411 }
1412 
1413 #if	!defined(TN3270)
1414     /*ARGSUSED*/
1415     int
1416 shell(argc, argv)
1417     int argc;
1418     char *argv[];
1419 {
1420     long oldrows, oldcols, newrows, newcols, err;
1421 
1422 #ifdef __GNUC__
1423     (void) &err;	/* XXX avoid GCC warning */
1424 #endif
1425 
1426     setcommandmode();
1427 
1428     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1429     switch(vfork()) {
1430     case -1:
1431 	perror("Fork failed\n");
1432 	break;
1433 
1434     case 0:
1435 	{
1436 	    /*
1437 	     * Fire up the shell in the child.
1438 	     */
1439 	    register char *shellp, *shellname;
1440 
1441 	    shellp = getenv("SHELL");
1442 	    if (shellp == NULL)
1443 		shellp = "/bin/sh";
1444 	    if ((shellname = strrchr(shellp, '/')) == 0)
1445 		shellname = shellp;
1446 	    else
1447 		shellname++;
1448 	    if (argc > 1)
1449 		execl(shellp, shellname, "-c", &saveline[1], 0);
1450 	    else
1451 		execl(shellp, shellname, 0);
1452 	    perror("Execl");
1453 	    _exit(1);
1454 	}
1455     default:
1456 	    (void)wait((int *)0);	/* Wait for the shell to complete */
1457 
1458 	    if (TerminalWindowSize(&newrows, &newcols) && connected &&
1459 		(err || ((oldrows != newrows) || (oldcols != newcols)))) {
1460 		    sendnaws();
1461 	    }
1462 	    break;
1463     }
1464     return 1;
1465 }
1466 #endif	/* !defined(TN3270) */
1467 
1468     /*VARARGS*/
1469     static int
1470 bye(argc, argv)
1471     int  argc;		/* Number of arguments */
1472     char *argv[];	/* arguments */
1473 {
1474     extern int resettermname;
1475 
1476     if (connected) {
1477 	(void) shutdown(net, 2);
1478 	printf("Connection closed.\n");
1479 	(void) NetClose(net);
1480 	connected = 0;
1481 	resettermname = 1;
1482 #if	defined(AUTHENTICATION)
1483 	auth_encrypt_connect(connected);
1484 #endif	/* defined(AUTHENTICATION) */
1485 	/* reset options */
1486 	tninit();
1487 #if	defined(TN3270)
1488 	SetIn3270();		/* Get out of 3270 mode */
1489 #endif	/* defined(TN3270) */
1490     }
1491     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1492 	longjmp(toplevel, 1);
1493 	/* NOTREACHED */
1494     }
1495     return 1;			/* Keep lint, etc., happy */
1496 }
1497 
1498 /*VARARGS*/
1499 int
1500 quit(argc, argv)
1501 	int argc;
1502 	char *argv[];
1503 {
1504 	(void) call(bye, "bye", "fromquit", 0);
1505 	Exit(0);
1506 	/*NOTREACHED*/
1507 }
1508 
1509 /*VARARGS*/
1510 	int
1511 logout(argc, argv)
1512 	int argc;
1513 	char *argv[];
1514 {
1515 	send_do(TELOPT_LOGOUT, 1);
1516 	(void) netflush();
1517 	return 1;
1518 }
1519 
1520 
1521 /*
1522  * The SLC command.
1523  */
1524 
1525 struct slclist {
1526 	char	*name;
1527 	char	*help;
1528 	void	(*handler) P((int));
1529 	int	arg;
1530 };
1531 
1532 struct slclist SlcList[] = {
1533     { "export",	"Use local special character definitions",
1534 						slc_mode_export,	0 },
1535     { "import",	"Use remote special character definitions",
1536 						slc_mode_import,	1 },
1537     { "check",	"Verify remote special character definitions",
1538 						slc_mode_import,	0 },
1539     { "help",	0,				slc_help,		0 },
1540     { "?",	"Print help information",	slc_help,		0 },
1541     { 0 },
1542 };
1543 
1544     static void
1545 slc_help(n)
1546     int n;
1547 {
1548     struct slclist *c;
1549 
1550     for (c = SlcList; c->name; c++) {
1551 	if (c->help) {
1552 	    if (*c->help)
1553 		printf("%-15s %s\n", c->name, c->help);
1554 	    else
1555 		printf("\n");
1556 	}
1557     }
1558 }
1559 
1560     static struct slclist *
1561 getslc(name)
1562     char *name;
1563 {
1564     return (struct slclist *)
1565 		genget(name, (char **) SlcList, sizeof(struct slclist));
1566 }
1567 
1568     static int
1569 slccmd(argc, argv)
1570     int  argc;
1571     char *argv[];
1572 {
1573     struct slclist *c;
1574 
1575     if (argc != 2) {
1576 	fprintf(stderr,
1577 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
1578 	return 0;
1579     }
1580     c = getslc(argv[1]);
1581     if (c == 0) {
1582 	fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1583     				argv[1]);
1584 	return 0;
1585     }
1586     if (Ambiguous(c)) {
1587 	fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1588     				argv[1]);
1589 	return 0;
1590     }
1591     (*c->handler)(c->arg);
1592     slcstate();
1593     return 1;
1594 }
1595 
1596 /*
1597  * The ENVIRON command.
1598  */
1599 
1600 struct envlist {
1601 	char	*name;
1602 	char	*help;
1603 	struct env_lst *(*handler) P((unsigned char *, unsigned char *));
1604 	int	narg;
1605 };
1606 
1607 struct envlist EnvList[] = {
1608     { "define",	"Define an environment variable",
1609 						env_define,	2 },
1610     { "undefine", "Undefine an environment variable",
1611 						env_undefine,	1 },
1612     { "export",	"Mark an environment variable for automatic export",
1613 						env_export,	1 },
1614     { "unexport", "Don't mark an environment variable for automatic export",
1615 						env_unexport,	1 },
1616     { "send",	"Send an environment variable", env_send,	1 },
1617     { "list",	"List the current environment variables",
1618 						env_list,	0 },
1619 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1620     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1621 						env_varval,    1 },
1622 #endif
1623     { "help",	0,				env_help,		0 },
1624     { "?",	"Print help information",	env_help,		0 },
1625     { 0 },
1626 };
1627 
1628     static struct env_lst *
1629 env_help(us1, us2)
1630     unsigned char *us1, *us2;
1631 {
1632     struct envlist *c;
1633 
1634     for (c = EnvList; c->name; c++) {
1635 	if (c->help) {
1636 	    if (*c->help)
1637 		printf("%-15s %s\n", c->name, c->help);
1638 	    else
1639 		printf("\n");
1640 	}
1641     }
1642     return NULL;
1643 }
1644 
1645     static struct envlist *
1646 getenvcmd(name)
1647     char *name;
1648 {
1649     return (struct envlist *)
1650 		genget(name, (char **) EnvList, sizeof(struct envlist));
1651 }
1652 
1653     int
1654 env_cmd(argc, argv)
1655     int  argc;
1656     char *argv[];
1657 {
1658     struct envlist *c;
1659 
1660     if (argc < 2) {
1661 	fprintf(stderr,
1662 	    "Need an argument to 'environ' command.  'environ ?' for help.\n");
1663 	return 0;
1664     }
1665     c = getenvcmd(argv[1]);
1666     if (c == 0) {
1667 	fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1668     				argv[1]);
1669 	return 0;
1670     }
1671     if (Ambiguous(c)) {
1672 	fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1673     				argv[1]);
1674 	return 0;
1675     }
1676     if (c->narg + 2 != argc) {
1677 	fprintf(stderr,
1678 	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
1679 		c->narg < argc + 2 ? "only " : "",
1680 		c->narg, c->narg == 1 ? "" : "s", c->name);
1681 	return 0;
1682     }
1683     (*c->handler)(argv[2], argv[3]);
1684     return 1;
1685 }
1686 
1687 struct env_lst {
1688 	struct env_lst *next;	/* pointer to next structure */
1689 	struct env_lst *prev;	/* pointer to previous structure */
1690 	unsigned char *var;	/* pointer to variable name */
1691 	unsigned char *value;	/* pointer to variable value */
1692 	int export;		/* 1 -> export with default list of variables */
1693 	int welldefined;	/* A well defined variable */
1694 };
1695 
1696 struct env_lst envlisthead;
1697 
1698 	struct env_lst *
1699 env_find(var)
1700 	unsigned char *var;
1701 {
1702 	register struct env_lst *ep;
1703 
1704 	for (ep = envlisthead.next; ep; ep = ep->next) {
1705 		if (strcmp((char *)ep->var, (char *)var) == 0)
1706 			return(ep);
1707 	}
1708 	return(NULL);
1709 }
1710 
1711 	void
1712 env_init()
1713 {
1714 	extern char **environ;
1715 	register char **epp, *cp;
1716 	register struct env_lst *ep;
1717 
1718 	for (epp = environ; *epp; epp++) {
1719 		if ((cp = strchr(*epp, '=')) != NULL) {
1720 			*cp = '\0';
1721 			ep = env_define((unsigned char *)*epp,
1722 					(unsigned char *)cp+1);
1723 			ep->export = 0;
1724 			*cp = '=';
1725 		}
1726 	}
1727 	/*
1728 	 * Special case for DISPLAY variable.  If it is ":0.0" or
1729 	 * "unix:0.0", we have to get rid of "unix" and insert our
1730 	 * hostname.
1731 	 */
1732 	if ((ep = env_find("DISPLAY"))
1733 	    && ((*ep->value == ':')
1734 		|| (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1735 		char hbuf[256+1];
1736 		char *cp2 = strchr((char *)ep->value, ':');
1737 
1738 		gethostname(hbuf, 256);
1739 		hbuf[256] = '\0';
1740 		cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1741 		sprintf((char *)cp, "%s%s", hbuf, cp2);
1742 		free(ep->value);
1743 		ep->value = (unsigned char *)cp;
1744 	}
1745 	/*
1746 	 * If USER is not defined, but LOGNAME is, then add
1747 	 * USER with the value from LOGNAME.  By default, we
1748 	 * don't export the USER variable.
1749 	 */
1750 	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1751 		env_define((unsigned char *)"USER", ep->value);
1752 		env_unexport((unsigned char *)"USER", NULL);
1753 	}
1754 	env_export((unsigned char *)"DISPLAY", NULL);
1755 	env_export((unsigned char *)"PRINTER", NULL);
1756 }
1757 
1758 	struct env_lst *
1759 env_define(var, value)
1760 	unsigned char *var, *value;
1761 {
1762 	register struct env_lst *ep;
1763 
1764 	if ((ep = env_find(var)) != NULL) {
1765 		if (ep->var)
1766 			free(ep->var);
1767 		if (ep->value)
1768 			free(ep->value);
1769 	} else {
1770 		ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1771 		ep->next = envlisthead.next;
1772 		envlisthead.next = ep;
1773 		ep->prev = &envlisthead;
1774 		if (ep->next)
1775 			ep->next->prev = ep;
1776 	}
1777 	ep->welldefined = opt_welldefined(var);
1778 	ep->export = 1;
1779 	ep->var = (unsigned char *)strdup((char *)var);
1780 	ep->value = (unsigned char *)strdup((char *)value);
1781 	return(ep);
1782 }
1783 
1784 	struct env_lst *
1785 env_undefine(var, d)
1786 	unsigned char *var;
1787 	unsigned char *d;
1788 {
1789 	register struct env_lst *ep;
1790 
1791 	if ((ep = env_find(var)) != NULL) {
1792 		ep->prev->next = ep->next;
1793 		if (ep->next)
1794 			ep->next->prev = ep->prev;
1795 		if (ep->var)
1796 			free(ep->var);
1797 		if (ep->value)
1798 			free(ep->value);
1799 		free(ep);
1800 	}
1801 	return NULL;
1802 }
1803 
1804 	struct env_lst *
1805 env_export(var, d)
1806 	unsigned char *var;
1807 	unsigned char *d;
1808 {
1809 	register struct env_lst *ep;
1810 
1811 	if ((ep = env_find(var)) != NULL)
1812 		ep->export = 1;
1813 	return NULL;
1814 }
1815 
1816 	struct env_lst *
1817 env_unexport(var, d)
1818 	unsigned char *var;
1819 	unsigned char *d;
1820 {
1821 	register struct env_lst *ep;
1822 
1823 	if ((ep = env_find(var)) != NULL)
1824 		ep->export = 0;
1825 	return NULL;
1826 }
1827 
1828 	struct env_lst *
1829 env_send(var, d)
1830 	unsigned char *var;
1831 	unsigned char *d;
1832 {
1833 	register struct env_lst *ep;
1834 
1835 	if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1836 #ifdef	OLD_ENVIRON
1837 	    && my_state_is_wont(TELOPT_OLD_ENVIRON)
1838 #endif
1839 		) {
1840 		fprintf(stderr,
1841 		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1842 									var);
1843 		return NULL;
1844 	}
1845 	ep = env_find(var);
1846 	if (ep == 0) {
1847 		fprintf(stderr, "Cannot send '%s': variable not defined\n",
1848 									var);
1849 		return NULL;
1850 	}
1851 	env_opt_start_info();
1852 	env_opt_add(ep->var);
1853 	env_opt_end(0);
1854 	return NULL;
1855 }
1856 
1857 	struct env_lst *
1858 env_list(d1, d2)
1859 	unsigned char *d1, *d2;
1860 {
1861 	register struct env_lst *ep;
1862 
1863 	for (ep = envlisthead.next; ep; ep = ep->next) {
1864 		printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1865 					ep->var, ep->value);
1866 	}
1867 	return NULL;
1868 }
1869 
1870 	unsigned char *
1871 env_default(init, welldefined)
1872 	int init;
1873 {
1874 	static struct env_lst *nep = NULL;
1875 
1876 	if (init) {
1877 		nep = &envlisthead;
1878 		return NULL;
1879 	}
1880 	if (nep) {
1881 		while ((nep = nep->next) != NULL) {
1882 			if (nep->export && (nep->welldefined == welldefined))
1883 				return(nep->var);
1884 		}
1885 	}
1886 	return(NULL);
1887 }
1888 
1889 	unsigned char *
1890 env_getvalue(var)
1891 	unsigned char *var;
1892 {
1893 	register struct env_lst *ep;
1894 
1895 	if ((ep = env_find(var)) != NULL)
1896 		return(ep->value);
1897 	return(NULL);
1898 }
1899 
1900 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1901 	void
1902 env_varval(what)
1903 	unsigned char *what;
1904 {
1905 	extern int old_env_var, old_env_value, env_auto;
1906 	int len = strlen((char *)what);
1907 
1908 	if (len == 0)
1909 		goto unknown;
1910 
1911 	if (strncasecmp((char *)what, "status", len) == 0) {
1912 		if (env_auto)
1913 			printf("%s%s", "VAR and VALUE are/will be ",
1914 					"determined automatically\n");
1915 		if (old_env_var == OLD_ENV_VAR)
1916 			printf("VAR and VALUE set to correct definitions\n");
1917 		else
1918 			printf("VAR and VALUE definitions are reversed\n");
1919 	} else if (strncasecmp((char *)what, "auto", len) == 0) {
1920 		env_auto = 1;
1921 		old_env_var = OLD_ENV_VALUE;
1922 		old_env_value = OLD_ENV_VAR;
1923 	} else if (strncasecmp((char *)what, "right", len) == 0) {
1924 		env_auto = 0;
1925 		old_env_var = OLD_ENV_VAR;
1926 		old_env_value = OLD_ENV_VALUE;
1927 	} else if (strncasecmp((char *)what, "wrong", len) == 0) {
1928 		env_auto = 0;
1929 		old_env_var = OLD_ENV_VALUE;
1930 		old_env_value = OLD_ENV_VAR;
1931 	} else {
1932 unknown:
1933 		printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1934 	}
1935 }
1936 #endif
1937 
1938 #if	defined(AUTHENTICATION)
1939 /*
1940  * The AUTHENTICATE command.
1941  */
1942 
1943 struct authlist {
1944 	char	*name;
1945 	char	*help;
1946 	int	(*handler)();
1947 	int	narg;
1948 };
1949 
1950 extern int
1951 	auth_enable P((char *)),
1952 	auth_disable P((char *)),
1953 	auth_status P((void));
1954 static int
1955 	auth_help P((void));
1956 
1957 struct authlist AuthList[] = {
1958     { "status",	"Display current status of authentication information",
1959 						auth_status,	0 },
1960     { "disable", "Disable an authentication type ('auth disable ?' for more)",
1961 						auth_disable,	1 },
1962     { "enable", "Enable an authentication type ('auth enable ?' for more)",
1963 						auth_enable,	1 },
1964     { "help",	0,				auth_help,		0 },
1965     { "?",	"Print help information",	auth_help,		0 },
1966     { 0 },
1967 };
1968 
1969     static int
1970 auth_help()
1971 {
1972     struct authlist *c;
1973 
1974     for (c = AuthList; c->name; c++) {
1975 	if (c->help) {
1976 	    if (*c->help)
1977 		printf("%-15s %s\n", c->name, c->help);
1978 	    else
1979 		printf("\n");
1980 	}
1981     }
1982     return 0;
1983 }
1984 
1985 auth_cmd(argc, argv)
1986     int  argc;
1987     char *argv[];
1988 {
1989     struct authlist *c;
1990 
1991     if (argc < 2) {
1992 	fprintf(stderr,
1993 	    "Need an argument to 'auth' command.  'auth ?' for help.\n");
1994 	return 0;
1995     }
1996 
1997     c = (struct authlist *)
1998 		genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1999     if (c == 0) {
2000 	fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
2001     				argv[1]);
2002 	return 0;
2003     }
2004     if (Ambiguous(c)) {
2005 	fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
2006     				argv[1]);
2007 	return 0;
2008     }
2009     if (c->narg + 2 != argc) {
2010 	fprintf(stderr,
2011 	    "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
2012 		c->narg < argc + 2 ? "only " : "",
2013 		c->narg, c->narg == 1 ? "" : "s", c->name);
2014 	return 0;
2015     }
2016     return((*c->handler)(argv[2], argv[3]));
2017 }
2018 #endif
2019 
2020 
2021 #if	defined(unix) && defined(TN3270)
2022     static void
2023 filestuff(fd)
2024     int fd;
2025 {
2026     int res;
2027 
2028 #ifdef	F_GETOWN
2029     setconnmode(0);
2030     res = fcntl(fd, F_GETOWN, 0);
2031     setcommandmode();
2032 
2033     if (res == -1) {
2034 	perror("fcntl");
2035 	return;
2036     }
2037     printf("\tOwner is %d.\n", res);
2038 #endif
2039 
2040     setconnmode(0);
2041     res = fcntl(fd, F_GETFL, 0);
2042     setcommandmode();
2043 
2044     if (res == -1) {
2045 	perror("fcntl");
2046 	return;
2047     }
2048 #ifdef notdef
2049     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2050 #endif
2051 }
2052 #endif /* defined(unix) && defined(TN3270) */
2053 
2054 /*
2055  * Print status about the connection.
2056  */
2057     /*ARGSUSED*/
2058     static int
2059 status(argc, argv)
2060     int	 argc;
2061     char *argv[];
2062 {
2063     if (connected) {
2064 	printf("Connected to %s.\n", hostname);
2065 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2066 	    int mode = getconnmode();
2067 
2068 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
2069 		printf("Operating with LINEMODE option\n");
2070 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2071 		printf("%s catching of signals\n",
2072 					(mode&MODE_TRAPSIG) ? "Local" : "No");
2073 		slcstate();
2074 #ifdef	KLUDGELINEMODE
2075 	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2076 		printf("Operating in obsolete linemode\n");
2077 #endif
2078 	    } else {
2079 		printf("Operating in single character mode\n");
2080 		if (localchars)
2081 		    printf("Catching signals locally\n");
2082 	    }
2083 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2084 	    if (my_want_state_is_will(TELOPT_LFLOW))
2085 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2086 	}
2087     } else {
2088 	printf("No connection.\n");
2089     }
2090 #   if !defined(TN3270)
2091     printf("Escape character is '%s'.\n", control(escape));
2092     (void) fflush(stdout);
2093 #   else /* !defined(TN3270) */
2094     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2095 	printf("Escape character is '%s'.\n", control(escape));
2096     }
2097 #   if defined(unix)
2098     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2099 	printf("SIGIO received %d time%s.\n",
2100 				sigiocount, (sigiocount == 1)? "":"s");
2101 	if (In3270) {
2102 	    printf("Process ID %d, process group %d.\n",
2103 					    getpid(), getpgrp());
2104 	    printf("Terminal input:\n");
2105 	    filestuff(tin);
2106 	    printf("Terminal output:\n");
2107 	    filestuff(tout);
2108 	    printf("Network socket:\n");
2109 	    filestuff(net);
2110 	}
2111     }
2112     if (In3270 && transcom) {
2113        printf("Transparent mode command is '%s'.\n", transcom);
2114     }
2115 #   endif /* defined(unix) */
2116     (void) fflush(stdout);
2117     if (In3270) {
2118 	return 0;
2119     }
2120 #   endif /* defined(TN3270) */
2121     return 1;
2122 }
2123 
2124 #ifdef	SIGINFO
2125 /*
2126  * Function that gets called when SIGINFO is received.
2127  */
2128 int
2129 ayt_status()
2130 {
2131     return call(status, "status", "notmuch", 0);
2132 }
2133 #endif
2134 
2135     int
2136 tn(argc, argv)
2137     int argc;
2138     char *argv[];
2139 {
2140     register struct hostent *host = 0;
2141     struct sockaddr_in sin;
2142     struct servent *sp = 0;
2143     unsigned long temp;
2144 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2145     char *srp = 0;
2146     unsigned long srlen;
2147 #endif
2148     char *cmd, *hostp = 0, *portp = 0, *user = 0;
2149 #ifdef __GNUC__	/* Avoid vfork clobbering */
2150     (void) &user;
2151 #endif
2152 
2153     /* clear the socket address prior to use */
2154     memset((char *)&sin, 0, sizeof(sin));
2155 
2156     if (connected) {
2157 	printf("?Already connected to %s\n", hostname);
2158 	setuid(getuid());
2159 	return 0;
2160     }
2161     if (argc < 2) {
2162 	(void) strcpy(line, "open ");
2163 	printf("(to) ");
2164 	(void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2165 	makeargv();
2166 	argc = margc;
2167 	argv = margv;
2168     }
2169     cmd = *argv;
2170     --argc; ++argv;
2171     while (argc) {
2172 	if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2173 	    goto usage;
2174 	if (strcmp(*argv, "-l") == 0) {
2175 	    --argc; ++argv;
2176 	    if (argc == 0)
2177 		goto usage;
2178 	    user = *argv++;
2179 	    --argc;
2180 	    continue;
2181 	}
2182 	if (strcmp(*argv, "-a") == 0) {
2183 	    --argc; ++argv;
2184 	    autologin = 1;
2185 	    continue;
2186 	}
2187 	if (hostp == 0) {
2188 	    hostp = *argv++;
2189 	    --argc;
2190 	    continue;
2191 	}
2192 	if (portp == 0) {
2193 	    portp = *argv++;
2194 	    --argc;
2195 	    continue;
2196 	}
2197     usage:
2198 	printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2199 	setuid(getuid());
2200 	return 0;
2201     }
2202     if (hostp == 0)
2203 	goto usage;
2204 
2205 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2206     if (hostp[0] == '@' || hostp[0] == '!') {
2207 	if ((hostname = strrchr(hostp, ':')) == NULL)
2208 	    hostname = strrchr(hostp, '@');
2209 	hostname++;
2210 	srp = 0;
2211 	temp = sourceroute(hostp, &srp, &srlen);
2212 	if (temp == 0) {
2213 	    herror(srp);
2214 	    setuid(getuid());
2215 	    return 0;
2216 	} else if (temp == -1) {
2217 	    printf("Bad source route option: %s\n", hostp);
2218 	    setuid(getuid());
2219 	    return 0;
2220 	} else {
2221 	    sin.sin_addr.s_addr = temp;
2222 	    sin.sin_family = AF_INET;
2223 	}
2224     } else {
2225 #endif
2226 	temp = inet_addr(hostp);
2227 	if (temp != INADDR_NONE) {
2228 	    sin.sin_addr.s_addr = temp;
2229 	    sin.sin_family = AF_INET;
2230 	    host = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
2231 	    if (host)
2232 	        (void) strcpy(_hostname, host->h_name);
2233 	    else
2234 	        (void) strcpy(_hostname, hostp);
2235 	    hostname = _hostname;
2236 	} else {
2237 	    host = gethostbyname(hostp);
2238 	    if (host) {
2239 		sin.sin_family = host->h_addrtype;
2240 #if	defined(h_addr)		/* In 4.3, this is a #define */
2241 		memmove((caddr_t)&sin.sin_addr,
2242 				host->h_addr_list[0], host->h_length);
2243 #else	/* defined(h_addr) */
2244 		memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2245 #endif	/* defined(h_addr) */
2246 		strncpy(_hostname, host->h_name, sizeof(_hostname));
2247 		_hostname[sizeof(_hostname)-1] = '\0';
2248 		hostname = _hostname;
2249 	    } else {
2250 		herror(hostp);
2251 		setuid(getuid());
2252 		return 0;
2253 	    }
2254 	}
2255 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2256     }
2257 #endif
2258     if (portp) {
2259 	if (*portp == '-') {
2260 	    portp++;
2261 	    telnetport = 1;
2262 	} else
2263 	    telnetport = 0;
2264 	sin.sin_port = atoi(portp);
2265 	if (sin.sin_port == 0) {
2266 	    sp = getservbyname(portp, "tcp");
2267 	    if (sp)
2268 		sin.sin_port = sp->s_port;
2269 	    else {
2270 		printf("%s: bad port number\n", portp);
2271 		setuid(getuid());
2272 		return 0;
2273 	    }
2274 	} else {
2275 #if	!defined(htons)
2276 	    u_short htons P((unsigned short));
2277 #endif	/* !defined(htons) */
2278 	    sin.sin_port = htons(sin.sin_port);
2279 	}
2280     } else {
2281 	if (sp == 0) {
2282 	    sp = getservbyname("telnet", "tcp");
2283 	    if (sp == 0) {
2284 		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2285 		setuid(getuid());
2286 		return 0;
2287 	    }
2288 	    sin.sin_port = sp->s_port;
2289 	}
2290 	telnetport = 1;
2291     }
2292     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2293     do {
2294 	net = socket(AF_INET, SOCK_STREAM, 0);
2295 	setuid(getuid());
2296 	if (net < 0) {
2297 	    perror("telnet: socket");
2298 	    return 0;
2299 	}
2300 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2301 	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2302 		perror("setsockopt (IP_OPTIONS)");
2303 #endif
2304 #if	defined(IPPROTO_IP) && defined(IP_TOS)
2305 	{
2306 # if	defined(HAS_GETTOS)
2307 	    struct tosent *tp;
2308 	    if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2309 		tos = tp->t_tos;
2310 # endif
2311 	    if (tos < 0)
2312 		tos = IPTOS_LOWDELAY;	/* Low Delay bit */
2313 	    if (tos
2314 		&& (setsockopt(net, IPPROTO_IP, IP_TOS,
2315 		    (char *)&tos, sizeof(int)) < 0)
2316 		&& (errno != ENOPROTOOPT))
2317 		    perror("telnet: setsockopt (IP_TOS) (ignored)");
2318 	}
2319 #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
2320 
2321 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2322 		perror("setsockopt (SO_DEBUG)");
2323 	}
2324 
2325 	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2326 #if	defined(h_addr)		/* In 4.3, this is a #define */
2327 	    if (host && host->h_addr_list[1]) {
2328 		int oerrno = errno;
2329 
2330 		fprintf(stderr, "telnet: connect to address %s: ",
2331 						inet_ntoa(sin.sin_addr));
2332 		errno = oerrno;
2333 		perror((char *)0);
2334 		host->h_addr_list++;
2335 		memmove((caddr_t)&sin.sin_addr,
2336 			host->h_addr_list[0], host->h_length);
2337 		(void) NetClose(net);
2338 		continue;
2339 	    }
2340 #endif	/* defined(h_addr) */
2341 	    perror("telnet: Unable to connect to remote host");
2342 	    return 0;
2343 	}
2344 	connected++;
2345 #if	defined(AUTHENTICATION)
2346 	auth_encrypt_connect(connected);
2347 #endif	/* defined(AUTHENTICATION) */
2348     } while (connected == 0);
2349     cmdrc(hostp, hostname);
2350     if (autologin && user == NULL) {
2351 	struct passwd *pw;
2352 
2353 	user = getenv("USER");
2354 	if (user == NULL ||
2355 	    ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2356 		if ((pw = getpwuid(getuid())) != NULL)
2357 			user = pw->pw_name;
2358 		else
2359 			user = NULL;
2360 	}
2361     }
2362     if (user) {
2363 	env_define((unsigned char *)"USER", (unsigned char *)user);
2364 	env_export((unsigned char *)"USER", NULL);
2365     }
2366     (void) call(status, "status", "notmuch", 0);
2367     if (setjmp(peerdied) == 0)
2368 	telnet(user);
2369     (void) NetClose(net);
2370     ExitString("Connection closed by foreign host.\n",1);
2371     /*NOTREACHED*/
2372 }
2373 
2374 #define HELPINDENT ((int)sizeof ("connect"))
2375 
2376 static char
2377 	openhelp[] =	"connect to a site",
2378 	closehelp[] =	"close current connection",
2379 	logouthelp[] =	"forcibly logout remote user and close the connection",
2380 	quithelp[] =	"exit telnet",
2381 	statushelp[] =	"print status information",
2382 	helphelp[] =	"print help information",
2383 	sendhelp[] =	"transmit special characters ('send ?' for more)",
2384 	sethelp[] = 	"set operating parameters ('set ?' for more)",
2385 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
2386 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2387 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
2388 	displayhelp[] =	"display operating parameters",
2389 #if	defined(TN3270) && defined(unix)
2390 	transcomhelp[] = "specify Unix command for transparent mode pipe",
2391 #endif	/* defined(TN3270) && defined(unix) */
2392 #if	defined(AUTHENTICATION)
2393 	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",
2394 #endif
2395 #if	defined(unix)
2396 	zhelp[] =	"suspend telnet",
2397 #endif	/* defined(unix) */
2398 	shellhelp[] =	"invoke a subshell",
2399 	envhelp[] =	"change environment variables ('environ ?' for more)",
2400 	modestring[] = "try to enter line or character mode ('mode ?' for more)";
2401 
2402 static Command cmdtab[] = {
2403 	{ "close",	closehelp,	bye,		1 },
2404 	{ "logout",	logouthelp,	logout,		1 },
2405 	{ "display",	displayhelp,	display,	0 },
2406 	{ "mode",	modestring,	modecmd,	0 },
2407 	{ "open",	openhelp,	tn,		0 },
2408 	{ "quit",	quithelp,	quit,		0 },
2409 	{ "send",	sendhelp,	sendcmd,	0 },
2410 	{ "set",	sethelp,	setcmd,		0 },
2411 	{ "unset",	unsethelp,	unsetcmd,	0 },
2412 	{ "status",	statushelp,	status,		0 },
2413 	{ "toggle",	togglestring,	toggle,		0 },
2414 	{ "slc",	slchelp,	slccmd,		0 },
2415 #if	defined(TN3270) && defined(unix)
2416 	{ "transcom",	transcomhelp,	settranscom,	0 },
2417 #endif	/* defined(TN3270) && defined(unix) */
2418 #if	defined(AUTHENTICATION)
2419 	{ "auth",	authhelp,	auth_cmd,	0 },
2420 #endif
2421 #if	defined(unix)
2422 	{ "z",		zhelp,		suspend,	0 },
2423 #endif	/* defined(unix) */
2424 #if	defined(TN3270)
2425 	{ "!",		shellhelp,	shell,		1 },
2426 #else
2427 	{ "!",		shellhelp,	shell,		0 },
2428 #endif
2429 	{ "environ",	envhelp,	env_cmd,	0 },
2430 	{ "?",		helphelp,	help,		0 },
2431 	{ NULL,		NULL,		NULL,		0 }
2432 };
2433 
2434 static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
2435 static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
2436 
2437 static Command cmdtab2[] = {
2438 	{ "help",	0,		help,		0 },
2439 	{ "escape",	escapehelp,	setescape,	0 },
2440 	{ "crmod",	crmodhelp,	togcrmod,	0 },
2441 	{ NULL,		NULL,		NULL,		0 }
2442 };
2443 
2444 
2445 /*
2446  * Call routine with argc, argv set from args (terminated by 0).
2447  */
2448 
2449     /*VARARGS1*/
2450     static int
2451 #ifdef __STDC__
2452 call(intrtn_t routine, ...)
2453 #else
2454 call(va_alist)
2455     va_dcl
2456 #endif
2457 {
2458     va_list ap;
2459     char *args[100];
2460     int argno = 0;
2461 #ifndef __STDC__
2462     intrtn_t routine;
2463 
2464     va_start(ap);
2465     routine = (va_arg(ap, intrtn_t));
2466 #else
2467     va_start(ap, routine);
2468 #endif
2469 
2470     while ((args[argno++] = va_arg(ap, char *)) != 0) {
2471 	;
2472     }
2473     va_end(ap);
2474     return (*routine)(argno-1, args);
2475 }
2476 
2477 
2478     static Command *
2479 getcmd(name)
2480     char *name;
2481 {
2482     Command *cm;
2483 
2484     if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))) != NULL)
2485 	return cm;
2486     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2487 }
2488 
2489     void
2490 command(top, tbuf, cnt)
2491     int top;
2492     char *tbuf;
2493     int cnt;
2494 {
2495     register Command *c;
2496 
2497     setcommandmode();
2498     if (!top) {
2499 	putchar('\n');
2500 #if	defined(unix)
2501     } else {
2502 	(void) signal(SIGINT, SIG_DFL);
2503 	(void) signal(SIGQUIT, SIG_DFL);
2504 #endif	/* defined(unix) */
2505     }
2506     for (;;) {
2507 	if (rlogin == _POSIX_VDISABLE)
2508 		printf("%s> ", prompt);
2509 	if (tbuf) {
2510 	    register char *cp;
2511 	    cp = line;
2512 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2513 		cnt--;
2514 	    tbuf = 0;
2515 	    if (cp == line || *--cp != '\n' || cp == line)
2516 		goto getline;
2517 	    *cp = '\0';
2518 	    if (rlogin == _POSIX_VDISABLE)
2519 		printf("%s\n", line);
2520 	} else {
2521 	getline:
2522 	    if (rlogin != _POSIX_VDISABLE)
2523 		printf("%s> ", prompt);
2524 	    if (fgets(line, sizeof(line), stdin) == NULL) {
2525 		if (feof(stdin) || ferror(stdin)) {
2526 		    (void) quit(0, NULL);
2527 		    /*NOTREACHED*/
2528 		}
2529 		break;
2530 	    }
2531 	}
2532 	if (line[0] == 0)
2533 	    break;
2534 	makeargv();
2535 	if (margv[0] == 0) {
2536 	    break;
2537 	}
2538 	c = getcmd(margv[0]);
2539 	if (Ambiguous(c)) {
2540 	    printf("?Ambiguous command\n");
2541 	    continue;
2542 	}
2543 	if (c == 0) {
2544 	    printf("?Invalid command\n");
2545 	    continue;
2546 	}
2547 	if (c->needconnect && !connected) {
2548 	    printf("?Need to be connected first.\n");
2549 	    continue;
2550 	}
2551 	if ((*c->handler)(margc, margv)) {
2552 	    break;
2553 	}
2554     }
2555     if (!top) {
2556 	if (!connected) {
2557 	    longjmp(toplevel, 1);
2558 	    /*NOTREACHED*/
2559 	}
2560 #if	defined(TN3270)
2561 	if (shell_active == 0) {
2562 	    setconnmode(0);
2563 	}
2564 #else	/* defined(TN3270) */
2565 	setconnmode(0);
2566 #endif	/* defined(TN3270) */
2567     }
2568 }
2569 
2570 /*
2571  * Help command.
2572  */
2573 	static int
2574 help(argc, argv)
2575 	int argc;
2576 	char *argv[];
2577 {
2578 	register Command *c;
2579 
2580 	if (argc == 1) {
2581 		printf("Commands may be abbreviated.  Commands are:\n\n");
2582 		for (c = cmdtab; c->name; c++)
2583 			if (c->help) {
2584 				printf("%-*s\t%s\n", HELPINDENT, c->name,
2585 								    c->help);
2586 			}
2587 		return 0;
2588 	}
2589 	while (--argc > 0) {
2590 		register char *arg;
2591 		arg = *++argv;
2592 		c = getcmd(arg);
2593 		if (Ambiguous(c))
2594 			printf("?Ambiguous help command %s\n", arg);
2595 		else if (c == (Command *)0)
2596 			printf("?Invalid help command %s\n", arg);
2597 		else
2598 			printf("%s\n", c->help);
2599 	}
2600 	return 0;
2601 }
2602 
2603 static char *rcname = 0;
2604 static char rcbuf[128];
2605 
2606 void
2607 cmdrc(m1, m2)
2608 	char *m1, *m2;
2609 {
2610     register Command *c;
2611     FILE *rcfile;
2612     int gotmachine = 0;
2613     int l1 = strlen(m1);
2614     int l2 = strlen(m2);
2615     char m1save[64];
2616 
2617     if (skiprc)
2618 	return;
2619 
2620     strcpy(m1save, m1);
2621     m1 = m1save;
2622 
2623     if (rcname == 0) {
2624 	rcname = getenv("HOME");
2625 	if (rcname)
2626 	    strcpy(rcbuf, rcname);
2627 	else
2628 	    rcbuf[0] = '\0';
2629 	strcat(rcbuf, "/.telnetrc");
2630 	rcname = rcbuf;
2631     }
2632 
2633     if ((rcfile = fopen(rcname, "r")) == 0) {
2634 	return;
2635     }
2636 
2637     for (;;) {
2638 	if (fgets(line, sizeof(line), rcfile) == NULL)
2639 	    break;
2640 	if (line[0] == 0)
2641 	    break;
2642 	if (line[0] == '#')
2643 	    continue;
2644 	if (gotmachine) {
2645 	    if (!isspace(line[0]))
2646 		gotmachine = 0;
2647 	}
2648 	if (gotmachine == 0) {
2649 	    if (isspace(line[0]))
2650 		continue;
2651 	    if (strncasecmp(line, m1, l1) == 0)
2652 		strncpy(line, &line[l1], sizeof(line) - l1);
2653 	    else if (strncasecmp(line, m2, l2) == 0)
2654 		strncpy(line, &line[l2], sizeof(line) - l2);
2655 	    else if (strncasecmp(line, "DEFAULT", 7) == 0)
2656 		strncpy(line, &line[7], sizeof(line) - 7);
2657 	    else
2658 		continue;
2659 	    if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2660 		continue;
2661 	    gotmachine = 1;
2662 	}
2663 	makeargv();
2664 	if (margv[0] == 0)
2665 	    continue;
2666 	c = getcmd(margv[0]);
2667 	if (Ambiguous(c)) {
2668 	    printf("?Ambiguous command: %s\n", margv[0]);
2669 	    continue;
2670 	}
2671 	if (c == 0) {
2672 	    printf("?Invalid command: %s\n", margv[0]);
2673 	    continue;
2674 	}
2675 	/*
2676 	 * This should never happen...
2677 	 */
2678 	if (c->needconnect && !connected) {
2679 	    printf("?Need to be connected first for %s.\n", margv[0]);
2680 	    continue;
2681 	}
2682 	(*c->handler)(margc, margv);
2683     }
2684     fclose(rcfile);
2685 }
2686 
2687 #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
2688 
2689 /*
2690  * Source route is handed in as
2691  *	[!]@hop1@hop2...[@|:]dst
2692  * If the leading ! is present, it is a
2693  * strict source route, otherwise it is
2694  * assmed to be a loose source route.
2695  *
2696  * We fill in the source route option as
2697  *	hop1,hop2,hop3...dest
2698  * and return a pointer to hop1, which will
2699  * be the address to connect() to.
2700  *
2701  * Arguments:
2702  *	arg:	pointer to route list to decipher
2703  *
2704  *	cpp: 	If *cpp is not equal to NULL, this is a
2705  *		pointer to a pointer to a character array
2706  *		that should be filled in with the option.
2707  *
2708  *	lenp:	pointer to an integer that contains the
2709  *		length of *cpp if *cpp != NULL.
2710  *
2711  * Return values:
2712  *
2713  *	Returns the address of the host to connect to.  If the
2714  *	return value is -1, there was a syntax error in the
2715  *	option, either unknown characters, or too many hosts.
2716  *	If the return value is 0, one of the hostnames in the
2717  *	path is unknown, and *cpp is set to point to the bad
2718  *	hostname.
2719  *
2720  *	*cpp:	If *cpp was equal to NULL, it will be filled
2721  *		in with a pointer to our static area that has
2722  *		the option filled in.  This will be 32bit aligned.
2723  *
2724  *	*lenp:	This will be filled in with how long the option
2725  *		pointed to by *cpp is.
2726  *
2727  */
2728 	unsigned long
2729 sourceroute(arg, cpp, lenp)
2730 	char	*arg;
2731 	char	**cpp;
2732 	unsigned long *lenp;
2733 {
2734 	static char lsr[44];
2735 #ifdef	sysV88
2736 	static IOPTN ipopt;
2737 #endif
2738 	char *cp, *cp2, *lsrp, *lsrep;
2739 	register int tmp;
2740 	struct in_addr sin_addr;
2741 	register struct hostent *host = 0;
2742 	register char c;
2743 
2744 	/*
2745 	 * Verify the arguments, and make sure we have
2746 	 * at least 7 bytes for the option.
2747 	 */
2748 	if (cpp == NULL || lenp == NULL)
2749 		return((unsigned long)-1);
2750 	if (*cpp != NULL && *lenp < 7)
2751 		return((unsigned long)-1);
2752 	/*
2753 	 * Decide whether we have a buffer passed to us,
2754 	 * or if we need to use our own static buffer.
2755 	 */
2756 	if (*cpp) {
2757 		lsrp = *cpp;
2758 		lsrep = lsrp + *lenp;
2759 	} else {
2760 		*cpp = lsrp = lsr;
2761 		lsrep = lsrp + 44;
2762 	}
2763 
2764 	cp = arg;
2765 
2766 	/*
2767 	 * Next, decide whether we have a loose source
2768 	 * route or a strict source route, and fill in
2769 	 * the begining of the option.
2770 	 */
2771 #ifndef	sysV88
2772 	if (*cp == '!') {
2773 		cp++;
2774 		*lsrp++ = IPOPT_SSRR;
2775 	} else
2776 		*lsrp++ = IPOPT_LSRR;
2777 #else
2778 	if (*cp == '!') {
2779 		cp++;
2780 		ipopt.io_type = IPOPT_SSRR;
2781 	} else
2782 		ipopt.io_type = IPOPT_LSRR;
2783 #endif
2784 
2785 	if (*cp != '@')
2786 		return((unsigned long)-1);
2787 
2788 #ifndef	sysV88
2789 	lsrp++;		/* skip over length, we'll fill it in later */
2790 	*lsrp++ = 4;
2791 #endif
2792 
2793 	cp++;
2794 
2795 	sin_addr.s_addr = 0;
2796 
2797 	for (c = 0;;) {
2798 		if (c == ':')
2799 			cp2 = 0;
2800 		else for (cp2 = cp; (c = *cp2) != '\0'; cp2++) {
2801 			if (c == ',') {
2802 				*cp2++ = '\0';
2803 				if (*cp2 == '@')
2804 					cp2++;
2805 			} else if (c == '@') {
2806 				*cp2++ = '\0';
2807 			} else if (c == ':') {
2808 				*cp2++ = '\0';
2809 			} else
2810 				continue;
2811 			break;
2812 		}
2813 		if (!c)
2814 			cp2 = 0;
2815 
2816 		if ((tmp = inet_addr(cp)) != INADDR_NONE) {
2817 			sin_addr.s_addr = tmp;
2818 		} else if ((host = gethostbyname(cp)) != NULL) {
2819 #if	defined(h_addr)
2820 			memmove((caddr_t)&sin_addr,
2821 				host->h_addr_list[0], host->h_length);
2822 #else
2823 			memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
2824 #endif
2825 		} else {
2826 			*cpp = cp;
2827 			return(0);
2828 		}
2829 		memmove(lsrp, (char *)&sin_addr, 4);
2830 		lsrp += 4;
2831 		if (cp2)
2832 			cp = cp2;
2833 		else
2834 			break;
2835 		/*
2836 		 * Check to make sure there is space for next address
2837 		 */
2838 		if (lsrp + 4 > lsrep)
2839 			return((unsigned long)-1);
2840 	}
2841 #ifndef	sysV88
2842 	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2843 		*cpp = 0;
2844 		*lenp = 0;
2845 		return((unsigned long)-1);
2846 	}
2847 	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2848 	*lenp = lsrp - *cpp;
2849 #else
2850 	ipopt.io_len = lsrp - *cpp;
2851 	if (ipopt.io_len <= 5) {		/* Is 3 better ? */
2852 		*cpp = 0;
2853 		*lenp = 0;
2854 		return((unsigned long)-1);
2855 	}
2856 	*lenp = sizeof(ipopt);
2857 	*cpp = (char *) &ipopt;
2858 #endif
2859 	return(sin_addr.s_addr);
2860 }
2861 #endif
2862