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