Make the behaviour of `read -e', ie. treating backslashes as special,

the default. Add -r option for the read builtin to reverse this.

PR:		13274
Reviewed by:	cpiazza, hoek, sheldonh
This commit is contained in:
tg 1999-08-26 08:16:27 +00:00
parent 945ebc8846
commit da827dcea3
2 changed files with 21 additions and 17 deletions

View File

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: miscbltin.c,v 1.18 1998/12/16 04:45:35 imp Exp $"; "$Id: miscbltin.c,v 1.19 1999/05/08 10:21:56 kris Exp $";
#endif /* not lint */ #endif /* not lint */
/* /*
@ -71,8 +71,8 @@ extern char **argptr; /* argument list for builtin command */
/* /*
* The read builtin. The -e option causes backslashes to escape the * The read builtin. The -r option causes backslashes to be treated like
* following character. * ordinary characters.
* *
* This uses unbuffered input, which may be avoidable in some cases. * This uses unbuffered input, which may be avoidable in some cases.
*/ */
@ -85,7 +85,7 @@ readcmd(argc, argv)
char **ap; char **ap;
int backslash; int backslash;
char c; char c;
int eflag; int rflag;
char *prompt; char *prompt;
char *ifs; char *ifs;
char *p; char *p;
@ -98,17 +98,19 @@ readcmd(argc, argv)
struct termios told, tnew; struct termios told, tnew;
int tsaved; int tsaved;
eflag = 0; rflag = 0;
prompt = NULL; prompt = NULL;
tv.tv_sec = -1; tv.tv_sec = -1;
tv.tv_usec = 0; tv.tv_usec = 0;
while ((i = nextopt("ep:t:")) != '\0') { while ((i = nextopt("erp:t:")) != '\0') {
switch(i) { switch(i) {
case 'p': case 'p':
prompt = optarg; prompt = optarg;
break; break;
case 'e': case 'e':
eflag = 1; break;
case 'r':
rflag = 1;
break; break;
case 't': case 't':
tv.tv_sec = strtol(optarg, &tvptr, 0); tv.tv_sec = strtol(optarg, &tvptr, 0);
@ -184,7 +186,7 @@ readcmd(argc, argv)
STPUTC(c, p); STPUTC(c, p);
continue; continue;
} }
if (eflag && c == '\\') { if (!rflag && c == '\\') {
backslash++; backslash++;
continue; continue;
} }

View File

@ -33,7 +33,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $Id: sh.1,v 1.27 1999/04/03 11:41:46 cracauer Exp $ .\" $Id: sh.1,v 1.28 1999/04/19 18:48:26 max Exp $
.\" .\"
.Dd May 5, 1995 .Dd May 5, 1995
.Dt SH 1 .Dt SH 1
@ -1150,7 +1150,7 @@ is rather than recomputing it each time. This makes
it faster. However, if the current directory is it faster. However, if the current directory is
renamed, the builtin version of pwd will continue to renamed, the builtin version of pwd will continue to
print the old name for the directory. print the old name for the directory.
.It Li "read [ -p prompt ] [ -t timeout ] [ -e ] variable ... .It Li "read [ -p prompt ] [ -t timeout ] [ -er ] variable ...
The prompt is printed if the -p option is specified The prompt is printed if the -p option is specified
and the standard input is a terminal. Then a line is and the standard input is a terminal. Then a line is
read from the standard input. The trailing newline read from the standard input. The trailing newline
@ -1163,6 +1163,14 @@ separated them) are assigned to the last variable.
If there are more variables than pieces, the remaining If there are more variables than pieces, the remaining
variables are assigned the null string. variables are assigned the null string.
.Pp .Pp
Backslashes are treated specially, unless the -r option is
specified. If a backslash is followed by
a newline, the backslash and the newline will be
deleted. If a backslash is followed by any other
character, the backslash will be deleted and the following
character will be treated as though it were
not in IFS, even if it is.
.Pp
If the -t option is specified the timeout elapses If the -t option is specified the timeout elapses
before any input is supplied, the read command will before any input is supplied, the read command will
return without assigning any values. The timeout value return without assigning any values. The timeout value
@ -1170,13 +1178,7 @@ may optionally be followed by one of 's', 'm' or 'h' to
explicitly specify seconds, minutes or or hours. If none explicitly specify seconds, minutes or or hours. If none
is supplied, 's' is assumed. is supplied, 's' is assumed.
.Pp .Pp
The -e option causes any backslashes in the input to The -e option exists only for backward compatibility with older scripts.
be treated specially. If a backslash is followed by
a newline, the backslash and the newline will be
deleted. If a backslash is followed by any other
character, the backslash will be deleted and the following
character will be treated as though it were
not in IFS, even if it is.
.It readonly name ... .It readonly name ...
The specified names are marked as read only, so that The specified names are marked as read only, so that
they cannot be subsequently modified or unset. The shell they cannot be subsequently modified or unset. The shell