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 $
33 #include <sys/types.h>
43 FU
*endfu
; /* format at end-of-data */
53 if ((fp
= fopen(name
, "r")) == NULL
)
55 while (fgets(buf
, sizeof(buf
), fp
)) {
56 if (!(p
= strchr(buf
, '\n'))) {
57 warnx("line too long");
58 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
62 for (p
= buf
; *p
&& isspace(*p
); ++p
);
73 unsigned const char *p
, *savep
;
78 /* start new linked list of format units */
79 if ((tfs
= calloc(1, sizeof(FS
))) == NULL
)
85 nextfs
= &tfs
->nextfs
;
86 nextfu
= &tfs
->nextfu
;
88 /* take the format string and break it up into format units */
90 /* skip leading white space */
91 for (; isspace(*p
); ++p
);
95 /* allocate a new format unit and link it in */
96 if ((tfu
= calloc(1, sizeof(FU
))) == NULL
)
99 nextfu
= &tfu
->nextfu
;
102 /* if leading digit, repetition count */
104 for (savep
= p
; isdigit(*p
); ++p
);
105 if (!isspace(*p
) && *p
!= '/')
107 /* may overwrite either white space or slash */
108 tfu
->reps
= atoi(savep
);
109 tfu
->flags
= F_SETREP
;
110 /* skip trailing white space */
111 for (++p
; isspace(*p
); ++p
);
114 /* skip slash and trailing white space */
116 while (isspace(*++p
));
120 for (savep
= p
; isdigit(*p
); ++p
);
123 tfu
->bcnt
= atoi(savep
);
124 /* skip trailing white space */
125 for (++p
; isspace(*p
); ++p
);
131 for (savep
= ++p
; *p
!= '"';)
134 if (!(tfu
->fmt
= malloc(p
- savep
+ 1)))
136 (void) strncpy(tfu
->fmt
, savep
, p
- savep
);
137 tfu
->fmt
[p
- savep
] = '\0';
143 static const char *spec
= ".#-+ 0123456789";
153 /* figure out the data block size needed for each format unit */
154 for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
156 cursize
+= fu
->bcnt
* fu
->reps
;
159 for (bcnt
= prec
= 0, fmt
= fu
->fmt
; *fmt
; ++fmt
) {
163 * skip any special chars -- save precision in
164 * case it's a %s format.
166 while (strchr(spec
+ 1, *++fmt
));
167 if (*fmt
== '.' && isdigit(*++fmt
)) {
169 while (isdigit(*++fmt
));
175 case 'd': case 'i': case 'o': case 'u':
179 case 'e': case 'E': case 'f': case 'g': case 'G':
187 case 'c': case 'p': case 'u':
193 cursize
+= bcnt
* fu
->reps
;
201 enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
202 PR
*pr
, **nextpr
= NULL
;
204 unsigned char *p1
, *p2
, *fmtp
;
208 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
210 * Break each format unit into print units; each conversion
211 * character gets its own.
213 for (nconv
= 0, fmtp
= fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
214 if ((pr
= calloc(1, sizeof(PR
))) == NULL
)
221 /* Skip preceding text and up to the next % sign. */
222 for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
224 /* Only text in the string. */
232 * Get precision for %s -- if have a byte count, don't
237 /* Skip to conversion character. */
238 for (++p1
; strchr(spec
, *p1
); ++p1
);
240 /* Skip any special chars, field width. */
241 while (strchr(spec
+ 1, *++p1
));
242 if (*p1
== '.' && isdigit(*++p1
)) {
245 while (isdigit(*++p1
));
250 p2
= p1
+ 1; /* Set end pointer. */
251 cs
[0] = *p1
; /* Set conversion string. */
255 * Figure out the byte count for each conversion;
256 * rewrite the format as necessary, set up blank-
257 * padding for end of data.
274 case 'o': case 'u': case 'x': case 'X':
294 case 'e': case 'E': case 'f': case 'g': case 'G':
304 if (fu
->bcnt
== sizeof(long double)) {
308 pr
->bcnt
= sizeof(long double);
333 fu
->flags
|= F_IGNORE
;
336 pr
->flags
= F_ADDRESS
;
339 case 'd': case 'o': case'x':
351 /* cs[0] = 'c'; set in conv_c */
359 /* cs[0] = 'c'; set in conv_u */
360 isint2
: switch(fu
->bcnt
) {
380 * Copy to PR format string, set conversion character
381 * pointer, update original.
385 if ((pr
->fmt
= calloc(1, strlen(fmtp
) + 2)) == NULL
)
387 (void)strcpy(pr
->fmt
, fmtp
);
388 (void)strcat(pr
->fmt
, cs
);
390 pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
393 /* Only one conversion character if byte count. */
394 if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++) {
395 errx(1, "byte count with multiple "
396 "conversion characters");
400 * If format unit byte count not specified, figure it out
401 * so can adjust rep count later.
404 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
405 fu
->bcnt
+= pr
->bcnt
;
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.
413 * If, rep count is greater than 1, no trailing whitespace
414 * gets output from the last iteration of the format unit.
416 for (fu
= fs
->nextfu
; fu
; 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
;
421 for (pr
= fu
->nextpr
;; pr
= pr
->nextpr
)
424 for (p1
= pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
425 p2
= isspace(*p1
) ? p1
: NULL
;
431 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
432 (void)printf("fmt:");
433 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
434 (void)printf(" {%s}", pr
->fmt
);
445 /* alphabetic escape sequences have to be done in place */
446 for (p2
= p1
;; ++p1
, ++p2
) {
485 errx(1, "%s: bad byte count", s
);
491 errx(1, "%%s: requires a precision or a byte count");
495 badfmt(const char *fmt
)
497 errx(1, "\"%s\": bad format", fmt
);
503 errx(1, "%%%s: bad conversion character", ch
);