zsh-workers
 help / color / mirror / code / Atom feed
* printf %b
@ 2006-12-27 14:58 Stephane Chazelas
  2007-01-04 15:36 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2006-12-27 14:58 UTC (permalink / raw)
  To: Zsh hackers list

Hiya,

just noticed that

printf %b '\0123'

doesn't work anymore as it used to and as SUSv3 specifies
(should output S, and not <NL>3).

echo '\0123'
and
printf '\123'

are correct though.

With 4.3.2-dev-1+20061217-1 (debian package) both in zsh and sh
emulation.

regards,
Stéphane


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

* Re: printf %b
  2006-12-27 14:58 printf %b Stephane Chazelas
@ 2007-01-04 15:36 ` Peter Stephenson
  2007-01-04 16:32   ` Stephane Chazelas
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2007-01-04 15:36 UTC (permalink / raw)
  To: Zsh hackers list

Stephane Chazelas <Stephane_Chazelas@yahoo.fr> wrote:
> Hiya,
> 
> just noticed that
> 
> printf %b '\0123'
> 
> doesn't work anymore as it used to and as SUSv3 specifies
> (should output S, and not <NL>3).

Thanks for noticing.  That was a mistake when I updated the getkeystring()
interface from it's previous Chasm-City-after-the-melding-plague look.

The following restores the previous behaviour.  However, now I look at it,
it all seems a bit weird.  We will either handle \0DDD, or \DDD (D is 0 to
7), but not both.  So

print '\123'

outputs S, but

print '\0123'

outputs newline + '3'.

It's not necessarily correct that those that currently handle '\0123'
shouldn't handle '\123': presumably this is supposed to impose the
standardised form as written, but actually bash allows the shorthand, i.e.
  printf %b '\0123'
  printf %b '\123'
are both handled.  (This isn't true of echo, which passes \123 straight
through... this is the same as zsh.)  It appears to be intelligent enough
to handle '\012S' as a newline and an S, too.  /usr/bin/printf on both GNU
and Solaris only accepts '\123' (i.e. the first is newline + 3), so this is
like the behaviour *before* the patch below!

It's even less clear the other way round; unless I've missed something it's
not even documented that this is how print with no options works.  The
documentation says:

- \0NNN works for "echo" (OK)
- printf's arguments are like echo (OK, need \0NNN.
  However, Stephane... this means that
    printf '\123'
  *doesn't* print S any more, it needs '\0123', too; that's changed
  back with the fix below.)
- print without options is the same as echo except for some additions
  (\C-..., \M-..., \E) (WRONG: \0NNN doesn't work but \NNN does)
- \NNN works for bindkey sequences (and hence for "print -b")
  (OK).

The same goes for \0xNN and \xNN.

My first inclination is to bury my head in the sand and pretend it's not
happening, but maybe we can do better.

- Handling \0NNN everywhere is a bit fraught, since it changes the
meaning of three-digit codes with another digit following.
- Handling \NNN in printf is less problematic: it's an incompatibility
but given the bash behaviour people wouldn't necessarily have assumed
this was passed straight through on the occasions when \0NNN was handled.
(We definitely don't want to handle \NNN specially in echo.)

I will anyway commit the following to restore the old behaviour.


Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.103
diff -u -r1.103 zsh.h
--- Src/zsh.h	3 Dec 2006 21:07:18 -0000	1.103
+++ Src/zsh.h	4 Jan 2007 14:57:28 -0000
@@ -1955,7 +1955,8 @@
 enum {
     /*
      * Handle octal where the first digit is non-zero e.g. \3, \33, \333
-     * \0333 etc. is always handled.
+     * Otherwise \0333 etc. is handled, i.e. one of \0123 or \123 will
+     * work, but not both.
      */
     GETKEY_OCTAL_ESC = (1 << 0),
     /*
@@ -1990,7 +1991,7 @@
 /* echo builtin */
 #define GETKEYS_ECHO	(GETKEY_BACKSLASH_C)
 /* printf format string */
-#define GETKEYS_PRINTF	(GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C)
+#define GETKEYS_PRINTF	(GETKEY_BACKSLASH_C)
 /* Full print without -e */
 #define GETKEYS_PRINT	(GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C|GETKEY_EMACS)
 /* bindkey */
Index: Test/B03print.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/B03print.ztst,v
retrieving revision 1.16
diff -u -r1.16 B03print.ztst
--- Test/B03print.ztst	23 Sep 2006 06:55:29 -0000	1.16
+++ Test/B03print.ztst	4 Jan 2007 14:57:28 -0000
@@ -259,3 +259,7 @@
  printf $'%\0'
 1:explicit null after % in format specifier
 ?(eval):printf:1: %: invalid directive
+
+ printf '%b\n' '\0123'
+0:printf handles \0... octal escapes in replacement text
+>S


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: printf %b
  2007-01-04 15:36 ` Peter Stephenson
@ 2007-01-04 16:32   ` Stephane Chazelas
  2007-01-04 17:07     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2007-01-04 16:32 UTC (permalink / raw)
  To: Zsh hackers list

On Thu, Jan 04, 2007 at 03:36:12PM +0000, Peter Stephenson wrote:
[...]
> - \0NNN works for "echo" (OK)
> - printf's arguments are like echo (OK, need \0NNN.
>   However, Stephane... this means that
>     printf '\123'
>   *doesn't* print S any more, it needs '\0123', too; that's changed
>   back with the fix below.)
> - print without options is the same as echo except for some additions
>   (\C-..., \M-..., \E) (WRONG: \0NNN doesn't work but \NNN does)
> - \NNN works for bindkey sequences (and hence for "print -b")
>   (OK).
> 
> The same goes for \0xNN and \xNN.
[...]

Hi Peter,

POSIX says

printf '\123'
and
printf %b '\0123'

should output "S" (note that the two syntaxes are different!)

and

printf '\0123' should output <NL>3
printf %b '\123' should output \123 (I think).

Cheers,
Stéphane


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

* Re: printf %b
  2007-01-04 16:32   ` Stephane Chazelas
@ 2007-01-04 17:07     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2007-01-04 17:07 UTC (permalink / raw)
  To: Zsh hackers list

Stephane Chazelas wrote:
> POSIX says
> 
> printf '\123'
> and
> printf %b '\0123'
> 
> should output "S" (note that the two syntaxes are different!)
> 
> and
> 
> printf '\0123' should output <NL>3
> printf %b '\123' should output \123 (I think).

Thanks, I hadn't found that bit.  At least the really world is screwed
up, too.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.171
diff -u -r1.171 builtin.c
--- Src/builtin.c	10 Dec 2006 23:27:03 -0000	1.171
+++ Src/builtin.c	4 Jan 2007 17:05:53 -0000
@@ -3490,7 +3490,7 @@
 	fmt = OPT_ARG(ops,'f');
     if (fmt)
 	fmt = getkeystring(fmt, &flen, OPT_ISSET(ops,'b') ? GETKEYS_BINDKEY :
-			   GETKEYS_PRINTF, &fmttrunc);
+			   GETKEYS_PRINTF_FMT, &fmttrunc);
 
     first = args;
     
@@ -3954,7 +3954,7 @@
 			b = getkeystring(metafy(curarg, curlen, META_USEHEAP),
 					 &l,
 					 OPT_ISSET(ops,'b') ? GETKEYS_BINDKEY :
-					 GETKEYS_PRINTF, &nnl);
+					 GETKEYS_PRINTF_ARG, &nnl);
 		    } else {
 			b = curarg;
 			l = curlen;
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.104
diff -u -r1.104 zsh.h
--- Src/zsh.h	4 Jan 2007 15:41:23 -0000	1.104
+++ Src/zsh.h	4 Jan 2007 17:05:55 -0000
@@ -1990,8 +1990,10 @@
  */
 /* echo builtin */
 #define GETKEYS_ECHO	(GETKEY_BACKSLASH_C)
-/* printf format string */
-#define GETKEYS_PRINTF	(GETKEY_BACKSLASH_C)
+/* printf format string:  \123 -> S, \0123 -> NL 3 */
+#define GETKEYS_PRINTF_FMT	(GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C)
+/* printf argument:  \123 -> \123, \0123 -> S */
+#define GETKEYS_PRINTF_ARG	(GETKEY_BACKSLASH_C)
 /* Full print without -e */
 #define GETKEYS_PRINT	(GETKEY_OCTAL_ESC|GETKEY_BACKSLASH_C|GETKEY_EMACS)
 /* bindkey */

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

end of thread, other threads:[~2007-01-04 17:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-27 14:58 printf %b Stephane Chazelas
2007-01-04 15:36 ` Peter Stephenson
2007-01-04 16:32   ` Stephane Chazelas
2007-01-04 17:07     ` Peter Stephenson

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).