wip: make a copy of words in pipeline_from_wordlist

This commit is contained in:
Khaïs COLIN 2025-02-26 14:07:55 +01:00 committed by Khaïs COLIN
parent ccbd89708f
commit 5cd755b73c
11 changed files with 85 additions and 22 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
/* Updated: 2025/02/26 16:53:55 by khais ### ########.fr */
/* Updated: 2025/03/04 12:41:51 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -46,11 +46,14 @@ static void cmdlist_builder_error(t_cmdlist_builder *builder)
if (builder == NULL)
return ;
cmdlist_destroy(builder->cmdlist);
builder->cmdlist = NULL;
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);
builder->current_wordlist = NULL;
ft_dprintf(STDERR_FILENO, "current wordlist has been freed: %p\n", builder->current_wordlist);
worddesc_destroy(builder->current_word);
builder->current_word = NULL;
builder->error = true;
}
@ -65,7 +68,7 @@ static void cmdlist_builder_delimit(
t_cmdlist_builder *builder,
t_wordlist **words)
{
ft_dprintf(STDERR_FILENO, "delimit: ");
ft_dprintf(STDERR_FILENO, "cmdlist delimit: ");
wordlist_debug(builder->current_wordlist);
builder->cmdlist->pipelines[builder->idx]
= pipeline_from_wordlist(builder->current_wordlist);
@ -73,7 +76,7 @@ static void cmdlist_builder_delimit(
{
ft_perror("invalid pipeline");
cmdlist_builder_error(builder);
ft_dprintf(STDERR_FILENO, "destroyed builder\n");
ft_dprintf(STDERR_FILENO, "destroyed builder: %d\n", builder->error);
return ;
}
if (cmdlist_builder_at_last_pipeline(builder))
@ -95,7 +98,7 @@ t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
t_cmdlist_builder builder;
if (setup_cmdlist_builder(&builder, &words) == NULL)
return (NULL);
return (ft_dprintf(STDERR_FILENO, "cmdlist: error during setup\n"), NULL);
while (builder.error == false && builder.current_word != NULL)
{
ft_dprintf(STDERR_FILENO, "current word: %s\n", builder.current_word->word);
@ -104,10 +107,14 @@ t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
else
cmdlist_builder_delimit(&builder, &words);
}
if (builder.error)
return (NULL);
ft_dprintf(STDERR_FILENO, "cmdlist builder error: %d\n", builder.error);
if (builder.current_wordlist != NULL)
cmdlist_builder_delimit(&builder, &words);
if (builder.error == true)
{
ft_dprintf(STDERR_FILENO, "cmdlist: returning null\n");
return (NULL);
}
return (builder.cmdlist);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */
/* Updated: 2025/02/24 15:03:25 by khais ### ########.fr */
/* Updated: 2025/03/04 12:07:13 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,8 +18,10 @@
** Parse a pipeline from the given wordlist.
**
** Return NULL and set ft_errno in case of error.
**
** Makes a copy of words
*/
t_pipeline *pipeline_from_wordlist(t_wordlist *words)
t_pipeline *pipeline_from_wordlist(const t_wordlist *words)
{
t_pipeline *pipeline;
t_pipeline_builder builder;
@ -34,7 +36,7 @@ t_pipeline *pipeline_from_wordlist(t_wordlist *words)
if (pipeline->cmds == NULL)
return (NULL);
ft_bzero(&builder, sizeof(t_pipeline_builder));
builder.words = words;
builder.words = wordlist_copy(words);
builder.pipeline = pipeline;
pipeline = pipeline_parse_wordlist(&builder);
return (pipeline);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:19:51 by khais #+# #+# */
/* Updated: 2025/02/24 15:02:35 by khais ### ########.fr */
/* Updated: 2025/03/03 13:36:43 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -91,7 +91,7 @@ typedef struct s_pipeline_builder
bool error;
} t_pipeline_builder;
t_pipeline *pipeline_from_wordlist(t_wordlist *words);
t_pipeline *pipeline_from_wordlist(const t_wordlist *words);
void pipeline_destroy(t_pipeline *pipeline);
void builder_destroy(t_pipeline_builder *builder);

View file

@ -6,11 +6,12 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 14:36:43 by khais #+# #+# */
/* Updated: 2025/02/28 13:13:38 by khais ### ########.fr */
/* Updated: 2025/03/04 12:21:38 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipeline_parse.h"
#include "ft_printf.h"
#include "pipeline.h"
#include "pipeline_parse_baseops.h"
#include "../../ft_errno.h"
@ -27,7 +28,7 @@
** Note that this does not handle errors such as repeated pipe tokens, or pipe
** tokens at the wrong place.
*/
int pipeline_count_cmds(t_wordlist *words)
int pipeline_count_cmds(const t_wordlist *words)
{
int count;
@ -106,6 +107,7 @@ t_pipeline *pipeline_parse_wordlist(t_pipeline_builder *builder)
return (builder_destroy(builder), ft_errno(FT_EUNEXPECTED_PIPE), NULL);
while (!eof(builder))
{
ft_dprintf(STDERR_FILENO, "pipeline parse: current word: [%s]\n", builder->current_word);
if (current_is_pipe(builder))
{
pipeline_delimit(builder);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 14:34:55 by khais #+# #+# */
/* Updated: 2025/02/24 15:01:46 by khais ### ########.fr */
/* Updated: 2025/03/04 12:07:12 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
# include "pipeline.h"
int pipeline_count_cmds(t_wordlist *words);
int pipeline_count_cmds(const t_wordlist *words);
t_pipeline *pipeline_parse_wordlist(t_pipeline_builder *builder);
#endif // PIPELINE_PARSE_H

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:20:36 by khais #+# #+# */
/* Updated: 2025/03/09 12:35:25 by khais ### ########.fr */
/* Updated: 2025/03/09 12:35:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,3 +52,22 @@ void worddesc_destroy(t_worddesc *worddesc)
free(worddesc->marker);
free(worddesc);
}
// copy the given worddesc, including all internal data
//
// return null on error
t_worddesc *worddesc_copy(t_worddesc *worddesc)
{
t_worddesc *out;
if (worddesc == NULL)
return (NULL);
out = ft_calloc(1, sizeof(t_worddesc));
if (out == NULL)
return (NULL);
out->flags = worddesc->flags;
out->word = ft_strdup(worddesc->word);
if (out->word == NULL)
return (free(out), NULL);
return (out);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:47:58 by khais #+# #+# */
/* Updated: 2025/03/06 17:21:07 by khais ### ########.fr */
/* Updated: 2025/03/09 12:36:05 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,5 +57,6 @@ typedef struct s_worddesc
t_worddesc *worddesc_create(char *word, char flags, char *marker);
void worddesc_destroy(t_worddesc *worddesc);
t_worddesc *worddesc_copy(t_worddesc *worddesc);
#endif

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
/* Updated: 2025/02/24 18:14:37 by khais ### ########.fr */
/* Updated: 2025/03/04 12:07:46 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,5 +40,6 @@ t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
t_worddesc *wordlist_pop(t_wordlist **wordlist);
void wordlist_debug(t_wordlist *wordlist);
t_wordlist *wordlist_copy(const t_wordlist *wordlist);
#endif

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wordlist_copy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/04 12:07:53 by khais #+# #+# */
/* Updated: 2025/03/04 12:15:38 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include "libft.h"
#include <stdlib.h>
// Make a copy of the given wordlist, copying all internal data as well.
//
// Return null in case of error.
t_wordlist *wordlist_copy(const t_wordlist *wordlist)
{
t_wordlist *outlist;
if (wordlist == NULL)
return (NULL);
outlist = ft_calloc(1, sizeof(t_wordlist));
outlist->word = worddesc_copy(wordlist->word);
while (wordlist != NULL)
{
wordlist = wordlist->next;
}
return (outlist);
}