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