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