forked from waelbt/minishell_
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
109 lines (99 loc) · 2.48 KB
/
main.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
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: waboutzo <waboutzo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/24 18:48:37 by waboutzo #+# #+# */
/* Updated: 2022/08/13 20:16:45 by waboutzo ### ########.fr */
/* */
/* ************************************************************************** */
#include "src/include/minishell.h"
void *ft_free(t_token *token, t_lexer *lexer, t_node *node, t_node *tmp)
{
if (!node)
free_node(&tmp);
free(lexer);
free(token->value);
free(token);
return (node);
}
int ft_pipe_check(t_token *token, t_token *previous)
{
int value;
value = 0;
if (ft_strchr(token->value, '|'))
{
ft_setter(258);
printf_error(NULL, "parse error near `||'\n", NULL);
}
else if (previous->e_type == TOKEN_PIPE && token->e_type == TOKEN_EOF)
{
ft_setter(258);
printf_error(NULL, "parse error near `|'\n", NULL);
}
else
value = 1;
free(previous->value);
free(previous);
return (value);
}
void ft_unlik(int *index)
{
char *tmp[2];
while (*index > -1)
{
tmp[0] = ft_itoa(*index);
tmp[1] = ft_strjoin("/var/TMP/her_doc", tmp[0]);
unlink(tmp[1]);
free(tmp[0]);
free(tmp[1]);
(*index)--;
}
*index = 0;
}
int minishell(char ***env)
{
char *str;
t_node *cmd;
int index;
signal(SIGINT, sig_handler);
signal(SIGQUIT, SIG_IGN);
str = readline("minishell$ ");
if (!str)
return (0);
if (ft_strcmp(str, ""))
add_history (str);
cmd = handler(init_lexer(str));
if (parsing(&cmd, *env, &index))
{
if (ft_lstsize(cmd) == 1)
execution_cmd(cmd, env);
else if (ft_lstsize(cmd) > 1)
execution_multi_cmds(cmd, *env);
}
ft_unlik(&index);
free_node(&cmd);
free(str);
return (1);
}
int main(int argc, char **argv, char **envp)
{
char **env;
if (argc == 1)
{
(void) argv;
env = my_envp(envp);
while (1)
{
if (!minishell(&env))
break ;
}
free(g_cwd_saver);
write(1, "exit\n", 5);
free_double_char(env, 0);
exit(ft_getter());
}
return (0);
}