Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham
	autolearn_force=no version=3.4.1
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH v2 3/3] Clean up chabspath() [':a' word modifier].
Date: Tue, 21 Jun 2016 01:53:24 +0000
Message-Id: <1466474004-4669-3-git-send-email-danielsh@tarsus.local2>
X-Mailer: git-send-email 2.1.4
In-Reply-To: <1466474004-4669-1-git-send-email-danielsh@tarsus.local2>
References: <20160613085218.GA9572@tarsus.local2>
 <1466474004-4669-1-git-send-email-danielsh@tarsus.local2>
X-Seq: zsh-workers 38727

Fail fast when the first character isn't "/", and use that to simplify the
'..' handling.

The incumbent code was subtly wrong in various ways (for example,
the first 'else if' branch didn't check that dest[-4] was a slash).
---
 Src/hist.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/Src/hist.c b/Src/hist.c
index 30a1bef..2623a32 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -1767,6 +1767,10 @@ chabspath(char **junkptr)
     if (**junkptr != '/') {
 	*junkptr = zhtricat(metafy(zgetcwd(), -1, META_HEAPDUP), "/", *junkptr);
     }
+    if (**junkptr != '/') {
+	/* Can happen after 'rmdir $PWD; zsh' */
+	return 0;
+    }
 
     current = *junkptr;
     dest = *junkptr;
@@ -1783,7 +1787,9 @@ chabspath(char **junkptr)
 
     for (;;) {
 	if (*current == '/') {
+	    /* Contract multiple slashes. */
 #ifdef __CYGWIN__
+	    /* Exception: on Cygwin, two slashes at the very start are significant. */
 	    if (current == *junkptr && current[1] == '/')
 		*dest++ = *current++;
 #endif
@@ -1791,28 +1797,17 @@ chabspath(char **junkptr)
 	    while (*current == '/')
 		current++;
 	} else if (!*current) {
+	    /* Remove trailing slashes. */
 	    while (dest > *junkptr + 1 && dest[-1] == '/')
 		dest--;
 	    *dest = '\0';
 	    break;
 	} else if (current[0] == '.' && current[1] == '.' &&
 		   (!current[2] || current[2] == '/')) {
-		if (current == *junkptr || dest == *junkptr) {
-		    *dest++ = '.';
-		    *dest++ = '.';
-		    current += 2;
-		} else if (dest > *junkptr + 2 &&
-			   !strncmp(dest - 3, "../", 3)) {
-		    *dest++ = '.';
-		    *dest++ = '.';
-		    current += 2;
-		} else if (dest > *junkptr + 1) {
-		    *dest = '\0';
+		if (dest > *junkptr + 1) {
 		    for (dest--;
-			 dest > *junkptr + 1 && dest[-1] != '/';
+			 dest[-1] != '/';
 			 dest--);
-		    if (dest[-1] != '/')
-			dest--;
 		    current += 2;
 		    if (*current == '/')
 			current++;

