diff --git a/README.md b/README.md index 3de8fae..83fe8fe 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ - [x] `||`/`&&` - [x] script with shebangs - [x] `-c` eval argument -- [ ] `-e` exit on failure +- [x] `-e` exit on failure - [x] `-h` help (open man?) - [x] globbing - [x] var interpreter diff --git a/src/common.h b/src/common.h index 7959c82..8a454ac 100644 --- a/src/common.h +++ b/src/common.h @@ -21,5 +21,6 @@ typedef struct { uint8_t flags; char *script_file; char *cmd; + bool exit_failure; } opt_t; #endif /* COMMON_H */ diff --git a/src/main.c b/src/main.c index 7117864..6c60acb 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ #include "debug.h" #include "shell.h" -const char OPT_FLAGS[] = "hc:"; +const char OPT_FLAGS[] = "hec:"; static void print_usages(FILE *file, char *bin) @@ -34,6 +34,9 @@ bool parse_args(opt_t *opt, int ac, char **av) case 'c': opt->cmd = strdup(optarg); break; + case 'e': + opt->exit_failure = true; + break; default: return print_usages(stderr, av[0]), false; } @@ -41,15 +44,14 @@ bool parse_args(opt_t *opt, int ac, char **av) if (optind < ac) { opt->script_file = av[optind]; U_DEBUG("Script file [%s]\n", opt->script_file); - } else { - U_DEBUG_MSG("No script file provided.\n"); } return true; } int main(int ac, char **av, char **env) { - opt_t opt = { 0, .script_file = nullptr, .cmd = nullptr }; + opt_t opt = { 0, .script_file = nullptr, .cmd = nullptr, + .exit_failure = false }; int result; U_DEBUG_MSG("Debug mode altivated.\n"); diff --git a/src/shell.c b/src/shell.c index d486d99..eb8ff2c 100644 --- a/src/shell.c +++ b/src/shell.c @@ -61,7 +61,6 @@ bool change_shell_command(buff_t *buff, exec_ctx_t *exec_ctx) buff->sz = 0; if (!readline(exec_ctx, buff)) return false; - U_DEBUG("BUFF SZ [%lu]\n", buff->sz); if (!buff->sz) return false; tmp_buff = buff->str; @@ -93,6 +92,8 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) write_prompt(is_a_tty, exec_ctx); if (!change_shell_command(&buff, exec_ctx)) return free(buff.str), exec_ctx->history->last_exit_code; + if (exec_ctx->opt->exit_failure && exec_ctx->history->last_exit_code) + return free(buff.str), exec_ctx->history->last_exit_code; } return free(buff.str), exec_ctx->history->last_exit_code; }