42_minishell/main.c

237 lines
4.1 KiB
C
Raw Normal View History

2023-02-14 01:26:18 -05:00
#include "libftx/libftx.h"
2023-01-31 08:40:15 -05:00
#include "minishell.h"
2023-02-14 01:26:18 -05:00
void ft_lstdel(void *ptr)
{
t_cmd *content;
content = (t_cmd *) ptr;
if (content->executable != NULL)
free(content->executable);
if (content->args != NULL)
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;
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_lstdel);
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_lstdel);
current = current->next;
i++;
}
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, '|')))
{
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);
}
void ft_strshift(char *str, int shift)
2023-02-01 11:28:38 -05:00
{
size_t i;
2023-02-14 01:26:18 -05:00
if (shift > 0)
return ;
2023-02-01 11:28:38 -05:00
i = 0;
2023-02-14 01:26:18 -05:00
while (str[i - shift] != '\0')
2023-02-01 11:28:38 -05:00
{
2023-02-14 01:26:18 -05:00
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] == '\''))
2023-02-01 11:28:38 -05:00
{
2023-02-14 01:26:18 -05:00
if (start == -1)
start = i;
else if (str[i] == str[start])
2023-02-01 11:28:38 -05:00
{
2023-02-14 01:26:18 -05:00
stop = i;
break ;
2023-02-01 11:28:38 -05:00
}
}
i++;
}
2023-02-14 01:26:18 -05:00
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;
//TODO check if executable exist
//TODO change it by env value
//TODO add switch to bultin
content->executable = ft_strjoin("/usr/bin/", args[0]);
if (content->executable == NULL)
return (1);
return (0);
}
int ft_cmds_fill(t_list **cmds, 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) == 1)
{
ft_lstclear(cmds, ft_lstdel);
ft_freer_tab_ultimate(2, args, tab);
return (1);
}
current = current->next;
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
2023-02-01 11:28:38 -05:00
}
2023-02-14 01:26:18 -05:00
t_list **ft_parse_cmds(char *line)
2023-01-31 09:02:23 -05:00
{
2023-02-14 01:26:18 -05:00
int infile;
int outfile;
2023-02-01 11:28:38 -05:00
t_list **cmds;
2023-02-14 01:26:18 -05:00
cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line);
infile = ft_infile(line);
if (ft_syntatic_verif(line) == 1)
return (NULL);
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(cmds, line) == 1)
return (NULL);
ft_printf("%s\n", line);
return (cmds);
}
int ft_cmds_excutor(t_list **cmds)
{
t_cmd *content;
t_list *current;
2023-02-01 11:28:38 -05:00
size_t i;
i = 0;
2023-02-14 01:26:18 -05:00
current = *cmds;
while (current != NULL)
2023-02-01 11:28:38 -05:00
{
2023-02-14 01:26:18 -05:00
content = current->content;
ft_printf("--- COMMAND %d\n", i);
ft_printf("excutable: %s\n", content->executable);
ft_printf("args:\n%S", content->args);
current = current->next;
2023-02-01 11:28:38 -05:00
i++;
}
2023-02-14 01:26:18 -05:00
return (0);
2023-01-31 09:02:23 -05:00
}
/*int main(int ac, char **av, char **env)
2023-01-31 09:02:23 -05:00
{
2023-02-14 01:26:18 -05:00
t_list **cmds;
t_data data;
2023-02-01 11:28:38 -05:00
if (ac == 1)
return (1);
data.env = init_env(env);
if (data.env == NULL)
return (1);
ft_putstr_fd(get_value_key("PATH", data.env), 1);
ft_putstr_fd("/n", 1);
set_value_key("PATH", i_env, "BITE");
ft_putstr_fd(get_value_key("PATH", data.env), 1);
2023-02-14 01:26:18 -05:00
cmds = ft_parse_cmds(av[1]);
if (cmds == NULL)
return (1);
if (ft_cmds_excutor(cmds) == 1)
{
ft_lstclear(cmds, ft_lstdel);
return (1);
}
ft_lstclear(cmds, ft_lstdel);
free(cmds);
2023-02-01 11:28:38 -05:00
return (1);
}*/