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