sabotage-linux / netbsd-curses

libcurses and dependencies taken from netbsd and brought into a portable shape (at least to musl or glibc)
Other
147 stars 14 forks source link

support for strfnames/strnames #34

Closed ghost closed 4 years ago

ghost commented 5 years ago

libtermkey requires strfnames/strnames, which are not currently available in netbsd-curses. The patch was created thanks to the help of Michael Forney.

Best regards, Daniel

diff -urN netbsd-curses.orig/libterminfo/names.awk netbsd-curses/libterminfo/names.awk
--- netbsd-curses.orig/libterminfo/names.awk    Thu Jan  1 00:00:00 1970
+++ netbsd-curses/libterminfo/names.awk Mon Nov 26 15:26:54 2018
@@ -0,0 +1,21 @@
+/TICODE_[a-z]+,/ {
+   names[++numnames] = substr($1, 8, length($1) - 8)
+}
+
+/^#define t_[a-z]+(t)/ {
+   fnames[++numfnames] = substr($2, 3, length($2) - 5)
+}
+
+END {
+   print "\nconst char *const strnames[] = {"
+   for (i = 1; i <= numnames; ++i)
+       print("\t\"" names[i] "\",")
+   print "\t(void *)0"
+   print "};"
+
+   print "\nconst char *const strfnames[] = {"
+   for (i = 1; i <= numfnames; ++i)
+       print("\t\"" fnames[i] "\",")
+   print "\t(void *)0"
+   print "};"
+}
diff -urN netbsd-curses.orig/libterminfo/term.h netbsd-curses/libterminfo/term.h
--- netbsd-curses.orig/libterminfo/term.h   Mon Nov 26 15:17:58 2018
+++ netbsd-curses/libterminfo/term.h    Mon Nov 26 15:26:00 2018
@@ -1944,6 +1944,8 @@
 #endif

 extern TERMINAL *cur_term;
+extern const char *const strfnames[];
+extern const char *const strnames[];

 /* setup functions */
 int        setupterm(const char *, int, int *);
diff -urN netbsd-curses.orig/GNUmakefile netbsd-curses/GNUmakefile
--- netbsd-curses.orig/GNUmakefile  Mon Nov 26 16:06:47 2018
+++ netbsd-curses/GNUmakefile   Mon Nov 26 16:08:49 2018
@@ -67,6 +67,7 @@
 TI_SRCS+=libterminfo/compile.c libterminfo/hash.c
 TI_SRCS+=libterminfo/cdbr.c
 TI_SRCS+=libterminfo/mi_vector_hash.c
+TI_SRCS+=libterminfo/names.c
 # Build in termcap emulation
 TI_SRCS+=libterminfo/termcap.c
 TI_LIBA=libterminfo/libterminfo.a
@@ -74,6 +75,9 @@
 TI_OBJS=$(TI_SRCS:.c=.o)
 TI_LOBJS=$(TI_SRCS:.c=.lo)
 TI_MAN =$(sort $(wildcard libterminfo/*.3))
+
+libterminfo/names.c: libterminfo/term.h
+   cat $^ | awk -f libterminfo/names.awk > $@

 libterminfo/term.o: CPPFLAGS+=-DINSTALL_PREFIX=\"$(PREFIX)\"
 libterminfo/term.lo: CPPFLAGS+=-DINSTALL_PREFIX=\"$(PREFIX)\"
diff -urN netbsd-curses.orig/libterminfo/GNUmakefile netbsd-curses/libterminfo/GNUmakefile
--- netbsd-curses.orig/libterminfo/GNUmakefile  Mon Nov 26 15:17:58 2018
+++ netbsd-curses/libterminfo/GNUmakefile   Mon Nov 26 15:23:01 2018
@@ -12,6 +12,7 @@
 SRCS+=     compile.c hash.c
 SRCS+=     cdbr.c
 SRCS+=     mi_vector_hash.c
+SRCS+=     names.c

 INCS=      term.h
 INCSDIR=   /usr/include
@@ -99,6 +100,9 @@

 term.c: compiled_terms.c
 termcap.c: termcap_hash.c
+
+names.c: term.h
+   cat $^ | awk -f names.awk > $@

 libterminfo.a: $(OBJS)
    ar cru $@ $^
rofl0r commented 5 years ago

thanks for your patch. i've forwarded it to roy smarples, the main author of the upstream sources.

i've seen through the site you linked that you sent a mail to https://www.openwall.com/lists/sabotage/2018/11/23/1 . i just want to let you know that that mailing list is currently unmaintained.

small nitpick: the code for the clean target is missing in the Makefile, but i'll tackle that after this gets merged upstream.

ghost commented 5 years ago

(BSD)Makefile: Yes, but I assumed that the (linux) port is based on your code in netbsd-curses/GNUMakefile. GNU make prefers GNUMakefile if it exists.

btw. we plan to use vis editor with netbsd-curses, but we have a problem with that.

https://lists.suckless.org/dev/1811/33034.html

rofl0r commented 5 years ago

just out of curiosity, what makes vis so desirable ? sounds like just another vim to me. and it seems to be the only program using these odd unibilium/libtermkey libs authored by the guys of ##perl.

ghost commented 5 years ago

@rofl0r just out of curiosity, what makes vis so desirable ?

statically linked + lua + netbsd-curses + all lexers, plugins and themes (full installation):

# du -hs /tmp/ports/vis/image/         
1.8M    /tmp/ports/vis/image
# ls -lh /tmp/ports/vis/image/usr/bin/vis                                                    
-r-xr-xr-x    1 root     bin        639.4k Nov 26 17:36 /tmp/ports/vis/image/usr/bin/vis

@rofl0r sounds like just another vim to me.

https://github.com/martanne/vis/wiki/Differences-from-Vi(m)

@rofl0r and it seems to be the only program using these odd unibilium/libtermkey libs authored by the guys of ##perl.

unibilium is optional - I want to use netbsd-curses instead.

btw. statically linked neat(vi)

# ls -lh /usr/bin/vi
-r-xr-xr-x    2 root     root       90.8K Nov 11 21:49 /usr/bin/vi
michaelforney commented 5 years ago

After I posted that awk snippet, I realized it isn't quite correct. It matches too much (not only from TISTRS enum), and doesn't match the last item in the enum (no trailing comma).

Some more work will need to go into names.awk if it is to be merged upstream.

ghost commented 5 years ago

@michaelforney: and doesn't match the last item in the enum (no trailing comma).

Yes, I also had doubts whether this is correct.

diff -urN netbsd-curses.orig/libterminfo/names.awk netbsd-curses/libterminfo/names.awk
--- netbsd-curses.orig/libterminfo/names.awk    Thu Jan  1 00:00:00 1970
+++ netbsd-curses/libterminfo/names.awk Mon Nov 26 15:26:54 2018
@@ -0,0 +1,21 @@
+/TICODE_[a-z]+,/ {
+   names[++numnames] = substr($1, 8, length($1) - 8)
+}
+
+/^#define t_[a-z]+(t)/ {
+   fnames[++numfnames] = substr($2, 3, length($2) - 5)
+}
+
+END {
+   print "\nconst char *const strnames[] = {"
+   for (i = 1; i <= numnames; ++i)
+       print("\t\"" names[i] "\",")
+   print "\t(void *)0,"
+   print "};"
+
+   print "\nconst char *const strfnames[] = {"
+   for (i = 1; i <= numfnames; ++i)
+       print("\t\"" fnames[i] "\",")
+   print "\t(void *)0,"
+   print "};"
+}
ghost commented 5 years ago

btw. MKnames from ncurses https://github.com/mirror/ncurses/blob/master/ncurses/tinfo/MKnames.awk

rsmarples commented 5 years ago

I don't see the point of returning a list of names terminfo provides like that. You can work out if a terminfo name exists at compile time and runtime like so:

/* runtime */
char *kdown1 = tigetstr("kcud1");
if (kdown1 == (char *)-1)
    kdown1 = NULL;

/* compile time */
#ifdef key_down
const char *kdown1 = key_down;
#else
const char *kdown1 = NULL;
#endif
ghost commented 5 years ago

@rsmarples: I don't see the point of returning a list of names terminfo provides like that.

ncurses compatibility?

This means that will have to rewrite the load_terminfo() function in the driver-ti.c in libtermkey.

Even on NetBSD, you can't use (netbsd-)curses if you want to compile the vis editor:

https://lists.suckless.org/dev/1811/33030.html

rsmarples commented 5 years ago

@silentbits happy to be ncurses compatible when it makes sense, but this doesn't make sense to me when you can get the same functionality from portable functions and variables.

libtermkey was deprecated upstream over 5 years ago, they should really look into using something more .... portable.

I'll ask other NetBSD terminfo/curses devs and see if they want to consider this non standard function.

rsmarples commented 5 years ago

In the meantime, you can get a list of terminfo names by grabbing the output of this command:

infocmp -1 | sed -n -e '1,2d' -e 's/[[:space:]]*\([^,=#]*\).*$/\1/p'

That works with NetBSD curses and ncurses. As it's only used when libtermkey fires up, any performance hit would be negligable.

ghost commented 5 years ago

@rsmarples: I agree with your opinion. The problem is deprecated libtermkey and should be replaced (or removed). I will try to extract the minimum necessary code from libtermkey, and then check what will be necessary to replace this functionality directly in vis. Thank you for your help and advice.

nbuwe commented 5 years ago

I agree with @rsmarples. I would also note that after getting the capability name the code calls funcname2keysym anyway and ignores the capability if it doesn't know about it. So I'd say iterating over strfnames[] just to use only the names the code already knows about is a very roundabout way to do what the code is trying to do.

michaelforney commented 4 years ago

It looks like as of libtermkey-0.21, it no longer uses strnames and strfnames, so I think this issue can be closed.