42_minishell/main.c

143 lines
3.3 KiB
C
Raw Normal View History

2023-02-16 09:17:32 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/03/30 12:41:52 by cchauvet ### ########.fr */
2023-02-16 09:17:32 -05:00
/* */
/* ************************************************************************** */
2023-02-15 14:52:27 -05:00
2023-03-20 10:31:39 -04:00
#include "data/data.h"
2023-03-29 13:07:57 -04:00
#include "env/env.h"
2023-03-22 09:29:32 -04:00
#include "libftx/libftx.h"
2023-02-16 09:17:32 -05:00
#include "minishell.h"
2023-02-09 12:47:05 -05:00
2023-03-28 09:55:08 -04:00
static char *ft_get_user_input(void)
2023-01-31 09:02:23 -05:00
{
2023-02-15 14:52:27 -05:00
char *line;
2023-02-17 10:33:52 -05:00
char *prompt;
2023-02-21 09:07:14 -05:00
char *pwd;
2023-02-17 10:33:52 -05:00
2023-02-21 09:07:14 -05:00
pwd = get_pwd(2);
if (pwd == NULL)
return (NULL);
prompt = ft_strmerger(2, pwd, "$ ");
free(pwd);
2023-02-17 10:33:52 -05:00
if (prompt == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
line = readline(prompt);
if (line != NULL && ft_strcmp(line, "") != 0)
add_history(line);
2023-02-17 10:33:52 -05:00
free(prompt);
2023-03-22 09:29:32 -04:00
if (line == NULL)
ft_printf("exit\n");
2023-02-17 10:33:52 -05:00
return (line);
}
2023-03-22 09:29:32 -04:00
static void ft_cmds_waiter(t_data *data)
2023-02-17 10:33:52 -05:00
{
2023-03-10 07:34:23 -05:00
t_list *current;
t_cmd *content;
2023-03-22 09:29:32 -04:00
int exit_status;
current = *data->cmds;
while (current != NULL)
{
content = current->content;
if (content->executable != NULL && content->own_cmd == 0
&& content->pid != -1 && content->fd_in[0] != -2 && content->fd_out[0] != -2)
2023-03-22 09:29:32 -04:00
{
waitpid(content->pid, &exit_status, 0);
if (WIFSIGNALED(exit_status))
{
if (WTERMSIG(exit_status) == SIGKILL)
data->exit_code = 131;
2023-03-28 09:55:08 -04:00
else
2023-03-22 09:29:32 -04:00
data->exit_code = 130;
}
else
data->exit_code = WEXITSTATUS(exit_status);
}
current = current->next;
}
2023-03-22 09:35:09 -04:00
data->child_pid = 0;
2023-03-22 09:29:32 -04:00
}
static int ft_minishell(t_data *data, char *line)
{
char *line_clean;
2023-02-09 12:47:05 -05:00
if (ft_syntax_verif(data, line))
return (0);
2023-03-13 10:34:43 -04:00
line_clean = ft_formater(data, line);
2023-03-20 08:18:20 -04:00
if (line_clean == NULL || line_clean[0] == '\0')
return (0);
if (ft_cmds_parser(data, line_clean))
{
free(line_clean);
return (0);
}
2023-03-10 07:34:23 -05:00
free(line_clean);
if (ft_cmds_executor(data) == 1)
return (1);
2023-03-22 09:29:32 -04:00
ft_cmds_waiter(data);
2023-03-10 07:34:23 -05:00
ft_lstclear(data->cmds, ft_cmddel);
return (0);
2023-01-31 09:02:23 -05:00
}
2023-02-17 10:33:52 -05:00
2023-03-29 13:07:57 -04:00
int ft_init_data(t_data *data, char **env)
{
2023-03-29 13:07:57 -04:00
data->exit_code = 0;
data->child_pid = 0;
data->cmds = malloc(sizeof(t_cmd *));
if (data->cmds == NULL)
2023-03-22 09:29:32 -04:00
{
2023-03-29 13:07:57 -04:00
ft_eprintf("minishell: malloc failed\n");
return (1);
2023-03-22 09:29:32 -04:00
}
2023-03-29 13:07:57 -04:00
*data->cmds = NULL;
data->env = init_env(env);
if (data->env == NULL)
2023-03-22 09:29:32 -04:00
{
2023-03-29 13:07:57 -04:00
ft_eprintf("minishell: malloc failed\n");
free(data->cmds);
return (1);
2023-03-22 09:29:32 -04:00
}
2023-03-29 13:07:57 -04:00
return (0);
2023-02-24 05:16:35 -05:00
}
2023-02-17 10:33:52 -05:00
int main(int ac, char **av, char **env)
{
2023-03-22 09:29:32 -04:00
t_data *data;
2023-02-17 10:33:52 -05:00
char *line;
2023-02-24 05:16:35 -05:00
(void) ac;
(void) av;
signal(SIGINT, ft_ctrlc);
2023-02-24 05:16:35 -05:00
signal(SIGQUIT, ft_quit);
2023-03-22 09:29:32 -04:00
data = ft_get_data();
2023-03-29 13:07:57 -04:00
if (ft_init_data(data, env))
2023-02-17 10:33:52 -05:00
return (1);
2023-02-24 05:16:35 -05:00
line = ft_get_user_input();
2023-02-17 10:33:52 -05:00
while (line != NULL)
{
2023-03-22 09:29:32 -04:00
if (ft_minishell(data, line) == 1)
2023-02-17 13:05:33 -05:00
break ;
2023-02-17 10:33:52 -05:00
free(line);
2023-02-24 05:16:35 -05:00
line = ft_get_user_input();
2023-02-17 10:33:52 -05:00
if (line == NULL)
2023-02-24 14:30:01 -05:00
break ;
2023-02-17 10:33:52 -05:00
}
2023-03-22 09:29:32 -04:00
ft_lstclear(data->cmds, ft_cmddel);
free(data->cmds);
ft_lstclear(data->env, env_del);
free(data->env);
return (data->exit_code);
2023-02-17 10:33:52 -05:00
}