save commit
This commit is contained in:
18
minishell-codex/src/core/init.c
Normal file
18
minishell-codex/src/core/init.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "minishell.h"
|
||||
|
||||
void ms_init(t_shell *sh, char **envp)
|
||||
{
|
||||
memset(sh, 0, sizeof(*sh));
|
||||
sh->interactive = isatty(STDIN_FILENO);
|
||||
sh->exit_status = 0;
|
||||
sh->last_status = 0;
|
||||
sh->exit_requested = 0;
|
||||
env_init(sh, envp);
|
||||
ms_setup_signals(sh);
|
||||
}
|
||||
|
||||
void ms_cleanup(t_shell *sh)
|
||||
{
|
||||
env_clear(sh);
|
||||
rl_clear_history();
|
||||
}
|
||||
46
minishell-codex/src/core/loop.c
Normal file
46
minishell-codex/src/core/loop.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "minishell.h"
|
||||
|
||||
static void handle_line(t_shell *sh, char *line)
|
||||
{
|
||||
int error = 0;
|
||||
t_token *toks = NULL;
|
||||
t_pipeline *p = NULL;
|
||||
|
||||
toks = lex_line(line, &error);
|
||||
if (error)
|
||||
{
|
||||
free_tokens(toks);
|
||||
return;
|
||||
}
|
||||
p = parse_tokens(toks, &error);
|
||||
free_tokens(toks);
|
||||
if (error || !p)
|
||||
{
|
||||
free_pipeline(p);
|
||||
return;
|
||||
}
|
||||
if (expand_pipeline(p, sh) != 0)
|
||||
{
|
||||
free_pipeline(p);
|
||||
return;
|
||||
}
|
||||
sh->exit_status = execute_pipeline(p, sh);
|
||||
sh->last_status = sh->exit_status;
|
||||
free_pipeline(p);
|
||||
}
|
||||
|
||||
void ms_loop(t_shell *sh)
|
||||
{
|
||||
char *line;
|
||||
|
||||
while (!sh->exit_requested)
|
||||
{
|
||||
line = readline(MS_PROMPT);
|
||||
if (!line)
|
||||
break;
|
||||
if (line[0] != '\0')
|
||||
add_history(line);
|
||||
handle_line(sh, line);
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
46
minishell-codex/src/core/signals.c
Normal file
46
minishell-codex/src/core/signals.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "minishell.h"
|
||||
|
||||
int g_signal = 0;
|
||||
|
||||
static void sigint_handler(int sig)
|
||||
{
|
||||
g_signal = sig;
|
||||
write(STDOUT_FILENO, "\n", 1);
|
||||
rl_on_new_line();
|
||||
rl_replace_line("", 0);
|
||||
rl_redisplay();
|
||||
}
|
||||
|
||||
static void sigquit_handler(int sig)
|
||||
{
|
||||
g_signal = sig;
|
||||
(void)sig;
|
||||
}
|
||||
|
||||
void ms_setup_signals(t_shell *sh)
|
||||
{
|
||||
struct sigaction sa_int;
|
||||
struct sigaction sa_quit;
|
||||
|
||||
(void)sh;
|
||||
memset(&sa_int, 0, sizeof(sa_int));
|
||||
memset(&sa_quit, 0, sizeof(sa_quit));
|
||||
sa_int.sa_handler = sigint_handler;
|
||||
sa_quit.sa_handler = sigquit_handler;
|
||||
sigemptyset(&sa_int.sa_mask);
|
||||
sigemptyset(&sa_quit.sa_mask);
|
||||
sigaction(SIGINT, &sa_int, NULL);
|
||||
sigaction(SIGQUIT, &sa_quit, NULL);
|
||||
}
|
||||
|
||||
void ms_set_child_signals(void)
|
||||
{
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGQUIT, SIG_DFL);
|
||||
}
|
||||
|
||||
void ms_set_heredoc_signals(void)
|
||||
{
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGQUIT, SIG_IGN);
|
||||
}
|
||||
Reference in New Issue
Block a user