Skip to content

Commit

Permalink
heredocs sigint to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Dec 27, 2022
1 parent df883f4 commit 4166645
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
22 changes: 15 additions & 7 deletions heredocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/20 12:33:11 by zhabri #+# #+# */
/* Updated: 2022/12/22 14:04:41 by zhabri ### ########.fr */
/* Updated: 2022/12/27 12:17:13 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -74,19 +74,27 @@ static void handle_here_doc(t_token *token)
free(file_name);
return ;
}
here_doc_entry = readline("> ");
here_doc_entry = NULL;
limiter = get_limiter(token);
while (here_doc_entry != NULL
&& strncmp(limiter, here_doc_entry, ft_strlen(limiter) + 1) != 0)
while (1)
{
here_doc_entry = readline("> ");
printf("%s\n", here_doc_entry);
if (!here_doc_entry || g_glob->sig_int \
|| !strncmp(limiter, here_doc_entry, ft_strlen(limiter) + 1))
{
if (g_glob->sig_int)
g_glob->input = here_doc_entry;
break ;
}
ft_putendl_fd(here_doc_entry, fd);
free(here_doc_entry);
here_doc_entry = readline("> ");
}
eof_limiter_not_found(here_doc_entry, limiter);
close(fd);
token->file = file_name;
}
g_glob->sig_int = false;
}

void scan_heredocs(void)
{
Expand Down Expand Up @@ -117,7 +125,7 @@ void unlink_heredocs(void)
if (curr->content)
{
token = ((t_token *)curr->content);
if (token->label == HEREDOC)
if (token->label == HEREDOC && token->file)
unlink(token->file);
}
curr = curr->next;
Expand Down
5 changes: 3 additions & 2 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 11:49:37 by zhabri #+# #+# */
/* Updated: 2022/12/26 15:54:56 by zhabri ### ########.fr */
/* Updated: 2022/12/27 12:13:57 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,6 +59,7 @@ void event_loop(void)
while (1)
{
g_glob->in_child = false;
g_glob->sig_int = false;
input = readline("minishell> ");
if (input == NULL)
{
Expand All @@ -81,7 +82,7 @@ void event_loop(void)

int main(int argc, char **argv, char **envp)
{
init_sig_callbacks(PARENT);
init_sig_callbacks(0);
if (argc > 1)
init_input(argv[1]);
get_envp(envp);
Expand Down
3 changes: 2 additions & 1 deletion minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 20:30:45 by zhabri #+# #+# */
/* Updated: 2022/12/26 16:56:17 by zhabri ### ########.fr */
/* Updated: 2022/12/27 12:13:00 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,6 +66,7 @@ typedef struct s_glob
t_list **cmds;
unsigned char exit_ret;
bool in_child;
bool sig_int;
} t_glob;

extern t_glob *g_glob;
Expand Down
16 changes: 12 additions & 4 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/23 10:32:29 by zhabri #+# #+# */
/* Updated: 2022/12/27 11:13:56 by zhabri ### ########.fr */
/* Updated: 2022/12/27 12:13:30 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft/includes/libft.h"
#include "minishell.h"
#include <readline/readline.h>
#include <signal.h>

static void handler(int sig)
{
if (sig == SIGINT && !g_glob->in_child)
{
g_glob->sig_int = true;
ft_putchar_fd('\n', 1);
rl_on_new_line();
rl_replace_line("", 0);
Expand All @@ -25,6 +28,7 @@ static void handler(int sig)
}
else if (sig == SIGINT && g_glob->in_child)
{
g_glob->sig_int = true;
ft_putchar_fd('\n', 1);
g_glob->exit_ret = 130;
}
Expand All @@ -44,7 +48,11 @@ void ignore_sig(int sig)

void init_sig_callbacks(int process)
{
(void)process;
signal(SIGINT, handler);
signal(SIGQUIT, handler);
if (process == 0)
{
signal(SIGINT, handler);
signal(SIGQUIT, handler);
}
else
signal(SIGINT, ignore_sig);
}

0 comments on commit 4166645

Please sign in to comment.