Skip to content

Commit 8717065

Browse files
committed
New JNI provider
1 parent 60bfd79 commit 8717065

File tree

42 files changed

+4703
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4703
-39
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ The dependencies are minimal: you may use JLine without any dependency on *nix s
4242
You can also use fine grained jars:
4343
* `jline-terminal`: the `Terminal` api and implementations
4444
* `jline-terminal-jansi`: terminal implementations leveraging the `Jansi` library
45+
* `jline-terminal-jni`: terminal implementations leveraging the JNI native library
4546
* `jline-terminal-jna`: terminal implementations leveraging the `JNA` library
47+
* `jline-terminal-ffm`: terminal implementations leveraging the Foreign Functions & Mapping layer
48+
* `jline-native`: the native library
4649
* `jline-reader`: the line reader (including completion, history, etc...)
4750
* `jline-style`: styling api
4851
* `jline-remote-ssh`: helpers for using jline with [Mina SSHD](http://mina.apache.org/sshd-project/)

demo/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<groupId>org.jline</groupId>
3232
<artifactId>jline-terminal-ffm</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.jline</groupId>
36+
<artifactId>jline-terminal-jni</artifactId>
37+
</dependency>
3438
<dependency>
3539
<groupId>org.jline</groupId>
3640
<artifactId>jline-terminal-jansi</artifactId>

jline/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<artifactId>jline-terminal</artifactId>
6969
<scope>test</scope>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.jline</groupId>
73+
<artifactId>jline-terminal-jni</artifactId>
74+
<scope>test</scope>
75+
</dependency>
7176
<dependency>
7277
<groupId>org.jline</groupId>
7378
<artifactId>jline-terminal-jansi</artifactId>
@@ -139,6 +144,14 @@
139144
<overWrite>false</overWrite>
140145
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
141146
</artifactItem>
147+
<artifactItem>
148+
<groupId>org.jline</groupId>
149+
<artifactId>jline-terminal-jni</artifactId>
150+
<classifier>sources</classifier>
151+
<type>jar</type>
152+
<overWrite>false</overWrite>
153+
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
154+
</artifactItem>
142155
<artifactItem>
143156
<groupId>org.jline</groupId>
144157
<artifactId>jline-terminal-jansi</artifactId>
@@ -213,6 +226,14 @@
213226
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
214227
<excludes>**/*.class</excludes>
215228
</artifactItem>
229+
<artifactItem>
230+
<groupId>org.jline</groupId>
231+
<artifactId>jline-terminal-jni</artifactId>
232+
<type>jar</type>
233+
<overWrite>false</overWrite>
234+
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
235+
<excludes>**/*.class</excludes>
236+
</artifactItem>
216237
<artifactItem>
217238
<groupId>org.jline</groupId>
218239
<artifactId>jline-terminal-jansi</artifactId>

native/Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ linux-armv6-digest:=@sha256:7bad6ab302af34bdf6634c8c2b02c8dc6ac932c67da9ecc199c5
2323
linux-x86-digest:=:latest
2424
windows-static-x86-digest:=@sha256:99d7069789560ef77a7504ead4a2b5e3c245cb45a45f964c74fecbf649398d3a
2525
windows-static-x64-digest:=@sha256:f159861bc80b29e5dafb223477167bec53ecec6cdacb051d31e90c5823542100
26+
windows-arm64-digest:=@sha256:f4b3c1a49ec8b53418cef1499dc3f9a54a5570b7a3ecdf42fc8c83eb94b01b7d
2627
cross-build-digest:=@sha256:8dbaa86462270db93ae1b1b319bdd88d89272faf3a68632daf4fa36b414a326e
2728
freebsd-crossbuild-digest:=@sha256:cda62697a15d8bdc0bc26e780b1771ee78f12c55e7d5813e62c478af5a747c43
2829
mcandre-snek-digest:=@sha256:9f84e9fcdf66daafc1f1c3fb772a6c97977714e17800aeac2e3bbe5dc5039dd0
@@ -68,9 +69,9 @@ $(JLINENATIVE_OUT)/%.o: src/main/native/%.c
6869
ifeq ($(OS_NAME), Windows)
6970
$(JLINENATIVE_OUT)/$(LIBNAME): ducible
7071
endif
71-
$(JLINENATIVE_OUT)/$(LIBNAME): $(JLINENATIVE_OUT)/jlinenative.o
72+
$(JLINENATIVE_OUT)/$(LIBNAME): $(JLINENATIVE_OUT)/jlinenative.o $(JLINENATIVE_OUT)/clibrary.o $(JLINENATIVE_OUT)/kernel32.o
7273
@mkdir -p $(@D)
73-
$(CC) $(CCFLAGS) -o $@ $(JLINENATIVE_OUT)/jlinenative.o $(LINKFLAGS)
74+
$(CC) $(CCFLAGS) -o $@ $(JLINENATIVE_OUT)/jlinenative.o $(JLINENATIVE_OUT)/clibrary.o $(JLINENATIVE_OUT)/kernel32.o $(LINKFLAGS)
7475
ifeq ($(OS_NAME), Windows)
7576
target/ducible/ducible $(JLINENATIVE_OUT)/$(LIBNAME)
7677
endif
@@ -82,7 +83,7 @@ NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
8283
# For cross-compilation, install docker. See also https://github.com/dockcross/dockcross
8384
# Disabled linux-armv6 build because of this issue; https://github.com/dockcross/dockcross/issues/190
8485
native-all: linux-x86 linux-x86_64 linux-arm linux-armv6 linux-armv7 \
85-
linux-arm64 linux-ppc64 win-x86 win-x86_64 mac-x86 mac-x86_64 mac-arm64 freebsd-x86 freebsd-x86_64
86+
linux-arm64 linux-ppc64 win-x86 win-x86_64 win-arm64 mac-x86 mac-x86_64 mac-arm64 freebsd-x86 freebsd-x86_64
8687

8788
native: $(NATIVE_DLL)
8889

@@ -139,6 +140,12 @@ target/dockcross/dockcross-windows-static-x64: dockcross
139140
win-x86_64: download-includes target/dockcross/dockcross-windows-static-x64
140141
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'
141142

143+
target/dockcross/dockcross-windows-arm64: dockcross
144+
docker run --rm dockcross/windows-arm64$(windows-arm64-digest) > target/dockcross/dockcross-windows-arm64
145+
chmod +x target/dockcross/dockcross-windows-arm64
146+
win-arm64: download-includes target/dockcross/dockcross-windows-arm64
147+
target/dockcross/dockcross-windows-arm64 bash -c 'make clean-native native CROSS_PREFIX=aarch64-w64-mingw32- OS_NAME=Windows OS_ARCH=arm64'
148+
142149
mac-x86: download-includes
143150
docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \
144151
-e CROSS_TRIPLE=i386-apple-darwin multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Mac OS_ARCH=x86

native/Makefile.common

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,105 +32,90 @@ Default_STRIP := $(CROSS_PREFIX)strip
3232
Default_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden
3333
Default_LINKFLAGS := -shared
3434
Default_LIBNAME := libjlinenative.so
35-
Default_JANSI_FLAGS :=
3635

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

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

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

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

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

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

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

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

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

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

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

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

121108
HPUX-ia64_32_CC := cc
122109
HPUX-ia64_32_STRIP := strip
123110
HPUX-ia64_32_CCFLAGS := -Itarget/inc -Itarget/inc/unix +Osize +z -Bhidden
124111
HPUX-ia64_32_LINKFLAGS := -b
125-
HPUX-ia64_32_LIBNAME := libjlinenative.so
126-
HPUX-ia64_32_JANSI_FLAGS :=
112+
SHPUX-ia64_32_LIBNAME := libjlinenative.so
127113

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

135120
Mac-x86_64_CC := gcc -arch $(OS_ARCH)
136121
Mac-x86_64_STRIP := strip -x
@@ -141,29 +126,25 @@ endif
141126
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
142127
Mac-x86_64_LINKFLAGS := -dynamiclib
143128
Mac-x86_64_LIBNAME := libjlinenative.jnilib
144-
Mac-x86_64_JANSI_FLAGS :=
145129

146130
Mac-arm64_CC := $(CROSS_PREFIX)clang -v
147131
Mac-arm64_STRIP := $(CROSS_PREFIX)strip -x
148132
MAC_SDK := /usr/local/osxcross/SDK/MacOSX11.3.sdk/
149133
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
150134
Mac-arm64_LINKFLAGS := -shared
151135
Mac-arm64_LIBNAME := libjlinenative.jnilib
152-
Mac-arm64_JANSI_FLAGS :=
153136

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

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

168149

169150
CC := $($(target)_CC)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2009-2023, the original author(s).
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 org.jline.nativ;
10+
11+
/**
12+
* Interface to access some low level POSIX functions,.
13+
*
14+
* @see JLineNativeLoader
15+
*/
16+
@SuppressWarnings("unused")
17+
public class CLibrary {
18+
19+
//
20+
// Initialization
21+
//
22+
23+
static {
24+
if (JLineNativeLoader.initialize()) {
25+
init();
26+
}
27+
}
28+
29+
private static native void init();
30+
31+
//
32+
// Constants
33+
//
34+
35+
public static int TCSANOW;
36+
public static int TCSADRAIN;
37+
public static int TCSAFLUSH;
38+
public static long TIOCGWINSZ;
39+
public static long TIOCSWINSZ;
40+
41+
/**
42+
* test whether a file descriptor refers to a terminal
43+
*
44+
* @param fd file descriptor
45+
* @return isatty() returns 1 if fd is an open file descriptor referring to a
46+
* terminal; otherwise 0 is returned, and errno is set to indicate the
47+
* error
48+
* @see <a href="http://man7.org/linux/man-pages/man3/isatty.3.html">ISATTY(3) man-page</a>
49+
* @see <a href="http://man7.org/linux/man-pages/man3/isatty.3p.html">ISATTY(3P) man-page</a>
50+
*/
51+
public static native int isatty(int fd);
52+
53+
public static native String ttyname(int filedes);
54+
55+
/**
56+
* The openpty() function finds an available pseudoterminal and returns
57+
* file descriptors for the master and slave in amaster and aslave.
58+
*
59+
* @param amaster master return value
60+
* @param aslave slave return value
61+
* @param name filename return value
62+
* @param termios optional pty attributes
63+
* @param winsize optional size
64+
* @return 0 on success
65+
* @see <a href="http://man7.org/linux/man-pages/man3/openpty.3.html">OPENPTY(3) man-page</a>
66+
*/
67+
public static native int openpty(int[] amaster, int[] aslave, byte[] name, Termios termios, WinSize winsize);
68+
69+
public static native int tcgetattr(int filedes, Termios termios);
70+
71+
public static native int tcsetattr(int filedes, int optional_actions, Termios termios);
72+
73+
/**
74+
* Control a STREAMS device.
75+
*
76+
* @see <a href="http://man7.org/linux/man-pages/man3/ioctl.3p.html">IOCTL(3P) man-page</a>
77+
*/
78+
public static native int ioctl(int filedes, long request, int[] params);
79+
80+
public static native int ioctl(int filedes, long request, WinSize params);
81+
82+
public static short getTerminalWidth(int fd) {
83+
WinSize sz = new WinSize();
84+
ioctl(fd, TIOCGWINSZ, sz);
85+
return sz.ws_col;
86+
}
87+
88+
/**
89+
* Window sizes.
90+
*
91+
* @see <a href="http://man7.org/linux/man-pages/man4/tty_ioctl.4.html">IOCTL_TTY(2) man-page</a>
92+
*/
93+
public static class WinSize {
94+
95+
static {
96+
if (JLineNativeLoader.initialize()) {
97+
init();
98+
}
99+
}
100+
101+
private static native void init();
102+
103+
public static int SIZEOF;
104+
105+
public short ws_row;
106+
public short ws_col;
107+
public short ws_xpixel;
108+
public short ws_ypixel;
109+
110+
public WinSize() {}
111+
112+
public WinSize(short ws_row, short ws_col) {
113+
this.ws_row = ws_row;
114+
this.ws_col = ws_col;
115+
}
116+
}
117+
118+
/**
119+
* termios structure for termios functions, describing a general terminal interface that is
120+
* provided to control asynchronous communications ports
121+
*
122+
* @see <a href="http://man7.org/linux/man-pages/man3/termios.3.html">TERMIOS(3) man-page</a>
123+
*/
124+
public static class Termios {
125+
126+
static {
127+
if (JLineNativeLoader.initialize()) {
128+
init();
129+
}
130+
}
131+
132+
private static native void init();
133+
134+
public static int SIZEOF;
135+
136+
public long c_iflag;
137+
public long c_oflag;
138+
public long c_cflag;
139+
public long c_lflag;
140+
public byte[] c_cc = new byte[32];
141+
public long c_ispeed;
142+
public long c_ospeed;
143+
}
144+
}

0 commit comments

Comments
 (0)