source: rtems/cpukit/libmisc/shell/hexdump-conv.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: 5.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#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#ifndef lint
39static const char sccsid[] = "@(#)conv.c        8.1 (Berkeley) 6/6/93";
40#endif /* not lint */
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: src/usr.bin/hexdump/conv.c,v 1.9 2006/07/31 14:17:04 jkoshy Exp $");
43
44#include <sys/types.h>
45
46#include <assert.h>
47#include <ctype.h>
48#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <wchar.h>
53#include <wctype.h>
54#include "hexdump.h"
55
56void
57conv_c(rtems_shell_hexdump_globals* globals, PR *pr, u_char *p, size_t bufsize)
58{
59        char buf[10];
60        char const *str;
61        wchar_t wc;
62        size_t clen, oclen;
63        int converr, pad, width;
64        char peekbuf[MB_LEN_MAX];
65
66  printf("MB_LEN_MAX=%i\n", MB_LEN_MAX);
67  return;
68        if (pr->mbleft > 0) {
69                str = "**";
70                pr->mbleft--;
71                goto strpr;
72        }
73
74        switch(*p) {
75        case '\0':
76                str = "\\0";
77                goto strpr;
78        /* case '\a': */
79        case '\007':
80                str = "\\a";
81                goto strpr;
82        case '\b':
83                str = "\\b";
84                goto strpr;
85        case '\f':
86                str = "\\f";
87                goto strpr;
88        case '\n':
89                str = "\\n";
90                goto strpr;
91        case '\r':
92                str = "\\r";
93                goto strpr;
94        case '\t':
95                str = "\\t";
96                goto strpr;
97        case '\v':
98                str = "\\v";
99                goto strpr;
100        default:
101                break;
102        }
103        /*
104         * Multibyte characters are disabled for hexdump(1) for backwards
105         * compatibility and consistency (none of its other output formats
106         * recognize them correctly).
107         */
108        converr = 0;
109        if (odmode && MB_CUR_MAX > 1) {
110                oclen = 0;
111retry:
112                clen = mbrtowc(&wc, (char*)p, bufsize, &pr->mbstate);
113                if (clen == 0)
114                        clen = 1;
115                else if (clen == (size_t)-1 || (clen == (size_t)-2 &&
116                    buf == peekbuf)) {
117                        memset(&pr->mbstate, 0, sizeof(pr->mbstate));
118                        wc = *p;
119                        clen = 1;
120                        converr = 1;
121                } else if (clen == (size_t)-2) {
122                        /*
123                         * Incomplete character; peek ahead and see if we
124                         * can complete it.
125                         */
126                        oclen = bufsize;
127                        bufsize = peek(globals, p = (u_char*)peekbuf, MB_CUR_MAX);
128                        goto retry;
129                }
130                clen += oclen;
131        } else {
132                wc = *p;
133                clen = 1;
134        }
135        if (!converr && iswprint(wc)) {
136                if (!odmode) {
137                        *pr->cchar = 'c';
138                        (void)printf(pr->fmt, (int)wc);
139                } else {
140                        *pr->cchar = 'C';
141                        assert(strcmp(pr->fmt, "%3C") == 0);
142                        width = wcwidth(wc);
143                        assert(width >= 0);
144                        pad = 3 - width;
145                        if (pad < 0)
146                                pad = 0;
147                        (void)printf("%*s%lc", pad, "", wc);
148                        pr->mbleft = clen - 1;
149                }
150        } else {
151                (void)sprintf(buf, "%03o", (int)*p);
152                str = buf;
153strpr:          *pr->cchar = 's';
154                (void)printf(pr->fmt, str);
155        }
156}
157
158void
159conv_u(rtems_shell_hexdump_globals* globals, PR *pr, u_char *p)
160{
161        static char const * list[] = {
162                "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
163                 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
164                "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
165                "can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
166        };
167
168                                                /* od used nl, not lf */
169        if (*p <= 0x1f) {
170                *pr->cchar = 's';
171                if (odmode && *p == 0x0a)
172                        (void)printf(pr->fmt, "nl");
173                else
174                        (void)printf(pr->fmt, list[*p]);
175        } else if (*p == 0x7f) {
176                *pr->cchar = 's';
177                (void)printf(pr->fmt, "del");
178        } else if (odmode && *p == 0x20) {      /* od replaced space with sp */
179                *pr->cchar = 's';
180                (void)printf(pr->fmt, " sp");
181        } else if (isprint(*p)) {
182                *pr->cchar = 'c';
183                (void)printf(pr->fmt, *p);
184        } else {
185                *pr->cchar = 'x';
186                (void)printf(pr->fmt, (int)*p);
187        }
188}
Note: See TracBrowser for help on using the repository browser.