summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-21 21:11:51 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-21 21:11:51 +0200
commit754cc63f327eb7fa40bf1b780d75da0a1482d3be (patch)
tree53a540a7b1ba88b5af435c5a27afd7df4349d43b /src
parentAdd new file (diff)
download42-minishell-754cc63f327eb7fa40bf1b780d75da0a1482d3be.tar.gz
42-minishell-754cc63f327eb7fa40bf1b780d75da0a1482d3be.tar.bz2
42-minishell-754cc63f327eb7fa40bf1b780d75da0a1482d3be.tar.xz
42-minishell-754cc63f327eb7fa40bf1b780d75da0a1482d3be.tar.zst
42-minishell-754cc63f327eb7fa40bf1b780d75da0a1482d3be.zip
Mostly works, || issues
Diffstat (limited to 'src')
-rw-r--r--src/p_lblock.c4
-rw-r--r--src/s_lpipes.c26
-rw-r--r--src/s_lpipes_split.c40
-rw-r--r--src/s_lpipes_split.h2
4 files changed, 53 insertions, 19 deletions
diff --git a/src/p_lblock.c b/src/p_lblock.c
index 369eb5a..b8341cd 100644
--- a/src/p_lblock.c
+++ b/src/p_lblock.c
@@ -23,8 +23,8 @@
#include "p_split.h"
#include "s_line.h"
#include "s_lpipes.h"
-#include "u_parse.h"
#include "s_struct.h"
+#include "u_parse.h"
#include "u_utils.h"
static uint8_t p_get_nextif(char *words[], size_t i)
@@ -54,7 +54,7 @@ static t_bool p_find_good_pipe(const char word[])
if (*ptr == C_PIPE)
{
if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE
- && *(ptr + 1) != C_PIPE)
+ && *(ptr + 1) != C_PIPE && *(ptr - 1) != C_PIPE)
return (TRUE);
}
if (*ptr == C_DQUOTE)
diff --git a/src/s_lpipes.c b/src/s_lpipes.c
index 23d0476..22ed2a5 100644
--- a/src/s_lpipes.c
+++ b/src/s_lpipes.c
@@ -10,11 +10,15 @@
/* */
/* ************************************************************************** */
-#include <cstddef>
#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
+#ifdef __linux__
+# include <linux/limits.h>
+#else
+# include <limits.h>
+#endif
#include "s_com.h"
#include "s_line.h"
@@ -76,30 +80,24 @@ struct s_lpipes *s_lpipes_new(const char pipedword[], t_msh *msh)
return (link);
}
-/*
-** TODO: Dangerous '|' split
-*/
-
struct s_lpipes *s_split_pipes(const char word[], t_msh *msh)
{
struct s_lpipes *lpipes;
size_t pos[256];
- size_t i;
+ char tmp[ARG_MAX];
+ int16_t i;
ft_bzero(pos, 256 * sizeof(size_t));
s_get_split_pos(pos, word);
-
-
i = 0;
- while (words[i] != NULL)
+ while (++i < 256)
{
- if ((lpipes = s_lpipes_new(words[i], msh)) == NULL)
- {
+ s_set_tmp(tmp, pos, i, word);
+ if (((lpipes = s_lpipes_new(tmp, msh))) == NULL)
return (NULL);
- }
s_lpipes_add_back(&msh->pipes, lpipes);
- i++;
+ if (pos[i] == 0)
+ break ;
}
- ft_delwords(words);
return (lpipes);
}
diff --git a/src/s_lpipes_split.c b/src/s_lpipes_split.c
index da23f47..4da4a03 100644
--- a/src/s_lpipes_split.c
+++ b/src/s_lpipes_split.c
@@ -13,20 +13,54 @@
#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
+#ifdef __linux__
+# include <linux/limits.h>
+#else
+# include <limits.h>
+#endif
#include "d_define.h"
#include "d_enum.h"
+#include "u_parse.h"
+#include "u_utils.h"
+
+void s_set_tmp(char tmp[], size_t pos[], int16_t i, const char word[])
+{
+ if (pos[i] != 0)
+ ft_strlcpy(tmp,
+ word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)),
+ pos[i] - ((pos[i - 1] == 0) ? (0) : (pos[i - 1]))
+ + ((pos[i - 1] == 0) ? (1) : (0)));
+ else
+ ft_strlcpy(tmp,
+ word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)),
+ ARG_MAX - pos[i] + 1);
+}
void s_get_split_pos(size_t pos[], const char word[])
{
char *ptr;
t_quote_mode mode;
- uint8_t i;
+ uint16_t i;
ptr = (char*)word;
mode = Q_NONE;
- i = 0;
- while (*ptr != C_NUL)
+ i = 1;
+ while (*ptr != C_NUL && (uint16_t)i < 256)
{
+ if (*ptr == C_PIPE)
+ {
+ if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE
+ && *(ptr + 1) != C_PIPE && *(ptr - 1) != C_PIPE)
+ {
+ pos[i] = (ptr - word);
+ i++;
+ }
+ }
+ if (*ptr == C_DQUOTE)
+ mode = u_meet_dquote(word, ptr, mode);
+ else if (*ptr == C_SQUOTE)
+ mode = u_meet_squote(word, ptr, mode);
+ ptr++;
}
}
diff --git a/src/s_lpipes_split.h b/src/s_lpipes_split.h
index 3a79dba..aa978f4 100644
--- a/src/s_lpipes_split.h
+++ b/src/s_lpipes_split.h
@@ -14,7 +14,9 @@
# define S_LPIPES_SPLIT_H
# include <stddef.h>
+# include <stdint.h>
+void s_set_tmp(char tmp[], size_t pos[], int16_t i, const char word[]);
void s_get_split_pos(size_t pos[], const char word[]);
#endif