heredoc fix

This commit is contained in:
Loic Deridder
2025-02-14 13:15:52 +01:00
parent bf6877e18c
commit bc365a1796
5 changed files with 31 additions and 26 deletions

View File

@@ -28,7 +28,7 @@ int check_unclosed(char *c, t_node *node);
int check_unclosed_quote(char *c, t_node *node); int check_unclosed_quote(char *c, t_node *node);
void interpret_cmd(char **input, t_msh *msh); 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); void exit_heredoc(char *limiter, t_msh *msh, t_node *lst);
#endif #endif

View File

@@ -92,13 +92,13 @@ int ifhere_remove_quote(t_ast_n *node, int j)
void read_input(t_ast_n *node, int j) void read_input(t_ast_n *node, int j)
{ {
char *str; char *str;
int len; // int len;
int check; int check;
check = ifhere_remove_quote(node, j); 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); 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) if (!check)
expander_here(&str, node); expander_here(&str, node);

View File

@@ -12,49 +12,51 @@
#include "../../../includes/minishell.h" #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 i;
int k; int k;
int len; int len;
i = 0; i = 0;
k = 0; k = 0;
len = ft_strlen(*str); len = ft_strlen(limiter);
new = ft_calloc(len - 1, sizeof(char)); *(new) = ft_calloc(len - 1, sizeof(char));
while (i < len - 2) while (i < len - 2)
{ {
if ((&((*str)[k]) == ft_strchr(*str, c)) if ((&(limiter[k]) == ft_strchr(limiter, c))
|| (&((*str)[k]) == ft_strrchr(*str, c))) || (&(limiter[k]) == ft_strrchr(limiter, c)))
{ {
k++; k++;
} }
else else
new[i++] = (*str)[k++]; (*new)[i++] = limiter[k++];
} }
ft_free(str); // ft_free(str);
*str = new; // *str = new;
} }
static int ifremove_quote(char **str) static int ifremove_quote(char **new, char *limiter)
{ {
char c; char c;
int ret; int ret;
ret = 0; ret = 0;
if (!ft_strchr(*str, '\'') && !ft_strchr(*str, '\"')) if (!ft_strchr(limiter, '\'') && !ft_strchr(limiter, '\"'))
c = 0; c = 0;
else if (!ft_strchr(*str, '\"')) else if (!ft_strchr(limiter, '\"'))
c = '\''; c = '\'';
else if (!ft_strchr(*str, '\'')) else if (!ft_strchr(limiter, '\''))
c = '\"'; c = '\"';
else if (ft_strchr(*str, '\'') < ft_strchr(*str, '\"')) else if (ft_strchr(limiter, '\'') < ft_strchr(limiter, '\"'))
c = '\''; c = '\'';
else else
c = '\"'; c = '\"';
if (c && (ft_strchr(*str, c) != ft_strrchr(*str, c))) if (c && (ft_strchr(limiter, c) != ft_strrchr(limiter, c)))
remove_quote(str, c); remove_quote(new, limiter, c);
else
*new = ft_strdup(limiter);
if (c) if (c)
ret = 1; ret = 1;
return (ret); return (ret);
@@ -80,8 +82,8 @@ void read_hereinput(char *limiter, t_node *lst, t_msh *msh)
r = read(0, &c, 1); r = read(0, &c, 1);
} }
buf[i] = '\0'; buf[i] = '\0';
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0) if (ft_strncmp(buf, limiter, -1) == 0)
end_heredoc(buf, msh, lst); end_heredoc(buf, msh, lst, limiter);
buf[i++] = '\n'; buf[i++] = '\n';
buf[i] = '\0'; buf[i] = '\0';
ft_fprintf(1, "%s", buf); ft_fprintf(1, "%s", buf);
@@ -91,16 +93,17 @@ void parse_heredoc(char *limiter, t_node *lst, t_msh *msh)
{ {
int fd; int fd;
pid_t pid; pid_t pid;
char *new;
fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666); fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666);
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
ifremove_quote(&limiter); ifremove_quote(&new, limiter);
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
close(fd); close(fd);
while (1) while (1)
read_hereinput(limiter, lst, msh); read_hereinput(new, lst, msh);
} }
else if (pid > 0) else if (pid > 0)
{ {

View File

@@ -18,13 +18,15 @@ void exit_heredoc(char *limiter, t_msh *msh, t_node *lst)
ft_fprintf(1, "%s\n", limiter); ft_fprintf(1, "%s\n", limiter);
free_linked_list(lst); free_linked_list(lst);
free_msh(msh); free_msh(msh);
ft_free(&limiter);
exit(EXIT_SUCCESS); 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); ft_fprintf(1, "%s\n", buf);
free_msh(msh); free_msh(msh);
free_linked_list(lst); free_linked_list(lst);
ft_free(&limiter);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@@ -59,7 +59,7 @@ void free_linked_list(t_node *head)
while (tmp) while (tmp)
{ {
next = tmp->next; next = tmp->next;
free(tmp->val); ft_free(&tmp->val);
free(tmp); free(tmp);
tmp = next; tmp = next;
} }