(server-process-filter): Don't force the authentication
[bpt/emacs.git] / lib-src / hexl.c
CommitLineData
2f939ddd 1/* Convert files for Emacs Hexl mode.
b3d90e46 2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005,
a5b68355 3 2006, 2007, 2008 Free Software Foundation, Inc.
2f939ddd
RS
4
5This file is not considered part of GNU Emacs.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4a9f99bd
GM
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
2f939ddd
RS
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
4a9f99bd
GM
18along with this program; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
2f939ddd 21
0072414b
RS
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
bdf34e49
JB
26#include <stdio.h>
27#include <ctype.h>
eac1956a 28#ifdef DOS_NT
20333561 29#include <fcntl.h>
659d3b43
RS
30#if __DJGPP__ >= 2
31#include <io.h>
32#endif
20333561 33#endif
eac1956a
RS
34#ifdef WINDOWSNT
35#include <io.h>
36#endif
bdf34e49
JB
37
38#define DEFAULT_GROUPING 0x01
39#define DEFAULT_BASE 16
40
41#undef TRUE
42#undef FALSE
43#define TRUE (1)
44#define FALSE (0)
45
bdf34e49
JB
46int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
47int group_by = DEFAULT_GROUPING;
48char *progname;
49
340ff9de
DM
50void usage();
51
52int
20333561
RS
53main (argc, argv)
54 int argc;
55 char *argv[];
bdf34e49 56{
20333561
RS
57 register long address;
58 char string[18];
59 FILE *fp;
60
61 progname = *argv++; --argc;
62
63 /*
64 ** -hex hex dump
65 ** -oct Octal dump
66 ** -group-by-8-bits
67 ** -group-by-16-bits
68 ** -group-by-32-bits
69 ** -group-by-64-bits
70 ** -iso iso character set.
71 ** -big-endian Big Endian
72 ** -little-endian Little Endian
73 ** -un || -de from hexl format to binary.
74 ** -- End switch list.
75 ** <filename> dump filename
76 ** - (as filename == stdin)
77 */
80b2cbf2 78
20333561 79 while (*argv && *argv[0] == '-' && (*argv)[1])
bdf34e49 80 {
20333561
RS
81 /* A switch! */
82 if (!strcmp (*argv, "--"))
bdf34e49 83 {
20333561
RS
84 --argc; argv++;
85 break;
86 }
87 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
bdf34e49 88 {
20333561
RS
89 un_flag = TRUE;
90 --argc; argv++;
91 }
92 else if (!strcmp (*argv, "-hex"))
bdf34e49 93 {
20333561
RS
94 base = 16;
95 --argc; argv++;
96 }
97 else if (!strcmp (*argv, "-iso"))
bdf34e49 98 {
20333561
RS
99 iso_flag = TRUE;
100 --argc; argv++;
101 }
102 else if (!strcmp (*argv, "-oct"))
bdf34e49 103 {
20333561
RS
104 base = 8;
105 --argc; argv++;
106 }
107 else if (!strcmp (*argv, "-big-endian"))
bdf34e49 108 {
20333561
RS
109 endian = 1;
110 --argc; argv++;
111 }
112 else if (!strcmp (*argv, "-little-endian"))
bdf34e49 113 {
20333561
RS
114 endian = 0;
115 --argc; argv++;
116 }
117 else if (!strcmp (*argv, "-group-by-8-bits"))
bdf34e49 118 {
20333561
RS
119 group_by = 0x00;
120 --argc; argv++;
121 }
122 else if (!strcmp (*argv, "-group-by-16-bits"))
bdf34e49 123 {
20333561
RS
124 group_by = 0x01;
125 --argc; argv++;
126 }
127 else if (!strcmp (*argv, "-group-by-32-bits"))
bdf34e49 128 {
20333561
RS
129 group_by = 0x03;
130 --argc; argv++;
131 }
132 else if (!strcmp (*argv, "-group-by-64-bits"))
bdf34e49 133 {
20333561
RS
134 group_by = 0x07;
135 endian = 0;
136 --argc; argv++;
137 }
138 else
bdf34e49 139 {
20333561
RS
140 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
141 *argv);
142 usage ();
bdf34e49
JB
143 }
144 }
145
20333561 146 do
bdf34e49 147 {
20333561
RS
148 if (*argv == NULL)
149 fp = stdin;
150 else
bdf34e49 151 {
20333561 152 char *filename = *argv++;
bdf34e49 153
20333561
RS
154 if (!strcmp (filename, "-"))
155 fp = stdin;
156 else if ((fp = fopen (filename, "r")) == NULL)
157 {
158 perror (filename);
159 continue;
160 }
bdf34e49
JB
161 }
162
20333561 163 if (un_flag)
bdf34e49 164 {
20333561 165 char buf[18];
bdf34e49 166
eac1956a
RS
167#ifdef DOS_NT
168#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
3f5b4e35
RS
169 if (!isatty (fileno (stdout)))
170 setmode (fileno (stdout), O_BINARY);
659d3b43 171#else
20333561
RS
172 (stdout)->_flag &= ~_IOTEXT; /* print binary */
173 _setmode (fileno (stdout), O_BINARY);
659d3b43 174#endif
20333561
RS
175#endif
176 for (;;)
bdf34e49 177 {
4431cfb5 178 register int i, c = 0, d;
bdf34e49 179
20333561 180#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
bdf34e49 181
20333561 182 fread (buf, 1, 10, fp); /* skip 10 bytes */
bdf34e49 183
20333561 184 for (i=0; i < 16; ++i)
bdf34e49 185 {
20333561
RS
186 if ((c = getc (fp)) == ' ' || c == EOF)
187 break;
bdf34e49 188
20333561
RS
189 d = getc (fp);
190 c = hexchar (c) * 0x10 + hexchar (d);
191 putchar (c);
bdf34e49 192
20333561
RS
193 if ((i&group_by) == group_by)
194 getc (fp);
bdf34e49
JB
195 }
196
20333561 197 if (c == ' ')
bdf34e49 198 {
20333561
RS
199 while ((c = getc (fp)) != '\n' && c != EOF)
200 ;
bdf34e49 201
20333561
RS
202 if (c == EOF)
203 break;
bdf34e49 204 }
20333561 205 else
bdf34e49 206 {
20333561
RS
207 if (i < 16)
208 break;
bdf34e49 209
20333561 210 fread (buf, 1, 18, fp); /* skip 18 bytes */
bdf34e49
JB
211 }
212 }
213 }
20333561 214 else
bdf34e49 215 {
eac1956a
RS
216#ifdef DOS_NT
217#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
3f5b4e35
RS
218 if (!isatty (fileno (fp)))
219 setmode (fileno (fp), O_BINARY);
659d3b43 220#else
20333561
RS
221 (fp)->_flag &= ~_IOTEXT; /* read binary */
222 _setmode (fileno (fp), O_BINARY);
659d3b43 223#endif
20333561
RS
224#endif
225 address = 0;
226 string[0] = ' ';
227 string[17] = '\0';
228 for (;;)
bdf34e49 229 {
4431cfb5 230 register int i, c = 0;
bdf34e49 231
20333561 232 for (i=0; i < 16; ++i)
bdf34e49 233 {
20333561 234 if ((c = getc (fp)) == EOF)
bdf34e49 235 {
20333561
RS
236 if (!i)
237 break;
bdf34e49 238
20333561
RS
239 fputs (" ", stdout);
240 string[i+1] = '\0';
bdf34e49 241 }
20333561 242 else
bdf34e49 243 {
20333561 244 if (!i)
345a935c 245 printf ("%08lx: ", address);
bdf34e49 246
20333561
RS
247 if (iso_flag)
248 string[i+1] =
249 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
250 else
251 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
bdf34e49 252
20333561 253 printf ("%02x", c);
bdf34e49
JB
254 }
255
20333561
RS
256 if ((i&group_by) == group_by)
257 putchar (' ');
bdf34e49
JB
258 }
259
20333561
RS
260 if (i)
261 puts (string);
bdf34e49 262
20333561
RS
263 if (c == EOF)
264 break;
bdf34e49 265
20333561 266 address += 0x10;
bdf34e49
JB
267
268 }
269 }
270
20333561
RS
271 if (fp != stdin)
272 fclose (fp);
bdf34e49
JB
273
274 } while (*argv != NULL);
65396510 275 return EXIT_SUCCESS;
bdf34e49
JB
276}
277
340ff9de 278void
20333561 279usage ()
bdf34e49 280{
20333561 281 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
65396510 282 exit (EXIT_FAILURE);
bdf34e49 283}
ab5796a9
MB
284
285/* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
286 (do not change this comment) */
65396510
TTN
287
288/* hexl.c ends here */