#include "../includes/minishell.h"
static void handler_1(int signo)
{
char *str;
int cnt;
if (signo == SIGINT)
{
str = ft_strdup(rl_line_buffer);
cnt = 15 + ft_strlen(str);
// printf("cnt : %d\n", cnt);
rl_replace_line("", 0);
// 현재까직 입력된 프롬포트의 문자열을 str로 바꿔준다.
//ft_putstr_fd("🐚minishell$ ",1);
ft_putstr_fd("\033[2K",1);
fprintf(stderr,"\033[%dD",cnt);
// ft_putstr_fd("\b\b \n", 1);
ft_putstr_fd("🐚minishell$ ",1);
ft_putstr_fd(str,1);
ft_putstr_fd("\n",1);
rl_on_new_line();
//rl_redisplay 를 실행시키기 위해 필요한 함수
rl_redisplay();
// rl_replace_line 을 출력 하지 않으면 작동하지 않는다.
// readline 함수의 인자로 넣은 문자열을 다시 출력한다.
free(str);
}
}
static void handler_2(int signo)
{
if (signo == SIGINT)
ft_putstr_fd("\n",1);
else if (signo == SIGQUIT)
ft_putendl_fd("Quit: 3", 1);
//else if (signal == SIGTERM)
}
void reset_signal(void)
{
signal(SIGINT, handler_2);
signal(SIGQUIT, handler_2);
}
void set_signal(void)
{
signal(SIGINT, handler_1);
signal(SIGQUIT, SIG_IGN);
}
signal.c 에서는
Ctrl + C 와
Ctrl + \ 만 정의 한다
Ctrl + D 는 ?
시그널이 아니라 EOF 이다
그래서 main.c 에서 exit가 입력으로 들어왔을때랑 같이 처리해준다
void eof_history(char *str)
{
if (str == NULL)
{
// ft_putstr_fd("\033[1A",2);
// ft_putstr_fd("\033[6C",2);
ft_putstr_fd("\033[15D",1);
ft_putstr_fd("🐚minishell$ ",1);
ft_putstr_fd("exit\n",2);
exit(EXIT_SUCCESS);
}
else
{
if (ft_strcmp(str, "exit") == 0)
{
ft_putendl_fd("exit",2);
free(str);
exit(EXIT_SUCCESS);
}
add_history(str);
}
}
eof_history 에서
Ctrl+D가 들어오면 readline 에서 읽어 온게 null이닌까 bash 에서 와 똑같이 작동하게끔 해놨다 (삭제하고 prompt 메세지 띄우고 exit 출력하고 exit() 실행되게
아니면 else 문 에 들어가고
PS.) 미니쉘 안에 미니쉘 에 들어가 시그널을 보내면 bash 환경과 다르게 시그널이 여러번 보내지고 받는다.