Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Using zle outside zsh



    Hi Bart :)

 * Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> dixit:
> >     What I'm thinking is: will it be difficult to use zsh as the
> > command line interpreter?
> This is actually relatively easy to do.  For a working example, see
> Functions/Misc/nslookup in the zsh distribution.

    The problem here is that if the command prints its own prompt, a
code like this:

#!/bin/zsh

emulate -L zsh

zpty telnet telnet

zpty -r telnet response 'telnet>'
print -nr $response

while line=''; vared -e line
do
    [[ "$line" == "quit" ]] && break

    zpty -w telnet "$line"
    zpty -r telnet response 'telnet>'
    print -nr $response
done

zpty -w telnet "quit"
zpty -d telnet


    doesn't work, because the prompt doesn't have a carriage return.
The first 'print -nr' doesn't print anything because the buffers
aren't flushed until a '\n' is printed (in line buffer mode, I mean).

    But the worst problem is in the inner loop. I want zpty to read
all that 'telnet' spits until it finds 'telnet>', but blocks forever.
And if I change the code to just "zpty -r telnet" (that is, no
parameter and no pattern), it spits about 700 bytes of data and
stops, waiting for a buffer to fill (which never does).

    If the command run by zpty doesn't ends its chat with end of
line, is almost impossible to use zsh as a 'frontend' (so to say...).
Am I doing anything wrong?

    Obviously I can change the code above to make it read by lines:

#!/bin/zsh

emulate -L zsh

zpty telnet telnet

zpty -r telnet response 'telnet>'
print -nr $response

while line=''; vared -e line
do
    [[ "$line" == "quit" ]] && break

    zpty -w telnet "$line"
    while zpty -r telnet response
    do
        print -nr $response
    done
done

zpty -w telnet "quit"
zpty -d telnet

    This works, but never prints the command prompt, since it doesn't
have "\n", and moreover the inner loop blocks forever. Making the
zpty non-blocking leads to a race condition (solved by adding some
delay between the writing and the reading in the inner loop) :(( I
think that this is more difficult than I thought when the command
doesn't ends with a carriage return, which is the common case since I
want to use zle as a frontend for commands that prints their own
prompt... Any suggestion?

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/



Messages sorted by: Reverse Date, Date, Thread, Author