~ubuntu-branches/ubuntu/wily/netkit-telnet-ssl/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/010-full_set_in_18.diff/telnet/utilities.cc

  • Committer: Package Import Robot
  • Author(s): Mats Erik Andersson
  • Date: 2015-04-27 23:20:22 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150427232022-c2f04nl1gr4qyqom
Tags: 0.17.40+0.2-1
* Bring in package changes from experimental to unstable.
* Update to source version 0.17-40 of netkit-telnet.
  + debian/rules: Define and use the variable LDDEFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1988 Regents of the University of California.
 
3
 * 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
/*
 
35
 * From: @(#)utilities.c        5.3 (Berkeley) 3/22/91
 
36
 */
 
37
char util_rcsid[] = 
 
38
  "$Id: utilities.cc,v 1.19 1999/12/12 15:33:40 dholland Exp $";
 
39
 
 
40
#define TELOPTS
 
41
#define TELCMDS
 
42
#define SLC_NAMES
 
43
 
 
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 <ctype.h>
 
50
 
 
51
#include "ring.h"
 
52
#include "defines.h"
 
53
#include "externs.h"
 
54
#include "proto.h"
 
55
#include "terminal.h"
 
56
 
 
57
FILE *NetTrace = 0;             /* Not in bss, since needs to stay */ /* ? */
 
58
char NetTraceFile[256] = "(standard output)";
 
59
 
 
60
/*
 
61
 * upcase()
 
62
 *
 
63
 *      Upcase (in place) the argument.
 
64
 */
 
65
void upcase(char *str) {
 
66
    for (int i=0; str[i]; i++) {
 
67
        if (islower(str[i])) str[i] = toupper(str[i]);
 
68
    }
 
69
}
 
70
 
 
71
/*
 
72
 * The following are routines used to print out debugging information.
 
73
 */
 
74
 
 
75
void SetNetTrace(const char *file) {
 
76
    if (NetTrace && NetTrace != stdout)
 
77
        fclose(NetTrace);
 
78
    if (file && strcmp(file, "-")) {
 
79
        NetTrace = fopen(file, "w");
 
80
        if (NetTrace) {
 
81
            strcpy((char *)NetTraceFile, file);
 
82
            return;
 
83
        }
 
84
        fprintf(stderr, "Cannot open %s.\n", file);
 
85
    }
 
86
    NetTrace = stdout;
 
87
    strcpy((char *)NetTraceFile, "(standard output)");
 
88
}
 
89
 
 
90
#define BYTES_PER_LINE  32
 
91
#define min(x,y) ((x<y)? x:y)
 
92
 
 
93
void Dump(int direction, char *buffer, int length) {
 
94
    char *pThis;
 
95
    int offset;
 
96
 
 
97
    offset = 0;
 
98
 
 
99
    while (length) {
 
100
        /* print one line */
 
101
        fprintf(NetTrace, "%c 0x%x\t", direction, offset);
 
102
        pThis = buffer;
 
103
        if (0 /*prettydump*/) {
 
104
            buffer = buffer + min(length, BYTES_PER_LINE/2);
 
105
            while (pThis < buffer) {
 
106
                fprintf(NetTrace, "%c%.2x",
 
107
                        (((*pThis)&0xff) == 0xff) ? '*' : ' ',
 
108
                        (*pThis)&0xff);
 
109
                pThis++;
 
110
            }
 
111
            length -= BYTES_PER_LINE/2;
 
112
            offset += BYTES_PER_LINE/2;
 
113
        } 
 
114
        else {
 
115
            buffer = buffer + min(length, BYTES_PER_LINE);
 
116
            while (pThis < buffer) {
 
117
                fprintf(NetTrace, "%.2x", (*pThis)&0xff);
 
118
                pThis++;
 
119
            }
 
120
            length -= BYTES_PER_LINE;
 
121
            offset += BYTES_PER_LINE;
 
122
        }
 
123
        if (NetTrace == stdout) {
 
124
            fprintf(NetTrace, "\r\n");
 
125
        } 
 
126
        else {
 
127
            fprintf(NetTrace, "\n");
 
128
        }
 
129
        if (length < 0) {
 
130
            fflush(NetTrace);
 
131
            return;
 
132
        }
 
133
        /* find next unique line */
 
134
    }
 
135
    fflush(NetTrace);
 
136
}
 
137
 
 
138
 
 
139
void printoption(const char *direction, int cmd, int option) {
 
140
    if (!showoptions)
 
141
        return;
 
142
    if (cmd == IAC) {
 
143
        if (TELCMD_OK(option))
 
144
            fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
 
145
        else
 
146
            fprintf(NetTrace, "%s IAC %d", direction, option);
 
147
    } 
 
148
    else {
 
149
        const char *fmt;
 
150
        fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
 
151
            (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
 
152
        if (fmt) {
 
153
            fprintf(NetTrace, "%s %s ", direction, fmt);
 
154
            if (TELOPT_OK(option))
 
155
                fprintf(NetTrace, "%s", TELOPT(option));
 
156
            else if (option == TELOPT_EXOPL)
 
157
                fprintf(NetTrace, "EXOPL");
 
158
            else
 
159
                fprintf(NetTrace, "%d", option);
 
160
        } 
 
161
        else
 
162
            fprintf(NetTrace, "%s %d %d", direction, cmd, option);
 
163
    }
 
164
    if (NetTrace == stdout)
 
165
        fprintf(NetTrace, "\r\n");
 
166
    else
 
167
        fprintf(NetTrace, "\n");
 
168
    return;
 
169
}
 
170
 
 
171
void optionstatus(void) {
 
172
    int i;
 
173
    extern char will_wont_resp[], do_dont_resp[];
 
174
 
 
175
    for (i = 0; i < 256; i++) {
 
176
        if (do_dont_resp[i]) {
 
177
            if (TELOPT_OK(i))
 
178
                printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
 
179
            else if (TELCMD_OK(i))
 
180
                printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
 
181
            else
 
182
                printf("resp DO_DONT %d: %d\n", i, do_dont_resp[i]);
 
183
            if (my_want_state_is_do(i)) {
 
184
                if (TELOPT_OK(i))
 
185
                    printf("want DO   %s\n", TELOPT(i));
 
186
                else if (TELCMD_OK(i))
 
187
                    printf("want DO   %s\n", TELCMD(i));
 
188
                else
 
189
                    printf("want DO   %d\n", i);
 
190
            } 
 
191
            else {
 
192
                if (TELOPT_OK(i))
 
193
                    printf("want DONT %s\n", TELOPT(i));
 
194
                else if (TELCMD_OK(i))
 
195
                    printf("want DONT %s\n", TELCMD(i));
 
196
                else
 
197
                    printf("want DONT %d\n", i);
 
198
            }
 
199
        } 
 
200
        else {
 
201
            if (my_state_is_do(i)) {
 
202
                if (TELOPT_OK(i))
 
203
                    printf("     DO   %s\n", TELOPT(i));
 
204
                else if (TELCMD_OK(i))
 
205
                    printf("     DO   %s\n", TELCMD(i));
 
206
                else
 
207
                    printf("     DO   %d\n", i);
 
208
            }
 
209
        }
 
210
        if (will_wont_resp[i]) {
 
211
            if (TELOPT_OK(i))
 
212
                printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
 
213
            else if (TELCMD_OK(i))
 
214
                printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
 
215
            else
 
216
                printf("resp WILL_WONT %d: %d\n",
 
217
                                i, will_wont_resp[i]);
 
218
            if (my_want_state_is_will(i)) {
 
219
                if (TELOPT_OK(i))
 
220
                    printf("want WILL %s\n", TELOPT(i));
 
221
                else if (TELCMD_OK(i))
 
222
                    printf("want WILL %s\n", TELCMD(i));
 
223
                else
 
224
                    printf("want WILL %d\n", i);
 
225
            } 
 
226
            else {
 
227
                if (TELOPT_OK(i))
 
228
                    printf("want WONT %s\n", TELOPT(i));
 
229
                else if (TELCMD_OK(i))
 
230
                    printf("want WONT %s\n", TELCMD(i));
 
231
                else
 
232
                    printf("want WONT %d\n", i);
 
233
            }
 
234
        } 
 
235
        else {
 
236
            if (my_state_is_will(i)) {
 
237
                if (TELOPT_OK(i))
 
238
                    printf("     WILL %s\n", TELOPT(i));
 
239
                else if (TELCMD_OK(i))
 
240
                    printf("     WILL %s\n", TELCMD(i));
 
241
                else
 
242
                    printf("     WILL %d\n", i);
 
243
            }
 
244
        }
 
245
    }
 
246
 
 
247
}
 
248
 
 
249
/* direction: '<' or '>' */
 
250
/* pointer: where suboption data sits */
 
251
/* length: length of suboption data */
 
252
void printsub(int direction, unsigned char *pointer, int length) {
 
253
    register int i = 0;
 
254
 
 
255
    extern int want_status_response;
 
256
 
 
257
    if (showoptions || direction == 0 ||
 
258
        (want_status_response && (pointer[0] == TELOPT_STATUS))) {
 
259
        if (direction) {
 
260
            fprintf(NetTrace, "%s IAC SB ",
 
261
                                (direction == '<')? "RCVD":"SENT");
 
262
            if (length >= 3) {
 
263
                register int j;
 
264
 
 
265
                i = pointer[length-2];
 
266
                j = pointer[length-1];
 
267
 
 
268
                if (i != IAC || j != SE) {
 
269
                    fprintf(NetTrace, "(terminated by ");
 
270
                    if (TELOPT_OK(i))
 
271
                        fprintf(NetTrace, "%s ", TELOPT(i));
 
272
                    else if (TELCMD_OK(i))
 
273
                        fprintf(NetTrace, "%s ", TELCMD(i));
 
274
                    else
 
275
                        fprintf(NetTrace, "%d ", i);
 
276
                    if (TELOPT_OK(j))
 
277
                        fprintf(NetTrace, "%s", TELOPT(j));
 
278
                    else if (TELCMD_OK(j))
 
279
                        fprintf(NetTrace, "%s", TELCMD(j));
 
280
                    else
 
281
                        fprintf(NetTrace, "%d", j);
 
282
                    fprintf(NetTrace, ", not IAC SE!) ");
 
283
                }
 
284
            }
 
285
            length -= 2;
 
286
        }
 
287
        if (length < 1) {
 
288
            fprintf(NetTrace, "(Empty suboption???)");
 
289
            return;
 
290
        }
 
291
        switch ((unsigned char)(pointer[0])) {
 
292
        case TELOPT_TTYPE:
 
293
            fprintf(NetTrace, "TERMINAL-TYPE ");
 
294
            switch (pointer[1]) {
 
295
            case TELQUAL_IS:
 
296
                fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
 
297
                break;
 
298
            case TELQUAL_SEND:
 
299
                fprintf(NetTrace, "SEND");
 
300
                break;
 
301
            default:
 
302
                fprintf(NetTrace,
 
303
                                "- unknown qualifier %d (0x%x).",
 
304
                                pointer[1], pointer[1]);
 
305
            }
 
306
            break;
 
307
        case TELOPT_TSPEED:
 
308
            fprintf(NetTrace, "TERMINAL-SPEED");
 
309
            if (length < 2) {
 
310
                fprintf(NetTrace, " (empty suboption???)");
 
311
                break;
 
312
            }
 
313
            switch (pointer[1]) {
 
314
            case TELQUAL_IS:
 
315
                fprintf(NetTrace, " IS ");
 
316
                fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
 
317
                break;
 
318
            default:
 
319
                if (pointer[1] == 1)
 
320
                    fprintf(NetTrace, " SEND");
 
321
                else
 
322
                    fprintf(NetTrace, " %d (unknown)", pointer[1]);
 
323
                for (i = 2; i < length; i++)
 
324
                    fprintf(NetTrace, " ?%d?", pointer[i]);
 
325
                break;
 
326
            }
 
327
            break;
 
328
 
 
329
        case TELOPT_LFLOW:
 
330
            fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
 
331
            if (length < 2) {
 
332
                fprintf(NetTrace, " (empty suboption???)");
 
333
                break;
 
334
            }
 
335
            switch (pointer[1]) {
 
336
            case 0:
 
337
                fprintf(NetTrace, " OFF"); break;
 
338
            case 1:
 
339
                fprintf(NetTrace, " ON"); break;
 
340
            default:
 
341
                fprintf(NetTrace, " %d (unknown)", pointer[1]);
 
342
            }
 
343
            for (i = 2; i < length; i++)
 
344
                fprintf(NetTrace, " ?%d?", pointer[i]);
 
345
            break;
 
346
 
 
347
        case TELOPT_NAWS:
 
348
            fprintf(NetTrace, "NAWS");
 
349
            if (length < 2) {
 
350
                fprintf(NetTrace, " (empty suboption???)");
 
351
                break;
 
352
            }
 
353
            if (length == 2) {
 
354
                fprintf(NetTrace, " ?%d?", pointer[1]);
 
355
                break;
 
356
            }
 
357
            fprintf(NetTrace, " %d %d (%d)",
 
358
                pointer[1], pointer[2],
 
359
                (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
 
360
            if (length == 4) {
 
361
                fprintf(NetTrace, " ?%d?", pointer[3]);
 
362
                break;
 
363
            }
 
364
            fprintf(NetTrace, " %d %d (%d)",
 
365
                pointer[3], pointer[4],
 
366
                (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
 
367
            for (i = 5; i < length; i++)
 
368
                fprintf(NetTrace, " ?%d?", pointer[i]);
 
369
            break;
 
370
 
 
371
        case TELOPT_LINEMODE:
 
372
            fprintf(NetTrace, "LINEMODE ");
 
373
            if (length < 2) {
 
374
                fprintf(NetTrace, " (empty suboption???)");
 
375
                break;
 
376
            }
 
377
            switch ((unsigned char)(pointer[1])) {
 
378
            case WILL:
 
379
                fprintf(NetTrace, "WILL ");
 
380
                goto common;
 
381
            case WONT:
 
382
                fprintf(NetTrace, "WONT ");
 
383
                goto common;
 
384
            case DO:
 
385
                fprintf(NetTrace, "DO ");
 
386
                goto common;
 
387
            case DONT:
 
388
                fprintf(NetTrace, "DONT ");
 
389
            common:
 
390
                if (length < 3) {
 
391
                    fprintf(NetTrace, "(no option???)");
 
392
                    break;
 
393
                }
 
394
                switch ((unsigned char)(pointer[2])) {
 
395
                case LM_FORWARDMASK:
 
396
                    fprintf(NetTrace, "Forward Mask");
 
397
                    for (i = 3; i < length; i++)
 
398
                        fprintf(NetTrace, " %x", pointer[i]);
 
399
                    break;
 
400
                default:
 
401
                    fprintf(NetTrace, "%d (unknown)", pointer[2]);
 
402
                    for (i = 3; i < length; i++)
 
403
                        fprintf(NetTrace, " %d", pointer[i]);
 
404
                    break;
 
405
                }
 
406
                break;
 
407
                
 
408
            case LM_SLC:
 
409
                fprintf(NetTrace, "SLC");
 
410
                for (i = 2; i < length - 2; i += 3) {
 
411
                    if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
 
412
                        fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
 
413
                    else
 
414
                        fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
 
415
                    switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
 
416
                    case SLC_NOSUPPORT:
 
417
                        fprintf(NetTrace, " NOSUPPORT"); break;
 
418
                    case SLC_CANTCHANGE:
 
419
                        fprintf(NetTrace, " CANTCHANGE"); break;
 
420
                    case SLC_VARIABLE:
 
421
                        fprintf(NetTrace, " VARIABLE"); break;
 
422
                    case SLC_DEFAULT:
 
423
                        fprintf(NetTrace, " DEFAULT"); break;
 
424
                    }
 
425
                    fprintf(NetTrace, "%s%s%s",
 
426
                        pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
 
427
                        pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
 
428
                        pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
 
429
                    if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
 
430
                                                SLC_FLUSHOUT| SLC_LEVELBITS))
 
431
                        fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
 
432
                    fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
 
433
                    if ((pointer[i+SLC_VALUE] == IAC) &&
 
434
                        (pointer[i+SLC_VALUE+1] == IAC))
 
435
                                i++;
 
436
                }
 
437
                for (; i < length; i++)
 
438
                    fprintf(NetTrace, " ?%d?", pointer[i]);
 
439
                break;
 
440
 
 
441
            case LM_MODE:
 
442
                fprintf(NetTrace, "MODE ");
 
443
                if (length < 3) {
 
444
                    fprintf(NetTrace, "(no mode???)");
 
445
                    break;
 
446
                }
 
447
                {
 
448
                    char tbuf[64];
 
449
                    snprintf(tbuf, sizeof(tbuf), "%s%s%s%s%s",
 
450
                        pointer[2]&MODE_EDIT ? "|EDIT" : "",
 
451
                        pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
 
452
                        pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
 
453
                        pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
 
454
                        pointer[2]&MODE_ACK ? "|ACK" : "");
 
455
                    fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
 
456
                }
 
457
                if (pointer[2]&~(MODE_MASK))
 
458
                    fprintf(NetTrace, " (0x%x)", pointer[2]);
 
459
                for (i = 3; i < length; i++)
 
460
                    fprintf(NetTrace, " ?0x%x?", pointer[i]);
 
461
                break;
 
462
            default:
 
463
                fprintf(NetTrace, "%d (unknown)", pointer[1]);
 
464
                for (i = 2; i < length; i++)
 
465
                    fprintf(NetTrace, " %d", pointer[i]);
 
466
            }
 
467
            break;
 
468
 
 
469
        case TELOPT_STATUS: {
 
470
            const char *cp;
 
471
            int j, k;
 
472
 
 
473
            fprintf(NetTrace, "STATUS");
 
474
 
 
475
            switch (pointer[1]) {
 
476
            default:
 
477
                if (pointer[1] == TELQUAL_SEND)
 
478
                    fprintf(NetTrace, " SEND");
 
479
                else
 
480
                    fprintf(NetTrace, " %d (unknown)", pointer[1]);
 
481
                for (i = 2; i < length; i++)
 
482
                    fprintf(NetTrace, " ?%d?", pointer[i]);
 
483
                break;
 
484
            case TELQUAL_IS:
 
485
                if (--want_status_response < 0)
 
486
                    want_status_response = 0;
 
487
                if (NetTrace == stdout)
 
488
                    fprintf(NetTrace, " IS\r\n");
 
489
                else
 
490
                    fprintf(NetTrace, " IS\n");
 
491
 
 
492
                for (i = 2; i < length; i++) {
 
493
                    switch((unsigned char)(pointer[i])) {
 
494
                    case DO:    cp = "DO"; goto common2;
 
495
                    case DONT:  cp = "DONT"; goto common2;
 
496
                    case WILL:  cp = "WILL"; goto common2;
 
497
                    case WONT:  cp = "WONT"; goto common2;
 
498
                    common2:
 
499
                        i++;
 
500
                        if (TELOPT_OK((int)pointer[i]))
 
501
                            fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
 
502
                        else
 
503
                            fprintf(NetTrace, " %s %d", cp, pointer[i]);
 
504
 
 
505
                        if (NetTrace == stdout)
 
506
                            fprintf(NetTrace, "\r\n");
 
507
                        else
 
508
                            fprintf(NetTrace, "\n");
 
509
                        break;
 
510
 
 
511
                    case SB:
 
512
                        fprintf(NetTrace, " SB ");
 
513
                        i++;
 
514
                        j = k = i;
 
515
                        while (j < length) {
 
516
                            if (pointer[j] == SE) {
 
517
                                if (j+1 == length)
 
518
                                    break;
 
519
                                if (pointer[j+1] == SE)
 
520
                                    j++;
 
521
                                else
 
522
                                    break;
 
523
                            }
 
524
                            pointer[k++] = pointer[j++];
 
525
                        }
 
526
                        printsub(0, &pointer[i], k - i);
 
527
                        if (i < length) {
 
528
                            fprintf(NetTrace, " SE");
 
529
                            i = j;
 
530
                        } else
 
531
                            i = j - 1;
 
532
 
 
533
                        if (NetTrace == stdout)
 
534
                            fprintf(NetTrace, "\r\n");
 
535
                        else
 
536
                            fprintf(NetTrace, "\n");
 
537
 
 
538
                        break;
 
539
                                
 
540
                    default:
 
541
                        fprintf(NetTrace, " %d", pointer[i]);
 
542
                        break;
 
543
                    }
 
544
                }
 
545
                break;
 
546
            }
 
547
            break;
 
548
          }
 
549
 
 
550
        case TELOPT_XDISPLOC:
 
551
            fprintf(NetTrace, "X-DISPLAY-LOCATION ");
 
552
            switch (pointer[1]) {
 
553
            case TELQUAL_IS:
 
554
                fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
 
555
                break;
 
556
            case TELQUAL_SEND:
 
557
                fprintf(NetTrace, "SEND");
 
558
                break;
 
559
            default:
 
560
                fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
 
561
                                pointer[1], pointer[1]);
 
562
            }
 
563
            break;
 
564
 
 
565
        case TELOPT_ENVIRON:
 
566
            fprintf(NetTrace, "ENVIRON ");
 
567
            switch (pointer[1]) {
 
568
            case TELQUAL_IS:
 
569
                fprintf(NetTrace, "IS ");
 
570
                goto env_common;
 
571
            case TELQUAL_SEND:
 
572
                fprintf(NetTrace, "SEND ");
 
573
                goto env_common;
 
574
            case TELQUAL_INFO:
 
575
                fprintf(NetTrace, "INFO ");
 
576
            env_common:
 
577
                {
 
578
                    register int noquote = 2;
 
579
                    for (i = 2; i < length; i++ ) {
 
580
                        switch (pointer[i]) {
 
581
                        case ENV_VAR:
 
582
                            if (pointer[1] == TELQUAL_SEND)
 
583
                                goto def_case;
 
584
                            fprintf(NetTrace, "\" VAR " + noquote);
 
585
                            noquote = 2;
 
586
                            break;
 
587
 
 
588
                        case ENV_VALUE:
 
589
                            fprintf(NetTrace, "\" VALUE " + noquote);
 
590
                            noquote = 2;
 
591
                            break;
 
592
 
 
593
                        case ENV_ESC:
 
594
                            fprintf(NetTrace, "\" ESC " + noquote);
 
595
                            noquote = 2;
 
596
                            break;
 
597
 
 
598
                        default:
 
599
                        def_case:
 
600
                            if (isprint(pointer[i]) && pointer[i] != '"') {
 
601
                                if (noquote) {
 
602
                                    putc('"', NetTrace);
 
603
                                    noquote = 0;
 
604
                                }
 
605
                                putc(pointer[i], NetTrace);
 
606
                            } else {
 
607
                                fprintf(NetTrace, "\" %03o " + noquote,
 
608
                                                        pointer[i]);
 
609
                                noquote = 2;
 
610
                            }
 
611
                            break;
 
612
                        }
 
613
                    }
 
614
                    if (!noquote)
 
615
                        putc('"', NetTrace);
 
616
                    break;
 
617
                }
 
618
            }
 
619
            break;
 
620
 
 
621
        default:
 
622
            if (TELOPT_OK(pointer[0]))
 
623
                fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
 
624
            else
 
625
                fprintf(NetTrace, "%d (unknown)", pointer[i]);
 
626
            for (i = 1; i < length; i++)
 
627
                fprintf(NetTrace, " %d", pointer[i]);
 
628
            break;
 
629
        }
 
630
        if (direction) {
 
631
            if (NetTrace == stdout)
 
632
                fprintf(NetTrace, "\r\n");
 
633
            else
 
634
                fprintf(NetTrace, "\n");
 
635
        }
 
636
    }
 
637
}
 
638
 
 
639
void SetForExit(void) {
 
640
    setconnmode(0);
 
641
#if defined(TN3270)
 
642
    if (In3270) {
 
643
        Finish3270();
 
644
    }
 
645
#else   /* defined(TN3270) */
 
646
    do {
 
647
        telrcv();                       /* Process any incoming data */
 
648
        EmptyTerminal();
 
649
    } while (netiring.full_count());    /* While there is any */
 
650
#endif  /* defined(TN3270) */
 
651
    setcommandmode();
 
652
    fflush(stdout);
 
653
    fflush(stderr);
 
654
#if defined(TN3270)
 
655
    if (In3270) {
 
656
        StopScreen(1);
 
657
    }
 
658
#endif  /* defined(TN3270) */
 
659
    setconnmode(0);
 
660
    EmptyTerminal();                    /* Flush the path to the tty */
 
661
    setcommandmode();
 
662
}
 
663
 
 
664
void Exit(int returnCode) {
 
665
    SetForExit();
 
666
    exit(returnCode);
 
667
}
 
668
 
 
669
void ExitString(const char *string, int returnCode) {
 
670
    SetForExit();
 
671
    fwrite(string, 1, strlen(string), stderr);
 
672
    exit(returnCode);
 
673
}