2016-05-02 02:54:25 +00:00
|
|
|
// Implementation file for the low level input library.
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-05-02 02:54:25 +00:00
|
|
|
#include <string.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <unistd.h>
|
2016-05-02 02:54:25 +00:00
|
|
|
#include <deque>
|
2012-11-25 00:58:30 +00:00
|
|
|
#include <list>
|
2014-06-18 17:14:35 +00:00
|
|
|
#include <queue>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <utility>
|
2006-08-10 19:02:46 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-05-07 04:22:28 +00:00
|
|
|
#include <sys/time.h>
|
2016-05-02 02:54:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <cwctype>
|
|
|
|
#include <memory>
|
2006-01-19 12:22:07 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "env.h"
|
2016-05-02 02:54:25 +00:00
|
|
|
#include "env_universal_common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "input_common.h"
|
2011-12-27 05:05:25 +00:00
|
|
|
#include "iothread.h"
|
2016-05-02 02:54:25 +00:00
|
|
|
#include "util.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
/// Time in milliseconds to wait for another byte to be available for reading
|
|
|
|
/// after \x1b is read before assuming that escape key was pressed, and not an
|
|
|
|
/// escape sequence.
|
2016-02-01 01:37:51 +00:00
|
|
|
#define WAIT_ON_ESCAPE_DEFAULT 300
|
2016-01-15 05:46:53 +00:00
|
|
|
static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
/// Characters that have been read and returned by the sequence matching code.
|
2015-04-05 18:07:17 +00:00
|
|
|
static std::deque<wint_t> lookahead_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// Queue of pairs of (function pointer, argument) to be invoked. Expected to be mostly empty.
|
2013-04-03 20:49:58 +00:00
|
|
|
typedef std::pair<void (*)(void *), void *> callback_info_t;
|
|
|
|
typedef std::queue<callback_info_t, std::list<callback_info_t> > callback_queue_t;
|
|
|
|
static callback_queue_t callback_queue;
|
|
|
|
static void input_flush_callbacks(void);
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static bool has_lookahead(void) { return !lookahead_list.empty(); }
|
2012-11-25 00:58:30 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static wint_t lookahead_pop(void) {
|
2015-04-05 18:07:17 +00:00
|
|
|
wint_t result = lookahead_list.front();
|
|
|
|
lookahead_list.pop_front();
|
2012-11-25 00:58:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static void lookahead_push_back(wint_t c) { lookahead_list.push_back(c); }
|
2012-11-25 00:58:30 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static void lookahead_push_front(wint_t c) { lookahead_list.push_front(c); }
|
2015-04-05 18:07:17 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static wint_t lookahead_front(void) { return lookahead_list.front(); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
/// Callback function for handling interrupts on reading.
|
2005-09-20 13:26:39 +00:00
|
|
|
static int (*interrupt_handler)();
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
void input_common_init(int (*ih)()) {
|
2012-11-19 00:30:30 +00:00
|
|
|
interrupt_handler = ih;
|
2016-01-15 05:46:53 +00:00
|
|
|
update_wait_on_escape_ms();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
void input_common_destroy() {}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00: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().
|
|
|
|
static wint_t readb() {
|
|
|
|
// 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 00:30:30 +00:00
|
|
|
unsigned char arr[1];
|
|
|
|
bool do_loop;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
do {
|
|
|
|
// Flush callbacks.
|
2013-04-03 20:49:58 +00:00
|
|
|
input_flush_callbacks();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
fd_set fdset;
|
2014-04-30 00:03:00 +00:00
|
|
|
int fd_max = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
int ioport = iothread_port();
|
|
|
|
int res;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(0, &fdset);
|
2016-05-02 02:54:25 +00:00
|
|
|
if (ioport > 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
FD_SET(ioport, &fdset);
|
2014-04-30 00:03:00 +00:00
|
|
|
fd_max = maxi(fd_max, ioport);
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get our uvar notifier.
|
2014-04-30 00:03:00 +00:00
|
|
|
universal_notifier_t ¬ifier = universal_notifier_t::default_notifier();
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get the notification fd (possibly none).
|
2014-04-30 22:50:03 +00:00
|
|
|
int notifier_fd = notifier.notification_fd();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (notifier_fd > 0) {
|
2014-04-30 22:50:03 +00:00
|
|
|
FD_SET(notifier_fd, &fdset);
|
|
|
|
fd_max = maxi(fd_max, notifier_fd);
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get its suggested delay (possibly none).
|
2014-04-30 00:03:00 +00:00
|
|
|
struct timeval tv = {};
|
|
|
|
const unsigned long usecs_delay = notifier.usec_delay_between_polls();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (usecs_delay > 0) {
|
2014-04-30 00:03:00 +00: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 02:54:25 +00:00
|
|
|
|
2014-04-30 00:03:00 +00:00
|
|
|
res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
|
2016-05-02 02:54:25 +00:00
|
|
|
if (res == -1) {
|
|
|
|
switch (errno) {
|
2012-11-19 08:31:03 +00:00
|
|
|
case EINTR:
|
2016-05-02 02:54:25 +00:00
|
|
|
case EAGAIN: {
|
|
|
|
if (interrupt_handler) {
|
2012-11-19 08:31:03 +00:00
|
|
|
int res = interrupt_handler();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (res) {
|
2012-11-19 08:31:03 +00:00
|
|
|
return res;
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
if (has_lookahead()) {
|
2012-11-25 00:58:30 +00:00
|
|
|
return lookahead_pop();
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
do_loop = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
default: {
|
|
|
|
// The terminal has been closed. Save and exit.
|
2012-11-19 08:31:03 +00:00
|
|
|
return R_EOF;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
} else {
|
|
|
|
// Assume we loop unless we see a character in stdin.
|
2012-03-26 08:21:10 +00:00
|
|
|
do_loop = true;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// Check to see if we want a universal variable barrier.
|
2014-05-14 06:16:53 +00:00
|
|
|
bool barrier_from_poll = notifier.poll();
|
|
|
|
bool barrier_from_readability = false;
|
2016-05-02 02:54:25 +00:00
|
|
|
if (notifier_fd > 0 && FD_ISSET(notifier_fd, &fdset)) {
|
2014-05-14 06:16:53 +00:00
|
|
|
barrier_from_readability = notifier.notification_fd_became_readable(notifier_fd);
|
2014-04-30 22:50:03 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
if (barrier_from_poll || barrier_from_readability) {
|
2014-05-14 06:16:53 +00:00
|
|
|
env_universal_barrier();
|
2014-04-30 00:03:00 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if (ioport > 0 && FD_ISSET(ioport, &fdset)) {
|
2012-03-26 08:21:10 +00:00
|
|
|
iothread_service_completion();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (has_lookahead()) {
|
2012-11-25 00:58:30 +00:00
|
|
|
return lookahead_pop();
|
2012-03-26 08:21:10 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if (FD_ISSET(STDIN_FILENO, &fdset)) {
|
|
|
|
if (read_blocked(0, arr, 1) != 1) {
|
|
|
|
// The teminal has been closed. Save and exit.
|
2012-11-19 00:30:30 +00:00
|
|
|
return R_EOF;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// We read from stdin, so don't loop.
|
2012-11-19 00:30:30 +00:00
|
|
|
do_loop = false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
} while (do_loop);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return arr[0];
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
// Directly set the input timeout.
|
|
|
|
void set_wait_on_escape_ms(int ms) { wait_on_escape_ms = ms; }
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// Update the wait_on_escape_ms value in response to the fish_escape_delay_ms user variable being
|
|
|
|
// set.
|
|
|
|
void update_wait_on_escape_ms() {
|
2016-01-15 05:46:53 +00:00
|
|
|
env_var_t escape_time_ms = env_get_string(L"fish_escape_delay_ms");
|
2016-05-02 02:54:25 +00:00
|
|
|
if (escape_time_ms.missing_or_empty()) {
|
2016-01-15 05:46:53 +00:00
|
|
|
wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *endptr;
|
|
|
|
long tmp = wcstol(escape_time_ms.c_str(), &endptr, 10);
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if (*endptr != '\0' || tmp < 10 || tmp >= 5000) {
|
|
|
|
fwprintf(stderr,
|
|
|
|
L"ignoring fish_escape_delay_ms: value '%ls' "
|
|
|
|
"is not an integer or is < 10 or >= 5000 ms\n",
|
|
|
|
escape_time_ms.c_str());
|
|
|
|
} else {
|
2016-01-15 05:46:53 +00:00
|
|
|
wait_on_escape_ms = (int)tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
wchar_t input_common_readch(int timed) {
|
|
|
|
if (!has_lookahead()) {
|
|
|
|
if (timed) {
|
2012-11-19 00:30:30 +00:00
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(0, &fds);
|
2016-05-02 02:54:25 +00:00
|
|
|
struct timeval tm = {wait_on_escape_ms / 1000, 1000 * (wait_on_escape_ms % 1000)};
|
2016-01-15 05:46:53 +00:00
|
|
|
int count = select(1, &fds, 0, 0, &tm);
|
2016-05-02 02:54:25 +00:00
|
|
|
if (count <= 0) {
|
2016-01-15 05:46:53 +00:00
|
|
|
return WEOF;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t res;
|
2014-09-22 03:47:03 +00:00
|
|
|
mbstate_t state = {};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
while (1) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wint_t b = readb();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if (MB_CUR_MAX == 1) // single-byte locale, all values are legal
|
2016-03-11 02:17:39 +00:00
|
|
|
{
|
|
|
|
return (unsigned char)b;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if ((b >= R_NULL) && (b < R_NULL + 1000)) return b;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-03-11 02:17:39 +00:00
|
|
|
char bb = b;
|
|
|
|
size_t sz = mbrtowc(&res, &bb, 1, &state);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
switch (sz) {
|
|
|
|
case (size_t)(-1): {
|
2012-11-19 08:31:03 +00:00
|
|
|
memset(&state, '\0', sizeof(state));
|
|
|
|
debug(2, L"Illegal input");
|
|
|
|
return R_NULL;
|
2016-05-02 02:54:25 +00:00
|
|
|
}
|
|
|
|
case (size_t)(-2): {
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-05-02 02:54:25 +00:00
|
|
|
}
|
|
|
|
case 0: {
|
2012-11-19 08:31:03 +00:00
|
|
|
return 0;
|
2016-05-02 02:54:25 +00:00
|
|
|
}
|
|
|
|
default: { return res; }
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
} else {
|
|
|
|
if (!timed) {
|
|
|
|
while (has_lookahead() && lookahead_front() == WEOF) lookahead_pop();
|
|
|
|
if (!has_lookahead()) return input_common_readch(0);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-25 00:58:30 +00:00
|
|
|
return lookahead_pop();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
void input_common_queue_ch(wint_t ch) { lookahead_push_back(ch); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
void input_common_next_ch(wint_t ch) { lookahead_push_front(ch); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
void input_common_add_callback(void (*callback)(void *), void *arg) {
|
2013-04-03 20:49:58 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
callback_queue.push(callback_info_t(callback, arg));
|
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
static void input_flush_callbacks(void) {
|
|
|
|
// Nothing to do if nothing to do.
|
|
|
|
if (callback_queue.empty()) return;
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// We move the queue into a local variable, so that events queued up during a callback don't get
|
|
|
|
// fired until next round.
|
2013-04-03 20:49:58 +00:00
|
|
|
callback_queue_t local_queue;
|
|
|
|
std::swap(local_queue, callback_queue);
|
2016-05-02 02:54:25 +00:00
|
|
|
while (!local_queue.empty()) {
|
2013-04-03 20:49:58 +00:00
|
|
|
const callback_info_t &callback = local_queue.front();
|
2016-05-02 02:54:25 +00:00
|
|
|
callback.first(callback.second); // cute
|
2013-04-03 20:49:58 +00:00
|
|
|
local_queue.pop();
|
|
|
|
}
|
|
|
|
}
|