42_minishell/main.c

99 lines
1.8 KiB
C

#include "libftx/libftx.h"
#include "minishell.h"
#include <stdlib.h>
void ft_lstdel(void *ptr)
{
t_list *element;
t_cmd *content;
element = (t_list *) ptr;
content = (t_cmd *) element->content;
if (content->executable != NULL)
free(content->executable);
if (content->args != NULL)
free(content->args);
free(content);
free(element);
}
int ft_cmds_init(t_list **cmds, size_t len)
{
t_cmd *content;
t_list *current;
size_t i;
current = malloc(sizeof(t_list));
if (current == NULL)
return (1);
i = 0;
while (i < len)
{
content = malloc(sizeof(t_cmd *));
if (content == NULL)
ft_lstclear(cmds, ft_lstdel);
content->args = NULL;
content->executable = NULL;
content->fd_in = -1;
content->fd_out = -1;
current->next = malloc(sizeof(t_list *));
if (current->next == NULL)
ft_lstclear(cmds, ft_lstdel);
current = current->next;
}
}
int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
{
size_t i;
size_t len;
t_cmd *cmd;
t_list *current;
len = ft_seglen_quoted(line, '|');
if (len == 0)
return (0);
cmds = malloc(sizeof(t_list *));
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
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);
}
int ft_cmds_fill(t_list **cmds, const char *line)
{
//TODO
//remplir les executables;
// les args;
}
t_list **ft_parse_cmd(char *line)
{
int infile;
int outfile;
t_list **cmds;
cmds = NULL;
outfile = ft_outfile(line);
infile = ft_infile(line);
if (ft_syntatic_verif(line) == 1)
return (NULL);
ft_cmds_prep(cmds, line, infile, outfile);
ft_printf("%s\n", line);
return (NULL);
}
int main(int ac, char **av)
{
if (ac == 1)
return (1);
ft_parse_cmd(av[1]);
return (1);
}