pipeline: reject repetitions of '|' token

This commit is contained in:
Khaïs COLIN 2025-02-21 15:46:34 +01:00
parent 68e923c09e
commit c9f8c5a4f9
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 27 additions and 6 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:46 by khais #+# #+# */
/* Updated: 2025/02/21 12:40:51 by khais ### ########.fr */
/* Updated: 2025/02/21 15:38:54 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,6 +50,7 @@ char *ft_strerror(t_errno errno)
[FT_ESUCCESS] = "Success",
[FT_EINVAL] = "Invalid argument",
[FT_EBADID] = "Bad identifier",
[FT_EUNEXPECTED_PIPE] = "minishell: syntax error near unexpected token `|'",
};
if (errno >= 0 && errno < FT_EMAXERRNO)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:58 by khais #+# #+# */
/* Updated: 2025/02/21 12:41:02 by khais ### ########.fr */
/* Updated: 2025/02/21 15:37:56 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ typedef enum e_errno
FT_ESUCCESS = 0,
FT_EINVAL,
FT_EBADID,
FT_EUNEXPECTED_PIPE,
FT_EMAXERRNO,
} t_errno;

View file

@ -6,13 +6,13 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */
/* Updated: 2025/02/21 15:23:12 by khais ### ########.fr */
/* Updated: 2025/02/21 15:42:34 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipeline.h"
#include "libft.h"
#include "unistd.h"
#include "../../ft_errno.h"
#include "../matchers/pipe.h"
static int pipeline_count_cmds(t_wordlist *words)
@ -53,9 +53,11 @@ static t_pipeline *pipeline_parse_wordlist(t_pipeline *pipeline,
{
pipeline->cmds[idx] = simple_cmd_from_wordlist(current_wordlist);
if (pipeline->cmds[idx] == NULL)
return (pipeline_destroy(pipeline), NULL);
return (pipeline_destroy(pipeline), wordlist_destroy(words), NULL);
current_wordlist = NULL;
current_word = ignore_word(current_word, &words);
if (is_pipe(current_word))
return (pipeline_destroy(pipeline), wordlist_destroy(words), worddesc_destroy(current_word), ft_errno(FT_EUNEXPECTED_PIPE), NULL);
idx++;
}
current_wordlist = wordlist_push(current_wordlist, current_word);

View file

@ -6,12 +6,13 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
/* Updated: 2025/02/21 15:29:48 by khais ### ########.fr */
/* Updated: 2025/02/21 16:03:39 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "../src/parser/pipeline/pipeline.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include "../src/ft_errno.h"
#include <assert.h>
#include "ft_printf.h"
#include "libft.h"
@ -95,6 +96,20 @@ static void test_parse_pipeline_four_cmd(void)
pipeline_destroy(pipeline);
}
static void test_parse_pipeline_double_pipe_rejected(void)
{
ft_errno(FT_ESUCCESS);
assert(parse_pipeline("echo hello | | tee output.txt") == NULL);
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
}
static void test_parse_pipeline_triple_pipe_rejected(void)
{
ft_errno(FT_ESUCCESS);
assert(parse_pipeline("echo hello | | | tee output.txt") == NULL);
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
}
int main(void)
{
test_parse_empty_pipeline();
@ -102,5 +117,7 @@ int main(void)
test_parse_pipeline_two_cmd();
test_parse_pipeline_three_cmd();
test_parse_pipeline_four_cmd();
test_parse_pipeline_double_pipe_rejected();
test_parse_pipeline_triple_pipe_rejected();
return (0);
}