bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Equivalent of ksh, zsh {N}<[WORD] ?


From: R. Bernstein
Subject: Re: Equivalent of ksh, zsh {N}<[WORD] ?
Date: Tue, 26 Aug 2008 13:58:46 -0400

Chet Ramey writes:
 > R. Bernstein wrote:
 > > Both zsh and ksh have a way to open a file or duplicate a file
 > > descriptor and let the interpreter pick the descriptor saving the
 > > newly-allocated file descriptor number in a variable. In particular:
 > > 
 > >    exec {fd}<&0
 > > 
 > > will duplicate stdin and save the newly allocated file-descriptor
 > > number to fd. Also:
 > > 
 > >    exec {fd}<filename
 > > 
 > > opens filename with a new file descriptor and saves the number
 > > allocated in fd. Short of going outside of the language and using
 > > lsof, /proc, or the processes table, I haven't been able to figure out
 > > how to do the corresponding thing in bash. Is there a way?
 > 
 > There's no current way to do this -- it's a new feature.  It's on the
 > list of possible future enhancements, but I haven't yet seen a
 > compelling enough case as to its value to make it a priority.
 > 
 > Chet

Ok. Let me then try to make a case for it. Basically the application
is stack-line input reading.

The bash debugger and has command (really a gdb command) "source FILE"
which opens FILE for input and starts reading debugger commands from
that. However inside that command file you could have another source
FILE command which reads that command file and when that's finished we
go back to reading the previous file or perhaps stdin.

Right now what bashdb assumes that file descriptors 4 through 9 are
free, but it really has no right to assume that. And in fact, GNU
autoconf configure will use file descriptors in this range. Also using
4-9 we is pretty limited in in file descriptors. This is less of an
issue, but still annoying and arbitrarily restrictive.

Leaving out the initial dup of stdin, here's some code that does this
in ksh93t:

filename='./1.cmd'
typeset -i i=0
typeset -a fd=()
{fd[0]}<$filename
while (( i >= 0 )) ; do
    while read cmd file <&${fd[i]}; do
    print $i ${fd[i]} $cmd $filename
    if [[ $cmd == 'source' ]] ; then
       {fd[++i]}<$filename
    fi
    done
    unset fd[i--]
done

Likewise code in zsh which does the same thing:

filename='./1.cmd'
exec {fdi}<$filename
fd+=($fdi)

while (( ${#fd[@]} > 0 )) ; do
    while read cmd filename <&${fdi}; do
    print ${fd[${#fd[@]}-1]} $cmd $filename
    if [[ $cmd == 'source' ]] ; then
        exec {fdi}<$filename
        fd+=($fdi)
    fi
    done
    fd[-1]=()
    (( ${#fd[@]} <= 0 )) && break
    fdi=${fd[-1]}
done

Both of these are in fact right now in the skeletal zshdb and kshdb code
For example: http://github.com/rocky/kshdb/tree/master/lib/processor.sh 
(_Dbg_process_commands)
and http://github.com/rocky/kshdb/tree/master/lib/processor.sh

I think it would be a useful fix to bashdb as well.

Thanks.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]