validating redirects WIP
This commit is contained in:
parent
52c46ed40a
commit
46a39c17cb
@ -17,7 +17,7 @@
|
|||||||
t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value);
|
t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value);
|
||||||
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list);
|
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list);
|
||||||
t_list *redirect_get_outputs(t_minishell *minishell, t_list *list);
|
t_list *redirect_get_outputs(t_minishell *minishell, t_list *list);
|
||||||
int redirect_is_valid(t_list *lst, t_token *token);
|
int redirect_is_valid(t_list *lst, t_token *token, int mode);
|
||||||
int redirect_token_type(t_token *token);
|
int redirect_token_type(t_token *token);
|
||||||
int redirect_is_delimiter(t_token *token);
|
int redirect_is_delimiter(t_token *token);
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,8 @@ void simple_builtins(t_minishell *minishell);
|
|||||||
void error_msg(char *func, char *msg);
|
void error_msg(char *func, char *msg);
|
||||||
void check_malloc(t_minishell *minishell, void *ptr);
|
void check_malloc(t_minishell *minishell, void *ptr);
|
||||||
char *ft_strdup_safe(t_minishell *minishell, const char *str);
|
char *ft_strdup_safe(t_minishell *minishell, const char *str);
|
||||||
|
char *ft_substr_safe(t_minishell *minishell, const char *str,
|
||||||
|
unsigned int start, size_t len);
|
||||||
char *ft_strjoin_safe(t_minishell *minishell,
|
char *ft_strjoin_safe(t_minishell *minishell,
|
||||||
const char *s1, const char *s2);
|
const char *s1, const char *s2);
|
||||||
void *malloc_safe(t_minishell *minishell, size_t size);
|
void *malloc_safe(t_minishell *minishell, size_t size);
|
||||||
|
|||||||
40
sources.mk
40
sources.mk
@ -1,23 +1,23 @@
|
|||||||
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
|
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
|
||||||
SOURCES = history_load.c history_write.c prompt.c error_msg.c \
|
SOURCES = history_load.c history_write.c prompt.c ft_lstclear_safe.c \
|
||||||
ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \
|
ft_lstnew_safe.c init_minishell.c malloc_safe.c print_banner.c \
|
||||||
ft_strjoin_safe.c init_minishell.c malloc_safe.c print_banner.c \
|
free_minishell_line.c error_msg.c free_command_list.c \
|
||||||
check_malloc.c free_command_list.c free_freelist.c free_lexer.c \
|
ft_strdup_safe.c ft_strjoin_safe.c ft_substr_safe.c check_malloc.c \
|
||||||
free_minishell.c free_minishell_line.c free_safe.c free_token.c \
|
free_freelist.c free_lexer.c free_minishell.c free_safe.c \
|
||||||
free_token_list.c lexer_read_char.c lexer_new.c lexer_parse_input.c \
|
free_token.c free_token_list.c lexer_parse_input.c lexer_read_word.c \
|
||||||
lexer_read_word.c lexer_token_next.c token_new.c token_parse.c \
|
lexer_token_next.c lexer_read_char.c lexer_new.c token_parse.c \
|
||||||
environment_add.c environment_del.c environment_free.c \
|
token_new.c environment_free.c environment_free_list.c \
|
||||||
environment_free_list.c environment_get.c environment_get_arr.c \
|
environment_print.c environment_add.c environment_del.c \
|
||||||
environment_parse.c environment_print.c executor_close_fds.c \
|
environment_get.c environment_get_arr.c environment_parse.c \
|
||||||
executor_absolute_path.c executor_child.c executor_count_fds.c \
|
executor_close_fds.c executor_count_fds.c executor_create_pipes.c \
|
||||||
executor_create_pipes.c executor_create_redirects.c \
|
executor_create_redirects.c executor_execute_pipeline.c \
|
||||||
executor_execute_pipeline.c executor_fork.c executor_open_fds.c \
|
executor_fork.c executor_open_fds.c executor_absolute_path.c \
|
||||||
parser_get_arguments.c parser_get_commands.c parser_new_command.c \
|
executor_child.c parser_get_commands.c parser_get_arguments.c \
|
||||||
print_commands.c print_freelist.c expander_is_character.c \
|
parser_new_command.c print_commands.c print_freelist.c \
|
||||||
expander_expand_dollar.c expander_allocate_memory.c \
|
expander_is_character.c expander_allocate_memory.c \
|
||||||
expander_get_variable.c expander_parse_string.c \
|
expander_expand_dollar.c expander_get_variable.c \
|
||||||
expander_parse_variables.c builtin_echo.c builtin_router.c \
|
expander_parse_string.c expander_parse_variables.c builtin_echo.c \
|
||||||
simple_builtins.c is_builtin.c builtin_cd.c builtin_env.c \
|
builtin_router.c simple_builtins.c builtin_cd.c builtin_env.c \
|
||||||
builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \
|
builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \
|
||||||
signal.c redirect_valid_type.c redirect_get_inputs.c \
|
is_builtin.c signal.c redirect_valid_type.c redirect_get_inputs.c \
|
||||||
redirect_get_outputs.c redirect_new.c main.c \
|
redirect_get_outputs.c redirect_new.c main.c \
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* ::: :::::::: */
|
||||||
/* expander_get_variable.c :+: :+: */
|
/* expander_get_variable.c :+: :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
|
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/02/25 15:26:36 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/26 14:10:53 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (expander_character_valid(s[idx + i]))
|
while (expander_character_valid(s[idx + i]))
|
||||||
i++;
|
i++;
|
||||||
name = ft_substr(s, idx, i);
|
name = ft_substr_safe(minishell, s, idx, i);
|
||||||
if (!name || !*name)
|
if (!name || !*name)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
env = environment_get(minishell, name);
|
env = environment_get(minishell, name);
|
||||||
|
|||||||
@ -11,12 +11,29 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
static int is_command_token(t_token *token)
|
static int is_command_token(t_token *token)
|
||||||
{
|
{
|
||||||
return (token->type < 3 || redirect_token_type(token));
|
return (token->type < 3 || redirect_token_type(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
static int validate_redirects(t_list *lst)
|
||||||
|
{
|
||||||
|
t_list *token;
|
||||||
|
|
||||||
|
token = lst;
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
if (((t_token *)token)->type == T_ERROR)
|
||||||
|
return (0);
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
t_list *parser_get_commands(t_minishell *minishell)
|
t_list *parser_get_commands(t_minishell *minishell)
|
||||||
{
|
{
|
||||||
t_list *command_list;
|
t_list *command_list;
|
||||||
|
|||||||
@ -12,16 +12,20 @@
|
|||||||
|
|
||||||
#include "redirect.h"
|
#include "redirect.h"
|
||||||
|
|
||||||
static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim)
|
static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
|
char *expand;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
if (fd < 0) // TODO: Will this work?
|
if (fd < 0) // TODO: Will this work?
|
||||||
return ;
|
{
|
||||||
|
error_msg("heredoc", "unable to write to temp file");
|
||||||
|
return (fd);
|
||||||
|
}
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE && fd > 0)
|
||||||
{
|
{
|
||||||
line = readline(">");
|
line = readline(">");
|
||||||
if ((*line && ft_strcmp(line, delim->value) == 0) || !line) // TODO: What if not line?
|
if ((*line && ft_strcmp(line, delim->value) == 0) || !line) // TODO: What if not line?
|
||||||
@ -29,11 +33,18 @@ static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim)
|
|||||||
if (!*line)
|
if (!*line)
|
||||||
ft_putendl_fd("", fd);
|
ft_putendl_fd("", fd);
|
||||||
else
|
else
|
||||||
ft_putendl_fd(line, fd);
|
{
|
||||||
|
expand = expander_parse_string(line, msh);
|
||||||
|
ft_putendl_fd(expand, fd);
|
||||||
|
free_safe(msh, (void **)&expand);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
free(line);
|
||||||
heredoc->type = T_REDIRECT_IN;
|
heredoc->type = T_REDIRECT_IN;
|
||||||
delim->value = ft_strdup_safe(ms, ".ms_heredoc");
|
delim->value = ft_strdup_safe(msh, ".ms_heredoc");
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
|
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
|
||||||
@ -41,10 +52,12 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
|
|||||||
t_list *current;
|
t_list *current;
|
||||||
t_list *redirects;
|
t_list *redirects;
|
||||||
t_token *token;
|
t_token *token;
|
||||||
|
int result;
|
||||||
|
|
||||||
redirects = NULL;
|
redirects = NULL;
|
||||||
current = list;
|
current = list;
|
||||||
while (current)
|
result = 1;
|
||||||
|
while (current && result)
|
||||||
{
|
{
|
||||||
token = (t_token *)current->content;
|
token = (t_token *)current->content;
|
||||||
if (redirect_is_delimiter(token))
|
if (redirect_is_delimiter(token))
|
||||||
@ -54,9 +67,9 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
|
|||||||
current = current->next;
|
current = current->next;
|
||||||
continue ;
|
continue ;
|
||||||
}
|
}
|
||||||
if (token->type == T_HEREDOC && redirect_is_valid(current, token))
|
if (token->type == T_HEREDOC && redirect_is_valid(current, token, F_OK | W_OK))
|
||||||
process_heredoc(minishell, token, current->next->content);
|
result = process_heredoc(minishell, token, current->next->content);
|
||||||
if (redirect_is_valid(current, token))
|
if (redirect_is_valid(current, token, F_OK | R_OK))
|
||||||
{
|
{
|
||||||
ft_lstadd_back(&redirects, ft_lstnew_safe(minishell,
|
ft_lstadd_back(&redirects, ft_lstnew_safe(minishell,
|
||||||
redirect_new(minishell, token->type,
|
redirect_new(minishell, token->type,
|
||||||
@ -72,5 +85,9 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
|
|||||||
}
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
if (result < 0)
|
||||||
|
{
|
||||||
|
ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL)));
|
||||||
|
}
|
||||||
return (redirects);
|
return (redirects);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,7 @@ t_list *redirect_get_outputs(t_minishell *minishell, t_list *list)
|
|||||||
current = current->next;
|
current = current->next;
|
||||||
continue ;
|
continue ;
|
||||||
}
|
}
|
||||||
if (redirect_is_valid(current, token))
|
if (redirect_is_valid(current, token, F_OK | W_OK))
|
||||||
{
|
{
|
||||||
ft_lstadd_front(&redirects,
|
ft_lstadd_front(&redirects,
|
||||||
ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value))));
|
ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value))));
|
||||||
|
|||||||
@ -18,7 +18,7 @@ int redirect_token_type(t_token *token)
|
|||||||
token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT);
|
token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
int redirect_is_valid(t_list *lst, t_token *token)
|
int redirect_is_valid(t_list *lst, t_token *token, int mode)
|
||||||
{
|
{
|
||||||
t_token *next;
|
t_token *next;
|
||||||
|
|
||||||
@ -26,6 +26,10 @@ int redirect_is_valid(t_list *lst, t_token *token)
|
|||||||
return (0);
|
return (0);
|
||||||
next = (t_token *)lst->next->content;
|
next = (t_token *)lst->next->content;
|
||||||
if (!next)
|
if (!next)
|
||||||
|
{
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (!access(next->value, mode))
|
||||||
return (0);
|
return (0);
|
||||||
return (redirect_token_type(token) && next->type < 3);
|
return (redirect_token_type(token) && next->type < 3);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* ::: :::::::: */
|
||||||
/* free_minishell_line.c :+: :+: */
|
/* free_minishell_line.c :+: :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
|
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/25 17:40:32 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/26 14:24:20 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
23
src/utils/ft_substr_safe.c
Normal file
23
src/utils/ft_substr_safe.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* ft_strdup_safe.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/02/20 18:01:27 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/02/20 18:04:53 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *ft_substr_safe(t_minishell *minishell, const char *str,
|
||||||
|
unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
char *new_str;
|
||||||
|
|
||||||
|
new_str = ft_substr(str, start, len);
|
||||||
|
check_malloc(minishell, new_str);
|
||||||
|
return (new_str);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user