summaryrefslogtreecommitdiffstats
path: root/src/p_lcom_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-06 21:56:20 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-06 21:56:20 +0200
commit4543c3ba3222d47780ad3e091cfe6f3098cc2bca (patch)
treefb8a70bf05464a6fa2c820cc43ea34421340c903 /src/p_lcom_next.c
parentCommit from minishell (diff)
download42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.gz
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.bz2
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.xz
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.zst
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.zip
Stacked
Diffstat (limited to '')
-rw-r--r--src/p_lcom_next.c93
1 files changed, 57 insertions, 36 deletions
diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c
index 435a710..7bc306e 100644
--- a/src/p_lcom_next.c
+++ b/src/p_lcom_next.c
@@ -13,72 +13,94 @@
#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
+#include <limits.h>
-#include "d_enum.h"
+#include "d_define.h"
#include "s_destroy.h"
#include "f_fail.h"
#include "s_struct.h"
+#include "u_utils.h"
#include "u_vars.h"
#include "u_vars_next.h"
/* TODO: norme */
-static int8_t
- subst_those_vars(int64_t i,
- char **p_words,
- t_msh *msh)
+/* static int8_t */
+/* p_subst_those_vars(int64_t i, */
+/* char **p_words, */
+/* t_msh *msh) */
+/* { */
+/* size_t varlen; */
+/* char *s_varname; */
+/* char *varval; */
+
+/* varval = NULL; */
+/* s_varname = NULL; */
+/* varlen = i + 1; */
+/* while ((*p_words)[varlen] != '\0' && */
+/* ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) */
+/* varlen += 1; */
+/* if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) */
+/* return (-1); */
+/* varval = u_get_var_value(s_varname, msh); */
+/* *p_words = ft_strsubst(*p_words, s_varname, varval); */
+/* ft_memdel((void*)&s_varname); */
+/* ft_memdel((void*)&varval); */
+/* return (0); */
+/* } */
+
+static char
+ *p_subst_this_var(int64_t i, char word[], t_msh *msh)
{
+ char tmp[4096];
+ char varval[4096];
+ char *ptr;
size_t varlen;
- char *s_varname;
- char *varval;
- varval = NULL;
- s_varname = NULL;
+ ptr = word;
varlen = i + 1;
- while ((*p_words)[varlen] != '\0' &&
- ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE)
+ while (ptr[varlen] != C_NUL &&
+ ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE &&
+ ft_iswhitespace(ptr[varlen]) == FALSE)
varlen += 1;
- if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i)))
- return (-1);
- varval = u_get_var_value(s_varname, msh);
- *p_words = ft_strsubst(*p_words, s_varname, varval);
- ft_memdel((void*)&s_varname);
- ft_memdel((void*)&varval);
- return (0);
+ ft_strlcpy(tmp, ptr + i, varlen + 1 - i);
+ u_get_var_value(varval, tmp, 4096, msh);
+ ft_strlcpy(tmp, ptr + varlen + 1, varlen);
+ word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1);
+ ft_strlcpy(word + i, varval, ft_strlen(varval) + 1);
+ ft_printf("[%s]\n", tmp);
+ ft_strlcpy(word + i + ft_strlen(varval), tmp, ft_strlen(tmp) + 1);
+ exit(0);
+ return (word);
}
char
- **p_subst_vars(char *words[],
- t_msh *msh)
+ *p_subst_vars(char word[], t_msh *msh)
{
- char **p_words;
- int64_t i;
+ char *ptr;
- p_words = words;
- i = 0;
- while (*p_words)
+ ptr = word;
+ while (*ptr != C_NUL)
{
- while ((i = ft_strlchr((*p_words), '$')) != -1)
+ if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE)
{
- if (*(*p_words) + i - 1 != '\\')
- {
- if (subst_those_vars(i, p_words, msh) != 0)
- return (NULL);
- }
+ if ((word = p_subst_this_var((ptr - word), word, msh)) == NULL)
+ return (NULL);
}
- p_words += 1;
+ ptr++;
}
- return (words);
+ return (word);
}
char
**p_subst_home(char *words[],
t_msh *msh)
{
- char *path;
+ char path[PATH_MAX];
char **ptr;
- if ((path = u_get_var_value("$HOME", msh)) == NULL)
+ u_get_var_value(path, "$HOME", PATH_MAX, msh);
+ if (path[0] == C_NUL)
return (words);
ptr = words;
while (*ptr != NULL)
@@ -89,7 +111,6 @@ char
}
ptr++;
}
- ft_memdel((void*)&path);
return (words);
}