From cabca14eecf67caf1d5d5567ccdd731838248643 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 26 Aug 2024 09:44:55 +0200 Subject: [PATCH] Move struct managing declarations to new file Also move var managing functions from execution.c to vars.c. --- inc/execution.h | 11 +--- inc/minishell.h | 47 +---------------- inc/minishell_structs.h | 67 ++++++++++++++++++++++++ src/env.c | 4 +- src/execution.c | 108 ++------------------------------------ src/helpers.c | 10 +++- src/input_handling.c | 3 +- src/main.c | 3 +- src/vars.c | 113 +++++++++++++++++++++++++++++++++++++--- src/wildcards.c | 3 +- 10 files changed, 194 insertions(+), 175 deletions(-) create mode 100644 inc/minishell_structs.h diff --git a/inc/execution.h b/inc/execution.h index 9e6a39a..5e4d6a5 100644 --- a/inc/execution.h +++ b/inc/execution.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 17:40:19 by ljiriste #+# #+# */ -/* Updated: 2024/08/23 18:01:03 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 11:41:25 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,15 +15,6 @@ #include "minishell.h" -const char *get_env_var_value(const t_execution_env *env, - const char *var_name); -int set_env_var_value(t_execution_env *env, const char *var_name, - const char *new_value); -char *get_var_name(const char *line); -ssize_t get_var_index(const t_vec *vars, const char *var_name); -const char *get_var_value(const t_vec *vars, const char *var_name); -int set_var_value(t_vec *vars, const char *name, const char *value); - int cd(int argc, char **argv, t_execution_env *env); int echo(int argc, char **argv); int pwd(void); diff --git a/inc/minishell.h b/inc/minishell.h index 6f30ad7..368990c 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,52 +6,16 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */ -/* Updated: 2024/08/23 16:15:16 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:23:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# include "minishell_structs.h" # include "libft.h" -typedef struct s_vars -{ - t_vec exported; - t_vec other; -} t_vars; - -typedef struct s_redirection -{ - int from_to_fds[2]; -} t_redirection; - -typedef struct s_execution_env -{ - int stdin_fd; - int stdout_fd; - int ret_val; - t_vars *vars; -} t_execution_env; - -typedef struct s_wildcard_info -{ - const char *to_expand; - const char *current_expand_char; - const char *entry; - const char *current_entry_char; - const char *path; -} t_wildcard_info; - -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_execution_env *env); void unquote_field(char *field); @@ -64,13 +28,6 @@ int tokenize(char **line, t_vec *tokens); int parse(t_vec *tokens, t_tree **parse_tree); int execute(t_tree *parse_tree, t_execution_env *env); -int init_tree(t_tree *tree); -void free_tree(t_tree *tree); -void free_token(void *token); -void free_str(void *str); - -const char *get_env_var_value(const t_execution_env *env, const char *var_name); char **quoted_split(const char *str); - #endif // MINISHELL_H diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h new file mode 100644 index 0000000..eb44ef1 --- /dev/null +++ b/inc/minishell_structs.h @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* minishell_structs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */ +/* Updated: 2024/08/26 09:31:09 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_STRUCTS_H +# define MINISHELL_STRUCTS_H + +#include "libft.h" + +typedef struct s_vars +{ + t_vec exported; + t_vec other; +} t_vars; + +typedef struct s_redirection +{ + int from_to_fds[2]; +} t_redirection; + +typedef struct s_execution_env +{ + int stdin_fd; + int stdout_fd; + int ret_val; + t_vars *vars; +} t_execution_env; + +typedef struct s_wildcard_info +{ + const char *to_expand; + const char *current_expand_char; + const char *entry; + const char *current_entry_char; + const char *path; +} t_wildcard_info; + +typedef t_parse_tree_node t_tree; + +int init_vars(t_vars *vars, char **envp); +int init_env(t_execution_env *env, char **envp); +int init_tree(t_tree *tree); + +int cmp_var_name(const char *a, const char *b); +int add_var_line(t_vec *vec, const char *line); +int add_var(t_vec *vec, const char *key, const char *value); +int set_var_value(t_vec *vars, const char *name, const char *value); +int set_env_var_value(t_execution_env *env, const char *name, const char *value); +ssize_t get_var_index(const t_vec *vars, const char *var_name); +const char *get_var_value(const t_vec *vars, const char *var_name); +const char *get_env_var_value(const t_execution_env *env, const char *var_name); +char *get_var_name(const char *line); + +void free_str(void *str); +void free_token(void *token); +void clean_vars(t_vars *vars); +void clean_env(t_execution_env *env); + +#endif // MINISHELL_STRUCTS diff --git a/src/env.c b/src/env.c index 236cfdf..ad5dca6 100644 --- a/src/env.c +++ b/src/env.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 10:46:15 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:19:22 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "minishell_structs.h" #include #include diff --git a/src/execution.c b/src/execution.c index a433096..3a8efda 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,12 +6,13 @@ /* By: lnikolov #include @@ -22,19 +23,6 @@ #include #include -int cmp_var_name(const char *a, const char *b) -{ - int namelen; - char *eq_ptr; - - eq_ptr = ft_strchr(a, '='); - namelen = eq_ptr - a; - eq_ptr = ft_strchr(b, '='); - if (namelen != eq_ptr - b) - return (1); - return (ft_strncmp(a, b, namelen)); -} - int is_token_type(const t_parse_tree_node *node, const char *type) { return (!ft_strcmp(node->token.type, type)); @@ -163,63 +151,6 @@ int add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd return (ft_vec_append(redirections, &redir) != success); } -char *get_var_name(const char *line) -{ - size_t i; - - i = 0; - while (ft_isalnum(line[i]) || line[i] == '_') - ++i; - return (ft_strndup(line, i)); -} - -ssize_t get_var_index(const t_vec *vars, const char *var_name) -{ - size_t len; - ssize_t i; - const char *const *line; - - len = ft_strlen(var_name); - i = 0; - while ((size_t)i < vars->size) - { - line = ft_vec_caccess(vars, i); - if (!*line) - break ; - if (!ft_strncmp(*line, var_name, len) && (*line)[len] == '=') - return (i); - ++i; - } - return (-1); -} - -const char *get_var_value(const t_vec *vars, const char *var_name) -{ - size_t len; - ssize_t i; - const char *const *line; - - len = ft_strlen(var_name); - i = get_var_index(vars, var_name); - if (i >= 0) - { - line = ft_vec_caccess(vars, i); - return (*line + len + 1); - } - return (NULL); -} - -const char *get_env_var_value(const t_execution_env *env, const char *var_name) -{ - const char *res; - - res = get_var_value(&env->vars->other, var_name); - if (res) - return (res); - res = get_var_value(&env->vars->exported, var_name); - return (res); -} - int write_line_to_pipe(int pipe_fd, const char *line, const t_execution_env *env, int expand) { size_t i; @@ -620,39 +551,6 @@ char **expand(t_parse_tree_node *simple_command, const t_execution_env *env) return (fields); } -int set_var_value(t_vec *vars, const char *name, const char *value) -{ - ssize_t i; - char *new_entry; - - i = get_var_index(vars, name); - new_entry = malloc(ft_strlen(name) + 1 + ft_strlen(value) + 1); - if (!new_entry) - return (1); - ft_memcpy(new_entry, name, ft_strlen(name)); - new_entry[ft_strlen(name)] = '='; - ft_memcpy(new_entry + ft_strlen(name) + 1, value, ft_strlen(value) + 1); - if (add_var_line(vars, new_entry)) - { - free(new_entry); - return (1); - } - if (i >= 0) - ft_vec_erase(vars, i, free_str); - free(new_entry); - return (0); -} - -int set_env_var_value(t_execution_env *env, const char *name, const char *value) -{ - if (get_var_value(&env->vars->other, name)) - return (set_var_value(&env->vars->other, name, value)); - else if (get_var_value(&env->vars->exported, name)) - return (set_var_value(&env->vars->exported, name, value)); - else - return (set_var_value(&env->vars->other, name, value)); -} - 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); diff --git a/src/helpers.c b/src/helpers.c index 3f193e1..02dec68 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 16:35:16 by ljiriste #+# #+# */ -/* Updated: 2024/06/21 16:40:21 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:20:18 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "minishell_structs.h" #include void free_token(void *token) @@ -18,3 +18,9 @@ void free_token(void *token) free(((t_token *)token)->str); return ; } + +void free_str(void *str) +{ + free(*(char **)str); + return ; +} diff --git a/src/input_handling.c b/src/input_handling.c index 9e98b55..d892870 100644 --- a/src/input_handling.c +++ b/src/input_handling.c @@ -6,10 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 09:00:00 by ljiriste #+# #+# */ -/* Updated: 2024/08/08 14:26:59 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:43:17 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "minishell_structs.h" #include "minishell.h" #include "libft.h" #include diff --git a/src/main.c b/src/main.c index d990bff..75762e7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,10 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 18:50:53 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:41:50 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "minishell_structs.h" #include "minishell.h" #include "libft.h" #include diff --git a/src/vars.c b/src/vars.c index 3a11e63..bebd8bf 100644 --- a/src/vars.c +++ b/src/vars.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 18:34:58 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:20:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "minishell_structs.h" #include "libft.h" #include @@ -71,15 +71,112 @@ int init_vars(t_vars *vars, char **envp) return (0); } -void free_str(void *str) -{ - free(*(char **)str); - return ; -} - void clean_vars(t_vars *vars) { ft_vec_free(&vars->exported, free_str); ft_vec_free(&vars->other, free_str); return ; } + +int cmp_var_name(const char *a, const char *b) +{ + int namelen; + char *eq_ptr; + + eq_ptr = ft_strchr(a, '='); + namelen = eq_ptr - a; + eq_ptr = ft_strchr(b, '='); + if (namelen != eq_ptr - b) + return (1); + return (ft_strncmp(a, b, namelen)); +} + +int set_var_value(t_vec *vars, const char *name, const char *value) +{ + ssize_t i; + char *new_entry; + + i = get_var_index(vars, name); + new_entry = malloc(ft_strlen(name) + 1 + ft_strlen(value) + 1); + if (!new_entry) + return (1); + ft_memcpy(new_entry, name, ft_strlen(name)); + new_entry[ft_strlen(name)] = '='; + ft_memcpy(new_entry + ft_strlen(name) + 1, value, ft_strlen(value) + 1); + if (add_var_line(vars, new_entry)) + { + free(new_entry); + return (1); + } + if (i >= 0) + ft_vec_erase(vars, i, free_str); + free(new_entry); + return (0); +} + +int set_env_var_value(t_execution_env *env, const char *name, const char *value) +{ + if (get_var_value(&env->vars->other, name)) + return (set_var_value(&env->vars->other, name, value)); + else if (get_var_value(&env->vars->exported, name)) + return (set_var_value(&env->vars->exported, name, value)); + else + return (set_var_value(&env->vars->other, name, value)); +} + +ssize_t get_var_index(const t_vec *vars, const char *var_name) +{ + size_t len; + ssize_t i; + const char *const *line; + + len = ft_strlen(var_name); + i = 0; + while ((size_t)i < vars->size) + { + line = ft_vec_caccess(vars, i); + if (!*line) + break ; + if (!ft_strncmp(*line, var_name, len) && (*line)[len] == '=') + return (i); + ++i; + } + return (-1); +} + +const char *get_var_value(const t_vec *vars, const char *var_name) +{ + size_t len; + ssize_t i; + const char *const *line; + + len = ft_strlen(var_name); + i = get_var_index(vars, var_name); + if (i >= 0) + { + line = ft_vec_caccess(vars, i); + return (*line + len + 1); + } + return (NULL); +} + +const char *get_env_var_value(const t_execution_env *env, const char *var_name) +{ + const char *res; + + res = get_var_value(&env->vars->other, var_name); + if (res) + return (res); + res = get_var_value(&env->vars->exported, var_name); + return (res); +} + +char *get_var_name(const char *line) +{ + size_t i; + + i = 0; + while (ft_isalnum(line[i]) || line[i] == '_') + ++i; + return (ft_strndup(line, i)); +} diff --git a/src/wildcards.c b/src/wildcards.c index e386bc7..7159aaf 100644 --- a/src/wildcards.c +++ b/src/wildcards.c @@ -6,10 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */ -/* Updated: 2024/08/23 16:15:58 by ljiriste ### ########.fr */ +/* Updated: 2024/08/26 09:42:45 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "minishell_structs.h" #include "minishell.h" #include "libft.h" #include -- 2.30.2