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