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 > 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 gpointer value1, value2;
167 char *initfunc, *deinitfunc;
170 gmodule = module_open(path, &found);
171 if (gmodule == NULL) {
172 if (!silent || found) {
173 module_error(MODULE_ERROR_LOAD, g_module_error(),
174 rootmodule, submodule);
176 return found ? 0 : -1;
179 /* get the module's init() and deinit() functions */
180 initfunc = module_get_func(rootmodule, submodule, "init");
181 deinitfunc = module_get_func(rootmodule, submodule, "deinit");
182 found = g_module_symbol(gmodule, initfunc, &value1) &&
183 g_module_symbol(gmodule, deinitfunc, &value2);
187 module_init = value1;
188 module_deinit = value2;
191 module_error(MODULE_ERROR_INVALID, NULL,
192 rootmodule, submodule);
193 g_module_close(gmodule);
197 /* Call the module's init() function - it should register itself
198 with module_register() function, abort if it doesn't. */
201 module = module_find(rootmodule);
202 rec = module == NULL ? NULL :
203 strcmp(rootmodule, submodule) == 0 ?
204 module_file_find(module, "core") :
205 module_file_find(module, submodule);
207 rec = module_register_full(rootmodule, submodule, NULL);
208 rec->gmodule = gmodule;
209 module_file_unload(rec);
211 module_error(MODULE_ERROR_INVALID, NULL,
212 rootmodule, submodule);
216 rec->module_deinit = module_deinit;
217 rec->gmodule = gmodule;
218 rec->initialized = TRUE;
220 settings_check_module(rec->defined_module_name);
222 signal_emit("module loaded", 2, rec->root, rec);
226 static int module_load_prefixes(const char *path, const char *module,
227 int start, int end, char **prefixes)
232 /* load module_core */
233 realpath = g_string_new(path);
234 g_string_insert(realpath, end, "_core");
236 /* Don't print the error message the first time, since the module
237 may not have the core part at all. */
238 status = module_load_name(realpath->str, module, "core", TRUE);
241 if (prefixes != NULL) {
242 /* load all the "prefix modules", like the fe-common, irc,
243 etc. part of the module */
244 while (*prefixes != NULL) {
245 g_string_assign(realpath, path);
246 g_string_insert_c(realpath, start, '_');
247 g_string_insert(realpath, start, *prefixes);
249 status = module_load_name(realpath->str, module,
259 /* error loading module, print the error message */
260 g_string_assign(realpath, path);
261 g_string_insert(realpath, end, "_core");
262 module_load_name(realpath->str, module, "core", FALSE);
265 g_string_free(realpath, TRUE);
269 static int module_load_full(const char *path, const char *rootmodule,
270 const char *submodule, int start, int end,
274 int status, try_prefixes;
276 if (!g_module_supported())
279 module = module_find(rootmodule);
280 if (module != NULL && (strcmp(submodule, rootmodule) == 0 ||
281 module_file_find(module, submodule) != NULL)) {
282 /* module is already loaded */
283 module_error(MODULE_ERROR_ALREADY_LOADED, NULL,
284 rootmodule, submodule);
288 /* check if the given module exists.. */
289 try_prefixes = strcmp(rootmodule, submodule) == 0;
290 status = module_load_name(path, rootmodule, submodule, try_prefixes);
291 if (status == -1 && try_prefixes) {
292 /* nope, try loading the module_core,
294 status = module_load_prefixes(path, rootmodule,
295 start, end, prefixes);
301 /* Load module - automatically tries to load also the related non-core
302 modules given in `prefixes' (like irc, fe, fe_text, ..) */
303 int module_load(const char *path, char **prefixes)
305 char *exppath, *name, *submodule, *rootmodule;
308 g_return_val_if_fail(path != NULL, FALSE);
310 exppath = convert_home(path);
312 name = module_get_name(exppath, &start, &end);
313 rootmodule = module_get_root(name, prefixes);
314 submodule = module_get_sub(name, rootmodule);
317 ret = module_load_full(exppath, rootmodule, submodule,
318 start, end, prefixes);
326 /* Load a sub module. */
327 int module_load_sub(const char *path, const char *submodule, char **prefixes)
330 char *exppath, *name, *rootmodule;
333 g_return_val_if_fail(path != NULL, FALSE);
334 g_return_val_if_fail(submodule != NULL, FALSE);
336 exppath = convert_home(path);
338 name = module_get_name(exppath, &start, &end);
339 rootmodule = module_get_root(name, prefixes);
342 full_path = g_string_new(exppath);
343 if (strcmp(submodule, "core") == 0)
344 g_string_insert(full_path, end, "_core");
346 g_string_insert_c(full_path, start, '_');
347 g_string_insert(full_path, start, submodule);
350 ret = module_load_full(full_path->str, rootmodule, submodule,
353 g_string_free(full_path, TRUE);
359 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
361 /* call the module's deinit() function */
362 if (file->module_deinit != NULL)
363 file->module_deinit();
365 if (file->defined_module_name != NULL) {
366 settings_remove_module(file->defined_module_name);
367 commands_remove_module(file->defined_module_name);
368 signals_remove_module(file->defined_module_name);
371 g_module_close(file->gmodule);
374 #else /* !HAVE_GMODULE - modules are not supported */
376 int module_load(const char *path, char **prefixes)
383 void module_file_unload(MODULE_FILE_REC *file)
388 root->files = g_slist_remove(root->files, file);
390 if (file->initialized)
391 signal_emit("module unloaded", 2, file->root, file);
394 if (file->gmodule != NULL)
395 module_file_deinit_gmodule(file);
399 g_free(file->defined_module_name);
402 if (root->files == NULL && g_slist_find(modules, root) != NULL)
406 void module_unload(MODULE_REC *module)
408 g_return_if_fail(module != NULL);
410 modules = g_slist_remove(modules, module);
412 signal_emit("module unloaded", 1, module);
414 while (module->files != NULL)
415 module_file_unload(module->files->data);
417 g_free(module->name);