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>
|
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
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
struct set_cmd_opts_t {
|
|
|
|
bool print_help = false;
|
2017-08-02 11:58:40 +08:00
|
|
|
bool show = false;
|
2017-07-23 12:15:20 +08:00
|
|
|
bool local = false;
|
|
|
|
bool global = false;
|
|
|
|
bool exportv = false;
|
|
|
|
bool erase = false;
|
|
|
|
bool list = false;
|
|
|
|
bool unexport = false;
|
|
|
|
bool universal = false;
|
|
|
|
bool query = false;
|
|
|
|
bool shorten_ok = true;
|
2017-08-04 14:57:36 +08:00
|
|
|
bool append = false;
|
|
|
|
bool prepend = false;
|
2017-07-23 12:15:20 +08:00
|
|
|
bool preserve_failure_exit_status = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-08-04 14:57:36 +08:00
|
|
|
static const wchar_t *short_options = L"+:LSUaeghlnpqux";
|
2017-08-02 11:58:40 +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"show", no_argument, NULL, 'S'},
|
2017-08-04 14:57:36 +08:00
|
|
|
{L"append", no_argument, NULL, 'a'}, {L"prepend", no_argument, NULL, 'p'},
|
2017-08-02 11:58:40 +08:00
|
|
|
{L"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}};
|
2017-07-23 12:15:20 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Hint for invalid path operation with a colon.
|
2018-01-09 15:02:47 +08:00
|
|
|
#define BUILTIN_SET_PATH_ERROR _(L"%ls: Warning: $%ls entry \"%ls\" is not valid (%s)\n")
|
2017-07-25 02:42:54 +08:00
|
|
|
#define BUILTIN_SET_PATH_HINT _(L"%ls: Did you mean 'set %ls $%ls %ls'?\n")
|
|
|
|
#define BUILTIN_SET_MISMATCHED_ARGS _(L"%ls: You provided %d indexes but %d values\n")
|
|
|
|
#define BUILTIN_SET_ERASE_NO_VAR _(L"%ls: Erase needs a variable name\n")
|
|
|
|
#define BUILTIN_SET_ARRAY_BOUNDS_ERR _(L"%ls: Array index out of bounds\n")
|
2017-08-07 11:22:31 +08:00
|
|
|
#define BUILTIN_SET_UVAR_ERR \
|
2017-08-10 03:30:33 +08:00
|
|
|
_(L"%ls: Universal variable '%ls' is shadowed by the global variable of the same name.\n")
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
static int parse_cmd_opts(set_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
2017-08-04 14:57:36 +08:00
|
|
|
case 'a': {
|
|
|
|
opts.append = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
case 'e': {
|
|
|
|
opts.erase = true;
|
|
|
|
opts.preserve_failure_exit_status = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-04 14:57:36 +08:00
|
|
|
case 'g': {
|
|
|
|
opts.global = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h': {
|
|
|
|
opts.print_help = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'l': {
|
|
|
|
opts.local = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
case 'n': {
|
|
|
|
opts.list = true;
|
|
|
|
opts.preserve_failure_exit_status = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-04 14:57:36 +08:00
|
|
|
case 'p': {
|
|
|
|
opts.prepend = true;
|
2017-07-23 12:15:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-08-04 14:57:36 +08:00
|
|
|
case 'q': {
|
|
|
|
opts.query = true;
|
|
|
|
opts.preserve_failure_exit_status = false;
|
2017-07-23 12:15:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-08-04 14:57:36 +08:00
|
|
|
case 'x': {
|
|
|
|
opts.exportv = true;
|
2017-07-23 12:15:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'u': {
|
|
|
|
opts.unexport = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'U': {
|
|
|
|
opts.universal = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'L': {
|
|
|
|
opts.shorten_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-02 11:58:40 +08:00
|
|
|
case 'S': {
|
|
|
|
opts.show = true;
|
|
|
|
opts.preserve_failure_exit_status = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:58:40 +08:00
|
|
|
static int validate_cmd_opts(const wchar_t *cmd, set_cmd_opts_t &opts, //!OCLINT(npath complexity)
|
|
|
|
int argc, parser_t &parser, io_streams_t &streams) {
|
2017-07-23 12:15:20 +08:00
|
|
|
// Can't query and erase or list.
|
|
|
|
if (opts.query && (opts.erase || opts.list)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't both list and erase variables.
|
|
|
|
if (opts.erase && opts.list) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables can only have one scope.
|
|
|
|
if (opts.local + opts.global + opts.universal > 1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_GLOCAL, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables can only have one export status.
|
|
|
|
if (opts.exportv && opts.unexport) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_EXPUNEXP, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-07-29 05:11:14 +08:00
|
|
|
// Trying to erase and (un)export at the same time doesn't make sense.
|
|
|
|
if (opts.erase && (opts.exportv || opts.unexport)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:58:40 +08:00
|
|
|
// The --show flag cannot be combined with any other flag.
|
|
|
|
if (opts.show &&
|
|
|
|
(opts.local || opts.global || opts.erase || opts.list || opts.exportv || opts.universal)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (argc == 0 && opts.erase) {
|
|
|
|
streams.err.append_format(BUILTIN_SET_ERASE_NO_VAR, cmd);
|
2017-07-23 12:15:20 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
// Check if we are setting a uvar and a global of the same name exists. See
|
|
|
|
// https://github.com/fish-shell/fish-shell/issues/806
|
|
|
|
static int check_global_scope_exists(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_t *dest,
|
|
|
|
io_streams_t &streams) {
|
|
|
|
if (opts.universal) {
|
2017-08-28 15:25:41 +08:00
|
|
|
auto global_dest = env_get(dest, ENV_GLOBAL);
|
|
|
|
if (global_dest && shell_is_interactive()) {
|
2017-08-07 11:22:31 +08:00
|
|
|
streams.err.append_format(BUILTIN_SET_UVAR_ERR, cmd, dest);
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-20 09:48:51 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-01-09 15:02:47 +08:00
|
|
|
// Validate the given path `list`. If there are any entries referring to invalid directories which
|
|
|
|
// contain a colon, then complain. Return true if any path element was valid, false if not.
|
|
|
|
static bool validate_path_warning_on_colons(const wchar_t *cmd,
|
|
|
|
const wchar_t *key, //!OCLINT(npath complexity)
|
|
|
|
const wcstring_list_t &list, io_streams_t &streams) {
|
|
|
|
// Always allow setting an empty value.
|
|
|
|
if (list.empty()) return true;
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
bool any_success = false;
|
|
|
|
|
|
|
|
// 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 /.
|
|
|
|
wcstring_list_t existing_values;
|
2017-08-28 15:25:41 +08:00
|
|
|
const auto existing_variable = env_get(key, ENV_DEFAULT);
|
|
|
|
if (!existing_variable.missing_or_empty()) existing_variable->to_list(existing_values);
|
2017-07-25 02:42:54 +08:00
|
|
|
|
2018-01-09 15:02:47 +08:00
|
|
|
for (const wcstring &dir : list) {
|
2017-07-25 02:42:54 +08:00
|
|
|
if (!string_prefixes_string(L"/", dir) || contains(existing_values, dir)) {
|
|
|
|
any_success = true;
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-01-09 15:02:47 +08:00
|
|
|
const wchar_t *colon = wcschr(dir.c_str(), L':');
|
|
|
|
bool looks_like_colon_sep = colon && colon[1];
|
|
|
|
if (!looks_like_colon_sep && any_success) {
|
|
|
|
// Once we have one valid entry, skip the remaining ones unless we might warn.
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-25 02:42:54 +08:00
|
|
|
struct stat buff;
|
2018-01-09 15:02:47 +08:00
|
|
|
bool valid = true;
|
2017-07-25 02:42:54 +08:00
|
|
|
if (wstat(dir, &buff) == -1) {
|
2018-01-09 15:02:47 +08:00
|
|
|
valid = false;
|
2017-07-25 02:42:54 +08:00
|
|
|
} else if (!S_ISDIR(buff.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
2018-01-09 15:02:47 +08:00
|
|
|
valid = false;
|
2017-07-25 02:42:54 +08:00
|
|
|
} else if (waccess(dir, X_OK) == -1) {
|
2018-01-09 15:02:47 +08:00
|
|
|
valid = false;
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
2018-01-09 15:02:47 +08:00
|
|
|
if (valid) {
|
2017-07-25 02:42:54 +08:00
|
|
|
any_success = true;
|
2018-01-09 15:02:47 +08:00
|
|
|
} else if (looks_like_colon_sep) {
|
2017-07-25 02:42:54 +08:00
|
|
|
streams.err.append_format(BUILTIN_SET_PATH_ERROR, cmd, key, dir.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
streams.err.append_format(BUILTIN_SET_PATH_HINT, cmd, key, key,
|
|
|
|
wcschr(dir.c_str(), L':') + 1);
|
2012-07-08 14:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 15:02:47 +08:00
|
|
|
return any_success;
|
2017-07-25 02:42:54 +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-08-20 06:45:46 +08:00
|
|
|
static int my_env_set(const wchar_t *cmd, const wchar_t *key, int scope, wcstring_list_t &list,
|
|
|
|
io_streams_t &streams) {
|
2018-01-09 15:02:47 +08:00
|
|
|
if (is_path_variable(key) && !validate_path_warning_on_colons(cmd, key, list, streams)) {
|
|
|
|
return STATUS_CMD_ERROR;
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-09 15:02:47 +08:00
|
|
|
int retval = env_set(key, scope | ENV_USER, list);
|
2017-07-25 02:42:54 +08:00
|
|
|
switch (retval) {
|
2016-10-30 08:25:48 +08:00
|
|
|
case ENV_OK: {
|
2017-07-25 02:42:54 +08:00
|
|
|
retval = 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"),
|
2017-07-25 02:42:54 +08:00
|
|
|
cmd, key);
|
|
|
|
retval = 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(
|
2017-07-25 02:42:54 +08:00
|
|
|
_(L"%ls: Tried to set the special variable '%ls' with the wrong scope\n"), cmd,
|
2016-04-20 10:49:15 +08:00
|
|
|
key);
|
2017-07-25 02:42:54 +08:00
|
|
|
retval = 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(
|
2017-07-25 02:42:54 +08:00
|
|
|
_(L"%ls: Tried to set the special variable '%ls' to an invalid value\n"), cmd, key);
|
|
|
|
retval = 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
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
return retval;
|
2012-01-10 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Extract indexes from an argument of the form `var_name[index1 index2...]`.
|
2016-04-20 10:49:15 +08:00
|
|
|
///
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Inputs:
|
|
|
|
/// indexes: the list to insert the new indexes into
|
|
|
|
/// src: The source string to parse. This must be a var spec of the form "var[...]"
|
2016-04-20 10:49:15 +08:00
|
|
|
///
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Returns:
|
|
|
|
/// The total number of indexes parsed, or -1 on error. If any indexes were found the `src` string
|
|
|
|
/// is modified to omit the index expression leaving just the var name.
|
|
|
|
static int parse_index(std::vector<long> &indexes, wchar_t *src, int scope, io_streams_t &streams) {
|
|
|
|
wchar_t *p = wcschr(src, L'[');
|
|
|
|
if (!p) return 0; // no slices so nothing for us to do
|
|
|
|
*p = L'\0'; // split the var name from the indexes/slices
|
|
|
|
p++;
|
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
auto var_str = env_get(src, scope);
|
2017-07-25 02:42:54 +08:00
|
|
|
wcstring_list_t var;
|
2017-08-28 15:25:41 +08:00
|
|
|
if (var_str) var_str->to_list(var);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
int count = 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
while (*p != L']') {
|
2016-11-23 12:24:03 +08:00
|
|
|
const wchar_t *end;
|
2017-07-25 02:42:54 +08:00
|
|
|
long l_ind = fish_wcstol(p, &end);
|
2016-11-23 12:24:03 +08:00
|
|
|
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);
|
2017-07-25 02:42:54 +08:00
|
|
|
return -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-07-25 02:42:54 +08:00
|
|
|
p = (wchar_t *)end;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
// Convert negative index to a positive index.
|
|
|
|
if (l_ind < 0) l_ind = var.size() + l_ind + 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (*p == L'.' && *(p + 1) == L'.') {
|
|
|
|
p += 2;
|
|
|
|
long l_ind2 = fish_wcstol(p, &end);
|
2016-11-23 12:24:03 +08:00
|
|
|
if (errno > 0) { // ignore errno == -1 meaning the int did not end with a '\0'
|
2017-07-25 02:42:54 +08:00
|
|
|
return -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-07-25 02:42:54 +08:00
|
|
|
p = (wchar_t *)end;
|
|
|
|
|
|
|
|
// Convert negative index to a positive index.
|
|
|
|
if (l_ind2 < 0) l_ind2 = var.size() + 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
|
|
|
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++;
|
|
|
|
}
|
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) {
|
|
|
|
// Replace values where needed.
|
2017-07-25 02:42:54 +08:00
|
|
|
for (size_t i = 0; i < indexes.size(); i++) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// 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
|
|
|
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.
|
2017-08-20 04:29:52 +08:00
|
|
|
decltype(indexes_set)::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
|
|
|
}
|
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
static int compute_scope(set_cmd_opts_t &opts) {
|
|
|
|
int scope = ENV_USER;
|
|
|
|
if (opts.local) scope |= ENV_LOCAL;
|
|
|
|
if (opts.global) scope |= ENV_GLOBAL;
|
|
|
|
if (opts.exportv) scope |= ENV_EXPORT;
|
|
|
|
if (opts.unexport) scope |= ENV_UNEXPORT;
|
|
|
|
if (opts.universal) scope |= ENV_UNIVERSAL;
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Print the names of all environment variables in the scope. It will include the values unless the
|
|
|
|
/// `set --list` flag was used.
|
2017-08-04 14:57:36 +08:00
|
|
|
static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
2017-07-25 02:42:54 +08:00
|
|
|
UNUSED(cmd);
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(parser);
|
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
bool names_only = opts.list;
|
|
|
|
wcstring_list_t names = env_get_names(compute_scope(opts));
|
2012-02-04 10:39:41 +08:00
|
|
|
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);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(e_key);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
if (!names_only) {
|
2017-08-28 15:25:41 +08:00
|
|
|
auto var = env_get(key, compute_scope(opts));
|
2017-08-10 02:11:58 +08:00
|
|
|
if (!var.missing_or_empty()) {
|
2017-07-23 12:15:20 +08:00
|
|
|
bool shorten = false;
|
2017-08-28 15:25:41 +08:00
|
|
|
wcstring val = expand_escape_variable(*var);
|
2017-08-06 09:22:49 +08:00
|
|
|
if (opts.shorten_ok && val.length() > 64) {
|
2017-07-23 12:15:20 +08:00
|
|
|
shorten = true;
|
2017-08-06 09:22:49 +08:00
|
|
|
val.resize(60);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L" ");
|
2017-08-10 02:11:58 +08:00
|
|
|
streams.out.append(val);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
if (shorten) 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
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
// Query mode. Return the number of variables that do not exist out of the specified variables.
|
2017-08-04 14:57:36 +08:00
|
|
|
static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
2017-07-23 12:15:20 +08:00
|
|
|
int retval = 0;
|
2017-07-25 02:42:54 +08:00
|
|
|
int scope = compute_scope(opts);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2017-07-23 12:15:20 +08:00
|
|
|
wchar_t *arg = argv[i];
|
|
|
|
wchar_t *dest = wcsdup(arg);
|
|
|
|
assert(dest);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
std::vector<long> indexes;
|
|
|
|
int idx_count = parse_index(indexes, dest, scope, streams);
|
|
|
|
if (idx_count == -1) {
|
|
|
|
free(dest);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_CMD_ERROR;
|
2017-07-23 12:15:20 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (idx_count) {
|
2017-07-23 12:15:20 +08:00
|
|
|
wcstring_list_t result;
|
2017-08-28 15:25:41 +08:00
|
|
|
auto dest_str = env_get(dest, scope);
|
|
|
|
if (dest_str) dest_str->to_list(result);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
for (auto idx : indexes) {
|
|
|
|
if (idx < 1 || (size_t)idx > result.size()) retval++;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
} else {
|
2017-08-28 15:25:41 +08:00
|
|
|
if (! env_get(arg, scope)) retval++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
|
|
|
|
free(dest);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:58:40 +08:00
|
|
|
static void show_scope(const wchar_t *var_name, int scope, io_streams_t &streams) {
|
|
|
|
const wchar_t *scope_name;
|
|
|
|
switch (scope) {
|
|
|
|
case ENV_LOCAL: {
|
|
|
|
scope_name = L"local";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ENV_GLOBAL: {
|
|
|
|
scope_name = L"global";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ENV_UNIVERSAL: {
|
|
|
|
scope_name = L"universal";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("invalid scope");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
const auto var = env_get(var_name, scope);
|
|
|
|
if (!var) {
|
2017-08-06 06:08:39 +08:00
|
|
|
streams.out.append_format(_(L"$%ls: not set in %ls scope\n"), var_name, scope_name);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-02 11:58:40 +08:00
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
|
|
|
|
const wchar_t *exportv = var->exportv ? _(L"exported") : _(L"unexported");
|
|
|
|
wcstring_list_t vals = var->as_list();
|
2017-08-06 06:08:39 +08:00
|
|
|
streams.out.append_format(_(L"$%ls: set in %ls scope, %ls, with %d elements\n"), var_name,
|
|
|
|
scope_name, exportv, vals.size());
|
|
|
|
for (size_t i = 0; i < vals.size(); i++) {
|
|
|
|
if (vals.size() > 100) {
|
|
|
|
if (i == 50) streams.out.append(L"...\n");
|
|
|
|
if (i >= 50 && i < vals.size() - 50) continue;
|
2017-08-02 11:58:40 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
const wcstring value = vals[i];
|
|
|
|
const wcstring escaped_val =
|
|
|
|
escape_string(value.c_str(), ESCAPE_NO_QUOTED, STRING_STYLE_SCRIPT);
|
|
|
|
streams.out.append_format(_(L"$%ls[%d]: length=%d value=|%ls|\n"), var_name, i + 1,
|
|
|
|
value.size(), escaped_val.c_str());
|
2017-08-02 11:58:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show mode. Show information about the named variable(s).
|
|
|
|
static int builtin_set_show(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
|
|
|
UNUSED(opts);
|
|
|
|
|
|
|
|
if (argc == 0) { // show all vars
|
|
|
|
wcstring_list_t names = env_get_names(ENV_USER);
|
|
|
|
sort(names.begin(), names.end());
|
|
|
|
for (auto it : names) {
|
|
|
|
show_scope(it.c_str(), ENV_LOCAL, streams);
|
|
|
|
show_scope(it.c_str(), ENV_GLOBAL, streams);
|
|
|
|
show_scope(it.c_str(), ENV_UNIVERSAL, streams);
|
|
|
|
streams.out.push_back(L'\n');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
wchar_t *arg = argv[i];
|
|
|
|
|
|
|
|
if (!valid_var_name(arg)) {
|
|
|
|
streams.err.append_format(_(L"$%ls: invalid var name\n"), arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wcschr(arg, L'[')) {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: `set --show` does not allow slices with the var names\n"), cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
show_scope(arg, ENV_LOCAL, streams);
|
|
|
|
show_scope(arg, ENV_GLOBAL, streams);
|
|
|
|
show_scope(arg, ENV_UNIVERSAL, streams);
|
|
|
|
streams.out.push_back(L'\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Erase a variable.
|
2017-08-04 14:57:36 +08:00
|
|
|
static int builtin_set_erase(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
2017-07-25 02:42:54 +08:00
|
|
|
if (argc != 1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, L"--erase", 1, argc);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int scope = compute_scope(opts); // calculate the variable scope based on the provided options
|
|
|
|
wchar_t *dest = argv[0];
|
|
|
|
|
|
|
|
std::vector<long> indexes;
|
|
|
|
int idx_count = parse_index(indexes, dest, scope, streams);
|
|
|
|
if (idx_count == -1) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
if (!valid_var_name(dest)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, dest);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx_count == 0) { // unset the var
|
|
|
|
retval = env_remove(dest, scope);
|
|
|
|
} else { // remove just the specified indexes of the var
|
2017-08-28 15:25:41 +08:00
|
|
|
const auto dest_var = env_get(dest, scope);
|
|
|
|
if (!dest_var) return STATUS_CMD_ERROR;
|
2017-07-25 02:42:54 +08:00
|
|
|
wcstring_list_t result;
|
2017-08-28 15:25:41 +08:00
|
|
|
dest_var->to_list(result);
|
2017-07-25 02:42:54 +08:00
|
|
|
erase_values(result, indexes);
|
2017-08-11 08:46:08 +08:00
|
|
|
retval = my_env_set(cmd, dest, scope, result, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2017-07-25 02:42:54 +08:00
|
|
|
return check_global_scope_exists(cmd, opts, dest, streams);
|
|
|
|
}
|
2017-07-23 12:15:20 +08:00
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
/// This handles the common case of setting the entire var to a set of values.
|
|
|
|
static int set_var_array(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_t *varname,
|
|
|
|
wcstring_list_t &new_values, int argc, wchar_t **argv, parser_t &parser,
|
|
|
|
io_streams_t &streams) {
|
|
|
|
UNUSED(cmd);
|
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
|
|
|
|
|
|
|
if (opts.prepend || opts.append) {
|
|
|
|
if (opts.prepend) {
|
|
|
|
for (int i = 0; i < argc; i++) new_values.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
auto var_str = env_get(varname, ENV_DEFAULT);
|
2017-08-04 14:57:36 +08:00
|
|
|
wcstring_list_t var_array;
|
2017-08-28 15:25:41 +08:00
|
|
|
if (var_str) var_str->to_list(var_array);
|
2017-08-04 14:57:36 +08:00
|
|
|
new_values.insert(new_values.end(), var_array.begin(), var_array.end());
|
|
|
|
|
|
|
|
if (opts.append) {
|
|
|
|
for (int i = 0; i < argc; i++) new_values.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < argc; i++) new_values.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// This handles the more difficult case of setting individual slices of a var.
|
2017-08-04 14:57:36 +08:00
|
|
|
static int set_var_slices(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_t *varname,
|
|
|
|
wcstring_list_t &new_values, std::vector<long> &indexes, int argc,
|
|
|
|
wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-07-25 02:42:54 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
if (opts.append || opts.prepend) {
|
|
|
|
streams.err.append_format(
|
|
|
|
L"%ls: Cannot use --append or --prepend when assigning to a slice", cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (indexes.size() != static_cast<size_t>(argc)) {
|
|
|
|
streams.err.append_format(BUILTIN_SET_MISMATCHED_ARGS, cmd, indexes.size(), argc);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
int scope = compute_scope(opts); // calculate the variable scope based on the provided options
|
2017-08-28 15:25:41 +08:00
|
|
|
const auto var_str = env_get(varname, scope);
|
|
|
|
if (var_str) var_str->to_list(new_values);
|
2017-07-25 02:42:54 +08:00
|
|
|
|
|
|
|
// Slice indexes have been calculated, do the actual work.
|
2017-08-04 14:57:36 +08:00
|
|
|
wcstring_list_t result;
|
|
|
|
for (int i = 0; i < argc; i++) result.push_back(argv[i]);
|
2017-07-25 02:42:54 +08:00
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
int retval = update_values(new_values, indexes, result);
|
2017-07-25 02:42:54 +08:00
|
|
|
if (retval != STATUS_CMD_OK) {
|
|
|
|
streams.err.append_format(BUILTIN_SET_ARRAY_BOUNDS_ERR, cmd);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
return STATUS_CMD_OK;
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// Set a variable.
|
2017-08-04 14:57:36 +08:00
|
|
|
static int builtin_set_set(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
2017-07-25 02:42:54 +08:00
|
|
|
if (argc == 0) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
int scope = compute_scope(opts); // calculate the variable scope based on the provided options
|
2017-08-04 14:57:36 +08:00
|
|
|
wchar_t *varname = argv[0];
|
2017-07-25 02:42:54 +08:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
std::vector<long> indexes;
|
2017-08-04 14:57:36 +08:00
|
|
|
int idx_count = parse_index(indexes, varname, scope, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
if (idx_count == -1) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
if (!valid_var_name(varname)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, varname);
|
2017-07-23 12:15:20 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-04 14:57:36 +08:00
|
|
|
int retval;
|
|
|
|
wcstring_list_t new_values;
|
|
|
|
if (idx_count == 0) {
|
|
|
|
// Handle the simple, common, case. Set the var to the specified values.
|
|
|
|
retval = set_var_array(cmd, opts, varname, new_values, argc, argv, parser, streams);
|
|
|
|
} else {
|
2017-07-25 02:42:54 +08:00
|
|
|
// Handle the uncommon case of setting specific slices of a var.
|
2017-08-04 14:57:36 +08:00
|
|
|
retval =
|
|
|
|
set_var_slices(cmd, opts, varname, new_values, indexes, argc, argv, parser, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
2017-08-04 14:57:36 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-08-11 08:46:08 +08:00
|
|
|
retval = my_env_set(cmd, varname, scope, new_values, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2017-08-04 14:57:36 +08:00
|
|
|
return check_global_scope_exists(cmd, opts, varname, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
}
|
2014-07-13 06:43:32 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
/// The set builtin creates, updates, and erases (removes, deletes) variables.
|
|
|
|
int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
const int incoming_exit_status = proc_get_last_status();
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
set_cmd_opts_t opts;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
int optind;
|
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
retval = validate_cmd_opts(cmd, opts, argc, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-07-25 02:42:54 +08:00
|
|
|
if (opts.query) {
|
|
|
|
retval = builtin_set_query(cmd, opts, argc, argv, parser, streams);
|
|
|
|
} else if (opts.erase) {
|
|
|
|
retval = builtin_set_erase(cmd, opts, argc, argv, parser, streams);
|
|
|
|
} else if (opts.list) { // explicit list the vars we know about
|
|
|
|
retval = builtin_set_list(cmd, opts, argc, argv, parser, streams);
|
2017-08-02 11:58:40 +08:00
|
|
|
} else if (opts.show) {
|
|
|
|
retval = builtin_set_show(cmd, opts, argc, argv, parser, streams);
|
2017-07-25 02:42:54 +08:00
|
|
|
} else if (argc == 0) { // implicit list the vars we know about
|
|
|
|
retval = builtin_set_list(cmd, opts, argc, argv, parser, streams);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2017-07-25 02:42:54 +08:00
|
|
|
retval = builtin_set_set(cmd, opts, argc, argv, parser, streams);
|
2014-10-06 15:10:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-23 12:15:20 +08:00
|
|
|
if (retval == STATUS_CMD_OK && opts.preserve_failure_exit_status) retval = incoming_exit_status;
|
|
|
|
return retval;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|