2016-04-29 03:10:27 +00:00
|
|
|
// String expansion functions. These functions perform several kinds of parameter expansion.
|
2016-04-21 06:00:54 +00:00
|
|
|
// IWYU pragma: no_include <cstddef>
|
|
|
|
#include "config.h"
|
2006-08-11 01:18:35 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2016-04-29 03:10:27 +00:00
|
|
|
#include <stdlib.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2016-04-29 03:10:27 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
|
2015-09-15 18:40:20 +00:00
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <sys/sysctl.h> // IWYU pragma: keep
|
2015-09-15 18:40:20 +00:00
|
|
|
#endif
|
2005-09-20 13:26:39 +00:00
|
|
|
#ifdef SunOS
|
|
|
|
#include <procfs.h>
|
|
|
|
#endif
|
2016-04-21 06:00:54 +00:00
|
|
|
#if __APPLE__
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
2016-04-29 03:10:27 +00:00
|
|
|
#include <sys/stat.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#endif
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory> // IWYU pragma: keep
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2016-04-29 03:10:27 +00:00
|
|
|
#include "complete.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "env.h"
|
|
|
|
#include "exec.h"
|
2016-04-29 03:10:27 +00:00
|
|
|
#include "expand.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2013-11-29 21:31:18 +00:00
|
|
|
#include "iothread.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "parse_constants.h"
|
2016-04-29 03:10:27 +00:00
|
|
|
#include "parse_util.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2016-04-21 06:00:54 +00:00
|
|
|
#ifdef KERN_PROCARGS2
|
|
|
|
#else
|
|
|
|
#include "tokenizer.h"
|
|
|
|
#endif
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Description for child process.
|
|
|
|
#define COMPLETE_CHILD_PROCESS_DESC _(L"Child process")
|
|
|
|
|
|
|
|
/// Description for non-child process.
|
|
|
|
#define COMPLETE_PROCESS_DESC _(L"Process")
|
|
|
|
|
|
|
|
/// Description for long job.
|
|
|
|
#define COMPLETE_JOB_DESC _(L"Job")
|
|
|
|
|
|
|
|
/// Description for short job. The job command is concatenated.
|
|
|
|
#define COMPLETE_JOB_DESC_VAL _(L"Job: %ls")
|
|
|
|
|
|
|
|
/// Description for the shells own pid.
|
|
|
|
#define COMPLETE_SELF_DESC _(L"Shell process")
|
|
|
|
|
|
|
|
/// Description for the shells own pid.
|
|
|
|
#define COMPLETE_LAST_DESC _(L"Last background job")
|
|
|
|
|
|
|
|
/// String in process expansion denoting ourself.
|
2005-09-20 13:26:39 +00:00
|
|
|
#define SELF_STR L"self"
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// String in process expansion denoting last background job.
|
2005-09-20 13:26:39 +00:00
|
|
|
#define LAST_STR L"last"
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Characters which make a string unclean if they are the first character of the string. See \c
|
|
|
|
/// expand_is_clean().
|
2005-10-26 10:51:02 +00:00
|
|
|
#define UNCLEAN_FIRST L"~%"
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Unclean characters. See \c expand_is_clean().
|
2005-10-26 10:51:02 +00:00
|
|
|
#define UNCLEAN L"$*?\\\"'({})"
|
|
|
|
|
2016-02-05 09:22:46 +00:00
|
|
|
static void remove_internal_separator(wcstring *s, bool conv);
|
2012-07-16 19:05:36 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Test if the specified argument is clean, i.e. it does not contain any tokens which need to be
|
|
|
|
/// expanded or otherwise altered. Clean strings can be passed through expand_string and expand_one
|
|
|
|
/// without changing them. About two thirds of all strings are clean, so skipping expansion on them
|
|
|
|
/// actually does save a small amount of time, since it avoids multiple memory allocations during
|
|
|
|
/// the expansion process.
|
|
|
|
///
|
|
|
|
/// \param in the string to test
|
|
|
|
static bool expand_is_clean(const wcstring &in) {
|
|
|
|
if (in.empty()) return true;
|
|
|
|
|
|
|
|
// Test characters that have a special meaning in the first character position.
|
|
|
|
if (wcschr(UNCLEAN_FIRST, in.at(0)) != NULL) return false;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Test characters that have a special meaning in any character position.
|
2016-02-05 07:57:21 +00:00
|
|
|
return in.find_first_of(UNCLEAN) == wcstring::npos;
|
2005-10-26 10:51:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Append a syntax error to the given error list.
|
|
|
|
static void append_syntax_error(parse_error_list_t *errors, size_t source_start, const wchar_t *fmt,
|
|
|
|
...) {
|
|
|
|
if (errors != NULL) {
|
2014-03-22 00:13:33 +00:00
|
|
|
parse_error_t error;
|
|
|
|
error.source_start = source_start;
|
|
|
|
error.source_length = 0;
|
|
|
|
error.code = parse_error_syntax;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
error.text = vformat_string(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
errors->push_back(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Append a cmdsub error to the given error list.
|
|
|
|
static void append_cmdsub_error(parse_error_list_t *errors, size_t source_start, const wchar_t *fmt,
|
|
|
|
...) {
|
|
|
|
if (errors != NULL) {
|
2014-03-22 00:13:33 +00:00
|
|
|
parse_error_t error;
|
|
|
|
error.source_start = source_start;
|
|
|
|
error.source_length = 0;
|
|
|
|
error.code = parse_error_cmdsubst;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
error.text = vformat_string(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
errors->push_back(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Return the environment variable value for the string starting at \c in.
|
|
|
|
static env_var_t expand_var(const wchar_t *in) {
|
|
|
|
if (!in) return env_var_t::missing_var();
|
2012-11-19 00:30:30 +00:00
|
|
|
return env_get_string(in);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Test if the specified string does not contain character which can not be used inside a quoted
|
|
|
|
/// string.
|
|
|
|
static int is_quotable(const wchar_t *str) {
|
|
|
|
switch (*str) {
|
|
|
|
case 0: {
|
2012-11-19 08:31:03 +00:00
|
|
|
return 1;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
case L'\n':
|
|
|
|
case L'\t':
|
|
|
|
case L'\r':
|
|
|
|
case L'\b':
|
2016-12-24 00:37:00 +00:00
|
|
|
case L'\e': {
|
2012-11-19 08:31:03 +00:00
|
|
|
return 0;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
|
|
|
default: { return is_quotable(str + 1); }
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static int is_quotable(const wcstring &str) { return is_quotable(str.c_str()); }
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
wcstring expand_escape_variable(const wcstring &in) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t lst;
|
|
|
|
wcstring buff;
|
2006-06-21 00:48:36 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
tokenize_variable_array(in, lst);
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-11-19 23:45:08 +00:00
|
|
|
size_t size = lst.size();
|
2016-10-23 03:32:25 +00:00
|
|
|
if (size == 0) {
|
|
|
|
buff.append(L"''");
|
|
|
|
} else if (size == 1) {
|
|
|
|
const wcstring &el = lst.at(0);
|
|
|
|
|
|
|
|
if (el.find(L' ') != wcstring::npos && is_quotable(el)) {
|
|
|
|
buff.append(L"'");
|
|
|
|
buff.append(el);
|
|
|
|
buff.append(L"'");
|
|
|
|
} else {
|
|
|
|
buff.append(escape_string(el, 1));
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
2016-10-23 03:32:25 +00:00
|
|
|
} else {
|
|
|
|
for (size_t j = 0; j < lst.size(); j++) {
|
|
|
|
const wcstring &el = lst.at(j);
|
|
|
|
if (j) buff.append(L" ");
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-10-23 03:32:25 +00:00
|
|
|
if (is_quotable(el)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
buff.append(L"'");
|
|
|
|
buff.append(el);
|
|
|
|
buff.append(L"'");
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2012-11-19 00:30:30 +00:00
|
|
|
buff.append(escape_string(el, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buff;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Tests if all characters in the wide string are numeric.
|
|
|
|
static int iswnumeric(const wchar_t *n) {
|
|
|
|
for (; *n; n++) {
|
|
|
|
if (*n < L'0' || *n > L'9') {
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// See if the process described by \c proc matches the commandline \c cmd.
|
2016-10-09 21:38:26 +00:00
|
|
|
static bool match_pid(const wcstring &cmd, const wchar_t *proc, size_t *offset) {
|
2016-04-29 03:10:27 +00:00
|
|
|
// Test for a direct match. If the proc string is empty (e.g. the user tries to complete against
|
|
|
|
// %), then return an offset pointing at the base command. That ensures that you don't see a
|
|
|
|
// bunch of dumb paths when completing against all processes.
|
|
|
|
if (proc[0] != L'\0' && wcsncmp(cmd.c_str(), proc, wcslen(proc)) == 0) {
|
|
|
|
if (offset) *offset = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Get the command to match against. We're only interested in the last path component.
|
2012-07-16 19:05:36 +00:00
|
|
|
const wcstring base_cmd = wbasename(cmd);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
bool result = string_prefixes_string(proc, base_cmd);
|
2016-10-22 18:21:13 +00:00
|
|
|
// It's a match. Return the offset within the full command.
|
|
|
|
if (result && offset) *offset = cmd.size() - base_cmd.size();
|
2012-07-16 19:05:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Helper class for iterating over processes. The names returned have been unescaped (e.g. may
|
|
|
|
/// include spaces).
|
2012-07-16 19:05:36 +00:00
|
|
|
#ifdef KERN_PROCARGS2
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// BSD / OS X process completions.
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
class process_iterator_t {
|
2012-07-16 19:05:36 +00:00
|
|
|
std::vector<pid_t> pids;
|
|
|
|
size_t idx;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring name_for_pid(pid_t pid);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
public:
|
2012-07-16 19:05:36 +00:00
|
|
|
process_iterator_t();
|
|
|
|
bool next_process(wcstring *str, pid_t *pid);
|
|
|
|
};
|
2006-12-14 11:58:11 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
wcstring process_iterator_t::name_for_pid(pid_t pid) {
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring result;
|
|
|
|
int mib[4], maxarg = 0, numArgs = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
char *args = NULL, *stringPtr = NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_ARGMAX;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
size = sizeof(maxarg);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (sysctl(mib, 2, &maxarg, &size, NULL, 0) == -1) {
|
2012-07-16 19:05:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
args = (char *)malloc(maxarg);
|
2017-05-05 05:42:42 +00:00
|
|
|
if (args == NULL) { // cppcheck-suppress memleak
|
2012-07-16 19:05:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROCARGS2;
|
|
|
|
mib[2] = pid;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
size = (size_t)maxarg;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (sysctl(mib, 3, args, &size, NULL, 0) == -1) {
|
2012-11-19 00:30:30 +00:00
|
|
|
free(args);
|
2016-04-29 03:10:27 +00:00
|
|
|
return result;
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
memcpy(&numArgs, args, sizeof(numArgs));
|
2012-07-16 19:05:36 +00:00
|
|
|
stringPtr = args + sizeof(numArgs);
|
|
|
|
result = str2wcstring(stringPtr);
|
|
|
|
free(args);
|
|
|
|
return result;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool process_iterator_t::next_process(wcstring *out_str, pid_t *out_pid) {
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring name;
|
|
|
|
pid_t pid = 0;
|
|
|
|
bool result = false;
|
2016-04-29 03:10:27 +00:00
|
|
|
while (idx < pids.size()) {
|
2012-07-16 19:05:36 +00:00
|
|
|
pid = pids.at(idx++);
|
|
|
|
name = name_for_pid(pid);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!name.empty()) {
|
2012-07-16 19:05:36 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
if (result) {
|
2012-07-16 19:05:36 +00:00
|
|
|
*out_str = name;
|
|
|
|
*out_pid = pid;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
process_iterator_t::process_iterator_t() : idx(0) {
|
|
|
|
int err;
|
|
|
|
struct kinfo_proc *result;
|
|
|
|
bool done;
|
|
|
|
static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
|
|
|
|
// Declaring name as const requires us to cast it when passing it to sysctl because the
|
|
|
|
// prototype doesn't include the const modifier.
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
// We start by calling sysctl with result == NULL and length == 0. That will succeed, and set
|
|
|
|
// length to the appropriate length. We then allocate a buffer of that size and call sysctl
|
|
|
|
// again with that buffer. If that succeeds, we're done. If that fails with ENOMEM, we have to
|
|
|
|
// throw away our buffer and loop. Note that the loop causes use to call sysctl with NULL
|
|
|
|
// again; this is necessary because the ENOMEM failure case sets length to the amount of data
|
|
|
|
// returned, not the amount of data that could have been returned.
|
2012-07-16 19:05:36 +00:00
|
|
|
result = NULL;
|
|
|
|
done = false;
|
2016-04-29 03:10:27 +00:00
|
|
|
do {
|
2012-07-16 19:05:36 +00:00
|
|
|
assert(result == NULL);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
// Call sysctl with a NULL buffer.
|
|
|
|
length = 0;
|
2016-04-29 03:10:27 +00:00
|
|
|
err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
|
|
|
|
if (err == -1) {
|
2012-07-16 19:05:36 +00:00
|
|
|
err = errno;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Allocate an appropriately sized buffer based on the results from the previous call.
|
|
|
|
if (err == 0) {
|
2012-07-16 19:05:36 +00:00
|
|
|
result = (struct kinfo_proc *)malloc(length);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (result == NULL) {
|
2012-07-16 19:05:36 +00:00
|
|
|
err = ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Call sysctl again with the new buffer. If we get an ENOMEM error, toss away our buffer
|
|
|
|
// and start again.
|
|
|
|
if (err == 0) {
|
|
|
|
err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0);
|
|
|
|
if (err == -1) {
|
2012-07-16 19:05:36 +00:00
|
|
|
err = errno;
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
if (err == 0) {
|
2012-07-16 19:05:36 +00:00
|
|
|
done = true;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (err == ENOMEM) {
|
2012-07-16 19:05:36 +00:00
|
|
|
assert(result != NULL);
|
|
|
|
free(result);
|
|
|
|
result = NULL;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
} while (err == 0 && !done);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
// Clean up and establish post conditions.
|
2016-04-29 03:10:27 +00:00
|
|
|
if (err == 0 && result != NULL) {
|
2012-07-16 19:05:36 +00:00
|
|
|
for (size_t idx = 0; idx < length / sizeof(struct kinfo_proc); idx++)
|
|
|
|
pids.push_back(result[idx].kp_proc.p_pid);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (result) free(result);
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// /proc style process completions.
|
|
|
|
class process_iterator_t {
|
2012-07-16 19:05:36 +00:00
|
|
|
DIR *dir;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
public:
|
2012-07-16 19:05:36 +00:00
|
|
|
process_iterator_t();
|
|
|
|
~process_iterator_t();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
bool next_process(wcstring *out_str, pid_t *out_pid);
|
|
|
|
};
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
process_iterator_t::process_iterator_t(void) { dir = opendir("/proc"); }
|
2012-07-16 19:05:36 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
process_iterator_t::~process_iterator_t(void) {
|
|
|
|
if (dir) closedir(dir);
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool process_iterator_t::next_process(wcstring *out_str, pid_t *out_pid) {
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring cmd;
|
|
|
|
pid_t pid = 0;
|
2016-04-29 03:10:27 +00:00
|
|
|
while (cmd.empty()) {
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring name;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!dir || !wreaddir(dir, name)) break;
|
|
|
|
if (!iswnumeric(name.c_str())) continue;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring path = wcstring(L"/proc/") + name;
|
|
|
|
struct stat buf;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (wstat(path, &buf)) continue;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (buf.st_uid != getuid()) continue;
|
2012-07-16 19:05:36 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Remember the pid.
|
2016-11-23 04:24:03 +00:00
|
|
|
pid = fish_wcstoi(name.c_str());
|
|
|
|
if (errno || pid < 0) {
|
|
|
|
debug(1, _(L"Unexpected failure to convert pid '%ls' to integer\n"), name.c_str());
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// The 'cmdline' file exists, it should contain the commandline.
|
2012-07-16 19:05:36 +00:00
|
|
|
FILE *cmdfile;
|
2016-04-29 03:10:27 +00:00
|
|
|
if ((cmdfile = wfopen(path + L"/cmdline", "r"))) {
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring full_command_line;
|
2012-11-19 00:30:30 +00:00
|
|
|
fgetws2(&full_command_line, cmdfile);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// The command line needs to be escaped.
|
2015-07-26 07:58:32 +00:00
|
|
|
cmd = tok_first(full_command_line);
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
|
|
|
#ifdef SunOS
|
2016-04-29 03:10:27 +00:00
|
|
|
else if ((cmdfile = wfopen(path + L"/psinfo", "r"))) {
|
2012-07-16 19:05:36 +00:00
|
|
|
psinfo_t info;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (fread(&info, sizeof(info), 1, cmdfile)) {
|
|
|
|
// The filename is unescaped.
|
2012-07-16 19:05:36 +00:00
|
|
|
cmd = str2wcstring(info.pr_fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-29 03:10:27 +00:00
|
|
|
if (cmdfile) fclose(cmdfile);
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool result = !cmd.empty();
|
|
|
|
if (result) {
|
2012-07-16 19:05:36 +00:00
|
|
|
*out_str = cmd;
|
|
|
|
*out_pid = pid;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2012-07-16 19:05:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// The following function is invoked on the main thread, because the job list is not thread safe.
|
2017-01-27 04:00:43 +00:00
|
|
|
/// It should search the job list for something matching the given proc, and then return true to
|
|
|
|
/// stop the search, false to continue it.
|
|
|
|
static bool find_job(const wchar_t *proc, expand_flags_t flags,
|
|
|
|
std::vector<completion_t> *completions) {
|
2013-11-29 21:31:18 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-01-23 18:44:36 +00:00
|
|
|
bool found = false;
|
2016-04-29 03:10:27 +00:00
|
|
|
// If we are not doing tab completion, we first check for the single '%' character, because an
|
|
|
|
// empty string will pass the numeric check below. But if we are doing tab completion, we want
|
|
|
|
// all of the job IDs as completion options, not just the last job backgrounded, so we pass this
|
2015-11-25 02:27:14 +00:00
|
|
|
// first block in favor of the second.
|
2016-04-29 03:10:27 +00:00
|
|
|
if (wcslen(proc) == 0 && !(flags & EXPAND_FOR_COMPLETIONS)) {
|
|
|
|
// This is an empty job expansion: '%'. It expands to the last job backgrounded.
|
2013-11-29 21:31:18 +00:00
|
|
|
job_iterator_t jobs;
|
2017-01-26 22:47:32 +00:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!j->command_is_empty()) {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, to_string<long>(j->pgid));
|
2013-11-29 21:31:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
// You don't *really* want to flip a coin between killing the last process backgrounded and
|
|
|
|
// all processes, do you? Let's not try other match methods with the solo '%' syntax.
|
2017-01-23 18:44:36 +00:00
|
|
|
found = true;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (iswnumeric(proc)) {
|
|
|
|
// This is a numeric job string, like '%2'.
|
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
2013-09-19 00:19:30 +00:00
|
|
|
job_iterator_t jobs;
|
2017-01-26 22:47:32 +00:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2013-11-29 21:31:18 +00:00
|
|
|
wchar_t jid[16];
|
2016-04-29 03:10:27 +00:00
|
|
|
if (j->command_is_empty()) continue;
|
2013-11-29 21:31:18 +00:00
|
|
|
|
|
|
|
swprintf(jid, 16, L"%d", j->job_id);
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (wcsncmp(proc, jid, wcslen(proc)) == 0) {
|
2013-11-29 21:31:18 +00:00
|
|
|
wcstring desc_buff = format_string(COMPLETE_JOB_DESC_VAL, j->command_wcstr());
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, jid + wcslen(proc), desc_buff, 0);
|
2013-09-19 00:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2016-11-23 04:24:03 +00:00
|
|
|
int jid = fish_wcstoi(proc);
|
|
|
|
if (!errno && jid > 0) {
|
2017-01-26 22:47:32 +00:00
|
|
|
const job_t *j = job_get(jid);
|
|
|
|
if (j && !j->command_is_empty()) {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, to_string<long>(j->pgid));
|
2012-08-07 07:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
// Stop here so you can't match a random process name when you're just trying to use job
|
|
|
|
// control.
|
2017-01-23 18:44:36 +00:00
|
|
|
found = true;
|
2013-11-29 21:31:18 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
job_iterator_t jobs;
|
2017-01-26 22:47:32 +00:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2016-10-30 22:37:16 +00:00
|
|
|
if (j->command_is_empty()) continue;
|
|
|
|
|
|
|
|
size_t offset;
|
|
|
|
if (match_pid(j->command(), proc, &offset)) {
|
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, j->command_wcstr() + offset + wcslen(proc),
|
2016-10-30 22:37:16 +00:00
|
|
|
COMPLETE_JOB_DESC, 0);
|
|
|
|
} else {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, to_string<long>(j->pgid));
|
2016-10-30 22:37:16 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs.reset();
|
2017-01-26 22:47:32 +00:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2016-10-30 22:37:16 +00:00
|
|
|
if (j->command_is_empty()) continue;
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-10-30 22:37:16 +00:00
|
|
|
if (p->actual_cmd.empty()) continue;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-08-07 07:01:48 +00:00
|
|
|
size_t offset;
|
2016-10-30 22:37:16 +00:00
|
|
|
if (match_pid(p->actual_cmd, proc, &offset)) {
|
2016-04-29 03:10:27 +00:00
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, wcstring(p->actual_cmd, offset + wcslen(proc)),
|
2016-10-30 22:37:16 +00:00
|
|
|
COMPLETE_CHILD_PROCESS_DESC, 0);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2017-01-23 18:44:36 +00:00
|
|
|
append_completion(completions, to_string<long>(p->pid), L"", 0);
|
2012-08-07 07:01:48 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-29 21:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Searches for a job with the specified job id, or a job or process which has the string \c proc
|
|
|
|
/// as a prefix of its commandline. Appends the name of the process as a completion in 'out'.
|
|
|
|
///
|
|
|
|
/// Otherwise, any job matching the specified string is matched, and the job pgid is returned. If no
|
|
|
|
/// job matches, all child processes are searched. If no child processes match, and <tt>fish</tt>
|
|
|
|
/// can understand the contents of the /proc filesystem, all the users processes are searched for
|
|
|
|
/// matches.
|
|
|
|
static void find_process(const wchar_t *proc, expand_flags_t flags,
|
|
|
|
std::vector<completion_t> *out) {
|
|
|
|
if (!(flags & EXPAND_SKIP_JOBS)) {
|
2017-01-23 18:44:36 +00:00
|
|
|
bool found = false;
|
2017-01-27 04:00:43 +00:00
|
|
|
iothread_perform_on_main([&]() { found = find_job(proc, flags, out); });
|
2016-04-29 03:10:27 +00:00
|
|
|
if (found) {
|
2014-05-30 20:49:55 +00:00
|
|
|
return;
|
2012-08-07 07:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Iterate over all processes.
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring process_name;
|
|
|
|
pid_t process_pid;
|
|
|
|
process_iterator_t iterator;
|
2016-04-29 03:10:27 +00:00
|
|
|
while (iterator.next_process(&process_name, &process_pid)) {
|
2012-08-04 18:07:42 +00:00
|
|
|
size_t offset;
|
2016-10-09 21:38:26 +00:00
|
|
|
if (match_pid(process_name, proc, &offset)) {
|
2016-04-29 03:10:27 +00:00
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
|
|
|
append_completion(out, process_name.c_str() + offset + wcslen(proc),
|
|
|
|
COMPLETE_PROCESS_DESC, 0);
|
|
|
|
} else {
|
2012-07-17 19:47:01 +00:00
|
|
|
append_completion(out, to_string<long>(process_pid));
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Process id expansion.
|
|
|
|
static bool expand_pid(const wcstring &instr_with_sep, expand_flags_t flags,
|
|
|
|
std::vector<completion_t> *out, parse_error_list_t *errors) {
|
|
|
|
// Hack. If there's no INTERNAL_SEP and no PROCESS_EXPAND, then there's nothing to do. Check out
|
|
|
|
// this "null terminated string."
|
2014-01-07 22:57:58 +00:00
|
|
|
const wchar_t some_chars[] = {INTERNAL_SEPARATOR, PROCESS_EXPAND, L'\0'};
|
2016-04-29 03:10:27 +00:00
|
|
|
if (instr_with_sep.find_first_of(some_chars) == wcstring::npos) {
|
|
|
|
// Nothing to do.
|
2014-01-07 22:57:58 +00:00
|
|
|
append_completion(out, instr_with_sep);
|
2014-05-30 20:49:55 +00:00
|
|
|
return true;
|
2014-01-07 22:57:58 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// expand_string calls us with internal separators in instr...sigh.
|
2012-07-16 19:05:36 +00:00
|
|
|
wcstring instr = instr_with_sep;
|
2016-02-05 09:22:46 +00:00
|
|
|
remove_internal_separator(&instr, false);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (instr.empty() || instr.at(0) != PROCESS_EXPAND) {
|
|
|
|
// Not a process expansion.
|
2012-07-17 19:47:01 +00:00
|
|
|
append_completion(out, instr);
|
2014-05-30 20:49:55 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
const wchar_t *const in = instr.c_str();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// We know we are a process expansion now.
|
2014-05-30 20:49:55 +00:00
|
|
|
assert(in[0] == PROCESS_EXPAND);
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
|
|
|
if (wcsncmp(in + 1, SELF_STR, wcslen(in + 1)) == 0) {
|
|
|
|
append_completion(out, &SELF_STR[wcslen(in + 1)], COMPLETE_SELF_DESC, 0);
|
|
|
|
} else if (wcsncmp(in + 1, LAST_STR, wcslen(in + 1)) == 0) {
|
|
|
|
append_completion(out, &LAST_STR[wcslen(in + 1)], COMPLETE_LAST_DESC, 0);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
if (wcscmp((in + 1), SELF_STR) == 0) {
|
2012-07-17 19:47:01 +00:00
|
|
|
append_completion(out, to_string<long>(getpid()));
|
2014-05-30 20:49:55 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
if (wcscmp((in + 1), LAST_STR) == 0) {
|
|
|
|
if (proc_last_bg_pid > 0) {
|
2012-07-17 19:47:01 +00:00
|
|
|
append_completion(out, to_string<long>(proc_last_bg_pid));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2014-05-30 20:49:55 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// This is sort of crummy - find_process doesn't return any indication of success, so instead we
|
|
|
|
// check to see if it inserted any completions.
|
2015-07-28 01:45:47 +00:00
|
|
|
const size_t prev_count = out->size();
|
2016-04-29 03:10:27 +00:00
|
|
|
find_process(in + 1, flags, out);
|
|
|
|
|
2016-10-22 18:21:13 +00:00
|
|
|
if (prev_count == out->size() && !(flags & EXPAND_FOR_COMPLETIONS)) {
|
|
|
|
// We failed to find anything.
|
|
|
|
append_syntax_error(errors, 1, FAILED_EXPANSION_PROCESS_ERR_MSG,
|
2017-03-14 03:19:08 +00:00
|
|
|
escape_string(in + 1, ESCAPE_NO_QUOTED).c_str());
|
2016-10-22 18:21:13 +00:00
|
|
|
return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2014-05-30 20:49:55 +00:00
|
|
|
return true;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-01-30 19:15:06 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Parse an array slicing specification Returns 0 on success. If a parse error occurs, returns the
|
|
|
|
/// index of the bad token. Note that 0 can never be a bad index because the string always starts
|
|
|
|
/// with [.
|
|
|
|
static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector<long> &idx,
|
|
|
|
std::vector<size_t> &source_positions, size_t array_size) {
|
2012-08-04 20:02:44 +00:00
|
|
|
const long size = (long)array_size;
|
2016-04-29 03:10:27 +00:00
|
|
|
size_t pos = 1; // skip past the opening square bracket
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
while (1) {
|
|
|
|
while (iswspace(in[pos]) || (in[pos] == INTERNAL_SEPARATOR)) pos++;
|
|
|
|
if (in[pos] == L']') {
|
2012-11-19 00:30:30 +00:00
|
|
|
pos++;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2014-03-22 00:13:33 +00:00
|
|
|
const size_t i1_src_pos = pos;
|
2016-11-23 04:24:03 +00:00
|
|
|
const wchar_t *end;
|
|
|
|
long tmp = fish_wcstol(&in[pos], &end);
|
|
|
|
// We don't test `*end` as is typically done because we expect it to not be the null char.
|
|
|
|
// Ignore the case of errno==-1 because it means the end char wasn't the null char.
|
|
|
|
if (errno > 0) {
|
2014-08-21 07:26:14 +00:00
|
|
|
return pos;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
// debug( 0, L"Push idx %d", tmp );
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
long i1 = tmp > -1 ? tmp : (long)array_size + tmp + 1;
|
|
|
|
pos = end - in;
|
|
|
|
while (in[pos] == INTERNAL_SEPARATOR) pos++;
|
|
|
|
if (in[pos] == L'.' && in[pos + 1] == L'.') {
|
|
|
|
pos += 2;
|
|
|
|
while (in[pos] == INTERNAL_SEPARATOR) pos++;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2014-03-22 00:13:33 +00:00
|
|
|
const size_t number_start = pos;
|
2016-11-23 04:24:03 +00:00
|
|
|
long tmp1 = fish_wcstol(&in[pos], &end);
|
|
|
|
// Ignore the case of errno==-1 because it means the end char wasn't the null char.
|
|
|
|
if (errno > 0) {
|
2014-08-21 07:26:14 +00:00
|
|
|
return pos;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
pos = end - in;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
// debug( 0, L"Push range %d %d", tmp, tmp1 );
|
2016-04-29 03:10:27 +00:00
|
|
|
long i2 = tmp1 > -1 ? tmp1 : size + tmp1 + 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
// debug( 0, L"Push range idx %d %d", i1, i2 );
|
2016-04-29 03:10:27 +00:00
|
|
|
short direction = i2 < i1 ? -1 : 1;
|
|
|
|
for (long jjj = i1; jjj * direction <= i2 * direction; jjj += direction) {
|
2012-11-19 00:30:30 +00:00
|
|
|
// debug(0, L"Expand range [subst]: %i\n", jjj);
|
|
|
|
idx.push_back(jjj);
|
2014-03-22 00:13:33 +00:00
|
|
|
source_positions.push_back(number_start);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
// debug( 0, L"Push idx %d", tmp );
|
|
|
|
idx.push_back(i1);
|
2014-03-22 00:13:33 +00:00
|
|
|
source_positions.push_back(i1_src_pos);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (end_ptr) {
|
|
|
|
*end_ptr = (wchar_t *)(in + pos);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
2006-08-24 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Expand all environment variables in the string *ptr.
|
|
|
|
///
|
|
|
|
/// This function is slow, fragile and complicated. There are lots of little corner cases, like
|
|
|
|
/// $$foo should do a double expansion, $foo$bar should not double expand bar, etc. Also, it's easy
|
|
|
|
/// to accidentally leak memory on array out of bounds errors an various other situations. All in
|
|
|
|
/// all, this function should be rewritten, split out into multiple logical units and carefully
|
|
|
|
/// tested. After that, it can probably be optimized to do fewer memory allocations, fewer string
|
|
|
|
/// scans and overall just less work. But until that happens, don't edit it unless you know exactly
|
|
|
|
/// what you are doing, and do proper testing afterwards.
|
|
|
|
///
|
|
|
|
/// This function operates on strings backwards, starting at last_idx.
|
|
|
|
///
|
|
|
|
/// Note: last_idx is considered to be where it previously finished procesisng. This means it
|
|
|
|
/// actually starts operating on last_idx-1. As such, to process a string fully, pass string.size()
|
|
|
|
/// as last_idx instead of string.size()-1.
|
|
|
|
static int expand_variables(const wcstring &instr, std::vector<completion_t> *out, long last_idx,
|
|
|
|
parse_error_list_t *errors) {
|
2014-08-28 20:19:38 +00:00
|
|
|
const size_t insize = instr.size();
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// last_idx may be 1 past the end of the string, but no further.
|
2014-08-28 20:19:38 +00:00
|
|
|
assert(last_idx >= 0 && (size_t)last_idx <= insize);
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (last_idx == 0) {
|
2014-08-24 21:28:31 +00:00
|
|
|
append_completion(out, instr);
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-28 20:19:38 +00:00
|
|
|
|
2014-08-24 21:28:31 +00:00
|
|
|
bool is_ok = true;
|
|
|
|
bool empty = false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-01-31 05:33:15 +00:00
|
|
|
wcstring var_tmp;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// List of indexes.
|
2012-01-31 05:33:15 +00:00
|
|
|
std::vector<long> var_idx_list;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Parallel array of source positions of each index in the variable list.
|
2014-03-22 00:13:33 +00:00
|
|
|
std::vector<size_t> var_pos_list;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// CHECK( out, 0 );
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
for (long i = last_idx - 1; (i >= 0) && is_ok && !empty; i--) {
|
2014-08-24 21:28:31 +00:00
|
|
|
const wchar_t c = instr.at(i);
|
2016-10-30 22:37:16 +00:00
|
|
|
if (c != VARIABLE_EXPAND && c != VARIABLE_EXPAND_SINGLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
long var_len;
|
|
|
|
int is_single = (c == VARIABLE_EXPAND_SINGLE);
|
|
|
|
size_t start_pos = i + 1;
|
|
|
|
size_t stop_pos = start_pos;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
while (stop_pos < insize) {
|
|
|
|
const wchar_t nc = instr.at(stop_pos);
|
|
|
|
if (nc == VARIABLE_EXPAND_EMPTY) {
|
2012-11-19 00:30:30 +00:00
|
|
|
stop_pos++;
|
2016-10-30 22:37:16 +00:00
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-04-20 06:43:02 +00:00
|
|
|
if (!valid_var_name_char(nc)) break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
stop_pos++;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-01-14 04:34:15 +00:00
|
|
|
// fwprintf(stdout, L"Stop for '%c'\n", in[stop_pos]);
|
2016-10-30 22:37:16 +00:00
|
|
|
var_len = stop_pos - start_pos;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
if (var_len == 0) {
|
|
|
|
if (errors) {
|
|
|
|
parse_util_expand_variable_error(instr, 0 /* global_token_pos */, i, errors);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
is_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var_tmp.append(instr, start_pos, var_len);
|
|
|
|
env_var_t var_val;
|
|
|
|
if (var_len == 1 && var_tmp[0] == VARIABLE_EXPAND_EMPTY) {
|
|
|
|
var_val = env_var_t::missing_var();
|
|
|
|
} else {
|
|
|
|
var_val = expand_var(var_tmp.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!var_val.missing()) {
|
|
|
|
int all_vars = 1;
|
|
|
|
wcstring_list_t var_item_list;
|
|
|
|
|
|
|
|
if (is_ok) {
|
|
|
|
tokenize_variable_array(var_val, var_item_list);
|
|
|
|
|
|
|
|
const size_t slice_start = stop_pos;
|
|
|
|
if (slice_start < insize && instr.at(slice_start) == L'[') {
|
|
|
|
wchar_t *slice_end;
|
|
|
|
size_t bad_pos;
|
|
|
|
all_vars = 0;
|
|
|
|
const wchar_t *in = instr.c_str();
|
|
|
|
bad_pos = parse_slice(in + slice_start, &slice_end, var_idx_list, var_pos_list,
|
|
|
|
var_item_list.size());
|
|
|
|
if (bad_pos != 0) {
|
|
|
|
append_syntax_error(errors, stop_pos + bad_pos, L"Invalid index value");
|
|
|
|
is_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stop_pos = (slice_end - in);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
if (!all_vars) {
|
|
|
|
wcstring_list_t string_values(var_idx_list.size());
|
|
|
|
for (size_t j = 0; j < var_idx_list.size(); j++) {
|
|
|
|
long tmp = var_idx_list.at(j);
|
|
|
|
// Check that we are within array bounds. If not, truncate the list to
|
|
|
|
// exit.
|
|
|
|
if (tmp < 1 || (size_t)tmp > var_item_list.size()) {
|
|
|
|
size_t var_src_pos = var_pos_list.at(j);
|
|
|
|
// The slice was parsed starting at stop_pos, so we have to add that
|
|
|
|
// to the error position.
|
|
|
|
append_syntax_error(errors, slice_start + var_src_pos,
|
|
|
|
ARRAY_BOUNDS_ERR);
|
2014-08-24 21:28:31 +00:00
|
|
|
is_ok = false;
|
2016-10-30 22:37:16 +00:00
|
|
|
var_idx_list.resize(j);
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
2016-10-30 22:37:16 +00:00
|
|
|
} else {
|
|
|
|
// Replace each index in var_idx_list inplace with the string value
|
|
|
|
// at the specified index.
|
|
|
|
// al_set( var_idx_list, j, wcsdup((const wchar_t *)al_get(
|
|
|
|
// &var_item_list, tmp-1 ) ) );
|
|
|
|
string_values.at(j) = var_item_list.at(tmp - 1);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
// string_values is the new var_item_list.
|
2017-01-27 00:14:50 +00:00
|
|
|
var_item_list = std::move(string_values);
|
2016-10-30 22:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
if (!is_ok) {
|
|
|
|
return is_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_single) {
|
|
|
|
wcstring res(instr, 0, i);
|
|
|
|
if (i > 0) {
|
|
|
|
if (instr.at(i - 1) != VARIABLE_EXPAND_SINGLE) {
|
|
|
|
res.push_back(INTERNAL_SEPARATOR);
|
|
|
|
} else if (var_item_list.empty() || var_item_list.front().empty()) {
|
|
|
|
// First expansion is empty, but we need to recursively expand.
|
|
|
|
res.push_back(VARIABLE_EXPAND_EMPTY);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
for (size_t j = 0; j < var_item_list.size(); j++) {
|
|
|
|
const wcstring &next = var_item_list.at(j);
|
|
|
|
if (is_ok) {
|
|
|
|
if (j != 0) res.append(L" ");
|
|
|
|
res.append(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(stop_pos <= insize);
|
|
|
|
res.append(instr, stop_pos, insize - stop_pos);
|
|
|
|
is_ok &= expand_variables(res, out, i, errors);
|
|
|
|
} else {
|
|
|
|
for (size_t j = 0; j < var_item_list.size(); j++) {
|
|
|
|
const wcstring &next = var_item_list.at(j);
|
|
|
|
if (is_ok && i == 0 && stop_pos == insize) {
|
|
|
|
append_completion(out, next);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2016-10-30 22:37:16 +00:00
|
|
|
if (is_ok) {
|
|
|
|
wcstring new_in;
|
|
|
|
new_in.append(instr, 0, i);
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
if (instr.at(i - 1) != VARIABLE_EXPAND) {
|
|
|
|
new_in.push_back(INTERNAL_SEPARATOR);
|
|
|
|
} else if (next.empty()) {
|
|
|
|
new_in.push_back(VARIABLE_EXPAND_EMPTY);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
assert(stop_pos <= insize);
|
|
|
|
new_in.append(next);
|
|
|
|
new_in.append(instr, stop_pos, insize - stop_pos);
|
|
|
|
is_ok &= expand_variables(new_in, out, i, errors);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
return is_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even with no value, we still need to parse out slice syntax. Behave as though we
|
|
|
|
// had 1 value, so $foo[1] always works.
|
|
|
|
const size_t slice_start = stop_pos;
|
|
|
|
if (slice_start < insize && instr.at(slice_start) == L'[') {
|
|
|
|
const wchar_t *in = instr.c_str();
|
|
|
|
wchar_t *slice_end;
|
|
|
|
size_t bad_pos;
|
|
|
|
|
|
|
|
bad_pos = parse_slice(in + slice_start, &slice_end, var_idx_list, var_pos_list, 1);
|
|
|
|
if (bad_pos != 0) {
|
|
|
|
append_syntax_error(errors, stop_pos + bad_pos, L"Invalid index value");
|
|
|
|
is_ok = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
return is_ok;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
stop_pos = (slice_end - in);
|
|
|
|
|
|
|
|
// Validate that the parsed indexes are valid.
|
|
|
|
for (size_t j = 0; j < var_idx_list.size(); j++) {
|
|
|
|
long tmp = var_idx_list.at(j);
|
|
|
|
if (tmp != 1) {
|
|
|
|
size_t var_src_pos = var_pos_list.at(j);
|
|
|
|
append_syntax_error(errors, slice_start + var_src_pos, ARRAY_BOUNDS_ERR);
|
2016-05-04 22:19:47 +00:00
|
|
|
is_ok = 0;
|
|
|
|
return is_ok;
|
|
|
|
}
|
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2016-10-30 22:37:16 +00:00
|
|
|
// Expand a non-existing variable.
|
|
|
|
if (c == VARIABLE_EXPAND) {
|
|
|
|
// Regular expansion, i.e. expand this argument to nothing.
|
|
|
|
empty = true;
|
|
|
|
} else {
|
|
|
|
// Expansion to single argument.
|
|
|
|
wcstring res;
|
|
|
|
res.append(instr, 0, i);
|
|
|
|
if (i > 0 && instr.at(i - 1) == VARIABLE_EXPAND_SINGLE) {
|
|
|
|
res.push_back(VARIABLE_EXPAND_EMPTY);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
assert(stop_pos <= insize);
|
|
|
|
res.append(instr, stop_pos, insize - stop_pos);
|
|
|
|
|
|
|
|
is_ok &= expand_variables(res, out, i, errors);
|
|
|
|
return is_ok;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!empty) {
|
2014-08-24 21:28:31 +00:00
|
|
|
append_completion(out, instr);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return is_ok;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Perform bracket expansion.
|
|
|
|
static expand_error_t expand_brackets(const wcstring &instr, expand_flags_t flags,
|
|
|
|
std::vector<completion_t> *out, parse_error_list_t *errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
bool syntax_error = false;
|
2016-04-29 03:10:27 +00:00
|
|
|
int bracket_count = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-20 21:52:53 +00:00
|
|
|
const wchar_t *bracket_begin = NULL, *bracket_end = NULL;
|
|
|
|
const wchar_t *last_sep = NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *item_begin;
|
2012-11-20 21:52:53 +00:00
|
|
|
size_t length_preceding_brackets, length_following_brackets, tot_len;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
const wchar_t *const in = instr.c_str();
|
|
|
|
|
|
|
|
// Locate the first non-nested bracket pair.
|
|
|
|
for (const wchar_t *pos = in; (*pos) && !syntax_error; pos++) {
|
|
|
|
switch (*pos) {
|
|
|
|
case BRACKET_BEGIN: {
|
|
|
|
if (bracket_count == 0) bracket_begin = pos;
|
2012-11-19 08:31:03 +00:00
|
|
|
bracket_count++;
|
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
case BRACKET_END: {
|
2012-11-19 08:31:03 +00:00
|
|
|
bracket_count--;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (bracket_count < 0) {
|
2012-11-19 08:31:03 +00:00
|
|
|
syntax_error = true;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (bracket_count == 0) {
|
2012-11-20 21:52:53 +00:00
|
|
|
bracket_end = pos;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
case BRACKET_SEP: {
|
|
|
|
if (bracket_count == 1) last_sep = pos;
|
2016-11-02 05:36:30 +00:00
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-11-03 01:29:14 +00:00
|
|
|
default: {
|
|
|
|
break; // we ignore all other characters here
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (bracket_count > 0) {
|
|
|
|
if (!(flags & EXPAND_FOR_COMPLETIONS)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
syntax_error = true;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
// The user hasn't typed an end bracket yet; make one up and append it, then expand
|
|
|
|
// that.
|
2012-02-22 20:00:02 +00:00
|
|
|
wcstring mod;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (last_sep) {
|
|
|
|
mod.append(in, bracket_begin - in + 1);
|
|
|
|
mod.append(last_sep + 1);
|
2012-11-19 00:30:30 +00:00
|
|
|
mod.push_back(BRACKET_END);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2012-02-22 20:00:02 +00:00
|
|
|
mod.append(in);
|
|
|
|
mod.push_back(BRACKET_END);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Note: this code looks very fishy, apparently it has never worked.
|
2016-02-05 07:57:21 +00:00
|
|
|
return expand_brackets(mod, 1, out, errors);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (syntax_error) {
|
|
|
|
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched brackets"));
|
2016-02-05 09:22:46 +00:00
|
|
|
return EXPAND_ERROR;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (bracket_begin == NULL) {
|
2012-11-20 21:52:53 +00:00
|
|
|
append_completion(out, instr);
|
2016-02-05 09:22:46 +00:00
|
|
|
return EXPAND_OK;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
length_preceding_brackets = (bracket_begin - in);
|
|
|
|
length_following_brackets = wcslen(bracket_end) - 1;
|
|
|
|
tot_len = length_preceding_brackets + length_following_brackets;
|
|
|
|
item_begin = bracket_begin + 1;
|
|
|
|
for (const wchar_t *pos = (bracket_begin + 1); true; pos++) {
|
2016-10-22 18:21:13 +00:00
|
|
|
if (bracket_count == 0 && ((*pos == BRACKET_SEP) || (pos == bracket_end))) {
|
|
|
|
assert(pos >= item_begin);
|
|
|
|
size_t item_len = pos - item_begin;
|
|
|
|
|
|
|
|
wcstring whole_item;
|
|
|
|
whole_item.reserve(tot_len + item_len + 2);
|
|
|
|
whole_item.append(in, length_preceding_brackets);
|
|
|
|
whole_item.append(item_begin, item_len);
|
|
|
|
whole_item.append(bracket_end + 1);
|
|
|
|
expand_brackets(whole_item, flags, out, errors);
|
|
|
|
|
|
|
|
item_begin = pos + 1;
|
|
|
|
if (pos == bracket_end) break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (*pos == BRACKET_BEGIN) {
|
2012-11-19 00:30:30 +00:00
|
|
|
bracket_count++;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (*pos == BRACKET_END) {
|
2012-11-19 00:30:30 +00:00
|
|
|
bracket_count--;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
return EXPAND_OK;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Perform cmdsubst expansion.
|
|
|
|
static int expand_cmdsubst(const wcstring &input, std::vector<completion_t> *out_list,
|
|
|
|
parse_error_list_t *errors) {
|
|
|
|
wchar_t *paran_begin = 0, *paran_end = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
std::vector<wcstring> sub_res;
|
2012-11-19 00:30:30 +00:00
|
|
|
size_t i, j;
|
|
|
|
wchar_t *tail_begin = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
const wchar_t *const in = input.c_str();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int parse_ret;
|
2016-04-29 03:10:27 +00:00
|
|
|
switch (parse_ret = parse_util_locate_cmdsubst(in, ¶n_begin, ¶n_end, false)) {
|
|
|
|
case -1: {
|
|
|
|
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, L"Mismatched parenthesis");
|
2012-11-19 08:31:03 +00:00
|
|
|
return 0;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
|
|
|
case 0: {
|
2014-01-07 22:57:58 +00:00
|
|
|
append_completion(out_list, input);
|
2012-11-19 08:31:03 +00:00
|
|
|
return 1;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
|
|
|
case 1: {
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
2016-11-03 01:29:14 +00:00
|
|
|
default: {
|
|
|
|
DIE("unhandled parse_ret value");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
const wcstring subcmd(paran_begin + 1, paran_end - paran_begin - 1);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
if (exec_subshell(subcmd, sub_res, true /* do apply exit status */) == -1) {
|
|
|
|
append_cmdsub_error(errors, SOURCE_LOCATION_UNKNOWN,
|
|
|
|
L"Unknown error while evaulating command substitution");
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
tail_begin = paran_end + 1;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (*tail_begin == L'[') {
|
2012-11-19 00:30:30 +00:00
|
|
|
std::vector<long> slice_idx;
|
2014-03-22 00:13:33 +00:00
|
|
|
std::vector<size_t> slice_source_positions;
|
2016-04-29 03:10:27 +00:00
|
|
|
const wchar_t *const slice_begin = tail_begin;
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t *slice_end;
|
2014-08-21 07:26:14 +00:00
|
|
|
size_t bad_pos;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bad_pos =
|
|
|
|
parse_slice(slice_begin, &slice_end, slice_idx, slice_source_positions, sub_res.size());
|
|
|
|
if (bad_pos != 0) {
|
2014-08-21 07:26:14 +00:00
|
|
|
append_syntax_error(errors, slice_begin - in + bad_pos, L"Invalid index value");
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
wcstring_list_t sub_res2;
|
|
|
|
tail_begin = slice_end;
|
|
|
|
for (i = 0; i < slice_idx.size(); i++) {
|
|
|
|
long idx = slice_idx.at(i);
|
|
|
|
if (idx < 1 || (size_t)idx > sub_res.size()) {
|
|
|
|
size_t pos = slice_source_positions.at(i);
|
|
|
|
append_syntax_error(errors, slice_begin - in + pos, ARRAY_BOUNDS_ERR);
|
|
|
|
return 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
idx = idx - 1;
|
|
|
|
|
|
|
|
sub_res2.push_back(sub_res.at(idx));
|
|
|
|
// debug( 0, L"Pushing item '%ls' with index %d onto sliced result", al_get(
|
|
|
|
// sub_res, idx ), idx );
|
|
|
|
// sub_res[idx] = 0; // ??
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
sub_res = sub_res2;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Recursively call ourselves to expand any remaining command substitutions. The result of this
|
|
|
|
// recursive call using the tail of the string is inserted into the tail_expand array list
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> tail_expand;
|
2016-04-29 03:10:27 +00:00
|
|
|
expand_cmdsubst(tail_begin, &tail_expand, errors); // TODO: offset error locations
|
|
|
|
|
|
|
|
// Combine the result of the current command substitution with the result of the recursive tail
|
|
|
|
// expansion.
|
|
|
|
for (i = 0; i < sub_res.size(); i++) {
|
2014-01-07 22:57:58 +00:00
|
|
|
const wcstring &sub_item = sub_res.at(i);
|
|
|
|
const wcstring sub_item2 = escape_string(sub_item, 1);
|
|
|
|
|
|
|
|
wcstring whole_item;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
for (j = 0; j < tail_expand.size(); j++) {
|
2014-01-07 22:57:58 +00:00
|
|
|
whole_item.clear();
|
|
|
|
const wcstring &tail_item = tail_expand.at(j).completion;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// sb_append_substring( &whole_item, in, len1 );
|
|
|
|
whole_item.append(in, paran_begin - in);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// sb_append_char( &whole_item, INTERNAL_SEPARATOR );
|
2011-12-27 03:18:46 +00:00
|
|
|
whole_item.push_back(INTERNAL_SEPARATOR);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// sb_append_substring( &whole_item, sub_item2, item_len );
|
2012-11-19 00:30:30 +00:00
|
|
|
whole_item.append(sub_item2);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// sb_append_char( &whole_item, INTERNAL_SEPARATOR );
|
2011-12-27 03:18:46 +00:00
|
|
|
whole_item.push_back(INTERNAL_SEPARATOR);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// sb_append( &whole_item, tail_item );
|
2011-12-27 03:18:46 +00:00
|
|
|
whole_item.append(tail_item);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// al_push( out, whole_item.buff );
|
2014-01-07 22:57:58 +00:00
|
|
|
append_completion(out_list, whole_item);
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Given that input[0] is HOME_DIRECTORY or tilde (ugh), return the user's name. Return the empty
|
|
|
|
// string if it is just a tilde. Also return by reference the index of the first character of the
|
|
|
|
// remaining part of the string (e.g. the subsequent slash).
|
|
|
|
static wcstring get_home_directory_name(const wcstring &input, size_t *out_tail_idx) {
|
|
|
|
const wchar_t *const in = input.c_str();
|
2013-04-08 06:54:43 +00:00
|
|
|
assert(in[0] == HOME_DIRECTORY || in[0] == L'~');
|
|
|
|
size_t tail_idx;
|
|
|
|
|
|
|
|
const wchar_t *name_end = wcschr(in, L'/');
|
2016-04-29 03:10:27 +00:00
|
|
|
if (name_end) {
|
2013-04-08 06:54:43 +00:00
|
|
|
tail_idx = name_end - in;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2013-04-08 06:54:43 +00:00
|
|
|
tail_idx = wcslen(in);
|
|
|
|
}
|
|
|
|
*out_tail_idx = tail_idx;
|
|
|
|
return input.substr(1, tail_idx - 1);
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Attempts tilde expansion of the string specified, modifying it in place.
|
|
|
|
static void expand_home_directory(wcstring &input) {
|
|
|
|
if (!input.empty() && input.at(0) == HOME_DIRECTORY) {
|
2011-12-27 03:18:46 +00:00
|
|
|
size_t tail_idx;
|
2013-04-08 06:54:43 +00:00
|
|
|
wcstring username = get_home_directory_name(input, &tail_idx);
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2013-04-08 06:54:43 +00:00
|
|
|
bool tilde_error = false;
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring home;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (username.empty()) {
|
|
|
|
// Current users home directory.
|
2012-11-19 00:30:30 +00:00
|
|
|
home = env_get_string(L"HOME");
|
2011-12-27 03:18:46 +00:00
|
|
|
tail_idx = 1;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
// Some other users home directory.
|
2013-04-08 06:54:43 +00:00
|
|
|
std::string name_cstr = wcs2string(username);
|
2017-05-10 05:23:32 +00:00
|
|
|
struct passwd userinfo;
|
|
|
|
struct passwd *result;
|
|
|
|
char buf[8192];
|
|
|
|
int retval = getpwnam_r(name_cstr.c_str(), &userinfo, buf, sizeof(buf), &result);
|
|
|
|
if (retval || !result) {
|
2013-04-08 06:54:43 +00:00
|
|
|
tilde_error = true;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2017-05-10 05:23:32 +00:00
|
|
|
home = str2wcstring(userinfo.pw_dir);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2015-01-20 09:04:01 +00:00
|
|
|
wchar_t *realhome = wrealpath(home, NULL);
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (!tilde_error && realhome) {
|
2014-10-25 05:58:52 +00:00
|
|
|
input.replace(input.begin(), input.begin() + tail_idx, realhome);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2014-10-25 05:58:52 +00:00
|
|
|
input[0] = L'~';
|
2015-01-20 09:04:01 +00:00
|
|
|
}
|
|
|
|
free((void *)realhome);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
void expand_tilde(wcstring &input) {
|
|
|
|
// Avoid needless COW behavior by ensuring we use const at.
|
2013-04-08 06:54:43 +00:00
|
|
|
const wcstring &tmp = input;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!tmp.empty() && tmp.at(0) == L'~') {
|
2012-11-19 00:30:30 +00:00
|
|
|
input.at(0) = HOME_DIRECTORY;
|
|
|
|
expand_home_directory(input);
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static void unexpand_tildes(const wcstring &input, std::vector<completion_t> *completions) {
|
|
|
|
// If input begins with tilde, then try to replace the corresponding string in each completion
|
|
|
|
// with the tilde. If it does not, there's nothing to do.
|
|
|
|
if (input.empty() || input.at(0) != L'~') return;
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// We only operate on completions that replace their contents. If we don't have any, we're done.
|
2013-04-08 06:54:43 +00:00
|
|
|
// In particular, empty vectors are common.
|
|
|
|
bool has_candidate_completion = false;
|
2016-04-29 03:10:27 +00:00
|
|
|
for (size_t i = 0; i < completions->size(); i++) {
|
|
|
|
if (completions->at(i).flags & COMPLETE_REPLACES_TOKEN) {
|
2013-04-08 06:54:43 +00:00
|
|
|
has_candidate_completion = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!has_candidate_completion) return;
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2013-04-08 06:54:43 +00:00
|
|
|
size_t tail_idx;
|
|
|
|
wcstring username_with_tilde = L"~";
|
|
|
|
username_with_tilde.append(get_home_directory_name(input, &tail_idx));
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Expand username_with_tilde.
|
2013-04-08 06:54:43 +00:00
|
|
|
wcstring home = username_with_tilde;
|
|
|
|
expand_tilde(home);
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Now for each completion that starts with home, replace it with the username_with_tilde.
|
|
|
|
for (size_t i = 0; i < completions->size(); i++) {
|
2013-04-08 06:54:43 +00:00
|
|
|
completion_t &comp = completions->at(i);
|
2016-04-29 03:10:27 +00:00
|
|
|
if ((comp.flags & COMPLETE_REPLACES_TOKEN) &&
|
|
|
|
string_prefixes_string(home, comp.completion)) {
|
2013-04-08 06:54:43 +00:00
|
|
|
comp.completion.replace(0, home.size(), username_with_tilde);
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// And mark that our tilde is literal, so it doesn't try to escape it.
|
2013-04-08 06:54:43 +00:00
|
|
|
comp.flags |= COMPLETE_DONT_ESCAPE_TILDES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// If the given path contains the user's home directory, replace that with a tilde. We don't try to
|
|
|
|
// be smart about case insensitivity, etc.
|
|
|
|
wcstring replace_home_directory_with_tilde(const wcstring &str) {
|
|
|
|
// Only absolute paths get this treatment.
|
2013-12-16 23:33:20 +00:00
|
|
|
wcstring result = str;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (string_prefixes_string(L"/", result)) {
|
2013-12-16 23:33:20 +00:00
|
|
|
wcstring home_directory = L"~";
|
|
|
|
expand_tilde(home_directory);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!string_suffixes_string(L"/", home_directory)) {
|
2013-12-16 23:33:20 +00:00
|
|
|
home_directory.push_back(L'/');
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Now check if the home_directory prefixes the string.
|
|
|
|
if (string_prefixes_string(home_directory, result)) {
|
2013-12-16 23:33:20 +00:00
|
|
|
// Success
|
|
|
|
result.replace(0, home_directory.size(), L"~/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Remove any internal separators. Also optionally convert wildcard characters to regular
|
|
|
|
/// equivalents. This is done to support EXPAND_SKIP_WILDCARDS.
|
|
|
|
static void remove_internal_separator(wcstring *str, bool conv) {
|
|
|
|
// Remove all instances of INTERNAL_SEPARATOR.
|
2016-02-05 09:22:46 +00:00
|
|
|
str->erase(std::remove(str->begin(), str->end(), (wchar_t)INTERNAL_SEPARATOR), str->end());
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// If conv is true, replace all instances of ANY_CHAR with '?', ANY_STRING with '*',
|
|
|
|
// ANY_STRING_RECURSIVE with '*'.
|
|
|
|
if (conv) {
|
|
|
|
for (size_t idx = 0; idx < str->size(); idx++) {
|
|
|
|
switch (str->at(idx)) {
|
|
|
|
case ANY_CHAR: {
|
2016-02-05 09:22:46 +00:00
|
|
|
str->at(idx) = L'?';
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
case ANY_STRING:
|
2016-04-29 03:10:27 +00:00
|
|
|
case ANY_STRING_RECURSIVE: {
|
2016-02-05 09:22:46 +00:00
|
|
|
str->at(idx) = L'*';
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2016-04-29 03:10:27 +00:00
|
|
|
}
|
2016-11-03 01:29:14 +00:00
|
|
|
default: {
|
|
|
|
break; // we ignore all other characters
|
|
|
|
}
|
2012-07-16 19:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// A stage in string expansion is represented as a function that takes an input and returns a list
|
|
|
|
/// of output (by reference). We get flags and errors. It may return an error; if so expansion
|
|
|
|
/// halts.
|
2016-11-02 02:12:14 +00:00
|
|
|
typedef expand_error_t (*expand_stage_t)(const wcstring &input, //!OCLINT(unused param)
|
|
|
|
std::vector<completion_t> *out, //!OCLINT(unused param)
|
|
|
|
expand_flags_t flags, //!OCLINT(unused param)
|
|
|
|
parse_error_list_t *errors); //!OCLINT(unused param)
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static expand_error_t expand_stage_cmdsubst(const wcstring &input, std::vector<completion_t> *out,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
2016-02-05 09:22:46 +00:00
|
|
|
expand_error_t result = EXPAND_OK;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (EXPAND_SKIP_CMDSUBST & flags) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t *begin, *end;
|
2016-04-29 03:10:27 +00:00
|
|
|
if (parse_util_locate_cmdsubst(input.c_str(), &begin, &end, true) == 0) {
|
2016-02-05 09:22:46 +00:00
|
|
|
append_completion(out, input);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
append_cmdsub_error(errors, SOURCE_LOCATION_UNKNOWN,
|
|
|
|
L"Command substitutions not allowed");
|
2016-02-05 09:22:46 +00:00
|
|
|
result = EXPAND_ERROR;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
2016-02-05 09:22:46 +00:00
|
|
|
int cmdsubst_ok = expand_cmdsubst(input, out, errors);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!cmdsubst_ok) {
|
2016-02-05 09:22:46 +00:00
|
|
|
result = EXPAND_ERROR;
|
2016-02-05 07:57:21 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static expand_error_t expand_stage_variables(const wcstring &input, std::vector<completion_t> *out,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
|
|
|
// We accept incomplete strings here, since complete uses expand_string to expand incomplete
|
|
|
|
// strings from the commandline.
|
2016-02-05 09:22:46 +00:00
|
|
|
wcstring next;
|
|
|
|
unescape_string(input, &next, UNESCAPE_SPECIAL | UNESCAPE_INCOMPLETE);
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (EXPAND_SKIP_VARIABLES & flags) {
|
|
|
|
for (size_t i = 0; i < next.size(); i++) {
|
|
|
|
if (next.at(i) == VARIABLE_EXPAND) {
|
2016-02-05 09:22:46 +00:00
|
|
|
next[i] = L'$';
|
2012-01-31 05:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
append_completion(out, next);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
if (!expand_variables(next, out, next.size(), errors)) {
|
2012-01-31 05:33:15 +00:00
|
|
|
return EXPAND_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
return EXPAND_OK;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static expand_error_t expand_stage_brackets(const wcstring &input, std::vector<completion_t> *out,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
2016-02-05 09:22:46 +00:00
|
|
|
return expand_brackets(input, flags, out, errors);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static expand_error_t expand_stage_home_and_pid(const wcstring &input,
|
|
|
|
std::vector<completion_t> *out,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
2016-02-05 09:22:46 +00:00
|
|
|
wcstring next = input;
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (!(EXPAND_SKIP_HOME_DIRECTORIES & flags)) {
|
2016-02-05 09:22:46 +00:00
|
|
|
expand_home_directory(next);
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
|
|
|
if (!next.empty() && next.at(0) == PROCESS_EXPAND) {
|
2016-02-05 09:22:46 +00:00
|
|
|
expand_pid(next, flags, out, NULL);
|
|
|
|
return EXPAND_OK;
|
2012-01-31 05:33:15 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
append_completion(out, next);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (!expand_pid(next, flags, out, errors)) {
|
2016-02-05 09:22:46 +00:00
|
|
|
return EXPAND_ERROR;
|
|
|
|
}
|
|
|
|
return EXPAND_OK;
|
|
|
|
}
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static expand_error_t expand_stage_wildcards(const wcstring &input, std::vector<completion_t> *out,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
2016-10-09 21:38:26 +00:00
|
|
|
UNUSED(errors);
|
2016-02-05 09:22:46 +00:00
|
|
|
expand_error_t result = EXPAND_OK;
|
2016-02-08 09:29:23 +00:00
|
|
|
wcstring path_to_expand = input;
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
remove_internal_separator(&path_to_expand, flags & EXPAND_SKIP_WILDCARDS);
|
2016-02-08 09:29:23 +00:00
|
|
|
const bool has_wildcard = wildcard_has(path_to_expand, true /* internal, i.e. ANY_CHAR */);
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (has_wildcard && (flags & EXECUTABLES_ONLY)) {
|
2016-11-02 03:42:02 +00:00
|
|
|
; // don't do wildcard expansion for executables, see issue #785
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (((flags & EXPAND_FOR_COMPLETIONS) && (!(flags & EXPAND_SKIP_WILDCARDS))) ||
|
|
|
|
has_wildcard) {
|
|
|
|
// We either have a wildcard, or we don't have a wildcard but we're doing completion
|
|
|
|
// expansion (so we want to get the completion of a file path). Note that if
|
|
|
|
// EXPAND_SKIP_WILDCARDS is set, we stomped wildcards in remove_internal_separator above, so
|
|
|
|
// there actually aren't any.
|
|
|
|
//
|
|
|
|
// So we're going to treat this input as a file path. Compute the "working directories",
|
|
|
|
// which may be CDPATH if the special flag is set.
|
2016-02-08 09:29:23 +00:00
|
|
|
const wcstring working_dir = env_get_pwd_slash();
|
|
|
|
wcstring_list_t effective_working_dirs;
|
2016-10-21 04:14:40 +00:00
|
|
|
bool for_cd = static_cast<bool>(flags & EXPAND_SPECIAL_FOR_CD);
|
|
|
|
bool for_command = static_cast<bool>(flags & EXPAND_SPECIAL_FOR_COMMAND);
|
2016-04-29 03:10:27 +00:00
|
|
|
if (!for_cd && !for_command) {
|
|
|
|
// Common case.
|
2016-02-08 09:29:23 +00:00
|
|
|
effective_working_dirs.push_back(working_dir);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
// Either EXPAND_SPECIAL_FOR_COMMAND or EXPAND_SPECIAL_FOR_CD. We can handle these
|
|
|
|
// mostly the same. There's the following differences:
|
|
|
|
//
|
|
|
|
// 1. An empty CDPATH should be treated as '.', but an empty PATH should be left empty
|
2017-04-04 06:16:11 +00:00
|
|
|
// (no commands can be found). Also, an empty element in either is treated as '.' for
|
|
|
|
// consistency with POSIX shells. Note that we rely on the latter by having called
|
|
|
|
// `munge_colon_delimited_array()` for these special env vars. Thus we do not
|
|
|
|
// special-case them here.
|
2016-04-29 03:10:27 +00:00
|
|
|
//
|
|
|
|
// 2. PATH is only "one level," while CDPATH is multiple levels. That is, input like
|
|
|
|
// 'foo/bar' should resolve against CDPATH, but not PATH.
|
|
|
|
//
|
|
|
|
// In either case, we ignore the path if we start with ./ or /. Also ignore it if we are
|
|
|
|
// doing command completion and we contain a slash, per IEEE 1003.1, chapter 8 under
|
|
|
|
// PATH.
|
2016-04-08 22:52:10 +00:00
|
|
|
if (string_prefixes_string(L"/", path_to_expand) ||
|
|
|
|
string_prefixes_string(L"./", path_to_expand) ||
|
|
|
|
string_prefixes_string(L"../", path_to_expand) ||
|
2016-04-29 03:10:27 +00:00
|
|
|
(for_command && path_to_expand.find(L'/') != wcstring::npos)) {
|
2016-02-08 09:29:23 +00:00
|
|
|
effective_working_dirs.push_back(working_dir);
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
// Get the PATH/CDPATH and cwd. Perhaps these should be passed in. An empty CDPATH
|
|
|
|
// implies just the current directory, while an empty PATH is left empty.
|
2016-04-07 22:24:52 +00:00
|
|
|
env_var_t paths = env_get_string(for_cd ? L"CDPATH" : L"PATH");
|
2016-04-29 03:10:27 +00:00
|
|
|
if (paths.missing_or_empty()) paths = for_cd ? L"." : L"";
|
|
|
|
|
2017-04-04 06:16:11 +00:00
|
|
|
// Tokenize it into path names.
|
2017-03-17 03:59:04 +00:00
|
|
|
std::vector<wcstring> pathsv;
|
|
|
|
tokenize_variable_array(paths, pathsv);
|
|
|
|
for (auto next_path : pathsv) {
|
2016-04-29 03:10:27 +00:00
|
|
|
effective_working_dirs.push_back(
|
|
|
|
path_apply_working_directory(next_path, working_dir));
|
2016-02-06 22:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2016-02-06 22:39:47 +00:00
|
|
|
result = EXPAND_WILDCARD_NO_MATCH;
|
2016-02-05 09:22:46 +00:00
|
|
|
std::vector<completion_t> expanded;
|
2016-04-29 03:10:27 +00:00
|
|
|
for (size_t wd_idx = 0; wd_idx < effective_working_dirs.size(); wd_idx++) {
|
|
|
|
int local_wc_res = wildcard_expand_string(
|
|
|
|
path_to_expand, effective_working_dirs.at(wd_idx), flags, &expanded);
|
|
|
|
if (local_wc_res > 0) {
|
|
|
|
// Something matched,so overall we matched.
|
2016-02-06 22:39:47 +00:00
|
|
|
result = EXPAND_WILDCARD_MATCH;
|
2016-04-29 03:10:27 +00:00
|
|
|
} else if (local_wc_res < 0) {
|
2016-02-06 22:39:47 +00:00
|
|
|
// Cancellation
|
|
|
|
result = EXPAND_ERROR;
|
|
|
|
break;
|
2012-01-31 05:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2016-02-22 11:37:39 +00:00
|
|
|
std::sort(expanded.begin(), expanded.end(), completion_t::is_naturally_less_than);
|
2016-02-06 22:39:47 +00:00
|
|
|
out->insert(out->end(), expanded.begin(), expanded.end());
|
2016-04-29 03:10:27 +00:00
|
|
|
} else {
|
|
|
|
// Can't fully justify this check. I think it's that SKIP_WILDCARDS is used when completing
|
|
|
|
// to mean don't do file expansions, so if we're not doing file expansions, just drop this
|
|
|
|
// completion on the floor.
|
|
|
|
if (!(flags & EXPAND_FOR_COMPLETIONS)) {
|
2016-02-08 09:29:23 +00:00
|
|
|
append_completion(out, path_to_expand);
|
2016-02-05 09:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
expand_error_t expand_string(const wcstring &input, std::vector<completion_t> *out_completions,
|
|
|
|
expand_flags_t flags, parse_error_list_t *errors) {
|
|
|
|
// Early out. If we're not completing, and there's no magic in the input, we're done.
|
|
|
|
if (!(flags & EXPAND_FOR_COMPLETIONS) && expand_is_clean(input)) {
|
2016-02-05 09:22:46 +00:00
|
|
|
append_completion(out_completions, input);
|
|
|
|
return EXPAND_OK;
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
// Our expansion stages.
|
|
|
|
const expand_stage_t stages[] = {expand_stage_cmdsubst, expand_stage_variables,
|
|
|
|
expand_stage_brackets, expand_stage_home_and_pid,
|
|
|
|
expand_stage_wildcards};
|
|
|
|
|
|
|
|
// Load up our single initial completion.
|
2016-02-05 09:22:46 +00:00
|
|
|
std::vector<completion_t> completions, output_storage;
|
|
|
|
append_completion(&completions, input);
|
2016-04-29 03:10:27 +00:00
|
|
|
|
2016-02-05 09:22:46 +00:00
|
|
|
expand_error_t total_result = EXPAND_OK;
|
2016-04-29 03:10:27 +00:00
|
|
|
for (size_t stage_idx = 0;
|
|
|
|
total_result != EXPAND_ERROR && stage_idx < sizeof stages / sizeof *stages; stage_idx++) {
|
|
|
|
for (size_t i = 0; total_result != EXPAND_ERROR && i < completions.size(); i++) {
|
2016-02-05 09:22:46 +00:00
|
|
|
const wcstring &next = completions.at(i).completion;
|
|
|
|
expand_error_t this_result = stages[stage_idx](next, &output_storage, flags, errors);
|
2016-04-29 03:10:27 +00:00
|
|
|
// If this_result was no match, but total_result is that we have a match, then don't
|
|
|
|
// change it.
|
|
|
|
if (!(this_result == EXPAND_WILDCARD_NO_MATCH &&
|
|
|
|
total_result == EXPAND_WILDCARD_MATCH)) {
|
2016-02-05 09:22:46 +00:00
|
|
|
total_result = this_result;
|
2012-01-31 05:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
// Output becomes our next stage's input.
|
2016-02-05 09:22:46 +00:00
|
|
|
completions.swap(output_storage);
|
|
|
|
output_storage.clear();
|
2012-01-31 05:33:15 +00:00
|
|
|
}
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
if (total_result != EXPAND_ERROR) {
|
|
|
|
// Hack to un-expand tildes (see #647).
|
|
|
|
if (!(flags & EXPAND_SKIP_HOME_DIRECTORIES)) {
|
2016-03-06 05:50:05 +00:00
|
|
|
unexpand_tildes(input, &completions);
|
|
|
|
}
|
|
|
|
out_completions->insert(out_completions->end(), completions.begin(), completions.end());
|
2013-04-08 06:54:43 +00:00
|
|
|
}
|
2016-02-05 09:22:46 +00:00
|
|
|
return total_result;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool expand_one(wcstring &string, expand_flags_t flags, parse_error_list_t *errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
std::vector<completion_t> completions;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-22 18:21:13 +00:00
|
|
|
if (!(flags & EXPAND_FOR_COMPLETIONS) && expand_is_clean(string)) {
|
2012-01-30 10:45:55 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-22 18:21:13 +00:00
|
|
|
if (expand_string(string, &completions, flags | EXPAND_NO_DESCRIPTIONS, errors) &&
|
|
|
|
completions.size() == 1) {
|
|
|
|
string = completions.at(0).completion;
|
|
|
|
return true;
|
2012-01-30 10:23:58 +00:00
|
|
|
}
|
2016-10-22 18:21:13 +00:00
|
|
|
return false;
|
2012-01-30 10:23:58 +00:00
|
|
|
}
|
2013-01-04 21:09:01 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// https://github.com/fish-shell/fish-shell/issues/367
|
|
|
|
//
|
|
|
|
// With them the Seed of Wisdom did I sow,
|
|
|
|
// And with my own hand labour'd it to grow:
|
|
|
|
// And this was all the Harvest that I reap'd---
|
|
|
|
// "I came like Water, and like Wind I go."
|
2013-01-04 21:09:01 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
static std::string escape_single_quoted_hack_hack_hack_hack(const char *str) {
|
2013-01-04 21:09:01 +00:00
|
|
|
std::string result;
|
|
|
|
size_t len = strlen(str);
|
|
|
|
result.reserve(len + 2);
|
|
|
|
result.push_back('\'');
|
2016-04-29 03:10:27 +00:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2013-01-04 21:09:01 +00:00
|
|
|
char c = str[i];
|
2016-04-29 03:10:27 +00:00
|
|
|
// Escape backslashes and single quotes only.
|
|
|
|
if (c == '\\' || c == '\'') result.push_back('\\');
|
2013-01-04 21:09:01 +00:00
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
result.push_back('\'');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool fish_xdm_login_hack_hack_hack_hack(std::vector<std::string> *cmds, int argc,
|
|
|
|
const char *const *argv) {
|
2016-10-30 22:37:16 +00:00
|
|
|
if (!cmds || cmds->size() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-04 21:09:01 +00:00
|
|
|
bool result = false;
|
2016-10-30 22:37:16 +00:00
|
|
|
const std::string &cmd = cmds->at(0);
|
|
|
|
if (cmd == "exec \"${@}\"" || cmd == "exec \"$@\"") {
|
|
|
|
// We're going to construct a new command that starts with exec, and then has the
|
|
|
|
// remaining arguments escaped.
|
|
|
|
std::string new_cmd = "exec";
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
const char *arg = argv[i];
|
|
|
|
if (arg) {
|
|
|
|
new_cmd.push_back(' ');
|
|
|
|
new_cmd.append(escape_single_quoted_hack_hack_hack_hack(arg));
|
2013-01-04 21:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-30 22:37:16 +00:00
|
|
|
|
|
|
|
cmds->at(0) = new_cmd;
|
|
|
|
result = true;
|
2013-01-04 21:09:01 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
bool expand_abbreviation(const wcstring &src, wcstring *output) {
|
|
|
|
if (src.empty()) return false;
|
2013-07-17 07:38:04 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Get the abbreviations. Return false if we have none.
|
2017-03-17 03:59:04 +00:00
|
|
|
env_var_t abbrs = env_get_string(USER_ABBREVIATIONS_VARIABLE_NAME);
|
|
|
|
if (abbrs.missing_or_empty()) return false;
|
2013-07-17 07:38:04 +00:00
|
|
|
|
|
|
|
bool result = false;
|
2017-03-17 03:59:04 +00:00
|
|
|
std::vector<wcstring> abbrsv;
|
|
|
|
tokenize_variable_array(abbrs, abbrsv);
|
|
|
|
for (auto abbr : abbrsv) {
|
|
|
|
// Abbreviation is expected to be of the form 'foo=bar' or 'foo bar'. Parse out the first =
|
|
|
|
// or space. Silently skip on failure (no equals, or equals at the end or beginning). Try to
|
2016-04-29 03:10:27 +00:00
|
|
|
// avoid copying any strings until we are sure this is a match.
|
2017-03-17 03:59:04 +00:00
|
|
|
size_t equals_pos = abbr.find(L'=');
|
|
|
|
size_t space_pos = abbr.find(L' ');
|
2014-10-11 23:50:16 +00:00
|
|
|
size_t separator = mini(equals_pos, space_pos);
|
2017-03-17 03:59:04 +00:00
|
|
|
if (separator == wcstring::npos || separator == 0 || separator + 1 == abbr.size()) continue;
|
2013-07-17 07:38:04 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Find the character just past the end of the command. Walk backwards, skipping spaces.
|
2014-10-11 23:50:16 +00:00
|
|
|
size_t cmd_end = separator;
|
2017-03-17 03:59:04 +00:00
|
|
|
while (cmd_end > 0 && iswspace(abbr.at(cmd_end - 1))) cmd_end--;
|
2016-04-29 03:10:27 +00:00
|
|
|
|
|
|
|
// See if this command matches.
|
2017-03-17 03:59:04 +00:00
|
|
|
if (abbr.compare(0, cmd_end, src) == 0) {
|
2016-08-16 22:30:49 +00:00
|
|
|
// Success. Set output to everything past the end of the string.
|
2017-03-17 03:59:04 +00:00
|
|
|
if (output != NULL) output->assign(abbr, separator + 1, wcstring::npos);
|
2013-07-17 07:38:04 +00:00
|
|
|
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|