Skip to content

Commit

Permalink
Merge pull request #1 from kisoo-han/tools-4438-runtime-loading-tinfo
Browse files Browse the repository at this point in the history
dynamic loading of tinfo functions using dlopen ()
  • Loading branch information
kisoo-han authored Mar 16, 2023
2 parents fc92009 + 68e5047 commit 91d08f2
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions src/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ __RCSID("$NetBSD: terminal.c,v 1.32 2016/05/09 21:46:56 christos Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_TERMCAP_H
#include <termcap.h>
#endif
#ifdef HAVE_CURSES_H
#include <curses.h>
#elif HAVE_NCURSES_H
Expand All @@ -75,6 +72,19 @@ __RCSID("$NetBSD: terminal.c,v 1.32 2016/05/09 21:46:56 christos Exp $");
#include "el.h"
#include "fcns.h"

#include <dlfcn.h>

void *dl_handle = NULL;
char *(*tgoto) (const char *cap, int col, int row);
int (*tgetent) (char *bp, const char *name);
int (*tgetflag) (char *id);
int (*tgetnum) (char *id);
char *(*tgetstr) (char *id, char **area);
int (*tputs) (const char *str, int affcnt, int (*putc)(int));

#define TINFO_HIGH_VERSION 10
#define TINFO_LOW_VERSION 5

/*
* IMPORTANT NOTE: these routines are allowed to look at the current screen
* and the current position assuming that it is correct. If this is not
Expand Down Expand Up @@ -268,6 +278,49 @@ terminal_setflags(EditLine *el)
libedit_private int
terminal_init(EditLine *el)
{
char tinfo_so[TC_BUFSIZE];
int major_version;

for (major_version = TINFO_HIGH_VERSION; major_version >= TINFO_LOW_VERSION; major_version--){
sprintf (tinfo_so, "libtinfo.so.%d", major_version);
if ((dl_handle = dlopen (tinfo_so, RTLD_LAZY)) != NULL)
break;
}

if (dl_handle == NULL){
fprintf (stderr, "ERROR: Cannot load tinfo library. Please install tinfo library.\n");
_exit(127);
}

if ((tgoto = dlsym(dl_handle, "tgoto")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tgoto' function in %s.\n", tinfo_so);
_exit(127);
}

if ((tgetent = dlsym(dl_handle, "tgetent")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tgetent' function in %s.\n", tinfo_so);
_exit(127);
}

if ((tgetflag = dlsym(dl_handle, "tgetflag")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tgetflag' function in %s.\n", tinfo_so);
_exit(127);
}

if ((tgetnum = dlsym(dl_handle, "tgetnum")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tgetnum' function in %s.\n", tinfo_so);
_exit(127);
}

if ((tgetstr = dlsym(dl_handle, "tgetstr")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tgetstr' function in %s.\n", tinfo_so);
_exit(127);
}

if ((tputs = dlsym(dl_handle, "tputs")) == NULL){
fprintf (stderr, "ERROR: Cannot find 'tputs' function in %s.\n", tinfo_so);
_exit(127);
}

el->el_terminal.t_buf = el_malloc(TC_BUFSIZE *
sizeof(*el->el_terminal.t_buf));
Expand Down Expand Up @@ -320,6 +373,11 @@ libedit_private void
terminal_end(EditLine *el)
{

if (dl_handle != NULL) {
dlclose(dl_handle);
dl_handle = NULL;
}

el_free(el->el_terminal.t_buf);
el->el_terminal.t_buf = NULL;
el_free(el->el_terminal.t_cap);
Expand Down

0 comments on commit 91d08f2

Please sign in to comment.