Home | History | Annotate | Line # | Download | only in hexdump
parse.c revision 1.4
      1 /*	$NetBSD: parse.c,v 1.4 1997/01/09 20:19:59 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989 The Regents of the University of California.
      5  * 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 /*static char sccsid[] = "from: @(#)parse.c	5.6 (Berkeley) 3/9/91";*/
     38 static char rcsid[] = "$NetBSD: parse.c,v 1.4 1997/01/09 20:19:59 tls Exp $";
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/file.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <ctype.h>
     46 #include <string.h>
     47 #include "hexdump.h"
     48 
     49 FU *endfu;					/* format at end-of-data */
     50 
     51 addfile(name)
     52 	char *name;
     53 {
     54 	register char *p;
     55 	FILE *fp;
     56 	int ch;
     57 	char buf[2048 + 1];
     58 
     59 	if (!(fp = fopen(name, "r"))) {
     60 		(void)fprintf(stderr, "hexdump: can't read %s.\n", name);
     61 		exit(1);
     62 	}
     63 	while (fgets(buf, sizeof(buf), fp)) {
     64 		if (!(p = index(buf, '\n'))) {
     65 			(void)fprintf(stderr, "hexdump: line too long.\n");
     66 			while ((ch = getchar()) != '\n' && ch != EOF);
     67 			continue;
     68 		}
     69 		*p = '\0';
     70 		for (p = buf; *p && isspace(*p); ++p);
     71 		if (!*p || *p == '#')
     72 			continue;
     73 		add(p);
     74 	}
     75 	(void)fclose(fp);
     76 }
     77 
     78 add(fmt)
     79 	char *fmt;
     80 {
     81 	register char *p;
     82 	static FS **nextfs;
     83 	FS *tfs;
     84 	FU *tfu, **nextfu;
     85 	char *savep, *emalloc();
     86 
     87 	/* start new linked list of format units */
     88 	/* NOSTRICT */
     89 	tfs = (FS *)emalloc(sizeof(FS));
     90 	if (!fshead)
     91 		fshead = tfs;
     92 	else
     93 		*nextfs = tfs;
     94 	nextfs = &tfs->nextfs;
     95 	nextfu = &tfs->nextfu;
     96 
     97 	/* take the format string and break it up into format units */
     98 	for (p = fmt;;) {
     99 		/* skip leading white space */
    100 		for (; isspace(*p); ++p);
    101 		if (!*p)
    102 			break;
    103 
    104 		/* allocate a new format unit and link it in */
    105 		/* NOSTRICT */
    106 		tfu = (FU *)emalloc(sizeof(FU));
    107 		*nextfu = tfu;
    108 		nextfu = &tfu->nextfu;
    109 		tfu->reps = 1;
    110 
    111 		/* if leading digit, repetition count */
    112 		if (isdigit(*p)) {
    113 			for (savep = p; isdigit(*p); ++p);
    114 			if (!isspace(*p) && *p != '/')
    115 				badfmt(fmt);
    116 			/* may overwrite either white space or slash */
    117 			tfu->reps = atoi(savep);
    118 			tfu->flags = F_SETREP;
    119 			/* skip trailing white space */
    120 			for (++p; isspace(*p); ++p);
    121 		}
    122 
    123 		/* skip slash and trailing white space */
    124 		if (*p == '/')
    125 			while (isspace(*++p));
    126 
    127 		/* byte count */
    128 		if (isdigit(*p)) {
    129 			for (savep = p; isdigit(*p); ++p);
    130 			if (!isspace(*p))
    131 				badfmt(fmt);
    132 			tfu->bcnt = atoi(savep);
    133 			/* skip trailing white space */
    134 			for (++p; isspace(*p); ++p);
    135 		}
    136 
    137 		/* format */
    138 		if (*p != '"')
    139 			badfmt(fmt);
    140 		for (savep = ++p; *p != '"';)
    141 			if (*p++ == 0)
    142 				badfmt(fmt);
    143 		if (!(tfu->fmt = malloc(p - savep + 1)))
    144 			nomem();
    145 		(void) strncpy(tfu->fmt, savep, p - savep);
    146 		tfu->fmt[p - savep] = '\0';
    147 		escape(tfu->fmt);
    148 		p++;
    149 	}
    150 }
    151 
    152 static char *spec = ".#-+ 0123456789";
    153 size(fs)
    154 	FS *fs;
    155 {
    156 	register FU *fu;
    157 	register int bcnt, cursize;
    158 	register char *fmt;
    159 	int prec;
    160 
    161 	/* figure out the data block size needed for each format unit */
    162 	for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
    163 		if (fu->bcnt) {
    164 			cursize += fu->bcnt * fu->reps;
    165 			continue;
    166 		}
    167 		for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
    168 			if (*fmt != '%')
    169 				continue;
    170 			/*
    171 			 * skip any special chars -- save precision in
    172 			 * case it's a %s format.
    173 			 */
    174 			while (index(spec + 1, *++fmt));
    175 			if (*fmt == '.' && isdigit(*++fmt)) {
    176 				prec = atoi(fmt);
    177 				while (isdigit(*++fmt));
    178 			}
    179 			switch(*fmt) {
    180 			case 'c':
    181 				bcnt += 1;
    182 				break;
    183 			case 'd': case 'i': case 'o': case 'u':
    184 			case 'x': case 'X':
    185 				bcnt += 4;
    186 				break;
    187 			case 'e': case 'E': case 'f': case 'g': case 'G':
    188 				bcnt += 8;
    189 				break;
    190 			case 's':
    191 				bcnt += prec;
    192 				break;
    193 			case '_':
    194 				switch(*++fmt) {
    195 				case 'c': case 'p': case 'u':
    196 					bcnt += 1;
    197 					break;
    198 				}
    199 			}
    200 		}
    201 		cursize += bcnt * fu->reps;
    202 	}
    203 	return(cursize);
    204 }
    205 
    206 rewrite(fs)
    207 	FS *fs;
    208 {
    209 	enum { NOTOKAY, USEBCNT, USEPREC } sokay;
    210 	register PR *pr, **nextpr;
    211 	register FU *fu;
    212 	register char *p1, *p2;
    213 	char savech, *fmtp;
    214 	int nconv, prec;
    215 
    216 	for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    217 		/*
    218 		 * break each format unit into print units; each
    219 		 * conversion character gets its own.
    220 		 */
    221 		for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
    222 			/* NOSTRICT */
    223 			pr = (PR *)emalloc(sizeof(PR));
    224 			if (!fu->nextpr)
    225 				fu->nextpr = pr;
    226 			else
    227 				*nextpr = pr;
    228 
    229 			/* skip preceding text and up to the next % sign */
    230 			for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
    231 
    232 			/* only text in the string */
    233 			if (!*p1) {
    234 				pr->fmt = fmtp;
    235 				pr->flags = F_TEXT;
    236 				break;
    237 			}
    238 
    239 			/*
    240 			 * get precision for %s -- if have a byte count, don't
    241 			 * need it.
    242 			 */
    243 			if (fu->bcnt) {
    244 				sokay = USEBCNT;
    245 				/* skip to conversion character */
    246 				for (++p1; index(spec, *p1); ++p1);
    247 			} else {
    248 				/* skip any special chars, field width */
    249 				while (index(spec + 1, *++p1));
    250 				if (*p1 == '.' && isdigit(*++p1)) {
    251 					sokay = USEPREC;
    252 					prec = atoi(p1);
    253 					while (isdigit(*++p1));
    254 				}
    255 				else
    256 					sokay = NOTOKAY;
    257 			}
    258 
    259 			p2 = p1 + 1;		/* set end pointer */
    260 
    261 			/*
    262 			 * figure out the byte count for each conversion;
    263 			 * rewrite the format as necessary, set up blank-
    264 			 * padding for end of data.
    265 			 */
    266 			switch(*p1) {
    267 			case 'c':
    268 				pr->flags = F_CHAR;
    269 				switch(fu->bcnt) {
    270 				case 0: case 1:
    271 					pr->bcnt = 1;
    272 					break;
    273 				default:
    274 					p1[1] = '\0';
    275 					badcnt(p1);
    276 				}
    277 				break;
    278 			case 'd': case 'i':
    279 				pr->flags = F_INT;
    280 				goto sw1;
    281 			case 'l':
    282 				++p2;
    283 				switch(p1[1]) {
    284 				case 'd': case 'i':
    285 					++p1;
    286 					pr->flags = F_INT;
    287 					goto sw1;
    288 				case 'o': case 'u': case 'x': case 'X':
    289 					++p1;
    290 					pr->flags = F_UINT;
    291 					goto sw1;
    292 				default:
    293 					p1[2] = '\0';
    294 					badconv(p1);
    295 				}
    296 				/* NOTREACHED */
    297 			case 'o': case 'u': case 'x': case 'X':
    298 				pr->flags = F_UINT;
    299 sw1:				switch(fu->bcnt) {
    300 				case 0: case 4:
    301 					pr->bcnt = 4;
    302 					break;
    303 				case 1:
    304 					pr->bcnt = 1;
    305 					break;
    306 				case 2:
    307 					pr->bcnt = 2;
    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 						*p1 = 'q';
    354 						p1[1] = p1[2];
    355 						break;
    356 					default:
    357 						p1[3] = '\0';
    358 						badconv(p1);
    359 					}
    360 					break;
    361 				case 'c':
    362 					pr->flags = F_C;
    363 					/* *p1 = 'c';	set in conv_c */
    364 					goto sw2;
    365 				case 'p':
    366 					pr->flags = F_P;
    367 					*p1 = 'c';
    368 					goto sw2;
    369 				case 'u':
    370 					pr->flags = F_U;
    371 					/* *p1 = 'c';	set in conv_u */
    372 sw2:					switch(fu->bcnt) {
    373 					case 0: case 1:
    374 						pr->bcnt = 1;
    375 						break;
    376 					default:
    377 						p1[2] = '\0';
    378 						badcnt(p1);
    379 					}
    380 					break;
    381 				default:
    382 					p1[2] = '\0';
    383 					badconv(p1);
    384 				}
    385 				break;
    386 			default:
    387 				p1[1] = '\0';
    388 				badconv(p1);
    389 			}
    390 
    391 			/*
    392 			 * copy to PR format string, set conversion character
    393 			 * pointer, update original.
    394 			 */
    395 			savech = *p2;
    396 			p1[(pr->flags&F_ADDRESS)?2:1] = '\0';
    397 			if (!(pr->fmt = strdup(fmtp)))
    398 				nomem();
    399 			*p2 = savech;
    400 			pr->cchar = pr->fmt + (p1 - fmtp);
    401 			fmtp = p2;
    402 
    403 			/* only one conversion character if byte count */
    404 			if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) {
    405 				(void)fprintf(stderr,
    406 				    "hexdump: byte count with multiple conversion characters.\n");
    407 				exit(1);
    408 			}
    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->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(*p1) ? p1 : NULL;
    437 			if (p2)
    438 				pr->nospace = p2;
    439 		}
    440 		if (!fu->nextfu)
    441 			break;
    442 	}
    443 }
    444 
    445 
    446 escape(p1)
    447 	register char *p1;
    448 {
    449 	register char *p2;
    450 
    451 	/* alphabetic escape sequences have to be done in place */
    452 	for (p2 = p1;; ++p1, ++p2) {
    453 		if (!*p1) {
    454 			*p2 = *p1;
    455 			break;
    456 		}
    457 		if (*p1 == '\\')
    458 			switch(*++p1) {
    459 			case 'a':
    460 			     /* *p2 = '\a'; */
    461 				*p2 = '\007';
    462 				break;
    463 			case 'b':
    464 				*p2 = '\b';
    465 				break;
    466 			case 'f':
    467 				*p2 = '\f';
    468 				break;
    469 			case 'n':
    470 				*p2 = '\n';
    471 				break;
    472 			case 'r':
    473 				*p2 = '\r';
    474 				break;
    475 			case 't':
    476 				*p2 = '\t';
    477 				break;
    478 			case 'v':
    479 				*p2 = '\v';
    480 				break;
    481 			default:
    482 				*p2 = *p1;
    483 				break;
    484 			}
    485 	}
    486 }
    487 
    488 badcnt(s)
    489 	char *s;
    490 {
    491 	(void)fprintf(stderr,
    492 	    "hexdump: bad byte count for conversion character %s.\n", s);
    493 	exit(1);
    494 }
    495 
    496 badsfmt()
    497 {
    498 	(void)fprintf(stderr,
    499 	    "hexdump: %%s requires a precision or a byte count.\n");
    500 	exit(1);
    501 }
    502 
    503 badfmt(fmt)
    504 	char *fmt;
    505 {
    506 	(void)fprintf(stderr, "hexdump: bad format {%s}\n", fmt);
    507 	exit(1);
    508 }
    509 
    510 badconv(ch)
    511 	char *ch;
    512 {
    513 	(void)fprintf(stderr, "hexdump: bad conversion character %%%s.\n", ch);
    514 	exit(1);
    515 }
    516