about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2006-09-20 09:22:34 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2006-09-20 09:22:34 +0000
commit2bfad02a94ca607d918ea871147680dbeff0999c (patch)
treea34147b24c87e9144029b7b02fc70787a0eafb93 /Src
parent1554b8105322defdb7df5580d84a40490597711f (diff)
downloadzsh-2bfad02a94ca607d918ea871147680dbeff0999c.tar.gz
zsh-2bfad02a94ca607d918ea871147680dbeff0999c.tar.xz
zsh-2bfad02a94ca607d918ea871147680dbeff0999c.zip
22742: parameter padding uses string lengths unless (m) flag set
22743: restrict sh "- [args]" -> "-xv [-- args]" hack to sh mode
Diffstat (limited to 'Src')
-rw-r--r--Src/builtin.c2
-rw-r--r--Src/subst.c105
-rw-r--r--Src/zsh.h2
3 files changed, 75 insertions, 34 deletions
diff --git a/Src/builtin.c b/Src/builtin.c
index dbe50d13c..713e41a98 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -535,7 +535,7 @@ bin_set(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 
     /* Obsolescent sh compatibility: set - is the same as set +xv *
      * and set - args is the same as set +xv -- args              */
-    if (*args && **args == '-' && !args[0][1]) {
+    if (emulation != EMULATE_ZSH && *args && **args == '-' && !args[0][1]) {
 	dosetopt(VERBOSE, 0, 0);
 	dosetopt(XTRACE, 0, 0);
 	if (!args[1])
diff --git a/Src/subst.c b/Src/subst.c
index 1a71a155c..28ef9c011 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -738,11 +738,20 @@ invinstrpcmp(const void *a, const void *b)
  * will be used.
  */
 
-/**/
 static char *
 dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
-	  char *premul, char *postmul)
+	  char *premul, char *postmul
+#ifdef MULTIBYTE_SUPPORT
+	  , int multi_width
+#endif
+    )
 {
+#ifdef MULTIBYTE_SUPPORT
+#define WCPADWIDTH(cchar)	(multi_width ? WCWIDTH(cchar) : 1)
+#else
+#define WCPADWIDTH(cchar)	(1)
+#endif
+
     char *def, *ret, *t, *r;
     int ls, ls2, lpreone, lpostone, lpremul, lpostmul, lr, f, m, c, cc, cl;
     convchar_t cchar;
@@ -761,11 +770,11 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
     if (!postmul || !*postmul)
 	postmul = def;
 
-    ls = MB_METASTRWIDTH(str);
-    lpreone = preone ? MB_METASTRWIDTH(preone) : 0;
-    lpostone = postone ? MB_METASTRWIDTH(postone) : 0;
-    lpremul = MB_METASTRWIDTH(premul);
-    lpostmul = MB_METASTRWIDTH(postmul);
+    ls = MB_METASTRLEN2(str, multi_width);
+    lpreone = preone ? MB_METASTRLEN2(preone, multi_width) : 0;
+    lpostone = postone ? MB_METASTRLEN2(postone, multi_width) : 0;
+    lpremul = MB_METASTRLEN2(premul, multi_width);
+    lpostmul = MB_METASTRLEN2(postmul, multi_width);
 
     if (prenum + postnum == ls)
 	return str;
@@ -808,14 +817,14 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		MB_METACHARINIT();
 		while (f > 0) {
 		    str += MB_METACHARLENCONV(str, &cchar);
-		    f -= WCWIDTH(cchar);
+		    f -= WCPADWIDTH(cchar);
 		}
 		/* Now finish the first half. */
 		for (c = prenum; c > 0; ) {
 		    cl = MB_METACHARLENCONV(str, &cchar);
 		    while (cl--)
 			*r++ = *str++;
-		    c -= WCWIDTH(cchar);
+		    c -= WCPADWIDTH(cchar);
 		}
 	    } else {
 		if (f <= lpreone) {
@@ -829,7 +838,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			/* So skip. */
 			for (t = preone; f > 0; ) {
 			    t += MB_METACHARLENCONV(t, &cchar);
-			    f -= WCWIDTH(cchar);
+			    f -= WCPADWIDTH(cchar);
 			}
 			/* Then copy the entire remainder. */
 			while (*t)
@@ -847,7 +856,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			    m = lpremul - m;
 			    for (t = premul; m > 0; ) {
 				t += MB_METACHARLENCONV(t, &cchar);
-				m -= WCWIDTH(cchar);
+				m -= WCPADWIDTH(cchar);
 			    }
 			    /* Output the rest. */
 			    while (*t)
@@ -860,7 +869,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 				cl = MB_METACHARLENCONV(t, &cchar);
 				while (cl--)
 				    *r++ = *t++;
-				c -= WCWIDTH(cchar);
+				c -= WCPADWIDTH(cchar);
 			    }
 			}
 		    }
@@ -873,7 +882,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		/* Output the first half width of the original string. */
 		for (c = ls2; c > 0; ) {
 		    cl = MB_METACHARLENCONV(str, &cchar);
-		    c -= WCWIDTH(cchar);
+		    c -= WCPADWIDTH(cchar);
 		    while (cl--)
 			*r++ = *str++;
 		}
@@ -887,7 +896,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		MB_METACHARINIT();
 		for (c = postnum; c > 0; ) {
 		    cl = MB_METACHARLENCONV(str, &cchar);
-		    c -= WCWIDTH(cchar);
+		    c -= WCPADWIDTH(cchar);
 		    while (cl--)
 			*r++ = *str++;
 		}
@@ -900,7 +909,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			/* Can't fit unrepeated string, truncate it */
 			for (c = f; c > 0; ) {
 			    cl = MB_METACHARLENCONV(postone, &cchar);
-			    c -= WCWIDTH(cchar);
+			    c -= WCPADWIDTH(cchar);
 			    while (cl--)
 				*r++ = *postone++;
 			}
@@ -923,7 +932,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			    MB_METACHARINIT();
 			    while (m > 0) {
 				cl = MB_METACHARLENCONV(postmul, &cchar);
-				m -= WCWIDTH(cchar);
+				m -= WCPADWIDTH(cchar);
 				while (cl--)
 				    *r++ = *postmul++;
 			    }
@@ -947,14 +956,14 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		MB_METACHARINIT();
 		while (f > 0) {
 		    str += MB_METACHARLENCONV(str, &cchar);
-		    f -= WCWIDTH(cchar);
+		    f -= WCPADWIDTH(cchar);
 		}
 		/* Copy the rest of the original string */
 		for (c = prenum; c > 0; ) {
 		    cl = MB_METACHARLENCONV(str, &cchar);
 		    while (cl--)
 			*r++ = *str++;
-		    c -= WCWIDTH(cchar);
+		    c -= WCPADWIDTH(cchar);
 		}
 	    } else {
 		/*
@@ -975,7 +984,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			MB_METACHARINIT();
 			for (t = preone; f > 0; ) {
 			    t += MB_METACHARLENCONV(t, &cchar);
-			    f -= WCWIDTH(cchar);
+			    f -= WCPADWIDTH(cchar);
 			}
 			/* Copy the rest of preone */
 			while (*t)
@@ -999,14 +1008,14 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			    MB_METACHARINIT();
 			    for (t = premul; m > 0; ) {
 				t += MB_METACHARLENCONV(t, &cchar);
-				m -= WCWIDTH(cchar);
+				m -= WCPADWIDTH(cchar);
 			    }
 			    /* Now the rest of the repeated string. */
 			    while (c > 0) {
 				cl = MB_METACHARLENCONV(t, &cchar);
 				while (cl--)
 				    *r++ = *t++;
-				c -= WCWIDTH(cchar);
+				c -= WCPADWIDTH(cchar);
 			    }
 			}
 			for (cc = f / lpremul; cc--;) {
@@ -1018,7 +1027,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 				cl = MB_METACHARLENCONV(t, &cchar);
 				while (cl--)
 				    *r++ = *t++;
-				c -= WCWIDTH(cchar);
+				c -= WCPADWIDTH(cchar);
 			    }
 			}
 		    }
@@ -1056,7 +1065,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		cl = MB_METACHARLENCONV(str, &cchar);
 		while (cl--)
 		    *r++ = *str++;
-		c -= WCWIDTH(cchar);
+		c -= WCPADWIDTH(cchar);
 	    }
 	} else {
 	    /*
@@ -1068,7 +1077,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 		cl = MB_METACHARLENCONV(str, &cchar);
 		while (cl--)
 		    *r++ = *str++;
-		c -= WCWIDTH(cchar);
+		c -= WCPADWIDTH(cchar);
 	    }
 	    MB_METACHARINIT();
 	    if (f <= lpostone) {
@@ -1081,7 +1090,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			cl = MB_METACHARLENCONV(postone, &cchar);
 			while (cl--)
 			    *r++ = *postone++;
-			c -= WCWIDTH(cchar);
+			c -= WCPADWIDTH(cchar);
 		    }
 		}
 	    } else {
@@ -1092,7 +1101,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			cl = MB_METACHARLENCONV(postone, &cchar);
 			while (cl--)
 			    *r++ = *postone++;
-			c -= WCWIDTH(cchar);
+			c -= WCPADWIDTH(cchar);
 		    }
 		}
 		if (lpostmul) {
@@ -1103,7 +1112,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			    cl = MB_METACHARLENCONV(t, &cchar);
 			    while (cl--)
 				*r++ = *t++;
-			    c -= WCWIDTH(cchar);
+			    c -= WCPADWIDTH(cchar);
 			}
 		    }
 		    /*
@@ -1116,7 +1125,7 @@ dopadding(char *str, int prenum, int postnum, char *preone, char *postone,
 			    cl = MB_METACHARLENCONV(postmul, &cchar);
 			    while (cl--)
 				*r++ = *postmul++;
-			    m -= WCWIDTH(cchar);
+			    m -= WCPADWIDTH(cchar);
 			}
 		    }
 		}
@@ -1414,6 +1423,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
     char *replstr = NULL;
     /* The numbers for (l) and (r) */
     zlong prenum = 0, postnum = 0;
+#ifdef MULTIBYTE_SUPPORT
+    /* The (m) flag: use width of multibyte characters */
+    int multi_width = 0;
+#endif
     /*
      * Whether the value has been copied.  Optimisation:  if we
      * are modifying an expression, we only need to copy it the
@@ -1702,6 +1715,12 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 		    s = t;
 		    break;
 
+		case 'm':
+#ifdef MULTIBYTE_SUPPORT
+		    multi_width = 1;
+#endif
+		    break;
+
 		case 'p':
 		    escapes = 1;
 		    break;
@@ -3048,7 +3067,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	    while ((x = *aval++)) {
 		if (prenum || postnum)
 		    x = dopadding(x, prenum, postnum, preone, postone,
-				  premul, postmul);
+				  premul, postmul
+#ifdef MULTIBYTE_SUPPORT
+				  , multi_width
+#endif
+			);
 		if (eval && subst_parse_str(&x, (qt && !nojoin), quoteerr))
 		    return NULL;
 		xlen = strlen(x);
@@ -3092,7 +3115,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	    x = aval[0];
 	    if (prenum || postnum)
 		x = dopadding(x, prenum, postnum, preone, postone,
-			      premul, postmul);
+			      premul, postmul
+#ifdef MULTIBYTE_SUPPORT
+			      , multi_width
+#endif
+		    );
 	    if (eval && subst_parse_str(&x, (qt && !nojoin), quoteerr))
 		return NULL;
 	    xlen = strlen(x);
@@ -3107,7 +3134,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 		x = aval[i++];
 		if (prenum || postnum)
 		    x = dopadding(x, prenum, postnum, preone, postone,
-				  premul, postmul);
+				  premul, postmul
+#ifdef MULTIBYTE_SUPPORT
+				  , multi_width
+#endif
+			);
 		if (eval && subst_parse_str(&x, (qt && !nojoin), quoteerr))
 		    return NULL;
 		if (qt && !*x && isarr != 2)
@@ -3123,7 +3154,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	    x = aval[i];
 	    if (prenum || postnum)
 		x = dopadding(x, prenum, postnum, preone, postone,
-			      premul, postmul);
+			      premul, postmul
+#ifdef MULTIBYTE_SUPPORT
+			      , multi_width
+#endif
+		    );
 	    if (eval && subst_parse_str(&x, (qt && !nojoin), quoteerr))
 		return NULL;
 	    xlen = strlen(x);
@@ -3147,7 +3182,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
 	x = val;
 	if (prenum || postnum)
 	    x = dopadding(x, prenum, postnum, preone, postone,
-			  premul, postmul);
+			  premul, postmul
+#ifdef MULTIBYTE_SUPPORT
+			  , multi_width
+#endif
+		);
 	if (eval && subst_parse_str(&x, (qt && !nojoin), quoteerr))
 	    return NULL;
 	xlen = strlen(x);
diff --git a/Src/zsh.h b/Src/zsh.h
index ce6cf989a..9a76bf9a5 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2008,6 +2008,7 @@ typedef wint_t convchar_t;
 #define MB_METACHARLEN(str)	mb_metacharlenconv(str, NULL)
 #define MB_METASTRLEN(str)	mb_metastrlen(str, 0)
 #define MB_METASTRWIDTH(str)	mb_metastrlen(str, 1)
+#define MB_METASTRLEN2(str, widthp)	mb_metastrlen(str, widthp)
 
 /*
  * Note WCWIDTH() takes wint_t, typically as a convchar_t.
@@ -2041,6 +2042,7 @@ typedef int convchar_t;
 #define MB_METACHARLEN(str)	(*(str) == Meta ? 2 : 1)
 #define MB_METASTRLEN(str)	ztrlen(str)
 #define MB_METASTRWIDTH(str)	ztrlen(str)
+#define MB_METASTRLEN2(str, widthp)	ztrlen(str)
 
 #define WCWIDTH(c)	(1)