summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--TODO.org1
-rw-r--r--src/p_lblock.c26
-rw-r--r--src/p_lblock.h2
-rw-r--r--src/s_com.c21
-rw-r--r--src/s_lredir.c71
-rw-r--r--src/s_lredir.h22
-rw-r--r--src/s_struct.h14
8 files changed, 116 insertions, 42 deletions
diff --git a/Makefile b/Makefile
index a4647bb..de610b2 100644
--- a/Makefile
+++ b/Makefile
@@ -65,6 +65,7 @@ SRCS_NAME += s_lalias
SRCS_NAME += s_line
SRCS_NAME += s_lvars
SRCS_NAME += s_lpipes
+SRCS_NAME += s_lredir
SRCS_NAME += p_args
SRCS_NAME += p_args_next
SRCS_NAME += p_args_len
diff --git a/TODO.org b/TODO.org
index aa54a35..b205dda 100644
--- a/TODO.org
+++ b/TODO.org
@@ -14,6 +14,7 @@
** DONE [#A] Don't fuck with '$vars'
** TODO [#A] <>>
** TODO [#A] msh ~> qwewqrqrqwrqwrqr ($? and output)
+** TODO [#A] pipes don't split "qweqwreq | qweqweqe"
** DONE [#A] SEGV on pipes
** DONE [#B] forked write(2) stuff on cd
** DONE [#B] Multiline && ||
diff --git a/src/p_lblock.c b/src/p_lblock.c
index 3acae1a..ba47028 100644
--- a/src/p_lblock.c
+++ b/src/p_lblock.c
@@ -83,36 +83,14 @@ static void
}
}
-int8_t
- p_get_redir(const char word[], t_com **com)
+int8_t p_get_redirs(const char word[], t_com **com)
{
char *ptr;
ptr = (char *)word;
- while (*ptr)
+ while (*ptr != C_NUL)
{
- if (*ptr == '<')
- {
- (*com)->redir = -1;
- break ;
- }
- if (*ptr == '>')
- {
- (*com)->redir = (*(ptr + 1) == '>') ? (2) : (1);
- break ;
- }
ptr++;
- /* gl hf */
- }
- if ((*com)->redir > 0)
- {
- if (ft_isdigit(*(ptr - 1)) == TRUE)
- get_rdrfd(ptr - 1, com);
- else
- (*com)->rdrfd = STDOUT_FILENO;
- rdr_err_check(ptr, com);
- if (get_rdrpath(ptr, com) != 0)
- return (-1);
}
return (0);
}
diff --git a/src/p_lblock.h b/src/p_lblock.h
index 3979e31..8247cfb 100644
--- a/src/p_lblock.h
+++ b/src/p_lblock.h
@@ -17,7 +17,7 @@
#include "s_struct.h"
-int8_t p_get_redir(const char word[], t_com **com);
+int8_t p_get_redirs(const char word[], t_com **com);
int8_t p_line_block(const char line[], t_msh *msh);
#endif
diff --git a/src/s_com.c b/src/s_com.c
index 28b460f..6dd4dc0 100644
--- a/src/s_com.c
+++ b/src/s_com.c
@@ -19,6 +19,7 @@
#include "p_args.h"
#include "p_lblock.h"
#include "p_lblock_next.h"
+#include "s_lredir.h"
#include "s_struct.h"
static int8_t
@@ -38,20 +39,12 @@ static int8_t
}
else
return (0);
- while(words[i])
- {
- /* TODO: cut fd number "msh ~> echo a 2>file" */
- /* ^ */
- if (ft_ischarset("<>", words[i][0]) == TRUE)
- break ;
- i++;
- }
- if (!((*com)->argv = (char**)malloc((i + 1) * sizeof(char*))))
+ if (((*com)->argv = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
return (-1);
j = 0;
while (i > 0 && j < i)
{
- if (!((*com)->argv[j] = ft_strdup(words[j])))
+ if (((*com)->argv[j] = ft_strdup(words[j])) == NULL)
return (-1);
j++;
}
@@ -93,6 +86,8 @@ void
ft_memdel((void*)&ptr->rdrpath);
if (ptr->env_fork != NULL)
ft_delwords(ptr->env_fork);
+ if (ptr->rdr != NULL)
+ s_lredir_clear(&ptr->rdr);
ft_memdel((void*)&ptr);
}
@@ -107,15 +102,13 @@ t_com
if ((com = (t_com*)malloc(sizeof(t_com))) == NULL)
return (NULL);
- com->redir = 0;
+ com->redir = NULL;
com->bin = NULL;
com->argv = NULL;
- com->rdrfd = 0;
- com->rdrpath = NULL;
com->env_fork = NULL;
nword[0] = C_NUL;
ft_strlcpy(nword, word, ARG_MAX);
- if (p_get_redir(nword, &com) != 0)
+ if (p_get_redirs(nword, &com) != 0)
return (NULL);
if (msh->alias != NULL)
{
diff --git a/src/s_lredir.c b/src/s_lredir.c
new file mode 100644
index 0000000..353149d
--- /dev/null
+++ b/src/s_lredir.c
@@ -0,0 +1,71 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* s_lredir.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 <stdint.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include "s_struct.h"
+
+static t_lredir *s_lredir_last(struct s_lredir *lredir)
+{
+ while (lredir->next != NULL)
+ {
+ lredir = lredir->next;
+ }
+ return (lredir);
+}
+
+void s_lredir_add_back(t_lredir **lredir, t_lredir *new)
+{
+ struct s_lredir *tmp;
+
+ if (*lredir == NULL)
+ {
+ *lredir = new;
+ }
+ else
+ {
+ tmp = s_lredir_last(*lredir);
+ tmp->next = new;
+ }
+}
+
+void s_lredir_clear(struct s_lredir **lredir)
+{
+ struct s_lredir *tmp;
+ struct s_lredir *renext;
+
+ if (lredir == NULL)
+ return ;
+ tmp = *lredir;
+ while (tmp != NULL)
+ {
+ renext = tmp->next;
+ ft_memdel((void*)&tmp);
+ tmp = renext;
+ }
+ *lredir = NULL;
+}
+
+struct s_lredir *s_lredir_new(const char path[], int32_t fd, int8_t redir)
+{
+ struct s_lredir *rdr;
+
+ if ((rdr = (struct s_lredir*)malloc(sizeof(struct s_lredir))) == NULL)
+ return (NULL);
+ rdr->fd = fd;
+ rdr->redir = redir;
+ rdr->next = NULL;
+ ft_strlcpy(rdr->path, path, PATH_MAX);
+ return (rdr);
+}
diff --git a/src/s_lredir.h b/src/s_lredir.h
new file mode 100644
index 0000000..3349329
--- /dev/null
+++ b/src/s_lredir.h
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* s_lredir.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 S_LREDIR_H
+#define S_LREDIR_H
+
+#include "s_struct.h"
+
+void s_lredir_add_back(t_lredir **lredir, t_lredir *new)
+void s_lredir_clear(struct s_lredir **lredir)
+struct s_lredir *s_lredir_new(const char path[], int32_t fd, int8_t redir)
+
+#endif
diff --git a/src/s_struct.h b/src/s_struct.h
index 84271de..0b83158 100644
--- a/src/s_struct.h
+++ b/src/s_struct.h
@@ -14,6 +14,7 @@
#define S_STRUCT_H
#include <stdint.h>
+#include <stddef.h>
#include <limits.h>
#include "d_define.h"
@@ -36,20 +37,27 @@ typedef struct s_lalias
/*
** redir(int8_t) index
** -------------------
+** -2: <<
** -1: <
** 1: >
** 2: >>
** 0: means no redirection
*/
+typedef struct s_lredir
+{
+ char path[PATH_MAX];
+ int32_t fd;
+ int8_t redir;
+ struct s_lredir *next;
+} t_lredir;
+
typedef struct s_com
{
char **argv;
char **env_fork;
char *bin;
- char *rdrpath;
- int32_t rdrfd;
- int8_t redir;
+ struct s_lredir *rdr;
} t_com;
struct s_lpipes