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