2023-02-16 09:17:32 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
2023-02-24 05:16:35 -05:00
|
|
|
/* Updated: 2023/02/24 11:04:40 by cchauvet ### ########.fr */
|
2023-02-16 09:17:32 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
2023-02-15 14:52:27 -05:00
|
|
|
|
2023-02-16 09:17:32 -05:00
|
|
|
#include "minishell.h"
|
2023-02-09 12:47:05 -05:00
|
|
|
|
2023-02-24 05:16:35 -05:00
|
|
|
static char *ft_get_user_input()
|
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);
|
2023-02-23 09:21:06 -05:00
|
|
|
if (line != NULL && ft_strcmp(line, "") != 0)
|
|
|
|
add_history(line);
|
2023-02-17 10:33:52 -05:00
|
|
|
free(prompt);
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:12:23 -05:00
|
|
|
static int ft_minishell(t_data *data, char *line)
|
2023-02-17 10:33:52 -05:00
|
|
|
{
|
|
|
|
char *line_clean;
|
2023-02-09 12:47:05 -05:00
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
if (ft_syntax_verif(data, line))
|
2023-03-08 12:19:10 -05:00
|
|
|
return (0);
|
2023-03-10 06:32:39 -05:00
|
|
|
line_clean = ft_formater(line);
|
2023-02-17 10:33:52 -05:00
|
|
|
if (line_clean == NULL)
|
2023-03-08 12:19:10 -05:00
|
|
|
return (0);
|
2023-03-10 06:32:39 -05:00
|
|
|
if (ft_cmds_parser(data, line_clean))
|
2023-03-06 10:06:12 -05:00
|
|
|
{
|
|
|
|
free(line_clean);
|
|
|
|
return (0);
|
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
if (ft_cmds_executor(data) == 1)
|
2023-03-06 10:06:12 -05:00
|
|
|
{
|
|
|
|
free(line_clean);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-17 13:05:33 -05:00
|
|
|
free(line_clean);
|
2023-03-06 10:06:12 -05:00
|
|
|
return (0);
|
2023-01-31 09:02:23 -05:00
|
|
|
}
|
2023-02-17 10:33:52 -05:00
|
|
|
|
2023-02-21 18:26:32 -05:00
|
|
|
void ft_ctrlc(int num)
|
|
|
|
{
|
2023-02-24 05:16:35 -05:00
|
|
|
(void) num;
|
2023-03-07 11:48:18 -05:00
|
|
|
|
|
|
|
if (*ft_get_heredoc() != -1)
|
2023-02-23 09:14:08 -05:00
|
|
|
{
|
2023-03-07 11:48:18 -05:00
|
|
|
close(*ft_get_heredoc());
|
|
|
|
*ft_get_heredoc() = -1;
|
2023-02-23 09:14:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-24 09:53:46 -05:00
|
|
|
rl_replace_line("", 0);
|
2023-02-23 12:03:22 -05:00
|
|
|
rl_on_new_line();
|
2023-02-24 05:16:35 -05:00
|
|
|
ft_putchar_fd('\n', 1);
|
|
|
|
rl_redisplay();
|
2023-02-23 09:14:08 -05:00
|
|
|
}
|
2023-02-21 18:26:32 -05:00
|
|
|
}
|
|
|
|
|
2023-02-24 05:16:35 -05:00
|
|
|
void ft_quit(int num)
|
|
|
|
{
|
|
|
|
(void) num;
|
2023-03-10 06:32:39 -05:00
|
|
|
|
|
|
|
ft_printf("%c%c %c%c", 0, 0, 0, 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)
|
|
|
|
{
|
|
|
|
t_data data;
|
|
|
|
char *line;
|
|
|
|
|
2023-02-24 05:16:35 -05:00
|
|
|
(void) ac;
|
|
|
|
(void) av;
|
2023-02-21 18:26:32 -05:00
|
|
|
signal(SIGINT, ft_ctrlc);
|
2023-02-24 05:16:35 -05:00
|
|
|
signal(SIGQUIT, ft_quit);
|
2023-02-24 14:30:01 -05:00
|
|
|
data.exit_code = 0;
|
2023-03-06 11:43:29 -05:00
|
|
|
data.exit_code_str = NULL;
|
2023-03-10 06:32:39 -05:00
|
|
|
ft_gen_exit_code(&data);
|
|
|
|
data.cmds = malloc(sizeof(t_cmd *));
|
|
|
|
if (data.cmds == NULL)
|
|
|
|
return (1);
|
|
|
|
*data.cmds = NULL;
|
2023-02-17 10:33:52 -05:00
|
|
|
data.env = init_env(env);
|
|
|
|
if (data.env == NULL)
|
|
|
|
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-06 10:06:12 -05: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-10 06:32:39 -05:00
|
|
|
|
2023-03-06 08:22:30 -05:00
|
|
|
free(data.exit_code_str);
|
2023-03-10 06:32:39 -05:00
|
|
|
ft_lstclear(data.cmds, ft_cmddel);
|
|
|
|
free(data.cmds);
|
2023-03-06 11:43:29 -05:00
|
|
|
ft_lstclear(data.env, env_del);
|
2023-02-17 10:33:52 -05:00
|
|
|
free(data.env);
|
2023-02-21 16:12:23 -05:00
|
|
|
return (data.exit_code);
|
2023-02-17 10:33:52 -05:00
|
|
|
}
|