2017-06-13 12:34:24 +08:00
|
|
|
// Implementation of the read builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include <errno.h>
|
2017-07-27 21:06:01 +08:00
|
|
|
#include <limits.h>
|
2017-06-13 12:34:24 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2017-08-28 01:40:53 +08:00
|
|
|
#include <stdlib.h>
|
2017-06-13 12:34:24 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-08-06 06:08:39 +08:00
|
|
|
#include <vector>
|
2017-06-13 12:34:24 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_read.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "highlight.h"
|
2017-07-01 12:03:05 +08:00
|
|
|
#include "history.h"
|
2017-06-13 12:34:24 +08:00
|
|
|
#include "io.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "wcstringutil.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
struct read_cmd_opts_t {
|
2017-06-13 12:34:24 +08:00
|
|
|
bool print_help = false;
|
|
|
|
int place = ENV_USER;
|
|
|
|
wcstring prompt_cmd;
|
|
|
|
const wchar_t *prompt = NULL;
|
|
|
|
const wchar_t *prompt_str = NULL;
|
|
|
|
const wchar_t *right_prompt = L"";
|
|
|
|
const wchar_t *commandline = L"";
|
2017-07-27 21:06:01 +08:00
|
|
|
// If a delimiter was given. Used to distinguish between the default
|
|
|
|
// empty string and a given empty delimiter.
|
|
|
|
bool have_delimiter = false;
|
|
|
|
wcstring delimiter;
|
2017-06-13 12:34:24 +08:00
|
|
|
bool shell = false;
|
|
|
|
bool array = false;
|
|
|
|
bool silent = false;
|
|
|
|
bool split_null = false;
|
|
|
|
int nchars = 0;
|
|
|
|
};
|
|
|
|
|
2017-07-27 21:06:01 +08:00
|
|
|
static const wchar_t *short_options = L":ac:ghilm:n:p:d:suxzP:UR:";
|
2017-06-13 12:34:24 +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"universal", no_argument, NULL, 'U'},
|
|
|
|
{L"unexport", no_argument, NULL, 'u'},
|
|
|
|
{L"prompt", required_argument, NULL, 'p'},
|
|
|
|
{L"prompt-str", required_argument, NULL, 'P'},
|
|
|
|
{L"right-prompt", required_argument, NULL, 'R'},
|
|
|
|
{L"command", required_argument, NULL, 'c'},
|
|
|
|
{L"mode-name", required_argument, NULL, 'm'},
|
|
|
|
{L"silent", no_argument, NULL, 'i'},
|
|
|
|
{L"nchars", required_argument, NULL, 'n'},
|
2017-07-27 21:06:01 +08:00
|
|
|
{L"delimiter", required_argument, NULL, 'd'},
|
2017-06-13 12:34:24 +08:00
|
|
|
{L"shell", no_argument, NULL, 's'},
|
|
|
|
{L"array", no_argument, NULL, 'a'},
|
|
|
|
{L"null", no_argument, NULL, 'z'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
static int parse_cmd_opts(read_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2017-06-14 04:03:32 +08:00
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-06-13 12:34:24 +08:00
|
|
|
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) {
|
|
|
|
case L'x': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.place |= ENV_EXPORT;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'g': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.place |= ENV_GLOBAL;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'l': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.place |= ENV_LOCAL;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'U': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.place |= ENV_UNIVERSAL;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'u': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.place |= ENV_UNEXPORT;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'p': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.prompt = w.woptarg;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'P': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.prompt_str = w.woptarg;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'R': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.right_prompt = w.woptarg;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'c': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.commandline = w.woptarg;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'm': {
|
2017-07-01 12:03:05 +08:00
|
|
|
streams.err.append_format(_(L"%ls: flags '--mode-name' / '-m' are now ignored. "
|
2017-09-24 14:07:45 +08:00
|
|
|
L"Set fish_history instead.\n"),
|
2017-07-01 12:03:05 +08:00
|
|
|
cmd);
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'n': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.nchars = fish_wcstoi(w.woptarg);
|
2017-06-13 12:34:24 +08:00
|
|
|
if (errno) {
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"), cmd,
|
|
|
|
w.woptarg);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), cmd,
|
|
|
|
w.woptarg);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-07-27 21:06:01 +08:00
|
|
|
case 'd': {
|
|
|
|
opts.have_delimiter = true;
|
|
|
|
opts.delimiter = w.woptarg;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-13 12:34:24 +08:00
|
|
|
case 's': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.shell = true;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.array = true;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'i': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.silent = true;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case L'z': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.split_null = true;
|
2017-06-13 12:34:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 13:12:29 +08:00
|
|
|
break;
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-13 12:34:24 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
case L'?': {
|
2017-06-15 03:26:05 +08:00
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-13 12:34:24 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read from the tty. This is only valid when the stream is stdin and it is attached to a tty and
|
|
|
|
/// we weren't asked to split on null characters.
|
|
|
|
static int read_interactive(wcstring &buff, int nchars, bool shell, bool silent,
|
2017-07-01 12:03:05 +08:00
|
|
|
const wchar_t *prompt, const wchar_t *right_prompt,
|
|
|
|
const wchar_t *commandline) {
|
2017-06-13 12:34:24 +08:00
|
|
|
int exit_res = STATUS_CMD_OK;
|
|
|
|
const wchar_t *line;
|
|
|
|
|
2017-07-02 04:23:24 +08:00
|
|
|
wcstring read_history_ID = history_session_id();
|
|
|
|
if (!read_history_ID.empty()) read_history_ID += L"_read";
|
2017-07-01 12:03:05 +08:00
|
|
|
reader_push(read_history_ID.c_str());
|
2017-07-02 04:23:24 +08:00
|
|
|
|
2017-06-13 12:34:24 +08:00
|
|
|
reader_set_left_prompt(prompt);
|
|
|
|
reader_set_right_prompt(right_prompt);
|
|
|
|
if (shell) {
|
|
|
|
reader_set_complete_function(&complete);
|
|
|
|
reader_set_highlight_function(&highlight_shell);
|
|
|
|
reader_set_test_function(&reader_shell_test);
|
|
|
|
}
|
|
|
|
// No autosuggestions or abbreviations in builtin_read.
|
|
|
|
reader_set_allow_autosuggesting(false);
|
|
|
|
reader_set_expand_abbreviations(false);
|
|
|
|
reader_set_exit_on_interrupt(true);
|
|
|
|
reader_set_silent_status(silent);
|
|
|
|
|
|
|
|
reader_set_buffer(commandline, wcslen(commandline));
|
|
|
|
proc_push_interactive(1);
|
|
|
|
|
|
|
|
event_fire_generic(L"fish_prompt");
|
|
|
|
line = reader_readline(nchars);
|
|
|
|
proc_pop_interactive();
|
|
|
|
if (line) {
|
|
|
|
if (0 < nchars && (size_t)nchars < wcslen(line)) {
|
|
|
|
// Line may be longer than nchars if a keybinding used `commandline -i`
|
|
|
|
// note: we're deliberately throwing away the tail of the commandline.
|
|
|
|
// It shouldn't be unread because it was produced with `commandline -i`,
|
|
|
|
// not typed.
|
|
|
|
buff = wcstring(line, nchars);
|
|
|
|
} else {
|
|
|
|
buff = wcstring(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exit_res = STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
reader_pop();
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bash uses 128 bytes for its chunk size. Very informal testing I did suggested that a smaller
|
|
|
|
/// chunk size performed better. However, we're going to use the bash value under the assumption
|
|
|
|
/// they've done more extensive testing.
|
|
|
|
#define READ_CHUNK_SIZE 128
|
|
|
|
|
|
|
|
/// Read from the fd in chunks until we see newline or null, as requested, is seen. This is only
|
|
|
|
/// used when the fd is seekable (so not from a tty or pipe) and we're not reading a specific number
|
|
|
|
/// of chars.
|
|
|
|
///
|
|
|
|
/// Returns an exit status.
|
|
|
|
static int read_in_chunks(int fd, wcstring &buff, bool split_null) {
|
|
|
|
int exit_res = STATUS_CMD_OK;
|
|
|
|
std::string str;
|
|
|
|
bool eof = false;
|
|
|
|
bool finished = false;
|
|
|
|
|
|
|
|
while (!finished) {
|
|
|
|
char inbuf[READ_CHUNK_SIZE];
|
|
|
|
long bytes_read = read_blocked(fd, inbuf, READ_CHUNK_SIZE);
|
|
|
|
|
|
|
|
if (bytes_read <= 0) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *end = std::find(inbuf, inbuf + bytes_read, split_null ? L'\0' : L'\n');
|
|
|
|
long bytes_consumed = end - inbuf; // must be signed for use in lseek
|
|
|
|
assert(bytes_consumed <= bytes_read);
|
|
|
|
str.append(inbuf, bytes_consumed);
|
|
|
|
if (bytes_consumed < bytes_read) {
|
|
|
|
// We found a splitter. The +1 because we need to treat the splitter as consumed, but
|
|
|
|
// not append it to the string.
|
|
|
|
CHECK(lseek(fd, bytes_consumed - bytes_read + 1, SEEK_CUR) != -1, STATUS_CMD_ERROR)
|
|
|
|
finished = true;
|
|
|
|
} else if (str.size() > read_byte_limit) {
|
|
|
|
exit_res = STATUS_READ_TOO_MUCH;
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buff = str2wcstring(str);
|
|
|
|
if (buff.empty() && eof) {
|
|
|
|
exit_res = STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read from the fd on char at a time until we've read the requested number of characters or a
|
|
|
|
/// newline or null, as appropriate, is seen. This is inefficient so should only be used when the
|
|
|
|
/// fd is not seekable.
|
|
|
|
static int read_one_char_at_a_time(int fd, wcstring &buff, int nchars, bool split_null) {
|
|
|
|
int exit_res = STATUS_CMD_OK;
|
|
|
|
bool eof = false;
|
|
|
|
size_t nbytes = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
bool finished = false;
|
|
|
|
wchar_t res = 0;
|
|
|
|
mbstate_t state = {};
|
|
|
|
|
|
|
|
while (!finished) {
|
|
|
|
char b;
|
|
|
|
if (read_blocked(fd, &b, 1) <= 0) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbytes++;
|
|
|
|
if (MB_CUR_MAX == 1) {
|
|
|
|
res = (unsigned char)b;
|
|
|
|
finished = true;
|
|
|
|
} else {
|
|
|
|
size_t sz = mbrtowc(&res, &b, 1, &state);
|
|
|
|
if (sz == (size_t)-1) {
|
|
|
|
memset(&state, 0, sizeof(state));
|
|
|
|
} else if (sz != (size_t)-2) {
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes > read_byte_limit) {
|
|
|
|
exit_res = STATUS_READ_TOO_MUCH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (eof) break;
|
|
|
|
if (!split_null && res == L'\n') break;
|
|
|
|
if (split_null && res == L'\0') break;
|
|
|
|
|
|
|
|
buff.push_back(res);
|
|
|
|
if (nchars > 0 && (size_t)nchars <= buff.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buff.empty() && eof) {
|
|
|
|
exit_res = STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
2017-07-20 11:22:34 +08:00
|
|
|
/// Validate the arguments given to `read` and provide defaults where needed.
|
|
|
|
static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int argc,
|
|
|
|
const wchar_t *const *argv, parser_t &parser, io_streams_t &streams) {
|
2017-06-13 12:34:24 +08:00
|
|
|
if (opts.prompt && opts.prompt_str) {
|
|
|
|
streams.err.append_format(_(L"%ls: You can't specify both -p and -P\n"), cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.prompt_str) {
|
|
|
|
opts.prompt_cmd = L"echo " + escape_string(opts.prompt_str, ESCAPE_ALL);
|
|
|
|
opts.prompt = opts.prompt_cmd.c_str();
|
|
|
|
} else if (!opts.prompt) {
|
|
|
|
opts.prompt = DEFAULT_READ_PROMPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opts.place & ENV_UNEXPORT) && (opts.place & ENV_EXPORT)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_EXPUNEXP, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opts.place & ENV_LOCAL ? 1 : 0) + (opts.place & ENV_GLOBAL ? 1 : 0) +
|
|
|
|
(opts.place & ENV_UNIVERSAL ? 1 : 0) >
|
|
|
|
1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_GLOCAL, cmd);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-07-20 11:22:34 +08:00
|
|
|
if (!opts.array && argc < 1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1, argc);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.array && argc != 1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, cmd, 1, argc);
|
2017-06-13 12:34:24 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify all variable names.
|
2017-07-20 11:22:34 +08:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2017-06-13 12:34:24 +08:00
|
|
|
if (!valid_var_name(argv[i])) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, argv[i]);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 11:22:34 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The read builtin. Reads from stdin and stores the values in environment variables.
|
|
|
|
int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
wcstring buff;
|
|
|
|
int exit_res = STATUS_CMD_OK;
|
|
|
|
read_cmd_opts_t opts;
|
|
|
|
|
|
|
|
int optind;
|
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = validate_read_args(cmd, opts, argc, argv, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
2017-06-13 12:34:24 +08:00
|
|
|
// TODO: Determine if the original set of conditions for interactive reads should be reinstated:
|
|
|
|
// if (isatty(0) && streams.stdin_fd == STDIN_FILENO && !split_null) {
|
|
|
|
int stream_stdin_is_a_tty = isatty(streams.stdin_fd);
|
|
|
|
if (stream_stdin_is_a_tty && !opts.split_null) {
|
|
|
|
// We should read interactively using reader_readline(). This does not support splitting on
|
|
|
|
// null.
|
2017-07-01 12:03:05 +08:00
|
|
|
exit_res = read_interactive(buff, opts.nchars, opts.shell, opts.silent, opts.prompt,
|
|
|
|
opts.right_prompt, opts.commandline);
|
2017-06-13 12:34:24 +08:00
|
|
|
} else if (!opts.nchars && !stream_stdin_is_a_tty &&
|
|
|
|
lseek(streams.stdin_fd, 0, SEEK_CUR) != -1) {
|
|
|
|
exit_res = read_in_chunks(streams.stdin_fd, buff, opts.split_null);
|
|
|
|
} else {
|
|
|
|
exit_res = read_one_char_at_a_time(streams.stdin_fd, buff, opts.nchars, opts.split_null);
|
|
|
|
}
|
|
|
|
|
2017-07-20 11:22:34 +08:00
|
|
|
if (exit_res != STATUS_CMD_OK) {
|
|
|
|
// Define the var(s) without any data. We do this because when this happens we want the user
|
|
|
|
// to be able to use the var but have it expand to nothing.
|
2017-08-06 06:08:39 +08:00
|
|
|
for (int i = 0; i < argc; i++) env_set_empty(argv[i], opts.place);
|
2017-06-13 12:34:24 +08:00
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:06:01 +08:00
|
|
|
if (!opts.have_delimiter) {
|
2017-08-28 15:25:41 +08:00
|
|
|
auto ifs = env_get(L"IFS");
|
|
|
|
if (!ifs.missing_or_empty()) opts.delimiter = ifs->as_string();
|
2017-07-27 21:06:01 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
|
2017-07-27 21:06:01 +08:00
|
|
|
if (opts.delimiter.empty()) {
|
2017-08-06 06:08:39 +08:00
|
|
|
// Every character is a separate token with one wrinkle involving non-array mode where the
|
|
|
|
// final var gets the remaining characters as a single string.
|
|
|
|
size_t x = std::max(static_cast<size_t>(1), buff.size());
|
|
|
|
size_t n_splits = (opts.array || static_cast<size_t>(argc) > x) ? x : argc;
|
|
|
|
wcstring_list_t chars;
|
|
|
|
chars.reserve(n_splits);
|
|
|
|
x = x - n_splits + 1;
|
|
|
|
int i = 0;
|
|
|
|
for (auto it = buff.begin(), end = buff.end(); it != end; ++i, ++it) {
|
|
|
|
if (opts.array || i < argc) {
|
|
|
|
chars.emplace_back(wcstring(1, *it));
|
2017-06-13 12:34:24 +08:00
|
|
|
} else {
|
2017-08-06 06:08:39 +08:00
|
|
|
if (x) {
|
|
|
|
chars.back().reserve(x);
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
chars.back().push_back(*it);
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.array) {
|
|
|
|
// Array mode: assign each char as a separate element of the sole var.
|
|
|
|
env_set(argv[0], opts.place, chars);
|
|
|
|
} else {
|
|
|
|
// Not array mode: assign each char to a separate var with the remainder being assigned
|
|
|
|
// to the last var.
|
2017-07-20 11:22:34 +08:00
|
|
|
int i = 0;
|
2017-06-13 12:34:24 +08:00
|
|
|
size_t j = 0;
|
2017-07-20 11:22:34 +08:00
|
|
|
for (; i + 1 < argc; ++i) {
|
2017-08-06 06:08:39 +08:00
|
|
|
if (j < chars.size()) {
|
|
|
|
env_set_one(argv[i], opts.place, chars[j]);
|
|
|
|
j++;
|
2017-06-13 12:34:24 +08:00
|
|
|
} else {
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(argv[i], opts.place, L"");
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
|
|
|
|
if (i < argc) {
|
|
|
|
wcstring val = chars.size() == static_cast<size_t>(argc) ? chars[i] : L"";
|
|
|
|
env_set_one(argv[i], opts.place, val);
|
|
|
|
} else {
|
|
|
|
env_set_empty(argv[i], opts.place);
|
|
|
|
}
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
|
|
|
} else if (opts.array) {
|
2017-08-06 06:08:39 +08:00
|
|
|
// The user has requested the input be split into a sequence of tokens and all the tokens
|
|
|
|
// assigned to a single var. How we do the tokenizing depends on whether the user specified
|
|
|
|
// the delimiter string or we're using IFS.
|
2017-07-27 21:06:01 +08:00
|
|
|
if (!opts.have_delimiter) {
|
2017-08-06 06:08:39 +08:00
|
|
|
// We're using IFS, so tokenize the buffer using each IFS char. This is for backward
|
|
|
|
// compatibility with old versions of fish.
|
|
|
|
wcstring_list_t tokens;
|
2017-07-27 21:06:01 +08:00
|
|
|
|
|
|
|
for (wcstring_range loc = wcstring_tok(buff, opts.delimiter);
|
|
|
|
loc.first != wcstring::npos; loc = wcstring_tok(buff, opts.delimiter, loc)) {
|
2017-08-06 06:08:39 +08:00
|
|
|
tokens.emplace_back(wcstring(buff, loc.first, loc.second));
|
2017-07-27 21:06:01 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set(argv[0], opts.place, tokens);
|
2017-07-27 21:06:01 +08:00
|
|
|
} else {
|
2017-08-06 06:08:39 +08:00
|
|
|
// We're using a delimiter provided by the user so use the `string split` behavior.
|
2017-07-27 21:06:01 +08:00
|
|
|
wcstring_list_t splits;
|
|
|
|
split_about(buff.begin(), buff.end(), opts.delimiter.begin(), opts.delimiter.end(),
|
|
|
|
&splits, LONG_MAX);
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set(argv[0], opts.place, splits);
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
} else {
|
|
|
|
// Not array mode. Split the input into tokens and assign each to the vars in sequence.
|
2017-07-27 21:06:01 +08:00
|
|
|
if (!opts.have_delimiter) {
|
2017-08-06 06:08:39 +08:00
|
|
|
// We're using IFS, so tokenize the buffer using each IFS char. This is for backward
|
|
|
|
// compatibility with old versions of fish.
|
2017-07-27 21:06:01 +08:00
|
|
|
wcstring_range loc = wcstring_range(0, 0);
|
|
|
|
for (int i = 0; i < argc; i++) {
|
2017-08-06 06:08:39 +08:00
|
|
|
wcstring substr;
|
2017-07-27 21:06:01 +08:00
|
|
|
loc = wcstring_tok(buff, (i + 1 < argc) ? opts.delimiter : wcstring(), loc);
|
2017-08-06 06:08:39 +08:00
|
|
|
if (loc.first != wcstring::npos) {
|
|
|
|
substr = wcstring(buff, loc.first, loc.second);
|
|
|
|
}
|
|
|
|
env_set_one(argv[i], opts.place, substr);
|
2017-07-27 21:06:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-06 06:08:39 +08:00
|
|
|
// We're using a delimiter provided by the user so use the `string split` behavior.
|
2017-07-27 21:06:01 +08:00
|
|
|
wcstring_list_t splits;
|
|
|
|
// We're making at most argc - 1 splits so the last variable
|
|
|
|
// is set to the remaining string.
|
|
|
|
split_about(buff.begin(), buff.end(), opts.delimiter.begin(), opts.delimiter.end(),
|
|
|
|
&splits, argc - 1);
|
|
|
|
for (size_t i = 0; i < (size_t)argc && i < splits.size(); i++) {
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(argv[i], opts.place, splits[i]);
|
2017-07-27 21:06:01 +08:00
|
|
|
}
|
2017-06-13 12:34:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|