2016-04-20 10:49:15 +08:00
|
|
|
// Functions used for implementing the complete builtin.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2006-01-23 05:10:55 +08:00
|
|
|
#include <wchar.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
|
2016-06-24 08:24:19 +08:00
|
|
|
#include <memory>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-01-23 05:10:55 +08:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "env.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
|
|
|
#include "parse_constants.h"
|
|
|
|
#include "parse_util.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "parser.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "wgetopt.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-07-20 06:55:49 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// builtin_complete_* are a set of rather silly looping functions that make sure that all the proper
|
|
|
|
// combinations of complete_add or complete_remove get called. This is needed since complete allows
|
|
|
|
// you to specify multiple switches on a single commandline, like 'complete -s a -s b -s c', but the
|
|
|
|
// complete_add function only accepts one short switch and one long switch.
|
|
|
|
|
|
|
|
/// Silly function.
|
|
|
|
static void builtin_complete_add2(const wchar_t *cmd, int cmd_type, const wchar_t *short_opt,
|
|
|
|
const wcstring_list_t &gnu_opt, const wcstring_list_t &old_opt,
|
|
|
|
int result_mode, const wchar_t *condition, const wchar_t *comp,
|
|
|
|
const wchar_t *desc, int flags) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t i;
|
|
|
|
const wchar_t *s;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (s = short_opt; *s; s++) {
|
|
|
|
complete_add(cmd, cmd_type, wcstring(1, *s), option_type_short, result_mode, condition,
|
|
|
|
comp, desc, flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (i = 0; i < gnu_opt.size(); i++) {
|
|
|
|
complete_add(cmd, cmd_type, gnu_opt.at(i), option_type_double_long, result_mode, condition,
|
|
|
|
comp, desc, flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (i = 0; i < old_opt.size(); i++) {
|
|
|
|
complete_add(cmd, cmd_type, old_opt.at(i), option_type_single_long, result_mode, condition,
|
|
|
|
comp, desc, flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (old_opt.empty() && gnu_opt.empty() && wcslen(short_opt) == 0) {
|
|
|
|
complete_add(cmd, cmd_type, wcstring(), option_type_args_only, result_mode, condition, comp,
|
|
|
|
desc, flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-01-23 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Silly function.
|
|
|
|
static void builtin_complete_add(const wcstring_list_t &cmd, const wcstring_list_t &path,
|
|
|
|
const wchar_t *short_opt, wcstring_list_t &gnu_opt,
|
2016-12-20 01:05:18 +08:00
|
|
|
wcstring_list_t &old_opt, int result_mode,
|
2016-04-20 10:49:15 +08:00
|
|
|
const wchar_t *condition, const wchar_t *comp, const wchar_t *desc,
|
|
|
|
int flags) {
|
|
|
|
for (size_t i = 0; i < cmd.size(); i++) {
|
|
|
|
builtin_complete_add2(cmd.at(i).c_str(), COMMAND, short_opt, gnu_opt, old_opt, result_mode,
|
|
|
|
condition, comp, desc, flags);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (size_t i = 0; i < path.size(); i++) {
|
|
|
|
builtin_complete_add2(path.at(i).c_str(), PATH, short_opt, gnu_opt, old_opt, result_mode,
|
|
|
|
condition, comp, desc, flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-01-23 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static void builtin_complete_remove_cmd(const wcstring &cmd, int cmd_type, const wchar_t *short_opt,
|
2016-01-17 14:42:14 +08:00
|
|
|
const wcstring_list_t &gnu_opt,
|
2016-04-20 10:49:15 +08:00
|
|
|
const wcstring_list_t &old_opt) {
|
2016-01-17 14:42:14 +08:00
|
|
|
bool removed = false;
|
|
|
|
size_t i;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (i = 0; short_opt[i] != L'\0'; i++) {
|
2016-01-17 14:42:14 +08:00
|
|
|
complete_remove(cmd, cmd_type, wcstring(1, short_opt[i]), option_type_short);
|
|
|
|
removed = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
for (i = 0; i < old_opt.size(); i++) {
|
2016-01-17 14:42:14 +08:00
|
|
|
complete_remove(cmd, cmd_type, old_opt.at(i), option_type_single_long);
|
|
|
|
removed = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
for (i = 0; i < gnu_opt.size(); i++) {
|
2016-01-17 14:42:14 +08:00
|
|
|
complete_remove(cmd, cmd_type, gnu_opt.at(i), option_type_double_long);
|
|
|
|
removed = true;
|
2014-09-03 06:10:52 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
if (!removed) {
|
|
|
|
// This means that all loops were empty.
|
2016-01-17 14:42:14 +08:00
|
|
|
complete_remove_all(cmd, cmd_type);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-01-23 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static void builtin_complete_remove(const wcstring_list_t &cmd, const wcstring_list_t &path,
|
|
|
|
const wchar_t *short_opt, const wcstring_list_t &gnu_opt,
|
|
|
|
const wcstring_list_t &old_opt) {
|
|
|
|
for (size_t i = 0; i < cmd.size(); i++) {
|
2016-01-17 14:42:14 +08:00
|
|
|
builtin_complete_remove_cmd(cmd.at(i), COMMAND, short_opt, gnu_opt, old_opt);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (size_t i = 0; i < path.size(); i++) {
|
2016-01-17 14:42:14 +08:00
|
|
|
builtin_complete_remove_cmd(path.at(i), PATH, short_opt, gnu_opt, old_opt);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-01-23 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// The complete builtin. Used for specifying programmable tab-completions. Calls the functions in
|
|
|
|
// complete.cpp for any heavy lifting.
|
|
|
|
int builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2011-12-27 11:18:46 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-11-18 06:53:50 +08:00
|
|
|
static int recursion_level = 0;
|
|
|
|
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2016-04-20 10:49:15 +08:00
|
|
|
int result_mode = SHARED;
|
2012-11-19 08:30:30 +08:00
|
|
|
int remove = 0;
|
|
|
|
wcstring short_opt;
|
|
|
|
wcstring_list_t gnu_opt, old_opt;
|
2016-04-20 10:49:15 +08:00
|
|
|
const wchar_t *comp = L"", *desc = L"", *condition = L"";
|
2012-02-27 05:46:21 +08:00
|
|
|
bool do_complete = false;
|
|
|
|
wcstring do_complete_param;
|
2016-11-18 06:53:50 +08:00
|
|
|
wcstring_list_t cmd_to_complete;
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t path;
|
2014-08-16 09:14:36 +08:00
|
|
|
wcstring_list_t wrap_targets;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-06-11 03:30:09 +08:00
|
|
|
static const wchar_t *short_options = L":a:c:p:s:l:o:d:frxeuAn:C::w:h";
|
|
|
|
static const struct woption long_options[] = {{L"exclusive", no_argument, NULL, 'x'},
|
|
|
|
{L"no-files", no_argument, NULL, 'f'},
|
|
|
|
{L"require-parameter", no_argument, NULL, 'r'},
|
|
|
|
{L"path", required_argument, NULL, 'p'},
|
|
|
|
{L"command", required_argument, NULL, 'c'},
|
|
|
|
{L"short-option", required_argument, NULL, 's'},
|
|
|
|
{L"long-option", required_argument, NULL, 'l'},
|
|
|
|
{L"old-option", required_argument, NULL, 'o'},
|
|
|
|
{L"description", required_argument, NULL, 'd'},
|
|
|
|
{L"arguments", required_argument, NULL, 'a'},
|
|
|
|
{L"erase", no_argument, NULL, 'e'},
|
|
|
|
{L"unauthoritative", no_argument, NULL, 'u'},
|
|
|
|
{L"authoritative", no_argument, NULL, 'A'},
|
|
|
|
{L"condition", required_argument, NULL, 'n'},
|
|
|
|
{L"wraps", required_argument, NULL, 'w'},
|
|
|
|
{L"do-complete", optional_argument, NULL, 'C'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2016-11-18 06:53:50 +08:00
|
|
|
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'x': {
|
2012-11-19 16:31:03 +08:00
|
|
|
result_mode |= EXCLUSIVE;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'f': {
|
2012-11-19 16:31:03 +08:00
|
|
|
result_mode |= NO_FILES;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'r': {
|
2012-11-19 16:31:03 +08:00
|
|
|
result_mode |= NO_COMMON;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'p':
|
2016-04-20 10:49:15 +08:00
|
|
|
case 'c': {
|
2013-11-25 14:57:49 +08:00
|
|
|
wcstring tmp;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (unescape_string(w.woptarg, &tmp, UNESCAPE_SPECIAL)) {
|
|
|
|
if (opt == 'p')
|
2012-11-19 16:31:03 +08:00
|
|
|
path.push_back(tmp);
|
|
|
|
else
|
2016-11-18 06:53:50 +08:00
|
|
|
cmd_to_complete.push_back(tmp);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2016-12-20 01:05:18 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid token '%ls'\n"), cmd, w.woptarg);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 'd': {
|
2015-07-26 09:16:00 +08:00
|
|
|
desc = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'u': {
|
2017-01-10 03:21:34 +08:00
|
|
|
// This option was removed in commit 1911298 and is now a no-op.
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'A': {
|
2017-01-10 03:21:34 +08:00
|
|
|
// This option was removed in commit 1911298 and is now a no-op.
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 's': {
|
2015-07-26 09:16:00 +08:00
|
|
|
short_opt.append(w.woptarg);
|
2016-07-05 11:28:21 +08:00
|
|
|
if (w.woptarg[0] == '\0') {
|
2016-12-20 01:05:18 +08:00
|
|
|
streams.err.append_format(_(L"%ls: -s requires a non-empty string\n"), cmd);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-07-05 11:28:21 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2015-07-26 09:16:00 +08:00
|
|
|
gnu_opt.push_back(w.woptarg);
|
2016-07-05 11:28:21 +08:00
|
|
|
if (w.woptarg[0] == '\0') {
|
2016-12-20 01:05:18 +08:00
|
|
|
streams.err.append_format(_(L"%ls: -l requires a non-empty string\n"), cmd);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-07-05 11:28:21 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'o': {
|
2015-07-26 09:16:00 +08:00
|
|
|
old_opt.push_back(w.woptarg);
|
2016-07-05 11:28:21 +08:00
|
|
|
if (w.woptarg[0] == '\0') {
|
2016-12-20 01:05:18 +08:00
|
|
|
streams.err.append_format(_(L"%ls: -o requires a non-empty string\n"), cmd);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-07-05 11:28:21 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
2015-07-26 09:16:00 +08:00
|
|
|
comp = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'e': {
|
2012-11-19 16:31:03 +08:00
|
|
|
remove = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2015-07-26 09:16:00 +08:00
|
|
|
condition = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'w': {
|
2015-07-26 09:16:00 +08:00
|
|
|
wrap_targets.push_back(w.woptarg);
|
2014-08-16 09:14:36 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'C': {
|
2012-11-19 16:31:03 +08:00
|
|
|
do_complete = true;
|
2015-09-11 09:08:42 +08:00
|
|
|
const wchar_t *arg = w.woptarg ? w.woptarg : reader_get_buffer();
|
2016-04-20 10:49:15 +08:00
|
|
|
if (arg == NULL) {
|
|
|
|
// This corresponds to using 'complete -C' in non-interactive mode.
|
|
|
|
// See #2361.
|
2016-11-18 06:53:50 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2015-09-11 09:08:42 +08:00
|
|
|
}
|
|
|
|
do_complete_param = arg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2015-09-11 09:08:42 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 'h': {
|
2016-11-18 06:53:50 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_OK;
|
2016-11-18 06:53:50 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2016-11-18 06:53:50 +08:00
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
2016-11-18 06:53:50 +08:00
|
|
|
DIE("unexpected retval from wgetopt_long");
|
2016-10-30 08:25:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-11-18 06:53:50 +08:00
|
|
|
if (w.woptind != argc) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-11-18 06:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (condition && wcslen(condition)) {
|
2016-10-23 02:21:13 +08:00
|
|
|
const wcstring condition_string = condition;
|
|
|
|
parse_error_list_t errors;
|
|
|
|
if (parse_util_detect_errors(condition_string, &errors,
|
|
|
|
false /* do not accept incomplete */)) {
|
2016-11-18 06:53:50 +08:00
|
|
|
streams.err.append_format(L"%ls: Condition '%ls' contained a syntax error", cmd,
|
2016-10-23 02:21:13 +08:00
|
|
|
condition);
|
|
|
|
for (size_t i = 0; i < errors.size(); i++) {
|
2016-11-18 06:53:50 +08:00
|
|
|
streams.err.append_format(L"\n%s: ", cmd);
|
2016-10-23 02:21:13 +08:00
|
|
|
streams.err.append(errors.at(i).describe(condition_string));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 06:53:50 +08:00
|
|
|
if (comp && wcslen(comp)) {
|
2016-10-23 02:21:13 +08:00
|
|
|
wcstring prefix;
|
2016-11-18 06:53:50 +08:00
|
|
|
prefix.append(cmd);
|
|
|
|
prefix.append(L": ");
|
2014-03-19 05:14:32 +08:00
|
|
|
|
2016-10-23 02:21:13 +08:00
|
|
|
wcstring err_text;
|
|
|
|
if (parser.detect_errors_in_argument_list(comp, &err_text, prefix.c_str())) {
|
2016-11-18 06:53:50 +08:00
|
|
|
streams.err.append_format(L"%ls: Completion '%ls' contained a syntax error\n", cmd,
|
2016-10-23 02:21:13 +08:00
|
|
|
comp);
|
|
|
|
streams.err.append(err_text);
|
|
|
|
streams.err.push_back(L'\n');
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:28:23 +08:00
|
|
|
if (do_complete) {
|
|
|
|
const wchar_t *token;
|
|
|
|
|
2016-12-04 12:12:53 +08:00
|
|
|
parse_util_token_extent(do_complete_param.c_str(), do_complete_param.size(), &token, 0, 0,
|
|
|
|
0);
|
2016-10-31 12:28:23 +08:00
|
|
|
|
|
|
|
// Create a scoped transient command line, so that bulitin_commandline will see our
|
|
|
|
// argument, not the reader buffer.
|
|
|
|
builtin_commandline_scoped_transient_t temp_buffer(do_complete_param);
|
|
|
|
|
|
|
|
if (recursion_level < 1) {
|
|
|
|
recursion_level++;
|
|
|
|
|
|
|
|
std::vector<completion_t> comp;
|
|
|
|
complete(do_complete_param, &comp, COMPLETION_REQUEST_DEFAULT,
|
2016-12-04 12:12:53 +08:00
|
|
|
env_vars_snapshot_t::current());
|
2016-10-31 12:28:23 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < comp.size(); i++) {
|
|
|
|
const completion_t &next = comp.at(i);
|
|
|
|
|
|
|
|
// Make a fake commandline, and then apply the completion to it.
|
|
|
|
const wcstring faux_cmdline = token;
|
|
|
|
size_t tmp_cursor = faux_cmdline.size();
|
|
|
|
wcstring faux_cmdline_with_completion = completion_apply_to_command_line(
|
|
|
|
next.completion, next.flags, faux_cmdline, &tmp_cursor, false);
|
|
|
|
|
|
|
|
// completion_apply_to_command_line will append a space unless COMPLETE_NO_SPACE
|
|
|
|
// is set. We don't want to set COMPLETE_NO_SPACE because that won't close
|
|
|
|
// quotes. What we want is to close the quote, but not append the space. So we
|
|
|
|
// just look for the space and clear it.
|
|
|
|
if (!(next.flags & COMPLETE_NO_SPACE) &&
|
|
|
|
string_suffixes_string(L" ", faux_cmdline_with_completion)) {
|
2016-12-04 12:12:53 +08:00
|
|
|
faux_cmdline_with_completion.resize(faux_cmdline_with_completion.size() - 1);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:28:23 +08:00
|
|
|
// The input data is meant to be something like you would have on the command
|
|
|
|
// line, e.g. includes backslashes. The output should be raw, i.e. unescaped. So
|
|
|
|
// we need to unescape the command line. See #1127.
|
|
|
|
unescape_string_in_place(&faux_cmdline_with_completion, UNESCAPE_DEFAULT);
|
|
|
|
streams.out.append(faux_cmdline_with_completion);
|
|
|
|
|
|
|
|
// Append any description.
|
|
|
|
if (!next.description.empty()) {
|
|
|
|
streams.out.push_back(L'\t');
|
|
|
|
streams.out.append(next.description);
|
|
|
|
}
|
|
|
|
streams.out.push_back(L'\n');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:28:23 +08:00
|
|
|
recursion_level--;
|
|
|
|
}
|
2016-11-18 06:53:50 +08:00
|
|
|
} else if (cmd_to_complete.empty() && path.empty()) {
|
2016-10-31 12:28:23 +08:00
|
|
|
// No arguments specified, meaning we print the definitions of all specified completions
|
|
|
|
// to stdout.
|
|
|
|
streams.out.append(complete_print());
|
|
|
|
} else {
|
|
|
|
int flags = COMPLETE_AUTO_SPACE;
|
2016-04-20 10:49:15 +08:00
|
|
|
|
2016-10-31 12:28:23 +08:00
|
|
|
if (remove) {
|
2016-11-18 06:53:50 +08:00
|
|
|
builtin_complete_remove(cmd_to_complete, path, short_opt.c_str(), gnu_opt, old_opt);
|
2016-10-31 12:28:23 +08:00
|
|
|
} else {
|
2016-11-18 06:53:50 +08:00
|
|
|
builtin_complete_add(cmd_to_complete, path, short_opt.c_str(), gnu_opt, old_opt,
|
2016-12-20 01:05:18 +08:00
|
|
|
result_mode, condition, comp, desc, flags);
|
2016-10-31 12:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle wrap targets (probably empty). We only wrap commands, not paths.
|
|
|
|
for (size_t w = 0; w < wrap_targets.size(); w++) {
|
|
|
|
const wcstring &wrap_target = wrap_targets.at(w);
|
2016-11-18 06:53:50 +08:00
|
|
|
for (size_t i = 0; i < cmd_to_complete.size(); i++) {
|
|
|
|
(remove ? complete_remove_wrapper : complete_add_wrapper)(cmd_to_complete.at(i),
|
|
|
|
wrap_target);
|
2014-08-16 09:14:36 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_OK;
|
2006-01-23 05:10:55 +08:00
|
|
|
}
|