From f8bb8b120e6d8a207d1e34d139ecbb30653f36d6 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Sat, 25 Apr 2020 15:41:44 +0200
Subject: Nice redirs

---
 Makefile             |  1 +
 src/ft_f_fail.c      |  5 ++++-
 src/ft_m_redirs.c    |  4 ++--
 src/ft_p_lcom.c      | 28 +++++++++++++++++++++++++++-
 src/ft_p_lcom.h      |  1 +
 src/ft_p_lcom_next.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/ft_p_lcom_next.h | 21 +++++++++++++++++++++
 src/ft_s_lcom.c      |  7 +++++--
 src/ft_s_struct.h    |  1 +
 9 files changed, 107 insertions(+), 6 deletions(-)
 create mode 100644 src/ft_p_lcom_next.c
 create mode 100644 src/ft_p_lcom_next.h

diff --git a/Makefile b/Makefile
index 83790cf..1959300 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRCS_NAME		+= ft_s_init.c
 SRCS_NAME		+= ft_s_lcom.c
 SRCS_NAME		+= ft_p_line.c
 SRCS_NAME		+= ft_p_lcom.c
+SRCS_NAME		+= ft_p_lcom_next.c
 SRCS_NAME		+= ft_u_utils.c
 #------------------------------------------------------------------------------#
 SRCS			= $(addprefix ${SRCS_DIR}, ${SRCS_NAME})
diff --git a/src/ft_f_fail.c b/src/ft_f_fail.c
index 621702b..b43281d 100644
--- a/src/ft_f_fail.c
+++ b/src/ft_f_fail.c
@@ -12,7 +12,10 @@
 
 #include <libft.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
+#include <errno.h>
+
 #include "ft_d_define.h"
 
 static void
@@ -45,6 +48,6 @@ void
 void
 	ft_fail_alloc(void)
 {
-	ft_write_fail("minishell", FT_FAIL_ALLOC);
+	ft_write_fail("minishell", strerror(errno));
 	exit(FT_RET_ALLOC);
 }
diff --git a/src/ft_m_redirs.c b/src/ft_m_redirs.c
index e601894..bdb5b21 100644
--- a/src/ft_m_redirs.c
+++ b/src/ft_m_redirs.c
@@ -37,7 +37,7 @@ void
 		if ((fd = open(ptr->rdrpath,
 					   O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
 			ft_f_redir(ptr->rdrpath, msh);
-		dup2(fd, STDOUT_FILENO);
+		dup2(fd, ptr->rdrfd);
 		close(fd);
 	}
 	if (ptr->redir == 2)
@@ -45,7 +45,7 @@ void
 		if ((fd = open(ptr->rdrpath,
 					   O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1)
 			ft_f_redir(ptr->rdrpath, msh);
-		dup2(fd, STDOUT_FILENO);
+		dup2(fd, ptr->rdrfd);
 		close(fd);
 	}
 }
diff --git a/src/ft_p_lcom.c b/src/ft_p_lcom.c
index 7a19bd6..ee2ec89 100644
--- a/src/ft_p_lcom.c
+++ b/src/ft_p_lcom.c
@@ -16,6 +16,8 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+
+#include "ft_f_fail.h"
 #include "ft_s_lcom.h"
 #include "ft_s_struct.h"
 
@@ -29,9 +31,11 @@ static void
 	}
 	else if ((*link)->redir == 1 && ft_ischarset("<", *(ptr + 1)))
 	{
+		/* TODO: syntax err */
 	}
 	else if ((*link)->redir == 2 && ft_ischarset("<>", *(ptr + 1)))
 	{
+		/* TODO: syntax err */
 	}
 }
 
@@ -61,10 +65,28 @@ static int8_t
 	return (0);
 }
 
+static void
+	ft_get_rdrfd(const char *ptr,
+				t_lcom **link)
+{
+	while (ft_isdigit(*ptr))
+	{
+		ptr--;
+	}
+	if (*ptr != ' ')
+		(*link)->rdrfd = STDOUT_FILENO;
+	else
+	{
+		ptr += 1;
+		(*link)->rdrfd = ft_atoi(ptr);
+	}
+}
+
 int8_t
 	ft_get_redir(const char word[],
 				t_lcom **link)
 {
+	/* TODO: norme */
 	char	*ptr;
 
 	ptr = (char *)word;
@@ -84,8 +106,12 @@ int8_t
 		/* TODO: handle correctly multiples "msh ~> echo qwe > qwe > asd >> zxc > qweasdzxc" */
 		/* hint: bash only handles the last onke */
 	}
-	if ((*link)->redir != 0)
+	if ((*link)->redir > 0)
 	{
+		if (ft_isdigit(*(ptr - 1)))
+			ft_get_rdrfd(ptr - 1, link);
+		else
+			(*link)->rdrfd = STDOUT_FILENO;
 		ft_rdr_err_check(ptr, link);
 		if (ft_get_rdrpath(ptr, link) != 0)
 			return (-1);
diff --git a/src/ft_p_lcom.h b/src/ft_p_lcom.h
index f3386e6..256fe75 100644
--- a/src/ft_p_lcom.h
+++ b/src/ft_p_lcom.h
@@ -14,6 +14,7 @@
 #define FT_P_LCOM_H
 
 #include <stdint.h>
+
 #include "ft_s_struct.h"
 
 int8_t	ft_get_redir(const char word[],
diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c
new file mode 100644
index 0000000..626bf50
--- /dev/null
+++ b/src/ft_p_lcom_next.c
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_p_lcom_next.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+char
+	**ft_subst_args(const char word[],
+					int8_t redir)
+{
+	char	**words;
+	char	*subst;
+	size_t	i;
+
+	if (redir == 0)
+	{
+		if (!(words = ft_split(word, ' ')))
+			return (NULL);
+		return (words);
+	}
+	i = 0;
+	while (word[i] && !ft_ischarset("<>", word[i]))
+		i++;
+	if (redir > 0)
+	{
+		while (ft_isdigit(word[i]))
+			i--;
+	}
+	if (!(subst = ft_substr(word, 0, i)))
+		return (NULL);
+	if (!(words = ft_split(subst, ' ')))
+		return (NULL);
+	ft_memdel((void*)&subst);
+	return (words);
+}
diff --git a/src/ft_p_lcom_next.h b/src/ft_p_lcom_next.h
new file mode 100644
index 0000000..6f0c2a8
--- /dev/null
+++ b/src/ft_p_lcom_next.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_p_lcom_next.h                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FT_P_LCOM_NEXT_H
+#define FT_P_LCOM_NEXT_H
+
+#include <stdint.h>
+
+char	**ft_subst_args(const char word[],
+						int8_t redir);
+
+#endif
diff --git a/src/ft_s_lcom.c b/src/ft_s_lcom.c
index de09e5c..d05d84c 100644
--- a/src/ft_s_lcom.c
+++ b/src/ft_s_lcom.c
@@ -13,7 +13,9 @@
 #include <libft.h>
 #include <stdlib.h>
 #include <stdint.h>
+
 #include "ft_p_lcom.h"
+#include "ft_p_lcom_next.h"
 #include "ft_s_struct.h"
 
 static int8_t
@@ -32,7 +34,7 @@ static int8_t
 	while(words[i])
 	{
 		/* TODO: better <> cut ex: "msh ~> echo qwe>qwe" | gl hf */
-		if (ft_ischarset("<>", words[i][0]))
+		if (ft_ischarset("<>0123456789", words[i][0]))
 			break ;
 		i++;
 	}
@@ -109,10 +111,11 @@ t_lcom
 	link->redir = 0;
 	link->com = NULL;
 	link->argv = NULL;
+	link->rdrfd = 0;
 	link->rdrpath = NULL;
 	if (ft_get_redir(word, &link) != 0)
 		return (NULL);
-	if (!(words = ft_split(word, ' ')))
+	if (!(words = ft_subst_args(word, link->redir)))
 		return (NULL);
 	if (ft_fill_lcom(words, &link) < 0)
 	{
diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h
index 02e6e7f..06674cc 100644
--- a/src/ft_s_struct.h
+++ b/src/ft_s_struct.h
@@ -31,6 +31,7 @@ typedef struct		s_lcom
 	char			*com;
 	char			**argv;
 	int8_t			redir;
+	int32_t			rdrfd;
 	char			*rdrpath;
 	struct s_lcom	*next;
 }					t_lcom;
-- 
cgit v1.2.3