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:02:23 +01:00
|
|
|
/* Updated: 2025/02/25 15:08:48 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-25 13:46:11 +01:00
|
|
|
#include "unistd.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.
|
|
|
|
|
*/
|
|
|
|
|
static t_command_list *allocate_command_list(t_wordlist *words)
|
|
|
|
|
{
|
|
|
|
|
t_command_list *output;
|
|
|
|
|
|
|
|
|
|
if (words == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
output = ft_calloc(1, sizeof(t_command_list));
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Create a new command list from the given wordlist.
|
|
|
|
|
*/
|
2025-02-24 17:52:05 +01:00
|
|
|
t_command_list *command_list_from_wordlist(t_wordlist *words)
|
|
|
|
|
{
|
2025-02-24 18:18:15 +01:00
|
|
|
t_command_list *output;
|
|
|
|
|
t_wordlist *current_wordlist;
|
|
|
|
|
t_worddesc *current_word;
|
2025-02-25 13:46:11 +01:00
|
|
|
int idx;
|
2025-02-24 18:18:15 +01:00
|
|
|
|
2025-02-25 12:53:12 +01:00
|
|
|
output = allocate_command_list(words);
|
|
|
|
|
if (output == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
current_wordlist = NULL;
|
|
|
|
|
current_word = wordlist_pop(&words);
|
2025-02-25 13:46:11 +01:00
|
|
|
idx = 0;
|
|
|
|
|
while (current_word != NULL)
|
2025-02-24 18:18:15 +01:00
|
|
|
{
|
2025-02-25 13:46:11 +01:00
|
|
|
if (match_op(current_word->word) == OP_INVALID)
|
|
|
|
|
{
|
|
|
|
|
current_wordlist = wordlist_push(current_wordlist, current_word);
|
|
|
|
|
current_word = wordlist_pop(&words);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
2025-02-25 15:02:23 +01:00
|
|
|
output->operators[idx] = match_op(current_word->word);
|
2025-02-25 13:46:11 +01:00
|
|
|
current_wordlist = NULL;
|
|
|
|
|
worddesc_destroy(current_word);
|
|
|
|
|
current_word = wordlist_pop(&words);
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
2025-02-24 18:18:15 +01:00
|
|
|
}
|
2025-02-25 13:46:11 +01:00
|
|
|
if (current_wordlist != NULL)
|
2025-02-25 15:02:23 +01:00
|
|
|
{
|
2025-02-25 13:46:11 +01:00
|
|
|
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
2025-02-25 15:02:23 +01:00
|
|
|
output->operators[idx] = OP_END;
|
|
|
|
|
}
|
2025-02-24 18:18:15 +01:00
|
|
|
return (output);
|
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-24 17:52:05 +01:00
|
|
|
void command_list_destroy(t_command_list *cmd)
|
|
|
|
|
{
|
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
|
|
|
}
|