From f07a80c762062512ff708d13f0a75c84577240ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 20 Mar 2025 17:34:07 +0100 Subject: [PATCH] postprocessing: do quote removal --- Makefile | 3 +- src/minishell.c | 6 +- .../remove_quotes/cmdgroup_remove_quotes.c | 112 ++++++++++++++++++ src/parser/remove_quotes/remove_quotes.h | 4 +- test.sh | 19 +++ 5 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 src/parser/remove_quotes/cmdgroup_remove_quotes.c diff --git a/Makefile b/Makefile index c59fd89..f729238 100644 --- a/Makefile +++ b/Makefile @@ -46,9 +46,10 @@ srcs = \ src/parser/matchers/pipe.c \ src/parser/matchers/quote.c \ src/parser/pipeline/pipeline.c \ - src/parser/pipeline/pipeline_parse.c \ src/parser/pipeline/pipeline_debug.c \ + src/parser/pipeline/pipeline_parse.c \ src/parser/pipeline/pipeline_parse_baseops.c \ + src/parser/remove_quotes/cmdgroup_remove_quotes.c \ src/parser/remove_quotes/remove_quotes.c \ src/parser/simple_cmd/simple_cmd.c \ src/parser/worddesc/worddesc.c \ diff --git a/src/minishell.c b/src/minishell.c index 6742ca9..3e87f9e 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/03/19 16:38:42 by khais ### ########.fr */ +/* Updated: 2025/03/20 17:36:58 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include "parser/wordlist/wordlist.h" #include "parser/wordsplit/wordsplit.h" #include "postprocess/redirections/redirection_parsing.h" +#include "parser/remove_quotes/remove_quotes.h" #include /* @@ -53,11 +54,14 @@ static void debug_command(t_cmdgroup *cmd) ** ** Currently, this is the following: ** 1. redirection parsing +** 2. quote removal */ static t_cmdgroup *post_process_command(t_cmdgroup *cmd) { if (cmdgroup_parse_redirections(cmd) == NULL) return (cmdgroup_destroy(cmd), NULL); + if (cmdgroup_remove_quotes(cmd) == NULL) + return (cmdgroup_destroy(cmd), NULL); return (cmd); } diff --git a/src/parser/remove_quotes/cmdgroup_remove_quotes.c b/src/parser/remove_quotes/cmdgroup_remove_quotes.c new file mode 100644 index 0000000..56f3373 --- /dev/null +++ b/src/parser/remove_quotes/cmdgroup_remove_quotes.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmdgroup_remove_quotes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 17:36:20 by khais #+# #+# */ +/* Updated: 2025/03/21 18:33:43 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "remove_quotes.h" +#include "../cmdlist/cmdlist_item.h" + +/* +** do quote removal on all words of this simple cmd +*/ +static t_simple_cmd *simple_cmd_remove_quotes(t_simple_cmd *cmd) +{ + t_wordlist *new; + t_worddesc *result; + t_worddesc *current; + + if (cmd == NULL) + return (NULL); + new = NULL; + current = wordlist_pop(&cmd->words); + while (current != NULL) + { + result = remove_quotes(current); + worddesc_destroy(current); + if (result == NULL) + return (NULL); + new = wordlist_push(new, result); + current = wordlist_pop(&cmd->words); + } + cmd->words = new; + return (cmd); +} + +/* +** do quote removal on all simple_cmds within this pipeline +*/ +static t_pipeline *pipeline_remove_quotes(t_pipeline *pipeline) +{ + int i; + + if (pipeline == NULL) + return (NULL); + i = 0; + while (i < pipeline->num_cmd) + { + if (simple_cmd_remove_quotes(pipeline->cmds[i]) == NULL) + return (NULL); + i++; + } + return (pipeline); +} + +/* +** do quote removal on the contained pipeline or cmdgroup +*/ +static t_cmdlist_item *cmdlist_item_remove_quotes(t_cmdlist_item *item) +{ + if (item == NULL) + return (NULL); + if (item->type == TYPE_PIPELINE) + { + if (pipeline_remove_quotes(item->inner.pipeline) == NULL) + return (NULL); + } + if (item->type == TYPE_CMDGROUP) + { + if (cmdgroup_remove_quotes(item->inner.cmdgroup) == NULL) + return (NULL); + } + return (item); +} + +/* +** iterate over all cmdlist_items within this cmdlist, and do quote removal on +** them +*/ +static t_cmdlist *cmdlist_remove_quotes(t_cmdlist *cmd) +{ + int i; + + if (cmd == NULL) + return (NULL); + i = 0; + while (i < cmd->num_cmd) + { + if (cmdlist_item_remove_quotes(cmd->cmds[i]) == NULL) + return (NULL); + i++; + } + return (cmd); +} + +/* +** Iterate over all simple_cmd contained within this cmdgroup, and do quote +** removal on them. +*/ +t_cmdgroup *cmdgroup_remove_quotes(t_cmdgroup *cmd) +{ + if (cmd == NULL) + return (NULL); + if (cmdlist_remove_quotes(cmd->item) == NULL) + return (NULL); + return (cmd); +} diff --git a/src/parser/remove_quotes/remove_quotes.h b/src/parser/remove_quotes/remove_quotes.h index 06805fa..dc5f8bc 100644 --- a/src/parser/remove_quotes/remove_quotes.h +++ b/src/parser/remove_quotes/remove_quotes.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/28 13:51:03 by khais #+# #+# */ -/* Updated: 2025/02/28 13:51:53 by khais ### ########.fr */ +/* Updated: 2025/03/20 17:36:11 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,9 @@ # define REMOVE_QUOTES_H # include "../worddesc/worddesc.h" +# include "../cmdgroup/cmdgroup.h" t_worddesc *remove_quotes(t_worddesc *word); +t_cmdgroup *cmdgroup_remove_quotes(t_cmdgroup *cmd); #endif // REMOVE_QUOTES_H diff --git a/test.sh b/test.sh index 5d3977e..352af17 100755 --- a/test.sh +++ b/test.sh @@ -291,6 +291,25 @@ expecting <