summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--libft/src/ft_nrealloc.c4
-rw-r--r--src/p_redirs.c18
-rw-r--r--src/p_redirs_heredoc.c40
-rw-r--r--src/p_redirs_heredoc.h20
5 files changed, 76 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index c6b8646..e920222 100644
--- a/Makefile
+++ b/Makefile
@@ -74,6 +74,7 @@ SRCS_NAME += p_line
SRCS_NAME += p_lblock
SRCS_NAME += p_lblock_next
SRCS_NAME += p_redirs
+SRCS_NAME += p_redirs_heredoc
SRCS_NAME += p_split
SRCS_NAME += u_alias
SRCS_NAME += u_parse
diff --git a/libft/src/ft_nrealloc.c b/libft/src/ft_nrealloc.c
index 6b42380..7369b42 100644
--- a/libft/src/ft_nrealloc.c
+++ b/libft/src/ft_nrealloc.c
@@ -17,9 +17,9 @@
void
*ft_nrealloc(void *ptr, size_t oldsize, size_t newsize)
{
- void *nptr;
+ void *nptr;
- if (!ptr)
+ if (ptr == NULL)
{
if ((ptr = malloc(newsize)) == NULL)
return (NULL);
diff --git a/src/p_redirs.c b/src/p_redirs.c
index 7c71d13..b615d01 100644
--- a/src/p_redirs.c
+++ b/src/p_redirs.c
@@ -18,6 +18,7 @@
#include "d_define.h"
#include "p_redirs.h"
+#include "p_redirs_heredoc.h"
#include "s_lredir.h"
#include "s_struct.h"
#include "s_lredir.h"
@@ -31,15 +32,22 @@ static void p_append_redir(const char path[],
t_msh *msh)
{
struct s_lredir *new;
+ int32_t fd;
+ int8_t redir;
+ fd = fd_rdr[FD];
+ redir = fd_rdr[RDR];
new = s_lredir_new(path, fd, redir);
if (new == NULL)
{
return ;
}
- s_lredir_add_back(&com->rdr, new);
+ new->heredoc = NULL;
if (redir == -2)
- p_get_heredoc(path, msh->fd);
+ {
+ new->heredoc = p_get_heredoc(path, msh);
+ }
+ s_lredir_add_back(&com->rdr, new);
}
static size_t p_get_path(char path[],
@@ -112,14 +120,14 @@ static void p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)
if (fd_rdr[FD] == STDOUT_FILENO)
pos[0] = (ptr - word);
else
- pos[0] = (ptr - word) - ft_intlen(fd);
+ pos[0] = (ptr - word) - ft_intlen(fd_rdr[FD]);
tmp.word = word;
tmp.ptr = ptr;
- tmp.redir = redir;
+ tmp.redir = fd_rdr[RDR];
pos[1] = p_get_path(path, tmp, msh);
(void)ft_memmove(word + pos[0],
word + pos[1], (ft_strlen(word + pos[1]) + 1) * sizeof(char));
- p_append_redir(path, fd_rdr, redir, com);
+ p_append_redir(path, fd_rdr, com, msh);
}
int8_t p_redirs(char word[], t_com **com, t_msh *msh)
diff --git a/src/p_redirs_heredoc.c b/src/p_redirs_heredoc.c
new file mode 100644
index 0000000..03d044b
--- /dev/null
+++ b/src/p_redirs_heredoc.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_redirs_heredoc.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 "d_define.h"
+#include "s_struct.h"
+
+char
+ *p_get_heredoc(const char path[], t_msh *msh)
+{
+ char *heredoc;
+ char *line;
+ int8_t gnl;
+
+ heredoc = NULL;
+ gnl = 1;
+ if ((heredoc = ft_strdup("")) == NULL)
+ return (NULL);
+ while (gnl > 0 && ft_strncmp(path, line, ft_strlen(path) + 1) != 0)
+ {
+ gnl = get_next_line(msh->fd, &line);
+ if ((heredoc = ft_nrealloc(heredoc, ft_strlen(heredoc),
+ ft_strlen(heredoc) + ft_strlen(line) + 2)) == NULL)
+ return (heredoc);
+ ft_strlcpy(heredoc + ft_strlen(heredoc), line, ft_strlen(line) + 1);
+ heredoc[ft_strlen(heredoc) + 1] = C_NUL;
+ heredoc[ft_strlen(heredoc)] = C_LF;
+ }
+ return (heredoc);
+}
diff --git a/src/p_redirs_heredoc.h b/src/p_redirs_heredoc.h
new file mode 100644
index 0000000..15d6e70
--- /dev/null
+++ b/src/p_redirs_heredoc.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_redirs_heredoc.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 P_REDIRS_HEREDOC_H
+#define P_REDIRS_HEREDOC_H
+
+#include "s_struct.h"
+
+char *p_get_heredoc(const char path[], t_msh *msh);
+
+#endif