gbdb: add support for multiple commands on single line

This commit is contained in:
2017-11-19 18:25:50 -08:00
parent 23898c72b6
commit 3976d8635e

View File

@@ -142,7 +142,7 @@ static void mem_dump(char *filename)
} }
strip_newline(token); strip_newline(token);
dump_file = fopen(token, "w"); dump_file = fopen(token, "w");
if(dump_file < 0) { if(dump_file == NULL) {
printf("Failed to open mem dump file: %d\n", errno); printf("Failed to open mem dump file: %d\n", errno);
return; return;
} }
@@ -286,7 +286,10 @@ static void load_file_to_buffer(char *filename, uint8_t *buffer, size_t size)
fseek(prog_file, 0, SEEK_END); fseek(prog_file, 0, SEEK_END);
filesize = ftell(prog_file); filesize = ftell(prog_file);
fseek(prog_file, 0, SEEK_SET); fseek(prog_file, 0, SEEK_SET);
fread(buffer, MIN(filesize, size), 1, prog_file);
if (fread(buffer, MIN(filesize, size), 1, prog_file) < 0) {
printf("Failed to read filed: %d\n", errno);
}
fclose(prog_file); fclose(prog_file);
} }
@@ -462,11 +465,20 @@ static bool cpu_at_breakpoint(void)
return breakpoint_is_at_addr(lr35902_get_reg_16(&cpu, LR35902_REG_PC)); return breakpoint_is_at_addr(lr35902_get_reg_16(&cpu, LR35902_REG_PC));
} }
static void process_cmd(char *cmd)
{
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct tri commands; struct tri commands;
char line_buffer[INPUT_MAX_LEN]; char line_buffer[INPUT_MAX_LEN]; /* Buffer for input command */
char old_buffer[INPUT_MAX_LEN]; char old_buffer[INPUT_MAX_LEN]; /* Buffer for previous input */
char cmd_buffer[INPUT_MAX_LEN]; /* Buffer for command parsing */
line_buffer[0] = '\0';
old_buffer[0] = '\0';
tri_init(&commands); tri_init(&commands);
tri_add_string(&commands, "step", step); tri_add_string(&commands, "step", step);
@@ -481,6 +493,7 @@ int main(int argc, char **argv)
tri_add_string(&commands, "dump", mem_dump); tri_add_string(&commands, "dump", mem_dump);
tri_add_string(&commands, "peek", peek); tri_add_string(&commands, "peek", peek);
tri_add_string(&commands, "help", help); tri_add_string(&commands, "help", help);
tri_add_string(&commands, "?", help);
tri_add_string(&commands, "breakpoint", breakpoint); tri_add_string(&commands, "breakpoint", breakpoint);
tri_add_string(&commands, "reset", init); tri_add_string(&commands, "reset", init);
@@ -488,37 +501,74 @@ int main(int argc, char **argv)
while (1) { while (1) {
bool ambiguous; bool ambiguous;
char *remainder;
gbdb_cmd* cmd; gbdb_cmd* cmd;
char *cmd_string;
/* String to use for input (line_buffer or
* old_buffer) */
char *cmd_str;
/* Current point in the line (there may be more than
* one command on a line) */
char *process_str;
/* String containing the arguments following a command */
char *arg_str;
show_prompt(); show_prompt();
fgets(line_buffer, INPUT_MAX_LEN, stdin); cmd_str = fgets(line_buffer, INPUT_MAX_LEN, stdin);
if (line_buffer[0] != '\n') { if (cmd_str == NULL) {
cmd_string = line_buffer; printf("\n");
} else { return 0;
cmd_string = old_buffer;
}
strip_newline(cmd_string);
remainder = strchr(cmd_string, ' ');
if (remainder != NULL) {
remainder[0] = '\0';
remainder++;
} }
cmd = tri_get_string_autocomplete(&commands, cmd_string, &ambiguous); if (line_buffer[0] != '\n') {
cmd_str = line_buffer;
strncpy(old_buffer, line_buffer, INPUT_MAX_LEN);
} else {
cmd_str = old_buffer;
}
process_str = cmd_str;
do {
int i;
while (process_str[0] == ' ') { process_str++; };
for (i = 0;; i++) {
if (process_str[i] == '\0' ||
process_str[i] == ';') {
cmd_buffer[i] = '\0';
break;
} else {
cmd_buffer[i] = process_str[i];
}
}
strip_newline(cmd_buffer);
arg_str = strchr(cmd_buffer, ' ');
if (arg_str != NULL) {
arg_str[0] = '\0';
arg_str++;
}
cmd = tri_get_string_autocomplete(&commands, cmd_buffer, &ambiguous);
if (ambiguous) { if (ambiguous) {
printf("ambiguous command: '%s'\n", cmd_string); printf("ambiguous command: '%s'\n", cmd_buffer);
break;
} else if (cmd == NULL) { } else if (cmd == NULL) {
printf("unrecognized command: '%s'\n", cmd_string); printf("unrecognized command: '%s'\n", cmd_buffer);
break;
} else { } else {
cmd(remainder); cmd(arg_str);
} }
if (cmd_string == line_buffer) { process_str = strchr(process_str, ';');
strncpy(old_buffer, line_buffer, INPUT_MAX_LEN); if (process_str != NULL) {
process_str++;
} else {
break;
} }
} while (1);
} }
return 0; return 0;