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