postprocessing: do quote removal

This commit is contained in:
Khaïs COLIN 2025-03-20 17:34:07 +01:00
parent 22b0b4746f
commit f07a80c762
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 141 additions and 3 deletions

View file

@ -46,9 +46,10 @@ srcs = \
src/parser/matchers/pipe.c \ src/parser/matchers/pipe.c \
src/parser/matchers/quote.c \ src/parser/matchers/quote.c \
src/parser/pipeline/pipeline.c \ src/parser/pipeline/pipeline.c \
src/parser/pipeline/pipeline_parse.c \
src/parser/pipeline/pipeline_debug.c \ src/parser/pipeline/pipeline_debug.c \
src/parser/pipeline/pipeline_parse.c \
src/parser/pipeline/pipeline_parse_baseops.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/remove_quotes/remove_quotes.c \
src/parser/simple_cmd/simple_cmd.c \ src/parser/simple_cmd/simple_cmd.c \
src/parser/worddesc/worddesc.c \ src/parser/worddesc/worddesc.c \

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 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/wordlist/wordlist.h"
#include "parser/wordsplit/wordsplit.h" #include "parser/wordsplit/wordsplit.h"
#include "postprocess/redirections/redirection_parsing.h" #include "postprocess/redirections/redirection_parsing.h"
#include "parser/remove_quotes/remove_quotes.h"
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -53,11 +54,14 @@ static void debug_command(t_cmdgroup *cmd)
** **
** Currently, this is the following: ** Currently, this is the following:
** 1. redirection parsing ** 1. redirection parsing
** 2. quote removal
*/ */
static t_cmdgroup *post_process_command(t_cmdgroup *cmd) static t_cmdgroup *post_process_command(t_cmdgroup *cmd)
{ {
if (cmdgroup_parse_redirections(cmd) == NULL) if (cmdgroup_parse_redirections(cmd) == NULL)
return (cmdgroup_destroy(cmd), NULL); return (cmdgroup_destroy(cmd), NULL);
if (cmdgroup_remove_quotes(cmd) == NULL)
return (cmdgroup_destroy(cmd), NULL);
return (cmd); return (cmd);
} }

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_remove_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/28 13:51:03 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 # define REMOVE_QUOTES_H
# include "../worddesc/worddesc.h" # include "../worddesc/worddesc.h"
# include "../cmdgroup/cmdgroup.h"
t_worddesc *remove_quotes(t_worddesc *word); t_worddesc *remove_quotes(t_worddesc *word);
t_cmdgroup *cmdgroup_remove_quotes(t_cmdgroup *cmd);
#endif // REMOVE_QUOTES_H #endif // REMOVE_QUOTES_H

19
test.sh
View file

@ -291,6 +291,25 @@ expecting <<EOF
╰─ (no redirections) ╰─ (no redirections)
EOF EOF
when_run <<EOF "quotes are removed"
echo "this contains quotes"
EOF
expecting <<EOF
╰─ t_cmdgroup
├─ t_cmdlist
│ ├─ num_cmds = 1
│ ╰─ cmd[0]
│ ├─ t_cmdlist_item
│ │ ╰─ t_pipeline
│ │ ├─ num_cmd = 1
│ │ ╰─ cmd[0]
│ │ ╰─ t_simple_cmd
│ │ ├─ words = [echo][this contains quotes]
│ │ ╰─ (no redirections)
│ ╰─ t_operator = END
╰─ (no redirections)
EOF
when_run <<EOF "quoted parentheses are not operators" when_run <<EOF "quoted parentheses are not operators"
echo unclosed '(' echo unclosed '('
EOF EOF