1
0
Fork 0
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:
Alain Magloire 2003-09-05 16:23:11 +00:00
parent 63ed749357
commit 553b984d2f
3 changed files with 40 additions and 18 deletions

View file

@ -15,12 +15,27 @@ import java.io.OutputStream;
public class PTY { public class PTY {
String slave; String slave;
public int master;
InputStream in; InputStream in;
OutputStream out; OutputStream out;
int master;
private static boolean hasPTY; 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 { public PTY() throws IOException {
if (hasPTY) { if (hasPTY) {
slave= forkpty(); slave= forkpty();
@ -29,8 +44,9 @@ public class PTY {
if (slave == null) { if (slave == null) {
throw new IOException("Can not create pty"); 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() { public String getSlaveName() {

View file

@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import org.eclipse.cdt.utils.pty.PTY.MasterFD;
class PTYInputStream extends InputStream { class PTYInputStream extends InputStream {
private int fd;
MasterFD master;
/** /**
* Fome a Unix valid file descriptor set a Reader. * Fome a Unix valid file descriptor set a Reader.
* @param desc file descriptor. * @param desc file descriptor.
*/ */
public PTYInputStream(int fd) { public PTYInputStream(MasterFD fd) {
this.fd = fd; master = fd;
} }
/** /**
@ -46,7 +49,7 @@ class PTYInputStream extends InputStream {
} }
byte[] tmpBuf = new byte[len]; byte[] tmpBuf = new byte[len];
len = read0(fd, tmpBuf, len); len = read0(master.getFD(), tmpBuf, len);
if (len <= 0) if (len <= 0)
return -1; return -1;
@ -59,12 +62,12 @@ class PTYInputStream extends InputStream {
* @exception IOException on error. * @exception IOException on error.
*/ */
public void close() throws IOException { public void close() throws IOException {
if (fd == -1) if (master.getFD() == -1)
return; return;
int status = close0(fd); int status = close0(master.getFD());
if (status == -1) if (status == -1)
throw new IOException("close error"); throw new IOException("close error");
fd = -1; master.setFD(-1);
} }
private native int read0(int fd, byte[] buf, int len) throws IOException; private native int read0(int fd, byte[] buf, int len) throws IOException;

View file

@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.IOException; import java.io.IOException;
import org.eclipse.cdt.utils.pty.PTY.MasterFD;
public class PTYOutputStream extends OutputStream { public class PTYOutputStream extends OutputStream {
private int fd;
MasterFD master;
/** /**
* Fome a Unix valid file descriptor set a Reader. * Fome a Unix valid file descriptor set a Reader.
* @param desc file descriptor. * @param desc file descriptor.
*/ */
public PTYOutputStream(int fd) { public PTYOutputStream(MasterFD fd) {
this.fd = fd; master = fd;
} }
/** /**
@ -37,7 +40,7 @@ public class PTYOutputStream extends OutputStream {
} }
byte[] tmpBuf = new byte[len]; byte[] tmpBuf = new byte[len];
System.arraycopy(b, off, tmpBuf, off, len); System.arraycopy(b, off, tmpBuf, off, len);
write0(fd, tmpBuf, len); write0(master.getFD(), tmpBuf, len);
} }
/** /**
* Implementation of read for the InputStream. * Implementation of read for the InputStream.
@ -55,12 +58,12 @@ public class PTYOutputStream extends OutputStream {
* @exception IOException on error. * @exception IOException on error.
*/ */
public void close() throws IOException { public void close() throws IOException {
if (fd == -1) if (master.getFD() == -1)
return; return;
int status = close0(fd); int status = close0(master.getFD());
if (status == -1) if (status == -1)
throw new IOException("close error"); //$NON-NLS-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; private native int write0(int fd, byte[] b, int len) throws IOException;