]> code.delx.au - gnu-emacs/blob - lib-src/hexl.c
7a2f127ae61a668a744b5937ed8814b7994284cb
[gnu-emacs] / lib-src / hexl.c
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
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <ctype.h>
26 #ifdef DOS_NT
27 #include <fcntl.h>
28 #if __DJGPP__ >= 2
29 #include <io.h>
30 #endif
31 #endif
32 #ifdef WINDOWSNT
33 #include <io.h>
34 #endif
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
48 void usage();
49
50 int
51 main (argc, argv)
52 int argc;
53 char *argv[];
54 {
55 register long address;
56 char string[18];
57 FILE *fp;
58
59 progname = *argv++; --argc;
60
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 */
76
77 while (*argv && *argv[0] == '-' && (*argv)[1])
78 {
79 /* A switch! */
80 if (!strcmp (*argv, "--"))
81 {
82 --argc; argv++;
83 break;
84 }
85 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
86 {
87 un_flag = TRUE;
88 --argc; argv++;
89 }
90 else if (!strcmp (*argv, "-hex"))
91 {
92 base = 16;
93 --argc; argv++;
94 }
95 else if (!strcmp (*argv, "-iso"))
96 {
97 iso_flag = TRUE;
98 --argc; argv++;
99 }
100 else if (!strcmp (*argv, "-oct"))
101 {
102 base = 8;
103 --argc; argv++;
104 }
105 else if (!strcmp (*argv, "-big-endian"))
106 {
107 endian = 1;
108 --argc; argv++;
109 }
110 else if (!strcmp (*argv, "-little-endian"))
111 {
112 endian = 0;
113 --argc; argv++;
114 }
115 else if (!strcmp (*argv, "-group-by-8-bits"))
116 {
117 group_by = 0x00;
118 --argc; argv++;
119 }
120 else if (!strcmp (*argv, "-group-by-16-bits"))
121 {
122 group_by = 0x01;
123 --argc; argv++;
124 }
125 else if (!strcmp (*argv, "-group-by-32-bits"))
126 {
127 group_by = 0x03;
128 --argc; argv++;
129 }
130 else if (!strcmp (*argv, "-group-by-64-bits"))
131 {
132 group_by = 0x07;
133 endian = 0;
134 --argc; argv++;
135 }
136 else
137 {
138 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
139 *argv);
140 usage ();
141 }
142 }
143
144 do
145 {
146 if (*argv == NULL)
147 fp = stdin;
148 else
149 {
150 char *filename = *argv++;
151
152 if (!strcmp (filename, "-"))
153 fp = stdin;
154 else if ((fp = fopen (filename, "r")) == NULL)
155 {
156 perror (filename);
157 continue;
158 }
159 }
160
161 if (un_flag)
162 {
163 char buf[18];
164
165 #ifdef DOS_NT
166 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
167 if (!isatty (fileno (stdout)))
168 setmode (fileno (stdout), O_BINARY);
169 #else
170 (stdout)->_flag &= ~_IOTEXT; /* print binary */
171 _setmode (fileno (stdout), O_BINARY);
172 #endif
173 #endif
174 for (;;)
175 {
176 register int i, c = 0, d;
177
178 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
179
180 fread (buf, 1, 10, fp); /* skip 10 bytes */
181
182 for (i=0; i < 16; ++i)
183 {
184 if ((c = getc (fp)) == ' ' || c == EOF)
185 break;
186
187 d = getc (fp);
188 c = hexchar (c) * 0x10 + hexchar (d);
189 putchar (c);
190
191 if ((i&group_by) == group_by)
192 getc (fp);
193 }
194
195 if (c == ' ')
196 {
197 while ((c = getc (fp)) != '\n' && c != EOF)
198 ;
199
200 if (c == EOF)
201 break;
202 }
203 else
204 {
205 if (i < 16)
206 break;
207
208 fread (buf, 1, 18, fp); /* skip 18 bytes */
209 }
210 }
211 }
212 else
213 {
214 #ifdef DOS_NT
215 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
216 if (!isatty (fileno (fp)))
217 setmode (fileno (fp), O_BINARY);
218 #else
219 (fp)->_flag &= ~_IOTEXT; /* read binary */
220 _setmode (fileno (fp), O_BINARY);
221 #endif
222 #endif
223 address = 0;
224 string[0] = ' ';
225 string[17] = '\0';
226 for (;;)
227 {
228 register int i, c = 0;
229
230 for (i=0; i < 16; ++i)
231 {
232 if ((c = getc (fp)) == EOF)
233 {
234 if (!i)
235 break;
236
237 fputs (" ", stdout);
238 string[i+1] = '\0';
239 }
240 else
241 {
242 if (!i)
243 printf ("%08lx: ", address);
244
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);
252 }
253
254 if ((i&group_by) == group_by)
255 putchar (' ');
256 }
257
258 if (i)
259 puts (string);
260
261 if (c == EOF)
262 break;
263
264 address += 0x10;
265
266 }
267 }
268
269 if (fp != stdin)
270 fclose (fp);
271
272 } while (*argv != NULL);
273 return EXIT_SUCCESS;
274 }
275
276 void
277 usage ()
278 {
279 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
280 exit (EXIT_FAILURE);
281 }
282
283 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
284 (do not change this comment) */
285
286 /* hexl.c ends here */