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