42_minishell/cmds.c

115 lines
2.8 KiB
C
Raw Normal View History

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
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
2023-02-15 14:52:27 -05:00
#include "minishell.h"
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;
*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)
2023-02-15 14:52:27 -05:00
{
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);
}
cmd = (t_cmd *)(*cmds)->content;
cmd->fd_in = infile;
current = *cmds;
while (current->next != NULL)
current = current->next;
cmd = (t_cmd *) current->content;
cmd->fd_out = outfile;
return (0);
}
static int ft_cmds_fill(t_list **cmds, t_list **env, 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], ' ');
if (ft_cmd_filler(current, args, env) == 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-17 07:21:50 -05:00
t_list **ft_parse_cmds(char *line, t_list **env, 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);
if (ft_cmds_fill(cmds, env, line) == 1)
2023-02-15 14:52:27 -05:00
return (NULL);
return (cmds);
}