Merge pull request #4407 from mqudsi/read_stdout

Support reading to stdout via builtin `read` invoked without parameters.
This commit is contained in:
Mahmoud Al-Qudsi 2017-10-10 08:37:41 +02:00 committed by GitHub
commit 2d99623b17
7 changed files with 32 additions and 8 deletions

View file

@ -22,6 +22,7 @@ This section is for changes merged to the `major` branch that are not also merge
- `math` is now a builtin rather than a wrapper around `bc` (#3157). - `math` is now a builtin rather than a wrapper around `bc` (#3157).
- `history search` supports globs for wildcard searching (#3136). - `history search` supports globs for wildcard searching (#3136).
- `bind` has a new `--silent` option to ignore bind requests for named keys not available under the current `$TERMINAL` (#4188, #4431) - `bind` has a new `--silent` option to ignore bind requests for named keys not available under the current `$TERMINAL` (#4188, #4431)
- `read` writes directly to stdout if called without arguments (#4407)
## Other significant changes ## Other significant changes
- Command substitution output is now limited to 10 MB by default (#3822). - Command substitution output is now limited to 10 MB by default (#3822).

View file

@ -7,10 +7,7 @@ read [OPTIONS] VARIABLES...
\subsection read-description Description \subsection read-description Description
`read` reads from standard input and stores the result in one or more shell variables. By default, `read` reads from standard input and either writes the result back to the terminal for use in command substitution or stores the result in one or more shell variables. By default, one line (terminated by a newline) is read into each variable. Alternatively, a null character or a maximum number of characters can be used to terminate the input. Unlike other shells, there is no default variable (such as `REPLY`) for storing the result.
one line (terminated by a newline) is read into each variable. Alternatively, a null character or a
maximum number of characters can be used to terminate the input. Unlike other shells, there is no
default variable (such as `REPLY`) for storing the result.
The following options are available: The following options are available:
@ -48,6 +45,12 @@ The following options are available:
`read` reads a single line of input from stdin, breaks it into tokens based on the delimiter set via `-d`/`--delimiter` as a complete string (like `string split` or, if that has not been given the (deprecated) `IFS` shell variable as a set of characters, and then assigns one token to each variable specified in `VARIABLES`. If there are more tokens than variables, the complete remainder is assigned to the last variable. As a special case, if `IFS` is set to the empty string, each character of the input is considered a separate token. `read` reads a single line of input from stdin, breaks it into tokens based on the delimiter set via `-d`/`--delimiter` as a complete string (like `string split` or, if that has not been given the (deprecated) `IFS` shell variable as a set of characters, and then assigns one token to each variable specified in `VARIABLES`. If there are more tokens than variables, the complete remainder is assigned to the last variable. As a special case, if `IFS` is set to the empty string, each character of the input is considered a separate token.
If no parameters are provided, `read` enters a special case that simply provides redirection from `stdin` to `stdout`, useful for command substitution. For instance, the fish shell command below can be used to read data that should be provided via a command line argument from the console instead of hardcoding it in the command itself, allowing the command to both be reused as-is in various contexts with different input values and preventing possibly sensitive text from being included in the shell history:
`mysql -uuser -p(read)`
When running in stdout redirect mode, `read` does not split the input in any way and text is redirected to standard output without any further processing or manipulation.
If `-a` or `--array` is provided, only one variable name is allowed and the tokens are stored as an array in this variable. If `-a` or `--array` is provided, only one variable name is allowed and the tokens are stored as an array in this variable.
See the documentation for `set` for more details on the scoping rules for variables. See the documentation for `set` for more details on the scoping rules for variables.

View file

@ -47,6 +47,7 @@ struct read_cmd_opts_t {
bool array = false; bool array = false;
bool silent = false; bool silent = false;
bool split_null = false; bool split_null = false;
bool stdout = false;
int nchars = 0; int nchars = 0;
}; };
@ -72,6 +73,11 @@ static const struct woption long_options[] = {{L"export", no_argument, NULL, 'x'
static int parse_cmd_opts(read_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) static int parse_cmd_opts(read_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
if (argc == 1) {
opts.stdout = true;
return STATUS_CMD_OK;
}
wchar_t *cmd = argv[0]; wchar_t *cmd = argv[0];
int opt; int opt;
wgetopter_t w; wgetopter_t w;
@ -383,6 +389,11 @@ static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int arg
return STATUS_CMD_OK; return STATUS_CMD_OK;
} }
void write_stdout(wcstring val) {
const std::string &out = wcs2string(val);
write_loop(STDOUT_FILENO, out.c_str(), out.size());
}
/// The read builtin. Reads from stdin and stores the values in environment variables. /// 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) { int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wchar_t *cmd = argv[0]; wchar_t *cmd = argv[0];
@ -427,6 +438,11 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return exit_res; return exit_res;
} }
if (opts.stdout) {
write_stdout(buff);
return exit_res;
}
if (!opts.have_delimiter) { if (!opts.have_delimiter) {
auto ifs = env_get(L"IFS"); auto ifs = env_get(L"IFS");
if (!ifs.missing_or_empty()) opts.delimiter = ifs->as_string(); if (!ifs.missing_or_empty()) opts.delimiter = ifs->as_string();

View file

@ -1,7 +1,6 @@
#################### ####################
# Read with no vars is an error # Read with no vars is not an error
read: Expected at least 1 args, got only 0
#################### ####################
# Read with -a and anything other than exactly on var name is an error # Read with -a and anything other than exactly on var name is an error

View file

@ -16,6 +16,11 @@ expect_prompt
# read # read
send_line "read"
expect_read_prompt
send_line "text"
expect_prompt
send_line "read foo" send_line "read foo"
expect_read_prompt expect_read_prompt
send_line "text" send_line "text"

View file

@ -3,7 +3,7 @@
# Test read builtin and IFS. # Test read builtin and IFS.
# #
logmsg Read with no vars is an error logmsg Read with no vars is not an error
read read
logmsg Read with -a and anything other than exactly on var name is an error logmsg Read with -a and anything other than exactly on var name is an error

View file

@ -1,6 +1,6 @@
#################### ####################
# Read with no vars is an error # Read with no vars is not an error
#################### ####################
# Read with -a and anything other than exactly on var name is an error # Read with -a and anything other than exactly on var name is an error