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