From f66cb8f28a2779159baeef6ea91a7684d59cc295 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 1 Apr 2020 23:16:47 +0200 Subject: Changed representation of environment to vector --- Doxyfile | 2 +- include/minishell.h | 7 +++--- libft | 2 +- src/builtin/cd.c | 4 ++-- src/builtin/env.c | 12 ++-------- src/builtin/export.c | 12 +++++++--- src/builtin/unset.c | 14 +++++++++--- src/env.c | 64 ++++++++++++++++++++++++++-------------------------- src/eval/eval.c | 6 ++--- src/main.c | 8 +++---- 10 files changed, 69 insertions(+), 62 deletions(-) diff --git a/Doxyfile b/Doxyfile index 1bd10b9..4fd5a71 100644 --- a/Doxyfile +++ b/Doxyfile @@ -608,7 +608,7 @@ INLINE_INFO = YES # name. If set to NO, the members will appear in declaration order. # The default value is: YES. -SORT_MEMBER_DOCS = YES +SORT_MEMBER_DOCS = NO # If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief # descriptions of file, namespace and class members alphabetically by member diff --git a/include/minishell.h b/include/minishell.h index b883fc2..ac00875 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/04/01 17:54:36 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:13:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ # include "libft_ht.h" # include "libft_lst.h" # include "libft_util.h" +# include "libft_vec.h" /* ** \brief Value of pipe entry if closed @@ -55,7 +56,7 @@ # define BUILTIN_NOT_FOUND -2 typedef t_ftht* t_path; -typedef t_ftht* t_env; +typedef t_ftvec* t_env; /* ** path.c @@ -68,7 +69,7 @@ t_path path_update(t_path path, char *path_var); */ t_env env_from_array(char **envp); -char **env_to_array(t_env env); +char *env_search(t_env env, char *key); /* ** builtin*.c - directory with all builtin commands diff --git a/libft b/libft index 9316f20..1925805 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 9316f2063255bd4a0abd5c38d4c065969a8980bb +Subproject commit 1925805cc760061d5742f9d215998561fcd45211 diff --git a/src/builtin/cd.c b/src/builtin/cd.c index dc88dae..66f1f81 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/04/01 17:10:21 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:15:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int builtin_cd(char **argv, t_env env) path = argv[1]; if (argv[1] == NULL) - path = ft_htget(env, "HOME"); + path = env_search(env, "HOME"); if (path == NULL) return (1); if (chdir(path) == -1) diff --git a/src/builtin/env.c b/src/builtin/env.c index d3f4024..5854828 100644 --- a/src/builtin/env.c +++ b/src/builtin/env.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:32 by charles #+# #+# */ -/* Updated: 2020/04/01 17:10:33 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:25:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,9 @@ #include "minishell.h" -void st_print_env_variable(t_ftht_entry *entry) -{ - ft_putstr(entry->key); - ft_putchar('='); - ft_putstr((char*)entry->value); - ft_putchar('\n'); -} - int builtin_env(char **argv, t_env env) { (void)argv; - ft_htiter(env, st_print_env_variable); + ft_veciter(env, (void (*)(void*))ft_putendl); return (0); } diff --git a/src/builtin/export.c b/src/builtin/export.c index 8a62412..650a421 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/04/01 17:11:38 by charles ### ########.fr */ +/* Updated: 2020/04/01 22:37:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,13 @@ int builtin_export(char **argv, t_env env) { - (void)argv; - (void)env; + char *tmp; + + if (ft_strchr(argv[1], '=') == NULL) + return (1); + if ((tmp = ft_strdup(argv[1])) == NULL) + return (2); + if (ft_vecpush(env, tmp) == NULL) + return (2); // internal error code return (0); } diff --git a/src/builtin/unset.c b/src/builtin/unset.c index 2ae6c27..ffcf60f 100644 --- a/src/builtin/unset.c +++ b/src/builtin/unset.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:51 by charles #+# #+# */ -/* Updated: 2020/04/01 17:10:51 by charles ### ########.fr */ +/* Updated: 2020/04/01 23:05:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,16 @@ int builtin_unset(char **argv, t_env env) { + size_t i; + if (argv[1] == NULL) return (1); - ft_htdelone(env, argv[1], ht_del_str_entry); - return (0); + i = 0; + while (i < env->size) + if (ft_strncmp(env->data[i], argv[1], ft_strlen(argv[1])) == 0) + { + ft_vecremove(env, i, free); + return (0); + } + return (1); } diff --git a/src/env.c b/src/env.c index fc26876..66b4994 100644 --- a/src/env.c +++ b/src/env.c @@ -6,65 +6,65 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/04/01 17:03:50 by charles ### ########.fr */ +/* Updated: 2020/04/01 23:09:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ /* ** \file env.c -** \brief Environment hash table manipulation +** \brief Environment manipulation */ #include "minishell.h" -/* -** \brief Number of buckets of an environment hash table -*/ - -#define MS_ENV_HT_SIZE 2048 - /* ** \brief Convert array of string to environment hash table ** \param envp array of string (each in the format `name=value`) ** \return Environment hash table or NULL on error */ -t_env env_from_array(char **envp) +t_env env_from_array(char **envp) { t_env env; - int i; - char *key; - char *value; + size_t i; - if (envp == NULL) - return (NULL); - if ((env = ft_htnew(MS_ENV_HT_SIZE)) == NULL) + i = 0; + while (envp[i] != NULL) + if (ft_strchr(envp[i++], '=') == NULL) + return (NULL); + if ((env = ft_vecnew(i)) == NULL) return (NULL); - i = -1; - while (envp[++i] != NULL) + env->size = i; + i = 0; + while (envp[i] != NULL) { - // free stuff on error - if ((value = ft_strchr(envp[i], '=')) == NULL) - return (NULL); - if ((key = ft_strndup(envp[i], ft_strcspn(envp[i], "="))) == NULL) + if ((env->data[i] = ft_strdup(envp[i])) == NULL) + { + ft_vecdestroy(env, free); return (NULL); - if ((value = ft_strdup(value + 1)) == NULL) - return (NULL); - if (ft_htset(env, key, value, ht_del_str_entry) == NULL) - return (NULL); - free(key); + } + i++; } return (env); } -/* -** \brief Convert environment to array of string -** \param env Environment hash table -** \return Array of string on NULL on error +/** +** \brief Search a key in environment +** \param env Search environment +** \param key Searched key +** \return Value after '=' in environment variable array or NULL if not found */ -char **env_to_array(t_env env) +char *env_search(t_env env, char *key) { - (void)env; + size_t i; + + i = 0; + while (i < env->size) + { + if (ft_strncmp((char*)env->data[i], key, ft_strlen(key)) == 0) + return (ft_strchr((char*)env->data[i], '=') + 1); + i++; + } return (NULL); } diff --git a/src/eval/eval.c b/src/eval/eval.c index c90618c..8c1e509 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:21 by charles #+# #+# */ -/* Updated: 2020/04/01 17:09:36 by charles ### ########.fr */ +/* Updated: 2020/04/01 23:15:16 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,11 +51,11 @@ static int eval_cmd(t_eval_state *state, t_cmd *cmd) char *exec_path; bool is_builtin; - is_builtin = builtin_check_exec_name(cmd->argv[0]); + is_builtin = builtin_check_exec_name(cmd->argv[0]); // no fork if builtin if (!is_builtin) { if ((exec_path = exec_search_path(state->path, - ft_htget(state->env, "PATH"), cmd->argv[0])) == NULL) + env_search(state->env, "PATH"), cmd->argv[0])) == NULL) return (-1); } pipe_setup_parent(cmd, state->pipe_in, state->pipe_out); diff --git a/src/main.c b/src/main.c index 233333d..c46324f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/02/28 11:50:29 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 23:12:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ int main(int argc, char **argv, char **envp) /* int ret; */ env = env_from_array(envp); - path = path_update(NULL, ft_htget(env, "PATH")); + path = path_update(NULL, env_search(env, "PATH")); t_ast *ast; t_line line; @@ -70,7 +70,7 @@ int main(int argc, char **argv, char **envp) state.pipe_out[1] = -1; state.path = path; state.env = env; - eval(&state, ast); + printf("ret: %d %s\n", eval(&state, ast), strerror(errno)); ast_destroy(ast); /* while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) */ @@ -81,6 +81,6 @@ int main(int argc, char **argv, char **envp) /* } */ /* free(line); */ ft_htdestroy(path, ht_del_str_entry); - ft_htdestroy(env, ht_del_str_entry); + ft_vecdestroy(env, free); return (0); } -- cgit