some more structure

This commit is contained in:
Quinten 2025-02-23 12:34:34 +01:00
parent 45968e547e
commit 3c14325711
8 changed files with 129 additions and 63 deletions

View File

@ -3,10 +3,10 @@
# :::::::: #
# Makefile :+: :+: #
# +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ #
# By: marvin <marvin@student.42.fr> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl #
# Updated: 2025/02/23 12:28:40 by Quinten ######## odam.nl #
# #
# **************************************************************************** #
@ -24,6 +24,7 @@ OBJ_PATH = obj
VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:
VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin
VPATH += src/redirect
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -3,10 +3,10 @@
/* :::::::: */
/* minishell.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* By: marvin <marvin@student.42.fr> +#+ */
/* +#+ */
/* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:35:24 by whaffman ######## odam.nl */
/* Updated: 2025/02/23 12:28:23 by Quinten ######## odam.nl */
/* */
/* ************************************************************************** */
@ -24,6 +24,7 @@
# include "executor.h"
# include "parser.h"
# include "expander.h"
# include "redirect.h"
# include "debug.h"
# include "utils.h"

20
inc/redirect.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* redirect.h :+: :+: */
/* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:26:29 by Quinten ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef REDIRECT_H
# define REDIRECT_H
# include "minishell.h"
t_redirect *redirect_new(t_token_type type, char *value);
t_list *redirect_get_inputs(t_list *list);
int redirect_is_valid(t_list *lst, t_token *token);
#endif

View File

@ -28,7 +28,7 @@ t_list *parser_get_commands(t_minishell *minishell)
token = (t_token *) current->content;
command = parser_command_new(ft_strdup(token->value));
command->args = parser_get_arguments(current, minishell);
command->redirect_in = parser_get_input_redirects(current);
command->redirect_in = redirect_get_inputs(current);
ft_lstadd_back(&command_list, ft_lstnew(command));
while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN))
current = current->next;

View File

@ -1,58 +0,0 @@
# include "minishell.h"
static int valid_def(t_list *list, t_token *token)
{
t_token *next;
if (!list->next)
return (0);
next = (t_token *)list->next->content;
if (!next)
return (0);
return ((token->type == T_REDIRECT_IN || token->type == T_HEREDOC) && next->type < 3);
}
static t_redirect *redirect_new(t_token_type type, char *value)
{
t_redirect *result;
result = ft_malloc_safe(sizeof(t_redirect));
result->type = type;
result->value = NULL;
if (value)
result->value = value;
return (result);
}
t_list *parser_get_input_redirects(t_list *list)
{
t_list *current;
t_list *redirects;
t_token *token;
redirects = NULL;
current = list;
while (current)
{
token = (t_token *)current->content;
if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
{
current = current->next;
continue ;
}
if (valid_def(current, token))
{
ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value))));
current = current->next;
continue ;
}
else
{
ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL)));
break ;
}
current = current->next;
}
ft_lstiter(redirects, print_redirects);
return (redirects);
}

View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* redirect_get_inputs.c :+: :+: */
/* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */
/* */
/* ************************************************************************** */
# include "redirect.h"
t_list *redirect_get_inputs(t_list *list)
{
t_list *current;
t_list *redirects;
t_token *token;
redirects = NULL;
current = list;
while (current)
{
token = (t_token *)current->content;
if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
{
current = current->next;
continue ;
}
if (redirect_is_valid(current, token))
{
ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value))));
current = current->next;
continue ;
}
else
{
ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL)));
break ;
}
current = current->next;
}
ft_lstiter(redirects, print_redirects);
return (redirects);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* redirect_new.c :+: :+: */
/* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:27:33 by Quinten ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_redirect *redirect_new(t_token_type type, char *value)
{
t_redirect *result;
result = ft_malloc_safe(sizeof(t_redirect));
result->type = type;
result->value = NULL;
if (value)
result->value = value;
return (result);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* redirect_valid_type.c :+: :+: */
/* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int is_redirection(t_token *token)
{
return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC ||
token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT);
}
int redirect_is_valid(t_list *lst, t_token *token)
{
t_token *next;
if (!lst->next)
return (0);
next = (t_token *)lst->next->content;
if (!next)
return (0);
return (is_redirection(token) && next->type < 3);
}