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