From d70952b7bede37b054fdaa43d9e74a20e5fc0f69 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Wed, 21 Feb 2024 15:55:45 +0100 Subject: [PATCH] migrating to msh struct, wip --- srcs/exec.c | 25 +++++++++++++++++++------ srcs/main.c | 40 ++++++++++++++++++++++------------------ srcs/minishell.h | 11 +++++++++-- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/srcs/exec.c b/srcs/exec.c index e1ab712..bc295e8 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/02/21 12:50:01 by marde-vr ### ########.fr */ +/* Updated: 2024/02/21 15:54:56 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,10 +37,23 @@ int exec_builtin(t_cmd *parsed_cmd, t_env **env, t_alias **aliases) return (STDOUT_FILENO); } -void ft_exit(t_cmd *parsed_cmd, t_env *env, int exit_code) +void free_msh(t_msh *msh) { - free_cmd(parsed_cmd); - free_env(env); + if (msh) + { + if (msh->cmds) + free_cmd(msh->cmds); + if (msh->env) + free_env(msh->env); + if (msh->aliases) + free_alias(msh->aliases); + free(msh); + } +} + +void ft_exit(t_msh *msh, int exit_code) +{ + free_msh(msh); exit(exit_code); } @@ -85,7 +98,7 @@ char **split_paths_from_env(t_env *env) return (ft_split(cur_env_var->value, ':')); } -void find_cmd_path(t_cmd *cmd, t_env *env, char **paths, int *found) +void find_cmd_path(t_msh *msh, char **paths, int *found) { char *tmp; char *path; @@ -97,7 +110,7 @@ void find_cmd_path(t_cmd *cmd, t_env *env, char **paths, int *found) path = paths[i]; tmp = ft_strjoin(path, "/"); if (!tmp) - ft_exit(cmd, env, 1); + ft_exit(msh, 1); path = ft_strjoin(tmp, cmd->token); if (!path) ft_exit(cmd, env, 1); diff --git a/srcs/main.c b/srcs/main.c index 3a12c9a..04eaacf 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/02/21 12:56:33 by marde-vr ### ########.fr */ +/* Updated: 2024/02/21 15:53:24 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,40 +68,44 @@ t_env *get_env(char **envp) return (env); } -int main(int argc, char **argv, char **envp) +int init_minishell(t_msh **msh, int argc, char **envp) +{ + *msh = ft_calloc(1, sizeof(t_msh)); + if (!*msh) + // + (void)argc; + (*msh)->env = get_env(envp); + (*msh)->aliases = 0; + return (0); +} + +int main(int argc, char **envp) { char *command; t_cmd *parsed_cmd; - t_env *env; - t_alias *aliases; char *prompt; - + t_msh *msh; + command = (char *)1; - (void)argc; - (void)argv; - env = get_env(envp); - aliases = 0; - aliases = alias_add_back(0, ft_strdup("test"), ft_strdup("echo test")); - // debug + init_minishell(&msh, argc, envp); handle_minishellrc(&env, &aliases); - while (env && command) + while (msh->env && command) { - prompt = get_prompt(env); + prompt = get_prompt(msh->env); if (!prompt) exit(STDIN_FILENO); command = readline(prompt); free(prompt); add_history(command); - parsed_cmd = parse_command(command, env); + msh->cmd = parse_command(command, msh->env); //print_parsed_cmd(parsed_cmd);//debug - parsed_cmd = handle_alias(parsed_cmd, env, aliases); + msh->cmd = handle_alias(msh->cmd, msh->env, msh->aliases); free(command); //print_parsed_cmd(parsed_cmd);//debug - exec_command(parsed_cmd, &env, &aliases); + exec_command(msh->cmd, &env, &aliases); free_cmd(parsed_cmd); } rl_clear_history(); - free_env(env); - free_alias(aliases); + free(msh); return (g_return_code); } diff --git a/srcs/minishell.h b/srcs/minishell.h index bf7fbd5..f95a0f9 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/02/21 13:09:11 by marde-vr ### ########.fr */ +/* Updated: 2024/02/21 15:46:50 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,13 @@ typedef struct s_alias struct s_alias *next; } t_alias; +typedef struct s_msh +{ + struct s_alias *aliases; + struct s_env *env; + struct s_cmd *cmds; +} t_msh; + extern int g_return_code; t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type); @@ -70,7 +77,7 @@ char *ft_get_env(t_env *env, char *var_name); int pwd(void); int is_cmd_char(char c); void print_parsed_cmd(t_cmd *cmd);//debug -void ft_exit(t_cmd *cmd, t_env *env, int error_code); +void ft_exit(t_msh *msh, int error_code); char **env_to_char_tab(t_env *env); void handle_minishellrc(t_env **env, t_alias **aliases); t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias);