Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/dump.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/libbb/dump.c

    r1765 r2725  
    55 *
    66 * Copyright (c) 1989
    7  *  The Regents of the University of California.  All rights reserved.
     7 * The Regents of the University of California.  All rights reserved.
    88 *
    9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1010 *
    1111 * Original copyright notice is retained at the end of this file.
     
    1515#include "dump.h"
    1616
    17 enum _vflag bb_dump_vflag = FIRST;
    18 FS *bb_dump_fshead;             /* head of format strings */
    19 static FU *endfu;
    20 static char **_argv;
    21 static off_t savaddress;    /* saved address/offset in stream */
    22 static off_t eaddress;  /* end address */
    23 static off_t address;   /* address/offset in stream */
    24 off_t bb_dump_skip;             /* bytes to skip */
    25 static int exitval;         /* final exit value */
    26 int bb_dump_blocksize;          /* data block size */
    27 int bb_dump_length = -1;        /* max bytes to read */
    28 
    2917static const char index_str[] ALIGN1 = ".#-+ 0123456789";
    3018
     
    3422static const char lcc[] ALIGN1 = "diouxX";
    3523
    36 int bb_dump_size(FS * fs)
     24
     25typedef struct priv_dumper_t {
     26    dumper_t pub;
     27
     28    char **argv;
     29    FU *endfu;
     30    off_t savaddress;        /* saved address/offset in stream */
     31    off_t eaddress;          /* end address */
     32    off_t address;           /* address/offset in stream */
     33    int blocksize;
     34    smallint exitval;        /* final exit value */
     35
     36    /* former statics */
     37    smallint next__done;
     38    smallint get__ateof; // = 1;
     39    unsigned char *get__curp;
     40    unsigned char *get__savp;
     41} priv_dumper_t;
     42
     43dumper_t* FAST_FUNC alloc_dumper(void)
     44{
     45    priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
     46    dumper->pub.dump_length = -1;
     47    dumper->pub.dump_vflag = FIRST;
     48    dumper->get__ateof = 1;
     49    return &dumper->pub;
     50}
     51
     52
     53static NOINLINE int bb_dump_size(FS *fs)
    3754{
    3855    FU *fu;
     
    5269                continue;
    5370            /*
    54              * bb_dump_skip any special chars -- save precision in
     71             * skip any special chars -- save precision in
    5572             * case it's a %s format.
    5673             */
     
    5875            if (*fmt == '.' && isdigit(*++fmt)) {
    5976                prec = atoi(fmt);
    60                 while (isdigit(*++fmt));
    61             }
    62             if (!(p = strchr(size_conv_str + 12, *fmt))) {
     77                while (isdigit(*++fmt))
     78                    continue;
     79            }
     80            p = strchr(size_conv_str + 12, *fmt);
     81            if (!p) {
    6382                if (*fmt == 's') {
    6483                    bcnt += prec;
     
    7897}
    7998
    80 static void rewrite(FS * fs)
     99static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
    81100{
    82101    enum { NOTOKAY, USEBCNT, USEPREC } sokay;
     
    95114        for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
    96115            /* NOSTRICT */
    97             /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
     116            /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
    98117            pr = xzalloc(sizeof(PR));
    99118            if (!fu->nextpr)
    100119                fu->nextpr = pr;
    101120            /* ignore nextpr -- its unused inside the loop and is
    102              * uninitialized 1st time thru.
     121             * uninitialized 1st time through.
    103122             */
    104123
    105             /* bb_dump_skip preceding text and up to the next % sign */
    106             for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
     124            /* skip preceding text and up to the next % sign */
     125            for (p1 = fmtp; *p1 && *p1 != '%'; ++p1)
     126                continue;
    107127
    108128            /* only text in the string */
     
    119139            if (fu->bcnt) {
    120140                sokay = USEBCNT;
    121                 /* bb_dump_skip to conversion character */
    122                 for (++p1; strchr(index_str, *p1); ++p1);
     141                /* skip to conversion character */
     142                for (++p1; strchr(index_str, *p1); ++p1)
     143                    continue;
    123144            } else {
    124                 /* bb_dump_skip any special chars, field width */
    125                 while (strchr(index_str + 1, *++p1));
     145                /* skip any special chars, field width */
     146                while (strchr(index_str + 1, *++p1))
     147                    continue;
    126148                if (*p1 == '.' && isdigit(*++p1)) {
    127149                    sokay = USEPREC;
    128150                    prec = atoi(p1);
    129                     while (isdigit(*++p1));
     151                    while (isdigit(*++p1))
     152                        continue;
    130153                } else
    131154                    sokay = NOTOKAY;
    132155            }
    133156
    134             p2 = p1 + 1;    /* set end pointer */
     157            p2 = p1 + 1; /* set end pointer */
    135158
    136159            /*
     
    139162             * pbb_dump_adding for end of data.
    140163             */
    141 
    142164            if (*p1 == 'c') {
    143165                pr->flags = F_CHAR;
    144             DO_BYTE_COUNT_1:
     166 DO_BYTE_COUNT_1:
    145167                byte_count_str = "\001";
    146             DO_BYTE_COUNT:
     168 DO_BYTE_COUNT:
    147169                if (fu->bcnt) {
    148170                    do {
     
    160182                ++p2;
    161183                ++p1;
    162             DO_INT_CONV:
     184 DO_INT_CONV:
    163185                {
    164186                    const char *e;
    165                     if (!(e = strchr(lcc, *p1))) {
     187                    e = strchr(lcc, *p1);
     188                    if (!e) {
    166189                        goto DO_BAD_CONV_CHAR;
    167190                    }
     
    186209                } else if (sokay == USEPREC) {
    187210                    pr->bcnt = prec;
    188                 } else {    /* NOTOKAY */
     211                } else {   /* NOTOKAY */
    189212                    bb_error_msg_and_die("%%s requires a precision or a byte count");
    190213                }
     
    193216                switch (p1[1]) {
    194217                case 'A':
    195                     endfu = fu;
     218                    dumper->endfu = fu;
    196219                    fu->flags |= F_IGNORE;
    197220                    /* FALLTHROUGH */
     
    220243                }
    221244            } else {
    222             DO_BAD_CONV_CHAR:
     245 DO_BAD_CONV_CHAR:
    223246                bb_error_msg_and_die("bad conversion character %%%s", p1);
    224247            }
     
    232255            pr->fmt = xstrdup(fmtp);
    233256            *p2 = savech;
    234             pr->cchar = pr->fmt + (p1 - fmtp);
     257            //Too early! xrealloc can move pr->fmt!
     258            //pr->cchar = pr->fmt + (p1 - fmtp);
    235259
    236260            /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
     
    239263             * we lose the " is a HEX number" part of fmt.
    240264             */
    241             for (p3 = p2; *p3 && *p3 != '%'; p3++);
    242             if (p3 > p2)
    243             {
     265            for (p3 = p2; *p3 && *p3 != '%'; p3++)
     266                continue;
     267            if (p3 > p2) {
    244268                savech = *p3;
    245269                *p3 = '\0';
    246                 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
     270                pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
    247271                strcat(pr->fmt, p2);
    248272                *p3 = savech;
     
    250274            }
    251275
     276            pr->cchar = pr->fmt + (p1 - fmtp);
    252277            fmtp = p2;
    253278
     
    267292    /*
    268293     * if the format string interprets any data at all, and it's
    269      * not the same as the bb_dump_blocksize, and its last format unit
     294     * not the same as the blocksize, and its last format unit
    270295     * interprets any data at all, and has no iteration count,
    271296     * repeat it as necessary.
     
    274299     * gets output from the last iteration of the format unit.
    275300     */
    276     for (fu = fs->nextfu;; fu = fu->nextfu) {
    277         if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
    278             !(fu->flags & F_SETREP) && fu->bcnt)
    279             fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
     301    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
     302        if (!fu->nextfu && fs->bcnt < dumper->blocksize
     303         && !(fu->flags & F_SETREP) && fu->bcnt
     304        ) {
     305            fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
     306        }
    280307        if (fu->reps > 1) {
    281308            for (pr = fu->nextpr;; pr = pr->nextpr)
     
    292319}
    293320
    294 static void do_skip(const char *fname, int statok)
     321static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
    295322{
    296323    struct stat sbuf;
    297324
    298325    if (statok) {
    299         if (fstat(STDIN_FILENO, &sbuf)) {
    300             bb_perror_msg_and_die("%s", fname);
    301         }
    302         if ((!(S_ISCHR(sbuf.st_mode) ||
    303                S_ISBLK(sbuf.st_mode) ||
    304                S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
    305             /* If bb_dump_size valid and bb_dump_skip >= size */
    306             bb_dump_skip -= sbuf.st_size;
    307             address += sbuf.st_size;
     326        xfstat(STDIN_FILENO, &sbuf, fname);
     327        if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
     328         && dumper->pub.dump_skip >= sbuf.st_size
     329        ) {
     330            /* If bb_dump_size valid and pub.dump_skip >= size */
     331            dumper->pub.dump_skip -= sbuf.st_size;
     332            dumper->address += sbuf.st_size;
    308333            return;
    309334        }
    310335    }
    311     if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
    312         bb_perror_msg_and_die("%s", fname);
    313     }
    314     savaddress = address += bb_dump_skip;
    315     bb_dump_skip = 0;
    316 }
    317 
    318 static int next(char **argv)
    319 {
    320     static smallint done;
    321 
     336    if (fseek(stdin, dumper->pub.dump_skip, SEEK_SET)) {
     337        bb_simple_perror_msg_and_die(fname);
     338    }
     339    dumper->address += dumper->pub.dump_skip;
     340    dumper->savaddress = dumper->address;
     341    dumper->pub.dump_skip = 0;
     342}
     343
     344static NOINLINE int next(priv_dumper_t *dumper)
     345{
    322346    int statok;
    323347
    324     if (argv) {
    325         _argv = argv;
    326         return 1;
    327     }
    328348    for (;;) {
    329         if (*_argv) {
    330             if (!(freopen(*_argv, "r", stdin))) {
    331                 bb_perror_msg("%s", *_argv);
    332                 exitval = 1;
    333                 ++_argv;
     349        if (*dumper->argv) {
     350            dumper->next__done = statok = 1;
     351            if (!(freopen(*dumper->argv, "r", stdin))) {
     352                bb_simple_perror_msg(*dumper->argv);
     353                dumper->exitval = 1;
     354                ++dumper->argv;
    334355                continue;
    335356            }
    336             done = statok = 1;
    337357        } else {
    338             if (done)
    339                 return 0;
    340             done = 1;
     358            if (dumper->next__done)
     359                return 0; /* no next file */
     360            dumper->next__done = 1;
    341361            statok = 0;
    342362        }
    343         if (bb_dump_skip)
    344             do_skip(statok ? *_argv : "stdin", statok);
    345         if (*_argv)
    346             ++_argv;
    347         if (!bb_dump_skip)
     363        if (dumper->pub.dump_skip)
     364            do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
     365        if (*dumper->argv)
     366            ++dumper->argv;
     367        if (!dumper->pub.dump_skip)
    348368            return 1;
    349369    }
     
    351371}
    352372
    353 static unsigned char *get(void)
    354 {
    355     static smallint ateof = 1;
    356     static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
    357 
     373static unsigned char *get(priv_dumper_t *dumper)
     374{
    358375    int n;
    359376    int need, nread;
    360     unsigned char *tmpp;
    361 
    362     if (!curp) {
    363         address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
    364         curp = xmalloc(bb_dump_blocksize);
    365         savp = xmalloc(bb_dump_blocksize);
     377    int blocksize = dumper->blocksize;
     378
     379    if (!dumper->get__curp) {
     380        dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
     381        dumper->get__curp = xmalloc(blocksize);
     382        dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
    366383    } else {
    367         tmpp = curp;
    368         curp = savp;
    369         savp = tmpp;
    370         address = savaddress += bb_dump_blocksize;
    371     }
    372     for (need = bb_dump_blocksize, nread = 0;;) {
     384        unsigned char *tmp = dumper->get__curp;
     385        dumper->get__curp = dumper->get__savp;
     386        dumper->get__savp = tmp;
     387        dumper->savaddress += blocksize;
     388        dumper->address = dumper->savaddress;
     389    }
     390    need = blocksize;
     391    nread = 0;
     392    while (1) {
    373393        /*
    374394         * if read the right number of bytes, or at EOF for one file,
     
    376396         * block and set the end flag.
    377397         */
    378         if (!bb_dump_length || (ateof && !next((char **) NULL))) {
    379             if (need == bb_dump_blocksize) {
     398        if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
     399            if (need == blocksize) {
    380400                return NULL;
    381401            }
    382             if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
    383                 if (bb_dump_vflag != DUP) {
     402            if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
     403                if (dumper->pub.dump_vflag != DUP) {
    384404                    puts("*");
    385405                }
    386406                return NULL;
    387407            }
    388             memset((char *) curp + nread, 0, need);
    389             eaddress = address + nread;
    390             return curp;
    391         }
    392         n = fread((char *) curp + nread, sizeof(unsigned char),
    393                   bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
     408            memset(dumper->get__curp + nread, 0, need);
     409            dumper->eaddress = dumper->address + nread;
     410            return dumper->get__curp;
     411        }
     412        n = fread(dumper->get__curp + nread, sizeof(unsigned char),
     413                dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
    394414        if (!n) {
    395415            if (ferror(stdin)) {
    396                 bb_perror_msg("%s", _argv[-1]);
    397             }
    398             ateof = 1;
     416                bb_simple_perror_msg(dumper->argv[-1]);
     417            }
     418            dumper->get__ateof = 1;
    399419            continue;
    400420        }
    401         ateof = 0;
    402         if (bb_dump_length != -1) {
    403             bb_dump_length -= n;
     421        dumper->get__ateof = 0;
     422        if (dumper->pub.dump_length != -1) {
     423            dumper->pub.dump_length -= n;
    404424        }
    405425        need -= n;
    406426        if (!need) {
    407             if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
    408                 || memcmp(curp, savp, bb_dump_blocksize)) {
    409                 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
    410                     bb_dump_vflag = WAIT;
    411                 }
    412                 return curp;
    413             }
    414             if (bb_dump_vflag == WAIT) {
     427            if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
     428             || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
     429            ) {
     430                if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
     431                    dumper->pub.dump_vflag = WAIT;
     432                }
     433                return dumper->get__curp;
     434            }
     435            if (dumper->pub.dump_vflag == WAIT) {
    415436                puts("*");
    416437            }
    417             bb_dump_vflag = DUP;
    418             address = savaddress += bb_dump_blocksize;
    419             need = bb_dump_blocksize;
     438            dumper->pub.dump_vflag = DUP;
     439            dumper->savaddress += blocksize;
     440            dumper->address = dumper->savaddress;
     441            need = blocksize;
    420442            nread = 0;
    421443        } else {
     
    425447}
    426448
    427 static void bpad(PR * pr)
     449static void bpad(PR *pr)
    428450{
    429451    char *p1, *p2;
     
    435457    pr->flags = F_BPAD;
    436458    *pr->cchar = 's';
    437     for (p1 = pr->fmt; *p1 != '%'; ++p1);
     459    for (p1 = pr->fmt; *p1 != '%'; ++p1)
     460        continue;
    438461    for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
    439         if (pr->nospace) pr->nospace--;
    440     while ((*p2++ = *p1++) != 0);
     462        if (pr->nospace)
     463            pr->nospace--;
     464    while ((*p2++ = *p1++) != 0)
     465        continue;
    441466}
    442467
    443468static const char conv_str[] ALIGN1 =
    444469    "\0\\0\0"
    445     "\007\\a\0"             /* \a */
     470    "\007\\a\0"  /* \a */
    446471    "\b\\b\0"
    447472    "\f\\b\0"
     
    453478
    454479
    455 static void conv_c(PR * pr, unsigned char * p)
     480static void conv_c(PR *pr, unsigned char *p)
    456481{
    457482    const char *str = conv_str;
     
    466491    } while (*str);
    467492
    468     if (isprint(*p)) {
     493    if (isprint_asciionly(*p)) {
    469494        *pr->cchar = 'c';
    470         (void) printf(pr->fmt, *p);
     495        printf(pr->fmt, *p);
    471496    } else {
    472497        sprintf(buf, "%03o", (int) *p);
    473498        str = buf;
    474       strpr:
     499 strpr:
    475500        *pr->cchar = 's';
    476501        printf(pr->fmt, str);
     
    478503}
    479504
    480 static void conv_u(PR * pr, unsigned char * p)
     505static void conv_u(PR *pr, unsigned char *p)
    481506{
    482507    static const char list[] ALIGN1 =
     
    493518        *pr->cchar = 's';
    494519        printf(pr->fmt, "del");
    495     } else if (isprint(*p)) {
     520    } else if (*p < 0x7f) { /* isprint() */
    496521        *pr->cchar = 'c';
    497522        printf(pr->fmt, *p);
     
    502527}
    503528
    504 static void display(void)
    505 {
    506 /*  extern FU *endfu; */
     529static void display(priv_dumper_t* dumper)
     530{
    507531    FS *fs;
    508532    FU *fu;
    509533    PR *pr;
    510534    int cnt;
    511     unsigned char *bp;
    512 
     535    unsigned char *bp, *savebp;
    513536    off_t saveaddress;
    514     unsigned char savech = 0, *savebp;
    515 
    516     while ((bp = get()) != NULL) {
    517         for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
    518              fs = fs->nextfs, bp = savebp, address = saveaddress) {
     537    unsigned char savech = '\0';
     538
     539    while ((bp = get(dumper)) != NULL) {
     540        fs = dumper->pub.fshead;
     541        savebp = bp;
     542        saveaddress = dumper->address;
     543        for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
    519544            for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    520545                if (fu->flags & F_IGNORE) {
     
    522547                }
    523548                for (cnt = fu->reps; cnt; --cnt) {
    524                     for (pr = fu->nextpr; pr; address += pr->bcnt,
    525                          bp += pr->bcnt, pr = pr->nextpr) {
    526                         if (eaddress && address >= eaddress &&
    527                             !(pr->flags & (F_TEXT | F_BPAD))) {
     549                    for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
     550                                bp += pr->bcnt, pr = pr->nextpr) {
     551                        if (dumper->eaddress && dumper->address >= dumper->eaddress
     552                         && !(pr->flags & (F_TEXT | F_BPAD))
     553                        ) {
    528554                            bpad(pr);
    529555                        }
     
    535561                        switch (pr->flags) {
    536562                        case F_ADDRESS:
    537                             printf(pr->fmt, (unsigned int) address);
     563                            printf(pr->fmt, (unsigned) dumper->address);
    538564                            break;
    539565                        case F_BPAD:
     
    546572                            printf(pr->fmt, *bp);
    547573                            break;
    548                         case F_DBL:{
     574                        case F_DBL: {
    549575                            double dval;
    550576                            float fval;
     
    552578                            switch (pr->bcnt) {
    553579                            case 4:
    554                                 memmove((char *) &fval, (char *) bp,
    555                                       sizeof(fval));
     580                                memcpy(&fval, bp, sizeof(fval));
    556581                                printf(pr->fmt, fval);
    557582                                break;
    558583                            case 8:
    559                                 memmove((char *) &dval, (char *) bp,
    560                                       sizeof(dval));
     584                                memcpy(&dval, bp, sizeof(dval));
    561585                                printf(pr->fmt, dval);
    562586                                break;
     
    564588                            break;
    565589                        }
    566                         case F_INT:{
     590                        case F_INT: {
    567591                            int ival;
    568592                            short sval;
     
    573597                                break;
    574598                            case 2:
    575                                 memmove((char *) &sval, (char *) bp,
    576                                       sizeof(sval));
     599                                memcpy(&sval, bp, sizeof(sval));
    577600                                printf(pr->fmt, (int) sval);
    578601                                break;
    579602                            case 4:
    580                                 memmove((char *) &ival, (char *) bp,
    581                                       sizeof(ival));
     603                                memcpy(&ival, bp, sizeof(ival));
    582604                                printf(pr->fmt, ival);
    583605                                break;
     
    586608                        }
    587609                        case F_P:
    588                             printf(pr->fmt, isprint(*bp) ? *bp : '.');
     610                            printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
    589611                            break;
    590612                        case F_STR:
     
    597619                            conv_u(pr, bp);
    598620                            break;
    599                         case F_UINT:{
    600                             unsigned int ival;
     621                        case F_UINT: {
     622                            unsigned ival;
    601623                            unsigned short sval;
    602624
    603625                            switch (pr->bcnt) {
    604626                            case 1:
    605                                 printf(pr->fmt, (unsigned int) * bp);
     627                                printf(pr->fmt, (unsigned) *bp);
    606628                                break;
    607629                            case 2:
    608                                 memmove((char *) &sval, (char *) bp,
    609                                       sizeof(sval));
    610                                 printf(pr->fmt, (unsigned int) sval);
     630                                memcpy(&sval, bp, sizeof(sval));
     631                                printf(pr->fmt, (unsigned) sval);
    611632                                break;
    612633                            case 4:
    613                                 memmove((char *) &ival, (char *) bp,
    614                                       sizeof(ival));
     634                                memcpy(&ival, bp, sizeof(ival));
    615635                                printf(pr->fmt, ival);
    616636                                break;
     
    627647        }
    628648    }
    629     if (endfu) {
     649    if (dumper->endfu) {
    630650        /*
    631          * if eaddress not set, error or file bb_dump_size was multiple of
    632          * bb_dump_blocksize, and no partial block ever found.
     651         * if eaddress not set, error or file size was multiple
     652         * of blocksize, and no partial block ever found.
    633653         */
    634         if (!eaddress) {
    635             if (!address) {
     654        if (!dumper->eaddress) {
     655            if (!dumper->address) {
    636656                return;
    637657            }
    638             eaddress = address;
    639         }
    640         for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
     658            dumper->eaddress = dumper->address;
     659        }
     660        for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
    641661            switch (pr->flags) {
    642662            case F_ADDRESS:
    643                 (void) printf(pr->fmt, (unsigned int) eaddress);
     663                printf(pr->fmt, (unsigned) dumper->eaddress);
    644664                break;
    645665            case F_TEXT:
    646                 (void) printf(pr->fmt);
     666                printf(pr->fmt);
    647667                break;
    648668            }
     
    651671}
    652672
    653 int bb_dump_dump(char **argv)
     673#define dumper ((priv_dumper_t*)pub_dumper)
     674int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
    654675{
    655676    FS *tfs;
     677    int blocksize;
    656678
    657679    /* figure out the data block bb_dump_size */
    658     for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
     680    blocksize = 0;
     681    tfs = dumper->pub.fshead;
     682    while (tfs) {
    659683        tfs->bcnt = bb_dump_size(tfs);
    660         if (bb_dump_blocksize < tfs->bcnt) {
    661             bb_dump_blocksize = tfs->bcnt;
    662         }
    663     }
     684        if (blocksize < tfs->bcnt) {
     685            blocksize = tfs->bcnt;
     686        }
     687        tfs = tfs->nextfs;
     688    }
     689    dumper->blocksize = blocksize;
     690
    664691    /* rewrite the rules, do syntax checking */
    665     for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
    666         rewrite(tfs);
    667     }
    668 
    669     next(argv);
    670     display();
    671 
    672     return exitval;
    673 }
    674 
    675 void bb_dump_add(const char *fmt)
     692    for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
     693        rewrite(dumper, tfs);
     694    }
     695
     696    dumper->argv = argv;
     697    display(dumper);
     698
     699    return dumper->exitval;
     700}
     701
     702void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
    676703{
    677704    const char *p;
    678705    char *p1;
    679706    char *p2;
    680     static FS **nextfs;
    681707    FS *tfs;
    682     FU *tfu, **nextfu;
     708    FU *tfu, **nextfupp;
    683709    const char *savep;
    684710
    685711    /* start new linked list of format units */
    686712    tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
    687     if (!bb_dump_fshead) {
    688         bb_dump_fshead = tfs;
     713    if (!dumper->pub.fshead) {
     714        dumper->pub.fshead = tfs;
    689715    } else {
    690         *nextfs = tfs;
    691     }
    692     nextfs = &tfs->nextfs;
    693     nextfu = &tfs->nextfu;
     716        FS *fslast = dumper->pub.fshead;
     717        while (fslast->nextfs)
     718            fslast = fslast->nextfs;
     719        fslast->nextfs = tfs;
     720    }
     721    nextfupp = &tfs->nextfu;
    694722
    695723    /* take the format string and break it up into format units */
    696     for (p = fmt;;) {
    697         /* bb_dump_skip leading white space */
     724    p = fmt;
     725    for (;;) {
    698726        p = skip_whitespace(p);
    699727        if (!*p) {
     
    703731        /* allocate a new format unit and link it in */
    704732        /* NOSTRICT */
    705         /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
     733        /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
    706734        tfu = xzalloc(sizeof(FU));
    707         *nextfu = tfu;
    708         nextfu = &tfu->nextfu;
     735        *nextfupp = tfu;
     736        nextfupp = &tfu->nextfu;
    709737        tfu->reps = 1;
    710738
    711739        /* if leading digit, repetition count */
    712740        if (isdigit(*p)) {
    713             for (savep = p; isdigit(*p); ++p);
     741            for (savep = p; isdigit(*p); ++p)
     742                continue;
    714743            if (!isspace(*p) && *p != '/') {
    715744                bb_error_msg_and_die("bad format {%s}", fmt);
     
    718747            tfu->reps = atoi(savep);
    719748            tfu->flags = F_SETREP;
    720             /* bb_dump_skip trailing white space */
     749            /* skip trailing white space */
    721750            p = skip_whitespace(++p);
    722751        }
    723752
    724         /* bb_dump_skip slash and trailing white space */
     753        /* skip slash and trailing white space */
    725754        if (*p == '/') {
    726755            p = skip_whitespace(++p);
     
    731760// TODO: use bb_strtou
    732761            savep = p;
    733             do p++; while (isdigit(*p));
     762            while (isdigit(*++p))
     763                continue;
    734764            if (!isspace(*p)) {
    735765                bb_error_msg_and_die("bad format {%s}", fmt);
    736766            }
    737767            tfu->bcnt = atoi(savep);
    738             /* bb_dump_skip trailing white space */
     768            /* skip trailing white space */
    739769            p = skip_whitespace(++p);
    740770        }
     
    749779            }
    750780        }
    751         tfu->fmt = xmalloc(p - savep + 1);
    752         strncpy(tfu->fmt, savep, p - savep);
    753         tfu->fmt[p - savep] = '\0';
     781        tfu->fmt = xstrndup(savep, p - savep);
    754782/*      escape(tfu->fmt); */
    755783
Note: See TracChangeset for help on using the changeset viewer.