Skip to content

Commit

Permalink
Restore terminal settings on SIGCONT, fixes #146
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 21, 2015
1 parent f8d3b27 commit 3715461
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/main/java/jline/UnixTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jline.internal.Log;
import jline.internal.TerminalLineSettings;

import static jline.internal.Preconditions.checkNotNull;

/**
* Terminal that is used for unix platforms. Terminal initialization
* is handled by issuing the <em>stty</em> command against the
Expand All @@ -33,15 +35,16 @@ public class UnixTerminal
private String lnext;

public UnixTerminal() throws Exception {
this(TerminalLineSettings.TTY);
this(TerminalLineSettings.DEFAULT_TTY);
}

public UnixTerminal(String ttyDevice) throws Exception {
super(true);
checkNotNull(ttyDevice);
settings = new TerminalLineSettings(ttyDevice);
}

protected TerminalLineSettings getSettings() {
public TerminalLineSettings getSettings() {
return settings;
}

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/jline/console/ConsoleReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.*;
import java.util.List;
Expand All @@ -43,6 +46,7 @@
import jline.internal.Log;
import jline.internal.NonBlockingInputStream;
import jline.internal.Nullable;
import jline.internal.TerminalLineSettings;
import jline.internal.Urls;
import org.fusesource.jansi.AnsiOutputStream;

Expand Down Expand Up @@ -230,6 +234,42 @@ public ConsoleReader(final @Nullable String appName, final InputStream in, final
this.inputrcUrl = getInputRc();

consoleKeys = new ConsoleKeys(this.appName, inputrcUrl);

if (terminal instanceof UnixTerminal
&& TerminalLineSettings.DEFAULT_TTY.equals(((UnixTerminal) terminal).getSettings().getTtyDevice())
&& Configuration.getBoolean("jline.sigcont", false)) {
setupSigCont();
}
}

private void setupSigCont() {
// Check that sun.misc.SignalHandler and sun.misc.Signal exists
try {
Class<?> signalClass = Class.forName("sun.misc.Signal");
Class<?> signalHandlerClass = Class.forName("sun.misc.SignalHandler");
// Implement signal handler
Object signalHandler = Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[]{signalHandlerClass}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// only method we are proxying is handle()
terminal.init();
try {
drawLine();
flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
});
// Register the signal handler, this code is equivalent to:
// Signal.handle(new Signal("CONT"), signalHandler);
signalClass.getMethod("handle", signalClass, signalHandlerClass).invoke(null, signalClass.getConstructor(String.class).newInstance("CONT"), signalHandler);
} catch (ClassNotFoundException cnfe) {
// sun.misc Signal handler classes don't exist
} catch (Exception e) {
// Ignore this one too, if the above failed, the signal API is incompatible with what we're expecting
}
}

private URL getInputRc() throws IOException {
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/jline/internal/TerminalLineSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public final class TerminalLineSettings

private static final String UNDEFINED;

public static final String TTY;

public static final String DEFAULT_TTY = "/dev/tty";

private static final boolean SUPPORTS_REDIRECT;
Expand All @@ -62,18 +60,6 @@ public final class TerminalLineSettings
supportsRedirect = false;
}
SUPPORTS_REDIRECT = supportsRedirect;

String tty;
if (supportsRedirect) {
try {
tty = waitAndCapture(inheritInput(new ProcessBuilder("tty")).start());
} catch (Throwable t) {
tty = DEFAULT_TTY;
}
} else {
tty = DEFAULT_TTY;
}
TTY = tty;
}

private String sttyCommand;
Expand All @@ -90,14 +76,15 @@ public final class TerminalLineSettings
private boolean useRedirect;

public TerminalLineSettings() throws IOException, InterruptedException {
this(TTY);
this(DEFAULT_TTY);
}

public TerminalLineSettings(String ttyDevice) throws IOException, InterruptedException {
checkNotNull(ttyDevice);
this.sttyCommand = Configuration.getString(JLINE_STTY, DEFAULT_STTY);
this.shCommand = Configuration.getString(JLINE_SH, DEFAULT_SH);
this.ttyDevice = ttyDevice;
this.useRedirect = SUPPORTS_REDIRECT && TTY.equals(ttyDevice);
this.useRedirect = SUPPORTS_REDIRECT && DEFAULT_TTY.equals(ttyDevice);
this.initialConfig = get("-g").trim();
this.config = get("-a");
this.configLastFetched = System.currentTimeMillis();
Expand All @@ -110,6 +97,10 @@ public TerminalLineSettings(String ttyDevice) throws IOException, InterruptedExc
}
}

public String getTtyDevice() {
return ttyDevice;
}

public String getConfig() {
return config;
}
Expand Down

0 comments on commit 3715461

Please sign in to comment.