// ************************************************************************* // Demonstrates a command line interpretter for use on small // embedded devices. Assumes use of stdio.h and string.h // Very simple, but must have a version of gets, strtok, sscanf available. // You may not have a real 'gets' or 'printf' function in which case // you have to build one. This is just to give a rough idea how // you might provide a monitor for a simple embedded device. // ************************************************************************* #include #include #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif // Delimiters for strtok. In this case, just a space characater. Can also include // TAB character, etc. // const char Delimiters[] = " "; // *** Functions Prototypes int cmdproc (void); // Specific command handlers int cmd_dump (unsigned long int start, int length); int cmd_dumplog (void); int cmd_test (int testnum); int cmd_help (void); // General purpose functions int getarg (char *tok, int *x); // *** Start of code *** int main () { // Call the command processor. // cmdproc(); } // Main menu loop // // Ficticious commands are: // // help // dump [] // dumplog // test // // Numeric arguments are in decimal unless prefixed with "0x" (see the function getarg). // int cmdproc () { int bDone; // Flag for input loop. char line[200]; // Command line buffer. char *tok; // Pointer for calls to 'strtok'. int rc; // unsigned long arg1, arg2, arg3, arg4; // Temps for potential arguments. printf ("\nCommand line interpretter.\n"); bDone = 0; while (!bDone) { // Display the prompt. May need to replace with system-specific routine. printf ("\n>"); // Get a line of input from user. May need to replace with system-specific routine. gets (line); // Begin parsing. Call strtok for the first time with line buffer. After this, pass // a NULL for the first argument to get the next token on the line. // tok = strtok (line, Delimiters); if (tok) { // HELP // if ( (strcmp(tok, "?") == 0) || (strcmp(tok, "h") == 0) || (strcmp(tok, "help") == 0)) { cmd_help (); } // DUMP // // e.g. dump 0xa000 200 // else if ( (strcmp(tok, "dump") == 0) ) { arg2 = 100; // Default for // Must have at least one argument tok = strtok (NULL, Delimiters); if (tok) { rc = getarg (tok, (int *)&arg1); if (rc == -1) { printf ("\nUnexpected 1st argument: %s", tok); } else { // See if the optional length was specified tok = strtok (NULL, Delimiters); if (tok) { rc = getarg (tok, (int *)&arg2); if (rc == -1) { printf ("\nUnexpected 1st argument: %s", tok); } } } } // Call it. cmd_dump (arg1, (int)arg2); } // DUMPLOG // else if ( (strcmp(tok, "dumplog") == 0) ) { // Call it. cmd_dumplog (); } // TEST // else if ( (strcmp(tok, "test") == 0) ) { // Must have at least one argument tok = strtok (NULL, Delimiters); if (tok) { rc = getarg (tok, (int *)&arg1); if (rc == -1) { printf ("\nUnexpected 1st argument: %s", tok); } } // Call it. cmd_test ((int)arg1); } // QUIT else if ( (strcmp(tok, "quit") == 0) ) { printf ("\nQUITING!\n"); bDone = TRUE; } // Unknown?? else { printf ("\nHuh? Type '?' or 'help' for help"); } } } return 0; } // General routine that extracts the numeric value from the string argument. // At this time, it will only look for decimal or hex. // int getarg (char *tok, int *x) { int nargs; if (!tok) return -1; if (!*tok) return -1; // Let's try scanning token as a hex value // if (strlen(tok) >= 3) { if (tok[0] == '0' && tok[1] == 'x') { // Looks like a hex number nargs = sscanf (tok, "%x", x); if (nargs) { // Scanned OK, return success return 0; } else { // Some problem parsing. Return failure return -1; } } } // Try decimal nargs = sscanf (tok, "%d", x); if (nargs) { // Scanned OK, return success return 0; } else { // Some problem parsing. Return failure return -1; } // Shouldn't get this far. return -1; } // *************************************************************** // // COMMANDS Functions. // // These are not filled in. That's for you to do.. // //**************************************************************** int cmd_dump (unsigned long int start, int length) { printf ("\nDump: start address = 0x%08X, length = %d", start, length); return 0; } int cmd_dumplog () { printf ("\nDumplog:"); return 0; } int cmd_test (int testnum) { printf ("\nTest: test number = %d", testnum); return 0; } int cmd_help () { printf ("\nHelp:"); printf ("\n"); printf ("\n dump [] - Dump memory."); printf ("\n test - Run test number indicated."); printf ("\n quit - Exit this menu."); printf ("\n help - Print this help"); printf ("\n"); return 0; }