core: heredocs is now forked to prepare signal

This commit is contained in:
Camille Chauvet 2023-02-23 18:03:22 +01:00
parent 2f386686c6
commit 0327d0f5d7
7 changed files with 31 additions and 29 deletions

6
cmds.c
View File

@ -63,12 +63,12 @@ static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile
free(cmds);
return (1);
}
cmd = (t_cmd *)(*cmds)->content;
cmd->fd_in = infile;
current = *cmds;
cmd = current->content;
cmd->fd_in = infile;
while (current->next != NULL)
current = current->next;
cmd = (t_cmd *) current->content;
cmd = current->content;
cmd->fd_out = outfile;
return (0);
}

View File

@ -12,6 +12,7 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include <stdlib.h>
int *ft_get_heredoc()
{
@ -20,50 +21,46 @@ int *ft_get_heredoc()
return (&heredoc);
}
int ft_heredoc_creator(char *stop)
int ft_heredoc_creator(char *stop, int fd)
{
int fds[2];
char *line;
if (pipe(fds) == -1)
return (-1);
line = readline("> ");
while (line != NULL)
{
if (ft_strcmp(line, stop) == 0)
break ;
ft_putendl_fd(line, fds[1]);
ft_putendl_fd(line, fd);
free(line);
line = readline("> ");
if (line == NULL)
{
close(fds[0]);
close(fds[1]);
return (-1);
return (1);
}
}
close(fds[1]);
return (fds[0]);
return (0);
}
int ft_heredoc(char *stop)
{
int pid;
int fd;
int fds[2];
int exit_code;
if (pipe(fds) == -1)
return (-1);
pid = fork();
if (pid == -1)
return (-1);
if (pid == 0)
{
fd = ft_heredoc_creator(stop);
exit (fd);
exit_code = ft_heredoc_creator(stop, fds[1]);
exit (exit_code + 1);
}
else
{
*ft_get_heredoc() = pid;
waitpid(pid, &fd, 0);
}
waitpid(pid, &exit_code, 0);
*ft_get_heredoc() = 0;
return (fd);
close(fds[1]);
return (fds[0]);
}
}

View File

@ -87,7 +87,7 @@ static int ft_remove_infile(char *line)
{
if (tab[i][0] == '<')
ft_strshift(line + y,
-1 * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 2));
-1 * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1));
else
y = y + ft_strlen(tab[i]) + (y != 0);
i++;

View File

@ -36,4 +36,4 @@ fclean: clean
re: fclean all
.PHONY: all bonus clean fclean re
.PHONY: all clean fclean re

Binary file not shown.

6
main.c
View File

@ -12,7 +12,7 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include <unistd.h>
#include <readline/readline.h>
static char *ft_get_user_input(t_list **env)
{
@ -113,9 +113,9 @@ void ft_ctrlc(int num)
}
else
{
rl_on_new_line();
ft_putchar_fd('\n', 1);
rl_redisplay();
rl_on_new_line();
rl_replace_line("", 0);
}
}

9
tags
View File

@ -60,6 +60,7 @@ LIBFTX_H libftx/libftx.h /^# define LIBFTX_H$/;" d
LIBFT_H libftx/libft/libft.h /^# define LIBFT_H$/;" d
MINISHELL_H minishell.h /^# define MINISHELL_H$/;" d
UTILS_H utils/utils.h /^# define UTILS_H$/;" d
add_export builtins/export.c /^void add_export(t_list **env, char *args, int fd, int *err)$/;" f typeref:typename:void
add_sort env.c /^void add_sort(t_list **head, t_env *var)$/;" f typeref:typename:void
args minishell.h /^ char **args;$/;" m struct:s_cmd typeref:typename:char **
check_argument builtins/echo.c /^int check_argument(char *str, int *check)$/;" f typeref:typename:int
@ -72,8 +73,11 @@ echo builtins/echo.c /^int echo(int fd, char **strs)$/;" f typeref:typename:int
env minishell.h /^ t_list **env;$/;" m struct:s_data typeref:typename:t_list **
env_del env2.c /^void env_del(void *ptr)$/;" f typeref:typename:void
env_to_strs env2.c /^char **env_to_strs(t_list **head)$/;" f typeref:typename:char **
error builtins/export.c /^int error(char *str, int fd)$/;" f typeref:typename:int
error builtins/unset.c /^int error(char *str, int fd)$/;" f typeref:typename:int
executable minishell.h /^ char *executable;$/;" m struct:s_cmd typeref:typename:char *
exit_code minishell.h /^ int exit_code;$/;" m struct:s_data typeref:typename:int
export builtins/export.c /^int export(t_list **env, char **args, int fd)$/;" f typeref:typename:int
fd_in minishell.h /^ int fd_in;$/;" m struct:s_cmd typeref:typename:int
fd_out minishell.h /^ int fd_out;$/;" m struct:s_cmd typeref:typename:int
ft_atoi libftx/libft/ft_atoi.c /^int ft_atoi(const char *nptr)$/;" f typeref:typename:int
@ -236,8 +240,9 @@ main main.c /^int main(int ac, char **av, char **env)$/;" f typeref:typename:int
move_folder builtins/cd.c /^int move_folder(char *path, int fd)$/;" f typeref:typename:int
next libftx/libft/libft.h /^ struct s_list *next;$/;" m struct:s_list typeref:struct:s_list *
next libftx/libftx.h /^ struct s_list *next;$/;" m struct:s_list typeref:struct:s_list *
print_env builtins/env.c /^int print_env(t_list **head, int fd)$/;" f typeref:typename:int
print_export builtins/export.c /^int print_export(t_list **head, int fd)$/;" f typeref:typename:int
possible_key env3.c /^int possible_key(char *key)$/;" f typeref:typename:int
print_env builtins/env.c /^int print_env(t_list **env, int fd)$/;" f typeref:typename:int
print_export builtins/export.c /^void print_export(t_list **env, int fd)$/;" f typeref:typename:void
pwd builtins/pwd.c /^int pwd(int fd)$/;" f typeref:typename:int
s_cmd minishell.h /^typedef struct s_cmd$/;" s
s_data minishell.h /^typedef struct s_data$/;" s