Skip to content

Commit

Permalink
Jansi native pty support, fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 17, 2017
1 parent be0e9c2 commit 33eb5d4
Show file tree
Hide file tree
Showing 11 changed files with 1,488 additions and 4 deletions.
2 changes: 1 addition & 1 deletion terminal-jansi/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2002-2016, the original author or authors.
Copyright (c) 2002-2017, the original author or authors.
This software is distributable under the BSD license. See the terms of the
BSD license in the documentation provided with this software.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2002-2017, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.terminal.impl.jansi;

import org.fusesource.jansi.internal.CLibrary;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.spi.Pty;
import org.jline.utils.OSUtils;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.fusesource.jansi.internal.CLibrary.TCSANOW;
import static org.jline.utils.ExecHelper.exec;

public abstract class JansiNativePty implements Pty {

private final int master;
private final int slave;
private final String name;
private final FileDescriptor masterFD;
private final FileDescriptor slaveFD;

public JansiNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) {
this.master = master;
this.slave = slave;
this.name = name;
this.masterFD = masterFD;
this.slaveFD = slaveFD;
}

protected static String ttyname() throws IOException {
String name = exec(true, OSUtils.TTY_COMMAND);
return name.trim();
}

@Override
public void close() throws IOException {
if (master > 0) {
getMasterInput().close();
}
if (slave > 0) {
getSlaveInput().close();
}
}

public int getMaster() {
return master;
}

public int getSlave() {
return slave;
}

public String getName() {
return name;
}

public FileDescriptor getMasterFD() {
return masterFD;
}

public FileDescriptor getSlaveFD() {
return slaveFD;
}

public InputStream getMasterInput() {
return new FileInputStream(getMasterFD());
}

public OutputStream getMasterOutput() {
return new FileOutputStream(getMasterFD());
}

public InputStream getSlaveInput() {
return new FileInputStream(getSlaveFD());
}

public OutputStream getSlaveOutput() {
return new FileOutputStream(getSlaveFD());
}


@Override
public Attributes getAttr() throws IOException {
CLibrary.Termios tios = new CLibrary.Termios();
CLibrary.tcgetattr(slave, tios);
return toAttributes(tios);
}

@Override
public void setAttr(Attributes attr) throws IOException {
CLibrary.Termios tios = toTermios(attr);
CLibrary.tcsetattr(slave, TCSANOW, tios);
}

@Override
public Size getSize() throws IOException {
CLibrary.WinSize sz = new CLibrary.WinSize();
CLibrary.ioctl(slave, CLibrary.TIOCGWINSZ, sz);
return new Size(sz.ws_col, sz.ws_row);
}

@Override
public void setSize(Size size) throws IOException {
CLibrary.WinSize sz = new CLibrary.WinSize((short) size.getRows(), (short) size.getColumns());
CLibrary.ioctl(slave, CLibrary.TIOCSWINSZ, sz);
}

protected abstract CLibrary.Termios toTermios(Attributes t);

protected abstract Attributes toAttributes(CLibrary.Termios tios);


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
/*
* Copyright (c) 2002-2017, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.terminal.impl.jansi;

import org.jline.terminal.Terminal;
import org.jline.terminal.impl.jansi.freebsd.FreeBsdNativePty;
import org.jline.terminal.impl.jansi.linux.LinuxNativePty;
import org.jline.terminal.impl.jansi.osx.OsXNativePty;
import org.jline.terminal.impl.jansi.solaris.SolarisNativePty;
import org.jline.terminal.impl.jansi.win.JansiWinSysTerminal;
import org.jline.terminal.spi.JansiSupport;
import org.jline.terminal.spi.Pty;

import java.io.IOException;

public class JansiSupportImpl implements JansiSupport {

@Override
public Pty current() throws IOException {
String osName = System.getProperty("os.name");
if (osName.startsWith("Linux")) {
return LinuxNativePty.current();
}
else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
return OsXNativePty.current();
}
else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) {
return SolarisNativePty.current();
}
else if (osName.startsWith("FreeBSD")) {
return FreeBsdNativePty.current();
}
throw new UnsupportedOperationException();
}

@Override
public Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException {
return new JansiWinSysTerminal(name, nativeSignals, signalHandler);
Expand Down
Loading

0 comments on commit 33eb5d4

Please sign in to comment.