Browse Source

[enh] handle to not split at '|' between coted blocks:
- pass len of initial string inside functions.
- new f() to count '|' nbr only on non-coted blocks.
- more explicit boolean output for is_on_coted_block().
- add corresponding unit test.
- check todo points.
- Add next todos.

Moul 4 years ago
parent
commit
460af09384
9 changed files with 38 additions and 16 deletions
  1. 5 3
      TODO.md
  2. 4 4
      src/cmd_splitting.c
  3. 1 1
      src/heredoc.c
  4. 1 1
      src/main.c
  5. 1 1
      src/parse_redirections.c
  6. 21 3
      src/pipe.c
  7. 1 1
      src/redirect.c
  8. 3 2
      src/sh.h
  9. 1 0
      tests.sh

+ 5 - 3
TODO.md

@@ -63,11 +63,13 @@
 * [ ] plus fonctionnel
 
 ### Gestion des cotes
-* [ ] Gérer le parsing des caractères entres côtes
+* [x] Gérer le parsing des caractères entres côtes
 - [ ] balancer dans le pipe les sous-commandes entourées de ``
-* [ ] si '|', ';', '<' ou '>' entre les cotes : problème de parsing :
+* [x] si '|', ';', '<' ou '>' entre les cotes : problème de parsing :
  * [x] parsing au tout début, avant les parsing des ';', '>', '<' et '|'.
- * [ ] détection du type de blocs: parsing en conséquence du type de bloc
+ * [x] détection du type de blocs: parsing en conséquence du type de bloc
+* [ ] gérer le retrait des cotes
+* [ ] retirer les trims qui enlèvent les espaces entre les blocs cotés
 
 ### Redirections
 * [x] Gestion 2>&1, 2<&1 : https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean

+ 4 - 4
src/cmd_splitting.c

@@ -8,7 +8,7 @@
 ** Allocate and fill new tab
 */
 
-char	**cmd_splitting(t_e *e, char *cmd, char c)
+char	**cmd_splitting(t_e *e, char *cmd, char c, int len)
 {
 	int		pos;
 	int		*poss;
@@ -21,7 +21,7 @@ char	**cmd_splitting(t_e *e, char *cmd, char c)
 		return (NULL);
 	poss = (int*)malloc(sizeof(int) * 50);
 	while (cmd[++pos] != '\0')
-		if (cmd[pos] == c && is_on_coted_block(e, pos) == 0)
+		if (cmd[pos] == c && is_on_coted_block(e, len + pos) == 1)
 			poss[nbr++] = pos;
 	tabe = (char**)malloc(sizeof(char*) * (nbr + 2));
 	tabe = fill_tabe(tabe, cmd, nbr, poss);
@@ -71,6 +71,6 @@ int		is_on_coted_block(t_e *e, int pos)
 	while (e->b[++b_pos] != NULL)
 		if (e->b[b_pos]->begin <= pos && pos <= e->b[b_pos]->end\
 			&& e->b[b_pos]->type != 0)
-			return (1);
-	return (0);
+			return (0);
+	return (1);
 }

+ 1 - 1
src/heredoc.c

@@ -26,7 +26,7 @@ void	prompt_redirection(t_e *e, char *cmd, char *eof)
 	stdfd = dup(0);
 	dup2(pipefd[0], 0);
 	wait(NULL);
-	launch_pipe(cmd);
+	launch_pipe(e, cmd, 0);
 	close(pipefd[0]);
 	close(0);
 	dup(stdfd);

+ 1 - 1
src/main.c

@@ -65,7 +65,7 @@ void	comma_split(t_e *e, char *line)
 
 	i = -1;
 	len = 0;
-	if ((cmds = cmd_splitting(e, line, ';')) != NULL)
+	if ((cmds = cmd_splitting(e, line, ';', 0)) != NULL)
 		while (cmds[++i] != NULL)
 		{
 			if (ft_strcmp(cmds[i], "") != 0)

+ 1 - 1
src/parse_redirections.c

@@ -42,7 +42,7 @@ void	parse_redirection(t_e *e, char **str, int i, int len)
 		redirections(e, split[0], split[1], split[2], split[3]);
 	}
 	else
-		launch_pipe(str[i]);
+		launch_pipe(e, str[i], len);
 }
 
 /*

+ 21 - 3
src/pipe.c

@@ -8,7 +8,7 @@
 ** Split at pipes
 */
 
-void	launch_pipe(char *all_cmd)
+void	launch_pipe(t_e *e, char *all_cmd, int len)
 {
 	int		**fd;
 	int		pipe_nbr;
@@ -20,7 +20,7 @@ void	launch_pipe(char *all_cmd)
 		error_message("", "I do not handle \"||\"", NULL);
 		return ;
 	}
-	pipe_nbr = count_char_str(all_cmd, '|');
+	pipe_nbr = count_pipe_nbr(e, all_cmd, len);
 	fd = (int**)malloc(sizeof(int*) * pipe_nbr);
 	i = -1;
 	while (++i < pipe_nbr)
@@ -32,7 +32,7 @@ void	launch_pipe(char *all_cmd)
 			return ;
 		}
 	}
-	cmd_pipe = ft_strsplit(all_cmd, '|');
+	cmd_pipe = cmd_splitting(e, all_cmd, '|', len);
 	pipe_parsing(fd, pipe_nbr, cmd_pipe, i);
 }
 
@@ -114,3 +114,21 @@ void	handle_multi_piping(int **fd, int pipe_nbr, char ***cmd_opt, int i)
 		close(fd[i - 1][1]);
 	}
 }
+
+/*
+** Count number of pipe occurences on comma splitted string.
+** Only pipe betweeen non-coted blocks are counted.
+*/
+
+int		count_pipe_nbr(t_e *e, char *str, int len)
+{
+	int		i;
+	int		occ;
+
+	occ = 0;
+	i = -1;
+	while (str[++i] != '\0')
+		if (str[i] == '|' && is_on_coted_block(e, len + i) == 1)
+			occ++;
+	return (occ);
+}

+ 1 - 1
src/redirect.c

@@ -26,7 +26,7 @@ void	redirections(t_e *e, char *cmd, char *operator, char *ofd, char *file)
 		right_redirections(fd, operator, ofd, file);
 	if (ft_strcmp(operator, "<<") != 0)
 	{
-		launch_pipe(cmd);
+		launch_pipe(e, cmd, 0);
 		close(fd[0]);
 		if (ft_strcmp(operator, "<") != 0)
 			operator[0] == '2' ? close(STDERR) : close(STDOUT);

+ 3 - 2
src/sh.h

@@ -57,13 +57,14 @@ char	*prompt_herething(t_e *e, int pos, char *fc);
 /*
 ** pipe.c
 */
-void	launch_pipe(char *all_cmd);
+void	launch_pipe(t_e *e, char *all_cmd, int len);
 void	pipe_parsing(int **fd, int pipe_nbr, char **cmd_pipe, int i);
 void	handle_multi_piping(int **fd, int pipe_nbr, char ***cmd_opt, int i);
+int		count_pipe_nbr(t_e *e, char *str, int len);
 /*
 ** cmd_splitting.c
 */
-char	**cmd_splitting(t_e *e, char *cmd, char c);
+char	**cmd_splitting(t_e *e, char *cmd, char c, int len);
 int		is_on_coted_block(t_e *e, int pos);
 char	**fill_tabe(char **tabe, char *cmd, int nbr, int *poss);
 /*

+ 1 - 0
tests.sh

@@ -130,6 +130,7 @@ tests=(
 "echo \"nrsatu;nrtsa\""
 "echo \"rasnt>inausrt\" > toto; cat toto"
 "echo \" nrastiu<rasntiu\" > toto; cat toto"
+"echo \"nrau|sntiu\" | cat"
 "ls ; echo \"nasruit><rnasuti\" > toto ; cat < toto"
 
 ### Back cotes: pipe