Skip to content

Commit

Permalink
8306983: Do not invoke external programs when switch terminal to raw …
Browse files Browse the repository at this point in the history
…mode on selected platforms

Co-authored-by: Adam Sotona <asotona@openjdk.org>
Reviewed-by: erikj, vromero, bpb
  • Loading branch information
lahodaj and asotona committed May 22, 2023
1 parent 6b65e57 commit a970519
Show file tree
Hide file tree
Showing 22 changed files with 2,072 additions and 15 deletions.
8 changes: 5 additions & 3 deletions make/modules/jdk.internal.le/Lib.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ include LibCommon.gmk

################################################################################

ifeq ($(call isTargetOs, windows), true)
ifeq ($(call isTargetOs, linux macosx windows), true)

$(eval $(call SetupJdkLibrary, BUILD_LIBLE, \
NAME := le, \
TOOLCHAIN := TOOLCHAIN_LINK_CXX, \
OPTIMIZATION := LOW, \
CFLAGS := $(CFLAGS_JDKLIB), \
CFLAGS := $(CXXFLAGS_JDKLIB), \
LDFLAGS := $(LDFLAGS_JDKLIB), \
LIBS := $(JDKLIB_LIBS) user32.lib, \
LIBS_unix := $(JDKLIB_LIBS) $(LIBCXX), \
LIBS_windows := $(JDKLIB_LIBS) user32.lib, \
))

TARGETS += $(BUILD_LIBLE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.org.jline.terminal.impl.jna;

import java.io.IOException;
import jdk.internal.org.jline.terminal.Attributes;
import jdk.internal.org.jline.terminal.Size;
import jdk.internal.org.jline.terminal.impl.jna.linux.LinuxNativePty;
import jdk.internal.org.jline.terminal.spi.TerminalProvider;

class JDKNativePty {

static JnaNativePty current(TerminalProvider.Stream console) throws IOException {
return LinuxNativePty.current(console);
}

static JnaNativePty open(Attributes attr, Size size) throws IOException {
return LinuxNativePty.open(attr, size);
}

static int isatty(int fd) {
return LinuxNativePty.isatty(fd);
}

static String ttyname(int fd) {
return LinuxNativePty.ttyname(fd);
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.org.jline.terminal.impl.jna.linux;

import jdk.internal.org.jline.terminal.impl.jna.LastErrorException;

public final class CLibraryImpl implements CLibrary {

static {
System.loadLibrary("le");
initIDs();
}

private static native void initIDs();

@Override
public native void tcgetattr(int fd, termios termios) throws LastErrorException;

@Override
public native void tcsetattr(int fd, int cmd, termios termios) throws LastErrorException;

@Override
public void ioctl(int fd, int cmd, winsize data) throws LastErrorException {
if (cmd == CLibrary.TIOCGWINSZ || cmd == CLibrary.TIOCSWINSZ) {
ioctl0(fd, cmd, data);
} else {
throw new UnsupportedOperationException("Command: " + cmd + ", not supported.");
}
}

private native void ioctl0(int fd, int cmd, winsize data) throws LastErrorException;

@Override
public native int isatty(int fd);

@Override
public native void ttyname_r(int fd, byte[] buf, int len) throws LastErrorException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package jdk.internal.org.jline.terminal.impl.jna.linux;

import java.io.FileDescriptor;
import java.io.IOException;

//import com.sun.jna.LastErrorException;
//import com.sun.jna.Native;
//import com.sun.jna.Platform;
import jdk.internal.org.jline.terminal.Attributes;
import jdk.internal.org.jline.terminal.Size;
import jdk.internal.org.jline.terminal.impl.jna.JnaNativePty;
import jdk.internal.org.jline.terminal.impl.jna.LastErrorException;
import jdk.internal.org.jline.terminal.spi.TerminalProvider;

import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TCSADRAIN;
import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TIOCGWINSZ;
import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.TIOCSWINSZ;
import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.termios;
import static jdk.internal.org.jline.terminal.impl.jna.linux.CLibrary.winsize;

public class LinuxNativePty extends JnaNativePty {

// private static final CLibrary C_LIBRARY = Native.load(Platform.C_LIBRARY_NAME, CLibrary.class);
private static final CLibrary C_LIBRARY = new CLibraryImpl();

public interface UtilLibrary {// extends com.sun.jna.Library {

void openpty(int[] master, int[] slave, byte[] name, CLibrary.termios t, CLibrary.winsize s) throws LastErrorException;

// UtilLibrary INSTANCE = Native.load("util", UtilLibrary.class);
UtilLibrary INSTANCE = new UtilLibraryImpl();
}

public static LinuxNativePty current(TerminalProvider.Stream consoleStream) throws IOException {
switch (consoleStream) {
case Output:
return new LinuxNativePty(-1, null, 0, FileDescriptor.in, 1, FileDescriptor.out, ttyname(0));
case Error:
return new LinuxNativePty(-1, null, 0, FileDescriptor.in, 2, FileDescriptor.err, ttyname(0));
default:
throw new IllegalArgumentException("Unsupport stream for console: " + consoleStream);
}
}

public static LinuxNativePty open(Attributes attr, Size size) throws IOException {
int[] master = new int[1];
int[] slave = new int[1];
byte[] buf = new byte[64];
UtilLibrary.INSTANCE.openpty(master, slave, buf,
attr != null ? new termios(attr) : null,
size != null ? new winsize(size) : null);
int len = 0;
while (buf[len] != 0) {
len++;
}
String name = new String(buf, 0, len);
return new LinuxNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name);
}

public LinuxNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) {
super(master, masterFD, slave, slaveFD, name);
}

public LinuxNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, int slaveOut, FileDescriptor slaveOutFD, String name) {
super(master, masterFD, slave, slaveFD, slaveOut, slaveOutFD, name);
}

@Override
public Attributes getAttr() throws IOException {
termios termios = new termios();
C_LIBRARY.tcgetattr(getSlave(), termios);
return termios.toAttributes();
}

@Override
protected void doSetAttr(Attributes attr) throws IOException {
termios termios = new termios(attr);
termios org = new termios();
C_LIBRARY.tcgetattr(getSlave(), org);
org.c_iflag = termios.c_iflag;
org.c_oflag = termios.c_oflag;
org.c_lflag = termios.c_lflag;
System.arraycopy(termios.c_cc, 0, org.c_cc, 0, termios.c_cc.length);
C_LIBRARY.tcsetattr(getSlave(), TCSADRAIN, org);
}

@Override
public Size getSize() throws IOException {
winsize sz = new winsize();
C_LIBRARY.ioctl(getSlave(), TIOCGWINSZ, sz);
return sz.toSize();
}

@Override
public void setSize(Size size) throws IOException {
winsize sz = new winsize(size);
C_LIBRARY.ioctl(getSlave(), TIOCSWINSZ, sz);
}

public static int isatty(int fd) {
return C_LIBRARY.isatty(fd);
}

public static String ttyname(int slave) {
byte[] buf = new byte[64];
C_LIBRARY.ttyname_r(slave, buf, buf.length);
int len = 0;
while (buf[len] != 0) {
len++;
}
return new String(buf, 0, len);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.org.jline.terminal.impl.jna.linux;

import jdk.internal.org.jline.terminal.impl.jna.LastErrorException;
import jdk.internal.org.jline.terminal.impl.jna.linux.LinuxNativePty.UtilLibrary;

public final class UtilLibraryImpl implements UtilLibrary {

@Override
public void openpty(int[] master, int[] slave, byte[] name, CLibrary.termios t, CLibrary.winsize s) throws LastErrorException {
throw new UnsupportedOperationException();
}

}
Loading

1 comment on commit a970519

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.