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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
48 FU *endfu; /* format at end-of-data */
51 addfile(const char *name)
58 if ((fp = fopen(name, "r")) == NULL)
60 while (fgets(buf, sizeof(buf), fp)) {
61 if (!(p = strchr(buf, '\n'))) {
62 warnx("line too long");
63 while ((ch = getchar()) != '\n' && ch != EOF);
67 for (p = buf; *p && isspace(*p); ++p);
78 unsigned const char *p, *savep;
83 /* start new linked list of format units */
84 if ((tfs = calloc(1, sizeof(FS))) == NULL)
90 nextfs = &tfs->nextfs;
91 nextfu = &tfs->nextfu;
93 /* take the format string and break it up into format units */
95 /* skip leading white space */
96 for (; isspace(*p); ++p);
100 /* allocate a new format unit and link it in */
101 if ((tfu = calloc(1, sizeof(FU))) == NULL)
104 nextfu = &tfu->nextfu;
107 /* if leading digit, repetition count */
109 for (savep = p; isdigit(*p); ++p);
110 if (!isspace(*p) && *p != '/')
112 /* may overwrite either white space or slash */
113 tfu->reps = atoi(savep);
114 tfu->flags = F_SETREP;
115 /* skip trailing white space */
116 for (++p; isspace(*p); ++p);
119 /* skip slash and trailing white space */
121 while (isspace(*++p));
125 for (savep = p; isdigit(*p); ++p);
128 tfu->bcnt = atoi(savep);
129 /* skip trailing white space */
130 for (++p; isspace(*p); ++p);
136 for (savep = ++p; *p != '"';)
139 if (!(tfu->fmt = malloc(p - savep + 1)))
141 (void) strlcpy(tfu->fmt, savep, p - savep + 1);
147 static const char *spec = ".#-+ 0123456789";
157 /* figure out the data block size needed for each format unit */
158 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
160 cursize += fu->bcnt * fu->reps;
163 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
167 * skip any special chars -- save precision in
168 * case it's a %s format.
170 while (strchr(spec + 1, *++fmt));
171 if (*fmt == '.' && isdigit(*++fmt)) {
173 while (isdigit(*++fmt));
179 case 'd': case 'i': case 'o': case 'u':
183 case 'e': case 'E': case 'f': case 'g': case 'G':
191 case 'c': case 'p': case 'u':
197 cursize += bcnt * fu->reps;
205 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
208 unsigned char *p1, *p2, *fmtp;
215 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
217 * Break each format unit into print units; each conversion
218 * character gets its own.
220 nextpr = &fu->nextpr;
221 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
222 if ((pr = calloc(1, sizeof(PR))) == NULL)
226 /* Skip preceding text and up to the next % sign. */
227 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
229 /* Only text in the string. */
237 * Get precision for %s -- if have a byte count, don't
242 /* Skip to conversion character. */
243 for (++p1; strchr(spec, *p1); ++p1);
245 /* Skip any special chars, field width. */
246 while (strchr(spec + 1, *++p1));
247 if (*p1 == '.' && isdigit(*++p1)) {
250 while (isdigit(*++p1));
255 p2 = *p1 ? p1 + 1 : p1; /* Set end pointer -- make sure
256 * that it's non-NUL/-NULL first
258 cs[0] = *p1; /* Set conversion string. */
262 * Figure out the byte count for each conversion;
263 * rewrite the format as necessary, set up blank-
264 * padding for end of data.
281 case 'o': case 'u': case 'x': case 'X':
301 case 'e': case 'E': case 'f': case 'g': case 'G':
311 if (fu->bcnt == sizeof(long double)) {
315 pr->bcnt = sizeof(long double);
340 fu->flags |= F_IGNORE;
343 pr->flags = F_ADDRESS;
346 case 'd': case 'o': case'x':
358 /* cs[0] = 'c'; set in conv_c */
366 /* cs[0] = 'c'; set in conv_u */
367 isint2: switch(fu->bcnt) {
387 * Copy to PR format string, set conversion character
388 * pointer, update original.
392 len = strlen(fmtp) + strlen(cs) + 1;
393 if ((pr->fmt = calloc(1, len)) == NULL)
395 snprintf(pr->fmt, len, "%s%s", fmtp, cs);
397 pr->cchar = pr->fmt + (p1 - fmtp);
400 /* Only one conversion character if byte count. */
401 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
402 errx(1, "byte count with multiple conversion characters");
405 * If format unit byte count not specified, figure it out
406 * so can adjust rep count later.
409 for (pr = fu->nextpr; pr; pr = pr->nextpr)
410 fu->bcnt += pr->bcnt;
413 * If the format string interprets any data at all, and it's
414 * not the same as the blocksize, and its last format unit
415 * interprets any data at all, and has no iteration count,
416 * repeat it as necessary.
418 * If, rep count is greater than 1, no trailing whitespace
419 * gets output from the last iteration of the format unit.
421 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
422 if (!fu->nextfu && fs->bcnt < blocksize &&
423 !(fu->flags&F_SETREP) && fu->bcnt)
424 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
426 for (pr = fu->nextpr;; pr = pr->nextpr)
429 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
430 p2 = isspace(*p1) ? p1 : NULL;
436 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
437 (void)printf("fmt:");
438 for (pr = fu->nextpr; pr; pr = pr->nextpr)
439 (void)printf(" {%s}", pr->fmt);
450 /* alphabetic escape sequences have to be done in place */
451 for (p2 = p1;; p1++, p2++) {
494 badcnt(const char *s)
496 errx(1, "%s: bad byte count", s);
502 errx(1, "%%s: requires a precision or a byte count");
506 badfmt(const char *fmt)
508 errx(1, "\"%s\": bad format", fmt);
512 badconv(const char *ch)
514 errx(1, "%%%s: bad conversion character", ch);