Switch the input interrupt function to return maybe_t

Allow returning none() to mean do nothing.
This commit is contained in:
ridiculousfish 2019-03-16 13:52:07 -07:00
parent 1e5c1c82c7
commit 46dfad52d9
3 changed files with 17 additions and 9 deletions

View file

@ -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 /// Handle interruptions to key reading by reaping finshed jobs and propagating the interrupt to the
/// reader. /// reader.
static int interrupt_handler() { static maybe_t<int> interrupt_handler() {
// Fire any pending events. // Fire any pending events.
event_fire_delayed(); event_fire_delayed();
// Reap stray processes, including printing exit status messages. // Reap stray processes, including printing exit status messages.
if (job_reap(true)) reader_repaint_needed(); if (job_reap(true)) reader_repaint_needed();
// Tell the reader an event occured. // Tell the reader an event occured.
if (reader_reading_interrupted()) { 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; return R_NULL;

View file

@ -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); } static void lookahead_push_front(char_event_t c) { lookahead_list.push_front(c); }
/// Callback function for handling interrupts on reading. /// 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 /// Internal function used by input_common_readch to read one byte from fd 0. This function should
/// only be called by input_common_readch(). /// only be called by input_common_readch().
@ -115,9 +115,9 @@ static maybe_t<wint_t> readb() {
if (res == -1) { if (res == -1) {
if (errno == EINTR || errno == EAGAIN) { if (errno == EINTR || errno == EAGAIN) {
if (interrupt_handler) { if (interrupt_handler) {
int res = interrupt_handler(); if (auto interrupt_evt = interrupt_handler()) {
if (res) return res; return *interrupt_evt;
if (auto mc = lookahead_pop_char()) { } else if (auto mc = lookahead_pop_char()) {
return *mc; return *mc;
} }
} }

View file

@ -122,8 +122,12 @@ class char_event_t {
} }
}; };
/// Init the library. /// A type of function invoked on interrupt.
void input_common_init(int (*ih)()); /// \return the event which is to be returned to the reader loop, or none if VINTR is 0.
using interrupt_func_t = maybe_t<int> (*)();
/// Init the library with an interrupt function.
void input_common_init(interrupt_func_t func);
/// Adjust the escape timeout. /// Adjust the escape timeout.
class environment_t; class environment_t;