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