blob: 74c5e1691753f965a7d543d4f029020aa812a44d [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
7 * The Regents of the University of California. All rights reserved.
8 *
Bernhard Reutner-Fischer30385572006-01-31 17:57:48 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 while (strchr(index_str + 1, *++fmt));
Glenn L McGrath60281112001-11-02 11:39:46 +000075 if (*fmt == '.' && isdigit(*++fmt)) {
76 prec = atoi(fmt);
Denis Vlasenko55f79122008-07-16 11:00:16 +000077 while (isdigit(*++fmt))
78 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +000079 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000080 p = strchr(size_conv_str + 12, *fmt);
81 if (!p) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 if (*fmt == 's') {
83 bcnt += prec;
84 } else if (*fmt == '_') {
85 ++fmt;
86 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
87 bcnt += 1;
88 }
Glenn L McGrath60281112001-11-02 11:39:46 +000089 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 } else {
91 bcnt += size_conv_str[p - (size_conv_str + 12)];
Glenn L McGrath60281112001-11-02 11:39:46 +000092 }
93 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 cur_size += bcnt * fu->reps;
Glenn L McGrath60281112001-11-02 11:39:46 +000095 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000096 return cur_size;
Glenn L McGrath60281112001-11-02 11:39:46 +000097}
98
Denis Vlasenko55f79122008-07-16 11:00:16 +000099static void rewrite(priv_dumper_t *dumper, FS *fs)
Glenn L McGrath60281112001-11-02 11:39:46 +0000100{
101 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
Rob Landleyea224be2006-06-18 20:20:07 +0000102 PR *pr, **nextpr = NULL;
103 FU *fu;
104 char *p1, *p2, *p3;
Glenn L McGrath60281112001-11-02 11:39:46 +0000105 char savech, *fmtp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 const char *byte_count_str;
Glenn L McGrath60281112001-11-02 11:39:46 +0000107 int nconv, prec = 0;
108
109 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
110 /*
111 * break each format unit into print units; each
112 * conversion character gets its own.
113 */
114 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
115 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000116 /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
Rob Landleyea224be2006-06-18 20:20:07 +0000117 pr = xzalloc(sizeof(PR));
Glenn L McGrath60281112001-11-02 11:39:46 +0000118 if (!fu->nextpr)
119 fu->nextpr = pr;
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000120 /* ignore nextpr -- its unused inside the loop and is
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +0000121 * uninitialized 1st time through.
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000122 */
Glenn L McGrath60281112001-11-02 11:39:46 +0000123
Denis Vlasenko55f79122008-07-16 11:00:16 +0000124 /* skip preceding text and up to the next % sign */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000125 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1)
126 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000127
128 /* only text in the string */
129 if (!*p1) {
130 pr->fmt = fmtp;
131 pr->flags = F_TEXT;
132 break;
133 }
134
135 /*
136 * get precision for %s -- if have a byte count, don't
137 * need it.
138 */
139 if (fu->bcnt) {
140 sokay = USEBCNT;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000141 /* skip to conversion character */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000142 for (++p1; strchr(index_str, *p1); ++p1)
143 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000144 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000145 /* skip any special chars, field width */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000146 while (strchr(index_str + 1, *++p1))
147 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000148 if (*p1 == '.' && isdigit(*++p1)) {
149 sokay = USEPREC;
150 prec = atoi(p1);
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000151 while (isdigit(*++p1))
152 continue;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000153 } else
Glenn L McGrath60281112001-11-02 11:39:46 +0000154 sokay = NOTOKAY;
155 }
156
Denis Vlasenko55f79122008-07-16 11:00:16 +0000157 p2 = p1 + 1; /* set end pointer */
Glenn L McGrath60281112001-11-02 11:39:46 +0000158
159 /*
160 * figure out the byte count for each conversion;
161 * rewrite the format as necessary, set up blank-
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 * pbb_dump_adding for end of data.
Glenn L McGrath60281112001-11-02 11:39:46 +0000163 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 if (*p1 == 'c') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000165 pr->flags = F_CHAR;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000166 DO_BYTE_COUNT_1:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 byte_count_str = "\001";
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000168 DO_BYTE_COUNT:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 if (fu->bcnt) {
170 do {
171 if (fu->bcnt == *byte_count_str) {
172 break;
173 }
174 } while (*++byte_count_str);
Glenn L McGrath60281112001-11-02 11:39:46 +0000175 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 /* Unlike the original, output the remainder of the format string. */
177 if (!*byte_count_str) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000178 bb_error_msg_and_die("bad byte count for conversion character %s", p1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 }
180 pr->bcnt = *byte_count_str;
181 } else if (*p1 == 'l') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000182 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 ++p1;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000184 DO_INT_CONV:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 {
186 const char *e;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000187 e = strchr(lcc, *p1);
188 if (!e) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 goto DO_BAD_CONV_CHAR;
190 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000191 pr->flags = F_INT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192 if (e > lcc + 1) {
193 pr->flags = F_UINT;
194 }
195 byte_count_str = "\004\002\001";
196 goto DO_BYTE_COUNT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000197 }
198 /* NOTREACHED */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 } else if (strchr(lcc, *p1)) {
200 goto DO_INT_CONV;
201 } else if (strchr("eEfgG", *p1)) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000202 pr->flags = F_DBL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 byte_count_str = "\010\004";
204 goto DO_BYTE_COUNT;
205 } else if (*p1 == 's') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000206 pr->flags = F_STR;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 if (sokay == USEBCNT) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000208 pr->bcnt = fu->bcnt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 } else if (sokay == USEPREC) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000210 pr->bcnt = prec;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 } else { /* NOTOKAY */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000212 bb_error_msg_and_die("%%s requires a precision or a byte count");
Glenn L McGrath60281112001-11-02 11:39:46 +0000213 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000214 } else if (*p1 == '_') {
Glenn L McGrath60281112001-11-02 11:39:46 +0000215 ++p2;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000216 switch (p1[1]) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000217 case 'A':
Denis Vlasenko55f79122008-07-16 11:00:16 +0000218 dumper->endfu = fu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000219 fu->flags |= F_IGNORE;
220 /* FALLTHROUGH */
221 case 'a':
222 pr->flags = F_ADDRESS;
223 ++p2;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
225 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000226 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 *p1 = p1[2];
Glenn L McGrath60281112001-11-02 11:39:46 +0000228 break;
229 case 'c':
230 pr->flags = F_C;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000231 /* *p1 = 'c'; set in conv_c */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000233 case 'p':
234 pr->flags = F_P;
235 *p1 = 'c';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000237 case 'u':
238 pr->flags = F_U;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000239 /* *p1 = 'c'; set in conv_u */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 goto DO_BYTE_COUNT_1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000241 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 goto DO_BAD_CONV_CHAR;
Glenn L McGrath60281112001-11-02 11:39:46 +0000243 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 } else {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000245 DO_BAD_CONV_CHAR:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000246 bb_error_msg_and_die("bad conversion character %%%s", p1);
Glenn L McGrath60281112001-11-02 11:39:46 +0000247 }
248
249 /*
250 * copy to PR format string, set conversion character
251 * pointer, update original.
252 */
253 savech = *p2;
254 p1[1] = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +0000255 pr->fmt = xstrdup(fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000256 *p2 = savech;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000257 //Too early! xrealloc can move pr->fmt!
258 //pr->cchar = pr->fmt + (p1 - fmtp);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000259
260 /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000261 * Skip subsequent text and up to the next % sign and tack the
262 * additional text onto fmt: eg. if fmt is "%x is a HEX number",
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000263 * we lose the " is a HEX number" part of fmt.
264 */
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000265 for (p3 = p2; *p3 && *p3 != '%'; p3++)
266 continue;
267 if (p3 > p2) {
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000268 savech = *p3;
269 *p3 = '\0';
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000270 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
Eric Andersen4a11e0f2003-04-19 23:18:35 +0000271 strcat(pr->fmt, p2);
272 *p3 = savech;
273 p2 = p3;
274 }
275
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000276 pr->cchar = pr->fmt + (p1 - fmtp);
Glenn L McGrath60281112001-11-02 11:39:46 +0000277 fmtp = p2;
278
279 /* only one conversion character if byte count */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000280 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000281 bb_error_msg_and_die("byte count with multiple conversion characters");
Glenn L McGrath60281112001-11-02 11:39:46 +0000282 }
283 }
284 /*
285 * if format unit byte count not specified, figure it out
286 * so can adjust rep count later.
287 */
288 if (!fu->bcnt)
289 for (pr = fu->nextpr; pr; pr = pr->nextpr)
290 fu->bcnt += pr->bcnt;
291 }
292 /*
293 * if the format string interprets any data at all, and it's
Denis Vlasenko55f79122008-07-16 11:00:16 +0000294 * not the same as the blocksize, and its last format unit
Glenn L McGrath60281112001-11-02 11:39:46 +0000295 * interprets any data at all, and has no iteration count,
296 * repeat it as necessary.
297 *
298 * if, rep count is greater than 1, no trailing whitespace
299 * gets output from the last iteration of the format unit.
300 */
301 for (fu = fs->nextfu;; fu = fu->nextfu) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000302 if (!fu->nextfu && fs->bcnt < dumper->blocksize
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000303 && !(fu->flags & F_SETREP) && fu->bcnt
304 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000305 fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000306 }
Glenn L McGrath60281112001-11-02 11:39:46 +0000307 if (fu->reps > 1) {
308 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) {
Eric Andersen70060d22004-03-27 10:02:48 +0000326 if (fstat(STDIN_FILENO, &sbuf)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000327 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000328 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000329 if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
330 && dumper->pub.dump_skip >= sbuf.st_size
331 ) {
332 /* If bb_dump_size valid and pub.dump_skip >= size */
333 dumper->pub.dump_skip -= sbuf.st_size;
334 dumper->address += sbuf.st_size;
Glenn L McGrath60281112001-11-02 11:39:46 +0000335 return;
336 }
337 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000338 if (fseek(stdin, dumper->pub.dump_skip, SEEK_SET)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000339 bb_simple_perror_msg_and_die(fname);
Glenn L McGrath60281112001-11-02 11:39:46 +0000340 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000341 dumper->address += dumper->pub.dump_skip;
342 dumper->savaddress = dumper->address;
343 dumper->pub.dump_skip = 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000344}
345
Denis Vlasenko55f79122008-07-16 11:00:16 +0000346static NOINLINE int next(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000347{
Glenn L McGrath60281112001-11-02 11:39:46 +0000348 int statok;
349
Glenn L McGrath60281112001-11-02 11:39:46 +0000350 for (;;) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000351 if (*dumper->argv) {
352 if (!(freopen(*dumper->argv, "r", stdin))) {
353 bb_simple_perror_msg(*dumper->argv);
354 dumper->exitval = 1;
355 ++dumper->argv;
Glenn L McGrath60281112001-11-02 11:39:46 +0000356 continue;
357 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000358 dumper->next__done = statok = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000359 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000360 if (dumper->next__done)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000361 return 0;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000362 dumper->next__done = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000363 statok = 0;
364 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000365 if (dumper->pub.dump_skip)
366 do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
367 if (*dumper->argv)
368 ++dumper->argv;
369 if (!dumper->pub.dump_skip)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000370 return 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000371 }
372 /* NOTREACHED */
373}
374
Denis Vlasenko55f79122008-07-16 11:00:16 +0000375static unsigned char *get(priv_dumper_t *dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000376{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000377 int n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000378 int need, nread;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000379 int blocksize = dumper->blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000380
Denis Vlasenko55f79122008-07-16 11:00:16 +0000381 if (!dumper->get__curp) {
382 dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
383 dumper->get__curp = xmalloc(blocksize);
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000384 dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
Glenn L McGrath60281112001-11-02 11:39:46 +0000385 } else {
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000386 unsigned char *tmp = dumper->get__curp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000387 dumper->get__curp = dumper->get__savp;
Denis Vlasenko2f86d132008-07-29 00:00:14 +0000388 dumper->get__savp = tmp;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000389 dumper->savaddress += blocksize;
390 dumper->address = dumper->savaddress;
Glenn L McGrath60281112001-11-02 11:39:46 +0000391 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000392 need = blocksize;
393 nread = 0;
394 while (1) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000395 /*
396 * if read the right number of bytes, or at EOF for one file,
397 * and no other files are available, zero-pad the rest of the
398 * block and set the end flag.
399 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000400 if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
401 if (need == blocksize) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000402 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000403 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000404 if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
405 if (dumper->pub.dump_vflag != DUP) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000406 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000407 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000408 return NULL;
Glenn L McGrath60281112001-11-02 11:39:46 +0000409 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000410 memset(dumper->get__curp + nread, 0, need);
411 dumper->eaddress = dumper->address + nread;
412 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000413 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000414 n = fread(dumper->get__curp + nread, sizeof(unsigned char),
415 dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
Glenn L McGrath60281112001-11-02 11:39:46 +0000416 if (!n) {
417 if (ferror(stdin)) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000418 bb_simple_perror_msg(dumper->argv[-1]);
Glenn L McGrath60281112001-11-02 11:39:46 +0000419 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000420 dumper->get__ateof = 1;
Glenn L McGrath60281112001-11-02 11:39:46 +0000421 continue;
422 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000423 dumper->get__ateof = 0;
424 if (dumper->pub.dump_length != -1) {
425 dumper->pub.dump_length -= n;
Glenn L McGrath60281112001-11-02 11:39:46 +0000426 }
Denis Vlasenko931de892007-06-21 12:43:45 +0000427 need -= n;
428 if (!need) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000429 if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
430 || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000431 ) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000432 if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
433 dumper->pub.dump_vflag = WAIT;
Glenn L McGrath60281112001-11-02 11:39:46 +0000434 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000435 return dumper->get__curp;
Glenn L McGrath60281112001-11-02 11:39:46 +0000436 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000437 if (dumper->pub.dump_vflag == WAIT) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000438 puts("*");
Glenn L McGrath60281112001-11-02 11:39:46 +0000439 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000440 dumper->pub.dump_vflag = DUP;
441 dumper->savaddress += blocksize;
442 dumper->address = dumper->savaddress;
443 need = blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000444 nread = 0;
445 } else {
446 nread += n;
447 }
448 }
449}
450
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000451static void bpad(PR *pr)
Glenn L McGrath60281112001-11-02 11:39:46 +0000452{
Rob Landleya6e60372006-06-28 14:36:50 +0000453 char *p1, *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000454
455 /*
456 * remove all conversion flags; '-' is the only one valid
457 * with %s, and it's not useful here.
458 */
459 pr->flags = F_BPAD;
460 *pr->cchar = 's';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000461 for (p1 = pr->fmt; *p1 != '%'; ++p1)
462 continue;
Rob Landleya6e60372006-06-28 14:36:50 +0000463 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000464 if (pr->nospace)
465 pr->nospace--;
466 while ((*p2++ = *p1++) != 0)
467 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000468}
469
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000470static const char conv_str[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000471 "\0\\0\0"
472 "\007\\a\0" /* \a */
473 "\b\\b\0"
474 "\f\\b\0"
475 "\n\\n\0"
476 "\r\\r\0"
477 "\t\\t\0"
478 "\v\\v\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000479 ;
Glenn L McGrath60281112001-11-02 11:39:46 +0000480
Manuel Novoa III cad53642003-03-19 09:13:01 +0000481
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000482static void conv_c(PR *pr, unsigned char *p)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000483{
484 const char *str = conv_str;
485 char buf[10];
486
487 do {
488 if (*p == *str) {
489 ++str;
490 goto strpr;
491 }
492 str += 4;
493 } while (*str);
494
Glenn L McGrath60281112001-11-02 11:39:46 +0000495 if (isprint(*p)) {
496 *pr->cchar = 'c';
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000497 printf(pr->fmt, *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000498 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000499 sprintf(buf, "%03o", (int) *p);
500 str = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000501 strpr:
Glenn L McGrath60281112001-11-02 11:39:46 +0000502 *pr->cchar = 's';
503 printf(pr->fmt, str);
504 }
505}
506
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000507static void conv_u(PR *pr, unsigned char *p)
Glenn L McGrath60281112001-11-02 11:39:46 +0000508{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000509 static const char list[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000510 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
511 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
512 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
513 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
Glenn L McGrath60281112001-11-02 11:39:46 +0000514
515 /* od used nl, not lf */
516 if (*p <= 0x1f) {
517 *pr->cchar = 's';
Glenn L McGratheeb06bf2004-07-23 01:35:41 +0000518 printf(pr->fmt, list + (4 * (int)*p));
Glenn L McGrath60281112001-11-02 11:39:46 +0000519 } else if (*p == 0x7f) {
520 *pr->cchar = 's';
521 printf(pr->fmt, "del");
522 } else if (isprint(*p)) {
523 *pr->cchar = 'c';
524 printf(pr->fmt, *p);
525 } else {
526 *pr->cchar = 'x';
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000527 printf(pr->fmt, (int) *p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000528 }
529}
530
Denis Vlasenko55f79122008-07-16 11:00:16 +0000531static void display(priv_dumper_t* dumper)
Glenn L McGrath60281112001-11-02 11:39:46 +0000532{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000533 FS *fs;
534 FU *fu;
535 PR *pr;
536 int cnt;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000537 unsigned char *bp, *savebp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000538 off_t saveaddress;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000539 unsigned char savech = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000540
Denis Vlasenko55f79122008-07-16 11:00:16 +0000541 while ((bp = get(dumper)) != NULL) {
542 fs = dumper->pub.fshead;
543 savebp = bp;
544 saveaddress = dumper->address;
545 for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000546 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000547 if (fu->flags & F_IGNORE) {
548 break;
549 }
550 for (cnt = fu->reps; cnt; --cnt) {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000551 for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
552 bp += pr->bcnt, pr = pr->nextpr) {
553 if (dumper->eaddress && dumper->address >= dumper->eaddress
554 && !(pr->flags & (F_TEXT | F_BPAD))
555 ) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000556 bpad(pr);
557 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000558 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000559 savech = *pr->nospace;
560 *pr->nospace = '\0';
Glenn L McGrath60281112001-11-02 11:39:46 +0000561 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000562/* PRINT; */
563 switch (pr->flags) {
564 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000565 printf(pr->fmt, (unsigned) dumper->address);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000566 break;
567 case F_BPAD:
568 printf(pr->fmt, "");
569 break;
570 case F_C:
571 conv_c(pr, bp);
572 break;
573 case F_CHAR:
574 printf(pr->fmt, *bp);
575 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000576 case F_DBL: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000577 double dval;
578 float fval;
579
580 switch (pr->bcnt) {
581 case 4:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000582 memmove(&fval, bp, sizeof(fval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000583 printf(pr->fmt, fval);
584 break;
585 case 8:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000586 memmove(&dval, bp, sizeof(dval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000587 printf(pr->fmt, dval);
588 break;
589 }
590 break;
591 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000592 case F_INT: {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000593 int ival;
594 short sval;
595
596 switch (pr->bcnt) {
597 case 1:
598 printf(pr->fmt, (int) *bp);
599 break;
600 case 2:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000601 memmove(&sval, bp, sizeof(sval));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000602 printf(pr->fmt, (int) sval);
603 break;
604 case 4:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000605 memmove(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000606 printf(pr->fmt, ival);
607 break;
608 }
609 break;
610 }
611 case F_P:
612 printf(pr->fmt, isprint(*bp) ? *bp : '.');
613 break;
614 case F_STR:
615 printf(pr->fmt, (char *) bp);
616 break;
617 case F_TEXT:
618 printf(pr->fmt);
619 break;
620 case F_U:
621 conv_u(pr, bp);
622 break;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000623 case F_UINT: {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000624 unsigned ival;
Eric Andersen0f56de62004-01-30 22:52:27 +0000625 unsigned short sval;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000626
627 switch (pr->bcnt) {
628 case 1:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000629 printf(pr->fmt, (unsigned) *bp);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000630 break;
631 case 2:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000632 memmove(&sval, bp, sizeof(sval));
633 printf(pr->fmt, (unsigned) sval);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000634 break;
635 case 4:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000636 memmove(&ival, bp, sizeof(ival));
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000637 printf(pr->fmt, ival);
638 break;
639 }
640 break;
641 }
642 }
643 if (cnt == 1 && pr->nospace) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000644 *pr->nospace = savech;
645 }
646 }
647 }
648 }
649 }
650 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000651 if (dumper->endfu) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000652 /*
Denis Vlasenko55f79122008-07-16 11:00:16 +0000653 * if eaddress not set, error or file size was multiple
654 * of blocksize, and no partial block ever found.
Glenn L McGrath60281112001-11-02 11:39:46 +0000655 */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000656 if (!dumper->eaddress) {
657 if (!dumper->address) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000658 return;
659 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000660 dumper->eaddress = dumper->address;
Glenn L McGrath60281112001-11-02 11:39:46 +0000661 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000662 for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000663 switch (pr->flags) {
Glenn L McGrath60281112001-11-02 11:39:46 +0000664 case F_ADDRESS:
Denis Vlasenko55f79122008-07-16 11:00:16 +0000665 printf(pr->fmt, (unsigned) dumper->eaddress);
Glenn L McGrath60281112001-11-02 11:39:46 +0000666 break;
667 case F_TEXT:
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000668 printf(pr->fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000669 break;
670 }
671 }
672 }
673}
674
Denis Vlasenko55f79122008-07-16 11:00:16 +0000675#define dumper ((priv_dumper_t*)pub_dumper)
676int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +0000677{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000678 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000679 int blocksize;
Glenn L McGrath60281112001-11-02 11:39:46 +0000680
Manuel Novoa III cad53642003-03-19 09:13:01 +0000681 /* figure out the data block bb_dump_size */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000682 blocksize = 0;
683 tfs = dumper->pub.fshead;
684 while (tfs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000685 tfs->bcnt = bb_dump_size(tfs);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000686 if (blocksize < tfs->bcnt) {
687 blocksize = tfs->bcnt;
Glenn L McGrath60281112001-11-02 11:39:46 +0000688 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000689 tfs = tfs->nextfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000690 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000691 dumper->blocksize = blocksize;
692
Glenn L McGrath60281112001-11-02 11:39:46 +0000693 /* rewrite the rules, do syntax checking */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000694 for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
695 rewrite(dumper, tfs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000696 }
697
Denis Vlasenko55f79122008-07-16 11:00:16 +0000698 dumper->argv = argv;
699 display(dumper);
Glenn L McGrath60281112001-11-02 11:39:46 +0000700
Denis Vlasenko55f79122008-07-16 11:00:16 +0000701 return dumper->exitval;
Glenn L McGrath60281112001-11-02 11:39:46 +0000702}
703
Denis Vlasenko55f79122008-07-16 11:00:16 +0000704void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
Glenn L McGrath60281112001-11-02 11:39:46 +0000705{
Rob Landleyea224be2006-06-18 20:20:07 +0000706 const char *p;
707 char *p1;
708 char *p2;
Glenn L McGrath60281112001-11-02 11:39:46 +0000709 FS *tfs;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000710 FU *tfu, **nextfupp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000711 const char *savep;
Glenn L McGrath60281112001-11-02 11:39:46 +0000712
713 /* start new linked list of format units */
Rob Landleyea224be2006-06-18 20:20:07 +0000714 tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
Denis Vlasenko55f79122008-07-16 11:00:16 +0000715 if (!dumper->pub.fshead) {
716 dumper->pub.fshead = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000717 } else {
Denis Vlasenko55f79122008-07-16 11:00:16 +0000718 FS *fslast = dumper->pub.fshead;
719 while (fslast->nextfs)
720 fslast = fslast->nextfs;
721 fslast->nextfs = tfs;
Glenn L McGrath60281112001-11-02 11:39:46 +0000722 }
Denis Vlasenko55f79122008-07-16 11:00:16 +0000723 nextfupp = &tfs->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000724
725 /* take the format string and break it up into format units */
726 for (p = fmt;;) {
Rob Landleyea224be2006-06-18 20:20:07 +0000727 p = skip_whitespace(p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000728 if (!*p) {
729 break;
730 }
731
732 /* allocate a new format unit and link it in */
733 /* NOSTRICT */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000734 /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
Rob Landleyea224be2006-06-18 20:20:07 +0000735 tfu = xzalloc(sizeof(FU));
Denis Vlasenko55f79122008-07-16 11:00:16 +0000736 *nextfupp = tfu;
737 nextfupp = &tfu->nextfu;
Glenn L McGrath60281112001-11-02 11:39:46 +0000738 tfu->reps = 1;
739
740 /* if leading digit, repetition count */
741 if (isdigit(*p)) {
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000742 for (savep = p; isdigit(*p); ++p)
743 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000744 if (!isspace(*p) && *p != '/') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000745 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000746 }
747 /* may overwrite either white space or slash */
748 tfu->reps = atoi(savep);
749 tfu->flags = F_SETREP;
Denis Vlasenko55f79122008-07-16 11:00:16 +0000750 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000751 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000752 }
753
Denis Vlasenko55f79122008-07-16 11:00:16 +0000754 /* skip slash and trailing white space */
Glenn L McGrath60281112001-11-02 11:39:46 +0000755 if (*p == '/') {
Rob Landleyea224be2006-06-18 20:20:07 +0000756 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000757 }
758
759 /* byte count */
760 if (isdigit(*p)) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000761// TODO: use bb_strtou
762 savep = p;
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000763 while (isdigit(*++p))
764 continue;
Glenn L McGrath60281112001-11-02 11:39:46 +0000765 if (!isspace(*p)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000766 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000767 }
768 tfu->bcnt = atoi(savep);
Denis Vlasenko55f79122008-07-16 11:00:16 +0000769 /* skip trailing white space */
Rob Landleyea224be2006-06-18 20:20:07 +0000770 p = skip_whitespace(++p);
Glenn L McGrath60281112001-11-02 11:39:46 +0000771 }
772
773 /* format */
774 if (*p != '"') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000775 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000776 }
777 for (savep = ++p; *p != '"';) {
778 if (*p++ == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000779 bb_error_msg_and_die("bad format {%s}", fmt);
Glenn L McGrath60281112001-11-02 11:39:46 +0000780 }
781 }
Denis Vlasenkobd9874d2008-07-16 07:22:14 +0000782 tfu->fmt = xstrndup(savep, p - savep);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000783/* escape(tfu->fmt); */
Glenn L McGrath60281112001-11-02 11:39:46 +0000784
785 p1 = tfu->fmt;
786
787 /* alphabetic escape sequences have to be done in place */
788 for (p2 = p1;; ++p1, ++p2) {
789 if (!*p1) {
790 *p2 = *p1;
791 break;
792 }
793 if (*p1 == '\\') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000794 const char *cs = conv_str + 4;
795 ++p1;
796 *p2 = *p1;
797 do {
798 if (*p1 == cs[2]) {
799 *p2 = cs[0];
800 break;
801 }
802 cs += 4;
803 } while (*cs);
Glenn L McGrath60281112001-11-02 11:39:46 +0000804 }
805 }
806
807 p++;
808 }
809}
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000810
Glenn L McGrath60281112001-11-02 11:39:46 +0000811/*
812 * Copyright (c) 1989 The Regents of the University of California.
813 * All rights reserved.
814 *
815 * Redistribution and use in source and binary forms, with or without
816 * modification, are permitted provided that the following conditions
817 * are met:
818 * 1. Redistributions of source code must retain the above copyright
819 * notice, this list of conditions and the following disclaimer.
820 * 2. Redistributions in binary form must reproduce the above copyright
821 * notice, this list of conditions and the following disclaimer in the
822 * documentation and/or other materials provided with the distribution.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000823 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000824 * may be used to endorse or promote products derived from this software
825 * without specific prior written permission.
826 *
827 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
828 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
829 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
830 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
831 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
832 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
833 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
834 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
835 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
836 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
837 * SUCH DAMAGE.
838 */