1 This file is read.def, from which is created read.c.
2 It implements the builtin "read" in Bash.
4 Copyright (C) 1987, 1989, 1991 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] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
27 One line is read from the standard input, and the first word is
28 assigned to the first NAME, the second word to the second NAME, and so
29 on, with leftover words assigned to the last NAME. Only the characters
30 found in $IFS are recognized as word delimiters. If no NAMEs are supplied,
31 the line read is stored in the REPLY variable. If the -r option is given,
32 this signifies `raw' input, and backslash escaping is disabled. The
33 -d option causes read to continue until the first character of DELIM is
34 read, rather than newline. If the `-p' option is supplied, the string
35 PROMPT is output without a trailing newline before attempting to read.
36 If -a is supplied, the words read are assigned to sequential indices of
37 ARRAY, starting at zero. If -e is supplied and the shell is interactive,
38 readline is used to obtain the line. If -n is supplied with a non-zero
39 NCHARS argument, read returns after NCHARS characters have been read.
40 The -s option causes input coming from a terminal to not be echoed.
42 The -t option causes read to time out and return failure if a complete line
43 of input is not read within TIMEOUT seconds. The return code is zero,
44 unless end-of-file is encountered or read times out.
49 #include "bashtypes.h"
50 #include "posixstat.h"
54 #if defined (HAVE_UNISTD_H)
68 #include "bashgetopt.h"
72 #if defined (READLINE)
73 #include "../bashline.h"
74 #include <readline/readline.h>
81 #define issep(c) (strchr (ifs_chars, (c)))
83 extern int interrupt_immediately;
85 #if defined (READLINE)
86 static char *edit_line __P((char *));
87 static void set_eol_delim __P((int));
88 static void reset_eol_delim __P((char *));
90 static SHELL_VAR *bind_read_variable __P((char *, char *));
92 static sighandler sigalrm __P((int));
93 static void reset_alarm __P((void));
95 static procenv_t alrmbuf;
96 static SigHandler *old_alrm;
97 static unsigned char delim;
103 longjmp (alrmbuf, 1);
109 set_signal_handler (SIGALRM, old_alrm);
113 /* Read the value of the shell variables whose names follow.
114 The reading is done from the current input stream, whatever
115 that may be. Successive words of the input line are assigned
116 to the variables mentioned in LIST. The last variable in LIST
117 gets the remainder of the words on the line. If no variables
118 are mentioned in LIST, then the default variable is $REPLY. */
123 register char *varname;
124 int size, i, pass_next, saw_escape, eof, opt, retval, code;
125 int input_is_tty, input_is_pipe, unbuffered_read;
126 int raw, edit, nchars, silent, have_timeout;
128 long timeoutval, ncharsval;
130 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
134 #if defined (ARRAY_VARS)
137 #if defined (READLINE)
146 USE_VAR(input_is_pipe);
155 #if defined (READLINE)
161 i = 0; /* Index into the string that we are reading. */
162 raw = edit = 0; /* Not reading raw input by default. */
164 arrayname = prompt = (char *)NULL;
166 #if defined (READLINE)
171 tmout = 0; /* no timeout */
172 nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
173 delim = '\n'; /* read until newline */
175 reset_internal_getopt ();
176 while ((opt = internal_getopt (list, "erp:a:d:t:n:s")) != -1)
184 prompt = list_optarg;
190 #if defined (READLINE)
194 #if defined (ARRAY_VARS)
196 arrayname = list_optarg;
200 code = legal_number (list_optarg, &timeoutval);
201 if (code == 0 || timeoutval < 0 || timeoutval != (unsigned int)timeoutval)
203 builtin_error ("%s: invalid timeout specification", list_optarg);
204 return (EXECUTION_FAILURE);
213 code = legal_number (list_optarg, &ncharsval);
214 if (code == 0 || ncharsval < 0 || ncharsval != (int)ncharsval)
216 builtin_error ("%s: invalid number specification", list_optarg);
217 return (EXECUTION_FAILURE);
223 delim = *list_optarg;
232 /* `read -t 0 var' returns failure immediately. */
233 if (have_timeout && tmout == 0)
234 return (EXECUTION_FAILURE);
236 /* IF IFS is unset, we use the default of " \t\n". */
237 var = find_variable ("IFS");
238 ifs_chars = var ? value_cell (var) : " \t\n";
239 if (ifs_chars == 0) /* XXX */
240 ifs_chars = ""; /* XXX */
242 input_string = (char *)xmalloc (size = 128);
244 begin_unwind_frame ("read_builtin");
245 #if defined (READLINE)
246 add_unwind_protect (xfree, rlbuf);
249 input_is_tty = isatty (0);
250 if (input_is_tty == 0)
252 input_is_pipe = (lseek (0, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
257 /* If the -p, -e or -s flags were given, but input is not coming from the
258 terminal, turn them off. */
259 if ((prompt || edit || silent) && input_is_tty == 0)
261 prompt = (char *)NULL;
265 if (prompt && edit == 0)
267 fprintf (stderr, "%s", prompt);
271 pass_next = 0; /* Non-zero signifies last char was backslash. */
272 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
276 /* Turn off the timeout if stdin is a regular file (e.g. from
277 input redirection). */
278 if ((fstat (0, &tsb) < 0) || S_ISREG (tsb.st_mode))
284 code = setjmp (alrmbuf);
287 run_unwind_frame ("read_builtin");
288 return (EXECUTION_FAILURE);
290 old_alrm = set_signal_handler (SIGALRM, sigalrm);
291 add_unwind_protect (reset_alarm, (char *)NULL);
295 /* If we've been asked to read only NCHARS chars, or we're using some
296 character other than newline to terminate the line, do the right
297 thing to readline or the tty. */
298 if (nchars > 0 || delim != '\n')
300 #if defined (READLINE)
305 unwind_protect_int (rl_num_chars_to_read);
306 rl_num_chars_to_read = nchars;
310 set_eol_delim (delim);
311 add_unwind_protect (reset_eol_delim, (char *)NULL);
323 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
326 else if (silent) /* turn off echo but leave term in canonical mode */
330 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
333 /* This *must* be the top unwind-protect on the stack, so the manipulation
334 of the unwind-protect stack after the realloc() works right. */
335 add_unwind_protect (xfree, input_string);
336 interrupt_immediately++;
338 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
340 #if defined (__CYGWIN__) && defined (O_TEXT)
346 #if defined (READLINE)
349 if (rlbuf && rlbuf[rlind] == '\0')
356 rlbuf = edit_line (prompt ? prompt : "");
371 retval = zread (0, &c, 1);
373 retval = zreadc (0, &c);
381 #if defined (READLINE)
387 input_string = (char *)xrealloc (input_string, size += 128);
388 remove_unwind_protect ();
389 add_unwind_protect (xfree, input_string);
392 /* If the next character is to be accepted verbatim, a backslash
393 newline pair still disappears from the input. */
397 i--; /* back up over the CTLESC */
399 input_string[i++] = c;
404 if (c == '\\' && raw == 0)
408 input_string[i++] = CTLESC;
412 if ((unsigned char)c == delim)
415 if (c == CTLESC || c == CTLNUL)
418 input_string[i++] = CTLESC;
421 input_string[i++] = c;
423 if (nchars > 0 && i >= nchars)
426 input_string[i] = '\0';
431 if (nchars > 0 || delim != '\n')
433 #if defined (READLINE)
437 rl_num_chars_to_read = 0;
439 reset_eol_delim ((char *)NULL);
449 if (unbuffered_read == 0)
452 interrupt_immediately--;
453 discard_unwind_frame ("read_builtin");
455 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
457 #if defined (ARRAY_VARS)
458 /* If -a was given, take the string read, break it into a list of words,
459 an assign them to `arrayname' in turn. */
462 var = find_or_make_array_variable (arrayname, 1);
464 return EXECUTION_FAILURE; /* readonly or noassign */
465 empty_array (array_cell (var));
467 alist = list_string (input_string, ifs_chars, 0);
470 assign_array_var_from_word_list (var, alist);
471 dispose_words (alist);
473 xfree (input_string);
476 #endif /* ARRAY_VARS */
478 /* If there are no variables, save the text of the line read to the
479 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
480 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
481 same way, but I believe that the difference in behaviors is useful
482 enough to not do it. Without the bash behavior, there is no way
483 to read a line completely without interpretation or modification
484 unless you mess with $IFS (e.g., setting it to the empty string).
485 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
489 orig_input_string = input_string;
490 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
493 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
498 t = dequote_string (input_string);
499 var = bind_variable ("REPLY", t);
503 var = bind_variable ("REPLY", input_string);
504 VUNSETATTR (var, att_invisible);
510 /* This code implements the Posix.2 spec for splitting the words
511 read and assigning them to variables. */
512 orig_input_string = input_string;
514 /* Remove IFS white space at the beginning of the input string. If
515 $IFS is null, no field splitting is performed. */
516 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
520 for (; list->next; list = list->next)
522 varname = list->word->word;
523 #if defined (ARRAY_VARS)
524 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
526 if (legal_identifier (varname) == 0)
529 builtin_error ("`%s': not a valid identifier", varname);
530 xfree (orig_input_string);
531 return (EXECUTION_FAILURE);
534 /* If there are more variables than words read from the input,
535 the remaining variables are set to the empty string. */
538 /* This call updates INPUT_STRING. */
539 t = get_word_from_string (&input_string, ifs_chars, &e);
542 /* Don't bother to remove the CTLESC unless we added one
543 somewhere while reading the string. */
546 t1 = dequote_string (t);
547 var = bind_read_variable (varname, t1);
551 var = bind_read_variable (varname, t);
556 var = bind_read_variable (varname, "");
562 xfree (orig_input_string);
563 return (EXECUTION_FAILURE);
566 stupidly_hack_special_variables (varname);
567 VUNSETATTR (var, att_invisible);
570 /* Now assign the rest of the line to the last variable argument. */
571 #if defined (ARRAY_VARS)
572 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
574 if (legal_identifier (list->word->word) == 0)
577 builtin_error ("`%s': not a valid identifier", list->word->word);
578 xfree (orig_input_string);
579 return (EXECUTION_FAILURE);
582 /* This has to be done this way rather than using string_list
583 and list_string because Posix.2 says that the last variable gets the
584 remaining words and their intervening separators. */
585 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
589 t = dequote_string (input_string);
590 var = bind_read_variable (list->word->word, t);
594 var = bind_read_variable (list->word->word, input_string);
595 stupidly_hack_special_variables (list->word->word);
597 VUNSETATTR (var, att_invisible);
598 xfree (orig_input_string);
604 bind_read_variable (name, value)
607 #if defined (ARRAY_VARS)
608 if (valid_array_reference (name) == 0)
609 return (bind_variable (name, value));
611 return (assign_array_element (name, value));
612 #else /* !ARRAY_VARS */
613 return bind_variable (name, value);
614 #endif /* !ARRAY_VARS */
617 #if defined (READLINE)
625 if (!bash_readline_initialized)
626 initialize_readline ();
631 ret = (char *)xrealloc (ret, len + 2);
637 static int old_delim_ctype;
638 static rl_command_func_t *old_delim_func;
639 static int old_newline_ctype;
640 static rl_command_func_t *old_newline_func;
642 static unsigned char delim_char;
650 if (bash_readline_initialized == 0)
651 initialize_readline ();
652 cmap = rl_get_keymap ();
654 /* Change newline to self-insert */
655 old_newline_ctype = cmap[RETURN].type;
656 old_newline_func = cmap[RETURN].function;
657 cmap[RETURN].type = ISFUNC;
658 cmap[RETURN].function = rl_insert;
660 /* Bind the delimiter character to accept-line. */
661 old_delim_ctype = cmap[c].type;
662 old_delim_func = cmap[c].function;
663 cmap[c].type = ISFUNC;
664 cmap[c].function = rl_newline;
675 cmap = rl_get_keymap ();
677 cmap[RETURN].type = old_newline_ctype;
678 cmap[RETURN].function = old_newline_func;
680 cmap[delim_char].type = old_delim_ctype;
681 cmap[delim_char].function = old_delim_func;