source: rtems/c/src/lib/libbsp/hppa1.1/simhppa/tools/print_dump.c @ 6fd3979

4.104.114.84.95
Last change on this file since 6fd3979 was 98e4ebf5, checked in by Joel Sherrill <joel.sherrill@…>, on 10/08/97 at 15:45:54

Fixed typo in the pointer to the license terms.

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