tweaks: factor out the installing and restoring of the ^C signal handler

So that these two functions can be used elsewhere too.
master
Benno Schulenberg 2019-05-26 13:10:47 +02:00
parent b2926eb675
commit 8c5b67379f
4 changed files with 31 additions and 17 deletions

View File

@ -32,9 +32,6 @@
#define LOCKBUFSIZE 8192 #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. */ /* A signal handler for when ^C is typed while reading from a file. */
RETSIGTYPE cancel_the_reading(int signal) RETSIGTYPE cancel_the_reading(int signal)
{ {

View File

@ -44,6 +44,9 @@ bool focusing = TRUE;
bool as_an_at = TRUE; bool as_an_at = TRUE;
/* Whether a 0x0A byte should be shown as a ^@ instead of a ^J. */ /* 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; bool suppress_cursorpos = FALSE;
/* Should we skip constant position display for current keystroke? */ /* Should we skip constant position display for current keystroke? */

View File

@ -62,8 +62,8 @@ static struct termios oldterm;
static struct sigaction act; static struct sigaction act;
/* Used to set up all our fun signal handlers. */ /* Used to set up all our fun signal handlers. */
static bool input_was_aborted = FALSE; static struct sigaction oldaction, newaction;
/* Whether reading from standard input was aborted via ^C. */ /* Containers for the original and the temporary handler for SIGINT. */
/* Create a new linestruct node. Note that we do not set prevnode->next. */ /* Create a new linestruct node. Note that we do not set prevnode->next. */
linestruct *make_new_node(linestruct *prevnode) linestruct *make_new_node(linestruct *prevnode)
@ -1044,17 +1044,34 @@ void close_and_go(void)
finish(); 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) 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. */ /* Read whatever comes from standard input into a new buffer. */
bool scoop_stdin(void) bool scoop_stdin(void)
{ {
struct sigaction oldaction, newaction;
/* Original and temporary handlers for SIGINT. */
FILE *stream; FILE *stream;
int thetty; int thetty;
@ -1067,9 +1084,6 @@ bool scoop_stdin(void)
fprintf(stderr, _("Reading data from keyboard; " fprintf(stderr, _("Reading data from keyboard; "
"type ^D or ^D^D to finish.\n")); "type ^D or ^D^D to finish.\n"));
/* Enable the generation of a SIGINT when Ctrl+C is pressed. */
enable_signals();
/* Open standard input. */ /* Open standard input. */
stream = fopen("/dev/stdin", "rb"); stream = fopen("/dev/stdin", "rb");
if (stream == NULL) { if (stream == NULL) {
@ -1082,9 +1096,7 @@ bool scoop_stdin(void)
} }
/* Set up a signal handler so that ^C will stop the reading. */ /* Set up a signal handler so that ^C will stop the reading. */
newaction.sa_handler = make_a_note; install_handler_for_Ctrl_C();
newaction.sa_flags = 0;
sigaction(SIGINT, &newaction, &oldaction);
/* Read the input into a new buffer. */ /* Read the input into a new buffer. */
make_new_buffer(); make_new_buffer();
@ -1102,11 +1114,11 @@ bool scoop_stdin(void)
close(thetty); close(thetty);
/* If things went well, store the current state of the terminal. */ /* If things went well, store the current state of the terminal. */
if (!input_was_aborted) if (!control_C_was_pressed)
tcgetattr(0, &oldterm); tcgetattr(0, &oldterm);
/* Restore the original ^C handler, the terminal setup, and curses mode. */ /* Restore the original ^C handler, the terminal setup, and curses mode. */
sigaction(SIGINT, &oldaction, NULL); restore_handler_for_Ctrl_C();
terminal_init(); terminal_init();
doupdate(); doupdate();

View File

@ -38,6 +38,8 @@ extern bool focusing;
extern bool as_an_at; extern bool as_an_at;
extern bool control_C_was_pressed;
extern bool suppress_cursorpos; extern bool suppress_cursorpos;
extern message_type lastmessage; extern message_type lastmessage;