42_minishell/main.c

143 lines
3.1 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 #+# #+# */
2023-02-21 08:34:49 -05:00
/* Updated: 2023/02/21 14:24:45 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;
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-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 13:05:33 -05:00
code = ft_cmds_executor(cmds, env);
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
int main(int ac, char **av, char **env)
{
t_data data;
char *line;
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-17 13:05:33 -05:00
if (ft_minishell(data.env, line) == 2)
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);
}
#endif