Refactor termios
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 07:48:47 +0000 (09:48 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 07:52:52 +0000 (09:52 +0200)
Makefile
inc/minishell.h
src/main.c
src/termios.c [new file with mode: 0644]

index 3e73d4ce00715b26101c076faf6e7594a5a60f78..26c0c027ae2562c58008cc61e328b1748b9968a7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ SRCDIR := src
 SOURCES :=     main.c                          \
                        env.c                           \
                        vars.c                          \
+                       termios.c                       \
                        input_handling.c        \
                        wildcards.c                     \
                        wildcards_inner.c       \
index 1dbc8b8d88ba21ad7f916b96974a81111ac98bfd..3516221a4308fa80b8f767487620d030251eb2ac 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:22:57 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/09/01 09:13:19 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/09/01 09:47:19 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -24,6 +24,8 @@ void  handler(int sig_num);
 char   *get_line(void);
 void   terminate_input(void);
 void   set_input_handler(void);
+void   setup_terminal(void);
+void   regenerate_terminal(void);
 
 void   handle_input(char **line, t_execution_env *env);
 
index e5e6f702119cfa91ae8aa9d668a47cb4e00bd894..6305a43fc77626e708068b270d9f6d74b767e8de 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/26 13:11:47 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/09/01 08:25:02 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/09/01 09:47:36 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,8 +16,6 @@
 #include <stdlib.h>
 
 #include <signal.h>
-#include <termios.h>
-#include <unistd.h>
 
 volatile sig_atomic_t  g_last_signal;
 
@@ -46,43 +44,6 @@ 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 ;
-}
-
 /*
 // This would be more portable than the main with 3 input args
 extern char    **environ;
diff --git a/src/termios.c b/src/termios.c
new file mode 100644 (file)
index 0000000..0b9f906
--- /dev/null
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   termios.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/09/01 09:46:34 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/09/01 09:47:37 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <unistd.h>
+#include <termios.h>
+
+static struct termios  *access_termios_saved(void)
+{
+       static struct termios   saved;
+
+       return (&saved);
+}
+
+void   setup_terminal(void)
+{
+       struct termios  termios;
+
+       tcgetattr(STDIN_FILENO, &termios);
+       *access_termios_saved() = termios;
+       termios.c_lflag |= NOFLSH;
+       tcsetattr(STDIN_FILENO, TCSANOW, &termios);
+       return ;
+}
+
+void   regenerate_terminal(void)
+{
+       struct termios  termios;
+
+       termios = *access_termios_saved();
+       tcsetattr(STDIN_FILENO, TCSANOW, &termios);
+       return ;
+}