aboutsummaryrefslogtreecommitdiff
path: root/src/collect_redirs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/collect_redirs.c')
-rw-r--r--src/collect_redirs.c132
1 files changed, 14 insertions, 118 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 4b7b955..84ebd71 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,50 +6,14 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 11:43:13 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 16:55:20 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-// static t_token *collect_redir(t_token **tokens, t_redirection *result,
-// t_token *cur);
-// static void collect_and_check_redir(t_token **tokens, t_redirection *result,
-// t_token **cur);
static void collect_and_check_redir(t_redirection *result, t_token **cur);
static void set_redir(t_redirection *redir, int type, char *specifier);
-// static int is_output_redir(int i);
-
-// static char *read_heredoc(char *delimiter)
-// {
-// char *line;
-// char *result;
-// size_t len;
-
-// result = NULL;
-// while (1)
-// {
-// line = readline("> ");
-// if (!line || ft_strcmp(line, delimiter) == 0)
-// {
-// free(line);
-// break ;
-// }
-// if (result)
-// len = ft_strlen(result);
-// else
-// len = 0;
-// result = realloc(result, len + ft_strlen(line) + 2);
-// if (!result)
-// return (NULL);
-// ft_strcpy(result + len, line);
-// strcat(result, "\n");
-// free(line);
-// }
-// return (result);
-// }
-
-//v2.0
static char *read_heredoc(char *delimiter)
{
@@ -74,8 +38,7 @@ static char *read_heredoc(char *delimiter)
if (!temp)
{
perror("malloc");
- free(result);
- return (NULL);
+ return (free(result), NULL);
}
if (result)
{
@@ -83,9 +46,7 @@ static char *read_heredoc(char *delimiter)
free(result);
}
else
- {
temp[0] = '\0';
- }
ft_strcat(temp, line);
ft_strcat(temp, "\n");
result = temp;
@@ -109,7 +70,7 @@ t_redirection *collect_redirs(t_token **tokens)
while (cur != NULL && cur->next != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur);// her is diff
+ collect_and_check_redir(result, &cur);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
@@ -120,62 +81,12 @@ t_redirection *collect_redirs(t_token **tokens)
return (result);
}
-// static void collect_and_check_redir(t_token **tokens, t_redirection *result,
-// t_token **cur)
-// {
-// int is_redir_only;
-
-// is_redir_only = 0;
-// if ((*cur)->previous == NULL && (*cur)->next->next == NULL)
-// is_redir_only = 1;
-// *cur = collect_redir(tokens, result, *cur);
-// if (is_redir_only)
-// *tokens = NULL;
-// }
-
-// static t_token *collect_redir(t_token **tokens, t_redirection *result,
-// t_token *cur)
-// {
-// set_redir(&result[is_output_redir(cur->content.redir_type)],
-// cur->content.redir_type, cur->next->content.string);
-// cur = cur->next;
-// free_token_and_connect(cur->previous);
-// if (cur->next != NULL)
-// {
-// if (cur->previous == NULL)
-// *tokens = cur->next;
-// cur = cur->next;
-// free_token_and_connect(cur->previous);
-// }
-// else
-// {
-// free_token(cur);
-// return (NULL);
-// }
-// return (cur);
-// }
-
static void set_redir(t_redirection *redir, int type, char *specifier)
{
redir->type = type;
redir->specifier = specifier;
}
-// static int is_output_redir(int i)
-// {
-// if (i & (INPUT_FILE | INPUT_LIMITER))
-// return (0);
-// else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
-// return (1);
-// else
-// {
-// panic(UNREACHABLE);
-// return (-1);
-// }
-// }
-
-//2.0
-
static void collect_and_check_redir(t_redirection *result, t_token **cur)
{
char *heredoc_data;
@@ -184,7 +95,6 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur)
heredoc_data = NULL;
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- // Handle Here Document (<<)
heredoc_data = read_heredoc((*cur)->next->content.string);
if (!heredoc_data)
{
@@ -194,35 +104,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur)
set_redir(&result[0], INPUT_LIMITER, heredoc_data);
}
else if ((*cur)->content.redir_type == INPUT_FILE)
- {
- // Handle Input File (<)
- set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string));
- }
+ set_redir(&result[0], INPUT_FILE,
+ ft_strdup((*cur)->next->content.string));
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
- {
- // Handle Output File Overwrite (>)
- set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string));
- }
+ set_redir(&result[1], OUTPUT_OVERRIDE,
+ ft_strdup((*cur)->next->content.string));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
- {
- // Handle Output File Append (>>)
- set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string));
- }
- else
- {
- // Handle unexpected cases
- printf("Unknown redirection type encountered\n");
- }
- // Advance the token pointer to skip the redirection token and its argument
+ set_redir(&result[1], OUTPUT_APPEND,
+ ft_strdup((*cur)->next->content.string));
next_token = (*cur)->next;
- free_token_and_connect(*cur); // Free the current redirection token
+ free_token_and_connect(*cur);
if (next_token)
{
- *cur = next_token->next; // Move to the next token after the argument
- free_token_and_connect(next_token); // Free the argument token
+ *cur = next_token->next;
+ free_token_and_connect(next_token);
}
else
- {
- *cur = NULL; // No more tokens
- }
-} \ No newline at end of file
+ *cur = NULL;
+}