42_minishell/main.c

209 lines
3.5 KiB
C
Raw Normal View History

#include "libftx/libftx.h"
2023-01-31 08:40:15 -05:00
#include "minishell.h"
void ft_lstdel(void *ptr)
2023-01-31 09:02:23 -05:00
{
t_cmd *content;
2023-02-14 01:08:25 -05:00
content = (t_cmd *) ptr;
if (content->executable != NULL)
free(content->executable);
if (content->args != NULL)
2023-02-09 12:47:05 -05:00
ft_freer_tab_ultimate(1, content->args);
free(content);
}
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;
2023-02-09 12:47:05 -05:00
*cmds = malloc(sizeof(t_list));
current = *cmds;
2023-02-01 11:28:38 -05:00
i = 0;
while (i < len)
{
2023-02-09 12:47:05 -05:00
content = malloc(sizeof(t_cmd));
if (content == NULL)
2023-02-09 12:47:05 -05:00
{
ft_lstclear(cmds, ft_lstdel);
2023-02-09 12:47:05 -05:00
return (1);
}
content->args = NULL;
content->executable = NULL;
content->fd_in = -1;
content->fd_out = -1;
2023-02-09 12:47:05 -05:00
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_lstdel);
current = current->next;
2023-02-09 12:47:05 -05:00
i++;
}
2023-02-09 12:47:05 -05:00
return (0);
}
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, '|')))
2023-02-09 12:47:05 -05:00
{
free(cmds);
return (1);
2023-02-09 12:47:05 -05:00
}
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);
}
2023-02-09 12:47:05 -05:00
void ft_strshift(char *str, int shift)
{
size_t i;
if (shift > 0)
return ;
i = 0;
while (str[i - shift] != '\0')
{
str[i] = str[i - shift];
i++;
}
str[i + shift] = '\0';
}
void ft_quote_remover(char *str)
{
size_t i;
ssize_t start;
ssize_t stop;
start = -1;
i = 0;
while (str[i] != '\0')
{
if ((str[i] == '\"' || str[i] == '\''))
{
if (start == -1)
start = i;
else if (str[i] == str[start])
{
stop = i;
break ;
}
}
i++;
}
if (start != -1)
{
ft_strshift(str, -1);
str[stop] = '\0';
}
}
int ft_cmd_filler(t_list *element, char **args)
{
t_cmd *content;
size_t i;
if (args == NULL)
return (1);
content = (t_cmd *)element->content;
i = 0;
while (args[i] != NULL)
{
ft_quote_remover(args[i]);
i++;
}
content->args = args;
2023-02-14 07:38:40 -05:00
content->executable = args[0];
2023-02-09 12:47:05 -05:00
return (0);
}
int ft_cmds_fill(t_list **cmds, const char *line)
{
2023-02-09 12:47:05 -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) == 1)
{
ft_lstclear(cmds, ft_lstdel);
2023-02-14 01:08:25 -05:00
ft_freer_tab_ultimate(2, args, tab);
2023-02-09 12:47:05 -05:00
return (1);
}
current = current->next;
i++;
}
2023-02-14 01:08:25 -05:00
ft_freer_tab_ultimate(1, tab);
2023-02-09 12:47:05 -05:00
return (0);
}
2023-02-09 12:47:05 -05:00
t_list **ft_parse_cmds(char *line)
{
int infile;
int outfile;
t_list **cmds;
2023-02-09 12:47:05 -05:00
cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line);
infile = ft_infile(line);
if (ft_syntatic_verif(line) == 1)
return (NULL);
2023-02-09 12:47:05 -05:00
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(cmds, line) == 1)
return (NULL);
2023-02-02 11:27:26 -05:00
ft_printf("%s\n", line);
2023-02-09 12:47:05 -05:00
return (cmds);
}
2023-02-14 07:38:40 -05:00
int main(int ac, char **av, char **env)
2023-01-31 09:02:23 -05:00
{
2023-02-09 12:47:05 -05:00
t_list **cmds;
2023-02-14 07:38:40 -05:00
t_data data;
2023-02-09 12:47:05 -05:00
2023-02-01 11:28:38 -05:00
if (ac == 1)
return (1);
2023-02-14 07:38:40 -05:00
data.env = init_env(env);
if (data.env == NULL)
return (1);
2023-02-09 12:47:05 -05:00
cmds = ft_parse_cmds(av[1]);
if (cmds == NULL)
return (1);
2023-02-14 07:38:40 -05:00
if (ft_cmds_executor(cmds, env) == 1)
2023-02-14 01:08:25 -05:00
{
2023-02-14 07:38:40 -05:00
ft_lstclear(data.env, env_del);
2023-02-14 01:08:25 -05:00
ft_lstclear(cmds, ft_lstdel);
2023-02-09 12:47:05 -05:00
return (1);
2023-02-14 01:08:25 -05:00
}
2023-02-14 07:38:40 -05:00
ft_lstclear(data.env, env_del);
2023-02-14 01:08:25 -05:00
ft_lstclear(cmds, ft_lstdel);
2023-02-14 07:38:40 -05:00
return (0);
2023-01-31 09:02:23 -05:00
}