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