]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/read.def
Bash-4.4 patch 16
[thirdparty/bash.git] / builtins / read.def
CommitLineData
726f6388
JA
1This file is read.def, from which is created read.c.
2It implements the builtin "read" in Bash.
3
a0c0a00f 4Copyright (C) 1987-2015 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
726f6388 12
3185942a
JA
13Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$PRODUCES read.c
22
23$BUILTIN read
24$FUNCTION read_builtin
0001803f 25$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
3185942a
JA
26Read a line from the standard input and split it into fields.
27
28Reads a single line from the standard input, or from file descriptor FD
29if the -u option is supplied. The line is split into fields as with word
30splitting, and the first word is assigned to the first NAME, the second
31word to the second NAME, and so on, with any leftover words assigned to
32the last NAME. Only the characters found in $IFS are recognized as word
33delimiters.
34
35If no NAMEs are supplied, the line read is stored in the REPLY variable.
36
37Options:
38 -a array assign the words read to sequential indices of the array
39 variable ARRAY, starting at zero
40 -d delim continue until the first character of DELIM is read, rather
41 than newline
a0c0a00f
CR
42 -e use Readline to obtain the line in an interactive shell
43 -i text use TEXT as the initial text for Readline
3185942a 44 -n nchars return after reading NCHARS characters rather than waiting
a0c0a00f
CR
45 for a newline, but honor a delimiter if fewer than
46 NCHARS characters are read before the delimiter
0001803f 47 -N nchars return only after reading exactly NCHARS characters, unless
a0c0a00f
CR
48 EOF is encountered or read times out, ignoring any
49 delimiter
3185942a
JA
50 -p prompt output the string PROMPT without a trailing newline before
51 attempting to read
a0c0a00f
CR
52 -r do not allow backslashes to escape any characters
53 -s do not echo input coming from a terminal
54 -t timeout time out and return failure if a complete line of
55 input is not read within TIMEOUT seconds. The value of the
56 TMOUT variable is the default timeout. TIMEOUT may be a
57 fractional number. If TIMEOUT is 0, read returns
58 immediately, without trying to read any data, returning
59 success only if input is available on the specified
60 file descriptor. The exit status is greater than 128
61 if the timeout is exceeded
62 -u fd read from file descriptor FD instead of the standard input
3185942a
JA
63
64Exit Status:
ac50fbac
CR
65The return code is zero, unless end-of-file is encountered, read times out
66(in which case it's greater than 128), a variable assignment error occurs,
3185942a 67or an invalid file descriptor is supplied as the argument to -u.
726f6388
JA
68$END
69
ccc6cda3
JA
70#include <config.h>
71
bb70624e
JA
72#include "bashtypes.h"
73#include "posixstat.h"
74
726f6388 75#include <stdio.h>
ccc6cda3 76
3185942a
JA
77#include "bashansi.h"
78
ccc6cda3
JA
79#if defined (HAVE_UNISTD_H)
80# include <unistd.h>
81#endif
82
bb70624e 83#include <signal.h>
e8ce775d
JA
84#include <errno.h>
85
28ef6c31
JA
86#ifdef __CYGWIN__
87# include <fcntl.h>
88# include <io.h>
89#endif
90
b80f6443
JA
91#include "../bashintl.h"
92
726f6388
JA
93#include "../shell.h"
94#include "common.h"
ccc6cda3 95#include "bashgetopt.h"
726f6388 96
bb70624e
JA
97#include <shtty.h>
98
ccc6cda3
JA
99#if defined (READLINE)
100#include "../bashline.h"
101#include <readline/readline.h>
102#endif
726f6388 103
95732b49
JA
104#if defined (BUFFERED_INPUT)
105# include "input.h"
106#endif
107
ac50fbac
CR
108#include "shmbutil.h"
109
e8ce775d
JA
110#if !defined(errno)
111extern int errno;
112#endif
113
ac50fbac
CR
114extern void run_pending_traps __P((void));
115
116extern int posixly_correct;
117extern int trapped_signal_received;
118
3185942a
JA
119struct ttsave
120{
121 int fd;
122 TTYSTRUCT *attrs;
123};
124
ccc6cda3 125#if defined (READLINE)
0628567a 126static void reset_attempted_completion_function __P((char *));
3185942a
JA
127static int set_itext __P((void));
128static char *edit_line __P((char *, char *));
f73dda09
JA
129static void set_eol_delim __P((int));
130static void reset_eol_delim __P((char *));
ccc6cda3 131#endif
f73dda09 132static SHELL_VAR *bind_read_variable __P((char *, char *));
3185942a
JA
133#if defined (HANDLE_MULTIBYTE)
134static int read_mbchar __P((int, char *, int, int, int));
135#endif
136static void ttyrestore __P((struct ttsave *));
f73dda09
JA
137
138static sighandler sigalrm __P((int));
139static void reset_alarm __P((void));
ccc6cda3 140
ac50fbac
CR
141/* Try this to see what the rest of the shell can do with the information. */
142procenv_t alrmbuf;
143int sigalrm_seen;
144
84c617ec 145static int reading, tty_modified;
bb70624e 146static SigHandler *old_alrm;
f73dda09 147static unsigned char delim;
bb70624e 148
84c617ec
CR
149static struct ttsave termsave;
150
ac50fbac
CR
151/* In all cases, SIGALRM just sets a flag that we check periodically. This
152 avoids problems with the semi-tricky stuff we do with the xfree of
153 input_string at the top of the unwind-protect list (see below). */
154
155/* Set a flag that CHECK_ALRM can check. This relies on zread calling
156 trap.c:check_signals_and_traps(), which knows about sigalrm_seen and
157 alrmbuf. */
bb70624e
JA
158static sighandler
159sigalrm (s)
160 int s;
161{
ac50fbac 162 sigalrm_seen = 1;
bb70624e
JA
163}
164
165static void
166reset_alarm ()
167{
a0c0a00f 168 /* Cancel alarm before restoring signal handler. */
3185942a 169 falarm (0, 0);
a0c0a00f 170 set_signal_handler (SIGALRM, old_alrm);
bb70624e
JA
171}
172
726f6388
JA
173/* Read the value of the shell variables whose names follow.
174 The reading is done from the current input stream, whatever
175 that may be. Successive words of the input line are assigned
176 to the variables mentioned in LIST. The last variable in LIST
177 gets the remainder of the words on the line. If no variables
ccc6cda3
JA
178 are mentioned in LIST, then the default variable is $REPLY. */
179int
726f6388
JA
180read_builtin (list)
181 WORD_LIST *list;
182{
183 register char *varname;
5b9762d6
CR
184 int size, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2;
185 volatile int i;
f1be666c 186 int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
ac50fbac 187 int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno;
3185942a
JA
188 unsigned int tmsec, tmusec;
189 long ival, uval;
7117c2d2 190 intmax_t intval;
ccc6cda3
JA
191 char c;
192 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
f1be666c 193 char *e, *t, *t1, *ps2, *tofree;
bb70624e 194 struct stat tsb;
726f6388 195 SHELL_VAR *var;
3185942a 196 TTYSTRUCT ttattrs, ttset;
ccc6cda3
JA
197#if defined (ARRAY_VARS)
198 WORD_LIST *alist;
199#endif
200#if defined (READLINE)
3185942a 201 char *rlbuf, *itext;
ccc6cda3
JA
202 int rlind;
203#endif
726f6388 204
f73dda09
JA
205 USE_VAR(size);
206 USE_VAR(i);
207 USE_VAR(pass_next);
f1be666c 208 USE_VAR(print_ps2);
f73dda09
JA
209 USE_VAR(saw_escape);
210 USE_VAR(input_is_pipe);
211/* USE_VAR(raw); */
212 USE_VAR(edit);
3185942a
JA
213 USE_VAR(tmsec);
214 USE_VAR(tmusec);
f73dda09
JA
215 USE_VAR(nchars);
216 USE_VAR(silent);
217 USE_VAR(ifs_chars);
218 USE_VAR(prompt);
219 USE_VAR(arrayname);
220#if defined (READLINE)
221 USE_VAR(rlbuf);
222 USE_VAR(rlind);
3185942a 223 USE_VAR(itext);
f73dda09
JA
224#endif
225 USE_VAR(list);
f1be666c 226 USE_VAR(ps2);
ac50fbac
CR
227 USE_VAR(lastsig);
228
84c617ec 229 sigalrm_seen = reading = tty_modified = 0;
f73dda09 230
726f6388 231 i = 0; /* Index into the string that we are reading. */
ccc6cda3 232 raw = edit = 0; /* Not reading raw input by default. */
bb70624e 233 silent = 0;
ccc6cda3 234 arrayname = prompt = (char *)NULL;
7117c2d2 235 fd = 0; /* file descriptor to read from */
ccc6cda3
JA
236
237#if defined (READLINE)
3185942a 238 rlbuf = itext = (char *)0;
ccc6cda3
JA
239 rlind = 0;
240#endif
726f6388 241
3185942a 242 tmsec = tmusec = 0; /* no timeout */
95732b49 243 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
bb70624e 244 delim = '\n'; /* read until newline */
0001803f 245 ignore_delim = 0;
bb70624e 246
ccc6cda3 247 reset_internal_getopt ();
0001803f 248 while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
726f6388 249 {
ccc6cda3 250 switch (opt)
28ef6c31
JA
251 {
252 case 'r':
ccc6cda3 253 raw = 1;
726f6388 254 break;
ccc6cda3
JA
255 case 'p':
256 prompt = list_optarg;
257 break;
bb70624e
JA
258 case 's':
259 silent = 1;
260 break;
ccc6cda3
JA
261 case 'e':
262#if defined (READLINE)
263 edit = 1;
3185942a
JA
264#endif
265 break;
266 case 'i':
267#if defined (READLINE)
268 itext = list_optarg;
ccc6cda3
JA
269#endif
270 break;
271#if defined (ARRAY_VARS)
272 case 'a':
273 arrayname = list_optarg;
274 break;
275#endif
bb70624e 276 case 't':
3185942a
JA
277 code = uconvert (list_optarg, &ival, &uval);
278 if (code == 0 || ival < 0 || uval < 0)
bb70624e 279 {
b80f6443 280 builtin_error (_("%s: invalid timeout specification"), list_optarg);
bb70624e
JA
281 return (EXECUTION_FAILURE);
282 }
283 else
f73dda09
JA
284 {
285 have_timeout = 1;
3185942a
JA
286 tmsec = ival;
287 tmusec = uval;
f73dda09 288 }
bb70624e 289 break;
0001803f
CR
290 case 'N':
291 ignore_delim = 1;
292 delim = -1;
bb70624e 293 case 'n':
7117c2d2
JA
294 code = legal_number (list_optarg, &intval);
295 if (code == 0 || intval < 0 || intval != (int)intval)
bb70624e 296 {
7117c2d2 297 sh_invalidnum (list_optarg);
bb70624e
JA
298 return (EXECUTION_FAILURE);
299 }
300 else
7117c2d2
JA
301 nchars = intval;
302 break;
303 case 'u':
304 code = legal_number (list_optarg, &intval);
305 if (code == 0 || intval < 0 || intval != (int)intval)
306 {
b80f6443 307 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
7117c2d2
JA
308 return (EXECUTION_FAILURE);
309 }
310 else
311 fd = intval;
312 if (sh_validfd (fd) == 0)
313 {
b80f6443 314 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
7117c2d2
JA
315 return (EXECUTION_FAILURE);
316 }
bb70624e
JA
317 break;
318 case 'd':
319 delim = *list_optarg;
320 break;
a0c0a00f 321 CASE_HELPOPT;
ccc6cda3
JA
322 default:
323 builtin_usage ();
726f6388
JA
324 return (EX_USAGE);
325 }
726f6388 326 }
ccc6cda3 327 list = loptend;
726f6388 328
17345e5a
JA
329 /* `read -t 0 var' tests whether input is available with select/FIONREAD,
330 and fails if those are unavailable */
3185942a
JA
331 if (have_timeout && tmsec == 0 && tmusec == 0)
332#if 0
bb70624e 333 return (EXECUTION_FAILURE);
3185942a
JA
334#else
335 return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
336#endif
bb70624e 337
ac50fbac
CR
338 /* Convenience: check early whether or not the first of possibly several
339 variable names is a valid identifier, and bail early if so. */
340#if defined (ARRAY_VARS)
a0c0a00f 341 if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, 0) == 0)
ac50fbac
CR
342#else
343 if (list && legal_identifier (list->word->word) == 0)
344#endif
345 {
346 sh_invalidid (list->word->word);
347 return (EXECUTION_FAILURE);
348 }
349
0001803f
CR
350 /* If we're asked to ignore the delimiter, make sure we do. */
351 if (ignore_delim)
352 delim = -1;
353
ccc6cda3 354 /* IF IFS is unset, we use the default of " \t\n". */
7117c2d2
JA
355 ifs_chars = getifs ();
356 if (ifs_chars == 0) /* XXX - shouldn't happen */
357 ifs_chars = "";
0001803f
CR
358 /* If we want to read exactly NCHARS chars, don't split on IFS */
359 if (ignore_delim)
360 ifs_chars = "";
3185942a
JA
361 for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
362 skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
726f6388 363
7117c2d2 364 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
3185942a 365 input_string[0] = '\0';
7117c2d2
JA
366
367 /* $TMOUT, if set, is the default timeout for read. */
368 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
369 {
3185942a
JA
370 code = uconvert (e, &ival, &uval);
371 if (code == 0 || ival < 0 || uval < 0)
372 tmsec = tmusec = 0;
7117c2d2 373 else
3185942a
JA
374 {
375 tmsec = ival;
376 tmusec = uval;
377 }
7117c2d2 378 }
726f6388
JA
379
380 begin_unwind_frame ("read_builtin");
726f6388 381
95732b49
JA
382#if defined (BUFFERED_INPUT)
383 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
384 sync_buffered_stream (default_buffered_input);
385#endif
386
7117c2d2 387 input_is_tty = isatty (fd);
bb70624e 388 if (input_is_tty == 0)
28ef6c31 389#ifndef __CYGWIN__
95732b49 390 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
bb70624e
JA
391#else
392 input_is_pipe = 1;
393#endif
394
395 /* If the -p, -e or -s flags were given, but input is not coming from the
ccc6cda3 396 terminal, turn them off. */
bb70624e 397 if ((prompt || edit || silent) && input_is_tty == 0)
ccc6cda3
JA
398 {
399 prompt = (char *)NULL;
3185942a
JA
400#if defined (READLINE)
401 itext = (char *)NULL;
402#endif
bb70624e 403 edit = silent = 0;
ccc6cda3
JA
404 }
405
7117c2d2
JA
406#if defined (READLINE)
407 if (edit)
408 add_unwind_protect (xfree, rlbuf);
409#endif
410
726f6388
JA
411 pass_next = 0; /* Non-zero signifies last char was backslash. */
412 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
413
3185942a 414 if (tmsec > 0 || tmusec > 0)
bb70624e
JA
415 {
416 /* Turn off the timeout if stdin is a regular file (e.g. from
417 input redirection). */
7117c2d2 418 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
3185942a 419 tmsec = tmusec = 0;
bb70624e
JA
420 }
421
3185942a 422 if (tmsec > 0 || tmusec > 0)
bb70624e 423 {
ac50fbac 424 code = setjmp_nosigs (alrmbuf);
bb70624e
JA
425 if (code)
426 {
ac50fbac 427 sigalrm_seen = 0;
89a92869
CR
428 /* Tricky. The top of the unwind-protect stack is the free of
429 input_string. We want to run all the rest and use input_string,
43aebe92 430 so we have to save input_string temporarily, run the unwind-
ac50fbac
CR
431 protects, then restore input_string so we can use it later */
432 orig_input_string = 0;
3185942a 433 input_string[i] = '\0'; /* make sure it's terminated */
43aebe92
CR
434 if (i == 0)
435 {
436 t = (char *)xmalloc (1);
437 t[0] = 0;
438 }
439 else
440 t = savestring (input_string);
441
442 run_unwind_frame ("read_builtin");
443 input_string = t;
89a92869 444 retval = 128+SIGALRM;
3185942a 445 goto assign_vars;
bb70624e 446 }
84c617ec
CR
447 if (interactive_shell == 0)
448 initialize_terminating_signals ();
bb70624e
JA
449 old_alrm = set_signal_handler (SIGALRM, sigalrm);
450 add_unwind_protect (reset_alarm, (char *)NULL);
0628567a
JA
451#if defined (READLINE)
452 if (edit)
9b86eb22
CR
453 {
454 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
455 add_unwind_protect (bashline_reset_event_hook, (char *)NULL);
456 }
0628567a 457#endif
3185942a 458 falarm (tmsec, tmusec);
bb70624e
JA
459 }
460
461 /* If we've been asked to read only NCHARS chars, or we're using some
462 character other than newline to terminate the line, do the right
463 thing to readline or the tty. */
464 if (nchars > 0 || delim != '\n')
465 {
466#if defined (READLINE)
467 if (edit)
468 {
469 if (nchars > 0)
470 {
471 unwind_protect_int (rl_num_chars_to_read);
472 rl_num_chars_to_read = nchars;
473 }
474 if (delim != '\n')
475 {
476 set_eol_delim (delim);
477 add_unwind_protect (reset_eol_delim, (char *)NULL);
478 }
479 }
480 else
481#endif
482 if (input_is_tty)
28ef6c31 483 {
3185942a
JA
484 /* ttsave() */
485 termsave.fd = fd;
486 ttgetattr (fd, &ttattrs);
487 termsave.attrs = &ttattrs;
488
489 ttset = ttattrs;
17345e5a
JA
490 i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
491 if (i < 0)
492 sh_ttyerror (1);
84c617ec 493 tty_modified = 1;
3185942a 494 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
84c617ec
CR
495 if (interactive_shell == 0)
496 initialize_terminating_signals ();
bb70624e
JA
497 }
498 }
499 else if (silent) /* turn off echo but leave term in canonical mode */
500 {
3185942a
JA
501 /* ttsave (); */
502 termsave.fd = fd;
503 ttgetattr (fd, &ttattrs);
504 termsave.attrs = &ttattrs;
505
506 ttset = ttattrs;
17345e5a
JA
507 i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */
508 if (i < 0)
509 sh_ttyerror (1);
3185942a 510
84c617ec 511 tty_modified = 1;
3185942a 512 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
84c617ec
CR
513 if (interactive_shell == 0)
514 initialize_terminating_signals ();
bb70624e
JA
515 }
516
28ef6c31
JA
517 /* This *must* be the top unwind-protect on the stack, so the manipulation
518 of the unwind-protect stack after the realloc() works right. */
519 add_unwind_protect (xfree, input_string);
28ef6c31 520
ac50fbac
CR
521 CHECK_ALRM;
522 if ((nchars > 0) && (input_is_tty == 0) && ignore_delim) /* read -N */
523 unbuffered_read = 2;
524 else if ((nchars > 0) || (delim != '\n') || input_is_pipe)
525 unbuffered_read = 1;
bb70624e 526
3185942a
JA
527 if (prompt && edit == 0)
528 {
529 fprintf (stderr, "%s", prompt);
530 fflush (stderr);
531 }
532
28ef6c31
JA
533#if defined (__CYGWIN__) && defined (O_TEXT)
534 setmode (0, O_TEXT);
535#endif
536
f1be666c
JA
537 ps2 = 0;
538 for (print_ps2 = eof = retval = 0;;)
726f6388 539 {
ac50fbac
CR
540 CHECK_ALRM;
541
ccc6cda3
JA
542#if defined (READLINE)
543 if (edit)
544 {
545 if (rlbuf && rlbuf[rlind] == '\0')
546 {
d166f048 547 xfree (rlbuf);
ccc6cda3
JA
548 rlbuf = (char *)0;
549 }
550 if (rlbuf == 0)
551 {
ac50fbac 552 reading = 1;
3185942a 553 rlbuf = edit_line (prompt ? prompt : "", itext);
ac50fbac 554 reading = 0;
ccc6cda3
JA
555 rlind = 0;
556 }
557 if (rlbuf == 0)
558 {
559 eof = 1;
560 break;
561 }
562 c = rlbuf[rlind++];
563 }
564 else
b72432fd 565 {
e8ce775d
JA
566#endif
567
f1be666c
JA
568 if (print_ps2)
569 {
570 if (ps2 == 0)
571 ps2 = get_string_value ("PS2");
572 fprintf (stderr, "%s", ps2 ? ps2 : "");
573 fflush (stderr);
574 print_ps2 = 0;
575 }
576
ac50fbac
CR
577#if 0
578 if (posixly_correct == 0)
579 interrupt_immediately++;
580#endif
581 reading = 1;
582 if (unbuffered_read == 2)
583 retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr);
584 else if (unbuffered_read)
585 retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1);
bb70624e 586 else
ac50fbac
CR
587 retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c);
588 reading = 0;
589#if 0
590 if (posixly_correct == 0)
591 interrupt_immediately--;
592#endif
bb70624e 593
e8ce775d 594 if (retval <= 0)
ccc6cda3 595 {
ac50fbac
CR
596 if (retval < 0 && errno == EINTR)
597 {
598 lastsig = LASTSIG();
599 if (lastsig == 0)
600 lastsig = trapped_signal_received;
601 run_pending_traps (); /* because interrupt_immediately is not set */
602 }
603 else
604 lastsig = 0;
84c617ec
CR
605 if (terminating_signal && tty_modified)
606 ttyrestore (&termsave); /* fix terminal before exiting */
ac50fbac 607 CHECK_TERMSIG;
ccc6cda3
JA
608 eof = 1;
609 break;
610 }
611
ac50fbac
CR
612 CHECK_ALRM;
613
b72432fd
JA
614#if defined (READLINE)
615 }
616#endif
617
ac50fbac 618 CHECK_ALRM;
3185942a 619 if (i + 4 >= size) /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */
28ef6c31 620 {
ac50fbac
CR
621 char *t;
622 t = (char *)xrealloc (input_string, size += 128);
623
624 /* Only need to change unwind-protect if input_string changes */
625 if (t != input_string)
626 {
627 input_string = t;
628 remove_unwind_protect ();
629 add_unwind_protect (xfree, input_string);
630 }
28ef6c31 631 }
726f6388
JA
632
633 /* If the next character is to be accepted verbatim, a backslash
634 newline pair still disappears from the input. */
635 if (pass_next)
636 {
95732b49 637 pass_next = 0;
726f6388 638 if (c == '\n')
f1be666c
JA
639 {
640 i--; /* back up over the CTLESC */
641 if (interactive && input_is_tty && raw == 0)
642 print_ps2 = 1;
643 }
726f6388 644 else
95732b49 645 goto add_char;
726f6388
JA
646 continue;
647 }
648
3185942a 649 /* This may cause problems if IFS contains CTLESC */
ccc6cda3 650 if (c == '\\' && raw == 0)
726f6388
JA
651 {
652 pass_next++;
3185942a
JA
653 if (skip_ctlesc == 0)
654 {
655 saw_escape++;
656 input_string[i++] = CTLESC;
657 }
726f6388
JA
658 continue;
659 }
660
ac50fbac 661 if (ignore_delim == 0 && (unsigned char)c == delim)
726f6388
JA
662 break;
663
ac50fbac
CR
664 if (c == '\0' && delim != '\0')
665 continue; /* skip NUL bytes in input */
666
3185942a 667 if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
726f6388
JA
668 {
669 saw_escape++;
670 input_string[i++] = CTLESC;
671 }
672
95732b49 673add_char:
726f6388 674 input_string[i++] = c;
ac50fbac 675 CHECK_ALRM;
3185942a
JA
676
677#if defined (HANDLE_MULTIBYTE)
ac50fbac 678 if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0)
3185942a
JA
679 {
680 input_string[i] = '\0'; /* for simplicity and debugging */
681 i += read_mbchar (fd, input_string, i, c, unbuffered_read);
682 }
683#endif
684
95732b49 685 nr++;
bb70624e 686
95732b49 687 if (nchars > 0 && nr >= nchars)
bb70624e 688 break;
726f6388
JA
689 }
690 input_string[i] = '\0';
ac50fbac 691 CHECK_ALRM;
726f6388 692
7117c2d2
JA
693 if (retval < 0)
694 {
ac50fbac
CR
695 t_errno = errno;
696 if (errno != EINTR)
697 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
0628567a 698 run_unwind_frame ("read_builtin");
ac50fbac 699 return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig);
7117c2d2 700 }
7117c2d2 701
3185942a 702 if (tmsec > 0 || tmusec > 0)
bb70624e
JA
703 reset_alarm ();
704
705 if (nchars > 0 || delim != '\n')
706 {
707#if defined (READLINE)
708 if (edit)
709 {
710 if (nchars > 0)
711 rl_num_chars_to_read = 0;
712 if (delim != '\n')
f73dda09 713 reset_eol_delim ((char *)NULL);
bb70624e
JA
714 }
715 else
716#endif
717 if (input_is_tty)
3185942a 718 ttyrestore (&termsave);
bb70624e
JA
719 }
720 else if (silent)
3185942a 721 ttyrestore (&termsave);
bb70624e
JA
722
723 if (unbuffered_read == 0)
7117c2d2 724 zsyncfd (fd);
bb70624e 725
726f6388
JA
726 discard_unwind_frame ("read_builtin");
727
ccc6cda3 728 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
726f6388 729
3185942a
JA
730assign_vars:
731
ccc6cda3
JA
732#if defined (ARRAY_VARS)
733 /* If -a was given, take the string read, break it into a list of words,
734 an assign them to `arrayname' in turn. */
735 if (arrayname)
726f6388 736 {
b80f6443
JA
737 if (legal_identifier (arrayname) == 0)
738 {
739 sh_invalidid (arrayname);
740 xfree (input_string);
741 return (EXECUTION_FAILURE);
742 }
743
f73dda09 744 var = find_or_make_array_variable (arrayname, 1);
ccc6cda3 745 if (var == 0)
0628567a
JA
746 {
747 xfree (input_string);
748 return EXECUTION_FAILURE; /* readonly or noassign */
749 }
b4d40164
CR
750 if (assoc_p (var))
751 {
752 builtin_error (_("%s: cannot convert associative to indexed array"), arrayname);
753 xfree (input_string);
754 return EXECUTION_FAILURE; /* existing associative array */
755 }
ac50fbac
CR
756 else if (invisible_p (var))
757 VUNSETATTR (var, att_invisible);
7117c2d2 758 array_flush (array_cell (var));
ccc6cda3
JA
759
760 alist = list_string (input_string, ifs_chars, 0);
761 if (alist)
762 {
0628567a
JA
763 if (saw_escape)
764 dequote_list (alist);
765 else
766 word_list_remove_quoted_nulls (alist);
95732b49 767 assign_array_var_from_word_list (var, alist, 0);
ccc6cda3
JA
768 dispose_words (alist);
769 }
d166f048 770 xfree (input_string);
ccc6cda3 771 return (retval);
726f6388 772 }
ccc6cda3 773#endif /* ARRAY_VARS */
726f6388 774
d166f048
JA
775 /* If there are no variables, save the text of the line read to the
776 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
777 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
778 same way, but I believe that the difference in behaviors is useful
779 enough to not do it. Without the bash behavior, there is no way
28ef6c31
JA
780 to read a line completely without interpretation or modification
781 unless you mess with $IFS (e.g., setting it to the empty string).
d166f048
JA
782 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
783 if (list == 0)
726f6388 784 {
d166f048
JA
785#if 0
786 orig_input_string = input_string;
7117c2d2 787 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
d166f048
JA
788 ;
789 input_string = t;
790 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
791#endif
792
726f6388
JA
793 if (saw_escape)
794 {
795 t = dequote_string (input_string);
95732b49 796 var = bind_variable ("REPLY", t, 0);
726f6388
JA
797 free (t);
798 }
799 else
95732b49 800 var = bind_variable ("REPLY", input_string, 0);
a0c0a00f
CR
801 if (var == 0 || readonly_p (var) || noassign_p (var))
802 retval = EXECUTION_FAILURE;
803 else
804 VUNSETATTR (var, att_invisible);
28ef6c31 805
ac50fbac 806 xfree (input_string);
ccc6cda3 807 return (retval);
726f6388 808 }
ccc6cda3
JA
809
810 /* This code implements the Posix.2 spec for splitting the words
811 read and assigning them to variables. */
812 orig_input_string = input_string;
813
814 /* Remove IFS white space at the beginning of the input string. If
815 $IFS is null, no field splitting is performed. */
7117c2d2 816 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
ccc6cda3
JA
817 ;
818 input_string = t;
ccc6cda3 819 for (; list->next; list = list->next)
726f6388 820 {
ccc6cda3
JA
821 varname = list->word->word;
822#if defined (ARRAY_VARS)
a0c0a00f 823 if (legal_identifier (varname) == 0 && valid_array_reference (varname, 0) == 0)
ccc6cda3
JA
824#else
825 if (legal_identifier (varname) == 0)
826#endif
726f6388 827 {
7117c2d2 828 sh_invalidid (varname);
f73dda09 829 xfree (orig_input_string);
ccc6cda3
JA
830 return (EXECUTION_FAILURE);
831 }
726f6388 832
ccc6cda3
JA
833 /* If there are more variables than words read from the input,
834 the remaining variables are set to the empty string. */
835 if (*input_string)
836 {
837 /* This call updates INPUT_STRING. */
838 t = get_word_from_string (&input_string, ifs_chars, &e);
839 if (t)
840 *e = '\0';
841 /* Don't bother to remove the CTLESC unless we added one
842 somewhere while reading the string. */
843 if (t && saw_escape)
726f6388 844 {
ccc6cda3
JA
845 t1 = dequote_string (t);
846 var = bind_read_variable (varname, t1);
f73dda09 847 xfree (t1);
726f6388
JA
848 }
849 else
952a5758 850 var = bind_read_variable (varname, t ? t : "");
ccc6cda3
JA
851 }
852 else
853 {
854 t = (char *)0;
855 var = bind_read_variable (varname, "");
726f6388
JA
856 }
857
ccc6cda3
JA
858 FREE (t);
859 if (var == 0)
726f6388 860 {
f73dda09 861 xfree (orig_input_string);
726f6388
JA
862 return (EXECUTION_FAILURE);
863 }
864
ccc6cda3 865 stupidly_hack_special_variables (varname);
bb70624e 866 VUNSETATTR (var, att_invisible);
ccc6cda3
JA
867 }
868
869 /* Now assign the rest of the line to the last variable argument. */
870#if defined (ARRAY_VARS)
a0c0a00f 871 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, 0) == 0)
ccc6cda3
JA
872#else
873 if (legal_identifier (list->word->word) == 0)
874#endif
875 {
7117c2d2 876 sh_invalidid (list->word->word);
f73dda09 877 xfree (orig_input_string);
ccc6cda3 878 return (EXECUTION_FAILURE);
726f6388
JA
879 }
880
95732b49 881#if 0
ccc6cda3
JA
882 /* This has to be done this way rather than using string_list
883 and list_string because Posix.2 says that the last variable gets the
884 remaining words and their intervening separators. */
885 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
95732b49
JA
886#else
887 /* Check whether or not the number of fields is exactly the same as the
888 number of variables. */
f1be666c 889 tofree = NULL;
95732b49
JA
890 if (*input_string)
891 {
892 t1 = input_string;
893 t = get_word_from_string (&input_string, ifs_chars, &e);
894 if (*input_string == 0)
f1be666c 895 tofree = input_string = t;
95732b49 896 else
89a92869
CR
897 {
898 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
899 tofree = t;
900 }
95732b49
JA
901 }
902#endif
ccc6cda3 903
0213a83a 904 if (saw_escape && input_string && *input_string)
ccc6cda3
JA
905 {
906 t = dequote_string (input_string);
907 var = bind_read_variable (list->word->word, t);
f73dda09 908 xfree (t);
ccc6cda3
JA
909 }
910 else
952a5758 911 var = bind_read_variable (list->word->word, input_string ? input_string : "");
f1be666c 912
ccc6cda3 913 if (var)
495aee44
CR
914 {
915 stupidly_hack_special_variables (list->word->word);
916 VUNSETATTR (var, att_invisible);
917 }
918 else
919 retval = EXECUTION_FAILURE;
920
921 FREE (tofree);
f73dda09 922 xfree (orig_input_string);
ccc6cda3 923
726f6388
JA
924 return (retval);
925}
926
ccc6cda3
JA
927static SHELL_VAR *
928bind_read_variable (name, value)
929 char *name, *value;
930{
495aee44 931 SHELL_VAR *v;
ac50fbac 932
ccc6cda3 933#if defined (ARRAY_VARS)
a0c0a00f 934 if (valid_array_reference (name, 0) == 0)
495aee44 935 v = bind_variable (name, value, 0);
ccc6cda3 936 else
495aee44 937 v = assign_array_element (name, value, 0);
f73dda09 938#else /* !ARRAY_VARS */
495aee44 939 v = bind_variable (name, value, 0);
f73dda09 940#endif /* !ARRAY_VARS */
495aee44
CR
941 return (v == 0 ? v
942 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
ccc6cda3
JA
943}
944
3185942a
JA
945#if defined (HANDLE_MULTIBYTE)
946static int
947read_mbchar (fd, string, ind, ch, unbuffered)
948 int fd;
949 char *string;
950 int ind, ch, unbuffered;
951{
952 char mbchar[MB_LEN_MAX + 1];
953 int i, n, r;
954 char c;
955 size_t ret;
956 mbstate_t ps, ps_back;
957 wchar_t wc;
958
959 memset (&ps, '\0', sizeof (mbstate_t));
960 memset (&ps_back, '\0', sizeof (mbstate_t));
961
962 mbchar[0] = ch;
963 i = 1;
964 for (n = 0; n <= MB_LEN_MAX; n++)
965 {
966 ps_back = ps;
967 ret = mbrtowc (&wc, mbchar, i, &ps);
968 if (ret == (size_t)-2)
969 {
970 ps = ps_back;
ac50fbac 971 /* We don't want to be interrupted during a multibyte char read */
3185942a
JA
972 if (unbuffered)
973 r = zread (fd, &c, 1);
974 else
975 r = zreadc (fd, &c);
976 if (r < 0)
977 goto mbchar_return;
978 mbchar[i++] = c;
979 continue;
980 }
981 else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0)
982 break;
983 }
984
985mbchar_return:
986 if (i > 1) /* read a multibyte char */
987 /* mbchar[0] is already string[ind-1] */
988 for (r = 1; r < i; r++)
989 string[ind+r-1] = mbchar[r];
990 return i - 1;
991}
992#endif
993
994
995static void
996ttyrestore (ttp)
997 struct ttsave *ttp;
998{
999 ttsetattr (ttp->fd, ttp->attrs);
84c617ec
CR
1000 tty_modified = 0;
1001}
1002
1003void
1004read_tty_cleanup ()
1005{
1006 if (tty_modified)
1007 ttyrestore (&termsave);
1008}
1009
1010int
1011read_tty_modified ()
1012{
1013 return (tty_modified);
3185942a
JA
1014}
1015
ccc6cda3 1016#if defined (READLINE)
0628567a 1017static rl_completion_func_t *old_attempted_completion_function = 0;
3185942a
JA
1018static rl_hook_func_t *old_startup_hook;
1019static char *deftext;
0628567a
JA
1020
1021static void
1022reset_attempted_completion_function (cp)
1023 char *cp;
1024{
1025 if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
1026 rl_attempted_completion_function = old_attempted_completion_function;
1027}
7117c2d2 1028
3185942a
JA
1029static int
1030set_itext ()
1031{
1032 int r1, r2;
1033
1034 r1 = r2 = 0;
1035 if (old_startup_hook)
1036 r1 = (*old_startup_hook) ();
1037 if (deftext)
1038 {
1039 r2 = rl_insert_text (deftext);
1040 deftext = (char *)NULL;
1041 rl_startup_hook = old_startup_hook;
1042 old_startup_hook = (rl_hook_func_t *)NULL;
1043 }
1044 return (r1 || r2);
1045}
1046
ccc6cda3 1047static char *
3185942a 1048edit_line (p, itext)
ccc6cda3 1049 char *p;
3185942a 1050 char *itext;
726f6388 1051{
ccc6cda3
JA
1052 char *ret;
1053 int len;
1054
95732b49 1055 if (bash_readline_initialized == 0)
ccc6cda3 1056 initialize_readline ();
0628567a 1057
7117c2d2
JA
1058 old_attempted_completion_function = rl_attempted_completion_function;
1059 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
9b86eb22 1060 bashline_set_event_hook ();
3185942a
JA
1061 if (itext)
1062 {
1063 old_startup_hook = rl_startup_hook;
1064 rl_startup_hook = set_itext;
1065 deftext = itext;
1066 }
ac50fbac 1067
ccc6cda3 1068 ret = readline (p);
ac50fbac 1069
7117c2d2 1070 rl_attempted_completion_function = old_attempted_completion_function;
0628567a 1071 old_attempted_completion_function = (rl_completion_func_t *)NULL;
9b86eb22 1072 bashline_reset_event_hook ();
0628567a 1073
ccc6cda3
JA
1074 if (ret == 0)
1075 return ret;
1076 len = strlen (ret);
f73dda09 1077 ret = (char *)xrealloc (ret, len + 2);
bb70624e 1078 ret[len++] = delim;
ccc6cda3
JA
1079 ret[len] = '\0';
1080 return ret;
726f6388 1081}
bb70624e
JA
1082
1083static int old_delim_ctype;
f73dda09 1084static rl_command_func_t *old_delim_func;
bb70624e 1085static int old_newline_ctype;
f73dda09 1086static rl_command_func_t *old_newline_func;
bb70624e 1087
f73dda09 1088static unsigned char delim_char;
bb70624e
JA
1089
1090static void
1091set_eol_delim (c)
1092 int c;
1093{
1094 Keymap cmap;
1095
1096 if (bash_readline_initialized == 0)
1097 initialize_readline ();
1098 cmap = rl_get_keymap ();
1099
1100 /* Change newline to self-insert */
1101 old_newline_ctype = cmap[RETURN].type;
1102 old_newline_func = cmap[RETURN].function;
1103 cmap[RETURN].type = ISFUNC;
1104 cmap[RETURN].function = rl_insert;
1105
1106 /* Bind the delimiter character to accept-line. */
1107 old_delim_ctype = cmap[c].type;
1108 old_delim_func = cmap[c].function;
1109 cmap[c].type = ISFUNC;
1110 cmap[c].function = rl_newline;
1111
1112 delim_char = c;
1113}
1114
1115static void
f73dda09
JA
1116reset_eol_delim (cp)
1117 char *cp;
bb70624e
JA
1118{
1119 Keymap cmap;
1120
1121 cmap = rl_get_keymap ();
1122
1123 cmap[RETURN].type = old_newline_ctype;
1124 cmap[RETURN].function = old_newline_func;
1125
1126 cmap[delim_char].type = old_delim_ctype;
1127 cmap[delim_char].function = old_delim_func;
1128}
ccc6cda3 1129#endif