Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 May 2011 16:03:37 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r222134 - in head: bin/sh tools/regression/bin/sh/parser
Message-ID:  <201105201603.p4KG3bAa091205@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Fri May 20 16:03:36 2011
New Revision: 222134
URL: http://svn.freebsd.org/changeset/base/222134

Log:
  sh: Allow terminating a heredoc with a terminator at EOF without a newline.
  
  This is sometimes used with eval or old-style command substitution, and most
  shells other than ash derivatives allow it.
  
  It can also be used with scripts that violate POSIX's requirement on the
  application that they end in a newline (scripts must be text files except
  that line length is unlimited).
  
  Example:
  v=`cat <<EOF
  foo
  EOF`
  echo $v
  
  This commit does not add support for the similar construct with new-style
  command substitution, like
  v=$(cat <<EOF
  foo
  EOF)
  This continues to require a newline after the terminator.

Added:
  head/tools/regression/bin/sh/parser/heredoc11.0   (contents, props changed)
Modified:
  head/bin/sh/parser.c

Modified: head/bin/sh/parser.c
==============================================================================
--- head/bin/sh/parser.c	Fri May 20 15:48:08 2011	(r222133)
+++ head/bin/sh/parser.c	Fri May 20 16:03:36 2011	(r222134)
@@ -1513,10 +1513,12 @@ checkend: {
 
 				p = line;
 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
-				if (*p == '\n' && *q == '\0') {
+				if ((*p == '\0' || *p == '\n') && *q == '\0') {
 					c = PEOF;
-					plinno++;
-					needprompt = doprompt;
+					if (*p == '\n') {
+						plinno++;
+						needprompt = doprompt;
+					}
 				} else {
 					pushstring(line, strlen(line), NULL);
 				}

Added: head/tools/regression/bin/sh/parser/heredoc11.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/parser/heredoc11.0	Fri May 20 16:03:36 2011	(r222134)
@@ -0,0 +1,26 @@
+# $FreeBSD$
+
+failures=''
+
+check() {
+	if eval "[ $* ]"; then
+		:
+	else
+		echo "Failed: $*"
+		failures=x$failures
+	fi
+}
+
+check '`cat <<EOF
+foo
+EOF` = foo'
+
+check '"`cat <<EOF
+foo
+EOF`" = foo'
+
+check '`eval "cat <<EOF
+foo
+EOF"` = foo'
+
+test "x$failures" = x



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201105201603.p4KG3bAa091205>