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)
68 #include "bashgetopt.h"
72 #if defined (READLINE)
73 #include "../bashline.h"
74 #include <readline/readline.h>
81 #define issep(c) (strchr (ifs_chars, (c)))
83 extern int interrupt_immediately;
85 #if defined (READLINE)
86 static char *edit_line ();
87 static void set_eol_delim ();
88 static void reset_eol_delim ();
90 static SHELL_VAR *bind_read_variable ();
92 static procenv_t alrmbuf;
93 static SigHandler *old_alrm;
100 longjmp (alrmbuf, 1);
106 set_signal_handler (SIGALRM, old_alrm);
110 /* Read the value of the shell variables whose names follow.
111 The reading is done from the current input stream, whatever
112 that may be. Successive words of the input line are assigned
113 to the variables mentioned in LIST. The last variable in LIST
114 gets the remainder of the words on the line. If no variables
115 are mentioned in LIST, then the default variable is $REPLY. */
120 register char *varname;
121 int size, i, pass_next, saw_escape, eof, opt, retval, code;
122 int input_is_tty, input_is_pipe, unbuffered_read;
123 int raw, edit, tmout, nchars, silent;
124 long timeoutval, ncharsval;
126 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
130 #if defined (ARRAY_VARS)
133 #if defined (READLINE)
138 i = 0; /* Index into the string that we are reading. */
139 raw = edit = 0; /* Not reading raw input by default. */
141 arrayname = prompt = (char *)NULL;
143 #if defined (READLINE)
148 tmout = -1; /* no timeout */
149 nchars = input_is_tty = input_is_pipe = unbuffered_read = 0;
150 delim = '\n'; /* read until newline */
152 reset_internal_getopt ();
153 while ((opt = internal_getopt (list, "erp:a:d:t:n:s")) != -1)
161 prompt = list_optarg;
167 #if defined (READLINE)
171 #if defined (ARRAY_VARS)
173 arrayname = list_optarg;
177 code = legal_number (list_optarg, &timeoutval);
178 if (code == 0 || timeoutval < 0)
180 builtin_error ("%s: invalid timeout specification", list_optarg);
181 return (EXECUTION_FAILURE);
187 code = legal_number (list_optarg, &ncharsval);
188 if (code == 0 || ncharsval < 0)
190 builtin_error ("%s: invalid number specification", list_optarg);
191 return (EXECUTION_FAILURE);
197 delim = *list_optarg;
206 /* `read -t 0 var' returns failure immediately. */
208 return (EXECUTION_FAILURE);
210 /* IF IFS is unset, we use the default of " \t\n". */
211 var = find_variable ("IFS");
212 ifs_chars = var ? value_cell (var) : " \t\n";
213 if (ifs_chars == 0) /* XXX */
214 ifs_chars = ""; /* XXX */
216 input_string = xmalloc (size = 128);
218 begin_unwind_frame ("read_builtin");
219 #if defined (READLINE)
220 add_unwind_protect (xfree, rlbuf);
223 input_is_tty = isatty (0);
224 if (input_is_tty == 0)
226 input_is_pipe = (lseek (0, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
231 /* If the -p, -e or -s flags were given, but input is not coming from the
232 terminal, turn them off. */
233 if ((prompt || edit || silent) && input_is_tty == 0)
235 prompt = (char *)NULL;
239 if (prompt && edit == 0)
241 fprintf (stderr, "%s", prompt);
245 pass_next = 0; /* Non-zero signifies last char was backslash. */
246 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
250 /* Turn off the timeout if stdin is a regular file (e.g. from
251 input redirection). */
252 if ((fstat (0, &tsb) < 0) || S_ISREG (tsb.st_mode))
258 code = setjmp (alrmbuf);
261 run_unwind_frame ("read_builtin");
262 return (EXECUTION_FAILURE);
264 old_alrm = set_signal_handler (SIGALRM, sigalrm);
265 add_unwind_protect (reset_alarm, (char *)NULL);
269 /* If we've been asked to read only NCHARS chars, or we're using some
270 character other than newline to terminate the line, do the right
271 thing to readline or the tty. */
272 if (nchars > 0 || delim != '\n')
274 #if defined (READLINE)
279 unwind_protect_int (rl_num_chars_to_read);
280 rl_num_chars_to_read = nchars;
284 set_eol_delim (delim);
285 add_unwind_protect (reset_eol_delim, (char *)NULL);
297 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
300 else if (silent) /* turn off echo but leave term in canonical mode */
304 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
307 /* This *must* be the top unwind-protect on the stack, so the manipulation
308 of the unwind-protect stack after the realloc() works right. */
309 add_unwind_protect (xfree, input_string);
310 interrupt_immediately++;
312 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
314 #if defined (__CYGWIN__) && defined (O_TEXT)
320 #if defined (READLINE)
323 if (rlbuf && rlbuf[rlind] == '\0')
330 rlbuf = edit_line (prompt ? prompt : "");
345 retval = zread (0, &c, 1);
347 retval = zreadc (0, &c);
355 #if defined (READLINE)
361 input_string = xrealloc (input_string, size += 128);
362 remove_unwind_protect ();
363 add_unwind_protect (xfree, input_string);
366 /* If the next character is to be accepted verbatim, a backslash
367 newline pair still disappears from the input. */
371 i--; /* back up over the CTLESC */
373 input_string[i++] = c;
378 if (c == '\\' && raw == 0)
382 input_string[i++] = CTLESC;
389 if (c == CTLESC || c == CTLNUL)
392 input_string[i++] = CTLESC;
395 input_string[i++] = c;
397 if (nchars > 0 && i >= nchars)
400 input_string[i] = '\0';
405 if (nchars > 0 || delim != '\n')
407 #if defined (READLINE)
411 rl_num_chars_to_read = 0;
423 if (unbuffered_read == 0)
426 interrupt_immediately--;
427 discard_unwind_frame ("read_builtin");
429 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
431 #if defined (ARRAY_VARS)
432 /* If -a was given, take the string read, break it into a list of words,
433 an assign them to `arrayname' in turn. */
436 var = find_variable (arrayname);
438 var = make_new_array_variable (arrayname);
439 else if (array_p (var) == 0)
440 var = convert_var_to_array (var);
442 empty_array (array_cell (var));
444 alist = list_string (input_string, ifs_chars, 0);
447 assign_array_var_from_word_list (var, alist);
448 dispose_words (alist);
450 xfree (input_string);
453 #endif /* ARRAY_VARS */
455 /* If there are no variables, save the text of the line read to the
456 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
457 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
458 same way, but I believe that the difference in behaviors is useful
459 enough to not do it. Without the bash behavior, there is no way
460 to read a line completely without interpretation or modification
461 unless you mess with $IFS (e.g., setting it to the empty string).
462 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
466 orig_input_string = input_string;
467 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
470 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
475 t = dequote_string (input_string);
476 var = bind_variable ("REPLY", t);
480 var = bind_variable ("REPLY", input_string);
481 VUNSETATTR (var, att_invisible);
487 /* This code implements the Posix.2 spec for splitting the words
488 read and assigning them to variables. */
489 orig_input_string = input_string;
491 /* Remove IFS white space at the beginning of the input string. If
492 $IFS is null, no field splitting is performed. */
493 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
497 for (; list->next; list = list->next)
499 varname = list->word->word;
500 #if defined (ARRAY_VARS)
501 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
503 if (legal_identifier (varname) == 0)
506 builtin_error ("`%s': not a valid identifier", varname);
507 free (orig_input_string);
508 return (EXECUTION_FAILURE);
511 /* If there are more variables than words read from the input,
512 the remaining variables are set to the empty string. */
515 /* This call updates INPUT_STRING. */
516 t = get_word_from_string (&input_string, ifs_chars, &e);
519 /* Don't bother to remove the CTLESC unless we added one
520 somewhere while reading the string. */
523 t1 = dequote_string (t);
524 var = bind_read_variable (varname, t1);
528 var = bind_read_variable (varname, t);
533 var = bind_read_variable (varname, "");
539 free (orig_input_string);
540 return (EXECUTION_FAILURE);
543 stupidly_hack_special_variables (varname);
544 VUNSETATTR (var, att_invisible);
547 /* Now assign the rest of the line to the last variable argument. */
548 #if defined (ARRAY_VARS)
549 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
551 if (legal_identifier (list->word->word) == 0)
554 builtin_error ("`%s': not a valid identifier", list->word->word);
555 free (orig_input_string);
556 return (EXECUTION_FAILURE);
559 /* This has to be done this way rather than using string_list
560 and list_string because Posix.2 says that the last variable gets the
561 remaining words and their intervening separators. */
562 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
566 t = dequote_string (input_string);
567 var = bind_read_variable (list->word->word, t);
571 var = bind_read_variable (list->word->word, input_string);
572 stupidly_hack_special_variables (list->word->word);
574 VUNSETATTR (var, att_invisible);
575 free (orig_input_string);
581 bind_read_variable (name, value)
584 #if defined (ARRAY_VARS)
585 if (valid_array_reference (name) == 0)
588 if (legal_identifier (name) == 0)
590 builtin_error ("`%s': not a valid identifier", name);
591 return ((SHELL_VAR *)NULL);
594 return (bind_variable (name, value));
597 return (do_array_element_assignment (name, value));
599 return bind_variable (name, value);
603 #if defined (READLINE)
611 if (!bash_readline_initialized)
612 initialize_readline ();
617 ret = xrealloc (ret, len + 2);
623 static int old_delim_ctype;
624 static Function *old_delim_func;
625 static int old_newline_ctype;
626 static Function *old_newline_func;
628 static int delim_char;
636 if (bash_readline_initialized == 0)
637 initialize_readline ();
638 cmap = rl_get_keymap ();
640 /* Change newline to self-insert */
641 old_newline_ctype = cmap[RETURN].type;
642 old_newline_func = cmap[RETURN].function;
643 cmap[RETURN].type = ISFUNC;
644 cmap[RETURN].function = rl_insert;
646 /* Bind the delimiter character to accept-line. */
647 old_delim_ctype = cmap[c].type;
648 old_delim_func = cmap[c].function;
649 cmap[c].type = ISFUNC;
650 cmap[c].function = rl_newline;
661 cmap = rl_get_keymap ();
663 cmap[RETURN].type = old_newline_ctype;
664 cmap[RETURN].function = old_newline_func;
666 cmap[delim_char].type = old_delim_ctype;
667 cmap[delim_char].function = old_delim_func;