| 1 |
/* |
| 2 |
* Copyright (c) 2002-2016, the original author or authors. |
| 3 |
* |
| 4 |
* This software is distributable under the BSD license. See the terms of the |
| 5 |
* BSD license in the documentation provided with this software. |
| 6 |
* |
| 7 |
* http://www.opensource.org/licenses/bsd-license.php |
| 8 |
*/ |
| 9 |
package org.jline.terminal.impl.jna.freebsd; |
| 10 |
|
| 11 |
import java.io.FileDescriptor; |
| 12 |
import java.io.IOException; |
| 13 |
|
| 14 |
import com.sun.jna.LastErrorException; |
| 15 |
import com.sun.jna.Native; |
| 16 |
import com.sun.jna.Platform; |
| 17 |
import org.jline.terminal.Attributes; |
| 18 |
import org.jline.terminal.Size; |
| 19 |
import org.jline.terminal.impl.jna.JnaNativePty; |
| 20 |
|
| 21 |
import static org.jline.terminal.impl.jna.freebsd.CLibrary.TCSANOW; |
| 22 |
import static org.jline.terminal.impl.jna.freebsd.CLibrary.TIOCGWINSZ; |
| 23 |
import static org.jline.terminal.impl.jna.freebsd.CLibrary.TIOCSWINSZ; |
| 24 |
import static org.jline.terminal.impl.jna.freebsd.CLibrary.termios; |
| 25 |
import static org.jline.terminal.impl.jna.freebsd.CLibrary.winsize; |
| 26 |
|
| 27 |
public class FreeBsdNativePty extends JnaNativePty { |
| 28 |
|
| 29 |
private static final CLibrary C_LIBRARY = (CLibrary) Native.loadLibrary(Platform.C_LIBRARY_NAME, CLibrary.class); |
| 30 |
|
| 31 |
public interface UtilLibrary extends com.sun.jna.Library { |
| 32 |
|
| 33 |
void openpty(int[] master, int[] slave, byte[] name, CLibrary.termios t, CLibrary.winsize s) throws LastErrorException; |
| 34 |
|
| 35 |
UtilLibrary INSTANCE = (UtilLibrary) Native.loadLibrary("util", UtilLibrary.class); |
| 36 |
} |
| 37 |
|
| 38 |
public static FreeBsdNativePty current() throws IOException { |
| 39 |
int slave = 0; |
| 40 |
byte[] buf = new byte[64]; |
| 41 |
C_LIBRARY.ttyname_r(slave, buf, buf.length); |
| 42 |
int len = 0; |
| 43 |
while (buf[len] != 0) { |
| 44 |
len++; |
| 45 |
} |
| 46 |
String name = new String(buf, 0, len); |
| 47 |
return new FreeBsdNativePty(-1, null, slave, FileDescriptor.in, name); |
| 48 |
} |
| 49 |
|
| 50 |
public static FreeBsdNativePty open(Attributes attr, Size size) throws IOException { |
| 51 |
int[] master = new int[1]; |
| 52 |
int[] slave = new int[1]; |
| 53 |
byte[] buf = new byte[64]; |
| 54 |
UtilLibrary.INSTANCE.openpty(master, slave, buf, |
| 55 |
attr != null ? new termios(attr) : null, |
| 56 |
size != null ? new winsize(size) : null); |
| 57 |
int len = 0; |
| 58 |
while (buf[len] != 0) { |
| 59 |
len++; |
| 60 |
} |
| 61 |
String name = new String(buf, 0, len); |
| 62 |
return new FreeBsdNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name); |
| 63 |
} |
| 64 |
|
| 65 |
public FreeBsdNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) { |
| 66 |
super(master, masterFD, slave, slaveFD, name); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public Attributes getAttr() throws IOException { |
| 71 |
termios termios = new termios(); |
| 72 |
C_LIBRARY.tcgetattr(getSlave(), termios); |
| 73 |
return termios.toAttributes(); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void setAttr(Attributes attr) throws IOException { |
| 78 |
termios termios = new termios(attr); |
| 79 |
C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public Size getSize() throws IOException { |
| 84 |
winsize sz = new winsize(); |
| 85 |
C_LIBRARY.ioctl(getSlave(), TIOCGWINSZ, sz); |
| 86 |
return sz.toSize(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void setSize(Size size) throws IOException { |
| 91 |
winsize sz = new winsize(size); |
| 92 |
C_LIBRARY.ioctl(getSlave(), TIOCSWINSZ, sz); |
| 93 |
} |
| 94 |
|
| 95 |
} |