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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION read_builtin
26 $SHORT_DOC read [-r] [-p prompt] [-a array] [-e] [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. The return code is
31 zero, unless end-of-file is encountered. If no NAMEs are supplied, the
32 line read is stored in the REPLY variable. If the -r option is given,
33 this signifies `raw' input, and backslash escaping is disabled. If
34 the `-p' option is supplied, the string supplied as an argument is
35 output without a trailing newline before attempting to read. If -a is
36 supplied, the words read are assigned to sequential indices of ARRAY,
37 starting at zero. If -e is supplied and the shell is interactive,
38 readline is used to obtain the line.
45 #if defined (HAVE_UNISTD_H)
47 # include <sys/types.h>
56 #include "bashgetopt.h"
58 #if defined (READLINE)
59 #include "../bashline.h"
60 #include <readline/readline.h>
67 #define issep(c) (strchr (ifs_chars, (c)))
69 extern int interrupt_immediately;
71 #if defined (READLINE)
72 static char *edit_line ();
74 static SHELL_VAR *bind_read_variable ();
76 /* Read the value of the shell variables whose names follow.
77 The reading is done from the current input stream, whatever
78 that may be. Successive words of the input line are assigned
79 to the variables mentioned in LIST. The last variable in LIST
80 gets the remainder of the words on the line. If no variables
81 are mentioned in LIST, then the default variable is $REPLY. */
86 register char *varname;
87 int size, i, raw, pass_next, saw_escape, eof, opt, retval, edit;
89 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
92 #if defined (ARRAY_VARS)
95 #if defined (READLINE)
100 i = 0; /* Index into the string that we are reading. */
101 raw = edit = 0; /* Not reading raw input by default. */
102 arrayname = prompt = (char *)NULL;
104 #if defined (READLINE)
109 reset_internal_getopt ();
110 while ((opt = internal_getopt (list, "erp:a:")) != -1)
118 prompt = list_optarg;
121 #if defined (READLINE)
125 #if defined (ARRAY_VARS)
127 arrayname = list_optarg;
137 /* IF IFS is unset, we use the default of " \t\n". */
138 var = find_variable ("IFS");
139 ifs_chars = var ? value_cell (var) : " \t\n";
140 if (ifs_chars == 0) /* XXX */
141 ifs_chars = ""; /* XXX */
143 input_string = xmalloc (size = 128);
145 begin_unwind_frame ("read_builtin");
146 add_unwind_protect (xfree, input_string);
147 #if defined (READLINE)
148 add_unwind_protect (xfree, rlbuf);
150 interrupt_immediately++;
152 /* If the -p or -e flags were given, but input is not coming from the
153 terminal, turn them off. */
154 if ((prompt || edit) && (isatty (0) == 0))
156 prompt = (char *)NULL;
160 if (prompt && edit == 0)
162 fprintf (stderr, "%s", prompt);
166 pass_next = 0; /* Non-zero signifies last char was backslash. */
167 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
171 #if defined (READLINE)
174 if (rlbuf && rlbuf[rlind] == '\0')
181 rlbuf = edit_line (prompt ? prompt : "");
195 while (((retval = read (0, &c, 1)) < 0) && errno == EINTR)
203 #if defined (READLINE)
208 input_string = xrealloc (input_string, size += 128);
210 /* If the next character is to be accepted verbatim, a backslash
211 newline pair still disappears from the input. */
215 i--; /* back up over the CTLESC */
217 input_string[i++] = c;
222 if (c == '\\' && raw == 0)
226 input_string[i++] = CTLESC;
233 if (c == CTLESC || c == CTLNUL)
236 input_string[i++] = CTLESC;
239 input_string[i++] = c;
241 input_string[i] = '\0';
243 interrupt_immediately--;
244 discard_unwind_frame ("read_builtin");
246 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
248 #if defined (ARRAY_VARS)
249 /* If -a was given, take the string read, break it into a list of words,
250 an assign them to `arrayname' in turn. */
253 var = find_variable (arrayname);
255 var = make_new_array_variable (arrayname);
256 else if (array_p (var) == 0)
257 var = convert_var_to_array (var);
259 empty_array (array_cell (var));
261 alist = list_string (input_string, ifs_chars, 0);
264 assign_array_var_from_word_list (var, alist);
265 dispose_words (alist);
267 xfree (input_string);
270 #endif /* ARRAY_VARS */
272 /* If there are no variables, save the text of the line read to the
273 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
274 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
275 same way, but I believe that the difference in behaviors is useful
276 enough to not do it. Without the bash behavior, there is no way
277 to read a line completely without interpretation or modification.
278 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
282 orig_input_string = input_string;
283 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
286 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
291 t = dequote_string (input_string);
292 var = bind_variable ("REPLY", t);
296 var = bind_variable ("REPLY", input_string);
297 var->attributes &= ~att_invisible;
299 free (orig_input_string);
306 /* This code implements the Posix.2 spec for splitting the words
307 read and assigning them to variables. */
308 orig_input_string = input_string;
310 /* Remove IFS white space at the beginning of the input string. If
311 $IFS is null, no field splitting is performed. */
312 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
316 for (; list->next; list = list->next)
318 varname = list->word->word;
319 #if defined (ARRAY_VARS)
320 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
322 if (legal_identifier (varname) == 0)
325 builtin_error ("`%s': not a valid identifier", varname);
326 free (orig_input_string);
327 return (EXECUTION_FAILURE);
330 /* If there are more variables than words read from the input,
331 the remaining variables are set to the empty string. */
334 /* This call updates INPUT_STRING. */
335 t = get_word_from_string (&input_string, ifs_chars, &e);
338 /* Don't bother to remove the CTLESC unless we added one
339 somewhere while reading the string. */
342 t1 = dequote_string (t);
343 var = bind_read_variable (varname, t1);
347 var = bind_read_variable (varname, t);
352 var = bind_read_variable (varname, "");
358 free (orig_input_string);
359 return (EXECUTION_FAILURE);
362 stupidly_hack_special_variables (varname);
363 var->attributes &= ~att_invisible;
366 /* Now assign the rest of the line to the last variable argument. */
367 #if defined (ARRAY_VARS)
368 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
370 if (legal_identifier (list->word->word) == 0)
373 builtin_error ("`%s': not a valid identifier", list->word->word);
374 free (orig_input_string);
375 return (EXECUTION_FAILURE);
378 /* This has to be done this way rather than using string_list
379 and list_string because Posix.2 says that the last variable gets the
380 remaining words and their intervening separators. */
381 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
385 t = dequote_string (input_string);
386 var = bind_read_variable (list->word->word, t);
390 var = bind_read_variable (list->word->word, input_string);
391 stupidly_hack_special_variables (list->word->word);
393 var->attributes &= ~att_invisible;
394 free (orig_input_string);
400 bind_read_variable (name, value)
403 #if defined (ARRAY_VARS)
404 if (valid_array_reference (name) == 0)
407 if (legal_identifier (name) == 0)
409 builtin_error ("`%s': not a valid identifier", name);
410 return ((SHELL_VAR *)NULL);
413 return (bind_variable (name, value));
416 return (do_array_element_assignment (name, value));
418 return bind_variable (name, value);
422 #if defined (READLINE)
430 if (!bash_readline_initialized)
431 initialize_readline ();
436 ret = xrealloc (ret, len + 2);