[BACK]Return to utilities.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / telnet

Annotation of src/usr.bin/telnet/utilities.c, Revision 1.20

1.20    ! jsg         1: /*     $OpenBSD: utilities.c,v 1.19 2014/07/20 12:08:55 guenther Exp $ */
1.2       niklas      2: /*     $NetBSD: utilities.c,v 1.5 1996/02/28 21:04:21 thorpej Exp $    */
                      3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.13      guenther   33: /* these three defines affect the behavior of <arpa/telnet.h> */
1.1       deraadt    34: #define        TELOPTS
                     35: #define        TELCMDS
                     36: #define        SLC_NAMES
                     37:
1.5       art        38: #include "telnet_locl.h"
1.14      guenther   39:
1.16      guenther   40: #include <arpa/telnet.h>
1.14      guenther   41: #include <ctype.h>
1.15      guenther   42: #include <limits.h>
1.13      guenther   43: #include <poll.h>
1.16      guenther   44: #include <stdlib.h>
                     45: #include <string.h>
1.1       deraadt    46:
1.17      guenther   47: static FILE    *NetTrace = NULL;
1.1       deraadt    48: int    prettydump;
                     49:
                     50: /*
                     51:  * upcase()
                     52:  *
                     53:  *     Upcase (in place) the argument.
                     54:  */
                     55:
1.14      guenther   56: void
                     57: upcase(char *argument)
1.1       deraadt    58: {
1.14      guenther   59:        int c;
1.1       deraadt    60:
1.14      guenther   61:        while ((c = *argument) != '\0')
                     62:                *argument++ = toupper((unsigned char)c);
1.1       deraadt    63: }
                     64:
                     65: /*
                     66:  * The following are routines used to print out debugging information.
                     67:  */
                     68:
1.15      guenther   69: unsigned char NetTraceFile[PATH_MAX] = "(standard output)";
1.1       deraadt    70:
1.20    ! jsg        71: void
        !            72: SetNetTrace(char *file)
1.1       deraadt    73: {
                     74:     if (NetTrace && NetTrace != stdout)
                     75:        fclose(NetTrace);
                     76:     if (file  && (strcmp(file, "-") != 0)) {
                     77:        NetTrace = fopen(file, "w");
                     78:        if (NetTrace) {
1.7       itojun     79:            strlcpy((char *)NetTraceFile, file, sizeof(NetTraceFile));
1.1       deraadt    80:            return;
                     81:        }
                     82:        fprintf(stderr, "Cannot open %s.\n", file);
                     83:     }
                     84:     NetTrace = stdout;
1.8       hin        85:     strlcpy((char *)NetTraceFile, "(standard output)", sizeof(NetTraceFile));
1.1       deraadt    86: }
                     87:
1.20    ! jsg        88: void
        !            89: Dump(char direction, unsigned char *buffer, int length)
1.1       deraadt    90: {
                     91: #   define BYTES_PER_LINE      32
                     92: #   define min(x,y)    ((x<y)? x:y)
                     93:     unsigned char *pThis;
                     94:     int offset;
                     95:
                     96:     offset = 0;
                     97:
                     98:     while (length) {
                     99:        /* print one line */
                    100:        fprintf(NetTrace, "%c 0x%x\t", direction, offset);
                    101:        pThis = buffer;
                    102:        if (prettydump) {
                    103:            buffer = buffer + min(length, BYTES_PER_LINE/2);
                    104:            while (pThis < buffer) {
                    105:                fprintf(NetTrace, "%c%.2x",
                    106:                    (((*pThis)&0xff) == 0xff) ? '*' : ' ',
                    107:                    (*pThis)&0xff);
                    108:                pThis++;
                    109:            }
                    110:            length -= BYTES_PER_LINE/2;
                    111:            offset += BYTES_PER_LINE/2;
                    112:        } else {
                    113:            buffer = buffer + min(length, BYTES_PER_LINE);
                    114:            while (pThis < buffer) {
                    115:                fprintf(NetTrace, "%.2x", (*pThis)&0xff);
                    116:                pThis++;
                    117:            }
                    118:            length -= BYTES_PER_LINE;
                    119:            offset += BYTES_PER_LINE;
                    120:        }
                    121:        if (NetTrace == stdout) {
                    122:            fprintf(NetTrace, "\r\n");
                    123:        } else {
                    124:            fprintf(NetTrace, "\n");
                    125:        }
                    126:        if (length < 0) {
                    127:            fflush(NetTrace);
                    128:            return;
                    129:        }
                    130:        /* find next unique line */
                    131:     }
                    132:     fflush(NetTrace);
                    133: }
                    134:
1.20    ! jsg       135: void
        !           136: printoption(char *direction, int cmd, int option)
1.1       deraadt   137: {
                    138:        if (!showoptions)
                    139:                return;
                    140:        if (cmd == IAC) {
                    141:                if (TELCMD_OK(option))
                    142:                    fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
                    143:                else
                    144:                    fprintf(NetTrace, "%s IAC %d", direction, option);
                    145:        } else {
1.6       mpech     146:                char *fmt;
1.1       deraadt   147:                fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
                    148:                        (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
                    149:                if (fmt) {
                    150:                    fprintf(NetTrace, "%s %s ", direction, fmt);
                    151:                    if (TELOPT_OK(option))
                    152:                        fprintf(NetTrace, "%s", TELOPT(option));
                    153:                    else if (option == TELOPT_EXOPL)
                    154:                        fprintf(NetTrace, "EXOPL");
                    155:                    else
                    156:                        fprintf(NetTrace, "%d", option);
                    157:                } else
                    158:                    fprintf(NetTrace, "%s %d %d", direction, cmd, option);
                    159:        }
                    160:        if (NetTrace == stdout) {
                    161:            fprintf(NetTrace, "\r\n");
                    162:            fflush(NetTrace);
                    163:        } else {
                    164:            fprintf(NetTrace, "\n");
                    165:        }
                    166:        return;
                    167: }
                    168:
1.20    ! jsg       169: void
        !           170: optionstatus(void)
1.1       deraadt   171: {
1.6       mpech     172:     int i;
1.1       deraadt   173:
                    174:     for (i = 0; i < 256; i++) {
                    175:        if (do_dont_resp[i]) {
                    176:            if (TELOPT_OK(i))
                    177:                printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
                    178:            else if (TELCMD_OK(i))
                    179:                printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
                    180:            else
                    181:                printf("resp DO_DONT %d: %d\n", i,
                    182:                                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:            } else {
                    191:                if (TELOPT_OK(i))
                    192:                    printf("want DONT %s\n", TELOPT(i));
                    193:                else if (TELCMD_OK(i))
                    194:                    printf("want DONT %s\n", TELCMD(i));
                    195:                else
                    196:                    printf("want DONT %d\n", i);
                    197:            }
                    198:        } else {
                    199:            if (my_state_is_do(i)) {
                    200:                if (TELOPT_OK(i))
                    201:                    printf("     DO   %s\n", TELOPT(i));
                    202:                else if (TELCMD_OK(i))
                    203:                    printf("     DO   %s\n", TELCMD(i));
                    204:                else
                    205:                    printf("     DO   %d\n", i);
                    206:            }
                    207:        }
                    208:        if (will_wont_resp[i]) {
                    209:            if (TELOPT_OK(i))
                    210:                printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
                    211:            else if (TELCMD_OK(i))
                    212:                printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
                    213:            else
                    214:                printf("resp WILL_WONT %d: %d\n",
                    215:                                i, will_wont_resp[i]);
                    216:            if (my_want_state_is_will(i)) {
                    217:                if (TELOPT_OK(i))
                    218:                    printf("want WILL %s\n", TELOPT(i));
                    219:                else if (TELCMD_OK(i))
                    220:                    printf("want WILL %s\n", TELCMD(i));
                    221:                else
                    222:                    printf("want WILL %d\n", i);
                    223:            } else {
                    224:                if (TELOPT_OK(i))
                    225:                    printf("want WONT %s\n", TELOPT(i));
                    226:                else if (TELCMD_OK(i))
                    227:                    printf("want WONT %s\n", TELCMD(i));
                    228:                else
                    229:                    printf("want WONT %d\n", i);
                    230:            }
                    231:        } else {
                    232:            if (my_state_is_will(i)) {
                    233:                if (TELOPT_OK(i))
                    234:                    printf("     WILL %s\n", TELOPT(i));
                    235:                else if (TELCMD_OK(i))
                    236:                    printf("     WILL %s\n", TELCMD(i));
                    237:                else
                    238:                    printf("     WILL %d\n", i);
                    239:            }
                    240:        }
                    241:     }
                    242:
                    243: }
                    244:
1.20    ! jsg       245: void
        !           246: printsub(char direction,       /* '<' or '>' */
        !           247:     unsigned char *pointer,    /* where suboption data sits */
        !           248:     int length)                        /* length of suboption data */
1.1       deraadt   249: {
1.6       mpech     250:     int i;
1.1       deraadt   251:
                    252:     if (showoptions || direction == 0 ||
                    253:        (want_status_response && (pointer[0] == TELOPT_STATUS))) {
                    254:        if (direction) {
                    255:            fprintf(NetTrace, "%s IAC SB ",
                    256:                                (direction == '<')? "RCVD":"SENT");
                    257:            if (length >= 3) {
1.6       mpech     258:                int j;
1.1       deraadt   259:
                    260:                i = pointer[length-2];
                    261:                j = pointer[length-1];
                    262:
                    263:                if (i != IAC || j != SE) {
                    264:                    fprintf(NetTrace, "(terminated by ");
                    265:                    if (TELOPT_OK(i))
                    266:                        fprintf(NetTrace, "%s ", TELOPT(i));
                    267:                    else if (TELCMD_OK(i))
                    268:                        fprintf(NetTrace, "%s ", TELCMD(i));
                    269:                    else
                    270:                        fprintf(NetTrace, "%d ", i);
                    271:                    if (TELOPT_OK(j))
                    272:                        fprintf(NetTrace, "%s", TELOPT(j));
                    273:                    else if (TELCMD_OK(j))
                    274:                        fprintf(NetTrace, "%s", TELCMD(j));
                    275:                    else
                    276:                        fprintf(NetTrace, "%d", j);
                    277:                    fprintf(NetTrace, ", not IAC SE!) ");
                    278:                }
                    279:            }
                    280:            length -= 2;
                    281:        }
                    282:        if (length < 1) {
                    283:            fprintf(NetTrace, "(Empty suboption??\?)");
                    284:            if (NetTrace == stdout)
                    285:                fflush(NetTrace);
                    286:            return;
                    287:        }
                    288:        switch (pointer[0]) {
                    289:        case TELOPT_TTYPE:
                    290:            fprintf(NetTrace, "TERMINAL-TYPE ");
                    291:            switch (pointer[1]) {
                    292:            case TELQUAL_IS:
                    293:                fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
                    294:                break;
                    295:            case TELQUAL_SEND:
                    296:                fprintf(NetTrace, "SEND");
                    297:                break;
                    298:            default:
                    299:                fprintf(NetTrace,
                    300:                                "- unknown qualifier %d (0x%x).",
                    301:                                pointer[1], pointer[1]);
                    302:            }
                    303:            break;
                    304:        case TELOPT_TSPEED:
                    305:            fprintf(NetTrace, "TERMINAL-SPEED");
                    306:            if (length < 2) {
                    307:                fprintf(NetTrace, " (empty suboption??\?)");
                    308:                break;
                    309:            }
                    310:            switch (pointer[1]) {
                    311:            case TELQUAL_IS:
                    312:                fprintf(NetTrace, " IS ");
                    313:                fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
                    314:                break;
                    315:            default:
                    316:                if (pointer[1] == 1)
                    317:                    fprintf(NetTrace, " SEND");
                    318:                else
                    319:                    fprintf(NetTrace, " %d (unknown)", pointer[1]);
                    320:                for (i = 2; i < length; i++)
                    321:                    fprintf(NetTrace, " ?%d?", pointer[i]);
                    322:                break;
                    323:            }
                    324:            break;
                    325:
                    326:        case TELOPT_LFLOW:
                    327:            fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
                    328:            if (length < 2) {
                    329:                fprintf(NetTrace, " (empty suboption??\?)");
                    330:                break;
                    331:            }
                    332:            switch (pointer[1]) {
                    333:            case LFLOW_OFF:
                    334:                fprintf(NetTrace, " OFF"); break;
                    335:            case LFLOW_ON:
                    336:                fprintf(NetTrace, " ON"); break;
                    337:            case LFLOW_RESTART_ANY:
                    338:                fprintf(NetTrace, " RESTART-ANY"); break;
                    339:            case LFLOW_RESTART_XON:
                    340:                fprintf(NetTrace, " RESTART-XON"); break;
                    341:            default:
                    342:                fprintf(NetTrace, " %d (unknown)", pointer[1]);
                    343:            }
                    344:            for (i = 2; i < length; i++)
                    345:                fprintf(NetTrace, " ?%d?", pointer[i]);
                    346:            break;
                    347:
                    348:        case TELOPT_NAWS:
                    349:            fprintf(NetTrace, "NAWS");
                    350:            if (length < 2) {
                    351:                fprintf(NetTrace, " (empty suboption??\?)");
                    352:                break;
                    353:            }
                    354:            if (length == 2) {
                    355:                fprintf(NetTrace, " ?%d?", pointer[1]);
                    356:                break;
                    357:            }
                    358:            fprintf(NetTrace, " %d %d (%d)",
                    359:                pointer[1], pointer[2],
                    360:                (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
                    361:            if (length == 4) {
                    362:                fprintf(NetTrace, " ?%d?", pointer[3]);
                    363:                break;
                    364:            }
                    365:            fprintf(NetTrace, " %d %d (%d)",
                    366:                pointer[3], pointer[4],
                    367:                (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
                    368:            for (i = 5; i < length; i++)
                    369:                fprintf(NetTrace, " ?%d?", pointer[i]);
                    370:            break;
                    371:
                    372:        case TELOPT_LINEMODE:
                    373:            fprintf(NetTrace, "LINEMODE ");
                    374:            if (length < 2) {
                    375:                fprintf(NetTrace, " (empty suboption??\?)");
                    376:                break;
                    377:            }
                    378:            switch (pointer[1]) {
                    379:            case WILL:
                    380:                fprintf(NetTrace, "WILL ");
                    381:                goto common;
                    382:            case WONT:
                    383:                fprintf(NetTrace, "WONT ");
                    384:                goto common;
                    385:            case DO:
                    386:                fprintf(NetTrace, "DO ");
                    387:                goto common;
                    388:            case DONT:
                    389:                fprintf(NetTrace, "DONT ");
                    390:            common:
                    391:                if (length < 3) {
                    392:                    fprintf(NetTrace, "(no option??\?)");
                    393:                    break;
                    394:                }
                    395:                switch (pointer[2]) {
                    396:                case LM_FORWARDMASK:
                    397:                    fprintf(NetTrace, "Forward Mask");
                    398:                    for (i = 3; i < length; i++)
                    399:                        fprintf(NetTrace, " %x", pointer[i]);
                    400:                    break;
                    401:                default:
                    402:                    fprintf(NetTrace, "%d (unknown)", pointer[2]);
                    403:                    for (i = 3; i < length; i++)
                    404:                        fprintf(NetTrace, " %d", pointer[i]);
                    405:                    break;
                    406:                }
                    407:                break;
1.2       niklas    408:
1.1       deraadt   409:            case LM_SLC:
                    410:                fprintf(NetTrace, "SLC");
                    411:                for (i = 2; i < length - 2; i += 3) {
                    412:                    if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
                    413:                        fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
                    414:                    else
                    415:                        fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
                    416:                    switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
                    417:                    case SLC_NOSUPPORT:
                    418:                        fprintf(NetTrace, " NOSUPPORT"); break;
                    419:                    case SLC_CANTCHANGE:
                    420:                        fprintf(NetTrace, " CANTCHANGE"); break;
                    421:                    case SLC_VARIABLE:
                    422:                        fprintf(NetTrace, " VARIABLE"); break;
                    423:                    case SLC_DEFAULT:
                    424:                        fprintf(NetTrace, " DEFAULT"); break;
                    425:                    }
                    426:                    fprintf(NetTrace, "%s%s%s",
                    427:                        pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
                    428:                        pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
                    429:                        pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
                    430:                    if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
                    431:                                                SLC_FLUSHOUT| SLC_LEVELBITS))
                    432:                        fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
                    433:                    fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
                    434:                    if ((pointer[i+SLC_VALUE] == IAC) &&
                    435:                        (pointer[i+SLC_VALUE+1] == IAC))
                    436:                                i++;
                    437:                }
                    438:                for (; i < length; i++)
                    439:                    fprintf(NetTrace, " ?%d?", pointer[i]);
                    440:                break;
                    441:
                    442:            case LM_MODE:
                    443:                fprintf(NetTrace, "MODE ");
                    444:                if (length < 3) {
                    445:                    fprintf(NetTrace, "(no mode??\?)");
                    446:                    break;
                    447:                }
                    448:                {
                    449:                    char tbuf[64];
1.5       art       450:                    snprintf(tbuf, sizeof(tbuf),
                    451:                             "%s%s%s%s%s",
                    452:                             pointer[2]&MODE_EDIT ? "|EDIT" : "",
                    453:                             pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
                    454:                             pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
                    455:                             pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
                    456:                             pointer[2]&MODE_ACK ? "|ACK" : "");
1.1       deraadt   457:                    fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
                    458:                }
                    459:                if (pointer[2]&~(MODE_MASK))
                    460:                    fprintf(NetTrace, " (0x%x)", pointer[2]);
                    461:                for (i = 3; i < length; i++)
                    462:                    fprintf(NetTrace, " ?0x%x?", pointer[i]);
                    463:                break;
                    464:            default:
                    465:                fprintf(NetTrace, "%d (unknown)", pointer[1]);
                    466:                for (i = 2; i < length; i++)
                    467:                    fprintf(NetTrace, " %d", pointer[i]);
                    468:            }
                    469:            break;
                    470:
                    471:        case TELOPT_STATUS: {
1.6       mpech     472:            char *cp;
                    473:            int j, k;
1.1       deraadt   474:
                    475:            fprintf(NetTrace, "STATUS");
                    476:
                    477:            switch (pointer[1]) {
                    478:            default:
                    479:                if (pointer[1] == TELQUAL_SEND)
                    480:                    fprintf(NetTrace, " SEND");
                    481:                else
                    482:                    fprintf(NetTrace, " %d (unknown)", pointer[1]);
                    483:                for (i = 2; i < length; i++)
                    484:                    fprintf(NetTrace, " ?%d?", pointer[i]);
                    485:                break;
                    486:            case TELQUAL_IS:
                    487:                if (--want_status_response < 0)
                    488:                    want_status_response = 0;
                    489:                if (NetTrace == stdout)
                    490:                    fprintf(NetTrace, " IS\r\n");
                    491:                else
                    492:                    fprintf(NetTrace, " IS\n");
                    493:
                    494:                for (i = 2; i < length; i++) {
                    495:                    switch(pointer[i]) {
                    496:                    case DO:    cp = "DO"; goto common2;
                    497:                    case DONT:  cp = "DONT"; goto common2;
                    498:                    case WILL:  cp = "WILL"; goto common2;
                    499:                    case WONT:  cp = "WONT"; goto common2;
                    500:                    common2:
                    501:                        i++;
                    502:                        if (TELOPT_OK((int)pointer[i]))
                    503:                            fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
                    504:                        else
                    505:                            fprintf(NetTrace, " %s %d", cp, pointer[i]);
                    506:
                    507:                        if (NetTrace == stdout)
                    508:                            fprintf(NetTrace, "\r\n");
                    509:                        else
                    510:                            fprintf(NetTrace, "\n");
                    511:                        break;
                    512:
                    513:                    case SB:
                    514:                        fprintf(NetTrace, " SB ");
                    515:                        i++;
                    516:                        j = k = i;
                    517:                        while (j < length) {
                    518:                            if (pointer[j] == SE) {
                    519:                                if (j+1 == length)
                    520:                                    break;
                    521:                                if (pointer[j+1] == SE)
                    522:                                    j++;
                    523:                                else
                    524:                                    break;
                    525:                            }
                    526:                            pointer[k++] = pointer[j++];
                    527:                        }
                    528:                        printsub(0, &pointer[i], k - i);
                    529:                        if (i < length) {
                    530:                            fprintf(NetTrace, " SE");
                    531:                            i = j;
                    532:                        } else
                    533:                            i = j - 1;
                    534:
                    535:                        if (NetTrace == stdout)
                    536:                            fprintf(NetTrace, "\r\n");
                    537:                        else
                    538:                            fprintf(NetTrace, "\n");
                    539:
                    540:                        break;
1.2       niklas    541:
1.1       deraadt   542:                    default:
                    543:                        fprintf(NetTrace, " %d", pointer[i]);
                    544:                        break;
                    545:                    }
                    546:                }
                    547:                break;
                    548:            }
                    549:            break;
                    550:          }
                    551:
                    552:        case TELOPT_XDISPLOC:
                    553:            fprintf(NetTrace, "X-DISPLAY-LOCATION ");
                    554:            switch (pointer[1]) {
                    555:            case TELQUAL_IS:
                    556:                fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
                    557:                break;
                    558:            case TELQUAL_SEND:
                    559:                fprintf(NetTrace, "SEND");
                    560:                break;
                    561:            default:
                    562:                fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
                    563:                                pointer[1], pointer[1]);
                    564:            }
                    565:            break;
                    566:
                    567:        case TELOPT_NEW_ENVIRON:
                    568:            fprintf(NetTrace, "NEW-ENVIRON ");
                    569:            switch (pointer[1]) {
                    570:            case TELQUAL_IS:
                    571:                fprintf(NetTrace, "IS ");
                    572:                goto env_common;
                    573:            case TELQUAL_SEND:
                    574:                fprintf(NetTrace, "SEND ");
                    575:                goto env_common;
                    576:            case TELQUAL_INFO:
                    577:                fprintf(NetTrace, "INFO ");
                    578:            env_common:
                    579:                {
1.6       mpech     580:                    int noquote = 2;
1.1       deraadt   581:                    for (i = 2; i < length; i++ ) {
                    582:                        switch (pointer[i]) {
                    583:                        case NEW_ENV_VALUE:
                    584:                                fprintf(NetTrace, "\" VALUE " + noquote);
                    585:                            noquote = 2;
                    586:                            break;
                    587:
                    588:                        case NEW_ENV_VAR:
                    589:                                fprintf(NetTrace, "\" VAR " + noquote);
                    590:                            noquote = 2;
                    591:                            break;
                    592:
                    593:                        case ENV_ESC:
                    594:                            fprintf(NetTrace, "\" ESC " + noquote);
                    595:                            noquote = 2;
                    596:                            break;
                    597:
                    598:                        case ENV_USERVAR:
                    599:                            fprintf(NetTrace, "\" USERVAR " + noquote);
                    600:                            noquote = 2;
                    601:                            break;
                    602:
                    603:                        default:
1.14      guenther  604:                            if (isprint((unsigned char)pointer[i]) &&
                    605:                                pointer[i] != '"') {
1.1       deraadt   606:                                if (noquote) {
                    607:                                    putc('"', NetTrace);
                    608:                                    noquote = 0;
                    609:                                }
                    610:                                putc(pointer[i], NetTrace);
                    611:                            } else {
                    612:                                fprintf(NetTrace, "\" %03o " + noquote,
                    613:                                                        pointer[i]);
                    614:                                noquote = 2;
                    615:                            }
                    616:                            break;
                    617:                        }
                    618:                    }
                    619:                    if (!noquote)
                    620:                        putc('"', NetTrace);
                    621:                    break;
                    622:                }
                    623:            }
                    624:            break;
                    625:
                    626:        default:
                    627:            if (TELOPT_OK(pointer[0]))
                    628:                fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
                    629:            else
                    630:                fprintf(NetTrace, "%d (unknown)", pointer[0]);
                    631:            for (i = 1; i < length; i++)
                    632:                fprintf(NetTrace, " %d", pointer[i]);
                    633:            break;
                    634:        }
                    635:        if (direction) {
                    636:            if (NetTrace == stdout)
                    637:                fprintf(NetTrace, "\r\n");
                    638:            else
                    639:                fprintf(NetTrace, "\n");
                    640:        }
                    641:        if (NetTrace == stdout)
                    642:            fflush(NetTrace);
                    643:     }
                    644: }
                    645:
                    646: /* EmptyTerminal - called to make sure that the terminal buffer is empty.
                    647:  *                     Note that we consider the buffer to run all the
1.11      millert   648:  *                     way to the kernel (thus the poll).
1.1       deraadt   649:  */
                    650:
1.20    ! jsg       651: void
        !           652: EmptyTerminal(void)
1.1       deraadt   653: {
1.11      millert   654:     struct pollfd pfd[1];
1.1       deraadt   655:
1.11      millert   656:     pfd[0].fd = tout;
                    657:     pfd[0].events = POLLOUT;
1.1       deraadt   658:
                    659:     if (TTYBYTES() == 0) {
1.18      guenther  660:        (void) poll(pfd, 1, INFTIM); /* wait for TTLOWAT */
1.1       deraadt   661:     } else {
                    662:        while (TTYBYTES()) {
                    663:            (void) ttyflush(0);
1.18      guenther  664:            (void) poll(pfd, 1, INFTIM); /* wait for TTLOWAT */
1.1       deraadt   665:        }
                    666:     }
                    667: }
                    668:
1.20    ! jsg       669: void
        !           670: SetForExit(void)
1.1       deraadt   671: {
                    672:     setconnmode(0);
                    673:     do {
                    674:        (void)telrcv();                 /* Process any incoming data */
                    675:        EmptyTerminal();
                    676:     } while (ring_full_count(&netiring));      /* While there is any */
                    677:     setcommandmode();
                    678:     fflush(stdout);
                    679:     fflush(stderr);
                    680:     setconnmode(0);
                    681:     EmptyTerminal();                   /* Flush the path to the tty */
                    682:     setcommandmode();
                    683: }
                    684:
1.20    ! jsg       685: void
        !           686: Exit(int returnCode)
1.1       deraadt   687: {
                    688:     SetForExit();
                    689:     exit(returnCode);
                    690: }
                    691:
1.20    ! jsg       692: void
        !           693: ExitString(char *string, int returnCode)
1.1       deraadt   694: {
                    695:     SetForExit();
                    696:     fwrite(string, 1, strlen(string), stderr);
                    697:     exit(returnCode);
                    698: }