mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
code to get a master pseudo Terminal.
This commit is contained in:
parent
ccb0662448
commit
5a8ae1ba8f
3 changed files with 199 additions and 0 deletions
|
@ -0,0 +1,49 @@
|
||||||
|
package org.eclipse.cdt.utils.pty;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author alain
|
||||||
|
*
|
||||||
|
* To change this generated comment edit the template variable "typecomment":
|
||||||
|
* Window>Preferences>Java>Templates.
|
||||||
|
* To enable and disable the creation of type comments go to
|
||||||
|
* Window>Preferences>Java>Code Generation.
|
||||||
|
*/
|
||||||
|
public class PTY {
|
||||||
|
|
||||||
|
String slave;
|
||||||
|
public int master;
|
||||||
|
InputStream in;
|
||||||
|
OutputStream out;
|
||||||
|
|
||||||
|
public PTY() throws IOException {
|
||||||
|
slave= forkpty();
|
||||||
|
if (slave == null) {
|
||||||
|
throw new IOException("Can not create pty");
|
||||||
|
}
|
||||||
|
in = new PTYInputStream(master);
|
||||||
|
out = new PTYOutputStream(master);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSlaveName() {
|
||||||
|
return slave;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutputStream getOutputStream() {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream getInputStream() {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
native String forkpty();
|
||||||
|
|
||||||
|
static {
|
||||||
|
System.loadLibrary("pty");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package org.eclipse.cdt.utils.pty;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (c) Copyright QNX Software Systems Ltd. 2002.
|
||||||
|
* All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
class PTYInputStream extends InputStream {
|
||||||
|
private int fd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fome a Unix valid file descriptor set a Reader.
|
||||||
|
* @param desc file descriptor.
|
||||||
|
*/
|
||||||
|
public PTYInputStream(int fd) {
|
||||||
|
this.fd = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of read for the InputStream.
|
||||||
|
*
|
||||||
|
* @exception IOException on error.
|
||||||
|
*/
|
||||||
|
public int read() throws IOException {
|
||||||
|
byte b[] = new byte[1];
|
||||||
|
if (1 != read(b, 0, 1))
|
||||||
|
return -1;
|
||||||
|
return (int) b[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see InputStream#read(byte[], int, int)
|
||||||
|
*/
|
||||||
|
public int read(byte[] buf, int off, int len) throws IOException {
|
||||||
|
if (buf == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
} else if ((off < 0) || (off > buf.length)
|
||||||
|
|| (len < 0) || ((off + len) > buf.length)
|
||||||
|
|| ((off + len) < 0)) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
} else if (len == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
byte[] tmpBuf = new byte[len];
|
||||||
|
|
||||||
|
len = read0(fd, tmpBuf, len);
|
||||||
|
if (len <= 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
System.arraycopy(tmpBuf, 0, buf, off, len);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the Reader
|
||||||
|
* @exception IOException on error.
|
||||||
|
*/
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (fd == -1)
|
||||||
|
return;
|
||||||
|
int status = close0(fd);
|
||||||
|
if (status == -1)
|
||||||
|
throw new IOException("close error");
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private native int read0(int fd, byte[] buf, int len) throws IOException;
|
||||||
|
native int close0(int fd);
|
||||||
|
|
||||||
|
static {
|
||||||
|
System.loadLibrary("pty");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
package org.eclipse.cdt.utils.pty;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (c) Copyright QNX Software Systems Ltd. 2002.
|
||||||
|
* All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class PTYOutputStream extends OutputStream {
|
||||||
|
private int fd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fome a Unix valid file descriptor set a Reader.
|
||||||
|
* @param desc file descriptor.
|
||||||
|
*/
|
||||||
|
public PTYOutputStream(int fd) {
|
||||||
|
this.fd = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see OutputStream#write(byte[], int, int)
|
||||||
|
*/
|
||||||
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
|
if (b == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
} else if (
|
||||||
|
(off < 0)
|
||||||
|
|| (off > b.length)
|
||||||
|
|| (len < 0)
|
||||||
|
|| ((off + len) > b.length)
|
||||||
|
|| ((off + len) < 0)) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
} else if (len == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
byte[] tmpBuf = new byte[len];
|
||||||
|
System.arraycopy(b, off, tmpBuf, off, len);
|
||||||
|
write0(fd, tmpBuf, len);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Implementation of read for the InputStream.
|
||||||
|
*
|
||||||
|
* @exception IOException on error.
|
||||||
|
*/
|
||||||
|
public void write(int b) throws IOException {
|
||||||
|
byte[] buf = new byte[1];
|
||||||
|
buf[0] = (byte) b;
|
||||||
|
write(buf, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the Reader
|
||||||
|
* @exception IOException on error.
|
||||||
|
*/
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (fd == -1)
|
||||||
|
return;
|
||||||
|
int status = close0(fd);
|
||||||
|
if (status == -1)
|
||||||
|
throw new IOException("close error");
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private native int write0(int fd, byte[] b, int len);
|
||||||
|
private native int close0(int fd);
|
||||||
|
|
||||||
|
static {
|
||||||
|
System.loadLibrary("pty");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue