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