2017-06-13 08:19:13 +08:00
|
|
|
// Implementation of the bind builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "builtin_bind.h"
|
|
|
|
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
2017-06-13 08:19:13 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "input.h" // IWYU pragma: keep
|
|
|
|
#include "io.h" // IWYU pragma: keep
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
enum { BIND_INSERT, BIND_ERASE, BIND_KEY_NAMES, BIND_FUNCTION_NAMES };
|
2017-06-16 08:57:37 +08:00
|
|
|
struct bind_cmd_opts_t {
|
2017-10-03 16:29:48 +08:00
|
|
|
bool all = false;
|
2017-06-16 08:57:37 +08:00
|
|
|
bool bind_mode_given = false;
|
|
|
|
bool list_modes = false;
|
2017-10-03 16:29:48 +08:00
|
|
|
bool print_help = false;
|
|
|
|
bool silent = false;
|
2017-06-13 08:19:13 +08:00
|
|
|
bool use_terminfo = false;
|
2018-09-18 17:52:25 +08:00
|
|
|
bool have_user = false;
|
|
|
|
bool user = false;
|
|
|
|
bool have_preset = false;
|
|
|
|
bool preset = false;
|
2017-06-16 08:57:37 +08:00
|
|
|
int mode = BIND_INSERT;
|
2017-06-13 08:19:13 +08:00
|
|
|
const wchar_t *bind_mode = DEFAULT_BIND_MODE;
|
|
|
|
const wchar_t *sets_bind_mode = L"";
|
|
|
|
};
|
|
|
|
|
|
|
|
// Here follows the definition of all builtin commands. The function names are all of the form
|
|
|
|
// builtin_NAME where NAME is the name of the builtin. so the function name for the builtin 'fg' is
|
|
|
|
// 'builtin_fg'.
|
|
|
|
//
|
|
|
|
// A few builtins, including 'while', 'command' and 'builtin' are not defined here as they are
|
|
|
|
// handled directly by the parser. (They are not parsed as commands, instead they only alter the
|
|
|
|
// parser state)
|
|
|
|
//
|
|
|
|
// The builtins 'break' and 'continue' are so closely related that they share the same
|
|
|
|
// implementation, namely 'builtin_break_continue.
|
|
|
|
//
|
|
|
|
// Several other builtins, including jobs, ulimit and set are so big that they have been given their
|
|
|
|
// own module. These files are all named 'builtin_NAME.cpp', where NAME is the name of the builtin.
|
|
|
|
|
|
|
|
/// List a single key binding.
|
|
|
|
/// Returns false if no binding with that sequence and mode exists.
|
2018-09-18 17:52:25 +08:00
|
|
|
bool builtin_bind_t::list_one(const wcstring &seq, const wcstring &bind_mode, bool user,
|
2019-05-05 18:09:25 +08:00
|
|
|
io_streams_t &streams) {
|
2019-03-15 02:15:50 +08:00
|
|
|
wcstring_list_t ecmds;
|
2017-06-13 08:19:13 +08:00
|
|
|
wcstring sets_mode;
|
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
if (!input_mappings_->get(seq, bind_mode, &ecmds, user, &sets_mode)) {
|
2017-06-13 08:19:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.out.append(L"bind");
|
|
|
|
|
|
|
|
// Append the mode flags if applicable.
|
2018-09-18 17:52:25 +08:00
|
|
|
if (!user) {
|
|
|
|
streams.out.append(L" --preset");
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
if (bind_mode != DEFAULT_BIND_MODE) {
|
|
|
|
const wcstring emode = escape_string(bind_mode, ESCAPE_ALL);
|
|
|
|
streams.out.append(L" -M ");
|
|
|
|
streams.out.append(emode);
|
|
|
|
}
|
|
|
|
if (!sets_mode.empty() && sets_mode != bind_mode) {
|
|
|
|
const wcstring esets_mode = escape_string(sets_mode, ESCAPE_ALL);
|
|
|
|
streams.out.append(L" -m ");
|
|
|
|
streams.out.append(esets_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the name.
|
|
|
|
wcstring tname;
|
|
|
|
if (input_terminfo_get_name(seq, &tname)) {
|
|
|
|
// Note that we show -k here because we have an input key name.
|
|
|
|
streams.out.append_format(L" -k %ls", tname.c_str());
|
|
|
|
} else {
|
|
|
|
// No key name, so no -k; we show the escape sequence directly.
|
|
|
|
const wcstring eseq = escape_string(seq, ESCAPE_ALL);
|
|
|
|
streams.out.append_format(L" %ls", eseq.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now show the list of commands.
|
|
|
|
for (size_t i = 0; i < ecmds.size(); i++) {
|
|
|
|
const wcstring &ecmd = ecmds.at(i);
|
|
|
|
const wcstring escaped_ecmd = escape_string(ecmd, ESCAPE_ALL);
|
|
|
|
streams.out.push_back(' ');
|
|
|
|
streams.out.append(escaped_ecmd);
|
|
|
|
}
|
|
|
|
streams.out.push_back(L'\n');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
// Overload with both kinds of bindings.
|
|
|
|
// Returns false only if neither exists.
|
2019-05-05 18:09:25 +08:00
|
|
|
bool builtin_bind_t::list_one(const wcstring &seq, const wcstring &bind_mode, bool user,
|
|
|
|
bool preset, io_streams_t &streams) {
|
2018-09-18 17:52:25 +08:00
|
|
|
bool retval = false;
|
|
|
|
if (preset) {
|
|
|
|
retval |= list_one(seq, bind_mode, false, streams);
|
|
|
|
}
|
|
|
|
if (user) {
|
|
|
|
retval |= list_one(seq, bind_mode, true, streams);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:19:13 +08:00
|
|
|
/// List all current key bindings.
|
2018-09-18 17:52:25 +08:00
|
|
|
void builtin_bind_t::list(const wchar_t *bind_mode, bool user, io_streams_t &streams) {
|
2019-06-03 13:36:11 +08:00
|
|
|
const std::vector<input_mapping_name_t> lst = input_mappings_->get_names(user);
|
2017-06-13 08:19:13 +08:00
|
|
|
|
|
|
|
for (const input_mapping_name_t &binding : lst) {
|
|
|
|
if (bind_mode && bind_mode != binding.mode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
list_one(binding.seq, binding.mode, user, streams);
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print terminfo key binding names to string buffer used for standard output.
|
|
|
|
///
|
|
|
|
/// \param all if set, all terminfo key binding names will be printed. If not set, only ones that
|
|
|
|
/// are defined for this terminal are printed.
|
2018-09-18 17:52:25 +08:00
|
|
|
void builtin_bind_t::key_names(bool all, io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
const wcstring_list_t names = input_terminfo_get_names(!all);
|
2019-09-15 05:22:37 +08:00
|
|
|
for (const wcstring &name : names) {
|
|
|
|
streams.out.append(name);
|
|
|
|
streams.out.append(L'\n');
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all the special key binding functions to string buffer used for standard output.
|
2017-10-03 16:29:48 +08:00
|
|
|
void builtin_bind_t::function_names(io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
wcstring_list_t names = input_function_get_names();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
|
|
|
const wchar_t *seq = names.at(i).c_str();
|
|
|
|
streams.out.append_format(L"%ls\n", seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps input_terminfo_get_sequence(), appending the correct error messages as needed.
|
2019-09-15 07:36:57 +08:00
|
|
|
bool builtin_bind_t::get_terminfo_sequence(const wcstring &seq, wcstring *out_seq,
|
2019-05-05 18:09:25 +08:00
|
|
|
io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
if (input_terminfo_get_sequence(seq, out_seq)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring eseq = escape_string(seq, 0);
|
2017-10-03 16:29:48 +08:00
|
|
|
if (!opts->silent) {
|
|
|
|
if (errno == ENOENT) {
|
2019-05-05 18:09:25 +08:00
|
|
|
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind",
|
|
|
|
eseq.c_str());
|
2017-10-03 16:29:48 +08:00
|
|
|
} else if (errno == EILSEQ) {
|
|
|
|
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
|
|
|
|
L"bind", eseq.c_str());
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
|
|
|
|
L"bind", eseq.c_str());
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add specified key binding.
|
2019-09-15 07:36:57 +08:00
|
|
|
bool builtin_bind_t::add(const wcstring &seq, const wchar_t *const *cmds, size_t cmds_len,
|
2018-09-18 17:52:25 +08:00
|
|
|
const wchar_t *mode, const wchar_t *sets_mode, bool terminfo, bool user,
|
2019-05-05 18:09:25 +08:00
|
|
|
io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
if (terminfo) {
|
|
|
|
wcstring seq2;
|
|
|
|
if (get_terminfo_sequence(seq, &seq2, streams)) {
|
2019-09-15 07:36:57 +08:00
|
|
|
input_mappings_->add(seq2, cmds, cmds_len, mode, sets_mode, user);
|
2017-06-13 08:19:13 +08:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2019-06-03 13:36:11 +08:00
|
|
|
input_mappings_->add(seq, cmds, cmds_len, mode, sets_mode, user);
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Erase specified key bindings
|
|
|
|
///
|
|
|
|
/// @param seq
|
|
|
|
/// an array of all key bindings to erase
|
|
|
|
/// @param all
|
|
|
|
/// if specified, _all_ key bindings will be erased
|
|
|
|
/// @param mode
|
|
|
|
/// if specified, only bindings from that mode will be erased. If not given
|
|
|
|
/// and @c all is @c false, @c DEFAULT_BIND_MODE will be used.
|
|
|
|
/// @param use_terminfo
|
|
|
|
/// Whether to look use terminfo -k name
|
|
|
|
///
|
2019-05-05 18:09:25 +08:00
|
|
|
bool builtin_bind_t::erase(wchar_t **seq, bool all, const wchar_t *mode, bool use_terminfo,
|
|
|
|
bool user, io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
if (all) {
|
2019-06-03 13:36:11 +08:00
|
|
|
input_mappings_->clear(mode, user);
|
2017-06-13 08:19:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res = false;
|
2019-11-19 10:34:50 +08:00
|
|
|
if (mode == nullptr) mode = DEFAULT_BIND_MODE; //!OCLINT(parameter reassignment)
|
2017-06-13 08:19:13 +08:00
|
|
|
|
|
|
|
while (*seq) {
|
|
|
|
if (use_terminfo) {
|
|
|
|
wcstring seq2;
|
|
|
|
if (get_terminfo_sequence(*seq++, &seq2, streams)) {
|
2019-06-03 13:36:11 +08:00
|
|
|
input_mappings_->erase(seq2, mode, user);
|
2017-06-13 08:19:13 +08:00
|
|
|
} else {
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-03 13:36:11 +08:00
|
|
|
input_mappings_->erase(*seq++, mode, user);
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
bool builtin_bind_t::insert(int optind, int argc, wchar_t **argv, io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int arg_count = argc - optind;
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
if (arg_count < 2) {
|
|
|
|
// If we get both or neither preset/user, we list both.
|
|
|
|
if (!opts->have_preset && !opts->have_user) {
|
|
|
|
opts->preset = true;
|
|
|
|
opts->user = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Inserting both on the other hand makes no sense.
|
|
|
|
if (opts->have_preset && opts->have_user) {
|
2019-05-05 18:09:25 +08:00
|
|
|
streams.err.append_format(
|
|
|
|
BUILTIN_ERR_COMBO2, cmd,
|
|
|
|
L"--preset and --user can not be used together when inserting bindings.");
|
2018-09-18 17:52:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:19:13 +08:00
|
|
|
if (arg_count == 0) {
|
2018-09-18 17:52:25 +08:00
|
|
|
// We don't overload this with user and def because we want them to be grouped.
|
|
|
|
// First the presets, then the users (because of scrolling).
|
|
|
|
if (opts->preset) {
|
2019-11-19 10:34:50 +08:00
|
|
|
list(opts->bind_mode_given ? opts->bind_mode : nullptr, false, streams);
|
2018-09-18 17:52:25 +08:00
|
|
|
}
|
|
|
|
if (opts->user) {
|
2019-11-19 10:34:50 +08:00
|
|
|
list(opts->bind_mode_given ? opts->bind_mode : nullptr, true, streams);
|
2018-09-18 17:52:25 +08:00
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
} else if (arg_count == 1) {
|
|
|
|
wcstring seq;
|
2017-10-03 16:29:48 +08:00
|
|
|
if (opts->use_terminfo) {
|
2017-06-13 08:19:13 +08:00
|
|
|
if (!get_terminfo_sequence(argv[optind], &seq, streams)) {
|
|
|
|
// get_terminfo_sequence already printed the error.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
seq = argv[optind];
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
if (!list_one(seq, opts->bind_mode, opts->user, opts->preset, streams)) {
|
2017-06-13 08:19:13 +08:00
|
|
|
wcstring eseq = escape_string(argv[optind], 0);
|
2017-10-03 16:29:48 +08:00
|
|
|
if (!opts->silent) {
|
|
|
|
if (opts->use_terminfo) {
|
|
|
|
streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"), cmd,
|
|
|
|
eseq.c_str());
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"), cmd,
|
|
|
|
eseq.c_str());
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-18 17:52:25 +08:00
|
|
|
// Actually insert!
|
2017-10-03 16:29:48 +08:00
|
|
|
if (add(argv[optind], argv + (optind + 1), argc - (optind + 1), opts->bind_mode,
|
2018-09-18 17:52:25 +08:00
|
|
|
opts->sets_bind_mode, opts->use_terminfo, opts->user, streams)) {
|
2017-06-13 08:19:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List all current bind modes.
|
2017-10-03 16:29:48 +08:00
|
|
|
void builtin_bind_t::list_modes(io_streams_t &streams) {
|
2018-09-18 17:52:25 +08:00
|
|
|
// List all known modes, even if they are only in preset bindings.
|
2019-06-03 13:36:11 +08:00
|
|
|
const std::vector<input_mapping_name_t> lst = input_mappings_->get_names(true);
|
|
|
|
const std::vector<input_mapping_name_t> preset_lst = input_mappings_->get_names(false);
|
2017-06-13 08:19:13 +08:00
|
|
|
// A set accomplishes two things for us here:
|
|
|
|
// - It removes duplicates (no twenty "default" entries).
|
|
|
|
// - It sorts it, which makes it nicer on the user.
|
|
|
|
std::set<wcstring> modes;
|
|
|
|
|
|
|
|
for (const input_mapping_name_t &binding : lst) {
|
|
|
|
modes.insert(binding.mode);
|
|
|
|
}
|
2018-09-18 17:52:25 +08:00
|
|
|
for (const input_mapping_name_t &binding : preset_lst) {
|
|
|
|
modes.insert(binding.mode);
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
for (const auto &mode : modes) {
|
|
|
|
streams.out.append_format(L"%ls\n", mode.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:29:48 +08:00
|
|
|
int parse_cmd_opts(bind_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2019-05-05 18:09:25 +08:00
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-06-13 08:19:13 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2018-09-29 11:45:56 +08:00
|
|
|
static const wchar_t *const short_options = L":aehkKfM:Lm:s";
|
2019-11-19 10:34:50 +08:00
|
|
|
static const struct woption long_options[] = {{L"all", no_argument, nullptr, 'a'},
|
|
|
|
{L"erase", no_argument, nullptr, 'e'},
|
|
|
|
{L"function-names", no_argument, nullptr, 'f'},
|
|
|
|
{L"help", no_argument, nullptr, 'h'},
|
|
|
|
{L"key", no_argument, nullptr, 'k'},
|
|
|
|
{L"key-names", no_argument, nullptr, 'K'},
|
|
|
|
{L"list-modes", no_argument, nullptr, 'L'},
|
|
|
|
{L"mode", required_argument, nullptr, 'M'},
|
|
|
|
{L"preset", no_argument, nullptr, 'p'},
|
|
|
|
{L"sets-mode", required_argument, nullptr, 'm'},
|
|
|
|
{L"silent", no_argument, nullptr, 's'},
|
|
|
|
{L"user", no_argument, nullptr, 'u'},
|
|
|
|
{nullptr, 0, nullptr, 0}};
|
2017-06-13 08:19:13 +08:00
|
|
|
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
2019-11-19 10:34:50 +08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
|
2017-06-13 08:19:13 +08:00
|
|
|
switch (opt) {
|
|
|
|
case L'a': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.all = true;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'e': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.mode = BIND_ERASE;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-10-03 16:29:48 +08:00
|
|
|
case L'f': {
|
|
|
|
opts.mode = BIND_FUNCTION_NAMES;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
case L'h': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 13:12:29 +08:00
|
|
|
break;
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
case L'k': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.use_terminfo = true;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'K': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.mode = BIND_KEY_NAMES;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-10-03 16:29:48 +08:00
|
|
|
case L'L': {
|
|
|
|
opts.list_modes = true;
|
|
|
|
return STATUS_CMD_OK;
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
case L'M': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_BIND_MODE, cmd, w.woptarg);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.bind_mode = w.woptarg;
|
|
|
|
opts.bind_mode_given = true;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'm': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_BIND_MODE, cmd, w.woptarg);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.sets_bind_mode = w.woptarg;
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-09-18 17:52:25 +08:00
|
|
|
case L'p': {
|
|
|
|
opts.have_preset = true;
|
|
|
|
opts.preset = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-03 16:29:48 +08:00
|
|
|
case L's': {
|
|
|
|
opts.silent = true;
|
|
|
|
break;
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
2018-09-18 17:52:25 +08:00
|
|
|
case L'u': {
|
|
|
|
opts.have_user = true;
|
|
|
|
opts.user = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-30 12:49:57 +08:00
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-30 12:49:57 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
case L'?': {
|
2017-06-15 03:26:05 +08:00
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-13 08:19:13 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The bind builtin, used for setting character sequences.
|
2017-10-03 16:29:48 +08:00
|
|
|
int builtin_bind_t::builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2017-06-13 08:19:13 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-16 08:57:37 +08:00
|
|
|
bind_cmd_opts_t opts;
|
2017-10-03 16:29:48 +08:00
|
|
|
this->opts = &opts;
|
2017-06-13 08:19:13 +08:00
|
|
|
|
|
|
|
int optind;
|
2017-06-16 08:57:37 +08:00
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-13 08:19:13 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.list_modes) {
|
2017-10-03 16:29:48 +08:00
|
|
|
list_modes(streams);
|
2017-06-13 08:19:13 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
if (opts.print_help) {
|
2019-10-20 17:38:17 +08:00
|
|
|
builtin_print_help(parser, streams, cmd);
|
2017-06-13 08:19:13 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
// Default to user mode
|
|
|
|
if (!opts.have_preset && !opts.have_user) opts.user = true;
|
2017-06-13 08:19:13 +08:00
|
|
|
switch (opts.mode) {
|
|
|
|
case BIND_ERASE: {
|
2019-11-19 10:34:50 +08:00
|
|
|
const wchar_t *bind_mode = opts.bind_mode_given ? opts.bind_mode : nullptr;
|
2018-09-18 17:52:25 +08:00
|
|
|
// If we get both, we erase both.
|
|
|
|
if (opts.user) {
|
|
|
|
if (erase(&argv[optind], opts.all, bind_mode, opts.use_terminfo, /* user */ true,
|
|
|
|
streams)) {
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opts.preset) {
|
|
|
|
if (erase(&argv[optind], opts.all, bind_mode, opts.use_terminfo, /* user */ false,
|
|
|
|
streams)) {
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2017-06-13 08:19:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BIND_INSERT: {
|
2017-10-03 16:29:48 +08:00
|
|
|
if (insert(optind, argc, argv, streams)) {
|
2017-06-13 08:19:13 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BIND_KEY_NAMES: {
|
2017-10-03 16:29:48 +08:00
|
|
|
key_names(opts.all, streams);
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BIND_FUNCTION_NAMES: {
|
2017-10-03 16:29:48 +08:00
|
|
|
function_names(streams);
|
2017-06-13 08:19:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
streams.err.append_format(_(L"%ls: Invalid state\n"), cmd);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|