Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

printf again



This patch to printf just allows widths and precisions to be used with
%b and I'm only posting it now to get it out of my way. I've also
documented -r. Is there any reason why it shouldn't be possible to use
print -o in combination with -s/-z. Allowing it is a simple cut and
paste so I've changed it here.

It is doing my head in to try to work out how to do the memory
management for using sprintf so that print -f can work with -s/-z.
Formats like `%#010000000x' don't make it easy. The easy way would be
to use open_memstream but it is a glibc extension. I could check for it
with configure and otherwise use a temporary file. print -s and -z with
-f are probably not going to be used much anyway. Any views on that?

Oliver

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.41
diff -u -r1.41 builtins.yo
--- Doc/Zsh/builtins.yo	2001/11/06 15:07:00	1.41
+++ Doc/Zsh/builtins.yo	2001/11/09 16:23:10
@@ -745,9 +745,10 @@
 to future change.
 
 If arguments remain unused after formatting, the format string is reused
-until all arguments have been consumed. If more arguments are required by
-the format than have been specified, the behaviour is as if zero or an
-empty string had been specified as the argument.
+until all arguments have been consumed. With the tt(print) builtin, this
+can be suppressed by using the tt(-r) option. If more arguments are
+required by the format than have been specified, the behaviour is as if
+zero or an empty string had been specified as the argument.
 )
 findex(pushd)
 pindex(PUSHD_TO_HOME, use of)
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.61
diff -u -r1.61 builtin.c
--- Src/builtin.c	2001/10/23 11:22:34	1.61
+++ Src/builtin.c	2001/11/09 16:23:10
@@ -2970,6 +2970,25 @@
 	}
     }
 
+    /* -o and -O -- sort the arguments */
+    if (ops['o']) {
+	if (fmt && !*args) return 0;
+	if (ops['i'])
+	    qsort(args, arrlen(args), sizeof(char *), cstrpcmp);
+	else
+	    qsort(args, arrlen(args), sizeof(char *), strpcmp);
+    } else if (ops['O']) {
+	if (fmt && !*args) return 0;
+	if (ops['i'])
+	    qsort(args, arrlen(args), sizeof(char *), invcstrpcmp);
+	else
+	    qsort(args, arrlen(args), sizeof(char *), invstrpcmp);
+    }
+    /* after sorting arguments, recalculate lengths */
+    if(ops['o'] || ops['O'])
+	for(n = 0; n < argc; n++)
+	    len[n] = strlen(args[n]);
+
     /* -z option -- push the arguments onto the editing buffer stack */
     if (ops['z']) {
 	queue_signals();
@@ -3028,25 +3047,6 @@
 	}
     }
 
-    /* -o and -O -- sort the arguments */
-    if (ops['o']) {
-	if (fmt && !*args) return 0;
-	if (ops['i'])
-	    qsort(args, arrlen(args), sizeof(char *), cstrpcmp);
-	else
-	    qsort(args, arrlen(args), sizeof(char *), strpcmp);
-    } else if (ops['O']) {
-	if (fmt && !*args) return 0;
-	if (ops['i'])
-	    qsort(args, arrlen(args), sizeof(char *), invcstrpcmp);
-	else
-	    qsort(args, arrlen(args), sizeof(char *), invstrpcmp);
-    }
-    /* after sorting arguments, recalculate lengths */
-    if(ops['o'] || ops['O'])
-	for(n = 0; n < argc; n++)
-	    len[n] = strlen(args[n]);
-
     /* -c -- output in columns */
     if (ops['c']) {
 	int l, nc, nr, sc, n, t, i;
@@ -3230,7 +3230,15 @@
 		if (curarg) {
 		    int l;
 		    char *b = getkeystring(curarg, &l, ops['b'] ? 2 : 0, &nnl);
+		    /* handle width/precision here and use fwrite so that
+		     * nul characters can be output */
+		    if (prec >= 0 && prec < l) l = prec;
+		    if (width > 0 && flags[2]) width = -width;
+		    if (width > 0 && l < width)
+		    	printf("%*c", width - l, ' ');
 		    fwrite(b, l, 1, fout);
+		    if (width < 0 && l < -width)
+		    	printf("%*c", -width - l, ' ');
 		    count += l;
 		}
 		break;

_____________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp



Messages sorted by: Reverse Date, Date, Thread, Author