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