Debugger: Actually execute system commands

This commit is contained in:
Jeffrey Pfau 2014-11-27 11:02:47 -08:00
parent 15ef638f95
commit 55ba676d7c
1 changed files with 33 additions and 20 deletions

View File

@ -8,7 +8,7 @@
#include <pthread.h> #include <pthread.h>
#endif #endif
static const char* ERROR_MISSING_ARGS = "Arguments missing"; static const char* ERROR_MISSING_ARGS = "Arguments missing"; // TODO: share
static struct CLIDebugger* _activeDebugger; static struct CLIDebugger* _activeDebugger;
@ -500,43 +500,56 @@ static void _DVFree(struct CLIDebugVector* dv) {
} }
} }
static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) { static int _tryCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, const char* command, size_t commandLen, const char* args, size_t argsLen) {
const char* firstSpace = strchr(line, ' ');
size_t cmdLength;
struct CLIDebugVector* dv = 0; struct CLIDebugVector* dv = 0;
if (firstSpace) {
cmdLength = firstSpace - line;
} else {
cmdLength = count;
}
int i; int i;
const char* name; const char* name;
for (i = 0; (name = _debuggerCommands[i].name); ++i) { for (i = 0; (name = commands[i].name); ++i) {
if (strlen(name) != cmdLength) { if (strlen(name) != commandLen) {
continue; continue;
} }
if (strncasecmp(name, line, cmdLength) == 0) { if (strncasecmp(name, command, commandLen) == 0) {
if (_debuggerCommands[i].parser) { if (commands[i].parser) {
if (firstSpace) { if (args) {
dv = _debuggerCommands[i].parser(debugger, firstSpace + 1, count - cmdLength - 1); dv = commands[i].parser(debugger, args, argsLen);
if (dv && dv->type == CLIDV_ERROR_TYPE) { if (dv && dv->type == CLIDV_ERROR_TYPE) {
printf("Parse error\n"); printf("Parse error\n");
_DVFree(dv); _DVFree(dv);
return false; return false;
} }
} }
} else if (firstSpace) { } else if (args) {
printf("Wrong number of arguments\n"); printf("Wrong number of arguments\n");
return false; return false;
} }
_debuggerCommands[i].command(debugger, dv); commands[i].command(debugger, dv);
_DVFree(dv); _DVFree(dv);
return true; return true;
} }
} }
_DVFree(dv); return -1;
printf("Command not found\n"); }
static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) {
const char* firstSpace = strchr(line, ' ');
size_t cmdLength;
if (firstSpace) {
cmdLength = firstSpace - line;
} else {
cmdLength = count;
}
const char* args = 0;
if (firstSpace) {
args = firstSpace + 1;
}
int result = _tryCommands(debugger, _debuggerCommands, line, cmdLength, args, count - cmdLength - 1);
if (result < 0 && debugger->system) {
result = _tryCommands(debugger, debugger->system->commands, line, cmdLength, args, count - cmdLength - 1);
}
if (result < 0) {
printf("Command not found\n");
}
return false; return false;
} }