Skip to content

Commit

Permalink
Issue #421: Invoke the correct method for STTY calls so Cygwin works …
Browse files Browse the repository at this point in the history
…again

Also use CYGWIN_HOME to find where the binaries are
  • Loading branch information
mabe02 committed Feb 1, 2020
1 parent 237b92a commit 7f0a93d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@
* This class extends UnixLikeTerminal and implements the Cygwin-specific implementations. This means, running a Java
* application using Lanterna inside the Cygwin Terminal application. The standard Windows command prompt (cmd.exe) is
* not supported by this class.
* <p>
* <p/>
* <b>NOTE:</b> This class is experimental and does not fully work! Some of the operations, like disabling echo and
* changing cbreak seems to be impossible to do without resorting to native code. Running "stty raw" before starting the
* JVM will improve compatibility.
* <p/>
* <b>NOTE:</b> This class will try to find Cygwin by scanning the directories on java.library.path, but you can also
* tell it where Cygwin is installed by setting the CYGWIN_HOME environment variable.
*
* @author Martin
* @author Andreas
*/
public class CygwinTerminal extends UnixLikeTTYTerminal {

private static final String STTY_LOCATION = findProgram("stty.exe");
private static final Pattern STTY_SIZE_PATTERN = Pattern.compile(".*rows ([0-9]+);.*columns ([0-9]+);.*");
private static final String JAVA_LIBRARY_PATH_PROPERTY = "java.library.path";
private static final String CYGWIN_HOME_ENV = "CYGWIN_HOME";

/**
* Creates a new CygwinTerminal based off input and output streams and a character set to use
Expand Down Expand Up @@ -90,6 +94,12 @@ protected String runSTTYCommand(String... parameters) throws IOException {
return exec(commandLine.toArray(new String[commandLine.size()]));
}

@Override
protected void acquire() throws IOException {
super.acquire();
// Placeholder in case we want to add extra stty invocations for Cygwin
}

private String findSTTY() {
return STTY_LOCATION;
}
Expand All @@ -101,7 +111,13 @@ private String getPseudoTerminalDevice() {
}

private static String findProgram(String programName) {
String[] paths = System.getProperty("java.library.path").split(";");
if (System.getenv(CYGWIN_HOME_ENV) != null) {
File cygwinHomeBinFile = new File(System.getenv(CYGWIN_HOME_ENV) + "/bin", programName);
if (cygwinHomeBinFile.exists()) {
return cygwinHomeBinFile.getAbsolutePath();
}
}
String[] paths = System.getProperty(JAVA_LIBRARY_PATH_PROPERTY).split(";");
for(String path : paths) {
File shBin = new File(path, programName);
if(shBin.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,36 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

@Override
protected void saveTerminalSettings() throws IOException {
sttyStatusToRestore = exec(getSTTYCommand(), "-g").trim();
sttyStatusToRestore = runSTTYCommand("-g").trim();
}

@Override
protected void restoreTerminalSettings() throws IOException {
if(sttyStatusToRestore != null) {
exec(getSTTYCommand(), sttyStatusToRestore);
runSTTYCommand(sttyStatusToRestore);
}
}

@Override
protected void keyEchoEnabled(boolean enabled) throws IOException {
exec(getSTTYCommand(), enabled ? "echo" : "-echo");
runSTTYCommand(enabled ? "echo" : "-echo");
}

@Override
protected void canonicalMode(boolean enabled) throws IOException {
exec(getSTTYCommand(), enabled ? "icanon" : "-icanon");
runSTTYCommand(enabled ? "icanon" : "-icanon");
if(!enabled) {
exec(getSTTYCommand(), "min", "1");
runSTTYCommand("min", "1");
}
}

@Override
protected void keyStrokeSignalsEnabled(boolean enabled) throws IOException {
if(enabled) {
exec(getSTTYCommand(), "intr", "^C");
runSTTYCommand("intr", "^C");
}
else {
exec(getSTTYCommand(), "intr", "undef");
runSTTYCommand("intr", "undef");
}
}

Expand Down

0 comments on commit 7f0a93d

Please sign in to comment.