Bugfix to avoid an endless loop.
[cake.git] / test / dumpmem.c
blob1a0788ea986025ce2898d006e6344fdc499d692b
1 /*
2 Copyright © 1995-2004 The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Memory Dump util functions.
6 Lang: english
7 */
9 #define DEBUG 1
10 #include <aros/debug.h>
12 #include <exec/memory.h>
13 #include <dos/dos.h>
14 #include <dos/exall.h>
15 #include <dos/datetime.h>
16 #include <proto/dos.h>
17 #include <proto/utility.h>
18 #include <utility/tagitem.h>
19 #include <utility/utility.h>
21 #include <proto/alib.h>
22 #include <proto/exec.h>
23 #include <proto/dos.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <memory.h>
30 static const char version[] = "$VER: dumpmem.c 45.0 (10.2.2004)\n";
32 #define ARG_TEMPLATE "ADDRESS/A,SIZE/N/A,SERIAL/S,QUIET/S"
34 enum
36 ARG_ADDRESS,
37 ARG_SIZE,
38 ARG_SERIAL,
39 ARG_QUIET,
40 NOOFARGS
43 /* unsigned int hextoint( char **value ); */
44 void clean_string( char *dirtystring );
46 int main(int argc, char **argv)
48 IPTR args[NOOFARGS] = { (IPTR) NULL, // ARG_ADDRESS
49 (IPTR) NULL, // ARG_SIZE
50 FALSE, // ARG_SERIAL
51 FALSE // ARG_QUIET
54 struct RDArgs *rda;
55 char outpstring[sizeof(unsigned int)*6];
56 unsigned int offset,start_address,dump_size;
57 BOOL line_end;
58 int PROGRAM_ERROR = RETURN_OK;
59 char *ERROR_TEXT,*HELPTXT;
61 HELPTXT =
62 "ADDRESS The start address to dump from (in hex)\n"
63 "SIZE The number of bytes to dump\n"
64 "SERIAL If specified, output will use serial debugging instead of stdout\n"
65 "QUIET Do not display warnings\n";
67 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
68 if (NULL != rda)
70 if (args[ARG_ADDRESS]!=0)
73 if (args[ARG_SIZE]!=0)
75 BOOL serial_out = (BOOL)args[ARG_SERIAL];
76 BOOL quiet_out = (BOOL)args[ARG_QUIET];
77 ULONG *sizptr = (ULONG *)args[ARG_SIZE];
78 char * pEnd;
80 if ( sizptr != NULL ) dump_size = *sizptr;
81 else PROGRAM_ERROR = RETURN_FAIL;
83 start_address = strtoul((CONST_STRPTR) args[ARG_ADDRESS], &pEnd, 16 );
84 dump_size = ( dump_size / sizeof(unsigned int));
86 if ( dump_size <= 0)
88 ERROR_TEXT = "Size must be a positive value\n";
89 PROGRAM_ERROR = RETURN_FAIL;
92 /* a quick warning */
93 if ( ( !quiet_out ) && ( PROGRAM_ERROR != RETURN_FAIL ) )
95 TEXT szInput[256];
96 printf( "\n *Some memory areas should NOT be read\n use this tool carefully!\n would you like to proceed? ");
98 gets ( szInput );
100 printf( "\n" );
102 if ( ( strncmp( szInput, "n", 1) )||( strncmp( szInput, "N", 1) ) )
104 ERROR_TEXT = "User canceled..\n";
105 PROGRAM_ERROR = RETURN_FAIL;
109 if ( PROGRAM_ERROR != RETURN_FAIL )
111 if (serial_out) kprintf("dumpmem - Memory Dump tool.\n© Copyright the AROS Dev Team.\n-----------------------------\n\nDumping From [%p] for %d bytes..\n\n", (void *)start_address, (dump_size * sizeof(unsigned int))); /* use kprintf so it is output on serial.. */
112 else printf("dumpmem - Memory Dump tool.\n© Copyright the AROS Dev Team.\n-----------------------------\n\nDumping From [%p] for %d bytes..\n\n", (void *)start_address, (dump_size * sizeof(unsigned int))); /* use kprintf so it is output on serial.. */
114 for ( offset = 0 ; offset < dump_size ; offset += sizeof(unsigned int) )
116 if ( SetSignal(0L,0L) & SIGBREAKF_CTRL_C )
118 SetIoErr( ERROR_BREAK );
119 PROGRAM_ERROR = RETURN_WARN;
120 break;
123 if ( ( (offset/sizeof(unsigned int) ) % 6) == 0 )
125 if (serial_out) kprintf("0x%8.8X ",start_address+offset);
126 else printf("0x%8.8X ",start_address+offset);
129 if (serial_out) kprintf("%8.8X", (unsigned int)((IPTR *)start_address+offset)[0]); /* use kprintf so it is output on serial.. */
130 else printf("%8.8X", (unsigned int)((IPTR *)start_address+offset)[0]); /* use kprintf so it is output on serial.. */
131 sprintf( (char *) &outpstring + ((((offset/sizeof(unsigned int)) % 6) * 4)) ,"%4.4s", (char *)((IPTR *)start_address+offset)[0]);
133 if ( ((offset/sizeof(unsigned int)) % 6) == 5 )
135 line_end = TRUE;
137 clean_string( outpstring );
139 if (serial_out) kprintf(" '%24.24s' \t\n",outpstring);
140 else printf(" '%24.24s' \t\n",outpstring);
142 else
144 line_end = FALSE;
146 if (serial_out) kprintf(" ");
147 else printf(" ");
152 if (serial_out) kprintf("\n\nDump Complete. ");
153 else printf("\n\nDump Complete. ");
156 else ERROR_TEXT = HELPTXT;
158 else ERROR_TEXT = HELPTXT;
159 FreeArgs(rda);
161 else ERROR_TEXT = HELPTXT;
163 if (ERROR_TEXT) printf( ERROR_TEXT );
164 return PROGRAM_ERROR;
167 /* convert a hex string to an int - deprecated */
169 unsigned int hextoint(char **value)
171 struct CHexMap
173 char chr;
174 int value;
176 const int HexMapL = 16;
177 struct CHexMap HiHexMap[] =
179 {"0", 0}, {"1", 1},
180 {"2", 2}, {"3", 3},
181 {"4", 4}, {"5", 5},
182 {"6", 6}, {"7", 7},
183 {"8", 8}, {"9", 9},
184 {"A", 10}, {"B", 11},
185 {"C", 12}, {"D", 13},
186 {"E", 14}, {"F", 15},
187 {""}
189 struct CHexMap LoHexMap[] =
191 {"0", 0}, {"1", 1},
192 {"2", 2}, {"3", 3},
193 {"4", 4}, {"5", 5},
194 {"6", 6}, {"7", 7},
195 {"8", 8}, {"9", 9},
196 {"a", 10}, {"b", 11},
197 {"c", 12}, {"d", 13},
198 {"e", 14}, {"f", 15},
199 {""}
201 unsigned int result = 0,start=0;
203 printf(" converting %s to an int..\n",value);
205 if ((char *)value[0] == '0' && (((char *)value[1] == 'X')||((char *)value[1] == 'x') ) ) start = 2;
207 BOOL firsttime = TRUE;
209 int vallen;
211 for (vallen = start; vallen < strlen(value) ; vallen++)
213 BOOL found = FALSE;
214 int i;
216 printf(" Parsing $s\n",(char *)value[vallen]);
218 for ( i = 0 ; i < HexMapL; i++)
220 if (( (char *)value[vallen] == HiHexMap[i].chr )||( (char *)value[vallen] == LoHexMap[i].chr ))
222 if (!firsttime) result <<= 4;
223 result |= HiHexMap[i].value;
224 found = TRUE;
225 break;
228 if (!found) break;
229 firsttime = FALSE;
231 return result;
234 /* Replace all non printable characters with a '.' */
236 void clean_string( char *dirtystring )
238 int i;
240 for (i = 0 ; i < (sizeof(unsigned int) * 6) ; i++)
242 if (( (TEXT)((IPTR *)dirtystring)[i] < 32 )||( (TEXT)((IPTR *)dirtystring)[i] > 126 ))
243 (char *)((IPTR *)dirtystring)[i] = ".";