Skip to content

Commit a970519

Browse files
lahodajasotona
andcommittedMay 22, 2023
8306983: Do not invoke external programs when switch terminal to raw mode on selected platforms
Co-authored-by: Adam Sotona <asotona@openjdk.org> Reviewed-by: erikj, vromero, bpb
1 parent 6b65e57 commit a970519

File tree

22 files changed

+2072
-15
lines changed

22 files changed

+2072
-15
lines changed
 

‎make/modules/jdk.internal.le/Lib.gmk

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ include LibCommon.gmk
2727

2828
################################################################################
2929

30-
ifeq ($(call isTargetOs, windows), true)
30+
ifeq ($(call isTargetOs, linux macosx windows), true)
3131

3232
$(eval $(call SetupJdkLibrary, BUILD_LIBLE, \
3333
NAME := le, \
34+
TOOLCHAIN := TOOLCHAIN_LINK_CXX, \
3435
OPTIMIZATION := LOW, \
35-
CFLAGS := $(CFLAGS_JDKLIB), \
36+
CFLAGS := $(CXXFLAGS_JDKLIB), \
3637
LDFLAGS := $(LDFLAGS_JDKLIB), \
37-
LIBS := $(JDKLIB_LIBS) user32.lib, \
38+
LIBS_unix := $(JDKLIB_LIBS) $(LIBCXX), \
39+
LIBS_windows := $(JDKLIB_LIBS) user32.lib, \
3840
))
3941

4042
TARGETS += $(BUILD_LIBLE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.org.jline.terminal.impl.jna;
26+
27+
import java.io.IOException;
28+
import jdk.internal.org.jline.terminal.Attributes;
29+
import jdk.internal.org.jline.terminal.Size;
30+
import jdk.internal.org.jline.terminal.impl.jna.linux.LinuxNativePty;
31+
import jdk.internal.org.jline.terminal.spi.TerminalProvider;
32+
33+
class JDKNativePty {
34+
35+
static JnaNativePty current(TerminalProvider.Stream console) throws IOException {
36+
return LinuxNativePty.current(console);
37+
}
38+
39+
static JnaNativePty open(Attributes attr, Size size) throws IOException {
40+
return LinuxNativePty.open(attr, size);
41+
}
42+
43+
static int isatty(int fd) {
44+
return LinuxNativePty.isatty(fd);
45+
}
46+
47+
static String ttyname(int fd) {
48+
return LinuxNativePty.ttyname(fd);
49+
}
50+
51+
}

‎src/jdk.internal.le/linux/classes/jdk/internal/org/jline/terminal/impl/jna/linux/CLibrary.java

+389
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.org.jline.terminal.impl.jna.linux;
26+
27+
import jdk.internal.org.jline.terminal.impl.jna.LastErrorException;
28+
29+
public final class CLibraryImpl implements CLibrary {
30+
31+
static {
32+
System.loadLibrary("le");
33+
initIDs();
34+
}
35+
36+
private static native void initIDs();
37+
38+
@Override
39+
public native void tcgetattr(int fd, termios termios) throws LastErrorException;
40+
41+
@Override
42+
public native void tcsetattr(int fd, int cmd, termios termios) throws LastErrorException;
43+
44+
@Override
45+
public void ioctl(int fd, int cmd, winsize data) throws LastErrorException {
46+
if (cmd == CLibrary.TIOCGWINSZ || cmd == CLibrary.TIOCSWINSZ) {
47+
ioctl0(fd, cmd, data);
48+
} else {
49+
throw new UnsupportedOperationException("Command: " + cmd + ", not supported.");
50+
}
51+
}
52+
53+
private native void ioctl0(int fd, int cmd, winsize data) throws LastErrorException;
54+
55+
@Override
56+
public native int isatty(int fd);
57+
58+
@Override
59+
public native void ttyname_r(int fd, byte[] buf, int len) throws LastErrorException;
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.org.jline.terminal.impl.jna.linux;
26+
27+
import jdk.internal.org.jline.terminal.impl.jna.LastErrorException;
28+
import jdk.internal.org.jline.terminal.impl.jna.linux.LinuxNativePty.UtilLibrary;
29+
30+
public final class UtilLibraryImpl implements UtilLibrary {
31+
32+
@Override
33+
public void openpty(int[] master, int[] slave, byte[] name, CLibrary.termios t, CLibrary.winsize s) throws LastErrorException {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.