source: rtems/cpukit/libmisc/shell/hexdump-conv.c @ ebb39a21

4.115
Last change on this file since ebb39a21 was ebb39a21, checked in by Joel Sherrill <joel.sherrill@…>, on 11/24/14 at 19:54:37

hexdump-conv.c: Use proper printf() formatting for wchar_t

  • Property mode set to 100644
File size: 5.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
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        if (pr->mbleft > 0) {
67                str = "**";
68                pr->mbleft--;
69                goto strpr;
70        }
71
72        switch(*p) {
73        case '\0':
74                str = "\\0";
75                goto strpr;
76        /* case '\a': */
77        case '\007':
78                str = "\\a";
79                goto strpr;
80        case '\b':
81                str = "\\b";
82                goto strpr;
83        case '\f':
84                str = "\\f";
85                goto strpr;
86        case '\n':
87                str = "\\n";
88                goto strpr;
89        case '\r':
90                str = "\\r";
91                goto strpr;
92        case '\t':
93                str = "\\t";
94                goto strpr;
95        case '\v':
96                str = "\\v";
97                goto strpr;
98        default:
99                break;
100        }
101        /*
102         * Multibyte characters are disabled for hexdump(1) for backwards
103         * compatibility and consistency (none of its other output formats
104         * recognize them correctly).
105         */
106        converr = 0;
107        if (odmode && MB_CUR_MAX > 1) {
108                oclen = 0;
109retry:
110                clen = mbrtowc(&wc, (char*)p, bufsize, &pr->mbstate);
111                if (clen == 0)
112                        clen = 1;
113                else if (clen == (size_t)-1 || (clen == (size_t)-2 &&
114                    buf == peekbuf)) {
115                        memset(&pr->mbstate, 0, sizeof(pr->mbstate));
116                        wc = *p;
117                        clen = 1;
118                        converr = 1;
119                } else if (clen == (size_t)-2) {
120                        /*
121                         * Incomplete character; peek ahead and see if we
122                         * can complete it.
123                         */
124                        oclen = bufsize;
125                        bufsize = peek(globals, p = (u_char*)peekbuf, MB_CUR_MAX);
126                        goto retry;
127                }
128                clen += oclen;
129        } else {
130                wc = *p;
131                clen = 1;
132        }
133        if (!converr && iswprint(wc)) {
134                if (!odmode) {
135                        *pr->cchar = 'c';
136                        (void)printf(pr->fmt, (int)wc);
137                } else {
138                        *pr->cchar = 'C';
139                        assert(strcmp(pr->fmt, "%3C") == 0);
140                        width = wcwidth(wc);
141                        assert(width >= 0);
142                        pad = 3 - width;
143                        if (pad < 0)
144                                pad = 0;
145                        #if defined(__rtems__)
146                        {
147                          wchar_t wc_tmp[2] = {0,0};
148                          wc_tmp[0] = wc;
149                          (void)printf("%*s%lc", pad, "", wc_tmp);
150                        }
151                        #else
152                          (void)printf("%*s%lc", pad, "", wc);
153                        #endif
154                        pr->mbleft = clen - 1;
155                }
156        } else {
157                (void)sprintf(buf, "%03o", (int)*p);
158                str = buf;
159strpr:          *pr->cchar = 's';
160                (void)printf(pr->fmt, str);
161        }
162}
163
164void
165conv_u(rtems_shell_hexdump_globals* globals, PR *pr, u_char *p)
166{
167        static char const * list[] = {
168                "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
169                 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
170                "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
171                "can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
172        };
173
174                                                /* od used nl, not lf */
175        if (*p <= 0x1f) {
176                *pr->cchar = 's';
177                if (odmode && *p == 0x0a)
178                        (void)printf(pr->fmt, "nl");
179                else
180                        (void)printf(pr->fmt, list[*p]);
181        } else if (*p == 0x7f) {
182                *pr->cchar = 's';
183                (void)printf(pr->fmt, "del");
184        } else if (odmode && *p == 0x20) {      /* od replaced space with sp */
185                *pr->cchar = 's';
186                (void)printf(pr->fmt, " sp");
187        } else if (isprint(*p)) {
188                *pr->cchar = 'c';
189                (void)printf(pr->fmt, *p);
190        } else {
191                *pr->cchar = 'x';
192                (void)printf(pr->fmt, (int)*p);
193        }
194}
Note: See TracBrowser for help on using the repository browser.