aboutsummaryrefslogtreecommitdiff
path: root/src/collect_redirs.c
blob: 4b7b955e38e98971573e52c526eacc0273a4b2f9 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   collect_redirs.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   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       */
/*                                                                            */
/* ************************************************************************** */

#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)
{
	char	*line;
	char	*result;
	char	*temp;
	size_t	total_length;
	size_t	line_length;

	total_length = 0;
	result = NULL;
	while (1)
	{
		line = readline("> ");
		if (!line || ft_strcmp(line, delimiter) == 0)
		{
			free(line);
			break ;
		}
		line_length = ft_strlen(line) + 1;
		temp = malloc(total_length + line_length + 1);
		if (!temp)
		{
			perror("malloc");
			free(result);
			return (NULL);
		}
		if (result)
		{
			ft_strcpy(temp, result);
			free(result);
		}
		else
		{
			temp[0] = '\0';
		}
		ft_strcat(temp, line);
		ft_strcat(temp, "\n");
		result = temp;
		total_length += line_length;
		free(line);
	}
	return (result);
}

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(result, &cur);// her is diff
		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);
// 	}
// }

//2.0

static void	collect_and_check_redir(t_redirection *result, t_token **cur)
{
	char	*heredoc_data;
	t_token	*next_token;

	heredoc_data = NULL;
	if ((*cur)->content.redir_type == INPUT_LIMITER)
	{
		// Handle Here Document (<<)
		heredoc_data = read_heredoc((*cur)->next->content.string);
		if (!heredoc_data)
		{
			perror("Heredoc allocation failed");
			return ;
		}
		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));
	}
	else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
	{
		// Handle Output File Overwrite (>)
		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
	next_token = (*cur)->next;
	free_token_and_connect(*cur);  // Free the current redirection token
	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
	}
	else
	{
		*cur = NULL;  // No more tokens
	}
}