On Fri, Jan 13, 2017 at 10:20:19AM +0100, Otto Moerbeek wrote:

> On Fri, Jan 13, 2017 at 09:51:51AM +0100, Otto Moerbeek wrote:
> 
> > On Fri, Jan 13, 2017 at 09:46:54AM +0100, Anton Lindqvist wrote:
> > 
> > > Consider the following script which calculates the sum of the first N
> > > integers recursively:
> > > 
> > > $ cat >sum.sh <<!
> > > sum() {
> > >   [ $1 -eq 0 ] && echo $2 || sum $(($1 - 1)) $(($2 + $1))
> > > }
> > > 
> > > sum 5
> > > !
> > > 
> > > Executing the script with the x option gives the following output:
> > > 
> > > $ sh -x sum.sh
> > > + sum 5
> > > 15
> > > 
> > > I would expect the recursive calls to be traced, similar to how GNU
> > > bash/sh behaves. With the patch below applied the output is as expected:
> > > 
> > > $ sh -x sum.sh
> > > + sum 5
> > > + [ 5 -eq 0 ]
> > > + sum 4 5
> > > + [ 4 -eq 0 ]
> > > + sum 3 9
> > > + [ 3 -eq 0 ]
> > > + sum 2 12
> > > + [ 2 -eq 0 ]
> > > + sum 1 14
> > > + [ 1 -eq 0 ]
> > > + sum 0 15
> > > + [ 0 -eq 0 ]
> > > + echo 15
> > > 15
> > > 
> > > The patch make sure to assigns the TRACE flag to every user-defined
> > > function if the x option is present. The piece of code that led me to
> > > this:
> > > 
> > > $ sed -n 606,607p /usr/src/bin/ksh/exec.c
> > > old_xflag = Flag(FXTRACE);
> > > Flag(FXTRACE) = tp->flag & TRACE ? true : false;
> > 
> > Hmmm,
> > 
> > afaik -x has always been local to a function in ksh-like shells.
> > To enable tracing within function call set -x in the fucntion te be traced.
> 
> or set the trace attribute for a specific function:
> 
> typeset -ft sum

Hmm, I see now that my memory is false, ksh93 (available as port) does
trace into functions. Still, I'm undecided if we want to change our
ksh.

        -Otto

> > 
> > > 
> > > Index: exec.c
> > > ===================================================================
> > > RCS file: /cvs/src/bin/ksh/exec.c,v
> > > retrieving revision 1.68
> > > diff -u -p -r1.68 exec.c
> > > --- exec.c        11 Dec 2016 17:49:19 -0000      1.68
> > > +++ exec.c        12 Jan 2017 20:10:48 -0000
> > > @@ -796,6 +796,9 @@ define(const char *name, struct op *t)
> > >   if (t->u.ksh_func)
> > >           tp->flag |= FKSH;
> > >  
> > > + if (Flag(FXTRACE))
> > > +         tp->flag |= TRACE;
> > > +
> > >   return 0;
> > >  }

Reply via email to