1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2014 Free Software Foundation, Inc.
4 Author: Keith Gabryelski
5 (according to authors.el)
7 This file is not considered part of GNU Emacs.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include <binary-io.h>
30 #define DEFAULT_GROUPING 0x01
31 #define DEFAULT_BASE 16
33 int base
= DEFAULT_BASE
;
34 bool un_flag
= false, iso_flag
= false, endian
= true;
35 int group_by
= DEFAULT_GROUPING
;
38 _Noreturn
void usage (void);
41 main (int argc
, char **argv
)
43 register long address
;
47 progname
= *argv
++; --argc
;
56 ** -iso iso character set.
57 ** -big-endian Big Endian
58 ** -little-endian Little Endian
59 ** -un || -de from hexl format to binary.
60 ** -- End switch list.
61 ** <filename> dump filename
62 ** - (as filename == stdin)
65 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
68 if (!strcmp (*argv
, "--"))
73 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
78 else if (!strcmp (*argv
, "-hex"))
83 else if (!strcmp (*argv
, "-iso"))
88 else if (!strcmp (*argv
, "-oct"))
93 else if (!strcmp (*argv
, "-big-endian"))
98 else if (!strcmp (*argv
, "-little-endian"))
103 else if (!strcmp (*argv
, "-group-by-8-bits"))
108 else if (!strcmp (*argv
, "-group-by-16-bits"))
113 else if (!strcmp (*argv
, "-group-by-32-bits"))
118 else if (!strcmp (*argv
, "-group-by-64-bits"))
126 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
138 char *filename
= *argv
++;
140 if (!strcmp (filename
, "-"))
142 else if ((fp
= fopen (filename
, "r")) == NULL
)
151 SET_BINARY (fileno (stdout
));
158 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
161 if (fread (buf
, 1, 10, fp
) != 10)
164 for (i
=0; i
< 16; ++i
)
166 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
170 c
= hexchar (c
) * 0x10 + hexchar (d
);
173 if ((i
&group_by
) == group_by
)
179 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
191 if (fread (buf
, 1, 18, fp
) != 18)
198 SET_BINARY (fileno (fp
));
204 register int i
, c
= 0;
206 for (i
=0; i
< 16; ++i
)
208 if ((c
= getc (fp
)) == EOF
)
219 printf ("%08lx: ", address
);
223 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
225 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
230 if ((i
&group_by
) == group_by
)
248 } while (*argv
!= NULL
);
255 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
260 /* hexl.c ends here */