summaryrefslogtreecommitdiffstats
path: root/lib/ss/get_readline.c
blob: aa1615747934e858dce0c3188ff902ec9bbb3225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2003 by MIT Student Information Processing Board
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose is hereby granted, provided that
 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  M.I.T. and the
 * M.I.T. S.I.P.B. make no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without
 * express or implied warranty.
 */

#include "config.h"
#ifdef HAS_STDLIB_H
#include <stdlib.h>
#endif
#include "ss_internal.h"
#define	size	sizeof(ss_data *)
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif

#ifdef HAVE_DLOPEN
static void ss_release_readline(ss_data *info)
{
	if (!info->readline_handle)
		return;

	info->readline = 0;
	info->add_history = 0;
	info->redisplay = 0;
	info->rl_completion_matches = 0;
	dlclose(info->readline_handle);
	info->readline_handle = 0;
}
#endif

/* Libraries we will try to use for readline/editline functionality */
#define DEFAULT_LIBPATH "libreadline.so.8:libreadline.so.7:libreadline.so.6:libreadline.so.5:libreadline.so.4:libreadline.so:libedit.so.2:libedit.so:libeditline.so.0:libeditline.so"

#ifdef HAVE_DLOPEN
void ss_get_readline(int sci_idx)
{
	void	*handle = NULL;
	ss_data *info = ss_info(sci_idx);
	const char **t, *libpath = 0;
	char	*tmp, *cp, *next;
	char **(**completion_func)(const char *, int, int);

	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;
	info->readline = (char *(*)(const char *))
		dlsym(handle, "readline");
	info->add_history = (void (*)(const char *))
		dlsym(handle, "add_history");
	info->redisplay = (void (*)(void))
		dlsym(handle, "rl_forced_update_display");
	info->rl_completion_matches = (char **(*)(const char *,
				    char *(*)(const char *, int)))
		dlsym(handle, "rl_completion_matches");
	if ((t = dlsym(handle, "rl_readline_name")) != NULL)
		*t = info->subsystem_name;
	if ((completion_func =
	     dlsym(handle, "rl_attempted_completion_function")) != NULL)
		*completion_func = ss_rl_completion;
	info->readline_shutdown = ss_release_readline;
}
#else
void ss_get_readline(int sci_idx __SS_ATTR((unused)))
{
}
#endif