From 247023e3fac41c99b5580d69262837429fb2bfed Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Sat, 5 Sep 2020 17:03:49 +0200
Subject: Fuck shell

---
 TODO.org            |  1 +
 src/m_loop_next.c   |  5 ++-
 src/m_prompt.c      |  3 +-
 src/p_args_escape.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/u_utils.c       | 11 +++++++
 src/u_utils.h       |  2 ++
 6 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/TODO.org b/TODO.org
index ac234bb..62f583e 100644
--- a/TODO.org
+++ b/TODO.org
@@ -18,6 +18,7 @@
 ** TODO [#C] Handle memory
 ** TODO [#C] Go full stack to ken dash in the ass
 ** TODO [#C] Handle comments better #
+** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works find already)
 
 * Stuff to add
 ** DONE [#A] builtins to pipes
diff --git a/src/m_loop_next.c b/src/m_loop_next.c
index 4bb8f78..3bc85e8 100644
--- a/src/m_loop_next.c
+++ b/src/m_loop_next.c
@@ -18,6 +18,7 @@
 #include "m_loop_next.h"
 #include "m_prompt.h"
 #include "s_struct.h"
+#include "u_utils.h"
 
 static char
 	*m_counter_line_backslash(int32_t fd, uint8_t psx, char *line, t_msh *msh)
@@ -198,9 +199,7 @@ static t_bool
 	{
 		if (*ptr == C_SQUOTE || *ptr == C_DQUOTE)
 		{
-			if (((ptr - line) == 0) ||
-				((ptr - line) >= 1 && *(ptr - 1) != C_BS) ||
-				((ptr - line) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS))
+			if (u_is_true_quote(line, ptr) == TRUE)
 			{
 				mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE);
 				if (m_find_next_quote(&ptr, line, mode) == FALSE)
diff --git a/src/m_prompt.c b/src/m_prompt.c
index ad3c957..1ea8d70 100644
--- a/src/m_prompt.c
+++ b/src/m_prompt.c
@@ -54,7 +54,8 @@ static char
 						while (tmp[j] != '/' && j != 0)
 							j--;
 						j++;
-						ft_memmove(tmp, tmp + j, (ft_strlen(tmp) - j) + 1);
+						(void)ft_memmove(tmp, tmp + j,
+							((ft_strlen(tmp) - j) + 1) * sizeof(char));
 					}
 					var = ft_strsubst(var, "\\W", tmp);
 					ft_memdel((void*)&tmp);
diff --git a/src/p_args_escape.c b/src/p_args_escape.c
index 2a3a702..182ef2f 100644
--- a/src/p_args_escape.c
+++ b/src/p_args_escape.c
@@ -10,18 +10,105 @@
 /*                                                                            */
 /* ************************************************************************** */
 
+#include <libft.h>
 #include <stddef.h>
 
 #include "d_define.h"
+#include "u_utils.h"
+
+static t_quote_mode
+	p_escape_squote(char *ptr, char word[], t_quote_mode mode)
+{
+	if (mode == Q_NONE)
+	{
+		(void)ft_memmove(word + (ptr - word), ptr + 1,
+			(ft_strlen(ptr + 1) + 1) * sizeof(char));
+		return (Q_SINGLE);
+	}
+	else if (mode == Q_SINGLE)
+	{
+		(void)ft_memmove(word + (ptr - word), ptr + 1,
+			(ft_strlen(ptr + 1) + 1) * sizeof(char));
+		return (Q_NONE);
+	}
+	return (mode);
+}
+
+static t_quote_mode
+	p_escape_dquote(char *ptr, char word[], t_quote_mode mode)
+{
+	if (mode == Q_NONE)
+	{
+		if (u_is_true_quote(word, ptr) == TRUE)
+		{
+			(void)ft_memmove(word + (ptr - word), ptr + 1,
+				(ft_strlen(ptr + 1) + 1) * sizeof(char));
+		}
+		return (Q_DOUBLE);
+	}
+	else if (mode == Q_DOUBLE)
+	{
+		if (u_is_true_quote(word, ptr) == TRUE)
+		{
+			(void)ft_memmove(word + (ptr - word), ptr + 1,
+				(ft_strlen(ptr + 1) + 1) * sizeof(char));
+		}
+		return (Q_NONE);
+	}
+	return (mode);
+}
+
+static void
+	p_escape_bs(char *ptr, char word[], t_quote_mode mode)
+{
+	if (mode == Q_NONE)
+	{
+		(void)ft_memmove(word + (ptr - word), ptr + 1,
+			(ft_strlen(ptr + 1) + 1) * sizeof(char));
+	}
+	else if (mode == Q_DOUBLE)
+	{
+		if (*(ptr + 1) == C_BS)
+		{
+			(void)ft_memmove(word + (ptr - word), ptr + 1,
+				(ft_strlen(ptr + 1) + 1) * sizeof(char));
+			*(word + (ptr - word)) = 26;
+		}
+		else if (*(ptr + 1) == C_DQUOTE)
+		{
+			(void)ft_memmove(word + (ptr - word), ptr + 1,
+				(ft_strlen(ptr + 1) + 1) * sizeof(char));
+		}
+	}
+}
 
 static void
-	p_escape_arg(char *ptr)
+	p_escape_arg(char word[])
 {
+	char			*ptr;
 	t_quote_mode	mode;
 
+	ptr = word;
 	mode = Q_NONE;
 	while (*ptr != C_NULL)
 	{
+		if (*ptr == C_SQUOTE)
+			mode = p_escape_squote(ptr, word, mode);
+		else if (*ptr == C_DQUOTE)
+			mode = p_escape_dquote(ptr, word, mode);
+		if (*ptr == C_BS)
+			p_escape_bs(ptr, word, mode);
+		ptr++;
+	}
+}
+
+static void
+	p_replace_bs(char *ptr)
+{
+	while (*ptr != C_NULL)
+	{
+		if (*ptr == 26)
+			*ptr = C_BS;
 		ptr++;
 	}
 }
@@ -32,12 +119,14 @@ void
 	/* TODO: escape \ */
 	/* TODO: escape $# special vars */
 	/* TODO: escape my life */
+	/* TODO: comments */
 	char	**ptr;
 
 	ptr = words;
 	while (*ptr != NULL)
 	{
 		p_escape_arg(*ptr);
+		p_replace_bs(*ptr);
 		ptr++;
 	}
 }
diff --git a/src/u_utils.c b/src/u_utils.c
index ad44e82..d554a98 100644
--- a/src/u_utils.c
+++ b/src/u_utils.c
@@ -15,9 +15,20 @@
 #include <stdint.h>
 #include <unistd.h>
 
+#include "d_define.h"
 #include "f_fail.h"
 #include "s_struct.h"
 
+t_bool
+	u_is_true_quote(char *head, char *ptr)
+{
+	if (((ptr - head) == 0) ||
+		((ptr - head) >= 1 && *(ptr - 1) != C_BS) ||
+		((ptr - head) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS))
+		return (TRUE);
+	return (FALSE);
+}
+
 void
 	u_eof_fd(int32_t fd)
 {
diff --git a/src/u_utils.h b/src/u_utils.h
index 22b63ff..1b83d65 100644
--- a/src/u_utils.h
+++ b/src/u_utils.h
@@ -13,10 +13,12 @@
 #ifndef U_UTILS_H
 #define U_UTILS_H
 
+#include <libft.h>
 #include <stdint.h>
 
 #include "s_struct.h"
 
+t_bool		u_is_true_quote(char *head, char *ptr);
 void		u_eof_fd(int32_t fd);
 uint64_t	u_builtins_get_argc(const char *args[]);
 char		**u_get_env_var_names(t_msh *msh);
-- 
cgit v1.2.3