1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)odsyntax.c 8.2 (Berkeley) 5/4/95 30 * $FreeBSD: src/usr.bin/hexdump/odsyntax.c,v 1.8.2.1 2002/07/23 14:27:06 tjr Exp $ 31 */ 32 33 #include <sys/types.h> 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <float.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "hexdump.h" 46 47 #define PADDING " " 48 49 int odmode; 50 51 static void odadd(const char *); 52 static void odformat(const char *); 53 static const char *odformatfp(char, const char *); 54 static const char *odformatint(char, const char *); 55 static void odoffset(int, char ***); 56 static void odusage(void); 57 58 void 59 oldsyntax(int argc, char ***argvp) 60 { 61 static char empty[] = "", padding[] = PADDING; 62 int ch; 63 char **argv, *end; 64 65 /* Add initial (default) address format. -A may change it later. */ 66 #define TYPE_OFFSET 7 67 add("\"%07.7_Ao\n\""); 68 add("\"%07.7_ao \""); 69 70 odmode = 1; 71 argv = *argvp; 72 while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1) { 73 switch (ch) { 74 case 'A': 75 switch (*optarg) { 76 case 'd': case 'o': case 'x': 77 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg; 78 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 79 *optarg; 80 break; 81 case 'n': 82 fshead->nextfu->fmt = empty; 83 fshead->nextfs->nextfu->fmt = padding; 84 break; 85 default: 86 errx(1, "%s: invalid address base", optarg); 87 } 88 break; 89 case 'a': 90 odformat("a"); 91 break; 92 case 'B': 93 case 'o': 94 odformat("o2"); 95 break; 96 case 'b': 97 odformat("o1"); 98 break; 99 case 'c': 100 odformat("c"); 101 break; 102 case 'd': 103 odformat("u2"); 104 break; 105 case 'D': 106 odformat("u4"); 107 break; 108 case 'e': /* undocumented in od */ 109 case 'F': 110 odformat("fD"); 111 break; 112 case 'f': 113 odformat("fF"); 114 break; 115 case 'H': 116 case 'X': 117 odformat("x4"); 118 break; 119 case 'h': 120 case 'x': 121 odformat("x2"); 122 break; 123 case 'I': 124 case 'L': 125 case 'l': 126 odformat("dL"); 127 break; 128 case 'i': 129 odformat("dI"); 130 break; 131 case 'j': 132 errno = 0; 133 skip = strtoll(optarg, &end, 0); 134 if (*end == 'b') 135 skip *= 512; 136 else if (*end == 'k') 137 skip *= 1024; 138 else if (*end == 'm') 139 skip *= 1048576L; 140 if (errno != 0 || skip < 0 || strlen(end) > 1) 141 errx(1, "%s: invalid skip amount", optarg); 142 break; 143 case 'N': 144 if ((length = atoi(optarg)) <= 0) 145 errx(1, "%s: invalid length", optarg); 146 break; 147 case 'O': 148 odformat("o4"); 149 break; 150 case 's': 151 odformat("d2"); 152 break; 153 case 't': 154 odformat(optarg); 155 break; 156 case 'v': 157 vflag = ALL; 158 break; 159 case '?': 160 default: 161 odusage(); 162 } 163 } 164 165 if (fshead->nextfs->nextfs == NULL) 166 odformat("oS"); 167 168 argc -= optind; 169 *argvp += optind; 170 171 if (argc) 172 odoffset(argc, argvp); 173 } 174 175 static void 176 odusage(void) 177 { 178 179 fprintf(stderr, 180 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n"); 181 fprintf(stderr, 182 " [[+]offset[.][Bb]] [file ...]\n"); 183 exit(1); 184 } 185 186 static void 187 odoffset(int argc, char ***argvp) 188 { 189 unsigned char *p, *num, *end; 190 int base; 191 192 /* 193 * The offset syntax of od(1) was genuinely bizarre. First, if 194 * it started with a plus it had to be an offset. Otherwise, if 195 * there were at least two arguments, a number or lower-case 'x' 196 * followed by a number makes it an offset. By default it was 197 * octal; if it started with 'x' or '0x' it was hex. If it ended 198 * in a '.', it was decimal. If a 'b' or 'B' was appended, it 199 * multiplied the number by 512 or 1024 byte units. There was 200 * no way to assign a block count to a hex offset. 201 * 202 * We assume it's a file if the offset is bad. 203 */ 204 p = argc == 1 ? (*argvp)[0] : (*argvp)[1]; 205 206 if (*p != '+' && (argc < 2 || 207 (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1]))))) 208 return; 209 210 base = 0; 211 /* 212 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and 213 * set base. 214 */ 215 if (p[0] == '+') 216 ++p; 217 if (p[0] == 'x' && isxdigit(p[1])) { 218 ++p; 219 base = 16; 220 } else if (p[0] == '0' && p[1] == 'x') { 221 p += 2; 222 base = 16; 223 } 224 225 /* skip over the number */ 226 if (base == 16) 227 for (num = p; isxdigit(*p); ++p); 228 else 229 for (num = p; isdigit(*p); ++p); 230 231 /* check for no number */ 232 if (num == p) 233 return; 234 235 /* if terminates with a '.', base is decimal */ 236 if (*p == '.') { 237 if (base) 238 return; 239 base = 10; 240 } 241 242 skip = strtoll(num, (char **)&end, base ? base : 8); 243 244 /* if end isn't the same as p, we got a non-octal digit */ 245 if (end != p) { 246 skip = 0; 247 return; 248 } 249 250 if (*p) { 251 if (*p == 'B') { 252 skip *= 1024; 253 ++p; 254 } else if (*p == 'b') { 255 skip *= 512; 256 ++p; 257 } 258 } 259 260 if (*p) { 261 skip = 0; 262 return; 263 } 264 265 /* 266 * If the offset uses a non-octal base, the base of the offset 267 * is changed as well. This isn't pretty, but it's easy. 268 */ 269 if (base == 16) { 270 fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; 271 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; 272 } else if (base == 10) { 273 fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; 274 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; 275 } 276 277 /* Terminate file list. */ 278 (*argvp)[1] = NULL; 279 } 280 281 static void 282 odformat(const char *fmt) 283 { 284 char fchar; 285 286 while (*fmt != '\0') { 287 switch ((fchar = *fmt++)) { 288 case 'a': 289 odadd("16/1 \"%3_u \" \"\\n\""); 290 break; 291 case 'c': 292 odadd("16/1 \"%3_c \" \"\\n\""); 293 break; 294 case 'o': case 'u': case 'd': case 'x': 295 fmt = odformatint(fchar, fmt); 296 break; 297 case 'f': 298 fmt = odformatfp(fchar, fmt); 299 break; 300 default: 301 errx(1, "%c: unrecognised format character", fchar); 302 } 303 } 304 } 305 306 static const char * 307 odformatfp(char fchar __unused, const char *fmt) 308 { 309 size_t isize; 310 int digits, width; 311 char *end, *hdfmt; 312 313 isize = sizeof(double); 314 switch (*fmt) { 315 case 'F': 316 isize = sizeof(float); 317 fmt++; 318 break; 319 case 'D': 320 isize = sizeof(double); 321 fmt++; 322 break; 323 case 'L': 324 isize = sizeof(long double); 325 fmt++; 326 break; 327 default: 328 if (isdigit((unsigned char)*fmt)) { 329 errno = 0; 330 isize = (size_t)strtoul(fmt, &end, 10); 331 if (errno != 0 || isize == 0) 332 errx(1, "%s: invalid size", fmt); 333 fmt = (const char *)end; 334 } 335 } 336 switch (isize) { 337 case sizeof(float): 338 digits = FLT_DIG; 339 width = digits + 7; /* -x.*e[+-]xx */ 340 break; 341 case sizeof(double): 342 digits = DBL_DIG; 343 width = digits + 8; /* -x.*e[+-]xxx */ 344 break; 345 default: 346 if (isize == sizeof(long double)) { 347 digits = LDBL_DIG; 348 width = digits + 9; /* -x.*e[+-]xxxx */ 349 } else { 350 errx(1, "unsupported floating point size %lu", 351 (u_long)isize); 352 } 353 } 354 355 asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"", 356 16UL / (u_long)isize, (u_long)isize, width, digits); 357 if (hdfmt == NULL) 358 err(1, NULL); 359 odadd(hdfmt); 360 free(hdfmt); 361 362 return (fmt); 363 } 364 365 static const char * 366 odformatint(char fchar, const char *fmt) 367 { 368 unsigned long long n; 369 size_t isize; 370 int digits; 371 char *end, *hdfmt; 372 373 isize = sizeof(int); 374 switch (*fmt) { 375 case 'C': 376 isize = sizeof(char); 377 fmt++; 378 break; 379 case 'I': 380 isize = sizeof(int); 381 fmt++; 382 break; 383 case 'L': 384 isize = sizeof(long); 385 fmt++; 386 break; 387 case 'S': 388 isize = sizeof(short); 389 fmt++; 390 break; 391 default: 392 if (isdigit((unsigned char)*fmt)) { 393 errno = 0; 394 isize = (size_t)strtoul(fmt, &end, 10); 395 if (errno != 0 || isize == 0) 396 errx(1, "%s: invalid size", fmt); 397 if (isize != sizeof(char) && isize != sizeof(short) && 398 isize != sizeof(int) && isize != sizeof(long)) 399 errx(1, "unsupported int size %lu", 400 (u_long)isize); 401 fmt = (const char *)end; 402 } 403 } 404 405 /* 406 * Calculate the maximum number of digits we need to 407 * fit the number. Overestimate for decimal with log 408 * base 8. We need one extra space for signed numbers 409 * to store the sign. 410 */ 411 n = (isize >= sizeof(n)) ? ULLONG_MAX : (1ULL << (8 * isize)) - 1; 412 digits = 0; 413 while (n != 0) { 414 digits++; 415 n >>= (fchar == 'x') ? 4 : 3; 416 } 417 if (fchar == 'd') 418 digits++; 419 asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"", 420 16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits), 421 "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar); 422 if (hdfmt == NULL) 423 err(1, NULL); 424 odadd(hdfmt); 425 free(hdfmt); 426 427 return (fmt); 428 } 429 430 static void 431 odadd(const char *fmt) 432 { 433 static int needpad; 434 435 if (needpad) 436 add("\""PADDING"\""); 437 add(fmt); 438 needpad = 1; 439 } 440