Support building Emacs on MS-Windows using the Posix configury.
[bpt/emacs.git] / lib-src / hexl.c
CommitLineData
2f939ddd 1/* Convert files for Emacs Hexl mode.
ab422c4d 2 Copyright (C) 1989, 2001-2013 Free Software Foundation, Inc.
eedcb417
GM
3
4Author: Keith Gabryelski
5(according to authors.el)
2f939ddd
RS
6
7This file is not considered part of GNU Emacs.
8
294981c7 9This program is free software: you can redistribute it and/or modify
2f939ddd 10it under the terms of the GNU General Public License as published by
294981c7
GM
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
2f939ddd
RS
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
294981c7
GM
20along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
2f939ddd 22
0072414b 23#include <config.h>
0072414b 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
45int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
46int group_by = DEFAULT_GROUPING;
47char *progname;
48
845ca893 49_Noreturn void usage (void);
340ff9de
DM
50
51int
873fbd0b 52main (int argc, char **argv)
bdf34e49 53{
20333561
RS
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 */
80b2cbf2 75
20333561 76 while (*argv && *argv[0] == '-' && (*argv)[1])
bdf34e49 77 {
20333561
RS
78 /* A switch! */
79 if (!strcmp (*argv, "--"))
bdf34e49 80 {
20333561
RS
81 --argc; argv++;
82 break;
83 }
84 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
bdf34e49 85 {
20333561
RS
86 un_flag = TRUE;
87 --argc; argv++;
88 }
89 else if (!strcmp (*argv, "-hex"))
bdf34e49 90 {
20333561
RS
91 base = 16;
92 --argc; argv++;
93 }
94 else if (!strcmp (*argv, "-iso"))
bdf34e49 95 {
20333561
RS
96 iso_flag = TRUE;
97 --argc; argv++;
98 }
99 else if (!strcmp (*argv, "-oct"))
bdf34e49 100 {
20333561
RS
101 base = 8;
102 --argc; argv++;
103 }
104 else if (!strcmp (*argv, "-big-endian"))
bdf34e49 105 {
20333561
RS
106 endian = 1;
107 --argc; argv++;
108 }
109 else if (!strcmp (*argv, "-little-endian"))
bdf34e49 110 {
20333561
RS
111 endian = 0;
112 --argc; argv++;
113 }
114 else if (!strcmp (*argv, "-group-by-8-bits"))
bdf34e49 115 {
20333561
RS
116 group_by = 0x00;
117 --argc; argv++;
118 }
119 else if (!strcmp (*argv, "-group-by-16-bits"))
bdf34e49 120 {
20333561
RS
121 group_by = 0x01;
122 --argc; argv++;
123 }
124 else if (!strcmp (*argv, "-group-by-32-bits"))
bdf34e49 125 {
20333561
RS
126 group_by = 0x03;
127 --argc; argv++;
128 }
129 else if (!strcmp (*argv, "-group-by-64-bits"))
bdf34e49 130 {
20333561
RS
131 group_by = 0x07;
132 endian = 0;
133 --argc; argv++;
134 }
135 else
bdf34e49 136 {
20333561
RS
137 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
138 *argv);
139 usage ();
bdf34e49
JB
140 }
141 }
142
20333561 143 do
bdf34e49 144 {
20333561
RS
145 if (*argv == NULL)
146 fp = stdin;
147 else
bdf34e49 148 {
20333561 149 char *filename = *argv++;
bdf34e49 150
20333561
RS
151 if (!strcmp (filename, "-"))
152 fp = stdin;
153 else if ((fp = fopen (filename, "r")) == NULL)
154 {
155 perror (filename);
156 continue;
157 }
bdf34e49
JB
158 }
159
20333561 160 if (un_flag)
bdf34e49 161 {
20333561 162 char buf[18];
bdf34e49 163
eac1956a
RS
164#ifdef DOS_NT
165#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
3f5b4e35
RS
166 if (!isatty (fileno (stdout)))
167 setmode (fileno (stdout), O_BINARY);
659d3b43 168#else
20333561
RS
169 (stdout)->_flag &= ~_IOTEXT; /* print binary */
170 _setmode (fileno (stdout), O_BINARY);
659d3b43 171#endif
20333561
RS
172#endif
173 for (;;)
bdf34e49 174 {
4431cfb5 175 register int i, c = 0, d;
bdf34e49 176
20333561 177#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
bdf34e49 178
9055082e
PE
179 /* Skip 10 bytes. */
180 if (fread (buf, 1, 10, fp) != 10)
181 break;
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
9055082e
PE
209 /* Skip 18 bytes. */
210 if (fread (buf, 1, 18, fp) != 18)
211 break;
bdf34e49
JB
212 }
213 }
214 }
20333561 215 else
bdf34e49 216 {
eac1956a
RS
217#ifdef DOS_NT
218#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
3f5b4e35
RS
219 if (!isatty (fileno (fp)))
220 setmode (fileno (fp), O_BINARY);
659d3b43 221#else
20333561
RS
222 (fp)->_flag &= ~_IOTEXT; /* read binary */
223 _setmode (fileno (fp), O_BINARY);
659d3b43 224#endif
20333561
RS
225#endif
226 address = 0;
227 string[0] = ' ';
228 string[17] = '\0';
229 for (;;)
bdf34e49 230 {
4431cfb5 231 register int i, c = 0;
bdf34e49 232
20333561 233 for (i=0; i < 16; ++i)
bdf34e49 234 {
20333561 235 if ((c = getc (fp)) == EOF)
bdf34e49 236 {
20333561
RS
237 if (!i)
238 break;
bdf34e49 239
20333561
RS
240 fputs (" ", stdout);
241 string[i+1] = '\0';
bdf34e49 242 }
20333561 243 else
bdf34e49 244 {
20333561 245 if (!i)
345a935c 246 printf ("%08lx: ", address);
bdf34e49 247
20333561
RS
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;
bdf34e49 253
20333561 254 printf ("%02x", c);
bdf34e49
JB
255 }
256
20333561
RS
257 if ((i&group_by) == group_by)
258 putchar (' ');
bdf34e49
JB
259 }
260
20333561
RS
261 if (i)
262 puts (string);
bdf34e49 263
20333561
RS
264 if (c == EOF)
265 break;
bdf34e49 266
20333561 267 address += 0x10;
bdf34e49
JB
268
269 }
270 }
271
20333561
RS
272 if (fp != stdin)
273 fclose (fp);
bdf34e49
JB
274
275 } while (*argv != NULL);
65396510 276 return EXIT_SUCCESS;
bdf34e49
JB
277}
278
340ff9de 279void
873fbd0b 280usage (void)
bdf34e49 281{
20333561 282 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
65396510 283 exit (EXIT_FAILURE);
bdf34e49 284}
ab5796a9 285
65396510
TTN
286
287/* hexl.c ends here */