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