Unix Programming - GNU readline / tty related questions

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2007 > GNU readline / tty related questions





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author GNU readline / tty related questions
emiller_news@yahoo.com

2007-05-25, 1:19 pm

Hi,
I have to control a tool "A", that uses the GNU readline library for
it's command line, 'remotely' from another process "B". Currently I
send commands to and receive the output from the tool "A" via pipes.

The problem is that for example sending a <Tab> from "B" to "A"
doesn't start Tab-Completion and <Cursor-Up> doesn't read from the
history.

How can I have every key, that is typed in "B", sent to "A" so that it
is immediately interpreted as if it was typed in "A"s command line?

A problem that is probably related to this question is that, when
starting "A" via "execl(".../tool",...)" inside "B", the tool "A"
recognizes by calling isatty(), that it is not connected to a
terminal. Can I attach it to a tty before or after "execl()"? If not,
another option is to start "A" first on a terminal, if I can later
grab/send to that terminal from the control process "B" as mentioned
above.

Any clues how to do this?

Thanks
Edi

Barry Margolin

2007-05-26, 1:21 am

In article <1180103675.094305.230600@p47g2000hsd.googlegroups.com>,
emiller_news@yahoo.com wrote:

> Hi,
> I have to control a tool "A", that uses the GNU readline library for
> it's command line, 'remotely' from another process "B". Currently I
> send commands to and receive the output from the tool "A" via pipes.
>
> The problem is that for example sending a <Tab> from "B" to "A"
> doesn't start Tab-Completion and <Cursor-Up> doesn't read from the
> history.


The readline library probably only processes control characters
specially when its input is a tty, not a pipe.

>
> How can I have every key, that is typed in "B", sent to "A" so that it
> is immediately interpreted as if it was typed in "A"s command line?
>
> A problem that is probably related to this question is that, when
> starting "A" via "execl(".../tool",...)" inside "B", the tool "A"
> recognizes by calling isatty(), that it is not connected to a
> terminal. Can I attach it to a tty before or after "execl()"? If not,
> another option is to start "A" first on a terminal, if I can later
> grab/send to that terminal from the control process "B" as mentioned
> above.
>
> Any clues how to do this?


Use a pseudo-tty (pty) rather than a pipe. You might want to make use
of Expect, a scripting language specifically designed for controlling
interactive programs.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Bin Chen

2007-05-26, 7:21 am

On May 25, 10:34 pm, emiller_n...@yahoo.com wrote:
> Hi,
> I have to control a tool "A", that uses the GNU readline library for
> it's command line, 'remotely' from another process "B". Currently I
> send commands to and receive the output from the tool "A" via pipes.
>
> The problem is that for example sending a <Tab> from "B" to "A"
> doesn't start Tab-Completion and <Cursor-Up> doesn't read from the
> history.
>
> How can I have every key, that is typed in "B", sent to "A" so that it
> is immediately interpreted as if it was typed in "A"s command line?
>
> A problem that is probably related to this question is that, when
> starting "A" via "execl(".../tool",...)" inside "B", the tool "A"
> recognizes by calling isatty(), that it is not connected to a
> terminal. Can I attach it to a tty before or after "execl()"? If not,
> another option is to start "A" first on a terminal, if I can later
> grab/send to that terminal from the control process "B" as mentioned
> above.
>
> Any clues how to do this?
>

readline may judge the input is a normal file desciptor or a tty...

emiller_news@yahoo.com

2007-05-28, 1:19 pm

On 26 Mai, 03:25, Barry Margolin <bar...@alum.mit.edu> wrote:

> Use a pseudo-tty(pty) rather than a pipe. You might want to make use
> of Expect, a scripting language specifically designed for controlling
> interactive programs.


Can you point me to some HOWTO or examples?

Thanks
Edi

>
> --
> Barry Margolin, bar...@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***



Logan Shaw

2007-05-28, 1:19 pm

emiller_news@yahoo.com wrote:
> On 26 Mai, 03:25, Barry Margolin <bar...@alum.mit.edu> wrote:
>
[vbcol=seagreen]
> Can you point me to some HOWTO or examples?


You apparently haven't tried typing "expect" into google and
clicking on the first link.

- Logan
quarkLore

2007-05-29, 7:23 am

On May 28, 7:55 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> emiller_n...@yahoo.com wrote:
>
>
> You apparently haven't tried typing "expect" into google and
> clicking on the first link.
>
> - Logan

I think expect is the traditional way to do this. Pipe will help till
you the receiver application reads from stdin but other applications
like telnet, passwd can't be used with pipes. Expect runs over tcl,tk
has regular expression features and controls the flow based on what
the program 'expect's from the output.

Expect is used a lot in automation of command line based utilities

emiller_news@yahoo.com

2007-05-29, 1:22 pm

On 28 Mai, 16:55, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> emiller_n...@yahoo.com wrote:
>
>
> You apparently haven't tried typing "expect" into google and
> clicking on the first link.


Correct. I was visiting some other pages on Expect. My impression is
that Expect is not exactly targeted at my purposes although it might
be possible to use it with some detouring. So I put my question more
specifically:

How can I open a tty or pty from a C/C++ program, start a process
inside that terminal and have character-level communication between
the C/C++ program and the terminal input/output?

If this is possible with Excpect, this is fine, but it wouldn't help
me in the long run, because I can't use an intermediate Tcl program,
nor does the Tcl code help me with the C implementation.

Thanks
Edi

Logan Shaw

2007-05-30, 1:21 am

emiller_news@yahoo.com wrote:
> How can I open a tty or pty from a C/C++ program, start a process
> inside that terminal and have character-level communication between
> the C/C++ program and the terminal input/output?
>
> If this is possible with Excpect, this is fine, but it wouldn't help
> me in the long run, because I can't use an intermediate Tcl program,
> nor does the Tcl code help me with the C implementation.


Tcl/Expect is Open Source and contains a C implementation of exactly
what you're talking about. It uses a pty and spawns a program in it.

Also, I believe the Tcl interpreter is specifically designed to be easy
to embed in another program (so that it is not a separate executable),
so maybe that is an option.

- Logan
emiller_news@yahoo.com

2007-05-30, 7:17 am

On 30 Mai, 08:15, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> emiller_n...@yahoo.com wrote:
>
>
> Tcl/Expect is Open Source and contains a C implementation of exactly
> what you're talking about. It uses a pty and spawns a program in it.
>
> Also, I believe the Tcl interpreter is specifically designed to be easy
> to embed in another program (so that it is not a separate executable),
> so maybe that is an option.
>
> - Logan



Logan, I am asking in c.u.p, because I hope to receive a few concise
hints about programming techniques, not about programs that are doing
what I try to do. Of course this problem has been solved in several
open source projects and I can learn about the techniques by studying
some 10000 lines of code, and you can call me lazy because I hope that
somebody more experienced than I might save me the time to do this.

So, if you can tell me how to open a pty in the way I asked, or if you
can point me to the relevant code in Expect, I'd consider your reply
really useful.

Thanks
Edi

Barry Margolin

2007-05-30, 7:17 am

In article <1180443510.886885.264030@k79g2000hse.googlegroups.com>,
emiller_news@yahoo.com wrote:

> How can I open a tty or pty from a C/C++ program, start a process
> inside that terminal and have character-level communication between
> the C/C++ program and the terminal input/output?


See openpty(3)

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com