minishell/src/parser/remove_quotes/cmdgroup_remove_quotes.c

112 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}