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
21void main_loop(t_ms_data *data, t_loop_data *loop_data);
22int main(int argc, char **argv, char **envp);
23
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;
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
43void main_loop(t_ms_data *data, t_loop_data *loop_data)
44{
45 t_token *tokens_start;
46
47 while (1)
48 {
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 }
69}
70
71int main(int argc, char **argv, char **envp)
72{
73 t_ms_data data;
74 t_loop_data loop_data;
75
77 init_ms_data(&data, argv, envp);
78 initialise(argc, argv);
79 main_loop(&data, &loop_data);
80 free_ms_data(&data);
82 return (0);
83}
int builtin_exit(t_ms_data *data)
Definition exit.c:61
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
void handle_io_fd(t_ms_data *data)
close temporary input/output file descriptors and reset them to -1
Definition utils_0.c:39
void set_shell_var_handler(t_ms_data *data)
Definition exit_status.c:53
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
char ** ft_split(char const *s, char c)
Definition ft_split.c:95
void process_command(t_ms_data *data, t_loop_data *loop_data, t_token *tokens_head)
Definition main.c:24
int main(int argc, char **argv, char **envp)
Definition main.c:71
void main_loop(t_ms_data *data, t_loop_data *loop_data)
Definition main.c:43
#define WAIT_NEXT_COMMAND
Definition pipe.h:19
void set_signals_interactive(t_ms_data *data)
Definition signals.c:43
void set_signals_noninteractive(void)
Definition signals.c:66
char * trimmed_input
Definition tokens.h:61
t_token * tokens
Definition tokens.h:62
char * input
Definition tokens.h:60
t_ast * tree
Definition tokens.h:63
int exit_status
Definition shell.h:30
char ** args
Definition shell.h:23
void loop_cleanup(t_loop_data *loop_data, t_token *tokens_head)
Definition clean.c:67
t_ast * parse_tokens(t_token **tokens, t_ms_data *data)
Definition AST_utils.c:23
void initialise(int argc, char **argv)
Definition initialise.c:36
void print_maxishell(void)
Definition prompt.c:56
t_token * tokenise(char *str)
Definition tokeniser.c:22
int input_error_checks(t_loop_data *loop_data)
void make_history(char *line)
Definition aux.c:15
void init_ms_data(t_ms_data *data, char **argv, char **envp)
Definition initialise.c:20
char * trim_input(char *str)
void clear_history_file(void)
void free_ms_data(t_ms_data *data)
Definition clean.c:51