From a2dc5174eb19caebf074b446dae129d17485e7bb Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Fri, 14 Aug 2020 20:50:57 +0200
Subject: && and || works pretty bav

---
 src/e_line.c  | 37 +++++++++++++++++++++++++++----------
 src/p_lcom.c  | 12 ++++++++++--
 src/p_lcom.h  |  2 +-
 src/p_line.c  | 20 +-------------------
 src/p_split.c | 26 ++++++++++++++------------
 5 files changed, 53 insertions(+), 44 deletions(-)

(limited to 'src')

diff --git a/src/e_line.c b/src/e_line.c
index 4489926..e584140 100644
--- a/src/e_line.c
+++ b/src/e_line.c
@@ -18,6 +18,7 @@
 #include "e_externs.h"
 #include "e_pipes.h"
 #include "s_lpipes.h"
+#include "s_com.h"
 #include "s_struct.h"
 
 static uint8_t
@@ -35,27 +36,43 @@ static uint8_t
 	return (i);
 }
 
+static void
+	e_line_destroy(t_line *ptr)
+{
+	if (ptr->pipes != NULL)
+	{
+		lpipes_clear(&ptr->pipes);
+	}
+}
+
 void
 	e_line(t_msh *msh)
 {
 	t_line	*ptr;
 	uint8_t	bu_id;
+	uint8_t	previf;
 
+	previf = 0;
 	ptr = msh->curr;
 	while (ptr != NULL)
 	{
-		if (ptr->pipes)
-		{
-			e_pipes(ptr, msh);
-		}
-		else if (ptr->com)
+		if ((previf == 0) || (previf == 1 && msh->ret == 0) ||
+			(previf == 2 && msh->ret != 0))
 		{
-			if ((bu_id = get_builtin_id(ptr->com->bin, msh))
-				< FT_BUILTINS_COUNT)
-				e_builtin(ptr->com, bu_id, msh);
-			else
-				e_extern(ptr->com, msh);
+			if (ptr->pipes)
+				e_pipes(ptr, msh);
+			else if (ptr->com)
+			{
+				if ((bu_id = get_builtin_id(ptr->com->bin, msh))
+					< FT_BUILTINS_COUNT)
+					e_builtin(ptr->com, bu_id, msh);
+				else
+					e_extern(ptr->com, msh);
+			}
 		}
+		else
+			e_line_destroy(ptr);
+		previf = ptr->nextif;
 		ptr = ptr->next;
 	}
 }
diff --git a/src/p_lcom.c b/src/p_lcom.c
index e65dd3e..0e849a0 100644
--- a/src/p_lcom.c
+++ b/src/p_lcom.c
@@ -125,7 +125,6 @@ int8_t
 
 int8_t
 	p_lcom(const char line[],
-		const uint64_t count,
 		t_msh *msh)
 {
 	/* TODO: norme */
@@ -133,13 +132,21 @@ int8_t
 	t_line			*link;
 	char			**words;
 	char			*ptr;
+	uint8_t			nextif;
 	t_bool			next;
 
 	i = 0;
 	if ((words = p_split_line(line)) == NULL)
 		return (-1);
-	while (i <= count && words[i] != NULL)
+	while (words[i] != NULL)
 	{
+		if (words[i][ft_strlen(words[i]) - 1] == ';')
+			nextif = 0;
+		else if (words[i][ft_strlen(words[i]) - 1] == '&')
+			nextif = 1;
+		else
+			nextif = 2;
+		words[i][ft_strlen(words[i]) - 1] = '\0';
 		next = FALSE;
 		if ((ptr = ft_strchr(words[i], '|')) != NULL)
 		{
@@ -151,6 +158,7 @@ int8_t
 		}
 		if (next == FALSE && (link = s_line_new(words[i], msh)) == NULL)
 			return (-1);
+		link->nextif = nextif;
 		s_line_add_back(&msh->curr, link);
 		i++;
 	}
diff --git a/src/p_lcom.h b/src/p_lcom.h
index d84269e..2ff2c7e 100644
--- a/src/p_lcom.h
+++ b/src/p_lcom.h
@@ -18,6 +18,6 @@
 #include "s_struct.h"
 
 int8_t	get_redir(const char word[], t_com **com);
-int8_t	p_lcom(const char line[], const uint64_t count, t_msh *msh);
+int8_t	p_lcom(const char line[], t_msh *msh);
 
 #endif
diff --git a/src/p_line.c b/src/p_line.c
index 23a3896..6e02525 100644
--- a/src/p_line.c
+++ b/src/p_line.c
@@ -24,25 +24,7 @@ void
 	p_line(char line[],
 		t_msh *msh)
 {
-	char		*ptr;
-	uint64_t	count;
-
-	count = 0;
-	ptr = line;
-	while (*ptr != '\0')
-	{
-
-		if (*ptr == ';')
-		{
-			count += 1;
-		}
-		ptr++;
-	}
-	if (*(ptr - 1) == ';')
-	{
-		count -= 1;
-	}
-	if (p_lcom(line, count, msh) < 0)
+	if (p_lcom(line, msh) < 0)
 	{
 		f_alloc_and_destroy_msh(msh);
 	}
diff --git a/src/p_split.c b/src/p_split.c
index d834e58..bbe9bf5 100644
--- a/src/p_split.c
+++ b/src/p_split.c
@@ -69,6 +69,18 @@ static char
 	return ((char*)line_ptr);
 }
 
+static char
+	**p_split_destroy(char **words,
+					size_t i)
+{
+	while (i > 0)
+	{
+		ft_memdel((void*)&words[i]);
+	}
+	ft_memdel((void*)&words);
+	return (NULL);
+}
+
 static char
 	**p_split_to_stuff(const char line[],
 					const size_t count)
@@ -94,7 +106,7 @@ static char
 		{
 			if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) *
 				sizeof(char)))) == NULL)
-				return (NULL);
+				return (p_split_destroy(words, i));
 			ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr));
 			words[i][ft_strlen(line_ptr)] = ';';
 			words[i][ft_strlen(line_ptr) + 1] = '\0';
@@ -103,7 +115,7 @@ static char
 		{
 			if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) *
 				sizeof(char))) == NULL)
-				return (NULL);
+				return (p_split_destroy(words, i));
 			ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1);
 			words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c;
 			words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0';
@@ -119,7 +131,6 @@ char
 	**p_split_line(const char line[])
 {
 	char	**words;
-	size_t	i;
 	size_t	count;
 
 	count = p_count_semi_words(line);
@@ -128,14 +139,5 @@ char
 	count += 1;
 	if ((words = p_split_to_stuff(line, count)) == NULL)
 		return (NULL);
-	/* TODO: delete this */
-	ft_printf("words[]:\n--------\n");
-	i = 0;
-	while (words[i] != NULL) {
-		ft_printf("[%s]\n", words[i]);
-		i++;
-	}
-	ft_printf("[%s]\n", words[i]);
-	exit(0);
 	return (words);
 }
-- 
cgit v1.2.3