diff --git a/src/apps/gbdb.c b/src/apps/gbdb.c index de1d397..ecc924a 100644 --- a/src/apps/gbdb.c +++ b/src/apps/gbdb.c @@ -142,7 +142,7 @@ static void mem_dump(char *filename) } strip_newline(token); dump_file = fopen(token, "w"); - if(dump_file < 0) { + if(dump_file == NULL) { printf("Failed to open mem dump file: %d\n", errno); 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); filesize = ftell(prog_file); 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); } @@ -412,12 +415,12 @@ static void breakpoint(char *arg_list) memset(breakpoints, 0, sizeof(breakpoints)); tri_init(&commands); tri_add_string(&commands, "add", set_breakpoint); - tri_add_string(&commands, "set", set_breakpoint); - tri_add_string(&commands, "delete", delete_breakpoint); - tri_add_string(&commands, "remove", delete_breakpoint); - tri_add_string(&commands, "info", display_breakpoints); - tri_add_string(&commands, "display", display_breakpoints); - tri_add_string(&commands, "list", display_breakpoints); + tri_add_string(&commands, "set", set_breakpoint); + tri_add_string(&commands, "delete", delete_breakpoint); + tri_add_string(&commands, "remove", delete_breakpoint); + tri_add_string(&commands, "info", display_breakpoints); + tri_add_string(&commands, "display", display_breakpoints); + tri_add_string(&commands, "list", display_breakpoints); init = true; } @@ -462,11 +465,20 @@ static bool cpu_at_breakpoint(void) 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) { struct tri commands; - char line_buffer[INPUT_MAX_LEN]; - char old_buffer[INPUT_MAX_LEN]; + char line_buffer[INPUT_MAX_LEN]; /* Buffer for input command */ + 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_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, "peek", peek); tri_add_string(&commands, "help", help); + tri_add_string(&commands, "?", help); tri_add_string(&commands, "breakpoint", breakpoint); tri_add_string(&commands, "reset", init); @@ -488,37 +501,74 @@ int main(int argc, char **argv) while (1) { bool ambiguous; - char *remainder; 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(); - fgets(line_buffer, INPUT_MAX_LEN, stdin); + cmd_str = fgets(line_buffer, INPUT_MAX_LEN, stdin); + if (cmd_str == NULL) { + printf("\n"); + return 0; + } + if (line_buffer[0] != '\n') { - cmd_string = line_buffer; - } else { - 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 (ambiguous) { - printf("ambiguous command: '%s'\n", cmd_string); - } else if (cmd == NULL) { - printf("unrecognized command: '%s'\n", cmd_string); - } else { - cmd(remainder); - } - - if (cmd_string == line_buffer) { + 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) { + printf("ambiguous command: '%s'\n", cmd_buffer); + break; + } else if (cmd == NULL) { + printf("unrecognized command: '%s'\n", cmd_buffer); + break; + } else { + cmd(arg_str); + } + + process_str = strchr(process_str, ';'); + if (process_str != NULL) { + process_str++; + } else { + break; + } + } while (1); } return 0;