diff --git a/includes/parser/parsing.h b/includes/parser/parsing.h index 44b7bd9..381d567 100644 --- a/includes/parser/parsing.h +++ b/includes/parser/parsing.h @@ -28,7 +28,7 @@ int check_unclosed(char *c, t_node *node); int check_unclosed_quote(char *c, t_node *node); void interpret_cmd(char **input, t_msh *msh); -void end_heredoc(char *buf, t_msh *msh, t_node *lst); +void end_heredoc(char *buf, t_msh *msh, t_node *lst, char *limiter); void exit_heredoc(char *limiter, t_msh *msh, t_node *lst); #endif diff --git a/srcs/execution/exec_heredoc.c b/srcs/execution/exec_heredoc.c index c22e812..9958647 100644 --- a/srcs/execution/exec_heredoc.c +++ b/srcs/execution/exec_heredoc.c @@ -92,13 +92,13 @@ int ifhere_remove_quote(t_ast_n *node, int j) void read_input(t_ast_n *node, int j) { char *str; - int len; + // int len; int check; check = ifhere_remove_quote(node, j); - len = ft_strlen(node->files[j]); + // len = ft_strlen(node->files[j]); str = get_next_line(node->msh->here_fd, 0); - while (str && ft_strncmp(str, node->files[j], len) != 0) + while (str && ft_strncmp(str, node->files[j], ft_strlen(str) - 1) != 0) { if (!check) expander_here(&str, node); diff --git a/srcs/parsing/heredoc/heredoc.c b/srcs/parsing/heredoc/heredoc.c index 8bbd423..c7e3a6a 100644 --- a/srcs/parsing/heredoc/heredoc.c +++ b/srcs/parsing/heredoc/heredoc.c @@ -12,49 +12,51 @@ #include "../../../includes/minishell.h" -static void remove_quote(char **str, char c) +static void remove_quote(char **new, char *limiter, char c) { - char *new; + // char *new; int i; int k; int len; i = 0; k = 0; - len = ft_strlen(*str); - new = ft_calloc(len - 1, sizeof(char)); + len = ft_strlen(limiter); + *(new) = ft_calloc(len - 1, sizeof(char)); while (i < len - 2) { - if ((&((*str)[k]) == ft_strchr(*str, c)) - || (&((*str)[k]) == ft_strrchr(*str, c))) + if ((&(limiter[k]) == ft_strchr(limiter, c)) + || (&(limiter[k]) == ft_strrchr(limiter, c))) { k++; } else - new[i++] = (*str)[k++]; + (*new)[i++] = limiter[k++]; } - ft_free(str); - *str = new; + // ft_free(str); + // *str = new; } -static int ifremove_quote(char **str) +static int ifremove_quote(char **new, char *limiter) { char c; int ret; ret = 0; - if (!ft_strchr(*str, '\'') && !ft_strchr(*str, '\"')) + if (!ft_strchr(limiter, '\'') && !ft_strchr(limiter, '\"')) c = 0; - else if (!ft_strchr(*str, '\"')) + else if (!ft_strchr(limiter, '\"')) c = '\''; - else if (!ft_strchr(*str, '\'')) + else if (!ft_strchr(limiter, '\'')) c = '\"'; - else if (ft_strchr(*str, '\'') < ft_strchr(*str, '\"')) + else if (ft_strchr(limiter, '\'') < ft_strchr(limiter, '\"')) c = '\''; else c = '\"'; - if (c && (ft_strchr(*str, c) != ft_strrchr(*str, c))) - remove_quote(str, c); + if (c && (ft_strchr(limiter, c) != ft_strrchr(limiter, c))) + remove_quote(new, limiter, c); + else + *new = ft_strdup(limiter); if (c) ret = 1; return (ret); @@ -80,8 +82,8 @@ void read_hereinput(char *limiter, t_node *lst, t_msh *msh) r = read(0, &c, 1); } buf[i] = '\0'; - if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0) - end_heredoc(buf, msh, lst); + if (ft_strncmp(buf, limiter, -1) == 0) + end_heredoc(buf, msh, lst, limiter); buf[i++] = '\n'; buf[i] = '\0'; ft_fprintf(1, "%s", buf); @@ -91,16 +93,17 @@ void parse_heredoc(char *limiter, t_node *lst, t_msh *msh) { int fd; pid_t pid; + char *new; fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666); pid = fork(); if (pid == 0) { - ifremove_quote(&limiter); + ifremove_quote(&new, limiter); dup2(fd, STDOUT_FILENO); close(fd); while (1) - read_hereinput(limiter, lst, msh); + read_hereinput(new, lst, msh); } else if (pid > 0) { diff --git a/srcs/parsing/heredoc/heredoc_utils.c b/srcs/parsing/heredoc/heredoc_utils.c index 749ecbe..64683c2 100644 --- a/srcs/parsing/heredoc/heredoc_utils.c +++ b/srcs/parsing/heredoc/heredoc_utils.c @@ -18,13 +18,15 @@ void exit_heredoc(char *limiter, t_msh *msh, t_node *lst) ft_fprintf(1, "%s\n", limiter); free_linked_list(lst); free_msh(msh); + ft_free(&limiter); exit(EXIT_SUCCESS); } -void end_heredoc(char *buf, t_msh *msh, t_node *lst) +void end_heredoc(char *buf, t_msh *msh, t_node *lst, char *limiter) { ft_fprintf(1, "%s\n", buf); free_msh(msh); free_linked_list(lst); + ft_free(&limiter); exit(EXIT_SUCCESS); } diff --git a/srcs/parsing/tokenizer/linked_list.c b/srcs/parsing/tokenizer/linked_list.c index 8376529..268795d 100644 --- a/srcs/parsing/tokenizer/linked_list.c +++ b/srcs/parsing/tokenizer/linked_list.c @@ -59,7 +59,7 @@ void free_linked_list(t_node *head) while (tmp) { next = tmp->next; - free(tmp->val); + ft_free(&tmp->val); free(tmp); tmp = next; }