Re: Bash command completion when not connected to a terminal

2007-06-08 Thread raner


raner wrote:
 
 Is there a way to configure bash to echo back each character that was sent
 to it's stdin?
 

All right, I spent quite some time with this problem, so I thought I share
what I've found out and answer my own question. First of all, I should
probably rephrase my original question. The real issue here is: How can I
enable input echoing for a Solaris bash that is not connected to a terminal?

I installed bash 3.2 on Solaris and compared the results with Cygwin bash
3.2 on Windows XP. When launched by my Java application, the Cygwin bash
will echo the user input, and the Solaris bash will not. I looked at the
bash source code and found out that the input echoing is done by GNU
readline. If you search rltty.c for the variable readline_echoing_p you will
see that it is initialized based on the ECHO flag in the terminal's TIOTYPE
structure (which is just an alias for struct termio/termios). As the bash
process is not connected to a terminal in my case (the original root of the
problem), I assume that the struct termio/termios is probably all
zero/undefined and doesn't have the ECHO flag set. Something like that, I
didn't really dig into the details. It looks like, on Solaris, bash will not
echo its input unless it is connected to a terminal. If someone in fact does
know a way to achieve this, I'd still like to hear it, though.

So, what is the work-around? Under Solaris, the situation can be summarized
as No terminal, no echo. So, to get echo to work, we need to run bash with
a terminal, or to be more precise, with a pseudo-terminal. I was under the
illusion that there would be a simple Solaris command to create a
pseudo-terminal (maybe something like pty bash), but, of course, there
isn't. After googling around a bit, I found a utility called empty
(http://empty.sf.net). It is designed for a slightly different purpose than
what I needed, but it can execute a command in a pseudo-terminal and makes
stdin/stdout available as named pipes. To run a bash with a pseudo-terminal
in my Java application, I had to write a little script that I called
ptybash:

#!/bin/sh
./empty -f -i pty.in -o pty.out bash -i
cat -u pty.out  cat -u pty.in

The script uses empty to create the pseudo-terminal and the named pipes. To
connect the current stdin/stdout with the pipes, it uses two separate cat
commands. My Java application now doesn't just launch bash but instead:

bash ptybash

The first bash, of course, will still suffer from the echo issue, but it
only serves to run the ptybash script, which launches a second bash in a
pseudo-terminal. That second bash should have its input echoing intact. Or
so I thought... Because, when I tried it, again, all my typed commands were
dutyfully executed, but I still couldn't see what I typed. However, now that
the bash was running in a terminal, I could actually type:

stty echo

And after this, I could finally see what I typed, and the completion also
showed me the completed filenames. It's a little bit nonstraightforward, but
it solved the problem. Does anybody know of an easier way to do this?

-- 
View this message in context: 
http://www.nabble.com/Bash-command-completion-when-not-connected-to-a-terminal-tf3876564.html#a11022070
Sent from the Gnu - Bash mailing list archive at Nabble.com.



___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Bash command completion when not connected to a terminal

2007-06-06 Thread raner

Hi all,

I am trying to integrate bash with a Java application, i.e. bash is started
as a process and the Java application sends data to the process's stdin and
reads data from stdout/stderr. This works reasonably well, but I am having a
problem with completion, e.g. the user pressing TAB to complete a filename.
The completion seems to work, but I do not receive the completed filename
from the shell's stdout.
For example, let's say bash is in a directory that contains only a file
named Hello. When I open a terminal window, and type cat He + TAB, my
command line will read cat Hello  after that. For my Java program that is
connected to  the bash process, I would expect that after sending cat He +
TAB to the process's stdin, I would receive a llo  from stdout (i.e. the
missing part that was necessary to complete the name). However, this is not
the case; sending the TAB character does not trigger a response on bash's
stdout. However, if send a carriage return after that, bash will correctly
execute the cat command with the complete filename. So, it seems at least
internally the completion succeeded somehow; there is just no feedback.

I am running bash with -i option to force interactive mode, so that should
not be the issue. Also, I flush the OutputStream that is connected to bash's
stdin after each character; so each character is sent immediately to the
shell without any buffering in between. I suspect that it has to with the
fact that neither stdin nor stdout are actually terminal devices (isatty
would return false). I am also wondering whether this is some issue with the
$TERM variable. When I run bash in a regular window, I get $TERM=xterm;
running a bash in XEmacs shell mode (where command completion works as
expected, including feedback when pressing TAB) shows $TERM=emacs. In my
Java application I get $TERM=dtterm (I'm using Solaris). Is there some way
to make this work by setting $TERM or by tricking bash to think that it is
connected to a terminal? Or am I misunderstanding how the completion is
supposed to work, and part of the work is actually done by the terminal and
not by the shell?

Any help is greatly appreciated!

Thanks,

Mirko

-- 
View this message in context: 
http://www.nabble.com/Bash-command-completion-when-not-connected-to-a-terminal-tf3876564.html#a10984513
Sent from the Gnu - Bash mailing list archive at Nabble.com.



___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: Bash command completion when not connected to a terminal

2007-06-06 Thread Paul Jarc
raner [EMAIL PROTECTED] wrote:
 The completion seems to work, but I do not receive the completed filename
 from the shell's stdout.

It's written to stderr, not stdout.


paul


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: Bash command completion when not connected to a terminal

2007-06-06 Thread raner


Paul Jarc wrote:
 
 raner [EMAIL PROTECTED] wrote:
 The completion seems to work, but I do not receive the completed filename
 from the shell's stdout.
 
 It's written to stderr, not stdout.
 

Thanks for your reply, Paul. I'm reading stderr, too, and it doesn't show up
there either. I am wondering whether I'm not experiencing a more general
problem with the input echoing here. I tried my application under Windows
with a Cygwin bash 3.2, and I actually do receive the completion string
after a TAB was sent (on stderr, as pointed out earlier). However, I also
receive an echo of each character that was sent to the bash process (i.e.
when typing cat He my application actually shows ccaatt  HHee). On
Solaris 10 with bash 3.00.16, I get no such echo, but I don't receive the
completion string either. Clearly, the behavior that I am seeing with the
Windows bash 3.2 is preferrable; my application can easily get rid of the
echo problem by not printing out the input characters and only print out
what is echoed by bash.

I guess my new question now is, how is that echoing feature controlled in
bash? Is this just a difference between bash 3.00.16 and 3.2 (or maybe a
difference between Windows bash and Solaris bash?), or do the two shells
just happen to use slightly different default settings? Is there a way to
configure bash to echo back each character that was sent to it's stdin?

-- 
View this message in context: 
http://www.nabble.com/Bash-command-completion-when-not-connected-to-a-terminal-tf3876564.html#a10998098
Sent from the Gnu - Bash mailing list archive at Nabble.com.



___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash