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 "$Id$"; 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 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 register char *p; 85 static FS **nextfs; 86 FS *tfs; 87 FU *tfu, **nextfu; 88 char *savep; 89 90 /* start new linked list of format units */ 91 tfs = emalloc(sizeof(FS)); 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 tfu = emalloc(sizeof(FU)); 108 *nextfu = tfu; 109 nextfu = &tfu->nextfu; 110 tfu->reps = 1; 111 112 /* if leading digit, repetition count */ 113 if (isdigit(*p)) { 114 for (savep = p; isdigit(*p); ++p); 115 if (!isspace(*p) && *p != '/') 116 badfmt(fmt); 117 /* may overwrite either white space or slash */ 118 tfu->reps = atoi(savep); 119 tfu->flags = F_SETREP; 120 /* skip trailing white space */ 121 for (++p; isspace(*p); ++p); 122 } 123 124 /* skip slash and trailing white space */ 125 if (*p == '/') 126 while (isspace(*++p)); 127 128 /* byte count */ 129 if (isdigit(*p)) { 130 for (savep = p; isdigit(*p); ++p); 131 if (!isspace(*p)) 132 badfmt(fmt); 133 tfu->bcnt = atoi(savep); 134 /* skip trailing white space */ 135 for (++p; isspace(*p); ++p); 136 } 137 138 /* format */ 139 if (*p != '"') 140 badfmt(fmt); 141 for (savep = ++p; *p != '"';) 142 if (*p++ == 0) 143 badfmt(fmt); 144 if (!(tfu->fmt = malloc(p - savep + 1))) 145 nomem(); 146 (void) strncpy(tfu->fmt, savep, p - savep); 147 tfu->fmt[p - savep] = '\0'; 148 escape(tfu->fmt); 149 p++; 150 } 151 } 152 153 static char *spec = ".#-+ 0123456789"; 154 155 int 156 size(fs) 157 FS *fs; 158 { 159 register FU *fu; 160 register int bcnt, cursize; 161 register char *fmt; 162 int prec; 163 164 /* figure out the data block size needed for each format unit */ 165 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) { 166 if (fu->bcnt) { 167 cursize += fu->bcnt * fu->reps; 168 continue; 169 } 170 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) { 171 if (*fmt != '%') 172 continue; 173 /* 174 * skip any special chars -- save precision in 175 * case it's a %s format. 176 */ 177 while (index(spec + 1, *++fmt)); 178 if (*fmt == '.' && isdigit(*++fmt)) { 179 prec = atoi(fmt); 180 while (isdigit(*++fmt)); 181 } 182 switch(*fmt) { 183 case 'c': 184 bcnt += 1; 185 break; 186 case 'd': case 'i': case 'o': case 'u': 187 case 'x': case 'X': 188 bcnt += 4; 189 break; 190 case 'e': case 'E': case 'f': case 'g': case 'G': 191 bcnt += 8; 192 break; 193 case 's': 194 bcnt += prec; 195 break; 196 case '_': 197 switch(*++fmt) { 198 case 'c': case 'p': case 'u': 199 bcnt += 1; 200 break; 201 } 202 } 203 } 204 cursize += bcnt * fu->reps; 205 } 206 return (cursize); 207 } 208 209 void 210 rewrite(fs) 211 FS *fs; 212 { 213 enum { NOTOKAY, USEBCNT, USEPREC } sokay; 214 register PR *pr, **nextpr; 215 register FU *fu; 216 register char *p1, *p2; 217 char savech, *fmtp, cs[3]; 218 int nconv, prec; 219 220 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 221 /* 222 * Break each format unit into print units; each conversion 223 * character gets its own. 224 */ 225 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) { 226 pr = emalloc(sizeof(PR)); 227 if (!fu->nextpr) 228 fu->nextpr = pr; 229 else 230 *nextpr = pr; 231 232 /* Skip preceding text and up to the next % sign. */ 233 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1); 234 235 /* Only text in the string. */ 236 if (!*p1) { 237 pr->fmt = fmtp; 238 pr->flags = F_TEXT; 239 break; 240 } 241 242 /* 243 * Get precision for %s -- if have a byte count, don't 244 * need it. 245 */ 246 if (fu->bcnt) { 247 sokay = USEBCNT; 248 /* Skip to conversion character. */ 249 for (++p1; index(spec, *p1); ++p1); 250 } else { 251 /* Skip any special chars, field width. */ 252 while (index(spec + 1, *++p1)); 253 if (*p1 == '.' && isdigit(*++p1)) { 254 sokay = USEPREC; 255 prec = atoi(p1); 256 while (isdigit(*++p1)); 257 } else 258 sokay = NOTOKAY; 259 } 260 261 p2 = p1 + 1; /* Set end pointer. */ 262 cs[0] = *p1; /* Set conversion string. */ 263 cs[1] = '\0'; 264 265 /* 266 * Figure out the byte count for each conversion; 267 * rewrite the format as necessary, set up blank- 268 * padding for end of data. 269 */ 270 switch(cs[0]) { 271 case 'c': 272 pr->flags = F_CHAR; 273 switch(fu->bcnt) { 274 case 0: case 1: 275 pr->bcnt = 1; 276 break; 277 default: 278 p1[1] = '\0'; 279 badcnt(p1); 280 } 281 break; 282 case 'd': case 'i': 283 pr->flags = F_INT; 284 goto isint; 285 case 'o': case 'u': case 'x': case 'X': 286 pr->flags = F_UINT; 287 isint: cs[2] = '\0'; 288 cs[1] = cs[0]; 289 cs[0] = 'q'; 290 switch(fu->bcnt) { 291 case 0: case 4: 292 pr->bcnt = 4; 293 break; 294 case 1: 295 pr->bcnt = 1; 296 break; 297 case 2: 298 pr->bcnt = 2; 299 break; 300 default: 301 p1[1] = '\0'; 302 badcnt(p1); 303 } 304 break; 305 case 'e': case 'E': case 'f': case 'g': case 'G': 306 pr->flags = F_DBL; 307 switch(fu->bcnt) { 308 case 0: case 8: 309 pr->bcnt = 8; 310 break; 311 case 4: 312 pr->bcnt = 4; 313 break; 314 default: 315 p1[1] = '\0'; 316 badcnt(p1); 317 } 318 break; 319 case 's': 320 pr->flags = F_STR; 321 switch(sokay) { 322 case NOTOKAY: 323 badsfmt(); 324 case USEBCNT: 325 pr->bcnt = fu->bcnt; 326 break; 327 case USEPREC: 328 pr->bcnt = prec; 329 break; 330 } 331 break; 332 case '_': 333 ++p2; 334 switch(p1[1]) { 335 case 'A': 336 endfu = fu; 337 fu->flags |= F_IGNORE; 338 /* FALLTHROUGH */ 339 case 'a': 340 pr->flags = F_ADDRESS; 341 ++p2; 342 switch(p1[2]) { 343 case 'd': case 'o': case'x': 344 cs[0] = 'q'; 345 cs[1] = p1[2]; 346 cs[2] = '\0'; 347 break; 348 default: 349 p1[3] = '\0'; 350 badconv(p1); 351 } 352 break; 353 case 'c': 354 pr->flags = F_C; 355 /* cs[0] = 'c'; set in conv_c */ 356 goto isint2; 357 case 'p': 358 pr->flags = F_P; 359 cs[0] = 'c'; 360 goto isint2; 361 case 'u': 362 pr->flags = F_U; 363 /* cs[0] = 'c'; set in conv_u */ 364 isint2: switch(fu->bcnt) { 365 case 0: case 1: 366 pr->bcnt = 1; 367 break; 368 default: 369 p1[2] = '\0'; 370 badcnt(p1); 371 } 372 break; 373 default: 374 p1[2] = '\0'; 375 badconv(p1); 376 } 377 break; 378 default: 379 p1[1] = '\0'; 380 badconv(p1); 381 } 382 383 /* 384 * Copy to PR format string, set conversion character 385 * pointer, update original. 386 */ 387 savech = *p2; 388 p1[0] = '\0'; 389 pr->fmt = emalloc(strlen(fmtp) + 2); 390 (void)strcpy(pr->fmt, fmtp); 391 (void)strcat(pr->fmt, cs); 392 *p2 = savech; 393 pr->cchar = pr->fmt + (p1 - fmtp); 394 fmtp = p2; 395 396 /* Only one conversion character if byte count. */ 397 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) 398 errx(1, "byte count with multiple conversion characters"); 399 } 400 /* 401 * If format unit byte count not specified, figure it out 402 * so can adjust rep count later. 403 */ 404 if (!fu->bcnt) 405 for (pr = fu->nextpr; pr; pr = pr->nextpr) 406 fu->bcnt += pr->bcnt; 407 } 408 /* 409 * If the format string interprets any data at all, and it's 410 * not the same as the blocksize, and its last format unit 411 * interprets any data at all, and has no iteration count, 412 * repeat it as necessary. 413 * 414 * If, rep count is greater than 1, no trailing whitespace 415 * gets output from the last iteration of the format unit. 416 */ 417 for (fu = fs->nextfu;; fu = fu->nextfu) { 418 if (!fu->nextfu && fs->bcnt < blocksize && 419 !(fu->flags&F_SETREP) && fu->bcnt) 420 fu->reps += (blocksize - fs->bcnt) / fu->bcnt; 421 if (fu->reps > 1) { 422 for (pr = fu->nextpr;; pr = pr->nextpr) 423 if (!pr->nextpr) 424 break; 425 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1) 426 p2 = isspace(*p1) ? p1 : NULL; 427 if (p2) 428 pr->nospace = p2; 429 } 430 if (!fu->nextfu) 431 break; 432 } 433 #ifdef DEBUG 434 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 435 (void)printf("fmt:"); 436 for (pr = fu->nextpr; pr; pr = pr->nextpr) 437 (void)printf(" {%s}", pr->fmt); 438 (void)printf("\n"); 439 } 440 #endif 441 } 442 443 void 444 escape(p1) 445 register char *p1; 446 { 447 register char *p2; 448 449 /* alphabetic escape sequences have to be done in place */ 450 for (p2 = p1;; ++p1, ++p2) { 451 if (!*p1) { 452 *p2 = *p1; 453 break; 454 } 455 if (*p1 == '\\') 456 switch(*++p1) { 457 case 'a': 458 /* *p2 = '\a'; */ 459 *p2 = '\007'; 460 break; 461 case 'b': 462 *p2 = '\b'; 463 break; 464 case 'f': 465 *p2 = '\f'; 466 break; 467 case 'n': 468 *p2 = '\n'; 469 break; 470 case 'r': 471 *p2 = '\r'; 472 break; 473 case 't': 474 *p2 = '\t'; 475 break; 476 case 'v': 477 *p2 = '\v'; 478 break; 479 default: 480 *p2 = *p1; 481 break; 482 } 483 } 484 } 485 486 void 487 badcnt(s) 488 char *s; 489 { 490 errx(1, "%s: bad byte count", s); 491 } 492 493 void 494 badsfmt() 495 { 496 errx(1, "%%s: requires a precision or a byte count"); 497 } 498 499 void 500 badfmt(fmt) 501 char *fmt; 502 { 503 errx(1, "\"%s\": bad format", fmt); 504 } 505 506 void 507 badconv(ch) 508 char *ch; 509 { 510 errx(1, "%%%s: bad conversion character", ch); 511 } 512