From owner-svn-src-all@FreeBSD.ORG Sun Mar 13 20:02:40 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 54436106566B; Sun, 13 Mar 2011 20:02:40 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 4260F8FC17; Sun, 13 Mar 2011 20:02:40 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p2DK2ebg064693; Sun, 13 Mar 2011 20:02:40 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p2DK2ejD064689; Sun, 13 Mar 2011 20:02:40 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201103132002.p2DK2ejD064689@svn.freebsd.org> From: Jilles Tjoelker Date: Sun, 13 Mar 2011 20:02:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r219623 - in head: bin/sh tools/regression/bin/sh/expansion X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Mar 2011 20:02:40 -0000 Author: jilles Date: Sun Mar 13 20:02:39 2011 New Revision: 219623 URL: http://svn.freebsd.org/changeset/base/219623 Log: sh: Fix some parameter expansion variants ${#...}. These already worked: $# ${#} ${##} ${#-} ${#?} These now work as well: ${#+word} ${#-word} ${##word} ${#%word} There is an ambiguity in the standard with ${#?}: it could be the length of $? or it could be $# giving an error in the (impossible) case that it is not set. We continue to use the former interpretation as it seems more useful. Added: head/tools/regression/bin/sh/expansion/plus-minus8.0 (contents, props changed) head/tools/regression/bin/sh/expansion/trim7.0 (contents, props changed) Modified: head/bin/sh/parser.c Modified: head/bin/sh/parser.c ============================================================================== --- head/bin/sh/parser.c Sun Mar 13 20:01:00 2011 (r219622) +++ head/bin/sh/parser.c Sun Mar 13 20:02:39 2011 (r219623) @@ -1447,6 +1447,7 @@ parsesub: { int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ int linno; int length; + int c1; c = pgetc(); if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && @@ -1473,15 +1474,9 @@ parsesub: { if (c == '{') { bracketed_name = 1; c = pgetc(); - if (c == '#') { - if ((c = pgetc()) == '}') - c = '#'; - else - subtype = VSLENGTH; - } - else - subtype = 0; + subtype = 0; } +varname: if (!is_eof(c) && is_name(c)) { length = 0; do { @@ -1511,19 +1506,35 @@ parsesub: { STPUTC(c, out); c = pgetc(); } - } else { - if (! is_special(c)) { - subtype = VSERROR; - if (c == '}') - pungetc(); - else if (c == '\n' || c == PEOF) - synerror("Unexpected end of line in substitution"); - else - USTPUTC(c, out); - } else { - USTPUTC(c, out); + } else if (is_special(c)) { + c1 = c; + c = pgetc(); + if (subtype == 0 && c1 == '#') { + subtype = VSLENGTH; + if (strchr(types, c) == NULL && c != ':' && + c != '#' && c != '%') + goto varname; + c1 = c; c = pgetc(); + if (c1 != '}' && c == '}') { + pungetc(); + c = c1; + goto varname; + } + pungetc(); + c = c1; + c1 = '#'; + subtype = 0; } + USTPUTC(c1, out); + } else { + subtype = VSERROR; + if (c == '}') + pungetc(); + else if (c == '\n' || c == PEOF) + synerror("Unexpected end of line in substitution"); + else + USTPUTC(c, out); } if (subtype == 0) { switch (c) { Added: head/tools/regression/bin/sh/expansion/plus-minus8.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/bin/sh/expansion/plus-minus8.0 Sun Mar 13 20:02:39 2011 (r219623) @@ -0,0 +1,5 @@ +# $FreeBSD$ + +set -- 1 2 3 4 5 6 7 8 9 10 11 12 13 +[ "${#+hi}" = hi ] || echo '${#+hi} wrong' +[ "${#-hi}" = 13 ] || echo '${#-hi} wrong' Added: head/tools/regression/bin/sh/expansion/trim7.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/bin/sh/expansion/trim7.0 Sun Mar 13 20:02:39 2011 (r219623) @@ -0,0 +1,16 @@ +# $FreeBSD$ + +set -- 1 2 3 4 5 6 7 8 9 10 11 12 13 +[ "${##1}" = 3 ] || echo '${##1} wrong' +[ "${###1}" = 3 ] || echo '${###1} wrong' +[ "${###}" = 13 ] || echo '${###} wrong' +[ "${#%3}" = 1 ] || echo '${#%3} wrong' +[ "${#%%3}" = 1 ] || echo '${#%%3} wrong' +[ "${#%%}" = 13 ] || echo '${#%%} wrong' +set -- +[ "${##0}" = "" ] || echo '${##0} wrong' +[ "${###0}" = "" ] || echo '${###0} wrong' +[ "${###}" = 0 ] || echo '${###} wrong' +[ "${#%0}" = "" ] || echo '${#%0} wrong' +[ "${#%%0}" = "" ] || echo '${#%%0} wrong' +[ "${#%%}" = 0 ] || echo '${#%%} wrong'