From 3976118a6e9acd035d20ef79d92632ecb962814e Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Sat, 5 Sep 2020 14:48:49 +0200
Subject: Rewrite in progress

---
 src/d_define.h      |  2 +-
 src/m_loop_next.c   | 44 +++++++++++++++++++++++---------------------
 src/m_loop_next.h   |  7 +++++++
 src/p_args.c        | 10 +++++-----
 src/p_args_len.c    |  6 +++---
 src/p_args_quotes.c |  6 +++---
 6 files changed, 42 insertions(+), 33 deletions(-)

(limited to 'src')

diff --git a/src/d_define.h b/src/d_define.h
index 66cc619..ce0ccc9 100644
--- a/src/d_define.h
+++ b/src/d_define.h
@@ -67,7 +67,7 @@
 
 #define C_SQUOTE	'\''
 #define C_DQUOTE	'"'
-#define C_BACKSLASH	'\\'
+#define C_BS		'\\'
 #define C_NULL		'\000'
 
 /*
diff --git a/src/m_loop_next.c b/src/m_loop_next.c
index 1163a49..75c00ac 100644
--- a/src/m_loop_next.c
+++ b/src/m_loop_next.c
@@ -15,6 +15,7 @@
 #include <unistd.h>
 
 #include "d_define.h"
+#include "m_loop_next.h"
 #include "m_prompt.h"
 #include "s_struct.h"
 
@@ -141,38 +142,39 @@ char
 	return (line);
 }
 
-static void
-	m_set_q_arr(size_t q[], char line[])
+static t_bool
+	m_check_missing_quotes(char line[])
 {
-	char	*ptr;
-
-	q[0] = 0;
-	q[1] = 0;
-	ptr = line;
-	while (*ptr != C_NULL)
+	char	*p;
+	uint8_t	mode;
+	t_bool	ret;
+
+	ret = FALSE;
+	p = line;
+	mode = M_NO_MODE;
+	while (*p != C_NULL)
 	{
-		if (*ptr == C_SQUOTE)
-		{
-			q[0] += 1;
-		}
-		else if (*ptr == C_DQUOTE)
+		if (*p == C_SQUOTE || *p == C_DQUOTE)
 		{
-			if (*(ptr - 1) != C_BACKSLASH)
-				q[1] += 1;
-			else if (*(ptr - 2) == C_BACKSLASH)
-				q[1] += 1;
+			if (((p - line) == 0) ||
+				((p - line) == 1 && *(p - 1) != C_BS) ||
+				((p - line) > 1 && *(p - 1) == C_BS &&
+				 *(p - 2) == C_BS))
+				mode = (*p == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE);
 		}
-		ptr++;
+		p++;
 	}
+	return (ret);
 }
 
 char
 	*m_check_multi_quotes(int32_t fd, char line[], t_msh *msh)
 {
-	size_t	q[2];
+	t_bool	reparse;
 
-	m_set_q_arr(q, line);
-	if (q[0] % 2 == 1 || q[1] % 2 == 1)
+	reparse = FALSE;
+	reparse = m_check_missing_quotes(line);
+	if (reparse == TRUE)
 	{
 		line = m_counter_line_quotes(fd, 2, line, msh);
 		line = m_check_multi_backslash(fd, line, msh);
diff --git a/src/m_loop_next.h b/src/m_loop_next.h
index b4ca62c..1c80e2a 100644
--- a/src/m_loop_next.h
+++ b/src/m_loop_next.h
@@ -17,6 +17,13 @@
 
 #include "s_struct.h"
 
+enum
+{
+	M_NO_MODE,
+	M_SQ_MODE,
+	M_DQ_MODE
+};
+
 char	*m_check_multi_backslash(int32_t fd, char line[], t_msh *msh);
 char	*m_check_multi_pipe(int32_t fd, char line[], t_msh *msh);
 char	*m_check_multi_and(int32_t fd, char line[], t_msh *msh);
diff --git a/src/p_args.c b/src/p_args.c
index 9ce0230..0ec391c 100644
--- a/src/p_args.c
+++ b/src/p_args.c
@@ -52,9 +52,9 @@ static char
 		ptr++;
 		if (*ptr == c && c == C_DQUOTE)
 		{
-			if (*(ptr - 1) == C_BACKSLASH)
+			if (*(ptr - 1) == C_BS)
 			{
-				if (*(ptr - 2) != C_BACKSLASH)
+				if (*(ptr - 2) != C_BS)
 					ptr++;
 			}
 		}
@@ -74,13 +74,13 @@ static int32_t
 	{
 		(*ptr)++;
 		if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) &&
-			*(*ptr - 1) != C_BACKSLASH)
+			*(*ptr - 1) != C_BS)
 		{
 			(*ptr) += 1;
 			return (p_count_args(head, *ptr, argc, start));
 		}
-		if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BACKSLASH &&
-			*(*ptr - 2) != C_BACKSLASH)
+		if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BS &&
+			*(*ptr - 2) != C_BS)
 		{
 			(*ptr) += 1;
 			return (-1);
diff --git a/src/p_args_len.c b/src/p_args_len.c
index 8bab9d7..c736a3a 100644
--- a/src/p_args_len.c
+++ b/src/p_args_len.c
@@ -26,7 +26,7 @@ static size_t
 		end++;
 		if (word[end] == c && c == C_DQUOTE)
 		{
-			if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH)
+			if (word[end - 1] == C_BS && word[end - 2] != C_BS)
 			{
 				end++;
 			}
@@ -46,12 +46,12 @@ static size_t
 	{
 		end++;
 		if (ft_iswhitespace(word[end]) == TRUE && end == 1 &&
-			word[end - 1] == C_BACKSLASH)
+			word[end - 1] == C_BS)
 		{
 			end++;
 		}
 		else if (ft_iswhitespace(word[end]) == TRUE && end > 1 &&
-			word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH)
+			word[end - 1] == C_BS && word[end - 2] != C_BS)
 		{
 			end++;
 		}
diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c
index 6ecbb90..e6fdd92 100644
--- a/src/p_args_quotes.c
+++ b/src/p_args_quotes.c
@@ -37,10 +37,10 @@ static void
 	{
 		if (ptr - word == 0)
 			ft_memmove(word, word + 1, ft_strlen(word));
-		else if (ptr - word == 1 && *(ptr - 1) == C_BACKSLASH)
+		else if (ptr - word == 1 && *(ptr - 1) == C_BS)
 			ptr++;
-		else if (ptr - word > 1 && *(ptr - 1) == C_BACKSLASH &&
-			*(ptr - 2) != C_BACKSLASH)
+		else if (ptr - word > 1 && *(ptr - 1) == C_BS &&
+			*(ptr - 2) != C_BS)
 			ptr++;
 		else
 			ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1);
-- 
cgit v1.2.3