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