mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
command list parse: handle single pipeline
This commit is contained in:
parent
3b36a77d34
commit
36fa53131a
3 changed files with 111 additions and 27 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
|
||||
/* Updated: 2025/02/24 18:58:48 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/25 13:29:39 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,40 +14,109 @@
|
|||
#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);
|
||||
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);
|
||||
return (output);
|
||||
}
|
||||
|
||||
/*
|
||||
** Create a new command list from the given wordlist.
|
||||
*/
|
||||
t_command_list *command_list_from_wordlist(t_wordlist *words)
|
||||
{
|
||||
t_command_list *output;
|
||||
t_wordlist *current_wordlist;
|
||||
t_worddesc *current_word;
|
||||
|
||||
output = NULL;
|
||||
if (words != NULL)
|
||||
output = allocate_command_list(words);
|
||||
if (output == NULL)
|
||||
return (NULL);
|
||||
current_wordlist = NULL;
|
||||
current_word = wordlist_pop(&words);
|
||||
while (current_word != NULL && match_op(current_word->word) == OP_INVALID)
|
||||
{
|
||||
output = ft_calloc(1, sizeof(t_command_list));
|
||||
if (output == NULL)
|
||||
return (NULL);
|
||||
current_wordlist = NULL;
|
||||
current_wordlist = wordlist_push(current_wordlist, current_word);
|
||||
current_word = wordlist_pop(&words);
|
||||
while (ft_strcmp("&&", current_word->word) != 0)
|
||||
{
|
||||
current_wordlist = wordlist_push(current_wordlist, current_word);
|
||||
current_word = wordlist_pop(&words);
|
||||
}
|
||||
worddesc_destroy(current_word);
|
||||
output->operator = OP_AND;
|
||||
wordlist_debug(current_wordlist);
|
||||
output->pipeline_left = pipeline_from_wordlist(current_wordlist);
|
||||
wordlist_debug(words);
|
||||
output->pipeline_right = pipeline_from_wordlist(words);
|
||||
}
|
||||
if (current_word != NULL)
|
||||
output->operator = match_op(current_word->word);
|
||||
worddesc_destroy(current_word);
|
||||
output->pipelines[0] = pipeline_from_wordlist(current_wordlist);
|
||||
if (words != NULL)
|
||||
output->pipelines[1] = pipeline_from_wordlist(words);
|
||||
return (output);
|
||||
}
|
||||
|
||||
/*
|
||||
** destroy the given command list and all associated memory
|
||||
*/
|
||||
void command_list_destroy(t_command_list *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (cmd == NULL)
|
||||
return ;
|
||||
pipeline_destroy(cmd->pipeline_left);
|
||||
pipeline_destroy(cmd->pipeline_right);
|
||||
i = 0;
|
||||
while (i < cmd->num_pipelines)
|
||||
{
|
||||
pipeline_destroy(cmd->pipelines[i]);
|
||||
i++;
|
||||
}
|
||||
free(cmd->pipelines);
|
||||
free(cmd);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/24 17:45:01 by khais #+# #+# */
|
||||
/* Updated: 2025/02/24 18:09:24 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/25 12:43:52 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -67,8 +67,8 @@ typedef enum e_operator
|
|||
*/
|
||||
typedef struct s_command_list
|
||||
{
|
||||
t_pipeline *pipeline_left;
|
||||
t_pipeline *pipeline_right;
|
||||
t_pipeline **pipelines;
|
||||
int num_pipelines;
|
||||
t_operator operator;
|
||||
} t_command_list;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue