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