1 This file is read.def, from which is created read.c.
2 It implements the builtin "read" in Bash.
4 Copyright (C) 1987-2005 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION read_builtin
26 $SHORT_DOC read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
27 One line is read from the standard input, or from file descriptor FD if the
28 -u option is supplied, and the first word is assigned to the first NAME,
29 the second word to the second NAME, and so on, with leftover words assigned
30 to the last NAME. Only the characters found in $IFS are recognized as word
31 delimiters. If no NAMEs are supplied, the line read is stored in the REPLY
32 variable. If the -r option is given, this signifies `raw' input, and
33 backslash escaping is disabled. The -d option causes read to continue
34 until the first character of DELIM is read, rather than newline. If the -p
35 option is supplied, the string PROMPT is output without a trailing newline
36 before attempting to read. If -a is supplied, the words read are assigned
37 to sequential indices of ARRAY, starting at zero. If -e is supplied and
38 the shell is interactive, readline is used to obtain the line. If -n is
39 supplied with a non-zero NCHARS argument, read returns after NCHARS
40 characters have been read. The -s option causes input coming from a
41 terminal to not be echoed.
43 The -t option causes read to time out and return failure if a complete line
44 of input is not read within TIMEOUT seconds. If the TMOUT variable is set,
45 its value is the default timeout. The return code is zero, unless end-of-file
46 is encountered, read times out, or an invalid file descriptor is supplied as
52 #include "bashtypes.h"
53 #include "posixstat.h"
57 #if defined (HAVE_UNISTD_H)
69 #include "../bashintl.h"
73 #include "bashgetopt.h"
77 #if defined (READLINE)
78 #include "../bashline.h"
79 #include <readline/readline.h>
82 #if defined (BUFFERED_INPUT)
90 #if defined (READLINE)
91 static void reset_attempted_completion_function __P((char *));
92 static char *edit_line __P((char *));
93 static void set_eol_delim __P((int));
94 static void reset_eol_delim __P((char *));
96 static SHELL_VAR *bind_read_variable __P((char *, char *));
98 static sighandler sigalrm __P((int));
99 static void reset_alarm __P((void));
101 static procenv_t alrmbuf;
102 static SigHandler *old_alrm;
103 static unsigned char delim;
109 longjmp (alrmbuf, 1);
115 set_signal_handler (SIGALRM, old_alrm);
119 /* Read the value of the shell variables whose names follow.
120 The reading is done from the current input stream, whatever
121 that may be. Successive words of the input line are assigned
122 to the variables mentioned in LIST. The last variable in LIST
123 gets the remainder of the words on the line. If no variables
124 are mentioned in LIST, then the default variable is $REPLY. */
129 register char *varname;
130 int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2;
131 int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
132 int raw, edit, nchars, silent, have_timeout, fd;
136 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
137 char *e, *t, *t1, *ps2, *tofree;
140 #if defined (ARRAY_VARS)
143 #if defined (READLINE)
153 USE_VAR(input_is_pipe);
162 #if defined (READLINE)
169 i = 0; /* Index into the string that we are reading. */
170 raw = edit = 0; /* Not reading raw input by default. */
172 arrayname = prompt = (char *)NULL;
173 fd = 0; /* file descriptor to read from */
175 #if defined (READLINE)
180 tmout = 0; /* no timeout */
181 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
182 delim = '\n'; /* read until newline */
184 reset_internal_getopt ();
185 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
193 prompt = list_optarg;
199 #if defined (READLINE)
203 #if defined (ARRAY_VARS)
205 arrayname = list_optarg;
209 code = legal_number (list_optarg, &intval);
210 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
212 builtin_error (_("%s: invalid timeout specification"), list_optarg);
213 return (EXECUTION_FAILURE);
222 code = legal_number (list_optarg, &intval);
223 if (code == 0 || intval < 0 || intval != (int)intval)
225 sh_invalidnum (list_optarg);
226 return (EXECUTION_FAILURE);
232 code = legal_number (list_optarg, &intval);
233 if (code == 0 || intval < 0 || intval != (int)intval)
235 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
236 return (EXECUTION_FAILURE);
240 if (sh_validfd (fd) == 0)
242 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
243 return (EXECUTION_FAILURE);
247 delim = *list_optarg;
256 /* `read -t 0 var' returns failure immediately. XXX - should it test
257 whether input is available with select/FIONREAD, and fail if those
259 if (have_timeout && tmout == 0)
260 return (EXECUTION_FAILURE);
262 /* IF IFS is unset, we use the default of " \t\n". */
263 ifs_chars = getifs ();
264 if (ifs_chars == 0) /* XXX - shouldn't happen */
267 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
269 /* $TMOUT, if set, is the default timeout for read. */
270 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
272 code = legal_number (e, &intval);
273 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
279 begin_unwind_frame ("read_builtin");
281 #if defined (BUFFERED_INPUT)
282 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
283 sync_buffered_stream (default_buffered_input);
286 input_is_tty = isatty (fd);
287 if (input_is_tty == 0)
289 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
294 /* If the -p, -e or -s flags were given, but input is not coming from the
295 terminal, turn them off. */
296 if ((prompt || edit || silent) && input_is_tty == 0)
298 prompt = (char *)NULL;
302 #if defined (READLINE)
304 add_unwind_protect (xfree, rlbuf);
307 if (prompt && edit == 0)
309 fprintf (stderr, "%s", prompt);
313 pass_next = 0; /* Non-zero signifies last char was backslash. */
314 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
318 /* Turn off the timeout if stdin is a regular file (e.g. from
319 input redirection). */
320 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
326 code = setjmp (alrmbuf);
329 run_unwind_frame ("read_builtin");
330 return (EXECUTION_FAILURE);
332 old_alrm = set_signal_handler (SIGALRM, sigalrm);
333 add_unwind_protect (reset_alarm, (char *)NULL);
334 #if defined (READLINE)
336 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
341 /* If we've been asked to read only NCHARS chars, or we're using some
342 character other than newline to terminate the line, do the right
343 thing to readline or the tty. */
344 if (nchars > 0 || delim != '\n')
346 #if defined (READLINE)
351 unwind_protect_int (rl_num_chars_to_read);
352 rl_num_chars_to_read = nchars;
356 set_eol_delim (delim);
357 add_unwind_protect (reset_eol_delim, (char *)NULL);
369 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
372 else if (silent) /* turn off echo but leave term in canonical mode */
376 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
379 /* This *must* be the top unwind-protect on the stack, so the manipulation
380 of the unwind-protect stack after the realloc() works right. */
381 add_unwind_protect (xfree, input_string);
382 interrupt_immediately++;
383 terminate_immediately = 1;
385 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
387 #if defined (__CYGWIN__) && defined (O_TEXT)
392 for (print_ps2 = eof = retval = 0;;)
394 #if defined (READLINE)
397 if (rlbuf && rlbuf[rlind] == '\0')
404 rlbuf = edit_line (prompt ? prompt : "");
421 ps2 = get_string_value ("PS2");
422 fprintf (stderr, "%s", ps2 ? ps2 : "");
428 retval = zread (fd, &c, 1);
430 retval = zreadc (fd, &c);
438 #if defined (READLINE)
444 input_string = (char *)xrealloc (input_string, size += 128);
445 remove_unwind_protect ();
446 add_unwind_protect (xfree, input_string);
449 /* If the next character is to be accepted verbatim, a backslash
450 newline pair still disappears from the input. */
456 i--; /* back up over the CTLESC */
457 if (interactive && input_is_tty && raw == 0)
465 if (c == '\\' && raw == 0)
469 input_string[i++] = CTLESC;
473 if ((unsigned char)c == delim)
476 if (c == CTLESC || c == CTLNUL)
479 input_string[i++] = CTLESC;
483 input_string[i++] = c;
486 if (nchars > 0 && nr >= nchars)
489 input_string[i] = '\0';
494 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
495 run_unwind_frame ("read_builtin");
496 return (EXECUTION_FAILURE);
503 if (nchars > 0 || delim != '\n')
505 #if defined (READLINE)
509 rl_num_chars_to_read = 0;
511 reset_eol_delim ((char *)NULL);
521 if (unbuffered_read == 0)
524 interrupt_immediately--;
525 terminate_immediately = 0;
526 discard_unwind_frame ("read_builtin");
528 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
530 #if defined (ARRAY_VARS)
531 /* If -a was given, take the string read, break it into a list of words,
532 an assign them to `arrayname' in turn. */
535 if (legal_identifier (arrayname) == 0)
537 sh_invalidid (arrayname);
538 xfree (input_string);
539 return (EXECUTION_FAILURE);
542 var = find_or_make_array_variable (arrayname, 1);
545 xfree (input_string);
546 return EXECUTION_FAILURE; /* readonly or noassign */
548 array_flush (array_cell (var));
550 alist = list_string (input_string, ifs_chars, 0);
554 dequote_list (alist);
556 word_list_remove_quoted_nulls (alist);
557 assign_array_var_from_word_list (var, alist, 0);
558 dispose_words (alist);
560 xfree (input_string);
563 #endif /* ARRAY_VARS */
565 /* If there are no variables, save the text of the line read to the
566 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
567 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
568 same way, but I believe that the difference in behaviors is useful
569 enough to not do it. Without the bash behavior, there is no way
570 to read a line completely without interpretation or modification
571 unless you mess with $IFS (e.g., setting it to the empty string).
572 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
576 orig_input_string = input_string;
577 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
580 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
585 t = dequote_string (input_string);
586 var = bind_variable ("REPLY", t, 0);
590 var = bind_variable ("REPLY", input_string, 0);
591 VUNSETATTR (var, att_invisible);
597 /* This code implements the Posix.2 spec for splitting the words
598 read and assigning them to variables. */
599 orig_input_string = input_string;
601 /* Remove IFS white space at the beginning of the input string. If
602 $IFS is null, no field splitting is performed. */
603 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
607 for (; list->next; list = list->next)
609 varname = list->word->word;
610 #if defined (ARRAY_VARS)
611 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
613 if (legal_identifier (varname) == 0)
616 sh_invalidid (varname);
617 xfree (orig_input_string);
618 return (EXECUTION_FAILURE);
621 /* If there are more variables than words read from the input,
622 the remaining variables are set to the empty string. */
625 /* This call updates INPUT_STRING. */
626 t = get_word_from_string (&input_string, ifs_chars, &e);
629 /* Don't bother to remove the CTLESC unless we added one
630 somewhere while reading the string. */
633 t1 = dequote_string (t);
634 var = bind_read_variable (varname, t1);
638 var = bind_read_variable (varname, t);
643 var = bind_read_variable (varname, "");
649 xfree (orig_input_string);
650 return (EXECUTION_FAILURE);
653 stupidly_hack_special_variables (varname);
654 VUNSETATTR (var, att_invisible);
657 /* Now assign the rest of the line to the last variable argument. */
658 #if defined (ARRAY_VARS)
659 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
661 if (legal_identifier (list->word->word) == 0)
664 sh_invalidid (list->word->word);
665 xfree (orig_input_string);
666 return (EXECUTION_FAILURE);
670 /* This has to be done this way rather than using string_list
671 and list_string because Posix.2 says that the last variable gets the
672 remaining words and their intervening separators. */
673 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
675 /* Check whether or not the number of fields is exactly the same as the
676 number of variables. */
681 t = get_word_from_string (&input_string, ifs_chars, &e);
682 if (*input_string == 0)
683 tofree = input_string = t;
685 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
691 t = dequote_string (input_string);
692 var = bind_read_variable (list->word->word, t);
696 var = bind_read_variable (list->word->word, input_string);
697 stupidly_hack_special_variables (list->word->word);
701 VUNSETATTR (var, att_invisible);
702 xfree (orig_input_string);
708 bind_read_variable (name, value)
711 #if defined (ARRAY_VARS)
712 if (valid_array_reference (name) == 0)
713 return (bind_variable (name, value, 0));
715 return (assign_array_element (name, value, 0));
716 #else /* !ARRAY_VARS */
717 return bind_variable (name, value, 0);
718 #endif /* !ARRAY_VARS */
721 #if defined (READLINE)
722 static rl_completion_func_t *old_attempted_completion_function = 0;
725 reset_attempted_completion_function (cp)
728 if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
729 rl_attempted_completion_function = old_attempted_completion_function;
739 if (bash_readline_initialized == 0)
740 initialize_readline ();
742 old_attempted_completion_function = rl_attempted_completion_function;
743 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
745 rl_attempted_completion_function = old_attempted_completion_function;
746 old_attempted_completion_function = (rl_completion_func_t *)NULL;
751 ret = (char *)xrealloc (ret, len + 2);
757 static int old_delim_ctype;
758 static rl_command_func_t *old_delim_func;
759 static int old_newline_ctype;
760 static rl_command_func_t *old_newline_func;
762 static unsigned char delim_char;
770 if (bash_readline_initialized == 0)
771 initialize_readline ();
772 cmap = rl_get_keymap ();
774 /* Change newline to self-insert */
775 old_newline_ctype = cmap[RETURN].type;
776 old_newline_func = cmap[RETURN].function;
777 cmap[RETURN].type = ISFUNC;
778 cmap[RETURN].function = rl_insert;
780 /* Bind the delimiter character to accept-line. */
781 old_delim_ctype = cmap[c].type;
782 old_delim_func = cmap[c].function;
783 cmap[c].type = ISFUNC;
784 cmap[c].function = rl_newline;
795 cmap = rl_get_keymap ();
797 cmap[RETURN].type = old_newline_ctype;
798 cmap[RETURN].function = old_newline_func;
800 cmap[delim_char].type = old_delim_ctype;
801 cmap[delim_char].function = old_delim_func;