Fix read to stdout output appearing first

echo banana (read)

will output whatever read reads _first_ because it uses a direct
write_loop().

This also removes some duplicate code.
This commit is contained in:
Fabian Homborg 2018-04-10 21:45:28 +02:00
parent 701259d372
commit ea49c14c62
3 changed files with 7 additions and 6 deletions

View file

@ -394,11 +394,6 @@ 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];
@ -446,7 +441,7 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
} }
if (opts.to_stdout) { if (opts.to_stdout) {
write_stdout(buff); streams.out.append(buff);
return exit_res; return exit_res;
} }

View file

@ -247,3 +247,7 @@ begin
echo a...b...c | read -l a b c echo a...b...c | read -l a b c
echo $a; echo $b; echo $c echo $a; echo $b; echo $c
end end
echo
# At one point, whatever was read was printed _before_ banana
echo banana (echo sausage | read)

View file

@ -130,3 +130,5 @@ Multi-char delimiters with IFS
a a
b b
..c ..c
banana sausage