utilities.c revision 1.15 1 /* $NetBSD: utilities.c,v 1.15 2003/06/18 20:51:01 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.15 2003/06/18 20:51:01 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 #include <poll.h>
56
57 #include <ctype.h>
58
59 #include "general.h"
60
61 #include "fdset.h"
62
63 #include "ring.h"
64
65 #include "defines.h"
66
67 #include "externs.h"
68
69 #if defined(TN3270)
70 #include "../sys_curses/telextrn.h"
71 #endif
72
73 #ifdef AUTHENTICATION
74 #include <libtelnet/auth.h>
75 #endif
76 #ifdef ENCRYPTION
77 #include <libtelnet/encrypt.h>
78 #endif
79
80 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
81 int prettydump;
82
83 /*
84 * upcase()
85 *
86 * Upcase (in place) the argument.
87 */
88
89 void
90 upcase(char *argument)
91 {
92 int c;
93
94 while ((c = *argument) != 0) {
95 if (islower(c)) {
96 *argument = toupper(c);
97 }
98 argument++;
99 }
100 }
101
102 /*
103 * SetSockOpt()
104 *
105 * Compensate for differences in 4.2 and 4.3 systems.
106 */
107
108 int
109 SetSockOpt(int fd, int level, int option, int yesno)
110 {
111 #ifndef NOT43
112 return setsockopt(fd, level, option,
113 (char *)&yesno, sizeof yesno);
114 #else /* NOT43 */
115 if (yesno == 0) { /* Can't do that in 4.2! */
116 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
117 option);
118 return -1;
119 }
120 return setsockopt(fd, level, option, 0, 0);
121 #endif /* NOT43 */
122 }
123
124 /*
126 * The following are routines used to print out debugging information.
127 */
128
129 unsigned char NetTraceFile[256] = "(standard output)";
130
131 void
132 SetNetTrace(char *file)
133 {
134 if (NetTrace && NetTrace != stdout)
135 fclose(NetTrace);
136 if (file && (strcmp(file, "-") != 0)) {
137 NetTrace = fopen(file, "w");
138 if (NetTrace) {
139 strcpy((char *)NetTraceFile, file);
140 return;
141 }
142 fprintf(stderr, "Cannot open %s.\n", file);
143 }
144 NetTrace = stdout;
145 strcpy((char *)NetTraceFile, "(standard output)");
146 }
147
148 void
149 Dump(int direction, unsigned char *buffer, int length)
150 {
151 # define BYTES_PER_LINE 32
152 # define min(x,y) ((x<y)? x:y)
153 unsigned char *pThis;
154 int offset;
155
156 offset = 0;
157
158 while (length) {
159 /* print one line */
160 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
161 pThis = buffer;
162 if (prettydump) {
163 buffer = buffer + min(length, BYTES_PER_LINE/2);
164 while (pThis < buffer) {
165 fprintf(NetTrace, "%c%.2x",
166 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
167 (*pThis)&0xff);
168 pThis++;
169 }
170 length -= BYTES_PER_LINE/2;
171 offset += BYTES_PER_LINE/2;
172 } else {
173 buffer = buffer + min(length, BYTES_PER_LINE);
174 while (pThis < buffer) {
175 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
176 pThis++;
177 }
178 length -= BYTES_PER_LINE;
179 offset += BYTES_PER_LINE;
180 }
181 if (NetTrace == stdout) {
182 fprintf(NetTrace, "\r\n");
183 } else {
184 fprintf(NetTrace, "\n");
185 }
186 if (length < 0) {
187 fflush(NetTrace);
188 return;
189 }
190 /* find next unique line */
191 }
192 fflush(NetTrace);
193 }
194
195
196 void
197 printoption(char *direction, int cmd, int 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 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(void)
232 {
233 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(
309 int direction, /* '<' or '>' */
310 unsigned char *pointer, /* where suboption data sits */
311 int length) /* length of suboption data */
312 {
313 int i;
314 #ifdef ENCRYPTION
315 char buf[512];
316 #endif /* ENCRYPTION */
317 extern int want_status_response;
318
319 if (showoptions || direction == 0 ||
320 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
321 if (direction) {
322 fprintf(NetTrace, "%s IAC SB ",
323 (direction == '<')? "RCVD":"SENT");
324 if (length >= 3) {
325 int j;
326
327 i = pointer[length-2];
328 j = pointer[length-1];
329
330 if (i != IAC || j != SE) {
331 fprintf(NetTrace, "(terminated by ");
332 if (TELOPT_OK(i))
333 fprintf(NetTrace, "%s ", TELOPT(i));
334 else if (TELCMD_OK(i))
335 fprintf(NetTrace, "%s ", TELCMD(i));
336 else
337 fprintf(NetTrace, "%d ", i);
338 if (TELOPT_OK(j))
339 fprintf(NetTrace, "%s", TELOPT(j));
340 else if (TELCMD_OK(j))
341 fprintf(NetTrace, "%s", TELCMD(j));
342 else
343 fprintf(NetTrace, "%d", j);
344 fprintf(NetTrace, ", not IAC SE!) ");
345 }
346 }
347 length -= 2;
348 }
349 if (length < 1) {
350 fprintf(NetTrace, "(Empty suboption??\?)");
351 if (NetTrace == stdout)
352 fflush(NetTrace);
353 return;
354 }
355 switch (pointer[0]) {
356 case TELOPT_TTYPE:
357 fprintf(NetTrace, "TERMINAL-TYPE ");
358 switch (pointer[1]) {
359 case TELQUAL_IS:
360 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
361 break;
362 case TELQUAL_SEND:
363 fprintf(NetTrace, "SEND");
364 break;
365 default:
366 fprintf(NetTrace,
367 "- unknown qualifier %d (0x%x).",
368 pointer[1], pointer[1]);
369 }
370 break;
371 case TELOPT_TSPEED:
372 fprintf(NetTrace, "TERMINAL-SPEED");
373 if (length < 2) {
374 fprintf(NetTrace, " (empty suboption??\?)");
375 break;
376 }
377 switch (pointer[1]) {
378 case TELQUAL_IS:
379 fprintf(NetTrace, " IS ");
380 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
381 break;
382 default:
383 if (pointer[1] == 1)
384 fprintf(NetTrace, " SEND");
385 else
386 fprintf(NetTrace, " %d (unknown)", pointer[1]);
387 for (i = 2; i < length; i++)
388 fprintf(NetTrace, " ?%d?", pointer[i]);
389 break;
390 }
391 break;
392
393 case TELOPT_LFLOW:
394 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
395 if (length < 2) {
396 fprintf(NetTrace, " (empty suboption??\?)");
397 break;
398 }
399 switch (pointer[1]) {
400 case LFLOW_OFF:
401 fprintf(NetTrace, " OFF"); break;
402 case LFLOW_ON:
403 fprintf(NetTrace, " ON"); break;
404 case LFLOW_RESTART_ANY:
405 fprintf(NetTrace, " RESTART-ANY"); break;
406 case LFLOW_RESTART_XON:
407 fprintf(NetTrace, " RESTART-XON"); break;
408 default:
409 fprintf(NetTrace, " %d (unknown)", pointer[1]);
410 }
411 for (i = 2; i < length; i++)
412 fprintf(NetTrace, " ?%d?", pointer[i]);
413 break;
414
415 case TELOPT_NAWS:
416 fprintf(NetTrace, "NAWS");
417 if (length < 2) {
418 fprintf(NetTrace, " (empty suboption??\?)");
419 break;
420 }
421 if (length == 2) {
422 fprintf(NetTrace, " ?%d?", pointer[1]);
423 break;
424 }
425 fprintf(NetTrace, " %d %d (%d)",
426 pointer[1], pointer[2],
427 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
428 if (length == 4) {
429 fprintf(NetTrace, " ?%d?", pointer[3]);
430 break;
431 }
432 fprintf(NetTrace, " %d %d (%d)",
433 pointer[3], pointer[4],
434 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
435 for (i = 5; i < length; i++)
436 fprintf(NetTrace, " ?%d?", pointer[i]);
437 break;
438
439 #if defined(AUTHENTICATION)
440 case TELOPT_AUTHENTICATION:
441 fprintf(NetTrace, "AUTHENTICATION");
442 if (length < 2) {
443 fprintf(NetTrace, " (empty suboption??\?)");
444 break;
445 }
446 switch (pointer[1]) {
447 case TELQUAL_REPLY:
448 case TELQUAL_IS:
449 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
450 "IS" : "REPLY");
451 if (AUTHTYPE_NAME_OK(pointer[2]))
452 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
453 else
454 fprintf(NetTrace, "%d ", pointer[2]);
455 if (length < 3) {
456 fprintf(NetTrace, "(partial suboption??\?)");
457 break;
458 }
459 fprintf(NetTrace, "%s|%s",
460 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
461 "CLIENT" : "SERVER",
462 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
463 "MUTUAL" : "ONE-WAY");
464
465 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
466 fprintf(NetTrace, "%s", buf);
467 break;
468
469 case TELQUAL_SEND:
470 i = 2;
471 fprintf(NetTrace, " SEND ");
472 while (i < length) {
473 if (AUTHTYPE_NAME_OK(pointer[i]))
474 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
475 else
476 fprintf(NetTrace, "%d ", pointer[i]);
477 if (++i >= length) {
478 fprintf(NetTrace, "(partial suboption??\?)");
479 break;
480 }
481 fprintf(NetTrace, "%s|%s ",
482 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
483 "CLIENT" : "SERVER",
484 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
485 "MUTUAL" : "ONE-WAY");
486 ++i;
487 }
488 break;
489
490 case TELQUAL_NAME:
491 i = 2;
492 fprintf(NetTrace, " NAME \"");
493 while (i < length)
494 putc(pointer[i++], NetTrace);
495 putc('"', NetTrace);
496 break;
497
498 default:
499 for (i = 2; i < length; i++)
500 fprintf(NetTrace, " ?%d?", pointer[i]);
501 break;
502 }
503 break;
504 #endif
505
506 #ifdef ENCRYPTION
507 case TELOPT_ENCRYPT:
508 fprintf(NetTrace, "ENCRYPT");
509 if (length < 2) {
510 fprintf(NetTrace, " (empty suboption??\?)");
511 break;
512 }
513 switch (pointer[1]) {
514 case ENCRYPT_START:
515 fprintf(NetTrace, " START");
516 break;
517
518 case ENCRYPT_END:
519 fprintf(NetTrace, " END");
520 break;
521
522 case ENCRYPT_REQSTART:
523 fprintf(NetTrace, " REQUEST-START");
524 break;
525
526 case ENCRYPT_REQEND:
527 fprintf(NetTrace, " REQUEST-END");
528 break;
529
530 case ENCRYPT_IS:
531 case ENCRYPT_REPLY:
532 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
533 "IS" : "REPLY");
534 if (length < 3) {
535 fprintf(NetTrace, " (partial suboption??\?)");
536 break;
537 }
538 if (ENCTYPE_NAME_OK(pointer[2]))
539 fprintf(NetTrace, "%s ",
540 ENCTYPE_NAME(pointer[2]));
541 else
542 fprintf(NetTrace, " %d (unknown)", pointer[2]);
543
544 encrypt_printsub(&pointer[1], length - 1, buf,
545 sizeof(buf));
546 fprintf(NetTrace, "%s", buf);
547 break;
548
549 case ENCRYPT_SUPPORT:
550 i = 2;
551 fprintf(NetTrace, " SUPPORT ");
552 while (i < length) {
553 if (ENCTYPE_NAME_OK(pointer[i]))
554 fprintf(NetTrace, "%s ",
555 ENCTYPE_NAME(pointer[i]));
556 else
557 fprintf(NetTrace, "%d ", pointer[i]);
558 i++;
559 }
560 break;
561
562 case ENCRYPT_ENC_KEYID:
563 fprintf(NetTrace, " ENC_KEYID ");
564 goto encommon;
565
566 case ENCRYPT_DEC_KEYID:
567 fprintf(NetTrace, " DEC_KEYID ");
568 goto encommon;
569
570 default:
571 fprintf(NetTrace, " %d (unknown)", pointer[1]);
572 encommon:
573 for (i = 2; i < length; i++)
574 fprintf(NetTrace, " %d", pointer[i]);
575 break;
576 }
577 break;
578 #endif /* ENCRYPTION */
579
580 case TELOPT_LINEMODE:
581 fprintf(NetTrace, "LINEMODE ");
582 if (length < 2) {
583 fprintf(NetTrace, " (empty suboption??\?)");
584 break;
585 }
586 switch (pointer[1]) {
587 case WILL:
588 fprintf(NetTrace, "WILL ");
589 goto common;
590 case WONT:
591 fprintf(NetTrace, "WONT ");
592 goto common;
593 case DO:
594 fprintf(NetTrace, "DO ");
595 goto common;
596 case DONT:
597 fprintf(NetTrace, "DONT ");
598 common:
599 if (length < 3) {
600 fprintf(NetTrace, "(no option??\?)");
601 break;
602 }
603 switch (pointer[2]) {
604 case LM_FORWARDMASK:
605 fprintf(NetTrace, "Forward Mask");
606 for (i = 3; i < length; i++)
607 fprintf(NetTrace, " %x", pointer[i]);
608 break;
609 default:
610 fprintf(NetTrace, "%d (unknown)", pointer[2]);
611 for (i = 3; i < length; i++)
612 fprintf(NetTrace, " %d", pointer[i]);
613 break;
614 }
615 break;
616
617 case LM_SLC:
618 fprintf(NetTrace, "SLC");
619 for (i = 2; i < length - 2; i += 3) {
620 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
621 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
622 else
623 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
624 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
625 case SLC_NOSUPPORT:
626 fprintf(NetTrace, " NOSUPPORT"); break;
627 case SLC_CANTCHANGE:
628 fprintf(NetTrace, " CANTCHANGE"); break;
629 case SLC_VARIABLE:
630 fprintf(NetTrace, " VARIABLE"); break;
631 case SLC_DEFAULT:
632 fprintf(NetTrace, " DEFAULT"); break;
633 }
634 fprintf(NetTrace, "%s%s%s",
635 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
636 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
637 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
638 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
639 SLC_FLUSHOUT| SLC_LEVELBITS))
640 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
641 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
642 if ((pointer[i+SLC_VALUE] == IAC) &&
643 (pointer[i+SLC_VALUE+1] == IAC))
644 i++;
645 }
646 for (; i < length; i++)
647 fprintf(NetTrace, " ?%d?", pointer[i]);
648 break;
649
650 case LM_MODE:
651 fprintf(NetTrace, "MODE ");
652 if (length < 3) {
653 fprintf(NetTrace, "(no mode??\?)");
654 break;
655 }
656 {
657 char tbuf[64];
658 sprintf(tbuf, "%s%s%s%s%s",
659 pointer[2]&MODE_EDIT ? "|EDIT" : "",
660 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
661 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
662 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
663 pointer[2]&MODE_ACK ? "|ACK" : "");
664 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
665 }
666 if (pointer[2]&~(MODE_MASK))
667 fprintf(NetTrace, " (0x%x)", pointer[2]);
668 for (i = 3; i < length; i++)
669 fprintf(NetTrace, " ?0x%x?", pointer[i]);
670 break;
671 default:
672 fprintf(NetTrace, "%d (unknown)", pointer[1]);
673 for (i = 2; i < length; i++)
674 fprintf(NetTrace, " %d", pointer[i]);
675 }
676 break;
677
678 case TELOPT_STATUS: {
679 char *cp;
680 int j, k;
681
682 fprintf(NetTrace, "STATUS");
683
684 switch (pointer[1]) {
685 default:
686 if (pointer[1] == TELQUAL_SEND)
687 fprintf(NetTrace, " SEND");
688 else
689 fprintf(NetTrace, " %d (unknown)", pointer[1]);
690 for (i = 2; i < length; i++)
691 fprintf(NetTrace, " ?%d?", pointer[i]);
692 break;
693 case TELQUAL_IS:
694 if (--want_status_response < 0)
695 want_status_response = 0;
696 if (NetTrace == stdout)
697 fprintf(NetTrace, " IS\r\n");
698 else
699 fprintf(NetTrace, " IS\n");
700
701 for (i = 2; i < length; i++) {
702 switch(pointer[i]) {
703 case DO: cp = "DO"; goto common2;
704 case DONT: cp = "DONT"; goto common2;
705 case WILL: cp = "WILL"; goto common2;
706 case WONT: cp = "WONT"; goto common2;
707 common2:
708 i++;
709 if (TELOPT_OK((int)pointer[i]))
710 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
711 else
712 fprintf(NetTrace, " %s %d", cp, pointer[i]);
713
714 if (NetTrace == stdout)
715 fprintf(NetTrace, "\r\n");
716 else
717 fprintf(NetTrace, "\n");
718 break;
719
720 case SB:
721 fprintf(NetTrace, " SB ");
722 i++;
723 j = k = i;
724 while (j < length) {
725 if (pointer[j] == SE) {
726 if (j+1 == length)
727 break;
728 if (pointer[j+1] == SE)
729 j++;
730 else
731 break;
732 }
733 pointer[k++] = pointer[j++];
734 }
735 printsub(0, &pointer[i], k - i);
736 if (i < length) {
737 fprintf(NetTrace, " SE");
738 i = j;
739 } else
740 i = j - 1;
741
742 if (NetTrace == stdout)
743 fprintf(NetTrace, "\r\n");
744 else
745 fprintf(NetTrace, "\n");
746
747 break;
748
749 default:
750 fprintf(NetTrace, " %d", pointer[i]);
751 break;
752 }
753 }
754 break;
755 }
756 break;
757 }
758
759 case TELOPT_XDISPLOC:
760 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
761 switch (pointer[1]) {
762 case TELQUAL_IS:
763 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
764 break;
765 case TELQUAL_SEND:
766 fprintf(NetTrace, "SEND");
767 break;
768 default:
769 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
770 pointer[1], pointer[1]);
771 }
772 break;
773
774 case TELOPT_NEW_ENVIRON:
775 fprintf(NetTrace, "NEW-ENVIRON ");
776 #ifdef OLD_ENVIRON
777 goto env_common1;
778 case TELOPT_OLD_ENVIRON:
779 fprintf(NetTrace, "OLD-ENVIRON");
780 env_common1:
781 #endif
782 switch (pointer[1]) {
783 case TELQUAL_IS:
784 fprintf(NetTrace, "IS ");
785 goto env_common;
786 case TELQUAL_SEND:
787 fprintf(NetTrace, "SEND ");
788 goto env_common;
789 case TELQUAL_INFO:
790 fprintf(NetTrace, "INFO ");
791 env_common:
792 {
793 int noquote = 2;
794 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
795 extern int old_env_var, old_env_value;
796 #endif
797 for (i = 2; i < length; i++ ) {
798 switch (pointer[i]) {
799 case NEW_ENV_VALUE:
800 #ifdef OLD_ENVIRON
801 /* case NEW_ENV_OVAR: */
802 if (pointer[0] == TELOPT_OLD_ENVIRON) {
803 # ifdef ENV_HACK
804 if (old_env_var == OLD_ENV_VALUE)
805 fprintf(NetTrace, "\" (VALUE) " + noquote);
806 else
807 # endif
808 fprintf(NetTrace, "\" VAR " + noquote);
809 } else
810 #endif /* OLD_ENVIRON */
811 fprintf(NetTrace, "\" VALUE " + noquote);
812 noquote = 2;
813 break;
814
815 case NEW_ENV_VAR:
816 #ifdef OLD_ENVIRON
817 /* case OLD_ENV_VALUE: */
818 if (pointer[0] == TELOPT_OLD_ENVIRON) {
819 # ifdef ENV_HACK
820 if (old_env_value == OLD_ENV_VAR)
821 fprintf(NetTrace, "\" (VAR) " + noquote);
822 else
823 # endif
824 fprintf(NetTrace, "\" VALUE " + noquote);
825 } else
826 #endif /* OLD_ENVIRON */
827 fprintf(NetTrace, "\" VAR " + noquote);
828 noquote = 2;
829 break;
830
831 case ENV_ESC:
832 fprintf(NetTrace, "\" ESC " + noquote);
833 noquote = 2;
834 break;
835
836 case ENV_USERVAR:
837 fprintf(NetTrace, "\" USERVAR " + noquote);
838 noquote = 2;
839 break;
840
841 default:
842 if (isprint(pointer[i]) && pointer[i] != '"') {
843 if (noquote) {
844 putc('"', NetTrace);
845 noquote = 0;
846 }
847 putc(pointer[i], NetTrace);
848 } else {
849 fprintf(NetTrace, "\" %03o " + noquote,
850 pointer[i]);
851 noquote = 2;
852 }
853 break;
854 }
855 }
856 if (!noquote)
857 putc('"', NetTrace);
858 break;
859 }
860 }
861 break;
862
863 default:
864 if (TELOPT_OK(pointer[0]))
865 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
866 else
867 fprintf(NetTrace, "%d (unknown)", pointer[0]);
868 for (i = 1; i < length; i++)
869 fprintf(NetTrace, " %d", pointer[i]);
870 break;
871 }
872 if (direction) {
873 if (NetTrace == stdout)
874 fprintf(NetTrace, "\r\n");
875 else
876 fprintf(NetTrace, "\n");
877 }
878 if (NetTrace == stdout)
879 fflush(NetTrace);
880 }
881 }
882
883 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
884 * Note that we consider the buffer to run all the
885 * way to the kernel (thus the select).
886 */
887
888 void
889 EmptyTerminal(void)
890 {
891 #if defined(unix)
892 struct pollfd set[1];
893
894 set[0].fd = tout;
895 set[0].events = POLLOUT;
896 #endif /* defined(unix) */
897
898 if (TTYBYTES() == 0) {
899 #if defined(unix)
900 (void) poll(set, 1, INFTIM);
901 #endif /* defined(unix) */
902 } else {
903 while (TTYBYTES()) {
904 (void) ttyflush(0);
905 #if defined(unix)
906 (void) poll(set, 1, INFTIM);
907 #endif /* defined(unix) */
908 }
909 }
910 }
911
912 void
913 SetForExit(void)
914 {
915 setconnmode(0);
916 #if defined(TN3270)
917 if (In3270) {
918 Finish3270();
919 }
920 #else /* defined(TN3270) */
921 do {
922 (void)telrcv(); /* Process any incoming data */
923 EmptyTerminal();
924 } while (ring_full_count(&netiring)); /* While there is any */
925 #endif /* defined(TN3270) */
926 setcommandmode();
927 fflush(stdout);
928 fflush(stderr);
929 #if defined(TN3270)
930 if (In3270) {
931 StopScreen(1);
932 }
933 #endif /* defined(TN3270) */
934 setconnmode(0);
935 EmptyTerminal(); /* Flush the path to the tty */
936 setcommandmode();
937 }
938
939 void
940 Exit(int returnCode)
941 {
942 SetForExit();
943 exit(returnCode);
944 }
945
946 void
947 ExitString(char *string, int returnCode)
948 {
949 SetForExit();
950 fwrite(string, 1, strlen(string), stderr);
951 exit(returnCode);
952 }
953