From b3f74c4179610f4594e12f8c75d74c48a8f7d68b Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Wed, 1 Dec 2021 17:24:32 +0100 Subject: [PATCH] refactoring exec --- Makefile | 2 +- headers/minishell_prototypes.h | 5 ++++- headers/minishell_structs.h | 4 ++-- srcs/error_wrappers.c | 26 ++++++++++++++++++++++++++ srcs/exec/exec_cmd_line.c | 10 +++++++--- srcs/exec/find_access.c | 24 +++++++++++++----------- srcs/exec/pipeline.c | 6 ++---- srcs/exec/simple_cmd_builtin.c | 4 ++-- srcs/exec/subshell_exec.c | 6 +++--- 9 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 srcs/error_wrappers.c diff --git a/Makefile b/Makefile index 61b8c34..e596344 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ LIBS = -L $(LIBFT_D) -lft \ LIBFT_D = ./libft LIBFT = $(LIBFT_D)/libft.a -SRCS = main.c init.c free.c generic.c \ +SRCS = main.c init.c free.c generic.c error_wrappers.c \ signals.c \ shell_loop.c shell_script.c \ lexing.c fill_token.c check_operators.c \ diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 245a32f..ad4c5b3 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/01 14:42:38 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 17:17:49 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,6 +70,9 @@ void free_pipeline(t_cmd **pipeline_ptr[]); void close_pipeline_fd(t_cmd *pipeline[]); typedef void (*t_free_f)(void *); // generic +// Error wrappers +void shell_error(char *s1, char *s2); + // Generic char *ft_strjoinfree(char *s1, char *s2); char *ft_strjoinfree_s1(char *s1, const char *s2); diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index b556125..18f55ee 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/11/29 12:25:34 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ typedef struct s_cmd { char **argv; char *path; - t_builtin_f builtin_func; + t_builtin_f builtin_f; int fd_in; int fd_out; pid_t pid; diff --git a/srcs/error_wrappers.c b/srcs/error_wrappers.c new file mode 100644 index 0000000..df1d6f2 --- /dev/null +++ b/srcs/error_wrappers.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_wrappers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */ +/* Updated: 2021/12/01 17:19:02 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void shell_error(char *s1, char *s2) +{ + char *prefix; + + prefix = "minishell: "; + write(STDERR_FILENO, prefix, ft_strlen(prefix)); + if (s1) + write(STDERR_FILENO, s1, ft_strlen(s1)); + if (s2) + write(STDERR_FILENO, s2, ft_strlen(s2)); + write(STDERR_FILENO, "\n", 1); +} diff --git a/srcs/exec/exec_cmd_line.c b/srcs/exec/exec_cmd_line.c index 1b1fbe6..3becd61 100644 --- a/srcs/exec/exec_cmd_line.c +++ b/srcs/exec/exec_cmd_line.c @@ -6,14 +6,12 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/29 12:26:08 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 16:07:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01 - int exec_cmd_line(t_all *c) { if (!pipeline(c)) @@ -23,3 +21,9 @@ int exec_cmd_line(t_all *c) } return (1); } + +/* + 2.9.1 Simple Commands - Command Search and Execution +https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +#tag_18_09_01_01 +*/ diff --git a/srcs/exec/find_access.c b/srcs/exec/find_access.c index cee9fa4..fbe16e5 100644 --- a/srcs/exec/find_access.c +++ b/srcs/exec/find_access.c @@ -6,19 +6,24 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/30 13:37:09 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 17:16:55 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" static int search_cmd_path(char *cmd_name, char **cmd_path, - char *path[]); + char *path[]); static t_builtin_f search_builtin(char *cmd_name); static int handle_access_error(char *file_name); -// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 -// faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...) +/* + 2.8.2 Exit Status for Commands +https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +#tag_18_08_02 +*/ + +// TODO : faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...) int cmd_find_access(t_cmd *cmd, char *path[]) { @@ -29,29 +34,26 @@ int cmd_find_access(t_cmd *cmd, char *path[]) else { cmd->path = ft_strdup(cmd->argv[0]); - if (!cmd->path) + if (!cmd->path) return (0); } } else { - cmd->builtin_func = search_builtin(cmd->argv[0]); - if (cmd->builtin_func) + cmd->builtin_f = search_builtin(cmd->argv[0]); + if (cmd->builtin_f) return (1); if (search_cmd_path(cmd->argv[0], &cmd->path, path) == -1) return (0); if (!cmd->path) { cmd->error = EXIT_CMD_NOT_FOUND; - ft_putstr_fd("minishell: ", 2); - ft_putstr_fd(cmd->argv[0], 2); - ft_putstr_fd(": command not found\n", 2); + shell_error(cmd->argv[0], ": command not found"); } } return (1); } - static int search_cmd_path(char *cmd_name, char **cmd_path, char *path[]) { int i; diff --git a/srcs/exec/pipeline.c b/srcs/exec/pipeline.c index 486a78f..6646d52 100644 --- a/srcs/exec/pipeline.c +++ b/srcs/exec/pipeline.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/29 12:43:52 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int pipeline(t_all *c) return (0); if (!pipeline_find_access(c->pipeline, c->path)) return (0); - if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_func) + if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_f) simple_command_builtin(c->pipeline[0], c); else wait_subshell(pipeline_exec(c->pipeline, c)); @@ -69,8 +69,6 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[]) return (1); } -// TODO : Change exit status as in documentation : -// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c) { int i; diff --git a/srcs/exec/simple_cmd_builtin.c b/srcs/exec/simple_cmd_builtin.c index 8f985d7..a97c95e 100644 --- a/srcs/exec/simple_cmd_builtin.c +++ b/srcs/exec/simple_cmd_builtin.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/27 11:07:19 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ int simple_command_builtin(t_cmd *cmd, t_all *c) if (dup2(cmd->fd_out, STDOUT_FILENO) == -1) return (ft_reti_perror(EXIT_FAILURE, "dup2()")); } - set_last_exit_status(cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c)); + set_last_exit_status(cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c)); if (!restore_stdio(stdin_dup, stdout_dup)) return (EXIT_FAILURE); return (EXIT_SUCCESS); diff --git a/srcs/exec/subshell_exec.c b/srcs/exec/subshell_exec.c index 3b660e3..273f9bf 100644 --- a/srcs/exec/subshell_exec.c +++ b/srcs/exec/subshell_exec.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/29 12:43:55 by lperrey ### ########.fr */ +/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,8 +28,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c) if (dup2(cmd->fd_out, STDOUT_FILENO) == -1) return (ft_reti_perror(EXIT_FAILURE, "dup2()")); close_pipeline_fd(c->pipeline); - if (cmd->builtin_func) - exit_free(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c)); + if (cmd->builtin_f) + exit_free(c, cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c)); else if (execve(cmd->path, cmd->argv, environ) == -1) return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0])); }