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 if (prefixes != NULL) {
68 while (*prefixes != NULL) {
69 len = strlen(*prefixes);
70 if (strncmp(name, *prefixes, len) == 0 &&
79 /* skip the _core part */
81 if (len > 5 && strcmp(name+len-5, "_core") == 0)
82 return g_strndup(name, len-5);
84 return g_strdup(name);
87 /* Returns the sub module name for given submodule (eg. perl_core -> core) */
88 static char *module_get_sub(const char *name, const char *root)
92 namelen = strlen(name);
93 rootlen = strlen(root);
94 g_return_val_if_fail(namelen >= rootlen, g_strdup(name));
96 if (strncmp(name, root, rootlen) == 0 &&
97 strcmp(name+rootlen, "_core") == 0)
98 return g_strdup("core");
100 if (namelen+1 > rootlen && name[namelen-rootlen-1] == '_' &&
101 strcmp(name+namelen-rootlen, root) == 0)
102 return g_strndup(name, namelen-rootlen-1);
104 return g_strdup(name);
107 static GModule *module_open(const char *name, int *found)
113 if (g_path_is_absolute(name) || *name == '~' ||
114 (*name == '.' && name[1] == G_DIR_SEPARATOR))
115 path = g_strdup(name);
117 /* first try from home dir */
118 str = g_strdup_printf("%s/modules", get_irssi_dir());
119 path = g_module_build_path(str, name);
122 if (stat(path, &statbuf) == 0) {
123 module = g_module_open(path, (GModuleFlags) 0);
129 /* module not found from home dir, try global module dir */
131 path = g_module_build_path(MODULEDIR, name);
134 *found = stat(path, &statbuf) == 0;
135 module = g_module_open(path, (GModuleFlags) 0);
140 static char *module_get_func(const char *rootmodule, const char *submodule,
141 const char *function)
143 if (strcmp(submodule, "core") == 0)
144 return g_strconcat(rootmodule, "_core_", function, NULL);
146 if (strcmp(rootmodule, submodule) == 0)
147 return g_strconcat(rootmodule, "_", function, NULL);
149 return g_strconcat(submodule, "_", rootmodule, "_", function, NULL);
152 #define module_error(error, text, rootmodule, submodule) \
153 signal_emit("module error", 4, GINT_TO_POINTER(error), text, \
154 rootmodule, submodule)
156 /* Returns 1 if ok, 0 if error in module and
157 -1 if module wasn't found */
158 static int module_load_name(const char *path, const char *rootmodule,
159 const char *submodule, int silent)
161 void (*module_init) (void);
162 void (*module_deinit) (void);
165 MODULE_FILE_REC *rec;
166 char *initfunc, *deinitfunc;
169 gmodule = module_open(path, &found);
170 if (gmodule == NULL) {
171 if (!silent || found) {
172 module_error(MODULE_ERROR_LOAD, g_module_error(),
173 rootmodule, submodule);
175 return found ? 0 : -1;
178 /* get the module's init() and deinit() functions */
179 initfunc = module_get_func(rootmodule, submodule, "init");
180 deinitfunc = module_get_func(rootmodule, submodule, "deinit");
181 found = g_module_symbol(gmodule, initfunc, (gpointer *) &module_init) &&
182 g_module_symbol(gmodule, deinitfunc, (gpointer *) &module_deinit);
187 module_error(MODULE_ERROR_INVALID, NULL,
188 rootmodule, submodule);
189 g_module_close(gmodule);
193 /* Call the module's init() function - it should register itself
194 with module_register() function, abort if it doesn't. */
197 module = module_find(rootmodule);
198 rec = module == NULL ? NULL :
199 strcmp(rootmodule, submodule) == 0 ?
200 module_file_find(module, "core") :
201 module_file_find(module, submodule);
203 rec = module_register_full(rootmodule, submodule, NULL);
204 rec->gmodule = gmodule;
205 module_file_unload(rec);
207 module_error(MODULE_ERROR_INVALID, NULL,
208 rootmodule, submodule);
212 rec->module_deinit = module_deinit;
213 rec->gmodule = gmodule;
214 rec->initialized = TRUE;
216 settings_check_module(rec->defined_module_name);
218 signal_emit("module loaded", 2, rec->root, rec);
222 static int module_load_prefixes(const char *path, const char *module,
223 int start, int end, char **prefixes)
228 /* load module_core */
229 realpath = g_string_new(path);
230 g_string_insert(realpath, end, "_core");
232 /* Don't print the error message the first time, since the module
233 may not have the core part at all. */
234 status = module_load_name(realpath->str, module, "core", TRUE);
237 if (prefixes != NULL) {
238 /* load all the "prefix modules", like the fe-common, irc,
239 etc. part of the module */
240 while (*prefixes != NULL) {
241 g_string_assign(realpath, path);
242 g_string_insert_c(realpath, start, '_');
243 g_string_insert(realpath, start, *prefixes);
245 status = module_load_name(realpath->str, module,
255 /* error loading module, print the error message */
256 g_string_assign(realpath, path);
257 g_string_insert(realpath, end, "_core");
258 module_load_name(realpath->str, module, "core", FALSE);
261 g_string_free(realpath, TRUE);
265 static int module_load_full(const char *path, const char *rootmodule,
266 const char *submodule, int start, int end,
270 int status, try_prefixes;
272 if (!g_module_supported())
275 module = module_find(rootmodule);
276 if (module != NULL && (strcmp(submodule, rootmodule) == 0 ||
277 module_file_find(module, submodule) != NULL)) {
278 /* module is already loaded */
279 module_error(MODULE_ERROR_ALREADY_LOADED, NULL,
280 rootmodule, submodule);
284 /* check if the given module exists.. */
285 try_prefixes = strcmp(rootmodule, submodule) == 0;
286 status = module_load_name(path, rootmodule, submodule, try_prefixes);
287 if (status == -1 && try_prefixes) {
288 /* nope, try loading the module_core,
290 status = module_load_prefixes(path, rootmodule,
291 start, end, prefixes);
297 /* Load module - automatically tries to load also the related non-core
298 modules given in `prefixes' (like irc, fe, fe_text, ..) */
299 int module_load(const char *path, char **prefixes)
301 char *exppath, *name, *submodule, *rootmodule;
304 g_return_val_if_fail(path != NULL, FALSE);
306 exppath = convert_home(path);
308 name = module_get_name(exppath, &start, &end);
309 rootmodule = module_get_root(name, prefixes);
310 submodule = module_get_sub(name, rootmodule);
313 ret = module_load_full(exppath, rootmodule, submodule,
314 start, end, prefixes);
322 /* Load a sub module. */
323 int module_load_sub(const char *path, const char *submodule, char **prefixes)
326 char *exppath, *name, *rootmodule;
329 g_return_val_if_fail(path != NULL, FALSE);
330 g_return_val_if_fail(submodule != NULL, FALSE);
332 exppath = convert_home(path);
334 name = module_get_name(exppath, &start, &end);
335 rootmodule = module_get_root(name, prefixes);
338 full_path = g_string_new(exppath);
339 if (strcmp(submodule, "core") == 0)
340 g_string_insert(full_path, end, "_core");
342 g_string_insert_c(full_path, start, '_');
343 g_string_insert(full_path, start, submodule);
346 ret = module_load_full(full_path->str, rootmodule, submodule,
349 g_string_free(full_path, TRUE);
355 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
357 /* call the module's deinit() function */
358 if (file->module_deinit != NULL)
359 file->module_deinit();
361 if (file->defined_module_name != NULL) {
362 settings_remove_module(file->defined_module_name);
363 commands_remove_module(file->defined_module_name);
364 signals_remove_module(file->defined_module_name);
367 g_module_close(file->gmodule);
370 #else /* !HAVE_GMODULE - modules are not supported */
372 int module_load(const char *path, char **prefixes)
379 void module_file_unload(MODULE_FILE_REC *file)
384 root->files = g_slist_remove(root->files, file);
386 if (file->initialized)
387 signal_emit("module unloaded", 2, file->root, file);
390 if (file->gmodule != NULL)
391 module_file_deinit_gmodule(file);
395 g_free(file->defined_module_name);
398 if (root->files == NULL && g_slist_find(modules, root) != NULL)
402 void module_unload(MODULE_REC *module)
404 g_return_if_fail(module != NULL);
406 modules = g_slist_remove(modules, module);
408 signal_emit("module unloaded", 1, module);
410 while (module->files != NULL)
411 module_file_unload(module->files->data);
413 g_free(module->name);