libbb: get rid of statics in dump.c; code shrinks a lot too

function                                             old     new   delta
alloc_dumper                                           -      26     +26
hexdump_main                                         600     601      +1
static.done                                            1       -      -1
static.ateof                                           1       -      -1
bb_dump_vflag                                          1       -      -1
static.savp                                            4       -      -4
static.nextfs                                          4       -      -4
static.curp                                            4       -      -4
exitval                                                4       -      -4
endfu                                                  4       -      -4
bb_dump_length                                         4       -      -4
bb_dump_fshead                                         4       -      -4
bb_dump_blocksize                                      4       -      -4
_argv                                                  4       -      -4
bb_dump_add                                          365     358      -7
savaddress                                             8       -      -8
eaddress                                               8       -      -8
bb_dump_skip                                           8       -      -8
address                                                8       -      -8
bb_dump_dump                                        2748    2672     -76
next                                                 538     445     -93
------------------------------------------------------------------------------
(add/remove: 1/16 grow/shrink: 1/3 up/down: 27/-247)         Total: -220 bytes
   text	   data	    bss	    dec	    hex	filename
 789458	    607	   6764	 796829	  c289d	busybox_old
 789309	    601	   6696	 796606	  c27be	busybox_unstripped
This commit is contained in:
Denis Vlasenko 2008-07-16 11:00:16 +00:00
parent 8ddb6410ed
commit 55f7912dda
4 changed files with 222 additions and 200 deletions

View File

@ -24,7 +24,7 @@
#define ishexdigit(c) (isxdigit)(c) #define ishexdigit(c) (isxdigit)(c)
static void static void
odoffset(int argc, char ***argvp) odoffset(dumper_t *dumper, int argc, char ***argvp)
{ {
char *num, *p; char *num, *p;
int base; int base;
@ -57,7 +57,7 @@ odoffset(int argc, char ***argvp)
base = 0; base = 0;
/* /*
* bb_dump_skip over leading '+', 'x[0-9a-fA-f]' or '0x', and * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
* set base. * set base.
*/ */
if (p[0] == '+') if (p[0] == '+')
@ -70,11 +70,13 @@ odoffset(int argc, char ***argvp)
base = 16; base = 16;
} }
/* bb_dump_skip over the number */ /* skip over the number */
if (base == 16) if (base == 16)
for (num = p; ishexdigit(*p); ++p); for (num = p; ishexdigit(*p); ++p)
continue;
else else
for (num = p; isdecdigit(*p); ++p); for (num = p; isdecdigit(*p); ++p)
continue;
/* check for no number */ /* check for no number */
if (num == p) if (num == p)
@ -87,23 +89,23 @@ odoffset(int argc, char ***argvp)
base = 10; base = 10;
} }
bb_dump_skip = strtol(num, &end, base ? base : 8); dumper->dump_skip = strtol(num, &end, base ? base : 8);
/* if end isn't the same as p, we got a non-octal digit */ /* if end isn't the same as p, we got a non-octal digit */
if (end != p) if (end != p)
bb_dump_skip = 0; dumper->dump_skip = 0;
else { else {
if (*p) { if (*p) {
if (*p == 'b') { if (*p == 'b') {
bb_dump_skip *= 512; dumper->dump_skip *= 512;
++p; ++p;
} else if (*p == 'B') { } else if (*p == 'B') {
bb_dump_skip *= 1024; dumper->dump_skip *= 1024;
++p; ++p;
} }
} }
if (*p) if (*p)
bb_dump_skip = 0; dumper->dump_skip = 0;
else { else {
++*argvp; ++*argvp;
/* /*
@ -120,9 +122,9 @@ odoffset(int argc, char ***argvp)
} }
if (base == 10) { if (base == 10) {
x_or_d = 'd'; x_or_d = 'd';
DO_X_OR_D: DO_X_OR_D:
bb_dump_fshead->nextfu->fmt[TYPE_OFFSET] dumper->fshead->nextfu->fmt[TYPE_OFFSET]
= bb_dump_fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = dumper->fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
= x_or_d; = x_or_d;
} }
} }
@ -161,36 +163,35 @@ int od_main(int argc, char **argv)
int ch; int ch;
int first = 1; int first = 1;
char *p; char *p;
bb_dump_vflag = FIRST; dumper_t *dumper = alloc_dumper();
bb_dump_length = -1;
while ((ch = getopt(argc, argv, od_opts)) > 0) { while ((ch = getopt(argc, argv, od_opts)) > 0) {
if (ch == 'v') { if (ch == 'v') {
bb_dump_vflag = ALL; dumper->dump_vflag = ALL;
} else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) { } else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) {
if (first) { if (first) {
first = 0; first = 0;
bb_dump_add("\"%07.7_Ao\n\""); bb_dump_add(dumper, "\"%07.7_Ao\n\"");
bb_dump_add("\"%07.7_ao \""); bb_dump_add(dumper, "\"%07.7_ao \"");
} else { } else {
bb_dump_add("\" \""); bb_dump_add(dumper, "\" \"");
} }
bb_dump_add(add_strings[(int)od_o2si[(p-od_opts)]]); bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
} else { /* P, p, s, w, or other unhandled */ } else { /* P, p, s, w, or other unhandled */
bb_show_usage(); bb_show_usage();
} }
} }
if (!bb_dump_fshead) { if (!dumper->fshead) {
bb_dump_add("\"%07.7_Ao\n\""); bb_dump_add(dumper, "\"%07.7_Ao\n\"");
bb_dump_add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); bb_dump_add(dumper, "\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
odoffset(argc, &argv); odoffset(dumper, argc, &argv);
return bb_dump_dump(argv); return bb_dump_dump(dumper, argv);
} }
#endif /* ENABLE_DESKTOP */ #endif /* ENABLE_DESKTOP */

View File

@ -18,10 +18,10 @@
#define F_UINT 0x200 /* %[ouXx] */ #define F_UINT 0x200 /* %[ouXx] */
#define F_TEXT 0x400 /* no conversions */ #define F_TEXT 0x400 /* no conversions */
enum _vflag { ALL, DUP, FIRST, WAIT }; /* -v values */ enum dump_vflag_t { ALL, DUP, FIRST, WAIT }; /* -v values */
typedef struct _pr { typedef struct PR {
struct _pr *nextpr; /* next print unit */ struct PR *nextpr; /* next print unit */
unsigned flags; /* flag values */ unsigned flags; /* flag values */
int bcnt; /* byte count */ int bcnt; /* byte count */
char *cchar; /* conversion character */ char *cchar; /* conversion character */
@ -29,30 +29,31 @@ typedef struct _pr {
char *nospace; /* no whitespace version */ char *nospace; /* no whitespace version */
} PR; } PR;
typedef struct _fu { typedef struct FU {
struct _fu *nextfu; /* next format unit */ struct FU *nextfu; /* next format unit */
struct _pr *nextpr; /* next print unit */ struct PR *nextpr; /* next print unit */
unsigned flags; /* flag values */ unsigned flags; /* flag values */
int reps; /* repetition count */ int reps; /* repetition count */
int bcnt; /* byte count */ int bcnt; /* byte count */
char *fmt; /* format string */ char *fmt; /* format string */
} FU; } FU;
typedef struct _fs { /* format strings */ typedef struct FS { /* format strings */
struct _fs *nextfs; /* linked list of format strings */ struct FS *nextfs; /* linked list of format strings */
struct _fu *nextfu; /* linked list of format units */ struct FU *nextfu; /* linked list of format units */
int bcnt; int bcnt;
} FS; } FS;
extern void bb_dump_add(const char *fmt) FAST_FUNC; typedef struct dumper_t {
extern int bb_dump_dump(char **argv) FAST_FUNC; off_t dump_skip; /* bytes to skip */
extern int bb_dump_size(FS * fs) FAST_FUNC; int dump_length; /* max bytes to read */
smallint dump_vflag; /*enum dump_vflag_t*/
FS *fshead;
} dumper_t;
extern FS *bb_dump_fshead; /* head of format strings */ dumper_t* alloc_dumper(void) FAST_FUNC;
extern int bb_dump_blocksize; /* data block size */ extern void bb_dump_add(dumper_t *dumper, const char *fmt) FAST_FUNC;
extern int bb_dump_length; /* max bytes to read */ extern int bb_dump_dump(dumper_t *dumper, char **argv) FAST_FUNC;
extern smallint /*enum _vflag*/ bb_dump_vflag;
extern off_t bb_dump_skip; /* bytes to skip */
#if __GNUC_PREREQ(4,1) #if __GNUC_PREREQ(4,1)
# pragma GCC visibility pop # pragma GCC visibility pop

View File

@ -14,19 +14,6 @@
#include "libbb.h" #include "libbb.h"
#include "dump.h" #include "dump.h"
FS *bb_dump_fshead; /* head of format strings */
off_t bb_dump_skip; /* bytes to skip */
int bb_dump_blocksize; /* data block size */
int bb_dump_length = -1; /* max bytes to read */
smallint /*enum _vflag*/ bb_dump_vflag = FIRST;
static FU *endfu;
static char **_argv;
static off_t savaddress; /* saved address/offset in stream */
static off_t eaddress; /* end address */
static off_t address; /* address/offset in stream */
static int exitval; /* final exit value */
static const char index_str[] ALIGN1 = ".#-+ 0123456789"; static const char index_str[] ALIGN1 = ".#-+ 0123456789";
static const char size_conv_str[] ALIGN1 = static const char size_conv_str[] ALIGN1 =
@ -34,7 +21,36 @@ static const char size_conv_str[] ALIGN1 =
static const char lcc[] ALIGN1 = "diouxX"; static const char lcc[] ALIGN1 = "diouxX";
int FAST_FUNC bb_dump_size(FS *fs)
typedef struct priv_dumper_t {
dumper_t pub;
char **argv;
FU *endfu;
off_t savaddress; /* saved address/offset in stream */
off_t eaddress; /* end address */
off_t address; /* address/offset in stream */
int blocksize;
smallint exitval; /* final exit value */
/* former statics */
smallint next__done;
smallint get__ateof; // = 1;
unsigned char *get__curp;
unsigned char *get__savp;
} priv_dumper_t;
dumper_t* FAST_FUNC alloc_dumper(void)
{
priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
dumper->pub.dump_length = -1;
dumper->pub.dump_vflag = FIRST;
dumper->get__ateof = 1;
return &dumper->pub;
}
static NOINLINE int bb_dump_size(FS *fs)
{ {
FU *fu; FU *fu;
int bcnt, cur_size; int bcnt, cur_size;
@ -52,13 +68,14 @@ int FAST_FUNC bb_dump_size(FS *fs)
if (*fmt != '%') if (*fmt != '%')
continue; continue;
/* /*
* bb_dump_skip any special chars -- save precision in * skip any special chars -- save precision in
* case it's a %s format. * case it's a %s format.
*/ */
while (strchr(index_str + 1, *++fmt)); while (strchr(index_str + 1, *++fmt));
if (*fmt == '.' && isdigit(*++fmt)) { if (*fmt == '.' && isdigit(*++fmt)) {
prec = atoi(fmt); prec = atoi(fmt);
while (isdigit(*++fmt)); while (isdigit(*++fmt))
continue;
} }
p = strchr(size_conv_str + 12, *fmt); p = strchr(size_conv_str + 12, *fmt);
if (!p) { if (!p) {
@ -79,7 +96,7 @@ int FAST_FUNC bb_dump_size(FS *fs)
return cur_size; return cur_size;
} }
static void rewrite(FS *fs) static void rewrite(priv_dumper_t *dumper, FS *fs)
{ {
enum { NOTOKAY, USEBCNT, USEPREC } sokay; enum { NOTOKAY, USEBCNT, USEPREC } sokay;
PR *pr, **nextpr = NULL; PR *pr, **nextpr = NULL;
@ -104,7 +121,7 @@ static void rewrite(FS *fs)
* uninitialized 1st time through. * uninitialized 1st time through.
*/ */
/* bb_dump_skip preceding text and up to the next % sign */ /* skip preceding text and up to the next % sign */
for (p1 = fmtp; *p1 && *p1 != '%'; ++p1) for (p1 = fmtp; *p1 && *p1 != '%'; ++p1)
continue; continue;
@ -121,11 +138,11 @@ static void rewrite(FS *fs)
*/ */
if (fu->bcnt) { if (fu->bcnt) {
sokay = USEBCNT; sokay = USEBCNT;
/* bb_dump_skip to conversion character */ /* skip to conversion character */
for (++p1; strchr(index_str, *p1); ++p1) for (++p1; strchr(index_str, *p1); ++p1)
continue; continue;
} else { } else {
/* bb_dump_skip any special chars, field width */ /* skip any special chars, field width */
while (strchr(index_str + 1, *++p1)) while (strchr(index_str + 1, *++p1))
continue; continue;
if (*p1 == '.' && isdigit(*++p1)) { if (*p1 == '.' && isdigit(*++p1)) {
@ -137,7 +154,7 @@ static void rewrite(FS *fs)
sokay = NOTOKAY; sokay = NOTOKAY;
} }
p2 = p1 + 1; /* set end pointer */ p2 = p1 + 1; /* set end pointer */
/* /*
* figure out the byte count for each conversion; * figure out the byte count for each conversion;
@ -198,7 +215,7 @@ static void rewrite(FS *fs)
++p2; ++p2;
switch (p1[1]) { switch (p1[1]) {
case 'A': case 'A':
endfu = fu; dumper->endfu = fu;
fu->flags |= F_IGNORE; fu->flags |= F_IGNORE;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 'a': case 'a':
@ -274,7 +291,7 @@ static void rewrite(FS *fs)
} }
/* /*
* if the format string interprets any data at all, and it's * if the format string interprets any data at all, and it's
* not the same as the bb_dump_blocksize, and its last format unit * not the same as the blocksize, and its last format unit
* interprets any data at all, and has no iteration count, * interprets any data at all, and has no iteration count,
* repeat it as necessary. * repeat it as necessary.
* *
@ -282,10 +299,10 @@ static void rewrite(FS *fs)
* gets output from the last iteration of the format unit. * gets output from the last iteration of the format unit.
*/ */
for (fu = fs->nextfu;; fu = fu->nextfu) { for (fu = fs->nextfu;; fu = fu->nextfu) {
if (!fu->nextfu && fs->bcnt < bb_dump_blocksize if (!fu->nextfu && fs->bcnt < dumper->blocksize
&& !(fu->flags & F_SETREP) && fu->bcnt && !(fu->flags & F_SETREP) && fu->bcnt
) { ) {
fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt; fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
} }
if (fu->reps > 1) { if (fu->reps > 1) {
for (pr = fu->nextpr;; pr = pr->nextpr) for (pr = fu->nextpr;; pr = pr->nextpr)
@ -301,7 +318,7 @@ static void rewrite(FS *fs)
} }
} }
static void do_skip(const char *fname, int statok) static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
{ {
struct stat sbuf; struct stat sbuf;
@ -309,125 +326,122 @@ static void do_skip(const char *fname, int statok)
if (fstat(STDIN_FILENO, &sbuf)) { if (fstat(STDIN_FILENO, &sbuf)) {
bb_simple_perror_msg_and_die(fname); bb_simple_perror_msg_and_die(fname);
} }
if ((!(S_ISCHR(sbuf.st_mode) || if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
S_ISBLK(sbuf.st_mode) || && dumper->pub.dump_skip >= sbuf.st_size
S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) { ) {
/* If bb_dump_size valid and bb_dump_skip >= size */ /* If bb_dump_size valid and pub.dump_skip >= size */
bb_dump_skip -= sbuf.st_size; dumper->pub.dump_skip -= sbuf.st_size;
address += sbuf.st_size; dumper->address += sbuf.st_size;
return; return;
} }
} }
if (fseek(stdin, bb_dump_skip, SEEK_SET)) { if (fseek(stdin, dumper->pub.dump_skip, SEEK_SET)) {
bb_simple_perror_msg_and_die(fname); bb_simple_perror_msg_and_die(fname);
} }
savaddress = address += bb_dump_skip; dumper->address += dumper->pub.dump_skip;
bb_dump_skip = 0; dumper->savaddress = dumper->address;
dumper->pub.dump_skip = 0;
} }
static int next(char **argv) static NOINLINE int next(priv_dumper_t *dumper)
{ {
static smallint done;
int statok; int statok;
if (argv) {
_argv = argv;
return 1;
}
for (;;) { for (;;) {
if (*_argv) { if (*dumper->argv) {
if (!(freopen(*_argv, "r", stdin))) { if (!(freopen(*dumper->argv, "r", stdin))) {
bb_simple_perror_msg(*_argv); bb_simple_perror_msg(*dumper->argv);
exitval = 1; dumper->exitval = 1;
++_argv; ++dumper->argv;
continue; continue;
} }
done = statok = 1; dumper->next__done = statok = 1;
} else { } else {
if (done) if (dumper->next__done)
return 0; return 0;
done = 1; dumper->next__done = 1;
statok = 0; statok = 0;
} }
if (bb_dump_skip) if (dumper->pub.dump_skip)
do_skip(statok ? *_argv : "stdin", statok); do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
if (*_argv) if (*dumper->argv)
++_argv; ++dumper->argv;
if (!bb_dump_skip) if (!dumper->pub.dump_skip)
return 1; return 1;
} }
/* NOTREACHED */ /* NOTREACHED */
} }
static unsigned char *get(void) static unsigned char *get(priv_dumper_t *dumper)
{ {
static smallint ateof = 1;
static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
int n; int n;
int need, nread; int need, nread;
unsigned char *tmpp; unsigned char *tmpp;
int blocksize = dumper->blocksize;
if (!curp) { if (!dumper->get__curp) {
address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/ dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
curp = xmalloc(bb_dump_blocksize); dumper->get__curp = xmalloc(blocksize);
savp = xmalloc(bb_dump_blocksize); dumper->get__savp = xmalloc(blocksize);
} else { } else {
tmpp = curp; tmpp = dumper->get__curp;
curp = savp; dumper->get__curp = dumper->get__savp;
savp = tmpp; dumper->get__savp = tmpp;
address = savaddress += bb_dump_blocksize; dumper->savaddress += blocksize;
dumper->address = dumper->savaddress;
} }
for (need = bb_dump_blocksize, nread = 0;;) { need = blocksize;
nread = 0;
while (1) {
/* /*
* if read the right number of bytes, or at EOF for one file, * if read the right number of bytes, or at EOF for one file,
* and no other files are available, zero-pad the rest of the * and no other files are available, zero-pad the rest of the
* block and set the end flag. * block and set the end flag.
*/ */
if (!bb_dump_length || (ateof && !next(NULL))) { if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
if (need == bb_dump_blocksize) { if (need == blocksize) {
return NULL; return NULL;
} }
if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) { if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
if (bb_dump_vflag != DUP) { if (dumper->pub.dump_vflag != DUP) {
puts("*"); puts("*");
} }
return NULL; return NULL;
} }
memset(curp + nread, 0, need); memset(dumper->get__curp + nread, 0, need);
eaddress = address + nread; dumper->eaddress = dumper->address + nread;
return curp; return dumper->get__curp;
} }
n = fread(curp + nread, sizeof(unsigned char), n = fread(dumper->get__curp + nread, sizeof(unsigned char),
bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin); dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
if (!n) { if (!n) {
if (ferror(stdin)) { if (ferror(stdin)) {
bb_simple_perror_msg(_argv[-1]); bb_simple_perror_msg(dumper->argv[-1]);
} }
ateof = 1; dumper->get__ateof = 1;
continue; continue;
} }
ateof = 0; dumper->get__ateof = 0;
if (bb_dump_length != -1) { if (dumper->pub.dump_length != -1) {
bb_dump_length -= n; dumper->pub.dump_length -= n;
} }
need -= n; need -= n;
if (!need) { if (!need) {
if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
|| memcmp(curp, savp, bb_dump_blocksize) || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
) { ) {
if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) { if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
bb_dump_vflag = WAIT; dumper->pub.dump_vflag = WAIT;
} }
return curp; return dumper->get__curp;
} }
if (bb_dump_vflag == WAIT) { if (dumper->pub.dump_vflag == WAIT) {
puts("*"); puts("*");
} }
bb_dump_vflag = DUP; dumper->pub.dump_vflag = DUP;
address = savaddress += bb_dump_blocksize; dumper->savaddress += blocksize;
need = bb_dump_blocksize; dumper->address = dumper->savaddress;
need = blocksize;
nread = 0; nread = 0;
} else { } else {
nread += n; nread += n;
@ -515,29 +529,31 @@ static void conv_u(PR *pr, unsigned char *p)
} }
} }
static void display(void) static void display(priv_dumper_t* dumper)
{ {
/* extern FU *endfu; */
FS *fs; FS *fs;
FU *fu; FU *fu;
PR *pr; PR *pr;
int cnt; int cnt;
unsigned char *bp; unsigned char *bp, *savebp;
off_t saveaddress; off_t saveaddress;
unsigned char savech = 0, *savebp; unsigned char savech = '\0';
while ((bp = get()) != NULL) { while ((bp = get(dumper)) != NULL) {
for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs; fs = dumper->pub.fshead;
fs = fs->nextfs, bp = savebp, address = saveaddress) { savebp = bp;
saveaddress = dumper->address;
for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
for (fu = fs->nextfu; fu; fu = fu->nextfu) { for (fu = fs->nextfu; fu; fu = fu->nextfu) {
if (fu->flags & F_IGNORE) { if (fu->flags & F_IGNORE) {
break; break;
} }
for (cnt = fu->reps; cnt; --cnt) { for (cnt = fu->reps; cnt; --cnt) {
for (pr = fu->nextpr; pr; address += pr->bcnt, for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
bp += pr->bcnt, pr = pr->nextpr) { bp += pr->bcnt, pr = pr->nextpr) {
if (eaddress && address >= eaddress && if (dumper->eaddress && dumper->address >= dumper->eaddress
!(pr->flags & (F_TEXT | F_BPAD))) { && !(pr->flags & (F_TEXT | F_BPAD))
) {
bpad(pr); bpad(pr);
} }
if (cnt == 1 && pr->nospace) { if (cnt == 1 && pr->nospace) {
@ -547,7 +563,7 @@ static void display(void)
/* PRINT; */ /* PRINT; */
switch (pr->flags) { switch (pr->flags) {
case F_ADDRESS: case F_ADDRESS:
printf(pr->fmt, (unsigned) address); printf(pr->fmt, (unsigned) dumper->address);
break; break;
case F_BPAD: case F_BPAD:
printf(pr->fmt, ""); printf(pr->fmt, "");
@ -558,7 +574,7 @@ static void display(void)
case F_CHAR: case F_CHAR:
printf(pr->fmt, *bp); printf(pr->fmt, *bp);
break; break;
case F_DBL:{ case F_DBL: {
double dval; double dval;
float fval; float fval;
@ -574,7 +590,7 @@ static void display(void)
} }
break; break;
} }
case F_INT:{ case F_INT: {
int ival; int ival;
short sval; short sval;
@ -605,7 +621,7 @@ static void display(void)
case F_U: case F_U:
conv_u(pr, bp); conv_u(pr, bp);
break; break;
case F_UINT:{ case F_UINT: {
unsigned ival; unsigned ival;
unsigned short sval; unsigned short sval;
@ -633,21 +649,21 @@ static void display(void)
} }
} }
} }
if (endfu) { if (dumper->endfu) {
/* /*
* if eaddress not set, error or file bb_dump_size was multiple of * if eaddress not set, error or file size was multiple
* bb_dump_blocksize, and no partial block ever found. * of blocksize, and no partial block ever found.
*/ */
if (!eaddress) { if (!dumper->eaddress) {
if (!address) { if (!dumper->address) {
return; return;
} }
eaddress = address; dumper->eaddress = dumper->address;
} }
for (pr = endfu->nextpr; pr; pr = pr->nextpr) { for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
switch (pr->flags) { switch (pr->flags) {
case F_ADDRESS: case F_ADDRESS:
printf(pr->fmt, (unsigned) eaddress); printf(pr->fmt, (unsigned) dumper->eaddress);
break; break;
case F_TEXT: case F_TEXT:
printf(pr->fmt); printf(pr->fmt);
@ -657,52 +673,58 @@ static void display(void)
} }
} }
int FAST_FUNC bb_dump_dump(char **argv) #define dumper ((priv_dumper_t*)pub_dumper)
int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
{ {
FS *tfs; FS *tfs;
int blocksize;
/* figure out the data block bb_dump_size */ /* figure out the data block bb_dump_size */
for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) { blocksize = 0;
tfs = dumper->pub.fshead;
while (tfs) {
tfs->bcnt = bb_dump_size(tfs); tfs->bcnt = bb_dump_size(tfs);
if (bb_dump_blocksize < tfs->bcnt) { if (blocksize < tfs->bcnt) {
bb_dump_blocksize = tfs->bcnt; blocksize = tfs->bcnt;
} }
tfs = tfs->nextfs;
} }
dumper->blocksize = blocksize;
/* rewrite the rules, do syntax checking */ /* rewrite the rules, do syntax checking */
for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) { for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
rewrite(tfs); rewrite(dumper, tfs);
} }
next(argv); dumper->argv = argv;
display(); display(dumper);
return exitval; return dumper->exitval;
} }
void FAST_FUNC bb_dump_add(const char *fmt) void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
{ {
static FS **nextfs;
const char *p; const char *p;
char *p1; char *p1;
char *p2; char *p2;
FS *tfs; FS *tfs;
FU *tfu, **nextfu; FU *tfu, **nextfupp;
const char *savep; const char *savep;
/* start new linked list of format units */ /* start new linked list of format units */
tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */ tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
if (!bb_dump_fshead) { if (!dumper->pub.fshead) {
bb_dump_fshead = tfs; dumper->pub.fshead = tfs;
} else { } else {
*nextfs = tfs; FS *fslast = dumper->pub.fshead;
while (fslast->nextfs)
fslast = fslast->nextfs;
fslast->nextfs = tfs;
} }
nextfs = &tfs->nextfs; nextfupp = &tfs->nextfu;
nextfu = &tfs->nextfu;
/* take the format string and break it up into format units */ /* take the format string and break it up into format units */
for (p = fmt;;) { for (p = fmt;;) {
/* bb_dump_skip leading white space */
p = skip_whitespace(p); p = skip_whitespace(p);
if (!*p) { if (!*p) {
break; break;
@ -712,8 +734,8 @@ void FAST_FUNC bb_dump_add(const char *fmt)
/* NOSTRICT */ /* NOSTRICT */
/* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */ /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
tfu = xzalloc(sizeof(FU)); tfu = xzalloc(sizeof(FU));
*nextfu = tfu; *nextfupp = tfu;
nextfu = &tfu->nextfu; nextfupp = &tfu->nextfu;
tfu->reps = 1; tfu->reps = 1;
/* if leading digit, repetition count */ /* if leading digit, repetition count */
@ -726,11 +748,11 @@ void FAST_FUNC bb_dump_add(const char *fmt)
/* may overwrite either white space or slash */ /* may overwrite either white space or slash */
tfu->reps = atoi(savep); tfu->reps = atoi(savep);
tfu->flags = F_SETREP; tfu->flags = F_SETREP;
/* bb_dump_skip trailing white space */ /* skip trailing white space */
p = skip_whitespace(++p); p = skip_whitespace(++p);
} }
/* bb_dump_skip slash and trailing white space */ /* skip slash and trailing white space */
if (*p == '/') { if (*p == '/') {
p = skip_whitespace(++p); p = skip_whitespace(++p);
} }
@ -745,7 +767,7 @@ void FAST_FUNC bb_dump_add(const char *fmt)
bb_error_msg_and_die("bad format {%s}", fmt); bb_error_msg_and_die("bad format {%s}", fmt);
} }
tfu->bcnt = atoi(savep); tfu->bcnt = atoi(savep);
/* bb_dump_skip trailing white space */ /* skip trailing white space */
p = skip_whitespace(++p); p = skip_whitespace(++p);
} }

View File

@ -15,7 +15,7 @@
/* This is a NOEXEC applet. Be very careful! */ /* This is a NOEXEC applet. Be very careful! */
static void bb_dump_addfile(char *name) static void bb_dump_addfile(dumper_t *dumper, char *name)
{ {
char *p; char *p;
FILE *fp; FILE *fp;
@ -27,7 +27,7 @@ static void bb_dump_addfile(char *name)
p = skip_whitespace(buf); p = skip_whitespace(buf);
if (*p && (*p != '#')) { if (*p && (*p != '#')) {
bb_dump_add(p); bb_dump_add(dumper, p);
} }
free(buf); free(buf);
} }
@ -56,6 +56,7 @@ static const struct suffix_mult suffixes[] = {
int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int hexdump_main(int argc, char **argv) int hexdump_main(int argc, char **argv)
{ {
dumper_t *dumper = alloc_dumper();
const char *p; const char *p;
int ch; int ch;
#if ENABLE_FEATURE_HEXDUMP_REVERSE #if ENABLE_FEATURE_HEXDUMP_REVERSE
@ -63,9 +64,6 @@ int hexdump_main(int argc, char **argv)
smallint rdump = 0; smallint rdump = 0;
#endif #endif
bb_dump_vflag = FIRST;
bb_dump_length = -1;
if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
ch = 'C'; ch = 'C';
goto hd_applet; goto hd_applet;
@ -78,30 +76,30 @@ int hexdump_main(int argc, char **argv)
if (!p) if (!p)
bb_show_usage(); bb_show_usage();
if ((p - hexdump_opts) < 5) { if ((p - hexdump_opts) < 5) {
bb_dump_add(add_first); bb_dump_add(dumper, add_first);
bb_dump_add(add_strings[(int)(p - hexdump_opts)]); bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
} }
/* Save a little bit of space below by omitting the 'else's. */ /* Save a little bit of space below by omitting the 'else's. */
if (ch == 'C') { if (ch == 'C') {
hd_applet: hd_applet:
bb_dump_add("\"%08.8_Ax\n\""); bb_dump_add(dumper, "\"%08.8_Ax\n\"");
bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\""); bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
} }
if (ch == 'e') { if (ch == 'e') {
bb_dump_add(optarg); bb_dump_add(dumper, optarg);
} /* else */ } /* else */
if (ch == 'f') { if (ch == 'f') {
bb_dump_addfile(optarg); bb_dump_addfile(dumper, optarg);
} /* else */ } /* else */
if (ch == 'n') { if (ch == 'n') {
bb_dump_length = xatoi_u(optarg); dumper->dump_length = xatoi_u(optarg);
} /* else */ } /* else */
if (ch == 's') { if (ch == 's') {
bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes); dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
} /* else */ } /* else */
if (ch == 'v') { if (ch == 'v') {
bb_dump_vflag = ALL; dumper->dump_vflag = ALL;
} }
#if ENABLE_FEATURE_HEXDUMP_REVERSE #if ENABLE_FEATURE_HEXDUMP_REVERSE
if (ch == 'R') { if (ch == 'R') {
@ -110,18 +108,18 @@ int hexdump_main(int argc, char **argv)
#endif #endif
} }
if (!bb_dump_fshead) { if (!dumper->fshead) {
bb_dump_add(add_first); bb_dump_add(dumper, add_first);
bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
} }
argv += optind; argv += optind;
#if !ENABLE_FEATURE_HEXDUMP_REVERSE #if !ENABLE_FEATURE_HEXDUMP_REVERSE
return bb_dump_dump(argv); return bb_dump_dump(dumper, argv);
#else #else
if (!rdump) { if (!rdump) {
return bb_dump_dump(argv); return bb_dump_dump(dumper, argv);
} }
/* -R: reverse of 'hexdump -Cv' */ /* -R: reverse of 'hexdump -Cv' */