2023-03-28 09:55:08 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* parse.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/03/27 13:44:38 by cchauvet #+# #+# */
|
2023-04-04 07:56:21 -04:00
|
|
|
/* Updated: 2023/04/04 13:50:18 by alouis-j ### ########.fr */
|
2023-03-28 09:55:08 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./parse_private.h"
|
|
|
|
|
2023-03-13 10:34:43 -04:00
|
|
|
static int ft_args_parse(char *cmd_str, t_cmd *cmd)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
char **tab;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
tab = ft_split_quoted(cmd_str, ' ');
|
|
|
|
if (tab == NULL)
|
|
|
|
return (1);
|
|
|
|
i = 0;
|
2023-03-10 07:40:20 -05:00
|
|
|
while (tab[i] != NULL)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
2023-03-13 10:34:43 -04:00
|
|
|
ft_quote_remover(tab[i]);
|
2023-03-10 06:32:39 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
cmd->args = tab;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-03-13 10:34:43 -04:00
|
|
|
static int ft_executable_parse(t_data *data, t_cmd *cmd)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
bool own;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = cmd->args[0];
|
|
|
|
own = 0;
|
2023-03-13 09:23:27 -04:00
|
|
|
if (cmd->args[0] == NULL)
|
|
|
|
{
|
|
|
|
ft_closer(cmd->fd_in);
|
|
|
|
ft_closer(cmd->fd_out);
|
|
|
|
}
|
2023-03-28 09:55:08 -04:00
|
|
|
else if (ft_strcmp(cmd->args[0], "env") == 0 || (ft_strcmp(cmd->args[0],
|
|
|
|
"export") == 0) || (ft_strcmp(cmd->args[0], "echo") == 0)
|
|
|
|
|| (ft_strcmp(cmd->args[0], "unset") == 0) || (ft_strcmp(cmd->args[0],
|
|
|
|
"exit") == 0) || (ft_strcmp(cmd->args[0], "pwd") == 0)
|
|
|
|
|| (ft_strcmp(cmd->args[0], "cd") == 0))
|
2023-03-10 06:32:39 -05:00
|
|
|
own = 1;
|
2023-03-13 09:23:27 -04:00
|
|
|
else
|
2023-03-20 10:31:39 -04:00
|
|
|
{
|
|
|
|
path = ft_get_executable(data, cmd->args[0]);
|
|
|
|
if (path == NULL)
|
|
|
|
return (1);
|
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
cmd->own_cmd = own;
|
|
|
|
cmd->executable = path;
|
2023-03-20 10:31:39 -04:00
|
|
|
return (0);
|
2023-03-10 06:32:39 -05:00
|
|
|
}
|
|
|
|
|
2023-03-28 09:55:08 -04:00
|
|
|
int ft_cmd_adder(t_data *data, t_cmd *cmd)
|
|
|
|
{
|
|
|
|
t_list *element;
|
|
|
|
|
|
|
|
element = ft_lstnew(cmd);
|
|
|
|
if (element == NULL)
|
|
|
|
{
|
|
|
|
ft_cmddel(cmd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
ft_lstadd_back(data->cmds, element);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
int ft_cmd_parser(t_data *data, char *cmd_str)
|
|
|
|
{
|
|
|
|
t_cmd *cmd;
|
|
|
|
|
|
|
|
cmd = ft_calloc(sizeof(t_cmd), 1);
|
|
|
|
if (cmd == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2023-04-04 07:56:21 -04:00
|
|
|
if (ft_redirection(data, cmd, cmd_str))
|
|
|
|
{
|
|
|
|
ft_cmddel(cmd);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-03-13 10:34:43 -04:00
|
|
|
if (ft_args_parse(cmd_str, cmd))
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
ft_cmddel(cmd);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-03-30 06:33:33 -04:00
|
|
|
ft_executable_parse(data, cmd);
|
2023-03-28 09:55:08 -04:00
|
|
|
return (ft_cmd_adder(data, cmd));
|
2023-03-10 06:32:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int ft_cmds_parser(t_data *data, const char *line)
|
|
|
|
{
|
|
|
|
char **tab;
|
2023-03-28 09:55:08 -04:00
|
|
|
ssize_t i;
|
2023-03-10 06:32:39 -05:00
|
|
|
|
|
|
|
tab = ft_split_quoted(line, '|');
|
|
|
|
if (tab == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2023-03-28 09:55:08 -04:00
|
|
|
i = -1;
|
|
|
|
while (tab[++i] != NULL)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
2023-03-20 10:31:39 -04:00
|
|
|
if (ft_cmd_parser(data, tab[i]))
|
|
|
|
{
|
|
|
|
ft_freer_tab_ultimate(1, tab);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
}
|
2023-03-20 08:18:20 -04:00
|
|
|
if (*data->cmds != NULL)
|
|
|
|
{
|
2023-03-22 09:59:19 -04:00
|
|
|
ft_add_fd(((t_cmd *)(*data->cmds)->content)->fd_in, 0);
|
|
|
|
ft_add_fd(((t_cmd *)(ft_lstlast(*data->cmds))->content)->fd_out, 1);
|
2023-03-20 08:18:20 -04:00
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
ft_freer_tab_ultimate(1, tab);
|
|
|
|
return (0);
|
|
|
|
}
|