Commit | Line | Data |
---|---|---|
2f939ddd | 1 | /* Convert files for Emacs Hexl mode. |
ba318903 | 2 | Copyright (C) 1989, 2001-2014 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 | 23 | #include <config.h> |
0072414b | 24 | |
bdf34e49 JB |
25 | #include <stdio.h> |
26 | #include <ctype.h> | |
eac1956a | 27 | #ifdef DOS_NT |
20333561 | 28 | #include <fcntl.h> |
659d3b43 RS |
29 | #if __DJGPP__ >= 2 |
30 | #include <io.h> | |
31 | #endif | |
20333561 | 32 | #endif |
eac1956a RS |
33 | #ifdef WINDOWSNT |
34 | #include <io.h> | |
35 | #endif | |
bdf34e49 JB |
36 | |
37 | #define DEFAULT_GROUPING 0x01 | |
38 | #define DEFAULT_BASE 16 | |
39 | ||
96c06863 PE |
40 | int base = DEFAULT_BASE; |
41 | bool un_flag = false, iso_flag = false, endian = true; | |
bdf34e49 JB |
42 | int group_by = DEFAULT_GROUPING; |
43 | char *progname; | |
44 | ||
845ca893 | 45 | _Noreturn void usage (void); |
340ff9de DM |
46 | |
47 | int | |
873fbd0b | 48 | main (int argc, char **argv) |
bdf34e49 | 49 | { |
20333561 RS |
50 | register long address; |
51 | char string[18]; | |
52 | FILE *fp; | |
53 | ||
54 | progname = *argv++; --argc; | |
55 | ||
56 | /* | |
57 | ** -hex hex dump | |
58 | ** -oct Octal dump | |
59 | ** -group-by-8-bits | |
60 | ** -group-by-16-bits | |
61 | ** -group-by-32-bits | |
62 | ** -group-by-64-bits | |
63 | ** -iso iso character set. | |
64 | ** -big-endian Big Endian | |
65 | ** -little-endian Little Endian | |
66 | ** -un || -de from hexl format to binary. | |
67 | ** -- End switch list. | |
68 | ** <filename> dump filename | |
69 | ** - (as filename == stdin) | |
70 | */ | |
80b2cbf2 | 71 | |
20333561 | 72 | while (*argv && *argv[0] == '-' && (*argv)[1]) |
bdf34e49 | 73 | { |
20333561 RS |
74 | /* A switch! */ |
75 | if (!strcmp (*argv, "--")) | |
bdf34e49 | 76 | { |
20333561 RS |
77 | --argc; argv++; |
78 | break; | |
79 | } | |
80 | else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de")) | |
bdf34e49 | 81 | { |
96c06863 | 82 | un_flag = true; |
20333561 RS |
83 | --argc; argv++; |
84 | } | |
85 | else if (!strcmp (*argv, "-hex")) | |
bdf34e49 | 86 | { |
20333561 RS |
87 | base = 16; |
88 | --argc; argv++; | |
89 | } | |
90 | else if (!strcmp (*argv, "-iso")) | |
bdf34e49 | 91 | { |
96c06863 | 92 | iso_flag = true; |
20333561 RS |
93 | --argc; argv++; |
94 | } | |
95 | else if (!strcmp (*argv, "-oct")) | |
bdf34e49 | 96 | { |
20333561 RS |
97 | base = 8; |
98 | --argc; argv++; | |
99 | } | |
100 | else if (!strcmp (*argv, "-big-endian")) | |
bdf34e49 | 101 | { |
96c06863 | 102 | endian = true; |
20333561 RS |
103 | --argc; argv++; |
104 | } | |
105 | else if (!strcmp (*argv, "-little-endian")) | |
bdf34e49 | 106 | { |
96c06863 | 107 | endian = false; |
20333561 RS |
108 | --argc; argv++; |
109 | } | |
110 | else if (!strcmp (*argv, "-group-by-8-bits")) | |
bdf34e49 | 111 | { |
20333561 RS |
112 | group_by = 0x00; |
113 | --argc; argv++; | |
114 | } | |
115 | else if (!strcmp (*argv, "-group-by-16-bits")) | |
bdf34e49 | 116 | { |
20333561 RS |
117 | group_by = 0x01; |
118 | --argc; argv++; | |
119 | } | |
120 | else if (!strcmp (*argv, "-group-by-32-bits")) | |
bdf34e49 | 121 | { |
20333561 RS |
122 | group_by = 0x03; |
123 | --argc; argv++; | |
124 | } | |
125 | else if (!strcmp (*argv, "-group-by-64-bits")) | |
bdf34e49 | 126 | { |
20333561 | 127 | group_by = 0x07; |
96c06863 | 128 | endian = false; |
20333561 RS |
129 | --argc; argv++; |
130 | } | |
131 | else | |
bdf34e49 | 132 | { |
20333561 RS |
133 | fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname, |
134 | *argv); | |
135 | usage (); | |
bdf34e49 JB |
136 | } |
137 | } | |
138 | ||
20333561 | 139 | do |
bdf34e49 | 140 | { |
20333561 RS |
141 | if (*argv == NULL) |
142 | fp = stdin; | |
143 | else | |
bdf34e49 | 144 | { |
20333561 | 145 | char *filename = *argv++; |
bdf34e49 | 146 | |
20333561 RS |
147 | if (!strcmp (filename, "-")) |
148 | fp = stdin; | |
149 | else if ((fp = fopen (filename, "r")) == NULL) | |
150 | { | |
151 | perror (filename); | |
152 | continue; | |
153 | } | |
bdf34e49 JB |
154 | } |
155 | ||
20333561 | 156 | if (un_flag) |
bdf34e49 | 157 | { |
20333561 | 158 | char buf[18]; |
bdf34e49 | 159 | |
eac1956a RS |
160 | #ifdef DOS_NT |
161 | #if (__DJGPP__ >= 2) || (defined WINDOWSNT) | |
3f5b4e35 RS |
162 | if (!isatty (fileno (stdout))) |
163 | setmode (fileno (stdout), O_BINARY); | |
659d3b43 | 164 | #else |
20333561 RS |
165 | (stdout)->_flag &= ~_IOTEXT; /* print binary */ |
166 | _setmode (fileno (stdout), O_BINARY); | |
659d3b43 | 167 | #endif |
20333561 RS |
168 | #endif |
169 | for (;;) | |
bdf34e49 | 170 | { |
4431cfb5 | 171 | register int i, c = 0, d; |
bdf34e49 | 172 | |
20333561 | 173 | #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10) |
bdf34e49 | 174 | |
9055082e PE |
175 | /* Skip 10 bytes. */ |
176 | if (fread (buf, 1, 10, fp) != 10) | |
177 | break; | |
bdf34e49 | 178 | |
20333561 | 179 | for (i=0; i < 16; ++i) |
bdf34e49 | 180 | { |
20333561 RS |
181 | if ((c = getc (fp)) == ' ' || c == EOF) |
182 | break; | |
bdf34e49 | 183 | |
20333561 RS |
184 | d = getc (fp); |
185 | c = hexchar (c) * 0x10 + hexchar (d); | |
186 | putchar (c); | |
bdf34e49 | 187 | |
20333561 RS |
188 | if ((i&group_by) == group_by) |
189 | getc (fp); | |
bdf34e49 JB |
190 | } |
191 | ||
20333561 | 192 | if (c == ' ') |
bdf34e49 | 193 | { |
20333561 RS |
194 | while ((c = getc (fp)) != '\n' && c != EOF) |
195 | ; | |
bdf34e49 | 196 | |
20333561 RS |
197 | if (c == EOF) |
198 | break; | |
bdf34e49 | 199 | } |
20333561 | 200 | else |
bdf34e49 | 201 | { |
20333561 RS |
202 | if (i < 16) |
203 | break; | |
bdf34e49 | 204 | |
9055082e PE |
205 | /* Skip 18 bytes. */ |
206 | if (fread (buf, 1, 18, fp) != 18) | |
207 | break; | |
bdf34e49 JB |
208 | } |
209 | } | |
210 | } | |
20333561 | 211 | else |
bdf34e49 | 212 | { |
eac1956a RS |
213 | #ifdef DOS_NT |
214 | #if (__DJGPP__ >= 2) || (defined WINDOWSNT) | |
3f5b4e35 RS |
215 | if (!isatty (fileno (fp))) |
216 | setmode (fileno (fp), O_BINARY); | |
659d3b43 | 217 | #else |
20333561 RS |
218 | (fp)->_flag &= ~_IOTEXT; /* read binary */ |
219 | _setmode (fileno (fp), O_BINARY); | |
659d3b43 | 220 | #endif |
20333561 RS |
221 | #endif |
222 | address = 0; | |
223 | string[0] = ' '; | |
224 | string[17] = '\0'; | |
225 | for (;;) | |
bdf34e49 | 226 | { |
4431cfb5 | 227 | register int i, c = 0; |
bdf34e49 | 228 | |
20333561 | 229 | for (i=0; i < 16; ++i) |
bdf34e49 | 230 | { |
20333561 | 231 | if ((c = getc (fp)) == EOF) |
bdf34e49 | 232 | { |
20333561 RS |
233 | if (!i) |
234 | break; | |
bdf34e49 | 235 | |
20333561 RS |
236 | fputs (" ", stdout); |
237 | string[i+1] = '\0'; | |
bdf34e49 | 238 | } |
20333561 | 239 | else |
bdf34e49 | 240 | { |
20333561 | 241 | if (!i) |
345a935c | 242 | printf ("%08lx: ", address); |
bdf34e49 | 243 | |
20333561 RS |
244 | if (iso_flag) |
245 | string[i+1] = | |
246 | (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c; | |
247 | else | |
248 | string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c; | |
bdf34e49 | 249 | |
20333561 | 250 | printf ("%02x", c); |
bdf34e49 JB |
251 | } |
252 | ||
20333561 RS |
253 | if ((i&group_by) == group_by) |
254 | putchar (' '); | |
bdf34e49 JB |
255 | } |
256 | ||
20333561 RS |
257 | if (i) |
258 | puts (string); | |
bdf34e49 | 259 | |
20333561 RS |
260 | if (c == EOF) |
261 | break; | |
bdf34e49 | 262 | |
20333561 | 263 | address += 0x10; |
bdf34e49 JB |
264 | |
265 | } | |
266 | } | |
267 | ||
20333561 RS |
268 | if (fp != stdin) |
269 | fclose (fp); | |
bdf34e49 JB |
270 | |
271 | } while (*argv != NULL); | |
65396510 | 272 | return EXIT_SUCCESS; |
bdf34e49 JB |
273 | } |
274 | ||
340ff9de | 275 | void |
873fbd0b | 276 | usage (void) |
bdf34e49 | 277 | { |
20333561 | 278 | fprintf (stderr, "usage: %s [-de] [-iso]\n", progname); |
65396510 | 279 | exit (EXIT_FAILURE); |
bdf34e49 | 280 | } |
ab5796a9 | 281 | |
65396510 TTN |
282 | |
283 | /* hexl.c ends here */ |