From nobody@FreeBSD.org  Thu Nov  1 19:18:24 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 8F8F016A417
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  1 Nov 2007 19:18:24 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 7B0D713C4AA
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  1 Nov 2007 19:18:24 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.1/8.14.1) with ESMTP id lA1JIKvg019309
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 1 Nov 2007 19:18:20 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.1/8.14.1/Submit) id lA1JIKHL019308;
	Thu, 1 Nov 2007 19:18:20 GMT
	(envelope-from nobody)
Message-Id: <200711011918.lA1JIKHL019308@www.freebsd.org>
Date: Thu, 1 Nov 2007 19:18:20 GMT
From: Richard Seay <rick.seay@yahoo.ca>
To: freebsd-gnats-submit@FreeBSD.org
Subject: /bin/sh:  Backslash quote fails in pattern for substring processing.
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         117748
>Category:       bin
>Synopsis:       sh(1): Backslash quote fails in pattern for substring processing.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Nov 01 19:20:01 UTC 2007
>Closed-Date:    Fri May 06 21:36:48 UTC 2011
>Last-Modified:  Fri May 06 21:36:48 UTC 2011
>Originator:     Richard Seay
>Release:        6.2-RELEASE-p5
>Organization:
>Environment:
$ uname -a
FreeBSD 78NVNXM 6.2-RELEASE-p5 FreeBSD 6.2-RELEASE-p5 #0: Fri Jun 22 13:02:29 EDT 2007     root@78NVNXM:/usr/obj/usr/src/sys/KERNEL  i386
>Description:
Trying to match a literal question mark in a string for substring
processing, the question mark (?) in the pattern cannot be quoted.

This is what I would expect (using ksh93):

$ echo $SHELL
/usr/local/bin/ksh93
$ export s=foo?bar
$ echo $s
foo?bar
$ echo ${s%\?*}
foo
$ echo ${s#*\?}
bar

This is what happens (using sh):

$ /bin/sh
$ echo $s
foo?bar
$ echo ${s%\?*}
foo?ba
$ echo ${s#*\?}
oo?bar

Bash shows the same behaviour as ksh93.

>How-To-Repeat:
See above.
>Fix:


>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, rick.seay@yahoo.ca
Cc:  
Subject: Re: bin/117748: sh(1): Backslash quote fails in pattern for
 substring processing.
Date: Sun, 14 Mar 2010 14:20:11 +0100

 [pattern matching characters in ${v#pat}, ${v%pat} cannot be quoted]
 
 This is easy to fix if bin/57554 is fixed first. Currently double quotes
 continue into ${v#pat} like they do into ${v+word}, different from what
 the man page says. Once that is fixed, a well-directed EXP_CASE will do
 it.
 
 -- 
 Jilles Tjoelker

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/117748: commit references a PR
Date: Sat,  3 Apr 2010 21:08:04 +0000 (UTC)

 Author: jilles
 Date: Sat Apr  3 21:07:50 2010
 New Revision: 206147
 URL: http://svn.freebsd.org/changeset/base/206147
 
 Log:
   sh: Allow quoting pattern match characters in ${v%pat} and ${v#pat}.
   
   Note that this depends on r206145 for allowing pattern match characters to
   have their special meaning inside a double-quoted expansion like "${v%pat}".
   
   PR:		bin/117748
   Exp-run done by:	erwin (with some other sh(1) changes)
 
 Added:
   head/tools/regression/bin/sh/expansion/trim2.0   (contents, props changed)
 Modified:
   head/bin/sh/expand.c
 
 Modified: head/bin/sh/expand.c
 ==============================================================================
 --- head/bin/sh/expand.c	Sat Apr  3 21:01:01 2010	(r206146)
 +++ head/bin/sh/expand.c	Sat Apr  3 21:07:50 2010	(r206147)
 @@ -506,7 +506,9 @@ subevalvar(char *p, char *str, int strlo
  	int amount;
  
  	herefd = -1;
 -	argstr(p, 0);
 +	argstr(p, subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
 +	    subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
 +	    EXP_CASE : 0);
  	STACKSTRNUL(expdest);
  	herefd = saveherefd;
  	argbackq = saveargbackq;
 
 Added: head/tools/regression/bin/sh/expansion/trim2.0
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/tools/regression/bin/sh/expansion/trim2.0	Sat Apr  3 21:07:50 2010	(r206147)
 @@ -0,0 +1,55 @@
 +# $FreeBSD$
 +
 +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}'
 +h='##'
 +failures=''
 +ok=''
 +
 +testcase() {
 +	code="$1"
 +	expected="$2"
 +	oIFS="$IFS"
 +	eval "$code"
 +	IFS='|'
 +	result="$#|$*"
 +	IFS="$oIFS"
 +	if [ "x$result" = "x$expected" ]; then
 +		ok=x$ok
 +	else
 +		failures=x$failures
 +		echo "For $code, expected $expected actual $result"
 +	fi
 +}
 +
 +set -f
 +testcase 'set -- $s'				'1|ast*que?non'
 +testcase 'set -- ${s%\?*}'			'1|ast*que'
 +testcase 'set -- "${s%\?*}"'			'1|ast*que'
 +testcase 'set -- ${s%\**}'			'1|ast'
 +testcase 'set -- "${s%\**}"'			'1|ast'
 +testcase 'set -- ${s%"$q"*}'			'1|ast*que'
 +testcase 'set -- "${s%"$q"*}"'			'1|ast*que'
 +testcase 'set -- ${s%"$a"*}'			'1|ast'
 +testcase 'set -- "${s%"$a"*}"'			'1|ast'
 +testcase 'set -- ${s%"$q"$a}'			'1|ast*que'
 +testcase 'set -- "${s%"$q"$a}"'			'1|ast*que'
 +testcase 'set -- ${s%"$a"$a}'			'1|ast'
 +testcase 'set -- "${s%"$a"$a}"'			'1|ast'
 +set +f
 +
 +testcase 'set -- "${b%\}}"'			'1|{{(#)}'
 +# Parentheses are special in ksh, check that they can be escaped
 +testcase 'set -- "${b%\)*}"'			'1|{{(#'
 +testcase 'set -- "${h#\#}"'			'1|#'
 +
 +testcase 'set -- ${p%"${p#?}"}'			'1|/'
 +testcase 'set -- ${p%"${p#??????}"}'		'1|/etc'
 +testcase 'set -- ${p%"${p#???????}"}'		'1|/etc/'
 +testcase 'set -- "${p%"${p#?}"}"'		'1|/'
 +testcase 'set -- "${p%"${p#??????}"}"'		'1|/et[c]'
 +testcase 'set -- "${p%"${p#???????}"}"'		'1|/et[c]/'
 +testcase 'set -- ${p#"${p}"}'			'0|'
 +testcase 'set -- "${p#"${p}"}"'			'1|'
 +testcase 'set -- "${p#*\[}"'			'1|c]/'
 +
 +test "x$failures" = x
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: jilles 
State-Changed-When: Sat Apr 10 15:23:30 UTC 2010 
State-Changed-Why:  
Fixed in 9-CURRENT. 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Sat Apr 10 15:23:30 UTC 2010 
Responsible-Changed-Why:  
Track. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117748 

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/117748: sh(1): Backslash quote fails in pattern for
 substring processing.
Date: Sat, 10 Apr 2010 17:28:06 +0200

 Also note that you can get the desired result without sh changes using
 [*], [?] and [[] to represent literal *, ? and [ respectively.
State-Changed-From-To: patched->closed 
State-Changed-By: jilles 
State-Changed-When: Fri May 6 21:35:08 UTC 2011 
State-Changed-Why:  
Although this appears to work well in 9-CURRENT, I consider this change 
too risky for a -STABLE branch. Existing scripts might rely on the old 
behaviour. 

As noted in a previous comment, you can get the desired result without 
sh changes using [*], [?] and [[] to represent literal *, ? and [ 
respectively. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117748 
>Unformatted:
