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

Re: printf \045 (or whatever the character code for % is)



On Jan 6, 12:09pm, Peter Stephenson wrote:
> Subject: Re: printf \045 (or whatever the character code for % is)
>
> On Wed, 5 Jan 2011 20:46:12 -0800
> Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > GETKEYS_PRINTF_FMT expands to GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C ...
> > seems as though an additional flag to getkeystring() could be used to
> > cause \045 to expand to %% as a special case, something like this in
> > utils.c:
> 
> That's sneaky, that should be OK.
> 
> Presumably since we're contracting an escape sequence there's always enough
> allocated space for the extra '%'.

It's a bit hard to follow getkeystring() in the multibyte branches,
but as that should always be allocating more rather than less space,
I believe the answer is "that's correct".  Minimum it's \45 -> %%.

> Unless we go down the route of separate builtin handlers, I think it
> would be better to keep printf and print -f in sync for now.

In that case, no need to touch builtin.c at all.

> [...] we haven't yet gone into the details of where printf actually
> needs to be different from print (we'd need to look at the relevant
> standards for printf to see where the code is doing the wrong thing at
> present).

For one thing, hasn't austin-group been discussing \Cx where zsh at
present uses the old Emacs syntax of \C-x ?

(Revision numbers below are from my local repository, ignore them.)

Index: Src/utils.c
===================================================================
RCS file: /extra/cvsroot/zsh/zsh-4.0/Src/utils.c,v
retrieving revision 1.40
diff -c -r1.40 utils.c
--- utils.c	21 Dec 2010 16:41:16 -0000	1.40
+++ utils.c	6 Jan 2011 15:43:40 -0000
@@ -5517,6 +5522,8 @@
 		    }
 		    *t++ = zstrtol(s + (*s == 'x'), &s,
 				   (*s == 'x') ? 16 : 8);
+		    if ((how & GETKEY_PRINTF_PERCENT) && t[-1] == '%')
+		        *t++ = '%';
 		    if (svchar) {
 			u[3] = svchar;
 			svchar = '\0';
Index: Src/zsh.h
===================================================================
RCS file: /extra/cvsroot/zsh/zsh-4.0/Src/zsh.h,v
retrieving revision 1.43
diff -c -r1.43 zsh.h
--- zsh.h	21 Dec 2010 16:41:16 -0000	1.43
+++ zsh.h	6 Jan 2011 15:50:06 -0000
@@ -2492,7 +2492,11 @@
      * Yes, I know that doesn't seem to make much sense.
      * It's for use in completion, comprenez?
      */
-    GETKEY_UPDATE_OFFSET = (1 << 7)
+    GETKEY_UPDATE_OFFSET = (1 << 7),
+    /*
+     * When replacing numeric escapes for printf format strings, % -> %%
+     */
+    GETKEY_PRINTF_PERCENT = (1 << 8)
 };
 
 /*
@@ -2501,8 +2505,9 @@
  */
 /* echo builtin */
 #define GETKEYS_ECHO	(GETKEY_BACKSLASH_C)
-/* printf format string:  \123 -> S, \0123 -> NL 3 */
-#define GETKEYS_PRINTF_FMT	(GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C)
+/* printf format string:  \123 -> S, \0123 -> NL 3, \045 -> %% */
+#define GETKEYS_PRINTF_FMT	\
+        (GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C|GETKEY_PRINTF_PERCENT)
 /* printf argument:  \123 -> \123, \0123 -> S */
 #define GETKEYS_PRINTF_ARG	(GETKEY_BACKSLASH_C)
 /* Full print without -e */



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