2025-02-24 17:52:05 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* command_list.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
|
2025-02-25 15:24:52 +01:00
|
|
|
/* Updated: 2025/02/26 12:51:57 by khais ### ########.fr */
|
2025-02-24 17:52:05 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "command_list.h"
|
2025-02-24 18:18:15 +01:00
|
|
|
#include "libft.h"
|
2025-02-24 17:52:05 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2025-02-25 12:53:12 +01:00
|
|
|
/*
|
|
|
|
|
** 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);
|
2025-02-25 12:39:19 +01:00
|
|
|
if (ft_strcmp("||", op) == 0)
|
|
|
|
|
return (OP_OR);
|
2025-02-25 12:53:12 +01:00
|
|
|
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.
|
|
|
|
|
*/
|
2025-02-25 15:15:23 +01:00
|
|
|
static t_cmdlist *allocate_command_list(t_wordlist *words)
|
2025-02-25 12:53:12 +01:00
|
|
|
{
|
2025-02-25 15:15:23 +01:00
|
|
|
t_cmdlist *output;
|
2025-02-25 12:53:12 +01:00
|
|
|
|
|
|
|
|
if (words == NULL)
|
|
|
|
|
return (NULL);
|
2025-02-25 15:15:23 +01:00
|
|
|
output = ft_calloc(1, sizeof(t_cmdlist));
|
2025-02-25 12:53:12 +01:00
|
|
|
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);
|
2025-02-25 15:02:23 +01:00
|
|
|
output->operators
|
|
|
|
|
= ft_calloc(output->num_pipelines, sizeof(t_operator));
|
|
|
|
|
if (output->operators == NULL)
|
|
|
|
|
return (free(output->pipelines), free(output), NULL);
|
2025-02-25 12:53:12 +01:00
|
|
|
return (output);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 15:24:52 +01:00
|
|
|
/*
|
|
|
|
|
** 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++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 12:53:12 +01:00
|
|
|
/*
|
|
|
|
|
** Create a new command list from the given wordlist.
|
|
|
|
|
*/
|
2025-02-25 15:15:23 +01:00
|
|
|
t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
|
2025-02-24 17:52:05 +01:00
|
|
|
{
|
2025-02-25 15:24:52 +01:00
|
|
|
t_cmdlist_builder builder;
|
2025-02-24 18:18:15 +01:00
|
|
|
|
2025-02-25 15:24:52 +01:00
|
|
|
if (setup_cmdlist_builder(&builder, &words) == NULL)
|
2025-02-25 12:53:12 +01:00
|
|
|
return (NULL);
|
2025-02-25 15:24:52 +01:00
|
|
|
while (builder.current_word != NULL)
|
2025-02-24 18:18:15 +01:00
|
|
|
{
|
2025-02-25 15:24:52 +01:00
|
|
|
if (match_op(builder.current_word->word) == OP_INVALID)
|
|
|
|
|
cmdlist_builder_next_word(&builder, &words);
|
2025-02-25 13:46:11 +01:00
|
|
|
else
|
2025-02-25 15:24:52 +01:00
|
|
|
cmdlist_builder_delimit(&builder, &words);
|
2025-02-24 18:18:15 +01:00
|
|
|
}
|
2025-02-25 15:24:52 +01:00
|
|
|
if (builder.current_wordlist != NULL)
|
|
|
|
|
cmdlist_builder_delimit(&builder, &words);
|
|
|
|
|
return (builder.cmdlist);
|
2025-02-24 17:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 12:53:12 +01:00
|
|
|
/*
|
|
|
|
|
** destroy the given command list and all associated memory
|
|
|
|
|
*/
|
2025-02-25 15:15:23 +01:00
|
|
|
void cmdlist_destroy(t_cmdlist *cmd)
|
2025-02-24 17:52:05 +01:00
|
|
|
{
|
2025-02-25 12:53:12 +01:00
|
|
|
int i;
|
|
|
|
|
|
2025-02-24 18:18:15 +01:00
|
|
|
if (cmd == NULL)
|
|
|
|
|
return ;
|
2025-02-25 12:53:12 +01:00
|
|
|
i = 0;
|
|
|
|
|
while (i < cmd->num_pipelines)
|
|
|
|
|
{
|
|
|
|
|
pipeline_destroy(cmd->pipelines[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
free(cmd->pipelines);
|
2025-02-25 15:02:23 +01:00
|
|
|
free(cmd->operators);
|
2025-02-24 18:18:15 +01:00
|
|
|
free(cmd);
|
2025-02-24 17:52:05 +01:00
|
|
|
}
|