diff --git a/Makefile b/Makefile index 6c0d754..4ac6af1 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ LIBFT = $(LIBFT_D)/libft.a SRCS = main.c \ shell_loop.c shell_script.c \ - init.c init_prompt.c init_readline.c init_shlvl.c handle_argv.c \ + init.c init_prompt.c init_readline.c init_shlvl.c open_script_file.c \ retrieve_path.c \ free.c \ signals.c error_wrappers.c last_exit_status.c \ diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 7d6c7d5..9958711 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/12/20 18:06:36 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 21:51:07 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,13 @@ extern char **environ; // Init int init(t_all *c, char *argv[]); -char *init_prompt(char *prompt_base); +char *update_prompt(char *prompt_base); char **retrieve_path(void); void set_signals_behaviour(void); // Shell modes void shell_loop(t_all *c); -void shell_script(t_all *c); +void shell_script(t_all *c, int script_fd); // Lexer t_token *lexing(char *input); diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index feaaa7e..ae23d03 100644 --- a/headers/minishell_structs.h +++ b/headers/minishell_structs.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */ -/* Updated: 2021/12/20 17:24:45 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 22:13:45 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ typedef struct s_all char *prompt_base; char *prompt; t_token *token_list; + int script_fd; } t_all; #endif diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index a1d5981..d602d31 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */ -/* Updated: 2021/12/08 22:33:56 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 21:51:00 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ static int change_prompt(t_all *c) { char *tmp; - tmp = init_prompt(c->prompt_base); + tmp = update_prompt(c->prompt_base); if (!tmp) return (-1); free(c->prompt); diff --git a/srcs/init/handle_argv.c b/srcs/init/handle_argv.c deleted file mode 100644 index 59f64b8..0000000 --- a/srcs/init/handle_argv.c +++ /dev/null @@ -1,62 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* handle_argv.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lperrey +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/12/18 04:46:05 by lperrey ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int remap_stdin_to_script_file(char *script_file); -char *init_prompt_base(void); - -int handle_argv(t_all *c, char *argv[]) -{ - int ret; - - if (argv[1]) - { - ret = remap_stdin_to_script_file(argv[1]); - if (ret) - exit_free(c, ret); - } - else if (isatty(STDIN_FILENO)) - { - c->prompt_base = init_prompt_base(); - if (!c->prompt_base) - return (ft_reti_perror(0, "init_prompt_base()")); - c->prompt = init_prompt(c->prompt_base); - if (!c->prompt) - return (ft_reti_perror(0, "init_prompt()")); - } - return (1); -} - -int remap_stdin_to_script_file(char *script_file) -{ - int fd; - int ret; - int tmp; - - fd = open(script_file, O_RDONLY); - if (fd == -1) - { - shell_perror(script_file, ": ", "", 0); - return (EXIT_CMD_NOT_FOUND); - } - ret = dup2(fd, STDIN_FILENO); - tmp = errno; - if (close(fd) == -1) - perror("close()"); - if (ret == -1) - { - errno = tmp; - return (ft_reti_perror(EXIT_FAILURE, "dup2()")); - } - return (0); -} diff --git a/srcs/init/init.c b/srcs/init/init.c index 8410826..99dfc53 100644 --- a/srcs/init/init.c +++ b/srcs/init/init.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/12/18 14:22:20 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 22:26:05 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ void init_readline(void); int init_shlvl(void); -int handle_argv(t_all *c, char *argv[]); +void open_script_file(t_all *c, char *argv[]); +int init_prompt(t_all *c, int script_fd); int init(t_all *c, char *argv[]) { @@ -27,7 +28,8 @@ int init(t_all *c, char *argv[]) c->path = retrieve_path(); if (!init_shlvl()) return (ft_reti_perror(0, "init_shlvl()")); - if (!handle_argv(c, argv)) - return (0); + open_script_file(c, argv); + if (!init_prompt(c, c->script_fd)) + return (ft_reti_perror(0, "init_prompt()")); return (1); } diff --git a/srcs/init/init_prompt.c b/srcs/init/init_prompt.c index 4ff91e1..585b1f7 100644 --- a/srcs/init/init_prompt.c +++ b/srcs/init/init_prompt.c @@ -6,13 +6,29 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/12/18 04:28:46 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 22:25:46 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *init_prompt_base(void) +static char *init_prompt_base(void); + +int init_prompt(t_all *c, int script_fd) +{ + if (!script_fd && isatty(STDIN_FILENO)) + { + c->prompt_base = init_prompt_base(); + if (!c->prompt_base) + return (0); + c->prompt = update_prompt(c->prompt_base); + if (!c->prompt) + return (0); + } + return (1); +} + +static char *init_prompt_base(void) { char *prompt_base; char *tmp; @@ -40,7 +56,7 @@ char *init_prompt_base(void) return (prompt_base); } -char *init_prompt(char *prompt_base) +char *update_prompt(char *prompt_base) { char *prompt; diff --git a/srcs/init/open_script_file.c b/srcs/init/open_script_file.c new file mode 100644 index 0000000..865acf7 --- /dev/null +++ b/srcs/init/open_script_file.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open_script_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/20 22:22:29 by lperrey #+# #+# */ +/* Updated: 2021/12/20 22:27:14 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void open_script_file(t_all *c, char *argv[]) +{ + if (argv[1]) + { + c->script_fd = open(argv[1], O_RDONLY); + if (c->script_fd == -1) + { + shell_perror(argv[1], ": ", "", 0); + exit_free(c, EXIT_CMD_NOT_FOUND); + } + } +} diff --git a/srcs/main.c b/srcs/main.c index ea21cb4..826f837 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/12/20 15:07:31 by hulamy ### ########.fr */ +/* Updated: 2021/12/20 22:26:14 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,10 @@ int main(int argc, char *argv[]) (void)argc; if (!init(&c, argv)) exit_free(&c, EXIT_FAILURE); - if (!isatty(STDIN_FILENO)) - shell_script(&c); + if (c.script_fd) + shell_script(&c, c.script_fd); + else if (!isatty(STDIN_FILENO)) + shell_script(&c, STDIN_FILENO); else shell_loop(&c); return (0); diff --git a/srcs/misc/free.c b/srcs/misc/free.c index 7e743c5..0e0ae5f 100644 --- a/srcs/misc/free.c +++ b/srcs/misc/free.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */ -/* Updated: 2021/12/20 18:06:15 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 22:47:26 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,13 @@ int exit_free(t_all *c, int exit_status) ft_free_2d_arr(environ); ft_free_2d_arr(c->path); free_pipeline(&c->pipeline); - if (!isatty(STDIN_FILENO)) + if (c->script_fd) + { + gnl(c->script_fd, NULL, 1); + if (close(c->script_fd) == -1) + perror("close()"); + } + else if (!isatty(STDIN_FILENO)) gnl(STDIN_FILENO, NULL, 1); else rl_clear_history(); diff --git a/srcs/shell_script.c b/srcs/shell_script.c index bc8e1e6..43090c4 100644 --- a/srcs/shell_script.c +++ b/srcs/shell_script.c @@ -6,16 +6,16 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/26 23:47:44 by lperrey #+# #+# */ -/* Updated: 2021/12/18 15:15:30 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 22:26:57 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char *read_input_script(t_all *c); +static char *read_input_script(t_all *c, int script_fd); static void exit_signal(t_all *c); -void shell_script(t_all *c) +void shell_script(t_all *c, int script_fd) { char *line_input; @@ -23,7 +23,7 @@ void shell_script(t_all *c) while (1) { free(line_input); - line_input = read_input_script(c); + line_input = read_input_script(c, script_fd); if (!line_input) break ; if (line_input && *line_input) @@ -44,7 +44,7 @@ void shell_script(t_all *c) exit_free(c, get_last_exit_status()); } -static char *read_input_script(t_all *c) +static char *read_input_script(t_all *c, int script_fd) { char *line_input; struct sigaction signal_behaviour; @@ -54,7 +54,7 @@ static char *read_input_script(t_all *c) signal_behaviour.sa_handler = SIG_IGN; sigaction(SIGINT, &signal_behaviour, NULL); line_input = NULL; - ret = gnl(STDIN_FILENO, &line_input, 0); + ret = gnl(script_fd, &line_input, 0); if (ret == -1) { free(line_input);