Browse Source

[wip] refactor pipe and redirection parsing.
- rework, new way of parsing/storing data.
- multi-piping is working again.
- redirections is broken.

Moul 2 years ago
parent
commit
dce0b34bfe
6 changed files with 112 additions and 56 deletions
  1. 1 0
      Makefile
  2. 0 42
      src/parse_redirections.c
  3. 5 4
      src/pipe_exec.c
  4. 20 6
      src/pipe_parsing.c
  5. 77 0
      src/pipe_redir_parsing.c
  6. 9 4
      src/sh.h

+ 1 - 0
Makefile

@@ -11,6 +11,7 @@ SRC_NAME =	main.c \
 			history.c \
 			history_tools.c \
 			detect_cotes.c \
+			pipe_redir_parsing.c \
 			pipe_parsing.c \
 			parse_redirections.c \
 			pipe_exec.c \

+ 0 - 42
src/parse_redirections.c

@@ -1,48 +1,6 @@
 #include "sh.h"
 
 /*
-** Get chevrons positions
-** If a chevron is present:
-**  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
-** Else: launch pipe without redirections
-*/
-
-void	parse_redirection(t_e *e, t_pipe *p, char *str)
-{
-	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 ;
-		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);
-		redirections(e, p, r);
-		free_redir(r);
-	}
-	else
-		parse_exec(e, p);
-}
-
-/*
 ** Free memory of redirect struct
 ** r->file is not freed as I couldn't check when it wasn't allocated
 */

+ 5 - 4
src/pipe_exec.c

@@ -6,12 +6,13 @@
 ** Wait for infinite fork to display again promt
 */
 
-void	launch_pipes(t_e *e, t_pipe *p)
+void	launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t)
 {
 	p->i = -1;
 	while (++p->i <= p->nbr)
 	{
-		parse_redirection(e, p, p->cmd_pipe[p->i]);
+		parse_exec(e, p, t);
+//		parse_redirection(e, p, p->cmd_pipe[p->i]);
 		if (p->i != 0)
 			free(p->fd[p->i - 1]);
 	}
@@ -29,9 +30,9 @@ void	launch_pipes(t_e *e, t_pipe *p)
 ** Launch pipes
 */
 
-void	parse_exec(t_e *e, t_pipe *p)
+void	parse_exec(t_e *e, t_pipe *p, t_pipe_redir **t)
 {
-	p->cmd_opt[p->i] = cmd_splitting(e, p->cmd_pipe[p->i], ' ');
+	p->cmd_opt[p->i] = cmd_splitting(e, t[p->i]->itxt, ' ');
 	ft_strdel(&p->cmd_pipe[p->i]);
 	remove_cotes(p->cmd_opt[p->i]);
 	handle_multi_piping(p);

+ 20 - 6
src/pipe_parsing.c

@@ -10,8 +10,9 @@
 
 void	pipe_init(t_e *e, char *all_cmd)
 {
-	t_pipe	*p;
-	int		i;
+	t_pipe			*p;
+	int				i;
+	t_pipe_redir	**t;
 
 	p = (t_pipe*)malloc(sizeof(t_pipe));
 	if (ft_strstr(all_cmd, "||") != NULL)
@@ -32,8 +33,21 @@ void	pipe_init(t_e *e, char *all_cmd)
 		}
 	}
 	p->cmd_pipe = cmd_splitting(e, all_cmd, '|');
-//ft_strdel(&all_cmd); // fonctionne pour l’appel depuis main(), mais pas des autres
-	pipe_parsing(p, e);
+	t = (t_pipe_redir**)malloc(sizeof(t_pipe_redir*) * (p->nbr + 2));
+	fill_pipe_redir_data(e, p, t);
+//	display_data(t);
+//	ft_strdel(&all_cmd); // segfault pour "b ; b"
+	pipe_parsing(p, e, t);
+}
+
+void	display_data(t_pipe_redir **t)
+{
+	int		i;
+
+	i = -1;
+	while (t[++i] != NULL)
+		ft_printf("itxt: '%s'\nifd: %d\noperator: '%s'\nofd: %d\notxt: '%s'\n\n", \
+			t[i]->itxt, t[i]->ifd, t[i]->operator, t[i]->ofd, t[i]->otxt);
 }
 
 /*
@@ -46,7 +60,7 @@ void	pipe_init(t_e *e, char *all_cmd)
 ** Free cmd_pipe
 */
 
-void	pipe_parsing(t_pipe *p, t_e *e)
+void	pipe_parsing(t_pipe *p, t_e *e, t_pipe_redir **t)
 {
 	int		val;
 	int		i;
@@ -64,7 +78,7 @@ void	pipe_parsing(t_pipe *p, t_e *e)
 			return ;
 		}
 	}
-	launch_pipes(e, p);
+	launch_pipes(e, p, t);
 }
 
 /*

+ 77 - 0
src/pipe_redir_parsing.c

@@ -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);
+}

+ 9 - 4
src/sh.h

@@ -35,12 +35,17 @@ int		non_coted_block(t_e *e, char *line, int pos, t_block *blk);
 ** pipe_parsing.c
 */
 void	pipe_init(t_e *e, char *all_cmd);
-void	pipe_parsing(t_pipe *p, t_e *e);
+void	pipe_parsing(t_pipe *p, t_e *e, t_pipe_redir **t);
 int		count_pipe_nbr(t_e *e, char *str);
 /*
+** pipe_redir_parsing.c
+*/
+void	fill_pipe_redir_data(t_e *e, t_pipe *p, t_pipe_redir **t);
+void	display_data(t_pipe_redir **t);
+int		parse_redirection(t_e *e, t_pipe *p, char *str, t_pipe_redir *t);
+/*
 ** parse_redirections.c
 */
-void	parse_redirection(t_e *e, t_pipe *p, char *str);
 void	free_redir(t_redir *r);
 int		check_chev_is_on_non_coted_block(t_e *e, char *str, char c);
 int		check_before_after_chevron(char *str, int chev);
@@ -48,8 +53,8 @@ void	parse_and(char *str, t_redir *r, int file_pos);
 /*
 ** pipe_exec.c
 */
-void	launch_pipes(t_e *e, t_pipe *p);
-void	parse_exec(t_e *e, t_pipe *p);
+void	launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t);
+void	parse_exec(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	handle_multi_piping(t_pipe *p);
 void	remove_cotes(char **cmd);
 /*