Whamcloud - gitweb
debugfs: fix range mode of freei
[tools/e2fsprogs.git] / lib / ss / get_readline.c
index 55b8b47..8da85f1 100644 (file)
@@ -11,6 +11,7 @@
  * express or implied warranty.
  */
 
+#include "config.h"
 #ifdef HAS_STDLIB_H
 #include <stdlib.h>
 #endif
@@ -25,7 +26,7 @@ static void ss_release_readline(ss_data *info)
 #ifdef HAVE_DLOPEN
        if (!info->readline_handle)
                return;
-       
+
        info->readline = 0;
        info->add_history = 0;
        info->redisplay = 0;
@@ -35,17 +36,44 @@ static void ss_release_readline(ss_data *info)
 #endif
 }
 
+/* Libraries we will try to use for readline/editline functionality */
+#define DEFAULT_LIBPATH "libreadline.so.6:libreadline.so.5:libreadline.so.4:libreadline.so:libedit.so.2:libedit.so:libeditline.so.0:libeditline.so"
+
 void ss_get_readline(int sci_idx)
 {
 #ifdef HAVE_DLOPEN
-       void    *handle;
+       void    *handle = NULL;
        ss_data *info = ss_info(sci_idx);
-       const char **t;
+       const char **t, *libpath = 0;
+       char    *tmp, *cp, *next;
        char **(**completion_func)(const char *, int, int);
-       
-       if (info->readline_handle ||
-           getenv("SS_NO_READLINE") ||
-           ((handle = dlopen("libreadline.so", RTLD_NOW)) == NULL))
+
+       if (info->readline_handle)
+               return;
+
+       libpath = ss_safe_getenv("SS_READLINE_PATH");
+       if (!libpath)
+               libpath = DEFAULT_LIBPATH;
+       if (*libpath == 0 || !strcmp(libpath, "none"))
+               return;
+
+       tmp = malloc(strlen(libpath)+1);
+       if (!tmp)
+               return;
+       strcpy(tmp, libpath);
+       for (cp = tmp; cp; cp = next) {
+               next = strchr(cp, ':');
+               if (next)
+                       *next++ = 0;
+               if (*cp == 0)
+                       continue;
+               if ((handle = dlopen(cp, RTLD_NOW))) {
+                       /* printf("Using %s for readline library\n", cp); */
+                       break;
+               }
+       }
+       free(tmp);
+       if (!handle)
                return;
 
        info->readline_handle = handle;
@@ -67,4 +95,4 @@ void ss_get_readline(int sci_idx)
 #endif
 }
 
-       
+