blob: b465c5d2241fb8057e484cdec521de7c50d110f4 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath60281112001-11-02 11:39:46 +00002/*
3 * Support code for the hexdump and od applets,
4 * based on code from util-linux v 2.11l
5 *
6 * Copyright (c) 1989
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02007 * The Regents of the University of California. All rights reserved.
Glenn L McGrath60281112001-11-02 11:39:46 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath60281112001-11-02 11:39:46 +000010 *
11 * Original copyright notice is retained at the end of this file.
12 */
13
Rob Landleyea224be2006-06-18 20:20:07 +000014#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000016
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000017static const char index_str[] ALIGN1 = ".#-+ 0123456789";
Glenn L McGrath60281112001-11-02 11:39:46 +000018
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000019static const char size_conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +000020"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
21
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000022static const char lcc[] ALIGN1 = "diouxX";
Manuel Novoa III cad53642003-03-19 09:13:01 +000023
Denis Vlasenko55f79122008-07-16 11:00:16 +000024
25typedef struct priv_dumper_t {
26 dumper_t pub;
27
28 char **argv;
29 FU *endfu;
30 off_t savaddress; /* saved address/offset in stream */
31 off_t eaddress; /* end address */
32 off_t address; /* address/offset in stream */
33 int blocksize;
34 smallint exitval; /* final exit value */
35
36 /* former statics */
37 smallint next__done;
38 smallint get__ateof; // = 1;
39 unsigned char *get__curp;
40 unsigned char *get__savp;
41} priv_dumper_t;
42
43dumper_t* FAST_FUNC alloc_dumper(void)
44{
45 priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
46 dumper->pub.dump_length = -1;
47 dumper->pub.dump_vflag = FIRST;
48 dumper->get__ateof = 1;
49 return &dumper->pub;
50}
51
52
53static NOINLINE int bb_dump_size(FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +000054{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000055 FU *fu;
56 int bcnt, cur_size;
57 char *fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000059 int prec;
60
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 /* figure out the data block bb_dump_size needed for each format unit */
62 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +000063 if (fu->bcnt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 cur_size += fu->bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000065 continue;
66 }
67 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
68 if (*fmt != '%')
69 continue;
70 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +000071 * skip any special chars -- save precision in
Glenn L McGrath60281112001-11-02 11:39:46 +000072 * case it's a %s format.
73 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020074 while (strchr(index_str + 1, *++fmt))
75 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000076 if (*fmt == '.' && isdigit(*++fmt)) {
77 prec = atoi(fmt);
Denis Vlasenko55f79122008-07-16 11:00:16 +000078 while (isdigit(*++fmt))
79 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000080 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000081 p = strchr(size_conv_str + 12, *fmt);
82 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 if (*fmt == 's') {
84 bcnt += prec;
85 } else if (*fmt == '_') {
86 ++fmt;
87 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
88 bcnt += 1;
89 }
Glenn L McGrath60281112001-11-02 11:39:46 +000090 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 } else {
92 bcnt += size_conv_str[p - (size_conv_str + 12)];
Glenn L McGrath60281112001-11-02 11:39:46 +000093 }
94 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000096 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000097 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000098}
99
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200100static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +0000101{
102 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
Rob Landleyea224be2006-06-18 20:20:07 +0000103 FU *fu;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200104 PR *pr;
Rob Landleyea224be2006-06-18 20:20:07 +0000105 char *p1, *p2, *p3;
Glenn L McGrath60281112001-11-02 11:39:46 +0000106 char savech, *fmtp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 const char *byte_count_str;
Glenn L McGrath60281112001-11-02 11:39:46 +0000108 int nconv, prec = 0;
109
110 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
111 /*
112 * break each format unit into print units; each
113 * conversion character gets its own.
114 */
Denys Vlasenko60a94142011-05-13 20:57:01 +0200115 for (nconv = 0, fmtp = fu->fmt; *fmtp; ) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000116 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000117 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
Rob Landleyea224be2006-06-18 20:20:07 +0000118 pr = xzalloc(sizeof(PR));
Glenn L McGrath60281112001-11-02 11:39:46 +0000119 if (!fu->nextpr)
120 fu->nextpr = pr;
Glenn L McGrath60281112001-11-02 11:39:46 +0000121
Denis Vlasenko55f79122008-07-16 11:00:16 +0000122 /* skip preceding text and up to the next % sign */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000123 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1)
124 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000125
126 /* only text in the string */
127 if (!*p1) {
128 pr->fmt = fmtp;
129 pr->flags = F_TEXT;
130 break;
131 }
132
133 /*
134 * get precision for %s -- if have a byte count, don't
135 * need it.
136 */
137 if (fu->bcnt) {
138 sokay = USEBCNT;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000139 /* skip to conversion character */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000140 for (++p1; strchr(index_str, *p1); ++p1)
141 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000142 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000143 /* skip any special chars, field width */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000144 while (strchr(index_str + 1, *++p1))
145 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000146 if (*p1 == '.' && isdigit(*++p1)) {
147 sokay = USEPREC;
148 prec = atoi(p1);
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000149 while (isdigit(*++p1))
150 continue;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000151 } else
Glenn L McGrath60281112001-11-02 11:39:46 +0000152 sokay = NOTOKAY;
153 }
154
Denis Vlasenko55f79122008-07-16 11:00:16 +0000155 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000156
157 /*
158 * figure out the byte count for each conversion;
159 * rewrite the format as necessary, set up blank-
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 * pbb_dump_adding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000161 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000163 pr->flags = F_CHAR;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000164 DO_BYTE_COUNT_1:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 byte_count_str = "\001";
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000166 DO_BYTE_COUNT:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 if (fu->bcnt) {
168 do {
169 if (fu->bcnt == *byte_count_str) {
170 break;
171 }
172 } while (*++byte_count_str);
Glenn L McGrath60281112001-11-02 11:39:46 +0000173 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 /* Unlike the original, output the remainder of the format string. */
175 if (!*byte_count_str) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000176 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 }
178 pr->bcnt = *byte_count_str;
179 } else if (*p1 == 'l') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000180 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181 ++p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000182 DO_INT_CONV:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 {
184 const char *e;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000185 e = strchr(lcc, *p1);
186 if (!e) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000187 goto DO_BAD_CONV_CHAR;
188 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000189 pr->flags = F_INT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190 if (e > lcc + 1) {
191 pr->flags = F_UINT;
192 }
193 byte_count_str = "\004\002\001";
194 goto DO_BYTE_COUNT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000195 }
196 /* NOTREACHED */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 } else if (strchr(lcc, *p1)) {
198 goto DO_INT_CONV;
199 } else if (strchr("eEfgG", *p1)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000200 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000201 byte_count_str = "\010\004";
202 goto DO_BYTE_COUNT;
203 } else if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000204 pr->flags = F_STR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000205 if (sokay == USEBCNT) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000206 pr->bcnt = fu->bcnt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 } else if (sokay == USEPREC) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000208 pr->bcnt = prec;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200209 } else { /* NOTOKAY */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000210 bb_error_msg_and_die("%%s requires a precision or a byte count");
Glenn L McGrath60281112001-11-02 11:39:46 +0000211 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 } else if (*p1 == '_') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000213 ++p2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000214 switch (p1[1]) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000215 case 'A':
Denis Vlasenko55f79122008-07-16 11:00:16 +0000216 dumper->endfu = fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000217 fu->flags |= F_IGNORE;
218 /* FALLTHROUGH */
219 case 'a':
220 pr->flags = F_ADDRESS;
221 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
223 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000224 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000226 break;
227 case 'c':
228 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000229 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000231 case 'p':
232 pr->flags = F_P;
233 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000235 case 'u':
236 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000237 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000239 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000241 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 } else {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000243 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000244 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000245 }
246
247 /*
248 * copy to PR format string, set conversion character
249 * pointer, update original.
250 */
251 savech = *p2;
252 p1[1] = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +0000253 pr->fmt = xstrdup(fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000254 *p2 = savech;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000255 //Too early! xrealloc can move pr->fmt!
256 //pr->cchar = pr->fmt + (p1 - fmtp);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000257
258 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000259 * Skip subsequent text and up to the next % sign and tack the
260 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000261 * we lose the " is a HEX number" part of fmt.
262 */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000263 for (p3 = p2; *p3 && *p3 != '%'; p3++)
264 continue;
265 if (p3 > p2) {
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000266 savech = *p3;
267 *p3 = '\0';
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000268 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000269 strcat(pr->fmt, p2);
270 *p3 = savech;
271 p2 = p3;
272 }
273
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000274 pr->cchar = pr->fmt + (p1 - fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000275 fmtp = p2;
276
277 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000278 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000279 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000280 }
281 }
282 /*
283 * if format unit byte count not specified, figure it out
284 * so can adjust rep count later.
285 */
286 if (!fu->bcnt)
287 for (pr = fu->nextpr; pr; pr = pr->nextpr)
288 fu->bcnt += pr->bcnt;
289 }
290 /*
291 * if the format string interprets any data at all, and it's
Denis Vlasenko55f79122008-07-16 11:00:16 +0000292 * not the same as the blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000293 * interprets any data at all, and has no iteration count,
294 * repeat it as necessary.
295 *
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200296 * if rep count is greater than 1, no trailing whitespace
Glenn L McGrath60281112001-11-02 11:39:46 +0000297 * gets output from the last iteration of the format unit.
298 */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000299 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200300 if (!fu->nextfu
301 && fs->bcnt < dumper->blocksize
302 && !(fu->flags & F_SETREP)
303 && fu->bcnt
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000304 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000305 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000306 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200307 if (fu->reps > 1 && fu->nextpr) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000308 for (pr = fu->nextpr;; pr = pr->nextpr)
309 if (!pr->nextpr)
310 break;
311 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
312 p2 = isspace(*p1) ? p1 : NULL;
313 if (p2)
314 pr->nospace = p2;
315 }
316 if (!fu->nextfu)
317 break;
318 }
319}
320
Denis Vlasenko55f79122008-07-16 11:00:16 +0000321static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
Glenn L McGrath60281112001-11-02 11:39:46 +0000322{
323 struct stat sbuf;
324
325 if (statok) {
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200326 xfstat(STDIN_FILENO, &sbuf, fname);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000327 if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
328 && dumper->pub.dump_skip >= sbuf.st_size
329 ) {
330 /* If bb_dump_size valid and pub.dump_skip >= size */
331 dumper->pub.dump_skip -= sbuf.st_size;
332 dumper->address += sbuf.st_size;
Glenn L McGrath60281112001-11-02 11:39:46 +0000333 return;
334 }
335 }
maxwen27116ba2015-08-14 21:41:28 +0200336 if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000337 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000338 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000339 dumper->address += dumper->pub.dump_skip;
340 dumper->savaddress = dumper->address;
341 dumper->pub.dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000342}
343
Denis Vlasenko55f79122008-07-16 11:00:16 +0000344static NOINLINE int next(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000345{
Glenn L McGrath60281112001-11-02 11:39:46 +0000346 int statok;
347
Glenn L McGrath60281112001-11-02 11:39:46 +0000348 for (;;) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000349 if (*dumper->argv) {
Denys Vlasenko6aca76d2010-02-06 13:53:21 +0100350 dumper->next__done = statok = 1;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000351 if (!(freopen(*dumper->argv, "r", stdin))) {
352 bb_simple_perror_msg(*dumper->argv);
353 dumper->exitval = 1;
354 ++dumper->argv;
Glenn L McGrath60281112001-11-02 11:39:46 +0000355 continue;
356 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000357 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000358 if (dumper->next__done)
Denys Vlasenko6aca76d2010-02-06 13:53:21 +0100359 return 0; /* no next file */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000360 dumper->next__done = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000361 statok = 0;
362 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000363 if (dumper->pub.dump_skip)
364 do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
365 if (*dumper->argv)
366 ++dumper->argv;
367 if (!dumper->pub.dump_skip)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000368 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000369 }
370 /* NOTREACHED */
371}
372
Denis Vlasenko55f79122008-07-16 11:00:16 +0000373static unsigned char *get(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000374{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000375 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000376 int need, nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000377 int blocksize = dumper->blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000378
Denis Vlasenko55f79122008-07-16 11:00:16 +0000379 if (!dumper->get__curp) {
380 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
381 dumper->get__curp = xmalloc(blocksize);
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000382 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
Glenn L McGrath60281112001-11-02 11:39:46 +0000383 } else {
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000384 unsigned char *tmp = dumper->get__curp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000385 dumper->get__curp = dumper->get__savp;
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000386 dumper->get__savp = tmp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000387 dumper->savaddress += blocksize;
388 dumper->address = dumper->savaddress;
Glenn L McGrath60281112001-11-02 11:39:46 +0000389 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000390 need = blocksize;
391 nread = 0;
392 while (1) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000393 /*
394 * if read the right number of bytes, or at EOF for one file,
395 * and no other files are available, zero-pad the rest of the
396 * block and set the end flag.
397 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000398 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
399 if (need == blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000400 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000401 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000402 if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
403 if (dumper->pub.dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000404 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000405 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000406 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000407 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000408 memset(dumper->get__curp + nread, 0, need);
409 dumper->eaddress = dumper->address + nread;
410 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000411 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000412 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
413 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
Glenn L McGrath60281112001-11-02 11:39:46 +0000414 if (!n) {
415 if (ferror(stdin)) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000416 bb_simple_perror_msg(dumper->argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000417 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000418 dumper->get__ateof = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000419 continue;
420 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000421 dumper->get__ateof = 0;
422 if (dumper->pub.dump_length != -1) {
423 dumper->pub.dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000424 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000425 need -= n;
426 if (!need) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000427 if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
428 || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000429 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000430 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
431 dumper->pub.dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000432 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000433 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000434 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000435 if (dumper->pub.dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000436 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000437 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000438 dumper->pub.dump_vflag = DUP;
439 dumper->savaddress += blocksize;
440 dumper->address = dumper->savaddress;
441 need = blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000442 nread = 0;
443 } else {
444 nread += n;
445 }
446 }
447}
448
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000449static void bpad(PR *pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000450{
Rob Landleya6e60372006-06-28 14:36:50 +0000451 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000452
453 /*
454 * remove all conversion flags; '-' is the only one valid
455 * with %s, and it's not useful here.
456 */
457 pr->flags = F_BPAD;
458 *pr->cchar = 's';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000459 for (p1 = pr->fmt; *p1 != '%'; ++p1)
460 continue;
Rob Landleya6e60372006-06-28 14:36:50 +0000461 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000462 if (pr->nospace)
463 pr->nospace--;
464 while ((*p2++ = *p1++) != 0)
465 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000466}
467
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000468static const char conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000469 "\0\\0\0"
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200470 "\007\\a\0" /* \a */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000471 "\b\\b\0"
472 "\f\\b\0"
473 "\n\\n\0"
474 "\r\\r\0"
475 "\t\\t\0"
476 "\v\\v\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000477 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000478
Manuel Novoa III cad53642003-03-19 09:13:01 +0000479
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000480static void conv_c(PR *pr, unsigned char *p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000481{
482 const char *str = conv_str;
483 char buf[10];
484
485 do {
486 if (*p == *str) {
487 ++str;
488 goto strpr;
489 }
490 str += 4;
491 } while (*str);
492
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100493 if (isprint_asciionly(*p)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000494 *pr->cchar = 'c';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000495 printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000496 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000497 sprintf(buf, "%03o", (int) *p);
498 str = buf;
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100499 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000500 *pr->cchar = 's';
501 printf(pr->fmt, str);
502 }
503}
504
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000505static void conv_u(PR *pr, unsigned char *p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000506{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000507 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000508 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
509 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
510 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
511 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000512
513 /* od used nl, not lf */
514 if (*p <= 0x1f) {
515 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000516 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000517 } else if (*p == 0x7f) {
518 *pr->cchar = 's';
519 printf(pr->fmt, "del");
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100520 } else if (*p < 0x7f) { /* isprint() */
Glenn L McGrath60281112001-11-02 11:39:46 +0000521 *pr->cchar = 'c';
522 printf(pr->fmt, *p);
523 } else {
524 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000525 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000526 }
527}
528
Denis Vlasenko55f79122008-07-16 11:00:16 +0000529static void display(priv_dumper_t* dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000530{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000531 FS *fs;
532 FU *fu;
533 PR *pr;
534 int cnt;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000535 unsigned char *bp, *savebp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000536 off_t saveaddress;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000537 unsigned char savech = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000538
Denis Vlasenko55f79122008-07-16 11:00:16 +0000539 while ((bp = get(dumper)) != NULL) {
540 fs = dumper->pub.fshead;
541 savebp = bp;
542 saveaddress = dumper->address;
543 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000544 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000545 if (fu->flags & F_IGNORE) {
546 break;
547 }
548 for (cnt = fu->reps; cnt; --cnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000549 for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
550 bp += pr->bcnt, pr = pr->nextpr) {
551 if (dumper->eaddress && dumper->address >= dumper->eaddress
552 && !(pr->flags & (F_TEXT | F_BPAD))
553 ) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000554 bpad(pr);
555 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000556 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000557 savech = *pr->nospace;
558 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000559 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000560/* PRINT; */
561 switch (pr->flags) {
562 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000563 printf(pr->fmt, (unsigned) dumper->address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000564 break;
565 case F_BPAD:
566 printf(pr->fmt, "");
567 break;
568 case F_C:
569 conv_c(pr, bp);
570 break;
571 case F_CHAR:
572 printf(pr->fmt, *bp);
573 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000574 case F_DBL: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000575 double dval;
576 float fval;
577
578 switch (pr->bcnt) {
579 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000580 memcpy(&fval, bp, sizeof(fval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000581 printf(pr->fmt, fval);
582 break;
583 case 8:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000584 memcpy(&dval, bp, sizeof(dval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000585 printf(pr->fmt, dval);
586 break;
587 }
588 break;
589 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000590 case F_INT: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000591 int ival;
592 short sval;
593
594 switch (pr->bcnt) {
595 case 1:
596 printf(pr->fmt, (int) *bp);
597 break;
598 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000599 memcpy(&sval, bp, sizeof(sval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000600 printf(pr->fmt, (int) sval);
601 break;
602 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000603 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000604 printf(pr->fmt, ival);
605 break;
606 }
607 break;
608 }
609 case F_P:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100610 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000611 break;
612 case F_STR:
613 printf(pr->fmt, (char *) bp);
614 break;
615 case F_TEXT:
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200616 printf("%s", pr->fmt);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000617 break;
618 case F_U:
619 conv_u(pr, bp);
620 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000621 case F_UINT: {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000622 unsigned ival;
Eric Andersen0f56de62004-01-30 22:52:27 +0000623 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000624
625 switch (pr->bcnt) {
626 case 1:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000627 printf(pr->fmt, (unsigned) *bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000628 break;
629 case 2:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000630 memcpy(&sval, bp, sizeof(sval));
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000631 printf(pr->fmt, (unsigned) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000632 break;
633 case 4:
Denis Vlasenko3eb44662008-11-16 22:21:23 +0000634 memcpy(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000635 printf(pr->fmt, ival);
636 break;
637 }
638 break;
639 }
640 }
641 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000642 *pr->nospace = savech;
643 }
644 }
645 }
646 }
647 }
648 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000649 if (dumper->endfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000650 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +0000651 * if eaddress not set, error or file size was multiple
652 * of blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000653 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000654 if (!dumper->eaddress) {
655 if (!dumper->address) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000656 return;
657 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000658 dumper->eaddress = dumper->address;
Glenn L McGrath60281112001-11-02 11:39:46 +0000659 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000660 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000661 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000662 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000663 printf(pr->fmt, (unsigned) dumper->eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000664 break;
665 case F_TEXT:
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200666 printf("%s", pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000667 break;
668 }
669 }
670 }
671}
672
Denis Vlasenko55f79122008-07-16 11:00:16 +0000673#define dumper ((priv_dumper_t*)pub_dumper)
674int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000675{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000676 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000677 int blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000678
Manuel Novoa III cad53642003-03-19 09:13:01 +0000679 /* figure out the data block bb_dump_size */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000680 blocksize = 0;
681 tfs = dumper->pub.fshead;
682 while (tfs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000683 tfs->bcnt = bb_dump_size(tfs);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000684 if (blocksize < tfs->bcnt) {
685 blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000686 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000687 tfs = tfs->nextfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000688 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000689 dumper->blocksize = blocksize;
690
Glenn L McGrath60281112001-11-02 11:39:46 +0000691 /* rewrite the rules, do syntax checking */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000692 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
693 rewrite(dumper, tfs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000694 }
695
Denis Vlasenko55f79122008-07-16 11:00:16 +0000696 dumper->argv = argv;
697 display(dumper);
Glenn L McGrath60281112001-11-02 11:39:46 +0000698
Denis Vlasenko55f79122008-07-16 11:00:16 +0000699 return dumper->exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000700}
701
Denis Vlasenko55f79122008-07-16 11:00:16 +0000702void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000703{
Rob Landleyea224be2006-06-18 20:20:07 +0000704 const char *p;
705 char *p1;
706 char *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000707 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000708 FU *tfu, **nextfupp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000709 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000710
711 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000712 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000713 if (!dumper->pub.fshead) {
714 dumper->pub.fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000715 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000716 FS *fslast = dumper->pub.fshead;
717 while (fslast->nextfs)
718 fslast = fslast->nextfs;
719 fslast->nextfs = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000720 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000721 nextfupp = &tfs->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000722
723 /* take the format string and break it up into format units */
Denis Vlasenko15f2fdb2008-08-23 23:15:48 +0000724 p = fmt;
725 for (;;) {
Rob Landleyea224be2006-06-18 20:20:07 +0000726 p = skip_whitespace(p);
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200727 if (*p == '\0') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000728 break;
729 }
730
731 /* allocate a new format unit and link it in */
732 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000733 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000734 tfu = xzalloc(sizeof(FU));
Denis Vlasenko55f79122008-07-16 11:00:16 +0000735 *nextfupp = tfu;
736 nextfupp = &tfu->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000737 tfu->reps = 1;
738
739 /* if leading digit, repetition count */
740 if (isdigit(*p)) {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000741 for (savep = p; isdigit(*p); ++p)
742 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000743 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000744 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000745 }
746 /* may overwrite either white space or slash */
747 tfu->reps = atoi(savep);
748 tfu->flags = F_SETREP;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000749 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000750 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000751 }
752
Denis Vlasenko55f79122008-07-16 11:00:16 +0000753 /* skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000754 if (*p == '/') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200755 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000756 }
757
758 /* byte count */
759 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000760// TODO: use bb_strtou
761 savep = p;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000762 while (isdigit(*++p))
763 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000764 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000765 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000766 }
767 tfu->bcnt = atoi(savep);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000768 /* skip trailing white space */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200769 p = skip_whitespace(p + 1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000770 }
771
772 /* format */
773 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000774 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000775 }
776 for (savep = ++p; *p != '"';) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200777 if (*p++ == '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000778 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000779 }
780 }
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000781 tfu->fmt = xstrndup(savep, p - savep);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000782/* escape(tfu->fmt); */
Glenn L McGrath60281112001-11-02 11:39:46 +0000783
784 p1 = tfu->fmt;
785
786 /* alphabetic escape sequences have to be done in place */
787 for (p2 = p1;; ++p1, ++p2) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200788 if (*p1 == '\0') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000789 *p2 = *p1;
790 break;
791 }
792 if (*p1 == '\\') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000793 const char *cs = conv_str + 4;
794 ++p1;
795 *p2 = *p1;
796 do {
797 if (*p1 == cs[2]) {
798 *p2 = cs[0];
799 break;
800 }
801 cs += 4;
802 } while (*cs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000803 }
804 }
805
806 p++;
807 }
808}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000809
Glenn L McGrath60281112001-11-02 11:39:46 +0000810/*
811 * Copyright (c) 1989 The Regents of the University of California.
812 * All rights reserved.
813 *
814 * Redistribution and use in source and binary forms, with or without
815 * modification, are permitted provided that the following conditions
816 * are met:
817 * 1. Redistributions of source code must retain the above copyright
818 * notice, this list of conditions and the following disclaimer.
819 * 2. Redistributions in binary form must reproduce the above copyright
820 * notice, this list of conditions and the following disclaimer in the
821 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000822 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000823 * may be used to endorse or promote products derived from this software
824 * without specific prior written permission.
825 *
826 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
827 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
828 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
829 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
830 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
831 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
832 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
833 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
834 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
835 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
836 * SUCH DAMAGE.
837 */