summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-25 15:41:44 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-25 15:41:44 +0200
commitf8bb8b120e6d8a207d1e34d139ecbb30653f36d6 (patch)
treefa9827b6f179035328fabfafee1e014ba31195a3
parentstrerror is nice actually (diff)
download42-minishell-f8bb8b120e6d8a207d1e34d139ecbb30653f36d6.tar.gz
42-minishell-f8bb8b120e6d8a207d1e34d139ecbb30653f36d6.tar.bz2
42-minishell-f8bb8b120e6d8a207d1e34d139ecbb30653f36d6.tar.xz
42-minishell-f8bb8b120e6d8a207d1e34d139ecbb30653f36d6.tar.zst
42-minishell-f8bb8b120e6d8a207d1e34d139ecbb30653f36d6.zip
Nice redirs
-rw-r--r--Makefile1
-rw-r--r--src/ft_f_fail.c5
-rw-r--r--src/ft_m_redirs.c4
-rw-r--r--src/ft_p_lcom.c28
-rw-r--r--src/ft_p_lcom.h1
-rw-r--r--src/ft_p_lcom_next.c45
-rw-r--r--src/ft_p_lcom_next.h21
-rw-r--r--src/ft_s_lcom.c7
-rw-r--r--src/ft_s_struct.h1
9 files changed, 107 insertions, 6 deletions
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;