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-21 17:55:32 -05:00
|
|
|
/* Updated: 2023/02/21 23:54:44 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-17 10:33:52 -05:00
|
|
|
static char *ft_get_user_input(t_list **env)
|
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);
|
|
|
|
add_history(line);
|
|
|
|
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
|
|
|
{
|
|
|
|
t_list **cmds;
|
2023-02-17 13:05:33 -05:00
|
|
|
int code;
|
2023-02-17 10:33:52 -05:00
|
|
|
char *line_clean;
|
2023-02-17 07:07:10 -05:00
|
|
|
int infile;
|
|
|
|
int outfile;
|
2023-02-09 12:47:05 -05:00
|
|
|
|
2023-02-21 17:55:32 -05:00
|
|
|
if (ft_syntatic_verif(line))
|
|
|
|
return (1);
|
2023-02-17 10:33:52 -05:00
|
|
|
line_clean = ft_normalizer(line);
|
|
|
|
if (line_clean == NULL)
|
2023-02-16 08:54:19 -05:00
|
|
|
return (1);
|
2023-02-17 10:33:52 -05:00
|
|
|
outfile = ft_outfile(line_clean);
|
2023-02-17 07:07:10 -05:00
|
|
|
if (outfile == -2)
|
|
|
|
{
|
2023-02-17 10:33:52 -05:00
|
|
|
free(line_clean);
|
2023-02-17 07:21:50 -05:00
|
|
|
return (1);
|
2023-02-17 07:07:10 -05:00
|
|
|
}
|
2023-02-17 10:33:52 -05:00
|
|
|
infile = ft_infile(line_clean);
|
2023-02-17 07:07:10 -05:00
|
|
|
if (infile == -2)
|
|
|
|
{
|
|
|
|
close(outfile);
|
2023-02-17 10:33:52 -05:00
|
|
|
free(line_clean);
|
2023-02-17 07:21:50 -05:00
|
|
|
return (1);
|
2023-02-17 07:07:10 -05:00
|
|
|
}
|
2023-02-21 16:12:23 -05:00
|
|
|
cmds = ft_parse_cmds(line_clean, data->env, infile, outfile);
|
2023-02-09 12:47:05 -05:00
|
|
|
if (cmds == NULL)
|
2023-02-17 07:07:10 -05:00
|
|
|
{
|
|
|
|
close(outfile);
|
|
|
|
close(infile);
|
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
|
|
free(cmds);
|
2023-02-17 10:33:52 -05:00
|
|
|
free(line_clean);
|
2023-02-09 12:47:05 -05:00
|
|
|
return (1);
|
2023-02-17 07:07:10 -05:00
|
|
|
}
|
2023-02-21 16:12:23 -05:00
|
|
|
code = ft_cmds_executor(data, cmds);
|
2023-02-17 13:05:33 -05:00
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
|
|
free(cmds);
|
|
|
|
free(line_clean);
|
|
|
|
return (code);
|
2023-02-17 10:33:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
int main(int ac, char **av, char **env)
|
|
|
|
{
|
|
|
|
t_data data;
|
|
|
|
|
|
|
|
if (ac == 1)
|
|
|
|
return (1);
|
|
|
|
data.env = init_env(env);
|
|
|
|
if (data.env == NULL)
|
|
|
|
return (1);
|
|
|
|
if (ft_minishell(data.env, av[1]) == 1)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
2023-02-17 07:07:10 -05:00
|
|
|
free(data.env);
|
2023-02-09 12:47:05 -05:00
|
|
|
return (1);
|
2023-02-14 01:08:25 -05:00
|
|
|
}
|
2023-02-14 07:38:40 -05:00
|
|
|
ft_lstclear(data.env, env_del);
|
2023-02-14 11:11:39 -05:00
|
|
|
free(data.env);
|
2023-02-14 07:38:40 -05:00
|
|
|
return (0);
|
2023-01-31 09:02:23 -05:00
|
|
|
}
|
2023-02-17 10:33:52 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2023-02-21 18:26:32 -05:00
|
|
|
void ft_ctrlc(int num)
|
|
|
|
{
|
|
|
|
rl_on_new_line();
|
|
|
|
ft_putchar_fd('\n', 1);
|
|
|
|
rl_redisplay();
|
|
|
|
}
|
|
|
|
|
2023-02-17 10:33:52 -05:00
|
|
|
int main(int ac, char **av, char **env)
|
|
|
|
{
|
|
|
|
t_data data;
|
|
|
|
char *line;
|
|
|
|
|
2023-02-21 18:26:32 -05:00
|
|
|
signal(SIGINT, ft_ctrlc);
|
2023-02-17 10:33:52 -05:00
|
|
|
data.env = init_env(env);
|
|
|
|
if (data.env == NULL)
|
|
|
|
return (1);
|
2023-02-21 08:34:49 -05:00
|
|
|
if (create_value_by_key_dup("", "$", data.env) == 1
|
|
|
|
|| create_value_by_key_dup("?", "0", data.env) == 1)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-17 10:33:52 -05:00
|
|
|
line = ft_get_user_input(data.env);
|
|
|
|
if (line == NULL)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
while (line != NULL)
|
|
|
|
{
|
2023-02-21 16:12:23 -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);
|
|
|
|
line = ft_get_user_input(data.env);
|
|
|
|
if (line == NULL)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
2023-02-21 16:12:23 -05:00
|
|
|
return (data.exit_code);
|
2023-02-17 10:33:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|