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