4 * Copyright (c) Tuomo Valkonen 1999-2001.
5 * See the included file LICENSE for details.
8 #ifndef CF_NO_MODULE_SUPPORT
26 static Module
*modules
=NULL
, *last_module
=NULL
;
27 static size_t longest_name
=0;
30 /*{{{ Loading and initialization code */
33 static bool call_init(void *handle
, char *name
)
35 char *p
=scat(name
, "_init");
43 initfn
=(bool (*)())dlsym(handle
, p
);
54 bool load_module(const char *fname
)
62 handle
=dlopen(fname
, RTLD_LAZY
);
65 warn_obj(fname
, "%s", dlerror());
69 /* Get the module name without directory or extension */
71 p
=strrchr(fname
, '/');
76 for(p
=fname
; *p
!='\0'; p
++){
77 if(!isalnum(*p
) && *p
!='_')
81 n
=ALLOC_N(char, p
-fname
);
88 memcpy(n
, fname
, p
-fname
);
91 if(!call_init(handle
, n
))
94 /* Allocate space for module info */
107 if(last_module
==NULL
){
108 modules
=last_module
=m
;
135 static void call_deinit(void *handle
, char *name
)
137 char *p
=scat(name
, "_deinit");
138 void (*deinitfn
)(void);
145 deinitfn
=(void (*)())dlsym(handle
, p
);
154 static void do_unload_module(Module
*m
)
156 call_deinit(m
->handle
, m
->name
);
165 void unload_modules()
169 for(m
=modules
; m
!=NULL
; m
=next
){
182 /*{{{ Hooks and iterative symbol lookup */
185 static char *symtmp
=NULL
;
186 static Module
*cmod
=NULL
;
189 static void *get_sym(const Module
*m
, const char *sym
)
191 sprintf(symtmp
, "%s_%s", m
->name
, sym
);
192 return dlsym(m
->handle
, symtmp
);
196 void *miter_begin(const char *sym
)
203 symtmp
=ALLOC_N(char, strlen(sym
)+longest_name
+1+1);
212 return miter_next(sym
);
216 void *miter_next(const char *sym
)
224 r
=get_sym(cmod
, sym
);
241 void call_hooks(const char *hook
)
245 for(fn
=(void (*)())miter_begin(hook
);
247 fn
=(void (*)())miter_next(hook
)){
256 void call_hooks_p(const char *hook
, void *p
)
260 for(fn
=(void (*)(void *))miter_begin(hook
);
262 fn
=(void (*)(void *))miter_next(hook
)){
274 #else /* CF_MODULE_SUPPORT */
280 /*{{{ Dummy functions for systems without sufficient dynamic
284 bool load_module(const char *name
)
286 warn_obj(name
, "Unabled to load: module support not enabled.");
291 void unload_modules()
297 void call_hooks(const char *hook
)
303 void call_hooks_p(const char *hook
, void *p
)
309 void *miter_begin(const char *sym
)
315 void *miter_next(const char *sym
)