package jdk.internal.org.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.Objects;
import jdk.internal.org.jline.terminal.Attributes;
import jdk.internal.org.jline.terminal.Attributes.ControlChar;
import jdk.internal.org.jline.terminal.Attributes.InputFlag;
import jdk.internal.org.jline.terminal.Attributes.LocalFlag;
import jdk.internal.org.jline.terminal.Attributes.OutputFlag;
import jdk.internal.org.jline.terminal.Size;
import jdk.internal.org.jline.terminal.Terminal;
import jdk.internal.org.jline.utils.NonBlocking;
import jdk.internal.org.jline.utils.NonBlockingPumpInputStream;
import jdk.internal.org.jline.utils.NonBlockingReader;
public class LineDisciplineTerminal extends AbstractTerminal {
private static final String DEFAULT_TERMINAL_ATTRIBUTES =
"speed 9600 baud; 24 rows; 80 columns;\n" +
"lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl\n" +
"\t-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo\n" +
"\t-extproc\n" +
"iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8\n" +
"\t-ignbrk brkint -inpck -ignpar -parmrk\n" +
"oflags: opost onlcr -oxtabs -onocr -onlret\n" +
"cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow\n" +
"\t-dtrflow -mdmbuf\n" +
"cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;\n" +
"\teol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;\n" +
"\tmin = 1; quit = ^\\; reprint = ^R; start = ^Q; status = ^T;\n" +
"\tstop = ^S; susp = ^Z; time = 0; werase = ^W;\n";
private static final int PIPE_SIZE = 1024;
protected final OutputStream masterOutput;
protected final OutputStream slaveInputPipe;
protected final NonBlockingPumpInputStream slaveInput;
protected final NonBlockingReader slaveReader;
protected final PrintWriter slaveWriter;
protected final OutputStream slaveOutput;
protected final Attributes attributes;
protected final Size size;
public LineDisciplineTerminal(String name,
String type,
OutputStream masterOutput,
Charset encoding) throws IOException {
this(name, type, masterOutput, encoding, SignalHandler.SIG_DFL);
}
public LineDisciplineTerminal(String name,
String type,
OutputStream masterOutput,
Charset encoding,
SignalHandler signalHandler) throws IOException {
super(name, type, encoding, signalHandler);
NonBlockingPumpInputStream input = NonBlocking.nonBlockingPumpInputStream(PIPE_SIZE);
this.slaveInputPipe = input.getOutputStream();
this.slaveInput = input;
this.slaveReader = NonBlocking.nonBlocking(getName(), slaveInput, encoding());
this.slaveOutput = new FilteringOutputStream();
this.slaveWriter = new PrintWriter(new OutputStreamWriter(slaveOutput, encoding()));
this.masterOutput = masterOutput;
this.attributes = ExecPty.doGetAttr(DEFAULT_TERMINAL_ATTRIBUTES);
this.size = new Size(160, 50);
parseInfoCmp();
}
public NonBlockingReader reader() {
return slaveReader;
}
public PrintWriter writer() {
return slaveWriter;
}
@Override
public InputStream input() {
return slaveInput;
}
@Override
public OutputStream output() {
return slaveOutput;
}
public Attributes getAttributes() {
Attributes attr = new Attributes();
attr.copy(attributes);
return attr;
}
public void setAttributes(Attributes attr) {
attributes.copy(attr);
}
public Size getSize() {
Size sz = new Size();
sz.copy(size);
return sz;
}
public void setSize(Size sz) {
size.copy(sz);
}
@Override
public void raise(Signal signal) {
Objects.requireNonNull(signal);
echoSignal(signal);
super.raise(signal);
}
public void processInputByte(int c) throws IOException {
boolean flushOut = doProcessInputByte(c);
slaveInputPipe.flush();
if (flushOut) {
masterOutput.flush();
}
}
public void processInputBytes(byte[] input) throws IOException {
processInputBytes(input, 0, input.length);
}
public void processInputBytes(byte[] input, int offset, int length) throws IOException {
boolean flushOut = false;
for (int i = 0; i < length; i++) {
flushOut |= doProcessInputByte(input[offset + i]);
}
slaveInputPipe.flush();
if (flushOut) {
masterOutput.flush();
}
}
protected boolean doProcessInputByte(int c) throws IOException {
if (attributes.getLocalFlag(LocalFlag.ISIG)) {
if (c == attributes.getControlChar(ControlChar.VINTR)) {
raise(Signal.INT);
return false;
} else if (c == attributes.getControlChar(ControlChar.VQUIT)) {
raise(Signal.QUIT);
return false;
} else if (c == attributes.getControlChar(ControlChar.VSUSP)) {
raise(Signal.TSTP);
return false;
} else if (c == attributes.getControlChar(ControlChar.VSTATUS)) {
raise(Signal.INFO);
}
}
if (c == '\r') {
if (attributes.getInputFlag(InputFlag.IGNCR)) {
return false;
}
if (attributes.getInputFlag(InputFlag.ICRNL)) {
c = '\n';
}
} else if (c == '\n' && attributes.getInputFlag(InputFlag.INLCR)) {
c = '\r';
}
boolean flushOut = false;
if (attributes.getLocalFlag(LocalFlag.ECHO)) {
processOutputByte(c);
flushOut = true;
}
slaveInputPipe.write(c);
return flushOut;
}
protected void processOutputByte(int c) throws IOException {
if (attributes.getOutputFlag(OutputFlag.OPOST)) {
if (c == '\n') {
if (attributes.getOutputFlag(OutputFlag.ONLCR)) {
masterOutput.write('\r');
masterOutput.write('\n');
return;
}
}
}
masterOutput.write(c);
}
protected void processIOException(IOException ioException) {
this.slaveInput.setIoException(ioException);
}
protected void doClose() throws IOException {
super.doClose();
try {
slaveReader.close();
} finally {
try {
slaveInputPipe.close();
} finally {
try {
} finally {
slaveWriter.close();
}
}
}
}
private class FilteringOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
processOutputByte(b);
flush();
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
processOutputByte(b[off + i]);
}
flush();
}
@Override
public void flush() throws IOException {
masterOutput.flush();
}
@Override
public void close() throws IOException {
masterOutput.close();
}
}
}