|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2020, 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 | + * https://opensource.org/licenses/BSD-3-Clause |
| 8 | + */ |
| 9 | +package jdk.internal.org.jline.terminal.impl.jna.linux; |
| 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 jdk.internal.org.jline.terminal.Attributes; |
| 18 | +import jdk.internal.org.jline.terminal.Size; |
| 19 | +import jdk.internal.org.jline.terminal.impl.jna.JnaNativePty; |
| 20 | +import jdk.internal.org.jline.terminal.impl.jna.LastErrorException; |
| 21 | +import jdk.internal.org.jline.terminal.spi.TerminalProvider; |
| 22 | + |
| 23 | +import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TCSADRAIN; |
| 24 | +import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TIOCGWINSZ; |
| 25 | +import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TIOCSWINSZ; |
| 26 | +import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.termios; |
| 27 | +import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.winsize; |
| 28 | + |
| 29 | +public class LinuxNativePty extends JnaNativePty { |
| 30 | + |
| 31 | +// private static final CLibrary C_LIBRARY = Native.load(Platform.C_LIBRARY_NAME, CLibrary.class); |
| 32 | + private static final CLibrary C_LIBRARY = new CLibraryImpl(); |
| 33 | + |
| 34 | + public interface UtilLibrary {// extends com.sun.jna.Library { |
| 35 | + |
| 36 | + void openpty(int[] master, int[] slave, byte[] name, CLibrary.termios t, CLibrary.winsize s) throws LastErrorException; |
| 37 | + |
| 38 | +// UtilLibrary INSTANCE = Native.load("util", UtilLibrary.class); |
| 39 | + UtilLibrary INSTANCE = new UtilLibraryImpl(); |
| 40 | + } |
| 41 | + |
| 42 | + public static LinuxNativePty current(TerminalProvider.Stream consoleStream) throws IOException { |
| 43 | + switch (consoleStream) { |
| 44 | + case Output: |
| 45 | + return new LinuxNativePty(-1, null, 0, FileDescriptor.in, 1, FileDescriptor.out, ttyname(0)); |
| 46 | + case Error: |
| 47 | + return new LinuxNativePty(-1, null, 0, FileDescriptor.in, 2, FileDescriptor.err, ttyname(0)); |
| 48 | + default: |
| 49 | + throw new IllegalArgumentException("Unsupport stream for console: " + consoleStream); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static LinuxNativePty open(Attributes attr, Size size) throws IOException { |
| 54 | + int[] master = new int[1]; |
| 55 | + int[] slave = new int[1]; |
| 56 | + byte[] buf = new byte[64]; |
| 57 | + UtilLibrary.INSTANCE.openpty(master, slave, buf, |
| 58 | + attr != null ? new termios(attr) : null, |
| 59 | + size != null ? new winsize(size) : null); |
| 60 | + int len = 0; |
| 61 | + while (buf[len] != 0) { |
| 62 | + len++; |
| 63 | + } |
| 64 | + String name = new String(buf, 0, len); |
| 65 | + return new LinuxNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name); |
| 66 | + } |
| 67 | + |
| 68 | + public LinuxNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) { |
| 69 | + super(master, masterFD, slave, slaveFD, name); |
| 70 | + } |
| 71 | + |
| 72 | + public LinuxNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, int slaveOut, FileDescriptor slaveOutFD, String name) { |
| 73 | + super(master, masterFD, slave, slaveFD, slaveOut, slaveOutFD, name); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Attributes getAttr() throws IOException { |
| 78 | + termios termios = new termios(); |
| 79 | + C_LIBRARY.tcgetattr(getSlave(), termios); |
| 80 | + return termios.toAttributes(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void doSetAttr(Attributes attr) throws IOException { |
| 85 | + termios termios = new termios(attr); |
| 86 | + termios org = new termios(); |
| 87 | + C_LIBRARY.tcgetattr(getSlave(), org); |
| 88 | + org.c_iflag = termios.c_iflag; |
| 89 | + org.c_oflag = termios.c_oflag; |
| 90 | + org.c_lflag = termios.c_lflag; |
| 91 | + System.arraycopy(termios.c_cc, 0, org.c_cc, 0, termios.c_cc.length); |
| 92 | + C_LIBRARY.tcsetattr(getSlave(), TCSADRAIN, org); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Size getSize() throws IOException { |
| 97 | + winsize sz = new winsize(); |
| 98 | + C_LIBRARY.ioctl(getSlave(), TIOCGWINSZ, sz); |
| 99 | + return sz.toSize(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void setSize(Size size) throws IOException { |
| 104 | + winsize sz = new winsize(size); |
| 105 | + C_LIBRARY.ioctl(getSlave(), TIOCSWINSZ, sz); |
| 106 | + } |
| 107 | + |
| 108 | + public static int isatty(int fd) { |
| 109 | + return C_LIBRARY.isatty(fd); |
| 110 | + } |
| 111 | + |
| 112 | + public static String ttyname(int slave) { |
| 113 | + byte[] buf = new byte[64]; |
| 114 | + C_LIBRARY.ttyname_r(slave, buf, buf.length); |
| 115 | + int len = 0; |
| 116 | + while (buf[len] != 0) { |
| 117 | + len++; |
| 118 | + } |
| 119 | + return new String(buf, 0, len); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments