/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 14:31:03 by ljiriste ### ########.fr */
+/* Updated: 2024/08/29 16:17:10 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell_structs.h"
# include "libft.h"
+# include <signal.h>
+extern volatile sig_atomic_t g_last_signal;
+
+void minishell_flush(void);
char *get_line(void);
void terminate_input(void);
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* input.c :+: :+: :+: */
+/* input_method.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 14:22:08 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 14:31:04 by ljiriste ### ########.fr */
+/* Updated: 2024/08/29 17:11:41 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
if (!line)
{
ft_printf("\n");
+ if (g_last_signal == SIGINT)
+ {
+ g_last_signal = 0;
+ minishell_flush();
+ return (get_line());
+ }
return (line);
}
if (line[ft_strlen(line) - 1] != '\n')
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/08/28 09:38:51 by ljiriste ### ########.fr */
+/* Updated: 2024/08/29 17:11:26 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
+#include <signal.h>
+#include <termios.h>
+#include <unistd.h>
+
+volatile sig_atomic_t g_last_signal;
+
+void handler(int sig_num)
+{
+ g_last_signal = sig_num;
+ return ;
+}
+
+void setup_signal_hadling(void)
+{
+ struct sigaction sa;
+
+ g_last_signal = 0;
+ ft_memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sigaction(SIGINT, &sa, NULL);
+ sa.sa_handler = SIG_IGN;
+ sigaction(SIGQUIT, &sa, NULL);
+ return ;
+}
static void print_help(void)
{
return ;
}
+typedef enum e_termios_action
+{
+ term_save,
+ term_load,
+} t_termios_action;
+
+void termios_control(t_termios_action action, struct termios *termios)
+{
+ static struct termios saved;
+
+ if (action == term_save)
+ saved = *termios;
+ else if (action == term_load)
+ *termios = saved;
+ return ;
+}
+
+void setup_terminal(void)
+{
+ struct termios termios;
+
+ tcgetattr(STDIN_FILENO, &termios);
+ termios_control(term_save, &termios);
+ termios.c_lflag |= NOFLSH;
+ tcsetattr(STDIN_FILENO, TCSANOW, &termios);
+ return ;
+}
+
+void regenerate_terminal(void)
+{
+ struct termios termios;
+
+ termios_control(term_load, &termios);
+ tcsetattr(STDIN_FILENO, TCSANOW, &termios);
+ return ;
+}
+
+void minishell_flush(void)
+{
+ struct termios termios;
+
+ tcgetattr(STDIN_FILENO, &termios);
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
+ return ;
+}
+
/*
// This would be more portable than the main with 3 input args
extern char **environ;
char *line;
t_execution_env env;
+ setup_signal_hadling();
if (argc > 1)
{
print_help();
}
while (env.exit == 0)
{
+ setup_terminal();
line = get_line();
+ regenerate_terminal();
if (!line)
{
env.ret_val = 0;