diff options
Diffstat (limited to '')
-rw-r--r-- | src/b_export.c | 65 | ||||
-rw-r--r-- | src/u_vars.c | 2 | ||||
-rw-r--r-- | src/u_vars.h | 2 |
3 files changed, 59 insertions, 10 deletions
diff --git a/src/b_export.c b/src/b_export.c index 8225b51..9fe5709 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -17,8 +17,12 @@ #include "d_enum.h" #include "b_env.h" #include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" +#include "u_vars.h" static t_bool check_valid_identifier(const char *arg) @@ -48,12 +52,55 @@ static t_bool return (FALSE); } +static void + b_add_to_env_from_globals(const char varname[], + const char var[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i++; + } + if (!(nenvp[i] = ft_strdup(var))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + lvars_delone(&msh->vars, varname); + msh->envp = nenvp; +} + uint8_t b_export(char *args[], - t_msh *msh) + t_msh *msh) { + /* TODO: norme */ const uint64_t argc = get_argc((const char**)args); char **ptr; + char *varval; + char fmt[255]; t_bool next; uint8_t r; @@ -65,7 +112,6 @@ uint8_t r = 0; while (*ptr) { - ft_printf("export: [%s]\n", *ptr); next = FALSE; if (check_valid_identifier(*ptr) == FALSE) { @@ -76,16 +122,17 @@ uint8_t if (next == FALSE && check_equals(*ptr) == FALSE) { next = TRUE; - /* TODO: export already set variable */ - /* examples: */ - /* ~> QWE=qwe */ - /* ~> export QWE */ - /* -------------- */ - /* # non-existant variable QWE */ - /* ~> export QWE */ + ft_sprintf(fmt, "$%s", *ptr); + varval = get_cstm_vr(fmt, msh); + if (varval != NULL) + { + ft_sprintf(fmt, "%s=%s", *ptr, varval); + b_add_to_env_from_globals(*ptr, fmt, msh); + } } else if (next == FALSE && check_equals(*ptr) == TRUE) { + /* TODO: verify global vars, delete if needed, export the thing */ } ptr++; } diff --git a/src/u_vars.c b/src/u_vars.c index ebd6a03..f7123ef 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -113,7 +113,7 @@ static char return (NULL); } -static char +char *get_cstm_vr(const char varname[], t_msh *msh) { diff --git a/src/u_vars.h b/src/u_vars.h index fb8b924..ec6ecf5 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,6 +15,8 @@ #include "s_struct.h" +char *get_cstm_vr(const char varname[], + t_msh *msh); char *u_get_var_value(const char varname[], t_msh *msh); |