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?master
parent
acd23551c3
commit
e3e81879b1
25
src/nano.c
25
src/nano.c
|
@ -1055,8 +1055,6 @@ bool scoop_stdin(void)
|
||||||
{
|
{
|
||||||
struct sigaction oldaction, newaction;
|
struct sigaction oldaction, newaction;
|
||||||
/* Original and temporary handlers for SIGINT. */
|
/* Original and temporary handlers for SIGINT. */
|
||||||
bool setup_failed = FALSE;
|
|
||||||
/* Whether setting up the temporary SIGINT handler failed. */
|
|
||||||
FILE *stream;
|
FILE *stream;
|
||||||
int thetty;
|
int thetty;
|
||||||
|
|
||||||
|
@ -1074,18 +1072,6 @@ bool scoop_stdin(void)
|
||||||
enable_signals();
|
enable_signals();
|
||||||
#endif
|
#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. */
|
/* Open standard input. */
|
||||||
stream = fopen("/dev/stdin", "rb");
|
stream = fopen("/dev/stdin", "rb");
|
||||||
if (stream == NULL) {
|
if (stream == NULL) {
|
||||||
|
@ -1097,6 +1083,11 @@ bool scoop_stdin(void)
|
||||||
return FALSE;
|
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. */
|
/* Read the input into a new buffer. */
|
||||||
make_new_buffer();
|
make_new_buffer();
|
||||||
read_file(stream, 0, "stdin", TRUE);
|
read_file(stream, 0, "stdin", TRUE);
|
||||||
|
@ -1116,10 +1107,8 @@ bool scoop_stdin(void)
|
||||||
if (!input_was_aborted)
|
if (!input_was_aborted)
|
||||||
tcgetattr(0, &oldterm);
|
tcgetattr(0, &oldterm);
|
||||||
|
|
||||||
/* If it was changed, restore the handler for SIGINT. */
|
/* Restore the original ^C handler, the terminal setup, and curses mode. */
|
||||||
if (!setup_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
|
sigaction(SIGINT, &oldaction, NULL);
|
||||||
perror("sigaction");
|
|
||||||
|
|
||||||
terminal_init();
|
terminal_init();
|
||||||
doupdate();
|
doupdate();
|
||||||
|
|
||||||
|
|
22
src/text.c
22
src/text.c
|
@ -951,8 +951,6 @@ bool execute_command(const char *command)
|
||||||
const char *shellenv;
|
const char *shellenv;
|
||||||
struct sigaction oldaction, newaction;
|
struct sigaction oldaction, newaction;
|
||||||
/* Original and temporary handlers for SIGINT. */
|
/* 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,
|
/* Create a pipe to read the command's output from, and, if needed,
|
||||||
* a pipe to feed the command's input through. */
|
* 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. */
|
* SIGINT when Ctrl-C is pressed. */
|
||||||
enable_signals();
|
enable_signals();
|
||||||
|
|
||||||
/* Set things up so that Ctrl-C will terminate the forked process. */
|
/* Set up a signal handler so that ^C will terminate the forked process. */
|
||||||
if (sigaction(SIGINT, NULL, &newaction) == -1) {
|
newaction.sa_handler = cancel_the_command;
|
||||||
setup_failed = TRUE;
|
newaction.sa_flags = 0;
|
||||||
nperror("sigaction");
|
sigaction(SIGINT, &newaction, &oldaction);
|
||||||
} else {
|
|
||||||
newaction.sa_handler = cancel_the_command;
|
|
||||||
if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
|
|
||||||
setup_failed = TRUE;
|
|
||||||
nperror("sigaction");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = fdopen(from_fd[0], "rb");
|
stream = fdopen(from_fd[0], "rb");
|
||||||
if (stream == NULL)
|
if (stream == NULL)
|
||||||
|
@ -1075,9 +1066,8 @@ bool execute_command(const char *command)
|
||||||
if (should_pipe && (wait(NULL) == -1))
|
if (should_pipe && (wait(NULL) == -1))
|
||||||
nperror("wait");
|
nperror("wait");
|
||||||
|
|
||||||
/* If it was changed, restore the handler for SIGINT. */
|
/* Restore the original handler for SIGINT. */
|
||||||
if (!setup_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
|
sigaction(SIGINT, &oldaction, NULL);
|
||||||
nperror("sigaction");
|
|
||||||
|
|
||||||
/* Restore the terminal to its desired state, and disable
|
/* Restore the terminal to its desired state, and disable
|
||||||
* interpretation of the special control keys again. */
|
* interpretation of the special control keys again. */
|
||||||
|
|
Loading…
Reference in New Issue