summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/p_lblock_next.c102
-rw-r--r--src/p_lblock_next.h1
-rw-r--r--src/p_subst_alias.c132
-rw-r--r--src/p_subst_alias.h23
-rw-r--r--src/s_com.c1
6 files changed, 160 insertions, 100 deletions
diff --git a/Makefile b/Makefile
index 4b77b31..4904816 100644
--- a/Makefile
+++ b/Makefile
@@ -80,6 +80,7 @@ SRCS_NAME += p_lblock_more
SRCS_NAME += p_redirs
SRCS_NAME += p_redirs_heredoc
SRCS_NAME += p_split
+SRCS_NAME += p_subst_alias
SRCS_NAME += p_subst_home
SRCS_NAME += u_alias
SRCS_NAME += u_parse
diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c
index d964f11..9def230 100644
--- a/src/p_lblock_next.c
+++ b/src/p_lblock_next.c
@@ -88,108 +88,12 @@ void p_subst_vars(char word[], t_msh *msh)
}
}
-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);
-}
+
+
+
static char **p_alloc_rewords(char *words[], int64_t j, t_msh *msh)
{
diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h
index 7af7881..5d8aeb8 100644
--- a/src/p_lblock_next.h
+++ b/src/p_lblock_next.h
@@ -19,7 +19,6 @@
# include "s_struct.h"
void p_subst_vars(char word[], t_msh *msh);
-size_t p_subst_alias(char word[], t_bool reset, t_msh *msh);
char **p_subst_args(const char word[], int8_t redir);
char **p_check_args_equals(char *words[], t_msh *msh);
diff --git a/src/p_subst_alias.c b/src/p_subst_alias.c
new file mode 100644
index 0000000..9e25087
--- /dev/null
+++ b/src/p_subst_alias.c
@@ -0,0 +1,132 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_subst_alias.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 <stddef.h>
+
+#include "s_struct.h"
+#include "u_alias.h"
+#include "u_parse.h"
+#include "u_utils.h"
+#include "u_vars.h"
+
+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);
+}
+
+static char *p_set_ptr(char *ptr, char word[], t_bool *good, size_t locat[])
+{
+ t_quote_mode mode;
+
+ mode = Q_NONE;
+ 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++;
+ }
+ return (ptr);
+}
+
+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;
+
+ if (reset == TRUE)
+ {
+ i = 0;
+ while (i < 4096)
+ {
+ used[i] = 0;
+ i++;
+ }
+ i = 0;
+ }
+ ptr = word;
+ ptr = p_skip_whitespace(ptr);
+ good = TRUE;
+ locat[0] = (ptr - word);
+ locat[1] = (ptr - word);
+ ptr = p_set_ptr(ptr, word, &good, locat);
+ 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);
+}
diff --git a/src/p_subst_alias.h b/src/p_subst_alias.h
new file mode 100644
index 0000000..5d5476f
--- /dev/null
+++ b/src/p_subst_alias.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_subst_alias.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* 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 */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_P_SUBST_ALIAS_H
+# define FT_P_SUBST_ALIAS_H
+
+# include <libft.h>
+# include <stddef.h>
+
+# include "s_struct.h"
+
+size_t p_subst_alias(char word[], t_bool reset, t_msh *msh);
+
+#endif
diff --git a/src/s_com.c b/src/s_com.c
index 84e0f35..2927a51 100644
--- a/src/s_com.c
+++ b/src/s_com.c
@@ -19,6 +19,7 @@
#include "p_args.h"
#include "p_lblock.h"
#include "p_lblock_next.h"
+#include "p_subst_alias.h"
#include "p_subst_home.h"
#include "p_redirs.h"
#include "s_lredir.h"