source: rtems/cpukit/libmisc/shell/hexdump-parse.c @ c648ca5

4.104.115
Last change on this file since c648ca5 was c648ca5, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/26/10 at 17:42:24

Add HAVE_STRINGS_H for better POSIX compliance.

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