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