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

4.104.115
Last change on this file since 040e7950 was 3faa7a29, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/10 at 20:25:18

2010-01-20 Joel Sherrill <joel.sherrill@…>

Coverity Id 30

  • libmisc/shell/hexdump-parse.c:
  • 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        pr = NULL;
221        nextpr = NULL;
222        prec = 0;
223
224        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
225                /*
226                 * Break each format unit into print units; each conversion
227                 * character gets its own.
228                 */
229                for (nconv = 0, fmtp = (unsigned char*)fu->fmt; *fmtp; nextpr = &pr->nextpr) {
230                        if ((pr = calloc(1, sizeof(PR))) == NULL)
231                                err(exit_jump, 1, NULL);
232                        if (!fu->nextpr)
233                                fu->nextpr = pr;
234                        else {
235                                if (nextpr)
236                                        *nextpr = pr;
237                        }
238
239                        /* Skip preceding text and up to the next % sign. */
240                        for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
241
242                        /* Only text in the string. */
243                        if (!*p1) {
244                                pr->fmt = (char*)fmtp;
245                                pr->flags = F_TEXT;
246                                break;
247                        }
248
249                        /*
250                         * Get precision for %s -- if have a byte count, don't
251                         * need it.
252                         */
253                        if (fu->bcnt) {
254                                sokay = USEBCNT;
255                                /* Skip to conversion character. */
256                                for (++p1; index(spec, *p1); ++p1);
257                        } else {
258                                /* Skip any special chars, field width. */
259                                while (index(spec + 1, *++p1));
260                                if (*p1 == '.' && isdigit(*++p1)) {
261                                        sokay = USEPREC;
262                                        prec = atoi((char*)p1);
263                                        while (isdigit(*++p1));
264                                } else
265                                        sokay = NOTOKAY;
266                        }
267
268                        p2 = p1 + 1;            /* Set end pointer. */
269                        cs[0] = *p1;            /* Set conversion string. */
270                        cs[1] = '\0';
271
272                        /*
273                         * Figure out the byte count for each conversion;
274                         * rewrite the format as necessary, set up blank-
275                         * padding for end of data.
276                         */
277                        switch(cs[0]) {
278                        case 'c':
279                                pr->flags = F_CHAR;
280                                switch(fu->bcnt) {
281                                case 0: case 1:
282                                        pr->bcnt = 1;
283                                        break;
284                                default:
285                                        p1[1] = '\0';
286                                        badcnt(globals, (char*)p1);
287                                }
288                                break;
289                        case 'd': case 'i':
290                                pr->flags = F_INT;
291                                goto isint;
292                        case 'o': case 'u': case 'x': case 'X':
293                                pr->flags = F_UINT;
294isint:                          cs[2] = '\0';
295                                cs[1] = cs[0];
296                                cs[0] = 'q';
297                                switch(fu->bcnt) {
298                                case 0: case 4:
299                                        pr->bcnt = 4;
300                                        break;
301                                case 1:
302                                        pr->bcnt = 1;
303                                        break;
304                                case 2:
305                                        pr->bcnt = 2;
306                                        break;
307                                default:
308                                        p1[1] = '\0';
309                                        badcnt(globals, (char*)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                                        if (fu->bcnt == sizeof(long double)) {
323                                                cs[2] = '\0';
324                                                cs[1] = cs[0];
325                                                cs[0] = 'L';
326                                                pr->bcnt = sizeof(long double);
327                                        } else {
328                                                p1[1] = '\0';
329                                                badcnt(globals, (char*)p1);
330                                        }
331                                }
332                                break;
333                        case 's':
334                                pr->flags = F_STR;
335                                switch(sokay) {
336                                case NOTOKAY:
337                                        badsfmt(globals);
338                                case USEBCNT:
339                                        pr->bcnt = fu->bcnt;
340                                        break;
341                                case USEPREC:
342                                        pr->bcnt = prec;
343                                        break;
344                                }
345                                break;
346                        case '_':
347                                ++p2;
348                                switch(p1[1]) {
349                                case 'A':
350                                        endfu = fu;
351                                        fu->flags |= F_IGNORE;
352                                        /* FALLTHROUGH */
353                                case 'a':
354                                        pr->flags = F_ADDRESS;
355                                        ++p2;
356                                        switch(p1[2]) {
357                                        case 'd': case 'o': case'x':
358                                                cs[0] = 'q';
359                                                cs[1] = p1[2];
360                                                cs[2] = '\0';
361                                                break;
362                                        default:
363                                                p1[3] = '\0';
364                                                badconv(globals, (char*)p1);
365                                        }
366                                        break;
367                                case 'c':
368                                        pr->flags = F_C;
369                                        /* cs[0] = 'c'; set in conv_c */
370                                        goto isint2;
371                                case 'p':
372                                        pr->flags = F_P;
373                                        cs[0] = 'c';
374                                        goto isint2;
375                                case 'u':
376                                        pr->flags = F_U;
377                                        /* cs[0] = 'c'; set in conv_u */
378isint2:                                 switch(fu->bcnt) {
379                                        case 0: case 1:
380                                                pr->bcnt = 1;
381                                                break;
382                                        default:
383                                                p1[2] = '\0';
384                                                badcnt(globals, (char*)p1);
385                                        }
386                                        break;
387                                default:
388                                        p1[2] = '\0';
389                                        badconv(globals, (char*)p1);
390                                }
391                                break;
392                        default:
393                                p1[1] = '\0';
394                                badconv(globals, (char*)p1);
395                        }
396
397                        /*
398                         * Copy to PR format string, set conversion character
399                         * pointer, update original.
400                         */
401                        savech = *p2;
402                        p1[0] = '\0';
403                        len = strlen((char*)fmtp) + strlen(cs) + 1;
404                        if ((pr->fmt = calloc(1, len)) == NULL)
405                                err(exit_jump, 1, NULL);
406                        snprintf(pr->fmt, len, "%s%s", fmtp, cs);
407                        *p2 = savech;
408                        pr->cchar = pr->fmt + (p1 - fmtp);
409                        fmtp = p2;
410
411                        /* Only one conversion character if byte count. */
412                        if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
413        errx(exit_jump, 1, "byte count with multiple conversion characters");
414                }
415                /*
416                 * If format unit byte count not specified, figure it out
417                 * so can adjust rep count later.
418                 */
419                if (!fu->bcnt)
420                        for (pr = fu->nextpr; pr; pr = pr->nextpr)
421                                fu->bcnt += pr->bcnt;
422        }
423        if (pr) {
424                free(pr);
425                pr = NULL;
426        }
427        /*
428         * If the format string interprets any data at all, and it's
429         * not the same as the blocksize, and its last format unit
430         * interprets any data at all, and has no iteration count,
431         * repeat it as necessary.
432         *
433         * If, rep count is greater than 1, no trailing whitespace
434         * gets output from the last iteration of the format unit.
435         */
436        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
437                if (!fu->nextfu && fs->bcnt < blocksize &&
438                    !(fu->flags&F_SETREP) && fu->bcnt)
439                        fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
440                if (fu->reps > 1) {
441                        for (pr = fu->nextpr;; pr = pr->nextpr)
442                                if (!pr->nextpr)
443                                        break;
444                        for (p1 = (unsigned char*)pr->fmt, p2 = NULL; *p1; ++p1)
445                                p2 = isspace(*p1) ? p1 : NULL;
446                        if (p2)
447                                pr->nospace = (char*)p2;
448                }
449        }
450#ifdef DEBUG
451        for (fu = fs->nextfu; fu; fu = fu->nextfu) {
452                (void)printf("fmt:");
453                for (pr = fu->nextpr; pr; pr = pr->nextpr)
454                        (void)printf(" {%s}", pr->fmt);
455                (void)printf("\n");
456        }
457#endif
458}
459
460void
461escape(char *p1)
462{
463        char *p2;
464
465        /* alphabetic escape sequences have to be done in place */
466        for (p2 = p1;; ++p1, ++p2) {
467                if (!*p1) {
468                        *p2 = *p1;
469                        break;
470                }
471                if (*p1 == '\\')
472                        switch(*++p1) {
473                        case 'a':
474                             /* *p2 = '\a'; */
475                                *p2 = '\007';
476                                break;
477                        case 'b':
478                                *p2 = '\b';
479                                break;
480                        case 'f':
481                                *p2 = '\f';
482                                break;
483                        case 'n':
484                                *p2 = '\n';
485                                break;
486                        case 'r':
487                                *p2 = '\r';
488                                break;
489                        case 't':
490                                *p2 = '\t';
491                                break;
492                        case 'v':
493                                *p2 = '\v';
494                                break;
495                        default:
496                                *p2 = *p1;
497                                break;
498                        }
499        }
500}
501
502void
503badcnt(rtems_shell_hexdump_globals* globals, char *s)
504{
505        errx(exit_jump, 1, "%s: bad byte count", s);
506}
507
508void
509badsfmt(rtems_shell_hexdump_globals* globals)
510{
511        errx(exit_jump, 1, "%%s: requires a precision or a byte count");
512}
513
514void
515badfmt(rtems_shell_hexdump_globals* globals, const char *fmt)
516{
517        errx(exit_jump, 1, "\"%s\": bad format", fmt);
518}
519
520void
521badconv(rtems_shell_hexdump_globals* globals, char *ch)
522{
523        errx(exit_jump, 1, "%%%s: bad conversion character", ch);
524}
Note: See TracBrowser for help on using the repository browser.