Change interpretation of '\"' in here documents with substitution

according to SUSv3 and other modern shells (POSIX allows both).
Idea for the patch (add another lex state) from OpenBSD, but the
implementation differs slightly (and is better in quality).

Also add two testcases (/bin/sh passes both, old mksh only one),
and document the change in the manual page. Sync RCS IDs with OBSD.
This commit is contained in:
tg 2005-11-22 18:36:20 +00:00
parent 1d6032acda
commit f38d8299f9
5 changed files with 47 additions and 24 deletions

26
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.34 2005/10/25 21:11:25 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.35 2005/11/22 18:36:19 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -1214,6 +1214,30 @@ expected-stdout:
h\b
done
---
name: heredoc-quoting-unsubst
description:
Check for correct handling of quoted characters in
here documents without substitution (marker is quoted).
stdin:
foo=bar
cat <<-'EOF'
x " \" \ \\ $ \$ `echo baz` \`echo baz\` $foo \$foo x
EOF
expected-stdout:
x " \" \ \\ $ \$ `echo baz` \`echo baz\` $foo \$foo x
---
name: heredoc-quoting-subst
description:
Check for correct handling of quoted characters in
here documents with substitution (marker is not quoted).
stdin:
foo=bar
cat <<-EOF
x " \" \ \\ $ \$ `echo baz` \`echo baz\` $foo \$foo x
EOF
expected-stdout:
x " \" \ \ $ $ baz `echo baz` bar $foo x
---
name: heredoc-tmpfile-1
description:
Check that heredoc temp files aren't removed too soon or too late.

8
exec.c
View File

@ -1,9 +1,9 @@
/** $MirOS: src/bin/mksh/exec.c,v 1.10 2005/10/25 19:53:27 tg Exp $ */
/* $OpenBSD: exec.c,v 1.41 2005/03/30 17:16:37 deraadt Exp $ */
/** $MirOS: src/bin/mksh/exec.c,v 1.11 2005/11/22 18:36:19 tg Exp $ */
/* $OpenBSD: exec.c,v 1.42 2005/09/11 18:02:27 otto Exp $ */
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.10 2005/10/25 19:53:27 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.11 2005/11/22 18:36:19 tg Exp $");
static int comexec(struct op *, struct tbl *volatile, char **,
int volatile);
@ -1170,7 +1170,7 @@ herein(const char *content, int sub)
s = pushs(SSTRING, ATEMP);
s->start = s->str = content;
source = s;
if (yylex(ONEWORD) != LWORD)
if (yylex(ONEWORD|HEREDOC) != LWORD)
internal_errorf(1, "herein: yylex");
source = osource;
shf_puts(evalstr(yylval.cp, 0), shf);

13
lex.c
View File

@ -1,9 +1,9 @@
/** $MirOS: src/bin/mksh/lex.c,v 1.8 2005/10/25 19:53:28 tg Exp $ */
/* $OpenBSD: lex.c,v 1.36 2005/03/30 17:16:37 deraadt Exp $ */
/** $MirOS: src/bin/mksh/lex.c,v 1.9 2005/11/22 18:36:19 tg Exp $ */
/* $OpenBSD: lex.c,v 1.37 2005/09/11 18:02:27 otto Exp $ */
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.8 2005/10/25 19:53:28 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.9 2005/11/22 18:36:19 tg Exp $");
/* Structure to keep track of the lexing state and the various pieces of info
* needed for each particular state. */
@ -218,11 +218,16 @@ yylex(int cf)
case '\\':
c = getsc();
switch (c) {
case '"': case '\\':
case '"':
if ((cf & HEREDOC))
goto heredocquote;
/* FALLTROUGH */
case '\\':
case '$': case '`':
*wp++ = QCHAR, *wp++ = c;
break;
default:
heredocquote:
Xcheck(ws, wp);
if (c) { /* trailing \ is lost */
*wp++ = CHAR, *wp++ = '\\';

19
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.24 2005/11/22 18:09:18 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.25 2005/11/22 18:36:20 tg Exp $
.\" $OpenBSD: ksh.1,v 1.107 2005/11/16 12:49:21 jmc Exp $
.\" $OpenBSD: sh.1tbl,v 1.53 2004/12/10 01:56:56 jaredy Exp $
.\"
@ -1849,9 +1849,9 @@ escapes for
.Ql ` ,
.Ql \e ,
and
.Ql \enewline ;
any backslash preceding double quotes is removed (see
.Sx BUGS ) .
.Ql \enewline .
Handling of double quotes follows
.St -susv3 .
If multiple here documents are used on the same command line, they are saved in
order.
.It \*(Lt\*(Lt- Ar marker
@ -5146,7 +5146,8 @@ Privileged shell profile.
.Sh AUTHORS
This shell is based on the public domain korn shell.
It has been developed further by the MirOS project
incorporating changes from the OpenBSD project.
incorporating changes from the Debian and OpenBSD
projects and suggestions from many people.
.Sh BUGS
Please report bugs in
.Nm
@ -5154,14 +5155,6 @@ to the
.Mx
development team.
.Pp
Quoting rules for here documents are the same as for
double-quoted strings due to legacy reasons; this conforms with
.St -p1003.2-92
interpretation #104 and considered a feature of
.Nm
despite breaking compatibility with
.St -susv3 .
.Pp
By the way, the most frequently reported bug is:
.Bd -literal -offset indent
$ print hi | read a; print $a # Does not show hi

5
sh.h
View File

@ -1,10 +1,10 @@
/** $MirOS: src/bin/mksh/sh.h,v 1.21 2005/10/25 19:53:28 tg Exp $ */
/** $MirOS: src/bin/mksh/sh.h,v 1.22 2005/11/22 18:36:20 tg Exp $ */
/* $OpenBSD: sh.h,v 1.28 2005/10/04 20:35:11 otto Exp $ */
/* $OpenBSD: shf.h,v 1.5 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: table.h,v 1.6 2004/12/18 20:55:52 millert Exp $ */
/* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */
/* $OpenBSD: expand.h,v 1.6 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: lex.h,v 1.9 2004/12/18 21:04:52 millert Exp $ */
/* $OpenBSD: lex.h,v 1.10 2005/09/11 18:02:27 otto Exp $ */
/* $OpenBSD: proto.h,v 1.27 2005/10/06 06:39:36 otto Exp $ */
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
@ -954,6 +954,7 @@ typedef union {
#define CMDWORD BIT(8) /* parsing simple command (alias related) */
#define HEREDELIM BIT(9) /* parsing <<,<<- delimiter */
#define LQCHAR BIT(10) /* source string contains QCHAR */
#define HEREDOC BIT(11) /* parsing a here document */
#define HERES 10 /* max << in line */