source: rtems/cpukit/libmisc/shell/hexdump-display.c @ 9a77af8

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

Add HAVE_CONFIG_H support to let files receive configure defines.

  • Property mode set to 100644
File size: 9.6 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[] = "@(#)display.c   8.1 (Berkeley) 6/6/93";
41#endif
42#endif /* not lint */
43#if 0
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: src/usr.bin/hexdump/display.c,v 1.22 2004/08/04 02:47:32 tjr Exp $");
46#endif
47
48#include <sys/param.h>
49#include <sys/stat.h>
50
51#include <stdint.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60#include "hexdump.h"
61
62#if RTEMS_REMOVED
63enum _vflag vflag = FIRST;
64
65static off_t address;                   /* address/offset in stream */
66static off_t eaddress;                  /* end address */
67#endif
68
69static void print(rtems_shell_hexdump_globals*, PR *, u_char *);
70
71void
72display(rtems_shell_hexdump_globals* globals)
73{
74        FS *fs;
75        FU *fu;
76        PR *pr;
77        int cnt;
78        u_char *bp;
79        off_t saveaddress;
80        u_char savech, *savebp;
81
82        savech = 0;
83        while ((bp = get(globals)))
84                for (fs = fshead, savebp = bp, saveaddress = address; fs;
85                fs = fs->nextfs, bp = savebp, address = saveaddress)
86                    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
87                        if (fu->flags&F_IGNORE)
88                                break;
89                        for (cnt = fu->reps; cnt; --cnt)
90                            for (pr = fu->nextpr; pr; address += pr->bcnt,
91                                bp += pr->bcnt, pr = pr->nextpr) {
92                                    if (eaddress && address >= eaddress &&
93                                        !(pr->flags & (F_TEXT|F_BPAD)))
94                                            bpad(pr);
95                                    if (cnt == 1 && pr->nospace) {
96                                        savech = *pr->nospace;
97                                        *pr->nospace = '\0';
98                                    }
99                                    print(globals, pr, bp);
100                                    if (cnt == 1 && pr->nospace)
101                                        *pr->nospace = savech;
102                            }
103                    }
104        if (endfu) {
105                /*
106                 * If eaddress not set, error or file size was multiple of
107                 * blocksize, and no partial block ever found.
108                 */
109                if (!eaddress) {
110                        if (!address)
111                                return;
112                        eaddress = address;
113                }
114                for (pr = endfu->nextpr; pr; pr = pr->nextpr)
115                        switch(pr->flags) {
116                        case F_ADDRESS:
117                                (void)printf(pr->fmt, (quad_t)eaddress);
118                                break;
119                        case F_TEXT:
120                                (void)printf("%s", pr->fmt);
121                                break;
122                        }
123        }
124}
125
126static void
127print(rtems_shell_hexdump_globals* globals, PR *pr, u_char *bp)
128{
129        long double ldbl;
130           double f8;
131            float f4;
132          int16_t s2;
133           int8_t s8;
134          int32_t s4;
135        u_int16_t u2;
136        u_int32_t u4;
137        u_int64_t u8;
138
139        switch(pr->flags) {
140        case F_ADDRESS:
141                (void)printf(pr->fmt, (quad_t)address);
142                break;
143        case F_BPAD:
144                (void)printf(pr->fmt, "");
145                break;
146        case F_C:
147                conv_c(globals, pr, bp, eaddress ? eaddress - address :
148                    blocksize - address % blocksize);
149                break;
150        case F_CHAR:
151                (void)printf(pr->fmt, *bp);
152                break;
153        case F_DBL:
154                switch(pr->bcnt) {
155                case 4:
156                        bcopy(bp, &f4, sizeof(f4));
157                        (void)printf(pr->fmt, f4);
158                        break;
159                case 8:
160                        bcopy(bp, &f8, sizeof(f8));
161                        (void)printf(pr->fmt, f8);
162                        break;
163                default:
164                        if (pr->bcnt == sizeof(long double)) {
165                                bcopy(bp, &ldbl, sizeof(ldbl));
166                                (void)printf(pr->fmt, ldbl);
167                        }
168                        break;
169                }
170                break;
171        case F_INT:
172                switch(pr->bcnt) {
173                case 1:
174                        (void)printf(pr->fmt, (quad_t)(signed char)*bp);
175                        break;
176                case 2:
177                        bcopy(bp, &s2, sizeof(s2));
178                        (void)printf(pr->fmt, (quad_t)s2);
179                        break;
180                case 4:
181                        bcopy(bp, &s4, sizeof(s4));
182                        (void)printf(pr->fmt, (quad_t)s4);
183                        break;
184                case 8:
185                        bcopy(bp, &s8, sizeof(s8));
186                        (void)printf(pr->fmt, s8);
187                        break;
188                }
189                break;
190        case F_P:
191                (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
192                break;
193        case F_STR:
194                (void)printf(pr->fmt, (char *)bp);
195                break;
196        case F_TEXT:
197                (void)printf("%s", pr->fmt);
198                break;
199        case F_U:
200                conv_u(globals, pr, bp);
201                break;
202        case F_UINT:
203                switch(pr->bcnt) {
204                case 1:
205                        (void)printf(pr->fmt, (u_quad_t)*bp);
206                        break;
207                case 2:
208                        bcopy(bp, &u2, sizeof(u2));
209                        (void)printf(pr->fmt, (u_quad_t)u2);
210                        break;
211                case 4:
212                        bcopy(bp, &u4, sizeof(u4));
213                        (void)printf(pr->fmt, (u_quad_t)u4);
214                        break;
215                case 8:
216                        bcopy(bp, &u8, sizeof(u8));
217                        (void)printf(pr->fmt, u8);
218                        break;
219                }
220                break;
221        }
222}
223
224void
225bpad(PR *pr)
226{
227        static char const *spec = " -0+#";
228        char *p1, *p2;
229
230        /*
231         * Remove all conversion flags; '-' is the only one valid
232         * with %s, and it's not useful here.
233         */
234        pr->flags = F_BPAD;
235        pr->cchar[0] = 's';
236        pr->cchar[1] = '\0';
237        for (p1 = pr->fmt; *p1 != '%'; ++p1);
238        for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
239        while ((*p2++ = *p1++));
240}
241
242static char **_argv;
243
244u_char *
245get(rtems_shell_hexdump_globals* globals)
246{
247#if RTEMS_REMOVED
248        static int ateof = 1;
249        static u_char *curp, *savp;
250#endif
251        int n;
252        int need, nread;
253        int valid_save = 0;
254        u_char *tmpp;
255
256        if (!curp) {
257                if ((curp = calloc(1, blocksize)) == NULL)
258                        err(exit_jump, 1, NULL);
259                if ((savp = calloc(1, blocksize)) == NULL)
260                        err(exit_jump, 1, NULL);
261        } else {
262                tmpp = curp;
263                curp = savp;
264                savp = tmpp;
265                address += blocksize;
266                valid_save = 1;
267        }
268        for (need = blocksize, nread = 0;;) {
269                /*
270                 * if read the right number of bytes, or at EOF for one file,
271                 * and no other files are available, zero-pad the rest of the
272                 * block and set the end flag.
273                 */
274                if (!length || (ateof && !next(globals, (char **)NULL))) {
275                        if (odmode && address < skip)
276                                errx(exit_jump, 1, "cannot skip past end of input");
277                        if (need == blocksize)
278                                return((u_char *)NULL);
279                        /*
280                         * XXX bcmp() is not quite right in the presence
281                         * of multibyte characters.
282                         */
[0893220]283                        if (vflag != ALL &&
284                            valid_save &&
[e4a3d93]285                            bcmp(curp, savp, nread) == 0) {
286                                if (vflag != DUP)
287                                        (void)printf("*\n");
288                                return((u_char *)NULL);
289                        }
290                        bzero((char *)curp + nread, need);
291                        eaddress = address + nread;
292                        return(curp);
293                }
294                n = fread((char *)curp + nread, sizeof(u_char),
295                    length == -1 ? need : MIN(length, need), hdstdin);
296                if (!n) {
297                        if (ferror(hdstdin))
298                                warn("%s", _argv[-1]);
299                        ateof = 1;
300                        continue;
301                }
302                ateof = 0;
303                if (length != -1)
304                        length -= n;
305                if (!(need -= n)) {
306                        /*
307                         * XXX bcmp() is not quite right in the presence
308                         * of multibyte characters.
309                         */
310                        if (vflag == ALL || vflag == FIRST ||
311                            valid_save == 0 ||
312                            bcmp(curp, savp, blocksize) != 0) {
313                                if (vflag == DUP || vflag == FIRST)
314                                        vflag = WAIT;
315                                return(curp);
316                        }
317                        if (vflag == WAIT)
318                                (void)printf("*\n");
319                        vflag = DUP;
320                        address += blocksize;
321                        need = blocksize;
322                        nread = 0;
323                }
324                else
325                        nread += n;
326        }
327}
328
329size_t
330peek(rtems_shell_hexdump_globals* globals, u_char *buf, size_t nbytes)
331{
332        size_t n, nread;
333        int c;
334
335        if (length != -1 && nbytes > (unsigned int)length)
336                nbytes = length;
337        nread = 0;
338        while (nread < nbytes && (c = getchar()) != EOF) {
339                *buf++ = c;
340                nread++;
341        }
342        n = nread;
343        while (n-- > 0) {
344                c = *--buf;
345                ungetc(c, hdstdin);
346        }
347        return (nread);
348}
349
350int
351next(rtems_shell_hexdump_globals* globals, char **argv)
352{
353#if RTEMS_REMOVED
354        static int done;
355#endif
356        int statok;
357
358        if (argv) {
359                _argv = argv;
360                return(1);
361        }
362        for (;;) {
363                if (*_argv) {
364                        done = 1;
365      if (!hdstdin) {
366        hdstdin = malloc(sizeof(FILE));
367        if (!hdstdin)
368        {
369          errno = ENOMEM;
370          err(exit_jump, 1, "file name allocation");
371        }
[eb649786]372        memset (hdstdin, 0, sizeof(FILE));
[e4a3d93]373      }
[eb649786]374                        if (!(hdstdin = freopen(*_argv, "r", hdstdin))) {
[e4a3d93]375                                warn("%s", *_argv);
376                                exitval = 1;
377                                ++_argv;
378                                continue;
379                        }
380                        statok = 1;
381                } else {
382      errno = ECANCELED;
383      err(exit_jump, 1, "no file (stdin no supported)");
384                        if (done++)
385                                return(0);
386                        statok = 0;
387                }
388                if (skip)
389                        doskip(globals, statok ? *_argv : "stdin", statok);
390                if (*_argv)
391                        ++_argv;
392                if (!skip)
393                        return(1);
394        }
395        /* NOTREACHED */
396}
397
398void
399doskip(rtems_shell_hexdump_globals* globals, const char *fname, int statok)
400{
401        int cnt;
402        struct stat sb;
403
404        if (statok) {
405                if (fstat(fileno(hdstdin), &sb))
406                        err(exit_jump, 1, "%s", fname);
[eb649786]407    /* can seek block devices on RTEMS */
408                if (0 && S_ISREG(sb.st_mode) && skip >= sb.st_size) {
[e4a3d93]409                        address += sb.st_size;
410                        skip -= sb.st_size;
411                        return;
412                }
[eb649786]413                if (1 || S_ISREG(sb.st_mode)) {
414                        if (fseeko(hdstdin, skip, SEEK_SET))
415                                err(exit_jump, 1, "%s", fname);
416                        address += skip;
417                        skip = 0;
418                } else {
419                        for (cnt = 0; cnt < skip; ++cnt)
420                                if (getchar() == EOF)
421                                        break;
422                        address += cnt;
423                        skip -= cnt;
424                }
[e4a3d93]425        }
426}
Note: See TracBrowser for help on using the repository browser.