-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminishell.c
More file actions
104 lines (97 loc) · 2.69 KB
/
Copy pathminishell.c
File metadata and controls
104 lines (97 loc) · 2.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmembril <mmembril@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/21 08:47:30 by lpalomin #+# #+# */
/* Updated: 2025/09/26 17:25:27 by mmembril ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// My little envp 🌈🦄
static char **dup_envp(char **envp)
{
int env_count;
int count;
char **new_env;
if (!envp)
return (NULL);
count = 0;
while (envp[count])
count++;
new_env = malloc((count + 100) * sizeof(char *));
if (!new_env)
return (NULL);
env_count = 0;
while (env_count < count)
{
new_env[env_count] = ft_strdup(envp[env_count]);
if (!new_env[env_count])
return (free_split(new_env), NULL);
env_count++;
}
new_env[env_count] = NULL;
return (new_env);
}
static int init_cmd(t_cmd *cmd)
{
cmd->amount_cmd = 0;
cmd->argv = malloc(sizeof(char *) * 30);
if (!cmd->argv)
return (1);
cmd->argv[0] = NULL;
cmd->pipe_argv = NULL;
cmd->redirs = NULL;
return (0);
}
static void minishell_procedure(t_cmd *cmd, char *line, char **envp)
{
if (init_cmd(cmd))
return (free_cmd(cmd));
if (!check_unclosed_quotes(line))
{
free_cmd(cmd);
return (ft_putstr_fd("Error: unclosed quote\n", 2));
}
parse_line(cmd, line);
cmd->argv[cmd->amount_cmd] = NULL;
expand_dollars(cmd, envp);
if (cmd->amount_cmd == 0 || !cmd->argv[0] || cmd->argv[0][0] == '\0')
return (free_cmd(cmd));
parse_pipe_argv(cmd);
cmd->amount_cmd = count_pipes(cmd) + 1;
while (cmd->pipe_argv[cmd->amount_cmd])
cmd->amount_cmd++;
redirs_init(cmd);
parse_all_redirs(cmd);
prepare_heredocs(cmd, envp);
remove_quotes_pipe_argv(cmd);
execute_pipes(cmd, envp);
free_cmd(cmd);
}
int main(int argc, char **argv, char **envp)
{
char *line;
char **my_little_envp;
t_cmd cmd;
(void)argc;
(void)argv;
my_little_envp = dup_envp(envp);
init_signals();
cmd.exit_status = 0;
while (1)
{
line = readline("minishell$ ");
if (!line)
break ;
if (*line)
add_history(line);
minishell_procedure(&cmd, line, my_little_envp);
free(line);
}
free_envp(my_little_envp);
rl_clear_history();
return (0);
}