commit e3e81879b1fa27a7b55ed5b856b9caf22011765c
parent acd23551c3322f397dc43f97796273a8958f6ef9
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Fri, 24 May 2019 10:04:59 +0200
tweaks: condense the setup of the two signal handlers for Ctrl+C
Don't bother checking for an error from sigaction(), because the only
things it could error out for is when it is passed an invalid signal
(which SIGINT isn't) or oldaction/newaction would point to memory that
is outside of nano's control (which is obviously not the case).
Also, the setup of signal handlers during startup does not check for
errors, so why do it here?
Diffstat:
2 files changed, 13 insertions(+), 34 deletions(-)
diff --git a/src/nano.c b/src/nano.c
@@ -1055,8 +1055,6 @@ bool scoop_stdin(void)
{
struct sigaction oldaction, newaction;
/* Original and temporary handlers for SIGINT. */
- bool setup_failed = FALSE;
- /* Whether setting up the temporary SIGINT handler failed. */
FILE *stream;
int thetty;
@@ -1074,18 +1072,6 @@ bool scoop_stdin(void)
enable_signals();
#endif
- /* Set things up so that SIGINT will cancel the reading. */
- if (sigaction(SIGINT, NULL, &newaction) == -1) {
- setup_failed = TRUE;
- perror("sigaction");
- } else {
- newaction.sa_handler = make_a_note;
- if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
- setup_failed = TRUE;
- perror("sigaction");
- }
- }
-
/* Open standard input. */
stream = fopen("/dev/stdin", "rb");
if (stream == NULL) {
@@ -1097,6 +1083,11 @@ bool scoop_stdin(void)
return FALSE;
}
+ /* 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);
+
/* Read the input into a new buffer. */
make_new_buffer();
read_file(stream, 0, "stdin", TRUE);
@@ -1116,10 +1107,8 @@ bool scoop_stdin(void)
if (!input_was_aborted)
tcgetattr(0, &oldterm);
- /* If it was changed, restore the handler for SIGINT. */
- if (!setup_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
- perror("sigaction");
-
+ /* Restore the original ^C handler, the terminal setup, and curses mode. */
+ sigaction(SIGINT, &oldaction, NULL);
terminal_init();
doupdate();
diff --git a/src/text.c b/src/text.c
@@ -951,8 +951,6 @@ bool execute_command(const char *command)
const char *shellenv;
struct sigaction oldaction, newaction;
/* Original and temporary handlers for SIGINT. */
- bool setup_failed = FALSE;
- /* Whether setting up the temporary SIGINT handler failed. */
/* Create a pipe to read the command's output from, and, if needed,
* a pipe to feed the command's input through. */
@@ -1046,17 +1044,10 @@ bool execute_command(const char *command)
* SIGINT when Ctrl-C is pressed. */
enable_signals();
- /* Set things up so that Ctrl-C will terminate the forked process. */
- if (sigaction(SIGINT, NULL, &newaction) == -1) {
- setup_failed = TRUE;
- nperror("sigaction");
- } else {
- newaction.sa_handler = cancel_the_command;
- if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
- setup_failed = TRUE;
- nperror("sigaction");
- }
- }
+ /* Set up a signal handler so that ^C will terminate the forked process. */
+ newaction.sa_handler = cancel_the_command;
+ newaction.sa_flags = 0;
+ sigaction(SIGINT, &newaction, &oldaction);
stream = fdopen(from_fd[0], "rb");
if (stream == NULL)
@@ -1075,9 +1066,8 @@ bool execute_command(const char *command)
if (should_pipe && (wait(NULL) == -1))
nperror("wait");
- /* If it was changed, restore the handler for SIGINT. */
- if (!setup_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
- nperror("sigaction");
+ /* Restore the original handler for SIGINT. */
+ sigaction(SIGINT, &oldaction, NULL);
/* Restore the terminal to its desired state, and disable
* interpretation of the special control keys again. */