aboutsummaryrefslogtreecommitdiff
path: root/src/collect_redirs.c
blob: 9ac1605ef413f7bf57d39ce75613e524d66bd7d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   collect_redirs.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/08/02 13:49:31 by dkaiser           #+#    #+#             */
/*   Updated: 2024/09/17 19:48:48 by dkaiser          ###   ########.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		set_redir(t_redirection *redir, int type, char *specifier);
static int		is_output_redir(int i);

t_redirection	*collect_redirs(t_token **tokens)
{
	t_redirection	*result;
	t_token			*cur;

	cur = *tokens;
	result = malloc(sizeof(t_redirection) * 2);
	if (result == NULL)
		return (free_tokens(*tokens), NULL);
	set_redir(&result[0], 0, NULL);
	set_redir(&result[1], 0, NULL);
	while (cur != NULL && cur->next != NULL)
	{
		if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
			collect_and_check_redir(tokens, result, &cur);
		else if (cur->type == REDIR_TOKEN)
			return (free(result), NULL);
		else
			cur = cur->next;
	}
	if (cur && cur->type == REDIR_TOKEN)
		return (free(result), NULL);
	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);
	}
}