maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2024/06/05 13:23:26 by rmikhayl #+# #+# */
9
/* Updated: 2024/11/08 15:32:55 by dmdemirk ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "
tokens.h
"
14
#include "
execute.h
"
15
#include "
shell.h
"
16
#include "
pipe.h
"
17
#include "
signals.h
"
18
#include "
exit_status.h
"
19
#include "
builtins.h
"
20
21
void
main_loop
(
t_ms_data
*data,
t_loop_data
*loop_data);
22
int
main
(
int
argc,
char
**argv,
char
**envp);
23
24
void
process_command
(
t_ms_data
*data, \
25
t_loop_data
*loop_data,
t_token
*tokens_head)
26
{
27
int
status;
28
29
if
(!data || !loop_data || !tokens_head || !loop_data->
tree
)
30
return ;
31
status =
execute_ast
(loop_data->
tree
, data);
32
data->
exit_status
= status;
33
set_shell_var_handler
(data);
34
if
(status ==
WAIT_NEXT_COMMAND
)
35
{
36
loop_cleanup
(loop_data, tokens_head);
37
return ;
38
}
39
handle_io_fd
(data);
40
loop_cleanup
(loop_data, tokens_head);
41
}
42
43
void
main_loop
(
t_ms_data
*data,
t_loop_data
*loop_data)
44
{
45
t_token
*tokens_start;
46
47
while
(1)
48
{
49
set_signals_interactive
(data);
50
set_signals_noninteractive
();
51
loop_data->
input
= readline(
"🌴 maxishell> "
);
52
if
(loop_data->
input
&& loop_data->
input
[0] ==
'\0'
)
53
continue ;
54
if
(!loop_data->
input
|| !
ft_strncmp
(loop_data->
input
,
"exit"
, 4))
55
{
56
data->
args
=
ft_split
(loop_data->
input
,
' '
);
57
builtin_exit
(data);
58
}
59
make_history
(loop_data->
input
);
60
loop_data->
trimmed_input
=
trim_input
(loop_data->
input
);
61
if
(
input_error_checks
(loop_data))
62
continue ;
63
loop_data->
tokens
=
tokenise
(loop_data->
trimmed_input
);
64
tokens_start = loop_data->
tokens
;
65
loop_data->
tree
=
parse_tokens
(&loop_data->
tokens
, data);
66
process_command
(data, loop_data, tokens_start);
67
}
68
clear_history_file
();
69
}
70
71
int
main
(
int
argc,
char
**argv,
char
**envp)
72
{
73
t_ms_data
data;
74
t_loop_data
loop_data;
75
76
print_maxishell
();
77
init_ms_data
(&data, argv, envp);
78
initialise
(argc, argv);
79
main_loop
(&data, &loop_data);
80
free_ms_data
(&data);
81
clear_history_file
();
82
return
(0);
83
}
builtins.h
builtin_exit
int builtin_exit(t_ms_data *data)
Definition
exit.c:61
execute.h
execute_ast
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition
execute.c:38
handle_io_fd
void handle_io_fd(t_ms_data *data)
close temporary input/output file descriptors and reset them to -1
Definition
utils_0.c:39
exit_status.h
set_shell_var_handler
void set_shell_var_handler(t_ms_data *data)
Definition
exit_status.c:53
ft_strncmp
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition
ft_strncmp.c:24
ft_split
char ** ft_split(char const *s, char c)
Definition
ft_split.c:95
process_command
void process_command(t_ms_data *data, t_loop_data *loop_data, t_token *tokens_head)
Definition
main.c:24
main
int main(int argc, char **argv, char **envp)
Definition
main.c:71
main_loop
void main_loop(t_ms_data *data, t_loop_data *loop_data)
Definition
main.c:43
pipe.h
WAIT_NEXT_COMMAND
#define WAIT_NEXT_COMMAND
Definition
pipe.h:19
shell.h
signals.h
set_signals_interactive
void set_signals_interactive(t_ms_data *data)
Definition
signals.c:43
set_signals_noninteractive
void set_signals_noninteractive(void)
Definition
signals.c:66
s_loop_data
Definition
tokens.h:58
s_loop_data::trimmed_input
char * trimmed_input
Definition
tokens.h:61
s_loop_data::tokens
t_token * tokens
Definition
tokens.h:62
s_loop_data::input
char * input
Definition
tokens.h:60
s_loop_data::tree
t_ast * tree
Definition
tokens.h:63
s_ms_data
Definition
shell.h:21
s_ms_data::exit_status
int exit_status
Definition
shell.h:30
s_ms_data::args
char ** args
Definition
shell.h:23
s_token
Definition
tokens.h:42
tokens.h
loop_cleanup
void loop_cleanup(t_loop_data *loop_data, t_token *tokens_head)
Definition
clean.c:67
parse_tokens
t_ast * parse_tokens(t_token **tokens, t_ms_data *data)
Definition
AST_utils.c:23
initialise
void initialise(int argc, char **argv)
Definition
initialise.c:36
print_maxishell
void print_maxishell(void)
Definition
prompt.c:56
tokenise
t_token * tokenise(char *str)
Definition
tokeniser.c:22
input_error_checks
int input_error_checks(t_loop_data *loop_data)
Definition
input_checker.c:96
make_history
void make_history(char *line)
Definition
aux.c:15
init_ms_data
void init_ms_data(t_ms_data *data, char **argv, char **envp)
Definition
initialise.c:20
trim_input
char * trim_input(char *str)
Definition
input_checker_helpers.c:15
clear_history_file
void clear_history_file(void)
Definition
builtins_utils.c:31
free_ms_data
void free_ms_data(t_ms_data *data)
Definition
clean.c:51
src
main.c
Generated by
1.9.8