1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 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
;
52 void usage(void) NO_RETURN
;
55 main (int argc
, char **argv
)
57 register long address
;
61 progname
= *argv
++; --argc
;
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)
79 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
82 if (!strcmp (*argv
, "--"))
87 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
92 else if (!strcmp (*argv
, "-hex"))
97 else if (!strcmp (*argv
, "-iso"))
102 else if (!strcmp (*argv
, "-oct"))
107 else if (!strcmp (*argv
, "-big-endian"))
112 else if (!strcmp (*argv
, "-little-endian"))
117 else if (!strcmp (*argv
, "-group-by-8-bits"))
122 else if (!strcmp (*argv
, "-group-by-16-bits"))
127 else if (!strcmp (*argv
, "-group-by-32-bits"))
132 else if (!strcmp (*argv
, "-group-by-64-bits"))
140 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
152 char *filename
= *argv
++;
154 if (!strcmp (filename
, "-"))
156 else if ((fp
= fopen (filename
, "r")) == NULL
)
168 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
169 if (!isatty (fileno (stdout
)))
170 setmode (fileno (stdout
), O_BINARY
);
172 (stdout
)->_flag
&= ~_IOTEXT
; /* print binary */
173 _setmode (fileno (stdout
), O_BINARY
);
178 register int i
, c
= 0, d
;
180 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
182 fread (buf
, 1, 10, fp
); /* skip 10 bytes */
184 for (i
=0; i
< 16; ++i
)
186 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
190 c
= hexchar (c
) * 0x10 + hexchar (d
);
193 if ((i
&group_by
) == group_by
)
199 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
210 fread (buf
, 1, 18, fp
); /* skip 18 bytes */
217 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
218 if (!isatty (fileno (fp
)))
219 setmode (fileno (fp
), O_BINARY
);
221 (fp
)->_flag
&= ~_IOTEXT
; /* read binary */
222 _setmode (fileno (fp
), O_BINARY
);
230 register int i
, c
= 0;
232 for (i
=0; i
< 16; ++i
)
234 if ((c
= getc (fp
)) == EOF
)
245 printf ("%08lx: ", address
);
249 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
251 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
256 if ((i
&group_by
) == group_by
)
274 } while (*argv
!= NULL
);
281 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
285 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
286 (do not change this comment) */
288 /* hexl.c ends here */