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