Commit | Line | Data |
---|---|---|
2f939ddd | 1 | /* Convert files for Emacs Hexl mode. |
b3d90e46 GM |
2 | Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, |
3 | 2006, 2007 Free Software Foundation, Inc. | |
2f939ddd RS |
4 | |
5 | This file is not considered part of GNU Emacs. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software Foundation, | |
364c38d3 | 19 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
2f939ddd | 20 | |
0072414b RS |
21 | #ifdef HAVE_CONFIG_H |
22 | #include <config.h> | |
23 | #endif | |
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 | ||
40 | #undef TRUE | |
41 | #undef FALSE | |
42 | #define TRUE (1) | |
43 | #define FALSE (0) | |
44 | ||
bdf34e49 JB |
45 | int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1; |
46 | int group_by = DEFAULT_GROUPING; | |
47 | char *progname; | |
48 | ||
340ff9de DM |
49 | void usage(); |
50 | ||
51 | int | |
20333561 RS |
52 | main (argc, argv) |
53 | int argc; | |
54 | 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 | |
20333561 | 181 | fread (buf, 1, 10, fp); /* skip 10 bytes */ |
bdf34e49 | 182 | |
20333561 | 183 | for (i=0; i < 16; ++i) |
bdf34e49 | 184 | { |
20333561 RS |
185 | if ((c = getc (fp)) == ' ' || c == EOF) |
186 | break; | |
bdf34e49 | 187 | |
20333561 RS |
188 | d = getc (fp); |
189 | c = hexchar (c) * 0x10 + hexchar (d); | |
190 | putchar (c); | |
bdf34e49 | 191 | |
20333561 RS |
192 | if ((i&group_by) == group_by) |
193 | getc (fp); | |
bdf34e49 JB |
194 | } |
195 | ||
20333561 | 196 | if (c == ' ') |
bdf34e49 | 197 | { |
20333561 RS |
198 | while ((c = getc (fp)) != '\n' && c != EOF) |
199 | ; | |
bdf34e49 | 200 | |
20333561 RS |
201 | if (c == EOF) |
202 | break; | |
bdf34e49 | 203 | } |
20333561 | 204 | else |
bdf34e49 | 205 | { |
20333561 RS |
206 | if (i < 16) |
207 | break; | |
bdf34e49 | 208 | |
20333561 | 209 | fread (buf, 1, 18, fp); /* skip 18 bytes */ |
bdf34e49 JB |
210 | } |
211 | } | |
212 | } | |
20333561 | 213 | else |
bdf34e49 | 214 | { |
eac1956a RS |
215 | #ifdef DOS_NT |
216 | #if (__DJGPP__ >= 2) || (defined WINDOWSNT) | |
3f5b4e35 RS |
217 | if (!isatty (fileno (fp))) |
218 | setmode (fileno (fp), O_BINARY); | |
659d3b43 | 219 | #else |
20333561 RS |
220 | (fp)->_flag &= ~_IOTEXT; /* read binary */ |
221 | _setmode (fileno (fp), O_BINARY); | |
659d3b43 | 222 | #endif |
20333561 RS |
223 | #endif |
224 | address = 0; | |
225 | string[0] = ' '; | |
226 | string[17] = '\0'; | |
227 | for (;;) | |
bdf34e49 | 228 | { |
4431cfb5 | 229 | register int i, c = 0; |
bdf34e49 | 230 | |
20333561 | 231 | for (i=0; i < 16; ++i) |
bdf34e49 | 232 | { |
20333561 | 233 | if ((c = getc (fp)) == EOF) |
bdf34e49 | 234 | { |
20333561 RS |
235 | if (!i) |
236 | break; | |
bdf34e49 | 237 | |
20333561 RS |
238 | fputs (" ", stdout); |
239 | string[i+1] = '\0'; | |
bdf34e49 | 240 | } |
20333561 | 241 | else |
bdf34e49 | 242 | { |
20333561 | 243 | if (!i) |
345a935c | 244 | printf ("%08lx: ", address); |
bdf34e49 | 245 | |
20333561 RS |
246 | if (iso_flag) |
247 | string[i+1] = | |
248 | (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c; | |
249 | else | |
250 | string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c; | |
bdf34e49 | 251 | |
20333561 | 252 | printf ("%02x", c); |
bdf34e49 JB |
253 | } |
254 | ||
20333561 RS |
255 | if ((i&group_by) == group_by) |
256 | putchar (' '); | |
bdf34e49 JB |
257 | } |
258 | ||
20333561 RS |
259 | if (i) |
260 | puts (string); | |
bdf34e49 | 261 | |
20333561 RS |
262 | if (c == EOF) |
263 | break; | |
bdf34e49 | 264 | |
20333561 | 265 | address += 0x10; |
bdf34e49 JB |
266 | |
267 | } | |
268 | } | |
269 | ||
20333561 RS |
270 | if (fp != stdin) |
271 | fclose (fp); | |
bdf34e49 JB |
272 | |
273 | } while (*argv != NULL); | |
65396510 | 274 | return EXIT_SUCCESS; |
bdf34e49 JB |
275 | } |
276 | ||
340ff9de | 277 | void |
20333561 | 278 | usage () |
bdf34e49 | 279 | { |
20333561 | 280 | fprintf (stderr, "usage: %s [-de] [-iso]\n", progname); |
65396510 | 281 | exit (EXIT_FAILURE); |
bdf34e49 | 282 | } |
ab5796a9 MB |
283 | |
284 | /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa | |
285 | (do not change this comment) */ | |
65396510 TTN |
286 | |
287 | /* hexl.c ends here */ |