Eldoc setup code enhancements
[emacs.git] / lib-src / hexl.c
blob51744ab08a2b2e552379f480fb504e7c9a80ac04
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>
28 #include <binary-io.h>
30 #define DEFAULT_GROUPING 0x01
31 #define DEFAULT_BASE 16
33 int base = DEFAULT_BASE;
34 bool un_flag = false, iso_flag = false, endian = true;
35 int group_by = DEFAULT_GROUPING;
36 char *progname;
38 _Noreturn void usage (void);
40 int
41 main (int argc, char **argv)
43 register long address;
44 char string[18];
45 FILE *fp;
47 progname = *argv++; --argc;
50 ** -hex hex dump
51 ** -oct Octal dump
52 ** -group-by-8-bits
53 ** -group-by-16-bits
54 ** -group-by-32-bits
55 ** -group-by-64-bits
56 ** -iso iso character set.
57 ** -big-endian Big Endian
58 ** -little-endian Little Endian
59 ** -un || -de from hexl format to binary.
60 ** -- End switch list.
61 ** <filename> dump filename
62 ** - (as filename == stdin)
65 while (*argv && *argv[0] == '-' && (*argv)[1])
67 /* A switch! */
68 if (!strcmp (*argv, "--"))
70 --argc; argv++;
71 break;
73 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
75 un_flag = true;
76 --argc; argv++;
78 else if (!strcmp (*argv, "-hex"))
80 base = 16;
81 --argc; argv++;
83 else if (!strcmp (*argv, "-iso"))
85 iso_flag = true;
86 --argc; argv++;
88 else if (!strcmp (*argv, "-oct"))
90 base = 8;
91 --argc; argv++;
93 else if (!strcmp (*argv, "-big-endian"))
95 endian = true;
96 --argc; argv++;
98 else if (!strcmp (*argv, "-little-endian"))
100 endian = false;
101 --argc; argv++;
103 else if (!strcmp (*argv, "-group-by-8-bits"))
105 group_by = 0x00;
106 --argc; argv++;
108 else if (!strcmp (*argv, "-group-by-16-bits"))
110 group_by = 0x01;
111 --argc; argv++;
113 else if (!strcmp (*argv, "-group-by-32-bits"))
115 group_by = 0x03;
116 --argc; argv++;
118 else if (!strcmp (*argv, "-group-by-64-bits"))
120 group_by = 0x07;
121 endian = false;
122 --argc; argv++;
124 else
126 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
127 *argv);
128 usage ();
134 if (*argv == NULL)
135 fp = stdin;
136 else
138 char *filename = *argv++;
140 if (!strcmp (filename, "-"))
141 fp = stdin;
142 else if ((fp = fopen (filename, "r")) == NULL)
144 perror (filename);
145 continue;
149 if (un_flag)
151 SET_BINARY (fileno (stdout));
153 for (;;)
155 int i, c = 0, d;
156 char buf[18];
158 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
160 /* Skip 10 bytes. */
161 if (fread (buf, 1, 10, fp) != 10)
162 break;
164 for (i=0; i < 16; ++i)
166 if ((c = getc (fp)) == ' ' || c == EOF)
167 break;
169 d = getc (fp);
170 c = hexchar (c) * 0x10 + hexchar (d);
171 putchar (c);
173 if ((i&group_by) == group_by)
174 getc (fp);
177 if (c == ' ')
179 while ((c = getc (fp)) != '\n' && c != EOF)
182 if (c == EOF)
183 break;
185 else
187 if (i < 16)
188 break;
190 /* Skip 18 bytes. */
191 if (fread (buf, 1, 18, fp) != 18)
192 break;
196 else
198 SET_BINARY (fileno (fp));
199 address = 0;
200 string[0] = ' ';
201 string[17] = '\0';
202 for (;;)
204 register int i, c = 0;
206 for (i=0; i < 16; ++i)
208 if ((c = getc (fp)) == EOF)
210 if (!i)
211 break;
213 fputs (" ", stdout);
214 string[i+1] = '\0';
216 else
218 if (!i)
219 printf ("%08lx: ", address);
221 if (iso_flag)
222 string[i+1] =
223 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
224 else
225 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
227 printf ("%02x", c);
230 if ((i&group_by) == group_by)
231 putchar (' ');
234 if (i)
235 puts (string);
237 if (c == EOF)
238 break;
240 address += 0x10;
245 if (fp != stdin)
246 fclose (fp);
248 } while (*argv != NULL);
249 return EXIT_SUCCESS;
252 void
253 usage (void)
255 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
256 exit (EXIT_FAILURE);
260 /* hexl.c ends here */