1 /* $NetBSD: parse.c,v 1.26 2009/01/18 21:34:32 apb Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: parse.c,v 1.26 2009/01/18 21:34:32 apb Exp $");
47 #include <sys/types.h>
62 FU
*endfu
; /* format at end-of-data */
72 if ((fp
= fopen(name
, "r")) == NULL
)
73 err(1, "fopen %s", name
);
74 while (fgets(buf
, sizeof(buf
), fp
)) {
75 if (!(p
= strchr(buf
, '\n'))) {
76 warnx("line too long.");
77 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
81 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
);
98 /* start new linked list of format units */
99 tfs
= ecalloc(1, sizeof(FS
));
104 nextfs
= &tfs
->nextfs
;
105 nextfu
= &tfs
->nextfu
;
107 /* take the format string and break it up into format units */
109 /* skip leading white space */
110 for (; isspace((unsigned char)*p
); ++p
);
114 /* allocate a new format unit and link it in */
115 tfu
= ecalloc(1, sizeof(FU
));
117 nextfu
= &tfu
->nextfu
;
120 /* if leading digit, repetition count */
121 if (isdigit((unsigned char)*p
)) {
122 for (savep
= p
; isdigit((unsigned char)*p
); ++p
);
123 if (!isspace((unsigned char)*p
) && *p
!= '/')
125 /* may overwrite either white space or slash */
126 tfu
->reps
= atoi(savep
);
127 tfu
->flags
= F_SETREP
;
128 /* skip trailing white space */
129 for (++p
; isspace((unsigned char)*p
); ++p
);
132 /* skip slash and trailing white space */
134 while (isspace((unsigned char)*++p
));
137 if (isdigit((unsigned char)*p
)) {
138 for (savep
= p
; isdigit((unsigned char)*p
); ++p
);
139 if (!isspace((unsigned char)*p
))
141 tfu
->bcnt
= atoi(savep
);
142 /* skip trailing white space */
143 for (++p
; isspace((unsigned char)*p
); ++p
);
149 for (savep
= ++p
; *p
!= '"';)
152 tfu
->fmt
= emalloc(p
- savep
+ 1);
153 (void) strncpy(tfu
->fmt
, savep
, p
- savep
);
154 tfu
->fmt
[p
- savep
] = '\0';
160 static const char *spec
= ".#-+ 0123456789";
170 /* figure out the data block size needed for each format unit */
171 for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
173 cursize
+= fu
->bcnt
* fu
->reps
;
176 for (bcnt
= prec
= 0, fmt
= fu
->fmt
; *fmt
; ++fmt
) {
180 * skip any special chars -- save precision in
181 * case it's a %s format.
183 while (strchr(spec
+ 1, *++fmt
));
184 if (*fmt
== '.' && isdigit((unsigned char)*++fmt
)) {
186 while (isdigit((unsigned char)*++fmt
));
192 case 'd': case 'i': case 'o': case 'u':
196 case 'e': case 'E': case 'f': case 'g': case 'G':
204 case 'c': case 'p': case 'u':
210 cursize
+= bcnt
* fu
->reps
;
218 enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
222 char savech
, *fmtp
, cs
[sizeof(PRId64
)];
226 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
228 * Break each format unit into print units; each conversion
229 * character gets its own.
231 nextpr
= &fu
->nextpr
;
232 for (nconv
= 0, fmtp
= fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
233 pr
= ecalloc(1, sizeof(*pr
));
236 /* Skip preceding text and up to the next % sign. */
237 for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
239 /* Only text in the string. */
247 * Get precision for %s -- if have a byte count, don't
252 /* Skip to conversion character. */
253 for (++p1
; *p1
&& strchr(spec
, *p1
); ++p1
);
255 /* Skip any special chars, field width. */
256 while (*++p1
&& strchr(spec
+ 1, *p1
));
258 isdigit((unsigned char)*++p1
)) {
261 while (isdigit((unsigned char)*++p1
))
267 p2
= *p1
? p1
+ 1 : p1
; /* Set end pointer. */
268 cs
[0] = *p1
; /* Set conversion string. */
272 * Figure out the byte count for each conversion;
273 * rewrite the format as necessary, set up blank-
274 * padding for end of data.
291 case 'o': case 'u': case 'x': case 'X':
295 * Regardless of pr->bcnt, all integer
296 * values are cast to [u]int64_t before
297 * being printed by display(). We
298 * therefore need to use PRI?64 as the
299 * format, where '?' could actually
300 * be any of [diouxX]. We make the
301 * assumption (not guaranteed by the
302 * C99 standard) that we can derive
303 * all the other PRI?64 values from
304 * PRId64 simply by changing the last
305 * character. For example, if PRId64 is
306 * "lld" or "qd", and cs[0] is 'o', then
307 * we end up with "llo" or "qo".
310 strncpy(cs
, PRId64
, sizeof(PRId64
) - 2);
311 cs
[sizeof(PRId64
) - 2] = savech
;
312 cs
[sizeof(PRId64
) - 1] = '\0';
331 case 'e': case 'E': case 'f': case 'g': case 'G':
363 fu
->flags
|= F_IGNORE
;
366 pr
->flags
= F_ADDRESS
;
369 case 'd': case 'o': case'x':
371 * See comments above for
372 * the way we use PRId64.
376 cs
[sizeof(PRId64
) - 2] = p1
[2];
377 cs
[sizeof(PRId64
) - 1] = '\0';
386 /* cs[0] = 'c'; set in conv_c */
394 /* cs[0] = 'c'; set in conv_u */
395 isint2
: switch(fu
->bcnt
) {
415 * Copy to PR format string, set conversion character
416 * pointer, update original.
420 pr
->fmt
= emalloc(strlen(fmtp
) + strlen(cs
) + 1);
421 (void)strcpy(pr
->fmt
, fmtp
);
422 (void)strcat(pr
->fmt
, cs
);
424 pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
427 /* Only one conversion character if byte count. */
428 if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++)
430 "byte count with multiple conversion characters");
433 * If format unit byte count not specified, figure it out
434 * so can adjust rep count later.
437 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
438 fu
->bcnt
+= pr
->bcnt
;
441 * If the format string interprets any data at all, and it's
442 * not the same as the blocksize, and its last format unit
443 * interprets any data at all, and has no iteration count,
444 * repeat it as necessary.
446 * If, rep count is greater than 1, no trailing whitespace
447 * gets output from the last iteration of the format unit.
449 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
450 if (!fu
->nextfu
&& fs
->bcnt
< blocksize
&&
451 !(fu
->flags
&F_SETREP
) && fu
->bcnt
)
452 fu
->reps
+= (blocksize
- fs
->bcnt
) / fu
->bcnt
;
456 for (pr
= fu
->nextpr
;; pr
= pr
->nextpr
)
459 for (p1
= pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
460 p2
= isspace((unsigned char)*p1
) ? p1
: NULL
;
466 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
467 (void)printf("fmt:");
468 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
469 (void)printf(" {%s}", pr
->fmt
);
480 /* alphabetic escape sequences have to be done in place */
481 for (p2
= p1
;; ++p1
, ++p2
) {
491 return; /* incomplete escape sequence */
526 errx(1, "%s: bad byte count", s
);
532 errx(1, "%%s: requires a precision or a byte count");
536 badfmt(const char *fmt
)
538 errx(1, "\"%s\": bad format", fmt
);
544 errx(1, "%%%s: bad conversion character", ch
);