migrating to msh struct, wip

This commit is contained in:
mdev9
2024-02-21 15:55:45 +01:00
parent 3d540f0d6d
commit d70952b7be
3 changed files with 50 additions and 26 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);