-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
99 lines (89 loc) · 3.32 KB
/
Copy pathmain.c
File metadata and controls
99 lines (89 loc) · 3.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmounsif <mmounsif@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/07 11:16:17 by mmounsif #+# #+# */
/* Updated: 2025/08/11 18:11:35 by mmounsif ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_env *initialize_shell(char **envp, int *exit_status, t_cmd **cmds);
static char *process_input(t_cmd *cmds, t_env *env, int *exit_status);
static int process_command(char *input, t_env **env, int exit_status);
/* main - Entry point of the minishell program
* Initializes the shell environment, enters the main command loop,
* and handles user input until program termination.
* Returns 0 on successful termination, 1 on initialization failure */
int main(int ac, char **av, char **envp)
{
char *input;
t_cmd *cmds;
t_env *env;
int exit_status;
env = initialize_shell(envp, &exit_status, &cmds);
if (!env)
return (1);
while (ac && av)
{
input = process_input(cmds, env, &exit_status);
if (*input)
exit_status = process_command(input, &env, exit_status);
free(input);
}
free_env(env);
}
/* initialize_shell - Initializes the shell environment and variables
* Sets up signal handlers, terminal settings, and parses environment variables.
* Returns parsed environment structure on success, NULL on failure */
static t_env *initialize_shell(char **envp, int *exit_status, t_cmd **cmds)
{
t_env *env;
setup_parent_signals();
set_echoctl(0);
*cmds = NULL;
*exit_status = 0;
env = env_parsing(envp);
if (!env)
{
ft_putstr_fd("minishell: failed to initialize environment\n",
STDERR_FILENO);
return (NULL);
}
return (env);
}
/* process_input - Handles input reading and signal checking
* Reads user input using readline, handles SIGINT signals, and exits on EOF.
* Returns pointer to input string (never returns NULL) */
static char *process_input(t_cmd *cmds, t_env *env, int *exit_status)
{
char *input;
input = readline("minishell$ ");
if (g_sig_flag == SIGINT)
{
g_sig_flag = 0;
*exit_status = 1;
}
if (!input)
{
printf("\033[1A\033[%ldCexit\n", ft_strlen("minishell$ "));
exit_cmd(cmds, env, *exit_status);
}
return (input);
}
/* process_command - Handles command parsing and execution
* Adds input to history, parses into command structures, executes commands,
* and cleans up memory. Returns new exit status after execution */
static int process_command(char *input, t_env **env, int exit_status)
{
t_cmd *cmds;
add_history(input);
cmds = parse(input, *env, &exit_status);
if (!cmds)
return (exit_status);
exit_status = execution(cmds, env, exit_status);
free_cmds(cmds);
return (exit_status);
}