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
32 #include <sys/types.h>
42 FU *endfu; /* format at end-of-data */
45 addfile(const char *name)
52 if ((fp = fopen(name, "r")) == NULL)
54 while (fgets(buf, sizeof(buf), fp)) {
55 if (!(p = strchr(buf, '\n'))) {
56 warnx("line too long");
57 while ((ch = getchar()) != '\n' && ch != EOF);
61 for (p = buf; *p && isspace(*p); ++p);
72 unsigned const char *p, *savep;
77 /* start new linked list of format units */
78 if ((tfs = calloc(1, sizeof(FS))) == NULL)
84 nextfs = &tfs->nextfs;
85 nextfu = &tfs->nextfu;
87 /* take the format string and break it up into format units */
89 /* skip leading white space */
90 for (; isspace(*p); ++p);
94 /* allocate a new format unit and link it in */
95 if ((tfu = calloc(1, sizeof(FU))) == NULL)
98 nextfu = &tfu->nextfu;
101 /* if leading digit, repetition count */
103 for (savep = p; isdigit(*p); ++p);
104 if (!isspace(*p) && *p != '/')
106 /* may overwrite either white space or slash */
107 tfu->reps = atoi(savep);
108 tfu->flags = F_SETREP;
109 /* skip trailing white space */
110 for (++p; isspace(*p); ++p);
113 /* skip slash and trailing white space */
115 while (isspace(*++p));
119 for (savep = p; isdigit(*p); ++p);
122 tfu->bcnt = atoi(savep);
123 /* skip trailing white space */
124 for (++p; isspace(*p); ++p);
130 for (savep = ++p; *p != '"';)
133 if (!(tfu->fmt = malloc(p - savep + 1)))
135 (void) strlcpy(tfu->fmt, savep, p - savep + 1);
141 static const char *spec = ".#-+ 0123456789";
151 /* figure out the data block size needed for each format unit */
152 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
154 cursize += fu->bcnt * fu->reps;
157 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
161 * skip any special chars -- save precision in
162 * case it's a %s format.
164 while (*++fmt != 0 && strchr(spec + 1, *fmt) != NULL)
168 if (*fmt == '.' && isdigit(*++fmt)) {
170 while (isdigit(*++fmt));
176 case 'd': case 'i': case 'o': case 'u':
180 case 'e': case 'E': case 'f': case 'g': case 'G':
188 case 'c': case 'p': case 'u':
194 cursize += bcnt * fu->reps;
202 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
205 unsigned char *p1, *p2, *fmtp;
211 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
213 * Break each format unit into print units; each conversion
214 * character gets its own.
216 nextpr = &fu->nextpr;
217 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
218 if ((pr = calloc(1, sizeof(PR))) == NULL)
222 /* Skip preceding text and up to the next % sign. */
223 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
225 /* Only text in the string. */
233 * Get precision for %s -- if have a byte count, don't
238 /* Skip to conversion character. */
239 while (*++p1 != 0 && strchr(spec, *p1) != NULL)
244 /* Skip any special chars, field width. */
245 while (*++p1 != 0 && strchr(spec + 1, *p1) != NULL)
249 if (*p1 == '.' && isdigit(*++p1)) {
252 while (isdigit(*++p1));
257 p2 = *p1 ? p1 + 1 : p1; /* Set end pointer -- make sure
258 * that it's non-NUL/-NULL first
260 cs[0] = *p1; /* Set conversion string. */
264 * Figure out the byte count for each conversion;
265 * rewrite the format as necessary, set up blank-
266 * padding for end of data.
283 case 'o': case 'u': case 'x': case 'X':
306 case 'e': case 'E': case 'f': case 'g': case 'G':
316 if (fu->bcnt == sizeof(long double)) {
320 pr->bcnt = sizeof(long double);
345 fu->flags |= F_IGNORE;
348 pr->flags = F_ADDRESS;
351 case 'd': case 'o': case'x':
363 /* cs[0] = 'c'; set in conv_c */
371 /* cs[0] = 'c'; set in conv_u */
372 isint2: switch(fu->bcnt) {
392 * Copy to PR format string, set conversion character
393 * pointer, update original.
397 if (asprintf(&pr->fmt, "%s%s", fmtp, cs) == -1)
400 pr->cchar = pr->fmt + (p1 - fmtp);
403 /* Only one conversion character if byte count. */
404 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
405 errx(1, "byte count with multiple conversion characters");
408 * If format unit byte count not specified, figure it out
409 * so can adjust rep count later.
412 for (pr = fu->nextpr; pr; pr = pr->nextpr)
413 fu->bcnt += pr->bcnt;
416 * If the format string interprets any data at all, and it's
417 * not the same as the blocksize, and its last format unit
418 * interprets any data at all, and has no iteration count,
419 * repeat it as necessary.
421 * If, rep count is greater than 1, no trailing whitespace
422 * gets output from the last iteration of the format unit.
424 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
425 if (!fu->nextfu && fs->bcnt < blocksize &&
426 !(fu->flags&F_SETREP) && fu->bcnt)
427 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
429 for (pr = fu->nextpr;; pr = pr->nextpr)
432 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
433 p2 = isspace(*p1) ? p1 : NULL;
439 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
440 (void)printf("fmt:");
441 for (pr = fu->nextpr; pr; pr = pr->nextpr)
442 (void)printf(" {%s}", pr->fmt);
453 /* alphabetic escape sequences have to be done in place */
454 for (p2 = p1;; p1++, p2++) {
497 badcnt(const char *s)
499 errx(1, "%s: bad byte count", s);
505 errx(1, "%%s: requires a precision or a byte count");
509 badfmt(const char *fmt)
511 errx(1, "\"%s\": bad format", fmt);
515 badconv(const char *ch)
517 errx(1, "%%%s: bad conversion character", ch);
523 errx(1, "missing conversion character");