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