Skip to content

Commit

Permalink
New JNI provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 12, 2023
1 parent 60bfd79 commit 8717065
Show file tree
Hide file tree
Showing 42 changed files with 4,703 additions and 39 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ The dependencies are minimal: you may use JLine without any dependency on *nix s
You can also use fine grained jars:
* `jline-terminal`: the `Terminal` api and implementations
* `jline-terminal-jansi`: terminal implementations leveraging the `Jansi` library
* `jline-terminal-jni`: terminal implementations leveraging the JNI native library
* `jline-terminal-jna`: terminal implementations leveraging the `JNA` library
* `jline-terminal-ffm`: terminal implementations leveraging the Foreign Functions & Mapping layer
* `jline-native`: the native library
* `jline-reader`: the line reader (including completion, history, etc...)
* `jline-style`: styling api
* `jline-remote-ssh`: helpers for using jline with [Mina SSHD](http://mina.apache.org/sshd-project/)
Expand Down
4 changes: 4 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.jline</groupId>
<artifactId>jline-terminal-ffm</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions jline/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>jline-terminal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
Expand Down Expand Up @@ -139,6 +144,14 @@
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
<classifier>sources</classifier>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
Expand Down Expand Up @@ -213,6 +226,14 @@
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
<excludes>**/*.class</excludes>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jni</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
<excludes>**/*.class</excludes>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
Expand Down
13 changes: 10 additions & 3 deletions native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linux-armv6-digest:=@sha256:7bad6ab302af34bdf6634c8c2b02c8dc6ac932c67da9ecc199c5
linux-x86-digest:=:latest
windows-static-x86-digest:=@sha256:99d7069789560ef77a7504ead4a2b5e3c245cb45a45f964c74fecbf649398d3a
windows-static-x64-digest:=@sha256:f159861bc80b29e5dafb223477167bec53ecec6cdacb051d31e90c5823542100
windows-arm64-digest:=@sha256:f4b3c1a49ec8b53418cef1499dc3f9a54a5570b7a3ecdf42fc8c83eb94b01b7d
cross-build-digest:=@sha256:8dbaa86462270db93ae1b1b319bdd88d89272faf3a68632daf4fa36b414a326e
freebsd-crossbuild-digest:=@sha256:cda62697a15d8bdc0bc26e780b1771ee78f12c55e7d5813e62c478af5a747c43
mcandre-snek-digest:=@sha256:9f84e9fcdf66daafc1f1c3fb772a6c97977714e17800aeac2e3bbe5dc5039dd0
Expand Down Expand Up @@ -68,9 +69,9 @@ $(JLINENATIVE_OUT)/%.o: src/main/native/%.c
ifeq ($(OS_NAME), Windows)
$(JLINENATIVE_OUT)/$(LIBNAME): ducible
endif
$(JLINENATIVE_OUT)/$(LIBNAME): $(JLINENATIVE_OUT)/jlinenative.o
$(JLINENATIVE_OUT)/$(LIBNAME): $(JLINENATIVE_OUT)/jlinenative.o $(JLINENATIVE_OUT)/clibrary.o $(JLINENATIVE_OUT)/kernel32.o
@mkdir -p $(@D)
$(CC) $(CCFLAGS) -o $@ $(JLINENATIVE_OUT)/jlinenative.o $(LINKFLAGS)
$(CC) $(CCFLAGS) -o $@ $(JLINENATIVE_OUT)/jlinenative.o $(JLINENATIVE_OUT)/clibrary.o $(JLINENATIVE_OUT)/kernel32.o $(LINKFLAGS)
ifeq ($(OS_NAME), Windows)
target/ducible/ducible $(JLINENATIVE_OUT)/$(LIBNAME)
endif
Expand All @@ -82,7 +83,7 @@ NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
# For cross-compilation, install docker. See also https://github.com/dockcross/dockcross
# Disabled linux-armv6 build because of this issue; https://github.com/dockcross/dockcross/issues/190
native-all: linux-x86 linux-x86_64 linux-arm linux-armv6 linux-armv7 \
linux-arm64 linux-ppc64 win-x86 win-x86_64 mac-x86 mac-x86_64 mac-arm64 freebsd-x86 freebsd-x86_64
linux-arm64 linux-ppc64 win-x86 win-x86_64 win-arm64 mac-x86 mac-x86_64 mac-arm64 freebsd-x86 freebsd-x86_64

native: $(NATIVE_DLL)

Expand Down Expand Up @@ -139,6 +140,12 @@ target/dockcross/dockcross-windows-static-x64: dockcross
win-x86_64: download-includes target/dockcross/dockcross-windows-static-x64
target/dockcross/dockcross-windows-static-x64 bash -c 'make clean-native native CROSS_PREFIX=x86_64-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86_64'

target/dockcross/dockcross-windows-arm64: dockcross
docker run --rm dockcross/windows-arm64$(windows-arm64-digest) > target/dockcross/dockcross-windows-arm64
chmod +x target/dockcross/dockcross-windows-arm64
win-arm64: download-includes target/dockcross/dockcross-windows-arm64
target/dockcross/dockcross-windows-arm64 bash -c 'make clean-native native CROSS_PREFIX=aarch64-w64-mingw32- OS_NAME=Windows OS_ARCH=arm64'

mac-x86: download-includes
docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \
-e CROSS_TRIPLE=i386-apple-darwin multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Mac OS_ARCH=x86
Expand Down
21 changes: 1 addition & 20 deletions native/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -32,105 +32,90 @@ Default_STRIP := $(CROSS_PREFIX)strip
Default_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
Default_LINKFLAGS := -shared
Default_LIBNAME := libjlinenative.so
Default_JANSI_FLAGS :=

Linux-x86_CC := $(CROSS_PREFIX)gcc
Linux-x86_STRIP := $(CROSS_PREFIX)strip
Linux-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -m32 -fvisibility=hidden
Linux-x86_LINKFLAGS := -shared -static-libgcc
Linux-x86_LIBNAME := libjlinenative.so
Linux-x86_JANSI_FLAGS :=

Linux-x86_64_CC := $(CROSS_PREFIX)gcc
Linux-x86_64_STRIP := $(CROSS_PREFIX)strip
Linux-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -m64 -fvisibility=hidden
Linux-x86_64_LINKFLAGS := -shared -static-libgcc
Linux-x86_64_LIBNAME := libjlinenative.so
Linux-x86_64_JANSI_FLAGS :=

Linux-arm_CC := $(CROSS_PREFIX)gcc
Linux-arm_STRIP := $(CROSS_PREFIX)strip
Linux-arm_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -mfloat-abi=softfp -mfpu=vfp -fvisibility=hidden
Linux-arm_LINKFLAGS := -shared -static-libgcc
Linux-arm_LIBNAME := libjlinenative.so
Linux-arm_JANSI_FLAGS :=

Linux-armv6_CC := $(CROSS_PREFIX)gcc
Linux-armv6_STRIP := $(CROSS_PREFIX)strip
Linux-armv6_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden
Linux-armv6_LINKFLAGS := -shared -static-libgcc
Linux-armv6_LIBNAME := libjlinenative.so
Linux-armv6_JANSI_FLAGS :=

Linux-armv7_CC := $(CROSS_PREFIX)gcc
Linux-armv7_STRIP := $(CROSS_PREFIX)strip
Linux-armv7_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden
Linux-armv7_LINKFLAGS := -shared -static-libgcc
Linux-armv7_LIBNAME := libjlinenative.so
Linux-armv7_JANSI_FLAGS :=

Linux-arm64_CC := $(CROSS_PREFIX)gcc
Linux-arm64_STRIP := $(CROSS_PREFIX)strip
Linux-arm64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden
Linux-arm64_LINKFLAGS := -shared -static-libgcc
Linux-arm64_LIBNAME := libjlinenative.so
Linux-arm64_JANSI_FLAGS :=

Linux-ppc64_CC := $(CROSS_PREFIX)gcc
Linux-ppc64_STRIP := $(CROSS_PREFIX)strip
Linux-ppc64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
Linux-ppc64_LINKFLAGS := -shared -static-libgcc
Linux-ppc64_LIBNAME := libjlinenative.so
Linux-ppc64_JANSI_FLAGS :=

DragonFly-x86_64_CC := $(CROSS_PREFIX)cc
DragonFly-x86_64_STRIP := $(CROSS_PREFIX)strip
DragonFly-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -O2 -fPIC -fvisibility=hidden
DragonFly-x86_64_LINKFLAGS := -shared
DragonFly-x86_64_LIBNAME := libjlinenative.so
DragonFly-x86_64_JANSI_FLAGS :=

FreeBSD-x86_CC := $(CROSS_PREFIX)gcc
FreeBSD-x86_STRIP := $(CROSS_PREFIX)strip
FreeBSD-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
FreeBSD-x86_LINKFLAGS := -shared
FreeBSD-x86_LIBNAME := libjlinenative.so
FreeBSD-x86_JANSI_FLAGS :=

FreeBSD-x86_64_CC := $(CROSS_PREFIX)gcc
FreeBSD-x86_64_STRIP := $(CROSS_PREFIX)strip
FreeBSD-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
FreeBSD-x86_64_LINKFLAGS := -shared
FreeBSD-x86_64_LIBNAME := libjlinenative.so
FreeBSD-x86_64_JANSI_FLAGS :=

OpenBSD-x86_64_CC := $(CROSS_PREFIX)gcc
OpenBSD-x86_64_STRIP := $(CROSS_PREFIX)strip
OpenBSD-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
OpenBSD-x86_64_LINKFLAGS := -shared
OpenBSD-x86_64_LIBNAME := libjlinenative.so
OpenBSD-x86_64_JANSI_FLAGS :=

SunOS-sparcv9_CC := $(CROSS_PREFIX)gcc
SunOS-sparcv9_STRIP := $(CROSS_PREFIX)strip
SunOS-sparcv9_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -O2s-fPIC -m64 -fvisibility=hidden
SunOS-sparcv9_LINKFLAGS := -shared -static-libgcc
SunOS-sparcv9_LIBNAME := libjlinenative.so
SunOS-sparcv9_JANSI_FLAGS :=

HPUX-ia64_32_CC := cc
HPUX-ia64_32_STRIP := strip
HPUX-ia64_32_CCFLAGS := -Itarget/inc -Itarget/inc/unix +Osize +z -Bhidden
HPUX-ia64_32_LINKFLAGS := -b
HPUX-ia64_32_LIBNAME := libjlinenative.so
HPUX-ia64_32_JANSI_FLAGS :=
SHPUX-ia64_32_LIBNAME := libjlinenative.so

Mac-x86_CC := gcc
Mac-x86_STRIP := strip -x
Mac-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=10.4 -fvisibility=hidden
Mac-x86_LINKFLAGS := -dynamiclib
Mac-x86_LIBNAME := libjlinenative.jnilib
Mac-x86_JANSI_FLAGS := -DJANSI_ENABLE_LOCKING_STYLE=0

Mac-x86_64_CC := gcc -arch $(OS_ARCH)
Mac-x86_64_STRIP := strip -x
Expand All @@ -141,29 +126,25 @@ endif
Mac-x86_64_CCFLAGS := -I$(MAC_SDK)/System/Library/Frameworks/JavaVM.framework/Headers -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=10.6 -fvisibility=hidden
Mac-x86_64_LINKFLAGS := -dynamiclib
Mac-x86_64_LIBNAME := libjlinenative.jnilib
Mac-x86_64_JANSI_FLAGS :=

Mac-arm64_CC := $(CROSS_PREFIX)clang -v
Mac-arm64_STRIP := $(CROSS_PREFIX)strip -x
MAC_SDK := /usr/local/osxcross/SDK/MacOSX11.3.sdk/
Mac-arm64_CCFLAGS := -I$(MAC_SDK)/System/Library/Frameworks/JavaVM.framework/Headers -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=11.0 -fvisibility=hidden
Mac-arm64_LINKFLAGS := -shared
Mac-arm64_LIBNAME := libjlinenative.jnilib
Mac-arm64_JANSI_FLAGS :=

Windows-x86_CC := $(CROSS_PREFIX)gcc
Windows-x86_STRIP := $(CROSS_PREFIX)strip
Windows-x86_CCFLAGS := -D_JNI_IMPLEMENTATION_ -Itarget/inc -Itarget/inc/windows -Os
Windows-x86_LINKFLAGS := -Wl,--kill-at -shared -static-libgcc
Windows-x86_LIBNAME := jlinenative.dll
Windows-x86_JANSI_FLAGS :=

Windows-x86_64_CC := $(CROSS_PREFIX)gcc
Windows-x86_64_STRIP := $(CROSS_PREFIX)strip
Windows-x86_64_CCFLAGS := -D_JNI_IMPLEMENTATION_ -Itarget/inc -Itarget/inc/windows -Os
Windows-x86_64_LINKFLAGS := -Wl,--kill-at -shared -static-libgcc
Windows-x86_64_LIBNAME := jlinenative.dll
Windows-x86_64_JANSI_FLAGS :=


CC := $($(target)_CC)
Expand Down
144 changes: 144 additions & 0 deletions native/src/main/java/org/jline/nativ/CLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2009-2023, the original author(s).
*
* 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 org.jline.nativ;

/**
* Interface to access some low level POSIX functions,.
*
* @see JLineNativeLoader
*/
@SuppressWarnings("unused")
public class CLibrary {

//
// Initialization
//

static {
if (JLineNativeLoader.initialize()) {
init();
}
}

private static native void init();

//
// Constants
//

public static int TCSANOW;
public static int TCSADRAIN;
public static int TCSAFLUSH;
public static long TIOCGWINSZ;
public static long TIOCSWINSZ;

/**
* test whether a file descriptor refers to a terminal
*
* @param fd file descriptor
* @return isatty() returns 1 if fd is an open file descriptor referring to a
* terminal; otherwise 0 is returned, and errno is set to indicate the
* error
* @see <a href="http://man7.org/linux/man-pages/man3/isatty.3.html">ISATTY(3) man-page</a>
* @see <a href="http://man7.org/linux/man-pages/man3/isatty.3p.html">ISATTY(3P) man-page</a>
*/
public static native int isatty(int fd);

public static native String ttyname(int filedes);

/**
* The openpty() function finds an available pseudoterminal and returns
* file descriptors for the master and slave in amaster and aslave.
*
* @param amaster master return value
* @param aslave slave return value
* @param name filename return value
* @param termios optional pty attributes
* @param winsize optional size
* @return 0 on success
* @see <a href="http://man7.org/linux/man-pages/man3/openpty.3.html">OPENPTY(3) man-page</a>
*/
public static native int openpty(int[] amaster, int[] aslave, byte[] name, Termios termios, WinSize winsize);

public static native int tcgetattr(int filedes, Termios termios);

public static native int tcsetattr(int filedes, int optional_actions, Termios termios);

/**
* Control a STREAMS device.
*
* @see <a href="http://man7.org/linux/man-pages/man3/ioctl.3p.html">IOCTL(3P) man-page</a>
*/
public static native int ioctl(int filedes, long request, int[] params);

public static native int ioctl(int filedes, long request, WinSize params);

public static short getTerminalWidth(int fd) {
WinSize sz = new WinSize();
ioctl(fd, TIOCGWINSZ, sz);
return sz.ws_col;
}

/**
* Window sizes.
*
* @see <a href="http://man7.org/linux/man-pages/man4/tty_ioctl.4.html">IOCTL_TTY(2) man-page</a>
*/
public static class WinSize {

static {
if (JLineNativeLoader.initialize()) {
init();
}
}

private static native void init();

public static int SIZEOF;

public short ws_row;
public short ws_col;
public short ws_xpixel;
public short ws_ypixel;

public WinSize() {}

public WinSize(short ws_row, short ws_col) {
this.ws_row = ws_row;
this.ws_col = ws_col;
}
}

/**
* termios structure for termios functions, describing a general terminal interface that is
* provided to control asynchronous communications ports
*
* @see <a href="http://man7.org/linux/man-pages/man3/termios.3.html">TERMIOS(3) man-page</a>
*/
public static class Termios {

static {
if (JLineNativeLoader.initialize()) {
init();
}
}

private static native void init();

public static int SIZEOF;

public long c_iflag;
public long c_oflag;
public long c_cflag;
public long c_lflag;
public byte[] c_cc = new byte[32];
public long c_ispeed;
public long c_ospeed;
}
}
Loading

0 comments on commit 8717065

Please sign in to comment.