1 /* $NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg 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>
39 static char sccsid
[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg Exp $");
45 #include <sys/types.h>
60 __dead
static void badcnt(char *);
61 __dead
static void badconv(char *);
62 __dead
static void badfmt(const char *);
63 __dead
static void badsfmt(void);
65 FU
*endfu
; /* format at end-of-data */
75 if ((fp
= fopen(name
, "r")) == NULL
)
76 err(1, "fopen %s", name
);
77 while (fgets(buf
, sizeof(buf
), fp
)) {
78 if (!(p
= strchr(buf
, '\n'))) {
79 warnx("line too long.");
80 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
84 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
);
101 /* start new linked list of format units */
102 tfs
= ecalloc(1, sizeof(FS
));
107 nextfs
= &tfs
->nextfs
;
108 nextfu
= &tfs
->nextfu
;
110 /* take the format string and break it up into format units */
112 /* skip leading white space */
113 for (; isspace((unsigned char)*p
); ++p
);
117 /* allocate a new format unit and link it in */
118 tfu
= ecalloc(1, sizeof(FU
));
120 nextfu
= &tfu
->nextfu
;
123 /* if leading digit, repetition count */
124 if (isdigit((unsigned char)*p
)) {
125 for (savep
= p
; isdigit((unsigned char)*p
); ++p
);
126 if (!isspace((unsigned char)*p
) && *p
!= '/')
128 /* may overwrite either white space or slash */
129 tfu
->reps
= atoi(savep
);
130 tfu
->flags
= F_SETREP
;
131 /* skip trailing white space */
132 for (++p
; isspace((unsigned char)*p
); ++p
);
135 /* skip slash and trailing white space */
137 while (isspace((unsigned char)*++p
));
140 if (isdigit((unsigned char)*p
)) {
141 for (savep
= p
; isdigit((unsigned char)*p
); ++p
);
142 if (!isspace((unsigned char)*p
))
144 tfu
->bcnt
= atoi(savep
);
145 /* skip trailing white space */
146 for (++p
; isspace((unsigned char)*p
); ++p
);
152 for (savep
= ++p
; *p
!= '"';)
155 tfu
->fmt
= emalloc(p
- savep
+ 1);
156 (void) strncpy(tfu
->fmt
, savep
, p
- savep
);
157 tfu
->fmt
[p
- savep
] = '\0';
163 static const char *spec
= ".#-+ 0123456789";
173 /* figure out the data block size needed for each format unit */
174 for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
176 cursize
+= fu
->bcnt
* fu
->reps
;
179 for (bcnt
= prec
= 0, fmt
= fu
->fmt
; *fmt
; ++fmt
) {
183 * skip any special chars -- save precision in
184 * case it's a %s format.
186 while (strchr(spec
+ 1, *++fmt
));
187 if (*fmt
== '.' && isdigit((unsigned char)*++fmt
)) {
189 while (isdigit((unsigned char)*++fmt
));
195 case 'd': case 'i': case 'o': case 'u':
199 case 'e': case 'E': case 'f': case 'g': case 'G':
207 case 'c': case 'p': case 'u':
213 cursize
+= bcnt
* fu
->reps
;
221 enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
225 char savech
, *fmtp
, cs
[sizeof(PRId64
)];
229 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
231 * Break each format unit into print units; each conversion
232 * character gets its own.
234 nextpr
= &fu
->nextpr
;
235 for (nconv
= 0, fmtp
= fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
236 pr
= ecalloc(1, sizeof(*pr
));
239 /* Skip preceding text and up to the next % sign. */
240 for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
242 /* Only text in the string. */
250 * Get precision for %s -- if have a byte count, don't
255 /* Skip to conversion character. */
256 for (++p1
; *p1
&& strchr(spec
, *p1
); ++p1
);
258 /* Skip any special chars, field width. */
259 while (*++p1
&& strchr(spec
+ 1, *p1
));
261 isdigit((unsigned char)*++p1
)) {
264 while (isdigit((unsigned char)*++p1
))
270 p2
= *p1
? p1
+ 1 : p1
; /* Set end pointer. */
271 cs
[0] = *p1
; /* Set conversion string. */
275 * Figure out the byte count for each conversion;
276 * rewrite the format as necessary, set up blank-
277 * padding for end of data.
294 case 'o': case 'u': case 'x': case 'X':
298 * Regardless of pr->bcnt, all integer
299 * values are cast to [u]int64_t before
300 * being printed by display(). We
301 * therefore need to use PRI?64 as the
302 * format, where '?' could actually
303 * be any of [diouxX]. We make the
304 * assumption (not guaranteed by the
305 * C99 standard) that we can derive
306 * all the other PRI?64 values from
307 * PRId64 simply by changing the last
308 * character. For example, if PRId64 is
309 * "lld" or "qd", and cs[0] is 'o', then
310 * we end up with "llo" or "qo".
313 strncpy(cs
, PRId64
, sizeof(PRId64
) - 2);
314 cs
[sizeof(PRId64
) - 2] = savech
;
315 cs
[sizeof(PRId64
) - 1] = '\0';
334 case 'e': case 'E': case 'f': case 'g': case 'G':
366 fu
->flags
|= F_IGNORE
;
369 pr
->flags
= F_ADDRESS
;
372 case 'd': case 'o': case'x':
374 * See comments above for
375 * the way we use PRId64.
379 cs
[sizeof(PRId64
) - 2] = p1
[2];
380 cs
[sizeof(PRId64
) - 1] = '\0';
389 /* cs[0] = 'c'; set in conv_c */
397 /* cs[0] = 'c'; set in conv_u */
398 isint2
: switch(fu
->bcnt
) {
418 * Copy to PR format string, set conversion character
419 * pointer, update original.
423 pr
->fmt
= emalloc(strlen(fmtp
) + strlen(cs
) + 1);
424 (void)strcpy(pr
->fmt
, fmtp
);
425 (void)strcat(pr
->fmt
, cs
);
427 pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
430 /* Only one conversion character if byte count. */
431 if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++)
433 "byte count with multiple conversion characters");
436 * If format unit byte count not specified, figure it out
437 * so can adjust rep count later.
440 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
441 fu
->bcnt
+= pr
->bcnt
;
444 * If the format string interprets any data at all, and it's
445 * not the same as the blocksize, and its last format unit
446 * interprets any data at all, and has no iteration count,
447 * repeat it as necessary.
449 * If, rep count is greater than 1, no trailing whitespace
450 * gets output from the last iteration of the format unit.
452 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
453 if (!fu
->nextfu
&& fs
->bcnt
< blocksize
&&
454 !(fu
->flags
&F_SETREP
) && fu
->bcnt
)
455 fu
->reps
+= (blocksize
- fs
->bcnt
) / fu
->bcnt
;
459 for (pr
= fu
->nextpr
;; pr
= pr
->nextpr
)
462 for (p1
= pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
463 p2
= isspace((unsigned char)*p1
) ? p1
: NULL
;
469 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
470 (void)printf("fmt:");
471 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
472 (void)printf(" {%s}", pr
->fmt
);
483 /* alphabetic escape sequences have to be done in place */
484 for (p2
= p1
;; ++p1
, ++p2
) {
494 return; /* incomplete escape sequence */
529 errx(1, "%s: bad byte count", s
);
535 errx(1, "%%s: requires a precision or a byte count");
539 badfmt(const char *fmt
)
541 errx(1, "\"%s\": bad format", fmt
);
547 errx(1, "%%%s: bad conversion character", ch
);