Merge branch 'no_percent'

Drops the % notation for process expansion. The existing notation was a
mess and expanded jobs, process ids, and process names via dark magic.
With this change, % is no longer a special character and can be used
unescaped with impunity.

The variables %self and %last, referring to fish's own pid and the pid
of the last backgrounded job respectively, have been replaced with $pid
and $last_pid. These are read-only variables, protected against being
redefined by the user.

Author's note: I would have personally preferred $fish_pid instead of
$pid but since we debated changing $version to $fish_version and then
reverted that change (with much acrimony), it makes no sense to break
with that precedent here. Additionally, $fish_last_pid is quite wordy.

Closes #4230. Closes #1202.
This commit is contained in:
Mahmoud Al-Qudsi
2018-03-09 09:16:31 -06:00
21 changed files with 29 additions and 107 deletions

View File

@@ -1002,7 +1002,6 @@ static void escape_string_script(const wchar_t *orig_in, size_t in_len, wcstring
case L'|':
case L';':
case L'"':
case L'%':
case L'~': {
if (!no_tilde || c != L'~') {
need_escape = 1;
@@ -1316,12 +1315,6 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in
}
break;
}
case L'%': {
if (unescape_special && (input_position == 0)) {
to_append_or_none = PROCESS_EXPAND;
}
break;
}
case L'*': {
if (unescape_special) {
// In general, this is ANY_STRING. But as a hack, if the last appended char

View File

@@ -322,7 +322,7 @@ bool string_set_contains(const T &set, const wchar_t *val) {
/// Check if a variable may not be set using the set command.
static bool is_read_only(const wchar_t *val) {
const string_set_t env_read_only = {L"PWD", L"SHLVL", L"_", L"history", L"status", L"version"};
const string_set_t env_read_only = {L"PWD", L"SHLVL", L"_", L"history", L"status", L"version", L"pid"};
return string_set_contains(env_read_only, val);
}
@@ -969,6 +969,9 @@ void env_init(const struct config_paths_t *paths /* or NULL */) {
wcstring version = str2wcstring(get_fish_version());
env_set_one(L"version", ENV_GLOBAL, version);
// Set the $pid variable (%self replacement)
env_set_one(L"pid", ENV_GLOBAL, to_string<long>(getpid()));
// Set up SHLVL variable. Not we can't use env_get because SHLVL is read-only, and therefore was
// not inherited from the environment.
wcstring nshlvl_str = L"1";

View File

@@ -1149,6 +1149,7 @@ void exec_job(parser_t &parser, job_t *j) {
j->set_flag(JOB_CONSTRUCTED, true);
if (!j->get_flag(JOB_FOREGROUND)) {
proc_last_bg_pid = j->pgid;
env_set(L"last_pid", ENV_GLOBAL, { to_string(proc_last_bg_pid) });
}
if (!exec_error) {

View File

@@ -564,67 +564,6 @@ static void find_process(const wchar_t *proc, expand_flags_t flags,
}
}
/// 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."
const wchar_t some_chars[] = {INTERNAL_SEPARATOR, PROCESS_EXPAND, L'\0'};
if (instr_with_sep.find_first_of(some_chars) == wcstring::npos) {
// Nothing to do.
append_completion(out, instr_with_sep);
return true;
}
// expand_string calls us with internal separators in instr...sigh.
wcstring instr = instr_with_sep;
remove_internal_separator(&instr, false);
if (instr.empty() || instr.at(0) != PROCESS_EXPAND) {
// Not a process expansion.
append_completion(out, instr);
return true;
}
const wchar_t *const in = instr.c_str();
// We know we are a process expansion now.
assert(in[0] == PROCESS_EXPAND);
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);
}
} else {
if (wcscmp((in + 1), SELF_STR) == 0) {
append_completion(out, to_string<long>(getpid()));
return true;
}
if (wcscmp((in + 1), LAST_STR) == 0) {
if (proc_last_bg_pid > 0) {
append_completion(out, to_string<long>(proc_last_bg_pid));
}
return true;
}
}
// 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.
const size_t prev_count = out->size();
find_process(in + 1, flags, out);
if (prev_count == out->size() && !(flags & EXPAND_FOR_COMPLETIONS)) {
// We failed to find anything.
append_syntax_error(errors, 1, FAILED_EXPANSION_PROCESS_ERR_MSG,
escape_string(in + 1, ESCAPE_NO_QUOTED).c_str());
return false;
}
return true;
}
/// 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 [.
@@ -1340,7 +1279,7 @@ static expand_error_t expand_stage_brackets(const wcstring &input, std::vector<c
return expand_brackets(input, flags, out, errors);
}
static expand_error_t expand_stage_home_and_pid(const wcstring &input,
static expand_error_t expand_stage_home(const wcstring &input,
std::vector<completion_t> *out,
expand_flags_t flags, parse_error_list_t *errors) {
wcstring next = input;
@@ -1348,16 +1287,7 @@ static expand_error_t expand_stage_home_and_pid(const wcstring &input,
if (!(EXPAND_SKIP_HOME_DIRECTORIES & flags)) {
expand_home_directory(next);
}
if (flags & EXPAND_FOR_COMPLETIONS) {
if (!next.empty() && next.at(0) == PROCESS_EXPAND) {
expand_pid(next, flags, out, NULL);
return EXPAND_OK;
}
append_completion(out, next);
} else if (!expand_pid(next, flags, out, errors)) {
return EXPAND_ERROR;
}
append_completion(out, next);
return EXPAND_OK;
}
@@ -1463,7 +1393,7 @@ expand_error_t expand_string(const wcstring &input, std::vector<completion_t> *o
// Our expansion stages.
const expand_stage_t stages[] = {expand_stage_cmdsubst, expand_stage_variables,
expand_stage_brackets, expand_stage_home_and_pid,
expand_stage_brackets, expand_stage_home,
expand_stage_wildcards};
// Load up our single initial completion.

View File

@@ -60,8 +60,6 @@ class completion_t;
enum {
/// Character representing a home directory.
HOME_DIRECTORY = EXPAND_RESERVED_BASE,
/// Character representing process expansion.
PROCESS_EXPAND,
/// Character representing variable expansion.
VARIABLE_EXPAND,
/// Character representing variable expansion into a single element.

View File

@@ -4422,7 +4422,6 @@ static void test_illegal_command_exit_code() {
{L"echo -n", STATUS_CMD_OK}, {L"pwd", STATUS_CMD_OK},
{L")", STATUS_ILLEGAL_CMD}, {L") ", STATUS_ILLEGAL_CMD},
{L"*", STATUS_ILLEGAL_CMD}, {L"**", STATUS_ILLEGAL_CMD},
{L"%", STATUS_ILLEGAL_CMD}, {L"%test", STATUS_ILLEGAL_CMD},
{L"?", STATUS_ILLEGAL_CMD}, {L"abc?def", STATUS_ILLEGAL_CMD},
{L") ", STATUS_ILLEGAL_CMD}};

View File

@@ -120,7 +120,6 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
for (size_t i = 0; i < path_with_magic.size(); i++) {
wchar_t c = path_with_magic.at(i);
switch (c) {
case PROCESS_EXPAND:
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
@@ -535,8 +534,7 @@ static void color_argument_internal(const wcstring &buffstr,
} else {
// Not a backslash.
switch (c) {
case L'~':
case L'%': {
case L'~': {
if (in_pos == 0) {
colors[in_pos] = highlight_spec_operator;
}

View File

@@ -259,7 +259,7 @@ void parse_error_offset_source_start(parse_error_list_t *errors, size_t amt);
#define ERROR_NOT_STATUS _(L"$? is not the exit status. In fish, please use $status.")
/// Error issued on $$.
#define ERROR_NOT_PID _(L"$$ is not the pid. In fish, please use %%self.")
#define ERROR_NOT_PID _(L"$$ is not the pid. In fish, please use $pid.")
/// Error issued on $#.
#define ERROR_NOT_ARGV_COUNT _(L"$# is not supported. In fish, please use 'count $argv'.")