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