Skip to content

Commit

Permalink
heredoc lol
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Dec 27, 2022
1 parent 4166645 commit 7e2b187
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
12 changes: 9 additions & 3 deletions find_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 14:14:40 by zhabri #+# #+# */
/* Updated: 2022/12/26 16:51:36 by zhabri ### ########.fr */
/* Updated: 2022/12/27 14:46:35 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,7 +73,10 @@ void find_cmd_infile(t_token *token, t_cmd *node, bool *pb)
token->file = remove_quotes(file);
else
free(file);
*pb = ft_open_in(node, token);
if (!g_glob->sig_int)
*pb = ft_open_in(node, token);
else
*pb = false;
}

void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
Expand All @@ -97,7 +100,10 @@ void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
" \t");
free(cmd);
token->file = remove_quotes(file);
*pb = ft_open_out(node, token);
if (!g_glob->sig_int)
*pb = ft_open_out(node, token);
else
*pb = false;
}

void get_cmd(void)
Expand Down
19 changes: 10 additions & 9 deletions heredocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/20 12:33:11 by zhabri #+# #+# */
/* Updated: 2022/12/27 12:17:13 by zhabri ### ########.fr */
/* Updated: 2022/12/27 13:59:47 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft/includes/libft.h"
#include "minishell.h"
#include <unistd.h>

static char *name_generator(void)
{
Expand Down Expand Up @@ -63,11 +64,13 @@ static char *get_limiter(t_token *token)
static void handle_here_doc(t_token *token)
{
int fd;
int stdin_cpy;
char *here_doc_entry;
char *file_name;
char *limiter;

file_name = name_generator();
stdin_cpy = dup(0);
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
Expand All @@ -76,24 +79,22 @@ static void handle_here_doc(t_token *token)
}
here_doc_entry = NULL;
limiter = get_limiter(token);
while (1)
g_glob->here_doc = true;
while (!g_glob->sig_int)
{
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);
}
eof_limiter_not_found(here_doc_entry, limiter);
g_glob->here_doc = false;
if (!g_glob->sig_int)
eof_limiter_not_found(here_doc_entry, limiter);
close(fd);
dup2(stdin_cpy, 0);
token->file = file_name;
g_glob->sig_int = false;
}

void scan_heredocs(void)
Expand Down
6 changes: 4 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/27 12:13:57 by zhabri ### ########.fr */
/* Updated: 2022/12/27 14:43:50 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,7 +40,8 @@ void events(t_list **head)
{
scan_heredocs();
get_cmd();
pipex();
if (!g_glob->sig_int)
pipex();
unlink_heredocs();
clear_cmds();
if (g_glob->cmds)
Expand All @@ -60,6 +61,7 @@ void event_loop(void)
{
g_glob->in_child = false;
g_glob->sig_int = false;
g_glob->here_doc = false;
input = readline("minishell> ");
if (input == NULL)
{
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/27 12:13:00 by zhabri ### ########.fr */
/* Updated: 2022/12/27 13:06:46 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -67,6 +67,7 @@ typedef struct s_glob
unsigned char exit_ret;
bool in_child;
bool sig_int;
bool here_doc;
} t_glob;

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

Expand All @@ -20,10 +20,18 @@ 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);
rl_redisplay();
if (g_glob->here_doc)
{
close(STDIN_FILENO);
printf("\n");
}
else
{
ft_putchar_fd('\n', 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
g_glob->exit_ret = 130;
}
else if (sig == SIGINT && g_glob->in_child)
Expand Down

0 comments on commit 7e2b187

Please sign in to comment.