]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * 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 | ||
b50945d4 | 34 | /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
35 | * - added Native Language Support |
36 | */ | |
37 | ||
6dbe3af9 | 38 | #include <sys/types.h> |
d4646ea4 | 39 | #include <unistd.h> |
6dbe3af9 | 40 | #include <stdio.h> |
22853e4a | 41 | #include <string.h> |
85bf44b7 | 42 | #include <stdlib.h> |
d4646ea4 OO |
43 | #include <errno.h> |
44 | #include <err.h> | |
45 | #include <limits.h> | |
46 | #include <getopt.h> | |
6dbe3af9 | 47 | #include "hexdump.h" |
0a080346 | 48 | |
9db51207 | 49 | #include "list.h" |
7eda085c | 50 | #include "nls.h" |
0a080346 | 51 | #include "c.h" |
098ab077 | 52 | #include "colors.h" |
d4646ea4 | 53 | #include "strutils.h" |
2152af72 | 54 | #include "xalloc.h" |
b87cbe84 | 55 | #include "closestream.h" |
6dbe3af9 | 56 | |
1f77e9c3 | 57 | void hex_free(struct hexdump *); |
d4646ea4 OO |
58 | |
59 | int | |
1f77e9c3 | 60 | parse_args(int argc, char **argv, struct hexdump *hex) |
d4646ea4 OO |
61 | { |
62 | int ch; | |
d0c9ddc3 | 63 | int colormode = UL_COLORMODE_UNDEF; |
d4646ea4 OO |
64 | char *hex_offt = "\"%07.7_Ax\n\""; |
65 | ||
098ab077 | 66 | |
d4646ea4 OO |
67 | static const struct option longopts[] = { |
68 | {"one-byte-octal", no_argument, NULL, 'b'}, | |
925ced83 PR |
69 | {"one-byte-char", no_argument, NULL, 'c'}, |
70 | {"canonical", no_argument, NULL, 'C'}, | |
d4646ea4 | 71 | {"two-bytes-decimal", no_argument, NULL, 'd'}, |
925ced83 | 72 | {"two-bytes-octal", no_argument, NULL, 'o'}, |
d4646ea4 OO |
73 | {"two-bytes-hex", no_argument, NULL, 'x'}, |
74 | {"format", required_argument, NULL, 'e'}, | |
75 | {"format-file", required_argument, NULL, 'f'}, | |
098ab077 | 76 | {"color", optional_argument, NULL, 'L'}, |
d4646ea4 OO |
77 | {"length", required_argument, NULL, 'n'}, |
78 | {"skip", required_argument, NULL, 's'}, | |
79 | {"no-squeezing", no_argument, NULL, 'v'}, | |
80 | {"help", no_argument, NULL, 'h'}, | |
81 | {"version", no_argument, NULL, 'V'}, | |
82 | {NULL, no_argument, NULL, 0} | |
83 | }; | |
84 | ||
85 | while ((ch = getopt_long(argc, argv, "bcCde:f:L::n:os:vxhV", longopts, NULL)) != -1) { | |
86 | switch (ch) { | |
87 | case 'b': | |
1f77e9c3 OO |
88 | add_fmt(hex_offt, hex); |
89 | add_fmt("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", hex); | |
d4646ea4 OO |
90 | break; |
91 | case 'c': | |
1f77e9c3 OO |
92 | add_fmt(hex_offt, hex); |
93 | add_fmt("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", hex); | |
d4646ea4 OO |
94 | break; |
95 | case 'C': | |
1f77e9c3 OO |
96 | add_fmt("\"%08.8_Ax\n\"", hex); |
97 | add_fmt("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ", hex); | |
98 | add_fmt("\" |\" 16/1 \"%_p\" \"|\\n\"", hex); | |
d4646ea4 OO |
99 | break; |
100 | case 'd': | |
1f77e9c3 OO |
101 | add_fmt(hex_offt, hex); |
102 | add_fmt("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", hex); | |
d4646ea4 OO |
103 | break; |
104 | case 'e': | |
1f77e9c3 | 105 | add_fmt(optarg, hex); |
d4646ea4 OO |
106 | break; |
107 | case 'f': | |
1f77e9c3 | 108 | addfile(optarg, hex); |
d4646ea4 | 109 | break; |
098ab077 OO |
110 | case 'L': |
111 | colormode = UL_COLORMODE_AUTO; | |
112 | if (optarg) | |
113 | colormode = colormode_or_err(optarg, | |
114 | _("unsupported color mode")); | |
115 | break; | |
d4646ea4 | 116 | case 'n': |
1f77e9c3 | 117 | hex->length = strtosize_or_err(optarg, _("failed to parse length")); |
d4646ea4 OO |
118 | break; |
119 | case 'o': | |
1f77e9c3 OO |
120 | add_fmt(hex_offt, hex); |
121 | add_fmt("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", hex); | |
d4646ea4 OO |
122 | break; |
123 | case 's': | |
1f77e9c3 | 124 | hex->skip = strtosize_or_err(optarg, _("failed to parse offset")); |
d4646ea4 OO |
125 | break; |
126 | case 'v': | |
127 | vflag = ALL; | |
128 | break; | |
129 | case 'x': | |
1f77e9c3 OO |
130 | add_fmt(hex_offt, hex); |
131 | add_fmt("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", hex); | |
d4646ea4 OO |
132 | break; |
133 | case 'h': | |
134 | usage(stdout); | |
135 | case 'V': | |
136 | printf(UTIL_LINUX_VERSION); | |
137 | exit(EXIT_SUCCESS); | |
138 | break; | |
139 | default: | |
677ec86c | 140 | errtryhelp(EXIT_FAILURE); |
d4646ea4 OO |
141 | } |
142 | } | |
143 | ||
1f77e9c3 OO |
144 | if (list_empty(&hex->fshead)) { |
145 | add_fmt(hex_offt, hex); | |
146 | add_fmt("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"", hex); | |
d4646ea4 | 147 | } |
d0c9ddc3 | 148 | colors_init (colormode, "hexdump"); |
d4646ea4 OO |
149 | return optind; |
150 | } | |
151 | ||
152 | void __attribute__((__noreturn__)) usage(FILE *out) | |
153 | { | |
154 | fputs(USAGE_HEADER, out); | |
155 | fprintf(out, _(" %s [options] <file>...\n"), program_invocation_short_name); | |
451dbcfa BS |
156 | |
157 | fputs(USAGE_SEPARATOR, out); | |
158 | fputs(_("Display file contents in hexadecimal, decimal, octal, or ascii.\n"), out); | |
159 | ||
d4646ea4 OO |
160 | fputs(USAGE_OPTIONS, out); |
161 | fputs(_(" -b, --one-byte-octal one-byte octal display\n"), out); | |
162 | fputs(_(" -c, --one-byte-char one-byte character display\n"), out); | |
163 | fputs(_(" -C, --canonical canonical hex+ASCII display\n"), out); | |
164 | fputs(_(" -d, --two-bytes-decimal two-byte decimal display\n"), out); | |
165 | fputs(_(" -o, --two-bytes-octal two-byte octal display\n"), out); | |
166 | fputs(_(" -x, --two-bytes-hex two-byte hexadecimal display\n"), out); | |
098ab077 | 167 | fputs(_(" -L, --color[=<mode>] interpret color formatting specifiers\n"), out); |
5d51dc2a KZ |
168 | fprintf(out, |
169 | " %s\n", USAGE_COLORS_DEFAULT); | |
d4646ea4 OO |
170 | fputs(_(" -e, --format <format> format string to be used for displaying data\n"), out); |
171 | fputs(_(" -f, --format-file <file> file that contains format strings\n"), out); | |
172 | fputs(_(" -n, --length <length> interpret only length bytes of input\n"), out); | |
173 | fputs(_(" -s, --skip <offset> skip offset bytes from the beginning\n"), out); | |
174 | fputs(_(" -v, --no-squeezing output identical lines\n"), out); | |
175 | fputs(USAGE_SEPARATOR, out); | |
176 | fputs(USAGE_HELP, out); | |
177 | fputs(USAGE_VERSION, out); | |
178 | fprintf(out, USAGE_MAN_TAIL("hexdump(1)")); | |
179 | ||
180 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
181 | } | |
182 | ||
22853e4a | 183 | int main(int argc, char **argv) |
6dbe3af9 | 184 | { |
9db51207 | 185 | struct list_head *p; |
bb8ae572 | 186 | struct hexdump_fs *tfs; |
917c3733 | 187 | int ret; |
1f77e9c3 | 188 | |
2152af72 | 189 | struct hexdump *hex = xcalloc(1, sizeof (struct hexdump)); |
1f77e9c3 OO |
190 | hex->length = -1; |
191 | INIT_LIST_HEAD(&hex->fshead); | |
6dbe3af9 | 192 | |
7eda085c KZ |
193 | setlocale(LC_ALL, ""); |
194 | bindtextdomain(PACKAGE, LOCALEDIR); | |
195 | textdomain(PACKAGE); | |
b87cbe84 | 196 | atexit(close_stdout); |
7eda085c | 197 | |
bf60e9f1 | 198 | argv += parse_args(argc, argv, hex); |
6dbe3af9 KZ |
199 | |
200 | /* figure out the data block size */ | |
1f77e9c3 OO |
201 | hex->blocksize = 0; |
202 | list_for_each(p, &hex->fshead) { | |
bb8ae572 | 203 | tfs = list_entry(p, struct hexdump_fs, fslist); |
1f77e9c3 OO |
204 | if ((tfs->bcnt = block_size(tfs)) > hex->blocksize) |
205 | hex->blocksize = tfs->bcnt; | |
6dbe3af9 | 206 | } |
9db51207 | 207 | |
6dbe3af9 | 208 | /* rewrite the rules, do syntax checking */ |
1f77e9c3 OO |
209 | list_for_each(p, &hex->fshead) |
210 | rewrite_rules(list_entry(p, struct hexdump_fs, fslist), hex); | |
6dbe3af9 | 211 | |
1f77e9c3 OO |
212 | next(argv, hex); |
213 | display(hex); | |
917c3733 AV |
214 | |
215 | ret = hex->exitval; | |
1f77e9c3 | 216 | hex_free(hex); |
917c3733 AV |
217 | |
218 | return ret; | |
6dbe3af9 | 219 | } |
d2740b0e | 220 | |
1f77e9c3 | 221 | void hex_free(struct hexdump *hex) |
d2740b0e | 222 | { |
098ab077 | 223 | struct list_head *p, *pn, *q, *qn, *r, *rn, *s, *sn; |
bb8ae572 | 224 | struct hexdump_fs *fs; |
046921da | 225 | struct hexdump_fu *fu; |
4c73d29c | 226 | struct hexdump_pr *pr; |
098ab077 OO |
227 | struct hexdump_clr *clr; |
228 | ||
1f77e9c3 | 229 | list_for_each_safe(p, pn, &hex->fshead) { |
bb8ae572 | 230 | fs = list_entry(p, struct hexdump_fs, fslist); |
d2740b0e | 231 | list_for_each_safe(q, qn, &fs->fulist) { |
046921da | 232 | fu = list_entry(q, struct hexdump_fu, fulist); |
d2740b0e | 233 | list_for_each_safe(r, rn, &fu->prlist) { |
4c73d29c | 234 | pr = list_entry(r, struct hexdump_pr, prlist); |
098ab077 OO |
235 | if (pr->colorlist) { |
236 | list_for_each_safe(s, sn, pr->colorlist) { | |
237 | clr = list_entry (s, struct hexdump_clr, colorlist); | |
238 | free(clr->str); | |
239 | free(clr); | |
240 | } | |
241 | } | |
d2740b0e OO |
242 | free(pr->fmt); |
243 | free(pr); | |
244 | } | |
245 | free(fu->fmt); | |
246 | free(fu); | |
247 | } | |
248 | free(fs); | |
249 | } | |
1f77e9c3 | 250 | free (hex); |
d2740b0e | 251 | } |