1 This file is read.def, from which is created read.c.
2 It implements the builtin "read" in Bash.
4 Copyright (C) 1987-2002 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)
71 #include "bashgetopt.h"
75 #if defined (READLINE)
76 #include "../bashline.h"
77 #include <readline/readline.h>
84 extern int interrupt_immediately;
86 #if defined (READLINE)
87 static char *edit_line __P((char *));
88 static void set_eol_delim __P((int));
89 static void reset_eol_delim __P((char *));
91 static SHELL_VAR *bind_read_variable __P((char *, char *));
93 static sighandler sigalrm __P((int));
94 static void reset_alarm __P((void));
96 static procenv_t alrmbuf;
97 static SigHandler *old_alrm;
98 static unsigned char delim;
104 longjmp (alrmbuf, 1);
110 set_signal_handler (SIGALRM, old_alrm);
114 /* Read the value of the shell variables whose names follow.
115 The reading is done from the current input stream, whatever
116 that may be. Successive words of the input line are assigned
117 to the variables mentioned in LIST. The last variable in LIST
118 gets the remainder of the words on the line. If no variables
119 are mentioned in LIST, then the default variable is $REPLY. */
124 register char *varname;
125 int size, i, pass_next, saw_escape, eof, opt, retval, code;
126 int input_is_tty, input_is_pipe, unbuffered_read;
127 int raw, edit, nchars, silent, have_timeout, fd;
131 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
135 #if defined (ARRAY_VARS)
138 #if defined (READLINE)
147 USE_VAR(input_is_pipe);
156 #if defined (READLINE)
162 i = 0; /* Index into the string that we are reading. */
163 raw = edit = 0; /* Not reading raw input by default. */
165 arrayname = prompt = (char *)NULL;
166 fd = 0; /* file descriptor to read from */
168 #if defined (READLINE)
173 tmout = 0; /* no timeout */
174 nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
175 delim = '\n'; /* read until newline */
177 reset_internal_getopt ();
178 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
186 prompt = list_optarg;
192 #if defined (READLINE)
196 #if defined (ARRAY_VARS)
198 arrayname = list_optarg;
202 code = legal_number (list_optarg, &intval);
203 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
205 builtin_error ("%s: invalid timeout specification", list_optarg);
206 return (EXECUTION_FAILURE);
215 code = legal_number (list_optarg, &intval);
216 if (code == 0 || intval < 0 || intval != (int)intval)
218 sh_invalidnum (list_optarg);
219 return (EXECUTION_FAILURE);
225 code = legal_number (list_optarg, &intval);
226 if (code == 0 || intval < 0 || intval != (int)intval)
228 builtin_error ("%s: invalid file descriptor specification", list_optarg);
229 return (EXECUTION_FAILURE);
233 if (sh_validfd (fd) == 0)
235 builtin_error ("%d: invalid file descriptor: %s", fd, strerror (errno));
236 return (EXECUTION_FAILURE);
240 delim = *list_optarg;
249 /* `read -t 0 var' returns failure immediately. XXX - should it test
250 whether input is available with select/FIONREAD, and fail if those
252 if (have_timeout && tmout == 0)
253 return (EXECUTION_FAILURE);
255 /* IF IFS is unset, we use the default of " \t\n". */
256 ifs_chars = getifs ();
257 if (ifs_chars == 0) /* XXX - shouldn't happen */
260 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
262 /* $TMOUT, if set, is the default timeout for read. */
263 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
265 code = legal_number (e, &intval);
266 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
272 begin_unwind_frame ("read_builtin");
274 input_is_tty = isatty (fd);
275 if (input_is_tty == 0)
277 input_is_pipe = (lseek (0, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
282 /* If the -p, -e or -s flags were given, but input is not coming from the
283 terminal, turn them off. */
284 if ((prompt || edit || silent) && input_is_tty == 0)
286 prompt = (char *)NULL;
290 #if defined (READLINE)
292 add_unwind_protect (xfree, rlbuf);
295 if (prompt && edit == 0)
297 fprintf (stderr, "%s", prompt);
301 pass_next = 0; /* Non-zero signifies last char was backslash. */
302 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
306 /* Turn off the timeout if stdin is a regular file (e.g. from
307 input redirection). */
308 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
314 code = setjmp (alrmbuf);
317 run_unwind_frame ("read_builtin");
318 return (EXECUTION_FAILURE);
320 old_alrm = set_signal_handler (SIGALRM, sigalrm);
321 add_unwind_protect (reset_alarm, (char *)NULL);
325 /* If we've been asked to read only NCHARS chars, or we're using some
326 character other than newline to terminate the line, do the right
327 thing to readline or the tty. */
328 if (nchars > 0 || delim != '\n')
330 #if defined (READLINE)
335 unwind_protect_int (rl_num_chars_to_read);
336 rl_num_chars_to_read = nchars;
340 set_eol_delim (delim);
341 add_unwind_protect (reset_eol_delim, (char *)NULL);
353 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
356 else if (silent) /* turn off echo but leave term in canonical mode */
360 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
363 /* This *must* be the top unwind-protect on the stack, so the manipulation
364 of the unwind-protect stack after the realloc() works right. */
365 add_unwind_protect (xfree, input_string);
366 interrupt_immediately++;
368 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
370 #if defined (__CYGWIN__) && defined (O_TEXT)
374 for (eof = retval = 0;;)
376 #if defined (READLINE)
379 if (rlbuf && rlbuf[rlind] == '\0')
386 rlbuf = edit_line (prompt ? prompt : "");
401 retval = zread (fd, &c, 1);
403 retval = zreadc (fd, &c);
411 #if defined (READLINE)
417 input_string = (char *)xrealloc (input_string, size += 128);
418 remove_unwind_protect ();
419 add_unwind_protect (xfree, input_string);
422 /* If the next character is to be accepted verbatim, a backslash
423 newline pair still disappears from the input. */
427 i--; /* back up over the CTLESC */
429 input_string[i++] = c;
434 if (c == '\\' && raw == 0)
438 input_string[i++] = CTLESC;
442 if ((unsigned char)c == delim)
445 if (c == CTLESC || c == CTLNUL)
448 input_string[i++] = CTLESC;
451 input_string[i++] = c;
453 if (nchars > 0 && i >= nchars)
456 input_string[i] = '\0';
461 builtin_error ("read error: %d: %s", fd, strerror (errno));
462 return (EXECUTION_FAILURE);
469 if (nchars > 0 || delim != '\n')
471 #if defined (READLINE)
475 rl_num_chars_to_read = 0;
477 reset_eol_delim ((char *)NULL);
487 if (unbuffered_read == 0)
490 interrupt_immediately--;
491 discard_unwind_frame ("read_builtin");
493 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
495 #if defined (ARRAY_VARS)
496 /* If -a was given, take the string read, break it into a list of words,
497 an assign them to `arrayname' in turn. */
500 var = find_or_make_array_variable (arrayname, 1);
502 return EXECUTION_FAILURE; /* readonly or noassign */
503 array_flush (array_cell (var));
505 alist = list_string (input_string, ifs_chars, 0);
508 assign_array_var_from_word_list (var, alist);
509 dispose_words (alist);
511 xfree (input_string);
514 #endif /* ARRAY_VARS */
516 /* If there are no variables, save the text of the line read to the
517 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
518 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
519 same way, but I believe that the difference in behaviors is useful
520 enough to not do it. Without the bash behavior, there is no way
521 to read a line completely without interpretation or modification
522 unless you mess with $IFS (e.g., setting it to the empty string).
523 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
527 orig_input_string = input_string;
528 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
531 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
536 t = dequote_string (input_string);
537 var = bind_variable ("REPLY", t);
541 var = bind_variable ("REPLY", input_string);
542 VUNSETATTR (var, att_invisible);
548 /* This code implements the Posix.2 spec for splitting the words
549 read and assigning them to variables. */
550 orig_input_string = input_string;
552 /* Remove IFS white space at the beginning of the input string. If
553 $IFS is null, no field splitting is performed. */
554 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
558 for (; list->next; list = list->next)
560 varname = list->word->word;
561 #if defined (ARRAY_VARS)
562 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
564 if (legal_identifier (varname) == 0)
567 sh_invalidid (varname);
568 xfree (orig_input_string);
569 return (EXECUTION_FAILURE);
572 /* If there are more variables than words read from the input,
573 the remaining variables are set to the empty string. */
576 /* This call updates INPUT_STRING. */
577 t = get_word_from_string (&input_string, ifs_chars, &e);
580 /* Don't bother to remove the CTLESC unless we added one
581 somewhere while reading the string. */
584 t1 = dequote_string (t);
585 var = bind_read_variable (varname, t1);
589 var = bind_read_variable (varname, t);
594 var = bind_read_variable (varname, "");
600 xfree (orig_input_string);
601 return (EXECUTION_FAILURE);
604 stupidly_hack_special_variables (varname);
605 VUNSETATTR (var, att_invisible);
608 /* Now assign the rest of the line to the last variable argument. */
609 #if defined (ARRAY_VARS)
610 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
612 if (legal_identifier (list->word->word) == 0)
615 sh_invalidid (list->word->word);
616 xfree (orig_input_string);
617 return (EXECUTION_FAILURE);
620 /* This has to be done this way rather than using string_list
621 and list_string because Posix.2 says that the last variable gets the
622 remaining words and their intervening separators. */
623 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
627 t = dequote_string (input_string);
628 var = bind_read_variable (list->word->word, t);
632 var = bind_read_variable (list->word->word, input_string);
633 stupidly_hack_special_variables (list->word->word);
635 VUNSETATTR (var, att_invisible);
636 xfree (orig_input_string);
642 bind_read_variable (name, value)
645 #if defined (ARRAY_VARS)
646 if (valid_array_reference (name) == 0)
647 return (bind_variable (name, value));
649 return (assign_array_element (name, value));
650 #else /* !ARRAY_VARS */
651 return bind_variable (name, value);
652 #endif /* !ARRAY_VARS */
655 #if defined (READLINE)
656 static rl_completion_func_t *old_attempted_completion_function;
665 if (!bash_readline_initialized)
666 initialize_readline ();
667 old_attempted_completion_function = rl_attempted_completion_function;
668 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
670 rl_attempted_completion_function = old_attempted_completion_function;
674 ret = (char *)xrealloc (ret, len + 2);
680 static int old_delim_ctype;
681 static rl_command_func_t *old_delim_func;
682 static int old_newline_ctype;
683 static rl_command_func_t *old_newline_func;
685 static unsigned char delim_char;
693 if (bash_readline_initialized == 0)
694 initialize_readline ();
695 cmap = rl_get_keymap ();
697 /* Change newline to self-insert */
698 old_newline_ctype = cmap[RETURN].type;
699 old_newline_func = cmap[RETURN].function;
700 cmap[RETURN].type = ISFUNC;
701 cmap[RETURN].function = rl_insert;
703 /* Bind the delimiter character to accept-line. */
704 old_delim_ctype = cmap[c].type;
705 old_delim_func = cmap[c].function;
706 cmap[c].type = ISFUNC;
707 cmap[c].function = rl_newline;
718 cmap = rl_get_keymap ();
720 cmap[RETURN].type = old_newline_ctype;
721 cmap[RETURN].function = old_newline_func;
723 cmap[delim_char].type = old_delim_ctype;
724 cmap[delim_char].function = old_delim_func;