Debian Codegraph @

terminal-jna/src/main/java/org/jline/terminal/impl/jna/JnaNativePty.java

raw
1 /*
2 * Copyright (c) 2002-2016, 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.jna;
10
11 import java.io.FileDescriptor;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.lang.reflect.Constructor;
18
19 import com.sun.jna.Platform;
20 import org.jline.terminal.Attributes;
21 import org.jline.terminal.Size;
22 import org.jline.terminal.spi.Pty;
23 import org.jline.terminal.impl.jna.freebsd.FreeBsdNativePty;
24 import org.jline.terminal.impl.jna.linux.LinuxNativePty;
25 import org.jline.terminal.impl.jna.osx.OsXNativePty;
26 import org.jline.terminal.impl.jna.solaris.SolarisNativePty;
27
28 public abstract class JnaNativePty implements Pty {
29
30 private final int master;
31 private final int slave;
32 private final String name;
33 private final FileDescriptor masterFD;
34 private final FileDescriptor slaveFD;
35
36 public static JnaNativePty current() throws IOException {
37 if (Platform.isMac()) {
38 return OsXNativePty.current();
39 } else if (Platform.isLinux()) {
40 return LinuxNativePty.current();
41 } else if (Platform.isSolaris()) {
42 return SolarisNativePty.current();
43 } else if (Platform.isFreeBSD()) {
44 return FreeBsdNativePty.current();
45 } else {
46 throw new UnsupportedOperationException();
47 }
48 }
49
50 public static JnaNativePty open(Attributes attr, Size size) throws IOException {
51 if (Platform.isMac()) {
52 return OsXNativePty.open(attr, size);
53 } else if (Platform.isLinux()) {
54 return LinuxNativePty.open(attr, size);
55 } else if (Platform.isSolaris()) {
56 return SolarisNativePty.open(attr, size);
57 } else if (Platform.isFreeBSD()) {
58 return FreeBsdNativePty.open(attr, size);
59 } else {
60 throw new UnsupportedOperationException();
61 }
62 }
63
64 protected JnaNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) {
65 this.master = master;
66 this.slave = slave;
67 this.name = name;
68 this.masterFD = masterFD;
69 this.slaveFD = slaveFD;
70 }
71
72 @Override
73 public void close() throws IOException {
74 if (master > 0) {
75 getMasterInput().close();
76 }
77 if (slave > 0) {
78 getSlaveInput().close();
79 }
80 }
81
82 public int getMaster() {
83 return master;
84 }
85
86 public int getSlave() {
87 return slave;
88 }
89
90 public String getName() {
91 return name;
92 }
93
94 public FileDescriptor getMasterFD() {
95 return masterFD;
96 }
97
98 public FileDescriptor getSlaveFD() {
99 return slaveFD;
100 }
101
102 public InputStream getMasterInput() {
103 return new FileInputStream(getMasterFD());
104 }
105
106 public OutputStream getMasterOutput() {
107 return new FileOutputStream(getMasterFD());
108 }
109
110 public InputStream getSlaveInput() {
111 return new FileInputStream(getSlaveFD());
112 }
113
114 public OutputStream getSlaveOutput() {
115 return new FileOutputStream(getSlaveFD());
116 }
117
118 protected static FileDescriptor newDescriptor(int fd) {
119 try {
120 Constructor<FileDescriptor> cns = FileDescriptor.class.getDeclaredConstructor(int.class);
121 cns.setAccessible(true);
122 return cns.newInstance(fd);
123 } catch (Throwable e) {
124 throw new RuntimeException("Unable to create FileDescriptor", e);
125 }
126 }
127
128 @Override
129 public String toString() {
130 return "JnaNativePty[" + getName() + "]";
131 }
132
133 }