/
builtin_read.cpp
505 lines (460 loc) · 18.1 KB
/
builtin_read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Implementation of the read builtin.
#include "config.h" // IWYU pragma: keep
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <xlocale.h>
#include <algorithm>
#include <memory>
#include <string>
#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"
#include "history.h"
#include "io.h"
#include "proc.h"
#include "reader.h"
#include "wcstringutil.h"
#include "wgetopt.h"
#include "wutil.h" // IWYU pragma: keep
struct read_cmd_opts_t {
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"";
// If a delimiter was given. Used to distinguish between the default
// empty string and a given empty delimiter.
bool have_delimiter = false;
wcstring delimiter;
bool shell = false;
bool array = false;
bool silent = false;
bool split_null = false;
int nchars = 0;
};
static const wchar_t *short_options = L":ac:ghilm:n:p:d:suxzP:UR:";
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'},
{L"delimiter", required_argument, NULL, 'd'},
{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}};
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) {
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': {
opts.place |= ENV_EXPORT;
break;
}
case L'g': {
opts.place |= ENV_GLOBAL;
break;
}
case L'l': {
opts.place |= ENV_LOCAL;
break;
}
case L'U': {
opts.place |= ENV_UNIVERSAL;
break;
}
case L'u': {
opts.place |= ENV_UNEXPORT;
break;
}
case L'p': {
opts.prompt = w.woptarg;
break;
}
case L'P': {
opts.prompt_str = w.woptarg;
break;
}
case L'R': {
opts.right_prompt = w.woptarg;
break;
}
case L'c': {
opts.commandline = w.woptarg;
break;
}
case L'm': {
streams.err.append_format(_(L"%ls: flags '--mode-name' / '-m' are now ignored. "
L"Set FISH_HISTORY instead.\n"),
cmd);
break;
}
case L'n': {
opts.nchars = fish_wcstoi(w.woptarg);
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;
}
case 'd': {
opts.have_delimiter = true;
opts.delimiter = w.woptarg;
break;
}
case 's': {
opts.shell = true;
break;
}
case 'a': {
opts.array = true;
break;
}
case L'i': {
opts.silent = true;
break;
}
case L'z': {
opts.split_null = true;
break;
}
case 'h': {
opts.print_help = true;
break;
}
case ':': {
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
case L'?': {
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;
}
/// 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,
const wchar_t *prompt, const wchar_t *right_prompt,
const wchar_t *commandline) {
int exit_res = STATUS_CMD_OK;
const wchar_t *line;
wcstring read_history_ID = history_session_id();
if (!read_history_ID.empty()) read_history_ID += L"_read";
reader_push(read_history_ID.c_str());
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;
}
/// 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) {
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;
}
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);
return STATUS_INVALID_ARGS;
}
// Verify all variable names.
for (int i = 0; i < argc; i++) {
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;
}
}
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;
// 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.
exit_res = read_interactive(buff, opts.nchars, opts.shell, opts.silent, opts.prompt,
opts.right_prompt, opts.commandline);
} 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);
}
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.
for (int i = 0; i < argc; i++) env_set(argv[i], NULL, opts.place);
return exit_res;
}
if (!opts.have_delimiter) {
env_var_t ifs = env_get(L"IFS");
if (!ifs.missing_or_empty()) {
opts.delimiter = ifs.as_string();
}
}
if (opts.delimiter.empty()) {
// Every character is a separate token.
size_t bufflen = buff.size();
if (opts.array) {
if (bufflen > 0) {
wcstring chars(bufflen + (bufflen - 1), ARRAY_SEP);
wcstring::iterator out = chars.begin();
for (wcstring::const_iterator it = buff.begin(), end = buff.end(); it != end;
++it) {
*out = *it;
out += 2;
}
env_set(argv[0], chars.c_str(), opts.place);
} else {
env_set(argv[0], NULL, opts.place);
}
} else { // not array mode
int i = 0;
size_t j = 0;
for (; i + 1 < argc; ++i) {
if (j < bufflen) {
wchar_t buffer[2] = {buff[j++], 0};
env_set(argv[i], buffer, opts.place);
} else {
env_set(argv[i], L"", opts.place);
}
}
if (i < argc) env_set(argv[i], &buff[j], opts.place);
}
} else if (opts.array) {
if (!opts.have_delimiter) {
// We're using IFS, so well split on every character in that,
// not on the entire thing at once.
wcstring tokens;
tokens.reserve(buff.size());
for (wcstring_range loc = wcstring_tok(buff, opts.delimiter);
loc.first != wcstring::npos; loc = wcstring_tok(buff, opts.delimiter, loc)) {
if (!tokens.empty()) tokens.push_back(ARRAY_SEP);
tokens.append(buff, loc.first, loc.second);
}
env_set(argv[0], tokens.empty() ? NULL : tokens.c_str(), opts.place);
} else {
wcstring_list_t splits;
split_about(buff.begin(), buff.end(), opts.delimiter.begin(), opts.delimiter.end(),
&splits, LONG_MAX);
auto val = list_to_array_val(splits);
env_set(argv[0], val->c_str(), opts.place);
}
} else { // not array
if (!opts.have_delimiter) {
wcstring_range loc = wcstring_range(0, 0);
for (int i = 0; i < argc; i++) {
loc = wcstring_tok(buff, (i + 1 < argc) ? opts.delimiter : wcstring(), loc);
env_set(argv[i], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first],
opts.place);
}
} else {
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++) {
env_set(argv[i], splits[i].c_str(), opts.place);
}
}
}
return exit_res;
}