148 lines
3.1 KiB
C
148 lines
3.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
|
/* Updated: 2023/02/21 23:54:44 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libftx/libftx.h"
|
|
#include "minishell.h"
|
|
#include "utils/utils.h"
|
|
#include <stdlib.h>
|
|
|
|
static char *ft_get_user_input(t_list **env)
|
|
{
|
|
char *line;
|
|
char *prompt;
|
|
char *pwd;
|
|
|
|
pwd = get_pwd(2);
|
|
if (pwd == NULL)
|
|
return (NULL);
|
|
prompt = ft_strmerger(2, pwd, "$ ");
|
|
free(pwd);
|
|
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_data *data, char *line)
|
|
{
|
|
t_list **cmds;
|
|
int code;
|
|
char *line_clean;
|
|
int infile;
|
|
int outfile;
|
|
|
|
if (ft_syntatic_verif(line))
|
|
return (1);
|
|
line_clean = ft_normalizer(line);
|
|
if (line_clean == NULL)
|
|
return (1);
|
|
outfile = ft_outfile(line_clean);
|
|
if (outfile == -2)
|
|
{
|
|
free(line_clean);
|
|
return (1);
|
|
}
|
|
infile = ft_infile(line_clean);
|
|
if (infile == -2)
|
|
{
|
|
close(outfile);
|
|
free(line_clean);
|
|
return (1);
|
|
}
|
|
cmds = ft_parse_cmds(line_clean, data->env, infile, outfile);
|
|
if (cmds == NULL)
|
|
{
|
|
close(outfile);
|
|
close(infile);
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
free(cmds);
|
|
free(line_clean);
|
|
return (1);
|
|
}
|
|
code = ft_cmds_executor(data, cmds);
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
free(cmds);
|
|
free(line_clean);
|
|
return (code);
|
|
}
|
|
|
|
#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);
|
|
free(data.env);
|
|
return (1);
|
|
}
|
|
ft_lstclear(data.env, env_del);
|
|
free(data.env);
|
|
return (0);
|
|
}
|
|
|
|
#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);
|
|
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);
|
|
}
|
|
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, line) == -1)
|
|
break ;
|
|
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);
|
|
return (data.exit_code);
|
|
}
|
|
|
|
#endif
|