xref: /freebsd/contrib/telnet/telnet/utilities.c (revision a0ee8cc6)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)utilities.c	8.3 (Berkeley) 5/30/95";
37 #endif
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #define	TELOPTS
43 #define	TELCMDS
44 #define	SLC_NAMES
45 #include <arpa/telnet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 
53 #include "general.h"
54 
55 #include "fdset.h"
56 
57 #include "ring.h"
58 
59 #include "defines.h"
60 
61 #include "externs.h"
62 
63 #ifdef	AUTHENTICATION
64 #include <libtelnet/auth.h>
65 #endif
66 #ifdef	ENCRYPTION
67 #include <libtelnet/encrypt.h>
68 #endif
69 
70 FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */
71 int	prettydump;
72 
73 /*
74  * upcase()
75  *
76  *	Upcase (in place) the argument.
77  */
78 
79 void
80 upcase(char *argument)
81 {
82     int c;
83 
84     while ((c = *argument) != 0) {
85 	if (islower(c)) {
86 	    *argument = toupper(c);
87 	}
88 	argument++;
89     }
90 }
91 
92 /*
93  * SetSockOpt()
94  *
95  * Compensate for differences in 4.2 and 4.3 systems.
96  */
97 
98 int
99 SetSockOpt(int fd, int level, int option, int yesno)
100 {
101     return setsockopt(fd, level, option,
102 				(char *)&yesno, sizeof yesno);
103 }
104 
105 /*
106  * The following are routines used to print out debugging information.
107  */
108 
109 unsigned char NetTraceFile[256] = "(standard output)";
110 
111 void
112 SetNetTrace(char *file)
113 {
114     if (NetTrace && NetTrace != stdout)
115 	fclose(NetTrace);
116     if (file  && (strcmp(file, "-") != 0)) {
117 	NetTrace = fopen(file, "w");
118 	if (NetTrace) {
119 	    strcpy((char *)NetTraceFile, file);
120 	    return;
121 	}
122 	fprintf(stderr, "Cannot open %s.\n", file);
123     }
124     NetTrace = stdout;
125     strcpy((char *)NetTraceFile, "(standard output)");
126 }
127 
128 void
129 Dump(char direction, unsigned char *buffer, int length)
130 {
131 #   define BYTES_PER_LINE	32
132 #   define min(x,y)	((x<y)? x:y)
133     unsigned char *pThis;
134     int offset;
135 
136     offset = 0;
137 
138     while (length) {
139 	/* print one line */
140 	fprintf(NetTrace, "%c 0x%x\t", direction, offset);
141 	pThis = buffer;
142 	if (prettydump) {
143 	    buffer = buffer + min(length, BYTES_PER_LINE/2);
144 	    while (pThis < buffer) {
145 		fprintf(NetTrace, "%c%.2x",
146 		    (((*pThis)&0xff) == 0xff) ? '*' : ' ',
147 		    (*pThis)&0xff);
148 		pThis++;
149 	    }
150 	    length -= BYTES_PER_LINE/2;
151 	    offset += BYTES_PER_LINE/2;
152 	} else {
153 	    buffer = buffer + min(length, BYTES_PER_LINE);
154 	    while (pThis < buffer) {
155 		fprintf(NetTrace, "%.2x", (*pThis)&0xff);
156 		pThis++;
157 	    }
158 	    length -= BYTES_PER_LINE;
159 	    offset += BYTES_PER_LINE;
160 	}
161 	if (NetTrace == stdout) {
162 	    fprintf(NetTrace, "\r\n");
163 	} else {
164 	    fprintf(NetTrace, "\n");
165 	}
166 	if (length < 0) {
167 	    fflush(NetTrace);
168 	    return;
169 	}
170 	/* find next unique line */
171     }
172     fflush(NetTrace);
173 }
174 
175 
176 void
177 printoption(const char *direction, int cmd, int option)
178 {
179 	if (!showoptions)
180 		return;
181 	if (cmd == IAC) {
182 		if (TELCMD_OK(option))
183 		    fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
184 		else
185 		    fprintf(NetTrace, "%s IAC %d", direction, option);
186 	} else {
187 		const char *fmt;
188 		fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
189 			(cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
190 		if (fmt) {
191 		    fprintf(NetTrace, "%s %s ", direction, fmt);
192 		    if (TELOPT_OK(option))
193 			fprintf(NetTrace, "%s", TELOPT(option));
194 		    else if (option == TELOPT_EXOPL)
195 			fprintf(NetTrace, "EXOPL");
196 		    else
197 			fprintf(NetTrace, "%d", option);
198 		} else
199 		    fprintf(NetTrace, "%s %d %d", direction, cmd, option);
200 	}
201 	if (NetTrace == stdout) {
202 	    fprintf(NetTrace, "\r\n");
203 	    fflush(NetTrace);
204 	} else {
205 	    fprintf(NetTrace, "\n");
206 	}
207 	return;
208 }
209 
210 void
211 optionstatus(void)
212 {
213     int i;
214     extern char will_wont_resp[], do_dont_resp[];
215 
216     for (i = 0; i < 256; i++) {
217 	if (do_dont_resp[i]) {
218 	    if (TELOPT_OK(i))
219 		printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
220 	    else if (TELCMD_OK(i))
221 		printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
222 	    else
223 		printf("resp DO_DONT %d: %d\n", i,
224 				do_dont_resp[i]);
225 	    if (my_want_state_is_do(i)) {
226 		if (TELOPT_OK(i))
227 		    printf("want DO   %s\n", TELOPT(i));
228 		else if (TELCMD_OK(i))
229 		    printf("want DO   %s\n", TELCMD(i));
230 		else
231 		    printf("want DO   %d\n", i);
232 	    } else {
233 		if (TELOPT_OK(i))
234 		    printf("want DONT %s\n", TELOPT(i));
235 		else if (TELCMD_OK(i))
236 		    printf("want DONT %s\n", TELCMD(i));
237 		else
238 		    printf("want DONT %d\n", i);
239 	    }
240 	} else {
241 	    if (my_state_is_do(i)) {
242 		if (TELOPT_OK(i))
243 		    printf("     DO   %s\n", TELOPT(i));
244 		else if (TELCMD_OK(i))
245 		    printf("     DO   %s\n", TELCMD(i));
246 		else
247 		    printf("     DO   %d\n", i);
248 	    }
249 	}
250 	if (will_wont_resp[i]) {
251 	    if (TELOPT_OK(i))
252 		printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
253 	    else if (TELCMD_OK(i))
254 		printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
255 	    else
256 		printf("resp WILL_WONT %d: %d\n",
257 				i, will_wont_resp[i]);
258 	    if (my_want_state_is_will(i)) {
259 		if (TELOPT_OK(i))
260 		    printf("want WILL %s\n", TELOPT(i));
261 		else if (TELCMD_OK(i))
262 		    printf("want WILL %s\n", TELCMD(i));
263 		else
264 		    printf("want WILL %d\n", i);
265 	    } else {
266 		if (TELOPT_OK(i))
267 		    printf("want WONT %s\n", TELOPT(i));
268 		else if (TELCMD_OK(i))
269 		    printf("want WONT %s\n", TELCMD(i));
270 		else
271 		    printf("want WONT %d\n", i);
272 	    }
273 	} else {
274 	    if (my_state_is_will(i)) {
275 		if (TELOPT_OK(i))
276 		    printf("     WILL %s\n", TELOPT(i));
277 		else if (TELCMD_OK(i))
278 		    printf("     WILL %s\n", TELCMD(i));
279 		else
280 		    printf("     WILL %d\n", i);
281 	    }
282 	}
283     }
284 
285 }
286 
287 void
288 printsub(char direction, unsigned char *pointer, int length)
289 {
290     int i;
291 #ifdef	AUTHENTICATION
292     char buf[512];
293 #endif
294     extern int want_status_response;
295 
296     if (showoptions || direction == 0 ||
297 	(want_status_response && (pointer[0] == TELOPT_STATUS))) {
298 	if (direction) {
299 	    fprintf(NetTrace, "%s IAC SB ",
300 				(direction == '<')? "RCVD":"SENT");
301 	    if (length >= 3) {
302 		int j;
303 
304 		i = pointer[length-2];
305 		j = pointer[length-1];
306 
307 		if (i != IAC || j != SE) {
308 		    fprintf(NetTrace, "(terminated by ");
309 		    if (TELOPT_OK(i))
310 			fprintf(NetTrace, "%s ", TELOPT(i));
311 		    else if (TELCMD_OK(i))
312 			fprintf(NetTrace, "%s ", TELCMD(i));
313 		    else
314 			fprintf(NetTrace, "%d ", i);
315 		    if (TELOPT_OK(j))
316 			fprintf(NetTrace, "%s", TELOPT(j));
317 		    else if (TELCMD_OK(j))
318 			fprintf(NetTrace, "%s", TELCMD(j));
319 		    else
320 			fprintf(NetTrace, "%d", j);
321 		    fprintf(NetTrace, ", not IAC SE!) ");
322 		}
323 	    }
324 	    length -= 2;
325 	}
326 	if (length < 1) {
327 	    fprintf(NetTrace, "(Empty suboption??\?)");
328 	    if (NetTrace == stdout)
329 		fflush(NetTrace);
330 	    return;
331 	}
332 	switch (pointer[0]) {
333 	case TELOPT_TTYPE:
334 	    fprintf(NetTrace, "TERMINAL-TYPE ");
335 	    switch (pointer[1]) {
336 	    case TELQUAL_IS:
337 		fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
338 		break;
339 	    case TELQUAL_SEND:
340 		fprintf(NetTrace, "SEND");
341 		break;
342 	    default:
343 		fprintf(NetTrace,
344 				"- unknown qualifier %d (0x%x).",
345 				pointer[1], pointer[1]);
346 	    }
347 	    break;
348 	case TELOPT_TSPEED:
349 	    fprintf(NetTrace, "TERMINAL-SPEED");
350 	    if (length < 2) {
351 		fprintf(NetTrace, " (empty suboption??\?)");
352 		break;
353 	    }
354 	    switch (pointer[1]) {
355 	    case TELQUAL_IS:
356 		fprintf(NetTrace, " IS ");
357 		fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
358 		break;
359 	    default:
360 		if (pointer[1] == 1)
361 		    fprintf(NetTrace, " SEND");
362 		else
363 		    fprintf(NetTrace, " %d (unknown)", pointer[1]);
364 		for (i = 2; i < length; i++)
365 		    fprintf(NetTrace, " ?%d?", pointer[i]);
366 		break;
367 	    }
368 	    break;
369 
370 	case TELOPT_LFLOW:
371 	    fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
372 	    if (length < 2) {
373 		fprintf(NetTrace, " (empty suboption??\?)");
374 		break;
375 	    }
376 	    switch (pointer[1]) {
377 	    case LFLOW_OFF:
378 		fprintf(NetTrace, " OFF"); break;
379 	    case LFLOW_ON:
380 		fprintf(NetTrace, " ON"); break;
381 	    case LFLOW_RESTART_ANY:
382 		fprintf(NetTrace, " RESTART-ANY"); break;
383 	    case LFLOW_RESTART_XON:
384 		fprintf(NetTrace, " RESTART-XON"); break;
385 	    default:
386 		fprintf(NetTrace, " %d (unknown)", pointer[1]);
387 	    }
388 	    for (i = 2; i < length; i++)
389 		fprintf(NetTrace, " ?%d?", pointer[i]);
390 	    break;
391 
392 	case TELOPT_NAWS:
393 	    fprintf(NetTrace, "NAWS");
394 	    if (length < 2) {
395 		fprintf(NetTrace, " (empty suboption??\?)");
396 		break;
397 	    }
398 	    if (length == 2) {
399 		fprintf(NetTrace, " ?%d?", pointer[1]);
400 		break;
401 	    }
402 	    fprintf(NetTrace, " %d %d (%d)",
403 		pointer[1], pointer[2],
404 		(int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
405 	    if (length == 4) {
406 		fprintf(NetTrace, " ?%d?", pointer[3]);
407 		break;
408 	    }
409 	    fprintf(NetTrace, " %d %d (%d)",
410 		pointer[3], pointer[4],
411 		(int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
412 	    for (i = 5; i < length; i++)
413 		fprintf(NetTrace, " ?%d?", pointer[i]);
414 	    break;
415 
416 #ifdef	AUTHENTICATION
417 	case TELOPT_AUTHENTICATION:
418 	    fprintf(NetTrace, "AUTHENTICATION");
419 	    if (length < 2) {
420 		fprintf(NetTrace, " (empty suboption??\?)");
421 		break;
422 	    }
423 	    switch (pointer[1]) {
424 	    case TELQUAL_REPLY:
425 	    case TELQUAL_IS:
426 		fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
427 							"IS" : "REPLY");
428 		if (AUTHTYPE_NAME_OK(pointer[2]))
429 		    fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
430 		else
431 		    fprintf(NetTrace, "%d ", pointer[2]);
432 		if (length < 3) {
433 		    fprintf(NetTrace, "(partial suboption??\?)");
434 		    break;
435 		}
436 		fprintf(NetTrace, "%s|%s",
437 			((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
438 			"CLIENT" : "SERVER",
439 			((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
440 			"MUTUAL" : "ONE-WAY");
441 
442 		auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
443 		fprintf(NetTrace, "%s", buf);
444 		break;
445 
446 	    case TELQUAL_SEND:
447 		i = 2;
448 		fprintf(NetTrace, " SEND ");
449 		while (i < length) {
450 		    if (AUTHTYPE_NAME_OK(pointer[i]))
451 			fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
452 		    else
453 			fprintf(NetTrace, "%d ", pointer[i]);
454 		    if (++i >= length) {
455 			fprintf(NetTrace, "(partial suboption??\?)");
456 			break;
457 		    }
458 		    fprintf(NetTrace, "%s|%s ",
459 			((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
460 							"CLIENT" : "SERVER",
461 			((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
462 							"MUTUAL" : "ONE-WAY");
463 		    ++i;
464 		}
465 		break;
466 
467 	    case TELQUAL_NAME:
468 		i = 2;
469 		fprintf(NetTrace, " NAME \"");
470 		while (i < length)
471 		    putc(pointer[i++], NetTrace);
472 		putc('"', NetTrace);
473 		break;
474 
475 	    default:
476 		    for (i = 2; i < length; i++)
477 			fprintf(NetTrace, " ?%d?", pointer[i]);
478 		    break;
479 	    }
480 	    break;
481 #endif
482 
483 #ifdef	ENCRYPTION
484 	case TELOPT_ENCRYPT:
485 	    fprintf(NetTrace, "ENCRYPT");
486 	    if (length < 2) {
487 		fprintf(NetTrace, " (empty suboption??\?)");
488 		break;
489 	    }
490 	    switch (pointer[1]) {
491 	    case ENCRYPT_START:
492 		fprintf(NetTrace, " START");
493 		break;
494 
495 	    case ENCRYPT_END:
496 		fprintf(NetTrace, " END");
497 		break;
498 
499 	    case ENCRYPT_REQSTART:
500 		fprintf(NetTrace, " REQUEST-START");
501 		break;
502 
503 	    case ENCRYPT_REQEND:
504 		fprintf(NetTrace, " REQUEST-END");
505 		break;
506 
507 	    case ENCRYPT_IS:
508 	    case ENCRYPT_REPLY:
509 		fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
510 							"IS" : "REPLY");
511 		if (length < 3) {
512 		    fprintf(NetTrace, " (partial suboption??\?)");
513 		    break;
514 		}
515 		if (ENCTYPE_NAME_OK(pointer[2]))
516 		    fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
517 		else
518 		    fprintf(NetTrace, " %d (unknown)", pointer[2]);
519 
520 		encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
521 		fprintf(NetTrace, "%s", buf);
522 		break;
523 
524 	    case ENCRYPT_SUPPORT:
525 		i = 2;
526 		fprintf(NetTrace, " SUPPORT ");
527 		while (i < length) {
528 		    if (ENCTYPE_NAME_OK(pointer[i]))
529 			fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
530 		    else
531 			fprintf(NetTrace, "%d ", pointer[i]);
532 		    i++;
533 		}
534 		break;
535 
536 	    case ENCRYPT_ENC_KEYID:
537 		fprintf(NetTrace, " ENC_KEYID ");
538 		goto encommon;
539 
540 	    case ENCRYPT_DEC_KEYID:
541 		fprintf(NetTrace, " DEC_KEYID ");
542 		goto encommon;
543 
544 	    default:
545 		fprintf(NetTrace, " %d (unknown)", pointer[1]);
546 	    encommon:
547 		for (i = 2; i < length; i++)
548 		    fprintf(NetTrace, " %d", pointer[i]);
549 		break;
550 	    }
551 	    break;
552 #endif	/* ENCRYPTION */
553 
554 	case TELOPT_LINEMODE:
555 	    fprintf(NetTrace, "LINEMODE ");
556 	    if (length < 2) {
557 		fprintf(NetTrace, " (empty suboption??\?)");
558 		break;
559 	    }
560 	    switch (pointer[1]) {
561 	    case WILL:
562 		fprintf(NetTrace, "WILL ");
563 		goto common;
564 	    case WONT:
565 		fprintf(NetTrace, "WONT ");
566 		goto common;
567 	    case DO:
568 		fprintf(NetTrace, "DO ");
569 		goto common;
570 	    case DONT:
571 		fprintf(NetTrace, "DONT ");
572 	    common:
573 		if (length < 3) {
574 		    fprintf(NetTrace, "(no option??\?)");
575 		    break;
576 		}
577 		switch (pointer[2]) {
578 		case LM_FORWARDMASK:
579 		    fprintf(NetTrace, "Forward Mask");
580 		    for (i = 3; i < length; i++)
581 			fprintf(NetTrace, " %x", pointer[i]);
582 		    break;
583 		default:
584 		    fprintf(NetTrace, "%d (unknown)", pointer[2]);
585 		    for (i = 3; i < length; i++)
586 			fprintf(NetTrace, " %d", pointer[i]);
587 		    break;
588 		}
589 		break;
590 
591 	    case LM_SLC:
592 		fprintf(NetTrace, "SLC");
593 		for (i = 2; i < length - 2; i += 3) {
594 		    if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
595 			fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
596 		    else
597 			fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
598 		    switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
599 		    case SLC_NOSUPPORT:
600 			fprintf(NetTrace, " NOSUPPORT"); break;
601 		    case SLC_CANTCHANGE:
602 			fprintf(NetTrace, " CANTCHANGE"); break;
603 		    case SLC_VARIABLE:
604 			fprintf(NetTrace, " VARIABLE"); break;
605 		    case SLC_DEFAULT:
606 			fprintf(NetTrace, " DEFAULT"); break;
607 		    }
608 		    fprintf(NetTrace, "%s%s%s",
609 			pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
610 			pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
611 			pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
612 		    if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
613 						SLC_FLUSHOUT| SLC_LEVELBITS))
614 			fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
615 		    fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
616 		    if ((pointer[i+SLC_VALUE] == IAC) &&
617 			(pointer[i+SLC_VALUE+1] == IAC))
618 				i++;
619 		}
620 		for (; i < length; i++)
621 		    fprintf(NetTrace, " ?%d?", pointer[i]);
622 		break;
623 
624 	    case LM_MODE:
625 		fprintf(NetTrace, "MODE ");
626 		if (length < 3) {
627 		    fprintf(NetTrace, "(no mode??\?)");
628 		    break;
629 		}
630 		{
631 		    char tbuf[64];
632 		    sprintf(tbuf, "%s%s%s%s%s",
633 			pointer[2]&MODE_EDIT ? "|EDIT" : "",
634 			pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
635 			pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
636 			pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
637 			pointer[2]&MODE_ACK ? "|ACK" : "");
638 		    fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
639 		}
640 		if (pointer[2]&~(MODE_MASK))
641 		    fprintf(NetTrace, " (0x%x)", pointer[2]);
642 		for (i = 3; i < length; i++)
643 		    fprintf(NetTrace, " ?0x%x?", pointer[i]);
644 		break;
645 	    default:
646 		fprintf(NetTrace, "%d (unknown)", pointer[1]);
647 		for (i = 2; i < length; i++)
648 		    fprintf(NetTrace, " %d", pointer[i]);
649 	    }
650 	    break;
651 
652 	case TELOPT_STATUS: {
653 	    const char *cp;
654 	    int j, k;
655 
656 	    fprintf(NetTrace, "STATUS");
657 
658 	    switch (pointer[1]) {
659 	    default:
660 		if (pointer[1] == TELQUAL_SEND)
661 		    fprintf(NetTrace, " SEND");
662 		else
663 		    fprintf(NetTrace, " %d (unknown)", pointer[1]);
664 		for (i = 2; i < length; i++)
665 		    fprintf(NetTrace, " ?%d?", pointer[i]);
666 		break;
667 	    case TELQUAL_IS:
668 		if (--want_status_response < 0)
669 		    want_status_response = 0;
670 		if (NetTrace == stdout)
671 		    fprintf(NetTrace, " IS\r\n");
672 		else
673 		    fprintf(NetTrace, " IS\n");
674 
675 		for (i = 2; i < length; i++) {
676 		    switch(pointer[i]) {
677 		    case DO:	cp = "DO"; goto common2;
678 		    case DONT:	cp = "DONT"; goto common2;
679 		    case WILL:	cp = "WILL"; goto common2;
680 		    case WONT:	cp = "WONT"; goto common2;
681 		    common2:
682 			i++;
683 			if (TELOPT_OK((int)pointer[i]))
684 			    fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
685 			else
686 			    fprintf(NetTrace, " %s %d", cp, pointer[i]);
687 
688 			if (NetTrace == stdout)
689 			    fprintf(NetTrace, "\r\n");
690 			else
691 			    fprintf(NetTrace, "\n");
692 			break;
693 
694 		    case SB:
695 			fprintf(NetTrace, " SB ");
696 			i++;
697 			j = k = i;
698 			while (j < length) {
699 			    if (pointer[j] == SE) {
700 				if (j+1 == length)
701 				    break;
702 				if (pointer[j+1] == SE)
703 				    j++;
704 				else
705 				    break;
706 			    }
707 			    pointer[k++] = pointer[j++];
708 			}
709 			printsub(0, &pointer[i], k - i);
710 			if (i < length) {
711 			    fprintf(NetTrace, " SE");
712 			    i = j;
713 			} else
714 			    i = j - 1;
715 
716 			if (NetTrace == stdout)
717 			    fprintf(NetTrace, "\r\n");
718 			else
719 			    fprintf(NetTrace, "\n");
720 
721 			break;
722 
723 		    default:
724 			fprintf(NetTrace, " %d", pointer[i]);
725 			break;
726 		    }
727 		}
728 		break;
729 	    }
730 	    break;
731 	  }
732 
733 	case TELOPT_XDISPLOC:
734 	    fprintf(NetTrace, "X-DISPLAY-LOCATION ");
735 	    switch (pointer[1]) {
736 	    case TELQUAL_IS:
737 		fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
738 		break;
739 	    case TELQUAL_SEND:
740 		fprintf(NetTrace, "SEND");
741 		break;
742 	    default:
743 		fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
744 				pointer[1], pointer[1]);
745 	    }
746 	    break;
747 
748 	case TELOPT_NEW_ENVIRON:
749 	    fprintf(NetTrace, "NEW-ENVIRON ");
750 #ifdef	OLD_ENVIRON
751 	    goto env_common1;
752 	case TELOPT_OLD_ENVIRON:
753 	    fprintf(NetTrace, "OLD-ENVIRON");
754 	env_common1:
755 #endif
756 	    switch (pointer[1]) {
757 	    case TELQUAL_IS:
758 		fprintf(NetTrace, "IS ");
759 		goto env_common;
760 	    case TELQUAL_SEND:
761 		fprintf(NetTrace, "SEND ");
762 		goto env_common;
763 	    case TELQUAL_INFO:
764 		fprintf(NetTrace, "INFO ");
765 	    env_common:
766 		{
767 		    int noquote = 2;
768 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
769 		    extern int old_env_var, old_env_value;
770 #endif
771 		    for (i = 2; i < length; i++ ) {
772 			switch (pointer[i]) {
773 			case NEW_ENV_VALUE:
774 #ifdef OLD_ENVIRON
775 		     /*	case NEW_ENV_OVAR: */
776 			    if (pointer[0] == TELOPT_OLD_ENVIRON) {
777 # ifdef	ENV_HACK
778 				if (old_env_var == OLD_ENV_VALUE)
779 				    fprintf(NetTrace, "\" (VALUE) " + noquote);
780 				else
781 # endif
782 				    fprintf(NetTrace, "\" VAR " + noquote);
783 			    } else
784 #endif /* OLD_ENVIRON */
785 				fprintf(NetTrace, "%s", "\" VALUE " + noquote);
786 			    noquote = 2;
787 			    break;
788 
789 			case NEW_ENV_VAR:
790 #ifdef OLD_ENVIRON
791 		     /* case OLD_ENV_VALUE: */
792 			    if (pointer[0] == TELOPT_OLD_ENVIRON) {
793 # ifdef	ENV_HACK
794 				if (old_env_value == OLD_ENV_VAR)
795 				    fprintf(NetTrace, "\" (VAR) " + noquote);
796 				else
797 # endif
798 				    fprintf(NetTrace, "\" VALUE " + noquote);
799 			    } else
800 #endif /* OLD_ENVIRON */
801 				fprintf(NetTrace, "%s", "\" VAR " + noquote);
802 			    noquote = 2;
803 			    break;
804 
805 			case ENV_ESC:
806 			    fprintf(NetTrace, "%s", "\" ESC " + noquote);
807 			    noquote = 2;
808 			    break;
809 
810 			case ENV_USERVAR:
811 			    fprintf(NetTrace, "%s", "\" USERVAR " + noquote);
812 			    noquote = 2;
813 			    break;
814 
815 			default:
816 			    if (isprint(pointer[i]) && pointer[i] != '"') {
817 				if (noquote) {
818 				    putc('"', NetTrace);
819 				    noquote = 0;
820 				}
821 				putc(pointer[i], NetTrace);
822 			    } else {
823 				fprintf(NetTrace, "\" %03o " + noquote,
824 							pointer[i]);
825 				noquote = 2;
826 			    }
827 			    break;
828 			}
829 		    }
830 		    if (!noquote)
831 			putc('"', NetTrace);
832 		    break;
833 		}
834 	    }
835 	    break;
836 
837 	default:
838 	    if (TELOPT_OK(pointer[0]))
839 		fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
840 	    else
841 		fprintf(NetTrace, "%d (unknown)", pointer[0]);
842 	    for (i = 1; i < length; i++)
843 		fprintf(NetTrace, " %d", pointer[i]);
844 	    break;
845 	}
846 	if (direction) {
847 	    if (NetTrace == stdout)
848 		fprintf(NetTrace, "\r\n");
849 	    else
850 		fprintf(NetTrace, "\n");
851 	}
852 	if (NetTrace == stdout)
853 	    fflush(NetTrace);
854     }
855 }
856 
857 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
858  *			Note that we consider the buffer to run all the
859  *			way to the kernel (thus the select).
860  */
861 
862 static void
863 EmptyTerminal(void)
864 {
865     fd_set	o;
866 
867     FD_ZERO(&o);
868 
869     if (TTYBYTES() == 0) {
870 	FD_SET(tout, &o);
871 	(void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
872 			(struct timeval *) 0);	/* wait for TTLOWAT */
873     } else {
874 	while (TTYBYTES()) {
875 	    (void) ttyflush(0);
876 	    FD_SET(tout, &o);
877 	    (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
878 				(struct timeval *) 0);	/* wait for TTLOWAT */
879 	}
880     }
881 }
882 
883 static void
884 SetForExit(void)
885 {
886     setconnmode(0);
887     do {
888 	(void)telrcv();			/* Process any incoming data */
889 	EmptyTerminal();
890     } while (ring_full_count(&netiring));	/* While there is any */
891     setcommandmode();
892     fflush(stdout);
893     fflush(stderr);
894     setconnmode(0);
895     EmptyTerminal();			/* Flush the path to the tty */
896     setcommandmode();
897 }
898 
899 void
900 Exit(int returnCode)
901 {
902     SetForExit();
903     exit(returnCode);
904 }
905 
906 void
907 ExitString(const char *string, int returnCode)
908 {
909     SetForExit();
910     fwrite(string, 1, strlen(string), stderr);
911     exit(returnCode);
912 }
913