diff options
Diffstat (limited to 'src/ft_u_vars.c')
-rw-r--r-- | src/ft_u_vars.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/ft_u_vars.c b/src/ft_u_vars.c new file mode 100644 index 0000000..4d10aeb --- /dev/null +++ b/src/ft_u_vars.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_u_vars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> + +#include "ft_f_fail.h" +#include "ft_s_destroy.h" +#include "ft_s_lcom.h" +#include "ft_s_struct.h" + + +static char + *ft_set_rva(const char varname[], + t_msh *msh) +{ + char *rvarname; + + if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char)))) + { + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + ft_memcpy((char*)rvarname, (const char*)varname + 1, + ft_strlen(varname + 1)); + ft_strlcpy(rvarname + ft_strlen(varname + 1), "=", 2); + return (rvarname); +} + +static char + *ft_dup_val(char *p_env, + char *rvarname, + t_msh *msh) +{ + char *varval; + + if (!(varval = ft_strdup(p_env))) + { + ft_memdel((void*)&rvarname); + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + ft_memdel((void*)&rvarname); + return (varval); +} + +static char + *ft_get_frm_env(char rvarname[], + t_msh *msh) +{ + char **p_env; + char *varval; + + p_env = msh->envp; + while (*p_env) + { + if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) + { + while (**p_env != '\0' && **p_env != '=') + *p_env += 1; + if (**p_env == '=') + *p_env += 1; + varval = ft_dup_val(*p_env, rvarname, msh); + return (varval); + } + p_env += 1; + } + return (NULL); +} + +/* +** char * +** ft_subst_var_value(const char varname[], const t_msh *msh); +** +** DESCRIPTION +** The ft_subst_var_value() function returns +** a heap-allocated, null-terminated string +** that may later be free'd containing the +** value of the variable varname[] including +** the '$' prefix. NULL is returned if varname[] +** wasn't found. +*/ + +char + *ft_subst_var_value(const char varname[], + t_msh *msh) +{ + /* TODO: check behaviour on empty vars -> "QWE=" */ + /* TODO: add support for special variables -> "$? $0..." */ + /* TODO: add support for global variables -> "$hey $nigga..." */ + char *varval; + char *rvarname; + + varval = NULL; + rvarname = ft_set_rva(varname, msh); + if ((varval = ft_get_special()) != NULL) + { + return (varval); + } + else + { + varval = ft_get_frm_env(rvarname, msh); + return (varval); + } + return (NULL); +} |