Browse Source

[fix] pipe: do not display error message. Add tests.

Moul 4 years ago
parent
commit
24458f290a
3 changed files with 12 additions and 3 deletions
  1. 7 2
      src/pipe.c
  2. 3 1
      src/tools.c
  3. 2 0
      tests.sh

+ 7 - 2
src/pipe.c

@@ -40,6 +40,8 @@ void	launch_pipe(char *all_cmd)
 ** Malloc for commands and their options
 ** For all command remove non visible char before and after cmd
 ** Check a command is present
+** If cmd == "": return
+** If no cmd between pipe: display error message and return
 ** Split cmd and his options
 ** Launch multi-piping
 ** Wait for infinite fork to display again promt
@@ -48,15 +50,18 @@ void	launch_pipe(char *all_cmd)
 void	pipe_parsing(int **fd, int pipe_nbr, char **cmd_pipe, int i)
 {
 	char	***cmd_opt;
+	int		val;
 
 	cmd_opt = (char***)malloc(sizeof(char**) * (pipe_nbr + 1));
 	i = -1;
 	while (cmd_pipe[++i] != NULL)
 	{
+		val = -1;
 		cmd_pipe[i] = ft_strtrim(cmd_pipe[i]);
-		if (check_cmd_presence(cmd_pipe[i]) != 1)
+		if ((val = check_cmd_presence(cmd_pipe[i])) != 1)
 		{
-			error_message("", "no command between pipes or semicolons", NULL);
+			if (val == 0)
+				error_message("", "no cmd between pipes or semicolons", NULL);
 			return ;
 		}
 		cmd_opt[i] = cmd_split(cmd_pipe[i], ' ', '\t');

+ 3 - 1
src/tools.c

@@ -26,8 +26,10 @@ int		check_cmd_presence(char *cmd)
 	int	i;
 
 	i = -1;
+	if (ft_strlen(cmd) == 0)
+		return (2);
 	while (cmd[++i] != '\0')
-		if (32 < cmd[i] && cmd[i] <= 126)
+		if (ft_isvisible(cmd[i]) == 1)
 			return (1);
 	return (0);
 }

+ 2 - 0
tests.sh

@@ -48,6 +48,7 @@ tests=(
 #"    \t  " # espaces, tabulations
 
 ## Pipe
+" " # empty
 "ls | cat" # pipe simple
 "ls | cat | cat" # pipe double
 "ls | cat | cat | cat" # pipe triple
@@ -57,6 +58,7 @@ tests=(
 	| cat | cat | cat | cat | cat | cat | cat | cat | cat | cat" # twenty pipes
 "ls || cat" # Segfault
 "ls ||||||||| cat"
+"ls | | ls" # Segfault
 
 ## Redirections droite
 "ls > $tmpf ; cat $tmpf ; rm $tmpf" # simple