From 870e8a742536c08d4102630d0e4ffa0f86e639f0 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Mon, 5 Oct 2020 15:18:41 +0200
Subject: Pipes rework incomming

---
 Makefile        |  2 +-
 TODO.org        |  1 +
 src/e_externs.c | 26 +++++++++++++++++++++-----
 src/e_pipes.c   | 14 ++++++++++++--
 src/f_com.c     |  3 +--
 src/f_com.h     |  4 +---
 src/u_path.c    |  4 ++--
 7 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index 9eefc38..46e4176 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-default: all
+default: debug
 #==============================================================================#
 #--------------------------------- SHELL --------------------------------------#
 #==============================================================================#
diff --git a/TODO.org b/TODO.org
index 7e6f387..cb51122 100644
--- a/TODO.org
+++ b/TODO.org
@@ -23,6 +23,7 @@
 ** TODO [#A] bus error on msh ~> exit
 ** TODO [#A] various very bad '|' splits
 ** TODO [#A] buildable on Linux
+** TODO [#A] 2>&1
 ** DONE [#A] SEGV on pipes
 ** DONE [#B] forked write(2) stuff on cd
 ** DONE [#B] Multiline && ||
diff --git a/src/e_externs.c b/src/e_externs.c
index e1b8b7c..a3d3594 100644
--- a/src/e_externs.c
+++ b/src/e_externs.c
@@ -30,7 +30,7 @@
 #include "u_path.h"
 #include "u_utils.h"
 
-static void	e_extern_child(const char *fullpath, t_com *ptr, t_msh *msh)
+static void	e_extern_child(const char fullpath[], t_com *ptr, t_msh *msh)
 {
 	if (execve(fullpath, ptr->argv, msh->envp) == -1)
 	{
@@ -55,11 +55,24 @@ static void	e_export_env_fork(t_com *ptr, t_msh *msh)
 	}
 }
 
+static void	e_fullpath_not_found(t_com *ptr, t_msh *msh)
+{
+	f_command_not_found(ptr->bin);
+	u_eof_fd(msh->fd);
+	s_com_destroy(&msh->com);
+	s_line_clear(&msh->curr);
+	s_destroy(msh);
+	exit(127);
+}
+
 /*
 ** TODO: handle fork failed
 */
 
-static void	e_exec_path(const char fullpath[], t_com *ptr, t_msh *msh)
+static void	e_exec_path(const char fullpath[],
+						t_com *ptr,
+						uint8_t fp_ret,
+						t_msh *msh)
 {
 	pid_t	pid;
 	int32_t	status;
@@ -69,6 +82,8 @@ static void	e_exec_path(const char fullpath[], t_com *ptr, t_msh *msh)
 		if (ptr->env_fork != NULL)
 			e_export_env_fork(ptr, msh);
 		e_dup_redirs(ptr, msh);
+		if (fp_ret == 2)
+			e_fullpath_not_found(ptr, msh);
 		e_extern_child(fullpath, ptr, msh);
 	}
 	else if (pid < 0)
@@ -85,17 +100,18 @@ static void	e_exec_path(const char fullpath[], t_com *ptr, t_msh *msh)
 void		e_extern(t_com *ptr, t_msh *msh)
 {
 	char	fullpath[PATH_MAX];
+	uint8_t	fp_ret;
 
 	fullpath[0] = C_NUL;
 	if (ft_ischarset("./", ptr->bin[0]) == TRUE)
 	{
 		ft_strlcpy(fullpath, ptr->bin, PATH_MAX);
-		e_exec_path(fullpath, ptr, msh);
+		e_exec_path(fullpath, ptr, 0, msh);
 		return ;
 	}
 	else
 	{
-		u_search_in_path(fullpath, ptr->bin, PATH_MAX, msh);
-		e_exec_path(fullpath, ptr, msh);
+		fp_ret = u_search_in_path(fullpath, ptr->bin, PATH_MAX, msh);
+		e_exec_path(fullpath, ptr, fp_ret, msh);
 	}
 }
diff --git a/src/e_pipes.c b/src/e_pipes.c
index be647b9..1b4e314 100644
--- a/src/e_pipes.c
+++ b/src/e_pipes.c
@@ -87,10 +87,21 @@ static void		e_pipe_exec_path(char *fullpath[],
 	msh->ret = WEXITSTATUS(status);
 }
 
+/* static void	e_fullpath_not_found(t_com *ptr, t_msh *msh) */
+/* { */
+/* 	f_command_not_found(ptr->bin); */
+/* 	u_eof_fd(msh->fd); */
+/* 	s_com_destroy(&msh->com); */
+/* 	s_line_clear(&msh->curr); */
+/* 	s_destroy(msh); */
+/* 	exit(127); */
+/* } */
+
 static char		*e_get_current_path(struct s_lpipes *rptr, t_msh *msh)
 {
 	char	tmp[PATH_MAX];
 	char	*path;
+	uint8_t	fp_ret;
 
 	path = NULL;
 	if (rptr->com->bin != NULL && ft_ischarset("/.", rptr->com->bin[0]) == TRUE)
@@ -107,7 +118,7 @@ static char		*e_get_current_path(struct s_lpipes *rptr, t_msh *msh)
 		}
 		else
 		{
-			u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh);
+			fp_ret = u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh);
 			if ((path = ft_strdup(tmp)) == NULL)
 				f_alloc_and_destroy_msh(msh);
 		}
@@ -138,7 +149,6 @@ static char		**e_get_fullpath(size_t pipes, t_msh *msh)
 void			e_pipes(t_msh *msh)
 {
 	const size_t	pipes = e_get_pipes_count(msh->pipes);
-	char			**fullpath;
 
 	fullpath = e_get_fullpath(pipes, msh);
 	e_pipe_exec_path(fullpath, pipes, msh);
diff --git a/src/f_com.c b/src/f_com.c
index 7b06128..d1edb2c 100644
--- a/src/f_com.c
+++ b/src/f_com.c
@@ -16,9 +16,8 @@
 #include "d_define.h"
 #include "s_struct.h"
 
-void	f_fail_command_not_found(const char command[], t_msh *msh)
+void	f_command_not_found(const char command[])
 {
 	ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", "minishell", command,
 		F_COMMAND_NOT_FOUND);
-	msh->ret = 127;
 }
diff --git a/src/f_com.h b/src/f_com.h
index a6859f5..bbf2bf6 100644
--- a/src/f_com.h
+++ b/src/f_com.h
@@ -13,8 +13,6 @@
 #ifndef FT_F_COM_H
 # define FT_F_COM_H
 
-# include "s_struct.h"
-
-void	f_fail_command_not_found(const char command[], t_msh *msh);
+void	f_command_not_found(const char command[]);
 
 #endif
diff --git a/src/u_path.c b/src/u_path.c
index 1ca1b2d..d851d8b 100644
--- a/src/u_path.c
+++ b/src/u_path.c
@@ -70,7 +70,7 @@ uint8_t			u_search_in_path(char fullpath[],
 	int8_t			ret;
 
 	if (u_get_var_value(tmp, "$PATH", ARG_MAX, msh) != 0)
-		return (1);
+		return (2);
 	s.dstsize = dstsize;
 	s.tok_path = ft_strtok(tmp, ":");
 	while (s.tok_path != NULL)
@@ -84,5 +84,5 @@ uint8_t			u_search_in_path(char fullpath[],
 		}
 		s.tok_path = ft_strtok(NULL, ":");
 	}
-	return (1);
+	return (2);
 }
-- 
cgit v1.2.3