From 3baf91afb32487d7471602b2ca9ae80610830b40 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 27 Nov 2021 12:59:16 +0100 Subject: [PATCH] replaced occurrences of last exit_status + deleted envp comments + WIP Macro exit status + TODO : Invalid free of environ in readline --- headers/minishell.h | 4 ++-- headers/minishell_macro.h | 29 ++++++++++++++++++++++++++++- headers/minishell_prototypes.h | 8 +++++--- headers/minishell_structs.h | 23 +---------------------- srcs/builtins/cd.c | 3 --- srcs/builtins/env.c | 3 +-- srcs/builtins/exit.c | 4 ++-- srcs/builtins/export.c | 4 ++-- srcs/exec/pipeline.c | 6 +++--- srcs/exec/simple_cmd_builtin.c | 4 ++-- srcs/exec/subshell_exec.c | 3 +-- srcs/exec/subshell_wait.c | 8 ++++---- srcs/generic.c | 9 ++++----- srcs/main.c | 3 +-- srcs/parsing/parsing.c | 9 +-------- srcs/shell_loop.c | 4 ++-- 16 files changed, 59 insertions(+), 65 deletions(-) diff --git a/headers/minishell.h b/headers/minishell.h index 51eecc0..c08f78b 100644 --- a/headers/minishell.h +++ b/headers/minishell.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/03 19:14:46 by lperrey #+# #+# */ -/* Updated: 2021/10/26 14:53:32 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 09:09:31 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,8 +38,8 @@ # include -# include "minishell_structs.h" # include "minishell_macro.h" +# include "minishell_structs.h" # include "minishell_term_colors.h" # include "minishell_user_macro.h" # include "minishell_prototypes.h" diff --git a/headers/minishell_macro.h b/headers/minishell_macro.h index 7ff7858..4803253 100644 --- a/headers/minishell_macro.h +++ b/headers/minishell_macro.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:35:55 by lperrey #+# #+# */ -/* Updated: 2021/10/30 22:32:48 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 10:43:43 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,37 @@ # define PROMPT_CHEVRON "> " # define PROMPT_EURO "\001€\002 \001\b\002" +# define EXIT_CMD_NOT_FOUND 127 +# define EXIT_CMD_NOT_EXE 126 +# define EXIT_SIGNAL 128 + +// 1 and 125 inclusive +# define EXIT_REDIRECTION 22 +# define EXIT_EXPANSION 33 + enum e_lexer_return { CONTINUE_TOKEN = 1, DELIMITE_TOKEN }; +enum e_token_id +{ + T_LESS = '<', + T_GREAT = '>', + T_PIPE = '|', + T_DLESS, + T_DGREAT, + T_WORD, + T_REDIRECTION_WORD +}; +// T_DLESS == '<<' +// T_DGREAT == '>>' + +enum e_quotes_state +{ + IN_QUOTES = '\'', + IN_DQUOTES = '\"' +}; + #endif diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 7841ea9..221b14e 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/11/26 21:45:26 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 12:28:20 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,12 @@ // variable globale int g_switch_heredoc_sigint; extern char **environ; +char **g_ori_environ; // WIP free test // Init int init(t_all *c); -char **retrieve_path(char *envp[]); +char *init_prompt(char *prompt_base); +char **retrieve_path(void); int set_terminal_attributes(struct termios *ori_termios, struct termios *interactive_termios, int *termios_changed); // WIP, TEST, TEMP, PLACEHOLDER, NOT IMPORTANT, :) @@ -49,7 +51,7 @@ int exec_cmd_line(t_all *c); int pipeline(t_all *c); int cmd_find_access(t_cmd *cmd, char *path[]); int cmd_exec_in_subshell(t_cmd *cmd, t_all *c); -void wait_subshell(pid_t last_cmd_pid, int *last_exit_status); +void wait_subshell(pid_t last_cmd_pid); int simple_command_builtin(t_cmd *cmd, t_all *c); // Builtins diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index e80ee31..44b1b42 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/26 21:53:39 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 11:28:13 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,6 @@ struct s_all; -enum e_token_id -{ - T_LESS = '<', - T_GREAT = '>', - T_PIPE = '|', - T_DLESS, - T_DGREAT, - T_WORD, - T_REDIRECTION_WORD -}; -// T_DLESS == '<<' -// T_DGREAT == '>>' - -enum e_quotes_state -{ - IN_QUOTES = '\'', - IN_DQUOTES = '\"' -}; - typedef struct s_token { char *content; @@ -57,12 +38,10 @@ typedef struct s_cmd typedef struct s_all { t_cmd **cmd_arr; -// char **envp; char **path; char *prompt_base; char *prompt; t_token *token_list; - int last_exit_status; // struct termios ori_termios; // struct termios interactive_termios; // int termios_changed; diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index 714c1ee..1ceaafa 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -1,9 +1,6 @@ #include "minishell.h" -// a integrer dans header -char *init_prompt(char *prompt_base); - int builtin_cd(int argc, char *argv[], t_all *c) { (void)argc; diff --git a/srcs/builtins/env.c b/srcs/builtins/env.c index c0e4ef9..6642079 100644 --- a/srcs/builtins/env.c +++ b/srcs/builtins/env.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 05:01:26 by lperrey #+# #+# */ -/* Updated: 2021/11/26 21:51:48 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 11:18:55 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ int builtin_env(int argc, char *argv[], t_all *c) // WIP { (void)argc; (void)argv; - //ft_putendl_arr_fd(c->envp, 1); ft_putendl_arr_fd(environ, 1); return (0); } diff --git a/srcs/builtins/exit.c b/srcs/builtins/exit.c index e6f6ca5..2a3e66f 100644 --- a/srcs/builtins/exit.c +++ b/srcs/builtins/exit.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 05:01:22 by lperrey #+# #+# */ -/* Updated: 2021/10/22 14:49:01 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 10:47:32 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int builtin_exit(int argc, char *argv[], t_all *c) // WIP unsigned char status; int i; - status = c->last_exit_status; + status = get_last_exit_status(); if (argc > 2) return (ft_reti_print(1, "exit: too many arguments\n", 2)); if (argc == 2) diff --git a/srcs/builtins/export.c b/srcs/builtins/export.c index e85543d..9657057 100644 --- a/srcs/builtins/export.c +++ b/srcs/builtins/export.c @@ -1,7 +1,7 @@ #include "minishell.h" -int getenv_position(char **envp, char *name) +int getenv_position(char **envp, char *name) // TODO : virer arg envp et utiliser "extern **environ" à la place { int i; @@ -26,7 +26,7 @@ int builtin_export(int argc, char *argv[], t_all *c) environ = ft_resize_2d_arr(environ, 1); environ[env_position] = ft_strdup(argv[1]); if (!ft_strncmp(var[0], "PATH", 4 + 1)) - c->path = retrieve_path(environ); + c->path = retrieve_path(); // free var return (0); } diff --git a/srcs/exec/pipeline.c b/srcs/exec/pipeline.c index 21e9d46..526fe99 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/18 23:08:16 by hulamy ### ########.fr */ +/* Updated: 2021/11/27 10:48:13 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int pipeline(t_all *c) if (ft_2d_arrlen(c->cmd_arr) == 1 && c->cmd_arr[0]->builtin_func) simple_command_builtin(c->cmd_arr[0], c); else - wait_subshell(pipeline_exec(c->cmd_arr, c), &c->last_exit_status); + wait_subshell(pipeline_exec(c->cmd_arr, c)); free_pipeline(&c->cmd_arr); return (1); } @@ -90,6 +90,6 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c) close_pipeline_fd(c->cmd_arr); i -= 1; if (pipeline[i]->error) - c->last_exit_status = pipeline[i]->error; + set_last_exit_status(pipeline[i]->error); return (pipeline[i]->pid); } diff --git a/srcs/exec/simple_cmd_builtin.c b/srcs/exec/simple_cmd_builtin.c index 902dfa8..8f985d7 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/18 14:08:56 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 11:07:19 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()")); } - c->last_exit_status = cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c); + set_last_exit_status(cmd->builtin_func(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 09fb010..635d837 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/26 21:51:15 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 11:11:12 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,6 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c) close_pipeline_fd(c->cmd_arr); if (cmd->builtin_func) free_exit(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c)); - //else if (execve(cmd->path, cmd->argv, c->envp) == -1) else if (execve(cmd->path, cmd->argv, environ) == -1) return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0])); } diff --git a/srcs/exec/subshell_wait.c b/srcs/exec/subshell_wait.c index d19a925..b5ae3b0 100644 --- a/srcs/exec/subshell_wait.c +++ b/srcs/exec/subshell_wait.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/18 23:09:46 by hulamy ### ########.fr */ +/* Updated: 2021/11/27 10:43:46 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ static int handle_wait_error(void); -void wait_subshell(pid_t last_cmd_pid, int *last_exit_status) +void wait_subshell(pid_t last_cmd_pid) { int wstatus; int ret; @@ -25,11 +25,11 @@ void wait_subshell(pid_t last_cmd_pid, int *last_exit_status) if (waitpid(last_cmd_pid, &wstatus, 0) == -1) perror("waitpid()"); if (WIFEXITED(wstatus)) - *last_exit_status = WEXITSTATUS(wstatus); + set_last_exit_status(WEXITSTATUS(wstatus)); if (WIFSIGNALED(wstatus)) { write(STDIN_FILENO, "\n", 1); - *last_exit_status = 128 + WTERMSIG(wstatus); + set_last_exit_status(EXIT_SIGNAL + WTERMSIG(wstatus)); } } ret = 0; diff --git a/srcs/generic.c b/srcs/generic.c index 9375282..d62d0be 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ -/* Updated: 2021/11/14 08:27:40 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 12:58:20 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,7 +84,7 @@ size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen() len++; return (len); } - +/* char **ft_dup_2d_char_arr(char **ptr) // Superflu si ft_dup_2d_arr() fonctionne { unsigned int i; @@ -105,7 +105,7 @@ char **ft_dup_2d_char_arr(char **ptr) // Superflu si ft_dup_2d_arr() fonctionne } new_arr[i] = NULL; return (new_arr); -} +} */ // Test generic. Pas certain que ça fonctionne bien avec le pointeur sur fonction void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)) @@ -114,7 +114,7 @@ void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)) char **arr; char **new_arr; - new_arr = malloc((ft_2d_arrlen(ptr) + 1) * sizeof (void *)); + new_arr = ft_calloc(ft_2d_arrlen(ptr) + 1, sizeof (void *)); if (!new_arr) return (NULL); arr = (char **)ptr; @@ -126,7 +126,6 @@ void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)) return (ft_retp_free(NULL, new_arr, ft_free_2d_arr)); i++; } - new_arr[i] = NULL; return (new_arr); } diff --git a/srcs/main.c b/srcs/main.c index d7d8f15..c37b540 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,13 +6,12 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/11/26 20:33:54 by lperrey ### ########.fr */ +/* Updated: 2021/11/27 11:19:15 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -//int main(int argc, char *argv[], char *envp[]) int main(int argc, char *argv[]) { t_all c; diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 1238543..78f5cb5 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,19 +6,12 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/11/18 22:35:07 by hulamy ### ########.fr */ +/* Updated: 2021/11/27 11:09:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// HUGO WIP -void handle_path(t_cmd **cmd_arr, char **envp); -void find_path(char **argv, char **envp); -int handle_builtin(t_cmd *cmd); -int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *)); -// HUGO WIP - void save_redirections_words(t_token *t) { while (t) diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index d6ec04a..edf4cfc 100644 --- a/srcs/shell_loop.c +++ b/srcs/shell_loop.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/11/17 15:29:35 by hulamy ### ########.fr */ +/* Updated: 2021/11/27 10:47:05 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ void shell_loop(t_all *c) else if (!line_input) { write(1, "exit\n", 5); - free_exit(c, c->last_exit_status); + free_exit(c, get_last_exit_status()); } } }