parse.c revision 1.5 1 /* $NetBSD: parse.c,v 1.5 1997/07/11 06:28:30 mikel 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 #if 0
38 static char sccsid[] = "from: @(#)parse.c 5.6 (Berkeley) 3/9/91";
39 #else
40 static char rcsid[] = "$NetBSD: parse.c,v 1.5 1997/07/11 06:28:30 mikel Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/file.h>
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "hexdump.h"
51
52 FU *endfu; /* format at end-of-data */
53
54 void
55 addfile(name)
56 char *name;
57 {
58 register char *p;
59 FILE *fp;
60 int ch;
61 char buf[2048 + 1];
62
63 if (!(fp = fopen(name, "r"))) {
64 (void)fprintf(stderr, "hexdump: can't read %s.\n", name);
65 exit(1);
66 }
67 while (fgets(buf, sizeof(buf), fp)) {
68 if (!(p = strchr(buf, '\n'))) {
69 (void)fprintf(stderr, "hexdump: line too long.\n");
70 while ((ch = getchar()) != '\n' && ch != EOF);
71 continue;
72 }
73 *p = '\0';
74 for (p = buf; *p && isspace(*p); ++p);
75 if (!*p || *p == '#')
76 continue;
77 add(p);
78 }
79 (void)fclose(fp);
80 }
81
82 void
83 add(fmt)
84 char *fmt;
85 {
86 register char *p;
87 static FS **nextfs;
88 FS *tfs;
89 FU *tfu, **nextfu;
90 char *savep;
91
92 /* start new linked list of format units */
93 /* NOSTRICT */
94 tfs = (FS *)emalloc(sizeof(FS));
95 if (!fshead)
96 fshead = tfs;
97 else
98 *nextfs = tfs;
99 nextfs = &tfs->nextfs;
100 nextfu = &tfs->nextfu;
101
102 /* take the format string and break it up into format units */
103 for (p = fmt;;) {
104 /* skip leading white space */
105 for (; isspace(*p); ++p);
106 if (!*p)
107 break;
108
109 /* allocate a new format unit and link it in */
110 /* NOSTRICT */
111 tfu = (FU *)emalloc(sizeof(FU));
112 *nextfu = tfu;
113 nextfu = &tfu->nextfu;
114 tfu->reps = 1;
115
116 /* if leading digit, repetition count */
117 if (isdigit(*p)) {
118 for (savep = p; isdigit(*p); ++p);
119 if (!isspace(*p) && *p != '/')
120 badfmt(fmt);
121 /* may overwrite either white space or slash */
122 tfu->reps = atoi(savep);
123 tfu->flags = F_SETREP;
124 /* skip trailing white space */
125 for (++p; isspace(*p); ++p);
126 }
127
128 /* skip slash and trailing white space */
129 if (*p == '/')
130 while (isspace(*++p));
131
132 /* byte count */
133 if (isdigit(*p)) {
134 for (savep = p; isdigit(*p); ++p);
135 if (!isspace(*p))
136 badfmt(fmt);
137 tfu->bcnt = atoi(savep);
138 /* skip trailing white space */
139 for (++p; isspace(*p); ++p);
140 }
141
142 /* format */
143 if (*p != '"')
144 badfmt(fmt);
145 for (savep = ++p; *p != '"';)
146 if (*p++ == 0)
147 badfmt(fmt);
148 if (!(tfu->fmt = malloc(p - savep + 1)))
149 nomem();
150 (void) strncpy(tfu->fmt, savep, p - savep);
151 tfu->fmt[p - savep] = '\0';
152 escape(tfu->fmt);
153 p++;
154 }
155 }
156
157 static const char *spec = ".#-+ 0123456789";
158
159 int
160 size(fs)
161 FS *fs;
162 {
163 register FU *fu;
164 register int bcnt, cursize;
165 register 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 (strchr(spec + 1, *++fmt));
182 if (*fmt == '.' && isdigit(*++fmt)) {
183 prec = atoi(fmt);
184 while (isdigit(*++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)
215 FS *fs;
216 {
217 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
218 register PR *pr, **nextpr;
219 register FU *fu;
220 register char *p1, *p2;
221 char savech, *fmtp;
222 int nconv, prec;
223
224 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
225 /*
226 * break each format unit into print units; each
227 * conversion character gets its own.
228 */
229 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
230 /* NOSTRICT */
231 pr = (PR *)emalloc(sizeof(PR));
232 if (!fu->nextpr)
233 fu->nextpr = pr;
234 else
235 *nextpr = pr;
236
237 /* skip preceding text and up to the next % sign */
238 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
239
240 /* only text in the string */
241 if (!*p1) {
242 pr->fmt = fmtp;
243 pr->flags = F_TEXT;
244 break;
245 }
246
247 /*
248 * get precision for %s -- if have a byte count, don't
249 * need it.
250 */
251 if (fu->bcnt) {
252 sokay = USEBCNT;
253 /* skip to conversion character */
254 for (++p1; strchr(spec, *p1); ++p1);
255 } else {
256 /* skip any special chars, field width */
257 while (strchr(spec + 1, *++p1));
258 if (*p1 == '.' && isdigit(*++p1)) {
259 sokay = USEPREC;
260 prec = atoi(p1);
261 while (isdigit(*++p1));
262 }
263 else
264 sokay = NOTOKAY;
265 }
266
267 p2 = p1 + 1; /* set end pointer */
268
269 /*
270 * figure out the byte count for each conversion;
271 * rewrite the format as necessary, set up blank-
272 * padding for end of data.
273 */
274 switch(*p1) {
275 case 'c':
276 pr->flags = F_CHAR;
277 switch(fu->bcnt) {
278 case 0: case 1:
279 pr->bcnt = 1;
280 break;
281 default:
282 p1[1] = '\0';
283 badcnt(p1);
284 }
285 break;
286 case 'd': case 'i':
287 pr->flags = F_INT;
288 goto sw1;
289 case 'l':
290 ++p2;
291 switch(p1[1]) {
292 case 'd': case 'i':
293 ++p1;
294 pr->flags = F_INT;
295 goto sw1;
296 case 'o': case 'u': case 'x': case 'X':
297 ++p1;
298 pr->flags = F_UINT;
299 goto sw1;
300 default:
301 p1[2] = '\0';
302 badconv(p1);
303 }
304 /* NOTREACHED */
305 case 'o': case 'u': case 'x': case 'X':
306 pr->flags = F_UINT;
307 sw1: switch(fu->bcnt) {
308 case 0: case 4:
309 pr->bcnt = 4;
310 break;
311 case 1:
312 pr->bcnt = 1;
313 break;
314 case 2:
315 pr->bcnt = 2;
316 break;
317 default:
318 p1[1] = '\0';
319 badcnt(p1);
320 }
321 break;
322 case 'e': case 'E': case 'f': case 'g': case 'G':
323 pr->flags = F_DBL;
324 switch(fu->bcnt) {
325 case 0: case 8:
326 pr->bcnt = 8;
327 break;
328 case 4:
329 pr->bcnt = 4;
330 break;
331 default:
332 p1[1] = '\0';
333 badcnt(p1);
334 }
335 break;
336 case 's':
337 pr->flags = F_STR;
338 switch(sokay) {
339 case NOTOKAY:
340 badsfmt();
341 case USEBCNT:
342 pr->bcnt = fu->bcnt;
343 break;
344 case USEPREC:
345 pr->bcnt = prec;
346 break;
347 }
348 break;
349 case '_':
350 ++p2;
351 switch(p1[1]) {
352 case 'A':
353 endfu = fu;
354 fu->flags |= F_IGNORE;
355 /* FALLTHROUGH */
356 case 'a':
357 pr->flags = F_ADDRESS;
358 ++p2;
359 switch(p1[2]) {
360 case 'd': case 'o': case'x':
361 *p1 = 'q';
362 p1[1] = p1[2];
363 break;
364 default:
365 p1[3] = '\0';
366 badconv(p1);
367 }
368 break;
369 case 'c':
370 pr->flags = F_C;
371 /* *p1 = 'c'; set in conv_c */
372 goto sw2;
373 case 'p':
374 pr->flags = F_P;
375 *p1 = 'c';
376 goto sw2;
377 case 'u':
378 pr->flags = F_U;
379 /* *p1 = 'c'; set in conv_u */
380 sw2: switch(fu->bcnt) {
381 case 0: case 1:
382 pr->bcnt = 1;
383 break;
384 default:
385 p1[2] = '\0';
386 badcnt(p1);
387 }
388 break;
389 default:
390 p1[2] = '\0';
391 badconv(p1);
392 }
393 break;
394 default:
395 p1[1] = '\0';
396 badconv(p1);
397 }
398
399 /*
400 * copy to PR format string, set conversion character
401 * pointer, update original.
402 */
403 savech = *p2;
404 p1[(pr->flags&F_ADDRESS)?2:1] = '\0';
405 if (!(pr->fmt = strdup(fmtp)))
406 nomem();
407 *p2 = savech;
408 pr->cchar = pr->fmt + (p1 - fmtp);
409 fmtp = p2;
410
411 /* only one conversion character if byte count */
412 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) {
413 (void)fprintf(stderr,
414 "hexdump: byte count with multiple conversion characters.\n");
415 exit(1);
416 }
417 }
418 /*
419 * if format unit byte count not specified, figure it out
420 * so can adjust rep count later.
421 */
422 if (!fu->bcnt)
423 for (pr = fu->nextpr; pr; pr = pr->nextpr)
424 fu->bcnt += pr->bcnt;
425 }
426 /*
427 * if the format string interprets any data at all, and it's
428 * not the same as the blocksize, and its last format unit
429 * interprets any data at all, and has no iteration count,
430 * repeat it as necessary.
431 *
432 * if, rep count is greater than 1, no trailing whitespace
433 * gets output from the last iteration of the format unit.
434 */
435 for (fu = fs->nextfu;; fu = fu->nextfu) {
436 if (!fu->nextfu && fs->bcnt < blocksize &&
437 !(fu->flags&F_SETREP) && fu->bcnt)
438 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
439 if (fu->reps > 1) {
440 for (pr = fu->nextpr;; pr = pr->nextpr)
441 if (!pr->nextpr)
442 break;
443 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
444 p2 = isspace(*p1) ? p1 : NULL;
445 if (p2)
446 pr->nospace = p2;
447 }
448 if (!fu->nextfu)
449 break;
450 }
451 }
452
453 void
454 escape(p1)
455 register char *p1;
456 {
457 register char *p2;
458
459 /* alphabetic escape sequences have to be done in place */
460 for (p2 = p1;; ++p1, ++p2) {
461 if (!*p1) {
462 *p2 = *p1;
463 break;
464 }
465 if (*p1 == '\\')
466 switch(*++p1) {
467 case 'a':
468 /* *p2 = '\a'; */
469 *p2 = '\007';
470 break;
471 case 'b':
472 *p2 = '\b';
473 break;
474 case 'f':
475 *p2 = '\f';
476 break;
477 case 'n':
478 *p2 = '\n';
479 break;
480 case 'r':
481 *p2 = '\r';
482 break;
483 case 't':
484 *p2 = '\t';
485 break;
486 case 'v':
487 *p2 = '\v';
488 break;
489 default:
490 *p2 = *p1;
491 break;
492 }
493 }
494 }
495
496 void
497 badcnt(s)
498 char *s;
499 {
500 (void)fprintf(stderr,
501 "hexdump: bad byte count for conversion character %s.\n", s);
502 exit(1);
503 }
504
505 void
506 badsfmt()
507 {
508 (void)fprintf(stderr,
509 "hexdump: %%s requires a precision or a byte count.\n");
510 exit(1);
511 }
512
513 void
514 badfmt(fmt)
515 char *fmt;
516 {
517 (void)fprintf(stderr, "hexdump: bad format {%s}\n", fmt);
518 exit(1);
519 }
520
521 void
522 badconv(ch)
523 char *ch;
524 {
525 (void)fprintf(stderr, "hexdump: bad conversion character %%%s.\n", ch);
526 exit(1);
527 }
528