WIP: parse command list tests: assert that invalid pipeline returns null

This commit is contained in:
Khaïs COLIN 2025-02-26 14:07:55 +01:00 committed by Khaïs COLIN
parent bccd68b11f
commit c57a4a69a7
4 changed files with 54 additions and 6 deletions

View file

@ -6,13 +6,16 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
/* Updated: 2025/02/26 13:56:45 by khais ### ########.fr */
/* Updated: 2025/02/26 16:53:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "command_list.h"
#include "command_list_builder.h"
#include "operator.h"
#include "libft.h"
#include "../../ft_errno.h"
#include <unistd.h>
#include <stdlib.h>
/*
@ -35,6 +38,22 @@ static bool cmdlist_builder_at_last_pipeline(t_cmdlist_builder *builder)
return (builder->idx == builder->cmdlist->num_pipelines - 1);
}
/*
** Frees all memory associated with this builder and set error to true.
*/
static void cmdlist_builder_error(t_cmdlist_builder *builder)
{
if (builder == NULL)
return ;
cmdlist_destroy(builder->cmdlist);
ft_dprintf(STDERR_FILENO, "current wordlist: %p\n", builder->current_wordlist);
ft_dprintf(STDERR_FILENO, "current wordlist->word: %p\n", builder->current_wordlist->word);
wordlist_destroy(builder->current_wordlist);
ft_dprintf(STDERR_FILENO, "current wordlist has been freed: %p\n", builder->current_wordlist);
worddesc_destroy(builder->current_word);
builder->error = true;
}
/*
** Delimit the current pipeline, creating a new t_pipeline object.
**
@ -46,8 +65,17 @@ static void cmdlist_builder_delimit(
t_cmdlist_builder *builder,
t_wordlist **words)
{
ft_dprintf(STDERR_FILENO, "delimit: ");
wordlist_debug(builder->current_wordlist);
builder->cmdlist->pipelines[builder->idx]
= pipeline_from_wordlist(builder->current_wordlist);
if (builder->cmdlist->pipelines[builder->idx] == NULL)
{
ft_perror("invalid pipeline");
cmdlist_builder_error(builder);
ft_dprintf(STDERR_FILENO, "destroyed builder\n");
return ;
}
if (cmdlist_builder_at_last_pipeline(builder))
builder->cmdlist->operators[builder->idx] = OP_END;
else
@ -68,13 +96,16 @@ t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
if (setup_cmdlist_builder(&builder, &words) == NULL)
return (NULL);
while (builder.current_word != NULL)
while (builder.error == false && builder.current_word != NULL)
{
ft_dprintf(STDERR_FILENO, "current word: %s\n", builder.current_word->word);
if (match_op(builder.current_word->word) == OP_INVALID)
cmdlist_builder_next_word(&builder, &words);
else
cmdlist_builder_delimit(&builder, &words);
}
if (builder.error)
return (NULL);
if (builder.current_wordlist != NULL)
cmdlist_builder_delimit(&builder, &words);
return (builder.cmdlist);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 13:51:18 by khais #+# #+# */
/* Updated: 2025/02/26 13:52:39 by khais ### ########.fr */
/* Updated: 2025/02/26 17:02:05 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,7 +78,10 @@ t_cmdlist_builder *setup_cmdlist_builder(
if (builder->cmdlist == NULL)
return (NULL);
builder->current_wordlist = NULL;
ft_dprintf(STDERR_FILENO, "words before pop in setup: %p\n", *words);
builder->current_word = wordlist_pop(words);
ft_dprintf(STDERR_FILENO, "builder->current_word after pop in setup: %p\n", builder->current_word);
ft_dprintf(STDERR_FILENO, "words after pop in setup: %p\n", *words);
builder->idx = 0;
return (builder);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 13:50:25 by khais #+# #+# */
/* Updated: 2025/02/26 13:54:55 by khais ### ########.fr */
/* Updated: 2025/02/26 14:05:36 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,11 @@ typedef struct s_cmdlist_builder
** index of the pipeline that is about to be inserted into the cmdlist
*/
int idx;
/*
** set to true in an error was encountered that requires stopping the
** processing.
*/
bool error;
} t_cmdlist_builder;
t_cmdlist_builder *setup_cmdlist_builder(