This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
108 lines (99 loc) · 2.54 KB
/
Copy pathmain.c
File metadata and controls
108 lines (99 loc) · 2.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shattori <shattori@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/11 01:08:04 by nando #+# #+# */
/* Updated: 2025/08/04 13:28:14 by shattori ### ########.fr */
/* */
/* ************************************************************************** */
#include "./minishell.h"
int add_history_and_parse(t_token *lex, t_shell *shell, char *cmd, t_ast **ast)
{
has_syntax_error(lex, shell);
if (shell->exit_status == 258)
{
add_history(cmd);
free_tokens(lex);
shell->exit_status = 2;
return (-1);
}
*ast = start_parse(lex);
free_tokens(lex);
if (!*ast)
return (-1);
add_history(cmd);
return (1);
}
t_andor *make_linearized_ast(char *cmd, t_shell *shell, t_ast **ast)
{
t_token *lex;
t_andor *linearized_ast;
lex = lexer(cmd);
if (!lex)
return (NULL);
if (!add_history_and_parse(lex, shell, cmd, ast))
return (NULL);
linearized_ast = linearizer(*ast, shell);
if (g_ack_status)
{
g_ack_status = 0;
free_ast(*ast);
free_andor_ast(linearized_ast);
return (NULL);
}
if (!linearized_ast)
return (NULL);
if (g_ack_status)
return (NULL);
return (linearized_ast);
}
void expand_and_execute(t_andor *linearized_ast, t_shell *shell)
{
expand_andor_arguments(linearized_ast, shell);
executor(linearized_ast, shell);
}
int prompt(t_shell *shell)
{
char *cmd;
t_andor *linearized_ast;
t_ast *ast;
ast = NULL;
cmd = run_readline(shell);
if (!cmd || cmd[0] == '\0')
{
if (cmd)
free(cmd);
return (0);
}
linearized_ast = make_linearized_ast(cmd, shell, &ast);
if (!linearized_ast)
{
if (cmd)
free(cmd);
return (0);
}
expand_and_execute(linearized_ast, shell);
free_ast(ast);
free_andor_ast(linearized_ast);
free(cmd);
return (1);
}
int main(int ac, char **av, char **envp)
{
t_shell shell;
(void)ac;
(void)av;
shell.is_interactive = isatty(STDIN_FILENO);
init_signal();
shell.env = init_env(envp);
shell.exit_status = 0;
while (1)
{
if (!prompt(&shell))
continue ;
}
return (0);
}