1996-08-26 18:22:31 +00:00
|
|
|
This file is read.def, from which is created read.c.
|
|
|
|
It implements the builtin "read" in Bash.
|
|
|
|
|
|
|
|
Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of GNU Bash, the Bourne Again SHell.
|
|
|
|
|
|
|
|
Bash is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 1, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Bash; see the file COPYING. If not, write to the Free Software
|
|
|
|
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
$PRODUCES read.c
|
|
|
|
|
|
|
|
$BUILTIN read
|
|
|
|
$FUNCTION read_builtin
|
1996-12-23 17:02:34 +00:00
|
|
|
$SHORT_DOC read [-r] [-p prompt] [-a array] [-e] [name ...]
|
1996-08-26 18:22:31 +00:00
|
|
|
One line is read from the standard input, and the first word is
|
1996-12-23 17:02:34 +00:00
|
|
|
assigned to the first NAME, the second word to the second NAME, and so
|
|
|
|
on, with leftover words assigned to the last NAME. Only the characters
|
1996-08-26 18:22:31 +00:00
|
|
|
found in $IFS are recognized as word delimiters. The return code is
|
1996-12-23 17:02:34 +00:00
|
|
|
zero, unless end-of-file is encountered. If no NAMEs are supplied, the
|
|
|
|
line read is stored in the REPLY variable. If the -r option is given,
|
|
|
|
this signifies `raw' input, and backslash escaping is disabled. If
|
|
|
|
the `-p' option is supplied, the string supplied as an argument is
|
|
|
|
output without a trailing newline before attempting to read. If -a is
|
|
|
|
supplied, the words read are assigned to sequential indices of ARRAY,
|
|
|
|
starting at zero. If -e is supplied and the shell is interactive,
|
|
|
|
readline is used to obtain the line.
|
1996-08-26 18:22:31 +00:00
|
|
|
$END
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
#include <stdio.h>
|
1996-12-23 17:02:34 +00:00
|
|
|
|
|
|
|
#if defined (HAVE_UNISTD_H)
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
#include "../shell.h"
|
|
|
|
#include "common.h"
|
1996-12-23 17:02:34 +00:00
|
|
|
#include "bashgetopt.h"
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (READLINE)
|
|
|
|
#include "../bashline.h"
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#endif
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
#define issep(c) (strchr (ifs_chars, (c)))
|
1996-08-26 18:22:31 +00:00
|
|
|
|
|
|
|
extern int interrupt_immediately;
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (READLINE)
|
|
|
|
static char *edit_line ();
|
|
|
|
#endif
|
|
|
|
static SHELL_VAR *bind_read_variable ();
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
/* Read the value of the shell variables whose names follow.
|
|
|
|
The reading is done from the current input stream, whatever
|
|
|
|
that may be. Successive words of the input line are assigned
|
|
|
|
to the variables mentioned in LIST. The last variable in LIST
|
|
|
|
gets the remainder of the words on the line. If no variables
|
1996-12-23 17:02:34 +00:00
|
|
|
are mentioned in LIST, then the default variable is $REPLY. */
|
|
|
|
int
|
1996-08-26 18:22:31 +00:00
|
|
|
read_builtin (list)
|
|
|
|
WORD_LIST *list;
|
|
|
|
{
|
|
|
|
register char *varname;
|
1996-12-23 17:02:34 +00:00
|
|
|
int size, i, raw, pass_next, saw_escape, eof, opt, retval, edit;
|
|
|
|
char c;
|
|
|
|
char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
|
|
|
|
char *e, *t, *t1;
|
1996-08-26 18:22:31 +00:00
|
|
|
SHELL_VAR *var;
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
WORD_LIST *alist;
|
|
|
|
#endif
|
|
|
|
#if defined (READLINE)
|
|
|
|
char *rlbuf;
|
|
|
|
int rlind;
|
|
|
|
#endif
|
1996-08-26 18:22:31 +00:00
|
|
|
|
|
|
|
i = 0; /* Index into the string that we are reading. */
|
1996-12-23 17:02:34 +00:00
|
|
|
raw = edit = 0; /* Not reading raw input by default. */
|
|
|
|
arrayname = prompt = (char *)NULL;
|
|
|
|
|
|
|
|
#if defined (READLINE)
|
|
|
|
rlbuf = (char *)0;
|
|
|
|
rlind = 0;
|
|
|
|
#endif
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
reset_internal_getopt ();
|
|
|
|
while ((opt = internal_getopt (list, "erp:a:")) != -1)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
raw = 1;
|
1996-08-26 18:22:31 +00:00
|
|
|
break;
|
1996-12-23 17:02:34 +00:00
|
|
|
case 'p':
|
|
|
|
prompt = list_optarg;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
#if defined (READLINE)
|
|
|
|
edit = 1;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
case 'a':
|
|
|
|
arrayname = list_optarg;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
builtin_usage ();
|
1996-08-26 18:22:31 +00:00
|
|
|
return (EX_USAGE);
|
|
|
|
}
|
|
|
|
}
|
1996-12-23 17:02:34 +00:00
|
|
|
list = loptend;
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
/* IF IFS is unset, we use the default of " \t\n". */
|
1996-08-26 18:22:31 +00:00
|
|
|
var = find_variable ("IFS");
|
|
|
|
ifs_chars = var ? value_cell (var) : " \t\n";
|
1996-12-23 17:02:34 +00:00
|
|
|
if (ifs_chars == 0) /* XXX */
|
|
|
|
ifs_chars = ""; /* XXX */
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
input_string = xmalloc (size = 128);
|
1996-08-26 18:22:31 +00:00
|
|
|
|
|
|
|
begin_unwind_frame ("read_builtin");
|
|
|
|
add_unwind_protect (xfree, input_string);
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (READLINE)
|
|
|
|
add_unwind_protect (xfree, rlbuf);
|
|
|
|
#endif
|
1996-08-26 18:22:31 +00:00
|
|
|
interrupt_immediately++;
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
/* If the -p or -e flags were given, but input is not coming from the
|
|
|
|
terminal, turn them off. */
|
|
|
|
if ((prompt || edit) && (isatty (0) == 0))
|
|
|
|
{
|
|
|
|
prompt = (char *)NULL;
|
|
|
|
edit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prompt && edit == 0)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s", prompt);
|
|
|
|
fflush (stderr);
|
|
|
|
}
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
pass_next = 0; /* Non-zero signifies last char was backslash. */
|
|
|
|
saw_escape = 0; /* Non-zero signifies that we saw an escape char */
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
for (eof = 0;;)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (READLINE)
|
|
|
|
if (edit)
|
|
|
|
{
|
|
|
|
if (rlbuf && rlbuf[rlind] == '\0')
|
|
|
|
{
|
1997-06-05 14:59:13 +00:00
|
|
|
xfree (rlbuf);
|
1996-12-23 17:02:34 +00:00
|
|
|
rlbuf = (char *)0;
|
|
|
|
}
|
|
|
|
if (rlbuf == 0)
|
|
|
|
{
|
|
|
|
rlbuf = edit_line (prompt ? prompt : "");
|
|
|
|
rlind = 0;
|
|
|
|
}
|
|
|
|
if (rlbuf == 0)
|
|
|
|
{
|
|
|
|
eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = rlbuf[rlind++];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (read (0, &c, 1) != 1)
|
|
|
|
{
|
|
|
|
eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
if (i + 2 >= size)
|
|
|
|
input_string = xrealloc (input_string, size += 128);
|
|
|
|
|
|
|
|
/* If the next character is to be accepted verbatim, a backslash
|
|
|
|
newline pair still disappears from the input. */
|
|
|
|
if (pass_next)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
i--; /* back up over the CTLESC */
|
|
|
|
else
|
|
|
|
input_string[i++] = c;
|
|
|
|
pass_next = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
if (c == '\\' && raw == 0)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
|
|
|
pass_next++;
|
|
|
|
saw_escape++;
|
|
|
|
input_string[i++] = CTLESC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (c == CTLESC || c == CTLNUL)
|
|
|
|
{
|
|
|
|
saw_escape++;
|
|
|
|
input_string[i++] = CTLESC;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_string[i++] = c;
|
|
|
|
}
|
|
|
|
input_string[i] = '\0';
|
|
|
|
|
|
|
|
interrupt_immediately--;
|
|
|
|
discard_unwind_frame ("read_builtin");
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
/* If -a was given, take the string read, break it into a list of words,
|
|
|
|
an assign them to `arrayname' in turn. */
|
|
|
|
if (arrayname)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
var = find_variable (arrayname);
|
|
|
|
if (var == 0)
|
|
|
|
var = make_new_array_variable (arrayname);
|
|
|
|
else if (array_p (var) == 0)
|
|
|
|
var = convert_var_to_array (var);
|
|
|
|
|
|
|
|
empty_array (array_cell (var));
|
|
|
|
|
|
|
|
alist = list_string (input_string, ifs_chars, 0);
|
|
|
|
if (alist)
|
|
|
|
{
|
|
|
|
assign_array_var_from_word_list (var, alist);
|
|
|
|
dispose_words (alist);
|
|
|
|
}
|
1997-06-05 14:59:13 +00:00
|
|
|
xfree (input_string);
|
1996-12-23 17:02:34 +00:00
|
|
|
return (retval);
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
1996-12-23 17:02:34 +00:00
|
|
|
#endif /* ARRAY_VARS */
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1997-06-05 14:59:13 +00:00
|
|
|
/* If there are no variables, save the text of the line read to the
|
|
|
|
variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
|
|
|
|
so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
|
|
|
|
same way, but I believe that the difference in behaviors is useful
|
|
|
|
enough to not do it. Without the bash behavior, there is no way
|
|
|
|
to read a line completely without interpretation or modification.
|
|
|
|
If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
|
|
|
|
if (list == 0)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1997-06-05 14:59:13 +00:00
|
|
|
#if 0
|
|
|
|
orig_input_string = input_string;
|
|
|
|
for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
|
|
|
|
;
|
|
|
|
input_string = t;
|
|
|
|
input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
|
|
|
|
#endif
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
if (saw_escape)
|
|
|
|
{
|
|
|
|
t = dequote_string (input_string);
|
|
|
|
var = bind_variable ("REPLY", t);
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
var = bind_variable ("REPLY", input_string);
|
|
|
|
var->attributes &= ~att_invisible;
|
1997-06-05 14:59:13 +00:00
|
|
|
#if 0
|
|
|
|
free (orig_input_string);
|
|
|
|
#else
|
1996-08-26 18:22:31 +00:00
|
|
|
free (input_string);
|
1997-06-05 14:59:13 +00:00
|
|
|
#endif
|
1996-12-23 17:02:34 +00:00
|
|
|
return (retval);
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
1996-12-23 17:02:34 +00:00
|
|
|
|
|
|
|
/* This code implements the Posix.2 spec for splitting the words
|
|
|
|
read and assigning them to variables. */
|
|
|
|
orig_input_string = input_string;
|
|
|
|
|
|
|
|
/* Remove IFS white space at the beginning of the input string. If
|
|
|
|
$IFS is null, no field splitting is performed. */
|
|
|
|
for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && issep(*t); t++)
|
|
|
|
;
|
|
|
|
input_string = t;
|
|
|
|
|
|
|
|
for (; list->next; list = list->next)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
varname = list->word->word;
|
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
|
|
|
|
#else
|
|
|
|
if (legal_identifier (varname) == 0)
|
|
|
|
#endif
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
builtin_error ("`%s': not a valid identifier", varname);
|
|
|
|
free (orig_input_string);
|
|
|
|
return (EXECUTION_FAILURE);
|
|
|
|
}
|
1996-08-26 18:22:31 +00:00
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
/* If there are more variables than words read from the input,
|
|
|
|
the remaining variables are set to the empty string. */
|
|
|
|
if (*input_string)
|
|
|
|
{
|
|
|
|
/* This call updates INPUT_STRING. */
|
|
|
|
t = get_word_from_string (&input_string, ifs_chars, &e);
|
|
|
|
if (t)
|
|
|
|
*e = '\0';
|
|
|
|
/* Don't bother to remove the CTLESC unless we added one
|
|
|
|
somewhere while reading the string. */
|
|
|
|
if (t && saw_escape)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
t1 = dequote_string (t);
|
|
|
|
var = bind_read_variable (varname, t1);
|
|
|
|
free (t1);
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
|
|
|
else
|
1996-12-23 17:02:34 +00:00
|
|
|
var = bind_read_variable (varname, t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t = (char *)0;
|
|
|
|
var = bind_read_variable (varname, "");
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
FREE (t);
|
|
|
|
if (var == 0)
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
|
|
|
free (orig_input_string);
|
|
|
|
return (EXECUTION_FAILURE);
|
|
|
|
}
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
stupidly_hack_special_variables (varname);
|
1996-08-26 18:22:31 +00:00
|
|
|
var->attributes &= ~att_invisible;
|
1996-12-23 17:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now assign the rest of the line to the last variable argument. */
|
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
|
|
|
|
#else
|
|
|
|
if (legal_identifier (list->word->word) == 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
builtin_error ("`%s': not a valid identifier", list->word->word);
|
1996-08-26 18:22:31 +00:00
|
|
|
free (orig_input_string);
|
1996-12-23 17:02:34 +00:00
|
|
|
return (EXECUTION_FAILURE);
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
/* This has to be done this way rather than using string_list
|
|
|
|
and list_string because Posix.2 says that the last variable gets the
|
|
|
|
remaining words and their intervening separators. */
|
|
|
|
input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
|
|
|
|
|
|
|
|
if (saw_escape)
|
|
|
|
{
|
|
|
|
t = dequote_string (input_string);
|
|
|
|
var = bind_read_variable (list->word->word, t);
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
var = bind_read_variable (list->word->word, input_string);
|
|
|
|
stupidly_hack_special_variables (list->word->word);
|
|
|
|
if (var)
|
|
|
|
var->attributes &= ~att_invisible;
|
|
|
|
free (orig_input_string);
|
|
|
|
|
1996-08-26 18:22:31 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
1996-12-23 17:02:34 +00:00
|
|
|
static SHELL_VAR *
|
|
|
|
bind_read_variable (name, value)
|
|
|
|
char *name, *value;
|
|
|
|
{
|
|
|
|
#if defined (ARRAY_VARS)
|
|
|
|
if (valid_array_reference (name) == 0)
|
|
|
|
{
|
|
|
|
if (legal_identifier (name) == 0)
|
|
|
|
{
|
|
|
|
builtin_error ("`%s': not a valid identifier", name);
|
|
|
|
return ((SHELL_VAR *)NULL);
|
|
|
|
}
|
|
|
|
return (bind_variable (name, value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (do_array_element_assignment (name, value));
|
|
|
|
#else
|
|
|
|
return bind_variable (name, value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined (READLINE)
|
|
|
|
static char *
|
|
|
|
edit_line (p)
|
|
|
|
char *p;
|
1996-08-26 18:22:31 +00:00
|
|
|
{
|
1996-12-23 17:02:34 +00:00
|
|
|
char *ret;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!bash_readline_initialized)
|
|
|
|
initialize_readline ();
|
|
|
|
ret = readline (p);
|
|
|
|
if (ret == 0)
|
|
|
|
return ret;
|
|
|
|
len = strlen (ret);
|
|
|
|
ret = xrealloc (ret, len + 2);
|
|
|
|
ret[len++] = '\n';
|
|
|
|
ret[len] = '\0';
|
|
|
|
return ret;
|
1996-08-26 18:22:31 +00:00
|
|
|
}
|
1996-12-23 17:02:34 +00:00
|
|
|
#endif
|