Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
fix heredoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 5, 2024
1 parent f464613 commit 5d55c59
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 42 deletions.
2 changes: 1 addition & 1 deletion codes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NAME = minishell

INPUT_FILES = input/input_readline.c \
input/input_terminal.c \
input/input_sighandler.c \
input/input_validator.c
PARSER_FILES = parser/ast_node.c \
parser/get_node_type.c \
Expand Down Expand Up @@ -37,7 +38,6 @@ LOADER_FILES = loader/ldpre_param_expansion.c \
loader/loader_wrapper.c
UTIL_FILES = utils/do_exit.c \
utils/free_ft_split.c \
utils/input_sighandler.c \
utils/ld_chdir.c \
utils/ld_errno_file.c \
utils/ld_map_functions.c \
Expand Down
13 changes: 11 additions & 2 deletions codes/includes/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* input.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:17:56 by kyungjle #+# #+# */
/* Updated: 2024/04/01 13:08:10 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/05 15:44:06 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,4 +36,13 @@ void input_terminal_restore(struct termios *oldterm);

t_bool input_validate(const char *input);

// input_sighandler.c
# define OLDACT_SIGINT 0
# define OLDACT_SIGQUIT 1

void input_sighandler_setup(struct sigaction oldacts[2]);
void input_sighandler_restore(struct sigaction oldacts[2]);
void input_sighandler(int sig, siginfo_t *info, void *ucontext);
void ldexec_sigign_setup(struct sigaction oldacts[2]);

#endif
3 changes: 2 additions & 1 deletion codes/includes/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:18:03 by kyungjle #+# #+# */
/* Updated: 2024/04/04 17:59:00 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:40:13 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,6 +33,7 @@ typedef enum e_bool
} t_bool;

extern volatile sig_atomic_t g_sigint;
# define INPUT_READLINE 2

// used by loader.preprocessor.ast
typedef struct s_ld_heredoc
Expand Down
11 changes: 1 addition & 10 deletions codes/includes/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 19:54:44 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 13:00:27 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:43:58 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,15 +39,6 @@ t_bool ld_chdir(char *path);

void do_exit(char *errorstr);

// input_sighandler.c
# define OLDACT_SIGINT 0
# define OLDACT_SIGQUIT 1

void input_sighandler_setup(struct sigaction oldacts[2]);
void input_sighandler_restore(struct sigaction oldacts[2]);
void input_sighandler(int sig, siginfo_t *info, void *ucontext);
void ldexec_sigign_setup(struct sigaction oldacts[2]);

// ld_map_functions.c

t_ld_map_env *ldpre_env_fromenvp_f(char **envp);
Expand Down
5 changes: 3 additions & 2 deletions codes/input/input_readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* input_readline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:19:01 by kyungjle #+# #+# */
/* Updated: 2024/03/28 02:09:10 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/05 15:47:04 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,6 +36,7 @@ char *input_readline_f(void)
prompt = make_prompt_f();
input_sighandler_setup(oldacts);
input_terminal_setup(&oldterm);
g_sigint = INPUT_READLINE;
input = readline(prompt);
input_sighandler_restore(oldacts);
input_terminal_restore(&oldterm);
Expand Down
17 changes: 10 additions & 7 deletions codes/utils/input_sighandler.c → codes/input/input_sighandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:19:05 by kyungjle #+# #+# */
/* Updated: 2024/03/21 17:30:13 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:53:42 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "input.h"
#include "utils.h"

void ldexec_sigign_setup(struct sigaction oldacts[2]);
Expand Down Expand Up @@ -84,7 +85,6 @@ void input_sighandler_restore(struct sigaction oldacts[2])
{
sigaction(SIGINT, &oldacts[OLDACT_SIGINT], NULL);
sigaction(SIGQUIT, &oldacts[OLDACT_SIGQUIT], NULL);
g_sigint = FALSE;
}

/*
Expand All @@ -106,9 +106,12 @@ void input_sighandler(int sig, siginfo_t *info, void *ucontext)
(void) ucontext;
(void) info;
(void) sig;
g_sigint = TRUE;
write(1, "\n", 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
if (g_sigint == INPUT_READLINE || g_sigint == TRUE)
{
write(1, "\n", 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
g_sigint = TRUE;
}
23 changes: 15 additions & 8 deletions codes/loader/ldpre_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 11:20:45 by kyungjle #+# #+# */
/* Updated: 2024/04/05 15:18:01 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:51:07 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -248,20 +248,27 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
if ((ast->right)->node_type == NODE_FILE)
{
// open right
ldpre_ast_redir_infile((ast->right)->pcmd[0], heredoc, ast->node_type, env);
if (ldpre_ast_redir_infile((ast->right)->pcmd[0], heredoc, ast->node_type, env) == FALSE)
return (-2);
// open left
if ((ast->left)->node_type == NODE_FILE)
ldpre_ast_redir_infile((ast->left)->pcmd[0], heredoc, ast->node_type, env);
else
ldpre_ast(ast->left, env, exec, heredoc);
{
if (ldpre_ast_redir_infile((ast->left)->pcmd[0], heredoc, ast->node_type, env) == FALSE)
return (-2);
}
else if (ldpre_ast(ast->left, env, exec, heredoc) == FALSE)
return (-2);
}
else
{
// open left
if ((ast->left)->node_type == NODE_FILE)
ldpre_ast_redir_infile((ast->left)->pcmd[0], heredoc, ast->node_type, env);
else
ldpre_ast(ast->left, env, exec, heredoc);
{
if (ldpre_ast_redir_infile((ast->left)->pcmd[0], heredoc, ast->node_type, env) == FALSE)
return (-2);
}
else if (ldpre_ast(ast->left, env, exec, heredoc) == FALSE)
return (-2);
return (ldpre_ast(ast->right, env, exec, heredoc));
}
return 0;
Expand Down
44 changes: 33 additions & 11 deletions codes/loader/ldpre_ast_redir_heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 14:37:13 by nicknamemoh #+# #+# */
/* Updated: 2024/04/04 19:13:18 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:01:18 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "loader.h"
#include "utils.h"
#include "input.h"
#include "types.h"

char *ldexec_heredoc_assign_f(void);
t_bool ldexec_heredoc(int fd, char *delim,
t_bool expansion, t_ld_map_env *env);
char *ldexec_heredoc_assign_f(void);
t_bool ldexec_heredoc(int fd, char *delim,
t_bool expansion, t_ld_map_env *env);
static t_bool heredoc_getline(int fd, char *delim,
t_bool expansion, t_ld_map_env *env);

/*
char *ldexec_heredoc_assign_f(void)
Expand Down Expand Up @@ -60,23 +63,43 @@ char *ldexec_heredoc_assign_f(void)
t_bool ldexec_heredoc(int fd, char *delim,
t_bool expansion, t_ld_map_env *env)
{
char *buf;
struct sigaction oldacts[2];
struct termios oldterm;
t_bool ret;

input_sighandler_setup(oldacts);
input_terminal_setup(&oldterm);
g_sigint = FALSE;
ret = heredoc_getline(fd, delim, expansion, env);
input_sighandler_restore(oldacts);
input_terminal_restore(&oldterm);
if (g_sigint != TRUE)
ret = TRUE;
if (!ret)
{
write(1, "\n", 1);
rl_on_new_line();
}
return (ret);
}

static t_bool heredoc_getline(int fd, char *delim,
t_bool expansion, t_ld_map_env *env)
{
t_bool ret;
char *buf;
const size_t delim_len = ft_strlen(delim);

ret = TRUE;
while (ret == TRUE)
{
// TODO ctrl c 입력시 입력 버퍼에 남아있던 부분들이 출력되는 문제 해결
// TODO EOF 입력시 이상하게 출력됨
write(2, "heredoc > ", 10);
write(1, "heredoc > ", 10);
buf = get_next_line(STDIN_FD);
rl_on_new_line();
if (g_sigint == TRUE || buf == NULL || *buf == '\0')
ret = FALSE;
if (ret == TRUE && ft_strlen(buf) - 1 == ft_strlen(delim)
&& ft_strncmp(buf, delim, ft_strlen(delim)) == 0)
if (ret == TRUE && ft_strlen(buf) - 1 == delim_len
&& ft_strncmp(buf, delim, delim_len) == 0)
{
free(buf);
break ;
Expand All @@ -87,6 +110,5 @@ t_bool ldexec_heredoc(int fd, char *delim,
write(fd, buf, ft_strlen(buf));
free(buf);
}
input_sighandler_restore(oldacts);
return (ret);
}

0 comments on commit 5d55c59

Please sign in to comment.