-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
121 lines (112 loc) · 2.04 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
110
111
112
113
114
115
116
117
118
119
120
121
#include "minishell.h"
char *line_env(char *line)
{
int ret;
int count;
char *tmp;
ret = 0;
count = nbr_of_dollars(line);
line = ft_strdup(line);
while (ft_strchr(line, '$') && ret != count)
{
tmp = ft_strdup(line);
free(line);
line = add_env(tmp, &ret, &count);
free(tmp);
}
return (line);
}
void parser_test(char *line)
{
t_token *tokens;
char **splited;
t_lst *commands;
if (is_error(line))
return ;
line = line_env(line);
if (*line == '\0')
{
rl_on_new_line();
free(line);
return ;
}
tokens = token_finder(line);
splited = ft_test(line, tokens);
free(line);
commands = put_in_list(splited);
if (add_files(commands) == -1)
{
printf("Malloc failed\n");
return ;
}
if (check_syntax(commands) == -1)
return ;
rem_handcom_clean(commands, tokens, splited);
}
char *ft_space_line(char *line)
{
int i;
int j;
char *line2;
i = 0;
j = 0;
while (line[i] == ' ' || line[i] == ' ')
i++;
line2 = malloc(sizeof(char) * ((ft_strlen(line) - i) + 1));
if (!line2)
return (NULL);
while (line[i])
{
line2[j] = line[i];
j++;
i++;
}
free(line);
line2[j] = '\0';
return (line2);
}
void prompt_test(char *line)
{
while (g_data.exit == -1)
{
g_data.here_doc = 0;
signal(SIGINT, sighandler);
signal(SIGQUIT, SIG_IGN);
line = readline("Minishell$ ");
if (!line)
{
ft_ctrl_d();
break ;
}
add_history(line);
if (line[0] == '|')
{
printf("bash: syntax error near unexpected token `|'\n");
free(line);
line = ft_strdup("");
}
if (line[0] == ' ' || line[0] == ' ')
line = ft_space_line(line);
if (ft_strlen(line))
parser_test(line);
ft_bzero(line, ft_strlen(line));
free(line);
}
}
int main(void)
{
char *line;
tcgetattr(0, &g_data.main_old);
g_data.main_new = g_data.main_old;
g_data.main_new.c_lflag &= ~(ECHOCTL);
tcsetattr(0, TCSANOW, &g_data.main_new);
if (copy_env() == -1)
return (EXIT_FAILURE);
line = NULL;
g_data.exit = -1;
g_data.exit_code = 0;
prompt_test(line);
free_envp();
tcsetattr(0, TCSANOW, &g_data.main_old);
return (g_data.exit_code);
}