From 0327d0f5d779ea82cf8d833a6a99720559316bec Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 23 Feb 2023 18:03:22 +0100 Subject: [PATCH] core: heredocs is now forked to prepare signal --- cmds.c | 6 +++--- heredoc.c | 35 ++++++++++++++----------------- infile.c | 2 +- libftx/gnl/Makefile | 2 +- libftx/gnl/get_next_line_utils.o | Bin 5424 -> 0 bytes main.c | 6 +++--- tags | 9 ++++++-- 7 files changed, 31 insertions(+), 29 deletions(-) delete mode 100644 libftx/gnl/get_next_line_utils.o diff --git a/cmds.c b/cmds.c index df201a1..6e5f661 100644 --- a/cmds.c +++ b/cmds.c @@ -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); } diff --git a/heredoc.c b/heredoc.c index eba9502..cc9ae71 100644 --- a/heredoc.c +++ b/heredoc.c @@ -12,6 +12,7 @@ #include "libftx/libftx.h" #include "minishell.h" +#include 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 pid; + 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; + close(fds[1]); + return (fds[0]); } - *ft_get_heredoc() = 0; - return (fd); } diff --git a/infile.c b/infile.c index dc5b3d9..1e53087 100644 --- a/infile.c +++ b/infile.c @@ -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++; diff --git a/libftx/gnl/Makefile b/libftx/gnl/Makefile index c2ce94d..66104cb 100644 --- a/libftx/gnl/Makefile +++ b/libftx/gnl/Makefile @@ -36,4 +36,4 @@ fclean: clean re: fclean all -.PHONY: all bonus clean fclean re +.PHONY: all clean fclean re diff --git a/libftx/gnl/get_next_line_utils.o b/libftx/gnl/get_next_line_utils.o deleted file mode 100644 index fccc944f2266e8251807bd5929c440453fbed53d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5424 zcmb_geQX>@6`$R^Szpc{>yIQjO+yS#DWLk!Hlb=7*KtX%Y>ybB2qQNTo$d49N4eXJ zZucyw;HJtsV8;lYN<|1ktNzml@saXJTi_34Xhh;5z^y=W#Yawv3L6z_5N*PTWZs*d z+1s0(k&t-O-pu>GH^2Ah&Ft>%osJ!SP*oHnL?HvDstJ^kp0;X#kaY)1D|ikYbFUla zH+qb@=?!D9v}Tm2H;6v78(fU}*qWhVjBWHT&s{Zcls1fmu{C|B8BLdCH<4sL&-`Gh zesOwj4IyJLRsoU<(a+X+9!A^c*edgxkF6T>L&!d~VH~)km-nD`9!Aa&t^On!!v3mJ zUQ&Q@*ni_pP$|kd0a8(MN;$AR}AH{QNFA! zVs(Va3S_z>XS(8MT7ib}ay$s#b*9h9HXz4}z8DOZZ$hE&LM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gtO06uw^8?!u?N4_!*ARNci&{|B{41 z%keKuIDRIfJR{*NE=(>+IGzMhy4bI~$Nl?!P{NnEJ}Tj29Uqr)QO}=p9K-wh8=Arz ziAmn1Hivnn!l|@YKoyGm}Ua$MPmg6-|@my;CVa zpVmLHGSemmGVnjdldvhUOG_>=y75IsBj_^-Ibyk9YY0pNS1Uj43M!9YgYFZ{U!Ek1+A zc(MLp1We8ehdj^YzbYjr`_6%Hz4-6t8&$-?__(BU{LcUuIJ#MY=5e?@8RX*yH=oWe Zx&9VLNGURoS)44$y*y!Ta7pF({{d;DXBPkf diff --git a/main.c b/main.c index 5a54f8a..aa23316 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,7 @@ #include "libftx/libftx.h" #include "minishell.h" -#include +#include 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); } } diff --git a/tags b/tags index c841307..1d70637 100644 --- a/tags +++ b/tags @@ -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