diff --git a/src/builtins/local.c b/src/builtins/local.c index 46e7a5f..f30404d 100644 --- a/src/builtins/local.c +++ b/src/builtins/local.c @@ -11,6 +11,7 @@ #include #include +#include "builtins_handler.h" #include "common.h" #include "u_mem.h" #include "u_str.h" diff --git a/src/common.h b/src/common.h index 8b7c3de..7959c82 100644 --- a/src/common.h +++ b/src/common.h @@ -7,7 +7,7 @@ #ifndef COMMON_H #define COMMON_H - #include "exec.h" + #include #define PROMPT_HEADER "┌─[" #define IF_PROMPT "if? " @@ -17,5 +17,9 @@ enum { RETURN_FAILURE = 1 }; -void free_everything(exec_ctx_t *exec_ctx); +typedef struct { + uint8_t flags; + char *script_file; + char *cmd; +} opt_t; #endif /* COMMON_H */ diff --git a/src/main.c b/src/main.c index 3bf4d19..e525dab 100644 --- a/src/main.c +++ b/src/main.c @@ -5,12 +5,52 @@ ** _ */ +#include +#include + +#include "common.h" #include "debug.h" #include "shell.h" -int main(int ac __attribute__((unused)), char **av __attribute__((unused)), - char **env) +const char OPT_FLAGS[] = "hc:"; + +static +void print_usages(FILE *file, char *bin) { - U_DEBUG_MSG("Debug mode activated.\n"); - return shell(env); + fprintf(file, "Usage: %s [-c command] [-dixv]" + " [file]\n", bin); +} + +static +bool parse_args(opt_t *opt, int ac, char **av) +{ + for (int o = getopt(ac, av, OPT_FLAGS); o != -1; + o = getopt(ac, av, OPT_FLAGS)) { + switch (o) { + case 'h': + exit((print_usages(stdout, av[0]), RETURN_SUCCESS)); + case 'c': + opt->cmd = optarg; + break; + default: + return print_usages(stderr, av[0]), false; + } + } + 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 }; + + U_DEBUG_MSG("Debug mode activated.\n"); + if (!parse_args(&opt, ac, av)) + return RETURN_FAILURE; + return shell(&opt, env); } diff --git a/src/repl.c b/src/repl.c index 41081e1..5dfcbf5 100644 --- a/src/repl.c +++ b/src/repl.c @@ -45,6 +45,7 @@ void init_shell_repl(exec_ctx_t *exec_ctx) signal(SIGINT, SIG_IGN); exec_ctx->is_running = true; if (isatty(STDIN_FILENO)) { + WRITE_CONST(STDOUT_FILENO, BLINKING_VERTICAL_CURSOR); tcgetattr(STDIN_FILENO, &repl_settings); exec_ctx->saved_term_settings = repl_settings; repl_settings.c_iflag = IXON; diff --git a/src/shell.c b/src/shell.c index 54ef44a..6a6f570 100644 --- a/src/shell.c +++ b/src/shell.c @@ -20,6 +20,7 @@ #include "repl.h" #include "shell.h" #include "u_str.h" +#include "utils.h" __attribute__((unused)) static @@ -119,7 +120,7 @@ bool error_in_init(exec_ctx_t *exec_ctx) return false; } -int shell(char **env_ptr) +int shell(opt_t *opt, char **env_ptr) { alias_t alias = init_alias(); env_t env = parse_env(env_ptr); diff --git a/src/shell.h b/src/shell.h index 9aea593..eea4258 100644 --- a/src/shell.h +++ b/src/shell.h @@ -7,6 +7,7 @@ #ifndef SHELL_H #define SHELL_H + #include "common.h" typedef struct { char **cmd_history; @@ -14,5 +15,5 @@ typedef struct { char *last_chdir; } history_t; -int shell(char **env); +int shell(opt_t *opt, char **env); #endif /* SHELL_H */ diff --git a/src/utils.h b/src/utils.h index e79f370..703c7cb 100644 --- a/src/utils.h +++ b/src/utils.h @@ -9,10 +9,12 @@ #ifndef UTILS_H #define UTILS_H #include "history.h" + #include "exec.h" char *strn_to_ndup(int start, int size, char *str); bool is_a_token(char *str, int index_str); char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy); int len_array(char **array); char *insert_str(const char *base, const char *insert, size_t pos); +void free_everything(exec_ctx_t *exec_ctx); #endif /* UTILS_H */ diff --git a/src/vt100_esc_codes.h b/src/vt100_esc_codes.h index a2860b7..24dd8f3 100644 --- a/src/vt100_esc_codes.h +++ b/src/vt100_esc_codes.h @@ -11,6 +11,9 @@ #define ESC "\033" #define CFMT(n) ESC "[" #n "m" + // special + #define BLINKING_VERTICAL_CURSOR ESC "[5 q" + // move #define MOVE_RIGHT(n) ESC "[" #n "C"