-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdr.c
105 lines (94 loc) · 2.69 KB
/
rdr.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rdr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: btaha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/07 16:25:03 by btaha #+# #+# */
/* Updated: 2023/01/12 21:31:05 by btaha ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/minishell.h"
int check_expand(char *line)
{
int i;
i = 0;
while (line[i])
{
if (line[i++] == '$')
return (1);
}
return (0);
}
char *expand(t_lexer *lexer)
{
char *str;
char *var_name;
var_name = get_var_name(lexer);
if (!ft_strcmp(var_name, "?"))
str = ft_itoa(g_shell.exit);
else
str = ft_getenv(var_name);
return (str);
}
static int write_heredoc_line(char *file, int fd, char *line)
{
t_lexer *exp;
char *nadi;
exp = init_lexer(line);
nadi = "";
if (!line)
return (close(fd), unlink(file), free(file), free(line), -1);
while (exp->c)
{
if (exp->c == '$')
nadi = ft_strjoin(nadi, expand(exp));
else
nadi = ft_strjoin(nadi, &exp->c);
lexer_advance(exp);
}
if (ft_strlen(nadi) > 0)
return (ft_putstr_fd(nadi, fd), ft_putchar_fd('\n', fd), free(nadi), 0);
return (ft_putstr_fd(nadi, fd), ft_putchar_fd('\n', fd), 0);
}
static int read_heredoc(char *file, char *delime)
{
char *line;
int fd;
rl_event_hook = event;
signal(SIGINT, handler);
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
return (ft_putstr_fd("minishell: ", 2), ft_putstr_fd(file, 2),
ft_putstr_fd(": ", 2), perror(NULL), fd);
while (1)
{
line = readline("> ");
if (!line)
return (0);
if (g_shell.exec == -1)
return (0);
if (!strcmp(line, delime))
break ;
if (write_heredoc_line(file, fd, line))
return (-1);
free(line);
}
return (free(line), close(fd), 0);
}
char *filename(char *str)
{
static int n = 0;
char *n_str;
char *delim;
char *file;
n_str = ft_itoa(n);
delim = ft_substr(str, 0, ft_strlen(str));
if (n_str)
file = ft_strjoin("/tmp/minishell-heredoc-", n_str);
free(n_str);
if (!delim || !n_str || !file || read_heredoc(file, delim))
return (perror("minishell"), free(delim), free(file), NULL);
return (free(delim), n++, file);
}