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