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