Merge branch 'master' into comment-cache
[emacs.git] / lib-src / hexl.c
blob2eef7b3a63dda75da21f709987b6631fdc146301
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2017 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 <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
29 #include <binary-io.h>
31 #define DEFAULT_GROUPING 0x01
32 #define DEFAULT_BASE 16
34 int base = DEFAULT_BASE;
35 bool un_flag = false, iso_flag = false, endian = true;
36 int group_by = DEFAULT_GROUPING;
37 char *progname;
39 _Noreturn void usage (void);
41 int
42 main (int argc, char **argv)
44 register long address;
45 char string[18];
46 FILE *fp;
48 progname = *argv++; --argc;
51 ** -hex hex dump
52 ** -oct Octal dump
53 ** -group-by-8-bits
54 ** -group-by-16-bits
55 ** -group-by-32-bits
56 ** -group-by-64-bits
57 ** -iso iso character set.
58 ** -big-endian Big Endian
59 ** -little-endian Little Endian
60 ** -un || -de from hexl format to binary.
61 ** -- End switch list.
62 ** <filename> dump filename
63 ** - (as filename == stdin)
66 while (*argv && *argv[0] == '-' && (*argv)[1])
68 /* A switch! */
69 if (!strcmp (*argv, "--"))
71 --argc; argv++;
72 break;
74 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
76 un_flag = true;
77 --argc; argv++;
79 else if (!strcmp (*argv, "-hex"))
81 base = 16;
82 --argc; argv++;
84 else if (!strcmp (*argv, "-iso"))
86 iso_flag = true;
87 --argc; argv++;
89 else if (!strcmp (*argv, "-oct"))
91 base = 8;
92 --argc; argv++;
94 else if (!strcmp (*argv, "-big-endian"))
96 endian = true;
97 --argc; argv++;
99 else if (!strcmp (*argv, "-little-endian"))
101 endian = false;
102 --argc; argv++;
104 else if (!strcmp (*argv, "-group-by-8-bits"))
106 group_by = 0x00;
107 --argc; argv++;
109 else if (!strcmp (*argv, "-group-by-16-bits"))
111 group_by = 0x01;
112 --argc; argv++;
114 else if (!strcmp (*argv, "-group-by-32-bits"))
116 group_by = 0x03;
117 --argc; argv++;
119 else if (!strcmp (*argv, "-group-by-64-bits"))
121 group_by = 0x07;
122 endian = false;
123 --argc; argv++;
125 else
127 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
128 *argv);
129 usage ();
135 if (*argv == NULL)
136 fp = stdin;
137 else
139 char *filename = *argv++;
141 if (!strcmp (filename, "-"))
142 fp = stdin;
143 else if ((fp = fopen (filename, "r")) == NULL)
145 perror (filename);
146 continue;
150 if (un_flag)
152 SET_BINARY (fileno (stdout));
154 for (;;)
156 int i, c = 0, d;
157 char buf[18];
159 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
161 /* Skip 10 bytes. */
162 if (fread (buf, 1, 10, fp) != 10)
163 break;
165 for (i=0; i < 16; ++i)
167 if ((c = getc (fp)) == ' ' || c == EOF)
168 break;
170 d = getc (fp);
171 c = hexchar (c) * 0x10 + hexchar (d);
172 putchar (c);
174 if ((i&group_by) == group_by)
175 getc (fp);
178 if (c == ' ')
180 while ((c = getc (fp)) != '\n' && c != EOF)
183 if (c == EOF)
184 break;
186 else
188 if (i < 16)
189 break;
191 /* Skip 18 bytes. */
192 if (fread (buf, 1, 18, fp) != 18)
193 break;
197 else
199 SET_BINARY (fileno (fp));
200 address = 0;
201 string[0] = ' ';
202 string[17] = '\0';
203 for (;;)
205 register int i, c = 0;
207 for (i=0; i < 16; ++i)
209 if ((c = getc (fp)) == EOF)
211 if (!i)
212 break;
214 fputs (" ", stdout);
215 string[i+1] = '\0';
217 else
219 if (!i)
220 printf ("%08lx: ", address + 0ul);
222 if (iso_flag)
223 string[i+1] =
224 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
225 else
226 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
228 printf ("%02x", c + 0u);
231 if ((i&group_by) == group_by)
232 putchar (' ');
235 if (i)
236 puts (string);
238 if (c == EOF)
239 break;
241 address += 0x10;
246 if (fp != stdin)
247 fclose (fp);
249 } while (*argv != NULL);
250 return EXIT_SUCCESS;
253 void
254 usage (void)
256 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
257 exit (EXIT_FAILURE);
261 /* hexl.c ends here */