]>
git.saurik.com Git - apple/shell_cmds.git/blob - hexdump/parse.c
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/hexdump/parse.c,v 1.12 2002/09/04 23:29:01 dwmalone Exp $");
42 #include <sys/types.h>
52 FU
*endfu
; /* format at end-of-data */
62 if ((fp
= fopen(name
, "r")) == NULL
)
64 while (fgets(buf
, sizeof(buf
), fp
)) {
65 if (!(p
= (unsigned char *)index(buf
, '\n'))) {
66 warnx("line too long");
67 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
71 for (p
= (unsigned char *)buf
; *p
&& isspace(*p
); ++p
);
82 unsigned const char *p
, *savep
;
87 /* start new linked list of format units */
88 if ((tfs
= calloc(1, sizeof(FS
))) == NULL
)
94 nextfs
= &tfs
->nextfs
;
95 nextfu
= &tfs
->nextfu
;
97 /* take the format string and break it up into format units */
98 for (p
= (unsigned const char *)fmt
;;) {
99 /* skip leading white space */
100 for (; isspace(*p
); ++p
);
104 /* allocate a new format unit and link it in */
105 if ((tfu
= calloc(1, sizeof(FU
))) == NULL
)
108 nextfu
= &tfu
->nextfu
;
111 /* if leading digit, repetition count */
113 for (savep
= p
; isdigit(*p
); ++p
);
114 if (!isspace(*p
) && *p
!= '/')
116 /* may overwrite either white space or slash */
117 tfu
->reps
= atoi((const char *)savep
);
118 tfu
->flags
= F_SETREP
;
119 /* skip trailing white space */
120 for (++p
; isspace(*p
); ++p
);
123 /* skip slash and trailing white space */
125 while (isspace(*++p
));
129 for (savep
= p
; isdigit(*p
); ++p
);
132 tfu
->bcnt
= atoi((const char *)savep
);
133 /* skip trailing white space */
134 for (++p
; isspace(*p
); ++p
);
140 for (savep
= ++p
; *p
!= '"';)
143 if (!(tfu
->fmt
= malloc(p
- savep
+ 1)))
145 (void) strncpy(tfu
->fmt
, (const char *)savep
, p
- savep
);
146 tfu
->fmt
[p
- savep
] = '\0';
152 static const char *spec
= ".#-+ 0123456789";
162 /* figure out the data block size needed for each format unit */
163 for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
165 cursize
+= fu
->bcnt
* fu
->reps
;
168 for (bcnt
= prec
= 0, fmt
= (unsigned char *)fu
->fmt
; *fmt
; ++fmt
) {
172 * skip any special chars -- save precision in
173 * case it's a %s format.
175 while (index(spec
+ 1, *++fmt
));
176 if (*fmt
== '.' && isdigit(*++fmt
)) {
177 prec
= atoi((const char *)fmt
);
178 while (isdigit(*++fmt
));
184 case 'd': case 'i': case 'o': case 'u':
188 case 'e': case 'E': case 'f': case 'g': case 'G':
196 case 'c': case 'p': case 'u':
202 cursize
+= bcnt
* fu
->reps
;
210 enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
211 PR
*pr
, **nextpr
= NULL
;
213 unsigned char *p1
, *p2
, *fmtp
;
217 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
219 * Break each format unit into print units; each conversion
220 * character gets its own.
222 for (nconv
= 0, fmtp
= (unsigned char *)fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
223 if ((pr
= calloc(1, sizeof(PR
))) == NULL
)
230 /* Skip preceding text and up to the next % sign. */
231 for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
233 /* Only text in the string. */
235 pr
->fmt
= (char *)fmtp
;
241 * Get precision for %s -- if have a byte count, don't
246 /* Skip to conversion character. */
247 for (++p1
; index(spec
, *p1
); ++p1
);
249 /* Skip any special chars, field width. */
250 while (index(spec
+ 1, *++p1
));
251 if (*p1
== '.' && isdigit(*++p1
)) {
253 prec
= atoi((const char *)p1
);
254 while (isdigit(*++p1
));
259 p2
= p1
+ 1; /* Set end pointer. */
260 cs
[0] = *p1
; /* Set conversion string. */
264 * Figure out the byte count for each conversion;
265 * rewrite the format as necessary, set up blank-
266 * padding for end of data.
283 case 'o': case 'u': case 'x': case 'X':
302 #endif /* __APPLE__ */
308 case 'e': case 'E': case 'f': case 'g': case 'G':
318 if (fu
->bcnt
== sizeof(long double)) {
322 pr
->bcnt
= sizeof(long double);
347 fu
->flags
|= F_IGNORE
;
350 pr
->flags
= F_ADDRESS
;
353 case 'd': case 'o': case'x':
365 /* cs[0] = 'c'; set in conv_c */
373 /* cs[0] = 'c'; set in conv_u */
374 isint2
: switch(fu
->bcnt
) {
383 case 'n': /* Force -A n to dump extra blank line like default od behavior */
385 fu
->flags
= F_IGNORE
;
387 fmtp
= (unsigned char *)"\n";
401 * Copy to PR format string, set conversion character
402 * pointer, update original.
406 if ((pr
->fmt
= calloc(1, strlen((const char *)fmtp
) + 2)) == NULL
)
408 (void)strcpy(pr
->fmt
, (const char *)fmtp
);
409 (void)strcat(pr
->fmt
, cs
);
411 pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
414 /* Only one conversion character if byte count. */
415 if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++)
416 errx(1, "byte count with multiple conversion characters");
419 * If format unit byte count not specified, figure it out
420 * so can adjust rep count later.
423 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
424 fu
->bcnt
+= pr
->bcnt
;
427 * If the format string interprets any data at all, and it's
428 * not the same as the blocksize, and its last format unit
429 * interprets any data at all, and has no iteration count,
430 * repeat it as necessary.
432 * If, rep count is greater than 1, no trailing whitespace
433 * gets output from the last iteration of the format unit.
435 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
436 if (!fu
->nextfu
&& fs
->bcnt
< blocksize
&&
437 !(fu
->flags
&F_SETREP
) && fu
->bcnt
)
438 fu
->reps
+= (blocksize
- fs
->bcnt
) / fu
->bcnt
;
440 for (pr
= fu
->nextpr
;; pr
= pr
->nextpr
)
443 for (p1
= (unsigned char *)pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
444 p2
= isspace(*p1
) ? p1
: NULL
;
446 pr
->nospace
= (char *)p2
;
450 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
451 (void)printf("fmt:");
452 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
453 (void)printf(" {%s}", pr
->fmt
);
464 /* alphabetic escape sequences have to be done in place */
465 for (p2
= p1
;; ++p1
, ++p2
) {
504 errx(1, "%s: bad byte count", s
);
510 errx(1, "%%s: requires a precision or a byte count");
514 badfmt(const char *fmt
)
516 errx(1, "\"%s\": bad format", fmt
);
522 errx(1, "%%%s: bad conversion character", ch
);