New silcconfig library and server parser. Merged silc-newconfig-final.patch.
[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         while (*prefixes != NULL) {
68                 len = strlen(*prefixes);
69                 if (strncmp(name, *prefixes, len) == 0 && name[len] == '_') {
70                         name += len+1;
71                         break;
72                 }
73                 prefixes++;
74         }
75
76         /* skip the _core part */
77         len = strlen(name);
78         if (len > 5 && strcmp(name+len-5, "_core") == 0)
79                 return g_strndup(name, len-5);
80
81         return g_strdup(name);
82 }
83
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)
86 {
87         int rootlen, namelen;
88
89         namelen = strlen(name);
90         rootlen = strlen(root);
91         g_return_val_if_fail(namelen >= rootlen, g_strdup(name));
92
93         if (strncmp(name, root, rootlen) == 0 &&
94             strcmp(name+rootlen, "_core") == 0)
95                 return g_strdup("core");
96
97         if (namelen+1 > rootlen && name[namelen-rootlen-1] == '_' &&
98             strcmp(name+namelen-rootlen, root) == 0)
99                 return g_strndup(name, namelen-rootlen-1);
100
101         return g_strdup(name);
102 }
103
104 static GModule *module_open(const char *name, int *found)
105 {
106         struct stat statbuf;
107         GModule *module;
108         char *path, *str;
109
110         if (g_path_is_absolute(name) || *name == '~' ||
111             (*name == '.' && name[1] == G_DIR_SEPARATOR))
112                 path = g_strdup(name);
113         else {
114                 /* first try from home dir */
115                 str = g_strdup_printf("%s/modules", get_irssi_dir());
116                 path = g_module_build_path(str, name);
117                 g_free(str);
118
119                 if (stat(path, &statbuf) == 0) {
120                         module = g_module_open(path, (GModuleFlags) 0);
121                         g_free(path);
122                         *found = TRUE;
123                         return module;
124                 }
125
126                 /* module not found from home dir, try global module dir */
127                 g_free(path);
128                 path = g_module_build_path(MODULEDIR, name);
129         }
130
131         *found = stat(path, &statbuf) == 0;
132         module = g_module_open(path, (GModuleFlags) 0);
133         g_free(path);
134         return module;
135 }
136
137 static char *module_get_func(const char *rootmodule, const char *submodule,
138                              const char *function)
139 {
140         if (strcmp(submodule, "core") == 0)
141                 return g_strconcat(rootmodule, "_core_", function, NULL);
142
143         if (strcmp(rootmodule, submodule) == 0)
144                 return g_strconcat(rootmodule, "_", function, NULL);
145
146         return g_strconcat(submodule, "_", rootmodule, "_", function, NULL);
147 }
148
149 #define module_error(error, text, rootmodule, submodule) \
150         signal_emit("module error", 4, GINT_TO_POINTER(error), text, \
151                     rootmodule, submodule)
152
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)
157 {
158         void (*module_init) (void);
159         void (*module_deinit) (void);
160         GModule *gmodule;
161         MODULE_REC *module;
162         MODULE_FILE_REC *rec;
163         char *initfunc, *deinitfunc;
164         int found;
165
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);
171                 }
172                 return found ? 0 : -1;
173         }
174
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);
180         g_free(initfunc);
181         g_free(deinitfunc);
182
183         if (!found) {
184                 module_error(MODULE_ERROR_INVALID, NULL,
185                              rootmodule, submodule);
186                 g_module_close(gmodule);
187                 return 0;
188         }
189
190         /* Call the module's init() function - it should register itself
191            with module_register() function, abort if it doesn't. */
192         module_init();
193
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);
199         if (rec == NULL) {
200                 rec = module_register_full(rootmodule, submodule, NULL);
201                 rec->gmodule = gmodule;
202                 module_file_unload(rec);
203
204                 module_error(MODULE_ERROR_INVALID, NULL,
205                              rootmodule, submodule);
206                 return 0;
207         }
208
209         rec->module_deinit = module_deinit;
210         rec->gmodule = gmodule;
211         rec->initialized = TRUE;
212
213         settings_check_module(rec->defined_module_name);
214
215         signal_emit("module loaded", 2, rec->root, rec);
216         return 1;
217 }
218
219 static int module_load_prefixes(const char *path, const char *module,
220                                 int start, int end, char **prefixes)
221 {
222         GString *realpath;
223         int status, ok;
224
225         /* load module_core */
226         realpath = g_string_new(path);
227         g_string_insert(realpath, end, "_core");
228
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);
232         ok = status > 0;
233
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);
241
242                         status = module_load_name(realpath->str, module,
243                                                   *prefixes, TRUE);
244                         if (status > 0)
245                                 ok = TRUE;
246
247                         prefixes++;
248                 }
249         }
250
251         if (!ok) {
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);
256         }
257
258         g_string_free(realpath, TRUE);
259         return ok;
260 }
261
262 static int module_load_full(const char *path, const char *rootmodule,
263                             const char *submodule, int start, int end,
264                             char **prefixes)
265 {
266         MODULE_REC *module;
267         int status, try_prefixes;
268
269         if (!g_module_supported())
270                 return FALSE;
271
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);
278                 return FALSE;
279         }
280
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,
286                    fe_module, etc. */
287                 status = module_load_prefixes(path, rootmodule,
288                                               start, end, prefixes);
289         }
290
291         return status > 0;
292 }
293
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)
297 {
298         char *exppath, *name, *submodule, *rootmodule;
299         int start, end, ret;
300
301         g_return_val_if_fail(path != NULL, FALSE);
302
303         exppath = convert_home(path);
304
305         name = module_get_name(exppath, &start, &end);
306         rootmodule = module_get_root(name, prefixes);
307         submodule = module_get_sub(name, rootmodule);
308         g_free(name);
309
310         ret = module_load_full(exppath, rootmodule, submodule,
311                                start, end, prefixes);
312
313         g_free(rootmodule);
314         g_free(submodule);
315         g_free(exppath);
316         return ret;
317 }
318
319 /* Load a sub module. */
320 int module_load_sub(const char *path, const char *submodule, char **prefixes)
321 {
322         GString *full_path;
323         char *exppath, *name, *rootmodule;
324         int start, end, ret;
325
326         g_return_val_if_fail(path != NULL, FALSE);
327         g_return_val_if_fail(submodule != NULL, FALSE);
328
329         exppath = convert_home(path);
330
331         name = module_get_name(exppath, &start, &end);
332         rootmodule = module_get_root(name, prefixes);
333         g_free(name);
334
335         full_path = g_string_new(exppath);
336         if (strcmp(submodule, "core") == 0)
337                 g_string_insert(full_path, end, "_core");
338         else {
339                 g_string_insert_c(full_path, start, '_');
340                 g_string_insert(full_path, start, submodule);
341         }
342
343         ret = module_load_full(full_path->str, rootmodule, submodule,
344                                start, end, NULL);
345
346         g_string_free(full_path, TRUE);
347         g_free(rootmodule);
348         g_free(exppath);
349         return ret;
350 }
351
352 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
353 {
354         /* call the module's deinit() function */
355         if (file->module_deinit != NULL)
356                 file->module_deinit();
357
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);
362         }
363
364         g_module_close(file->gmodule);
365 }
366
367 void module_file_unload(MODULE_FILE_REC *file)
368 {
369         MODULE_REC *root;
370
371         root = file->root;
372         root->files = g_slist_remove(root->files, file);
373
374         if (file->initialized)
375                 signal_emit("module unloaded", 2, file->root, file);
376
377         if (file->gmodule != NULL)
378                 module_file_deinit_gmodule(file);
379
380         g_free(file->name);
381         g_free(file->defined_module_name);
382         g_free(file);
383
384         if (root->files == NULL && g_slist_find(modules, root) != NULL)
385                 module_unload(root);
386 }
387
388 void module_unload(MODULE_REC *module)
389 {
390         g_return_if_fail(module != NULL);
391
392         modules = g_slist_remove(modules, module);
393
394         signal_emit("module unloaded", 1, module);
395
396         while (module->files != NULL)
397                 module_file_unload(module->files->data);
398
399         g_free(module->name);
400         g_free(module);
401 }
402
403 #else /* !HAVE_GMODULE - modules are not supported */
404
405 int module_load(const char *path, char **prefixes)
406 {
407         return FALSE;
408 }
409
410 void module_unload(MODULE_REC *module)
411 {
412 }
413
414 #endif