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

Annotation of src/usr.bin/hexdump/odsyntax.c, Revision 1.18

1.18    ! nicm        1: /*     $OpenBSD: odsyntax.c,v 1.17 2009/10/27 23:59:39 deraadt Exp $   */
1.9       pvalchev    2: /*     $NetBSD: odsyntax.c,v 1.15 2001/12/07 15:14:29 bjh21 Exp $      */
1.2       deraadt     3:
1.1       deraadt     4: /*-
1.9       pvalchev    5:  * Copyright (c) 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     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.12      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:
                     33: #include <sys/types.h>
1.9       pvalchev   34:
1.4       deraadt    35: #include <ctype.h>
1.7       mickey     36: #include <err.h>
1.9       pvalchev   37: #include <stdio.h>
                     38: #include <stdlib.h>
                     39: #include <unistd.h>
                     40:
1.1       deraadt    41: #include "hexdump.h"
                     42:
                     43: int deprecated;
                     44:
1.10      millert    45: static void odoffset(int, char ***);
                     46: static void posixtypes(char *);
                     47: static void odprecede(void);
1.9       pvalchev   48:
                     49:
                     50: /*
                     51:  * formats used for -t
                     52:  */
                     53: static const char *fmt[4][4] = {
                     54:        {
                     55:                "16/1 \"%3d \" \"\\n\"",
                     56:                "8/2  \"  %05d \" \"\\n\"",
                     57:                "4/4  \"     %010d \" \"\\n\"",
                     58:                "2/8  \" %019d \" \"\\n\""
                     59:        }, {
                     60:                "16/1 \"%03o \" \"\\n\"",
                     61:                "8/2  \" %06o \" \"\\n\"",
                     62:                "4/4  \"    %011o\" \"\\n\"",
                     63:                "2/8  \" %022o \" \"\\n\""
                     64:        }, {
                     65:                "16/1 \"%03u \" \"\\n\"",
                     66:                "8/2  \"  %05u \" \"\\n\"",
                     67:                "4/4  \"     %010u \" \"\\n\"",
                     68:                "2/8  \" %020u \" \"\\n\""
                     69:        }, {
                     70:                "16/1 \" %02x \" \"\\n\"",
                     71:                "8/2  \"   %04x \" \"\\n\"",
                     72:                "4/4  \"       %08x \" \"\\n\"",
                     73:                "2/8  \" %16x \" \"\\n\""
                     74:        }
                     75: };
                     76:
1.6       pvalchev   77: void
1.13      deraadt    78: oldsyntax(int argc, char ***argvp)
1.1       deraadt    79: {
                     80:        int ch;
1.9       pvalchev   81:        char *p, **argv;
1.1       deraadt    82:
                     83:        deprecated = 1;
                     84:        argv = *argvp;
1.9       pvalchev   85:        while ((ch = getopt(argc, argv,
                     86:            "aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
1.1       deraadt    87:                switch (ch) {
                     88:                case 'a':
                     89:                        odprecede();
                     90:                        add("16/1 \"%3_u \" \"\\n\"");
                     91:                        break;
                     92:                case 'B':
                     93:                case 'o':
                     94:                        odprecede();
                     95:                        add("8/2 \" %06o \" \"\\n\"");
                     96:                        break;
                     97:                case 'b':
                     98:                        odprecede();
                     99:                        add("16/1 \"%03o \" \"\\n\"");
                    100:                        break;
                    101:                case 'c':
                    102:                        odprecede();
                    103:                        add("16/1 \"%3_c \" \"\\n\"");
                    104:                        break;
                    105:                case 'd':
                    106:                        odprecede();
                    107:                        add("8/2 \"  %05u \" \"\\n\"");
                    108:                        break;
                    109:                case 'D':
                    110:                        odprecede();
                    111:                        add("4/4 \"     %010u \" \"\\n\"");
                    112:                        break;
1.16      jmc       113:                case 'e':
1.1       deraadt   114:                case 'F':
                    115:                        odprecede();
                    116:                        add("2/8 \"          %21.14e \" \"\\n\"");
                    117:                        break;
                    118:
                    119:                case 'f':
                    120:                        odprecede();
                    121:                        add("4/4 \" %14.7e \" \"\\n\"");
                    122:                        break;
                    123:                case 'H':
                    124:                case 'X':
                    125:                        odprecede();
                    126:                        add("4/4 \"       %08x \" \"\\n\"");
                    127:                        break;
                    128:                case 'h':
                    129:                case 'x':
                    130:                        odprecede();
                    131:                        add("8/2 \"   %04x \" \"\\n\"");
                    132:                        break;
                    133:                case 'I':
                    134:                case 'L':
                    135:                case 'l':
                    136:                        odprecede();
                    137:                        add("4/4 \"    %11d \" \"\\n\"");
                    138:                        break;
                    139:                case 'i':
                    140:                        odprecede();
                    141:                        add("8/2 \" %6d \" \"\\n\"");
                    142:                        break;
1.9       pvalchev  143:                case 'j':
                    144:                        if ((skip = strtol(optarg, &p, 0)) < 0)
                    145:                                errx(1, "%s: bad skip value", optarg);
                    146:                        switch(*p) {
                    147:                        case 'b':
                    148:                                skip *= 512;
                    149:                                break;
                    150:                        case 'k':
                    151:                                skip *= 1024;
                    152:                                break;
                    153:                        case 'm':
                    154:                                skip *= 1048576;
                    155:                                break;
                    156:                        }
                    157:                        break;
                    158:                case 'N':
                    159:                        if ((length = atoi(optarg)) < 0)
                    160:                                errx(1, "%s: bad length value", optarg);
                    161:                        break;
1.1       deraadt   162:                case 'O':
                    163:                        odprecede();
                    164:                        add("4/4 \"    %011o \" \"\\n\"");
                    165:                        break;
1.9       pvalchev  166:                case 't':
                    167:                        posixtypes(optarg);
                    168:                        break;
1.1       deraadt   169:                case 'v':
                    170:                        vflag = ALL;
                    171:                        break;
                    172:                case 'P':
                    173:                case 'p':
                    174:                case 's':
                    175:                case 'w':
                    176:                case '?':
                    177:                default:
1.9       pvalchev  178:                        warnx("od(1) has been deprecated for hexdump(1).");
1.1       deraadt   179:                        if (ch != '?')
1.9       pvalchev  180:                                warnx(
1.14      deraadt   181:                                    "hexdump(1) compatibility doesn't"
                    182:                                    " support the -%c option%s",
1.1       deraadt   183:                                    ch, ch == 's' ? "; see strings(1)." : ".");
1.11      millert   184:                        oldusage();
1.1       deraadt   185:                }
                    186:
                    187:        if (!fshead) {
                    188:                add("\"%07.7_Ao\n\"");
                    189:                add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
                    190:        }
                    191:
                    192:        argc -= optind;
                    193:        *argvp += optind;
                    194:
1.9       pvalchev  195:        if (argc)
                    196:                odoffset(argc, argvp);
1.1       deraadt   197: }
                    198:
1.9       pvalchev  199: /*
                    200:  * Interpret a POSIX-style -t argument.
                    201:  */
                    202: static void
1.13      deraadt   203: posixtypes(char *type_string)
1.9       pvalchev  204: {
                    205:        int x, y, nbytes;
1.1       deraadt   206:
1.9       pvalchev  207:        while (*type_string) {
                    208:                odprecede();
                    209:                switch (*type_string) {
                    210:                case 'a':
                    211:                        type_string++;
                    212:                        add("16/1 \"%3_u \" \"\\n\"");
                    213:                        break;
                    214:                case 'c':
                    215:                        type_string++;
                    216:                        add("16/1 \"%3_c \" \"\\n\"");
                    217:                        break;
                    218:                case 'f':
                    219:                        type_string++;
                    220:                        if        (*type_string == 'F' ||
                    221:                                   *type_string == '4') {
                    222:                                type_string++;
                    223:                                add("4/4 \" %14.7e\" \"\\n\"");
                    224:                        } else if (*type_string == 'L' ||
                    225:                                   *type_string == '8') {
                    226:                                type_string++;
                    227:                                add("2/8 \" %16.14e\" \"\\n\"");
                    228:                        } else if (*type_string == 'D')
                    229:                                /* long doubles vary in size */
1.11      millert   230:                                oldusage();
1.9       pvalchev  231:                        else
                    232:                                add("2/8 \" %16.14e\" \"\\n\"");
                    233:                        break;
                    234:                case 'd':
                    235:                        x = 0;
                    236:                        goto extensions;
                    237:                case 'o':
                    238:                        x = 1;
                    239:                        goto extensions;
                    240:                case 'u':
                    241:                        x = 2;
                    242:                        goto extensions;
                    243:                case 'x':
                    244:                        x = 3;
                    245:                extensions:
                    246:                        type_string++;
                    247:                        y = 2;
                    248:                        if (isupper(*type_string)) {
                    249:                                switch(*type_string) {
                    250:                                case 'C':
                    251:                                        nbytes = sizeof(char);
                    252:                                        break;
                    253:                                case 'S':
                    254:                                        nbytes = sizeof(short);
                    255:                                        break;
                    256:                                case 'I':
                    257:                                        nbytes = sizeof(int);
                    258:                                        break;
                    259:                                case 'L':
                    260:                                        nbytes = sizeof(long);
                    261:                                        break;
                    262:                                default:
                    263:                                        warnx("Bad type-size qualifier '%c'",
                    264:                                            *type_string);
1.11      millert   265:                                        oldusage();
1.9       pvalchev  266:                                }
                    267:                                type_string++;
                    268:                        } else if (isdigit(*type_string))
                    269:                                nbytes = strtol(type_string, &type_string, 10);
1.18    ! nicm      270:                        else
        !           271:                                nbytes = 4;
1.9       pvalchev  272:
                    273:                        switch (nbytes) {
                    274:                        case 1:
                    275:                                y = 0;
                    276:                                break;
                    277:                        case 2:
                    278:                                y = 1;
                    279:                                break;
                    280:                        case 4:
                    281:                                y = 2;
                    282:                                break;
                    283:                        case 8:
                    284:                                y = 3;
                    285:                                break;
                    286:                        default:
                    287:                                warnx("%d-byte integer formats are not "
                    288:                                    "supported", nbytes);
1.11      millert   289:                                oldusage();
1.9       pvalchev  290:                        }
                    291:                        add(fmt[x][y]);
                    292:                        break;
                    293:                default:
1.11      millert   294:                        oldusage();
1.9       pvalchev  295:                }
                    296:        }
1.11      millert   297: }
                    298:
                    299: void
1.13      deraadt   300: oldusage(void)
1.11      millert   301: {
                    302:        extern char *__progname;
1.15      jmc       303:        fprintf(stderr, "usage: %s [-aBbcDdeFfHhIiLlOovXx] [-j offset] "
1.14      deraadt   304:            "[-N length] [-t type_string]\n"
1.15      jmc       305:            "\t[[+]offset[.][Bb]] [file ...]\n", __progname);
1.11      millert   306:        exit(1);
1.9       pvalchev  307: }
                    308:
                    309: static void
1.13      deraadt   310: odoffset(int argc, char ***argvp)
1.1       deraadt   311: {
1.8       mpech     312:        char *num, *p;
1.1       deraadt   313:        int base;
                    314:        char *end;
                    315:
                    316:        /*
                    317:         * The offset syntax of od(1) was genuinely bizarre.  First, if
                    318:         * it started with a plus it had to be an offset.  Otherwise, if
                    319:         * there were at least two arguments, a number or lower-case 'x'
                    320:         * followed by a number makes it an offset.  By default it was
                    321:         * octal; if it started with 'x' or '0x' it was hex.  If it ended
                    322:         * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
                    323:         * multiplied the number by 512 or 1024 byte units.  There was
                    324:         * no way to assign a block count to a hex offset.
                    325:         *
1.9       pvalchev  326:         * We assume it's a file if the offset is bad.
1.1       deraadt   327:         */
1.9       pvalchev  328:        p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
1.1       deraadt   329:        if (!p)
                    330:                return;
1.9       pvalchev  331:
1.1       deraadt   332:        if (*p != '+' && (argc < 2 ||
1.9       pvalchev  333:            (!isdigit((unsigned char)p[0]) &&
                    334:            (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
1.1       deraadt   335:                return;
                    336:
                    337:        base = 0;
                    338:        /*
                    339:         * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
                    340:         * set base.
                    341:         */
                    342:        if (p[0] == '+')
                    343:                ++p;
1.9       pvalchev  344:        if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
1.1       deraadt   345:                ++p;
                    346:                base = 16;
                    347:        } else if (p[0] == '0' && p[1] == 'x') {
                    348:                p += 2;
                    349:                base = 16;
                    350:        }
                    351:
                    352:        /* skip over the number */
                    353:        if (base == 16)
1.9       pvalchev  354:                for (num = p; isxdigit((unsigned char)*p); ++p);
1.1       deraadt   355:        else
1.9       pvalchev  356:                for (num = p; isdigit((unsigned char)*p); ++p);
1.1       deraadt   357:
                    358:        /* check for no number */
                    359:        if (num == p)
                    360:                return;
                    361:
                    362:        /* if terminates with a '.', base is decimal */
                    363:        if (*p == '.') {
                    364:                if (base)
                    365:                        return;
                    366:                base = 10;
                    367:        }
                    368:
                    369:        skip = strtol(num, &end, base ? base : 8);
                    370:
                    371:        /* if end isn't the same as p, we got a non-octal digit */
1.9       pvalchev  372:        if (end != p) {
1.1       deraadt   373:                skip = 0;
1.9       pvalchev  374:                return;
                    375:        }
                    376:
                    377:        if (*p) {
                    378:                if (*p == 'B') {
                    379:                        skip *= 1024;
                    380:                        ++p;
                    381:                } else if (*p == 'b') {
                    382:                        skip *= 512;
1.1       deraadt   383:                        ++p;
                    384:                }
1.9       pvalchev  385:        }
                    386:        if (*p) {
                    387:                skip = 0;
                    388:                return;
                    389:        }
                    390:        /*
                    391:         * If the offset uses a non-octal base, the base of the offset
                    392:         * is changed as well.  This isn't pretty, but it's easy.
                    393:         */
1.1       deraadt   394: #define        TYPE_OFFSET     7
1.9       pvalchev  395:        if (base == 16) {
                    396:                fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
                    397:                fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
                    398:        } else if (base == 10) {
                    399:                fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
                    400:                fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
1.1       deraadt   401:        }
1.9       pvalchev  402:
                    403:        /* Terminate file list. */
                    404:        (*argvp)[1] = NULL;
1.1       deraadt   405: }
                    406:
                    407: static void
1.13      deraadt   408: odprecede(void)
1.1       deraadt   409: {
                    410:        static int first = 1;
                    411:
                    412:        if (first) {
                    413:                first = 0;
                    414:                add("\"%07.7_Ao\n\"");
                    415:                add("\"%07.7_ao  \"");
                    416:        } else
                    417:                add("\"         \"");
                    418: }