summaryrefslogtreecommitdiffstats
path: root/src/p_lblock_next.c
diff options
context:
space:
mode:
authorsalad <fmoenne-@student.le-101.fr>2020-10-26 13:42:56 +0100
committersalad <fmoenne-@student.le-101.fr>2020-10-26 13:42:56 +0100
commit0277ddfac754ab4ad5bdd2b692e31a717efbe569 (patch)
tree49d7c5fd3a12248af85e2c3a3254bc1538ae5775 /src/p_lblock_next.c
parentreqdy for MERGE (diff)
parentTODO update (diff)
download42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.gz
42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.bz2
42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.xz
42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.zst
42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.zip
merge wif master
Diffstat (limited to '')
-rw-r--r--src/p_lblock_next.c349
1 files changed, 349 insertions, 0 deletions
diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c
new file mode 100644
index 0000000..02ecbb2
--- /dev/null
+++ b/src/p_lblock_next.c
@@ -0,0 +1,349 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_lblock_next.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 <limits.h>
+
+#include "d_define.h"
+#include "s_destroy.h"
+#include "f_fail.h"
+#include "s_struct.h"
+#include "u_alias.h"
+#include "u_parse.h"
+#include "u_utils.h"
+#include "u_vars.h"
+#include "u_vars_next.h"
+
+/* TODO: norme */
+
+static char
+ *p_double_them_bs(char varval[])
+{
+ char *ptr;
+
+ ptr = varval;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_BACKS)
+ {
+ ptr = ft_memmove(ptr + 1, ptr, ft_strlen(ptr) + 1);
+ varval[ptr - varval] = C_BACKS;
+ }
+ ptr++;
+ }
+ return (varval);
+}
+
+static void
+ p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh)
+{
+ char tmp[ARG_MAX];
+ char varval[ARG_MAX];
+ char *ptr;
+ size_t varlen;
+
+ ptr = word;
+ varlen = i + 1;
+ while (ptr[varlen] != C_NUL &&
+ ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE &&
+ ft_iswhitespace(ptr[varlen]) == FALSE)
+ varlen += 1;
+ ft_strlcpy(tmp, ptr + i, varlen + 1 - i);
+ u_get_var_value(varval, tmp, ARG_MAX, msh);
+ p_double_them_bs(varval);
+ ft_strlcpy(ptr + (i + ft_strlen(varval)),
+ ptr + varlen,
+ ft_strlen(ptr + varlen) + 1);
+ (void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char));
+ *(p) = word + (i + ft_strlen(varval) - 1);
+}
+
+void
+ p_subst_vars(char word[], t_msh *msh)
+{
+ char *ptr;
+ t_quote_mode mode;
+
+ mode = Q_NONE;
+ ptr = word;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_DQUOTE)
+ mode = u_meet_dquote(word, ptr, mode);
+ else if (*ptr == C_SQUOTE)
+ mode = u_meet_squote(word, ptr, mode);
+ if ((mode == Q_NONE || mode == Q_DOUBLE) && *ptr == C_DOLLAR &&
+ u_is_not_escaped(word, ptr) == TRUE)
+ {
+ p_subst_this_var(&ptr, (ptr - word), word, msh);
+ }
+ ptr++;
+ }
+}
+
+static char
+ *p_skip_whitespace(char *ptr)
+{
+ while (*ptr != C_NUL && ft_iswhitespace(*ptr))
+ ptr++;
+ return (ptr);
+}
+
+static t_bool
+ p_meet_whitespace(char *head, char *ptr, t_quote_mode mode)
+{
+ if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE)
+ {
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+size_t
+ p_subst_alias(char word[], t_bool reset, t_msh *msh)
+{
+ static size_t used[4096];
+ static size_t i = 0;
+ char value[ARG_MAX];
+ char tmp[255];
+ size_t locat[2];
+ size_t j;
+ size_t usedcmp;
+ char *ptr;
+ t_bool good;
+ t_quote_mode mode;
+
+ if (reset == TRUE)
+ {
+ i = 0;
+ while (i < 4096)
+ {
+ used[i] = 0;
+ i++;
+ }
+ i = 0;
+ }
+ mode = Q_NONE;
+ ptr = word;
+ ptr = p_skip_whitespace(ptr);
+ good = TRUE;
+ locat[0] = (ptr - word);
+ locat[1] = (ptr - word);
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_DQUOTE)
+ mode = u_meet_dquote(word, ptr, mode);
+ if (*ptr == C_SQUOTE)
+ mode = u_meet_squote(word, ptr, mode);
+ if (mode == Q_NONE && *ptr == C_EQUALS)
+ good = FALSE;
+ if (ft_iswhitespace(*ptr) == TRUE &&
+ p_meet_whitespace((char*)word, ptr, mode) == TRUE)
+ {
+ locat[1] = (ptr - word);
+ if (good == TRUE)
+ break ;
+ else
+ {
+ ptr = p_skip_whitespace(ptr);
+ locat[0] = (ptr - word);
+ ptr -= 1;
+ good = TRUE;
+ }
+ }
+ ptr++;
+ }
+ if (*ptr == C_NUL && good == TRUE)
+ locat[1] = (ptr - word);
+ if (good == TRUE)
+ {
+ ft_strlcpy(tmp,
+ word + locat[0],
+ ((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1);
+ if ((usedcmp = u_get_alias_value(value, tmp, ARG_MAX, msh)) != 0)
+ {
+ j = 0;
+ good = TRUE;
+ while (j < i)
+ {
+ if (used[j] == usedcmp)
+ good = FALSE;
+ j++;
+ }
+ if (good == TRUE)
+ {
+ (void)ft_memmove(word + (locat[0] + ft_strlen(value)),
+ word + locat[1],
+ ft_strlen(word + locat[1]) + 1 * sizeof(char));
+ (void)ft_memmove(word + locat[0],
+ value,
+ ft_strlen(value) * sizeof(char));
+ used[i] = usedcmp;
+ i++;
+ return (usedcmp);
+ }
+ }
+ }
+ return (0);
+}
+
+char
+ **p_subst_home(char *words[], t_msh *msh)
+{
+ char path[PATH_MAX];
+ char **ptr;
+
+ u_get_var_value(path, "$HOME", PATH_MAX, msh);
+ if (path[0] == C_NUL)
+ return (words);
+ ptr = words;
+ while (*ptr != NULL)
+ {
+ if (*ptr[0] == '~')
+ {
+ *ptr = ft_strsubst(*ptr, "~", path);
+ }
+ ptr++;
+ }
+ return (words);
+}
+
+static void
+ p_register_word(char word[], t_msh *msh)
+{
+ char name[255];
+ char val[255];
+ char *ptr;
+ size_t i;
+
+ name[0] = '$';
+ ptr = word;
+ i = 1;
+ while (*ptr != '=' && *ptr != '\0')
+ {
+ name[i] = *ptr;
+ i++;
+ ptr++;
+ }
+ name[i] = '\0';
+ ptr++;
+ i = 0;
+ while (*ptr != '\0')
+ {
+ val[i] = *ptr;
+ i++;
+ ptr++;
+ }
+ val[i] = '\0';
+ u_subst_var_value(name, val, msh);
+}
+
+static char
+ **p_add_to_variables_and_delete(char *words[],
+ t_bool reg,
+ int64_t i,
+ t_msh *msh)
+{
+ char **rewords;
+ int64_t j;
+ int64_t k;
+
+ j = 0;
+ if (reg == TRUE)
+ {
+ while (words[j] && j < i)
+ {
+ p_register_word(words[j], msh);
+ j++;
+ }
+ }
+ j = 0;
+ while (words[i + j] != NULL)
+ j++;
+ if ((rewords = (char**)malloc((j + 1) * sizeof(char*))) == NULL)
+ {
+ ft_delwords(words);
+ f_alloc_and_destroy_msh(msh);
+ }
+ k = i;
+ while (i - k < j)
+ {
+ if ((rewords[i - k] = ft_strdup(words[i])) == NULL)
+ {
+ ft_delwords(words);
+ f_alloc_and_destroy_msh(msh);
+ }
+ i++;
+ }
+ rewords[i - k] = 0;
+ ft_delwords(words);
+ i++;
+ return (rewords);
+}
+
+static void
+ p_add_to_env_fork(int64_t i,
+ char *words[],
+ t_msh *msh)
+{
+ int64_t j;
+
+ j = 0;
+ while(j < i)
+ {
+ ft_strlcpy(msh->env_fork_tmp[j], words[j], ft_strlen(words[j]) + 1);
+ j++;
+ }
+ msh->env_fork_tmp[j][0] = '\0';
+}
+
+char
+ **p_check_args_equals(char *words[], t_msh *msh)
+{
+ char *ptr;
+ t_bool reg;
+ t_bool isvar;
+ int64_t i;
+
+ i = 0;
+ reg = FALSE;
+ isvar = FALSE;
+ while (words[i])
+ {
+ ptr = words[i];
+ while (*ptr != '\0' && *ptr != '=')
+ ptr++;
+ if (*ptr == '=')
+ {
+ reg = TRUE;
+ isvar = TRUE;
+ }
+ if (*ptr == '\0' || words[i][0] == '=' ||
+ ft_isdigit(words[i][0]) == TRUE)
+ {
+ reg = FALSE;
+ if (i == 0)
+ isvar = FALSE;
+ if (isvar == TRUE)
+ p_add_to_env_fork(i, words, msh);
+ else
+ msh->env_fork_tmp[0][0] = '\0';
+ break ;
+ }
+ i++;
+ }
+ if (isvar == TRUE)
+ return (p_add_to_variables_and_delete(words, reg, i, msh));
+ return (words);
+}