OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [telnet/] [utilities.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.