diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-03 19:00:47 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-03 19:00:47 +0200 |
commit | bfaf4c53a8406ea0bfbd8699807b567a5322a03f (patch) | |
tree | b8ce7d07b3094de0c4ba98160f35cf7e64eeae1d /src | |
parent | Added $ for varname (diff) | |
download | 42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.gz 42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.bz2 42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.xz 42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.tar.zst 42-minishell-bfaf4c53a8406ea0bfbd8699807b567a5322a03f.zip |
Export is working
Diffstat (limited to '')
-rw-r--r-- | src/b_export.c | 6 | ||||
-rw-r--r-- | src/b_export_next.c | 64 | ||||
-rw-r--r-- | src/d_enum.h | 6 | ||||
-rw-r--r-- | src/m_minishell.c | 9 | ||||
-rw-r--r-- | src/s_init.c | 16 | ||||
-rw-r--r-- | src/u_vars.c | 22 | ||||
-rw-r--r-- | src/u_vars.h | 2 |
7 files changed, 94 insertions, 31 deletions
diff --git a/src/b_export.c b/src/b_export.c index 7aeed93..43e1c7d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -31,7 +31,7 @@ static t_bool char *ptr; ptr = (char*)arg; - if (ft_isalpha(ptr[0])) + if (ft_isalpha(ptr[0]) || ptr[0] == '_') { return (TRUE); } @@ -89,7 +89,7 @@ uint8_t const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; - char fmt[255]; + char fmt[4096]; t_bool next; uint8_t r; @@ -112,7 +112,7 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = get_cstm_vr(fmt, msh); + varval = u_get_cstm_vr(fmt, msh); if (varval != NULL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); diff --git a/src/b_export_next.c b/src/b_export_next.c index 683fdd0..0c143b2 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -15,7 +15,9 @@ #include "b_export_next.h" #include "f_fail.h" +#include "s_lvars.h" #include "s_struct.h" +#include "u_vars.h" static char **b_get_var(const char arg[], @@ -40,13 +42,73 @@ static char return (var); } +static int64_t + b_is_it_in_env(const char varname[], + t_msh *msh) +{ + char **p_env; + + p_env = msh->envp; + while (*p_env != NULL) + { + if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0) + { + return (p_env - msh->envp); + } + p_env++; + } + return (-1); +} + +static void + b_add_to_env(const char arg[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + f_fail_alloc_and_destroy(msh); + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + f_fail_alloc_and_destroy(msh); + i++; + } + if (!(nenvp[i] = ft_strdup(arg))) + f_fail_alloc_and_destroy(msh); + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + msh->envp = nenvp; +} + void b_export_with_equals(const char arg[], t_msh *msh) { + char *varval; char **var; + int64_t env_i; var = b_get_var(arg, msh); - ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]); + if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1) + { + ft_memdel((void*)&msh->envp[env_i]); + if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) + f_fail_alloc_and_destroy(msh); + } + else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL) + { + b_add_to_env(arg, msh); + lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1); + } + else + { + b_add_to_env(arg, msh); + } ft_delwords(var); } diff --git a/src/d_enum.h b/src/d_enum.h index ec35ca3..921195f 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -20,12 +20,6 @@ ** 1: alloc err */ -typedef enum -{ - FALSE, - TRUE -} t_bool; - enum { FT_RET_FINE, diff --git a/src/m_minishell.c b/src/m_minishell.c index b4a8876..dd47ac3 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -21,8 +21,6 @@ #include "s_struct.h" #include "s_init.h" #include "s_destroy.h" -#include "u_vars.h" -#include "u_vars_next.h" #include "s_lvars.h" int @@ -31,8 +29,6 @@ int char *const envp[]) { int32_t ret; - char *str_one; - char *str_two; t_msh *msh; /* TODO: handle general variables | $var */ @@ -41,11 +37,6 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } - u_subst_var_value("$SHLVL", - str_one =ft_itoa( - ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh); - ft_memdel((void*)&str_one); - ft_memdel((void*)&str_two); /* TODO: delet this */ ret = m_argv(argc, argv, msh); s_destroy(msh); diff --git a/src/s_init.c b/src/s_init.c index 454cb67..377cd91 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -18,6 +18,8 @@ #include "d_define.h" #include "m_funptr.h" #include "s_init.h" +#include "u_vars.h" +#include "u_vars_next.h" static char **dupenv_del(char **nenvp, @@ -58,6 +60,19 @@ static char return (nenvp); } +static void + inc_shlvl(t_msh *msh) +{ + char *str_one; + char *str_two; + + u_subst_var_value("$SHLVL", + str_one = ft_itoa( + ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh); + ft_memdel((void*)&str_one); + ft_memdel((void*)&str_two); +} + t_msh *init_msh(char *const argv[], char *const envp[]) @@ -81,5 +96,6 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; + inc_shlvl(msh); return (msh); } diff --git a/src/u_vars.c b/src/u_vars.c index f7123ef..913b07b 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -21,7 +21,7 @@ #include "s_struct.h" static char - *set_rva(const char varname[], + *u_set_rva(const char varname[], t_msh *msh) { char *rvarname; @@ -32,7 +32,7 @@ static char s_destroy(msh); f_fail_alloc(msh); } - ft_memcpy((char*)rvarname, (const char*)varname + 1, + (void)ft_memcpy((char*)rvarname, (const char*)varname + 1, ft_strlen(varname + 1)); *(rvarname + ft_strlen(varname + 1)) = '='; *(rvarname + ft_strlen(varname + 1) + 1) = '\0'; @@ -40,7 +40,7 @@ static char } static char - *dup_env(char *p_env, + *u_dup_env(char *p_env, char *rvarname, t_msh *msh) { @@ -58,7 +58,7 @@ static char } static char - *get_frm_env(char rvarname[], + *u_get_frm_env(char rvarname[], t_msh *msh) { char **p_env; @@ -75,7 +75,7 @@ static char pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = dup_env(pp_env, rvarname, msh); + varval = u_dup_env(pp_env, rvarname, msh); return (varval); } p_env += 1; @@ -85,7 +85,7 @@ static char } static char - *get_special_var(const char varname[], + *u_get_special_var(const char varname[], t_msh *msh) { char *varval; @@ -114,7 +114,7 @@ static char } char - *get_cstm_vr(const char varname[], + *u_get_cstm_vr(const char varname[], t_msh *msh) { t_lvars *ptr; @@ -165,18 +165,18 @@ char char *rvarname; varval = NULL; - if ((varval = get_special_var(varname, msh)) != NULL) + if ((varval = u_get_special_var(varname, msh)) != NULL) { return (varval); } - else if ((varval = get_cstm_vr(varname, msh)) != NULL) + else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) { return (varval); } else { - rvarname = set_rva(varname, msh); - varval = get_frm_env(rvarname, msh); + rvarname = u_set_rva(varname, msh); + varval = u_get_frm_env(rvarname, msh); return (varval); } return (NULL); diff --git a/src/u_vars.h b/src/u_vars.h index 8d96050..0d83b4b 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,7 @@ #include "s_struct.h" -char *get_cstm_vr(const char varname[], t_msh *msh); +char *u_get_cstm_vr(const char varname[], t_msh *msh); char *u_get_var_value(const char varname[], t_msh *msh); #endif |