| 1 |
/* |
| 2 |
* Copyright (c) 2002-2017, the original author or authors. |
| 3 |
* |
| 4 |
* This software is distributable under the BSD license. See the terms of the |
| 5 |
* BSD license in the documentation provided with this software. |
| 6 |
* |
| 7 |
* http://www.opensource.org/licenses/bsd-license.php |
| 8 |
*/ |
| 9 |
package org.jline.terminal.impl.jansi; |
| 10 |
|
| 11 |
import org.fusesource.jansi.internal.CLibrary; |
| 12 |
import org.jline.terminal.Attributes; |
| 13 |
import org.jline.terminal.Size; |
| 14 |
import org.jline.terminal.spi.Pty; |
| 15 |
import org.jline.utils.OSUtils; |
| 16 |
|
| 17 |
import java.io.FileDescriptor; |
| 18 |
import java.io.FileInputStream; |
| 19 |
import java.io.FileOutputStream; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStream; |
| 22 |
import java.io.OutputStream; |
| 23 |
import java.lang.reflect.Constructor; |
| 24 |
|
| 25 |
import static org.fusesource.jansi.internal.CLibrary.TCSANOW; |
| 26 |
import static org.jline.terminal.impl.jansi.JansiSupportImpl.JANSI_MAJOR_VERSION; |
| 27 |
import static org.jline.terminal.impl.jansi.JansiSupportImpl.JANSI_MINOR_VERSION; |
| 28 |
import static org.jline.utils.ExecHelper.exec; |
| 29 |
|
| 30 |
public abstract class JansiNativePty implements Pty { |
| 31 |
|
| 32 |
private final int master; |
| 33 |
private final int slave; |
| 34 |
private final String name; |
| 35 |
private final FileDescriptor masterFD; |
| 36 |
private final FileDescriptor slaveFD; |
| 37 |
|
| 38 |
public JansiNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) { |
| 39 |
this.master = master; |
| 40 |
this.slave = slave; |
| 41 |
this.name = name; |
| 42 |
this.masterFD = masterFD; |
| 43 |
this.slaveFD = slaveFD; |
| 44 |
} |
| 45 |
|
| 46 |
protected static String ttyname() throws IOException { |
| 47 |
String name; |
| 48 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 16) { |
| 49 |
name = CLibrary.ttyname(0); |
| 50 |
} else { |
| 51 |
try { |
| 52 |
name = exec(true, OSUtils.TTY_COMMAND); |
| 53 |
} catch (IOException e) { |
| 54 |
throw new IOException("Not a tty", e); |
| 55 |
} |
| 56 |
} |
| 57 |
if (name != null) { |
| 58 |
name = name.trim(); |
| 59 |
} |
| 60 |
if (name == null || name.isEmpty()) { |
| 61 |
throw new IOException("Not a tty"); |
| 62 |
} |
| 63 |
return name; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void close() throws IOException { |
| 68 |
if (master > 0) { |
| 69 |
getMasterInput().close(); |
| 70 |
} |
| 71 |
if (slave > 0) { |
| 72 |
getSlaveInput().close(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public int getMaster() { |
| 77 |
return master; |
| 78 |
} |
| 79 |
|
| 80 |
public int getSlave() { |
| 81 |
return slave; |
| 82 |
} |
| 83 |
|
| 84 |
public String getName() { |
| 85 |
return name; |
| 86 |
} |
| 87 |
|
| 88 |
public FileDescriptor getMasterFD() { |
| 89 |
return masterFD; |
| 90 |
} |
| 91 |
|
| 92 |
public FileDescriptor getSlaveFD() { |
| 93 |
return slaveFD; |
| 94 |
} |
| 95 |
|
| 96 |
public InputStream getMasterInput() { |
| 97 |
return new FileInputStream(getMasterFD()); |
| 98 |
} |
| 99 |
|
| 100 |
public OutputStream getMasterOutput() { |
| 101 |
return new FileOutputStream(getMasterFD()); |
| 102 |
} |
| 103 |
|
| 104 |
public InputStream getSlaveInput() { |
| 105 |
return new FileInputStream(getSlaveFD()); |
| 106 |
} |
| 107 |
|
| 108 |
public OutputStream getSlaveOutput() { |
| 109 |
return new FileOutputStream(getSlaveFD()); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
@Override |
| 114 |
public Attributes getAttr() throws IOException { |
| 115 |
CLibrary.Termios tios = new CLibrary.Termios(); |
| 116 |
CLibrary.tcgetattr(slave, tios); |
| 117 |
return toAttributes(tios); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void setAttr(Attributes attr) throws IOException { |
| 122 |
CLibrary.Termios tios = toTermios(attr); |
| 123 |
CLibrary.tcsetattr(slave, TCSANOW, tios); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public Size getSize() throws IOException { |
| 128 |
CLibrary.WinSize sz = new CLibrary.WinSize(); |
| 129 |
CLibrary.ioctl(slave, CLibrary.TIOCGWINSZ, sz); |
| 130 |
return new Size(sz.ws_col, sz.ws_row); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void setSize(Size size) throws IOException { |
| 135 |
CLibrary.WinSize sz = new CLibrary.WinSize((short) size.getRows(), (short) size.getColumns()); |
| 136 |
CLibrary.ioctl(slave, CLibrary.TIOCSWINSZ, sz); |
| 137 |
} |
| 138 |
|
| 139 |
protected abstract CLibrary.Termios toTermios(Attributes t); |
| 140 |
|
| 141 |
protected abstract Attributes toAttributes(CLibrary.Termios tios); |
| 142 |
|
| 143 |
@Override |
| 144 |
public String toString() { |
| 145 |
return "JansiNativePty[" + getName() + "]"; |
| 146 |
} |
| 147 |
|
| 148 |
protected static FileDescriptor newDescriptor(int fd) { |
| 149 |
try { |
| 150 |
Constructor<FileDescriptor> cns = FileDescriptor.class.getDeclaredConstructor(int.class); |
| 151 |
cns.setAccessible(true); |
| 152 |
return cns.newInstance(fd); |
| 153 |
} catch (Throwable e) { |
| 154 |
throw new RuntimeException("Unable to create FileDescriptor", e); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
} |