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