Class ExecPty

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, Pty

    public class ExecPty
    extends AbstractPty
    implements Pty
    A pseudoterminal implementation that uses external commands to interact with the terminal.

    The ExecPty class provides a Pty implementation that uses external commands (such as stty, tput, etc.) to interact with the terminal. This approach allows JLine to work in environments where native libraries are not available or cannot be used, by relying on standard command-line utilities that are typically available on Unix-like systems.

    This implementation executes external commands to perform operations such as:

    • Getting and setting terminal attributes
    • Getting and setting terminal size
    • Determining the current terminal device

    The ExecPty is typically used as a fallback when more direct methods of terminal interaction (such as JNI or JNA) are not available. While it provides good compatibility, it may have higher overhead due to the need to spawn external processes for many operations.

    See Also:
    AbstractPty, Pty
    • Constructor Detail

      • ExecPty

        protected ExecPty​(TerminalProvider provider,
                          SystemStream systemStream,
                          java.lang.String name)
        Creates a new ExecPty instance.

        This constructor creates a new ExecPty instance with the specified provider, system stream, and terminal device name. It is protected because instances should typically be created using the current(TerminalProvider, SystemStream) method.

        Parameters:
        provider - the terminal provider that will own this Pty
        systemStream - the system stream associated with this Pty
        name - the name of the terminal device (e.g., "/dev/tty")
    • Method Detail

      • current

        public static Pty current​(TerminalProvider provider,
                                  SystemStream systemStream)
                           throws java.io.IOException
        Creates an ExecPty instance for the current terminal.

        This method creates an ExecPty instance for the current terminal by executing the 'tty' command to determine the terminal device name. It is used to obtain a Pty object that can interact with the current terminal using external commands.

        Parameters:
        provider - the terminal provider that will own this Pty
        systemStream - the system stream (must be Output or Error) associated with this Pty
        Returns:
        a new ExecPty instance for the current terminal
        Throws:
        java.io.IOException - if the current terminal is not a TTY or if an error occurs while executing the 'tty' command
        java.lang.IllegalArgumentException - if systemStream is not Output or Error
      • close

        public void close()
                   throws java.io.IOException
        Closes this Pty.

        This implementation does nothing, as there are no resources to release. The terminal device is not actually opened by this class, so it does not need to be closed.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - if an I/O error occurs (never thrown by this implementation)
      • getName

        public java.lang.String getName()
        Returns the name of the terminal device.

        This method returns the name of the terminal device associated with this Pty, which was determined when the Pty was created. This is typically a device path such as "/dev/tty" or "/dev/pts/0".

        Returns:
        the name of the terminal device
      • getMasterInput

        public java.io.InputStream getMasterInput()
        Description copied from interface: Pty
        Returns the input stream for the master side of the PTY.

        This stream receives data that has been written to the slave's output stream. Terminal emulators typically read from this stream to get the output from processes running in the terminal.

        Specified by:
        getMasterInput in interface Pty
        Returns:
        the master's input stream
      • getMasterOutput

        public java.io.OutputStream getMasterOutput()
        Description copied from interface: Pty
        Returns the output stream for the master side of the PTY.

        Data written to this stream will be available for reading from the slave's input stream. Terminal emulators typically write to this stream to send input to processes running in the terminal.

        Specified by:
        getMasterOutput in interface Pty
        Returns:
        the master's output stream
      • doGetSlaveInput

        protected java.io.InputStream doGetSlaveInput()
                                               throws java.io.IOException
        Specified by:
        doGetSlaveInput in class AbstractPty
        Throws:
        java.io.IOException
      • getSlaveOutput

        public java.io.OutputStream getSlaveOutput()
                                            throws java.io.IOException
        Description copied from interface: Pty
        Returns the output stream for the slave side of the PTY.

        Data written to this stream will be available for reading from the master's input stream. Processes running in the terminal write to this stream to produce their output.

        Specified by:
        getSlaveOutput in interface Pty
        Returns:
        the slave's output stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • getAttr

        public Attributes getAttr()
                           throws java.io.IOException
        Description copied from interface: Pty
        Returns the current terminal attributes for this PTY.

        Terminal attributes control various aspects of terminal behavior, such as echo settings, line discipline, and control characters.

        Specified by:
        getAttr in interface Pty
        Returns:
        the current terminal attributes
        Throws:
        java.io.IOException - if an I/O error occurs
        See Also:
        Attributes
      • doSetAttr

        protected void doSetAttr​(Attributes attr)
                          throws java.io.IOException
        Specified by:
        doSetAttr in class AbstractPty
        Throws:
        java.io.IOException
      • getFlagsToSet

        protected java.util.List<java.lang.String> getFlagsToSet​(Attributes attr,
                                                                 Attributes current)
      • getSize

        public Size getSize()
                     throws java.io.IOException
        Description copied from interface: Pty
        Returns the current size (dimensions) of this PTY.

        The size includes the number of rows and columns in the terminal window.

        Specified by:
        getSize in interface Pty
        Returns:
        the current terminal size
        Throws:
        java.io.IOException - if an I/O error occurs
        See Also:
        Size
      • doGetConfig

        protected java.lang.String doGetConfig()
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • doGetAttr

        public static Attributes doGetAttr​(java.lang.String cfg)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • setSize

        public void setSize​(Size size)
                     throws java.io.IOException
        Description copied from interface: Pty
        Sets the size (dimensions) of this PTY.

        This method changes the number of rows and columns in the terminal window. When the size changes, a SIGWINCH signal is typically sent to processes running in the terminal.

        Specified by:
        setSize in interface Pty
        Parameters:
        size - the new terminal size to set
        Throws:
        java.io.IOException - if an I/O error occurs
        See Also:
        Size
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object