1 /* emodules.c - Support routines for dynamic module loading
2 (C) Copyright 1998, 1999 J. Kean Johnston. All rights reserved.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 /* CE-Emacs version number */
27 Lisp_Object Vmodule_version;
29 /* Do we do our work quietly? */
30 int load_modules_quietly;
33 Lisp_Object Vmodule_load_path;
35 typedef struct _emodules_list
37 int used; /* Is this slot used? */
38 char *soname; /* Name of the shared object loaded (full path) */
39 char *modname; /* The name of the module */
40 char *modver; /* The version that the module is at */
41 char *modtitle; /* How the module announces itself */
42 dll_handle dlhandle; /* Dynamic lib handle */
45 static Lisp_Object Vmodule_extensions;
47 static int emodules_depth;
48 static dll_handle dlhandle;
49 static emodules_list *modules;
52 static int find_make_module (const char *mod, const char *name, const char *ver, int make_or_find);
53 static Lisp_Object module_load_unwind (Lisp_Object);
54 static void attempt_module_delete (int mod);
56 DEFUN ("load-module", Fload_module, 1, 3, "FLoad dynamic module: ", /*
57 Load in a C Emacs Extension module named FILE.
58 The optional NAME and VERSION are used to identify specific modules.
60 This function is similar in intent to `load' except that it loads in
61 pre-compiled C or C++ code, using dynamic shared objects. If NAME is
62 specified, then the module is only loaded if its internal name matches
63 the NAME specified. If VERSION is specified, then the module is only
64 loaded if it matches that VERSION. This function will check to make
65 sure that the same module is not loaded twice. Modules are searched
66 for in the same way as Lisp files, except that the valid file
67 extensions are `.so', `.dll' or `.ell'.
69 All symbols in the shared module must be completely resolved in order
70 for this function to be successful. Any modules which the specified
71 FILE depends on will be automatically loaded. You can determine which
72 modules have been loaded as dynamic shared objects by examining the
73 return value of the function `list-modules'.
75 It is possible, although unwise, to unload modules using `unload-module'.
76 The preferred mechanism for unloading or reloading modules is to quit
77 XEmacs, and then reload those new or changed modules that are required.
79 Messages informing you of the progress of the load are displayed unless
80 the variable `load-modules-quietly' is non-NIL.
82 (file, name, version))
84 char *mod, *mname, *mver;
85 int speccount = specpdl_depth();
89 mod = (char *)XSTRING_DATA (file);
94 mname = (char *)XSTRING_DATA (name);
99 mver = (char *)XSTRING_DATA (version);
102 record_unwind_protect (module_load_unwind, make_int(modnum));
103 emodules_load (mod, mname, mver);
104 unbind_to (speccount, Qnil);
109 #ifdef DANGEROUS_NASTY_SCARY_MONSTER
111 DEFUN ("unload-module", Fmodule_unload, 1, 3, 0, /*
112 Unload a module previously loaded with load-module.
114 As with load-module, this function requires at least the module FILE, and
115 optionally the module NAME and VERSION to unload. It may not be possible
116 for the module to be unloaded from memory, as there may be Lisp objects
117 referring to variables inside the module code. However, once you have
118 requested a module to be unloaded, it will be unloaded from memory as
119 soon as the last reference to symbols within the module is destroyed.
121 (file, name, version))
124 char *mod, *mname, *mver;
128 mod = (char *)XSTRING_DATA (file);
133 mname = (char *)XSTRING_DATA (name);
138 mver = (char *)XSTRING_DATA (version);
140 x = find_make_module (mod, mname, mver, 1);
142 attempt_module_delete (x);
145 #endif /* DANGEROUS_NASTY_SCARY_MONSTER */
147 DEFUN ("list-modules", Flist_modules, 0, 0, "", /*
148 Produce a list of loaded dynamic modules.
150 This function will return a list of all the loaded dynamic modules.
151 Each element in the list is a list in the form (SONAME NAME VER DESC),
152 where SONAME is the name of the shared object that was loaded, NAME
153 is the internal module name, VER is the version of the module, and DESC
154 is how the module describes itself.
156 This function returns a list, so you will need to assign the return value
157 to a variable and then examine the variable with `describe-variable'.
160 (setq mylist (list-modules))
161 (describe-variable 'mylist)
164 NOTE: It is possible for the same module to be loaded more than once,
165 at different versions. However, you should never see the same module,
166 with the same name and version, loaded more than once. If you do, this
167 is a bug, and you are encouraged to report it.
171 Lisp_Object mlist = Qnil;
174 for (i = 0; i < modnum; i++)
176 if (modules[i].used == 1)
177 mlist = Fcons (list4 (build_string (modules[i].soname),
178 build_string (modules[i].modname),
179 build_string (modules[i].modver),
180 build_string (modules[i].modtitle)), mlist);
187 find_make_module (const char *mod, const char *name, const char *ver, int mof)
191 for (i = 0; i < modnum; i++)
193 if (fs == -1 && modules[i].used == 0)
195 if (strcmp (modules[i].soname, mod) == 0)
197 if (name && name[0] && strcmp (modules[i].modname, name))
199 if (ver && ver[0] && strcmp (modules[i].modver, ver))
201 return i; /* Found a match */
209 return fs; /* First free slot */
212 * We only get here if we haven't found a free slot and the module was
213 * not previously loaded.
215 if (modules == (emodules_list *)0)
216 modules = (emodules_list *) xmalloc (sizeof (emodules_list));
218 modules = (emodules_list *) xrealloc (modules, modnum * sizeof (emodules_list));
221 memset (&modules[fs], 0, sizeof(emodules_list));
226 attempt_module_delete (int mod)
228 if (dll_close (modules[mod].dlhandle) == 0)
230 xfree (modules[mod].soname);
231 xfree (modules[mod].modname);
232 xfree (modules[mod].modver);
233 xfree (modules[mod].modtitle);
234 modules[mod].dlhandle = 0;
235 modules[mod].used = 0;
237 else if (modules[mod].used > 1)
238 modules[mod].used = 1; /* We couldn't delete it - it stays */
242 module_load_unwind (Lisp_Object upto)
247 * First close off the current handle if it is open.
250 dll_close (dlhandle);
255 if (INTP (XCAR (upto)))
256 l = XINT (XCAR (upto));
257 free_cons (XCONS (upto));
263 * Here we need to go through and dlclose() (IN REVERSE ORDER!) any
264 * modules that were loaded as part of this load chain. We only mark
265 * the slots as closed if the dlclose() succeeds.
267 for (x = modnum-1; x >= l; x--)
269 if (modules[x].used > 1)
270 attempt_module_delete (x);
278 * Do the actual grunt-work of loading in a module. We first try and
279 * dlopen() the module. If that fails, we have an error and we bail
280 * out immediately. If the dlopen() succeeds, we need to check for the
281 * existence of certain special symbols.
283 * All modules will have complete access to the variables and functions
284 * defined within XEmacs itself. It is up to the module to declare any
285 * variables or functions it uses, however. Modules will also have access
286 * to other functions and variables in other loaded modules, unless they
287 * are defined as STATIC.
289 * We need to be very careful with how we load modules. If we encounter an
290 * error along the way, we need to back out completely to the point at
291 * which the user started. Since we can be called recursively, we need to
292 * take care with marking modules as loaded. When we first start loading
293 * modules, we set the counter to zero. As we enter the function each time,
294 * we increment the counter, and before we leave we decrement it. When
295 * we get back down to 0, we know we are at the end of the chain and we
296 * can mark all the modules in the list as loaded.
298 * When we signal an error, we need to be sure to unwind all modules loaded
299 * thus far (but only for this module chain). It is assumed that if any
300 * modules in a chain fail, then they all do. This is logical, considering
301 * that the only time we recurse is when we have dependent modules. So in
302 * the error handler we take great care to close off the module chain before
303 * we call "error" and let the Fmodule_load unwind_protect() function handle
307 emodules_load(const char *module, const char *modname, const char *modver)
309 Lisp_Object filename;
310 Lisp_Object foundname;
314 const long *ellcc_rev;
315 char *mver, *mname, *mtitle, *symname;
316 void (*modload)(void) = 0;
317 void (*modsyms)(void) = 0;
318 void (*modvars)(void) = 0;
319 void (*moddocs)(void) = 0;
321 struct gcpro gcpro1,gcpro2;
329 if ((module == (const char *)0) || (module[0] == '\0'))
330 error ("Empty module name");
332 /* This is to get around the fact that build_string() is not declared
333 as taking a const char * as an argument. I HATE compiler warnings. */
334 tmod = (char *)alloca (strlen (module) + 1);
335 strcpy (tmod, module);
337 GCPRO2(filename, foundname);
338 filename = build_string (tmod);
339 fd = locate_file(Vmodule_load_path, filename, Vmodule_extensions,
344 signal_simple_error ("Cannot open dynamic module", filename);
346 soname = (char *)alloca (XSTRING_LENGTH (foundname) + 1);
347 strcpy (soname, (char *)XSTRING_DATA (foundname));
349 dlhandle = dll_open (soname);
350 if (dlhandle == (dll_handle)0)
351 error ("Opening dynamic module: %s", dll_error (dlhandle));
353 ellcc_rev = (const long *)dll_variable (dlhandle, "emodule_compiler");
354 if ((ellcc_rev == (const long *)0) || (*ellcc_rev <= 0))
355 error ("Missing symbol `emodule_compiler': Invalid dynamic module");
356 if (*ellcc_rev > EMODULES_REVISION)
357 error ("Unsupported version `%ld(%ld)': Invalid dynamic module",
358 *ellcc_rev, EMODULES_REVISION);
360 f = (const char **)dll_variable (dlhandle, "emodule_name");
361 if ((f == (const char **)0) || (*f == (const char *)0))
362 error ("Missing symbol `emodule_name': Invalid dynamic module");
364 mname = (char *)alloca (strlen (*f) + 1);
366 if (mname[0] == '\0')
367 error ("Empty value for `emodule_name': Invalid dynamic module");
369 f = (const char **)dll_variable (dlhandle, "emodule_version");
370 if ((f == (const char **)0) || (*f == (const char *)0))
371 error ("Missing symbol `emodule_version': Invalid dynamic module");
373 mver = (char *)alloca (strlen (*f) + 1);
376 f = (const char **)dll_variable (dlhandle, "emodule_title");
377 if ((f == (const char **)0) || (*f == (const char *)0))
378 error ("Missing symbol `emodule_title': Invalid dynamic module");
380 mtitle = (char *)alloca (strlen (*f) + 1);
383 symname = (char *)alloca (strlen (mname) + 15);
385 strcpy (symname, "modules_of_");
386 strcat (symname, mname);
387 modload = (void (*)(void))dll_function (dlhandle, symname);
389 * modload is optional. If the module doesn't require other modules it can
393 strcpy (symname, "syms_of_");
394 strcat (symname, mname);
395 modsyms = (void (*)(void))dll_function (dlhandle, symname);
396 if (modsyms == (void (*)(void))0)
397 error ("Missing symbol `%s': Invalid dynamic module", symname);
399 strcpy (symname, "vars_of_");
400 strcat (symname, mname);
401 modvars = (void (*)(void))dll_function (dlhandle, symname);
402 if (modvars == (void (*)(void))0)
403 error ("Missing symbol `%s': Invalid dynamic module", symname);
405 strcpy (symname, "docs_of_");
406 strcat (symname, mname);
407 moddocs = (void (*)(void))dll_function (dlhandle, symname);
408 if (moddocs == (void (*)(void))0)
409 error ("Missing symbol `%s': Invalid dynamic module", symname);
411 if (modname && modname[0] && strcmp (modname, mname))
412 error ("Module name mismatch");
414 if (modver && modver[0] && strcmp (modver, mver))
415 error ("Module version mismatch");
418 * Attempt to make a new slot for this module. If this really is the
419 * first time we are loading this module, the used member will be 0.
420 * If that is non-zero, we know that we have a previously loaded module
421 * of the same name and version, and we don't need to go any further.
423 mpx = find_make_module (soname, mname, mver, 0);
428 dll_close (dlhandle);
429 dlhandle = 0; /* Zero this out before module_load_unwind runs */
433 if (!load_modules_quietly)
434 message ("Loading %s v%s (%s)", mname, mver, mtitle);
437 * We have passed the basic initialization, and can now add this
438 * module to the list of modules.
440 mp->used = emodules_depth + 1;
441 mp->soname = xstrdup (soname);
442 mp->modname = xstrdup (mname);
443 mp->modver = xstrdup (mver);
444 mp->modtitle = xstrdup (mtitle);
445 mp->dlhandle = dlhandle;
449 * Now we need to call the module init function and perform the various
456 * Now we can get the module to initialize its symbols, and then its
457 * variables, and lastly the documentation strings.
463 if (!load_modules_quietly)
464 message ("Loaded module %s v%s (%s)", mname, mver, mtitle);
468 if (emodules_depth == 0)
471 * We have reached the end of the load chain. We now go through the
472 * list of loaded modules and mark all the valid modules as just
475 for (x = 0; x < modnum; x++)
476 if (modules[x].used > 1)
482 emodules_doc_subr(const char *symname, const char *doc)
484 Bytecount len = strlen (symname);
485 Lisp_Object sym = oblookup (Vobarray, (const Bufbyte *)symname, len);
490 subr = XSUBR( XSYMBOL(sym)->function);
491 subr->doc = xstrdup (doc);
494 * FIXME: I wish there was some way to avoid the xstrdup(). Is it
495 * possible to just set a pointer to the string, or somehow create a
496 * symbol whose value we can point to the constant string? Can someone
502 emodules_doc_sym (const char *symname, const char *doc)
504 Bytecount len = strlen (symname);
505 Lisp_Object sym = oblookup (Vobarray, (const Bufbyte *)symname, len);
511 docstr = build_string (doc);
513 Fput (sym, Qvariable_documentation, docstr);
520 syms_of_module (void)
522 DEFSUBR(Fload_module);
523 DEFSUBR(Flist_modules);
524 #ifdef DANGEROUS_NASTY_SCARY_MONSTER
525 DEFSUBR(Funload_module);
530 reinit_vars_of_module (void)
533 modules = (emodules_list *)0;
538 vars_of_module (void)
540 reinit_vars_of_module ();
542 DEFVAR_LISP ("module-version", &Vmodule_version /*
543 Emacs dynamic loading mechanism version, as a string.
545 This string is in the form XX.YY.ppp, where XX is the major version
546 number, YY is the minor version number, and ppp is the patch level.
547 This variable can be used to distinguish between different versions of
548 the dynamic loading technology used in Emacs, if required. It is not
549 a given that this value will be the same as the Emacs version number.
551 Vmodule_version = build_string (EMODULES_VERSION);
553 DEFVAR_BOOL ("load-modules-quietly", &load_modules_quietly /*
554 *Set to t if module loading is to be silent.
556 Normally, when loading dynamic modules, Emacs will inform you of its
557 progress, and will display the module name and version if the module
558 is loaded correctly. Setting this variable to `t' will suppress these
559 messages. This would normally only be done if `load-module' was being
560 called by a Lisp function.
563 DEFVAR_LISP ("module-load-path", &Vmodule_load_path /*
564 *List of directories to search for dynamic modules to load.
565 Each element is a string (directory name) or nil (try default directory).
567 Note that elements of this list *may not* begin with "~", so you must
568 call `expand-file-name' on them before adding them to this list.
570 Initialized based on EMACSMODULEPATH environment variable, if any, otherwise
571 to default specified the file `paths.h' when XEmacs was built. If there
572 were no paths specified in `paths.h', then XEmacs chooses a default
573 value for this variable by looking around in the file-system near the
574 directory in which the XEmacs executable resides.
576 Due to the nature of dynamic modules, the path names should almost always
577 refer to architecture-dependent directories. It is unwise to attempt to
578 store dynamic modules in a heterogenous environment. Some environments
579 are similar enough to each other that XEmacs will be unable to determine
580 the correctness of a dynamic module, which can have unpredictable results
581 when a dynamic module is loaded.
584 /* #### Export this to Lisp */
585 Vmodule_extensions = build_string (":.ell:.so:.dll");
586 staticpro (&Vmodule_extensions);
588 load_modules_quietly = 0;
589 Vmodule_load_path = Qnil;
590 Fprovide (intern ("modules"));
593 #endif /* HAVE_SHLIB */