; NEWS: Move entries where they belong to
[emacs.git] / lib-src / hexl.c
blob9a5ca7e1543d2560f8312d0bd0d9677dfd18efed
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2016 Free Software Foundation, Inc.
4 Author: Keith Gabryelski (according to authors.el)
6 This file is not considered part of GNU Emacs.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
27 #include <binary-io.h>
29 #define DEFAULT_GROUPING 0x01
30 #define DEFAULT_BASE 16
32 int base = DEFAULT_BASE;
33 bool un_flag = false, iso_flag = false, endian = true;
34 int group_by = DEFAULT_GROUPING;
35 char *progname;
37 _Noreturn void usage (void);
39 int
40 main (int argc, char **argv)
42 register long address;
43 char string[18];
44 FILE *fp;
46 progname = *argv++; --argc;
49 ** -hex hex dump
50 ** -oct Octal dump
51 ** -group-by-8-bits
52 ** -group-by-16-bits
53 ** -group-by-32-bits
54 ** -group-by-64-bits
55 ** -iso iso character set.
56 ** -big-endian Big Endian
57 ** -little-endian Little Endian
58 ** -un || -de from hexl format to binary.
59 ** -- End switch list.
60 ** <filename> dump filename
61 ** - (as filename == stdin)
64 while (*argv && *argv[0] == '-' && (*argv)[1])
66 /* A switch! */
67 if (!strcmp (*argv, "--"))
69 --argc; argv++;
70 break;
72 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
74 un_flag = true;
75 --argc; argv++;
77 else if (!strcmp (*argv, "-hex"))
79 base = 16;
80 --argc; argv++;
82 else if (!strcmp (*argv, "-iso"))
84 iso_flag = true;
85 --argc; argv++;
87 else if (!strcmp (*argv, "-oct"))
89 base = 8;
90 --argc; argv++;
92 else if (!strcmp (*argv, "-big-endian"))
94 endian = true;
95 --argc; argv++;
97 else if (!strcmp (*argv, "-little-endian"))
99 endian = false;
100 --argc; argv++;
102 else if (!strcmp (*argv, "-group-by-8-bits"))
104 group_by = 0x00;
105 --argc; argv++;
107 else if (!strcmp (*argv, "-group-by-16-bits"))
109 group_by = 0x01;
110 --argc; argv++;
112 else if (!strcmp (*argv, "-group-by-32-bits"))
114 group_by = 0x03;
115 --argc; argv++;
117 else if (!strcmp (*argv, "-group-by-64-bits"))
119 group_by = 0x07;
120 endian = false;
121 --argc; argv++;
123 else
125 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
126 *argv);
127 usage ();
133 if (*argv == NULL)
134 fp = stdin;
135 else
137 char *filename = *argv++;
139 if (!strcmp (filename, "-"))
140 fp = stdin;
141 else if ((fp = fopen (filename, "r")) == NULL)
143 perror (filename);
144 continue;
148 if (un_flag)
150 SET_BINARY (fileno (stdout));
152 for (;;)
154 int i, c = 0, d;
155 char buf[18];
157 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
159 /* Skip 10 bytes. */
160 if (fread (buf, 1, 10, fp) != 10)
161 break;
163 for (i=0; i < 16; ++i)
165 if ((c = getc (fp)) == ' ' || c == EOF)
166 break;
168 d = getc (fp);
169 c = hexchar (c) * 0x10 + hexchar (d);
170 putchar (c);
172 if ((i&group_by) == group_by)
173 getc (fp);
176 if (c == ' ')
178 while ((c = getc (fp)) != '\n' && c != EOF)
181 if (c == EOF)
182 break;
184 else
186 if (i < 16)
187 break;
189 /* Skip 18 bytes. */
190 if (fread (buf, 1, 18, fp) != 18)
191 break;
195 else
197 SET_BINARY (fileno (fp));
198 address = 0;
199 string[0] = ' ';
200 string[17] = '\0';
201 for (;;)
203 register int i, c = 0;
205 for (i=0; i < 16; ++i)
207 if ((c = getc (fp)) == EOF)
209 if (!i)
210 break;
212 fputs (" ", stdout);
213 string[i+1] = '\0';
215 else
217 if (!i)
218 printf ("%08lx: ", address + 0ul);
220 if (iso_flag)
221 string[i+1] =
222 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
223 else
224 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
226 printf ("%02x", c + 0u);
229 if ((i&group_by) == group_by)
230 putchar (' ');
233 if (i)
234 puts (string);
236 if (c == EOF)
237 break;
239 address += 0x10;
244 if (fp != stdin)
245 fclose (fp);
247 } while (*argv != NULL);
248 return EXIT_SUCCESS;
251 void
252 usage (void)
254 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
255 exit (EXIT_FAILURE);
259 /* hexl.c ends here */