2023-02-15 14:52:27 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* cmds.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
2023-02-17 10:33:52 -05:00
|
|
|
/* Updated: 2023/02/17 15:20:29 by cchauvet ### ########.fr */
|
2023-02-15 14:52:27 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-02-16 12:28:10 -05:00
|
|
|
#include "libftx/libftx.h"
|
2023-02-15 14:52:27 -05:00
|
|
|
#include "minishell.h"
|
|
|
|
|
2023-02-16 12:28:10 -05:00
|
|
|
static int ft_cmds_init(t_list **cmds, size_t len)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
t_cmd *content;
|
|
|
|
t_list *current;
|
|
|
|
size_t i;
|
|
|
|
|
2023-02-24 07:25:29 -05:00
|
|
|
*cmds = NULL;
|
|
|
|
if (len > 0)
|
|
|
|
*cmds = malloc(sizeof(t_list));
|
2023-02-15 14:52:27 -05:00
|
|
|
current = *cmds;
|
|
|
|
i = 0;
|
|
|
|
while (i < len)
|
|
|
|
{
|
|
|
|
content = malloc(sizeof(t_cmd));
|
|
|
|
if (content == NULL)
|
|
|
|
{
|
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
content->args = NULL;
|
|
|
|
content->executable = NULL;
|
|
|
|
content->fd_in = -1;
|
|
|
|
content->fd_out = -1;
|
|
|
|
current->content = content;
|
|
|
|
if (!((i + 1) < len))
|
|
|
|
{
|
|
|
|
current->next = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
current->next = malloc(sizeof(t_list));
|
|
|
|
if (current->next == NULL)
|
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
|
|
current = current->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:28:10 -05:00
|
|
|
static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
t_cmd *cmd;
|
|
|
|
t_list *current;
|
|
|
|
|
|
|
|
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
|
|
|
|
{
|
|
|
|
free(cmds);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-24 07:25:29 -05:00
|
|
|
if (*cmds == NULL)
|
|
|
|
return (0);
|
2023-02-15 14:52:27 -05:00
|
|
|
current = *cmds;
|
2023-02-23 12:03:22 -05:00
|
|
|
cmd = current->content;
|
|
|
|
cmd->fd_in = infile;
|
2023-03-08 12:19:10 -05:00
|
|
|
cmd = ft_lstlast(*cmds)->content;
|
2023-02-15 14:52:27 -05:00
|
|
|
cmd->fd_out = outfile;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
static int ft_cmds_fill(t_data *data, t_list **cmds, const char *line)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
char **tab;
|
|
|
|
char **args;
|
|
|
|
t_list *current;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
tab = ft_split_quoted(line, '|');
|
|
|
|
if (tab == NULL)
|
|
|
|
return (1);
|
|
|
|
i = 0;
|
|
|
|
current = *cmds;
|
|
|
|
while (tab[i] != NULL)
|
|
|
|
{
|
|
|
|
args = ft_split_quoted(tab[i], ' ');
|
2023-02-24 14:30:01 -05:00
|
|
|
if (ft_cmd_filler(data, current, args) == 1)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
ft_lstclear(cmds, ft_cmddel);
|
|
|
|
ft_freer_tab_ultimate(2, args, tab);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
ft_freer_tab_ultimate(1, tab);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
t_list **ft_parse_cmds(t_data *data, char *line, int infile, int outfile)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
t_list **cmds;
|
|
|
|
|
|
|
|
cmds = malloc(sizeof(t_list *));
|
|
|
|
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
|
|
|
|
return (NULL);
|
2023-02-24 14:30:01 -05:00
|
|
|
if (ft_cmds_fill(data, cmds, line) == 1)
|
2023-02-15 14:52:27 -05:00
|
|
|
return (NULL);
|
|
|
|
return (cmds);
|
|
|
|
}
|