2016-04-20 10:49:15 +08:00
|
|
|
// Functions used for implementing the set builtin.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <errno.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
2016-11-15 13:31:51 +08:00
|
|
|
#include <string.h>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <sys/stat.h>
|
2016-11-15 13:31:51 +08:00
|
|
|
#include <unistd.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
|
2012-01-10 03:49:37 +08:00
|
|
|
#include <algorithm>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <iterator>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "builtin.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "common.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "env.h"
|
|
|
|
#include "expand.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"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "proc.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2016-04-21 14:00:54 +08:00
|
|
|
|
|
|
|
class parser_t;
|
2006-07-20 06:55:49 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Error message for invalid path operations.
|
2016-10-15 11:26:23 +08:00
|
|
|
#define BUILTIN_SET_PATH_ERROR L"%ls: Warning: $%ls entry \"%ls\" is not valid (%s)\n"
|
2006-05-19 16:58:13 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Hint for invalid path operation with a colon.
|
2006-04-21 02:29:44 +08:00
|
|
|
#define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n"
|
2006-05-19 16:58:13 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Error for mismatch between index count and elements.
|
|
|
|
#define BUILTIN_SET_ARG_COUNT \
|
|
|
|
L"%ls: The number of variable indexes does not match the number of values\n"
|
|
|
|
|
|
|
|
// Test if the specified variable should be subject to path validation.
|
2017-04-05 12:28:57 +08:00
|
|
|
static const wcstring_list_t path_variables({L"PATH", L"CDPATH"});
|
|
|
|
static int is_path_variable(const wchar_t *env) { return contains(path_variables, env); }
|
2006-04-21 02:29:44 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Call env_set. If this is a path variable, e.g. PATH, validate the elements. On error, print a
|
|
|
|
/// description of the problem to stderr.
|
2017-07-09 04:14:30 +08:00
|
|
|
static int my_env_set(const wchar_t *key, const wcstring_list_t &list, int scope,
|
2016-04-20 10:49:15 +08:00
|
|
|
io_streams_t &streams) {
|
|
|
|
if (is_path_variable(key)) {
|
|
|
|
// Fix for https://github.com/fish-shell/fish-shell/issues/199 . Return success if any path
|
|
|
|
// setting succeeds.
|
2013-01-30 18:22:38 +08:00
|
|
|
bool any_success = false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Don't bother validating (or complaining about) values that are already present. When
|
|
|
|
// determining already-present values, use ENV_DEFAULT instead of the passed-in scope
|
|
|
|
// because in:
|
|
|
|
//
|
|
|
|
// set -l PATH stuff $PATH
|
|
|
|
//
|
|
|
|
// where we are temporarily shadowing a variable, we want to compare against the shadowed
|
|
|
|
// value, not the (missing) local value. Also don't bother to complain about relative paths,
|
|
|
|
// which don't start with /.
|
2013-02-20 09:48:51 +08:00
|
|
|
wcstring_list_t existing_values;
|
2016-04-08 06:44:56 +08:00
|
|
|
const env_var_t existing_variable = env_get_string(key, ENV_DEFAULT);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!existing_variable.missing_or_empty())
|
2013-02-20 09:48:51 +08:00
|
|
|
tokenize_variable_array(existing_variable, existing_values);
|
|
|
|
|
2017-07-09 04:14:30 +08:00
|
|
|
for (size_t i = 0; i < list.size(); i++) {
|
|
|
|
const wcstring &dir = list.at(i);
|
2017-04-05 12:28:57 +08:00
|
|
|
if (!string_prefixes_string(L"/", dir) || contains(existing_values, dir)) {
|
2013-02-20 09:48:51 +08:00
|
|
|
any_success = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-08 14:04:02 +08:00
|
|
|
int show_hint = 0;
|
|
|
|
bool error = false;
|
|
|
|
struct stat buff;
|
2016-10-15 11:26:23 +08:00
|
|
|
if (wstat(dir, &buff) == -1) {
|
2012-07-08 14:04:02 +08:00
|
|
|
error = true;
|
2016-11-03 12:54:57 +08:00
|
|
|
} else if (!S_ISDIR(buff.st_mode)) {
|
2016-10-15 11:26:23 +08:00
|
|
|
error = true;
|
|
|
|
errno = ENOTDIR;
|
2016-11-03 12:54:57 +08:00
|
|
|
} else if (waccess(dir, X_OK) == -1) {
|
2012-07-08 14:04:02 +08:00
|
|
|
error = true;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!error) {
|
2012-07-08 14:04:02 +08:00
|
|
|
any_success = true;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2016-10-15 11:26:23 +08:00
|
|
|
streams.err.append_format(_(BUILTIN_SET_PATH_ERROR), L"set", key, dir.c_str(),
|
2016-11-03 12:54:57 +08:00
|
|
|
strerror(errno));
|
2013-02-20 09:48:51 +08:00
|
|
|
const wchar_t *colon = wcschr(dir.c_str(), L':');
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (colon && *(colon + 1)) {
|
2012-07-08 14:04:02 +08:00
|
|
|
show_hint = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (show_hint) {
|
|
|
|
streams.err.append_format(_(BUILTIN_SET_PATH_HINT), L"set", key, key,
|
|
|
|
wcschr(dir.c_str(), L':') + 1);
|
2012-07-08 14:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Fail at setting the path if we tried to set it to something non-empty, but it wound up
|
|
|
|
// empty.
|
2017-07-09 04:14:30 +08:00
|
|
|
if (!list.empty() && !any_success) {
|
|
|
|
return STATUS_CMD_ERROR;
|
2012-07-08 14:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-09 04:14:30 +08:00
|
|
|
// We don't check `val->empty()` because an array var with a single empty string will be
|
|
|
|
// "empty". A truly empty array var is set to the special value `ENV_NULL`.
|
|
|
|
auto val = list_to_array_val(list);
|
|
|
|
int retcode = env_set(key, *val == ENV_NULL ? NULL : val->c_str(), scope | ENV_USER);
|
|
|
|
switch (retcode) {
|
2016-10-30 08:25:48 +08:00
|
|
|
case ENV_OK: {
|
2017-07-09 04:14:30 +08:00
|
|
|
retcode = STATUS_CMD_OK;
|
2016-10-30 08:25:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case ENV_PERM: {
|
|
|
|
streams.err.append_format(_(L"%ls: Tried to change the read-only variable '%ls'\n"),
|
|
|
|
L"set", key);
|
2017-07-09 04:14:30 +08:00
|
|
|
retcode = STATUS_CMD_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case ENV_SCOPE: {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Tried to set the special variable '%ls' with the wrong scope\n"), L"set",
|
|
|
|
key);
|
2017-07-09 04:14:30 +08:00
|
|
|
retcode = STATUS_CMD_ERROR;
|
2014-07-13 05:05:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case ENV_INVALID: {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Tried to set the special variable '%ls' to an invalid value\n"), L"set",
|
|
|
|
key);
|
2017-07-09 04:14:30 +08:00
|
|
|
retcode = STATUS_CMD_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected env_set() ret val");
|
|
|
|
break;
|
|
|
|
}
|
2012-07-08 14:04:02 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-07-08 14:04:02 +08:00
|
|
|
return retcode;
|
2012-01-10 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Extract indexes from a destination argument of the form name[index1 index2...]
|
|
|
|
///
|
|
|
|
/// \param indexes the list to insert the new indexes into
|
|
|
|
/// \param src the source string to parse
|
|
|
|
/// \param name the name of the element. Return null if the name in \c src does not match this name
|
|
|
|
/// \param var_count the number of elements in the array to parse.
|
|
|
|
///
|
|
|
|
/// \return the total number of indexes parsed, or -1 on error
|
|
|
|
static int parse_index(std::vector<long> &indexes, const wchar_t *src, const wchar_t *name,
|
|
|
|
size_t var_count, io_streams_t &streams) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t len;
|
|
|
|
int count = 0;
|
|
|
|
const wchar_t *src_orig = src;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (src == 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-21 09:53:31 +08:00
|
|
|
while (*src != L'\0' && (iswalnum(*src) || *src == L'_')) src++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (*src != L'[') {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(BUILTIN_SET_ARG_COUNT), L"set");
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
len = src - src_orig;
|
|
|
|
if ((wcsncmp(src_orig, name, len) != 0) || (wcslen(name) != (len))) {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"), L"set",
|
|
|
|
name, len, src_orig);
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
src++;
|
2016-10-21 09:53:31 +08:00
|
|
|
while (iswspace(*src)) src++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
while (*src != L']') {
|
2016-11-23 12:24:03 +08:00
|
|
|
const wchar_t *end;
|
|
|
|
long l_ind = fish_wcstol(src, &end);
|
|
|
|
if (errno > 0) { // ignore errno == -1 meaning the int did not end with a '\0'
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-21 09:53:31 +08:00
|
|
|
if (l_ind < 0) l_ind = var_count + l_ind + 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-21 09:53:31 +08:00
|
|
|
src = end; //!OCLINT(parameter reassignment)
|
2016-04-20 10:49:15 +08:00
|
|
|
if (*src == L'.' && *(src + 1) == L'.') {
|
|
|
|
src += 2;
|
2016-11-23 12:24:03 +08:00
|
|
|
long l_ind2 = fish_wcstol(src, &end);
|
|
|
|
if (errno > 0) { // ignore errno == -1 meaning the int did not end with a '\0'
|
2012-11-19 08:30:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-10-21 09:53:31 +08:00
|
|
|
src = end; //!OCLINT(parameter reassignment)
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (l_ind2 < 0) {
|
|
|
|
l_ind2 = var_count + l_ind2 + 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
int direction = l_ind2 < l_ind ? -1 : 1;
|
|
|
|
for (long jjj = l_ind; jjj * direction <= l_ind2 * direction; jjj += direction) {
|
2012-11-19 08:30:30 +08:00
|
|
|
// debug(0, L"Expand range [set]: %i\n", jjj);
|
|
|
|
indexes.push_back(jjj);
|
|
|
|
count++;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
indexes.push_back(l_ind);
|
|
|
|
count++;
|
|
|
|
}
|
2016-10-21 09:53:31 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
while (iswspace(*src)) src++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return count;
|
2012-01-10 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int update_values(wcstring_list_t &list, std::vector<long> &indexes,
|
|
|
|
wcstring_list_t &values) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t i;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Replace values where needed.
|
|
|
|
for (i = 0; i < indexes.size(); i++) {
|
|
|
|
// The '- 1' below is because the indices in fish are one-based, but the vector uses
|
|
|
|
// zero-based indices.
|
2012-11-19 08:30:30 +08:00
|
|
|
long ind = indexes[i] - 1;
|
2016-04-20 10:49:15 +08:00
|
|
|
const wcstring newv = values[i];
|
|
|
|
if (ind < 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((size_t)ind >= list.size()) {
|
|
|
|
list.resize(ind + 1);
|
2012-06-22 01:24:49 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// free((void *) al_get(list, ind));
|
|
|
|
list[ind] = newv;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
2012-01-10 03:49:37 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Erase from a list of wcstring values at specified indexes.
|
|
|
|
static void erase_values(wcstring_list_t &list, const std::vector<long> &indexes) {
|
2012-05-10 16:04:18 +08:00
|
|
|
// Make a set of indexes.
|
|
|
|
// This both sorts them into ascending order and removes duplicates.
|
|
|
|
const std::set<long> indexes_set(indexes.begin(), indexes.end());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Now walk the set backwards, so we encounter larger indexes first, and remove elements at the
|
|
|
|
// given (1-based) indexes.
|
2012-05-10 16:04:18 +08:00
|
|
|
std::set<long>::const_reverse_iterator iter;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (iter = indexes_set.rbegin(); iter != indexes_set.rend(); ++iter) {
|
2012-05-10 16:04:18 +08:00
|
|
|
long val = *iter;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (val > 0 && (size_t)val <= list.size()) {
|
2012-05-10 16:04:18 +08:00
|
|
|
// One-based indexing!
|
|
|
|
list.erase(list.begin() + val - 1);
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Print the names of all environment variables in the scope, with or without shortening, with or
|
|
|
|
/// without values, with or without escaping
|
|
|
|
static void print_variables(int include_values, int esc, bool shorten_ok, int scope,
|
|
|
|
io_streams_t &streams) {
|
2012-02-04 10:39:41 +08:00
|
|
|
wcstring_list_t names = env_get_names(scope);
|
|
|
|
sort(names.begin(), names.end());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring key = names.at(i);
|
2012-02-04 10:39:41 +08:00
|
|
|
const wcstring e_key = escape_string(key, 0);
|
2005-10-07 18:36:51 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(e_key);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (include_values) {
|
2014-07-13 05:40:46 +08:00
|
|
|
env_var_t value = env_get_string(key, scope);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!value.missing()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int shorten = 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (shorten_ok && value.length() > 64) {
|
2012-11-19 08:30:30 +08:00
|
|
|
shorten = 1;
|
|
|
|
value.resize(60);
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring e_value = esc ? expand_escape_variable(value) : value;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L" ");
|
|
|
|
streams.out.append(e_value);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (shorten) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(ellipsis_char);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L"\n");
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:00:30 +08:00
|
|
|
/// The set builtin creates, updates, and erases (removes, deletes) variables.
|
2016-04-20 10:49:15 +08:00
|
|
|
int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2017-04-20 14:43:02 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Flags to set the work mode.
|
2012-11-19 08:30:30 +08:00
|
|
|
int local = 0, global = 0, exportv = 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
int erase = 0, list = 0, unexport = 0;
|
|
|
|
int universal = 0, query = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
bool shorten_ok = true;
|
2016-11-03 12:54:57 +08:00
|
|
|
bool preserve_failure_exit_status = true;
|
2013-02-01 07:57:08 +08:00
|
|
|
const int incoming_exit_status = proc_get_last_status();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Variables used for performing the actual work.
|
2017-06-10 04:29:16 +08:00
|
|
|
wchar_t *dest = NULL;
|
2017-05-05 12:35:41 +08:00
|
|
|
int retcode = STATUS_CMD_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
int scope;
|
2016-04-20 10:49:15 +08:00
|
|
|
int slice = 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-06-10 04:29:16 +08:00
|
|
|
// Variables used for parsing the argument list. This command is atypical in using the "+"
|
|
|
|
// (REQUIRE_ORDER) option for flag parsing. This is not typical of most fish commands. It means
|
|
|
|
// we stop scanning for flags when the first non-flag argument is seen.
|
2017-06-30 12:49:57 +08:00
|
|
|
static const wchar_t *short_options = L"+:LUeghlnqux";
|
2017-06-10 04:29:16 +08:00
|
|
|
static const struct woption long_options[] = {{L"export", no_argument, NULL, 'x'},
|
|
|
|
{L"global", no_argument, NULL, 'g'},
|
|
|
|
{L"local", no_argument, NULL, 'l'},
|
|
|
|
{L"erase", no_argument, NULL, 'e'},
|
|
|
|
{L"names", no_argument, NULL, 'n'},
|
|
|
|
{L"unexport", no_argument, NULL, 'u'},
|
|
|
|
{L"universal", no_argument, NULL, 'U'},
|
|
|
|
{L"long", no_argument, NULL, 'L'},
|
|
|
|
{L"query", no_argument, NULL, 'q'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-06-10 04:29:16 +08:00
|
|
|
// Parse options to obtain the requested operation and the modifiers.
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 'e': {
|
2012-11-19 16:31:03 +08:00
|
|
|
erase = 1;
|
2016-11-03 12:54:57 +08:00
|
|
|
preserve_failure_exit_status = false;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2012-11-19 16:31:03 +08:00
|
|
|
list = 1;
|
2016-11-03 12:54:57 +08:00
|
|
|
preserve_failure_exit_status = false;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'x': {
|
2012-11-19 16:31:03 +08:00
|
|
|
exportv = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2012-11-19 16:31:03 +08:00
|
|
|
local = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'g': {
|
2012-11-19 16:31:03 +08:00
|
|
|
global = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'u': {
|
2012-11-19 16:31:03 +08:00
|
|
|
unexport = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'U': {
|
2012-11-19 16:31:03 +08:00
|
|
|
universal = 1;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'L': {
|
2012-11-19 16:31:03 +08:00
|
|
|
shorten_ok = false;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2012-11-19 16:31:03 +08:00
|
|
|
query = 1;
|
2016-11-03 12:54:57 +08:00
|
|
|
preserve_failure_exit_status = false;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2017-04-20 14:43:02 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_CMD_OK;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2017-06-30 12:49:57 +08:00
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-30 12:49:57 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
case '?': {
|
2017-04-20 14:43:02 +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-05-04 07:23:30 +08:00
|
|
|
}
|
2017-06-10 04:29:16 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Ok, all arguments have been parsed, let's validate them. If we are checking the existance of
|
|
|
|
// a variable (-q) we can not also specify scope.
|
|
|
|
if (query && (erase || list)) {
|
2017-04-20 14:43:02 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// We can't both list and erase variables.
|
|
|
|
if (erase && list) {
|
2017-04-20 14:43:02 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Variables can only have one scope.
|
|
|
|
if (local + global + universal > 1) {
|
2017-04-20 14:43:02 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_GLOCAL, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Variables can only have one export status.
|
|
|
|
if (exportv && unexport) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_EXPUNEXP, argv[0]);
|
2017-04-20 14:43:02 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Calculate the scope value for variable assignement.
|
|
|
|
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) |
|
|
|
|
(unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL : 0) | ENV_USER;
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
// Query mode. Return the number of variables that do not exist out of the specified
|
|
|
|
// variables.
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (i = w.woptind; i < argc; i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t *arg = argv[i];
|
2016-04-20 10:49:15 +08:00
|
|
|
int slice = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-02-14 12:37:27 +08:00
|
|
|
dest = wcsdup(arg);
|
|
|
|
assert(dest);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (wcschr(dest, L'[')) {
|
2012-11-19 08:30:30 +08:00
|
|
|
slice = 1;
|
2016-04-20 10:49:15 +08:00
|
|
|
*wcschr(dest, L'[') = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (slice) {
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<long> indexes;
|
|
|
|
wcstring_list_t result;
|
|
|
|
size_t j;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-07-13 05:40:46 +08:00
|
|
|
env_var_t dest_str = env_get_string(dest, scope);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!dest_str.missing()) tokenize_variable_array(dest_str, result);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!parse_index(indexes, arg, dest, result.size(), streams)) {
|
2017-04-20 14:43:02 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
retcode = 1;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
for (j = 0; j < indexes.size(); j++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
long idx = indexes[j];
|
2016-04-20 10:49:15 +08:00
|
|
|
if (idx < 1 || (size_t)idx > result.size()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
retcode++;
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
|
|
|
if (!env_exist(arg, scope)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
retcode++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dest);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (list) {
|
|
|
|
// Maybe we should issue an error if there are any other arguments?
|
2015-09-22 02:24:49 +08:00
|
|
|
print_variables(0, 0, shorten_ok, scope, streams);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_CMD_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (w.woptind == argc) {
|
|
|
|
// Print values of variables.
|
|
|
|
if (erase) {
|
2017-04-20 14:43:02 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Erase needs a variable name\n"), cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
retcode = STATUS_INVALID_ARGS;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-22 02:24:49 +08:00
|
|
|
print_variables(1, 1, shorten_ok, scope, streams);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return retcode;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 12:37:27 +08:00
|
|
|
dest = wcsdup(argv[w.woptind]);
|
|
|
|
assert(dest);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (wcschr(dest, L'[')) {
|
2012-11-19 08:30:30 +08:00
|
|
|
slice = 1;
|
2016-04-20 10:49:15 +08:00
|
|
|
*wcschr(dest, L'[') = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-04-20 14:43:02 +08:00
|
|
|
if (!valid_var_name(dest)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, dest);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Set assignment can work in two modes, either using slices or using the whole array. We detect
|
|
|
|
// which mode is used here.
|
|
|
|
if (slice) {
|
|
|
|
// Slice mode.
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<long> indexes;
|
|
|
|
wcstring_list_t result;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-07-13 05:40:46 +08:00
|
|
|
const env_var_t dest_str = env_get_string(dest, scope);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!dest_str.missing()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
tokenize_variable_array(dest_str, result);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (erase) {
|
2014-07-13 06:43:32 +08:00
|
|
|
retcode = 1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!retcode) {
|
|
|
|
for (; w.woptind < argc; w.woptind++) {
|
|
|
|
if (!parse_index(indexes, argv[w.woptind], dest, result.size(), streams)) {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2014-07-13 06:43:32 +08:00
|
|
|
retcode = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-07-13 06:43:32 +08:00
|
|
|
|
|
|
|
size_t idx_count = indexes.size();
|
2016-04-20 10:49:15 +08:00
|
|
|
size_t val_count = argc - w.woptind - 1;
|
2014-07-13 06:43:32 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!erase) {
|
|
|
|
if (val_count < idx_count) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(BUILTIN_SET_ARG_COUNT), argv[0]);
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2016-04-20 10:49:15 +08:00
|
|
|
retcode = 1;
|
2014-07-13 06:43:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (val_count == idx_count) {
|
2015-07-26 09:16:00 +08:00
|
|
|
w.woptind++;
|
2014-07-13 06:43:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!retcode) {
|
|
|
|
// Slice indexes have been calculated, do the actual work.
|
|
|
|
if (erase) {
|
2012-11-19 08:30:30 +08:00
|
|
|
erase_values(result, indexes);
|
2015-09-22 02:24:49 +08:00
|
|
|
my_env_set(dest, result, scope, streams);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t value;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
while (w.woptind < argc) {
|
2015-07-26 09:16:00 +08:00
|
|
|
value.push_back(argv[w.woptind++]);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (update_values(result, indexes, value)) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(L"%ls: ", argv[0]);
|
|
|
|
streams.err.append_format(ARRAY_BOUNDS_ERR);
|
|
|
|
streams.err.push_back(L'\n');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
my_env_set(dest, result, scope, streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-07-26 09:16:00 +08:00
|
|
|
w.woptind++;
|
2016-04-20 10:49:15 +08:00
|
|
|
// No slicing.
|
|
|
|
if (erase) {
|
|
|
|
if (w.woptind != argc) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Values cannot be specfied with erase\n"),
|
2016-04-20 10:49:15 +08:00
|
|
|
argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2016-04-20 10:49:15 +08:00
|
|
|
retcode = 1;
|
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
retcode = env_remove(dest, scope);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2012-02-08 15:53:34 +08:00
|
|
|
wcstring_list_t val;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (int i = w.woptind; i < argc; i++) val.push_back(argv[i]);
|
2015-09-22 02:24:49 +08:00
|
|
|
retcode = my_env_set(dest, val, scope, streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Check if we are setting variables above the effective scope. See
|
|
|
|
// https://github.com/fish-shell/fish-shell/issues/806
|
2014-10-06 15:10:56 +08:00
|
|
|
env_var_t global_dest = env_get_string(dest, ENV_GLOBAL);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (universal && !global_dest.missing()) {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Warning: universal scope selected, but a global variable '%ls' exists.\n"),
|
|
|
|
L"set", dest);
|
2014-10-06 15:10:56 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free(dest);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-05-05 13:42:42 +08:00
|
|
|
if (retcode == STATUS_CMD_OK && preserve_failure_exit_status) retcode = incoming_exit_status;
|
2012-11-19 08:30:30 +08:00
|
|
|
return retcode;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|