1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009 Free Software Foundation, Inc.
5 Author: Keith Gabryelski
6 (according to authors.el)
8 This file is not considered part of GNU Emacs.
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
40 #define DEFAULT_GROUPING 0x01
41 #define DEFAULT_BASE 16
48 int base
= DEFAULT_BASE
, un_flag
= FALSE
, iso_flag
= FALSE
, endian
= 1;
49 int group_by
= DEFAULT_GROUPING
;
59 register long address
;
63 progname
= *argv
++; --argc
;
72 ** -iso iso character set.
73 ** -big-endian Big Endian
74 ** -little-endian Little Endian
75 ** -un || -de from hexl format to binary.
76 ** -- End switch list.
77 ** <filename> dump filename
78 ** - (as filename == stdin)
81 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
84 if (!strcmp (*argv
, "--"))
89 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
94 else if (!strcmp (*argv
, "-hex"))
99 else if (!strcmp (*argv
, "-iso"))
104 else if (!strcmp (*argv
, "-oct"))
109 else if (!strcmp (*argv
, "-big-endian"))
114 else if (!strcmp (*argv
, "-little-endian"))
119 else if (!strcmp (*argv
, "-group-by-8-bits"))
124 else if (!strcmp (*argv
, "-group-by-16-bits"))
129 else if (!strcmp (*argv
, "-group-by-32-bits"))
134 else if (!strcmp (*argv
, "-group-by-64-bits"))
142 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
154 char *filename
= *argv
++;
156 if (!strcmp (filename
, "-"))
158 else if ((fp
= fopen (filename
, "r")) == NULL
)
170 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
171 if (!isatty (fileno (stdout
)))
172 setmode (fileno (stdout
), O_BINARY
);
174 (stdout
)->_flag
&= ~_IOTEXT
; /* print binary */
175 _setmode (fileno (stdout
), O_BINARY
);
180 register int i
, c
= 0, d
;
182 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
184 fread (buf
, 1, 10, fp
); /* skip 10 bytes */
186 for (i
=0; i
< 16; ++i
)
188 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
192 c
= hexchar (c
) * 0x10 + hexchar (d
);
195 if ((i
&group_by
) == group_by
)
201 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
212 fread (buf
, 1, 18, fp
); /* skip 18 bytes */
219 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
220 if (!isatty (fileno (fp
)))
221 setmode (fileno (fp
), O_BINARY
);
223 (fp
)->_flag
&= ~_IOTEXT
; /* read binary */
224 _setmode (fileno (fp
), O_BINARY
);
232 register int i
, c
= 0;
234 for (i
=0; i
< 16; ++i
)
236 if ((c
= getc (fp
)) == EOF
)
247 printf ("%08lx: ", address
);
251 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
253 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
258 if ((i
&group_by
) == group_by
)
276 } while (*argv
!= NULL
);
283 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
287 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
288 (do not change this comment) */
290 /* hexl.c ends here */