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] [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, etc.
29 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 the -r option is given,
32 this signifies `raw' input, and backslash processing is disabled.
39 #define issep(c) (strchr (ifs_chars, (c)) != (char *)0)
41 static int stream_close ();
43 extern int interrupt_immediately;
45 /* Read the value of the shell variables whose names follow.
46 The reading is done from the current input stream, whatever
47 that may be. Successive words of the input line are assigned
48 to the variables mentioned in LIST. The last variable in LIST
49 gets the remainder of the words on the line. If no variables
50 are mentioned in LIST, then the default variable is $REPLY.
52 S. R. Bourne's shell complains if you don't name a variable
53 to receive the stuff that is read. GNU's shell doesn't. This
54 allows you to let the user type random things. */
58 register char *varname;
59 int size, c, i, fildes, raw_mode, pass_next, saw_escape, retval;
60 char *input_string, *orig_input_string, *ifs_chars, *t;
64 i = 0; /* Index into the string that we are reading. */
65 raw_mode = 0; /* Not reading raw input be default. */
69 if (ISOPTION (list->word->word, 'r'))
74 else if (ISOPTION (list->word->word, '-'))
79 else if (*list->word->word == '-')
81 bad_option (list->word->word);
82 builtin_error ("usage: read [-r] [name ...]");
89 /* We need unbuffered input from stdin. So we make a new stream with
90 the same file descriptor as stdin, then unbuffer it. */
91 fildes = dup (fileno (stdin));
94 return (EXECUTION_FAILURE);
96 input_stream = fdopen (fildes, "r");
101 return (EXECUTION_FAILURE);
104 var = find_variable ("IFS");
105 ifs_chars = var ? value_cell (var) : " \t\n";
107 input_string = xmalloc (size = 128);
109 setbuf (input_stream, (char *)NULL);
111 begin_unwind_frame ("read_builtin");
112 add_unwind_protect (xfree, input_string);
113 add_unwind_protect (stream_close, input_stream);
114 interrupt_immediately++;
116 pass_next = 0; /* Non-zero signifies last char was backslash. */
117 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
119 while ((c = getc (input_stream)) != EOF)
122 input_string = xrealloc (input_string, size += 128);
124 /* If the next character is to be accepted verbatim, a backslash
125 newline pair still disappears from the input. */
129 i--; /* back up over the CTLESC */
131 input_string[i++] = c;
136 if (c == '\\' && !raw_mode)
140 input_string[i++] = CTLESC;
147 if (c == CTLESC || c == CTLNUL)
150 input_string[i++] = CTLESC;
153 input_string[i++] = c;
155 input_string[i] = '\0';
157 interrupt_immediately--;
158 discard_unwind_frame ("read_builtin");
160 fclose (input_stream);
164 retval = EXECUTION_FAILURE;
165 /* input_string[0] = '\0'; */
168 retval = EXECUTION_SUCCESS;
174 t = dequote_string (input_string);
175 var = bind_variable ("REPLY", t);
179 var = bind_variable ("REPLY", input_string);
180 var->attributes &= ~att_invisible;
185 /* This code implements the Posix.2 spec for splitting the words
186 read and assigning them to variables. If $IFS is unset, we
187 use the default value of " \t\n". */
188 orig_input_string = input_string;
190 /* Remove IFS white space at the beginning of the input string. If
191 $IFS is null, no field splitting is performed. */
192 for (t = input_string; *ifs_chars && spctabnl (*t) && issep (*t); t++)
196 for (; list->next; list = list->next)
200 varname = list->word->word;
201 if (legal_identifier (varname) == 0)
203 builtin_error ("%s: not a legal variable name", varname);
204 free (orig_input_string);
205 return (EXECUTION_FAILURE);
208 /* If there are more variables than words read from the input,
209 the remaining variables are set to the empty string. */
212 /* This call updates INPUT_STRING. */
213 t = get_word_from_string (&input_string, ifs_chars, &e);
216 /* Don't bother to remove the CTLESC unless we added one
217 somewhere while reading the string. */
220 t1 = dequote_string (t);
221 var = bind_variable (varname, t1);
225 var = bind_variable (varname, t);
230 var = bind_variable (varname, "");
233 stupidly_hack_special_variables (varname);
234 var->attributes &= ~att_invisible;
240 if (legal_identifier (list->word->word) == 0)
242 builtin_error ("%s: not a legal variable name", list->word->word);
243 free (orig_input_string);
244 return (EXECUTION_FAILURE);
247 /* This has to be done this way rather than using string_list
248 and list_string because Posix.2 says that the last variable gets the
249 remaining words and their intervening separators. */
250 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars,
255 t = dequote_string (input_string);
256 var = bind_variable (list->word->word, t);
260 var = bind_variable (list->word->word, input_string);
261 stupidly_hack_special_variables (list->word->word);
262 var->attributes &= ~att_invisible;
263 free (orig_input_string);
269 /* This way I don't have to know whether fclose () is a
270 function or a macro. */
275 return (fclose (file));