general improvement
This commit is contained in:
74
srcs/parsing/heredoc.c
Normal file
74
srcs/parsing/heredoc.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void read_hereinput(char *limiter)
|
||||
{
|
||||
char buf[100000];
|
||||
char c;
|
||||
int r;
|
||||
int i;
|
||||
|
||||
r = 0;
|
||||
i = 0;
|
||||
ft_fprintf(2, "heredoc> ");
|
||||
r = read(0, &c, 1);
|
||||
if (r == 0)
|
||||
{
|
||||
ft_fprintf (2, "etdsttdt\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
while (r && c != '\n' && c != '\0')
|
||||
{
|
||||
if (c != '\n' && c != '\0')
|
||||
buf[i++] = c;
|
||||
r = read(0, &c, 1);
|
||||
}
|
||||
buf[i] = '\0';
|
||||
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0)
|
||||
{
|
||||
ft_fprintf(1, "%s", buf);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
buf[i++] = '\n';
|
||||
buf[i] = '\0';
|
||||
ft_fprintf(1, "%s", buf);
|
||||
}
|
||||
|
||||
void parse_heredoc(char *limiter)
|
||||
{
|
||||
int fd;
|
||||
pid_t pid;
|
||||
|
||||
fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
close (fd);
|
||||
while (1)
|
||||
read_hereinput(limiter);
|
||||
}
|
||||
else if (pid > 0)
|
||||
{
|
||||
waitpid(pid, NULL, 0);
|
||||
close (fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
close (fd);
|
||||
perror("fork");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void create_heredoc(t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if (lst->pressision == HEREDOC && lst->next && lst->next->pressision)
|
||||
{
|
||||
lst = lst->next;
|
||||
parse_heredoc(lst->val);
|
||||
}
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ t_ast_n *parser(char *input, t_msh *msh)
|
||||
lst = tokenize(input);
|
||||
if (!lst)
|
||||
return (NULL);
|
||||
create_heredoc(lst);
|
||||
if (DEBUG)
|
||||
{
|
||||
dio = drawio_init("ast.xml");
|
||||
|
||||
@@ -109,13 +109,13 @@ void debug_token_list(t_node* lst, char *msg)
|
||||
cpy = lst;
|
||||
if (DEBUG)
|
||||
{
|
||||
ft_debug("========================={%s}\n", msg);
|
||||
ft_debug("==================================================================={%s}\n", msg);
|
||||
while (cpy)
|
||||
{
|
||||
ft_debug("|%s|\n", cpy->val);
|
||||
ft_fprintf(2, "| %10s | TOKEN : %3d | PRESSISION : %3d |\n", cpy->val, cpy->token, cpy->pressision);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
ft_debug("=========================\n\n");
|
||||
ft_debug("===================================================================\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user