Debian Codegraph @

terminal-jna/src/main/java/org/jline/terminal/impl/jna/linux/LinuxNativePty.java

raw
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.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 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.linux.CLibrary.TCSADRAIN;
22 import static org.jline.terminal.impl.jna.linux.CLibrary.TIOCGWINSZ;
23 import static org.jline.terminal.impl.jna.linux.CLibrary.TIOCSWINSZ;
24 import static org.jline.terminal.impl.jna.linux.CLibrary.termios;
25 import static org.jline.terminal.impl.jna.linux.CLibrary.winsize;
26
27 public class LinuxNativePty 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 LinuxNativePty 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 LinuxNativePty(-1, null, slave, FileDescriptor.in, name);
48 }
49
50 public static LinuxNativePty 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 LinuxNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name);
63 }
64
65 public LinuxNativePty(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 termios org = new termios();
80 C_LIBRARY.tcgetattr(getSlave(), org);
81 org.c_iflag = termios.c_iflag;
82 org.c_oflag = termios.c_oflag;
83 org.c_lflag = termios.c_lflag;
84 System.arraycopy(termios.c_cc, 0, org.c_cc, 0, termios.c_cc.length);
85 C_LIBRARY.tcsetattr(getSlave(), TCSADRAIN, org);
86 }
87
88 @Override
89 public Size getSize() throws IOException {
90 winsize sz = new winsize();
91 C_LIBRARY.ioctl(getSlave(), TIOCGWINSZ, sz);
92 return sz.toSize();
93 }
94
95 @Override
96 public void setSize(Size size) throws IOException {
97 winsize sz = new winsize(size);
98 C_LIBRARY.ioctl(getSlave(), TIOCSWINSZ, sz);
99 }
100
101 }