From e467f630757b2551756cadf39316af8ac7372aa8 Mon Sep 17 00:00:00 2001
From: hanwen <hanwen>
Date: Sun, 6 Mar 2005 19:30:25 +0000
Subject: [PATCH] (open_library): dlopen libkpathsea.so

---
 ChangeLog           |  9 ++---
 VERSION             |  1 -
 config.hh.in        |  4 +-
 config.make.in      |  2 +-
 kpath-guile/kpath.c | 98 ++++++++++++++++++++++++++++++++++++---------
 5 files changed, 86 insertions(+), 28 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d3409d655a..115564af53 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2005-03-06  Han-Wen Nienhuys  <hanwen@xs4all.nl>
+
+	* kpath-guile/kpath.c (open_library): dlopen libkpathsea.so
+
 2005-03-06  Jan Nieuwenhuizen  <janneke@gnu.org>
 
 	Drop run-time dependency on teTeX.
@@ -40,8 +44,6 @@
 
 	* lily/font-config.cc (init_fontconfig): add cff/ too.
 
-	* mf/GNUmakefile: generate .commonff to not confuse fontconfig.
-
 	* ly/generate-embedded-cff.ly: capitalize.
 
 	* lily/pango-select.cc (select_pango_font): use ::find_pango_font,
@@ -53,9 +55,6 @@
 	* lily/main.cc: only look at one prefix directory, LILYPONDPREFIX
 	overrides all. 
 
-	* config.make (package_suffix): use lilypond/MAJOR.MINOR as install
-	directory.
-
 	* configure.in (gui_b): remove ec mftrace check
 
 	* config.hh.in: use lilypond-Major.Minor as data directory.
diff --git a/VERSION b/VERSION
index b54a42ecf3..f2bd041d8b 100644
--- a/VERSION
+++ b/VERSION
@@ -1,5 +1,4 @@
 PACKAGE_NAME=LilyPond
-
 MAJOR_VERSION=2
 MINOR_VERSION=5
 PATCH_LEVEL=14
diff --git a/config.hh.in b/config.hh.in
index 759ad72f0b..9e482eab0b 100644
--- a/config.hh.in
+++ b/config.hh.in
@@ -5,9 +5,11 @@
 /* datadir */
 #define DATADIR "@DATADIR@"
 
+#define TOPLEVEL_VERSION "@TOPLEVEL_VERSION@"
+
 /* LilyPond init and input base directory */
 
-#define PACKAGE_DATADIR DATADIR "/" PACKAGE / "@TOPLEVEL_VERSION@"
+#define PACKAGE_DATADIR DATADIR "/" PACKAGE "/" TOPLEVEL_VERSION
 #define LILYPOND_DATADIR PACKAGE_DATADIR
 
 /* default lilypond locale dir */
diff --git a/config.make.in b/config.make.in
index 553f291aa9..4508b2a92e 100644
--- a/config.make.in
+++ b/config.make.in
@@ -43,7 +43,7 @@ elispdir = $(datadir)/emacs/site-lisp
 # forced instead of $(datadir)/vim
 
 # move out of config.make.in?
-package_suffix = $(package)$(MAJOR_VERSION).$(MINOR_VERSION)
+package_suffix = $(package)/$(TOPLEVEL_VERSION)
 package_datadir = $(datadir)/$(package_suffix)
 package_infodir = $(infodir)/$(package_suffix)
 package_libdir = $(libdir)/$(package_suffix)
diff --git a/kpath-guile/kpath.c b/kpath-guile/kpath.c
index 01f3fde558..5e6e9155eb 100644
--- a/kpath-guile/kpath.c
+++ b/kpath-guile/kpath.c
@@ -12,8 +12,7 @@
 #include "config.hh"
 #include "guile-compatibility.hh"
 
-#if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
-
+#include <dlfcn.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -42,7 +41,16 @@ I found a somewhat more elegant patch for this: Just #include
 
 
 #if KPATHSEA
-/* FIXME: this should be part of kpathsea */
+
+
+#if HAVE_KPATHSEA_KPATHSEA_H
+static  void *kpathsea_handle = 0;
+static  char *(*dl_kpse_find_file) (char const*, kpse_file_format_type, boolean) = 0;
+static  void (*dl_kpse_maketex_option) (char const*, boolean) = 0;
+static  void (*dl_kpse_set_program_name) (char const*) = 0;
+static  char const *(*dl_kpse_init_format) (kpse_file_format_type) = 0;
+static  char *(*dl_kpse_var_expand) (char const*) = 0;
+static  kpse_format_info_type (*dl_kpse_format_info)[kpse_last_format] = 0;
 
 kpse_file_format_type
 kpathsea_find_format (const char* name)
@@ -51,12 +59,12 @@ kpathsea_find_format (const char* name)
   int len = strlen (name);
   for (i = 0; i < kpse_last_format; i++)
     {
-      if (!kpse_format_info[i].type)
-        kpse_init_format ((kpse_file_format_type) i);
+       if (!(*dl_kpse_format_info)[i].type)
+        (*dl_kpse_init_format) ((kpse_file_format_type) i);
 
-      char const **suffixes[] = { kpse_format_info[i].suffix,
-				  kpse_format_info[i].alt_suffix };
-      for (int j = 0; j < 2; j++)
+       char const **suffixes[] = { (*dl_kpse_format_info)[i].suffix,
+				   (*dl_kpse_format_info)[i].alt_suffix };
+       for (int j = 0; j < 2; j++)
 	for (char const **p = suffixes[j]; p && *p; p++)
 	  {
 	    int suflen = strlen (*p);
@@ -69,11 +77,6 @@ kpathsea_find_format (const char* name)
 }
 #endif
 
-
-
-
-
-
 //	   "Return the absolute file name of @var{name}, "
 //	   "or @code{#f} if not found.")
 SCM
@@ -82,8 +85,8 @@ ly_kpathsea_find_file(SCM name)
   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
 
   char const * nm = scm_i_string_chars (name);
-  char *p = kpse_find_file (nm, kpathsea_find_format (nm),
-			    true);
+  char *p = (*dl_kpse_find_file) (nm, kpathsea_find_format (nm),
+				  true);
   if (p)
     return scm_makfrom0str (p);
   return SCM_BOOL_F;
@@ -103,16 +106,71 @@ SCM ly_kpathsea_expand_variable(SCM var)
 }
 
 
+static char const* LIBKPATHSEA = "libkpathsea.so";
+
+int
+open_library ()
+{
+#if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
+  struct
+  {
+    void **func_pointer;
+    char const *name; 
+  } symbols[] = {
+    {(void*)&dl_kpse_find_file, "kpse_find_file"},
+    {(void*)&dl_kpse_set_program_name, "kpse_set_program_name"},
+    {(void*)&dl_kpse_format_info, "kpse_format_info"},
+    {(void*)&dl_kpse_init_format, "kpse_init_format"},
+    {(void*)&dl_kpse_maketex_option, "kpse_maketex_option"},
+    {(void*)&dl_kpse_var_expand, "kpse_var_expand"},
+    {0,0}
+  };
+
+
+  dlerror ();
+  kpathsea_handle = dlopen (LIBKPATHSEA, RTLD_LAZY);
+  if (!kpathsea_handle)
+    {
+      /*
+	todo i18n.
+       */
+      fprintf (stderr, "can't dlopen: %s: %s", LIBKPATHSEA, dlerror ());
+      fprintf (stderr,"install package: %s or %s",
+	       "libkpathsea3 (teTeX 2.x)",
+	       "libkpathsea4 (teTeX 3.x)");
+
+      return 1;
+    }
+
+  for (int i = 0; symbols[i].func_pointer; i++)
+    {
+      dlerror ();
+      *symbols[i].func_pointer = dlsym (kpathsea_handle, symbols[i].name);
+      if (!symbols[i].func_pointer)
+	{
+	  fprintf(stderr, "no such symbol: %s: %s",
+		  symbols[i].name,
+		  dlerror ());
+	  return 1;
+	}
+    }
+
+  return 0;
+#endif
+}
 
 void
 initialize_kpathsea ()
 {
-  /*
-   initialize kpathsea
-   */
-  kpse_set_program_name ("lilypond", NULL);
-  kpse_maketex_option ("tfm", TRUE);
+  if (open_library ())
+    {
+      fprintf (stderr, "Error opening kpathsea library. Aborting");
+      exit (1);
+    }
 
+  (*dl_kpse_set_program_name) ("lilypond");
+  (*dl_kpse_maketex_option) ("tfm", TRUE);
+  
   SCM find = scm_c_define_gsubr ("ly:kpathsea-find-file", 1, 0, 0, ly_kpathsea_find_file);
   scm_c_export ("ly:kpathsea-find-file", NULL);
   SCM expand = scm_c_define_gsubr ("ly:kpathsea-expand-variable", 1, 0, 0, ly_kpathsea_find_file);
-- 
2.39.5