2016-05-07 04:22:28 +00:00
|
|
|
// A small utility to print information related to pressing keys. This is similar to using tools
|
|
|
|
// like `xxd` and `od -tx1z` but provides more information such as the time delay between each
|
|
|
|
// character. It also allows pressing and interpreting keys that are normally special such as
|
|
|
|
// [ctrl-C] (interrupt the program) or [ctrl-D] (EOF to signal the program should exit).
|
|
|
|
// And unlike those other tools this one disables ICRNL mode so it can distinguish between
|
|
|
|
// carriage-return (\cM) and newline (\cJ).
|
|
|
|
//
|
|
|
|
// Type "exit" or "quit" to terminate the program.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-06-28 09:57:14 +00:00
|
|
|
#include <errno.h>
|
2016-05-07 04:22:28 +00:00
|
|
|
#include <getopt.h>
|
2016-06-27 00:09:29 +00:00
|
|
|
#include <signal.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
#include <stddef.h>
|
2016-05-07 04:22:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-12-20 00:11:36 +00:00
|
|
|
#include <termios.h>
|
2016-06-28 09:57:14 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
|
2016-07-22 02:36:32 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-05-07 04:22:28 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2017-07-04 20:52:14 +00:00
|
|
|
#include "fish_version.h"
|
2016-05-07 04:22:28 +00:00
|
|
|
#include "input.h"
|
|
|
|
#include "input_common.h"
|
2016-07-03 12:02:32 +00:00
|
|
|
#include "print_help.h"
|
2016-07-22 02:36:32 +00:00
|
|
|
#include "proc.h"
|
2016-05-25 03:42:50 +00:00
|
|
|
#include "reader.h"
|
2016-06-28 09:57:14 +00:00
|
|
|
#include "signal.h"
|
2016-07-22 02:36:32 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2016-05-07 04:22:28 +00:00
|
|
|
|
|
|
|
struct config_paths_t determine_config_directory_paths(const char *argv0);
|
|
|
|
|
2017-01-03 05:11:53 +00:00
|
|
|
static const wchar_t *ctrl_symbolic_names[] = {
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, L"\\a", L"\\b", L"\\t", L"\\n",
|
|
|
|
L"\\v", L"\\f", L"\\r", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, L"\\e", NULL, NULL, NULL, NULL};
|
2016-06-26 02:47:50 +00:00
|
|
|
static bool keep_running = true;
|
2016-05-07 04:22:28 +00:00
|
|
|
|
|
|
|
/// Return true if the recent sequence of characters indicates the user wants to exit the program.
|
2016-06-30 02:46:11 +00:00
|
|
|
static bool should_exit(wchar_t wc) {
|
|
|
|
unsigned char c = wc < 0x80 ? wc : 0;
|
2016-05-07 04:22:28 +00:00
|
|
|
static unsigned char recent_chars[4] = {0};
|
|
|
|
|
|
|
|
recent_chars[0] = recent_chars[1];
|
|
|
|
recent_chars[1] = recent_chars[2];
|
|
|
|
recent_chars[2] = recent_chars[3];
|
|
|
|
recent_chars[3] = c;
|
2016-12-20 00:11:36 +00:00
|
|
|
if (c == shell_modes.c_cc[VINTR]) {
|
|
|
|
if (recent_chars[2] == shell_modes.c_cc[VINTR]) return true;
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"Press [ctrl-%c] again to exit\n", shell_modes.c_cc[VINTR] + 0x40);
|
2016-12-20 00:11:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (c == shell_modes.c_cc[VEOF]) {
|
|
|
|
if (recent_chars[2] == shell_modes.c_cc[VEOF]) return true;
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"Press [ctrl-%c] again to exit\n", shell_modes.c_cc[VEOF] + 0x40);
|
2016-12-20 00:11:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return memcmp(recent_chars, "exit", 4) == 0 || memcmp(recent_chars, "quit", 4) == 0;
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 09:48:50 +00:00
|
|
|
/// Return the name if the recent sequence of characters matches a known terminfo sequence.
|
2016-10-09 21:43:25 +00:00
|
|
|
static char *sequence_name(wchar_t wc) {
|
2016-06-30 02:46:11 +00:00
|
|
|
unsigned char c = wc < 0x80 ? wc : 0;
|
2016-05-07 04:22:28 +00:00
|
|
|
static char recent_chars[8] = {0};
|
|
|
|
|
|
|
|
recent_chars[0] = recent_chars[1];
|
|
|
|
recent_chars[1] = recent_chars[2];
|
|
|
|
recent_chars[2] = recent_chars[3];
|
|
|
|
recent_chars[3] = recent_chars[4];
|
|
|
|
recent_chars[4] = recent_chars[5];
|
|
|
|
recent_chars[5] = recent_chars[6];
|
|
|
|
recent_chars[6] = recent_chars[7];
|
|
|
|
recent_chars[7] = c;
|
|
|
|
|
|
|
|
for (int idx = 7; idx >= 0; idx--) {
|
|
|
|
wcstring out_name;
|
|
|
|
wcstring seq = str2wcstring(recent_chars + idx, 8 - idx);
|
|
|
|
bool found = input_terminfo_get_name(seq, &out_name);
|
|
|
|
if (found) {
|
|
|
|
return strdup(wcs2string(out_name).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-12 04:56:04 +00:00
|
|
|
/// Return true if the character must be escaped when used in the sequence of chars to be bound in
|
|
|
|
/// a `bind` command.
|
2017-05-18 06:07:47 +00:00
|
|
|
static bool must_escape(wchar_t wc) { return wcschr(L"[]()<>{}*\\?$#;&|'\"", wc) != NULL; }
|
2016-06-30 02:46:11 +00:00
|
|
|
|
2017-05-12 04:56:04 +00:00
|
|
|
static void ctrl_to_symbol(wchar_t *buf, int buf_len, wchar_t wc, bool bind_friendly) {
|
|
|
|
if (ctrl_symbolic_names[wc]) {
|
2016-08-11 05:15:52 +00:00
|
|
|
if (bind_friendly) {
|
2017-05-12 04:56:04 +00:00
|
|
|
swprintf(buf, buf_len, L"%ls", ctrl_symbolic_names[wc]);
|
2016-06-30 02:46:11 +00:00
|
|
|
} else {
|
2017-05-12 04:56:04 +00:00
|
|
|
swprintf(buf, buf_len, L"\\c%c (or %ls)", wc + 0x40, ctrl_symbolic_names[wc]);
|
2016-06-30 02:46:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-12 04:56:04 +00:00
|
|
|
swprintf(buf, buf_len, L"\\c%c", wc + 0x40);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void space_to_symbol(wchar_t *buf, int buf_len, wchar_t wc, bool bind_friendly) {
|
|
|
|
if (bind_friendly) {
|
|
|
|
swprintf(buf, buf_len, L"\\x%X", wc);
|
|
|
|
} else {
|
|
|
|
swprintf(buf, buf_len, L"\\x%X (aka \"space\")", wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void del_to_symbol(wchar_t *buf, int buf_len, wchar_t wc, bool bind_friendly) {
|
|
|
|
if (bind_friendly) {
|
|
|
|
swprintf(buf, buf_len, L"\\x%X", wc);
|
|
|
|
} else {
|
|
|
|
swprintf(buf, buf_len, L"\\x%X (aka \"del\")", wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ascii_printable_to_symbol(wchar_t *buf, int buf_len, wchar_t wc, bool bind_friendly) {
|
|
|
|
if (bind_friendly && must_escape(wc)) {
|
|
|
|
swprintf(buf, buf_len, L"\\%c", wc);
|
|
|
|
} else {
|
|
|
|
swprintf(buf, buf_len, L"%c", wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a wide-char to a symbol that can be used in our output. The use of a static buffer
|
|
|
|
/// requires that the returned string be used before we are called again.
|
|
|
|
static wchar_t *char_to_symbol(wchar_t wc, bool bind_friendly) {
|
|
|
|
static wchar_t buf[64];
|
|
|
|
|
|
|
|
if (wc < L' ') { // ASCII control character
|
|
|
|
ctrl_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
|
|
|
|
} else if (wc == L' ') { // the "space" character
|
|
|
|
space_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
|
|
|
|
} else if (wc == 0x7F) { // the "del" character
|
|
|
|
del_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
|
|
|
|
} else if (wc < 0x80) { // ASCII characters that are not control characters
|
|
|
|
ascii_printable_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
|
|
|
|
} else if (wc <= 0xFFFF) { // BMP Unicode chararacter
|
|
|
|
swprintf(buf, sizeof(buf) / sizeof(*buf), L"\\u%04X", wc);
|
|
|
|
} else { // Non-BMP Unicode chararacter
|
|
|
|
swprintf(buf, sizeof(buf) / sizeof(*buf), L"\\U%06X", wc);
|
2016-06-26 02:47:50 +00:00
|
|
|
}
|
2017-01-03 05:11:53 +00:00
|
|
|
|
2016-06-30 02:46:11 +00:00
|
|
|
return buf;
|
2016-06-26 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 02:46:11 +00:00
|
|
|
static void add_char_to_bind_command(wchar_t wc, std::vector<wchar_t> &bind_chars) {
|
|
|
|
bind_chars.push_back(wc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_bind_command(std::vector<wchar_t> &bind_chars) {
|
|
|
|
if (bind_chars.size()) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fputws(L"bind ", stdout);
|
2016-10-09 21:43:25 +00:00
|
|
|
for (size_t i = 0; i < bind_chars.size(); i++) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fputws(char_to_symbol(bind_chars[i], true), stdout);
|
2016-06-30 02:46:11 +00:00
|
|
|
}
|
2017-01-03 05:11:53 +00:00
|
|
|
fputws(L" 'do something'\n", stdout);
|
2016-06-30 02:46:11 +00:00
|
|
|
bind_chars.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_info_about_char(wchar_t wc) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"hex: %4X char: %ls\n", wc, char_to_symbol(wc, false));
|
2016-06-30 02:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool output_matching_key_name(wchar_t wc) {
|
2016-07-01 09:48:50 +00:00
|
|
|
char *name = sequence_name(wc);
|
2016-06-26 02:47:50 +00:00
|
|
|
if (name) {
|
2017-01-14 04:34:15 +00:00
|
|
|
fwprintf(stdout, L"bind -k %s 'do something'\n", name);
|
2016-06-26 02:47:50 +00:00
|
|
|
free(name);
|
2016-06-30 02:46:11 +00:00
|
|
|
return true;
|
2016-06-26 02:47:50 +00:00
|
|
|
}
|
2016-06-30 02:46:11 +00:00
|
|
|
return false;
|
2016-06-26 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double output_elapsed_time(double prev_tstamp, bool first_char_seen) {
|
|
|
|
// How much time has passed since the previous char was received in microseconds.
|
|
|
|
double now = timef();
|
|
|
|
long long int delta_tstamp_us = 1000000 * (now - prev_tstamp);
|
|
|
|
|
2017-01-03 05:11:53 +00:00
|
|
|
if (delta_tstamp_us >= 200000 && first_char_seen) fputwc(L'\n', stderr);
|
2016-06-26 02:47:50 +00:00
|
|
|
if (delta_tstamp_us >= 1000000) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L" ");
|
2016-06-26 02:47:50 +00:00
|
|
|
} else {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"(%3lld.%03lld ms) ", delta_tstamp_us / 1000, delta_tstamp_us % 1000);
|
2016-06-26 02:47:50 +00:00
|
|
|
}
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
/// Process the characters we receive as the user presses keys.
|
2016-06-26 02:47:50 +00:00
|
|
|
static void process_input(bool continuous_mode) {
|
2016-05-07 04:22:28 +00:00
|
|
|
bool first_char_seen = false;
|
2016-06-26 02:47:50 +00:00
|
|
|
double prev_tstamp = 0.0;
|
2016-06-30 02:46:11 +00:00
|
|
|
std::vector<wchar_t> bind_chars;
|
2016-06-26 02:47:50 +00:00
|
|
|
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"Press a key\n\n");
|
2016-06-26 02:47:50 +00:00
|
|
|
while (keep_running) {
|
2016-12-20 00:11:36 +00:00
|
|
|
wchar_t wc;
|
|
|
|
if (reader_interrupted()) {
|
|
|
|
wc = shell_modes.c_cc[VINTR];
|
|
|
|
} else {
|
|
|
|
wc = input_common_readch(true);
|
|
|
|
}
|
2016-07-12 03:31:30 +00:00
|
|
|
if (wc == R_TIMEOUT || wc == R_EOF) {
|
2016-06-30 02:46:11 +00:00
|
|
|
output_bind_command(bind_chars);
|
|
|
|
if (first_char_seen && !continuous_mode) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-05 01:40:22 +00:00
|
|
|
continue;
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 02:47:50 +00:00
|
|
|
prev_tstamp = output_elapsed_time(prev_tstamp, first_char_seen);
|
2016-06-30 02:46:11 +00:00
|
|
|
add_char_to_bind_command(wc, bind_chars);
|
|
|
|
output_info_about_char(wc);
|
|
|
|
if (output_matching_key_name(wc)) {
|
|
|
|
output_bind_command(bind_chars);
|
|
|
|
}
|
2016-05-07 04:22:28 +00:00
|
|
|
|
2016-06-30 02:46:11 +00:00
|
|
|
if (should_exit(wc)) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"\nExiting at your request.\n");
|
2016-05-07 04:22:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
first_char_seen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 02:47:50 +00:00
|
|
|
/// Make sure we cleanup before exiting if we receive a signal that should cause us to exit.
|
|
|
|
/// Otherwise just report receipt of the signal.
|
2016-06-27 00:09:29 +00:00
|
|
|
static struct sigaction old_sigactions[32];
|
|
|
|
static void signal_handler(int signo, siginfo_t *siginfo, void *siginfo_arg) {
|
2016-12-20 00:11:36 +00:00
|
|
|
fwprintf(stdout, _(L"signal #%d (%ls) received\n"), signo, sig2wcs(signo));
|
2016-06-27 00:09:29 +00:00
|
|
|
if (signo == SIGHUP || signo == SIGTERM || signo == SIGABRT || signo == SIGSEGV) {
|
2016-06-26 02:47:50 +00:00
|
|
|
keep_running = false;
|
|
|
|
}
|
2016-12-20 00:11:36 +00:00
|
|
|
|
2016-06-27 00:09:29 +00:00
|
|
|
if (old_sigactions[signo].sa_handler != SIG_IGN &&
|
|
|
|
old_sigactions[signo].sa_handler != SIG_DFL) {
|
|
|
|
int needs_siginfo = old_sigactions[signo].sa_flags & SA_SIGINFO;
|
|
|
|
if (needs_siginfo) {
|
|
|
|
old_sigactions[signo].sa_sigaction(signo, siginfo, siginfo_arg);
|
|
|
|
} else {
|
|
|
|
old_sigactions[signo].sa_handler(signo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Install a handler for every signal. This allows us to restore the tty modes so the terminal is
|
|
|
|
/// still usable when we die. If the signal already has a handler arrange to invoke it from within
|
|
|
|
/// our handler.
|
|
|
|
static void install_our_signal_handlers() {
|
|
|
|
struct sigaction new_sa, old_sa;
|
|
|
|
sigemptyset(&new_sa.sa_mask);
|
|
|
|
new_sa.sa_flags = SA_SIGINFO;
|
|
|
|
new_sa.sa_sigaction = signal_handler;
|
|
|
|
|
|
|
|
for (int signo = 1; signo < 32; signo++) {
|
|
|
|
if (sigaction(signo, &new_sa, &old_sa) != -1) {
|
|
|
|
memcpy(&old_sigactions[signo], &old_sa, sizeof(old_sa));
|
|
|
|
if (old_sa.sa_handler == SIG_IGN) {
|
2016-06-30 02:46:11 +00:00
|
|
|
debug(3, "signal #%d (%ls) was being ignored", signo, sig2wcs(signo));
|
2016-06-27 00:09:29 +00:00
|
|
|
}
|
|
|
|
if (old_sa.sa_flags && ~SA_SIGINFO != 0) {
|
2016-06-30 02:46:11 +00:00
|
|
|
debug(3, L"signal #%d (%ls) handler had flags 0x%X", signo, sig2wcs(signo),
|
2016-06-27 00:09:29 +00:00
|
|
|
old_sa.sa_flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Setup our environment (e.g., tty modes), process key strokes, then reset the environment.
|
2016-06-26 02:47:50 +00:00
|
|
|
static void setup_and_process_keys(bool continuous_mode) {
|
2016-07-22 02:36:32 +00:00
|
|
|
is_interactive_session = 1; // by definition this program is interactive
|
2016-05-07 04:22:28 +00:00
|
|
|
set_main_thread();
|
|
|
|
setup_fork_guards();
|
2017-06-18 22:54:13 +00:00
|
|
|
proc_push_interactive(1);
|
2016-06-26 02:47:50 +00:00
|
|
|
env_init();
|
|
|
|
reader_init();
|
2016-06-27 00:09:29 +00:00
|
|
|
install_our_signal_handlers();
|
2016-06-26 02:47:50 +00:00
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
if (continuous_mode) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"\n");
|
|
|
|
fwprintf(stderr, L"To terminate this program type \"exit\" or \"quit\" in this window,\n");
|
|
|
|
fwprintf(stderr, L"or press [ctrl-%c] or [ctrl-%c] twice in a row.\n",
|
|
|
|
shell_modes.c_cc[VINTR] + 0x40, shell_modes.c_cc[VEOF] + 0x40);
|
|
|
|
fwprintf(stderr, L"\n");
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process_input(continuous_mode);
|
2016-05-25 03:42:50 +00:00
|
|
|
restore_term_mode();
|
2016-06-23 14:43:55 +00:00
|
|
|
restore_term_foreground_process_group();
|
|
|
|
input_destroy();
|
|
|
|
reader_destroy();
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 04:56:04 +00:00
|
|
|
static bool parse_debug_level_flag() {
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(optarg, &end, 10);
|
|
|
|
|
|
|
|
if (tmp >= 0 && tmp <= 10 && !*end && !errno) {
|
|
|
|
debug_level = (int)tmp;
|
|
|
|
} else {
|
|
|
|
fwprintf(stderr, _(L"Invalid value '%s' for debug-level flag\n"), optarg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_debug_frames_flag() {
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(optarg, &end, 10);
|
|
|
|
if (tmp > 0 && tmp <= 128 && !*end && !errno) {
|
|
|
|
debug_stack_frames = (int)tmp;
|
|
|
|
} else {
|
|
|
|
fwprintf(stderr, _(L"Invalid value '%s' for debug-stack-frames flag\n"), optarg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_flags(int argc, char **argv, bool *continuous_mode) {
|
2017-07-04 20:52:14 +00:00
|
|
|
const char *short_opts = "+cd:D:hv";
|
2016-06-26 02:47:50 +00:00
|
|
|
const struct option long_opts[] = {{"continuous", no_argument, NULL, 'c'},
|
|
|
|
{"debug-level", required_argument, NULL, 'd'},
|
|
|
|
{"debug-stack-frames", required_argument, NULL, 'D'},
|
2016-07-03 12:02:32 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2017-07-04 20:52:14 +00:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
2016-06-26 02:47:50 +00:00
|
|
|
{NULL, 0, NULL, 0}};
|
2016-05-07 04:22:28 +00:00
|
|
|
int opt;
|
2016-11-05 01:40:22 +00:00
|
|
|
bool error = false;
|
|
|
|
while (!error && (opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
2016-05-07 04:22:28 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'c': {
|
2017-05-12 04:56:04 +00:00
|
|
|
*continuous_mode = true;
|
2016-05-07 04:22:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-07-03 12:02:32 +00:00
|
|
|
case 'h': {
|
|
|
|
print_help("fish_key_reader", 0);
|
2017-05-12 04:56:04 +00:00
|
|
|
error = true;
|
2016-11-05 01:40:22 +00:00
|
|
|
break;
|
2016-07-03 12:02:32 +00:00
|
|
|
}
|
2016-06-26 02:47:50 +00:00
|
|
|
case 'd': {
|
2017-05-12 04:56:04 +00:00
|
|
|
error = !parse_debug_level_flag();
|
2016-06-26 02:47:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'D': {
|
2017-05-12 04:56:04 +00:00
|
|
|
error = !parse_debug_frames_flag();
|
2016-06-26 02:47:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-07-04 20:52:14 +00:00
|
|
|
case 'v': {
|
|
|
|
fwprintf(stdout, L"%s\n", get_fish_version());
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-07 04:22:28 +00:00
|
|
|
default: {
|
|
|
|
// We assume getopt_long() has already emitted a diagnostic msg.
|
2016-11-05 01:40:22 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 04:56:04 +00:00
|
|
|
|
|
|
|
if (error) return false;
|
2016-05-07 04:22:28 +00:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
if (argc != 0) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"Expected no arguments, got %d\n", argc);
|
2017-05-12 04:56:04 +00:00
|
|
|
return false;
|
2016-05-07 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 04:56:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
program_name = L"fish_key_reader";
|
|
|
|
bool continuous_mode = false;
|
|
|
|
|
|
|
|
if (!parse_flags(argc, argv, &continuous_mode)) return 1;
|
|
|
|
|
2016-07-03 07:09:11 +00:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
2017-01-03 05:11:53 +00:00
|
|
|
fwprintf(stderr, L"Stdin must be attached to a tty.\n");
|
2016-06-27 04:47:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
setup_and_process_keys(continuous_mode);
|
|
|
|
return 0;
|
|
|
|
}
|