| 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.solaris; |
| 10 |
|
| 11 |
import java.io.FileDescriptor; |
| 12 |
import java.io.IOException; |
| 13 |
|
| 14 |
import com.sun.jna.Native; |
| 15 |
import com.sun.jna.Platform; |
| 16 |
import org.jline.terminal.Attributes; |
| 17 |
import org.jline.terminal.Size; |
| 18 |
import org.jline.terminal.impl.jna.JnaNativePty; |
| 19 |
|
| 20 |
import static org.jline.terminal.impl.jna.solaris.CLibrary.TCSANOW; |
| 21 |
import static org.jline.terminal.impl.jna.solaris.CLibrary.TIOCGWINSZ; |
| 22 |
import static org.jline.terminal.impl.jna.solaris.CLibrary.TIOCSWINSZ; |
| 23 |
import static org.jline.terminal.impl.jna.solaris.CLibrary.termios; |
| 24 |
import static org.jline.terminal.impl.jna.solaris.CLibrary.winsize; |
| 25 |
|
| 26 |
public class SolarisNativePty extends JnaNativePty { |
| 27 |
|
| 28 |
private static final CLibrary C_LIBRARY = (CLibrary) Native.loadLibrary(Platform.C_LIBRARY_NAME, CLibrary.class); |
| 29 |
|
| 30 |
public static SolarisNativePty current() throws IOException { |
| 31 |
int slave = 0; |
| 32 |
byte[] buf = new byte[64]; |
| 33 |
C_LIBRARY.ttyname_r(slave, buf, buf.length); |
| 34 |
int len = 0; |
| 35 |
while (buf[len] != 0) { |
| 36 |
len++; |
| 37 |
} |
| 38 |
String name = new String(buf, 0, len); |
| 39 |
return new SolarisNativePty(-1, null, slave, FileDescriptor.in, name); |
| 40 |
} |
| 41 |
|
| 42 |
public static SolarisNativePty open(Attributes attr, Size size) throws IOException { |
| 43 |
int[] master = new int[1]; |
| 44 |
int[] slave = new int[1]; |
| 45 |
byte[] buf = new byte[64]; |
| 46 |
C_LIBRARY.openpty(master, slave, buf, |
| 47 |
attr != null ? new termios(attr) : null, |
| 48 |
size != null ? new winsize(size) : null); |
| 49 |
int len = 0; |
| 50 |
while (buf[len] != 0) { |
| 51 |
len++; |
| 52 |
} |
| 53 |
String name = new String(buf, 0, len); |
| 54 |
return new SolarisNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name); |
| 55 |
} |
| 56 |
|
| 57 |
public SolarisNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) { |
| 58 |
super(master, masterFD, slave, slaveFD, name); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public Attributes getAttr() throws IOException { |
| 63 |
termios termios = new termios(); |
| 64 |
C_LIBRARY.tcgetattr(getSlave(), termios); |
| 65 |
return termios.toAttributes(); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void setAttr(Attributes attr) throws IOException { |
| 70 |
termios termios = new termios(attr); |
| 71 |
C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public Size getSize() throws IOException { |
| 76 |
winsize sz = new winsize(); |
| 77 |
C_LIBRARY.ioctl(getSlave(), TIOCGWINSZ, sz); |
| 78 |
return sz.toSize(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void setSize(Size size) throws IOException { |
| 83 |
winsize sz = new winsize(size); |
| 84 |
C_LIBRARY.ioctl(getSlave(), TIOCSWINSZ, sz); |
| 85 |
} |
| 86 |
|
| 87 |
} |