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