utilities.c revision 1.23 1 /* $NetBSD: utilities.c,v 1.23 2012/01/09 16:08:55 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. 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.23 2012/01/09 16:08:55 christos 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(const 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 const 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 const 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 static const char NQ[] = "\" ";
773 const char *noquote = NQ;
774 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
775 extern int old_env_var, old_env_value;
776 #endif
777 for (i = 2; i < length; i++ ) {
778 switch (pointer[i]) {
779 case NEW_ENV_VALUE:
780 #ifdef OLD_ENVIRON
781 /* case NEW_ENV_OVAR: */
782 if (pointer[0] == TELOPT_OLD_ENVIRON) {
783 # ifdef ENV_HACK
784 if (old_env_var == OLD_ENV_VALUE)
785 fprintf(NetTrace, "%s(VALUE) ", noquote);
786 else
787 # endif
788 fprintf(NetTrace, "%sVAR ", noquote);
789 } else
790 #endif /* OLD_ENVIRON */
791 fprintf(NetTrace, "%sVALUE ", noquote);
792 noquote = NQ;
793 break;
794
795 case NEW_ENV_VAR:
796 #ifdef OLD_ENVIRON
797 /* case OLD_ENV_VALUE: */
798 if (pointer[0] == TELOPT_OLD_ENVIRON) {
799 # ifdef ENV_HACK
800 if (old_env_value == OLD_ENV_VAR)
801 fprintf(NetTrace, "%s(VAR) ", noquote);
802 else
803 # endif
804 fprintf(NetTrace, "%sVALUE ", noquote);
805 } else
806 #endif /* OLD_ENVIRON */
807 fprintf(NetTrace, "%sVAR ", noquote);
808 noquote = NQ;
809 break;
810
811 case ENV_ESC:
812 fprintf(NetTrace, "%sESC ", noquote);
813 noquote = NQ;
814 break;
815
816 case ENV_USERVAR:
817 fprintf(NetTrace, "%sUSERVAR ", noquote);
818 noquote = NQ;
819 break;
820
821 default:
822 if (isprint(pointer[i]) && pointer[i] != '"') {
823 if (*noquote) {
824 putc('"', NetTrace);
825 noquote = "";
826 }
827 putc(pointer[i], NetTrace);
828 } else {
829 fprintf(NetTrace, "%s%03o ", noquote,
830 pointer[i]);
831 noquote = NQ;
832 }
833 break;
834 }
835 }
836 if (!noquote)
837 putc('"', NetTrace);
838 break;
839 }
840 }
841 break;
842
843 default:
844 if (TELOPT_OK(pointer[0]))
845 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
846 else
847 fprintf(NetTrace, "%d (unknown)", pointer[0]);
848 for (i = 1; i < length; i++)
849 fprintf(NetTrace, " %d", pointer[i]);
850 break;
851 }
852 if (direction) {
853 if (NetTrace == stdout)
854 fprintf(NetTrace, "\r\n");
855 else
856 fprintf(NetTrace, "\n");
857 }
858 if (NetTrace == stdout)
859 fflush(NetTrace);
860 }
861 }
862
863 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
864 * Note that we consider the buffer to run all the
865 * way to the kernel (thus the poll).
866 */
867
868 void
869 EmptyTerminal(void)
870 {
871 struct pollfd set[1];
872
873 set[0].fd = tout;
874 set[0].events = POLLOUT;
875
876 if (TTYBYTES() == 0) {
877 (void) poll(set, 1, INFTIM);
878 } else {
879 while (TTYBYTES()) {
880 (void) ttyflush(0);
881 (void) poll(set, 1, INFTIM);
882 }
883 }
884 }
885
886 void
887 SetForExit(void)
888 {
889 setconnmode(0);
890 #ifdef TN3270
891 if (In3270) {
892 Finish3270();
893 }
894 #else /* defined(TN3270) */
895 do {
896 (void)telrcv(); /* Process any incoming data */
897 EmptyTerminal();
898 } while (ring_full_count(&netiring)); /* While there is any */
899 #endif /* defined(TN3270) */
900 setcommandmode();
901 fflush(stdout);
902 fflush(stderr);
903 #ifdef TN3270
904 if (In3270) {
905 StopScreen(1);
906 }
907 #endif /* defined(TN3270) */
908 setconnmode(0);
909 EmptyTerminal(); /* Flush the path to the tty */
910 setcommandmode();
911 }
912
913 void
914 Exit(int returnCode)
915 {
916 SetForExit();
917 exit(returnCode);
918 }
919
920 void
921 ExitString(const char *string, int returnCode)
922 {
923 SetForExit();
924 fwrite(string, 1, strlen(string), stderr);
925 exit(returnCode);
926 }
927