diff --git a/.gitignore b/.gitignore index 8ebde57..8c3a0f0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ unit_tests test_shell/ func_tests -.vscode \ No newline at end of file +.vscode +scri.sh \ No newline at end of file diff --git a/include/builtin.h b/include/builtin.h index 3d1d673..303ad26 100644 --- a/include/builtin.h +++ b/include/builtin.h @@ -43,8 +43,8 @@ void remove_duplicate_history(env_t *env); int execute_command_history(history_t *old, history_t *new, char **args, env_t *env); //source -char *parse_source_cmd(char *cmd, char **argv, int len_argv); - +void init_source_args(char **argv, int len_argv, env_t *env); +void reset_source_args(int len_argv, env_t *env); //alias int add_alias(alias_t **list, char *alias, char **command); char *concatenate(char **command); diff --git a/src/builtin/builtin_source.c b/src/builtin/builtin_source.c index 6ea5ebc..1ef2ea1 100644 --- a/src/builtin/builtin_source.c +++ b/src/builtin/builtin_source.c @@ -16,27 +16,40 @@ #include "utility.h" #include "shell.h" -static void start_script(char **argv, env_t *env, int len_argv) +char **get_arr_from_fd(int fd, char *filepath, char *str) { - char *str = NULL; - char **arr = NULL; - int fd = open(argv[1], O_RDONLY); - bool should_close = false; struct stat st_buff; + char **arr = NULL; - if (fd == -1) - return; - stat(argv[1], &st_buff); + stat(filepath, &st_buff); str = malloc(st_buff.st_size + 1); if (str) { read(fd, str, st_buff.st_size); arr = split_str(str, '\n'); - if (arr) - for (int i = 0; arr[i] && !should_close; i++) - should_close = (eval_raw_cmd(parse_source_cmd(arr[i], - argv, len_argv), env) < 0); - free(str); } + return (arr); +} + +static void start_script(char **argv, env_t *env, int len_argv) +{ + int fd = open(argv[1], O_RDONLY); + bool should_close = false; + char **arr = NULL; + char *str = NULL; + + if (fd == -1) + return; + arr = get_arr_from_fd(fd, argv[1], str); + if (arr) { + for (int i = 0; arr[i] && !should_close; i++) { + if (arr[i][0] == '#') + continue; + should_close = (eval_raw_cmd(parse_source_cmd(arr[i], + argv, len_argv), env) < 0); + } + } + free(arr); + free(str); close(fd); } diff --git a/src/main.c b/src/main.c index c96729b..0e1b1d2 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ ** main */ +#include "builtin.h" #include "shell.h" #include "key_functions.h" #include @@ -38,9 +39,10 @@ int main(int argc, char **argv, char **env) if (!envt) return (ERROR); - start_shell(envt); - (void)argc; - (void)argv; + if (argc >= 2) + builtin_source(argv, envt); + else + start_shell(envt); ret = get_return(my_getenv(envt->vars, "?")); free_env(envt); return (ret);