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

5
Last change on this file since 2806e10 was 5b1f1979, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/02/11 at 04:21:26

2011-02-02 Ralf Corsépius <ralf.corsepius@…>

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