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