commit 8c5b67379fe3af530a98903a2e56f1327f0ca823
parent b2926eb6753cce993b7df2710593a0684bea09f3
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Sun, 26 May 2019 13:10:47 +0200
tweaks: factor out the installing and restoring of the ^C signal handler
So that these two functions can be used elsewhere too.
Diffstat:
4 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -32,9 +32,6 @@
#define LOCKBUFSIZE 8192
-static bool control_C_was_pressed;
- /* Whether ^C was pressed while reading a file. */
-
/* A signal handler for when ^C is typed while reading from a file. */
RETSIGTYPE cancel_the_reading(int signal)
{
diff --git a/src/global.c b/src/global.c
@@ -44,6 +44,9 @@ bool focusing = TRUE;
bool as_an_at = TRUE;
/* Whether a 0x0A byte should be shown as a ^@ instead of a ^J. */
+bool control_C_was_pressed = FALSE;
+ /* Whether Ctrl+C was pressed (when a keyboard interrupt is enabled). */
+
bool suppress_cursorpos = FALSE;
/* Should we skip constant position display for current keystroke? */
diff --git a/src/nano.c b/src/nano.c
@@ -62,8 +62,8 @@ static struct termios oldterm;
static struct sigaction act;
/* Used to set up all our fun signal handlers. */
-static bool input_was_aborted = FALSE;
- /* Whether reading from standard input was aborted via ^C. */
+static struct sigaction oldaction, newaction;
+ /* Containers for the original and the temporary handler for SIGINT. */
/* Create a new linestruct node. Note that we do not set prevnode->next. */
linestruct *make_new_node(linestruct *prevnode)
@@ -1044,17 +1044,34 @@ void close_and_go(void)
finish();
}
-/* Make a note that reading from stdin was concluded with ^C. */
+/* Note that Ctrl+C was pressed during some system call. */
RETSIGTYPE make_a_note(int signal)
{
- input_was_aborted = TRUE;
+ control_C_was_pressed = TRUE;
+}
+
+/* Make ^C interrupt a system call and set a flag. */
+void install_handler_for_Ctrl_C(void)
+{
+ /* Enable the generation of a SIGINT when ^C is pressed. */
+ enable_signals();
+
+ /* Set up a signal handler so that pressing ^C will set a flag. */
+ newaction.sa_handler = make_a_note;
+ newaction.sa_flags = 0;
+ sigaction(SIGINT, &newaction, &oldaction);
+}
+
+/* Go back to ignoring ^C. */
+void restore_handler_for_Ctrl_C(void)
+{
+ sigaction(SIGINT, &oldaction, NULL);
+ disable_signals();
}
/* Read whatever comes from standard input into a new buffer. */
bool scoop_stdin(void)
{
- struct sigaction oldaction, newaction;
- /* Original and temporary handlers for SIGINT. */
FILE *stream;
int thetty;
@@ -1067,9 +1084,6 @@ bool scoop_stdin(void)
fprintf(stderr, _("Reading data from keyboard; "
"type ^D or ^D^D to finish.\n"));
- /* Enable the generation of a SIGINT when Ctrl+C is pressed. */
- enable_signals();
-
/* Open standard input. */
stream = fopen("/dev/stdin", "rb");
if (stream == NULL) {
@@ -1082,9 +1096,7 @@ bool scoop_stdin(void)
}
/* Set up a signal handler so that ^C will stop the reading. */
- newaction.sa_handler = make_a_note;
- newaction.sa_flags = 0;
- sigaction(SIGINT, &newaction, &oldaction);
+ install_handler_for_Ctrl_C();
/* Read the input into a new buffer. */
make_new_buffer();
@@ -1102,11 +1114,11 @@ bool scoop_stdin(void)
close(thetty);
/* If things went well, store the current state of the terminal. */
- if (!input_was_aborted)
+ if (!control_C_was_pressed)
tcgetattr(0, &oldterm);
/* Restore the original ^C handler, the terminal setup, and curses mode. */
- sigaction(SIGINT, &oldaction, NULL);
+ restore_handler_for_Ctrl_C();
terminal_init();
doupdate();
diff --git a/src/proto.h b/src/proto.h
@@ -38,6 +38,8 @@ extern bool focusing;
extern bool as_an_at;
+extern bool control_C_was_pressed;
+
extern bool suppress_cursorpos;
extern message_type lastmessage;