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