Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

character-at-a-time input from command line

24 views
Skip to first unread message

William Morris

unread,
Jun 25, 2003, 2:22:51 PM6/25/03
to
I would like to implement something like command-completion
in an interactive console-based program.

For this I need to read from the console (stdin in C terms),
unbuffered, one character at a time, without the need to press
return. I also want to disable echoing of typed characters.

I cannot find how to do this. Can anyone help?

I'm using 'gcj', the Java front end to the GNU compiler, on
Debian GNU/Linux, although I imagine this is not relevant.

Thanks
William

David Zimmerman

unread,
Jun 25, 2003, 2:49:44 PM6/25/03
to

Not with out doing some platform dependent things. Most shells (a DOS
prompt uses the 'cmd' or 'command' shell) accumulate the user's input
until he hits enter and then sends it to the reading program. This
allows the shell to do editing (backspace and such) before sending it to
the program. In UNIX land this is called 'cooked' mode. You want to be
in 'raw' mode where every chracacter is sent as is to your program. You
can do this with some JNI or you can do it with something like 'stty
raw' before you start. 'stty raw' works on Solaris

Gordon Beaton

unread,
Jun 26, 2003, 3:03:48 AM6/26/03
to

Actually since you need to use platform dependent mechanisms, it's
pretty relevant what your platform is. Others have suggested JNI or
will tell you to use a gui. Here's another way that uses stty to
change the input mode of the current console:

static String getInputMode() {
String mode = null;

try {
String[] cmd = {
"/bin/sh",
"-c",
"/bin/stty -g < /dev/tty"
};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new
InputStreamReader(p.getInputStream()));
mode = br.readLine();
p.waitFor();
br.close();
}
catch (IOException e) {} // should probably deal with this
catch (InterruptedException e) {}

return mode;
}

static void setInputMode(String mode) {
try {
String[] cmd = {
"/bin/sh",
"-c",
"/bin/stty " + mode + " < /dev/tty"
};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
catch (IOException e) {}
catch (InterruptedException e) {}
}


setInputMode("-icanon min 1") will do what you're asking for, but
realize that you will lose all of the normal command line editing
features you're used to (you can't *just* implement completion).

Save the initial mode someplace before changing the mode, and restore
it in a finally block before exiting the application.

You can use the same mechanism to turn input echo on and off (e.g. for
reading passwords):

setInputMode("echo"); // normal input echo
setInputMode("-echo"); // no input echo

This should work on any unix-like system.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

William Morris

unread,
Jun 27, 2003, 4:54:46 AM6/27/03
to
Thanks Gordon. Naively, I expected there to be a pure Java way to do
this.

> setInputMode("-icanon min 1") will do what you're asking for, but
> realize that you will lose all of the normal command line editing
> features you're used to (you can't *just* implement completion).

This will do fine. I'm not actually doing command completion, just
something that, like command completion, needs raw input.

As a Java novice, I thought restricting myself to the console would be
an easy way to start learning, without worrying about AWT or Swing.
Perhaps I am being too influenced by my dislike of GUIs.

Thanks again
William

Jon Skeet

unread,
Jun 27, 2003, 5:30:13 AM6/27/03
to
William Morris <wil...@bangel.demon.co.uk> wrote:
> As a Java novice, I thought restricting myself to the console would be
> an easy way to start learning, without worrying about AWT or Swing.
> Perhaps I am being too influenced by my dislike of GUIs.

Restricting yourself to the console *is* a good way to start learning -
but it means that you'll be mostly limiting yourself to command line
parameters for input.

I always recommend avoiding GUIs to start with - they introduce far too
many difficult bits.

(It's amazing how many people still present problems which have nothing
to do with GUIs using a GUI sample program though - especially in C#.)

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Tim Tyler

unread,
Jun 27, 2003, 12:46:37 PM6/27/03
to
William Morris <wil...@bangel.demon.co.uk> wrote:

: As a Java novice, I thought restricting myself to the console would be


: an easy way to start learning, without worrying about AWT or Swing.

What you get by default in Java (without messing with native stuff)
is pretty basic.

Java basically emulates a teletype :-| No fancy stuff like colours
or monitoring keystrokes - it's strictly one line at a time input only.

Its command-line interface is one of the places where the rumours
that Java represents portability via the lowest common denominator
really are true.
--
__________
|im |yler http://timtyler.org/ t...@tt1.org

Tom McGlynn

unread,
Jun 27, 2003, 1:25:40 PM6/27/03
to

Apropos of nothing and surely off topic...
It comes to me that this common math metaphor is wrong.
An LCD is the smallest number that includes all the factors
in any element of the starting set. By analogy LCD software
would include any feature found in any environment
it is ported to.

It would be more appropriate to call software that
only includes what's available in >all< environments as using the
Greatest Common Factor. That's the number which shares all the
factors that are in every number in the starting set.
I suppose something that begins with 'Lowest' sounds more
disparaging that something that starts 'Greatest'. I guess
we won't hear about things being reduced to the GCF any
time soon!

Regards,
Tom McGlynn

Tim Tyler

unread,
Jun 28, 2003, 4:48:28 AM6/28/03
to
Tom McGlynn <t...@lheapop.gsfc.nasa.gov> wrote:
: Tim Tyler wrote:

:> Its command-line interface is one of the places where the rumours


:> that Java represents portability via the lowest common denominator
:> really are true.

: Apropos of nothing and surely off topic...
: It comes to me that this common math metaphor is wrong.
: An LCD is the smallest number that includes all the factors
: in any element of the starting set. By analogy LCD software
: would include any feature found in any environment
: it is ported to.

: It would be more appropriate to call software that
: only includes what's available in >all< environments as using the
: Greatest Common Factor. That's the number which shares all the

: factors that are in every number in the starting set. [...]

You are not alone:

http://groups.google.com/groups?selm=97k2sk%244iv%241%40cantaloupe.srv.cs.cmu.edu&output=gplain
http://groups.google.com/groups?selm=3e674f7a%240%2449108%24e4fe514c%40news.xs4all.nl&output=gplain

0 new messages