33

I'm designing a console application for a server running RedHat. The end users should be able to run this app with any terminal of their choosing. (For example; Gnome Terminal, Putty SSH/ Telnet, MS Telnet Client and others).

In most terminal applications there's nothing wrong, however when I launch my program from a MS telnet session I notice my special inputs for System.in and System.console() get totally messed up. A backspace will write ^H to the screen and other keys write gibberish as well.

I've hacked at it enough that I can get it to consistently work, but I'm sure what I'm doing is gross:

if (!System.getenv("TERM").equals("xterm"))
{
    System.out.println("\nWARNING: The TERM type is now set to xterm\n");
    final String[] cmd = { "/bin/sh", "-c", "export TERM=xterm" };
    Runtime.getRuntime().exec(cmd);
}

Would there be an issue here for terminals that don't support xterm? I notice that the Microsoft Telnet client doesn't allow you to set the TERM type to xterm before you begin a session. Once the session is started, however, setting TERM=xterm seems to solve the problem.

How do most console applications go about this issue?

flakes
  • 12,841
  • 5
  • 28
  • 69
  • Probably your gibberish are ansi escape sequnces http://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences?lq=1 – Andreas Frische Feb 18 '16 at 14:36
  • 2
    look there: https://github.com/fusesource/jansi Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows. – Andreas Frische Feb 18 '16 at 14:40
  • @AndreasFrische oh wow that looks promising! will have to play around with it! – flakes Feb 18 '16 at 14:42
  • @AndreasFrische ah, sadly i don't think this will work. Looks like it's only used for output, whereas my issue is the text displaying during inputs. – flakes Feb 18 '16 at 15:31
  • https://sourceforge.net/p/telnetd/code/HEAD/tree/osgi/trunk/src/java/net/wimpi/telnetd/io/TerminalReader.java – Andreas Frische Feb 18 '16 at 20:53
  • http://serverfault.com/questions/497793/how-do-telnet-based-programs-report-their-terminal-type-to-a-socket-server – Andreas Frische Feb 18 '16 at 20:53
  • http://mud-dev.wikidot.com/telnet:negotiation – Andreas Frische Feb 18 '16 at 20:54
  • pretty arcane stuff. If it works, i wouldn't bother with a more complex solution. Maybe the user can switch the telnet terminal type? https://technet.microsoft.com/en-us/library/cc771967(v=ws.10).aspx?f=255&mspperror=-2147217396 commons-net also has some classes for Telnet; maybe look there as well. – Andreas Frische Feb 18 '16 at 20:58

2 Answers2

1

With character terminal applications, there are always two ends in the communication, which must agree on how to interpret the control characters. Usually both sides are capable of using a variety of codings described in termcap/terminfo database.

On the Unix server-side you can define the coding by setting the TERM environmental variable, or by using stty (a default is used otherwise, often a dumb terminal emulation).

On the client side you also have to set the same terminal emulation as on the server side. Windows native telnet has the capability to define the emulation (see e.g. Configure the Telnet Terminal Type), as have other terminal emulators (e.g. Putty), too.

Regarding your design decisions: The above terminal setting are usually only described in user documentation, and not hardcoded in application, to leave more flexibility. After all, you do not know in advance which terminal (only a simple hardware terminal, supporting a single termcap coding, perhaps?) your users are going to use.

(As your question has little to do with Java or system.in, so you could re-consider the tags you use.)

Artur Opalinski
  • 1,018
  • 7
  • 12
-4

You should look into these two posts, as they are related to what you are doing.

check env variable

set environment variable

As you are running the console on top of the Unix server, redhat in your case, I would also recommend that you look into the Unix command expect, that allows you to read the input in the console application, and performs the action according to the input of the user.

Here are some examples of the command usage.

sample Expect usages

Community
  • 1
  • 1
g5thomas
  • 187
  • 1
  • 3
  • 11