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)
51 #include "bashgetopt.h"
53 #if defined (READLINE)
54 #include "../bashline.h"
55 #include <readline/readline.h>
58 #define issep(c) (strchr (ifs_chars, (c)))
60 extern int interrupt_immediately;
62 #if defined (READLINE)
63 static char *edit_line ();
65 static SHELL_VAR *bind_read_variable ();
67 /* Read the value of the shell variables whose names follow.
68 The reading is done from the current input stream, whatever
69 that may be. Successive words of the input line are assigned
70 to the variables mentioned in LIST. The last variable in LIST
71 gets the remainder of the words on the line. If no variables
72 are mentioned in LIST, then the default variable is $REPLY. */
77 register char *varname;
78 int size, i, raw, pass_next, saw_escape, eof, opt, retval, edit;
80 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
83 #if defined (ARRAY_VARS)
86 #if defined (READLINE)
91 i = 0; /* Index into the string that we are reading. */
92 raw = edit = 0; /* Not reading raw input by default. */
93 arrayname = prompt = (char *)NULL;
95 #if defined (READLINE)
100 reset_internal_getopt ();
101 while ((opt = internal_getopt (list, "erp:a:")) != -1)
109 prompt = list_optarg;
112 #if defined (READLINE)
116 #if defined (ARRAY_VARS)
118 arrayname = list_optarg;
128 /* IF IFS is unset, we use the default of " \t\n". */
129 var = find_variable ("IFS");
130 ifs_chars = var ? value_cell (var) : " \t\n";
131 if (ifs_chars == 0) /* XXX */
132 ifs_chars = ""; /* XXX */
134 input_string = xmalloc (size = 128);
136 begin_unwind_frame ("read_builtin");
137 add_unwind_protect (xfree, input_string);
138 #if defined (READLINE)
139 add_unwind_protect (xfree, rlbuf);
141 interrupt_immediately++;
143 /* If the -p or -e flags were given, but input is not coming from the
144 terminal, turn them off. */
145 if ((prompt || edit) && (isatty (0) == 0))
147 prompt = (char *)NULL;
151 if (prompt && edit == 0)
153 fprintf (stderr, "%s", prompt);
157 pass_next = 0; /* Non-zero signifies last char was backslash. */
158 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
162 #if defined (READLINE)
165 if (rlbuf && rlbuf[rlind] == '\0')
172 rlbuf = edit_line (prompt ? prompt : "");
184 if (read (0, &c, 1) != 1)
191 input_string = xrealloc (input_string, size += 128);
193 /* If the next character is to be accepted verbatim, a backslash
194 newline pair still disappears from the input. */
198 i--; /* back up over the CTLESC */
200 input_string[i++] = c;
205 if (c == '\\' && raw == 0)
209 input_string[i++] = CTLESC;
216 if (c == CTLESC || c == CTLNUL)
219 input_string[i++] = CTLESC;
222 input_string[i++] = c;
224 input_string[i] = '\0';
226 interrupt_immediately--;
227 discard_unwind_frame ("read_builtin");
229 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
231 #if defined (ARRAY_VARS)
232 /* If -a was given, take the string read, break it into a list of words,
233 an assign them to `arrayname' in turn. */
236 var = find_variable (arrayname);
238 var = make_new_array_variable (arrayname);
239 else if (array_p (var) == 0)
240 var = convert_var_to_array (var);
242 empty_array (array_cell (var));
244 alist = list_string (input_string, ifs_chars, 0);
247 assign_array_var_from_word_list (var, alist);
248 dispose_words (alist);
253 #endif /* ARRAY_VARS */
259 t = dequote_string (input_string);
260 var = bind_variable ("REPLY", t);
264 var = bind_variable ("REPLY", input_string);
265 var->attributes &= ~att_invisible;
270 /* This code implements the Posix.2 spec for splitting the words
271 read and assigning them to variables. */
272 orig_input_string = input_string;
274 /* Remove IFS white space at the beginning of the input string. If
275 $IFS is null, no field splitting is performed. */
276 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
280 for (; list->next; list = list->next)
282 varname = list->word->word;
283 #if defined (ARRAY_VARS)
284 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
286 if (legal_identifier (varname) == 0)
289 builtin_error ("`%s': not a valid identifier", varname);
290 free (orig_input_string);
291 return (EXECUTION_FAILURE);
294 /* If there are more variables than words read from the input,
295 the remaining variables are set to the empty string. */
298 /* This call updates INPUT_STRING. */
299 t = get_word_from_string (&input_string, ifs_chars, &e);
302 /* Don't bother to remove the CTLESC unless we added one
303 somewhere while reading the string. */
306 t1 = dequote_string (t);
307 var = bind_read_variable (varname, t1);
311 var = bind_read_variable (varname, t);
316 var = bind_read_variable (varname, "");
322 free (orig_input_string);
323 return (EXECUTION_FAILURE);
326 stupidly_hack_special_variables (varname);
327 var->attributes &= ~att_invisible;
330 /* Now assign the rest of the line to the last variable argument. */
331 #if defined (ARRAY_VARS)
332 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
334 if (legal_identifier (list->word->word) == 0)
337 builtin_error ("`%s': not a valid identifier", list->word->word);
338 free (orig_input_string);
339 return (EXECUTION_FAILURE);
342 /* This has to be done this way rather than using string_list
343 and list_string because Posix.2 says that the last variable gets the
344 remaining words and their intervening separators. */
345 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
349 t = dequote_string (input_string);
350 var = bind_read_variable (list->word->word, t);
354 var = bind_read_variable (list->word->word, input_string);
355 stupidly_hack_special_variables (list->word->word);
357 var->attributes &= ~att_invisible;
358 free (orig_input_string);
364 bind_read_variable (name, value)
367 #if defined (ARRAY_VARS)
368 if (valid_array_reference (name) == 0)
370 if (legal_identifier (name) == 0)
372 builtin_error ("`%s': not a valid identifier", name);
373 return ((SHELL_VAR *)NULL);
375 return (bind_variable (name, value));
378 return (do_array_element_assignment (name, value));
380 return bind_variable (name, value);
384 #if defined (READLINE)
392 if (!bash_readline_initialized)
393 initialize_readline ();
398 ret = xrealloc (ret, len + 2);