#include "libftx/libftx.h" #include "minishell.h" 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) { 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; //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); } t_list **ft_parse_cmds(char *line) { int infile; int outfile; t_list **cmds; 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; size_t i; i = 0; current = *cmds; while (current != NULL) { 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; i++; } return (0); } int main(int ac, char **av) { t_list **cmds; if (ac == 1) return (1); 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); return (1); }