-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·140 lines (132 loc) · 3.52 KB
/
main.c
File metadata and controls
executable file
·140 lines (132 loc) · 3.52 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isalayan <isalayan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/14 14:10:16 by isalayan #+# #+# */
/* Updated: 2025/04/21 13:12:58 by isalayan ### ########.fr */
/* */
/* ************************************************************************** */
#include "mini_shell.h"
int g_v = 0;
int handle_parse_tokens_error(char *line, t_tokenlist *token_list,
t_parser *parser, int value)
{
if (!parser || !parser->command || value == -1)
{
free(line);
free_token_list(token_list);
free_parser(parser);
return (1);
}
return (0);
}
int initiate_input(char *line, t_tokenlist **token_list,
t_parser **parser, t_env *my_env)
{
int value;
if (ft_manage_empty_input(line))
return (0);
if (*line)
add_history(line);
*token_list = tokenize_input(line);
if (handle_tokenization_error(*token_list, line))
return (0);
*parser = create_parser();
if (handle_parser_error(*parser, *token_list, line))
return (0);
value = parse_tokens(parser, *token_list, my_env);
if (handle_parse_tokens_error(line, *token_list, *parser, value))
return (0);
return (1);
}
int main(int argc, char **argv, char **envp)
{
char *line;
t_env *my_env;
t_tokenlist *token_list;
t_parser *parser;
token_list = NULL;
parser = NULL;
(void)argc;
(void)argv;
line = NULL;
my_env = initialize_environment(envp);
restore_signals();
while (1)
{
line = readline("minishell>");
if (!line)
break ;
if (initiate_input(line, &token_list, &parser, my_env) != 1)
continue ;
cmds_exec(parser, my_env);
cleanup_resources(line, token_list, parser);
}
return (ft_free_env(&my_env), EXIT_SUCCESS);
}
/*int main(int argc, char **argv, char **envp)
{
(void)argc;
(void)argv;
char *line = NULL;
t_env *my_env;
t_tokenlist *token_list;
t_parser *parser;
int value ;
if(envp == NULL || *envp == NULL)
my_env = manage_env_i();
else
my_env = init_env(envp);
restore_signals();
while (1)
{
line = readline("minishell>");
if (!line)
break ;
if (ft_strlen(line) == 0)
{
free(line);
continue;
}
if (*line)
add_history(line);
token_list = tokenize_input(line);
if (!token_list)
{
printf("Error: Failed to tokenize input.\n");
free(line);
continue ;
}
// Print tokens (for debugging)
printf("\nTokens:______________________\n");
print_token_list(token_list);
// Parse tokens
parser = create_parser(); // Initialize parser
if (!parser)
{
printf("Error: Failed to create parser.\n");
free(line);
free_token_list(token_list); // Free token list
continue ;
}
value = parse_tokens(&parser, token_list, my_env);
if (!parser || !parser->command || value == -1)
{
free(line);
free_token_list(token_list);
free_parser(parser);
continue ;
}
printf("\nParsed commands:\n");
print_parser(parser);
cmds_exec(parser, my_env);
free_token_list(token_list);
free(line);
free_parser(parser);
}
ft_free_env(&my_env);
return (EXIT_SUCCESS);
}*/