Discussion:
[ast-developers] typeset in signal trap ==> compound array contains typeset statements
Cedric Blancher
2013-08-08 22:42:12 UTC
Permalink
Can anyone explain why the following code contains FOUR array elements
instead of two? I'm getting desperate with this. I only want to
declare c.car[index].int as a integer variable (aka typeset -li) but
receive this garbage instead when running it with ast-ksh.20130807:

ksh -c 'set -o nounset ; compound c ; compound -a c.car; integer
c.cari=0; trap "typeset -i c.car[c.cari++].int=\${.sh.sig.value.int}"
RTMIN RTMAX ; kill -q4 -s RTMIN $$ ; true ; kill -q17 -s RTMAX $$ ;
true ; print -v c'
(
typeset -C -a car=(
[0]=(
int=4
)
[1]=(
typeset -i int=0
)
[2]=(
int=17
)
[3]=(
typeset -i int=0
)
)
typeset -l -i cari=4
)

Ced
--
Cedric Blancher <cedric.blancher at gmail.com>
Institute Pasteur
David Korn
2013-08-08 23:00:37 UTC
Permalink
cc: cedric.blancher at gmail.com
Subject: Re: [ast-developers] typeset in signal trap ==> compound array contains typeset statements
--------
Post by Cedric Blancher
Can anyone explain why the following code contains FOUR array elements
instead of two? I'm getting desperate with this. I only want to
declare c.car[index].int as a integer variable (aka typeset -li) but
ksh -c 'set -o nounset ; compound c ; compound -a c.car; integer
c.cari=0; trap "typeset -i c.car[c.cari++].int=\${.sh.sig.value.int}"
RTMIN RTMAX ; kill -q4 -s RTMIN $$ ; true ; kill -q17 -s RTMAX $$ ;
true ; print -v c'
(
typeset -C -a car=(
[0]=(
int=4
)
[1]=(
typeset -i int=0
)
[2]=(
int=17
)
[3]=(
typeset -i int=0
)
)
typeset -l -i cari=4
)
Ced
--
Cedric Blancher <cedric.blancher at gmail.com>
Institute Pasteur
I can explain why it is happening, but I haven't decided how to resolve it.

typeset foo=bar
is parsed as if it is
foo=bar typeset foo

However since you have
typeset foo[i++]=bar
this becomes
foo[i++]=bar typeset foo[i++]

Your workaround is to do
typeset -i c.car
before the trap and do
trap "tc.car[c.cari++].int=\${.sh.sig.value.int}"
for the trap.

David Korn
dgk at research.att.com
Roland Mainz
2013-08-08 23:07:27 UTC
Permalink
Post by David Korn
cc: cedric.blancher at gmail.com
Subject: Re: [ast-developers] typeset in signal trap ==> compound array contains typeset statements
--------
Post by Cedric Blancher
Can anyone explain why the following code contains FOUR array elements
instead of two? I'm getting desperate with this. I only want to
declare c.car[index].int as a integer variable (aka typeset -li) but
ksh -c 'set -o nounset ; compound c ; compound -a c.car; integer
c.cari=0; trap "typeset -i c.car[c.cari++].int=\${.sh.sig.value.int}"
RTMIN RTMAX ; kill -q4 -s RTMIN $$ ; true ; kill -q17 -s RTMAX $$ ;
true ; print -v c'
(
typeset -C -a car=(
[0]=(
int=4
)
[1]=(
typeset -i int=0
)
[2]=(
int=17
)
[3]=(
typeset -i int=0
)
)
typeset -l -i cari=4
)
I can explain why it is happening, but I haven't decided how to resolve it.
typeset foo=bar
is parsed as if it is
foo=bar typeset foo
However since you have
typeset foo[i++]=bar
this becomes
foo[i++]=bar typeset foo[i++]
Your workaround is to do
typeset -i c.car
before the trap and do
trap "tc.car[c.cari++].int=\${.sh.sig.value.int}"
for the trap.
Mhhh... a workaround may be this one:
-- snip --
$ cat test1.sh
set -o nounset
compound c
compound -a c.car
integer c.cari=0
integer trap_i # only be used by trap below

trap '(( trap_i=c.cari++ )) ; typeset -i
c.car[trap_i].int=${.sh.sig.value.int}' RTMIN RTMAX
kill -q4 -s RTMIN $$
kill -q17 -s RTMAX $$

print -v c

$ ./arch/linux.i386-64/bin/ksh -x test1.sh
+ set -o nounset
+ typeset -C c
+ typeset -C -a c.car
+ c.cari=0
+ typeset -li c.cari
+ typeset -li trap_i
+ trap '(( trap_i=c.cari++ )) ; typeset -i
c.car[trap_i].int=${.sh.sig.value.int}' RTMIN RTMAX
+ kill -q4 -s RTMIN 55281
+ (( trap_i=c.cari++ ))
+ c.car[0].int=4
+ typeset -i 'c.car[trap_i].int'
+ kill -q17 -s RTMAX 55281
+ (( trap_i=c.cari++ ))
+ c.car[1].int=17
+ typeset -i 'c.car[trap_i].int'
+ print -v c
(
typeset -C -a car=(
[0]=(
typeset -i int=4
)
[1]=(
typeset -i int=17
)
)
typeset -l -i cari=2
)
-- snip --

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
Irek Szczesniak
2013-08-09 11:12:49 UTC
Permalink
Post by David Korn
cc: cedric.blancher at gmail.com
Subject: Re: [ast-developers] typeset in signal trap ==> compound array contains typeset statements
--------
Post by Cedric Blancher
Can anyone explain why the following code contains FOUR array elements
instead of two? I'm getting desperate with this. I only want to
declare c.car[index].int as a integer variable (aka typeset -li) but
ksh -c 'set -o nounset ; compound c ; compound -a c.car; integer
c.cari=0; trap "typeset -i c.car[c.cari++].int=\${.sh.sig.value.int}"
RTMIN RTMAX ; kill -q4 -s RTMIN $$ ; true ; kill -q17 -s RTMAX $$ ;
true ; print -v c'
(
typeset -C -a car=(
[0]=(
int=4
)
[1]=(
typeset -i int=0
)
[2]=(
int=17
)
[3]=(
typeset -i int=0
)
)
typeset -l -i cari=4
)
Ced
--
Cedric Blancher <cedric.blancher at gmail.com>
Institute Pasteur
I can explain why it is happening, but I haven't decided how to resolve it.
typeset foo=bar
is parsed as if it is
foo=bar typeset foo
However since you have
typeset foo[i++]=bar
this becomes
foo[i++]=bar typeset foo[i++]
this is wrong because i++ would be evaluated twice
Post by David Korn
Your workaround is to do
typeset -i c.car
before the trap and do
trap "tc.car[c.cari++].int=\${.sh.sig.value.int}"
for the trap.
This doesn't work. How is it supposed to work in your opinion?

Irek

Loading...