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-17 10:33:52 -05:00
|
|
|
/* Updated: 2023/02/17 16:06:02 by cchauvet ### ########.fr */
|
2023-02-16 09:17:32 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
2023-02-15 14:52:27 -05:00
|
|
|
|
2023-02-17 10:33:52 -05: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-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;
|
|
|
|
|
|
|
|
prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
|
|
|
|
if (prompt == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
line = readline(prompt);
|
|
|
|
add_history(line);
|
|
|
|
free(prompt);
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ft_minishell(t_list **env, char *line)
|
|
|
|
{
|
|
|
|
t_list **cmds;
|
|
|
|
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-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
|
|
|
if (ft_syntatic_verif(line_clean))
|
2023-02-16 08:54:19 -05:00
|
|
|
{
|
2023-02-17 10:33:52 -05:00
|
|
|
free(line_clean);
|
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-17 10:33:52 -05:00
|
|
|
cmds = ft_parse_cmds(line_clean, 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-17 10:33:52 -05:00
|
|
|
if (ft_cmds_executor(cmds, env) == 1)
|
2023-02-14 01:08:25 -05:00
|
|
|
{
|
2023-02-17 07:07:10 -05:00
|
|
|
close(outfile);
|
|
|
|
close(infile);
|
2023-02-15 14:52:27 -05:00
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
2023-02-17 07:07:10 -05:00
|
|
|
free(cmds);
|
2023-02-17 10:33:52 -05:00
|
|
|
free(line_clean);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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);
|
|
|
|
free(data.env);
|
|
|
|
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-15 14:52:27 -05:00
|
|
|
free(line);
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int ac, char **av, char **env)
|
|
|
|
{
|
|
|
|
t_data data;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
data.env = init_env(env);
|
|
|
|
if (data.env == NULL)
|
|
|
|
return (1);
|
|
|
|
line = ft_get_user_input(data.env);
|
|
|
|
if (line == NULL)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
while (line != NULL)
|
|
|
|
{
|
|
|
|
if (ft_minishell(data.env, line) == 1)
|
|
|
|
{
|
|
|
|
ft_lstclear(data.env, env_del);
|
|
|
|
free(data.env);
|
|
|
|
free(line);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|