* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)parse.c 8.1 (Berkeley) %G%";
FU
*endfu
; /* format at end-of-data */
if ((fp
= fopen(name
, "r")) == NULL
)
err("%s: %s\n", name
, strerror(errno
));
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
= 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
= 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
;
char savech
, *fmtp
, cs
[3];
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
= 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. */
cs
[0] = *p1
; /* Set conversion string. */
* 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 'e': case 'E': case 'f': case 'g': case 'G':
case 'd': case 'o': case'x':
/* cs[0] = 'c'; set in conv_c */
/* cs[0] = 'c'; set in conv_u */
isint2
: switch(fu
->bcnt
) {
* Copy to PR format string, set conversion character
* pointer, update original.
pr
->fmt
= emalloc(strlen(fmtp
) + 2);
(void)strcpy(pr
->fmt
, fmtp
);
(void)strcat(pr
->fmt
, cs
);
pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
/* Only one conversion character if byte count. */
if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++)
err("byte count with multiple conversion characters");
* 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
;
for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
(void)printf(" {%s}", pr
->fmt
);
/* alphabetic escape sequences have to be done in place */
for (p2
= p1
;; ++p1
, ++p2
) {
err("%s: bad byte count", s
);
err("%%s: requires a precision or a byte count\n");
err("\"%s\": bad format\n", fmt
);
err("%%%s: bad conversion character\n", ch
);