diff --git a/src/input.cpp b/src/input.cpp index d784419dc..a228a9e44 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -249,14 +249,18 @@ void input_mapping_add(const wchar_t *sequence, const wchar_t *command, const wc /// Handle interruptions to key reading by reaping finshed jobs and propagating the interrupt to the /// reader. -static int interrupt_handler() { +static maybe_t interrupt_handler() { // Fire any pending events. event_fire_delayed(); // Reap stray processes, including printing exit status messages. if (job_reap(true)) reader_repaint_needed(); // Tell the reader an event occured. if (reader_reading_interrupted()) { - return shell_modes.c_cc[VINTR]; + auto vintr = shell_modes.c_cc[VINTR]; + if (vintr == 0) { + return none(); + } + return vintr; } return R_NULL; diff --git a/src/input_common.cpp b/src/input_common.cpp index c72ba8c7c..526a5d9e6 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -63,9 +63,9 @@ static void lookahead_push_back(char_event_t c) { lookahead_list.push_back(c); } static void lookahead_push_front(char_event_t c) { lookahead_list.push_front(c); } /// Callback function for handling interrupts on reading. -static int (*interrupt_handler)(); +static interrupt_func_t interrupt_handler; -void input_common_init(int (*ih)()) { interrupt_handler = ih; } +void input_common_init(interrupt_func_t func) { interrupt_handler = func; } /// Internal function used by input_common_readch to read one byte from fd 0. This function should /// only be called by input_common_readch(). @@ -115,9 +115,9 @@ static maybe_t readb() { if (res == -1) { if (errno == EINTR || errno == EAGAIN) { if (interrupt_handler) { - int res = interrupt_handler(); - if (res) return res; - if (auto mc = lookahead_pop_char()) { + if (auto interrupt_evt = interrupt_handler()) { + return *interrupt_evt; + } else if (auto mc = lookahead_pop_char()) { return *mc; } } diff --git a/src/input_common.h b/src/input_common.h index 1c8a2db6b..81c857d5d 100644 --- a/src/input_common.h +++ b/src/input_common.h @@ -122,8 +122,12 @@ class char_event_t { } }; -/// Init the library. -void input_common_init(int (*ih)()); +/// A type of function invoked on interrupt. +/// \return the event which is to be returned to the reader loop, or none if VINTR is 0. +using interrupt_func_t = maybe_t (*)(); + +/// Init the library with an interrupt function. +void input_common_init(interrupt_func_t func); /// Adjust the escape timeout. class environment_t;