(Minibuffer Commands): Use last-command-event rather than last-command-char.
[emacs.git] / lib-src / hexl.c
blob6002e020b7dc15dbf4e2254f3a734f141d8f92c3
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
5 This file is not considered part of GNU Emacs.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
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 #undef TRUE
41 #undef FALSE
42 #define TRUE (1)
43 #define FALSE (0)
45 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
46 int group_by = DEFAULT_GROUPING;
47 char *progname;
49 void usage();
51 int
52 main (argc, argv)
53 int argc;
54 char *argv[];
56 register long address;
57 char string[18];
58 FILE *fp;
60 progname = *argv++; --argc;
63 ** -hex hex dump
64 ** -oct Octal dump
65 ** -group-by-8-bits
66 ** -group-by-16-bits
67 ** -group-by-32-bits
68 ** -group-by-64-bits
69 ** -iso iso character set.
70 ** -big-endian Big Endian
71 ** -little-endian Little Endian
72 ** -un || -de from hexl format to binary.
73 ** -- End switch list.
74 ** <filename> dump filename
75 ** - (as filename == stdin)
78 while (*argv && *argv[0] == '-' && (*argv)[1])
80 /* A switch! */
81 if (!strcmp (*argv, "--"))
83 --argc; argv++;
84 break;
86 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
88 un_flag = TRUE;
89 --argc; argv++;
91 else if (!strcmp (*argv, "-hex"))
93 base = 16;
94 --argc; argv++;
96 else if (!strcmp (*argv, "-iso"))
98 iso_flag = TRUE;
99 --argc; argv++;
101 else if (!strcmp (*argv, "-oct"))
103 base = 8;
104 --argc; argv++;
106 else if (!strcmp (*argv, "-big-endian"))
108 endian = 1;
109 --argc; argv++;
111 else if (!strcmp (*argv, "-little-endian"))
113 endian = 0;
114 --argc; argv++;
116 else if (!strcmp (*argv, "-group-by-8-bits"))
118 group_by = 0x00;
119 --argc; argv++;
121 else if (!strcmp (*argv, "-group-by-16-bits"))
123 group_by = 0x01;
124 --argc; argv++;
126 else if (!strcmp (*argv, "-group-by-32-bits"))
128 group_by = 0x03;
129 --argc; argv++;
131 else if (!strcmp (*argv, "-group-by-64-bits"))
133 group_by = 0x07;
134 endian = 0;
135 --argc; argv++;
137 else
139 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
140 *argv);
141 usage ();
147 if (*argv == NULL)
148 fp = stdin;
149 else
151 char *filename = *argv++;
153 if (!strcmp (filename, "-"))
154 fp = stdin;
155 else if ((fp = fopen (filename, "r")) == NULL)
157 perror (filename);
158 continue;
162 if (un_flag)
164 char buf[18];
166 #ifdef DOS_NT
167 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
168 if (!isatty (fileno (stdout)))
169 setmode (fileno (stdout), O_BINARY);
170 #else
171 (stdout)->_flag &= ~_IOTEXT; /* print binary */
172 _setmode (fileno (stdout), O_BINARY);
173 #endif
174 #endif
175 for (;;)
177 register int i, c = 0, d;
179 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
181 fread (buf, 1, 10, fp); /* skip 10 bytes */
183 for (i=0; i < 16; ++i)
185 if ((c = getc (fp)) == ' ' || c == EOF)
186 break;
188 d = getc (fp);
189 c = hexchar (c) * 0x10 + hexchar (d);
190 putchar (c);
192 if ((i&group_by) == group_by)
193 getc (fp);
196 if (c == ' ')
198 while ((c = getc (fp)) != '\n' && c != EOF)
201 if (c == EOF)
202 break;
204 else
206 if (i < 16)
207 break;
209 fread (buf, 1, 18, fp); /* skip 18 bytes */
213 else
215 #ifdef DOS_NT
216 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
217 if (!isatty (fileno (fp)))
218 setmode (fileno (fp), O_BINARY);
219 #else
220 (fp)->_flag &= ~_IOTEXT; /* read binary */
221 _setmode (fileno (fp), O_BINARY);
222 #endif
223 #endif
224 address = 0;
225 string[0] = ' ';
226 string[17] = '\0';
227 for (;;)
229 register int i, c = 0;
231 for (i=0; i < 16; ++i)
233 if ((c = getc (fp)) == EOF)
235 if (!i)
236 break;
238 fputs (" ", stdout);
239 string[i+1] = '\0';
241 else
243 if (!i)
244 printf ("%08lx: ", address);
246 if (iso_flag)
247 string[i+1] =
248 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
249 else
250 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
252 printf ("%02x", c);
255 if ((i&group_by) == group_by)
256 putchar (' ');
259 if (i)
260 puts (string);
262 if (c == EOF)
263 break;
265 address += 0x10;
270 if (fp != stdin)
271 fclose (fp);
273 } while (*argv != NULL);
274 return EXIT_SUCCESS;
277 void
278 usage ()
280 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
281 exit (EXIT_FAILURE);
284 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
285 (do not change this comment) */
287 /* hexl.c ends here */