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