* Copyright (c) 1989 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)parse.c 5.6 (Berkeley) 3/9/91";
FU
*endfu
; /* format at end-of-data */
if (!(fp
= fopen(name
, "r"))) {
(void)fprintf(stderr
, "hexdump: can't read %s.\n", name
);
while (fgets(buf
, sizeof(buf
), fp
)) {
if (!(p
= index(buf
, '\n'))) {
(void)fprintf(stderr
, "hexdump: line too long.\n");
while ((ch
= getchar()) != '\n' && ch
!= EOF
);
for (p
= buf
; *p
&& isspace(*p
); ++p
);
/* start new linked list of format units */
tfs
= (FS
*)emalloc(sizeof(FS
));
/* take the format string and break it up into format units */
/* skip leading white space */
for (; isspace(*p
); ++p
);
/* allocate a new format unit and link it in */
tfu
= (FU
*)emalloc(sizeof(FU
));
/* if leading digit, repetition count */
for (savep
= p
; isdigit(*p
); ++p
);
if (!isspace(*p
) && *p
!= '/')
/* may overwrite either white space or slash */
/* skip trailing white space */
for (++p
; isspace(*p
); ++p
);
/* skip slash and trailing white space */
for (savep
= p
; isdigit(*p
); ++p
);
/* skip trailing white space */
for (++p
; isspace(*p
); ++p
);
for (savep
= ++p
; *p
!= '"';)
if (!(tfu
->fmt
= malloc(p
- savep
+ 1)))
(void) strncpy(tfu
->fmt
, savep
, p
- savep
);
tfu
->fmt
[p
- savep
] = '\0';
static char *spec
= ".#-+ 0123456789";
register int bcnt
, cursize
;
/* figure out the data block size needed for each format unit */
for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
cursize
+= fu
->bcnt
* fu
->reps
;
for (bcnt
= prec
= 0, fmt
= fu
->fmt
; *fmt
; ++fmt
) {
* skip any special chars -- save precision in
while (index(spec
+ 1, *++fmt
));
if (*fmt
== '.' && isdigit(*++fmt
)) {
case 'd': case 'i': case 'o': case 'u':
case 'e': case 'E': case 'f': case 'g': case 'G':
case 'c': case 'p': case 'u':
cursize
+= bcnt
* fu
->reps
;
enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
register PR
*pr
, **nextpr
;
for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
* break each format unit into print units; each
* conversion character gets its own.
for (nconv
= 0, fmtp
= fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
pr
= (PR
*)emalloc(sizeof(PR
));
/* skip preceding text and up to the next % sign */
for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
/* only text in the string */
* get precision for %s -- if have a byte count, don't
/* skip to conversion character */
for (++p1
; index(spec
, *p1
); ++p1
);
/* skip any special chars, field width */
while (index(spec
+ 1, *++p1
));
if (*p1
== '.' && isdigit(*++p1
)) {
p2
= p1
+ 1; /* set end pointer */
* figure out the byte count for each conversion;
* rewrite the format as necessary, set up blank-
* padding for end of data.
case 'o': case 'u': case 'x': case 'X':
case 'o': case 'u': case 'x': case 'X':
case 'e': case 'E': case 'f': case 'g': case 'G':
case 'd': case 'o': case'x':
/* *p1 = 'c'; set in conv_c */
/* *p1 = 'c'; set in conv_u */
* copy to PR format string, set conversion character
* pointer, update original.
if (!(pr
->fmt
= strdup(fmtp
)))
pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
/* only one conversion character if byte count */
if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++) {
"hexdump: byte count with multiple conversion characters.\n");
* if format unit byte count not specified, figure it out
* so can adjust rep count later.
for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
* if the format string interprets any data at all, and it's
* not the same as the blocksize, and its last format unit
* interprets any data at all, and has no iteration count,
* repeat it as necessary.
* if, rep count is greater than 1, no trailing whitespace
* gets output from the last iteration of the format unit.
for (fu
= fs
->nextfu
;; fu
= fu
->nextfu
) {
if (!fu
->nextfu
&& fs
->bcnt
< blocksize
&&
!(fu
->flags
&F_SETREP
) && fu
->bcnt
)
fu
->reps
+= (blocksize
- fs
->bcnt
) / fu
->bcnt
;
for (pr
= fu
->nextpr
;; pr
= pr
->nextpr
)
for (p1
= pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
p2
= isspace(*p1
) ? p1
: NULL
;
/* alphabetic escape sequences have to be done in place */
for (p2
= p1
;; ++p1
, ++p2
) {
"hexdump: bad byte count for conversion character %s.\n", s
);
"hexdump: %%s requires a precision or a byte count.\n");
(void)fprintf(stderr
, "hexdump: bad format {%s}\n", fmt
);
(void)fprintf(stderr
, "hexdump: bad conversion character %%%s.\n", ch
);