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