Whamcloud - gitweb
Add to the ss (subsystem) library the ability to dynamically link
[tools/e2fsprogs.git] / lib / ss / get_readline.c
1 /*
2  * Copyright 2003 by MIT Student Information Processing Board
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose is hereby granted, provided that
6  * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7  * advertising or publicity pertaining to distribution of the software
8  * without specific, written prior permission.  M.I.T. and the
9  * M.I.T. S.I.P.B. make no representations about the suitability of
10  * this software for any purpose.  It is provided "as is" without
11  * express or implied warranty.
12  */
13
14 #ifdef HAS_STDLIB_H
15 #include <stdlib.h>
16 #endif
17 #include "ss_internal.h"
18 #define size    sizeof(ss_data *)
19 #ifdef HAVE_DLOPEN
20 #include <dlfcn.h>
21 #endif
22
23 static void ss_release_readline(ss_data *info)
24 {
25 #ifdef HAVE_DLOPEN
26         if (!info->readline_handle)
27                 return;
28         
29         info->readline = 0;
30         info->add_history = 0;
31         info->redisplay = 0;
32         info->rl_completion_matches = 0;
33         dlclose(info->readline_handle);
34         info->readline_handle = 0;
35 #endif
36 }
37
38 void ss_get_readline(int sci_idx)
39 {
40 #ifdef HAVE_DLOPEN
41         void    *handle;
42         ss_data *info = ss_info(sci_idx);
43         const char **t;
44         char **(**completion_func)(const char *, int, int);
45         
46         if (info->readline_handle ||
47             getenv("SS_NO_READLINE") ||
48             ((handle = dlopen("libreadline.so", RTLD_NOW)) == NULL))
49                 return;
50
51         info->readline_handle = handle;
52         info->readline = dlsym(handle, "readline");
53         info->add_history = dlsym(handle, "add_history");
54         info->redisplay = dlsym(handle, "rl_forced_update_display");
55         info->rl_completion_matches = dlsym(handle, "rl_completion_matches");
56         if ((t = dlsym(handle, "rl_readline_name")) != NULL)
57                 *t = info->subsystem_name;
58         if ((completion_func =
59              dlsym(handle, "rl_attempted_completion_function")) != NULL)
60                 *completion_func = ss_rl_completion;
61         info->readline_shutdown = ss_release_readline;
62 #endif
63 }
64
65