1 /* $OpenBSD: parse.c,v 1.5 1999/02/07 20:54:09 aaron Exp $ */ 2 3 /* 4 * Copyright (c) 1989 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 /*static char sccsid[] = "from: @(#)parse.c 5.6 (Berkeley) 3/9/91";*/ 38 static char rcsid[] = "$OpenBSD: parse.c,v 1.5 1999/02/07 20:54:09 aaron Exp $"; 39 #endif /* not lint */ 40 41 #include <sys/types.h> 42 #include <sys/file.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <ctype.h> 46 #include <string.h> 47 #include "hexdump.h" 48 49 FU *endfu; /* format at end-of-data */ 50 51 addfile(name) 52 char *name; 53 { 54 register char *p; 55 FILE *fp; 56 size_t len; 57 58 if (!(fp = fopen(name, "r"))) { 59 (void)fprintf(stderr, "hexdump: can't read %s.\n", name); 60 exit(1); 61 } 62 while ((p = fgetln(fp, &len))) { 63 if (*(p + len - 1) == '\n') 64 *(p + len - 1) = '\0'; 65 else { 66 (void)fprintf(stderr, "hexdump: incomplete line.\n"); 67 continue; 68 } 69 for (; *p && isspace(*p); ++p); 70 if (!*p || *p == '#') 71 continue; 72 add(p); 73 } 74 (void)fclose(fp); 75 } 76 77 add(fmt) 78 char *fmt; 79 { 80 register char *p; 81 static FS **nextfs; 82 FS *tfs; 83 FU *tfu, **nextfu; 84 char *savep, *emalloc(); 85 86 /* start new linked list of format units */ 87 /* NOSTRICT */ 88 tfs = (FS *)emalloc(sizeof(FS)); 89 if (!fshead) 90 fshead = tfs; 91 else 92 *nextfs = tfs; 93 nextfs = &tfs->nextfs; 94 nextfu = &tfs->nextfu; 95 96 /* take the format string and break it up into format units */ 97 for (p = fmt;;) { 98 /* skip leading white space */ 99 for (; isspace(*p); ++p); 100 if (!*p) 101 break; 102 103 /* allocate a new format unit and link it in */ 104 /* NOSTRICT */ 105 tfu = (FU *)emalloc(sizeof(FU)); 106 *nextfu = tfu; 107 nextfu = &tfu->nextfu; 108 tfu->reps = 1; 109 110 /* if leading digit, repetition count */ 111 if (isdigit(*p)) { 112 for (savep = p; isdigit(*p); ++p); 113 if (!isspace(*p) && *p != '/') 114 badfmt(fmt); 115 /* may overwrite either white space or slash */ 116 tfu->reps = atoi(savep); 117 tfu->flags = F_SETREP; 118 /* skip trailing white space */ 119 for (++p; isspace(*p); ++p); 120 } 121 122 /* skip slash and trailing white space */ 123 if (*p == '/') 124 while (isspace(*++p)); 125 126 /* byte count */ 127 if (isdigit(*p)) { 128 for (savep = p; isdigit(*p); ++p); 129 if (!isspace(*p)) 130 badfmt(fmt); 131 tfu->bcnt = atoi(savep); 132 /* skip trailing white space */ 133 for (++p; isspace(*p); ++p); 134 } 135 136 /* format */ 137 if (*p != '"') 138 badfmt(fmt); 139 for (savep = ++p; *p != '"';) 140 if (*p++ == 0) 141 badfmt(fmt); 142 if (!(tfu->fmt = malloc(p - savep + 1))) 143 nomem(); 144 (void) strncpy(tfu->fmt, savep, p - savep); 145 tfu->fmt[p - savep] = '\0'; 146 escape(tfu->fmt); 147 p++; 148 } 149 /* no single fu in fmt */ 150 if (tfs->nextfu == NULL) 151 badfmt(fmt); 152 } 153 154 static char *spec = ".#-+ 0123456789"; 155 size(fs) 156 FS *fs; 157 { 158 register FU *fu; 159 register int bcnt, cursize; 160 register char *fmt; 161 int prec; 162 163 /* figure out the data block size needed for each format unit */ 164 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) { 165 if (fu->bcnt) { 166 cursize += fu->bcnt * fu->reps; 167 continue; 168 } 169 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) { 170 if (*fmt != '%') 171 continue; 172 /* 173 * skip any special chars -- save precision in 174 * case it's a %s format. 175 */ 176 while (strchr(spec + 1, *++fmt)); 177 if (*fmt == '.' && isdigit(*++fmt)) { 178 prec = atoi(fmt); 179 while (isdigit(*++fmt)); 180 } 181 switch(*fmt) { 182 case 'c': 183 bcnt += 1; 184 break; 185 case 'd': case 'i': case 'o': case 'u': 186 case 'x': case 'X': 187 bcnt += 4; 188 break; 189 case 'e': case 'E': case 'f': case 'g': case 'G': 190 bcnt += 8; 191 break; 192 case 's': 193 bcnt += prec; 194 break; 195 case '_': 196 switch(*++fmt) { 197 case 'c': case 'p': case 'u': 198 bcnt += 1; 199 break; 200 } 201 } 202 } 203 cursize += bcnt * fu->reps; 204 } 205 return(cursize); 206 } 207 208 rewrite(fs) 209 FS *fs; 210 { 211 enum { NOTOKAY, USEBCNT, USEPREC } sokay; 212 register PR *pr, **nextpr; 213 register FU *fu; 214 register char *p1, *p2; 215 char savech, *fmtp; 216 int nconv, prec; 217 218 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 219 /* 220 * break each format unit into print units; each 221 * conversion character gets its own. 222 */ 223 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) { 224 /* NOSTRICT */ 225 pr = (PR *)emalloc(sizeof(PR)); 226 if (!fu->nextpr) 227 fu->nextpr = pr; 228 else 229 *nextpr = pr; 230 231 /* skip preceding text and up to the next % sign */ 232 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1); 233 234 /* only text in the string */ 235 if (!*p1) { 236 pr->fmt = fmtp; 237 pr->flags = F_TEXT; 238 break; 239 } 240 241 /* 242 * get precision for %s -- if have a byte count, don't 243 * need it. 244 */ 245 if (fu->bcnt) { 246 sokay = USEBCNT; 247 /* skip to conversion character */ 248 for (++p1; strchr(spec, *p1); ++p1); 249 } else { 250 /* skip any special chars, field width */ 251 while (strchr(spec + 1, *++p1)); 252 if (*p1 == '.' && isdigit(*++p1)) { 253 sokay = USEPREC; 254 prec = atoi(p1); 255 while (isdigit(*++p1)); 256 } 257 else 258 sokay = NOTOKAY; 259 } 260 261 p2 = p1 + 1; /* set end pointer */ 262 263 /* 264 * figure out the byte count for each conversion; 265 * rewrite the format as necessary, set up blank- 266 * padding for end of data. 267 */ 268 switch(*p1) { 269 case 'c': 270 pr->flags = F_CHAR; 271 switch(fu->bcnt) { 272 case 0: case 1: 273 pr->bcnt = 1; 274 break; 275 default: 276 p1[1] = '\0'; 277 badcnt(p1); 278 } 279 break; 280 case 'd': case 'i': 281 pr->flags = F_INT; 282 goto sw1; 283 case 'l': 284 ++p2; 285 switch(p1[1]) { 286 case 'd': case 'i': 287 ++p1; 288 pr->flags = F_INT; 289 goto sw1; 290 case 'o': case 'u': case 'x': case 'X': 291 ++p1; 292 pr->flags = F_UINT; 293 goto sw1; 294 default: 295 p1[2] = '\0'; 296 badconv(p1); 297 } 298 /* NOTREACHED */ 299 case 'o': case 'u': case 'x': case 'X': 300 pr->flags = F_UINT; 301 sw1: switch(fu->bcnt) { 302 case 0: case 4: 303 pr->bcnt = 4; 304 break; 305 case 1: 306 pr->bcnt = 1; 307 break; 308 case 2: 309 pr->bcnt = 2; 310 break; 311 default: 312 p1[1] = '\0'; 313 badcnt(p1); 314 } 315 break; 316 case 'e': case 'E': case 'f': case 'g': case 'G': 317 pr->flags = F_DBL; 318 switch(fu->bcnt) { 319 case 0: case 8: 320 pr->bcnt = 8; 321 break; 322 case 4: 323 pr->bcnt = 4; 324 break; 325 default: 326 p1[1] = '\0'; 327 badcnt(p1); 328 } 329 break; 330 case 's': 331 pr->flags = F_STR; 332 switch(sokay) { 333 case NOTOKAY: 334 badsfmt(); 335 case USEBCNT: 336 pr->bcnt = fu->bcnt; 337 break; 338 case USEPREC: 339 pr->bcnt = prec; 340 break; 341 } 342 break; 343 case '_': 344 ++p2; 345 switch(p1[1]) { 346 case 'A': 347 endfu = fu; 348 fu->flags |= F_IGNORE; 349 /* FALLTHROUGH */ 350 case 'a': 351 pr->flags = F_ADDRESS; 352 ++p2; 353 switch(p1[2]) { 354 case 'd': case 'o': case'x': 355 *p1 = 'q'; 356 p1[1] = p1[2]; 357 break; 358 default: 359 p1[3] = '\0'; 360 badconv(p1); 361 } 362 break; 363 case 'c': 364 pr->flags = F_C; 365 /* *p1 = 'c'; set in conv_c */ 366 goto sw2; 367 case 'p': 368 pr->flags = F_P; 369 *p1 = 'c'; 370 goto sw2; 371 case 'u': 372 pr->flags = F_U; 373 /* *p1 = 'c'; set in conv_u */ 374 sw2: switch(fu->bcnt) { 375 case 0: case 1: 376 pr->bcnt = 1; 377 break; 378 default: 379 p1[2] = '\0'; 380 badcnt(p1); 381 } 382 break; 383 default: 384 p1[2] = '\0'; 385 badconv(p1); 386 } 387 break; 388 default: 389 p1[1] = '\0'; 390 badconv(p1); 391 } 392 393 /* 394 * copy to PR format string, set conversion character 395 * pointer, update original. 396 */ 397 savech = *p2; 398 p1[(pr->flags&F_ADDRESS)?2:1] = '\0'; 399 if (!(pr->fmt = strdup(fmtp))) 400 nomem(); 401 *p2 = savech; 402 pr->cchar = pr->fmt + (p1 - fmtp); 403 fmtp = p2; 404 405 /* only one conversion character if byte count */ 406 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) { 407 (void)fprintf(stderr, 408 "hexdump: byte count with multiple conversion characters.\n"); 409 exit(1); 410 } 411 } 412 /* 413 * if format unit byte count not specified, figure it out 414 * so can adjust rep count later. 415 */ 416 if (!fu->bcnt) 417 for (pr = fu->nextpr; pr; pr = pr->nextpr) 418 fu->bcnt += pr->bcnt; 419 } 420 /* 421 * if the format string interprets any data at all, and it's 422 * not the same as the blocksize, and its last format unit 423 * interprets any data at all, and has no iteration count, 424 * repeat it as necessary. 425 * 426 * if, rep count is greater than 1, no trailing whitespace 427 * gets output from the last iteration of the format unit. 428 */ 429 for (fu = fs->nextfu;; fu = fu->nextfu) { 430 if (!fu->nextfu && fs->bcnt < blocksize && 431 !(fu->flags&F_SETREP) && fu->bcnt) 432 fu->reps += (blocksize - fs->bcnt) / fu->bcnt; 433 if (fu->reps > 1) { 434 for (pr = fu->nextpr;; pr = pr->nextpr) 435 if (!pr->nextpr) 436 break; 437 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1) 438 p2 = isspace(*p1) ? p1 : NULL; 439 if (p2) 440 pr->nospace = p2; 441 } 442 if (!fu->nextfu) 443 break; 444 } 445 } 446 447 448 escape(p1) 449 register char *p1; 450 { 451 register char *p2; 452 453 /* alphabetic escape sequences have to be done in place */ 454 for (p2 = p1;; ++p1, ++p2) { 455 if (!*p1) { 456 *p2 = *p1; 457 break; 458 } 459 if (*p1 == '\\') 460 switch(*++p1) { 461 case 'a': 462 /* *p2 = '\a'; */ 463 *p2 = '\007'; 464 break; 465 case 'b': 466 *p2 = '\b'; 467 break; 468 case 'f': 469 *p2 = '\f'; 470 break; 471 case 'n': 472 *p2 = '\n'; 473 break; 474 case 'r': 475 *p2 = '\r'; 476 break; 477 case 't': 478 *p2 = '\t'; 479 break; 480 case 'v': 481 *p2 = '\v'; 482 break; 483 default: 484 *p2 = *p1; 485 break; 486 } 487 } 488 } 489 490 badcnt(s) 491 char *s; 492 { 493 (void)fprintf(stderr, 494 "hexdump: bad byte count for conversion character %s.\n", s); 495 exit(1); 496 } 497 498 badsfmt() 499 { 500 (void)fprintf(stderr, 501 "hexdump: %%s requires a precision or a byte count.\n"); 502 exit(1); 503 } 504 505 badfmt(fmt) 506 char *fmt; 507 { 508 (void)fprintf(stderr, "hexdump: bad format {%s}\n", fmt); 509 exit(1); 510 } 511 512 badconv(ch) 513 char *ch; 514 { 515 (void)fprintf(stderr, "hexdump: bad conversion character %%%s.\n", ch); 516 exit(1); 517 } 518