From: Lukas Jiriste Date: Sun, 21 Jul 2024 19:46:24 +0000 (+0200) Subject: Add the rest of execution except for ex_fields X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=3fbffbf23eaac93dbdd7b88c848e52918d68d703;p=42%2Fminishell.git Add the rest of execution except for ex_fields This commit makes the project compilable (though it crashes). It implements the small missing functions in execution.c and integrates it into the project. --- diff --git a/Makefile b/Makefile index fc1e0a8..56d6170 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ INCLUDE := $(addprefix -I, $(INCDIR)) SRCDIR := src SOURCES := main.c \ + env.c \ vars.c \ input_handling.c \ tokenization.c \ diff --git a/inc/minishell.h b/inc/minishell.h index d708866..ebfa49e 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */ -/* Updated: 2024/07/21 17:46:51 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 21:33:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ typedef struct s_redirection { int from_to_fds[2]; size_t created; -} t_redirection; +} t_redirection; typedef struct s_execution_env { @@ -38,16 +38,18 @@ typedef struct s_execution_env typedef t_parse_tree_node t_tree; +int init_env(t_execution_env *env, char **envp); int init_vars(t_vars *vars, char **envp); int add_var_line(t_vec *vec, const char *line); int add_var(t_vec *vec, const char *key, const char *value); void clean_vars(t_vars *vars); +void clean_env(t_execution_env *env); -void handle_input(char **line, t_vars *vars); +void handle_input(char **line, t_execution_env *env); int tokenize(char **line, t_vec *tokens); int parse(t_vec *tokens, t_tree **parse_tree); -int execute(t_tree *parse_tree, t_vars *vars); +int execute(t_tree *parse_tree, t_execution_env *env); int init_tree(t_tree *tree); void free_tree(t_tree *tree); diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..4f7e580 --- /dev/null +++ b/src/env.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */ +/* Updated: 2024/07/21 21:34:58 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include +#include + +int init_env(t_execution_env *env, char **envp) +{ + env->stdin_fd = STDIN_FILENO; + env->stdout_fd = STDOUT_FILENO; + env->ret_val = 0; + if (ft_vec_init(&env->redirections, sizeof(t_redirection))) + return (1); + env->vars = malloc(sizeof(t_vars)); + if (!env->vars) + { + ft_vec_free(&env->redirections, NULL); + return (1); + } + return (init_vars(env->vars, envp)); +} + +void clean_env(t_execution_env *env) +{ + clean_vars(env->vars); + ft_vec_free(&env->redirections, NULL); + return ; +} diff --git a/src/execution.c b/src/execution.c index bbd0f4e..f342bcf 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */ -/* Updated: 2024/07/21 20:31:47 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 21:19:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,6 +90,7 @@ int save_assignments_prefix(t_vec *assignments, t_parse_tree_node *prefix) int save_assignments(t_vec *assignments, t_parse_tree_node *simple_command) { t_parse_tree_node *subnode; + if (ft_vec_init(assignments, sizeof(char *)) != success) return (1); subnode = ft_vec_access(&simple_command->children, 0); @@ -447,7 +448,30 @@ char **expand(t_parse_tree_node *simple_command, const t_execution_env *env) int assignments_to_env(const t_vec *assignments, t_execution_env *env) { + return (ft_vec_insert_range(&env->vars->other, assignments->vec, assignments->size, 0) == success); +} + +void free_fields(char **fields) +{ + size_t i; + i = 0; + while (fields[i]) + { + free(fields[i]); + ++i; + } + free(fields); + return ; +} + +int ex_fields(char **fields,__attribute__((unused)) const t_vec *assignments,__attribute__((unused)) const t_vec *redirections,__attribute__((unused)) t_execution_env *env) +{ + size_t i; + + while (fields[i]) + ft_printf("%s\n", fields[i++]); + return (0); } int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) @@ -463,26 +487,78 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) fields = expand(simple_command, env); if (!fields || res) { - ft_vec_free(&redirections, void_free_redirection); - ft_vec_free(&assignments, void_free_assignments); + ft_vec_free(&redirections, NULL); + ft_vec_free(&assignments, free); return (1); } if (!*fields) { assignments_to_env(&assignments, env); - ex_redirections(&redirections); - ft_vec_free(&redirections, void_free_redirection); - ft_vec_free(&assignments, void_free_assignments); + ft_vec_free(&redirections, NULL); + ft_vec_free(&assignments, NULL); return (0); } res = ex_fields(fields, &assignments, &redirections, env); - ft_vec_free(&redirections, void_free_redirection); - ft_vec_free(&assignments, void_free_assignments); + ft_vec_free(&redirections, NULL); + ft_vec_free(&assignments, free); free_fields(fields); return (res); } -int subshell(t_parse_tree_node *program, const t_execution_env *env) +t_ft_stat v_strcopy(void *v_dest, const void *v_src) +{ + char **dest; + const char *const *src; + + dest = v_dest; + src = v_src; + if (!*src) + { + *dest = NULL; + return (success); + } + *dest = ft_strdup(*src); + if (!*dest) + return (alloc_fail); + return (success); +} + +t_ft_stat v_copy_redir(void *v_dest, const void *v_src) +{ + t_redirection *dest; + const t_redirection *src; + + dest = v_dest; + src = v_src; + *dest = *src; + return (success); +} + +int copy_env(t_execution_env *copy, const t_execution_env *env) +{ + *copy = *env; + copy->vars = malloc(sizeof(t_vars)); + if (!copy->vars) + return (1); + ft_vec_init(©->vars->exported, sizeof(char *)); + ft_vec_init(©->vars->other, sizeof(char *)); + ft_vec_copy(©->vars->exported, &env->vars->exported, v_strcopy, free); + ft_vec_copy(©->vars->other, &env->vars->other, v_strcopy, free); + ft_vec_copy(©->redirections, &env->redirections, v_copy_redir, NULL); + return (0); +} + +void free_env(t_execution_env *env) +{ + clean_vars(env->vars); + free(env->vars); + ft_vec_free(&env->redirections, NULL); + return ; +} + +int ex_program(t_parse_tree_node *program, t_execution_env *env); + +int subshell(t_parse_tree_node *program, t_execution_env *env) { int res; t_execution_env env_copy; @@ -549,11 +625,11 @@ int ex_program(t_parse_tree_node *program, t_execution_env *env) return (0); } -int execute(t_tree *parse_tree, t_vars *vars) +int execute(t_tree *parse_tree, t_execution_env *env) { - t_execution_env env; + int res; - construct_env(&env, vars); - return (ex_program(parse_tree, &env)); - free_env(&env); + res = ex_program(parse_tree, env); + ft_vec_forget_range(&env->redirections, env->redirections.size, 0); + return (res); } diff --git a/src/input_handling.c b/src/input_handling.c index cfb0caf..ea1f68a 100644 --- a/src/input_handling.c +++ b/src/input_handling.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 09:00:00 by ljiriste #+# #+# */ -/* Updated: 2024/06/23 18:04:27 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 21:20:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include "libft.h" #include -void handle_input(char **input, t_vars *vars) +void handle_input(char **input, t_execution_env *env) { int res; t_vec tokens; @@ -24,8 +24,7 @@ void handle_input(char **input, t_vars *vars) parse_tree = NULL; res = tokenize(input, &tokens); res = res || parse(&tokens, &parse_tree); - res = res || expand(parse_tree, vars); - res = res || execute(parse_tree, vars); + res = res || execute(parse_tree, env); ft_vec_free(&tokens, free_token); ft_parse_tree_free(parse_tree); if (res != 0) diff --git a/src/main.c b/src/main.c index 4d1fc49..f09b0ed 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/06/23 18:55:39 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 21:24:30 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,19 +68,20 @@ extern char **environ; const char **envp = environ; int main(int argc, char **argv) */ + int main(int argc, __attribute__((unused)) char **argv, char **envp) { - char *line; - t_vars vars; + char *line; + t_execution_env env; if (argc > 1) { print_help(); return (1); } - if (init_vars(&vars, envp)) + if (init_env(&env, envp)) { - clean_vars(&vars); + clean_env(&env); return (2); } while (1) @@ -88,11 +89,11 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) line = rl_get_line(); if (!line || !ft_strncmp(line, "exit", 4)) break ; - handle_input(&line, &vars); + handle_input(&line, &env); free(line); } + clean_env(&env); rl_clear_history(); - clean_vars(&vars); free(line); ft_printf("exit\n"); return (0); @@ -124,17 +125,17 @@ char *get_line(void) int main(int argc, __attribute__((unused)) char **argv, char **envp) { - char *line; - t_vars vars; + char *line; + t_execution_env env; if (argc > 1) { print_help(); return (1); } - if (init_vars(&vars, envp)) + if (init_env(&env, envp)) { - clean_vars(&vars); + clean_env(&env); return (2); } while (1) @@ -142,10 +143,10 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) line = get_line(); if (!line || !ft_strncmp(line, "exit", 4)) break ; - handle_input(&line, &vars); + handle_input(&line, &env); free(line); } - clean_vars(&vars); + clean_env(&env); get_next_line(-1); free(line); ft_printf("exit\n");