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