115 lines
2.8 KiB
C
115 lines
2.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* cmds.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
|
/* Updated: 2023/02/17 15:20:29 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libftx/libftx.h"
|
|
#include "minishell.h"
|
|
|
|
static int ft_cmds_init(t_list **cmds, size_t len)
|
|
{
|
|
t_cmd *content;
|
|
t_list *current;
|
|
size_t i;
|
|
|
|
*cmds = malloc(sizeof(t_list));
|
|
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);
|
|
}
|
|
|
|
static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
|
|
{
|
|
size_t len;
|
|
t_cmd *cmd;
|
|
t_list *current;
|
|
|
|
len = ft_seglen_quoted(line, '|');
|
|
if (len == 0)
|
|
return (0);
|
|
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
|
|
{
|
|
free(cmds);
|
|
return (1);
|
|
}
|
|
current = *cmds;
|
|
cmd = current->content;
|
|
cmd->fd_in = infile;
|
|
while (current->next != NULL)
|
|
current = current->next;
|
|
cmd = current->content;
|
|
cmd->fd_out = outfile;
|
|
return (0);
|
|
}
|
|
|
|
static int ft_cmds_fill(t_list **cmds, t_list **env, const char *line)
|
|
{
|
|
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], ' ');
|
|
if (ft_cmd_filler(current, args, env) == 1)
|
|
{
|
|
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);
|
|
}
|
|
|
|
t_list **ft_parse_cmds(char *line, t_list **env, int infile, int outfile)
|
|
{
|
|
t_list **cmds;
|
|
|
|
cmds = malloc(sizeof(t_list *));
|
|
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
|
|
return (NULL);
|
|
if (ft_cmds_fill(cmds, env, line) == 1)
|
|
return (NULL);
|
|
return (cmds);
|
|
}
|