1*d6555585Sjoerg /* $NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg Exp $ */
29d225a17Stls
361f28255Scgd /*
473973984Smrg * Copyright (c) 1989, 1993
573973984Smrg * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
33171d6532Slukem #include "nbtool_config.h"
34171d6532Slukem #endif
35171d6532Slukem
36c6810a02Slukem #include <sys/cdefs.h>
37171d6532Slukem #if !defined(lint)
38769693f9Smikel #if 0
3973973984Smrg static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
40769693f9Smikel #else
41*d6555585Sjoerg __RCSID("$NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg Exp $");
42769693f9Smikel #endif
4361f28255Scgd #endif /* not lint */
4461f28255Scgd
4561f28255Scgd #include <sys/types.h>
46e9d867efSmycroft #include <sys/file.h>
4773973984Smrg
48769693f9Smikel #include <ctype.h>
49c6810a02Slukem #include <err.h>
5073973984Smrg #include <errno.h>
5173973984Smrg #include <fcntl.h>
5253ee7761Sapb #include <inttypes.h>
5361f28255Scgd #include <stdio.h>
5461f28255Scgd #include <stdlib.h>
5561f28255Scgd #include <string.h>
56cdab3a7aSchristos #include <util.h>
5773973984Smrg
5861f28255Scgd #include "hexdump.h"
5961f28255Scgd
60*d6555585Sjoerg __dead static void badcnt(char *);
61*d6555585Sjoerg __dead static void badconv(char *);
62*d6555585Sjoerg __dead static void badfmt(const char *);
63*d6555585Sjoerg __dead static void badsfmt(void);
64*d6555585Sjoerg
6561f28255Scgd FU *endfu; /* format at end-of-data */
6661f28255Scgd
67769693f9Smikel void
addfile(char * name)68d310ebb1Sperry addfile(char *name)
6961f28255Scgd {
70c6810a02Slukem char *p;
7161f28255Scgd FILE *fp;
7261f28255Scgd int ch;
7361f28255Scgd char buf[2048 + 1];
7461f28255Scgd
7573973984Smrg if ((fp = fopen(name, "r")) == NULL)
76c6810a02Slukem err(1, "fopen %s", name);
7761f28255Scgd while (fgets(buf, sizeof(buf), fp)) {
78769693f9Smikel if (!(p = strchr(buf, '\n'))) {
79c6810a02Slukem warnx("line too long.");
8061f28255Scgd while ((ch = getchar()) != '\n' && ch != EOF);
8161f28255Scgd continue;
8261f28255Scgd }
8361f28255Scgd *p = '\0';
849794a7e0Schristos for (p = buf; *p && isspace((unsigned char)*p); ++p);
8561f28255Scgd if (!*p || *p == '#')
8661f28255Scgd continue;
8761f28255Scgd add(p);
8861f28255Scgd }
8961f28255Scgd (void)fclose(fp);
9061f28255Scgd }
9161f28255Scgd
92769693f9Smikel void
add(const char * fmt)93d310ebb1Sperry add(const char *fmt)
9461f28255Scgd {
95d88f58cbSchristos const char *p;
9661f28255Scgd static FS **nextfs;
9761f28255Scgd FS *tfs;
9861f28255Scgd FU *tfu, **nextfu;
99d88f58cbSchristos const char *savep;
10061f28255Scgd
10161f28255Scgd /* start new linked list of format units */
102fadb259bShira tfs = ecalloc(1, sizeof(FS));
10361f28255Scgd if (!fshead)
10461f28255Scgd fshead = tfs;
10561f28255Scgd else
10661f28255Scgd *nextfs = tfs;
10761f28255Scgd nextfs = &tfs->nextfs;
10861f28255Scgd nextfu = &tfs->nextfu;
10961f28255Scgd
11061f28255Scgd /* take the format string and break it up into format units */
11161f28255Scgd for (p = fmt;;) {
11261f28255Scgd /* skip leading white space */
1139794a7e0Schristos for (; isspace((unsigned char)*p); ++p);
11461f28255Scgd if (!*p)
11561f28255Scgd break;
11661f28255Scgd
11761f28255Scgd /* allocate a new format unit and link it in */
118fadb259bShira tfu = ecalloc(1, sizeof(FU));
11961f28255Scgd *nextfu = tfu;
12061f28255Scgd nextfu = &tfu->nextfu;
12161f28255Scgd tfu->reps = 1;
12261f28255Scgd
12361f28255Scgd /* if leading digit, repetition count */
1249794a7e0Schristos if (isdigit((unsigned char)*p)) {
1259794a7e0Schristos for (savep = p; isdigit((unsigned char)*p); ++p);
1269794a7e0Schristos if (!isspace((unsigned char)*p) && *p != '/')
12761f28255Scgd badfmt(fmt);
12861f28255Scgd /* may overwrite either white space or slash */
12961f28255Scgd tfu->reps = atoi(savep);
13061f28255Scgd tfu->flags = F_SETREP;
13161f28255Scgd /* skip trailing white space */
1329794a7e0Schristos for (++p; isspace((unsigned char)*p); ++p);
13361f28255Scgd }
13461f28255Scgd
13561f28255Scgd /* skip slash and trailing white space */
13661f28255Scgd if (*p == '/')
1379794a7e0Schristos while (isspace((unsigned char)*++p));
13861f28255Scgd
13961f28255Scgd /* byte count */
1409794a7e0Schristos if (isdigit((unsigned char)*p)) {
1419794a7e0Schristos for (savep = p; isdigit((unsigned char)*p); ++p);
1429794a7e0Schristos if (!isspace((unsigned char)*p))
14361f28255Scgd badfmt(fmt);
14461f28255Scgd tfu->bcnt = atoi(savep);
14561f28255Scgd /* skip trailing white space */
1469794a7e0Schristos for (++p; isspace((unsigned char)*p); ++p);
14761f28255Scgd }
14861f28255Scgd
14961f28255Scgd /* format */
15061f28255Scgd if (*p != '"')
15161f28255Scgd badfmt(fmt);
15261f28255Scgd for (savep = ++p; *p != '"';)
15361f28255Scgd if (*p++ == 0)
15461f28255Scgd badfmt(fmt);
155cdab3a7aSchristos tfu->fmt = emalloc(p - savep + 1);
15661f28255Scgd (void) strncpy(tfu->fmt, savep, p - savep);
15761f28255Scgd tfu->fmt[p - savep] = '\0';
15861f28255Scgd escape(tfu->fmt);
15961f28255Scgd p++;
16061f28255Scgd }
16161f28255Scgd }
16261f28255Scgd
163769693f9Smikel static const char *spec = ".#-+ 0123456789";
164769693f9Smikel
165769693f9Smikel int
size(FS * fs)166d310ebb1Sperry size(FS *fs)
16761f28255Scgd {
168c6810a02Slukem FU *fu;
169c6810a02Slukem int bcnt, cursize;
170c6810a02Slukem char *fmt;
17161f28255Scgd int prec;
17261f28255Scgd
17361f28255Scgd /* figure out the data block size needed for each format unit */
17461f28255Scgd for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
17561f28255Scgd if (fu->bcnt) {
17661f28255Scgd cursize += fu->bcnt * fu->reps;
17761f28255Scgd continue;
17861f28255Scgd }
17961f28255Scgd for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
18061f28255Scgd if (*fmt != '%')
18161f28255Scgd continue;
18261f28255Scgd /*
18361f28255Scgd * skip any special chars -- save precision in
18461f28255Scgd * case it's a %s format.
18561f28255Scgd */
186769693f9Smikel while (strchr(spec + 1, *++fmt));
1879794a7e0Schristos if (*fmt == '.' && isdigit((unsigned char)*++fmt)) {
18861f28255Scgd prec = atoi(fmt);
1899794a7e0Schristos while (isdigit((unsigned char)*++fmt));
19061f28255Scgd }
19161f28255Scgd switch(*fmt) {
19261f28255Scgd case 'c':
19361f28255Scgd bcnt += 1;
19461f28255Scgd break;
19561f28255Scgd case 'd': case 'i': case 'o': case 'u':
19661f28255Scgd case 'x': case 'X':
19761f28255Scgd bcnt += 4;
19861f28255Scgd break;
19961f28255Scgd case 'e': case 'E': case 'f': case 'g': case 'G':
20061f28255Scgd bcnt += 8;
20161f28255Scgd break;
20261f28255Scgd case 's':
20361f28255Scgd bcnt += prec;
20461f28255Scgd break;
20561f28255Scgd case '_':
20661f28255Scgd switch(*++fmt) {
20761f28255Scgd case 'c': case 'p': case 'u':
20861f28255Scgd bcnt += 1;
20961f28255Scgd break;
21061f28255Scgd }
21161f28255Scgd }
21261f28255Scgd }
21361f28255Scgd cursize += bcnt * fu->reps;
21461f28255Scgd }
21561f28255Scgd return (cursize);
21661f28255Scgd }
21761f28255Scgd
218769693f9Smikel void
rewrite(FS * fs)219d310ebb1Sperry rewrite(FS *fs)
22061f28255Scgd {
22161f28255Scgd enum { NOTOKAY, USEBCNT, USEPREC } sokay;
222c6810a02Slukem PR *pr, **nextpr;
223c6810a02Slukem FU *fu;
224c6810a02Slukem char *p1, *p2;
22553ee7761Sapb char savech, *fmtp, cs[sizeof(PRId64)];
22661f28255Scgd int nconv, prec;
22761f28255Scgd
228c6810a02Slukem prec = 0;
22961f28255Scgd for (fu = fs->nextfu; fu; fu = fu->nextfu) {
23061f28255Scgd /*
23173973984Smrg * Break each format unit into print units; each conversion
23273973984Smrg * character gets its own.
23361f28255Scgd */
234e4573e12Sdsl nextpr = &fu->nextpr;
23561f28255Scgd for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
236be4d21eeSchristos pr = ecalloc(1, sizeof(*pr));
23761f28255Scgd *nextpr = pr;
23861f28255Scgd
23973973984Smrg /* Skip preceding text and up to the next % sign. */
24061f28255Scgd for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
24161f28255Scgd
24273973984Smrg /* Only text in the string. */
24361f28255Scgd if (!*p1) {
24461f28255Scgd pr->fmt = fmtp;
24561f28255Scgd pr->flags = F_TEXT;
24661f28255Scgd break;
24761f28255Scgd }
24861f28255Scgd
24961f28255Scgd /*
25073973984Smrg * Get precision for %s -- if have a byte count, don't
25161f28255Scgd * need it.
25261f28255Scgd */
25361f28255Scgd if (fu->bcnt) {
25461f28255Scgd sokay = USEBCNT;
25573973984Smrg /* Skip to conversion character. */
256a01c1f47Selad for (++p1; *p1 && strchr(spec, *p1); ++p1);
25761f28255Scgd } else {
25873973984Smrg /* Skip any special chars, field width. */
259a01c1f47Selad while (*++p1 && strchr(spec + 1, *p1));
2609794a7e0Schristos if (*p1 == '.' &&
2619794a7e0Schristos isdigit((unsigned char)*++p1)) {
26261f28255Scgd sokay = USEPREC;
26361f28255Scgd prec = atoi(p1);
2649794a7e0Schristos while (isdigit((unsigned char)*++p1))
2659794a7e0Schristos continue;
26673973984Smrg } else
26761f28255Scgd sokay = NOTOKAY;
26861f28255Scgd }
26961f28255Scgd
270a01c1f47Selad p2 = *p1 ? p1 + 1 : p1; /* Set end pointer. */
27173973984Smrg cs[0] = *p1; /* Set conversion string. */
27273973984Smrg cs[1] = '\0';
27361f28255Scgd
27461f28255Scgd /*
27573973984Smrg * Figure out the byte count for each conversion;
27661f28255Scgd * rewrite the format as necessary, set up blank-
27761f28255Scgd * padding for end of data.
27861f28255Scgd */
27973973984Smrg switch(cs[0]) {
28061f28255Scgd case 'c':
28161f28255Scgd pr->flags = F_CHAR;
28261f28255Scgd switch(fu->bcnt) {
28361f28255Scgd case 0: case 1:
28461f28255Scgd pr->bcnt = 1;
28561f28255Scgd break;
28661f28255Scgd default:
28761f28255Scgd p1[1] = '\0';
28861f28255Scgd badcnt(p1);
28961f28255Scgd }
29061f28255Scgd break;
29161f28255Scgd case 'd': case 'i':
29261f28255Scgd pr->flags = F_INT;
29373973984Smrg goto isint;
29461f28255Scgd case 'o': case 'u': case 'x': case 'X':
29561f28255Scgd pr->flags = F_UINT;
29653ee7761Sapb isint:
29753ee7761Sapb /*
29853ee7761Sapb * Regardless of pr->bcnt, all integer
29953ee7761Sapb * values are cast to [u]int64_t before
30053ee7761Sapb * being printed by display(). We
30153ee7761Sapb * therefore need to use PRI?64 as the
30253ee7761Sapb * format, where '?' could actually
30353ee7761Sapb * be any of [diouxX]. We make the
30453ee7761Sapb * assumption (not guaranteed by the
30553ee7761Sapb * C99 standard) that we can derive
30653ee7761Sapb * all the other PRI?64 values from
30753ee7761Sapb * PRId64 simply by changing the last
30853ee7761Sapb * character. For example, if PRId64 is
30953ee7761Sapb * "lld" or "qd", and cs[0] is 'o', then
31053ee7761Sapb * we end up with "llo" or "qo".
31153ee7761Sapb */
31253ee7761Sapb savech = cs[0];
31353ee7761Sapb strncpy(cs, PRId64, sizeof(PRId64) - 2);
31453ee7761Sapb cs[sizeof(PRId64) - 2] = savech;
31553ee7761Sapb cs[sizeof(PRId64) - 1] = '\0';
31673973984Smrg switch(fu->bcnt) {
31761f28255Scgd case 0: case 4:
31861f28255Scgd pr->bcnt = 4;
31961f28255Scgd break;
32061f28255Scgd case 1:
32161f28255Scgd pr->bcnt = 1;
32261f28255Scgd break;
32361f28255Scgd case 2:
32461f28255Scgd pr->bcnt = 2;
32561f28255Scgd break;
32684f98450Sbjh21 case 8:
32784f98450Sbjh21 pr->bcnt = 8;
32884f98450Sbjh21 break;
32961f28255Scgd default:
33061f28255Scgd p1[1] = '\0';
33161f28255Scgd badcnt(p1);
33261f28255Scgd }
33361f28255Scgd break;
33461f28255Scgd case 'e': case 'E': case 'f': case 'g': case 'G':
33561f28255Scgd pr->flags = F_DBL;
33661f28255Scgd switch(fu->bcnt) {
33761f28255Scgd case 0: case 8:
33861f28255Scgd pr->bcnt = 8;
33961f28255Scgd break;
34061f28255Scgd case 4:
34161f28255Scgd pr->bcnt = 4;
34261f28255Scgd break;
34361f28255Scgd default:
34461f28255Scgd p1[1] = '\0';
34561f28255Scgd badcnt(p1);
34661f28255Scgd }
34761f28255Scgd break;
34861f28255Scgd case 's':
34961f28255Scgd pr->flags = F_STR;
35061f28255Scgd switch(sokay) {
35161f28255Scgd case NOTOKAY:
35261f28255Scgd badsfmt();
35361f28255Scgd case USEBCNT:
35461f28255Scgd pr->bcnt = fu->bcnt;
35561f28255Scgd break;
35661f28255Scgd case USEPREC:
35761f28255Scgd pr->bcnt = prec;
35861f28255Scgd break;
35961f28255Scgd }
36061f28255Scgd break;
36161f28255Scgd case '_':
36261f28255Scgd ++p2;
36361f28255Scgd switch(p1[1]) {
36461f28255Scgd case 'A':
36561f28255Scgd endfu = fu;
36661f28255Scgd fu->flags |= F_IGNORE;
36761f28255Scgd /* FALLTHROUGH */
36861f28255Scgd case 'a':
36961f28255Scgd pr->flags = F_ADDRESS;
37061f28255Scgd ++p2;
37161f28255Scgd switch(p1[2]) {
37261f28255Scgd case 'd': case 'o': case'x':
37353ee7761Sapb /*
37453ee7761Sapb * See comments above for
37553ee7761Sapb * the way we use PRId64.
37653ee7761Sapb */
37753ee7761Sapb strncpy(cs, PRId64,
37853ee7761Sapb sizeof(PRId64) - 2);
37953ee7761Sapb cs[sizeof(PRId64) - 2] = p1[2];
38053ee7761Sapb cs[sizeof(PRId64) - 1] = '\0';
38161f28255Scgd break;
38261f28255Scgd default:
38361f28255Scgd p1[3] = '\0';
38461f28255Scgd badconv(p1);
38561f28255Scgd }
38661f28255Scgd break;
38761f28255Scgd case 'c':
38861f28255Scgd pr->flags = F_C;
38973973984Smrg /* cs[0] = 'c'; set in conv_c */
39073973984Smrg goto isint2;
39161f28255Scgd case 'p':
39261f28255Scgd pr->flags = F_P;
39373973984Smrg cs[0] = 'c';
39473973984Smrg goto isint2;
39561f28255Scgd case 'u':
39661f28255Scgd pr->flags = F_U;
39773973984Smrg /* cs[0] = 'c'; set in conv_u */
39873973984Smrg isint2: switch(fu->bcnt) {
39961f28255Scgd case 0: case 1:
40061f28255Scgd pr->bcnt = 1;
40161f28255Scgd break;
40261f28255Scgd default:
40361f28255Scgd p1[2] = '\0';
40461f28255Scgd badcnt(p1);
40561f28255Scgd }
40661f28255Scgd break;
40761f28255Scgd default:
40861f28255Scgd p1[2] = '\0';
40961f28255Scgd badconv(p1);
41061f28255Scgd }
41161f28255Scgd break;
41261f28255Scgd default:
41361f28255Scgd p1[1] = '\0';
41461f28255Scgd badconv(p1);
41561f28255Scgd }
41661f28255Scgd
41761f28255Scgd /*
41873973984Smrg * Copy to PR format string, set conversion character
41961f28255Scgd * pointer, update original.
42061f28255Scgd */
42161f28255Scgd savech = *p2;
42273973984Smrg p1[0] = '\0';
42381d0adc8Sitojun pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1);
42473973984Smrg (void)strcpy(pr->fmt, fmtp);
42573973984Smrg (void)strcat(pr->fmt, cs);
42661f28255Scgd *p2 = savech;
42761f28255Scgd pr->cchar = pr->fmt + (p1 - fmtp);
42861f28255Scgd fmtp = p2;
42961f28255Scgd
43073973984Smrg /* Only one conversion character if byte count. */
43173973984Smrg if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
432c6810a02Slukem errx(1,
433c6810a02Slukem "byte count with multiple conversion characters");
43461f28255Scgd }
43561f28255Scgd /*
43673973984Smrg * If format unit byte count not specified, figure it out
43761f28255Scgd * so can adjust rep count later.
43861f28255Scgd */
43961f28255Scgd if (!fu->bcnt)
44061f28255Scgd for (pr = fu->nextpr; pr; pr = pr->nextpr)
44161f28255Scgd fu->bcnt += pr->bcnt;
44261f28255Scgd }
44361f28255Scgd /*
44473973984Smrg * If the format string interprets any data at all, and it's
44561f28255Scgd * not the same as the blocksize, and its last format unit
44661f28255Scgd * interprets any data at all, and has no iteration count,
44761f28255Scgd * repeat it as necessary.
44861f28255Scgd *
44973973984Smrg * If, rep count is greater than 1, no trailing whitespace
45061f28255Scgd * gets output from the last iteration of the format unit.
45161f28255Scgd */
4521897a4fbSbjh21 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
45361f28255Scgd if (!fu->nextfu && fs->bcnt < blocksize &&
45461f28255Scgd !(fu->flags&F_SETREP) && fu->bcnt)
45561f28255Scgd fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
45661f28255Scgd if (fu->reps > 1) {
457a01c1f47Selad if (!fu->nextpr)
458a01c1f47Selad break;
45961f28255Scgd for (pr = fu->nextpr;; pr = pr->nextpr)
46061f28255Scgd if (!pr->nextpr)
46161f28255Scgd break;
46261f28255Scgd for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
4639794a7e0Schristos p2 = isspace((unsigned char)*p1) ? p1 : NULL;
46461f28255Scgd if (p2)
46561f28255Scgd pr->nospace = p2;
46661f28255Scgd }
46761f28255Scgd }
46873973984Smrg #ifdef DEBUG
46973973984Smrg for (fu = fs->nextfu; fu; fu = fu->nextfu) {
47073973984Smrg (void)printf("fmt:");
47173973984Smrg for (pr = fu->nextpr; pr; pr = pr->nextpr)
47273973984Smrg (void)printf(" {%s}", pr->fmt);
47373973984Smrg (void)printf("\n");
47473973984Smrg }
47573973984Smrg #endif
47661f28255Scgd }
47761f28255Scgd
478769693f9Smikel void
escape(char * p1)479d310ebb1Sperry escape(char *p1)
48061f28255Scgd {
481c6810a02Slukem char *p2;
48261f28255Scgd
48361f28255Scgd /* alphabetic escape sequences have to be done in place */
48461f28255Scgd for (p2 = p1;; ++p1, ++p2) {
48561f28255Scgd if (!*p1) {
48661f28255Scgd *p2 = *p1;
48761f28255Scgd break;
48861f28255Scgd }
48961f28255Scgd if (*p1 == '\\')
49061f28255Scgd switch(*++p1) {
491a01c1f47Selad case '\0':
492a01c1f47Selad *p2 = '\\';
493a01c1f47Selad *++p2 = '\0';
494a01c1f47Selad return; /* incomplete escape sequence */
49561f28255Scgd case 'a':
49661f28255Scgd /* *p2 = '\a'; */
49761f28255Scgd *p2 = '\007';
49861f28255Scgd break;
49961f28255Scgd case 'b':
50061f28255Scgd *p2 = '\b';
50161f28255Scgd break;
50261f28255Scgd case 'f':
50361f28255Scgd *p2 = '\f';
50461f28255Scgd break;
50561f28255Scgd case 'n':
50661f28255Scgd *p2 = '\n';
50761f28255Scgd break;
50861f28255Scgd case 'r':
50961f28255Scgd *p2 = '\r';
51061f28255Scgd break;
51161f28255Scgd case 't':
51261f28255Scgd *p2 = '\t';
51361f28255Scgd break;
51461f28255Scgd case 'v':
51561f28255Scgd *p2 = '\v';
51661f28255Scgd break;
51761f28255Scgd default:
51861f28255Scgd *p2 = *p1;
51961f28255Scgd break;
52061f28255Scgd }
521a01c1f47Selad else
522a01c1f47Selad *p2 = *p1;
52361f28255Scgd }
52461f28255Scgd }
52561f28255Scgd
526*d6555585Sjoerg static void
badcnt(char * s)527d310ebb1Sperry badcnt(char *s)
52861f28255Scgd {
529c6810a02Slukem errx(1, "%s: bad byte count", s);
53061f28255Scgd }
53161f28255Scgd
532*d6555585Sjoerg static void
badsfmt(void)533d310ebb1Sperry badsfmt(void)
53461f28255Scgd {
535f51456c2Sitojun errx(1, "%%s: requires a precision or a byte count");
53661f28255Scgd }
53761f28255Scgd
538*d6555585Sjoerg static void
badfmt(const char * fmt)539d310ebb1Sperry badfmt(const char *fmt)
54061f28255Scgd {
541f51456c2Sitojun errx(1, "\"%s\": bad format", fmt);
54261f28255Scgd }
54361f28255Scgd
544*d6555585Sjoerg static void
badconv(char * ch)545d310ebb1Sperry badconv(char *ch)
54661f28255Scgd {
547f51456c2Sitojun errx(1, "%%%s: bad conversion character", ch);
54861f28255Scgd }
549