2016-05-02 10:54:25 +08:00
|
|
|
// Implementation file for the low level input library.
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-03-13 06:07:07 +08:00
|
|
|
#include <cstring>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <unistd.h>
|
2006-08-11 03:02:46 +08:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-05-07 12:22:28 +08:00
|
|
|
#include <sys/time.h>
|
2016-05-02 10:54:25 +08:00
|
|
|
#include <sys/types.h>
|
2019-03-13 05:06:01 +08:00
|
|
|
#include <cwchar>
|
2017-02-11 10:47:02 +08:00
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2016-05-02 10:54:25 +08:00
|
|
|
#include <memory>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2018-02-19 10:39:03 +08:00
|
|
|
#include <utility>
|
2006-01-19 20:22:07 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "common.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "env.h"
|
2016-05-02 10:54:25 +08:00
|
|
|
#include "env_universal_common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "input_common.h"
|
2011-12-27 13:05:25 +08:00
|
|
|
#include "iothread.h"
|
2016-11-23 12:24:03 +08:00
|
|
|
#include "wutil.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-07 12:22:28 +08:00
|
|
|
/// Time in milliseconds to wait for another byte to be available for reading
|
2018-06-18 13:01:32 +08:00
|
|
|
/// after \x1B is read before assuming that escape key was pressed, and not an
|
2016-05-07 12:22:28 +08:00
|
|
|
/// escape sequence.
|
2019-02-07 19:08:24 +08:00
|
|
|
#define WAIT_ON_ESCAPE_DEFAULT 30
|
2016-01-15 13:46:53 +08:00
|
|
|
static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
/// Events which have been read and returned by the sequence matching code.
|
|
|
|
static std::deque<char_event_t> lookahead_list;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
// Queue of pairs of (function pointer, argument) to be invoked. Expected to be mostly empty.
|
2017-01-22 09:15:45 +08:00
|
|
|
typedef std::list<std::function<void(void)>> callback_queue_t;
|
2013-04-04 04:49:58 +08:00
|
|
|
static callback_queue_t callback_queue;
|
2018-02-19 10:33:04 +08:00
|
|
|
static void input_flush_callbacks();
|
2013-04-04 04:49:58 +08:00
|
|
|
|
2018-02-19 10:33:04 +08:00
|
|
|
static bool has_lookahead() { return !lookahead_list.empty(); }
|
2012-11-25 08:58:30 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
static char_event_t lookahead_pop() {
|
|
|
|
auto result = lookahead_list.front();
|
2015-04-06 02:07:17 +08:00
|
|
|
lookahead_list.pop_front();
|
2012-11-25 08:58:30 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
/// \return the next lookahead char, or none if none. Discards timeouts.
|
2019-03-17 06:49:35 +08:00
|
|
|
static maybe_t<char_event_t> lookahead_pop_evt() {
|
2019-03-15 16:11:15 +08:00
|
|
|
while (has_lookahead()) {
|
|
|
|
auto evt = lookahead_pop();
|
2019-03-17 06:49:35 +08:00
|
|
|
if (! evt.is_timeout()) {
|
|
|
|
return evt;
|
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
}
|
|
|
|
return none();
|
|
|
|
}
|
2012-11-25 08:58:30 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
static void lookahead_push_back(char_event_t c) { lookahead_list.push_back(c); }
|
2015-04-06 02:07:17 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
static void lookahead_push_front(char_event_t c) { lookahead_list.push_front(c); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Callback function for handling interrupts on reading.
|
2019-03-17 04:52:07 +08:00
|
|
|
static interrupt_func_t interrupt_handler;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-17 04:52:07 +08:00
|
|
|
void input_common_init(interrupt_func_t func) { interrupt_handler = func; }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Internal function used by input_common_readch to read one byte from fd 0. This function should
|
|
|
|
/// only be called by input_common_readch().
|
2019-03-17 05:08:00 +08:00
|
|
|
static char_event_t readb() {
|
2016-05-02 10:54:25 +08:00
|
|
|
// do_loop must be set on every path through the loop; leaving it uninitialized allows the
|
|
|
|
// static analyzer to assist in catching mistakes.
|
2012-11-19 08:30:30 +08:00
|
|
|
unsigned char arr[1];
|
|
|
|
bool do_loop;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
do {
|
|
|
|
// Flush callbacks.
|
2013-04-04 04:49:58 +08:00
|
|
|
input_flush_callbacks();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
fd_set fdset;
|
2014-04-30 08:03:00 +08:00
|
|
|
int fd_max = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
int ioport = iothread_port();
|
|
|
|
int res;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(0, &fdset);
|
2016-05-02 10:54:25 +08:00
|
|
|
if (ioport > 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
FD_SET(ioport, &fdset);
|
2019-03-13 13:38:42 +08:00
|
|
|
fd_max = std::max(fd_max, ioport);
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
|
|
|
|
// Get our uvar notifier.
|
2014-04-30 08:03:00 +08:00
|
|
|
universal_notifier_t ¬ifier = universal_notifier_t::default_notifier();
|
2016-05-02 10:54:25 +08:00
|
|
|
|
|
|
|
// Get the notification fd (possibly none).
|
2014-05-01 06:50:03 +08:00
|
|
|
int notifier_fd = notifier.notification_fd();
|
2016-05-02 10:54:25 +08:00
|
|
|
if (notifier_fd > 0) {
|
2014-05-01 06:50:03 +08:00
|
|
|
FD_SET(notifier_fd, &fdset);
|
2019-03-13 13:38:42 +08:00
|
|
|
fd_max = std::max(fd_max, notifier_fd);
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
|
|
|
|
// Get its suggested delay (possibly none).
|
2014-04-30 08:03:00 +08:00
|
|
|
struct timeval tv = {};
|
|
|
|
const unsigned long usecs_delay = notifier.usec_delay_between_polls();
|
2016-05-02 10:54:25 +08:00
|
|
|
if (usecs_delay > 0) {
|
2014-04-30 08:03:00 +08:00
|
|
|
unsigned long usecs_per_sec = 1000000;
|
|
|
|
tv.tv_sec = (int)(usecs_delay / usecs_per_sec);
|
|
|
|
tv.tv_usec = (int)(usecs_delay % usecs_per_sec);
|
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
|
2014-04-30 08:03:00 +08:00
|
|
|
res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
|
2016-05-02 10:54:25 +08:00
|
|
|
if (res == -1) {
|
2016-10-23 11:32:25 +08:00
|
|
|
if (errno == EINTR || errno == EAGAIN) {
|
|
|
|
if (interrupt_handler) {
|
2019-03-17 04:52:07 +08:00
|
|
|
if (auto interrupt_evt = interrupt_handler()) {
|
|
|
|
return *interrupt_evt;
|
2019-03-17 06:49:35 +08:00
|
|
|
} else if (auto mc = lookahead_pop_evt()) {
|
2019-03-15 16:11:15 +08:00
|
|
|
return *mc;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
|
|
|
|
do_loop = true;
|
|
|
|
} else {
|
2019-03-17 03:35:49 +08:00
|
|
|
// The terminal has been closed.
|
2019-03-17 05:08:00 +08:00
|
|
|
return char_event_type_t::eof;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
} else {
|
|
|
|
// Assume we loop unless we see a character in stdin.
|
2012-03-26 16:21:10 +08:00
|
|
|
do_loop = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
// Check to see if we want a universal variable barrier.
|
2014-05-14 14:16:53 +08:00
|
|
|
bool barrier_from_poll = notifier.poll();
|
|
|
|
bool barrier_from_readability = false;
|
2016-05-02 10:54:25 +08:00
|
|
|
if (notifier_fd > 0 && FD_ISSET(notifier_fd, &fdset)) {
|
2014-05-14 14:16:53 +08:00
|
|
|
barrier_from_readability = notifier.notification_fd_became_readable(notifier_fd);
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
if (barrier_from_poll || barrier_from_readability) {
|
2014-05-14 14:16:53 +08:00
|
|
|
env_universal_barrier();
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
if (ioport > 0 && FD_ISSET(ioport, &fdset)) {
|
2012-03-26 16:21:10 +08:00
|
|
|
iothread_service_completion();
|
2019-03-17 06:49:35 +08:00
|
|
|
if (auto mc = lookahead_pop_evt()) {
|
2019-03-15 16:11:15 +08:00
|
|
|
return *mc;
|
2012-03-26 16:21:10 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
if (FD_ISSET(STDIN_FILENO, &fdset)) {
|
|
|
|
if (read_blocked(0, arr, 1) != 1) {
|
2019-03-17 03:35:49 +08:00
|
|
|
// The teminal has been closed.
|
2019-03-17 05:08:00 +08:00
|
|
|
return char_event_type_t::eof;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
// We read from stdin, so don't loop.
|
2012-11-19 08:30:30 +08:00
|
|
|
do_loop = false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 10:54:25 +08:00
|
|
|
} while (do_loop);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return arr[0];
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
// Update the wait_on_escape_ms value in response to the fish_escape_delay_ms user variable being
|
|
|
|
// set.
|
2018-09-18 12:26:21 +08:00
|
|
|
void update_wait_on_escape_ms(const environment_t &vars) {
|
|
|
|
auto escape_time_ms = vars.get(L"fish_escape_delay_ms");
|
2016-05-02 10:54:25 +08:00
|
|
|
if (escape_time_ms.missing_or_empty()) {
|
2016-01-15 13:46:53 +08:00
|
|
|
wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
long tmp = fish_wcstol(escape_time_ms->as_string().c_str());
|
2016-11-23 12:24:03 +08:00
|
|
|
if (errno || tmp < 10 || tmp >= 5000) {
|
2019-03-13 05:06:01 +08:00
|
|
|
std::fwprintf(stderr,
|
2016-05-02 10:54:25 +08:00
|
|
|
L"ignoring fish_escape_delay_ms: value '%ls' "
|
2016-11-23 12:24:03 +08:00
|
|
|
L"is not an integer or is < 10 or >= 5000 ms\n",
|
2017-08-28 15:25:41 +08:00
|
|
|
escape_time_ms->as_string().c_str());
|
2016-05-02 10:54:25 +08:00
|
|
|
} else {
|
2016-01-15 13:46:53 +08:00
|
|
|
wait_on_escape_ms = (int)tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
char_event_t input_common_readch() {
|
2019-03-17 06:49:35 +08:00
|
|
|
if (auto mc = lookahead_pop_evt()) {
|
2019-03-15 16:11:15 +08:00
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
wchar_t res;
|
|
|
|
mbstate_t state = {};
|
|
|
|
while (1) {
|
2019-03-17 05:08:00 +08:00
|
|
|
auto evt = readb();
|
2019-03-17 07:48:23 +08:00
|
|
|
if (!evt.is_char()) {
|
2019-03-17 05:08:00 +08:00
|
|
|
return evt;
|
2019-03-17 03:35:49 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-03-17 05:08:00 +08:00
|
|
|
wint_t b = evt.get_char();
|
2019-03-15 16:11:15 +08:00
|
|
|
if (MB_CUR_MAX == 1) {
|
|
|
|
return b; // single-byte locale, all values are legal
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
char bb = b;
|
|
|
|
size_t sz = std::mbrtowc(&res, &bb, 1, &state);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
switch (sz) {
|
|
|
|
case (size_t)(-1): {
|
|
|
|
std::memset(&state, '\0', sizeof(state));
|
|
|
|
debug(2, L"Illegal input");
|
2019-03-17 06:49:35 +08:00
|
|
|
return char_event_type_t::check_exit;
|
2016-07-12 11:31:30 +08:00
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
case (size_t)(-2): {
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
case 0: {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
default: { return res; }
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char_event_t input_common_readch_timed(bool dequeue_timeouts) {
|
|
|
|
char_event_t result{char_event_type_t::timeout};
|
|
|
|
if (has_lookahead()) {
|
|
|
|
result = lookahead_pop();
|
2016-05-02 10:54:25 +08:00
|
|
|
} else {
|
2019-03-15 16:11:15 +08:00
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
|
|
|
struct timeval tm = {wait_on_escape_ms / 1000, 1000 * (wait_on_escape_ms % 1000)};
|
|
|
|
if (select(1, &fds, 0, 0, &tm) > 0) {
|
|
|
|
result = input_common_readch();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
// If we got a timeout, either through dequeuing or creating, ensure it stays on the queue.
|
|
|
|
if (result.is_timeout()) {
|
|
|
|
if (!dequeue_timeouts) lookahead_push_front(char_event_type_t::timeout);
|
|
|
|
return char_event_type_t::timeout;
|
|
|
|
}
|
2019-03-17 07:48:23 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
void input_common_queue_ch(char_event_t ch) { lookahead_push_back(ch); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
void input_common_next_ch(char_event_t ch) { lookahead_push_front(ch); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-22 09:15:45 +08:00
|
|
|
void input_common_add_callback(std::function<void(void)> callback) {
|
2013-04-04 04:49:58 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2017-01-22 09:15:45 +08:00
|
|
|
callback_queue.push_back(std::move(callback));
|
2013-04-04 04:49:58 +08:00
|
|
|
}
|
|
|
|
|
2018-02-19 10:33:04 +08:00
|
|
|
static void input_flush_callbacks() {
|
2016-05-02 10:54:25 +08:00
|
|
|
// We move the queue into a local variable, so that events queued up during a callback don't get
|
|
|
|
// fired until next round.
|
2017-01-22 09:15:45 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2013-04-04 04:49:58 +08:00
|
|
|
callback_queue_t local_queue;
|
|
|
|
std::swap(local_queue, callback_queue);
|
2017-01-22 09:15:45 +08:00
|
|
|
for (auto &f : local_queue) {
|
|
|
|
f();
|
2013-04-04 04:49:58 +08:00
|
|
|
}
|
|
|
|
}
|