[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.17

1.17    ! deraadt     1: /*     $OpenBSD: odsyntax.c,v 1.16 2007/02/06 20:55:13 jmc 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);
                    270:
                    271:                        switch (nbytes) {
                    272:                        case 1:
                    273:                                y = 0;
                    274:                                break;
                    275:                        case 2:
                    276:                                y = 1;
                    277:                                break;
                    278:                        case 4:
                    279:                                y = 2;
                    280:                                break;
                    281:                        case 8:
                    282:                                y = 3;
                    283:                                break;
                    284:                        default:
                    285:                                warnx("%d-byte integer formats are not "
                    286:                                    "supported", nbytes);
1.11      millert   287:                                oldusage();
1.9       pvalchev  288:                        }
                    289:                        add(fmt[x][y]);
                    290:                        break;
                    291:                default:
1.11      millert   292:                        oldusage();
1.9       pvalchev  293:                }
                    294:        }
1.11      millert   295: }
                    296:
                    297: void
1.13      deraadt   298: oldusage(void)
1.11      millert   299: {
                    300:        extern char *__progname;
1.15      jmc       301:        fprintf(stderr, "usage: %s [-aBbcDdeFfHhIiLlOovXx] [-j offset] "
1.14      deraadt   302:            "[-N length] [-t type_string]\n"
1.15      jmc       303:            "\t[[+]offset[.][Bb]] [file ...]\n", __progname);
1.11      millert   304:        exit(1);
1.9       pvalchev  305: }
                    306:
                    307: static void
1.13      deraadt   308: odoffset(int argc, char ***argvp)
1.1       deraadt   309: {
1.8       mpech     310:        char *num, *p;
1.1       deraadt   311:        int base;
                    312:        char *end;
                    313:
                    314:        /*
                    315:         * The offset syntax of od(1) was genuinely bizarre.  First, if
                    316:         * it started with a plus it had to be an offset.  Otherwise, if
                    317:         * there were at least two arguments, a number or lower-case 'x'
                    318:         * followed by a number makes it an offset.  By default it was
                    319:         * octal; if it started with 'x' or '0x' it was hex.  If it ended
                    320:         * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
                    321:         * multiplied the number by 512 or 1024 byte units.  There was
                    322:         * no way to assign a block count to a hex offset.
                    323:         *
1.9       pvalchev  324:         * We assume it's a file if the offset is bad.
1.1       deraadt   325:         */
1.9       pvalchev  326:        p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
1.1       deraadt   327:        if (!p)
                    328:                return;
1.9       pvalchev  329:
1.1       deraadt   330:        if (*p != '+' && (argc < 2 ||
1.9       pvalchev  331:            (!isdigit((unsigned char)p[0]) &&
                    332:            (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
1.1       deraadt   333:                return;
                    334:
                    335:        base = 0;
                    336:        /*
                    337:         * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
                    338:         * set base.
                    339:         */
                    340:        if (p[0] == '+')
                    341:                ++p;
1.9       pvalchev  342:        if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
1.1       deraadt   343:                ++p;
                    344:                base = 16;
                    345:        } else if (p[0] == '0' && p[1] == 'x') {
                    346:                p += 2;
                    347:                base = 16;
                    348:        }
                    349:
                    350:        /* skip over the number */
                    351:        if (base == 16)
1.9       pvalchev  352:                for (num = p; isxdigit((unsigned char)*p); ++p);
1.1       deraadt   353:        else
1.9       pvalchev  354:                for (num = p; isdigit((unsigned char)*p); ++p);
1.1       deraadt   355:
                    356:        /* check for no number */
                    357:        if (num == p)
                    358:                return;
                    359:
                    360:        /* if terminates with a '.', base is decimal */
                    361:        if (*p == '.') {
                    362:                if (base)
                    363:                        return;
                    364:                base = 10;
                    365:        }
                    366:
                    367:        skip = strtol(num, &end, base ? base : 8);
                    368:
                    369:        /* if end isn't the same as p, we got a non-octal digit */
1.9       pvalchev  370:        if (end != p) {
1.1       deraadt   371:                skip = 0;
1.9       pvalchev  372:                return;
                    373:        }
                    374:
                    375:        if (*p) {
                    376:                if (*p == 'B') {
                    377:                        skip *= 1024;
                    378:                        ++p;
                    379:                } else if (*p == 'b') {
                    380:                        skip *= 512;
1.1       deraadt   381:                        ++p;
                    382:                }
1.9       pvalchev  383:        }
                    384:        if (*p) {
                    385:                skip = 0;
                    386:                return;
                    387:        }
                    388:        /*
                    389:         * If the offset uses a non-octal base, the base of the offset
                    390:         * is changed as well.  This isn't pretty, but it's easy.
                    391:         */
1.1       deraadt   392: #define        TYPE_OFFSET     7
1.9       pvalchev  393:        if (base == 16) {
                    394:                fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
                    395:                fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
                    396:        } else if (base == 10) {
                    397:                fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
                    398:                fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
1.1       deraadt   399:        }
1.9       pvalchev  400:
                    401:        /* Terminate file list. */
                    402:        (*argvp)[1] = NULL;
1.1       deraadt   403: }
                    404:
                    405: static void
1.13      deraadt   406: odprecede(void)
1.1       deraadt   407: {
                    408:        static int first = 1;
                    409:
                    410:        if (first) {
                    411:                first = 0;
                    412:                add("\"%07.7_Ao\n\"");
                    413:                add("\"%07.7_ao  \"");
                    414:        } else
                    415:                add("\"         \"");
                    416: }