hush: fix heredoc_bkslash_newline1.tests failure
[oweals/busybox.git] / libbb / dump.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Support code for the hexdump and od applets,
4  * based on code from util-linux v 2.11l
5  *
6  * Copyright (c) 1989
7  * The Regents of the University of California.  All rights reserved.
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10  *
11  * Original copyright notice is retained at the end of this file.
12  */
13 #include "libbb.h"
14 #include "dump.h"
15
16 static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789";
17
18 static const char size_conv_str[] ALIGN1 =
19 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
20
21 static const char int_convs[] ALIGN1 = "diouxX";
22
23
24 typedef struct priv_dumper_t {
25         dumper_t pub;
26
27         char **argv;
28         FU *endfu;
29         off_t savaddress;        /* saved address/offset in stream */
30         off_t eaddress;          /* end address */
31         off_t address;           /* address/offset in stream */
32         int blocksize;
33         smallint exitval;        /* final exit value */
34
35         /* former statics */
36         smallint next__done;
37         smallint get__ateof; // = 1;
38         unsigned char *get__curp;
39         unsigned char *get__savp;
40 } priv_dumper_t;
41
42 dumper_t* FAST_FUNC alloc_dumper(void)
43 {
44         priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
45         dumper->pub.dump_length = -1;
46         dumper->pub.dump_vflag = FIRST;
47         dumper->get__ateof = 1;
48         return &dumper->pub;
49 }
50
51
52 static NOINLINE int bb_dump_size(FS *fs)
53 {
54         FU *fu;
55         int bcnt, cur_size;
56         char *fmt;
57         const char *p;
58         int prec;
59
60         /* figure out the data block size needed for each format unit */
61         for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
62                 if (fu->bcnt) {
63                         cur_size += fu->bcnt * fu->reps;
64                         continue;
65                 }
66                 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
67                         if (*fmt != '%')
68                                 continue;
69                         /*
70                          * skip any special chars -- save precision in
71                          * case it's a %s format.
72                          */
73                         while (strchr(dot_flags_width_chars + 1, *++fmt))
74                                 continue;
75                         if (*fmt == '.' && isdigit(*++fmt)) {
76                                 prec = atoi(fmt);
77                                 while (isdigit(*++fmt))
78                                         continue;
79                         }
80                         p = strchr(size_conv_str + 12, *fmt);
81                         if (!p) {
82                                 if (*fmt == 's') {
83                                         bcnt += prec;
84                                 }
85                                 if (*fmt == '_') {
86                                         ++fmt;
87                                         if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
88                                                 bcnt += 1;
89                                         }
90                                 }
91                         } else {
92                                 bcnt += p[-12];
93                         }
94                 }
95                 cur_size += bcnt * fu->reps;
96         }
97         return cur_size;
98 }
99
100 static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
101 {
102         FU *fu;
103
104         for (fu = fs->nextfu; fu; fu = fu->nextfu) {
105                 PR *pr;
106                 char *p1, *p2, *p3;
107                 char *fmtp;
108                 int nconv = 0;
109                 /*
110                  * break each format unit into print units; each
111                  * conversion character gets its own.
112                  */
113                 for (fmtp = fu->fmt; *fmtp; ) {
114                         unsigned len;
115                         const char *prec;
116                         const char *byte_count_str;
117
118                         /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */
119                         pr = xzalloc(sizeof(*pr));
120                         if (!fu->nextpr)
121                                 fu->nextpr = pr;
122
123                         /* skip preceding text and up to the next % sign */
124                         p1 = strchr(fmtp, '%');
125                         if (!p1) { /* only text in the string */
126                                 pr->fmt = fmtp;
127                                 pr->flags = F_TEXT;
128                                 break;
129                         }
130
131                         /*
132                          * get precision for %s -- if have a byte count, don't
133                          * need it.
134                          */
135                         prec = NULL;
136                         if (fu->bcnt) {
137                                 /* skip to conversion character */
138                                 while (strchr(dot_flags_width_chars, *++p1))
139                                         continue;
140                         } else {
141                                 /* skip any special chars, field width */
142                                 while (strchr(dot_flags_width_chars + 1, *++p1))
143                                         continue;
144                                 if (*p1 == '.' && isdigit(*++p1)) {
145                                         prec = p1;
146                                         while (isdigit(*++p1))
147                                                 continue;
148                                 }
149                         }
150
151                         p2 = p1 + 1; /* set end pointer */
152
153                         /*
154                          * figure out the byte count for each conversion;
155                          * rewrite the format as necessary, set up blank-
156                          * padding for end of data.
157                          */
158                         if (*p1 == 'c') {
159                                 pr->flags = F_CHAR;
160  DO_BYTE_COUNT_1:
161                                 byte_count_str = "\001";
162  DO_BYTE_COUNT:
163                                 if (fu->bcnt) {
164                                         for (;;) {
165                                                 if (fu->bcnt == *byte_count_str)
166                                                         break;
167                                                 if (*++byte_count_str == 0)
168                                                         bb_error_msg_and_die("bad byte count for conversion character %s", p1);
169                                         }
170                                 }
171                                 /* Unlike the original, output the remainder of the format string. */
172                                 pr->bcnt = *byte_count_str;
173                         } else
174                         if (*p1 == 'l') { /* %ld etc */
175                                 const char *e;
176
177                                 ++p2;
178                                 ++p1;
179  DO_INT_CONV:
180                                 e = strchr(int_convs, *p1); /* "diouxX"? */
181                                 if (!e)
182                                         goto DO_BAD_CONV_CHAR;
183                                 pr->flags = F_INT;
184                                 if (e > int_convs + 1) /* not d or i? */
185                                         pr->flags = F_UINT;
186                                 byte_count_str = "\004\002\001";
187                                 goto DO_BYTE_COUNT;
188                         } else
189                         if (strchr(int_convs, *p1)) { /* %d etc */
190                                 goto DO_INT_CONV;
191                         } else
192                         if (strchr("eEfgG", *p1)) { /* floating point */
193                                 pr->flags = F_DBL;
194                                 byte_count_str = "\010\004";
195                                 goto DO_BYTE_COUNT;
196                         } else
197                         if (*p1 == 's') {
198                                 pr->flags = F_STR;
199                                 pr->bcnt = fu->bcnt;
200                                 if (fu->bcnt == 0) {
201                                         if (!prec)
202                                                 bb_error_msg_and_die("%%s needs precision or byte count");
203                                         pr->bcnt = atoi(prec);
204                                 }
205                         } else
206                         if (*p1 == '_') {
207                                 p2++;  /* move past a in "%_a" */
208                                 switch (p1[1]) {
209                                 case 'A':       /* %_A[dox]: print address and the end */
210                                         dumper->endfu = fu;
211                                         fu->flags |= F_IGNORE;
212                                         /* FALLTHROUGH */
213                                 case 'a':       /* %_a[dox]: current address */
214                                         pr->flags = F_ADDRESS;
215                                         p2++;  /* move past x in "%_ax" */
216                                         if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
217                                                 goto DO_BAD_CONV_CHAR;
218                                         }
219                                         *p1 = p1[2];
220                                         break;
221                                 case 'c':       /* %_c: chars, \ooo, \n \r \t etc */
222                                         pr->flags = F_C;
223                                         /* *p1 = 'c';   set in conv_c */
224                                         goto DO_BYTE_COUNT_1;
225                                 case 'p':       /* %_p: chars, dots for nonprintable */
226                                         pr->flags = F_P;
227                                         *p1 = 'c';
228                                         goto DO_BYTE_COUNT_1;
229                                 case 'u':       /* %_p: chars, 'nul', 'esc' etc for nonprintable */
230                                         pr->flags = F_U;
231                                         /* *p1 = 'c';   set in conv_u */
232                                         goto DO_BYTE_COUNT_1;
233                                 default:
234                                         goto DO_BAD_CONV_CHAR;
235                                 }
236                         } else {
237  DO_BAD_CONV_CHAR:
238                                 bb_error_msg_and_die("bad conversion character %%%s", p1);
239                         }
240
241                         /*
242                          * copy to PR format string, set conversion character
243                          * pointer, update original.
244                          */
245                         len = (p1 - fmtp) + 1;
246                         pr->fmt = xstrndup(fmtp, len);
247                         /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
248                          * Skip subsequent text and up to the next % sign and tack the
249                          * additional text onto fmt: eg. if fmt is "%x is a HEX number",
250                          * we lose the " is a HEX number" part of fmt.
251                          */
252                         for (p3 = p2; *p3 && *p3 != '%'; p3++)
253                                 continue;
254                         if ((p3 - p2) != 0) {
255                                 char *d;
256                                 pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1);
257                                 d += len;
258                                 do {
259                                         *d++ = *p2++;
260                                 } while (p2 != p3);
261                                 *d = '\0';
262                                 /* now p2 = p3 */
263                         }
264                         pr->cchar = pr->fmt + len - 1; /* must be after realloc! */
265                         fmtp = p2;
266
267                         /* only one conversion character if byte count */
268                         if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
269                                 bb_error_msg_and_die("byte count with multiple conversion characters");
270                         }
271                 }
272                 /*
273                  * if format unit byte count not specified, figure it out
274                  * so can adjust rep count later.
275                  */
276                 if (fu->bcnt == 0)
277                         for (pr = fu->nextpr; pr; pr = pr->nextpr)
278                                 fu->bcnt += pr->bcnt;
279         }
280         /*
281          * if the format string interprets any data at all, and it's
282          * not the same as the blocksize, and its last format unit
283          * interprets any data at all, and has no iteration count,
284          * repeat it as necessary.
285          *
286          * if rep count is greater than 1, no trailing whitespace
287          * gets output from the last iteration of the format unit.
288          */
289         for (fu = fs->nextfu; fu; fu = fu->nextfu) {
290                 if (!fu->nextfu
291                  && fs->bcnt < dumper->blocksize
292                  && !(fu->flags & F_SETREP)
293                  && fu->bcnt
294                 ) {
295                         fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
296                 }
297                 if (fu->reps > 1 && fu->nextpr) {
298                         PR *pr;
299                         char *p1, *p2;
300
301                         for (pr = fu->nextpr;; pr = pr->nextpr)
302                                 if (!pr->nextpr)
303                                         break;
304                         p2 = NULL;
305                         for (p1 = pr->fmt; *p1; ++p1)
306                                 p2 = isspace(*p1) ? p1 : NULL;
307                         if (p2)
308                                 pr->nospace = p2;
309                 }
310         }
311 }
312
313 static void do_skip(priv_dumper_t *dumper, const char *fname)
314 {
315         struct stat sbuf;
316
317         xfstat(STDIN_FILENO, &sbuf, fname);
318         if (S_ISREG(sbuf.st_mode)
319          && dumper->pub.dump_skip >= sbuf.st_size
320         ) {
321                 /* If st_size is valid and pub.dump_skip >= st_size */
322                 dumper->pub.dump_skip -= sbuf.st_size;
323                 dumper->address += sbuf.st_size;
324                 return;
325         }
326         if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
327                 bb_simple_perror_msg_and_die(fname);
328         }
329         dumper->address += dumper->pub.dump_skip;
330         dumper->savaddress = dumper->address;
331         dumper->pub.dump_skip = 0;
332 }
333
334 static NOINLINE int next(priv_dumper_t *dumper)
335 {
336         for (;;) {
337                 const char *fname = *dumper->argv;
338
339                 if (fname) {
340                         dumper->argv++;
341                         if (NOT_LONE_DASH(fname)) {
342                                 if (!freopen(fname, "r", stdin)) {
343                                         bb_simple_perror_msg(fname);
344                                         dumper->exitval = 1;
345                                         continue;
346                                 }
347                         }
348                 } else {
349                         if (dumper->next__done)
350                                 return 0; /* no next file */
351                 }
352                 dumper->next__done = 1;
353                 if (dumper->pub.dump_skip)
354                         do_skip(dumper, fname ? fname : "stdin");
355                 if (dumper->pub.dump_skip == 0)
356                         return 1;
357         }
358         /* NOTREACHED */
359 }
360
361 static unsigned char *get(priv_dumper_t *dumper)
362 {
363         int n;
364         int need, nread;
365         int blocksize = dumper->blocksize;
366
367         if (!dumper->get__curp) {
368                 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
369                 dumper->get__curp = xmalloc(blocksize);
370                 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
371         } else {
372                 unsigned char *tmp = dumper->get__curp;
373                 dumper->get__curp = dumper->get__savp;
374                 dumper->get__savp = tmp;
375                 dumper->savaddress += blocksize;
376                 dumper->address = dumper->savaddress;
377         }
378         need = blocksize;
379         nread = 0;
380         while (1) {
381                 /*
382                  * if read the right number of bytes, or at EOF for one file,
383                  * and no other files are available, zero-pad the rest of the
384                  * block and set the end flag.
385                  */
386                 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
387                         if (need == blocksize) {
388                                 return NULL;
389                         }
390                         if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
391                                 if (dumper->pub.dump_vflag != DUP) {
392                                         puts("*");
393                                 }
394                                 return NULL;
395                         }
396                         memset(dumper->get__curp + nread, 0, need);
397                         dumper->eaddress = dumper->address + nread;
398                         return dumper->get__curp;
399                 }
400                 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
401                                 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
402                 if (!n) {
403                         if (ferror(stdin)) {
404                                 bb_simple_perror_msg(dumper->argv[-1]);
405                         }
406                         dumper->get__ateof = 1;
407                         continue;
408                 }
409                 dumper->get__ateof = 0;
410                 if (dumper->pub.dump_length != -1) {
411                         dumper->pub.dump_length -= n;
412                 }
413                 need -= n;
414                 if (!need) {
415                         if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
416                          || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
417                         ) {
418                                 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
419                                         dumper->pub.dump_vflag = WAIT;
420                                 }
421                                 return dumper->get__curp;
422                         }
423                         if (dumper->pub.dump_vflag == WAIT) {
424                                 puts("*");
425                         }
426                         dumper->pub.dump_vflag = DUP;
427                         dumper->savaddress += blocksize;
428                         dumper->address = dumper->savaddress;
429                         need = blocksize;
430                         nread = 0;
431                 } else {
432                         nread += n;
433                 }
434         }
435 }
436
437 static void bpad(PR *pr)
438 {
439         char *p1, *p2;
440
441         /*
442          * remove all conversion flags; '-' is the only one valid
443          * with %s, and it's not useful here.
444          */
445         pr->flags = F_BPAD;
446         *pr->cchar = 's';
447         for (p1 = pr->fmt; *p1 != '%'; ++p1)
448                 continue;
449         for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
450                 if (pr->nospace)
451                         pr->nospace--;
452         while ((*p2++ = *p1++) != 0)
453                 continue;
454 }
455
456 static const char conv_str[] ALIGN1 =
457         "\0"  "\\""0""\0"
458         "\007""\\""a""\0"  /* \a */
459         "\b"  "\\""b""\0"
460         "\f"  "\\""f""\0"
461         "\n"  "\\""n""\0"
462         "\r"  "\\""r""\0"
463         "\t"  "\\""t""\0"
464         "\v"  "\\""v""\0"
465         ;
466
467
468 static void conv_c(PR *pr, unsigned char *p)
469 {
470         const char *str = conv_str;
471         char buf[10];
472
473         do {
474                 if (*p == *str) {
475                         ++str;
476                         goto strpr; /* map e.g. '\n' to "\\n" */
477                 }
478                 str += 4;
479         } while (*str);
480
481         if (isprint_asciionly(*p)) {
482                 *pr->cchar = 'c';
483                 printf(pr->fmt, *p);
484         } else {
485                 sprintf(buf, "%03o", (int) *p);
486                 str = buf;
487  strpr:
488                 *pr->cchar = 's';
489                 printf(pr->fmt, str);
490         }
491 }
492
493 static void conv_u(PR *pr, unsigned char *p)
494 {
495         static const char list[] ALIGN1 =
496                 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
497                 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
498                 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
499                 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
500
501         /* od used nl, not lf */
502         if (*p <= 0x1f) {
503                 *pr->cchar = 's';
504                 printf(pr->fmt, list + (4 * (int)*p));
505         } else if (*p == 0x7f) {
506                 *pr->cchar = 's';
507                 printf(pr->fmt, "del");
508         } else if (*p < 0x7f) { /* isprint() */
509                 *pr->cchar = 'c';
510                 printf(pr->fmt, *p);
511         } else {
512                 *pr->cchar = 'x';
513                 printf(pr->fmt, (int) *p);
514         }
515 }
516
517 static void display(priv_dumper_t* dumper)
518 {
519         FS *fs;
520         FU *fu;
521         PR *pr;
522         int cnt;
523         unsigned char *bp, *savebp;
524         off_t saveaddress;
525         unsigned char savech = '\0';
526
527         while ((bp = get(dumper)) != NULL) {
528                 fs = dumper->pub.fshead;
529                 savebp = bp;
530                 saveaddress = dumper->address;
531                 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
532                         for (fu = fs->nextfu; fu; fu = fu->nextfu) {
533                                 if (fu->flags & F_IGNORE) {
534                                         break;
535                                 }
536                                 for (cnt = fu->reps; cnt; --cnt) {
537                                         for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
538                                                                 bp += pr->bcnt, pr = pr->nextpr) {
539                                                 if (dumper->eaddress && dumper->address >= dumper->eaddress
540                                                  && !(pr->flags & (F_TEXT | F_BPAD))
541                                                 ) {
542                                                         bpad(pr);
543                                                 }
544                                                 if (cnt == 1 && pr->nospace) {
545                                                         savech = *pr->nospace;
546                                                         *pr->nospace = '\0';
547                                                 }
548 /*                      PRINT; */
549                                                 switch (pr->flags) {
550                                                 case F_ADDRESS:
551                                                         printf(pr->fmt, (unsigned) dumper->address);
552                                                         break;
553                                                 case F_BPAD:
554                                                         printf(pr->fmt, "");
555                                                         break;
556                                                 case F_C:
557                                                         conv_c(pr, bp);
558                                                         break;
559                                                 case F_CHAR:
560                                                         printf(pr->fmt, *bp);
561                                                         break;
562                                                 case F_DBL: {
563                                                         double dval;
564                                                         float fval;
565
566                                                         switch (pr->bcnt) {
567                                                         case 4:
568                                                                 memcpy(&fval, bp, sizeof(fval));
569                                                                 printf(pr->fmt, fval);
570                                                                 break;
571                                                         case 8:
572                                                                 memcpy(&dval, bp, sizeof(dval));
573                                                                 printf(pr->fmt, dval);
574                                                                 break;
575                                                         }
576                                                         break;
577                                                 }
578                                                 case F_INT: {
579                                                         int ival;
580                                                         short sval;
581
582                                                         switch (pr->bcnt) {
583                                                         case 1:
584                                                                 printf(pr->fmt, (int) *bp);
585                                                                 break;
586                                                         case 2:
587                                                                 memcpy(&sval, bp, sizeof(sval));
588                                                                 printf(pr->fmt, (int) sval);
589                                                                 break;
590                                                         case 4:
591                                                                 memcpy(&ival, bp, sizeof(ival));
592                                                                 printf(pr->fmt, ival);
593                                                                 break;
594                                                         }
595                                                         break;
596                                                 }
597                                                 case F_P:
598                                                         printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
599                                                         break;
600                                                 case F_STR:
601                                                         printf(pr->fmt, (char *) bp);
602                                                         break;
603                                                 case F_TEXT:
604                                                         printf(pr->fmt);
605                                                         break;
606                                                 case F_U:
607                                                         conv_u(pr, bp);
608                                                         break;
609                                                 case F_UINT: {
610                                                         unsigned ival;
611                                                         unsigned short sval;
612
613                                                         switch (pr->bcnt) {
614                                                         case 1:
615                                                                 printf(pr->fmt, (unsigned) *bp);
616                                                                 break;
617                                                         case 2:
618                                                                 memcpy(&sval, bp, sizeof(sval));
619                                                                 printf(pr->fmt, (unsigned) sval);
620                                                                 break;
621                                                         case 4:
622                                                                 memcpy(&ival, bp, sizeof(ival));
623                                                                 printf(pr->fmt, ival);
624                                                                 break;
625                                                         }
626                                                         break;
627                                                 }
628                                                 }
629                                                 if (cnt == 1 && pr->nospace) {
630                                                         *pr->nospace = savech;
631                                                 }
632                                         }
633                                 }
634                         }
635                 }
636         }
637         if (dumper->endfu) {
638                 /*
639                  * if eaddress not set, error or file size was multiple
640                  * of blocksize, and no partial block ever found.
641                  */
642                 if (!dumper->eaddress) {
643                         if (!dumper->address) {
644                                 return;
645                         }
646                         dumper->eaddress = dumper->address;
647                 }
648                 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
649                         switch (pr->flags) {
650                         case F_ADDRESS:
651                                 printf(pr->fmt, (unsigned) dumper->eaddress);
652                                 break;
653                         case F_TEXT:
654                                 printf(pr->fmt);
655                                 break;
656                         }
657                 }
658         }
659 }
660
661 #define dumper ((priv_dumper_t*)pub_dumper)
662 int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
663 {
664         FS *tfs;
665         int blocksize;
666
667         /* figure out the data block size */
668         blocksize = 0;
669         tfs = dumper->pub.fshead;
670         while (tfs) {
671                 tfs->bcnt = bb_dump_size(tfs);
672                 if (blocksize < tfs->bcnt) {
673                         blocksize = tfs->bcnt;
674                 }
675                 tfs = tfs->nextfs;
676         }
677         dumper->blocksize = blocksize;
678
679         /* rewrite the rules, do syntax checking */
680         for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
681                 rewrite(dumper, tfs);
682         }
683
684         dumper->argv = argv;
685         display(dumper);
686
687         return dumper->exitval;
688 }
689
690 void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
691 {
692         const char *p;
693         FS *tfs;
694         FU *tfu, **nextfupp;
695         const char *savep;
696
697         /* start new linked list of format units */
698         tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
699         if (!dumper->pub.fshead) {
700                 dumper->pub.fshead = tfs;
701         } else {
702                 FS *fslast = dumper->pub.fshead;
703                 while (fslast->nextfs)
704                         fslast = fslast->nextfs;
705                 fslast->nextfs = tfs;
706         }
707         nextfupp = &tfs->nextfu;
708
709         /* take the format string and break it up into format units */
710         p = fmt;
711         for (;;) {
712                 p = skip_whitespace(p);
713                 if (*p == '\0') {
714                         break;
715                 }
716
717                 /* allocate a new format unit and link it in */
718                 /* NOSTRICT */
719                 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
720                 tfu = xzalloc(sizeof(FU));
721                 *nextfupp = tfu;
722                 nextfupp = &tfu->nextfu;
723                 tfu->reps = 1;
724
725                 /* if leading digit, repetition count */
726                 if (isdigit(*p)) {
727                         for (savep = p; isdigit(*p); ++p)
728                                 continue;
729                         if (!isspace(*p) && *p != '/') {
730                                 bb_error_msg_and_die("bad format {%s}", fmt);
731                         }
732                         /* may overwrite either white space or slash */
733                         tfu->reps = atoi(savep);
734                         tfu->flags = F_SETREP;
735                         /* skip trailing white space */
736                         p = skip_whitespace(++p);
737                 }
738
739                 /* skip slash and trailing white space */
740                 if (*p == '/') {
741                         p = skip_whitespace(p + 1);
742                 }
743
744                 /* byte count */
745                 if (isdigit(*p)) {
746 // TODO: use bb_strtou
747                         savep = p;
748                         while (isdigit(*++p))
749                                 continue;
750                         if (!isspace(*p)) {
751                                 bb_error_msg_and_die("bad format {%s}", fmt);
752                         }
753 // Above check prohibits formats such as '/1"%02x"' - it requires space after 1.
754 // Other than this, formats can be pretty much jammed together:
755 // "%07_ax:"8/2 "%04x|""\n"
756 // but this space is required. The check *can* be removed, but
757 // keeping it to stay compat with util-linux hexdump.
758                         tfu->bcnt = atoi(savep);
759                         /* skip trailing white space */
760                         p = skip_whitespace(p + 1);
761                 }
762
763                 /* format */
764                 if (*p != '"') {
765                         bb_error_msg_and_die("bad format {%s}", fmt);
766                 }
767                 for (savep = ++p; *p != '"';) {
768                         if (*p++ == '\0') {
769                                 bb_error_msg_and_die("bad format {%s}", fmt);
770                         }
771                 }
772                 tfu->fmt = xstrndup(savep, p - savep);
773
774                 /* alphabetic escape sequences have to be done in place */
775                 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
776                 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
777                 /* trailing backslash, if any, is preserved */
778 #if 0
779                 char *p1;
780                 char *p2;
781                 p1 = tfu->fmt;
782                 for (p2 = p1;; ++p1, ++p2) {
783                         *p2 = *p1;
784                         if (*p1 == '\0')
785                                 break;
786
787                         if (*p1 == '\\') {
788                                 const char *cs;
789
790                                 p1++;
791                                 *p2 = *p1;
792                                 if (*p1 == '\0') {
793                                         /* "...\" trailing backslash. Eaten. */
794                                         break;
795                                 }
796                                 cs = conv_str + 4; /* skip NUL element */
797                                 do {
798                                         /* map e.g. "\n" -> '\n' */
799                                         if (*p1 == cs[2]) {
800                                                 *p2 = cs[0];
801                                                 break;
802                                         }
803                                         cs += 4;
804                                 } while (*cs);
805                                 /* unknown mappings remove bkslash: "\z" -> 'z' */
806                         }
807                 }
808 #endif
809
810                 p++;
811         }
812 }
813
814 /*
815  * Copyright (c) 1989 The Regents of the University of California.
816  * All rights reserved.
817  *
818  * Redistribution and use in source and binary forms, with or without
819  * modification, are permitted provided that the following conditions
820  * are met:
821  * 1. Redistributions of source code must retain the above copyright
822  *    notice, this list of conditions and the following disclaimer.
823  * 2. Redistributions in binary form must reproduce the above copyright
824  *    notice, this list of conditions and the following disclaimer in the
825  *    documentation and/or other materials provided with the distribution.
826  * 3. Neither the name of the University nor the names of its contributors
827  *    may be used to endorse or promote products derived from this software
828  *    without specific prior written permission.
829  *
830  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
831  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
832  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
833  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
834  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
835  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
836  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
837  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
838  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
839  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
840  * SUCH DAMAGE.
841  */