Standardize on the vi editing directives being on the first line.
[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 tarball for details.
10  *
11  * Original copyright notice is retained at the end of this file.
12  */
13
14 #include "libbb.h"
15 #include <string.h>
16 #include <unistd.h>
17 #include <ctype.h>              /* for isdigit() */
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         FU *fu;
42         int bcnt, cur_size;
43         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         PR *pr, **nextpr = NULL;
87         FU *fu;
88         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 = xzalloc(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                                 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
250                                 strcat(pr->fmt, p2);
251                                 *p3 = savech;
252                                 p2 = p3;
253                         }
254
255                         fmtp = p2;
256
257                         /* only one conversion character if byte count */
258                         if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
259                                 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
260                         }
261                 }
262                 /*
263                  * if format unit byte count not specified, figure it out
264                  * so can adjust rep count later.
265                  */
266                 if (!fu->bcnt)
267                         for (pr = fu->nextpr; pr; pr = pr->nextpr)
268                                 fu->bcnt += pr->bcnt;
269         }
270         /*
271          * if the format string interprets any data at all, and it's
272          * not the same as the bb_dump_blocksize, and its last format unit
273          * interprets any data at all, and has no iteration count,
274          * repeat it as necessary.
275          *
276          * if, rep count is greater than 1, no trailing whitespace
277          * gets output from the last iteration of the format unit.
278          */
279         for (fu = fs->nextfu;; fu = fu->nextfu) {
280                 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
281                         !(fu->flags & F_SETREP) && fu->bcnt)
282                         fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
283                 if (fu->reps > 1) {
284                         for (pr = fu->nextpr;; pr = pr->nextpr)
285                                 if (!pr->nextpr)
286                                         break;
287                         for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
288                                 p2 = isspace(*p1) ? p1 : NULL;
289                         if (p2)
290                                 pr->nospace = p2;
291                 }
292                 if (!fu->nextfu)
293                         break;
294         }
295 }
296
297 static void do_skip(char *fname, int statok)
298 {
299         struct stat sbuf;
300
301         if (statok) {
302                 if (fstat(STDIN_FILENO, &sbuf)) {
303                         bb_perror_msg_and_die("%s", fname);
304                 }
305                 if ((!(S_ISCHR(sbuf.st_mode) ||
306                            S_ISBLK(sbuf.st_mode) ||
307                            S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
308                         /* If bb_dump_size valid and bb_dump_skip >= size */
309                         bb_dump_skip -= sbuf.st_size;
310                         address += sbuf.st_size;
311                         return;
312                 }
313         }
314         if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
315                 bb_perror_msg_and_die("%s", fname);
316         }
317         savaddress = address += bb_dump_skip;
318         bb_dump_skip = 0;
319 }
320
321 static int next(char **argv)
322 {
323         static int done;
324         int statok;
325
326         if (argv) {
327                 _argv = argv;
328                 return (1);
329         }
330         for (;;) {
331                 if (*_argv) {
332                         if (!(freopen(*_argv, "r", stdin))) {
333                                 bb_perror_msg("%s", *_argv);
334                                 exitval = 1;
335                                 ++_argv;
336                                 continue;
337                         }
338                         statok = done = 1;
339                 } else {
340                         if (done++)
341                                 return (0);
342                         statok = 0;
343                 }
344                 if (bb_dump_skip)
345                         do_skip(statok ? *_argv : "stdin", statok);
346                 if (*_argv)
347                         ++_argv;
348                 if (!bb_dump_skip)
349                         return (1);
350         }
351         /* NOTREACHED */
352 }
353
354 static unsigned char *get(void)
355 {
356         static int ateof = 1;
357         static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
358         int n;
359         int need, nread;
360         unsigned char *tmpp;
361
362         if (!curp) {
363                 address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
364                 curp = (unsigned char *) xmalloc(bb_dump_blocksize);
365                 savp = (unsigned char *) xmalloc(bb_dump_blocksize);
366         } else {
367                 tmpp = curp;
368                 curp = savp;
369                 savp = tmpp;
370                 address = savaddress += bb_dump_blocksize;
371         }
372         for (need = bb_dump_blocksize, nread = 0;;) {
373                 /*
374                  * if read the right number of bytes, or at EOF for one file,
375                  * and no other files are available, zero-pad the rest of the
376                  * block and set the end flag.
377                  */
378                 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
379                         if (need == bb_dump_blocksize) {
380                                 return ((unsigned char *) NULL);
381                         }
382                         if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
383                                 if (bb_dump_vflag != DUP) {
384                                         printf("*\n");
385                                 }
386                                 return ((unsigned char *) NULL);
387                         }
388                         memset((char *) curp + nread, 0, need);
389                         eaddress = address + nread;
390                         return (curp);
391                 }
392                 n = fread((char *) curp + nread, sizeof(unsigned char),
393                                   bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
394                 if (!n) {
395                         if (ferror(stdin)) {
396                                 bb_perror_msg("%s", _argv[-1]);
397                         }
398                         ateof = 1;
399                         continue;
400                 }
401                 ateof = 0;
402                 if (bb_dump_length != -1) {
403                         bb_dump_length -= n;
404                 }
405                 if (!(need -= n)) {
406                         if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
407                                 || memcmp(curp, savp, bb_dump_blocksize)) {
408                                 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
409                                         bb_dump_vflag = WAIT;
410                                 }
411                                 return (curp);
412                         }
413                         if (bb_dump_vflag == WAIT) {
414                                 printf("*\n");
415                         }
416                         bb_dump_vflag = DUP;
417                         address = savaddress += bb_dump_blocksize;
418                         need = bb_dump_blocksize;
419                         nread = 0;
420                 } else {
421                         nread += n;
422                 }
423         }
424 }
425
426 static void bpad(PR * pr)
427 {
428         char *p1, *p2;
429
430         /*
431          * remove all conversion flags; '-' is the only one valid
432          * with %s, and it's not useful here.
433          */
434         pr->flags = F_BPAD;
435         *pr->cchar = 's';
436         for (p1 = pr->fmt; *p1 != '%'; ++p1);
437         for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
438                 if (pr->nospace) pr->nospace--;
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         FS *fs;
507         FU *fu;
508         PR *pr;
509         int cnt;
510         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                                                                 memmove((char *) &fval, (char *) bp,
554                                                                           sizeof(fval));
555                                                                 printf(pr->fmt, fval);
556                                                                 break;
557                                                         case 8:
558                                                                 memmove((char *) &dval, (char *) bp,
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                                                                 memmove((char *) &sval, (char *) bp,
575                                                                           sizeof(sval));
576                                                                 printf(pr->fmt, (int) sval);
577                                                                 break;
578                                                         case 4:
579                                                                 memmove((char *) &ival, (char *) bp,
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                                                                 memmove((char *) &sval, (char *) bp,
608                                                                           sizeof(sval));
609                                                                 printf(pr->fmt, (unsigned int) sval);
610                                                                 break;
611                                                         case 4:
612                                                                 memmove((char *) &ival, (char *) bp,
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         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         const char *p;
677         char *p1;
678         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         tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
686         if (!bb_dump_fshead) {
687                 bb_dump_fshead = tfs;
688         } else {
689                 *nextfs = tfs;
690         }
691         nextfs = &tfs->nextfs;
692         nextfu = &tfs->nextfu;
693
694         /* take the format string and break it up into format units */
695         for (p = fmt;;) {
696                 /* bb_dump_skip leading white space */
697                 p = skip_whitespace(p);
698                 if (!*p) {
699                         break;
700                 }
701
702                 /* allocate a new format unit and link it in */
703                 /* NOSTRICT */
704                 /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
705                 tfu = xzalloc(sizeof(FU));
706                 *nextfu = tfu;
707                 nextfu = &tfu->nextfu;
708                 tfu->reps = 1;
709
710                 /* if leading digit, repetition count */
711                 if (isdigit(*p)) {
712                         for (savep = p; isdigit(*p); ++p);
713                         if (!isspace(*p) && *p != '/') {
714                                 bb_error_msg_and_die("bad format {%s}", fmt);
715                         }
716                         /* may overwrite either white space or slash */
717                         tfu->reps = atoi(savep);
718                         tfu->flags = F_SETREP;
719                         /* bb_dump_skip trailing white space */
720                         p = skip_whitespace(++p);
721                 }
722
723                 /* bb_dump_skip slash and trailing white space */
724                 if (*p == '/') {
725                         p = skip_whitespace(++p);
726                 }
727
728                 /* byte count */
729                 if (isdigit(*p)) {
730                         for (savep = p; isdigit(*p); ++p);
731                         if (!isspace(*p)) {
732                                 bb_error_msg_and_die("bad format {%s}", fmt);
733                         }
734                         tfu->bcnt = atoi(savep);
735                         /* bb_dump_skip trailing white space */
736                         p = skip_whitespace(++p);
737                 }
738
739                 /* format */
740                 if (*p != '"') {
741                         bb_error_msg_and_die("bad format {%s}", fmt);
742                 }
743                 for (savep = ++p; *p != '"';) {
744                         if (*p++ == 0) {
745                                 bb_error_msg_and_die("bad format {%s}", fmt);
746                         }
747                 }
748                 tfu->fmt = xmalloc(p - savep + 1);
749                 strncpy(tfu->fmt, savep, p - savep);
750                 tfu->fmt[p - savep] = '\0';
751 /*      escape(tfu->fmt); */
752
753                 p1 = tfu->fmt;
754
755                 /* alphabetic escape sequences have to be done in place */
756                 for (p2 = p1;; ++p1, ++p2) {
757                         if (!*p1) {
758                                 *p2 = *p1;
759                                 break;
760                         }
761                         if (*p1 == '\\') {
762                                 const char *cs = conv_str + 4;
763                                 ++p1;
764                                 *p2 = *p1;
765                                 do {
766                                         if (*p1 == cs[2]) {
767                                                 *p2 = cs[0];
768                                                 break;
769                                         }
770                                         cs += 4;
771                                 } while (*cs);
772                         }
773                 }
774
775                 p++;
776         }
777 }
778
779 /*
780  * Copyright (c) 1989 The Regents of the University of California.
781  * All rights reserved.
782  *
783  * Redistribution and use in source and binary forms, with or without
784  * modification, are permitted provided that the following conditions
785  * are met:
786  * 1. Redistributions of source code must retain the above copyright
787  *    notice, this list of conditions and the following disclaimer.
788  * 2. Redistributions in binary form must reproduce the above copyright
789  *    notice, this list of conditions and the following disclaimer in the
790  *    documentation and/or other materials provided with the distribution.
791  * 3. Neither the name of the University nor the names of its contributors
792  *    may be used to endorse or promote products derived from this software
793  *    without specific prior written permission.
794  *
795  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
796  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
797  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
798  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
799  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
800  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
801  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
802  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
803  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
804  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
805  * SUCH DAMAGE.
806  */