1 | /* |
---|
2 | * print_dump |
---|
3 | * |
---|
4 | * $Id$ |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1994 by Division Incorporated |
---|
7 | * |
---|
8 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
9 | * without any express or implied warranty: |
---|
10 | * permission to use, copy, modify, and distribute this file |
---|
11 | * for any purpose is hereby granted without fee, provided that |
---|
12 | * the above copyright notice and this notice appears in all |
---|
13 | * copies, and that the name of Division Incorporated not be |
---|
14 | * used in advertising or publicity pertaining to distribution |
---|
15 | * of the software without specific, written prior permission. |
---|
16 | * Division Incorporated makes no representations about the |
---|
17 | * suitability of this software for any purpose. |
---|
18 | */ |
---|
19 | |
---|
20 | #define GETOPTARGS "v" |
---|
21 | |
---|
22 | char *USAGE = "\ |
---|
23 | usage: print_dump [ -v ] \n\ |
---|
24 | -v -- verbose\n\ |
---|
25 | Reads HP simulator 'memdump' output of 'print_buffer' structure |
---|
26 | on stdin. Dumps it out in vanilla ASCII. |
---|
27 | "; |
---|
28 | |
---|
29 | #include <stdio.h> |
---|
30 | #include <fcntl.h> |
---|
31 | #include <ctype.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <unistd.h> |
---|
34 | #include <string.h> |
---|
35 | #include <memory.h> |
---|
36 | #include <stdarg.h> |
---|
37 | |
---|
38 | #define Failed(x) (((int) (x)) == -1) |
---|
39 | #define TRUE 1 |
---|
40 | #define FALSE 0 |
---|
41 | #define STREQ(a,b) (strcmp(a,b) == 0) |
---|
42 | #define NUMELEMS(arr) (sizeof(arr) / sizeof(arr[0])) |
---|
43 | |
---|
44 | /* |
---|
45 | * Definitions for unsigned "ints"; especially for use in data structures |
---|
46 | * that will be shared among (potentially) different cpu's (we punt on |
---|
47 | * byte ordering problems tho) |
---|
48 | */ |
---|
49 | |
---|
50 | typedef unsigned char u8; |
---|
51 | typedef unsigned short u16; |
---|
52 | typedef unsigned int u32; |
---|
53 | |
---|
54 | typedef union uval { |
---|
55 | u8 uv_chars[4]; |
---|
56 | u16 uv_words[2]; |
---|
57 | u32 uv_long; |
---|
58 | void *uv_ptr[sizeof(long) / sizeof(void *)]; |
---|
59 | } uval_t; |
---|
60 | |
---|
61 | |
---|
62 | /* |
---|
63 | * vars controlled by command line options |
---|
64 | */ |
---|
65 | |
---|
66 | int verbose = FALSE; /* be verbose */ |
---|
67 | |
---|
68 | extern char *optarg; /* getopt(3) control vars */ |
---|
69 | extern int optind, opterr; |
---|
70 | extern int errno; |
---|
71 | |
---|
72 | char *progname; /* for error() */ |
---|
73 | |
---|
74 | void error(int errn, ...); |
---|
75 | |
---|
76 | #define ERR_ERRNO (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */ |
---|
77 | #define ERR_FATAL (ERR_ERRNO / 2) /* error is fatal; no return */ |
---|
78 | #define ERR_ABORT (ERR_ERRNO / 4) /* error is fatal; abort */ |
---|
79 | #define ERR_MASK (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */ |
---|
80 | |
---|
81 | int process(void); |
---|
82 | void prchar(unsigned int ch); |
---|
83 | |
---|
84 | |
---|
85 | int |
---|
86 | main(int argc, char **argv, char **env) |
---|
87 | { |
---|
88 | register int c; |
---|
89 | int showusage = FALSE; /* usage error? */ |
---|
90 | |
---|
91 | /* |
---|
92 | * figure out invocation leaf-name |
---|
93 | */ |
---|
94 | |
---|
95 | if ((progname = strrchr(argv[0], '/')) == (char *) NULL) |
---|
96 | progname = argv[0]; |
---|
97 | else |
---|
98 | progname++; |
---|
99 | |
---|
100 | argv[0] = progname; /* for getopt err reporting */ |
---|
101 | |
---|
102 | /* |
---|
103 | * Check options and arguments. |
---|
104 | */ |
---|
105 | |
---|
106 | opterr = 0; /* we'll report all errors */ |
---|
107 | while ((c = getopt(argc, argv, GETOPTARGS)) != EOF) |
---|
108 | switch (c) |
---|
109 | { |
---|
110 | case 'v': /* toggle verbose */ |
---|
111 | verbose = ! verbose; |
---|
112 | break; |
---|
113 | |
---|
114 | case '?': |
---|
115 | showusage = TRUE; |
---|
116 | } |
---|
117 | |
---|
118 | if (showusage) |
---|
119 | { |
---|
120 | (void) fprintf(stderr, "%s", USAGE); |
---|
121 | exit(1); |
---|
122 | } |
---|
123 | |
---|
124 | return process(); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /* |
---|
129 | * process(arg) |
---|
130 | * |
---|
131 | * Input looks like this |
---|
132 | * |
---|
133 | * Starting address: 00000001.480035a0 |
---|
134 | * ----------------------------------- |
---|
135 | |
---|
136 | +0000 / 0d0a0d0a 2a2a2a20 53454d20 54455354 202d2d20 4e4f4445 2032202a 2a2a0d0a |
---|
137 | +0020 / 73703a20 30783433 30303030 31300d0a 30783438 30613161 38383a20 676f7420 |
---|
138 | .... |
---|
139 | +0b40 / xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
---|
140 | |
---|
141 | * |
---|
142 | * The 'xxxxxxxxx' shows up if the page has not been allocated. |
---|
143 | */ |
---|
144 | |
---|
145 | int |
---|
146 | process(void) |
---|
147 | { |
---|
148 | uval_t b[8]; |
---|
149 | u32 ignore; |
---|
150 | char *p; |
---|
151 | int i; |
---|
152 | int failed_once; |
---|
153 | |
---|
154 | char line[1024]; |
---|
155 | |
---|
156 | #define PRINT_BUFFER_SIZE (16 * 1024) |
---|
157 | struct { |
---|
158 | int index; |
---|
159 | int size; |
---|
160 | u8 buffer[PRINT_BUFFER_SIZE]; |
---|
161 | } print_buffer; |
---|
162 | |
---|
163 | /* we stuff the data into print_buffer using memcpy() */ |
---|
164 | p = (char *) &print_buffer; |
---|
165 | |
---|
166 | failed_once = 0; |
---|
167 | |
---|
168 | while (gets(line)) |
---|
169 | { |
---|
170 | char *cp; |
---|
171 | |
---|
172 | /* hack; deal with the 'xxxxxxxx' problem noted above */ |
---|
173 | for (cp=line; *cp; cp++) |
---|
174 | if (*cp == 'x') |
---|
175 | *cp = '0'; |
---|
176 | |
---|
177 | if (*line != '+') |
---|
178 | continue; |
---|
179 | if (sscanf(line, "+%x / %x %x %x %x %x %x %x %x\n", |
---|
180 | &ignore, |
---|
181 | &b[0].uv_long, |
---|
182 | &b[1].uv_long, |
---|
183 | &b[2].uv_long, |
---|
184 | &b[3].uv_long, |
---|
185 | &b[4].uv_long, |
---|
186 | &b[5].uv_long, |
---|
187 | &b[6].uv_long, |
---|
188 | &b[7].uv_long) != 9) |
---|
189 | { |
---|
190 | if (failed_once) |
---|
191 | error(ERR_FATAL, "2nd format problem; giving up"); |
---|
192 | error(0, "format problem in line: `%s`", line); |
---|
193 | failed_once = 1; |
---|
194 | } |
---|
195 | |
---|
196 | memcpy((void *) p, (void *) b, sizeof(b)); |
---|
197 | p += sizeof(b); |
---|
198 | } |
---|
199 | |
---|
200 | if (verbose) |
---|
201 | printf("buffer size: %d\n", print_buffer.size); |
---|
202 | |
---|
203 | if (print_buffer.size < 0) |
---|
204 | error(ERR_FATAL, "size is too small"); |
---|
205 | |
---|
206 | if (print_buffer.size != sizeof(print_buffer.buffer)) |
---|
207 | { |
---|
208 | error(ERR_FATAL, "buffer size mismatch, expected %d", |
---|
209 | sizeof(print_buffer.buffer)); |
---|
210 | /* XXX we really should just dynamically allocate the buffer */ |
---|
211 | } |
---|
212 | |
---|
213 | i = print_buffer.index + 1; |
---|
214 | while (i != print_buffer.index) |
---|
215 | { |
---|
216 | unsigned int c; |
---|
217 | c = print_buffer.buffer[i++]; |
---|
218 | if (c && (c != '\r')) |
---|
219 | prchar(c); |
---|
220 | i %= print_buffer.size; |
---|
221 | } |
---|
222 | printf("\n"); |
---|
223 | return 0; |
---|
224 | } |
---|
225 | |
---|
226 | /* de-controlify */ |
---|
227 | char *de_control[] = { |
---|
228 | "^@", "^A", "^B", "^C", "^D", "^E", "^F", "^G", "^H", "^I", "^J", "^K", |
---|
229 | "^L", "^M", "^N", "^O", "^P", "^Q", "^R", "^S", "^T", "^U", "^V", "^W", |
---|
230 | "^X", "^Y", "^Z", "^[", "^\\", "^]", "^~", "^_", |
---|
231 | " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", |
---|
232 | ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", |
---|
233 | "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", |
---|
234 | "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", |
---|
235 | "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", |
---|
236 | "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", |
---|
237 | "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "^?", |
---|
238 | "M-^@", "M-^A", "M-^B", "M-^C", "M-^D", "M-^E", "M-^F", "M-^G", |
---|
239 | "M-^H", "M-^I", "M-^J", "M-^K", "M-^L", "M-^M", "M-^N", "M-^O", |
---|
240 | "M-^P", "M-^Q", "M-^R", "M-^S", "M-^T", "M-^U", "M-^V", "M-^W", |
---|
241 | "M-^X", "M-^Y", "M-^Z", "M-^[", "M-^\\", "M-^]", "M-^~", "M-^_", |
---|
242 | "M- ", "M-!", "M-\"", "M-#", "M-$", "M-%", "M-&", "M-'", |
---|
243 | "M-(", "M-)", "M-*", "M-+", "M-,", "M--", "M-.", "M-/", |
---|
244 | "M-0", "M-1", "M-2", "M-3", "M-4", "M-5", "M-6", "M-7", |
---|
245 | "M-8", "M-9", "M-:", "M-;", "M-<", "M-=", "M->", "M-?", |
---|
246 | "M-@", "M-A", "M-B", "M-C", "M-D", "M-E", "M-F", "M-G", |
---|
247 | "M-H", "M-I", "M-J", "M-K", "M-L", "M-M", "M-N", "M-O", |
---|
248 | "M-P", "M-Q", "M-R", "M-S", "M-T", "M-U", "M-V", "M-W", |
---|
249 | "M-X", "M-Y", "M-Z", "M-[", "M-\\", "M-]", "M-^", "M-_", |
---|
250 | "M-`", "M-a", "M-b", "M-c", "M-d", "M-e", "M-f", "M-g", |
---|
251 | "M-h", "M-i", "M-j", "M-k", "M-l", "M-m", "M-n", "M-o", |
---|
252 | "M-p", "M-q", "M-r", "M-s", "M-t", "M-u", "M-v", "M-w", |
---|
253 | "M-x", "M-y", "M-z", "M-{", "M-|", "M-}", "M-~", "M-^?" |
---|
254 | }; |
---|
255 | |
---|
256 | /* |
---|
257 | * prchar(ch); print ch in a readable format, ie ^X or X or ~^X or DEL, etc. |
---|
258 | */ |
---|
259 | |
---|
260 | void |
---|
261 | prchar(unsigned int ch) |
---|
262 | { |
---|
263 | if (isprint(ch) || isspace(ch)) |
---|
264 | putchar(ch); |
---|
265 | else |
---|
266 | printf("%s", de_control[ch]); |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | /* |
---|
271 | * error(errn, arglist) |
---|
272 | * report an error to stderr using printf(3) conventions. |
---|
273 | * Any output is preceded by '<progname>: ' |
---|
274 | * |
---|
275 | * Uses ERR_EXIT bit to request exit(errn) |
---|
276 | * ERR_ABORT to request abort() |
---|
277 | * ERR_ERRNO to indicate use of errno instead of argument. |
---|
278 | * |
---|
279 | * If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its |
---|
280 | * associated error message is appended to the output. |
---|
281 | */ |
---|
282 | |
---|
283 | /*VARARGS*/ |
---|
284 | |
---|
285 | void |
---|
286 | error(int error_flag, ...) |
---|
287 | { |
---|
288 | va_list arglist; |
---|
289 | register char *format; |
---|
290 | extern char *sys_errlist[]; |
---|
291 | extern int sys_nerr; |
---|
292 | int local_errno; |
---|
293 | |
---|
294 | extern int errno; |
---|
295 | |
---|
296 | (void) fflush(stdout); /* in case stdout/stderr same */ |
---|
297 | |
---|
298 | local_errno = error_flag & ~ERR_MASK; |
---|
299 | if (error_flag & ERR_ERRNO) /* use errno? */ |
---|
300 | local_errno = errno; |
---|
301 | |
---|
302 | va_start(arglist, error_flag); |
---|
303 | format = va_arg(arglist, char *); |
---|
304 | (void) fprintf(stderr, "%s: ", progname); |
---|
305 | (void) vfprintf(stderr, format, arglist); |
---|
306 | va_end(arglist); |
---|
307 | |
---|
308 | if (local_errno) |
---|
309 | if ((local_errno > 0) && (local_errno < sys_nerr)) |
---|
310 | (void) fprintf(stderr, " (%s)\n", sys_errlist[local_errno]); |
---|
311 | else |
---|
312 | (void) fprintf(stderr, " (unknown errno=%d)\n", local_errno); |
---|
313 | else |
---|
314 | (void) fprintf(stderr, "\n"); |
---|
315 | |
---|
316 | (void) fflush(stderr); |
---|
317 | |
---|
318 | if (error_flag & (ERR_FATAL | ERR_ABORT)) |
---|
319 | { |
---|
320 | if (error_flag & ERR_FATAL) |
---|
321 | { |
---|
322 | error(0, local_errno ? "fatal error, exiting" : "exiting"); |
---|
323 | exit(local_errno); |
---|
324 | } |
---|
325 | else |
---|
326 | { |
---|
327 | error(0, "fatal error, aborting"); |
---|
328 | abort(); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|