File: plugin.c

package info (click to toggle)
vile 9.8za-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,644 kB
  • sloc: ansic: 120,894; lex: 14,981; sh: 4,478; perl: 3,511; cpp: 3,180; makefile: 1,425; awk: 271
file content (58 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: plugin.c,v 1.2 2004/06/17 23:48:26 tom Exp $
 */
#include "estruct.h"
#include "edef.h"
#include "plugin.h"

#include <dlfcn.h>

#if defined(RTLD_NOW)
#define my_RTLD RTLD_NOW
#elif defined(RTLD_LAZY)
#define my_RTLD RTLD_LAZY
#else
#define my_RTLD 0
#endif

int
loadplugin(int f GCC_UNUSED, int n GCC_UNUSED)
{
    int ret = FALSE;
    int status;
    static char name[NSTRING - sizeof(PLUGIN_INIT_NAME)] = "";
    char leafname[NSTRING];
    char symname[NSTRING];
    const char *cp = libdir_path;
    void *found = 0;
    plugin_init init;

    /* obtain name from user/rcfile */
    status = mlreply("Plugin name: ", name, (sizeof name) - 1);
    if (status == ABORT)
	return FALSE;

    sprintf(leafname, PLUGIN_SO_FMT, name);
    sprintf(symname, PLUGIN_INIT_NAME, name);

    TRACE(("leafname %s\n", leafname));
    TRACE(("symname  %s\n", symname));

    if ((cp = cfg_locate(leafname, LOCATE_EXEC)) != 0) {
	TRACE(("try dlopen(%s)\n", cp));
	found = dlopen(cp, my_RTLD);
    }

    if (found == 0)
	mlwarn("Error: plugin '%s' not found", name);
    else if ((init = (plugin_init) dlsym(found, symname)) == 0)
	mlwarn("Error: plugin '%s' initialization function not found", name);
    else if (init() == 0)
	mlwarn("Error: failed to initialize plugin '%s'", name);
    else
	ret = TRUE;

    /* TODO: on success, remember in loaded plugin list */

    return ret;
}