-
Notifications
You must be signed in to change notification settings - Fork 212
/
JnaNativePty.java
164 lines (140 loc) · 4.79 KB
/
JnaNativePty.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2002-2020, the original author or authors.
*
* 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.jline.terminal.impl.jna;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import com.sun.jna.Platform;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.impl.AbstractPty;
import org.jline.terminal.spi.Pty;
import org.jline.terminal.impl.jna.freebsd.FreeBsdNativePty;
import org.jline.terminal.impl.jna.linux.LinuxNativePty;
import org.jline.terminal.impl.jna.osx.OsXNativePty;
import org.jline.terminal.impl.jna.solaris.SolarisNativePty;
public abstract class JnaNativePty extends AbstractPty implements Pty {
private final int master;
private final int slave;
private final int slaveOut;
private final String name;
private final FileDescriptor masterFD;
private final FileDescriptor slaveFD;
private final FileDescriptor slaveOutFD;
public static JnaNativePty current() throws IOException {
if (Platform.isMac()) {
return OsXNativePty.current();
} else if (Platform.isLinux()) {
return LinuxNativePty.current();
} else if (Platform.isSolaris()) {
return SolarisNativePty.current();
} else if (Platform.isFreeBSD()) {
return FreeBsdNativePty.current();
} else {
throw new UnsupportedOperationException();
}
}
public static JnaNativePty open(Attributes attr, Size size) throws IOException {
if (Platform.isMac()) {
return OsXNativePty.open(attr, size);
} else if (Platform.isLinux()) {
return LinuxNativePty.open(attr, size);
} else if (Platform.isSolaris()) {
return SolarisNativePty.open(attr, size);
} else if (Platform.isFreeBSD()) {
return FreeBsdNativePty.open(attr, size);
} else {
throw new UnsupportedOperationException();
}
}
protected JnaNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) {
this(master, masterFD, slave, slaveFD, slave, slaveFD, name);
}
protected JnaNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, int slaveOut, FileDescriptor slaveOutFD, String name) {
this.master = master;
this.slave = slave;
this.slaveOut = slaveOut;
this.name = name;
this.masterFD = masterFD;
this.slaveFD = slaveFD;
this.slaveOutFD = slaveOutFD;
}
@Override
public void close() throws IOException {
if (master > 0) {
getMasterInput().close();
}
if (slave > 0) {
getSlaveInput().close();
}
}
public int getMaster() {
return master;
}
public int getSlave() {
return slave;
}
public int getSlaveOut() {
return slaveOut;
}
public String getName() {
return name;
}
public FileDescriptor getMasterFD() {
return masterFD;
}
public FileDescriptor getSlaveFD() {
return slaveFD;
}
public FileDescriptor getSlaveOutFD() {
return slaveOutFD;
}
public InputStream getMasterInput() {
return new FileInputStream(getMasterFD());
}
public OutputStream getMasterOutput() {
return new FileOutputStream(getMasterFD());
}
protected InputStream doGetSlaveInput() {
return new FileInputStream(getSlaveFD());
}
public OutputStream getSlaveOutput() {
return new FileOutputStream(getSlaveOutFD());
}
protected static FileDescriptor newDescriptor(int fd) {
try {
Constructor<FileDescriptor> cns = FileDescriptor.class.getDeclaredConstructor(int.class);
cns.setAccessible(true);
return cns.newInstance(fd);
} catch (Throwable e) {
throw new RuntimeException("Unable to create FileDescriptor", e);
}
}
@Override
public String toString() {
return "JnaNativePty[" + getName() + "]";
}
public static boolean isConsoleOutput() {
if (Platform.isMac()) {
return OsXNativePty.isatty(1) == 1;
} else if (Platform.isLinux()) {
return LinuxNativePty.isatty(1) == 1;
} else if (Platform.isSolaris()) {
return SolarisNativePty.isatty(1) == 1;
} else if (Platform.isFreeBSD()) {
return FreeBsdNativePty.isatty(1) == 1;
} else {
return false;
}
}
}