zsh-workers
 help / color / mirror / code / Atom feed
* printf again
@ 2001-11-09 16:31 Oliver Kiddle
  2001-11-09 17:24 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2001-11-09 16:31 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: printf again
  2001-11-09 16:31 printf again Oliver Kiddle
@ 2001-11-09 17:24 ` Bart Schaefer
  2001-11-09 17:51   ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2001-11-09 17:24 UTC (permalink / raw)
  To: Oliver Kiddle, zsh-workers

On Nov 9,  4:31pm, Oliver Kiddle wrote:
}
} 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.

Not surprising.  You basically have to emulate the action of the printf
implementation.  This is why some C libraries have snprintf() which will
return the length of the result without actually doing the print.

I suggest you punt:  Just fprintf() to a temp file and then read it back.
If you wanted to get really fancy you could memory-map the file where
that is supported.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: printf again
  2001-11-09 17:24 ` Bart Schaefer
@ 2001-11-09 17:51   ` Oliver Kiddle
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kiddle @ 2001-11-09 17:51 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer wrote:
> 
> On Nov 9,  4:31pm, Oliver Kiddle wrote:
> }
> } 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.
> 
> Not surprising.  You basically have to emulate the action of the printf
> implementation.  This is why some C libraries have snprintf() which will
> return the length of the result without actually doing the print.
> 
> I suggest you punt:  Just fprintf() to a temp file and then read it back.
> If you wanted to get really fancy you could memory-map the file where
> that is supported.

Good, thanks, that'll be a lot easier.

I thought about mmap() mainly because I was trying to think of a way to
ensure that the OS didn't bother touching the actual disc to write a
file unless it was large. Would it be worth using any of the setbuf()
calls - I've never used them before but they look like they may do
that job. Or should I just rely on the filesystem code having some
intelligence?

I may also try out open_memstream where it is available because it is
fairly much perfect for the job.

Oliver

_____________________________________________________________________
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-11-09 17:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-09 16:31 printf again Oliver Kiddle
2001-11-09 17:24 ` Bart Schaefer
2001-11-09 17:51   ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).