Commit | Line | Data |
---|---|---|
2f939ddd | 1 | /* Convert files for Emacs Hexl mode. |
73b0cd50 | 2 | Copyright (C) 1989, 2001-2011 Free Software Foundation, Inc. |
eedcb417 GM |
3 | |
4 | Author: Keith Gabryelski | |
5 | (according to authors.el) | |
2f939ddd RS |
6 | |
7 | This file is not considered part of GNU Emacs. | |
8 | ||
294981c7 | 9 | This program is free software: you can redistribute it and/or modify |
2f939ddd | 10 | it under the terms of the GNU General Public License as published by |
294981c7 GM |
11 | the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | |
2f939ddd RS |
13 | |
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
294981c7 GM |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
21 | ||
2f939ddd | 22 | |
0072414b RS |
23 | #ifdef HAVE_CONFIG_H |
24 | #include <config.h> | |
25 | #endif | |
26 | ||
bdf34e49 JB |
27 | #include <stdio.h> |
28 | #include <ctype.h> | |
eac1956a | 29 | #ifdef DOS_NT |
20333561 | 30 | #include <fcntl.h> |
659d3b43 RS |
31 | #if __DJGPP__ >= 2 |
32 | #include <io.h> | |
33 | #endif | |
20333561 | 34 | #endif |
eac1956a RS |
35 | #ifdef WINDOWSNT |
36 | #include <io.h> | |
37 | #endif | |
bdf34e49 JB |
38 | |
39 | #define DEFAULT_GROUPING 0x01 | |
40 | #define DEFAULT_BASE 16 | |
41 | ||
42 | #undef TRUE | |
43 | #undef FALSE | |
44 | #define TRUE (1) | |
45 | #define FALSE (0) | |
46 | ||
bdf34e49 JB |
47 | int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1; |
48 | int group_by = DEFAULT_GROUPING; | |
49 | char *progname; | |
50 | ||
68441b90 | 51 | void usage(void) NO_RETURN; |
340ff9de DM |
52 | |
53 | int | |
873fbd0b | 54 | main (int argc, char **argv) |
bdf34e49 | 55 | { |
20333561 RS |
56 | register long address; |
57 | char string[18]; | |
58 | FILE *fp; | |
59 | ||
60 | progname = *argv++; --argc; | |
61 | ||
62 | /* | |
63 | ** -hex hex dump | |
64 | ** -oct Octal dump | |
65 | ** -group-by-8-bits | |
66 | ** -group-by-16-bits | |
67 | ** -group-by-32-bits | |
68 | ** -group-by-64-bits | |
69 | ** -iso iso character set. | |
70 | ** -big-endian Big Endian | |
71 | ** -little-endian Little Endian | |
72 | ** -un || -de from hexl format to binary. | |
73 | ** -- End switch list. | |
74 | ** <filename> dump filename | |
75 | ** - (as filename == stdin) | |
76 | */ | |
80b2cbf2 | 77 | |
20333561 | 78 | while (*argv && *argv[0] == '-' && (*argv)[1]) |
bdf34e49 | 79 | { |
20333561 RS |
80 | /* A switch! */ |
81 | if (!strcmp (*argv, "--")) | |
bdf34e49 | 82 | { |
20333561 RS |
83 | --argc; argv++; |
84 | break; | |
85 | } | |
86 | else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de")) | |
bdf34e49 | 87 | { |
20333561 RS |
88 | un_flag = TRUE; |
89 | --argc; argv++; | |
90 | } | |
91 | else if (!strcmp (*argv, "-hex")) | |
bdf34e49 | 92 | { |
20333561 RS |
93 | base = 16; |
94 | --argc; argv++; | |
95 | } | |
96 | else if (!strcmp (*argv, "-iso")) | |
bdf34e49 | 97 | { |
20333561 RS |
98 | iso_flag = TRUE; |
99 | --argc; argv++; | |
100 | } | |
101 | else if (!strcmp (*argv, "-oct")) | |
bdf34e49 | 102 | { |
20333561 RS |
103 | base = 8; |
104 | --argc; argv++; | |
105 | } | |
106 | else if (!strcmp (*argv, "-big-endian")) | |
bdf34e49 | 107 | { |
20333561 RS |
108 | endian = 1; |
109 | --argc; argv++; | |
110 | } | |
111 | else if (!strcmp (*argv, "-little-endian")) | |
bdf34e49 | 112 | { |
20333561 RS |
113 | endian = 0; |
114 | --argc; argv++; | |
115 | } | |
116 | else if (!strcmp (*argv, "-group-by-8-bits")) | |
bdf34e49 | 117 | { |
20333561 RS |
118 | group_by = 0x00; |
119 | --argc; argv++; | |
120 | } | |
121 | else if (!strcmp (*argv, "-group-by-16-bits")) | |
bdf34e49 | 122 | { |
20333561 RS |
123 | group_by = 0x01; |
124 | --argc; argv++; | |
125 | } | |
126 | else if (!strcmp (*argv, "-group-by-32-bits")) | |
bdf34e49 | 127 | { |
20333561 RS |
128 | group_by = 0x03; |
129 | --argc; argv++; | |
130 | } | |
131 | else if (!strcmp (*argv, "-group-by-64-bits")) | |
bdf34e49 | 132 | { |
20333561 RS |
133 | group_by = 0x07; |
134 | endian = 0; | |
135 | --argc; argv++; | |
136 | } | |
137 | else | |
bdf34e49 | 138 | { |
20333561 RS |
139 | fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname, |
140 | *argv); | |
141 | usage (); | |
bdf34e49 JB |
142 | } |
143 | } | |
144 | ||
20333561 | 145 | do |
bdf34e49 | 146 | { |
20333561 RS |
147 | if (*argv == NULL) |
148 | fp = stdin; | |
149 | else | |
bdf34e49 | 150 | { |
20333561 | 151 | char *filename = *argv++; |
bdf34e49 | 152 | |
20333561 RS |
153 | if (!strcmp (filename, "-")) |
154 | fp = stdin; | |
155 | else if ((fp = fopen (filename, "r")) == NULL) | |
156 | { | |
157 | perror (filename); | |
158 | continue; | |
159 | } | |
bdf34e49 JB |
160 | } |
161 | ||
20333561 | 162 | if (un_flag) |
bdf34e49 | 163 | { |
20333561 | 164 | char buf[18]; |
bdf34e49 | 165 | |
eac1956a RS |
166 | #ifdef DOS_NT |
167 | #if (__DJGPP__ >= 2) || (defined WINDOWSNT) | |
3f5b4e35 RS |
168 | if (!isatty (fileno (stdout))) |
169 | setmode (fileno (stdout), O_BINARY); | |
659d3b43 | 170 | #else |
20333561 RS |
171 | (stdout)->_flag &= ~_IOTEXT; /* print binary */ |
172 | _setmode (fileno (stdout), O_BINARY); | |
659d3b43 | 173 | #endif |
20333561 RS |
174 | #endif |
175 | for (;;) | |
bdf34e49 | 176 | { |
4431cfb5 | 177 | register int i, c = 0, d; |
bdf34e49 | 178 | |
20333561 | 179 | #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10) |
bdf34e49 | 180 | |
9055082e PE |
181 | /* Skip 10 bytes. */ |
182 | if (fread (buf, 1, 10, fp) != 10) | |
183 | break; | |
bdf34e49 | 184 | |
20333561 | 185 | for (i=0; i < 16; ++i) |
bdf34e49 | 186 | { |
20333561 RS |
187 | if ((c = getc (fp)) == ' ' || c == EOF) |
188 | break; | |
bdf34e49 | 189 | |
20333561 RS |
190 | d = getc (fp); |
191 | c = hexchar (c) * 0x10 + hexchar (d); | |
192 | putchar (c); | |
bdf34e49 | 193 | |
20333561 RS |
194 | if ((i&group_by) == group_by) |
195 | getc (fp); | |
bdf34e49 JB |
196 | } |
197 | ||
20333561 | 198 | if (c == ' ') |
bdf34e49 | 199 | { |
20333561 RS |
200 | while ((c = getc (fp)) != '\n' && c != EOF) |
201 | ; | |
bdf34e49 | 202 | |
20333561 RS |
203 | if (c == EOF) |
204 | break; | |
bdf34e49 | 205 | } |
20333561 | 206 | else |
bdf34e49 | 207 | { |
20333561 RS |
208 | if (i < 16) |
209 | break; | |
bdf34e49 | 210 | |
9055082e PE |
211 | /* Skip 18 bytes. */ |
212 | if (fread (buf, 1, 18, fp) != 18) | |
213 | break; | |
bdf34e49 JB |
214 | } |
215 | } | |
216 | } | |
20333561 | 217 | else |
bdf34e49 | 218 | { |
eac1956a RS |
219 | #ifdef DOS_NT |
220 | #if (__DJGPP__ >= 2) || (defined WINDOWSNT) | |
3f5b4e35 RS |
221 | if (!isatty (fileno (fp))) |
222 | setmode (fileno (fp), O_BINARY); | |
659d3b43 | 223 | #else |
20333561 RS |
224 | (fp)->_flag &= ~_IOTEXT; /* read binary */ |
225 | _setmode (fileno (fp), O_BINARY); | |
659d3b43 | 226 | #endif |
20333561 RS |
227 | #endif |
228 | address = 0; | |
229 | string[0] = ' '; | |
230 | string[17] = '\0'; | |
231 | for (;;) | |
bdf34e49 | 232 | { |
4431cfb5 | 233 | register int i, c = 0; |
bdf34e49 | 234 | |
20333561 | 235 | for (i=0; i < 16; ++i) |
bdf34e49 | 236 | { |
20333561 | 237 | if ((c = getc (fp)) == EOF) |
bdf34e49 | 238 | { |
20333561 RS |
239 | if (!i) |
240 | break; | |
bdf34e49 | 241 | |
20333561 RS |
242 | fputs (" ", stdout); |
243 | string[i+1] = '\0'; | |
bdf34e49 | 244 | } |
20333561 | 245 | else |
bdf34e49 | 246 | { |
20333561 | 247 | if (!i) |
345a935c | 248 | printf ("%08lx: ", address); |
bdf34e49 | 249 | |
20333561 RS |
250 | if (iso_flag) |
251 | string[i+1] = | |
252 | (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c; | |
253 | else | |
254 | string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c; | |
bdf34e49 | 255 | |
20333561 | 256 | printf ("%02x", c); |
bdf34e49 JB |
257 | } |
258 | ||
20333561 RS |
259 | if ((i&group_by) == group_by) |
260 | putchar (' '); | |
bdf34e49 JB |
261 | } |
262 | ||
20333561 RS |
263 | if (i) |
264 | puts (string); | |
bdf34e49 | 265 | |
20333561 RS |
266 | if (c == EOF) |
267 | break; | |
bdf34e49 | 268 | |
20333561 | 269 | address += 0x10; |
bdf34e49 JB |
270 | |
271 | } | |
272 | } | |
273 | ||
20333561 RS |
274 | if (fp != stdin) |
275 | fclose (fp); | |
bdf34e49 JB |
276 | |
277 | } while (*argv != NULL); | |
65396510 | 278 | return EXIT_SUCCESS; |
bdf34e49 JB |
279 | } |
280 | ||
340ff9de | 281 | void |
873fbd0b | 282 | usage (void) |
bdf34e49 | 283 | { |
20333561 | 284 | fprintf (stderr, "usage: %s [-de] [-iso]\n", progname); |
65396510 | 285 | exit (EXIT_FAILURE); |
bdf34e49 | 286 | } |
ab5796a9 | 287 | |
65396510 TTN |
288 | |
289 | /* hexl.c ends here */ |