1 |
/* |
2 |
* module.C: Module management. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
*/ |
7 |
#include <svsconfig.h> |
8 |
#include <common.h> |
9 |
#include <ermyth/module.h> |
10 |
|
11 |
modules::modules (impl::srcinf const &si, char const * const name, bool norestart, bool (*init)(module *), void (*fini)(), |
12 |
char const * const vendor, char const * const modversion) |
13 |
{ |
14 |
if (provides (name)) |
15 |
throw module_exception (si, "Trying to register two modules with the same name: %s", name); |
16 |
module *mod = new module (name, norestart, init, fini, vendor, modversion); |
17 |
modlist ().push_back (mod); |
18 |
provides (name); |
19 |
} |
20 |
|
21 |
bool |
22 |
modules::provides (char const * const name) |
23 |
{ |
24 |
if (find (name) != NULL) |
25 |
return true; |
26 |
return false; |
27 |
} |
28 |
|
29 |
fault::code |
30 |
modules::enable (char const * const name) |
31 |
{ |
32 |
module *m = find (name); |
33 |
|
34 |
if (!m) |
35 |
return fault::nosuch_target; |
36 |
|
37 |
if (m->enabled) |
38 |
return fault::nochange; |
39 |
|
40 |
m->enabled = m->init (m); |
41 |
|
42 |
return m->enabled ? fault::ok : fault::failed; |
43 |
} |
44 |
|
45 |
fault::code |
46 |
modules::disable (char const * const name) |
47 |
{ |
48 |
module *m = find (name); |
49 |
|
50 |
if (!m) |
51 |
return fault::nosuch_target; |
52 |
|
53 |
if (m->norestart) |
54 |
return fault::noprivs; |
55 |
|
56 |
if (m->enabled) |
57 |
m->fini (); |
58 |
else |
59 |
return fault::nochange; |
60 |
|
61 |
m->enabled = false; |
62 |
|
63 |
return fault::ok; |
64 |
} |
65 |
|
66 |
module * |
67 |
modules::find (char const * const name) |
68 |
{ |
69 |
foreach (module *m, modlist ()) |
70 |
if (!strcmp (m->name, name)) |
71 |
return m; |
72 |
|
73 |
return NULL; |
74 |
} |
75 |
|
76 |
void |
77 |
modules::cleanup () |
78 |
{ |
79 |
while (!modlist ().empty ()) |
80 |
{ |
81 |
module *m = modlist ().back (); |
82 |
|
83 |
if (m->enabled) |
84 |
m->fini (); |
85 |
delete m; |
86 |
|
87 |
modlist ().pop_back (); |
88 |
} |
89 |
} |