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

PATCH: C_BASES option for outputting hex and octal



Does anyone object to my adding options for trivial things like this?
Because if so they ought to have got the point and given up objecting years
ago :-/.  (I finally got fed up with 0x${$(([#16]$num))##*\#}.)

If it weren't for backward compatibility, I'd set C_BASES by default and
just unset it for ksh compatibility.  But the current format could have
worked its way into a lot of scripts.

% print $(( [#16] 0xff ))
16#FF
% setopt cbases
% print $(( [#16] 0xff ))
0xFF
% print $(( [#8] 8#77 ))
8#77
% setopt octalzeroes
% print $(( [#8] 077 ))
077

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.11
diff -u -r1.11 options.yo
--- Doc/Zsh/options.yo	2000/08/03 07:51:53	1.11
+++ Doc/Zsh/options.yo	2000/08/10 16:05:06
@@ -206,6 +206,19 @@
 This disables backslashed escape sequences in echo strings unless the
 tt(-e) option is specified.
 )
+pindex(C_BASES)
+cindex(bases, output in C format)
+cindex(hexadecimal, output in C format)
+cindex(octal, output in C format)
+item(tt(C_BASES))(
+Output hexadecimal numbers in the standard C format, for example `tt(0xFF)'
+instead of the usual `tt(16#FF)'.  If the option tt(OCTAL_ZEROES) is also
+set (it is not by default), octal numbers will be treated similarly and
+hence appear as `tt(077)' instead of `tt(8#77)'.  This option has no effect
+on the choice of the output base, nor on the output of bases other than
+hexadecimal and octal.  Note that these formats will be understood on input
+irrespective of the setting of tt(C_BASES).
+)
 pindex(CDABLE_VARS)
 cindex(cd, to parameter)
 item(tt(CDABLE_VARS) (tt(-T)))(
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.5
diff -u -r1.5 options.c
--- Src/options.c	2000/07/30 17:03:53	1.5
+++ Src/options.c	2000/08/10 16:05:07
@@ -90,6 +90,7 @@
 {NULL, "bgnice",	      OPT_EMULATE|OPT_NONBOURNE, BGNICE},
 {NULL, "braceccl",	      OPT_EMULATE,		 BRACECCL},
 {NULL, "bsdecho",	      OPT_EMULATE|OPT_SH,	 BSDECHO},
+{NULL, "cbases",	      0,			 CBASES},
 {NULL, "cdablevars",	      OPT_EMULATE,		 CDABLEVARS},
 {NULL, "chasedots",	      OPT_EMULATE,		 CHASEDOTS},
 {NULL, "chaselinks",	      OPT_EMULATE,		 CHASELINKS},
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.25
diff -u -r1.25 params.c
--- Src/params.c	2000/08/03 13:10:13	1.25
+++ Src/params.c	2000/08/10 16:05:07
@@ -3045,7 +3045,12 @@
 	base = 10;
 
     if (base != 10) {
-	sprintf(s, "%d#", base);
+	if (isset(CBASES) && base == 16)
+	    sprintf(s, "0x");
+	else if (isset(CBASES) && base == 8 && isset(OCTALZEROES))
+	    sprintf(s, "0");
+	else
+	    sprintf(s, "%d#", base);
 	s += strlen(s);
     }
     for (x = v; x; digs++)
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.20
diff -u -r1.20 zsh.h
--- Src/zsh.h	2000/08/08 09:13:37	1.20
+++ Src/zsh.h	2000/08/10 16:05:07
@@ -1326,6 +1326,7 @@
     BGNICE,
     BRACECCL,
     BSDECHO,
+    CBASES,
     CDABLEVARS,
     CHASEDOTS,
     CHASELINKS,

-- 
Peter Stephenson <pws@xxxxxxx>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070



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