mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 06:45:43 +02:00
Wrap the master fd, since they are share by two streams
This commit is contained in:
parent
63ed749357
commit
553b984d2f
3 changed files with 40 additions and 18 deletions
|
@ -15,12 +15,27 @@ import java.io.OutputStream;
|
|||
public class PTY {
|
||||
|
||||
String slave;
|
||||
public int master;
|
||||
InputStream in;
|
||||
OutputStream out;
|
||||
int master;
|
||||
|
||||
private static boolean hasPTY;
|
||||
|
||||
/**
|
||||
* The master fd is use on two streams. We need to wrap the fd
|
||||
* so when stream.close() is call the other stream is disable.
|
||||
*/
|
||||
public class MasterFD {
|
||||
|
||||
public int getFD() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setFD(int fd) {
|
||||
master = fd;
|
||||
}
|
||||
}
|
||||
|
||||
public PTY() throws IOException {
|
||||
if (hasPTY) {
|
||||
slave= forkpty();
|
||||
|
@ -29,8 +44,9 @@ public class PTY {
|
|||
if (slave == null) {
|
||||
throw new IOException("Can not create pty");
|
||||
}
|
||||
in = new PTYInputStream(master);
|
||||
out = new PTYOutputStream(master);
|
||||
|
||||
in = new PTYInputStream(new MasterFD());
|
||||
out = new PTYOutputStream(new MasterFD());
|
||||
}
|
||||
|
||||
public String getSlaveName() {
|
||||
|
|
|
@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty;
|
|||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.cdt.utils.pty.PTY.MasterFD;
|
||||
|
||||
class PTYInputStream extends InputStream {
|
||||
private int fd;
|
||||
|
||||
MasterFD master;
|
||||
|
||||
/**
|
||||
* Fome a Unix valid file descriptor set a Reader.
|
||||
* @param desc file descriptor.
|
||||
*/
|
||||
public PTYInputStream(int fd) {
|
||||
this.fd = fd;
|
||||
public PTYInputStream(MasterFD fd) {
|
||||
master = fd;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,7 +49,7 @@ class PTYInputStream extends InputStream {
|
|||
}
|
||||
byte[] tmpBuf = new byte[len];
|
||||
|
||||
len = read0(fd, tmpBuf, len);
|
||||
len = read0(master.getFD(), tmpBuf, len);
|
||||
if (len <= 0)
|
||||
return -1;
|
||||
|
||||
|
@ -59,12 +62,12 @@ class PTYInputStream extends InputStream {
|
|||
* @exception IOException on error.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (fd == -1)
|
||||
if (master.getFD() == -1)
|
||||
return;
|
||||
int status = close0(fd);
|
||||
int status = close0(master.getFD());
|
||||
if (status == -1)
|
||||
throw new IOException("close error");
|
||||
fd = -1;
|
||||
master.setFD(-1);
|
||||
}
|
||||
|
||||
private native int read0(int fd, byte[] buf, int len) throws IOException;
|
||||
|
|
|
@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty;
|
|||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.cdt.utils.pty.PTY.MasterFD;
|
||||
|
||||
public class PTYOutputStream extends OutputStream {
|
||||
private int fd;
|
||||
|
||||
MasterFD master;
|
||||
|
||||
/**
|
||||
* Fome a Unix valid file descriptor set a Reader.
|
||||
* @param desc file descriptor.
|
||||
*/
|
||||
public PTYOutputStream(int fd) {
|
||||
this.fd = fd;
|
||||
public PTYOutputStream(MasterFD fd) {
|
||||
master = fd;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,7 +40,7 @@ public class PTYOutputStream extends OutputStream {
|
|||
}
|
||||
byte[] tmpBuf = new byte[len];
|
||||
System.arraycopy(b, off, tmpBuf, off, len);
|
||||
write0(fd, tmpBuf, len);
|
||||
write0(master.getFD(), tmpBuf, len);
|
||||
}
|
||||
/**
|
||||
* Implementation of read for the InputStream.
|
||||
|
@ -55,12 +58,12 @@ public class PTYOutputStream extends OutputStream {
|
|||
* @exception IOException on error.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (fd == -1)
|
||||
if (master.getFD() == -1)
|
||||
return;
|
||||
int status = close0(fd);
|
||||
int status = close0(master.getFD());
|
||||
if (status == -1)
|
||||
throw new IOException("close error"); //$NON-NLS-1$
|
||||
fd = -1;
|
||||
master.setFD(-1);
|
||||
}
|
||||
|
||||
private native int write0(int fd, byte[] b, int len) throws IOException;
|
||||
|
|
Loading…
Add table
Reference in a new issue