1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2002, 2003, 2004, 2005,
3 2006 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 2 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, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
37 #define DEFAULT_GROUPING 0x01
38 #define DEFAULT_BASE 16
45 int base
= DEFAULT_BASE
, un_flag
= FALSE
, iso_flag
= FALSE
, endian
= 1;
46 int group_by
= DEFAULT_GROUPING
;
56 register long address
;
60 progname
= *argv
++; --argc
;
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])
81 if (!strcmp (*argv
, "--"))
86 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
91 else if (!strcmp (*argv
, "-hex"))
96 else if (!strcmp (*argv
, "-iso"))
101 else if (!strcmp (*argv
, "-oct"))
106 else if (!strcmp (*argv
, "-big-endian"))
111 else if (!strcmp (*argv
, "-little-endian"))
116 else if (!strcmp (*argv
, "-group-by-8-bits"))
121 else if (!strcmp (*argv
, "-group-by-16-bits"))
126 else if (!strcmp (*argv
, "-group-by-32-bits"))
131 else if (!strcmp (*argv
, "-group-by-64-bits"))
139 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
151 char *filename
= *argv
++;
153 if (!strcmp (filename
, "-"))
155 else if ((fp
= fopen (filename
, "r")) == NULL
)
167 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
168 if (!isatty (fileno (stdout
)))
169 setmode (fileno (stdout
), O_BINARY
);
171 (stdout
)->_flag
&= ~_IOTEXT
; /* print binary */
172 _setmode (fileno (stdout
), O_BINARY
);
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
)
189 c
= hexchar (c
) * 0x10 + hexchar (d
);
192 if ((i
&group_by
) == group_by
)
198 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
209 fread (buf
, 1, 18, fp
); /* skip 18 bytes */
216 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
217 if (!isatty (fileno (fp
)))
218 setmode (fileno (fp
), O_BINARY
);
220 (fp
)->_flag
&= ~_IOTEXT
; /* read binary */
221 _setmode (fileno (fp
), O_BINARY
);
229 register int i
, c
= 0;
231 for (i
=0; i
< 16; ++i
)
233 if ((c
= getc (fp
)) == EOF
)
244 printf ("%08lx: ", address
);
248 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
250 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
255 if ((i
&group_by
) == group_by
)
273 } while (*argv
!= NULL
);
280 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
284 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
285 (do not change this comment) */
287 /* hexl.c ends here */