On Tue, Dec 11, 2012 at 2:59 PM, Irek Szczesniak <[email protected]> wrote:
> On Mon, Dec 10, 2012 at 7:39 PM, David Korn <[email protected]> wrote:
>> cc: [email protected]
>> Subject: Re: [ast-developers] [rfe] typeset -B to define a (C99-like)
>> <stdbool.h> datatype
>> --------
>>
>>> I'd like to propose a new typeset option to define a new boolean
>>> datatype which works like C99 "bool" datatype defined in <stdbool.h>,
>>> e.g. typeset -B bl; integer i; (( bl=true, i=bl )) ; print "$bl\n$i"
>>> prints $'true\n1\n'
>>>
>>> The purpose is to have a (memory space-)efficient way to define a
>>> boolean datatype in shell scripts which can be used with arithmetic
>>> expressions and arrays and works exactly like the "bool" datatype
>>> defined in <stdbool.h>.
>>> I know it is possible to define such a datatype with typeset -T right
>>> now, however we'd like to use such this datatype primarily in very
>>> large arrays, in which case usage of user-defined types becomes a
>>> memory usage nightmare. That's why "hardcoded" support via typeset -B
>>> would be beneficial - it could use bitfields instead of a nval
>>> structure and use two bits to represent a typeset -B array entry - one
>>> bit is used for the value and a 2nd bit defines whether the array
>>> entry is actually defined or not so that sparse bitfield arrays are
>>> possible. This should be at least 80 times (sizeof(struct
>>> nval)==5*sizeof(void*) vs 2 bits) more space efficient than any
>>> possible implementation via typeset -T.
>>>
>>> Irek
>>> _______________________________________________
>>> ast-developers mailing list
>>> [email protected]
>>> http://lists.research.att.com/mailman/listinfo/ast-developers
>>>
>>
>> The shell already has enumeration types. Maybe I should look
>> for an efficient way to store an indexed array of enumeration types.
>> The you can do
>> enum Bool=(true false)
>> Bool x=(true false false true false)
>> to create an indexed array of 5 elements.
>
> I have two concerns:
> 1. ksh -c 'enum bool=(false true) ; bool -a bar ; integer i=5 ;
> bar[3]=true ; print $(( i+bar[3] ))' prints 5, not 6. There seems to
> be a bug.
AFAIK this is a bug... the same example works if you use an associative array.
> 2. The request is to have the bool type enabled by default, as the
> bool datatype is a mandatory part of C99.
Attached (as "ksh93_bool_prototype20121211_001.diff.txt") is a
prototype patch for ast-ksh.2012-11-21 which implements such a "Bool"
datatype more or less along the conventions defined by <stdbool.h>
([1]):
1. "bool" is an alias pointing to "_Bool" (this gives an easy way to
disable the definition of the "bool" datatype)
2. "_Bool" is defined as $ enum _Bool=(false true) #
Example usage:
-- snip --
$ ksh -c 'bool b=true ; print $(( b?4:3 )) $(( x+1 ))'
4 1
-- snip --
The patch seems to work except for this ksh93 test suite "hit":
-- snip --
test attributes begins at 2012-12-11+23:59:07
attributes.sh[436]: typeset -pa does not list only index arrays
test attributes failed at 2012-12-11+23:59:09 with exit code 1 [ 111
tests 1 error ]
-- snip --
This failure seems to be a result from the ksh93 implementation detail
for "enum"'s - declaring an "enum" type creates an indexed array of
the name of the "enum" ... which causes $ typeset -pa # to show a
non-empty output.
Erm... David/Glenn/Irek: What do you think about the prototype ?
[1]=The POSIX stdbool.h manpage says this:
-- snip --
NAME
stdbool.h - boolean type and values
SYNOPSIS
#include <stdbool.h>
DESCRIPTION
The <stdbool.h> header shall define the following macros:
bool Expands to _Bool.
true Expands to the integer constant 1.
false Expands to the integer constant 0.
__bool_true_false_are_defined
Expands to the integer constant 1.
An application may undefine and then possibly redefine the
macros bool, true, and false.
The following sections are informative.
-- snip --
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) [email protected]
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
diff -r -u original/src/cmd/ksh93/data/aliases.c
build_bool/src/cmd/ksh93/data/aliases.c
--- src/cmd/ksh93/data/aliases.c 2012-06-12 21:00:58.000000000 +0200
+++ src/cmd/ksh93/data/aliases.c 2012-12-11 23:21:46.073236479 +0100
@@ -33,6 +33,7 @@
"2d", NV_NOFREE, "set -f;_2d",
#endif /* SHOPT_FS_3D */
"autoload", NV_NOFREE, "typeset -fu",
+ "bool", NV_NOFREE|BLT_DCL, "_Bool",
"command", NV_NOFREE, "command ",
"compound", NV_NOFREE|BLT_DCL, "typeset -C",
"fc", NV_NOFREE, "hist",
diff -r -u original/src/cmd/ksh93/sh/main.c build_bool/src/cmd/ksh93/sh/main.c
--- src/cmd/ksh93/sh/main.c 2012-11-20 18:09:41.000000000 +0100
+++ src/cmd/ksh93/sh/main.c 2012-12-11 23:54:27.840396883 +0100
@@ -240,6 +240,9 @@
else if(sh_isoption(shp,SH_INTERACTIVE) &&
sh_isoption(shp,SH_PRIVILEGED))
sh_source(shp, iop, e_suidprofile);
}
+
+ sh_trap(shp,"enum _Bool=(false true) ;",0);
+
shp->st.cmdname = error_info.id = command;
sh_offstate(shp,SH_PROFILE);
if(rshflag)
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers