From e40feed849fb24aee23e4a19110e642a77c38726 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 13 Oct 2019 18:33:46 -0700 Subject: [PATCH] Cleaned up the command parsing code a bit to be slightly more correct and more compact. Now uses if-else-if chain instead of nested switch statements. This may be very slightly less performant, but probably not noticeable and much cleaner code. --- bb.c | 414 +++++++++++++++++++++++++++-------------------------------- bb.h | 3 +- 2 files changed, 189 insertions(+), 228 deletions(-) diff --git a/bb.c b/bb.c index 2272b0c..2ce3a00 100644 --- a/bb.c +++ b/bb.c @@ -59,7 +59,7 @@ void bb_browse(bb_t *bb, const char *path) while (cmdfile && getdelim(&cmd, &space, '\0', cmdfile) >= 0) { cmdpos = ftell(cmdfile); if (!cmd[0]) continue; - process_cmd(bb, cmd); + run_bbcmd(bb, cmd); if (bb->dirty) { free(cmd); fclose(cmdfile); @@ -121,7 +121,7 @@ void bb_browse(bb_t *bb, const char *path) if (cmdpos != 0) err("Command file still open"); if (is_simple_bbcmd(binding->script)) { - process_cmd(bb, binding->script); + run_bbcmd(bb, binding->script); } else { move_cursor(tty_out, 0, termheight-1); restore_term(&default_termios); @@ -405,6 +405,16 @@ entry_t* load_entry(bb_t *bb, const char *path, int clear_dots) return entry; } +/* + * Return whether a string matches a command + * e.g. matches_cmd("cd:..", "cd") == 1, matches_cmd("q", "quit") == 1 + */ +static inline int matches_cmd(const char *str, const char *cmd) +{ + while (*str == *cmd && *cmd) ++str, ++cmd; + return *str == '\0' || *str == ':'; +} + /* * Memory allocation failures are unrecoverable in bb, so this wrapper just * prints an error message and exits if that happens. @@ -565,249 +575,199 @@ void print_bindings(int fd) * Run a bb internal command (e.g. "+refresh") and return an indicator of what * needs to happen next. */ -bb_result_t process_cmd(bb_t *bb, const char *cmd) +bb_result_t run_bbcmd(bb_t *bb, const char *cmd) { if (cmd[0] == '+') ++cmd; else if (strncmp(cmd, "bb +", 4) == 0) cmd = &cmd[4]; const char *value = strchr(cmd, ':'); if (value) ++value; #define set_bool(target) do { if (!value) { target = !target; } else { target = value[0] == '1'; } } while (0) - switch (cmd[0]) { - case '.': { // +..:, +.: - if (cmd[1] == '.') // +..: - set_bool(bb->show_dotdot); - else // +.: - set_bool(bb->show_dot); - populate_files(bb, 1); - return BB_OK; - } - case 'b': { // +bind::