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