2 * SPDX-License-Identifier: BSD-3-Clause
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
34 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
50 FU *endfu; /* format at end-of-data */
53 addfile(const char *name)
60 if ((fp = fopen(name, "r")) == NULL)
62 while (fgets(buf, sizeof(buf), fp)) {
63 if (!(p = strchr(buf, '\n'))) {
64 warnx("line too long");
65 while ((ch = getchar()) != '\n' && ch != EOF);
69 for (p = buf; *p && isspace(*p); ++p);
80 unsigned const char *p, *savep;
85 /* start new linked list of format units */
86 if ((tfs = calloc(1, sizeof(FS))) == NULL)
92 nextfs = &tfs->nextfs;
93 nextfu = &tfs->nextfu;
95 /* take the format string and break it up into format units */
97 /* skip leading white space */
98 for (; isspace(*p); ++p);
102 /* allocate a new format unit and link it in */
103 if ((tfu = calloc(1, sizeof(FU))) == NULL)
106 nextfu = &tfu->nextfu;
109 /* if leading digit, repetition count */
111 for (savep = p; isdigit(*p); ++p);
112 if (!isspace(*p) && *p != '/')
114 /* may overwrite either white space or slash */
115 tfu->reps = atoi(savep);
116 tfu->flags = F_SETREP;
117 /* skip trailing white space */
118 for (++p; isspace(*p); ++p);
121 /* skip slash and trailing white space */
123 while (isspace(*++p));
127 for (savep = p; isdigit(*p); ++p);
130 tfu->bcnt = atoi(savep);
131 /* skip trailing white space */
132 for (++p; isspace(*p); ++p);
138 for (savep = ++p; *p != '"';)
141 if (!(tfu->fmt = malloc(p - savep + 1)))
143 (void) strlcpy(tfu->fmt, savep, p - savep + 1);
149 static const char *spec = ".#-+ 0123456789";
159 /* figure out the data block size needed for each format unit */
160 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
162 cursize += fu->bcnt * fu->reps;
165 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
169 * skip any special chars -- save precision in
170 * case it's a %s format.
172 while (*++fmt != 0 && strchr(spec + 1, *fmt) != NULL)
176 if (*fmt == '.' && isdigit(*++fmt)) {
178 while (isdigit(*++fmt));
184 case 'd': case 'i': case 'o': case 'u':
188 case 'e': case 'E': case 'f': case 'g': case 'G':
196 case 'c': case 'p': case 'u':
202 cursize += bcnt * fu->reps;
210 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
213 unsigned char *p1, *p2, *fmtp;
219 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
221 * Break each format unit into print units; each conversion
222 * character gets its own.
224 nextpr = &fu->nextpr;
225 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
226 if ((pr = calloc(1, sizeof(PR))) == NULL)
230 /* Skip preceding text and up to the next % sign. */
231 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
233 /* Only text in the string. */
241 * Get precision for %s -- if have a byte count, don't
246 /* Skip to conversion character. */
247 while (*++p1 != 0 && strchr(spec, *p1) != NULL)
252 /* Skip any special chars, field width. */
253 while (*++p1 != 0 && strchr(spec + 1, *p1) != NULL)
257 if (*p1 == '.' && isdigit(*++p1)) {
260 while (isdigit(*++p1));
265 p2 = *p1 ? p1 + 1 : p1; /* Set end pointer -- make sure
266 * that it's non-NUL/-NULL first
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':
311 case 'e': case 'E': case 'f': case 'g': case 'G':
321 if (fu->bcnt == sizeof(long double)) {
325 pr->bcnt = sizeof(long double);
350 fu->flags |= F_IGNORE;
353 pr->flags = F_ADDRESS;
356 case 'd': case 'o': case'x':
368 /* cs[0] = 'c'; set in conv_c */
376 /* cs[0] = 'c'; set in conv_u */
377 isint2: switch(fu->bcnt) {
397 * Copy to PR format string, set conversion character
398 * pointer, update original.
402 if (asprintf(&pr->fmt, "%s%s", fmtp, cs) == -1)
405 pr->cchar = pr->fmt + (p1 - fmtp);
408 /* Only one conversion character if byte count. */
409 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
410 errx(1, "byte count with multiple conversion characters");
413 * If format unit byte count not specified, figure it out
414 * so can adjust rep count later.
417 for (pr = fu->nextpr; pr; pr = pr->nextpr)
418 fu->bcnt += pr->bcnt;
421 * If the format string interprets any data at all, and it's
422 * not the same as the blocksize, and its last format unit
423 * interprets any data at all, and has no iteration count,
424 * repeat it as necessary.
426 * If, rep count is greater than 1, no trailing whitespace
427 * gets output from the last iteration of the format unit.
429 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
430 if (!fu->nextfu && fs->bcnt < blocksize &&
431 !(fu->flags&F_SETREP) && fu->bcnt)
432 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
434 for (pr = fu->nextpr;; pr = pr->nextpr)
437 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
438 p2 = isspace(*p1) ? p1 : NULL;
444 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
445 (void)printf("fmt:");
446 for (pr = fu->nextpr; pr; pr = pr->nextpr)
447 (void)printf(" {%s}", pr->fmt);
458 /* alphabetic escape sequences have to be done in place */
459 for (p2 = p1;; p1++, p2++) {
502 badcnt(const char *s)
504 errx(1, "%s: bad byte count", s);
510 errx(1, "%%s: requires a precision or a byte count");
514 badfmt(const char *fmt)
516 errx(1, "\"%s\": bad format", fmt);
520 badconv(const char *ch)
522 errx(1, "%%%s: bad conversion character", ch);
528 errx(1, "missing conversion character");