parse.c revision 1.6 1 /* $NetBSD: parse.c,v 1.6 1997/10/18 13:54:39 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. 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[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: parse.c,v 1.6 1997/10/18 13:54:39 mrg Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/file.h>
46
47 #include <ctype.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "hexdump.h"
55
56 FU *endfu; /* format at end-of-data */
57
58 void
59 addfile(name)
60 char *name;
61 {
62 register char *p;
63 FILE *fp;
64 int ch;
65 char buf[2048 + 1];
66
67 if ((fp = fopen(name, "r")) == NULL)
68 err("%s: %s\n", name, strerror(errno));
69 while (fgets(buf, sizeof(buf), fp)) {
70 if (!(p = strchr(buf, '\n'))) {
71 (void)fprintf(stderr, "hexdump: line too long.\n");
72 while ((ch = getchar()) != '\n' && ch != EOF);
73 continue;
74 }
75 *p = '\0';
76 for (p = buf; *p && isspace(*p); ++p);
77 if (!*p || *p == '#')
78 continue;
79 add(p);
80 }
81 (void)fclose(fp);
82 }
83
84 void
85 add(fmt)
86 char *fmt;
87 {
88 register char *p;
89 static FS **nextfs;
90 FS *tfs;
91 FU *tfu, **nextfu;
92 char *savep;
93
94 /* start new linked list of format units */
95 tfs = emalloc(sizeof(FS));
96 if (!fshead)
97 fshead = tfs;
98 else
99 *nextfs = tfs;
100 nextfs = &tfs->nextfs;
101 nextfu = &tfs->nextfu;
102
103 /* take the format string and break it up into format units */
104 for (p = fmt;;) {
105 /* skip leading white space */
106 for (; isspace(*p); ++p);
107 if (!*p)
108 break;
109
110 /* allocate a new format unit and link it in */
111 tfu = 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, cs[3];
222 int nconv, prec;
223
224 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
225 /*
226 * Break each format unit into print units; each conversion
227 * character gets its own.
228 */
229 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
230 pr = emalloc(sizeof(PR));
231 if (!fu->nextpr)
232 fu->nextpr = pr;
233 else
234 *nextpr = pr;
235
236 /* Skip preceding text and up to the next % sign. */
237 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
238
239 /* Only text in the string. */
240 if (!*p1) {
241 pr->fmt = fmtp;
242 pr->flags = F_TEXT;
243 break;
244 }
245
246 /*
247 * Get precision for %s -- if have a byte count, don't
248 * need it.
249 */
250 if (fu->bcnt) {
251 sokay = USEBCNT;
252 /* Skip to conversion character. */
253 for (++p1; strchr(spec, *p1); ++p1);
254 } else {
255 /* Skip any special chars, field width. */
256 while (strchr(spec + 1, *++p1));
257 if (*p1 == '.' && isdigit(*++p1)) {
258 sokay = USEPREC;
259 prec = atoi(p1);
260 while (isdigit(*++p1));
261 } else
262 sokay = NOTOKAY;
263 }
264
265 p2 = p1 + 1; /* Set end pointer. */
266 cs[0] = *p1; /* Set conversion string. */
267 cs[1] = '\0';
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(cs[0]) {
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 isint;
289 case 'o': case 'u': case 'x': case 'X':
290 pr->flags = F_UINT;
291 isint: cs[2] = '\0';
292 cs[1] = cs[0];
293 cs[0] = 'q';
294 switch(fu->bcnt) {
295 case 0: case 4:
296 pr->bcnt = 4;
297 break;
298 case 1:
299 pr->bcnt = 1;
300 break;
301 case 2:
302 pr->bcnt = 2;
303 break;
304 default:
305 p1[1] = '\0';
306 badcnt(p1);
307 }
308 break;
309 case 'e': case 'E': case 'f': case 'g': case 'G':
310 pr->flags = F_DBL;
311 switch(fu->bcnt) {
312 case 0: case 8:
313 pr->bcnt = 8;
314 break;
315 case 4:
316 pr->bcnt = 4;
317 break;
318 default:
319 p1[1] = '\0';
320 badcnt(p1);
321 }
322 break;
323 case 's':
324 pr->flags = F_STR;
325 switch(sokay) {
326 case NOTOKAY:
327 badsfmt();
328 case USEBCNT:
329 pr->bcnt = fu->bcnt;
330 break;
331 case USEPREC:
332 pr->bcnt = prec;
333 break;
334 }
335 break;
336 case '_':
337 ++p2;
338 switch(p1[1]) {
339 case 'A':
340 endfu = fu;
341 fu->flags |= F_IGNORE;
342 /* FALLTHROUGH */
343 case 'a':
344 pr->flags = F_ADDRESS;
345 ++p2;
346 switch(p1[2]) {
347 case 'd': case 'o': case'x':
348 cs[0] = 'q';
349 cs[1] = p1[2];
350 cs[2] = '\0';
351 break;
352 default:
353 p1[3] = '\0';
354 badconv(p1);
355 }
356 break;
357 case 'c':
358 pr->flags = F_C;
359 /* cs[0] = 'c'; set in conv_c */
360 goto isint2;
361 case 'p':
362 pr->flags = F_P;
363 cs[0] = 'c';
364 goto isint2;
365 case 'u':
366 pr->flags = F_U;
367 /* cs[0] = 'c'; set in conv_u */
368 isint2: switch(fu->bcnt) {
369 case 0: case 1:
370 pr->bcnt = 1;
371 break;
372 default:
373 p1[2] = '\0';
374 badcnt(p1);
375 }
376 break;
377 default:
378 p1[2] = '\0';
379 badconv(p1);
380 }
381 break;
382 default:
383 p1[1] = '\0';
384 badconv(p1);
385 }
386
387 /*
388 * Copy to PR format string, set conversion character
389 * pointer, update original.
390 */
391 savech = *p2;
392 p1[0] = '\0';
393 pr->fmt = emalloc(strlen(fmtp) + 2);
394 (void)strcpy(pr->fmt, fmtp);
395 (void)strcat(pr->fmt, cs);
396 *p2 = savech;
397 pr->cchar = pr->fmt + (p1 - fmtp);
398 fmtp = p2;
399
400 /* Only one conversion character if byte count. */
401 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
402 err("byte count with multiple conversion characters");
403 }
404 /*
405 * If format unit byte count not specified, figure it out
406 * so can adjust rep count later.
407 */
408 if (!fu->bcnt)
409 for (pr = fu->nextpr; pr; pr = pr->nextpr)
410 fu->bcnt += pr->bcnt;
411 }
412 /*
413 * If the format string interprets any data at all, and it's
414 * not the same as the blocksize, and its last format unit
415 * interprets any data at all, and has no iteration count,
416 * repeat it as necessary.
417 *
418 * If, rep count is greater than 1, no trailing whitespace
419 * gets output from the last iteration of the format unit.
420 */
421 for (fu = fs->nextfu;; fu = fu->nextfu) {
422 if (!fu->nextfu && fs->bcnt < blocksize &&
423 !(fu->flags&F_SETREP) && fu->bcnt)
424 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
425 if (fu->reps > 1) {
426 for (pr = fu->nextpr;; pr = pr->nextpr)
427 if (!pr->nextpr)
428 break;
429 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
430 p2 = isspace(*p1) ? p1 : NULL;
431 if (p2)
432 pr->nospace = p2;
433 }
434 if (!fu->nextfu)
435 break;
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 err("%s: bad byte count", s);
495 }
496
497 void
498 badsfmt()
499 {
500 err("%%s: requires a precision or a byte count\n");
501 }
502
503 void
504 badfmt(fmt)
505 char *fmt;
506 {
507 err("\"%s\": bad format\n", fmt);
508 }
509
510 void
511 badconv(ch)
512 char *ch;
513 {
514 err("%%%s: bad conversion character\n", ch);
515 }
516