4 Copyright (C) 1999-2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "modules-load.h"
32 /* Returns the module name without path, "lib" prefix or ".so" suffix */
33 static char *module_get_name(const char *path, int *start, int *end)
36 char *module_name, *ptr;
39 if (*path == '~' || g_path_is_absolute(path)) {
40 name = strrchr(path, G_DIR_SEPARATOR);
41 if (name != NULL) name++;
47 if (strncmp(name, "lib", 3) == 0)
50 module_name = g_strdup(name);
51 ptr = strchr(module_name, '.');
52 if (ptr != NULL) *ptr = '\0';
54 *start = (int) (name-path);
55 *end = *start + (ptr == NULL ? strlen(name) :
56 (int) (ptr-module_name));
61 /* Returns the root module name for given submodule (eg. perl_core -> perl) */
62 static char *module_get_root(const char *name, char **prefixes)
66 /* skip any of the prefixes.. */
67 while (*prefixes != NULL) {
68 len = strlen(*prefixes);
69 if (strncmp(name, *prefixes, len) == 0 && name[len] == '_') {
76 /* skip the _core part */
78 if (len > 5 && strcmp(name+len-5, "_core") == 0)
79 return g_strndup(name, len-5);
81 return g_strdup(name);
84 /* Returns the sub module name for given submodule (eg. perl_core -> core) */
85 static char *module_get_sub(const char *name, const char *root)
89 namelen = strlen(name);
90 rootlen = strlen(root);
91 g_return_val_if_fail(namelen >= rootlen, g_strdup(name));
93 if (strncmp(name, root, rootlen) == 0 &&
94 strcmp(name+rootlen, "_core") == 0)
95 return g_strdup("core");
97 if (namelen+1 > rootlen && name[namelen-rootlen-1] == '_' &&
98 strcmp(name+namelen-rootlen, root) == 0)
99 return g_strndup(name, namelen-rootlen-1);
101 return g_strdup(name);
104 static GModule *module_open(const char *name, int *found)
110 if (g_path_is_absolute(name) || *name == '~' ||
111 (*name == '.' && name[1] == G_DIR_SEPARATOR))
112 path = g_strdup(name);
114 /* first try from home dir */
115 str = g_strdup_printf("%s/modules", get_irssi_dir());
116 path = g_module_build_path(str, name);
119 if (stat(path, &statbuf) == 0) {
120 module = g_module_open(path, (GModuleFlags) 0);
126 /* module not found from home dir, try global module dir */
128 path = g_module_build_path(MODULEDIR, name);
131 *found = stat(path, &statbuf) == 0;
132 module = g_module_open(path, (GModuleFlags) 0);
137 static char *module_get_func(const char *rootmodule, const char *submodule,
138 const char *function)
140 if (strcmp(submodule, "core") == 0)
141 return g_strconcat(rootmodule, "_core_", function, NULL);
143 if (strcmp(rootmodule, submodule) == 0)
144 return g_strconcat(rootmodule, "_", function, NULL);
146 return g_strconcat(submodule, "_", rootmodule, "_", function, NULL);
149 #define module_error(error, text, rootmodule, submodule) \
150 signal_emit("module error", 4, GINT_TO_POINTER(error), text, \
151 rootmodule, submodule)
153 /* Returns 1 if ok, 0 if error in module and
154 -1 if module wasn't found */
155 static int module_load_name(const char *path, const char *rootmodule,
156 const char *submodule, int silent)
158 void (*module_init) (void);
159 void (*module_deinit) (void);
162 MODULE_FILE_REC *rec;
163 char *initfunc, *deinitfunc;
166 gmodule = module_open(path, &found);
167 if (gmodule == NULL) {
168 if (!silent || found) {
169 module_error(MODULE_ERROR_LOAD, g_module_error(),
170 rootmodule, submodule);
172 return found ? 0 : -1;
175 /* get the module's init() and deinit() functions */
176 initfunc = module_get_func(rootmodule, submodule, "init");
177 deinitfunc = module_get_func(rootmodule, submodule, "deinit");
178 found = g_module_symbol(gmodule, initfunc, (gpointer *) &module_init) &&
179 g_module_symbol(gmodule, deinitfunc, (gpointer *) &module_deinit);
184 module_error(MODULE_ERROR_INVALID, NULL,
185 rootmodule, submodule);
186 g_module_close(gmodule);
190 /* Call the module's init() function - it should register itself
191 with module_register() function, abort if it doesn't. */
194 module = module_find(rootmodule);
195 rec = module == NULL ? NULL :
196 strcmp(rootmodule, submodule) == 0 ?
197 module_file_find(module, "core") :
198 module_file_find(module, submodule);
200 rec = module_register_full(rootmodule, submodule, NULL);
201 rec->gmodule = gmodule;
202 module_file_unload(rec);
204 module_error(MODULE_ERROR_INVALID, NULL,
205 rootmodule, submodule);
209 rec->module_deinit = module_deinit;
210 rec->gmodule = gmodule;
211 rec->initialized = TRUE;
213 settings_check_module(rec->defined_module_name);
215 signal_emit("module loaded", 2, rec->root, rec);
219 static int module_load_prefixes(const char *path, const char *module,
220 int start, int end, char **prefixes)
225 /* load module_core */
226 realpath = g_string_new(path);
227 g_string_insert(realpath, end, "_core");
229 /* Don't print the error message the first time, since the module
230 may not have the core part at all. */
231 status = module_load_name(realpath->str, module, "core", TRUE);
234 if (prefixes != NULL) {
235 /* load all the "prefix modules", like the fe-common, irc,
236 etc. part of the module */
237 while (*prefixes != NULL) {
238 g_string_assign(realpath, path);
239 g_string_insert_c(realpath, start, '_');
240 g_string_insert(realpath, start, *prefixes);
242 status = module_load_name(realpath->str, module,
252 /* error loading module, print the error message */
253 g_string_assign(realpath, path);
254 g_string_insert(realpath, end, "_core");
255 module_load_name(realpath->str, module, "core", FALSE);
258 g_string_free(realpath, TRUE);
262 static int module_load_full(const char *path, const char *rootmodule,
263 const char *submodule, int start, int end,
267 int status, try_prefixes;
269 if (!g_module_supported())
272 module = module_find(rootmodule);
273 if (module != NULL && (strcmp(submodule, rootmodule) == 0 ||
274 module_file_find(module, submodule) != NULL)) {
275 /* module is already loaded */
276 module_error(MODULE_ERROR_ALREADY_LOADED, NULL,
277 rootmodule, submodule);
281 /* check if the given module exists.. */
282 try_prefixes = strcmp(rootmodule, submodule) == 0;
283 status = module_load_name(path, rootmodule, submodule, try_prefixes);
284 if (status == -1 && try_prefixes) {
285 /* nope, try loading the module_core,
287 status = module_load_prefixes(path, rootmodule,
288 start, end, prefixes);
294 /* Load module - automatically tries to load also the related non-core
295 modules given in `prefixes' (like irc, fe, fe_text, ..) */
296 int module_load(const char *path, char **prefixes)
298 char *exppath, *name, *submodule, *rootmodule;
301 g_return_val_if_fail(path != NULL, FALSE);
303 exppath = convert_home(path);
305 name = module_get_name(exppath, &start, &end);
306 rootmodule = module_get_root(name, prefixes);
307 submodule = module_get_sub(name, rootmodule);
310 ret = module_load_full(exppath, rootmodule, submodule,
311 start, end, prefixes);
319 /* Load a sub module. */
320 int module_load_sub(const char *path, const char *submodule, char **prefixes)
323 char *exppath, *name, *rootmodule;
326 g_return_val_if_fail(path != NULL, FALSE);
327 g_return_val_if_fail(submodule != NULL, FALSE);
329 exppath = convert_home(path);
331 name = module_get_name(exppath, &start, &end);
332 rootmodule = module_get_root(name, prefixes);
335 full_path = g_string_new(exppath);
336 if (strcmp(submodule, "core") == 0)
337 g_string_insert(full_path, end, "_core");
339 g_string_insert_c(full_path, start, '_');
340 g_string_insert(full_path, start, submodule);
343 ret = module_load_full(full_path->str, rootmodule, submodule,
346 g_string_free(full_path, TRUE);
352 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
354 /* call the module's deinit() function */
355 if (file->module_deinit != NULL)
356 file->module_deinit();
358 if (file->defined_module_name != NULL) {
359 settings_remove_module(file->defined_module_name);
360 commands_remove_module(file->defined_module_name);
361 signals_remove_module(file->defined_module_name);
364 g_module_close(file->gmodule);
367 void module_file_unload(MODULE_FILE_REC *file)
372 root->files = g_slist_remove(root->files, file);
374 if (file->initialized)
375 signal_emit("module unloaded", 2, file->root, file);
377 if (file->gmodule != NULL)
378 module_file_deinit_gmodule(file);
381 g_free(file->defined_module_name);
384 if (root->files == NULL && g_slist_find(modules, root) != NULL)
388 void module_unload(MODULE_REC *module)
390 g_return_if_fail(module != NULL);
392 modules = g_slist_remove(modules, module);
394 signal_emit("module unloaded", 1, module);
396 while (module->files != NULL)
397 module_file_unload(module->files->data);
399 g_free(module->name);
403 #else /* !HAVE_GMODULE - modules are not supported */
405 int module_load(const char *path, char **prefixes)
410 void module_unload(MODULE_REC *module)