2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)parse.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/hexdump/parse.c,v 1.4.2.1 2002/07/23 14:27:06 tjr Exp $
31 * $DragonFly: src/usr.bin/hexdump/parse.c,v 1.6 2005/04/10 20:55:38 drhodus Exp $
34 #include <sys/types.h>
44 FU *endfu; /* format at end-of-data */
54 if ((fp = fopen(name, "r")) == NULL)
56 while (fgets(buf, sizeof(buf), fp)) {
57 if (!(p = strchr(buf, '\n'))) {
58 warnx("line too long");
59 while ((ch = getchar()) != '\n' && ch != EOF);
63 for (p = buf; *p && isspace(*p); ++p);
74 unsigned const char *p, *savep;
79 /* start new linked list of format units */
80 if ((tfs = calloc(1, sizeof(FS))) == NULL)
86 nextfs = &tfs->nextfs;
87 nextfu = &tfs->nextfu;
89 /* take the format string and break it up into format units */
91 /* skip leading white space */
92 for (; isspace(*p); ++p);
96 /* allocate a new format unit and link it in */
97 if ((tfu = calloc(1, sizeof(FU))) == NULL)
100 nextfu = &tfu->nextfu;
103 /* if leading digit, repetition count */
105 for (savep = p; isdigit(*p); ++p);
106 if (!isspace(*p) && *p != '/')
108 /* may overwrite either white space or slash */
109 tfu->reps = atoi(savep);
110 tfu->flags = F_SETREP;
111 /* skip trailing white space */
112 for (++p; isspace(*p); ++p);
115 /* skip slash and trailing white space */
117 while (isspace(*++p));
121 for (savep = p; isdigit(*p); ++p);
124 tfu->bcnt = atoi(savep);
125 /* skip trailing white space */
126 for (++p; isspace(*p); ++p);
132 for (savep = ++p; *p != '"';)
135 if (!(tfu->fmt = malloc(p - savep + 1)))
137 (void) strncpy(tfu->fmt, savep, p - savep);
138 tfu->fmt[p - savep] = '\0';
144 static const char *spec = ".#-+ 0123456789";
154 /* figure out the data block size needed for each format unit */
155 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
157 cursize += fu->bcnt * fu->reps;
160 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
164 * skip any special chars -- save precision in
165 * case it's a %s format.
167 while (strchr(spec + 1, *++fmt));
168 if (*fmt == '.' && isdigit(*++fmt)) {
170 while (isdigit(*++fmt));
176 case 'd': case 'i': case 'o': case 'u':
180 case 'e': case 'E': case 'f': case 'g': case 'G':
188 case 'c': case 'p': case 'u':
194 cursize += bcnt * fu->reps;
202 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
203 PR *pr, **nextpr = NULL;
205 unsigned char *p1, *p2, *fmtp;
209 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
211 * Break each format unit into print units; each conversion
212 * character gets its own.
214 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
215 if ((pr = calloc(1, sizeof(PR))) == NULL)
222 /* Skip preceding text and up to the next % sign. */
223 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
225 /* Only text in the string. */
233 * Get precision for %s -- if have a byte count, don't
238 /* Skip to conversion character. */
239 for (++p1; strchr(spec, *p1); ++p1);
241 /* Skip any special chars, field width. */
242 while (strchr(spec + 1, *++p1));
243 if (*p1 == '.' && isdigit(*++p1)) {
246 while (isdigit(*++p1));
251 p2 = p1 + 1; /* Set end pointer. */
252 cs[0] = *p1; /* Set conversion string. */
256 * Figure out the byte count for each conversion;
257 * rewrite the format as necessary, set up blank-
258 * padding for end of data.
275 case 'o': case 'u': case 'x': case 'X':
295 case 'e': case 'E': case 'f': case 'g': case 'G':
305 if (fu->bcnt == sizeof(long double)) {
309 pr->bcnt = sizeof(long double);
334 fu->flags |= F_IGNORE;
337 pr->flags = F_ADDRESS;
340 case 'd': case 'o': case'x':
352 /* cs[0] = 'c'; set in conv_c */
360 /* cs[0] = 'c'; set in conv_u */
361 isint2: switch(fu->bcnt) {
381 * Copy to PR format string, set conversion character
382 * pointer, update original.
386 if ((pr->fmt = calloc(1, strlen(fmtp) + 2)) == NULL)
388 (void)strcpy(pr->fmt, fmtp);
389 (void)strcat(pr->fmt, cs);
391 pr->cchar = pr->fmt + (p1 - fmtp);
394 /* Only one conversion character if byte count. */
395 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
396 errx(1, "byte count with multiple conversion characters");
399 * If format unit byte count not specified, figure it out
400 * so can adjust rep count later.
403 for (pr = fu->nextpr; pr; pr = pr->nextpr)
404 fu->bcnt += pr->bcnt;
407 * If the format string interprets any data at all, and it's
408 * not the same as the blocksize, and its last format unit
409 * interprets any data at all, and has no iteration count,
410 * repeat it as necessary.
412 * If, rep count is greater than 1, no trailing whitespace
413 * gets output from the last iteration of the format unit.
415 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
416 if (!fu->nextfu && fs->bcnt < blocksize &&
417 !(fu->flags&F_SETREP) && fu->bcnt)
418 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
420 for (pr = fu->nextpr;; pr = pr->nextpr)
423 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
424 p2 = isspace(*p1) ? p1 : NULL;
430 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
431 (void)printf("fmt:");
432 for (pr = fu->nextpr; pr; pr = pr->nextpr)
433 (void)printf(" {%s}", pr->fmt);
444 /* alphabetic escape sequences have to be done in place */
445 for (p2 = p1;; ++p1, ++p2) {
484 errx(1, "%s: bad byte count", s);
490 errx(1, "%%s: requires a precision or a byte count");
494 badfmt(const char *fmt)
496 errx(1, "\"%s\": bad format", fmt);
502 errx(1, "%%%s: bad conversion character", ch);