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