minishell/src/parser/command_list/command_list.c

178 lines
4.7 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
/* Updated: 2025/02/26 12:51:57 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "command_list.h"
#include "libft.h"
#include <stdlib.h>
/*
** Match a string to an operator
**
** If the string does not match any operator, return OP_INVALID
*/
static t_operator match_op(char *op)
{
if (ft_strcmp("&&", op) == 0)
return (OP_AND);
if (ft_strcmp("||", op) == 0)
return (OP_OR);
return (OP_INVALID);
}
/*
** Count the number of pipelines in the given wordstream
**
** Pipelines are separated by operators || and &&.
**
** Does not do error checking about repeated operators, or operators in the
** wrong place.
*/
static int command_list_count_pipelines(t_wordlist *words)
{
t_wordlist *current_word;
int count;
current_word = words;
if (current_word == NULL)
return (0);
count = 1;
while (current_word != NULL)
{
if (match_op(current_word->word->word) != OP_INVALID)
count++;
current_word = current_word->next;
}
return (count);
}
/*
** Allocate memory for a new command_list, given the input word stream.
**
** Handles malloc error.
*/
static t_cmdlist *allocate_command_list(t_wordlist *words)
{
t_cmdlist *output;
if (words == NULL)
return (NULL);
output = ft_calloc(1, sizeof(t_cmdlist));
if (output == NULL)
return (NULL);
output->num_pipelines = command_list_count_pipelines(words);
output->pipelines
= ft_calloc(output->num_pipelines, sizeof(t_pipeline *));
if (output->pipelines == NULL)
return (free(output), NULL);
output->operators
= ft_calloc(output->num_pipelines, sizeof(t_operator));
if (output->operators == NULL)
return (free(output->pipelines), free(output), NULL);
return (output);
}
/*
** setup a cmdlist_builder by allocating needed memory, and performing other
** initialization steps.
*/
static t_cmdlist_builder *setup_cmdlist_builder(
t_cmdlist_builder *builder,
t_wordlist **words)
{
ft_bzero(builder, sizeof(t_cmdlist_builder));
builder->cmdlist = allocate_command_list(*words);
if (builder->cmdlist == NULL)
return (NULL);
builder->current_wordlist = NULL;
builder->current_word = wordlist_pop(words);
builder->idx = 0;
return (builder);
}
/*
** Advance the state to the next word
*/
static void cmdlist_builder_next_word(
t_cmdlist_builder *builder,
t_wordlist **words)
{
builder->current_wordlist
= wordlist_push(builder->current_wordlist, builder->current_word);
builder->current_word = wordlist_pop(words);
}
/*
** return true if we are currently adding the last pipeline in the chain
*/
static bool cmdlist_builder_at_last_pipeline(t_cmdlist_builder *builder)
{
return (builder->idx == builder->cmdlist->num_pipelines - 1);
}
static void cmdlist_builder_delimit(
t_cmdlist_builder *builder,
t_wordlist **words)
{
builder->cmdlist->pipelines[builder->idx]
= pipeline_from_wordlist(builder->current_wordlist);
if (cmdlist_builder_at_last_pipeline(builder))
builder->cmdlist->operators[builder->idx] = OP_END;
else
builder->cmdlist->operators[builder->idx]
= match_op(builder->current_word->word);
builder->current_wordlist = NULL;
worddesc_destroy(builder->current_word);
builder->current_word = wordlist_pop(words);
builder->idx++;
}
/*
** Create a new command list from the given wordlist.
*/
t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
{
t_cmdlist_builder builder;
if (setup_cmdlist_builder(&builder, &words) == NULL)
return (NULL);
while (builder.current_word != NULL)
{
if (match_op(builder.current_word->word) == OP_INVALID)
cmdlist_builder_next_word(&builder, &words);
else
cmdlist_builder_delimit(&builder, &words);
}
if (builder.current_wordlist != NULL)
cmdlist_builder_delimit(&builder, &words);
return (builder.cmdlist);
}
/*
** destroy the given command list and all associated memory
*/
void cmdlist_destroy(t_cmdlist *cmd)
{
int i;
if (cmd == NULL)
return ;
i = 0;
while (i < cmd->num_pipelines)
{
pipeline_destroy(cmd->pipelines[i]);
i++;
}
free(cmd->pipelines);
free(cmd->operators);
free(cmd);
}