Home | History | Annotate | Line # | Download | only in hexdump
      1  1.27     joerg /*	$NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg 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.27     joerg __RCSID("$NetBSD: parse.c,v 1.27 2011/09/04 20:27:27 joerg 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.27     joerg __dead static void	 badcnt(char *);
     61  1.27     joerg __dead static void	 badconv(char *);
     62  1.27     joerg __dead static void	 badfmt(const char *);
     63  1.27     joerg __dead static void	 badsfmt(void);
     64  1.27     joerg 
     65   1.1       cgd FU *endfu;					/* format at end-of-data */
     66   1.1       cgd 
     67   1.5     mikel void
     68  1.18     perry addfile(char *name)
     69   1.1       cgd {
     70   1.7     lukem 	char *p;
     71   1.1       cgd 	FILE *fp;
     72   1.1       cgd 	int ch;
     73   1.1       cgd 	char buf[2048 + 1];
     74   1.1       cgd 
     75   1.6       mrg 	if ((fp = fopen(name, "r")) == NULL)
     76   1.7     lukem 		err(1, "fopen %s", name);
     77   1.1       cgd 	while (fgets(buf, sizeof(buf), fp)) {
     78   1.5     mikel 		if (!(p = strchr(buf, '\n'))) {
     79   1.7     lukem 			warnx("line too long.");
     80   1.1       cgd 			while ((ch = getchar()) != '\n' && ch != EOF);
     81   1.1       cgd 			continue;
     82   1.1       cgd 		}
     83   1.1       cgd 		*p = '\0';
     84   1.8  christos 		for (p = buf; *p && isspace((unsigned char)*p); ++p);
     85   1.1       cgd 		if (!*p || *p == '#')
     86   1.1       cgd 			continue;
     87   1.1       cgd 		add(p);
     88   1.1       cgd 	}
     89   1.1       cgd 	(void)fclose(fp);
     90   1.1       cgd }
     91   1.1       cgd 
     92   1.5     mikel void
     93  1.18     perry add(const char *fmt)
     94   1.1       cgd {
     95  1.11  christos 	const char *p;
     96   1.1       cgd 	static FS **nextfs;
     97   1.1       cgd 	FS *tfs;
     98   1.1       cgd 	FU *tfu, **nextfu;
     99  1.11  christos 	const char *savep;
    100   1.1       cgd 
    101   1.1       cgd 	/* start new linked list of format units */
    102  1.21      hira 	tfs = ecalloc(1, sizeof(FS));
    103   1.1       cgd 	if (!fshead)
    104   1.1       cgd 		fshead = tfs;
    105   1.1       cgd 	else
    106   1.1       cgd 		*nextfs = tfs;
    107   1.1       cgd 	nextfs = &tfs->nextfs;
    108   1.1       cgd 	nextfu = &tfs->nextfu;
    109   1.1       cgd 
    110   1.1       cgd 	/* take the format string and break it up into format units */
    111   1.1       cgd 	for (p = fmt;;) {
    112   1.1       cgd 		/* skip leading white space */
    113   1.8  christos 		for (; isspace((unsigned char)*p); ++p);
    114   1.1       cgd 		if (!*p)
    115   1.1       cgd 			break;
    116   1.1       cgd 
    117   1.1       cgd 		/* allocate a new format unit and link it in */
    118  1.21      hira 		tfu = ecalloc(1, sizeof(FU));
    119   1.1       cgd 		*nextfu = tfu;
    120   1.1       cgd 		nextfu = &tfu->nextfu;
    121   1.1       cgd 		tfu->reps = 1;
    122   1.1       cgd 
    123   1.1       cgd 		/* if leading digit, repetition count */
    124   1.8  christos 		if (isdigit((unsigned char)*p)) {
    125   1.8  christos 			for (savep = p; isdigit((unsigned char)*p); ++p);
    126   1.8  christos 			if (!isspace((unsigned char)*p) && *p != '/')
    127   1.1       cgd 				badfmt(fmt);
    128   1.1       cgd 			/* may overwrite either white space or slash */
    129   1.1       cgd 			tfu->reps = atoi(savep);
    130   1.1       cgd 			tfu->flags = F_SETREP;
    131   1.1       cgd 			/* skip trailing white space */
    132   1.8  christos 			for (++p; isspace((unsigned char)*p); ++p);
    133   1.1       cgd 		}
    134   1.1       cgd 
    135   1.1       cgd 		/* skip slash and trailing white space */
    136   1.1       cgd 		if (*p == '/')
    137   1.8  christos 			while (isspace((unsigned char)*++p));
    138   1.1       cgd 
    139   1.1       cgd 		/* byte count */
    140   1.8  christos 		if (isdigit((unsigned char)*p)) {
    141   1.8  christos 			for (savep = p; isdigit((unsigned char)*p); ++p);
    142   1.8  christos 			if (!isspace((unsigned char)*p))
    143   1.1       cgd 				badfmt(fmt);
    144   1.1       cgd 			tfu->bcnt = atoi(savep);
    145   1.1       cgd 			/* skip trailing white space */
    146   1.8  christos 			for (++p; isspace((unsigned char)*p); ++p);
    147   1.1       cgd 		}
    148   1.1       cgd 
    149   1.1       cgd 		/* format */
    150   1.1       cgd 		if (*p != '"')
    151   1.1       cgd 			badfmt(fmt);
    152   1.1       cgd 		for (savep = ++p; *p != '"';)
    153   1.1       cgd 			if (*p++ == 0)
    154   1.1       cgd 				badfmt(fmt);
    155  1.20  christos 		tfu->fmt = emalloc(p - savep + 1);
    156   1.1       cgd 		(void) strncpy(tfu->fmt, savep, p - savep);
    157   1.1       cgd 		tfu->fmt[p - savep] = '\0';
    158   1.1       cgd 		escape(tfu->fmt);
    159   1.1       cgd 		p++;
    160   1.1       cgd 	}
    161   1.1       cgd }
    162   1.1       cgd 
    163   1.5     mikel static const char *spec = ".#-+ 0123456789";
    164   1.5     mikel 
    165   1.5     mikel int
    166  1.18     perry size(FS *fs)
    167   1.1       cgd {
    168   1.7     lukem 	FU *fu;
    169   1.7     lukem 	int bcnt, cursize;
    170   1.7     lukem 	char *fmt;
    171   1.1       cgd 	int prec;
    172   1.1       cgd 
    173   1.1       cgd 	/* figure out the data block size needed for each format unit */
    174   1.1       cgd 	for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
    175   1.1       cgd 		if (fu->bcnt) {
    176   1.1       cgd 			cursize += fu->bcnt * fu->reps;
    177   1.1       cgd 			continue;
    178   1.1       cgd 		}
    179   1.1       cgd 		for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
    180   1.1       cgd 			if (*fmt != '%')
    181   1.1       cgd 				continue;
    182   1.1       cgd 			/*
    183   1.1       cgd 			 * skip any special chars -- save precision in
    184   1.1       cgd 			 * case it's a %s format.
    185   1.1       cgd 			 */
    186   1.5     mikel 			while (strchr(spec + 1, *++fmt));
    187   1.8  christos 			if (*fmt == '.' && isdigit((unsigned char)*++fmt)) {
    188   1.1       cgd 				prec = atoi(fmt);
    189   1.8  christos 				while (isdigit((unsigned char)*++fmt));
    190   1.1       cgd 			}
    191   1.1       cgd 			switch(*fmt) {
    192   1.1       cgd 			case 'c':
    193   1.1       cgd 				bcnt += 1;
    194   1.1       cgd 				break;
    195   1.1       cgd 			case 'd': case 'i': case 'o': case 'u':
    196   1.1       cgd 			case 'x': case 'X':
    197   1.1       cgd 				bcnt += 4;
    198   1.1       cgd 				break;
    199   1.1       cgd 			case 'e': case 'E': case 'f': case 'g': case 'G':
    200   1.1       cgd 				bcnt += 8;
    201   1.1       cgd 				break;
    202   1.1       cgd 			case 's':
    203   1.1       cgd 				bcnt += prec;
    204   1.1       cgd 				break;
    205   1.1       cgd 			case '_':
    206   1.1       cgd 				switch(*++fmt) {
    207   1.1       cgd 				case 'c': case 'p': case 'u':
    208   1.1       cgd 					bcnt += 1;
    209   1.1       cgd 					break;
    210   1.1       cgd 				}
    211   1.1       cgd 			}
    212   1.1       cgd 		}
    213   1.1       cgd 		cursize += bcnt * fu->reps;
    214   1.1       cgd 	}
    215   1.6       mrg 	return (cursize);
    216   1.1       cgd }
    217   1.1       cgd 
    218   1.5     mikel void
    219  1.18     perry rewrite(FS *fs)
    220   1.1       cgd {
    221   1.1       cgd 	enum { NOTOKAY, USEBCNT, USEPREC } sokay;
    222   1.7     lukem 	PR *pr, **nextpr;
    223   1.7     lukem 	FU *fu;
    224   1.7     lukem 	char *p1, *p2;
    225  1.26       apb 	char savech, *fmtp, cs[sizeof(PRId64)];
    226   1.1       cgd 	int nconv, prec;
    227   1.1       cgd 
    228   1.7     lukem 	prec = 0;
    229   1.1       cgd 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    230   1.1       cgd 		/*
    231   1.6       mrg 		 * Break each format unit into print units; each conversion
    232   1.6       mrg 		 * character gets its own.
    233   1.1       cgd 		 */
    234  1.19       dsl 		nextpr = &fu->nextpr;
    235   1.1       cgd 		for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
    236  1.24  christos 			pr = ecalloc(1, sizeof(*pr));
    237  1.19       dsl 			*nextpr = pr;
    238   1.1       cgd 
    239   1.6       mrg 			/* Skip preceding text and up to the next % sign. */
    240   1.1       cgd 			for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
    241   1.1       cgd 
    242   1.6       mrg 			/* Only text in the string. */
    243   1.1       cgd 			if (!*p1) {
    244   1.1       cgd 				pr->fmt = fmtp;
    245   1.1       cgd 				pr->flags = F_TEXT;
    246   1.1       cgd 				break;
    247   1.1       cgd 			}
    248   1.1       cgd 
    249   1.1       cgd 			/*
    250   1.6       mrg 			 * Get precision for %s -- if have a byte count, don't
    251   1.1       cgd 			 * need it.
    252   1.1       cgd 			 */
    253   1.1       cgd 			if (fu->bcnt) {
    254   1.1       cgd 				sokay = USEBCNT;
    255   1.6       mrg 				/* Skip to conversion character. */
    256  1.22      elad 				for (++p1; *p1 && strchr(spec, *p1); ++p1);
    257   1.1       cgd 			} else {
    258   1.6       mrg 				/* Skip any special chars, field width. */
    259  1.22      elad 				while (*++p1 && strchr(spec + 1, *p1));
    260   1.8  christos 				if (*p1 == '.' &&
    261   1.8  christos 				    isdigit((unsigned char)*++p1)) {
    262   1.1       cgd 					sokay = USEPREC;
    263   1.1       cgd 					prec = atoi(p1);
    264   1.8  christos 					while (isdigit((unsigned char)*++p1))
    265   1.8  christos 						continue;
    266   1.6       mrg 				} else
    267   1.1       cgd 					sokay = NOTOKAY;
    268   1.1       cgd 			}
    269   1.1       cgd 
    270  1.22      elad 			p2 = *p1 ? p1 + 1 : p1;	/* Set end pointer. */
    271   1.6       mrg 			cs[0] = *p1;		/* Set conversion string. */
    272   1.6       mrg 			cs[1] = '\0';
    273   1.1       cgd 
    274   1.1       cgd 			/*
    275   1.6       mrg 			 * Figure out the byte count for each conversion;
    276   1.1       cgd 			 * rewrite the format as necessary, set up blank-
    277   1.1       cgd 			 * padding for end of data.
    278   1.1       cgd 			 */
    279   1.6       mrg 			switch(cs[0]) {
    280   1.1       cgd 			case 'c':
    281   1.1       cgd 				pr->flags = F_CHAR;
    282   1.1       cgd 				switch(fu->bcnt) {
    283   1.1       cgd 				case 0: case 1:
    284   1.1       cgd 					pr->bcnt = 1;
    285   1.1       cgd 					break;
    286   1.1       cgd 				default:
    287   1.1       cgd 					p1[1] = '\0';
    288   1.1       cgd 					badcnt(p1);
    289   1.1       cgd 				}
    290   1.1       cgd 				break;
    291   1.1       cgd 			case 'd': case 'i':
    292   1.1       cgd 				pr->flags = F_INT;
    293   1.6       mrg 				goto isint;
    294   1.1       cgd 			case 'o': case 'u': case 'x': case 'X':
    295   1.1       cgd 				pr->flags = F_UINT;
    296  1.26       apb isint:
    297  1.26       apb 				/*
    298  1.26       apb 				 * Regardless of pr->bcnt, all integer
    299  1.26       apb 				 * values are cast to [u]int64_t before
    300  1.26       apb 				 * being printed by display().  We
    301  1.26       apb 				 * therefore need to use PRI?64 as the
    302  1.26       apb 				 * format, where '?' could actually
    303  1.26       apb 				 * be any of [diouxX].  We make the
    304  1.26       apb 				 * assumption (not guaranteed by the
    305  1.26       apb 				 * C99 standard) that we can derive
    306  1.26       apb 				 * all the other PRI?64 values from
    307  1.26       apb 				 * PRId64 simply by changing the last
    308  1.26       apb 				 * character.  For example, if PRId64 is
    309  1.26       apb 				 * "lld" or "qd", and cs[0] is 'o', then
    310  1.26       apb 				 * we end up with "llo" or "qo".
    311  1.26       apb 				 */
    312  1.26       apb 				savech = cs[0];
    313  1.26       apb 				strncpy(cs, PRId64, sizeof(PRId64) - 2);
    314  1.26       apb 				cs[sizeof(PRId64) - 2] = savech;
    315  1.26       apb 				cs[sizeof(PRId64) - 1] = '\0';
    316   1.6       mrg 				switch(fu->bcnt) {
    317   1.1       cgd 				case 0: case 4:
    318   1.1       cgd 					pr->bcnt = 4;
    319   1.1       cgd 					break;
    320   1.1       cgd 				case 1:
    321   1.1       cgd 					pr->bcnt = 1;
    322   1.1       cgd 					break;
    323   1.1       cgd 				case 2:
    324   1.1       cgd 					pr->bcnt = 2;
    325  1.12     bjh21 					break;
    326  1.12     bjh21 				case 8:
    327  1.12     bjh21 					pr->bcnt = 8;
    328   1.1       cgd 					break;
    329   1.1       cgd 				default:
    330   1.1       cgd 					p1[1] = '\0';
    331   1.1       cgd 					badcnt(p1);
    332   1.1       cgd 				}
    333   1.1       cgd 				break;
    334   1.1       cgd 			case 'e': case 'E': case 'f': case 'g': case 'G':
    335   1.1       cgd 				pr->flags = F_DBL;
    336   1.1       cgd 				switch(fu->bcnt) {
    337   1.1       cgd 				case 0: case 8:
    338   1.1       cgd 					pr->bcnt = 8;
    339   1.1       cgd 					break;
    340   1.1       cgd 				case 4:
    341   1.1       cgd 					pr->bcnt = 4;
    342   1.1       cgd 					break;
    343   1.1       cgd 				default:
    344   1.1       cgd 					p1[1] = '\0';
    345   1.1       cgd 					badcnt(p1);
    346   1.1       cgd 				}
    347   1.1       cgd 				break;
    348   1.1       cgd 			case 's':
    349   1.1       cgd 				pr->flags = F_STR;
    350   1.1       cgd 				switch(sokay) {
    351   1.1       cgd 				case NOTOKAY:
    352   1.1       cgd 					badsfmt();
    353   1.1       cgd 				case USEBCNT:
    354   1.1       cgd 					pr->bcnt = fu->bcnt;
    355   1.1       cgd 					break;
    356   1.1       cgd 				case USEPREC:
    357   1.1       cgd 					pr->bcnt = prec;
    358   1.1       cgd 					break;
    359   1.1       cgd 				}
    360   1.1       cgd 				break;
    361   1.1       cgd 			case '_':
    362   1.1       cgd 				++p2;
    363   1.1       cgd 				switch(p1[1]) {
    364   1.1       cgd 				case 'A':
    365   1.1       cgd 					endfu = fu;
    366   1.1       cgd 					fu->flags |= F_IGNORE;
    367   1.1       cgd 					/* FALLTHROUGH */
    368   1.1       cgd 				case 'a':
    369   1.1       cgd 					pr->flags = F_ADDRESS;
    370   1.1       cgd 					++p2;
    371   1.1       cgd 					switch(p1[2]) {
    372   1.1       cgd 					case 'd': case 'o': case'x':
    373  1.26       apb 						/*
    374  1.26       apb 						 * See comments above for
    375  1.26       apb 						 * the way we use PRId64.
    376  1.26       apb 						 */
    377  1.26       apb 						strncpy(cs, PRId64,
    378  1.26       apb 							sizeof(PRId64) - 2);
    379  1.26       apb 						cs[sizeof(PRId64) - 2] = p1[2];
    380  1.26       apb 						cs[sizeof(PRId64) - 1] = '\0';
    381   1.1       cgd 						break;
    382   1.1       cgd 					default:
    383   1.1       cgd 						p1[3] = '\0';
    384   1.1       cgd 						badconv(p1);
    385   1.1       cgd 					}
    386   1.1       cgd 					break;
    387   1.1       cgd 				case 'c':
    388   1.1       cgd 					pr->flags = F_C;
    389   1.6       mrg 					/* cs[0] = 'c';	set in conv_c */
    390   1.6       mrg 					goto isint2;
    391   1.1       cgd 				case 'p':
    392   1.1       cgd 					pr->flags = F_P;
    393   1.6       mrg 					cs[0] = 'c';
    394   1.6       mrg 					goto isint2;
    395   1.1       cgd 				case 'u':
    396   1.1       cgd 					pr->flags = F_U;
    397   1.6       mrg 					/* cs[0] = 'c';	set in conv_u */
    398   1.6       mrg isint2:					switch(fu->bcnt) {
    399   1.1       cgd 					case 0: case 1:
    400   1.1       cgd 						pr->bcnt = 1;
    401   1.1       cgd 						break;
    402   1.1       cgd 					default:
    403   1.1       cgd 						p1[2] = '\0';
    404   1.1       cgd 						badcnt(p1);
    405   1.1       cgd 					}
    406   1.1       cgd 					break;
    407   1.1       cgd 				default:
    408   1.1       cgd 					p1[2] = '\0';
    409   1.1       cgd 					badconv(p1);
    410   1.1       cgd 				}
    411   1.1       cgd 				break;
    412   1.1       cgd 			default:
    413   1.1       cgd 				p1[1] = '\0';
    414   1.1       cgd 				badconv(p1);
    415   1.1       cgd 			}
    416   1.1       cgd 
    417   1.1       cgd 			/*
    418   1.6       mrg 			 * Copy to PR format string, set conversion character
    419   1.1       cgd 			 * pointer, update original.
    420   1.1       cgd 			 */
    421   1.1       cgd 			savech = *p2;
    422   1.6       mrg 			p1[0] = '\0';
    423   1.9    itojun 			pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1);
    424   1.6       mrg 			(void)strcpy(pr->fmt, fmtp);
    425   1.6       mrg 			(void)strcat(pr->fmt, cs);
    426   1.1       cgd 			*p2 = savech;
    427   1.1       cgd 			pr->cchar = pr->fmt + (p1 - fmtp);
    428   1.1       cgd 			fmtp = p2;
    429   1.1       cgd 
    430   1.6       mrg 			/* Only one conversion character if byte count. */
    431   1.6       mrg 			if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
    432   1.7     lukem 				errx(1,
    433   1.7     lukem 			    "byte count with multiple conversion characters");
    434   1.1       cgd 		}
    435   1.1       cgd 		/*
    436   1.6       mrg 		 * If format unit byte count not specified, figure it out
    437   1.1       cgd 		 * so can adjust rep count later.
    438   1.1       cgd 		 */
    439   1.1       cgd 		if (!fu->bcnt)
    440   1.1       cgd 			for (pr = fu->nextpr; pr; pr = pr->nextpr)
    441   1.1       cgd 				fu->bcnt += pr->bcnt;
    442   1.1       cgd 	}
    443   1.1       cgd 	/*
    444   1.6       mrg 	 * If the format string interprets any data at all, and it's
    445   1.1       cgd 	 * not the same as the blocksize, and its last format unit
    446   1.1       cgd 	 * interprets any data at all, and has no iteration count,
    447   1.1       cgd 	 * repeat it as necessary.
    448   1.1       cgd 	 *
    449   1.6       mrg 	 * If, rep count is greater than 1, no trailing whitespace
    450   1.1       cgd 	 * gets output from the last iteration of the format unit.
    451   1.1       cgd 	 */
    452  1.10     bjh21 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    453   1.1       cgd 		if (!fu->nextfu && fs->bcnt < blocksize &&
    454   1.1       cgd 		    !(fu->flags&F_SETREP) && fu->bcnt)
    455   1.1       cgd 			fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
    456   1.1       cgd 		if (fu->reps > 1) {
    457  1.22      elad 			if (!fu->nextpr)
    458  1.22      elad 				break;
    459   1.1       cgd 			for (pr = fu->nextpr;; pr = pr->nextpr)
    460   1.1       cgd 				if (!pr->nextpr)
    461   1.1       cgd 					break;
    462   1.1       cgd 			for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
    463   1.8  christos 				p2 = isspace((unsigned char)*p1) ? p1 : NULL;
    464   1.1       cgd 			if (p2)
    465   1.1       cgd 				pr->nospace = p2;
    466   1.1       cgd 		}
    467   1.1       cgd 	}
    468   1.6       mrg #ifdef DEBUG
    469   1.6       mrg 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    470   1.6       mrg 		(void)printf("fmt:");
    471   1.6       mrg 		for (pr = fu->nextpr; pr; pr = pr->nextpr)
    472   1.6       mrg 			(void)printf(" {%s}", pr->fmt);
    473   1.6       mrg 		(void)printf("\n");
    474   1.6       mrg 	}
    475   1.6       mrg #endif
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.5     mikel void
    479  1.18     perry escape(char *p1)
    480   1.1       cgd {
    481   1.7     lukem 	char *p2;
    482   1.1       cgd 
    483   1.1       cgd 	/* alphabetic escape sequences have to be done in place */
    484   1.1       cgd 	for (p2 = p1;; ++p1, ++p2) {
    485   1.1       cgd 		if (!*p1) {
    486   1.1       cgd 			*p2 = *p1;
    487   1.1       cgd 			break;
    488   1.1       cgd 		}
    489   1.1       cgd 		if (*p1 == '\\')
    490   1.1       cgd 			switch(*++p1) {
    491  1.22      elad 			case '\0':
    492  1.22      elad 				*p2 = '\\';
    493  1.22      elad 				*++p2 = '\0';
    494  1.22      elad 				return;	/* incomplete escape sequence */
    495   1.1       cgd 			case 'a':
    496   1.1       cgd 			     /* *p2 = '\a'; */
    497   1.1       cgd 				*p2 = '\007';
    498   1.1       cgd 				break;
    499   1.1       cgd 			case 'b':
    500   1.1       cgd 				*p2 = '\b';
    501   1.1       cgd 				break;
    502   1.1       cgd 			case 'f':
    503   1.1       cgd 				*p2 = '\f';
    504   1.1       cgd 				break;
    505   1.1       cgd 			case 'n':
    506   1.1       cgd 				*p2 = '\n';
    507   1.1       cgd 				break;
    508   1.1       cgd 			case 'r':
    509   1.1       cgd 				*p2 = '\r';
    510   1.1       cgd 				break;
    511   1.1       cgd 			case 't':
    512   1.1       cgd 				*p2 = '\t';
    513   1.1       cgd 				break;
    514   1.1       cgd 			case 'v':
    515   1.1       cgd 				*p2 = '\v';
    516   1.1       cgd 				break;
    517   1.1       cgd 			default:
    518   1.1       cgd 				*p2 = *p1;
    519   1.1       cgd 				break;
    520   1.1       cgd 			}
    521  1.22      elad 		else
    522  1.22      elad 			*p2 = *p1;
    523   1.1       cgd 	}
    524   1.1       cgd }
    525   1.1       cgd 
    526  1.27     joerg static void
    527  1.18     perry badcnt(char *s)
    528   1.1       cgd {
    529   1.7     lukem 	errx(1, "%s: bad byte count", s);
    530   1.1       cgd }
    531   1.1       cgd 
    532  1.27     joerg static void
    533  1.18     perry badsfmt(void)
    534   1.1       cgd {
    535  1.13    itojun 	errx(1, "%%s: requires a precision or a byte count");
    536   1.1       cgd }
    537   1.1       cgd 
    538  1.27     joerg static void
    539  1.18     perry badfmt(const char *fmt)
    540   1.1       cgd {
    541  1.13    itojun 	errx(1, "\"%s\": bad format", fmt);
    542   1.1       cgd }
    543   1.1       cgd 
    544  1.27     joerg static void
    545  1.18     perry badconv(char *ch)
    546   1.1       cgd {
    547  1.13    itojun 	errx(1, "%%%s: bad conversion character", ch);
    548   1.1       cgd }
    549