1 This file is read.def, from which is created read.c.
2 It implements the builtin "read" in Bash.
4 Copyright (C) 1987-2004 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>
86 extern int interrupt_immediately;
88 #if defined (READLINE)
89 static char *edit_line __P((char *));
90 static void set_eol_delim __P((int));
91 static void reset_eol_delim __P((char *));
93 static SHELL_VAR *bind_read_variable __P((char *, char *));
95 static sighandler sigalrm __P((int));
96 static void reset_alarm __P((void));
98 static procenv_t alrmbuf;
99 static SigHandler *old_alrm;
100 static unsigned char delim;
106 longjmp (alrmbuf, 1);
112 set_signal_handler (SIGALRM, old_alrm);
116 /* Read the value of the shell variables whose names follow.
117 The reading is done from the current input stream, whatever
118 that may be. Successive words of the input line are assigned
119 to the variables mentioned in LIST. The last variable in LIST
120 gets the remainder of the words on the line. If no variables
121 are mentioned in LIST, then the default variable is $REPLY. */
126 register char *varname;
127 int size, i, pass_next, saw_escape, eof, opt, retval, code;
128 int input_is_tty, input_is_pipe, unbuffered_read;
129 int raw, edit, nchars, silent, have_timeout, fd;
133 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
137 #if defined (ARRAY_VARS)
140 #if defined (READLINE)
149 USE_VAR(input_is_pipe);
158 #if defined (READLINE)
164 i = 0; /* Index into the string that we are reading. */
165 raw = edit = 0; /* Not reading raw input by default. */
167 arrayname = prompt = (char *)NULL;
168 fd = 0; /* file descriptor to read from */
170 #if defined (READLINE)
175 tmout = 0; /* no timeout */
176 nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
177 delim = '\n'; /* read until newline */
179 reset_internal_getopt ();
180 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
188 prompt = list_optarg;
194 #if defined (READLINE)
198 #if defined (ARRAY_VARS)
200 arrayname = list_optarg;
204 code = legal_number (list_optarg, &intval);
205 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
207 builtin_error (_("%s: invalid timeout specification"), list_optarg);
208 return (EXECUTION_FAILURE);
217 code = legal_number (list_optarg, &intval);
218 if (code == 0 || intval < 0 || intval != (int)intval)
220 sh_invalidnum (list_optarg);
221 return (EXECUTION_FAILURE);
227 code = legal_number (list_optarg, &intval);
228 if (code == 0 || intval < 0 || intval != (int)intval)
230 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
231 return (EXECUTION_FAILURE);
235 if (sh_validfd (fd) == 0)
237 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
238 return (EXECUTION_FAILURE);
242 delim = *list_optarg;
251 /* `read -t 0 var' returns failure immediately. XXX - should it test
252 whether input is available with select/FIONREAD, and fail if those
254 if (have_timeout && tmout == 0)
255 return (EXECUTION_FAILURE);
257 /* IF IFS is unset, we use the default of " \t\n". */
258 ifs_chars = getifs ();
259 if (ifs_chars == 0) /* XXX - shouldn't happen */
262 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
264 /* $TMOUT, if set, is the default timeout for read. */
265 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
267 code = legal_number (e, &intval);
268 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
274 begin_unwind_frame ("read_builtin");
276 input_is_tty = isatty (fd);
277 if (input_is_tty == 0)
279 input_is_pipe = (lseek (0, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
284 /* If the -p, -e or -s flags were given, but input is not coming from the
285 terminal, turn them off. */
286 if ((prompt || edit || silent) && input_is_tty == 0)
288 prompt = (char *)NULL;
292 #if defined (READLINE)
294 add_unwind_protect (xfree, rlbuf);
297 if (prompt && edit == 0)
299 fprintf (stderr, "%s", prompt);
303 pass_next = 0; /* Non-zero signifies last char was backslash. */
304 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
308 /* Turn off the timeout if stdin is a regular file (e.g. from
309 input redirection). */
310 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
316 code = setjmp (alrmbuf);
319 run_unwind_frame ("read_builtin");
320 return (EXECUTION_FAILURE);
322 old_alrm = set_signal_handler (SIGALRM, sigalrm);
323 add_unwind_protect (reset_alarm, (char *)NULL);
327 /* If we've been asked to read only NCHARS chars, or we're using some
328 character other than newline to terminate the line, do the right
329 thing to readline or the tty. */
330 if (nchars > 0 || delim != '\n')
332 #if defined (READLINE)
337 unwind_protect_int (rl_num_chars_to_read);
338 rl_num_chars_to_read = nchars;
342 set_eol_delim (delim);
343 add_unwind_protect (reset_eol_delim, (char *)NULL);
355 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
358 else if (silent) /* turn off echo but leave term in canonical mode */
362 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
365 /* This *must* be the top unwind-protect on the stack, so the manipulation
366 of the unwind-protect stack after the realloc() works right. */
367 add_unwind_protect (xfree, input_string);
368 interrupt_immediately++;
370 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
372 #if defined (__CYGWIN__) && defined (O_TEXT)
376 for (eof = retval = 0;;)
378 #if defined (READLINE)
381 if (rlbuf && rlbuf[rlind] == '\0')
388 rlbuf = edit_line (prompt ? prompt : "");
403 retval = zread (fd, &c, 1);
405 retval = zreadc (fd, &c);
413 #if defined (READLINE)
419 input_string = (char *)xrealloc (input_string, size += 128);
420 remove_unwind_protect ();
421 add_unwind_protect (xfree, input_string);
424 /* If the next character is to be accepted verbatim, a backslash
425 newline pair still disappears from the input. */
429 i--; /* back up over the CTLESC */
431 input_string[i++] = c;
436 if (c == '\\' && raw == 0)
440 input_string[i++] = CTLESC;
444 if ((unsigned char)c == delim)
447 if (c == CTLESC || c == CTLNUL)
450 input_string[i++] = CTLESC;
453 input_string[i++] = c;
455 if (nchars > 0 && i >= nchars)
458 input_string[i] = '\0';
463 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
464 return (EXECUTION_FAILURE);
471 if (nchars > 0 || delim != '\n')
473 #if defined (READLINE)
477 rl_num_chars_to_read = 0;
479 reset_eol_delim ((char *)NULL);
489 if (unbuffered_read == 0)
492 interrupt_immediately--;
493 discard_unwind_frame ("read_builtin");
495 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
497 #if defined (ARRAY_VARS)
498 /* If -a was given, take the string read, break it into a list of words,
499 an assign them to `arrayname' in turn. */
502 if (legal_identifier (arrayname) == 0)
504 sh_invalidid (arrayname);
505 xfree (input_string);
506 return (EXECUTION_FAILURE);
509 var = find_or_make_array_variable (arrayname, 1);
511 return EXECUTION_FAILURE; /* readonly or noassign */
512 array_flush (array_cell (var));
514 alist = list_string (input_string, ifs_chars, 0);
517 word_list_remove_quoted_nulls (alist);
518 assign_array_var_from_word_list (var, alist);
519 dispose_words (alist);
521 xfree (input_string);
524 #endif /* ARRAY_VARS */
526 /* If there are no variables, save the text of the line read to the
527 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
528 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
529 same way, but I believe that the difference in behaviors is useful
530 enough to not do it. Without the bash behavior, there is no way
531 to read a line completely without interpretation or modification
532 unless you mess with $IFS (e.g., setting it to the empty string).
533 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
537 orig_input_string = input_string;
538 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
541 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
546 t = dequote_string (input_string);
547 var = bind_variable ("REPLY", t);
551 var = bind_variable ("REPLY", input_string);
552 VUNSETATTR (var, att_invisible);
558 /* This code implements the Posix.2 spec for splitting the words
559 read and assigning them to variables. */
560 orig_input_string = input_string;
562 /* Remove IFS white space at the beginning of the input string. If
563 $IFS is null, no field splitting is performed. */
564 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
568 for (; list->next; list = list->next)
570 varname = list->word->word;
571 #if defined (ARRAY_VARS)
572 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
574 if (legal_identifier (varname) == 0)
577 sh_invalidid (varname);
578 xfree (orig_input_string);
579 return (EXECUTION_FAILURE);
582 /* If there are more variables than words read from the input,
583 the remaining variables are set to the empty string. */
586 /* This call updates INPUT_STRING. */
587 t = get_word_from_string (&input_string, ifs_chars, &e);
590 /* Don't bother to remove the CTLESC unless we added one
591 somewhere while reading the string. */
594 t1 = dequote_string (t);
595 var = bind_read_variable (varname, t1);
599 var = bind_read_variable (varname, t);
604 var = bind_read_variable (varname, "");
610 xfree (orig_input_string);
611 return (EXECUTION_FAILURE);
614 stupidly_hack_special_variables (varname);
615 VUNSETATTR (var, att_invisible);
618 /* Now assign the rest of the line to the last variable argument. */
619 #if defined (ARRAY_VARS)
620 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
622 if (legal_identifier (list->word->word) == 0)
625 sh_invalidid (list->word->word);
626 xfree (orig_input_string);
627 return (EXECUTION_FAILURE);
630 /* This has to be done this way rather than using string_list
631 and list_string because Posix.2 says that the last variable gets the
632 remaining words and their intervening separators. */
633 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
637 t = dequote_string (input_string);
638 var = bind_read_variable (list->word->word, t);
642 var = bind_read_variable (list->word->word, input_string);
643 stupidly_hack_special_variables (list->word->word);
645 VUNSETATTR (var, att_invisible);
646 xfree (orig_input_string);
652 bind_read_variable (name, value)
655 #if defined (ARRAY_VARS)
656 if (valid_array_reference (name) == 0)
657 return (bind_variable (name, value));
659 return (assign_array_element (name, value));
660 #else /* !ARRAY_VARS */
661 return bind_variable (name, value);
662 #endif /* !ARRAY_VARS */
665 #if defined (READLINE)
666 static rl_completion_func_t *old_attempted_completion_function;
675 if (!bash_readline_initialized)
676 initialize_readline ();
677 old_attempted_completion_function = rl_attempted_completion_function;
678 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
680 rl_attempted_completion_function = old_attempted_completion_function;
684 ret = (char *)xrealloc (ret, len + 2);
690 static int old_delim_ctype;
691 static rl_command_func_t *old_delim_func;
692 static int old_newline_ctype;
693 static rl_command_func_t *old_newline_func;
695 static unsigned char delim_char;
703 if (bash_readline_initialized == 0)
704 initialize_readline ();
705 cmap = rl_get_keymap ();
707 /* Change newline to self-insert */
708 old_newline_ctype = cmap[RETURN].type;
709 old_newline_func = cmap[RETURN].function;
710 cmap[RETURN].type = ISFUNC;
711 cmap[RETURN].function = rl_insert;
713 /* Bind the delimiter character to accept-line. */
714 old_delim_ctype = cmap[c].type;
715 old_delim_func = cmap[c].function;
716 cmap[c].type = ISFUNC;
717 cmap[c].function = rl_newline;
728 cmap = rl_get_keymap ();
730 cmap[RETURN].type = old_newline_ctype;
731 cmap[RETURN].function = old_newline_func;
733 cmap[delim_char].type = old_delim_ctype;
734 cmap[delim_char].function = old_delim_func;