1*1e33498fSThomas Cort /* $NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg Exp $ */
2*1e33498fSThomas Cort
3*1e33498fSThomas Cort /*
4*1e33498fSThomas Cort * Copyright (c) 1989, 1993
5*1e33498fSThomas Cort * The Regents of the University of California. All rights reserved.
6*1e33498fSThomas Cort *
7*1e33498fSThomas Cort * Redistribution and use in source and binary forms, with or without
8*1e33498fSThomas Cort * modification, are permitted provided that the following conditions
9*1e33498fSThomas Cort * are met:
10*1e33498fSThomas Cort * 1. Redistributions of source code must retain the above copyright
11*1e33498fSThomas Cort * notice, this list of conditions and the following disclaimer.
12*1e33498fSThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*1e33498fSThomas Cort * notice, this list of conditions and the following disclaimer in the
14*1e33498fSThomas Cort * documentation and/or other materials provided with the distribution.
15*1e33498fSThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*1e33498fSThomas Cort * may be used to endorse or promote products derived from this software
17*1e33498fSThomas Cort * without specific prior written permission.
18*1e33498fSThomas Cort *
19*1e33498fSThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*1e33498fSThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*1e33498fSThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*1e33498fSThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*1e33498fSThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*1e33498fSThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*1e33498fSThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*1e33498fSThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*1e33498fSThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*1e33498fSThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*1e33498fSThomas Cort * SUCH DAMAGE.
30*1e33498fSThomas Cort */
31*1e33498fSThomas Cort
32*1e33498fSThomas Cort #if HAVE_NBTOOL_CONFIG_H
33*1e33498fSThomas Cort #include "nbtool_config.h"
34*1e33498fSThomas Cort #endif
35*1e33498fSThomas Cort
36*1e33498fSThomas Cort #include <sys/cdefs.h>
37*1e33498fSThomas Cort #if !defined(lint)
38*1e33498fSThomas Cort #if 0
39*1e33498fSThomas Cort static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
40*1e33498fSThomas Cort #else
41*1e33498fSThomas Cort __RCSID("$NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg Exp $");
42*1e33498fSThomas Cort #endif
43*1e33498fSThomas Cort #endif /* not lint */
44*1e33498fSThomas Cort
45*1e33498fSThomas Cort #include <sys/types.h>
46*1e33498fSThomas Cort #include <sys/file.h>
47*1e33498fSThomas Cort
48*1e33498fSThomas Cort #include <ctype.h>
49*1e33498fSThomas Cort #include <err.h>
50*1e33498fSThomas Cort #include <errno.h>
51*1e33498fSThomas Cort #include <fcntl.h>
52*1e33498fSThomas Cort #include <inttypes.h>
53*1e33498fSThomas Cort #include <stdio.h>
54*1e33498fSThomas Cort #include <stdlib.h>
55*1e33498fSThomas Cort #include <string.h>
56*1e33498fSThomas Cort #include <util.h>
57*1e33498fSThomas Cort
58*1e33498fSThomas Cort #include "hexdump.h"
59*1e33498fSThomas Cort
60*1e33498fSThomas Cort __dead static void badcnt(char *);
61*1e33498fSThomas Cort __dead static void badconv(char *);
62*1e33498fSThomas Cort __dead static void badfmt(const char *);
63*1e33498fSThomas Cort __dead static void badsfmt(void);
64*1e33498fSThomas Cort
65*1e33498fSThomas Cort FU *endfu; /* format at end-of-data */
66*1e33498fSThomas Cort
67*1e33498fSThomas Cort void
addfile(char * name)68*1e33498fSThomas Cort addfile(char *name)
69*1e33498fSThomas Cort {
70*1e33498fSThomas Cort char *p;
71*1e33498fSThomas Cort FILE *fp;
72*1e33498fSThomas Cort int ch;
73*1e33498fSThomas Cort char buf[2048 + 1];
74*1e33498fSThomas Cort
75*1e33498fSThomas Cort if ((fp = fopen(name, "r")) == NULL)
76*1e33498fSThomas Cort err(1, "fopen %s", name);
77*1e33498fSThomas Cort while (fgets(buf, sizeof(buf), fp)) {
78*1e33498fSThomas Cort if (!(p = strchr(buf, '\n'))) {
79*1e33498fSThomas Cort warnx("line too long.");
80*1e33498fSThomas Cort while ((ch = getchar()) != '\n' && ch != EOF);
81*1e33498fSThomas Cort continue;
82*1e33498fSThomas Cort }
83*1e33498fSThomas Cort *p = '\0';
84*1e33498fSThomas Cort for (p = buf; *p && isspace((unsigned char)*p); ++p);
85*1e33498fSThomas Cort if (!*p || *p == '#')
86*1e33498fSThomas Cort continue;
87*1e33498fSThomas Cort add(p);
88*1e33498fSThomas Cort }
89*1e33498fSThomas Cort (void)fclose(fp);
90*1e33498fSThomas Cort }
91*1e33498fSThomas Cort
92*1e33498fSThomas Cort void
add(const char * fmt)93*1e33498fSThomas Cort add(const char *fmt)
94*1e33498fSThomas Cort {
95*1e33498fSThomas Cort const char *p;
96*1e33498fSThomas Cort static FS **nextfs;
97*1e33498fSThomas Cort FS *tfs;
98*1e33498fSThomas Cort FU *tfu, **nextfu;
99*1e33498fSThomas Cort const char *savep;
100*1e33498fSThomas Cort
101*1e33498fSThomas Cort /* start new linked list of format units */
102*1e33498fSThomas Cort tfs = ecalloc(1, sizeof(FS));
103*1e33498fSThomas Cort if (!fshead)
104*1e33498fSThomas Cort fshead = tfs;
105*1e33498fSThomas Cort else
106*1e33498fSThomas Cort *nextfs = tfs;
107*1e33498fSThomas Cort nextfs = &tfs->nextfs;
108*1e33498fSThomas Cort nextfu = &tfs->nextfu;
109*1e33498fSThomas Cort
110*1e33498fSThomas Cort /* take the format string and break it up into format units */
111*1e33498fSThomas Cort for (p = fmt;;) {
112*1e33498fSThomas Cort /* skip leading white space */
113*1e33498fSThomas Cort for (; isspace((unsigned char)*p); ++p);
114*1e33498fSThomas Cort if (!*p)
115*1e33498fSThomas Cort break;
116*1e33498fSThomas Cort
117*1e33498fSThomas Cort /* allocate a new format unit and link it in */
118*1e33498fSThomas Cort tfu = ecalloc(1, sizeof(FU));
119*1e33498fSThomas Cort *nextfu = tfu;
120*1e33498fSThomas Cort nextfu = &tfu->nextfu;
121*1e33498fSThomas Cort tfu->reps = 1;
122*1e33498fSThomas Cort
123*1e33498fSThomas Cort /* if leading digit, repetition count */
124*1e33498fSThomas Cort if (isdigit((unsigned char)*p)) {
125*1e33498fSThomas Cort for (savep = p; isdigit((unsigned char)*p); ++p);
126*1e33498fSThomas Cort if (!isspace((unsigned char)*p) && *p != '/')
127*1e33498fSThomas Cort badfmt(fmt);
128*1e33498fSThomas Cort /* may overwrite either white space or slash */
129*1e33498fSThomas Cort tfu->reps = atoi(savep);
130*1e33498fSThomas Cort tfu->flags = F_SETREP;
131*1e33498fSThomas Cort /* skip trailing white space */
132*1e33498fSThomas Cort for (++p; isspace((unsigned char)*p); ++p);
133*1e33498fSThomas Cort }
134*1e33498fSThomas Cort
135*1e33498fSThomas Cort /* skip slash and trailing white space */
136*1e33498fSThomas Cort if (*p == '/')
137*1e33498fSThomas Cort while (isspace((unsigned char)*++p));
138*1e33498fSThomas Cort
139*1e33498fSThomas Cort /* byte count */
140*1e33498fSThomas Cort if (isdigit((unsigned char)*p)) {
141*1e33498fSThomas Cort for (savep = p; isdigit((unsigned char)*p); ++p);
142*1e33498fSThomas Cort if (!isspace((unsigned char)*p))
143*1e33498fSThomas Cort badfmt(fmt);
144*1e33498fSThomas Cort tfu->bcnt = atoi(savep);
145*1e33498fSThomas Cort /* skip trailing white space */
146*1e33498fSThomas Cort for (++p; isspace((unsigned char)*p); ++p);
147*1e33498fSThomas Cort }
148*1e33498fSThomas Cort
149*1e33498fSThomas Cort /* format */
150*1e33498fSThomas Cort if (*p != '"')
151*1e33498fSThomas Cort badfmt(fmt);
152*1e33498fSThomas Cort for (savep = ++p; *p != '"';)
153*1e33498fSThomas Cort if (*p++ == 0)
154*1e33498fSThomas Cort badfmt(fmt);
155*1e33498fSThomas Cort tfu->fmt = emalloc(p - savep + 1);
156*1e33498fSThomas Cort (void) strncpy(tfu->fmt, savep, p - savep);
157*1e33498fSThomas Cort tfu->fmt[p - savep] = '\0';
158*1e33498fSThomas Cort escape(tfu->fmt);
159*1e33498fSThomas Cort p++;
160*1e33498fSThomas Cort }
161*1e33498fSThomas Cort }
162*1e33498fSThomas Cort
163*1e33498fSThomas Cort static const char *spec = ".#-+ 0123456789";
164*1e33498fSThomas Cort
165*1e33498fSThomas Cort int
size(FS * fs)166*1e33498fSThomas Cort size(FS *fs)
167*1e33498fSThomas Cort {
168*1e33498fSThomas Cort FU *fu;
169*1e33498fSThomas Cort int bcnt, cursize;
170*1e33498fSThomas Cort char *fmt;
171*1e33498fSThomas Cort int prec;
172*1e33498fSThomas Cort
173*1e33498fSThomas Cort /* figure out the data block size needed for each format unit */
174*1e33498fSThomas Cort for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
175*1e33498fSThomas Cort if (fu->bcnt) {
176*1e33498fSThomas Cort cursize += fu->bcnt * fu->reps;
177*1e33498fSThomas Cort continue;
178*1e33498fSThomas Cort }
179*1e33498fSThomas Cort for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
180*1e33498fSThomas Cort if (*fmt != '%')
181*1e33498fSThomas Cort continue;
182*1e33498fSThomas Cort /*
183*1e33498fSThomas Cort * skip any special chars -- save precision in
184*1e33498fSThomas Cort * case it's a %s format.
185*1e33498fSThomas Cort */
186*1e33498fSThomas Cort while (strchr(spec + 1, *++fmt));
187*1e33498fSThomas Cort if (*fmt == '.' && isdigit((unsigned char)*++fmt)) {
188*1e33498fSThomas Cort prec = atoi(fmt);
189*1e33498fSThomas Cort while (isdigit((unsigned char)*++fmt));
190*1e33498fSThomas Cort }
191*1e33498fSThomas Cort switch(*fmt) {
192*1e33498fSThomas Cort case 'c':
193*1e33498fSThomas Cort bcnt += 1;
194*1e33498fSThomas Cort break;
195*1e33498fSThomas Cort case 'd': case 'i': case 'o': case 'u':
196*1e33498fSThomas Cort case 'x': case 'X':
197*1e33498fSThomas Cort bcnt += 4;
198*1e33498fSThomas Cort break;
199*1e33498fSThomas Cort case 'e': case 'E': case 'f': case 'g': case 'G':
200*1e33498fSThomas Cort bcnt += 8;
201*1e33498fSThomas Cort break;
202*1e33498fSThomas Cort case 's':
203*1e33498fSThomas Cort bcnt += prec;
204*1e33498fSThomas Cort break;
205*1e33498fSThomas Cort case '_':
206*1e33498fSThomas Cort switch(*++fmt) {
207*1e33498fSThomas Cort case 'c': case 'p': case 'u':
208*1e33498fSThomas Cort bcnt += 1;
209*1e33498fSThomas Cort break;
210*1e33498fSThomas Cort }
211*1e33498fSThomas Cort }
212*1e33498fSThomas Cort }
213*1e33498fSThomas Cort cursize += bcnt * fu->reps;
214*1e33498fSThomas Cort }
215*1e33498fSThomas Cort return (cursize);
216*1e33498fSThomas Cort }
217*1e33498fSThomas Cort
218*1e33498fSThomas Cort void
rewrite(FS * fs)219*1e33498fSThomas Cort rewrite(FS *fs)
220*1e33498fSThomas Cort {
221*1e33498fSThomas Cort enum { NOTOKAY, USEBCNT, USEPREC } sokay;
222*1e33498fSThomas Cort PR *pr, **nextpr;
223*1e33498fSThomas Cort FU *fu;
224*1e33498fSThomas Cort char *p1, *p2;
225*1e33498fSThomas Cort char savech, *fmtp, cs[sizeof(PRId64)];
226*1e33498fSThomas Cort int nconv, prec;
227*1e33498fSThomas Cort
228*1e33498fSThomas Cort prec = 0;
229*1e33498fSThomas Cort for (fu = fs->nextfu; fu; fu = fu->nextfu) {
230*1e33498fSThomas Cort /*
231*1e33498fSThomas Cort * Break each format unit into print units; each conversion
232*1e33498fSThomas Cort * character gets its own.
233*1e33498fSThomas Cort */
234*1e33498fSThomas Cort nextpr = &fu->nextpr;
235*1e33498fSThomas Cort for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
236*1e33498fSThomas Cort pr = ecalloc(1, sizeof(*pr));
237*1e33498fSThomas Cort *nextpr = pr;
238*1e33498fSThomas Cort
239*1e33498fSThomas Cort /* Skip preceding text and up to the next % sign. */
240*1e33498fSThomas Cort for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
241*1e33498fSThomas Cort
242*1e33498fSThomas Cort /* Only text in the string. */
243*1e33498fSThomas Cort if (!*p1) {
244*1e33498fSThomas Cort pr->fmt = fmtp;
245*1e33498fSThomas Cort pr->flags = F_TEXT;
246*1e33498fSThomas Cort break;
247*1e33498fSThomas Cort }
248*1e33498fSThomas Cort
249*1e33498fSThomas Cort /*
250*1e33498fSThomas Cort * Get precision for %s -- if have a byte count, don't
251*1e33498fSThomas Cort * need it.
252*1e33498fSThomas Cort */
253*1e33498fSThomas Cort if (fu->bcnt) {
254*1e33498fSThomas Cort sokay = USEBCNT;
255*1e33498fSThomas Cort /* Skip to conversion character. */
256*1e33498fSThomas Cort for (++p1; *p1 && strchr(spec, *p1); ++p1);
257*1e33498fSThomas Cort } else {
258*1e33498fSThomas Cort /* Skip any special chars, field width. */
259*1e33498fSThomas Cort while (*++p1 && strchr(spec + 1, *p1));
260*1e33498fSThomas Cort if (*p1 == '.' &&
261*1e33498fSThomas Cort isdigit((unsigned char)*++p1)) {
262*1e33498fSThomas Cort sokay = USEPREC;
263*1e33498fSThomas Cort prec = atoi(p1);
264*1e33498fSThomas Cort while (isdigit((unsigned char)*++p1))
265*1e33498fSThomas Cort continue;
266*1e33498fSThomas Cort } else
267*1e33498fSThomas Cort sokay = NOTOKAY;
268*1e33498fSThomas Cort }
269*1e33498fSThomas Cort
270*1e33498fSThomas Cort p2 = *p1 ? p1 + 1 : p1; /* Set end pointer. */
271*1e33498fSThomas Cort cs[0] = *p1; /* Set conversion string. */
272*1e33498fSThomas Cort cs[1] = '\0';
273*1e33498fSThomas Cort
274*1e33498fSThomas Cort /*
275*1e33498fSThomas Cort * Figure out the byte count for each conversion;
276*1e33498fSThomas Cort * rewrite the format as necessary, set up blank-
277*1e33498fSThomas Cort * padding for end of data.
278*1e33498fSThomas Cort */
279*1e33498fSThomas Cort switch(cs[0]) {
280*1e33498fSThomas Cort case 'c':
281*1e33498fSThomas Cort pr->flags = F_CHAR;
282*1e33498fSThomas Cort switch(fu->bcnt) {
283*1e33498fSThomas Cort case 0: case 1:
284*1e33498fSThomas Cort pr->bcnt = 1;
285*1e33498fSThomas Cort break;
286*1e33498fSThomas Cort default:
287*1e33498fSThomas Cort p1[1] = '\0';
288*1e33498fSThomas Cort badcnt(p1);
289*1e33498fSThomas Cort }
290*1e33498fSThomas Cort break;
291*1e33498fSThomas Cort case 'd': case 'i':
292*1e33498fSThomas Cort pr->flags = F_INT;
293*1e33498fSThomas Cort goto isint;
294*1e33498fSThomas Cort case 'o': case 'u': case 'x': case 'X':
295*1e33498fSThomas Cort pr->flags = F_UINT;
296*1e33498fSThomas Cort isint:
297*1e33498fSThomas Cort /*
298*1e33498fSThomas Cort * Regardless of pr->bcnt, all integer
299*1e33498fSThomas Cort * values are cast to [u]int64_t before
300*1e33498fSThomas Cort * being printed by display(). We
301*1e33498fSThomas Cort * therefore need to use PRI?64 as the
302*1e33498fSThomas Cort * format, where '?' could actually
303*1e33498fSThomas Cort * be any of [diouxX]. We make the
304*1e33498fSThomas Cort * assumption (not guaranteed by the
305*1e33498fSThomas Cort * C99 standard) that we can derive
306*1e33498fSThomas Cort * all the other PRI?64 values from
307*1e33498fSThomas Cort * PRId64 simply by changing the last
308*1e33498fSThomas Cort * character. For example, if PRId64 is
309*1e33498fSThomas Cort * "lld" or "qd", and cs[0] is 'o', then
310*1e33498fSThomas Cort * we end up with "llo" or "qo".
311*1e33498fSThomas Cort */
312*1e33498fSThomas Cort savech = cs[0];
313*1e33498fSThomas Cort strncpy(cs, PRId64, sizeof(PRId64) - 2);
314*1e33498fSThomas Cort cs[sizeof(PRId64) - 2] = savech;
315*1e33498fSThomas Cort cs[sizeof(PRId64) - 1] = '\0';
316*1e33498fSThomas Cort switch(fu->bcnt) {
317*1e33498fSThomas Cort case 0: case 4:
318*1e33498fSThomas Cort pr->bcnt = 4;
319*1e33498fSThomas Cort break;
320*1e33498fSThomas Cort case 1:
321*1e33498fSThomas Cort pr->bcnt = 1;
322*1e33498fSThomas Cort break;
323*1e33498fSThomas Cort case 2:
324*1e33498fSThomas Cort pr->bcnt = 2;
325*1e33498fSThomas Cort break;
326*1e33498fSThomas Cort case 8:
327*1e33498fSThomas Cort pr->bcnt = 8;
328*1e33498fSThomas Cort break;
329*1e33498fSThomas Cort default:
330*1e33498fSThomas Cort p1[1] = '\0';
331*1e33498fSThomas Cort badcnt(p1);
332*1e33498fSThomas Cort }
333*1e33498fSThomas Cort break;
334*1e33498fSThomas Cort case 'e': case 'E': case 'f': case 'g': case 'G':
335*1e33498fSThomas Cort pr->flags = F_DBL;
336*1e33498fSThomas Cort switch(fu->bcnt) {
337*1e33498fSThomas Cort case 0: case 8:
338*1e33498fSThomas Cort pr->bcnt = 8;
339*1e33498fSThomas Cort break;
340*1e33498fSThomas Cort case 4:
341*1e33498fSThomas Cort pr->bcnt = 4;
342*1e33498fSThomas Cort break;
343*1e33498fSThomas Cort default:
344*1e33498fSThomas Cort p1[1] = '\0';
345*1e33498fSThomas Cort badcnt(p1);
346*1e33498fSThomas Cort }
347*1e33498fSThomas Cort break;
348*1e33498fSThomas Cort case 's':
349*1e33498fSThomas Cort pr->flags = F_STR;
350*1e33498fSThomas Cort switch(sokay) {
351*1e33498fSThomas Cort case NOTOKAY:
352*1e33498fSThomas Cort badsfmt();
353*1e33498fSThomas Cort case USEBCNT:
354*1e33498fSThomas Cort pr->bcnt = fu->bcnt;
355*1e33498fSThomas Cort break;
356*1e33498fSThomas Cort case USEPREC:
357*1e33498fSThomas Cort pr->bcnt = prec;
358*1e33498fSThomas Cort break;
359*1e33498fSThomas Cort }
360*1e33498fSThomas Cort break;
361*1e33498fSThomas Cort case '_':
362*1e33498fSThomas Cort ++p2;
363*1e33498fSThomas Cort switch(p1[1]) {
364*1e33498fSThomas Cort case 'A':
365*1e33498fSThomas Cort endfu = fu;
366*1e33498fSThomas Cort fu->flags |= F_IGNORE;
367*1e33498fSThomas Cort /* FALLTHROUGH */
368*1e33498fSThomas Cort case 'a':
369*1e33498fSThomas Cort pr->flags = F_ADDRESS;
370*1e33498fSThomas Cort ++p2;
371*1e33498fSThomas Cort switch(p1[2]) {
372*1e33498fSThomas Cort case 'd': case 'o': case'x':
373*1e33498fSThomas Cort /*
374*1e33498fSThomas Cort * See comments above for
375*1e33498fSThomas Cort * the way we use PRId64.
376*1e33498fSThomas Cort */
377*1e33498fSThomas Cort strncpy(cs, PRId64,
378*1e33498fSThomas Cort sizeof(PRId64) - 2);
379*1e33498fSThomas Cort cs[sizeof(PRId64) - 2] = p1[2];
380*1e33498fSThomas Cort cs[sizeof(PRId64) - 1] = '\0';
381*1e33498fSThomas Cort break;
382*1e33498fSThomas Cort default:
383*1e33498fSThomas Cort p1[3] = '\0';
384*1e33498fSThomas Cort badconv(p1);
385*1e33498fSThomas Cort }
386*1e33498fSThomas Cort break;
387*1e33498fSThomas Cort case 'c':
388*1e33498fSThomas Cort pr->flags = F_C;
389*1e33498fSThomas Cort /* cs[0] = 'c'; set in conv_c */
390*1e33498fSThomas Cort goto isint2;
391*1e33498fSThomas Cort case 'p':
392*1e33498fSThomas Cort pr->flags = F_P;
393*1e33498fSThomas Cort cs[0] = 'c';
394*1e33498fSThomas Cort goto isint2;
395*1e33498fSThomas Cort case 'u':
396*1e33498fSThomas Cort pr->flags = F_U;
397*1e33498fSThomas Cort /* cs[0] = 'c'; set in conv_u */
398*1e33498fSThomas Cort isint2: switch(fu->bcnt) {
399*1e33498fSThomas Cort case 0: case 1:
400*1e33498fSThomas Cort pr->bcnt = 1;
401*1e33498fSThomas Cort break;
402*1e33498fSThomas Cort default:
403*1e33498fSThomas Cort p1[2] = '\0';
404*1e33498fSThomas Cort badcnt(p1);
405*1e33498fSThomas Cort }
406*1e33498fSThomas Cort break;
407*1e33498fSThomas Cort default:
408*1e33498fSThomas Cort p1[2] = '\0';
409*1e33498fSThomas Cort badconv(p1);
410*1e33498fSThomas Cort }
411*1e33498fSThomas Cort break;
412*1e33498fSThomas Cort default:
413*1e33498fSThomas Cort p1[1] = '\0';
414*1e33498fSThomas Cort badconv(p1);
415*1e33498fSThomas Cort }
416*1e33498fSThomas Cort
417*1e33498fSThomas Cort /*
418*1e33498fSThomas Cort * Copy to PR format string, set conversion character
419*1e33498fSThomas Cort * pointer, update original.
420*1e33498fSThomas Cort */
421*1e33498fSThomas Cort savech = *p2;
422*1e33498fSThomas Cort p1[0] = '\0';
423*1e33498fSThomas Cort pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1);
424*1e33498fSThomas Cort (void)strcpy(pr->fmt, fmtp);
425*1e33498fSThomas Cort (void)strcat(pr->fmt, cs);
426*1e33498fSThomas Cort *p2 = savech;
427*1e33498fSThomas Cort pr->cchar = pr->fmt + (p1 - fmtp);
428*1e33498fSThomas Cort fmtp = p2;
429*1e33498fSThomas Cort
430*1e33498fSThomas Cort /* Only one conversion character if byte count. */
431*1e33498fSThomas Cort if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
432*1e33498fSThomas Cort errx(1,
433*1e33498fSThomas Cort "byte count with multiple conversion characters");
434*1e33498fSThomas Cort }
435*1e33498fSThomas Cort /*
436*1e33498fSThomas Cort * If format unit byte count not specified, figure it out
437*1e33498fSThomas Cort * so can adjust rep count later.
438*1e33498fSThomas Cort */
439*1e33498fSThomas Cort if (!fu->bcnt)
440*1e33498fSThomas Cort for (pr = fu->nextpr; pr; pr = pr->nextpr)
441*1e33498fSThomas Cort fu->bcnt += pr->bcnt;
442*1e33498fSThomas Cort }
443*1e33498fSThomas Cort /*
444*1e33498fSThomas Cort * If the format string interprets any data at all, and it's
445*1e33498fSThomas Cort * not the same as the blocksize, and its last format unit
446*1e33498fSThomas Cort * interprets any data at all, and has no iteration count,
447*1e33498fSThomas Cort * repeat it as necessary.
448*1e33498fSThomas Cort *
449*1e33498fSThomas Cort * If, rep count is greater than 1, no trailing whitespace
450*1e33498fSThomas Cort * gets output from the last iteration of the format unit.
451*1e33498fSThomas Cort */
452*1e33498fSThomas Cort for (fu = fs->nextfu; fu; fu = fu->nextfu) {
453*1e33498fSThomas Cort if (!fu->nextfu && fs->bcnt < blocksize &&
454*1e33498fSThomas Cort !(fu->flags&F_SETREP) && fu->bcnt)
455*1e33498fSThomas Cort fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
456*1e33498fSThomas Cort if (fu->reps > 1) {
457*1e33498fSThomas Cort if (!fu->nextpr)
458*1e33498fSThomas Cort break;
459*1e33498fSThomas Cort for (pr = fu->nextpr;; pr = pr->nextpr)
460*1e33498fSThomas Cort if (!pr->nextpr)
461*1e33498fSThomas Cort break;
462*1e33498fSThomas Cort for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
463*1e33498fSThomas Cort p2 = isspace((unsigned char)*p1) ? p1 : NULL;
464*1e33498fSThomas Cort if (p2)
465*1e33498fSThomas Cort pr->nospace = p2;
466*1e33498fSThomas Cort }
467*1e33498fSThomas Cort }
468*1e33498fSThomas Cort #ifdef DEBUG
469*1e33498fSThomas Cort for (fu = fs->nextfu; fu; fu = fu->nextfu) {
470*1e33498fSThomas Cort (void)printf("fmt:");
471*1e33498fSThomas Cort for (pr = fu->nextpr; pr; pr = pr->nextpr)
472*1e33498fSThomas Cort (void)printf(" {%s}", pr->fmt);
473*1e33498fSThomas Cort (void)printf("\n");
474*1e33498fSThomas Cort }
475*1e33498fSThomas Cort #endif
476*1e33498fSThomas Cort }
477*1e33498fSThomas Cort
478*1e33498fSThomas Cort void
escape(char * p1)479*1e33498fSThomas Cort escape(char *p1)
480*1e33498fSThomas Cort {
481*1e33498fSThomas Cort char *p2;
482*1e33498fSThomas Cort
483*1e33498fSThomas Cort /* alphabetic escape sequences have to be done in place */
484*1e33498fSThomas Cort for (p2 = p1;; ++p1, ++p2) {
485*1e33498fSThomas Cort if (!*p1) {
486*1e33498fSThomas Cort *p2 = *p1;
487*1e33498fSThomas Cort break;
488*1e33498fSThomas Cort }
489*1e33498fSThomas Cort if (*p1 == '\\')
490*1e33498fSThomas Cort switch(*++p1) {
491*1e33498fSThomas Cort case '\0':
492*1e33498fSThomas Cort *p2 = '\\';
493*1e33498fSThomas Cort *++p2 = '\0';
494*1e33498fSThomas Cort return; /* incomplete escape sequence */
495*1e33498fSThomas Cort case 'a':
496*1e33498fSThomas Cort /* *p2 = '\a'; */
497*1e33498fSThomas Cort *p2 = '\007';
498*1e33498fSThomas Cort break;
499*1e33498fSThomas Cort case 'b':
500*1e33498fSThomas Cort *p2 = '\b';
501*1e33498fSThomas Cort break;
502*1e33498fSThomas Cort case 'f':
503*1e33498fSThomas Cort *p2 = '\f';
504*1e33498fSThomas Cort break;
505*1e33498fSThomas Cort case 'n':
506*1e33498fSThomas Cort *p2 = '\n';
507*1e33498fSThomas Cort break;
508*1e33498fSThomas Cort case 'r':
509*1e33498fSThomas Cort *p2 = '\r';
510*1e33498fSThomas Cort break;
511*1e33498fSThomas Cort case 't':
512*1e33498fSThomas Cort *p2 = '\t';
513*1e33498fSThomas Cort break;
514*1e33498fSThomas Cort case 'v':
515*1e33498fSThomas Cort *p2 = '\v';
516*1e33498fSThomas Cort break;
517*1e33498fSThomas Cort default:
518*1e33498fSThomas Cort *p2 = *p1;
519*1e33498fSThomas Cort break;
520*1e33498fSThomas Cort }
521*1e33498fSThomas Cort else
522*1e33498fSThomas Cort *p2 = *p1;
523*1e33498fSThomas Cort }
524*1e33498fSThomas Cort }
525*1e33498fSThomas Cort
526*1e33498fSThomas Cort static void
badcnt(char * s)527*1e33498fSThomas Cort badcnt(char *s)
528*1e33498fSThomas Cort {
529*1e33498fSThomas Cort errx(1, "%s: bad byte count", s);
530*1e33498fSThomas Cort }
531*1e33498fSThomas Cort
532*1e33498fSThomas Cort static void
badsfmt(void)533*1e33498fSThomas Cort badsfmt(void)
534*1e33498fSThomas Cort {
535*1e33498fSThomas Cort errx(1, "%%s: requires a precision or a byte count");
536*1e33498fSThomas Cort }
537*1e33498fSThomas Cort
538*1e33498fSThomas Cort static void
badfmt(const char * fmt)539*1e33498fSThomas Cort badfmt(const char *fmt)
540*1e33498fSThomas Cort {
541*1e33498fSThomas Cort errx(1, "\"%s\": bad format", fmt);
542*1e33498fSThomas Cort }
543*1e33498fSThomas Cort
544*1e33498fSThomas Cort static void
badconv(char * ch)545*1e33498fSThomas Cort badconv(char *ch)
546*1e33498fSThomas Cort {
547*1e33498fSThomas Cort errx(1, "%%%s: bad conversion character", ch);
548*1e33498fSThomas Cort }
549