Some further steps towards removing R_NULL

Introduce char_event_type_t::check_exit to represent "check for exit"
instead of R_NULL.
This commit is contained in:
ridiculousfish 2019-03-16 15:49:35 -07:00
parent 70a92a9710
commit 28b79a2c88
5 changed files with 25 additions and 19 deletions

View file

@ -348,12 +348,12 @@ static void input_mapping_execute(const input_mapping_t &m, bool allow_commands)
} }
if (has_commands && !allow_commands) { if (has_commands && !allow_commands) {
// We don't want to run commands yet. Put the characters back and return R_NULL. // We don't want to run commands yet. Put the characters back and return check_exit.
for (wcstring::const_reverse_iterator it = m.seq.rbegin(), end = m.seq.rend(); it != end; for (wcstring::const_reverse_iterator it = m.seq.rbegin(), end = m.seq.rend(); it != end;
++it) { ++it) {
input_common_next_ch(*it); input_common_next_ch(*it);
} }
input_common_next_ch(R_NULL); input_common_next_ch(char_event_type_t::check_exit);
return; // skip the input_set_bind_mode return; // skip the input_set_bind_mode
} else if (has_functions && !has_commands) { } else if (has_functions && !has_commands) {
// Functions are added at the head of the input queue. // Functions are added at the head of the input queue.
@ -374,11 +374,11 @@ static void input_mapping_execute(const input_mapping_t &m, bool allow_commands)
parser_t::principal_parser().eval(cmd, io_chain_t(), TOP); parser_t::principal_parser().eval(cmd, io_chain_t(), TOP);
} }
proc_set_last_statuses(std::move(last_statuses)); proc_set_last_statuses(std::move(last_statuses));
input_common_next_ch(R_NULL); input_common_next_ch(char_event_type_t::check_exit);
} else { } else {
// Invalid binding, mixed commands and functions. We would need to execute these one by // Invalid binding, mixed commands and functions. We would need to execute these one by
// one. // one.
input_common_next_ch(R_NULL); input_common_next_ch(char_event_type_t::check_exit);
} }
// Empty bind mode indicates to not reset the mode (#2871) // Empty bind mode indicates to not reset the mode (#2871)
@ -504,9 +504,9 @@ char_event_t input_readch(bool allow_commands) {
} else { } else {
input_common_next_ch(evt); input_common_next_ch(evt);
input_mapping_execute_matching_or_generic(allow_commands); input_mapping_execute_matching_or_generic(allow_commands);
// Regarding allow_commands, we're in a loop, but if a fish command // Regarding allow_commands, we're in a loop, but if a fish command is executed,
// is executed, R_NULL is unread, so the next pass through the loop // check_exit is unread, so the next pass through the loop we'll break out and return
// we'll break out and return it. // it.
} }
} }
} }

View file

@ -31,8 +31,8 @@ void init_input();
/// key press, and is returned as such. /// key press, and is returned as such.
/// ///
/// The argument determines whether fish commands are allowed to be run as bindings. If false, when /// The argument determines whether fish commands are allowed to be run as bindings. If false, when
/// a character is encountered that would invoke a fish command, it is unread and R_NULL is /// a character is encountered that would invoke a fish command, it is unread and
/// returned. /// char_event_type_t::check_exit is returned.
char_event_t input_readch(bool allow_commands = true); char_event_t input_readch(bool allow_commands = true);
/// Enqueue a character or a readline function to the queue of unread characters that input_readch /// Enqueue a character or a readline function to the queue of unread characters that input_readch

View file

@ -50,10 +50,12 @@ static char_event_t lookahead_pop() {
} }
/// \return the next lookahead char, or none if none. Discards timeouts. /// \return the next lookahead char, or none if none. Discards timeouts.
static maybe_t<wchar_t> lookahead_pop_char() { static maybe_t<char_event_t> lookahead_pop_evt() {
while (has_lookahead()) { while (has_lookahead()) {
auto evt = lookahead_pop(); auto evt = lookahead_pop();
if (evt.is_char()) return evt.get_char(); if (! evt.is_timeout()) {
return evt;
}
} }
return none(); return none();
} }
@ -116,7 +118,7 @@ static char_event_t readb() {
if (interrupt_handler) { if (interrupt_handler) {
if (auto interrupt_evt = interrupt_handler()) { if (auto interrupt_evt = interrupt_handler()) {
return *interrupt_evt; return *interrupt_evt;
} else if (auto mc = lookahead_pop_char()) { } else if (auto mc = lookahead_pop_evt()) {
return *mc; return *mc;
} }
} }
@ -142,7 +144,7 @@ static char_event_t readb() {
if (ioport > 0 && FD_ISSET(ioport, &fdset)) { if (ioport > 0 && FD_ISSET(ioport, &fdset)) {
iothread_service_completion(); iothread_service_completion();
if (auto mc = lookahead_pop_char()) { if (auto mc = lookahead_pop_evt()) {
return *mc; return *mc;
} }
} }
@ -183,7 +185,7 @@ void update_wait_on_escape_ms(const environment_t &vars) {
} }
char_event_t input_common_readch() { char_event_t input_common_readch() {
if (auto mc = lookahead_pop_char()) { if (auto mc = lookahead_pop_evt()) {
return *mc; return *mc;
} }
wchar_t res; wchar_t res;
@ -208,7 +210,7 @@ char_event_t input_common_readch() {
case (size_t)(-1): { case (size_t)(-1): {
std::memset(&state, '\0', sizeof(state)); std::memset(&state, '\0', sizeof(state));
debug(2, L"Illegal input"); debug(2, L"Illegal input");
return R_NULL; return char_event_type_t::check_exit;
} }
case (size_t)(-2): { case (size_t)(-2): {
break; break;

View file

@ -11,8 +11,6 @@
enum { enum {
R_MIN = INPUT_COMMON_BASE, R_MIN = INPUT_COMMON_BASE,
// R_NULL is sometimes returned by the input when a character was requested but none could be
// delivered, or when an exception happened.
R_NULL = R_MIN, R_NULL = R_MIN,
R_BEGINNING_OF_LINE, R_BEGINNING_OF_LINE,
@ -89,7 +87,11 @@ enum class char_event_type_t {
timeout, timeout,
/// end-of-file was reached. /// end-of-file was reached.
eof eof,
/// An event was handled internally, or an interrupt was received. Check to see if the reader
/// loop should exit.
check_exit,
}; };
class char_event_t { class char_event_t {
@ -105,6 +107,8 @@ class char_event_t {
bool is_eof() const { return type == char_event_type_t::eof; } bool is_eof() const { return type == char_event_type_t::eof; }
bool is_check_exit() const { return type == char_event_type_t::check_exit; }
bool is_readline() const { bool is_readline() const {
return is_char() && c_ >= R_BEGIN_INPUT_FUNCTIONS && c_ < R_END_INPUT_FUNCTIONS; return is_char() && c_ >= R_BEGIN_INPUT_FUNCTIONS && c_ < R_END_INPUT_FUNCTIONS;
} }

View file

@ -2501,7 +2501,7 @@ maybe_t<wcstring> reader_data_t::readline(int nchars) {
} }
} }
if (!event_needing_handling) { if (!event_needing_handling || event_needing_handling->is_check_exit()) {
event_needing_handling = R_NULL; event_needing_handling = R_NULL;
} else if (event_needing_handling->is_eof()) { } else if (event_needing_handling->is_eof()) {
reader_force_exit(); reader_force_exit();