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 extern int interrupt_immediately;
92 #if defined (READLINE)
93 static char *edit_line __P((char *));
94 static void set_eol_delim __P((int));
95 static void reset_eol_delim __P((char *));
97 static SHELL_VAR *bind_read_variable __P((char *, char *));
99 static sighandler sigalrm __P((int));
100 static void reset_alarm __P((void));
102 static procenv_t alrmbuf;
103 static SigHandler *old_alrm;
104 static unsigned char delim;
110 longjmp (alrmbuf, 1);
116 set_signal_handler (SIGALRM, old_alrm);
120 /* Read the value of the shell variables whose names follow.
121 The reading is done from the current input stream, whatever
122 that may be. Successive words of the input line are assigned
123 to the variables mentioned in LIST. The last variable in LIST
124 gets the remainder of the words on the line. If no variables
125 are mentioned in LIST, then the default variable is $REPLY. */
130 register char *varname;
131 int size, i, nr, pass_next, saw_escape, eof, opt, retval, code;
132 int input_is_tty, input_is_pipe, unbuffered_read;
133 int raw, edit, nchars, silent, have_timeout, fd;
137 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
141 #if defined (ARRAY_VARS)
144 #if defined (READLINE)
153 USE_VAR(input_is_pipe);
162 #if defined (READLINE)
168 i = 0; /* Index into the string that we are reading. */
169 raw = edit = 0; /* Not reading raw input by default. */
171 arrayname = prompt = (char *)NULL;
172 fd = 0; /* file descriptor to read from */
174 #if defined (READLINE)
179 tmout = 0; /* no timeout */
180 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
181 delim = '\n'; /* read until newline */
183 reset_internal_getopt ();
184 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
192 prompt = list_optarg;
198 #if defined (READLINE)
202 #if defined (ARRAY_VARS)
204 arrayname = list_optarg;
208 code = legal_number (list_optarg, &intval);
209 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
211 builtin_error (_("%s: invalid timeout specification"), list_optarg);
212 return (EXECUTION_FAILURE);
221 code = legal_number (list_optarg, &intval);
222 if (code == 0 || intval < 0 || intval != (int)intval)
224 sh_invalidnum (list_optarg);
225 return (EXECUTION_FAILURE);
231 code = legal_number (list_optarg, &intval);
232 if (code == 0 || intval < 0 || intval != (int)intval)
234 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
235 return (EXECUTION_FAILURE);
239 if (sh_validfd (fd) == 0)
241 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
242 return (EXECUTION_FAILURE);
246 delim = *list_optarg;
255 /* `read -t 0 var' returns failure immediately. XXX - should it test
256 whether input is available with select/FIONREAD, and fail if those
258 if (have_timeout && tmout == 0)
259 return (EXECUTION_FAILURE);
261 /* IF IFS is unset, we use the default of " \t\n". */
262 ifs_chars = getifs ();
263 if (ifs_chars == 0) /* XXX - shouldn't happen */
266 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
268 /* $TMOUT, if set, is the default timeout for read. */
269 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
271 code = legal_number (e, &intval);
272 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
278 begin_unwind_frame ("read_builtin");
280 #if defined (BUFFERED_INPUT)
281 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
282 sync_buffered_stream (default_buffered_input);
285 input_is_tty = isatty (fd);
286 if (input_is_tty == 0)
288 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
293 /* If the -p, -e or -s flags were given, but input is not coming from the
294 terminal, turn them off. */
295 if ((prompt || edit || silent) && input_is_tty == 0)
297 prompt = (char *)NULL;
301 #if defined (READLINE)
303 add_unwind_protect (xfree, rlbuf);
306 if (prompt && edit == 0)
308 fprintf (stderr, "%s", prompt);
312 pass_next = 0; /* Non-zero signifies last char was backslash. */
313 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
317 /* Turn off the timeout if stdin is a regular file (e.g. from
318 input redirection). */
319 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
325 code = setjmp (alrmbuf);
328 run_unwind_frame ("read_builtin");
329 return (EXECUTION_FAILURE);
331 old_alrm = set_signal_handler (SIGALRM, sigalrm);
332 add_unwind_protect (reset_alarm, (char *)NULL);
336 /* If we've been asked to read only NCHARS chars, or we're using some
337 character other than newline to terminate the line, do the right
338 thing to readline or the tty. */
339 if (nchars > 0 || delim != '\n')
341 #if defined (READLINE)
346 unwind_protect_int (rl_num_chars_to_read);
347 rl_num_chars_to_read = nchars;
351 set_eol_delim (delim);
352 add_unwind_protect (reset_eol_delim, (char *)NULL);
364 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
367 else if (silent) /* turn off echo but leave term in canonical mode */
371 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
374 /* This *must* be the top unwind-protect on the stack, so the manipulation
375 of the unwind-protect stack after the realloc() works right. */
376 add_unwind_protect (xfree, input_string);
377 interrupt_immediately++;
379 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
381 #if defined (__CYGWIN__) && defined (O_TEXT)
385 for (eof = retval = 0;;)
387 #if defined (READLINE)
390 if (rlbuf && rlbuf[rlind] == '\0')
397 rlbuf = edit_line (prompt ? prompt : "");
412 retval = zread (fd, &c, 1);
414 retval = zreadc (fd, &c);
422 #if defined (READLINE)
428 input_string = (char *)xrealloc (input_string, size += 128);
429 remove_unwind_protect ();
430 add_unwind_protect (xfree, input_string);
433 /* If the next character is to be accepted verbatim, a backslash
434 newline pair still disappears from the input. */
439 i--; /* back up over the CTLESC */
445 if (c == '\\' && raw == 0)
449 input_string[i++] = CTLESC;
453 if ((unsigned char)c == delim)
456 if (c == CTLESC || c == CTLNUL)
459 input_string[i++] = CTLESC;
463 input_string[i++] = c;
466 if (nchars > 0 && nr >= nchars)
469 input_string[i] = '\0';
474 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
475 return (EXECUTION_FAILURE);
482 if (nchars > 0 || delim != '\n')
484 #if defined (READLINE)
488 rl_num_chars_to_read = 0;
490 reset_eol_delim ((char *)NULL);
500 if (unbuffered_read == 0)
503 interrupt_immediately--;
504 discard_unwind_frame ("read_builtin");
506 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
508 #if defined (ARRAY_VARS)
509 /* If -a was given, take the string read, break it into a list of words,
510 an assign them to `arrayname' in turn. */
513 if (legal_identifier (arrayname) == 0)
515 sh_invalidid (arrayname);
516 xfree (input_string);
517 return (EXECUTION_FAILURE);
520 var = find_or_make_array_variable (arrayname, 1);
522 return EXECUTION_FAILURE; /* readonly or noassign */
523 array_flush (array_cell (var));
525 alist = list_string (input_string, ifs_chars, 0);
528 word_list_remove_quoted_nulls (alist);
529 assign_array_var_from_word_list (var, alist, 0);
530 dispose_words (alist);
532 xfree (input_string);
535 #endif /* ARRAY_VARS */
537 /* If there are no variables, save the text of the line read to the
538 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
539 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
540 same way, but I believe that the difference in behaviors is useful
541 enough to not do it. Without the bash behavior, there is no way
542 to read a line completely without interpretation or modification
543 unless you mess with $IFS (e.g., setting it to the empty string).
544 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
548 orig_input_string = input_string;
549 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
552 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
557 t = dequote_string (input_string);
558 var = bind_variable ("REPLY", t, 0);
562 var = bind_variable ("REPLY", input_string, 0);
563 VUNSETATTR (var, att_invisible);
569 /* This code implements the Posix.2 spec for splitting the words
570 read and assigning them to variables. */
571 orig_input_string = input_string;
573 /* Remove IFS white space at the beginning of the input string. If
574 $IFS is null, no field splitting is performed. */
575 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
579 for (; list->next; list = list->next)
581 varname = list->word->word;
582 #if defined (ARRAY_VARS)
583 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
585 if (legal_identifier (varname) == 0)
588 sh_invalidid (varname);
589 xfree (orig_input_string);
590 return (EXECUTION_FAILURE);
593 /* If there are more variables than words read from the input,
594 the remaining variables are set to the empty string. */
597 /* This call updates INPUT_STRING. */
598 t = get_word_from_string (&input_string, ifs_chars, &e);
601 /* Don't bother to remove the CTLESC unless we added one
602 somewhere while reading the string. */
605 t1 = dequote_string (t);
606 var = bind_read_variable (varname, t1);
610 var = bind_read_variable (varname, t);
615 var = bind_read_variable (varname, "");
621 xfree (orig_input_string);
622 return (EXECUTION_FAILURE);
625 stupidly_hack_special_variables (varname);
626 VUNSETATTR (var, att_invisible);
629 /* Now assign the rest of the line to the last variable argument. */
630 #if defined (ARRAY_VARS)
631 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
633 if (legal_identifier (list->word->word) == 0)
636 sh_invalidid (list->word->word);
637 xfree (orig_input_string);
638 return (EXECUTION_FAILURE);
642 /* This has to be done this way rather than using string_list
643 and list_string because Posix.2 says that the last variable gets the
644 remaining words and their intervening separators. */
645 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
647 /* Check whether or not the number of fields is exactly the same as the
648 number of variables. */
652 t = get_word_from_string (&input_string, ifs_chars, &e);
653 if (*input_string == 0)
656 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
662 t = dequote_string (input_string);
663 var = bind_read_variable (list->word->word, t);
667 var = bind_read_variable (list->word->word, input_string);
668 stupidly_hack_special_variables (list->word->word);
670 VUNSETATTR (var, att_invisible);
671 xfree (orig_input_string);
677 bind_read_variable (name, value)
680 #if defined (ARRAY_VARS)
681 if (valid_array_reference (name) == 0)
682 return (bind_variable (name, value, 0));
684 return (assign_array_element (name, value, 0));
685 #else /* !ARRAY_VARS */
686 return bind_variable (name, value, 0);
687 #endif /* !ARRAY_VARS */
690 #if defined (READLINE)
691 static rl_completion_func_t *old_attempted_completion_function;
700 if (bash_readline_initialized == 0)
701 initialize_readline ();
702 old_attempted_completion_function = rl_attempted_completion_function;
703 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
705 rl_attempted_completion_function = old_attempted_completion_function;
709 ret = (char *)xrealloc (ret, len + 2);
715 static int old_delim_ctype;
716 static rl_command_func_t *old_delim_func;
717 static int old_newline_ctype;
718 static rl_command_func_t *old_newline_func;
720 static unsigned char delim_char;
728 if (bash_readline_initialized == 0)
729 initialize_readline ();
730 cmap = rl_get_keymap ();
732 /* Change newline to self-insert */
733 old_newline_ctype = cmap[RETURN].type;
734 old_newline_func = cmap[RETURN].function;
735 cmap[RETURN].type = ISFUNC;
736 cmap[RETURN].function = rl_insert;
738 /* Bind the delimiter character to accept-line. */
739 old_delim_ctype = cmap[c].type;
740 old_delim_func = cmap[c].function;
741 cmap[c].type = ISFUNC;
742 cmap[c].function = rl_newline;
753 cmap = rl_get_keymap ();
755 cmap[RETURN].type = old_newline_ctype;
756 cmap[RETURN].function = old_newline_func;
758 cmap[delim_char].type = old_delim_ctype;
759 cmap[delim_char].function = old_delim_func;