Merged silc_1_1_branch to trunk.
[silc.git] / apps / irssi / src / core / modules-load.c
1 /*
2  modules-load.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "modules.h"
23 #include "modules-load.h"
24 #include "signals.h"
25
26 #include "settings.h"
27 #include "commands.h"
28 #include "misc.h"
29
30 #ifdef HAVE_GMODULE
31
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)
34 {
35         const char *name;
36         char *module_name, *ptr;
37
38         name = NULL;
39         if (*path == '~' || g_path_is_absolute(path)) {
40                 name = strrchr(path, G_DIR_SEPARATOR);
41                 if (name != NULL) name++;
42         }
43
44         if (name == NULL)
45                 name = path;
46
47         if (strncmp(name, "lib", 3) == 0)
48                 name += 3;
49
50         module_name = g_strdup(name);
51         ptr = strchr(module_name, '.');
52         if (ptr != NULL) *ptr = '\0';
53
54         *start = (int) (name-path);
55         *end = *start + (ptr == NULL ? strlen(name) :
56                          (int) (ptr-module_name));
57
58         return module_name;
59 }
60
61 /* Returns the root module name for given submodule (eg. perl_core -> perl) */
62 static char *module_get_root(const char *name, char **prefixes)
63 {
64         int len;
65
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 &&
71                             name[len] == '_') {
72                                 name += len+1;
73                                 break;
74                         }
75                         prefixes++;
76                 }
77         }
78
79         /* skip the _core part */
80         len = strlen(name);
81         if (len > 5 && strcmp(name+len-5, "_core") == 0)
82                 return g_strndup(name, len-5);
83
84         return g_strdup(name);
85 }
86
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)
89 {
90         int rootlen, namelen;
91
92         namelen = strlen(name);
93         rootlen = strlen(root);
94         g_return_val_if_fail(namelen >= rootlen, g_strdup(name));
95
96         if (strncmp(name, root, rootlen) == 0 &&
97             strcmp(name+rootlen, "_core") == 0)
98                 return g_strdup("core");
99
100         if (namelen > rootlen && name[namelen-rootlen-1] == '_' &&
101             strcmp(name+namelen-rootlen, root) == 0)
102                 return g_strndup(name, namelen-rootlen-1);
103
104         return g_strdup(name);
105 }
106
107 static GModule *module_open(const char *name, int *found)
108 {
109         struct stat statbuf;
110         GModule *module;
111         char *path, *str;
112
113         if (g_path_is_absolute(name) || *name == '~' ||
114             (*name == '.' && name[1] == G_DIR_SEPARATOR))
115                 path = g_strdup(name);
116         else {
117                 /* first try from home dir */
118                 str = g_strdup_printf("%s/modules", get_irssi_dir());
119                 path = g_module_build_path(str, name);
120                 g_free(str);
121
122                 if (stat(path, &statbuf) == 0) {
123                         module = g_module_open(path, (GModuleFlags) 0);
124                         g_free(path);
125                         *found = TRUE;
126                         return module;
127                 }
128
129                 /* module not found from home dir, try global module dir */
130                 g_free(path);
131                 path = g_module_build_path(MODULEDIR, name);
132         }
133
134         *found = stat(path, &statbuf) == 0;
135         module = g_module_open(path, (GModuleFlags) 0);
136         g_free(path);
137         return module;
138 }
139
140 static char *module_get_func(const char *rootmodule, const char *submodule,
141                              const char *function)
142 {
143         if (strcmp(submodule, "core") == 0)
144                 return g_strconcat(rootmodule, "_core_", function, NULL);
145
146         if (strcmp(rootmodule, submodule) == 0)
147                 return g_strconcat(rootmodule, "_", function, NULL);
148
149         return g_strconcat(submodule, "_", rootmodule, "_", function, NULL);
150 }
151
152 #define module_error(error, text, rootmodule, submodule) \
153         signal_emit("module error", 4, GINT_TO_POINTER(error), text, \
154                     rootmodule, submodule)
155
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)
160 {
161         void (*module_init) (void);
162         void (*module_deinit) (void);
163         GModule *gmodule;
164         MODULE_REC *module;
165         MODULE_FILE_REC *rec;
166         gpointer value1, value2;
167         char *initfunc, *deinitfunc;
168         int found;
169
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);
175                 }
176                 return found ? 0 : -1;
177         }
178
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);
184         g_free(initfunc);
185         g_free(deinitfunc);
186
187         module_init = value1;
188         module_deinit = value2;
189
190         if (!found) {
191                 module_error(MODULE_ERROR_INVALID, NULL,
192                              rootmodule, submodule);
193                 g_module_close(gmodule);
194                 return 0;
195         }
196
197         /* Call the module's init() function - it should register itself
198            with module_register() function, abort if it doesn't. */
199         module_init();
200
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);
206         if (rec == NULL) {
207                 rec = module_register_full(rootmodule, submodule, NULL);
208                 rec->gmodule = gmodule;
209                 module_file_unload(rec);
210
211                 module_error(MODULE_ERROR_INVALID, NULL,
212                              rootmodule, submodule);
213                 return 0;
214         }
215
216         rec->module_deinit = module_deinit;
217         rec->gmodule = gmodule;
218         rec->initialized = TRUE;
219
220         settings_check_module(rec->defined_module_name);
221
222         signal_emit("module loaded", 2, rec->root, rec);
223         return 1;
224 }
225
226 static int module_load_prefixes(const char *path, const char *module,
227                                 int start, int end, char **prefixes)
228 {
229         GString *realpath;
230         int status, ok;
231
232         /* load module_core */
233         realpath = g_string_new(path);
234         g_string_insert(realpath, end, "_core");
235
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);
239         ok = status > 0;
240
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);
248
249                         status = module_load_name(realpath->str, module,
250                                                   *prefixes, TRUE);
251                         if (status > 0)
252                                 ok = TRUE;
253
254                         prefixes++;
255                 }
256         }
257
258         if (!ok) {
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);
263         }
264
265         g_string_free(realpath, TRUE);
266         return ok;
267 }
268
269 static int module_load_full(const char *path, const char *rootmodule,
270                             const char *submodule, int start, int end,
271                             char **prefixes)
272 {
273         MODULE_REC *module;
274         int status, try_prefixes;
275
276         if (!g_module_supported())
277                 return FALSE;
278
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);
285                 return FALSE;
286         }
287
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,
293                    fe_module, etc. */
294                 status = module_load_prefixes(path, rootmodule,
295                                               start, end, prefixes);
296         }
297
298         return status > 0;
299 }
300
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)
304 {
305         char *exppath, *name, *submodule, *rootmodule;
306         int start, end, ret;
307
308         g_return_val_if_fail(path != NULL, FALSE);
309
310         exppath = convert_home(path);
311
312         name = module_get_name(exppath, &start, &end);
313         rootmodule = module_get_root(name, prefixes);
314         submodule = module_get_sub(name, rootmodule);
315         g_free(name);
316
317         ret = module_load_full(exppath, rootmodule, submodule,
318                                start, end, prefixes);
319
320         g_free(rootmodule);
321         g_free(submodule);
322         g_free(exppath);
323         return ret;
324 }
325
326 /* Load a sub module. */
327 int module_load_sub(const char *path, const char *submodule, char **prefixes)
328 {
329         GString *full_path;
330         char *exppath, *name, *rootmodule;
331         int start, end, ret;
332
333         g_return_val_if_fail(path != NULL, FALSE);
334         g_return_val_if_fail(submodule != NULL, FALSE);
335
336         exppath = convert_home(path);
337
338         name = module_get_name(exppath, &start, &end);
339         rootmodule = module_get_root(name, prefixes);
340         g_free(name);
341
342         full_path = g_string_new(exppath);
343         if (strcmp(submodule, "core") == 0)
344                 g_string_insert(full_path, end, "_core");
345         else {
346                 g_string_insert_c(full_path, start, '_');
347                 g_string_insert(full_path, start, submodule);
348         }
349
350         ret = module_load_full(full_path->str, rootmodule, submodule,
351                                start, end, NULL);
352
353         g_string_free(full_path, TRUE);
354         g_free(rootmodule);
355         g_free(exppath);
356         return ret;
357 }
358
359 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
360 {
361         /* call the module's deinit() function */
362         if (file->module_deinit != NULL)
363                 file->module_deinit();
364
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);
369         }
370
371         g_module_close(file->gmodule);
372 }
373
374 #else /* !HAVE_GMODULE - modules are not supported */
375
376 int module_load(const char *path, char **prefixes)
377 {
378         return FALSE;
379 }
380
381 #endif
382
383 void module_file_unload(MODULE_FILE_REC *file)
384 {
385         MODULE_REC *root;
386
387         root = file->root;
388         root->files = g_slist_remove(root->files, file);
389
390         if (file->initialized)
391                 signal_emit("module unloaded", 2, file->root, file);
392
393 #ifdef HAVE_GMODULE
394         if (file->gmodule != NULL)
395                 module_file_deinit_gmodule(file);
396 #endif
397
398         g_free(file->name);
399         g_free(file->defined_module_name);
400         g_free(file);
401
402         if (root->files == NULL && g_slist_find(modules, root) != NULL)
403                 module_unload(root);
404 }
405
406 void module_unload(MODULE_REC *module)
407 {
408         g_return_if_fail(module != NULL);
409
410         modules = g_slist_remove(modules, module);
411
412         signal_emit("module unloaded", 1, module);
413
414         while (module->files != NULL)
415                 module_file_unload(module->files->data);
416
417         g_free(module->name);
418         g_free(module);
419 }