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

4.104.115
Last change on this file since 0893220 was e4a3d93, checked in by Chris Johns <chrisj@…>, on 06/12/09 at 05:51:43

2009-06-12 Chris Johns <chrisj@…>

  • libmisc/shell/dd-args.c, libmisc/shell/dd-conv.c, libmisc/shell/dd-conv_tab.c, libmisc/shell/dd-misc.c, libmisc/shell/dd-position.c, libmisc/shell/dd.h, libmisc/shell/extern-dd.h, libmisc/shell/hexdump-conv.c, libmisc/shell/hexdump-display.c, libmisc/shell/hexdump-odsyntax.c, libmisc/shell/hexdump-parse.c, libmisc/shell/hexdump.h, libmisc/shell/hexsyntax.c, libmisc/shell/main_dd.c, libmisc/shell/main_hexdump.c: New.
  • libmisc/Makefile.am, libmisc/shell/shellconfig.h: Add dd and hexdump commands.
  • Property mode set to 100644
File size: 12.0 KB
Line 
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
36static char sccsid[] = "@(#)parse.c     8.1 (Berkeley) 6/6/93";
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: src/usr.bin/hexdump/parse.c,v 1.14 2006/08/09 19:12:10 maxim Exp $");
39#endif
40#endif /* not lint */
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#if RTEMS_REMOVED
53FU *endfu;                                      /* format at end-of-data */
54#endif
55
56void
57addfile(rtems_shell_hexdump_globals* globals, char *name)
58{
59        unsigned char *p;
60        FILE *fp;
61        int ch;
62        char buf[2048 + 1];
63
64        if ((fp = fopen(name, "r")) == NULL)
65                err(exit_jump, 1, "%s", name);
66        while (fgets(buf, sizeof(buf), fp)) {
67                if (!(p = (unsigned char*)index(buf, '\n'))) {
68                        warnx("line too long");
69                        while ((ch = getchar()) != '\n' && ch != EOF);
70                        continue;
71                }
72                *p = '\0';
73                for (p = (unsigned char*) buf; *p && isspace(*p); ++p);
74                if (!*p || *p == '#')
75                        continue;
76                add(globals, (char*)p);
77        }
78        (void)fclose(fp);
79}
80
81void
82add(rtems_shell_hexdump_globals* globals, const char *fmt)
83{
84        unsigned const char *p, *savep;
85        static FS **nextfs;
86        FS *tfs;
87        FU *tfu, **nextfu;
88
89        /* start new linked list of format units */
90        if ((tfs = calloc(1, sizeof(FS))) == NULL)
91                err(exit_jump, 1, NULL);
92        if (!fshead)
93                fshead = tfs;
94        else
95                *nextfs = tfs;
96        nextfs = &tfs->nextfs;
97        nextfu = &tfs->nextfu;
98
99        /* take the format string and break it up into format units */
100        for (p = (unsigned const char*)fmt;;) {
101                /* skip leading white space */
102                for (; isspace(*p); ++p);
103                if (!*p)
104                        break;
105
106                /* allocate a new format unit and link it in */
107                if ((tfu = calloc(1, sizeof(FU))) == NULL)
108                        err(exit_jump, 1, NULL);
109                *nextfu = tfu;
110                nextfu = &tfu->nextfu;
111                tfu->reps = 1;
112
113                /* if leading digit, repetition count */
114                if (isdigit(*p)) {
115                        for (savep = p; isdigit(*p); ++p);
116                        if (!isspace(*p) && *p != '/')
117                                badfmt(globals, fmt);
118                        /* may overwrite either white space or slash */
119                        tfu->reps = atoi((char*)savep);
120                        tfu->flags = F_SETREP;
121                        /* skip trailing white space */
122                        for (++p; isspace(*p); ++p);
123                }
124
125                /* skip slash and trailing white space */
126                if (*p == '/')
127                        while (isspace(*++p));
128
129                /* byte count */
130                if (isdigit(*p)) {
131                        for (savep = p; isdigit(*p); ++p);
132                        if (!isspace(*p))
133                                badfmt(globals, fmt);
134                        tfu->bcnt = atoi((char*)savep);
135                        /* skip trailing white space */
136                        for (++p; isspace(*p); ++p);
137                }
138
139                /* format */
140                if (*p != '"')
141                        badfmt(globals, fmt);
142                for (savep = ++p; *p != '"';)
143                        if (*p++ == 0)
144                                badfmt(globals, fmt);
145                if (!(tfu->fmt = malloc(p - savep + 1)))
146                        err(exit_jump, 1, NULL);
147                (void) strncpy(tfu->fmt, (char*)savep, p - savep);
148                tfu->fmt[p - savep] = '\0';
149                escape(tfu->fmt);
150                p++;
151        }
152}
153
154static const char *spec = ".#-+ 0123456789";
155
156int
157size(rtems_shell_hexdump_globals* globals, FS *fs)
158{
159        FU *fu;
160        int bcnt, cursize;
161        unsigned char *fmt;
162        int prec;
163
164        /* figure out the data block size needed for each format unit */
165        for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
166                if (fu->bcnt) {
167                        cursize += fu->bcnt * fu->reps;
168                        continue;
169                }
170                for (bcnt = prec = 0, fmt = (unsigned char*) fu->fmt; *fmt; ++fmt) {
171                        if (*fmt != '%')
172                                continue;
173                        /*
174                         * skip any special chars -- save precision in
175                         * case it's a %s format.
176                         */
177                        while (index(spec + 1, *++fmt));
178                        if (*fmt == '.' && isdigit(*++fmt)) {
179                                prec = atoi((char*)fmt);
180                                while (isdigit(*++fmt));
181                        }
182                        switch(*fmt) {
183                        case 'c':
184                                bcnt += 1;
185                                break;
186                        case 'd': case 'i': case 'o': case 'u':
187                        case 'x': case 'X':
188                                bcnt += 4;
189                                break;
190                        case 'e': case 'E': case 'f': case 'g': case 'G':
191                                bcnt += 8;
192                                break;
193                        case 's':
194                                bcnt += prec;
195                                break;
196                        case '_':
197                                switch(*++fmt) {
198                                case 'c': case 'p': case 'u':
199                                        bcnt += 1;
200                                        break;
201                                }
202                        }
203                }
204                cursize += bcnt * fu->reps;
205        }
206        return (cursize);
207}
208
209void
210rewrite(rtems_shell_hexdump_globals* globals, FS *fs)
211{
212        enum { NOTOKAY, USEBCNT, USEPREC } sokay;
213        PR *pr, **nextpr;
214        FU *fu;
215        unsigned char *p1, *p2, *fmtp;
216        char savech, cs[3];
217        int nconv, prec;
218        size_t len;
219
220        nextpr = NULL;
221        prec = 0;
222
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                for (nconv = 0, fmtp = (unsigned char*)fu->fmt; *fmtp; nextpr = &pr->nextpr) {
229                        if ((pr = calloc(1, sizeof(PR))) == NULL)
230                                err(exit_jump, 1, NULL);
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 = (char*)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; index(spec, *p1); ++p1);
254                        } else {
255                                /* Skip any special chars, field width. */
256                                while (index(spec + 1, *++p1));
257                                if (*p1 == '.' && isdigit(*++p1)) {
258                                        sokay = USEPREC;
259                                        prec = atoi((char*)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(globals, (char*)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;
291isint:                          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(globals, (char*)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                                        if (fu->bcnt == sizeof(long double)) {
320                                                cs[2] = '\0';
321                                                cs[1] = cs[0];
322                                                cs[0] = 'L';
323                                                pr->bcnt = sizeof(long double);
324                                        } else {
325                                                p1[1] = '\0';
326                                                badcnt(globals, (char*)p1);
327                                        }
328                                }
329                                break;
330                        case 's':
331                                pr->flags = F_STR;
332                                switch(sokay) {
333                                case NOTOKAY:
334                                        badsfmt(globals);
335                                case USEBCNT:
336                                        pr->bcnt = fu->bcnt;
337                                        break;
338                                case USEPREC:
339                                        pr->bcnt = prec;
340                                        break;
341                                }
342                                break;
343                        case '_':
344                                ++p2;
345                                switch(p1[1]) {
346                                case 'A':
347                                        endfu = fu;
348                                        fu->flags |= F_IGNORE;
349                                        /* FALLTHROUGH */
350                                case 'a':
351                                        pr->flags = F_ADDRESS;
352                                        ++p2;
353                                        switch(p1[2]) {
354                                        case 'd': case 'o': case'x':
355                                                cs[0] = 'q';
356                                                cs[1] = p1[2];
357                                                cs[2] = '\0';
358                                                break;
359                                        default:
360                                                p1[3] = '\0';
361                                                badconv(globals, (char*)p1);
362                                        }
363                                        break;
364                                case 'c':
365                                        pr->flags = F_C;
366                                        /* cs[0] = 'c'; set in conv_c */
367                                        goto isint2;
368                                case 'p':
369                                        pr->flags = F_P;
370                                        cs[0] = 'c';
371                                        goto isint2;
372                                case 'u':
373                                        pr->flags = F_U;
374                                        /* cs[0] = 'c'; set in conv_u */
375isint2:                                 switch(fu->bcnt) {
376                                        case 0: case 1:
377                                                pr->bcnt = 1;
378                                                break;
379                                        default:
380                                                p1[2] = '\0';
381                                                badcnt(globals, (char*)p1);
382                                        }
383                                        break;
384                                default:
385                                        p1[2] = '\0';
386                                        badconv(globals, (char*)p1);
387                                }
388                                break;
389                        default:
390                                p1[1] = '\0';
391                                badconv(globals, (char*)p1);
392                        }
393
394                        /*
395                         * Copy to PR format string, set conversion character
396                         * pointer, update original.
397                         */
398                        savech = *p2;
399                        p1[0] = '\0';
400                        len = strlen((char*)fmtp) + strlen(cs) + 1;
401                        if ((pr->fmt = calloc(1, len)) == NULL)
402                                err(exit_jump, 1, NULL);
403                        snprintf(pr->fmt, len, "%s%s", fmtp, cs);
404                        *p2 = savech;
405                        pr->cchar = pr->fmt + (p1 - fmtp);
406                        fmtp = p2;
407
408                        /* Only one conversion character if byte count. */
409                        if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
410        errx(exit_jump, 1, "byte count with multiple conversion characters");
411                }
412                /*
413                 * If format unit byte count not specified, figure it out
414                 * so can adjust rep count later.
415                 */
416                if (!fu->bcnt)
417                        for (pr = fu->nextpr; pr; pr = pr->nextpr)
418                                fu->bcnt += pr->bcnt;
419        }
420        /*
421         * If the format string interprets any data at all, and it's
422         * not the same as the blocksize, and its last format unit
423         * interprets any data at all, and has no iteration count,
424         * repeat it as necessary.
425         *
426         * If, rep count is greater than 1, no trailing whitespace
427         * gets output from the last iteration of the format unit.
428         */
429        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
430                if (!fu->nextfu && fs->bcnt < blocksize &&
431                    !(fu->flags&F_SETREP) && fu->bcnt)
432                        fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
433                if (fu->reps > 1) {
434                        for (pr = fu->nextpr;; pr = pr->nextpr)
435                                if (!pr->nextpr)
436                                        break;
437                        for (p1 = (unsigned char*)pr->fmt, p2 = NULL; *p1; ++p1)
438                                p2 = isspace(*p1) ? p1 : NULL;
439                        if (p2)
440                                pr->nospace = (char*)p2;
441                }
442        }
443#ifdef DEBUG
444        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
445                (void)printf("fmt:");
446                for (pr = fu->nextpr; pr; pr = pr->nextpr)
447                        (void)printf(" {%s}", pr->fmt);
448                (void)printf("\n");
449        }
450#endif
451}
452
453void
454escape(char *p1)
455{
456        char *p2;
457
458        /* alphabetic escape sequences have to be done in place */
459        for (p2 = p1;; ++p1, ++p2) {
460                if (!*p1) {
461                        *p2 = *p1;
462                        break;
463                }
464                if (*p1 == '\\')
465                        switch(*++p1) {
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        }
493}
494
495void
496badcnt(rtems_shell_hexdump_globals* globals, char *s)
497{
498        errx(exit_jump, 1, "%s: bad byte count", s);
499}
500
501void
502badsfmt(rtems_shell_hexdump_globals* globals)
503{
504        errx(exit_jump, 1, "%%s: requires a precision or a byte count");
505}
506
507void
508badfmt(rtems_shell_hexdump_globals* globals, const char *fmt)
509{
510        errx(exit_jump, 1, "\"%s\": bad format", fmt);
511}
512
513void
514badconv(rtems_shell_hexdump_globals* globals, char *ch)
515{
516        errx(exit_jump, 1, "%%%s: bad conversion character", ch);
517}
Note: See TracBrowser for help on using the repository browser.