From 5a8ae1ba8f8078f0bfcce2ca1d59e3988e80c6bf Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Fri, 6 Sep 2002 14:08:29 +0000 Subject: [PATCH] code to get a master pseudo Terminal. --- .../utils/org/eclipse/cdt/utils/pty/PTY.java | 49 ++++++++++++ .../eclipse/cdt/utils/pty/PTYInputStream.java | 77 +++++++++++++++++++ .../cdt/utils/pty/PTYOutputStream.java | 73 ++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java create mode 100644 core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java create mode 100644 core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java new file mode 100644 index 00000000000..84c3f08a441 --- /dev/null +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java @@ -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"); + } + +} diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java new file mode 100644 index 00000000000..ad4caa68661 --- /dev/null +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java @@ -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"); + } + +} diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java new file mode 100644 index 00000000000..e9331f2e6cc --- /dev/null +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java @@ -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"); + } + +}