get input string in raw unix command-line programming

hello

i am trying to write a simple input/output (text-based) program that gets option from user as string.

however, in raw mode, when user changes input by using backspace, it returns backspace within the return string

ex)

user input > abc(backspace)d

should return "abd" but it's not. how can i get this fix?

this is the function that i use to get user input:

public static String Input()

{

String inputLine = "";

try

{

BufferedReader bufIn;

bufIn = new BufferedReader(new InputStreamReader(System.in));

inputLine = bufIn.readLine();

}

catch (IOException ex)

{

ex.printStackTrace();

}

return inputLine;

}

(*) the only way that i can think about this problem is read-in inputs, character by character, and if it is backspace ("\n0008 \u0008"), remove previous character and start from there...

is there any java api call (method) that i can use?

[1004 byte] By [hsong3] at [2007-9-30 14:13:04]
# 1

I'm curious about this problem as I tested on Windows and it works.

It would be very inconvenient if you have to process the input char by char,

the UNIX shell should do the work for you.

It might be a problem of key mapping. Maybe try to use DEL instead of Backspace?

Catalin Merfu

http://www.accendia.com

accendia at 2007-7-5 1:21:20 > top of Java-index,Administration Tools,Sun Connection...
# 2
It should work if the backspace key works on the shell input.I tried on Debian with bash, and your code worked well.
Lacek at 2007-7-5 1:21:20 > top of Java-index,Administration Tools,Sun Connection...
# 3

> I'm curious about this problem as I tested on Windows and it works.

Yes, it would.

> It might be a problem of key mapping. Maybe try to use DEL instead of Backspace?

That's not the problem. The problem is that the program is expecting the OS to do the so-called "line processing" before the input comes in to it.

Normally, both Unix TTYs and Windows Consoles do what's known as "cooked" processing. I.e. the input handler in the OS will handle backspaces, and erase the previously input character, and all that good stuff.

However, the original poster put the terminal in RAW mode. This is a Unix-specific operation (Windows has a different analogue which I'll explain in a bit). In RAW mode, the TTY does not do any processing of any special characters (backspace, line erase, etc.). Instead, it's being asked to forward all the user's keystrokes *exactly* to whatever program is reading the TTY.

So of course, you get backspaces in the line, because (by putting the TTY in RAW mode), you're *asking* the TTY to do exactly that.Bottom line for Unix:

* If you want the line to be fully processed for backspaces, etc., before coming to your program, make sure that the TTY is in a "sane" mode (i.e. not RAW, or any other oddball TTY modes).

* If you can't help the TTY being in RAW mode, you'll have to do the input processing after you read the line (i.e. scan it for backspace, line erase (^U), etc., and do the equivalent processing yourself).

PS. There's no exact analogue for "raw" mode in Windows. However, there is a mode where you can read characters directly from the keyboard without waiting for a <CR>, but it's not portable, and not easily accessible from Java without using JNI - you have to call the low-level keyboard apis to do this.

shankar.unni at 2007-7-5 1:21:20 > top of Java-index,Administration Tools,Sun Connection...
# 4
So "raw" is the key word here.Catalin MerfuHigh Performance Java Networking http://www.accendia.com
accendia at 2007-7-5 1:21:20 > top of Java-index,Administration Tools,Sun Connection...