Discussion:
[ast-developers] Create _Bool variable from C code?
Simon Toedt
2013-03-22 03:14:16 UTC
Permalink
I'm trying to teach me to write my own shell using libshell but hit
two questions:
1. How can I create an enum variable of type _Bool (i.e. the builtin
"bool" type) from C code using the nv* functions and without resorting
to using b_eval()?
2. How can I test whether a variable opened with nv_open is of type _Bool?

Simon
David Korn
2013-03-22 17:42:05 UTC
Permalink
cc: simon.toedt at gmail.com
Subject: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
I'm trying to teach me to write my own shell using libshell but hit
1. How can I create an enum variable of type _Bool (i.e. the builtin
"bool" type) from C code using the nv* functions and without resorting
to using b_eval()?
In ksh93u+, you can do
enum _Bool=(false true)
In ksh93v- this is predefined and there is a preset alias bool=_Bool.
If you are asking how to do this in C, the simplist way is with an
eval which is what ksh93v- uses. Otherwise, you need to create an
array of true and false, and apply the discipline function as is done
in bltins/enum.c. You should be able to call b_enum() to do this.
Post by Simon Toedt
2. How can I test whether a variable opened with nv_open is of type _Bool?
print ${@var}
If you are asking how to do this from C code, use nv_type(np) where np
is a pointer to a variable. It returns a pointer to the type of NULL
if there is no type.
Post by Simon Toedt
Simon
David Korn
dgk at research.att.com
Simon Toedt
2013-03-22 19:31:48 UTC
Permalink
Post by David Korn
cc: simon.toedt at gmail.com
Subject: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
I'm trying to teach me to write my own shell using libshell but hit
1. How can I create an enum variable of type _Bool (i.e. the builtin
"bool" type) from C code using the nv* functions and without resorting
to using b_eval()?
In ksh93u+, you can do
enum _Bool=(false true)
In ksh93v- this is predefined and there is a preset alias bool=_Bool.
If you are asking how to do this in C, the simplist way is with an
eval which is what ksh93v- uses. Otherwise, you need to create an
array of true and false, and apply the discipline function as is done
in bltins/enum.c. You should be able to call b_enum() to do this.
I want to do this with ksh93v- only. I want to create a *variable* of
type _Bool, i.e. the C equivalent of this shell code:
bool myboolean=true

Of course this is translated to:
_Bool myboolean=true

How do I do that from C?
Post by David Korn
Post by Simon Toedt
2. How can I test whether a variable opened with nv_open is of type _Bool?
If you are asking how to do this from C code, use nv_type(np) where np
is a pointer to a variable. It returns a pointer to the type of NULL
if there is no type.
if I have the type pointer from nv_type(np), how can I figure out if
the type is of type _Bool?
Post by David Korn
Post by Simon Toedt
Simon
David Korn
dgk at research.att.com
Simon
David Korn
2013-03-22 20:04:51 UTC
Permalink
cc: ast-developers at lists.research.att.com
Subject: Re: Re: [ast-developers] Create _Bool variable from C code?
--------

The function you need to call is
nv_settype()

It needs the node corresponding to the variable name which it can get
with nv_open() and the type
which it can get by looking up "_Bool" in the builtin table, sh.bltin_tree.

David Korn
dgk at research.att.com
Simon Toedt
2013-03-22 20:52:16 UTC
Permalink
Post by David Korn
cc: ast-developers at lists.research.att.com
Subject: Re: Re: [ast-developers] Create _Bool variable from C code?
--------
The function you need to call is
nv_settype()
It needs the node corresponding to the variable name which it can get
with nv_open() and the type
which it can get by looking up "_Bool" in the builtin table, sh.bltin_tree.
OK, I will try that.

How can I figure out if the type is of type _Bool when I have the
pointer returned by nv_type(np)?

Simon
David Korn
2013-03-22 21:58:02 UTC
Permalink
cc: ast-developers at lists.research.att.com
Subject: Re: Re: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
How can I figure out if the type is of type _Bool when I have the
pointer returned by nv_type(np)?
nv_type(np)->nvname

David Korn
dgk at research.att.com
Simon Toedt
2013-07-17 04:44:23 UTC
Permalink
Post by David Korn
cc: ast-developers at lists.research.att.com
Subject: Re: Re: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
How can I figure out if the type is of type _Bool when I have the
pointer returned by nv_type(np)?
nv_type(np)->nvname
I tried the suggestion and was granted a SEGV ;(

Does this look correct to you?

bt=nv_search("_Bool",shp->bltin_tree,0);
np = nv_open(shp->var_tree,...);
nv_settype(np, bt, 0);

The code gives me this SEGV:
(gdb) where
#0 0x0000000000803200 in sh ()
#1 0x0000000000495e91 in clone_all_disc (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:893
#2 0x00000000004960d0 in nv_clone (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:944
#3 0x000000000049cd16 in nv_settype (np=0x7ffff7f29ca0,
tp=0x7ffff7ee2700, flags=0) at src/cmd/ksh93/sh/nvtype.c:1437

Simon
ольга крыжановская
2013-07-17 13:40:38 UTC
Permalink
Simon, this sequence should work:

bnp=nv_search("_Bool",shp->bltin_tree,0);
b_t=nv_type(bnp); // get type node
np = nv_open(shp->var_tree,...);
nv_settype(np, b_t, 0);

Olga
Post by Simon Toedt
Post by David Korn
cc: ast-developers at lists.research.att.com
Subject: Re: Re: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
How can I figure out if the type is of type _Bool when I have the
pointer returned by nv_type(np)?
nv_type(np)->nvname
I tried the suggestion and was granted a SEGV ;(
Does this look correct to you?
bt=nv_search("_Bool",shp->bltin_tree,0);
np = nv_open(shp->var_tree,...);
nv_settype(np, bt, 0);
(gdb) where
#0 0x0000000000803200 in sh ()
#1 0x0000000000495e91 in clone_all_disc (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:893
#2 0x00000000004960d0 in nv_clone (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:944
#3 0x000000000049cd16 in nv_settype (np=0x7ffff7f29ca0,
tp=0x7ffff7ee2700, flags=0) at src/cmd/ksh93/sh/nvtype.c:1437
Simon
_______________________________________________
ast-developers mailing list
ast-developers at lists.research.att.com
http://lists.research.att.com/mailman/listinfo/ast-developers
--
, _ _ ,
{ \/`o;====- Olga Kryzhanovska -====;o`\/ }
.----'-/`-/ olga.kryzhanovska at gmail.com \-`\-'----.
`'-..-| / http://twitter.com/fleyta \ |-..-'`
/\/\ Solaris/BSD//C/C++ programmer /\/\
`--` `--`
David Korn
2013-07-17 14:01:30 UTC
Permalink
cc: ast-developers at lists.research.att.com
Subject: Re: Re: Re: Re: [ast-developers] Create _Bool variable from C code?
--------
Post by Simon Toedt
Post by David Korn
Post by Simon Toedt
How can I figure out if the type is of type _Bool when I have the
pointer returned by nv_type(np)?
nv_type(np)->nvname
If np is not a type then nv_type() returns NULL so you can't get the
name witout first checking for the type.
Post by Simon Toedt
I tried the suggestion and was granted a SEGV ;(
Does this look correct to you?
bt=nv_search("_Bool",shp->bltin_tree,0);
np = nv_open(shp->var_tree,...);
nv_settype(np, bt, 0);
(gdb) where
#0 0x0000000000803200 in sh ()
#1 0x0000000000495e91 in clone_all_disc (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:893
#2 0x00000000004960d0 in nv_clone (np=0x7ffff7ee2700,
mp=0x7ffff7f29ca0, flags=512) at src/cmd/ksh93/sh/nvdisc.c:944
#3 0x000000000049cd16 in nv_settype (np=0x7ffff7f29ca0,
tp=0x7ffff7ee2700, flags=0) at src/cmd/ksh93/sh/nvtype.c:1437
Simon
This is not correct.
bt=nv_search("_Bool",shp->bltin_tree,0);

will give a pointer the the built-in that creates an instance of bool.
bt = nv_open(".sh.type._Bool",shp->var_base,NV_NOADD|NV_VARNAME);
should give you a pointer to the type _Bool if it exists.

David Korn
dgk at research.att.com

Loading...