about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
Diffstat (limited to 'Src')
-rw-r--r--Src/prompt.c17
-rw-r--r--Src/subst.c178
-rw-r--r--Src/utils.c20
3 files changed, 118 insertions, 97 deletions
diff --git a/Src/prompt.c b/Src/prompt.c
index 974f70e40..fc0c7ea47 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -944,10 +944,15 @@ countprompt(char *str, int *wp, int *hp, int overf)
 		multi = 0;
 		break;
 	    default:
-		/* If the character isn't printable, wcwidth() returns -1. */
+		/*
+		 * If the character isn't printable, wcwidth() returns
+		 * -1.  We assume width 1.
+		 */
 		wcw = wcwidth(wc);
-		if (wcw > 0)
+		if (wcw >= 0)
 		    w += wcw;
+		else
+		    w++;
 		multi = 0;
 		break;
 	    }
@@ -1152,8 +1157,10 @@ prompttrunc(int arg, int truncchar, int doprint, int endchar)
 				break;
 			    default:
 				wcw = wcwidth(cc);
-				if (wcw > 0)
+				if (wcw >= 0)
 				    remw -= wcw;
+				else
+				    remw--;
 				break;
 			    }
 #else
@@ -1215,8 +1222,10 @@ prompttrunc(int arg, int truncchar, int doprint, int endchar)
 				break;
 			    default:
 				wcw = wcwidth(cc);
-				if (wcw > 0)
+				if (wcw >= 0)
 				    maxwidth -= wcw;
+				else
+				    maxwidth--;
 				break;
 			    }
 #else
diff --git a/Src/subst.c b/Src/subst.c
index 2be854524..1a71a155c 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -837,29 +837,31 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		    }
 		} else {
 		    f -= lpreone;
-		    if ((m = f % lpremul)) {
-			/*
-			 * Left over fraction of repeated string.
-			 */
-			MB_METACHARINIT();
-			/* Skip this much. */
-			m = lpremul - m;
-			for (t = premul; m > 0; ) {
-			    t += MB_METACHARLENCONV(t, &cchar);
-			    m -= WCWIDTH(cchar);
-			}
-			/* Output the rest. */
-			while (*t)
-			    *r++ = *t++;
-		    }
-		    for (cc = f / lpremul; cc--;) {
-			/* Repeat the repeated string */
-			MB_METACHARINIT();
-			for (c = lpremul, t = premul; c > 0; ) {
-			    cl = MB_METACHARLENCONV(t, &cchar);
-			    while (cl--)
+		    if (lpremul) {
+			if ((m = f % lpremul)) {
+			    /*
+			     * Left over fraction of repeated string.
+			     */
+			    MB_METACHARINIT();
+			    /* Skip this much. */
+			    m = lpremul - m;
+			    for (t = premul; m > 0; ) {
+				t += MB_METACHARLENCONV(t, &cchar);
+				m -= WCWIDTH(cchar);
+			    }
+			    /* Output the rest. */
+			    while (*t)
 				*r++ = *t++;
-			    c -= WCWIDTH(cchar);
+			}
+			for (cc = f / lpremul; cc--;) {
+			    /* Repeat the repeated string */
+			    MB_METACHARINIT();
+			    for (c = lpremul, t = premul; c > 0; ) {
+				cl = MB_METACHARLENCONV(t, &cchar);
+				while (cl--)
+				    *r++ = *t++;
+				c -= WCWIDTH(cchar);
+			    }
 			}
 		    }
 		    if (preone) {
@@ -910,19 +912,21 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			while (*postone)
 			    *r++ = *postone++;
 		    }
-		    for (cc = f / lpostmul; cc--;) {
-			/* Begin the beguine */
-			for (t = postmul; *t; )
-			    *r++ = *t++;
-		    }
-		    if ((m = f % lpostmul)) {
-			/* Fill leftovers with chunk of repeated string */
-			MB_METACHARINIT();
-			while (m > 0) {
-			    cl = MB_METACHARLENCONV(postmul, &cchar);
-			    m -= WCWIDTH(cchar);
-			    while (cl--)
-				*r++ = *postmul++;
+		    if (lpostmul) {
+			for (cc = f / lpostmul; cc--;) {
+			    /* Begin the beguine */
+			    for (t = postmul; *t; )
+				*r++ = *t++;
+			}
+			if ((m = f % lpostmul)) {
+			    /* Fill leftovers with chunk of repeated string */
+			    MB_METACHARINIT();
+			    while (m > 0) {
+				cl = MB_METACHARLENCONV(postmul, &cchar);
+				m -= WCWIDTH(cchar);
+				while (cl--)
+				    *r++ = *postmul++;
+			    }
 			}
 		    }
 		}
@@ -983,37 +987,39 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		     * first
 		     */
 		    f -= lpreone;
-		    if ((m = f % lpremul)) {
-			/*
-			 * Some fraction of the repeated string needed.
-			 */
-			/* Need this much... */
-			c = m;
-			/* ...skipping this much first. */
-			m = lpremul - m;
-			MB_METACHARINIT();
-			for (t = premul; m > 0; ) {
-			    t += MB_METACHARLENCONV(t, &cchar);
-			    m -= WCWIDTH(cchar);
-			}
-			/* Now the rest of the repeated string. */
-			while (c > 0) {
-			    cl = MB_METACHARLENCONV(t, &cchar);
-			    while (cl--)
-				*r++ = *t++;
-			    c -= WCWIDTH(cchar);
+		    if (lpremul) {
+			if ((m = f % lpremul)) {
+			    /*
+			     * Some fraction of the repeated string needed.
+			     */
+			    /* Need this much... */
+			    c = m;
+			    /* ...skipping this much first. */
+			    m = lpremul - m;
+			    MB_METACHARINIT();
+			    for (t = premul; m > 0; ) {
+				t += MB_METACHARLENCONV(t, &cchar);
+				m -= WCWIDTH(cchar);
+			    }
+			    /* Now the rest of the repeated string. */
+			    while (c > 0) {
+				cl = MB_METACHARLENCONV(t, &cchar);
+				while (cl--)
+				    *r++ = *t++;
+				c -= WCWIDTH(cchar);
+			    }
 			}
-		    }
-		    for (cc = f / lpremul; cc--;) {
-			/*
-			 * Repeat the repeated string.
-			 */
-			MB_METACHARINIT();
-			for (c = lpremul, t = premul; c > 0; ) {
-			    cl = MB_METACHARLENCONV(t, &cchar);
-			    while (cl--)
-				*r++ = *t++;
-			    c -= WCWIDTH(cchar);
+			for (cc = f / lpremul; cc--;) {
+			    /*
+			     * Repeat the repeated string.
+			     */
+			    MB_METACHARINIT();
+			    for (c = lpremul, t = premul; c > 0; ) {
+				cl = MB_METACHARLENCONV(t, &cchar);
+				while (cl--)
+				    *r++ = *t++;
+				c -= WCWIDTH(cchar);
+			    }
 			}
 		    }
 		    if (preone) {
@@ -1089,27 +1095,29 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			c -= WCWIDTH(cchar);
 		    }
 		}
-		/* Repeat the repeated string */
-		for (cc = f / lpostmul; cc--;) {
-		    MB_METACHARINIT();
-		    for (c = lpostmul, t = postmul; *t; ) {
-			cl = MB_METACHARLENCONV(t, &cchar);
-			while (cl--)
-			    *r++ = *t++;
-			c -= WCWIDTH(cchar);
+		if (lpostmul) {
+		    /* Repeat the repeated string */
+		    for (cc = f / lpostmul; cc--;) {
+			MB_METACHARINIT();
+			for (c = lpostmul, t = postmul; *t; ) {
+			    cl = MB_METACHARLENCONV(t, &cchar);
+			    while (cl--)
+				*r++ = *t++;
+			    c -= WCWIDTH(cchar);
+			}
 		    }
-		}
-		/*
-		 * See if there's any fraction of the repeated
-		 * string needed to fill up the remaining space.
-		 */
-		if ((m = f % lpostmul)) {
-		    MB_METACHARINIT();
-		    while (m > 0) {
-			cl = MB_METACHARLENCONV(postmul, &cchar);
-			while (cl--)
-			    *r++ = *postmul++;
-			m -= WCWIDTH(cchar);
+		    /*
+		     * See if there's any fraction of the repeated
+		     * string needed to fill up the remaining space.
+		     */
+		    if ((m = f % lpostmul)) {
+			MB_METACHARINIT();
+			while (m > 0) {
+			    cl = MB_METACHARLENCONV(postmul, &cchar);
+			    while (cl--)
+				*r++ = *postmul++;
+			    m -= WCWIDTH(cchar);
+			}
 		    }
 		}
 	    }
diff --git a/Src/utils.c b/Src/utils.c
index 37017bdc7..415e86151 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -527,8 +527,10 @@ wcs_nicechar(wchar_t c, size_t *widthp, char **swidep)
     if (widthp) {
 	int wcw = wcwidth(c);
 	*widthp = (s - buf);
-	if (wcw > 0)
+	if (wcw >= 0)
 	    *widthp += wcw;
+	else
+	    (*widthp)++;
     }
     if (swidep)
 	*swidep = s;
@@ -550,12 +552,12 @@ zwcwidth(wint_t wc)
 {
     int wcw;
     /* assume a single-byte character if not valid */
-    if (wc == WEOF)
+    if (wc == WEOF || unset(MULTIBYTE))
 	return 1;
     wcw = wcwidth(wc);
-    /* if not printable, assume zero width */
-    if (wcw <= 0)
-	return 0;
+    /* if not printable, assume width 1 */
+    if (wcw < 0)
+	return 1;
     return wcw;
 }
 
@@ -4077,12 +4079,14 @@ mb_metastrlen(char *ptr, int width)
 		num++;
 	    } else if (width) {
 		/*
-		 * Returns -1 if not a printable character; best
-		 * just to ignore these.
+		 * Returns -1 if not a printable character.  We
+		 * turn this into 1 for backward compatibility.
 		 */
 		int wcw = wcwidth(wc);
-		if (wcw > 0)
+		if (wcw >= 0)
 		    num += wcw;
+		else
+		    num++;
 	    } else
 		num++;
 	    laststart = ptr;