-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheredoc_utils_2.c
More file actions
105 lines (95 loc) · 2.72 KB
/
heredoc_utils_2.c
File metadata and controls
105 lines (95 loc) · 2.72 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_utils_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aylaaouf <aylaaouf@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/08 13:18:21 by ayelasef #+# #+# */
/* Updated: 2025/07/10 11:21:13 by aylaaouf ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void heredoc_child_process(int pipefd, t_heredoc_data *data)
{
char *line;
char *processed;
signal(SIGINT, heredoc_child_handler);
signal(SIGQUIT, SIG_IGN);
while (1)
{
line = readline("> ");
if (!line)
{
write_eof_warning(data->delim);
break ;
}
if (ft_strcmp(line, data->delim) == 0)
break ;
processed = maybe_expand(data->gc, line, data->expand, data->env);
write(pipefd, processed, ft_strlen(processed));
write(pipefd, "\n", 1);
free(line);
}
close(pipefd);
exit(0);
}
char *expand_variable(t_expand_data *data, size_t *i)
{
size_t start;
char *key;
char *val;
start = *i;
while (ft_isalnum(data->line[*i]) || data->line[*i] == '_')
(*i)++;
key = gc_strndup(data->gc, &data->line[start], *i - start);
val = get_env_value_heredoc(key, data->env);
data->result = gc_strjoin_free_a(data->gc, data->result, val);
return (data->result);
}
char *expand_line(t_gc *gc, char *line, t_env *env)
{
t_expand_data data;
size_t i;
data.gc = gc;
data.result = gc_strdup(gc, "");
data.line = line;
data.env = env;
i = 0;
while (line[i])
{
if (line[i] == '$' && line[i + 1])
{
i++;
if (line[i] == '?')
data.result = expand_status(gc, data.result, &i);
else
data.result = expand_variable(&data, &i);
}
else
{
data.result = ft_strjoin_char_gc(gc, data.result, line[i]);
i++;
}
}
return (data.result);
}
char *maybe_expand(t_gc *gc, char *line, int expand, t_env *env)
{
if (!expand)
return (gc_strdup(gc, line));
return (expand_line(gc, line, env));
}
void write_eof_warning(char *delim)
{
char *msg1;
char *msg2;
char *msg3;
msg1 = "minishell: warning: here-document ";
msg2 = "delimited by end-of-file (wanted `";
msg3 = "`)\n";
write(2, msg1, ft_strlen(msg1));
write(2, msg2, ft_strlen(msg2));
write(2, delim, ft_strlen(delim));
write(2, msg3, ft_strlen(msg3));
}