From 5cd755b73c1fb9bef52811b3d10b6098b4238750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 26 Feb 2025 14:07:55 +0100 Subject: [PATCH] wip: make a copy of words in pipeline_from_wordlist --- Makefile | 1 + libft/libft.h | 5 +--- src/parser/command_list/command_list.c | 19 ++++++++++----- src/parser/pipeline/pipeline.c | 8 ++++--- src/parser/pipeline/pipeline.h | 4 ++-- src/parser/pipeline/pipeline_parse.c | 6 +++-- src/parser/pipeline/pipeline_parse.h | 4 ++-- src/parser/worddesc/worddesc.c | 21 +++++++++++++++- src/parser/worddesc/worddesc.h | 3 ++- src/parser/wordlist/wordlist.h | 3 ++- src/parser/wordlist/wordlist_copy.c | 33 ++++++++++++++++++++++++++ 11 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 src/parser/wordlist/wordlist_copy.c diff --git a/Makefile b/Makefile index 1f682db..1c0bafe 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ srcs = \ src/parser/simple_cmd/simple_cmd.c \ src/parser/worddesc/worddesc.c \ src/parser/wordlist/wordlist.c \ + src/parser/wordlist/wordlist_copy.c \ src/parser/wordlist/wordlist_debug.c \ src/parser/wordsplit/rule_utils.c \ src/parser/wordsplit/tokenizing_1_5.c \ diff --git a/libft/libft.h b/libft/libft.h index 3a24099..c8c7e1e 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* 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 \ fprintf(stderr, "malloc at(%p) at %s:%d\n", \ ret, __FILE__, __LINE__); \ - print_backtrace(); \ ret; \ }) @@ -199,7 +198,6 @@ void *free2(void *p1, void *p2); else \ fprintf(stderr, "ft_calloc at(%p) at %s:%d\n", \ ret, __FILE__, __LINE__); \ - print_backtrace(); \ ret; \ }) @@ -207,7 +205,6 @@ void *free2(void *p1, void *p2); ({ \ fprintf(stderr, "free(%p) at %s:%d\n", \ ptr, __FILE__, __LINE__); \ - print_backtrace(); \ free(ptr); \ }) diff --git a/src/parser/command_list/command_list.c b/src/parser/command_list/command_list.c index 3829bf6..9e16d78 100644 --- a/src/parser/command_list/command_list.c +++ b/src/parser/command_list/command_list.c @@ -6,7 +6,7 @@ /* 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) 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); } diff --git a/src/parser/pipeline/pipeline.c b/src/parser/pipeline/pipeline.c index 2500c44..1d1880b 100644 --- a/src/parser/pipeline/pipeline.c +++ b/src/parser/pipeline/pipeline.c @@ -6,7 +6,7 @@ /* 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. ** ** 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); diff --git a/src/parser/pipeline/pipeline.h b/src/parser/pipeline/pipeline.h index bd57dfe..34ec836 100644 --- a/src/parser/pipeline/pipeline.h +++ b/src/parser/pipeline/pipeline.h @@ -6,7 +6,7 @@ /* 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; } 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); diff --git a/src/parser/pipeline/pipeline_parse.c b/src/parser/pipeline/pipeline_parse.c index d6199fc..49d1188 100644 --- a/src/parser/pipeline/pipeline_parse.c +++ b/src/parser/pipeline/pipeline_parse.c @@ -6,11 +6,12 @@ /* 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 "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); diff --git a/src/parser/pipeline/pipeline_parse.h b/src/parser/pipeline/pipeline_parse.h index 5b8ecdd..8bcad23 100644 --- a/src/parser/pipeline/pipeline_parse.h +++ b/src/parser/pipeline/pipeline_parse.h @@ -6,7 +6,7 @@ /* 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" -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 diff --git a/src/parser/worddesc/worddesc.c b/src/parser/worddesc/worddesc.c index f45db0d..08ab9f3 100644 --- a/src/parser/worddesc/worddesc.c +++ b/src/parser/worddesc/worddesc.c @@ -6,7 +6,7 @@ /* 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); } + +// 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); +} diff --git a/src/parser/worddesc/worddesc.h b/src/parser/worddesc/worddesc.h index 3d7b6b0..61218cc 100644 --- a/src/parser/worddesc/worddesc.h +++ b/src/parser/worddesc/worddesc.h @@ -6,7 +6,7 @@ /* 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); void worddesc_destroy(t_worddesc *worddesc); +t_worddesc *worddesc_copy(t_worddesc *worddesc); #endif diff --git a/src/parser/wordlist/wordlist.h b/src/parser/wordlist/wordlist.h index 055e885..1de3c22 100644 --- a/src/parser/wordlist/wordlist.h +++ b/src/parser/wordlist/wordlist.h @@ -6,7 +6,7 @@ /* 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_worddesc *wordlist_pop(t_wordlist **wordlist); void wordlist_debug(t_wordlist *wordlist); +t_wordlist *wordlist_copy(const t_wordlist *wordlist); #endif diff --git a/src/parser/wordlist/wordlist_copy.c b/src/parser/wordlist/wordlist_copy.c new file mode 100644 index 0000000..f8dcc2e --- /dev/null +++ b/src/parser/wordlist/wordlist_copy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* wordlist_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +// 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); +}