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

@ -44,6 +44,7 @@ srcs = \
src/parser/simple_cmd/simple_cmd.c \ src/parser/simple_cmd/simple_cmd.c \
src/parser/worddesc/worddesc.c \ src/parser/worddesc/worddesc.c \
src/parser/wordlist/wordlist.c \ src/parser/wordlist/wordlist.c \
src/parser/wordlist/wordlist_copy.c \
src/parser/wordlist/wordlist_debug.c \ src/parser/wordlist/wordlist_debug.c \
src/parser/wordsplit/rule_utils.c \ src/parser/wordsplit/rule_utils.c \
src/parser/wordsplit/tokenizing_1_5.c \ src/parser/wordsplit/tokenizing_1_5.c \

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */ /* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
/* Updated: 2025/03/03 13:02:45 by khais ### ########.fr */ /* Updated: 2025/03/04 12:44:07 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -186,7 +186,6 @@ void *free2(void *p1, void *p2);
else \ else \
fprintf(stderr, "malloc at(%p) at %s:%d\n", \ fprintf(stderr, "malloc at(%p) at %s:%d\n", \
ret, __FILE__, __LINE__); \ ret, __FILE__, __LINE__); \
print_backtrace(); \
ret; \ ret; \
}) })
@ -199,7 +198,6 @@ void *free2(void *p1, void *p2);
else \ else \
fprintf(stderr, "ft_calloc at(%p) at %s:%d\n", \ fprintf(stderr, "ft_calloc at(%p) at %s:%d\n", \
ret, __FILE__, __LINE__); \ ret, __FILE__, __LINE__); \
print_backtrace(); \
ret; \ ret; \
}) })
@ -207,7 +205,6 @@ void *free2(void *p1, void *p2);
({ \ ({ \
fprintf(stderr, "free(%p) at %s:%d\n", \ fprintf(stderr, "free(%p) at %s:%d\n", \
ptr, __FILE__, __LINE__); \ ptr, __FILE__, __LINE__); \
print_backtrace(); \
free(ptr); \ free(ptr); \
}) })

View file

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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */ /* 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. ** Parse a pipeline from the given wordlist.
** **
** Return NULL and set ft_errno in case of error. ** 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 *pipeline;
t_pipeline_builder builder; t_pipeline_builder builder;
@ -34,7 +36,7 @@ t_pipeline *pipeline_from_wordlist(t_wordlist *words)
if (pipeline->cmds == NULL) if (pipeline->cmds == NULL)
return (NULL); return (NULL);
ft_bzero(&builder, sizeof(t_pipeline_builder)); ft_bzero(&builder, sizeof(t_pipeline_builder));
builder.words = words; builder.words = wordlist_copy(words);
builder.pipeline = pipeline; builder.pipeline = pipeline;
pipeline = pipeline_parse_wordlist(&builder); pipeline = pipeline_parse_wordlist(&builder);
return (pipeline); return (pipeline);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:19:51 by khais #+# #+# */ /* 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; bool error;
} t_pipeline_builder; } 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 pipeline_destroy(t_pipeline *pipeline);
void builder_destroy(t_pipeline_builder *builder); void builder_destroy(t_pipeline_builder *builder);

View file

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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 14:34:55 by khais #+# #+# */ /* 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" # 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); t_pipeline *pipeline_parse_wordlist(t_pipeline_builder *builder);
#endif // PIPELINE_PARSE_H #endif // PIPELINE_PARSE_H

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:20:36 by khais #+# #+# */ /* 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->marker);
free(worddesc); 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> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:47:58 by khais #+# #+# */ /* 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); t_worddesc *worddesc_create(char *word, char flags, char *marker);
void worddesc_destroy(t_worddesc *worddesc); void worddesc_destroy(t_worddesc *worddesc);
t_worddesc *worddesc_copy(t_worddesc *worddesc);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */ /* 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_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
t_worddesc *wordlist_pop(t_wordlist **wordlist); t_worddesc *wordlist_pop(t_wordlist **wordlist);
void wordlist_debug(t_wordlist *wordlist); void wordlist_debug(t_wordlist *wordlist);
t_wordlist *wordlist_copy(const t_wordlist *wordlist);
#endif #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);
}