1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool prefix_match( const char* arg, const char* pattern )
{
return strstr( pattern, arg ) == pattern;
}
int main()
{
//declare variables
char command_line[256]; //store entire command line
char* arguments[100]; //store arguments from command line
int argcount; //argument count index
int startAddress,endAddress; //start and end address for the dump command
printf("Welcome to the Command Interpreter\nCreated by Michael Herald\n\n");
do
{
//show prompt to user
printf("COMMAND->");
fgets(command_line,256,stdin); //get command from user
argcount = 0; //argument counter index
//split command line into tokens
(arguments[argcount]) = strtok(command_line," \n");
while(arguments[argcount]!=NULL)
{
argcount ++;
arguments[argcount] = strtok(NULL," \n");
}
//select command from list
if(argcount >0 )
{
.
.
.
else if(prefix_match(arguments[0],"dump")==true)
{
if (argcount!=3)
{ //If the user does not enter parameters, then ask for them
switch(argcount)
{
case 1: // Start Address
printf("Enter start address: ");
scanf("%x", &startAddress);
case 2: // End Address
if(argcount==2)
startAddress = strtol(arguments[1],NULL,16);
printf("Enter end address: ");
scanf("%x",&endAddress);
break;
default:
printf("Wrong number of arguments! See help for more info.\n");
continue;
}
}
else
{
startAddress = strtol(arguments[1],NULL,16);
endAddress = strtol(arguments[2],NULL,16);
}
printf("Dumping memory from %x to %x.\n",startAddress,endAddress);
continue;
}
.
.
.
}
}while(1); //main loop
return 0;
}
|