-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishell.c
86 lines (80 loc) · 2.2 KB
/
minishell.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkassand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:27:46 by jkassand #+# #+# */
/* Updated: 2021/11/27 01:27:51 by jkassand ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int part1(char **str, t_arg *arg)
{
signal(SIGINT, handler);
signal(SIGQUIT, SIG_IGN);
*str = readline("minishell$ ");
if (*str == NULL)
{
write(1, "\033[Aminishell$ exit\n", \
ft_strlen("\033[Aminishell$ exit\n"));
rl_redisplay();
ft_exit(0, NULL, arg);
}
if ((*str)[0])
add_history(*str);
if (empty_str(*str))
{
free(*str);
return (1);
}
return (0);
}
void part2(char *str, t_arg *arg)
{
int fd;
parser(str, arg);
set_built(arg->cmd);
ft_env_list_to_array(arg->envp, &arg->env, arg);
red_to_cmd(arg->red, arg->cmd);
if (!pre_open(arg))
{
arg->num = count_cmd(arg->cmd);
if (arg->num > 1)
ft_pipe(arg);
if (arg->num == 1 && arg->cmd->built)
{
fd = dup_built(arg->cmd, arg);
arg->errnum = run_built(arg->cmd, arg);
redup_built(fd, arg);
}
else if (arg->num != 1 || arg->cmd->cmd[0])
pipex(arg);
}
}
int main(int argc, char **argv, char **env)
{
t_arg arg;
char *str;
(void)argv;
ft_init_structs(&arg);
if (argc != 1)
ft_exit(1, NULL, &arg);
env_parse(env, &arg.envp, &arg);
ft_shlvl_check(&arg);
while (1)
{
if (part1(&str, &arg))
continue ;
if (pre_parse(str) == 1)
{
write(2, "minishell: syntax error near unexpected token\n", 46);
arg.errnum = 258;
}
else
part2(str, &arg);
free_str(&arg, str);
}
ft_exit(arg.errnum, NULL, &arg);
}