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

Commit

Permalink
heredoc: add sighandler
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 5, 2024
1 parent 5d55c59 commit cc6b0ae
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 42 deletions.
4 changes: 2 additions & 2 deletions codes/builtins/builtin_exit.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/03/27 06:04:41 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 12:31:53 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:12:22 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,7 +50,7 @@ static t_bool check_digit(const char *c)
return (TRUE);
}

t_bool builtin_check_argument_count(const char *args[], int limit)
t_bool builtin_check_argument_count(const char *args[], int limit)
{
int count;

Expand Down
14 changes: 9 additions & 5 deletions codes/builtins/builtin_export.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/03/27 06:04:43 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 15:19:43 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:13:32 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,6 @@ int builtin_export(char *args[], t_ld_map_env *env);
static int builtin_export_print(t_ld_map_env *env);
static t_bool cmp(const void *c1, const void *c2);


int builtin_export(char *args[], t_ld_map_env *env)
{
int i;
Expand Down Expand Up @@ -50,12 +49,17 @@ int builtin_export(char *args[], t_ld_map_env *env)
static int builtin_export_print(t_ld_map_env *env)
{
char **envp;
char **envp_ptr;

envp = ldpre_env_toenvp_f(env);
envp_ptr = envp;
ft_qsort((void **)envp, 0, env->count - 1, cmp);
for (int i = 0; envp[i] != NULL; i++)
printf("%s\n", envp[i]);
free_ft_split(envp);
while (*envp != NULL)
{
printf("%s\n", *envp);
envp++;
}
free_ft_split(envp_ptr);
return (EXIT_SUCCESS);
}

Expand Down
10 changes: 5 additions & 5 deletions codes/includes/input.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:17:56 by kyungjle #+# #+# */
/* Updated: 2024/04/05 15:44:06 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:19:20 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,9 +40,9 @@ t_bool input_validate(const char *input);
# 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]);
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
29 changes: 20 additions & 9 deletions codes/input/input_sighandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:19:05 by kyungjle #+# #+# */
/* Updated: 2024/04/05 15:53:42 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 17:49:18 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

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

void ldexec_sigign_setup(struct sigaction oldacts[2]);
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]);
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);
static int rl_event_void(void);

/*
void input_sighandler_setup(struct sigaction oldacts[2])
Expand All @@ -33,8 +34,11 @@ void input_sighandler_setup(struct sigaction oldacts[2])
sigset_t mask;

g_sigint = FALSE;
if (sigemptyset(&mask) != 0 || sigaddset(&mask, SIGINT) != 0)
if (sigemptyset(&mask) != 0
|| sigaddset(&mask, SIGINT) != 0 || sigaddset(&mask, SIGQUIT) != 0)
do_exit("input_sighandler_setup.sigemptyset");
rl_catch_signals = 0;
rl_event_hook = rl_event_void;
action.sa_flags = 0 | SA_SIGINFO;
action.sa_flags &= ~SA_RESTART;
action.sa_mask = mask;
Expand All @@ -50,7 +54,7 @@ void input_sighandler_setup(struct sigaction oldacts[2])
void ldexec_sigign_setup(struct sigaction oldacts[2])
:param oldacts: sigaction array to store default handlers
Similar to input_sighandler_setup, but choose to ignore.
Similar to input_sighandler_setup, but choose to ignore.
Shell environment after forking child should use this function.
Deactivates SIGINT(Ctrl + C) and SIGQUIT(Ctrl + \)
Expand Down Expand Up @@ -93,7 +97,7 @@ void input_sighandler(int sig, siginfo_t *info, void *ucontext)
:param info: defined in sigaction(2)
:param ucontext: defined in sigaction(2)
signal handler for SIGINT in interactive mode.
signal handler for SIGINT in interactive mode.
Modifies g_sigint variable to notice that sigint has occured,
and changes readline state to print next prompt.
Expand All @@ -106,12 +110,19 @@ void input_sighandler(int sig, siginfo_t *info, void *ucontext)
(void) ucontext;
(void) info;
(void) sig;
rl_done = 1;
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;
g_sigint = TRUE;
rl_on_new_line();
}

static int rl_event_void(void)
{
return (0);
}
8 changes: 4 additions & 4 deletions codes/input/input_terminal.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/03/12 22:19:08 by kyungjle #+# #+# */
/* Updated: 2024/04/05 13:57:49 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 17:06:19 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,13 +25,13 @@ void input_terminal_setup(struct termios *oldterm)
struct termios term;

if (tcgetattr(STDIN_FILENO, &term) != 0)
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
"input_terminal_setup.tcgetattr");
*oldterm = term;
term.c_lflag |= ECHO;
term.c_lflag &= ~ECHOCTL;
if (tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0)
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
"input_terminal_setup.tcsetattr");
}

Expand All @@ -42,6 +42,6 @@ void input_terminal_restore(struct termios *oldterm)
void input_terminal_restore(struct termios *oldterm)
{
if (tcsetattr(STDIN_FILENO, TCSANOW, oldterm) != 0)
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
printf(TERM_COLOR_RED "WARN" TERM_COLOR_END
"input_terminal_restore.tcsetattr");
}
4 changes: 2 additions & 2 deletions codes/libft/srcs/libft/ft_strcmp.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 18:34:24 by kyungjle #+# #+# */
/* Updated: 2024/04/04 18:36:30 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:19:12 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,4 +26,4 @@ int ft_strcmp(const char *s1, const char *s2)
i++;
}
return ((unsigned const char)s1[i] - (unsigned const char)s2[i]);
}
}
5 changes: 3 additions & 2 deletions codes/loader/ldexec_run.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/03/14 01:11:55 by nicknamemoh #+# #+# */
/* Updated: 2024/04/04 15:38:18 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:25:50 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,7 @@

pid_t ldexec_run_bin(t_ld_exec exec);
void ldexec_select_type(t_ld_exec exec, t_ld_exec_nodes *node,
t_ld_map_env *env);
t_ld_map_env *env);

pid_t ldexec_run_bin(t_ld_exec exec)
{
Expand All @@ -28,6 +28,7 @@ pid_t ldexec_run_bin(t_ld_exec exec)
return (-127);
}
pid = fork();
// TODO pipe subshell인지 감지, fork를 하지 않음
if (pid < 0)
do_exit("ldexec_run_bin.fork");
else if (pid == 0)
Expand Down
17 changes: 8 additions & 9 deletions codes/loader/ldpre_ast_redir_heredoc.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/01 14:37:13 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 16:01:18 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 17:49:04 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -93,22 +93,21 @@ static t_bool heredoc_getline(int fd, char *delim,
ret = TRUE;
while (ret == TRUE)
{
write(1, "heredoc > ", 10);
buf = get_next_line(STDIN_FD);
rl_on_new_line();
buf = readline("heredoc > ");
if (g_sigint == TRUE || buf == NULL || *buf == '\0')
ret = FALSE;
if (ret == TRUE && ft_strlen(buf) - 1 == delim_len
if (ret == TRUE && ft_strlen(buf) == delim_len
&& ft_strncmp(buf, delim, delim_len) == 0)
{
free(buf);
break ;
}
if (ret == TRUE && expansion)
buf = ldpre_param_expansion_f(buf, env);
if (ret == TRUE)
{
write(fd, buf, ft_strlen(buf));
free(buf);
write(fd, "\n", 1);
free(buf);
}
}
free(buf);
return (ret);
}
10 changes: 6 additions & 4 deletions codes/loader/ldpre_param_quote.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/03/19 18:18:30 by nicknamemoh #+# #+# */
/* Updated: 2024/04/04 18:17:33 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 16:30:16 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,8 @@ static char *quote_removal_f(char *s,
char *ldpre_param_quote_f(char *arg, t_ld_map_env *env, t_bool *wildcard)
:param arg: raw string to remove (and expand)
:param env: environment variables
:param wildcard: t_bool ptr of caller
:param wildcard: t_bool ptr of caller. if wildcard is NULL, than parameter
expansion is not executed.
:return: quote removed string
Performs quote removal. If needed, performs shell parmeter expansion.
Expand Down Expand Up @@ -64,7 +65,8 @@ static char *quote_removal_f(char *s,
char **ptr, t_bool *wildcard, t_ld_map_env *env)
:param s: arg
:param ptr: arg ptr of caller
:param wildcard: t_bool ptr of caller
:param wildcard: t_bool ptr of caller. if wildcard is NULL, than parameter
expansion is not executed.
:param env: environment variables
:return: parsed argument
Expand Down Expand Up @@ -94,7 +96,7 @@ static char *quote_removal_f(char *s,
}
if (ret == NULL)
do_exit("ldpre_param_quote.quote_removal_f.malloc");
if (*s != '\'')
if (*s != '\'' && wildcard != NULL)
ret = ldpre_param_expansion_f(ret, env);
*ptr = end_ptr;
return (ret);
Expand Down

0 comments on commit cc6b0ae

Please sign in to comment.