-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
102 lines (93 loc) · 2.27 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acollin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/06 20:38:28 by acollin #+# #+# */
/* Updated: 2021/10/09 12:01:13 by acollin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void clear_all(char *input, t_tlist *tokens, char *prompt)
{
unlink(".tmp_file");
if (input)
free(input);
if (tokens)
tlist_clear(tokens);
if (prompt)
free(prompt);
}
void print_tokens(t_tlist *tokens)
{
t_tlist *current_node;
int i;
int j;
i = 0;
current_node = tokens;
if (!tokens)
{
printf("no tokens");
return ;
}
while (current_node != NULL)
{
printf("[");
j = -1;
while (current_node->cmd[++j])
printf("%s ", current_node->cmd[j]);
printf("]->");
current_node = current_node->next;
i++;
}
}
t_list *save_var(void)
{
t_list *var_list;
extern char **environ;
var_list = NULL;
while (*environ != NULL)
{
ft_save_var(&var_list, *environ, 1);
environ++;
}
ft_save_var(&var_list, "?=0", 0);
return (var_list);
}
void check_eof(char *line, t_list *var_list)
{
if (line)
return ;
printf("exit\n");
ft_clear_vars(var_list);
exit(g_exit_status);
}
int main(void)
{
char *prompt;
char *input;
t_tlist *tokens;
t_list *var_list;
var_list = save_var();
ft_change_lvl(&var_list, 1);
while (1)
{
tokens = NULL;
sig_init();
prompt = create_prompt();
input = readline(prompt);
if (input)
add_history(input);
check_eof(input, var_list);
input = preparse(input);
if (input)
{
lexer(input, &tokens, &var_list);
ft_start(tokens, &var_list);
}
ft_add_status(&var_list);
clear_all(input, tokens, prompt);
}
}