42_minishell/main.c

99 lines
1.8 KiB
C
Raw Normal View History

#include "libftx/libftx.h"
2023-01-31 08:40:15 -05:00
#include "minishell.h"
#include <stdlib.h>
2023-01-31 08:40:15 -05:00
void ft_lstdel(void *ptr)
2023-01-31 09:02:23 -05:00
{
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;
2023-02-01 11:28:38 -05:00
size_t i;
current = malloc(sizeof(t_list));
if (current == NULL)
return (1);
2023-02-01 11:28:38 -05:00
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);
2023-02-02 11:27:26 -05:00
ft_printf("%s\n", line);
return (NULL);
2023-01-31 09:02:23 -05:00
}
int main(int ac, char **av)
{
2023-02-01 11:28:38 -05:00
if (ac == 1)
return (1);
ft_parse_cmd(av[1]);
return (1);
2023-01-31 09:02:23 -05:00
}