Fix bug #17199 with incorrect character produced by ipa-x-sampa IM.
[emacs.git] / lib-src / hexl.c
blob9e21ddf9de639aef1a0f0a7db83fdc1df0ca8d8f
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2014 Free Software Foundation, Inc.
4 Author: Keith Gabryelski
5 (according to authors.el)
7 This file is not considered part of GNU Emacs.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #ifdef DOS_NT
28 #include <fcntl.h>
29 #if __DJGPP__ >= 2
30 #include <io.h>
31 #endif
32 #endif
33 #ifdef WINDOWSNT
34 #include <io.h>
35 #endif
37 #define DEFAULT_GROUPING 0x01
38 #define DEFAULT_BASE 16
40 int base = DEFAULT_BASE;
41 bool un_flag = false, iso_flag = false, endian = true;
42 int group_by = DEFAULT_GROUPING;
43 char *progname;
45 _Noreturn void usage (void);
47 int
48 main (int argc, char **argv)
50 register long address;
51 char string[18];
52 FILE *fp;
54 progname = *argv++; --argc;
57 ** -hex hex dump
58 ** -oct Octal dump
59 ** -group-by-8-bits
60 ** -group-by-16-bits
61 ** -group-by-32-bits
62 ** -group-by-64-bits
63 ** -iso iso character set.
64 ** -big-endian Big Endian
65 ** -little-endian Little Endian
66 ** -un || -de from hexl format to binary.
67 ** -- End switch list.
68 ** <filename> dump filename
69 ** - (as filename == stdin)
72 while (*argv && *argv[0] == '-' && (*argv)[1])
74 /* A switch! */
75 if (!strcmp (*argv, "--"))
77 --argc; argv++;
78 break;
80 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
82 un_flag = true;
83 --argc; argv++;
85 else if (!strcmp (*argv, "-hex"))
87 base = 16;
88 --argc; argv++;
90 else if (!strcmp (*argv, "-iso"))
92 iso_flag = true;
93 --argc; argv++;
95 else if (!strcmp (*argv, "-oct"))
97 base = 8;
98 --argc; argv++;
100 else if (!strcmp (*argv, "-big-endian"))
102 endian = true;
103 --argc; argv++;
105 else if (!strcmp (*argv, "-little-endian"))
107 endian = false;
108 --argc; argv++;
110 else if (!strcmp (*argv, "-group-by-8-bits"))
112 group_by = 0x00;
113 --argc; argv++;
115 else if (!strcmp (*argv, "-group-by-16-bits"))
117 group_by = 0x01;
118 --argc; argv++;
120 else if (!strcmp (*argv, "-group-by-32-bits"))
122 group_by = 0x03;
123 --argc; argv++;
125 else if (!strcmp (*argv, "-group-by-64-bits"))
127 group_by = 0x07;
128 endian = false;
129 --argc; argv++;
131 else
133 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
134 *argv);
135 usage ();
141 if (*argv == NULL)
142 fp = stdin;
143 else
145 char *filename = *argv++;
147 if (!strcmp (filename, "-"))
148 fp = stdin;
149 else if ((fp = fopen (filename, "r")) == NULL)
151 perror (filename);
152 continue;
156 if (un_flag)
158 char buf[18];
160 #ifdef DOS_NT
161 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
162 if (!isatty (fileno (stdout)))
163 setmode (fileno (stdout), O_BINARY);
164 #else
165 (stdout)->_flag &= ~_IOTEXT; /* print binary */
166 _setmode (fileno (stdout), O_BINARY);
167 #endif
168 #endif
169 for (;;)
171 register int i, c = 0, d;
173 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
175 /* Skip 10 bytes. */
176 if (fread (buf, 1, 10, fp) != 10)
177 break;
179 for (i=0; i < 16; ++i)
181 if ((c = getc (fp)) == ' ' || c == EOF)
182 break;
184 d = getc (fp);
185 c = hexchar (c) * 0x10 + hexchar (d);
186 putchar (c);
188 if ((i&group_by) == group_by)
189 getc (fp);
192 if (c == ' ')
194 while ((c = getc (fp)) != '\n' && c != EOF)
197 if (c == EOF)
198 break;
200 else
202 if (i < 16)
203 break;
205 /* Skip 18 bytes. */
206 if (fread (buf, 1, 18, fp) != 18)
207 break;
211 else
213 #ifdef DOS_NT
214 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
215 if (!isatty (fileno (fp)))
216 setmode (fileno (fp), O_BINARY);
217 #else
218 (fp)->_flag &= ~_IOTEXT; /* read binary */
219 _setmode (fileno (fp), O_BINARY);
220 #endif
221 #endif
222 address = 0;
223 string[0] = ' ';
224 string[17] = '\0';
225 for (;;)
227 register int i, c = 0;
229 for (i=0; i < 16; ++i)
231 if ((c = getc (fp)) == EOF)
233 if (!i)
234 break;
236 fputs (" ", stdout);
237 string[i+1] = '\0';
239 else
241 if (!i)
242 printf ("%08lx: ", address);
244 if (iso_flag)
245 string[i+1] =
246 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
247 else
248 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
250 printf ("%02x", c);
253 if ((i&group_by) == group_by)
254 putchar (' ');
257 if (i)
258 puts (string);
260 if (c == EOF)
261 break;
263 address += 0x10;
268 if (fp != stdin)
269 fclose (fp);
271 } while (*argv != NULL);
272 return EXIT_SUCCESS;
275 void
276 usage (void)
278 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
279 exit (EXIT_FAILURE);
283 /* hexl.c ends here */