summaryrefslogtreecommitdiffstats
path: root/src/plugin.c
blob: 55a1f516cfb3e930db33803eb92a822679b9c87f (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
  LibRCC - plugin's abstraction

  Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as published
  by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "internal.h"
#include "rcchome.h"
#include "plugin.h"

#ifdef RCC_PLUGINS
# include <dlfcn.h>
# ifndef RTLD_NOW
#  define RTLD_NOW 0
# endif
#endif /* RCC_PLUGINS */


rcc_library_handle rccLibraryOpen(char *filename)
{
#ifdef RCC_PLUGINS
    if (filename) return (rcc_library_handle)dlopen(filename, RTLD_NOW);
#endif /* RCC_PLUGINS */

    return NULL;
}

void rccLibraryClose(rcc_library_handle handle)
{
#ifdef RCC_PLUGINS
    if (handle) dlclose(handle);
#endif /* RCC_PLUGINS */
}

void* rccLibraryFind(rcc_library_handle handle, const char *symbol)
{
#ifdef RCC_PLUGINS
    if ((handle)||(symbol)) return dlsym(handle, symbol);
#endif /* RCC_PLUGINS */

    return NULL;
}


static rcc_plugin_handle_s rcc_engine_handles[RCC_MAX_PLUGINS];

int rccPluginInit() {
    unsigned int i;
    
    for (i=0;i<RCC_MAX_PLUGINS;i++)
	rcc_engine_handles[i].sn = NULL;
    
    return 0;
}

void rccPluginFree() {
    unsigned int i;
    
    for (i=0;i<RCC_MAX_PLUGINS;i++)
	if (rcc_engine_handles[i].sn) {
	    rccLibraryClose(rcc_engine_handles[i].handle);
	    free(rcc_engine_handles[i].sn);
	    rcc_engine_handles[i].sn = NULL;
	}
}


rcc_plugin_handle rccPluginGetHandleByName(rcc_plugin_type type, const char *name) {
    unsigned int i;

    rcc_plugin_handle handles;
    
    if (!name) return NULL;
    
    switch(type) {
	case RCC_PLUGIN_TYPE_ENGINE:
	    handles = rcc_engine_handles;
	break;
	default:
	    return NULL;
    }
    
    for (i=0;i<RCC_MAX_PLUGINS;i++)
	if ((handles[i].sn)&&(!strcasecmp(handles[i].sn, name))) return handles+i;
    
    return NULL;
}

rcc_plugin_handle rccPluginGetFreeHandle(rcc_plugin_type type) {
    unsigned int i;

    rcc_plugin_handle handles;
    
    switch(type) {
	case RCC_PLUGIN_TYPE_ENGINE:
	    handles = rcc_engine_handles;
	break;
	default:
	    return NULL;
    }
    
    for (i=0;i<RCC_MAX_PLUGINS;i++)
	if (!handles[i].sn) return handles+i;
    
    return NULL;
}

rcc_plugin_handle rccPluginLoad(rcc_plugin_type type, const char *name) {
#ifdef RCC_PLUGINS
    void *res;
    void *infofunc;
    char *pluginfn;

    rcc_plugin_handle plugin;
    
    if (!name) return NULL;
    
    plugin = rccPluginGetHandleByName(type, name);
    if (plugin) return plugin;
    
    plugin = rccPluginGetFreeHandle(type);
    if (!plugin) return plugin;
    
    switch (type) {
	case RCC_PLUGIN_TYPE_ENGINE:
	    pluginfn = (char*)malloc((48 + strlen(rcc_home_dir) + strlen(name))*sizeof(char));
	    if (!pluginfn) return NULL;
	
	    sprintf(pluginfn, "%s/.rcc/engines/%s_engine.so", rcc_home_dir, name);
	    res = rccLibraryOpen(pluginfn);
	    if (!res) {
		sprintf(pluginfn, LIBRCC_DATA_DIR "/engines/%s_engine.so", name);
		res = rccLibraryOpen(pluginfn);
	    }
	    free(pluginfn);
	    
	    if (res) {
		infofunc = rccLibraryFind(res, "rccGetInfo");
		if (infofunc) {
		    plugin->sn = strdup(name);
		    if (plugin->sn) {
			plugin->handle = (rcc_library_handle)res;
			plugin->info_function = infofunc;
			return plugin;
		    } else rccLibraryClose(res);
		} else rccLibraryClose(res);
	    }
	break;
	default:
	    return NULL;
    }
#endif /* RCC_PLUGINS */

    return NULL;
}


rcc_engine *rccPluginEngineGetInfo(const char *name, const char *language) {
    rcc_plugin_handle handle;
    rcc_plugin_engine_info_function infofunc;

    handle = rccPluginLoad(RCC_PLUGIN_TYPE_ENGINE, name);
    if (!handle) return NULL;
    
    infofunc = (rcc_plugin_engine_info_function)(handle->info_function);
    if (!infofunc) return NULL;
    
    return infofunc(language);
}