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