about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWayne Davison <wayned@users.sourceforge.net>2005-07-26 00:07:11 +0000
committerWayne Davison <wayned@users.sourceforge.net>2005-07-26 00:07:11 +0000
commitb77b51cda827cf6b282911c4e359b69721cba9e6 (patch)
tree526f83b8af665d03cba7f6ea5e0b1e66b3f87715
parentfa8923e1f99b326603dcd6f753019955f6b95a9b (diff)
downloadzsh-b77b51cda827cf6b282911c4e359b69721cba9e6.tar.gz
zsh-b77b51cda827cf6b282911c4e359b69721cba9e6.tar.xz
zsh-b77b51cda827cf6b282911c4e359b69721cba9e6.zip
Fixed bin_print() to handle \c escapes as per POSIX (truncating the
output at the \c).  (Based on a patch by Thorsten Dahlheimer.)
-rw-r--r--Src/builtin.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/Src/builtin.c b/Src/builtin.c
index 48e836996..982bfe60c 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3284,7 +3284,7 @@ int
 bin_print(char *name, char **args, Options ops, int func)
 {
     int flen, width, prec, type, argc, n, narg;
-    int nnl = 0, ret = 0, maxarg = 0;
+    int nnl = 0, fmttrunc = 0, ret = 0, maxarg = 0;
     int flags[5], *len;
     char *start, *endptr, *c, *d, *flag, *buf, spec[13], *fmt = NULL;
     char **first, *curarg, *flagch = "0+- #", save = '\0', nullstr = '\0';
@@ -3313,7 +3313,7 @@ bin_print(char *name, char **args, Options ops, int func)
     else if (OPT_HASARG(ops,'f'))
 	fmt = OPT_ARG(ops,'f');
     if (fmt)
-	fmt = getkeystring(fmt, &flen, OPT_ISSET(ops,'b') ? 2 : 0, &nnl);
+	fmt = getkeystring(fmt, &flen, OPT_ISSET(ops,'b') ? 2 : 0, &fmttrunc);
 
     first = args;
     
@@ -3349,10 +3349,16 @@ bin_print(char *name, char **args, Options ops, int func)
 	    (!OPT_ISSET(ops,'e') && 
 	     (OPT_ISSET(ops,'R') || OPT_ISSET(ops,'r') || OPT_ISSET(ops,'E'))))
 	    unmetafy(args[n], &len[n]);
-	else
+	else {
 	    args[n] = getkeystring(args[n], &len[n], OPT_ISSET(ops,'b') ? 2 :
 				   (func != BIN_ECHO && !OPT_ISSET(ops,'e')),
 				   &nnl);
+	    if (nnl) {
+		/* If there was a \c escape, make this the last arg. */
+		argc = n + 1;
+		args[argc] = NULL;
+	    }
+	}
 	/* -P option -- interpret as a prompt sequence */
 	if(OPT_ISSET(ops,'P')) {
 	    /*
@@ -3754,6 +3760,11 @@ bin_print(char *name, char **args, Options ops, int func)
 		    if (width < 0 && l < -width)
 		    	printf("%*c", -width - l, ' ');
 		    count += l;
+		    if (nnl) {
+			/* If the %b arg had a \c escape, truncate the fmt. */
+			flen = c - fmt + 1;
+			fmttrunc = 1;
+		    }
 		}
 		break;
 	    case 'q':
@@ -3845,7 +3856,7 @@ bin_print(char *name, char **args, Options ops, int func)
 			    ret = 1;
 			}
 			print_val(zulongval)
-			    }
+		    }
 		}
 	    }
 	    if (maxarg && (args - first > maxarg))
@@ -3854,7 +3865,7 @@ bin_print(char *name, char **args, Options ops, int func)
 
     	if (maxarg) args = first + maxarg;
 	/* if there are remaining args, reuse format string */
-    } while (*args && args != first && !OPT_ISSET(ops,'r'));
+    } while (*args && args != first && !fmttrunc && !OPT_ISSET(ops,'r'));
 
     if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s')) {
 #ifdef HAVE_OPEN_MEMSTREAM