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)
63 #include "bashgetopt.h"
67 #if defined (READLINE)
68 #include "../bashline.h"
69 #include <readline/readline.h>
76 #define issep(c) (strchr (ifs_chars, (c)))
78 extern int interrupt_immediately;
80 #if defined (READLINE)
81 static char *edit_line ();
82 static void set_eol_delim ();
83 static void reset_eol_delim ();
85 static SHELL_VAR *bind_read_variable ();
87 static procenv_t alrmbuf;
88 static SigHandler *old_alrm;
101 set_signal_handler (SIGALRM, old_alrm);
105 /* Read the value of the shell variables whose names follow.
106 The reading is done from the current input stream, whatever
107 that may be. Successive words of the input line are assigned
108 to the variables mentioned in LIST. The last variable in LIST
109 gets the remainder of the words on the line. If no variables
110 are mentioned in LIST, then the default variable is $REPLY. */
115 register char *varname;
116 int size, i, pass_next, saw_escape, eof, opt, retval, code;
117 int input_is_tty, input_is_pipe, unbuffered_read;
118 int raw, edit, tmout, nchars, silent;
119 long timeoutval, ncharsval;
121 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
125 #if defined (ARRAY_VARS)
128 #if defined (READLINE)
133 i = 0; /* Index into the string that we are reading. */
134 raw = edit = 0; /* Not reading raw input by default. */
136 arrayname = prompt = (char *)NULL;
138 #if defined (READLINE)
143 tmout = -1; /* no timeout */
144 nchars = input_is_tty = input_is_pipe = unbuffered_read = 0;
145 delim = '\n'; /* read until newline */
147 reset_internal_getopt ();
148 while ((opt = internal_getopt (list, "erp:a:d:t:n:s")) != -1)
156 prompt = list_optarg;
162 #if defined (READLINE)
166 #if defined (ARRAY_VARS)
168 arrayname = list_optarg;
172 code = legal_number (list_optarg, &timeoutval);
173 if (code == 0 || timeoutval < 0)
175 builtin_error ("%s: invalid timeout specification", list_optarg);
176 return (EXECUTION_FAILURE);
182 code = legal_number (list_optarg, &ncharsval);
183 if (code == 0 || ncharsval < 0)
185 builtin_error ("%s: invalid number specification", list_optarg);
186 return (EXECUTION_FAILURE);
192 delim = *list_optarg;
201 /* `read -t 0 var' returns failure immediately. */
203 return (EXECUTION_FAILURE);
205 /* IF IFS is unset, we use the default of " \t\n". */
206 var = find_variable ("IFS");
207 ifs_chars = var ? value_cell (var) : " \t\n";
208 if (ifs_chars == 0) /* XXX */
209 ifs_chars = ""; /* XXX */
211 input_string = xmalloc (size = 128);
213 begin_unwind_frame ("read_builtin");
214 add_unwind_protect (xfree, input_string);
215 #if defined (READLINE)
216 add_unwind_protect (xfree, rlbuf);
218 interrupt_immediately++;
220 input_is_tty = isatty (0);
221 if (input_is_tty == 0)
223 input_is_pipe = (lseek (0, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
228 /* If the -p, -e or -s flags were given, but input is not coming from the
229 terminal, turn them off. */
230 if ((prompt || edit || silent) && input_is_tty == 0)
232 prompt = (char *)NULL;
236 if (prompt && edit == 0)
238 fprintf (stderr, "%s", prompt);
242 pass_next = 0; /* Non-zero signifies last char was backslash. */
243 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
247 /* Turn off the timeout if stdin is a regular file (e.g. from
248 input redirection). */
249 if ((fstat (0, &tsb) < 0) || S_ISREG (tsb.st_mode))
255 code = setjmp (alrmbuf);
258 run_unwind_frame ("read_builtin");
259 return (EXECUTION_FAILURE);
261 old_alrm = set_signal_handler (SIGALRM, sigalrm);
262 add_unwind_protect (reset_alarm, (char *)NULL);
266 /* If we've been asked to read only NCHARS chars, or we're using some
267 character other than newline to terminate the line, do the right
268 thing to readline or the tty. */
269 if (nchars > 0 || delim != '\n')
271 #if defined (READLINE)
276 unwind_protect_int (rl_num_chars_to_read);
277 rl_num_chars_to_read = nchars;
281 set_eol_delim (delim);
282 add_unwind_protect (reset_eol_delim, (char *)NULL);
294 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
297 else if (silent) /* turn off echo but leave term in canonical mode */
301 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
304 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
308 #if defined (READLINE)
311 if (rlbuf && rlbuf[rlind] == '\0')
318 rlbuf = edit_line (prompt ? prompt : "");
333 retval = zread (0, &c, 1);
335 retval = zreadc (0, &c);
343 #if defined (READLINE)
348 input_string = xrealloc (input_string, size += 128);
350 /* If the next character is to be accepted verbatim, a backslash
351 newline pair still disappears from the input. */
355 i--; /* back up over the CTLESC */
357 input_string[i++] = c;
362 if (c == '\\' && raw == 0)
366 input_string[i++] = CTLESC;
373 if (c == CTLESC || c == CTLNUL)
376 input_string[i++] = CTLESC;
379 input_string[i++] = c;
381 if (nchars > 0 && i >= nchars)
384 input_string[i] = '\0';
389 if (nchars > 0 || delim != '\n')
391 #if defined (READLINE)
395 rl_num_chars_to_read = 0;
407 if (unbuffered_read == 0)
410 interrupt_immediately--;
411 discard_unwind_frame ("read_builtin");
413 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
415 #if defined (ARRAY_VARS)
416 /* If -a was given, take the string read, break it into a list of words,
417 an assign them to `arrayname' in turn. */
420 var = find_variable (arrayname);
422 var = make_new_array_variable (arrayname);
423 else if (array_p (var) == 0)
424 var = convert_var_to_array (var);
426 empty_array (array_cell (var));
428 alist = list_string (input_string, ifs_chars, 0);
431 assign_array_var_from_word_list (var, alist);
432 dispose_words (alist);
434 xfree (input_string);
437 #endif /* ARRAY_VARS */
439 /* If there are no variables, save the text of the line read to the
440 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
441 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
442 same way, but I believe that the difference in behaviors is useful
443 enough to not do it. Without the bash behavior, there is no way
444 to read a line completely without interpretation or modification.
445 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
449 orig_input_string = input_string;
450 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
453 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
458 t = dequote_string (input_string);
459 var = bind_variable ("REPLY", t);
463 var = bind_variable ("REPLY", input_string);
464 VUNSETATTR (var, att_invisible);
466 free (orig_input_string);
473 /* This code implements the Posix.2 spec for splitting the words
474 read and assigning them to variables. */
475 orig_input_string = input_string;
477 /* Remove IFS white space at the beginning of the input string. If
478 $IFS is null, no field splitting is performed. */
479 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
483 for (; list->next; list = list->next)
485 varname = list->word->word;
486 #if defined (ARRAY_VARS)
487 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
489 if (legal_identifier (varname) == 0)
492 builtin_error ("`%s': not a valid identifier", varname);
493 free (orig_input_string);
494 return (EXECUTION_FAILURE);
497 /* If there are more variables than words read from the input,
498 the remaining variables are set to the empty string. */
501 /* This call updates INPUT_STRING. */
502 t = get_word_from_string (&input_string, ifs_chars, &e);
505 /* Don't bother to remove the CTLESC unless we added one
506 somewhere while reading the string. */
509 t1 = dequote_string (t);
510 var = bind_read_variable (varname, t1);
514 var = bind_read_variable (varname, t);
519 var = bind_read_variable (varname, "");
525 free (orig_input_string);
526 return (EXECUTION_FAILURE);
529 stupidly_hack_special_variables (varname);
530 VUNSETATTR (var, att_invisible);
533 /* Now assign the rest of the line to the last variable argument. */
534 #if defined (ARRAY_VARS)
535 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
537 if (legal_identifier (list->word->word) == 0)
540 builtin_error ("`%s': not a valid identifier", list->word->word);
541 free (orig_input_string);
542 return (EXECUTION_FAILURE);
545 /* This has to be done this way rather than using string_list
546 and list_string because Posix.2 says that the last variable gets the
547 remaining words and their intervening separators. */
548 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
552 t = dequote_string (input_string);
553 var = bind_read_variable (list->word->word, t);
557 var = bind_read_variable (list->word->word, input_string);
558 stupidly_hack_special_variables (list->word->word);
560 VUNSETATTR (var, att_invisible);
561 free (orig_input_string);
567 bind_read_variable (name, value)
570 #if defined (ARRAY_VARS)
571 if (valid_array_reference (name) == 0)
574 if (legal_identifier (name) == 0)
576 builtin_error ("`%s': not a valid identifier", name);
577 return ((SHELL_VAR *)NULL);
580 return (bind_variable (name, value));
583 return (do_array_element_assignment (name, value));
585 return bind_variable (name, value);
589 #if defined (READLINE)
597 if (!bash_readline_initialized)
598 initialize_readline ();
603 ret = xrealloc (ret, len + 2);
609 static int old_delim_ctype;
610 static Function *old_delim_func;
611 static int old_newline_ctype;
612 static Function *old_newline_func;
614 static int delim_char;
622 if (bash_readline_initialized == 0)
623 initialize_readline ();
624 cmap = rl_get_keymap ();
626 /* Change newline to self-insert */
627 old_newline_ctype = cmap[RETURN].type;
628 old_newline_func = cmap[RETURN].function;
629 cmap[RETURN].type = ISFUNC;
630 cmap[RETURN].function = rl_insert;
632 /* Bind the delimiter character to accept-line. */
633 old_delim_ctype = cmap[c].type;
634 old_delim_func = cmap[c].function;
635 cmap[c].type = ISFUNC;
636 cmap[c].function = rl_newline;
647 cmap = rl_get_keymap ();
649 cmap[RETURN].type = old_newline_ctype;
650 cmap[RETURN].function = old_newline_func;
652 cmap[delim_char].type = old_delim_ctype;
653 cmap[delim_char].function = old_delim_func;