generated from jos-felipe/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
90 lines (81 loc) · 2.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: josfelip <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 17:09:14 by gfantoni #+# #+# */
/* Updated: 2024/09/06 17:22:43 by josfelip ### ########.fr */
/* */
/* ************************************************************************** */
#include "./include/minishell.h"
static char *mini_pwd_prompt(t_mini *mini);
static void mini_no_error_detect(t_mini *mini);
static void mini_recursive_init(t_mini *mini);
int main(void)
{
t_mini *mini;
mini = get_mini_address();
mini_ctrl_signal(mini);
mini_getenv(mini);
mini->status = 0;
while (42)
{
mini_recursive_init(mini);
if (mini->cmd_line == NULL)
{
mini_ctrl_d_exit();
break ;
}
if (mini->cmd_line[0] == '\0')
continue ;
add_history(mini->cmd_line);
mini_tokenizer(mini);
if (!mini->syntax_error && mini->token_list)
mini_no_error_detect(mini);
}
ft_free_trashman(ft_get_mem_address());
ft_free_trashman_env(ft_get_mem_address_env());
}
void mini_no_error_detect(t_mini *mini)
{
mini_parser(mini);
mini_expansion(mini);
mini_redirect(mini);
mini_execute(mini);
}
void mini_recursive_init(t_mini *mini)
{
char *prompt;
mini_init(mini);
mini->int_action.sa_handler = sig_handler;
sigaction(SIGINT, &mini->int_action, NULL);
mini->quit_action.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &mini->quit_action, NULL);
prompt = mini_pwd_prompt(mini);
mini->cmd_line = readline(prompt);
free(prompt);
ft_collect_mem(mini->cmd_line);
}
char *mini_pwd_prompt(t_mini *mini)
{
char *prompt;
char *pwd;
char *tmp;
pwd = ft_dict_get_value(mini->env_list, "PWD");
if (pwd == NULL)
return (ft_strdup("?/$ "));
tmp = ft_split_get_last(pwd, '/');
if (tmp == NULL)
pwd = ft_strdup(pwd);
else
{
pwd = ft_calloc(PWD_LIMIT + 1, sizeof(char));
ft_memcpy(pwd, tmp, ft_min(strlen(tmp), PWD_LIMIT));
free(tmp);
}
prompt = ft_strjoin(pwd, "/$ ");
free(pwd);
return (prompt);
}