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 : "");
194 while (((retval = read (0, &c, 1)) < 0) && errno == EINTR)
203 input_string = xrealloc (input_string, size += 128);
205 /* If the next character is to be accepted verbatim, a backslash
206 newline pair still disappears from the input. */
210 i--; /* back up over the CTLESC */
212 input_string[i++] = c;
217 if (c == '\\' && raw == 0)
221 input_string[i++] = CTLESC;
228 if (c == CTLESC || c == CTLNUL)
231 input_string[i++] = CTLESC;
234 input_string[i++] = c;
236 input_string[i] = '\0';
238 interrupt_immediately--;
239 discard_unwind_frame ("read_builtin");
241 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
243 #if defined (ARRAY_VARS)
244 /* If -a was given, take the string read, break it into a list of words,
245 an assign them to `arrayname' in turn. */
248 var = find_variable (arrayname);
250 var = make_new_array_variable (arrayname);
251 else if (array_p (var) == 0)
252 var = convert_var_to_array (var);
254 empty_array (array_cell (var));
256 alist = list_string (input_string, ifs_chars, 0);
259 assign_array_var_from_word_list (var, alist);
260 dispose_words (alist);
262 xfree (input_string);
265 #endif /* ARRAY_VARS */
267 /* If there are no variables, save the text of the line read to the
268 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
269 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
270 same way, but I believe that the difference in behaviors is useful
271 enough to not do it. Without the bash behavior, there is no way
272 to read a line completely without interpretation or modification.
273 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
277 orig_input_string = input_string;
278 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
281 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
286 t = dequote_string (input_string);
287 var = bind_variable ("REPLY", t);
291 var = bind_variable ("REPLY", input_string);
292 var->attributes &= ~att_invisible;
294 free (orig_input_string);
301 /* This code implements the Posix.2 spec for splitting the words
302 read and assigning them to variables. */
303 orig_input_string = input_string;
305 /* Remove IFS white space at the beginning of the input string. If
306 $IFS is null, no field splitting is performed. */
307 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
311 for (; list->next; list = list->next)
313 varname = list->word->word;
314 #if defined (ARRAY_VARS)
315 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
317 if (legal_identifier (varname) == 0)
320 builtin_error ("`%s': not a valid identifier", varname);
321 free (orig_input_string);
322 return (EXECUTION_FAILURE);
325 /* If there are more variables than words read from the input,
326 the remaining variables are set to the empty string. */
329 /* This call updates INPUT_STRING. */
330 t = get_word_from_string (&input_string, ifs_chars, &e);
333 /* Don't bother to remove the CTLESC unless we added one
334 somewhere while reading the string. */
337 t1 = dequote_string (t);
338 var = bind_read_variable (varname, t1);
342 var = bind_read_variable (varname, t);
347 var = bind_read_variable (varname, "");
353 free (orig_input_string);
354 return (EXECUTION_FAILURE);
357 stupidly_hack_special_variables (varname);
358 var->attributes &= ~att_invisible;
361 /* Now assign the rest of the line to the last variable argument. */
362 #if defined (ARRAY_VARS)
363 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
365 if (legal_identifier (list->word->word) == 0)
368 builtin_error ("`%s': not a valid identifier", list->word->word);
369 free (orig_input_string);
370 return (EXECUTION_FAILURE);
373 /* This has to be done this way rather than using string_list
374 and list_string because Posix.2 says that the last variable gets the
375 remaining words and their intervening separators. */
376 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
380 t = dequote_string (input_string);
381 var = bind_read_variable (list->word->word, t);
385 var = bind_read_variable (list->word->word, input_string);
386 stupidly_hack_special_variables (list->word->word);
388 var->attributes &= ~att_invisible;
389 free (orig_input_string);
395 bind_read_variable (name, value)
398 #if defined (ARRAY_VARS)
399 if (valid_array_reference (name) == 0)
402 if (legal_identifier (name) == 0)
404 builtin_error ("`%s': not a valid identifier", name);
405 return ((SHELL_VAR *)NULL);
408 return (bind_variable (name, value));
411 return (do_array_element_assignment (name, value));
413 return bind_variable (name, value);
417 #if defined (READLINE)
425 if (!bash_readline_initialized)
426 initialize_readline ();
431 ret = xrealloc (ret, len + 2);