From 7fd7a979e27b06034f1f47189bf410c9a1180357 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Wed, 21 Oct 2020 18:38:29 +0200
Subject: One good function

---
 src/p_lblock.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/p_lblock.c b/src/p_lblock.c
index 7213ee4..e7faeea 100644
--- a/src/p_lblock.c
+++ b/src/p_lblock.c
@@ -18,11 +18,14 @@
 #include <errno.h>
 
 #include "d_define.h"
+#include "d_enum.h"
 #include "f_fail.h"
 #include "p_split.h"
 #include "s_line.h"
 #include "s_lpipes.h"
+#include "u_parse.h"
 #include "s_struct.h"
+#include "u_utils.h"
 
 static uint8_t	p_get_nextif(char *words[], size_t i)
 {
@@ -43,6 +46,30 @@ static uint8_t	p_get_nextif(char *words[], size_t i)
 ** TODO: ft_strchr('|'): danger /!\ rewrite this shit
 */
 
+static t_bool	p_find_good_pipe(const char word[])
+{
+	char			*ptr;
+	t_quote_mode	mode;
+
+	mode = Q_NONE;
+	ptr = (char*)word;
+	while (*ptr != C_NUL)
+	{
+		if (*ptr == C_PIPE)
+		{
+			if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE
+					&& *(ptr + 1) != C_PIPE)
+				return (TRUE);
+		}
+		if (*ptr == C_DQUOTE)
+			mode = u_meet_dquote(word, ptr, mode);
+		else if (*ptr == C_SQUOTE)
+			mode = u_meet_squote(word, ptr, mode);
+		ptr++;
+	}
+	return (FALSE);
+}
+
 static int8_t	p_loop(char *words[], t_msh *msh)
 {
 	t_line_block	*link;
@@ -53,7 +80,7 @@ static int8_t	p_loop(char *words[], t_msh *msh)
 	while (words[i] != NULL)
 	{
 		nextif = p_get_nextif(words, i);
-		if (ft_strchr(words[i], '|') != NULL)
+		if (p_find_good_pipe(words[i]) == TRUE)
 		{
 			if ((link = s_line_new(NULL, 0)) == NULL)
 				return (-1);
-- 
cgit v1.2.3