"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 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 |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7 | * The Regents of the University of California. All rights reserved. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 10 | * |
| 11 | * Original copyright notice is retained at the end of this file. |
| 12 | */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 14 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 15 | |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 16 | #define F_IGNORE 0x01 /* %_A */ |
| 17 | #define F_SETREP 0x02 /* rep count set, not default */ |
| 18 | #define F_ADDRESS 0x001 /* print offset */ |
| 19 | #define F_BPAD 0x002 /* blank pad */ |
| 20 | #define F_C 0x004 /* %_c */ |
| 21 | #define F_CHAR 0x008 /* %c */ |
| 22 | #define F_DBL 0x010 /* %[EefGf] */ |
| 23 | #define F_INT 0x020 /* %[di] */ |
| 24 | #define F_P 0x040 /* %_p */ |
| 25 | #define F_STR 0x080 /* %s */ |
| 26 | #define F_U 0x100 /* %_u */ |
| 27 | #define F_UINT 0x200 /* %[ouXx] */ |
| 28 | #define F_TEXT 0x400 /* no conversions */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 30 | typedef struct priv_dumper_t { |
| 31 | dumper_t pub; |
| 32 | |
| 33 | char **argv; |
| 34 | FU *endfu; |
| 35 | off_t savaddress; /* saved address/offset in stream */ |
| 36 | off_t eaddress; /* end address */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 37 | int blocksize; |
| 38 | smallint exitval; /* final exit value */ |
| 39 | |
| 40 | /* former statics */ |
| 41 | smallint next__done; |
| 42 | smallint get__ateof; // = 1; |
| 43 | unsigned char *get__curp; |
| 44 | unsigned char *get__savp; |
| 45 | } priv_dumper_t; |
| 46 | |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 47 | static const char dot_flags_width_chars[] ALIGN1 = ".#-+ 0123456789"; |
| 48 | |
| 49 | static const char size_conv_str[] ALIGN1 = |
| 50 | "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG"; |
| 51 | |
| 52 | static const char int_convs[] ALIGN1 = "diouxX"; |
| 53 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 54 | dumper_t* FAST_FUNC alloc_dumper(void) |
| 55 | { |
| 56 | priv_dumper_t *dumper = xzalloc(sizeof(*dumper)); |
| 57 | dumper->pub.dump_length = -1; |
| 58 | dumper->pub.dump_vflag = FIRST; |
| 59 | dumper->get__ateof = 1; |
| 60 | return &dumper->pub; |
| 61 | } |
| 62 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 63 | static NOINLINE int bb_dump_size(FS *fs) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 64 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 65 | FU *fu; |
| 66 | int bcnt, cur_size; |
| 67 | char *fmt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 69 | int prec; |
| 70 | |
Denys Vlasenko | 5f7904b | 2017-07-14 16:03:43 +0200 | [diff] [blame] | 71 | /* figure out the data block size needed for each format unit */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 73 | if (fu->bcnt) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | cur_size += fu->bcnt * fu->reps; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 75 | continue; |
| 76 | } |
| 77 | for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) { |
| 78 | if (*fmt != '%') |
| 79 | continue; |
| 80 | /* |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 81 | * skip any special chars -- save precision in |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 82 | * case it's a %s format. |
| 83 | */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 84 | while (strchr(dot_flags_width_chars + 1, *++fmt)) |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 85 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 86 | if (*fmt == '.' && isdigit(*++fmt)) { |
| 87 | prec = atoi(fmt); |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 88 | while (isdigit(*++fmt)) |
| 89 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 90 | } |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 91 | p = strchr(size_conv_str + 12, *fmt); |
| 92 | if (!p) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | if (*fmt == 's') { |
| 94 | bcnt += prec; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 95 | } |
| 96 | if (*fmt == '_') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | ++fmt; |
| 98 | if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) { |
| 99 | bcnt += 1; |
| 100 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 101 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | } else { |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 103 | bcnt += p[-12]; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | cur_size += bcnt * fu->reps; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 107 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 108 | return cur_size; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 111 | static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 112 | { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 113 | FU *fu; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 114 | |
| 115 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 116 | PR *pr; |
| 117 | char *p1, *p2, *p3; |
| 118 | char *fmtp; |
| 119 | int nconv = 0; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 120 | /* |
| 121 | * break each format unit into print units; each |
| 122 | * conversion character gets its own. |
| 123 | */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 124 | for (fmtp = fu->fmt; *fmtp; ) { |
| 125 | unsigned len; |
| 126 | const char *prec; |
| 127 | const char *byte_count_str; |
| 128 | |
| 129 | /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL */ |
| 130 | pr = xzalloc(sizeof(*pr)); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 131 | if (!fu->nextpr) |
| 132 | fu->nextpr = pr; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 134 | /* skip preceding text and up to the next % sign */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 135 | p1 = strchr(fmtp, '%'); |
| 136 | if (!p1) { /* only text in the string */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 137 | pr->fmt = fmtp; |
| 138 | pr->flags = F_TEXT; |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * get precision for %s -- if have a byte count, don't |
| 144 | * need it. |
| 145 | */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 146 | prec = NULL; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 147 | if (fu->bcnt) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 148 | /* skip to conversion character */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 149 | while (strchr(dot_flags_width_chars, *++p1)) |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 150 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 151 | } else { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 152 | /* skip any special chars, field width */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 153 | while (strchr(dot_flags_width_chars + 1, *++p1)) |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 154 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 155 | if (*p1 == '.' && isdigit(*++p1)) { |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 156 | prec = p1; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 157 | while (isdigit(*++p1)) |
| 158 | continue; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 159 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 162 | p2 = p1 + 1; /* set end pointer */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * figure out the byte count for each conversion; |
| 166 | * rewrite the format as necessary, set up blank- |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 167 | * padding for end of data. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 168 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | if (*p1 == 'c') { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 170 | pr->flags = F_CHAR; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 171 | DO_BYTE_COUNT_1: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | byte_count_str = "\001"; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 173 | DO_BYTE_COUNT: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 174 | if (fu->bcnt) { |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 175 | for (;;) { |
| 176 | if (fu->bcnt == *byte_count_str) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 177 | break; |
Denys Vlasenko | 0f43647 | 2017-01-25 01:58:00 +0100 | [diff] [blame] | 178 | if (*++byte_count_str == 0) |
| 179 | bb_error_msg_and_die("bad byte count for conversion character %s", p1); |
| 180 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 181 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 182 | /* Unlike the original, output the remainder of the format string. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 183 | pr->bcnt = *byte_count_str; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 184 | } else |
| 185 | if (*p1 == 'l') { /* %ld etc */ |
| 186 | const char *e; |
| 187 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 188 | ++p2; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 189 | ++p1; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 190 | DO_INT_CONV: |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 191 | e = strchr(int_convs, *p1); /* "diouxX"? */ |
| 192 | if (!e) |
| 193 | goto DO_BAD_CONV_CHAR; |
| 194 | pr->flags = F_INT; |
| 195 | if (e > int_convs + 1) /* not d or i? */ |
| 196 | pr->flags = F_UINT; |
| 197 | byte_count_str = "\004\002\001"; |
| 198 | goto DO_BYTE_COUNT; |
| 199 | } else |
| 200 | if (strchr(int_convs, *p1)) { /* %d etc */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 201 | goto DO_INT_CONV; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 202 | } else |
| 203 | if (strchr("eEfgG", *p1)) { /* floating point */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 204 | pr->flags = F_DBL; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 205 | byte_count_str = "\010\004"; |
| 206 | goto DO_BYTE_COUNT; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 207 | } else |
| 208 | if (*p1 == 's') { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 209 | pr->flags = F_STR; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 210 | pr->bcnt = fu->bcnt; |
| 211 | if (fu->bcnt == 0) { |
| 212 | if (!prec) |
Denys Vlasenko | a0f8076 | 2021-06-17 23:23:26 +0200 | [diff] [blame] | 213 | bb_simple_error_msg_and_die("%s needs precision or byte count"); |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 214 | pr->bcnt = atoi(prec); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 215 | } |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 216 | } else |
| 217 | if (*p1 == '_') { |
| 218 | p2++; /* move past a in "%_a" */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 219 | switch (p1[1]) { |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 220 | case 'A': /* %_A[dox]: print address and the end */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 221 | dumper->endfu = fu; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 222 | fu->flags |= F_IGNORE; |
| 223 | /* FALLTHROUGH */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 224 | case 'a': /* %_a[dox]: current address */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 225 | pr->flags = F_ADDRESS; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 226 | p2++; /* move past x in "%_ax" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 227 | if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) { |
| 228 | goto DO_BAD_CONV_CHAR; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 229 | } |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 230 | *p1++ = 'l'; |
| 231 | *p1++ = 'l'; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 232 | break; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 233 | case 'c': /* %_c: chars, \ooo, \n \r \t etc */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 234 | pr->flags = F_C; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 235 | /* *p1 = 'c'; set in conv_c */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 236 | goto DO_BYTE_COUNT_1; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 237 | case 'p': /* %_p: chars, dots for nonprintable */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 238 | pr->flags = F_P; |
| 239 | *p1 = 'c'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 240 | goto DO_BYTE_COUNT_1; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 241 | case 'u': /* %_p: chars, 'nul', 'esc' etc for nonprintable */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 242 | pr->flags = F_U; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 243 | /* *p1 = 'c'; set in conv_u */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 244 | goto DO_BYTE_COUNT_1; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 245 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 246 | goto DO_BAD_CONV_CHAR; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 247 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 248 | } else { |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 249 | DO_BAD_CONV_CHAR: |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 250 | bb_error_msg_and_die("bad conversion character %%%s", p1); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * copy to PR format string, set conversion character |
| 255 | * pointer, update original. |
| 256 | */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 257 | len = (p1 - fmtp) + 1; |
| 258 | pr->fmt = xstrndup(fmtp, len); |
Eric Andersen | 4a11e0f | 2003-04-19 23:18:35 +0000 | [diff] [blame] | 259 | /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 260 | * Skip subsequent text and up to the next % sign and tack the |
| 261 | * additional text onto fmt: eg. if fmt is "%x is a HEX number", |
Eric Andersen | 4a11e0f | 2003-04-19 23:18:35 +0000 | [diff] [blame] | 262 | * we lose the " is a HEX number" part of fmt. |
| 263 | */ |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 264 | for (p3 = p2; *p3 && *p3 != '%'; p3++) |
| 265 | continue; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 266 | if ((p3 - p2) != 0) { |
| 267 | char *d; |
| 268 | pr->fmt = d = xrealloc(pr->fmt, len + (p3 - p2) + 1); |
| 269 | d += len; |
| 270 | do { |
| 271 | *d++ = *p2++; |
| 272 | } while (p2 != p3); |
| 273 | *d = '\0'; |
| 274 | /* now p2 = p3 */ |
Eric Andersen | 4a11e0f | 2003-04-19 23:18:35 +0000 | [diff] [blame] | 275 | } |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 276 | pr->cchar = pr->fmt + len - 1; /* must be after realloc! */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 277 | fmtp = p2; |
| 278 | |
| 279 | /* only one conversion character if byte count */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 280 | if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 281 | bb_simple_error_msg_and_die("byte count with multiple conversion characters"); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | /* |
| 285 | * if format unit byte count not specified, figure it out |
| 286 | * so can adjust rep count later. |
| 287 | */ |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 288 | if (fu->bcnt == 0) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 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 Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 294 | * not the same as the blocksize, and its last format unit |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 295 | * interprets any data at all, and has no iteration count, |
| 296 | * repeat it as necessary. |
| 297 | * |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 298 | * if rep count is greater than 1, no trailing whitespace |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 299 | * gets output from the last iteration of the format unit: |
| 300 | * 2/1 "%02x " prints "XX XX", not "XX XX " |
| 301 | * 2/1 "%02x\n" prints "XX\nXX", not "XX\nXX\n" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 302 | */ |
Denis Vlasenko | 15f2fdb | 2008-08-23 23:15:48 +0000 | [diff] [blame] | 303 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 304 | if (!fu->nextfu |
| 305 | && fs->bcnt < dumper->blocksize |
| 306 | && !(fu->flags & F_SETREP) |
| 307 | && fu->bcnt |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 308 | ) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 309 | fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 310 | } |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 311 | if (fu->reps > 1 && fu->nextpr) { |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 312 | PR *pr; |
| 313 | char *p1, *p2; |
| 314 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 315 | for (pr = fu->nextpr;; pr = pr->nextpr) |
| 316 | if (!pr->nextpr) |
| 317 | break; |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 318 | p2 = NULL; |
| 319 | for (p1 = pr->fmt; *p1; ++p1) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 320 | p2 = isspace(*p1) ? p1 : NULL; |
| 321 | if (p2) |
| 322 | pr->nospace = p2; |
| 323 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 327 | static void do_skip(priv_dumper_t *dumper, const char *fname) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 328 | { |
| 329 | struct stat sbuf; |
| 330 | |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 331 | xfstat(STDIN_FILENO, &sbuf, fname); |
| 332 | if (S_ISREG(sbuf.st_mode) |
| 333 | && dumper->pub.dump_skip >= sbuf.st_size |
| 334 | ) { |
| 335 | /* If st_size is valid and pub.dump_skip >= st_size */ |
| 336 | dumper->pub.dump_skip -= sbuf.st_size; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 337 | dumper->pub.address += sbuf.st_size; |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 338 | return; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 339 | } |
Denys Vlasenko | ef6747e | 2013-03-27 15:15:33 +0100 | [diff] [blame] | 340 | if (fseeko(stdin, dumper->pub.dump_skip, SEEK_SET)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 341 | bb_simple_perror_msg_and_die(fname); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 342 | } |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 343 | dumper->pub.address += dumper->pub.dump_skip; |
| 344 | dumper->savaddress = dumper->pub.address; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 345 | dumper->pub.dump_skip = 0; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 348 | static NOINLINE int next(priv_dumper_t *dumper) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 349 | { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 350 | for (;;) { |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 351 | const char *fname = *dumper->argv; |
Denys Vlasenko | 5f7904b | 2017-07-14 16:03:43 +0200 | [diff] [blame] | 352 | |
| 353 | if (fname) { |
| 354 | dumper->argv++; |
Denys Vlasenko | 90678f0 | 2017-07-14 16:29:30 +0200 | [diff] [blame] | 355 | if (NOT_LONE_DASH(fname)) { |
| 356 | if (!freopen(fname, "r", stdin)) { |
| 357 | bb_simple_perror_msg(fname); |
| 358 | dumper->exitval = 1; |
Denys Vlasenko | 25fe2d5 | 2021-06-18 00:32:29 +0200 | [diff] [blame] | 359 | dumper->next__done = 1; |
Denys Vlasenko | 90678f0 | 2017-07-14 16:29:30 +0200 | [diff] [blame] | 360 | continue; |
| 361 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 362 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 363 | } else { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 364 | if (dumper->next__done) |
Denys Vlasenko | 6aca76d | 2010-02-06 13:53:21 +0100 | [diff] [blame] | 365 | return 0; /* no next file */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 366 | } |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 367 | dumper->next__done = 1; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 368 | if (dumper->pub.dump_skip) |
Denys Vlasenko | 63214a6 | 2017-07-14 16:18:16 +0200 | [diff] [blame] | 369 | do_skip(dumper, fname ? fname : "stdin"); |
| 370 | if (dumper->pub.dump_skip == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 371 | return 1; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 372 | } |
| 373 | /* NOTREACHED */ |
| 374 | } |
| 375 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 376 | static unsigned char *get(priv_dumper_t *dumper) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 377 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 378 | int n; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 379 | int need, nread; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 380 | int blocksize = dumper->blocksize; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 381 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 382 | if (!dumper->get__curp) { |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 383 | dumper->pub.address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 384 | dumper->get__curp = xmalloc(blocksize); |
Denis Vlasenko | 2f86d13 | 2008-07-29 00:00:14 +0000 | [diff] [blame] | 385 | dumper->get__savp = xzalloc(blocksize); /* need to be initialized */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 386 | } else { |
Denis Vlasenko | 2f86d13 | 2008-07-29 00:00:14 +0000 | [diff] [blame] | 387 | unsigned char *tmp = dumper->get__curp; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 388 | dumper->get__curp = dumper->get__savp; |
Denis Vlasenko | 2f86d13 | 2008-07-29 00:00:14 +0000 | [diff] [blame] | 389 | dumper->get__savp = tmp; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 390 | dumper->savaddress += blocksize; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 391 | dumper->pub.address = dumper->savaddress; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 392 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 393 | need = blocksize; |
| 394 | nread = 0; |
| 395 | while (1) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 396 | /* |
| 397 | * if read the right number of bytes, or at EOF for one file, |
| 398 | * and no other files are available, zero-pad the rest of the |
| 399 | * block and set the end flag. |
| 400 | */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 401 | if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) { |
| 402 | if (need == blocksize) { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 403 | return NULL; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 404 | } |
Denys Vlasenko | e5d5f5b | 2018-07-03 16:27:54 +0200 | [diff] [blame] | 405 | if (dumper->pub.dump_vflag != ALL /* not "show all"? */ |
| 406 | && dumper->pub.dump_vflag != FIRST /* not first line? */ |
| 407 | && memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */ |
| 408 | ) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 409 | if (dumper->pub.dump_vflag != DUP) { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 410 | puts("*"); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 411 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 412 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 413 | memset(dumper->get__curp + nread, 0, need); |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 414 | dumper->eaddress = dumper->pub.address + nread; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 415 | return dumper->get__curp; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 416 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 417 | n = fread(dumper->get__curp + nread, sizeof(unsigned char), |
| 418 | dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin); |
Denys Vlasenko | e5d5f5b | 2018-07-03 16:27:54 +0200 | [diff] [blame] | 419 | if (n == 0) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 420 | if (ferror(stdin)) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 421 | bb_simple_perror_msg(dumper->argv[-1]); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 422 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 423 | dumper->get__ateof = 1; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 424 | continue; |
| 425 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 426 | dumper->get__ateof = 0; |
| 427 | if (dumper->pub.dump_length != -1) { |
| 428 | dumper->pub.dump_length -= n; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 429 | } |
Denis Vlasenko | 931de89 | 2007-06-21 12:43:45 +0000 | [diff] [blame] | 430 | need -= n; |
Denys Vlasenko | e5d5f5b | 2018-07-03 16:27:54 +0200 | [diff] [blame] | 431 | if (need == 0) { |
| 432 | if (dumper->pub.dump_vflag == ALL /* "show all"? */ |
| 433 | || dumper->pub.dump_vflag == FIRST /* first line? */ |
| 434 | || memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */ |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 435 | ) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 436 | if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) { |
| 437 | dumper->pub.dump_vflag = WAIT; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 438 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 439 | return dumper->get__curp; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 440 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 441 | if (dumper->pub.dump_vflag == WAIT) { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 442 | puts("*"); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 443 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 444 | dumper->pub.dump_vflag = DUP; |
| 445 | dumper->savaddress += blocksize; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 446 | dumper->pub.address = dumper->savaddress; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 447 | need = blocksize; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 448 | nread = 0; |
| 449 | } else { |
| 450 | nread += n; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 455 | static void bpad(PR *pr) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 456 | { |
Rob Landley | a6e6037 | 2006-06-28 14:36:50 +0000 | [diff] [blame] | 457 | char *p1, *p2; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * remove all conversion flags; '-' is the only one valid |
| 461 | * with %s, and it's not useful here. |
| 462 | */ |
| 463 | pr->flags = F_BPAD; |
| 464 | *pr->cchar = 's'; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 465 | for (p1 = pr->fmt; *p1 != '%'; ++p1) |
| 466 | continue; |
Rob Landley | a6e6037 | 2006-06-28 14:36:50 +0000 | [diff] [blame] | 467 | for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1) |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 468 | if (pr->nospace) |
| 469 | pr->nospace--; |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 470 | while ((*p2++ = *p1++) != '\0') |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 471 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 474 | static const char conv_str[] ALIGN1 = |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 475 | "\0" "\\""0""\0" |
| 476 | "\007""\\""a""\0" /* \a */ |
| 477 | "\b" "\\""b""\0" |
| 478 | "\f" "\\""f""\0" |
| 479 | "\n" "\\""n""\0" |
| 480 | "\r" "\\""r""\0" |
| 481 | "\t" "\\""t""\0" |
| 482 | "\v" "\\""v""\0" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 483 | ; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 484 | |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 485 | static void conv_c(PR *pr, unsigned char *p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 486 | { |
| 487 | const char *str = conv_str; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 488 | |
| 489 | do { |
| 490 | if (*p == *str) { |
| 491 | ++str; |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 492 | goto strpr; /* map e.g. '\n' to "\\n" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 493 | } |
| 494 | str += 4; |
| 495 | } while (*str); |
| 496 | |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 497 | if (isprint_asciionly(*p)) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 498 | *pr->cchar = 'c'; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 499 | printf(pr->fmt, *p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 500 | } else { |
Denys Vlasenko | 2ab994f | 2018-04-06 18:26:33 +0200 | [diff] [blame] | 501 | char buf[4]; |
| 502 | /* gcc-8.0.1 needs lots of casts to shut up */ |
| 503 | sprintf(buf, "%03o", (unsigned)(uint8_t)*p); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 504 | str = buf; |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 505 | strpr: |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 506 | *pr->cchar = 's'; |
| 507 | printf(pr->fmt, str); |
| 508 | } |
| 509 | } |
| 510 | |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 511 | static void conv_u(PR *pr, unsigned char *p) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 512 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 513 | static const char list[] ALIGN1 = |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 514 | "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0" |
| 515 | "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_" |
| 516 | "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0" |
| 517 | "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us"; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 518 | |
| 519 | /* od used nl, not lf */ |
| 520 | if (*p <= 0x1f) { |
| 521 | *pr->cchar = 's'; |
Glenn L McGrath | eeb06bf | 2004-07-23 01:35:41 +0000 | [diff] [blame] | 522 | printf(pr->fmt, list + (4 * (int)*p)); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 523 | } else if (*p == 0x7f) { |
| 524 | *pr->cchar = 's'; |
| 525 | printf(pr->fmt, "del"); |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 526 | } else if (*p < 0x7f) { /* isprint() */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 527 | *pr->cchar = 'c'; |
| 528 | printf(pr->fmt, *p); |
| 529 | } else { |
| 530 | *pr->cchar = 'x'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 531 | printf(pr->fmt, (int) *p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Denys Vlasenko | 53b2fdc | 2021-10-10 13:50:53 +0200 | [diff] [blame] | 535 | static NOINLINE void display(priv_dumper_t* dumper) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 536 | { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 537 | unsigned char *bp; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 538 | unsigned char savech = '\0'; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 539 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 540 | while ((bp = get(dumper)) != NULL) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 541 | FS *fs; |
| 542 | unsigned char *savebp; |
| 543 | off_t saveaddress; |
| 544 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 545 | fs = dumper->pub.fshead; |
| 546 | savebp = bp; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 547 | saveaddress = dumper->pub.address; |
| 548 | for (; fs; fs = fs->nextfs, bp = savebp, dumper->pub.address = saveaddress) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 549 | FU *fu; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 550 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 551 | int cnt; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 552 | if (fu->flags & F_IGNORE) { |
| 553 | break; |
| 554 | } |
| 555 | for (cnt = fu->reps; cnt; --cnt) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 556 | PR *pr; |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 557 | for (pr = fu->nextpr; pr; dumper->pub.address += pr->bcnt, |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 558 | bp += pr->bcnt, pr = pr->nextpr) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 559 | if (dumper->eaddress |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 560 | && dumper->pub.address >= dumper->eaddress |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 561 | ) { |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 562 | if (dumper->pub.xxd_eofstring) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 563 | /* xxd support: requested to not pad incomplete blocks */ |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 564 | fputs_stdout(dumper->pub.xxd_eofstring); |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | if (!(pr->flags & (F_TEXT | F_BPAD))) |
| 568 | bpad(pr); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 569 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 570 | if (cnt == 1 && pr->nospace) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 571 | savech = *pr->nospace; |
| 572 | *pr->nospace = '\0'; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 573 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 574 | switch (pr->flags) { |
| 575 | case F_ADDRESS: |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 576 | printf(pr->fmt, (unsigned long long) dumper->pub.address + dumper->pub.xxd_displayoff); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 577 | break; |
| 578 | case F_BPAD: |
| 579 | printf(pr->fmt, ""); |
| 580 | break; |
| 581 | case F_C: |
| 582 | conv_c(pr, bp); |
| 583 | break; |
| 584 | case F_CHAR: |
| 585 | printf(pr->fmt, *bp); |
| 586 | break; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 587 | case F_DBL: { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 588 | double dval; |
| 589 | float fval; |
| 590 | |
| 591 | switch (pr->bcnt) { |
| 592 | case 4: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 593 | memcpy(&fval, bp, sizeof(fval)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 594 | printf(pr->fmt, fval); |
| 595 | break; |
| 596 | case 8: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 597 | memcpy(&dval, bp, sizeof(dval)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 598 | printf(pr->fmt, dval); |
| 599 | break; |
| 600 | } |
| 601 | break; |
| 602 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 603 | case F_INT: { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 604 | int ival; |
| 605 | short sval; |
| 606 | |
| 607 | switch (pr->bcnt) { |
| 608 | case 1: |
| 609 | printf(pr->fmt, (int) *bp); |
| 610 | break; |
| 611 | case 2: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 612 | memcpy(&sval, bp, sizeof(sval)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 613 | printf(pr->fmt, (int) sval); |
| 614 | break; |
| 615 | case 4: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 616 | memcpy(&ival, bp, sizeof(ival)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 617 | printf(pr->fmt, ival); |
| 618 | break; |
| 619 | } |
| 620 | break; |
| 621 | } |
| 622 | case F_P: |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 623 | printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.'); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 624 | break; |
| 625 | case F_STR: |
| 626 | printf(pr->fmt, (char *) bp); |
| 627 | break; |
| 628 | case F_TEXT: |
| 629 | printf(pr->fmt); |
| 630 | break; |
| 631 | case F_U: |
| 632 | conv_u(pr, bp); |
| 633 | break; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 634 | case F_UINT: { |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 635 | unsigned ival; |
Eric Andersen | 0f56de6 | 2004-01-30 22:52:27 +0000 | [diff] [blame] | 636 | unsigned short sval; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 637 | |
| 638 | switch (pr->bcnt) { |
| 639 | case 1: |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 640 | printf(pr->fmt, (unsigned) *bp); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 641 | break; |
| 642 | case 2: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 643 | memcpy(&sval, bp, sizeof(sval)); |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 644 | printf(pr->fmt, (unsigned) sval); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 645 | break; |
| 646 | case 4: |
Denis Vlasenko | 3eb4466 | 2008-11-16 22:21:23 +0000 | [diff] [blame] | 647 | memcpy(&ival, bp, sizeof(ival)); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 648 | printf(pr->fmt, ival); |
| 649 | break; |
| 650 | } |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | if (cnt == 1 && pr->nospace) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 655 | *pr->nospace = savech; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 662 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 663 | if (dumper->endfu) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 664 | PR *pr; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 665 | /* |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 666 | * if eaddress not set, error or file size was multiple |
| 667 | * of blocksize, and no partial block ever found. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 668 | */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 669 | if (!dumper->eaddress) { |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 670 | if (!dumper->pub.address) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Denys Vlasenko | 2c43667 | 2021-06-18 00:59:17 +0200 | [diff] [blame] | 673 | dumper->eaddress = dumper->pub.address; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 674 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 675 | for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 676 | switch (pr->flags) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 677 | case F_ADDRESS: |
Denys Vlasenko | 4d16161 | 2021-06-17 23:53:30 +0200 | [diff] [blame] | 678 | printf(pr->fmt, (unsigned long long) dumper->eaddress + dumper->pub.xxd_displayoff); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 679 | break; |
| 680 | case F_TEXT: |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 681 | printf(pr->fmt); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 688 | #define dumper ((priv_dumper_t*)pub_dumper) |
| 689 | int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 690 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 691 | FS *tfs; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 692 | int blocksize; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 693 | |
Denys Vlasenko | 5f7904b | 2017-07-14 16:03:43 +0200 | [diff] [blame] | 694 | /* figure out the data block size */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 695 | blocksize = 0; |
| 696 | tfs = dumper->pub.fshead; |
| 697 | while (tfs) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 698 | tfs->bcnt = bb_dump_size(tfs); |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 699 | if (blocksize < tfs->bcnt) { |
| 700 | blocksize = tfs->bcnt; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 701 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 702 | tfs = tfs->nextfs; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 703 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 704 | dumper->blocksize = blocksize; |
| 705 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 706 | /* rewrite the rules, do syntax checking */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 707 | for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) { |
| 708 | rewrite(dumper, tfs); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 711 | dumper->argv = argv; |
| 712 | display(dumper); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 713 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 714 | return dumper->exitval; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 717 | void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 718 | { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 719 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 720 | FS *tfs; |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 721 | FU **nextfupp; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 722 | |
| 723 | /* start new linked list of format units */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 724 | tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */ |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 725 | if (!dumper->pub.fshead) { |
| 726 | dumper->pub.fshead = tfs; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 727 | } else { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 728 | FS *fslast = dumper->pub.fshead; |
| 729 | while (fslast->nextfs) |
| 730 | fslast = fslast->nextfs; |
| 731 | fslast->nextfs = tfs; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 732 | } |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 733 | nextfupp = &tfs->nextfu; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 734 | |
| 735 | /* take the format string and break it up into format units */ |
Denis Vlasenko | 15f2fdb | 2008-08-23 23:15:48 +0000 | [diff] [blame] | 736 | p = fmt; |
| 737 | for (;;) { |
Denys Vlasenko | dac5b83 | 2020-10-20 18:54:36 +0200 | [diff] [blame] | 738 | FU *tfu; |
| 739 | const char *savep; |
| 740 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 741 | p = skip_whitespace(p); |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 742 | if (*p == '\0') { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 743 | break; |
| 744 | } |
| 745 | |
| 746 | /* allocate a new format unit and link it in */ |
| 747 | /* NOSTRICT */ |
Denis Vlasenko | d02db89 | 2008-03-17 09:05:21 +0000 | [diff] [blame] | 748 | /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 749 | tfu = xzalloc(sizeof(FU)); |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 750 | *nextfupp = tfu; |
| 751 | nextfupp = &tfu->nextfu; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 752 | tfu->reps = 1; |
| 753 | |
| 754 | /* if leading digit, repetition count */ |
| 755 | if (isdigit(*p)) { |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 756 | for (savep = p; isdigit(*p); ++p) |
| 757 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 758 | if (!isspace(*p) && *p != '/') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 759 | bb_error_msg_and_die("bad format {%s}", fmt); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 760 | } |
| 761 | /* may overwrite either white space or slash */ |
| 762 | tfu->reps = atoi(savep); |
| 763 | tfu->flags = F_SETREP; |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 764 | /* skip trailing white space */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 765 | p = skip_whitespace(++p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 768 | /* skip slash and trailing white space */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 769 | if (*p == '/') { |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 770 | p = skip_whitespace(p + 1); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* byte count */ |
| 774 | if (isdigit(*p)) { |
Denis Vlasenko | 0de9375 | 2006-12-26 02:51:29 +0000 | [diff] [blame] | 775 | // TODO: use bb_strtou |
| 776 | savep = p; |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 777 | while (isdigit(*++p)) |
| 778 | continue; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 779 | if (!isspace(*p)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 780 | bb_error_msg_and_die("bad format {%s}", fmt); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 781 | } |
Denys Vlasenko | cb8e84e | 2017-01-25 16:21:00 +0100 | [diff] [blame] | 782 | // Above check prohibits formats such as '/1"%02x"' - it requires space after 1. |
| 783 | // Other than this, formats can be pretty much jammed together: |
| 784 | // "%07_ax:"8/2 "%04x|""\n" |
| 785 | // but this space is required. The check *can* be removed, but |
| 786 | // keeping it to stay compat with util-linux hexdump. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 787 | tfu->bcnt = atoi(savep); |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 788 | /* skip trailing white space */ |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 789 | p = skip_whitespace(p + 1); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* format */ |
| 793 | if (*p != '"') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 794 | bb_error_msg_and_die("bad format {%s}", fmt); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 795 | } |
| 796 | for (savep = ++p; *p != '"';) { |
Denys Vlasenko | a0bef7c | 2011-11-18 02:47:35 +0100 | [diff] [blame] | 797 | if (*p++ == '\0') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 798 | bb_error_msg_and_die("bad format {%s}", fmt); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
Denis Vlasenko | bd9874d | 2008-07-16 07:22:14 +0000 | [diff] [blame] | 801 | tfu->fmt = xstrndup(savep, p - savep); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 802 | |
| 803 | /* alphabetic escape sequences have to be done in place */ |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 804 | strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt); |
| 805 | /* unknown mappings are not changed: "\z" -> '\\' 'z' */ |
| 806 | /* trailing backslash, if any, is preserved */ |
| 807 | #if 0 |
| 808 | char *p1; |
| 809 | char *p2; |
| 810 | p1 = tfu->fmt; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 811 | for (p2 = p1;; ++p1, ++p2) { |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 812 | *p2 = *p1; |
| 813 | if (*p1 == '\0') |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 814 | break; |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 815 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 816 | if (*p1 == '\\') { |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 817 | const char *cs; |
| 818 | |
| 819 | p1++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 820 | *p2 = *p1; |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 821 | if (*p1 == '\0') { |
| 822 | /* "...\" trailing backslash. Eaten. */ |
| 823 | break; |
| 824 | } |
| 825 | cs = conv_str + 4; /* skip NUL element */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 826 | do { |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 827 | /* map e.g. "\n" -> '\n' */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 828 | if (*p1 == cs[2]) { |
| 829 | *p2 = cs[0]; |
| 830 | break; |
| 831 | } |
| 832 | cs += 4; |
| 833 | } while (*cs); |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 834 | /* unknown mappings remove bkslash: "\z" -> 'z' */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Denys Vlasenko | d3d7f08 | 2016-08-26 20:14:31 +0200 | [diff] [blame] | 837 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 838 | |
| 839 | p++; |
| 840 | } |
| 841 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 842 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 843 | /* |
| 844 | * Copyright (c) 1989 The Regents of the University of California. |
| 845 | * All rights reserved. |
| 846 | * |
| 847 | * Redistribution and use in source and binary forms, with or without |
| 848 | * modification, are permitted provided that the following conditions |
| 849 | * are met: |
| 850 | * 1. Redistributions of source code must retain the above copyright |
| 851 | * notice, this list of conditions and the following disclaimer. |
| 852 | * 2. Redistributions in binary form must reproduce the above copyright |
| 853 | * notice, this list of conditions and the following disclaimer in the |
| 854 | * documentation and/or other materials provided with the distribution. |
Aaron Lehmann | 69d4178 | 2002-06-23 22:25:24 +0000 | [diff] [blame] | 855 | * 3. Neither the name of the University nor the names of its contributors |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 856 | * may be used to endorse or promote products derived from this software |
| 857 | * without specific prior written permission. |
| 858 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 859 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 860 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 861 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 862 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 863 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 864 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 865 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 866 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 867 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 868 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 869 | * SUCH DAMAGE. |
| 870 | */ |