org.pkl.thirdparty.jline.terminal.impl.PosixSysTerminal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-tools Show documentation
Show all versions of pkl-tools Show documentation
Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.
/*
* Copyright (c) 2002-2018, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.pkl.thirdparty.jline.terminal.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.pkl.thirdparty.jline.terminal.spi.Pty;
import org.pkl.thirdparty.jline.utils.NonBlocking;
import org.pkl.thirdparty.jline.utils.NonBlockingInputStream;
import org.pkl.thirdparty.jline.utils.NonBlockingReader;
import org.pkl.thirdparty.jline.utils.ShutdownHooks;
import org.pkl.thirdparty.jline.utils.ShutdownHooks.Task;
import org.pkl.thirdparty.jline.utils.Signals;
public class PosixSysTerminal extends AbstractPosixTerminal {
protected final NonBlockingInputStream input;
protected final OutputStream output;
protected final NonBlockingReader reader;
protected final PrintWriter writer;
protected final Map nativeHandlers = new HashMap<>();
protected final Task closer;
public PosixSysTerminal(
String name, String type, Pty pty, Charset encoding, boolean nativeSignals, SignalHandler signalHandler)
throws IOException {
super(name, type, pty, encoding, signalHandler);
this.input = NonBlocking.nonBlocking(getName(), pty.getSlaveInput());
this.output = pty.getSlaveOutput();
this.reader = NonBlocking.nonBlocking(getName(), input, encoding());
this.writer = new PrintWriter(new OutputStreamWriter(output, encoding()));
parseInfoCmp();
if (nativeSignals) {
for (final Signal signal : Signal.values()) {
if (signalHandler == SignalHandler.SIG_DFL) {
nativeHandlers.put(signal, Signals.registerDefault(signal.name()));
} else {
nativeHandlers.put(signal, Signals.register(signal.name(), () -> raise(signal)));
}
}
}
closer = PosixSysTerminal.this::close;
ShutdownHooks.add(closer);
}
@Override
public SignalHandler handle(Signal signal, SignalHandler handler) {
SignalHandler prev = super.handle(signal, handler);
if (prev != handler) {
if (handler == SignalHandler.SIG_DFL) {
Signals.registerDefault(signal.name());
} else {
Signals.register(signal.name(), () -> raise(signal));
}
}
return prev;
}
public NonBlockingReader reader() {
return reader;
}
public PrintWriter writer() {
return writer;
}
@Override
public InputStream input() {
return input;
}
@Override
public OutputStream output() {
return output;
}
@Override
protected void doClose() throws IOException {
ShutdownHooks.remove(closer);
for (Map.Entry entry : nativeHandlers.entrySet()) {
Signals.unregister(entry.getKey().name(), entry.getValue());
}
super.doClose();
// Do not call reader.close()
reader.shutdown();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy