]>
jfr.im git - irc/evilnet/x3.git/blob - src/modules.c
1 /* modules.c - Compiled-in module support
2 * Copyright 2002-2003 srvx Development Team
4 * This file is part of x3.
6 * x3 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 3 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.
37 int (*init_func
)(void);
38 int (*finalize_func
)(void);
40 enum init_state state
;
43 #define WITH_MODULE(x) extern int x##_init(void); extern int x##_finalize(void); extern const char *x##_module_deps[];
44 #include "modules-list.h"
47 static struct cmodule cmodules
[] = {
48 #define WITH_MODULE(x) { #x, x##_init, x##_finalize, x##_module_deps, UNINIT },
49 #include "modules-list.h"
51 /* Placeholder at end of array */
52 { NULL
, NULL
, NULL
, NULL
, UNINIT
}
56 modules_bsearch(const void *a
, const void *b
) {
58 const struct cmodule
*cmod
= b
;
59 return irccasecmp(key
, cmod
->name
);
63 modules_qsort(const void *a
, const void *b
) {
64 const struct cmodule
*ca
= a
, *cb
= b
;
65 return irccasecmp(ca
->name
, cb
->name
);
69 module_init(struct cmodule
*cmod
, int final
) {
73 switch (cmod
->state
) {
75 case INITED
: if (!final
) return 1; break;
77 case BROKEN
: return 0;
79 log_module(MAIN_LOG
, LOG_ERROR
, "Tried to recursively enable code module %s.", cmod
->name
);
82 cmod
->state
= WORKING
;
83 for (ii
=0; cmod
->deps
[ii
]; ++ii
) {
84 dep
= bsearch(cmod
->deps
[ii
], cmodules
, ArrayLength(cmodules
)-1, sizeof(cmodules
[0]), modules_bsearch
);
86 log_module(MAIN_LOG
, LOG_ERROR
, "Code module %s depends on unknown module %s.", cmod
->name
, cmod
->deps
[ii
]);
90 if (!module_init(dep
, final
)) {
91 log_module(MAIN_LOG
, LOG_ERROR
, "Failed to initialize dependency %s of code module %s.", dep
->name
, cmod
->name
);
97 if (!cmod
->finalize_func()) {
98 log_module(MAIN_LOG
, LOG_ERROR
, "Failed to finalize code module %s.", cmod
->name
);
105 if (!cmod
->init_func()) {
106 log_module(MAIN_LOG
, LOG_ERROR
, "Failed to initialize code module %s.", cmod
->name
);
107 cmod
->state
= BROKEN
;
110 cmod
->state
= INITED
;
119 qsort(cmodules
, ArrayLength(cmodules
)-1, sizeof(cmodules
[0]), modules_qsort
);
120 for (ii
=0; cmodules
[ii
].name
; ++ii
) {
121 if (cmodules
[ii
].state
!= UNINIT
) continue;
122 module_init(cmodules
+ ii
, 0);
123 if (cmodules
[ii
].state
!= INITED
) {
124 log_module(MAIN_LOG
, LOG_WARNING
, "Code module %s not properly initialized.", cmodules
[ii
].name
);
130 modules_finalize(void) {
133 for (ii
=0; cmodules
[ii
].name
; ++ii
) {
134 if (cmodules
[ii
].state
!= INITED
) continue;
135 module_init(cmodules
+ ii
, 1);
136 if (cmodules
[ii
].state
!= DONE
) {
137 log_module(MAIN_LOG
, LOG_WARNING
, "Code module %s not properly finalized.", cmodules
[ii
].name
);