1 /* modules.c - Compiled-in module support
2 * Copyright 2002-2003 srvx Development Team
4 * This file is part of srvx.
6 * srvx 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 srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
34 int (*init_func)(void);
35 int (*finalize_func)(void);
37 enum init_state state;
40 #define WITH_MODULE(x) extern int x##_init(void); extern int x##_finalize(void); extern const char *x##_module_deps[];
41 #include "modules-list.h"
44 static struct cmodule cmodules[] = {
45 #define WITH_MODULE(x) { #x, x##_init, x##_finalize, x##_module_deps, UNINIT },
46 #include "modules-list.h"
48 /* Placeholder at end of array */
49 { NULL, NULL, NULL, NULL, UNINIT }
53 modules_bsearch(const void *a, const void *b) {
55 const struct cmodule *cmod = b;
56 return irccasecmp(key, cmod->name);
60 modules_qsort(const void *a, const void *b) {
61 const struct cmodule *ca = a, *cb = b;
62 return irccasecmp(ca->name, cb->name);
66 module_init(struct cmodule *cmod, int final) {
70 switch (cmod->state) {
72 case INITED: if (!final) return 1; break;
74 case BROKEN: return 0;
76 log_module(MAIN_LOG, LOG_ERROR, "Tried to recursively enable code module %s.", cmod->name);
79 cmod->state = WORKING;
80 for (ii=0; cmod->deps[ii]; ++ii) {
81 dep = bsearch(cmod->deps[ii], cmodules, ArrayLength(cmodules)-1, sizeof(cmodules[0]), modules_bsearch);
83 log_module(MAIN_LOG, LOG_ERROR, "Code module %s depends on unknown module %s.", cmod->name, cmod->deps[ii]);
87 if (!module_init(dep, final)) {
88 log_module(MAIN_LOG, LOG_ERROR, "Failed to initialize dependency %s of code module %s.", dep->name, cmod->name);
94 if (!cmod->finalize_func()) {
95 log_module(MAIN_LOG, LOG_ERROR, "Failed to finalize code module %s.", cmod->name);
102 if (!cmod->init_func()) {
103 log_module(MAIN_LOG, LOG_ERROR, "Failed to initialize code module %s.", cmod->name);
104 cmod->state = BROKEN;
107 cmod->state = INITED;
116 qsort(cmodules, ArrayLength(cmodules)-1, sizeof(cmodules[0]), modules_qsort);
117 for (ii=0; cmodules[ii].name; ++ii) {
118 if (cmodules[ii].state != UNINIT) continue;
119 module_init(cmodules + ii, 0);
120 if (cmodules[ii].state != INITED) {
121 log_module(MAIN_LOG, LOG_WARNING, "Code module %s not properly initialized.", cmodules[ii].name);
127 modules_finalize(void) {
130 for (ii=0; cmodules[ii].name; ++ii) {
131 if (cmodules[ii].state != INITED) continue;
132 module_init(cmodules + ii, 1);
133 if (cmodules[ii].state != DONE) {
134 log_module(MAIN_LOG, LOG_WARNING, "Code module %s not properly finalized.", cmodules[ii].name);