|
@@ -0,0 +1,77 @@
|
|
|
+#include "sh.h"
|
|
|
+
|
|
|
+/*
|
|
|
+** Fill redirections
|
|
|
+** Else: fill pipe
|
|
|
+*/
|
|
|
+
|
|
|
+void fill_pipe_redir_data(t_e *e, t_pipe *p, t_pipe_redir **t)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ int j;
|
|
|
+
|
|
|
+ i = -1;
|
|
|
+ j = -1;
|
|
|
+ while (p->cmd_pipe[++j] != NULL)
|
|
|
+ {
|
|
|
+ t[++i] = (t_pipe_redir*)malloc(sizeof(t_pipe_redir));
|
|
|
+ if (parse_redirection(e, p, p->cmd_pipe[j], t[i]) == 1)
|
|
|
+ ;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ t[i]->itxt = p->cmd_pipe[j];
|
|
|
+ t[i]->ifd = 1;
|
|
|
+ t[i]->operator = "|";
|
|
|
+ t[i]->ofd = 0;
|
|
|
+ t[i]->otxt = NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ t[++i] = NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+** Get chevrons positions
|
|
|
+** Take closer chevron
|
|
|
+** Prevent segfault by checking there is something before and after chevron
|
|
|
+** Parse redirections: split in four parts:
|
|
|
+** Get position of the first chevron
|
|
|
+** Before chevron, chevron, after chevron
|
|
|
+** Check if fd is specified
|
|
|
+** Check if there is two chevrons
|
|
|
+** Split with strsub
|
|
|
+*/
|
|
|
+
|
|
|
+int parse_redirection(t_e *e, t_pipe *p, char *str, t_pipe_redir *t)
|
|
|
+{
|
|
|
+ int chev;
|
|
|
+ int chev2;
|
|
|
+ int nchev;
|
|
|
+ int fd;
|
|
|
+ t_redir *r;
|
|
|
+
|
|
|
+ chev = check_chev_is_on_non_coted_block(e, str, '>');
|
|
|
+ chev2 = check_chev_is_on_non_coted_block(e, str, '<');
|
|
|
+ if (chev != -1 || chev2 != -1)
|
|
|
+ {
|
|
|
+ chev = (chev != -1 && chev < chev2) || chev2 == -1 ? chev : chev2;
|
|
|
+ if (check_before_after_chevron(str, chev) == 1)
|
|
|
+ return (0);
|
|
|
+ r = (t_redir*)malloc(sizeof(t_redir));
|
|
|
+ fd = str[chev - 1] == '1' || str[chev - 1] == '2' ? 1 : 0;
|
|
|
+ nchev = str[chev] == str[chev + 1] ? 2 : 1;
|
|
|
+ r->cmd = ft_strsub(str, 0, chev - fd);
|
|
|
+ r->operator = ft_strsub(str, chev - fd, nchev + fd);
|
|
|
+ parse_and(str, r, chev + nchev);
|
|
|
+ t->itxt = ft_strdup(r->cmd);
|
|
|
+ t->operator = ft_strdup(r->operator);
|
|
|
+ t->otxt = ft_strdup(r->file);
|
|
|
+// t->otxt = r->ofd;
|
|
|
+ t->ifd = r->pfd[0];
|
|
|
+ t->ofd = r->pfd[1];
|
|
|
+// redirections(e, p, r);
|
|
|
+// free_redir(r);
|
|
|
+ return (1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ return (0);
|
|
|
+}
|