comparison src/emodules.c @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 8de8e3f6228a
children 576fb035e263
comparison
equal deleted inserted replaced
441:72a7cfa4a488 442:abe6d1db359e
47 static int emodules_depth; 47 static int emodules_depth;
48 static dll_handle dlhandle; 48 static dll_handle dlhandle;
49 static emodules_list *modules; 49 static emodules_list *modules;
50 static int modnum; 50 static int modnum;
51 51
52 static int find_make_module (CONST char *mod, CONST char *name, CONST char *ver, int make_or_find); 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); 53 static Lisp_Object module_load_unwind (Lisp_Object);
54 static void attempt_module_delete (int mod); 54 static void attempt_module_delete (int mod);
55 55
56 DEFUN ("load-module", Fload_module, 1, 3, "FLoad dynamic module: ", /* 56 DEFUN ("load-module", Fload_module, 1, 3, "FLoad dynamic module: ", /*
57 Load in a C Emacs Extension module named FILE. 57 Load in a C Emacs Extension module named FILE.
71 FILE depends on will be automatically loaded. You can determine which 71 FILE depends on will be automatically loaded. You can determine which
72 modules have been loaded as dynamic shared objects by examining the 72 modules have been loaded as dynamic shared objects by examining the
73 return value of the function `list-modules'. 73 return value of the function `list-modules'.
74 74
75 It is possible, although unwise, to unload modules using `unload-module'. 75 It is possible, although unwise, to unload modules using `unload-module'.
76 The prefered mechanism for unloading or reloading modules is to quit 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. 77 XEmacs, and then reload those new or changed modules that are required.
78 78
79 Messages informing you of the progress of the load are displayed unless 79 Messages informing you of the progress of the load are displayed unless
80 the variable `load-modules-quietly' is non-NIL. 80 the variable `load-modules-quietly' is non-NIL.
81 */ 81 */
112 Unload a module previously loaded with load-module. 112 Unload a module previously loaded with load-module.
113 113
114 As with load-module, this function requires at least the module FILE, and 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 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 116 for the module to be unloaded from memory, as there may be Lisp objects
117 refering to variables inside the module code. However, once you have 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 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. 119 soon as the last reference to symbols within the module is destroyed.
120 */ 120 */
121 (file,name,version)) 121 (file,name,version))
122 { 122 {
182 182
183 return mlist; 183 return mlist;
184 } 184 }
185 185
186 static int 186 static int
187 find_make_module (CONST char *mod, CONST char *name, CONST char *ver, int mof) 187 find_make_module (const char *mod, const char *name, const char *ver, int mof)
188 { 188 {
189 int i, fs = -1; 189 int i, fs = -1;
190 190
191 for (i = 0; i < modnum; i++) 191 for (i = 0; i < modnum; i++)
192 { 192 {
207 207
208 if (fs != -1) 208 if (fs != -1)
209 return fs; /* First free slot */ 209 return fs; /* First free slot */
210 210
211 /* 211 /*
212 * We only get here if we havent found a free slot and the module was 212 * We only get here if we haven't found a free slot and the module was
213 * not previously loaded. 213 * not previously loaded.
214 */ 214 */
215 if (modules == (emodules_list *)0) 215 if (modules == (emodules_list *)0)
216 modules = (emodules_list *) xmalloc (sizeof (emodules_list)); 216 modules = (emodules_list *) xmalloc (sizeof (emodules_list));
217 modnum++; 217 modnum++;
276 276
277 /* 277 /*
278 * Do the actual grunt-work of loading in a module. We first try and 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 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 280 * out immediately. If the dlopen() succeeds, we need to check for the
281 * existance of certain special symbols. 281 * existence of certain special symbols.
282 * 282 *
283 * All modules will have complete access to the variables and functions 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 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 285 * variables or functions it uses, however. Modules will also have access
286 * to other functions and variables in other loaded modules, unless they 286 * to other functions and variables in other loaded modules, unless they
287 * are defined as STATIC. 287 * are defined as STATIC.
288 * 288 *
289 * We need to be very careful with how we load modules. If we encounter an 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 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 resursively, we need to 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 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, 293 * modules, we set the counter to zero. As we enter the function each time,
294 * we incremement the counter, and before we leave we decrement it. When 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 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. 296 * can mark all the modules in the list as loaded.
297 * 297 *
298 * When we signal an error, we need to be sure to unwind all modules 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 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 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 dependant modules. So in 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 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 303 * we call "error" and let the Fmodule_load unwind_protect() function handle
304 * the cleaning up. 304 * the cleaning up.
305 */ 305 */
306 void 306 void
307 emodules_load(CONST char *module, CONST char *modname, CONST char *modver) 307 emodules_load(const char *module, const char *modname, const char *modver)
308 { 308 {
309 Lisp_Object filename; 309 Lisp_Object filename;
310 Lisp_Object foundname; 310 Lisp_Object foundname;
311 int fd, x, mpx; 311 int fd, x, mpx;
312 char *soname, *tmod; 312 char *soname, *tmod;
313 CONST char **f; 313 const char **f;
314 CONST long *ellcc_rev; 314 const long *ellcc_rev;
315 char *mver, *mname, *mtitle, *symname; 315 char *mver, *mname, *mtitle, *symname;
316 void (*modload)(void) = 0; 316 void (*modload)(void) = 0;
317 void (*modsyms)(void) = 0; 317 void (*modsyms)(void) = 0;
318 void (*modvars)(void) = 0; 318 void (*modvars)(void) = 0;
319 void (*moddocs)(void) = 0; 319 void (*moddocs)(void) = 0;
324 foundname = Qnil; 324 foundname = Qnil;
325 325
326 emodules_depth++; 326 emodules_depth++;
327 dlhandle = 0; 327 dlhandle = 0;
328 328
329 if ((module == (CONST char *)0) || (module[0] == '\0')) 329 if ((module == (const char *)0) || (module[0] == '\0'))
330 error ("Empty module name"); 330 error ("Empty module name");
331 331
332 /* This is to get around the fact that build_string() is not declared 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. */ 333 as taking a const char * as an argument. I HATE compiler warnings. */
334 tmod = (char *)alloca (strlen (module) + 1); 334 tmod = (char *)alloca (strlen (module) + 1);
348 348
349 dlhandle = dll_open (soname); 349 dlhandle = dll_open (soname);
350 if (dlhandle == (dll_handle)0) 350 if (dlhandle == (dll_handle)0)
351 error ("Opening dynamic module: %s", dll_error (dlhandle)); 351 error ("Opening dynamic module: %s", dll_error (dlhandle));
352 352
353 ellcc_rev = (CONST long *)dll_variable (dlhandle, "emodule_compiler"); 353 ellcc_rev = (const long *)dll_variable (dlhandle, "emodule_compiler");
354 if ((ellcc_rev == (CONST long *)0) || (*ellcc_rev <= 0)) 354 if ((ellcc_rev == (const long *)0) || (*ellcc_rev <= 0))
355 error ("Missing symbol `emodule_compiler': Invalid dynamic module"); 355 error ("Missing symbol `emodule_compiler': Invalid dynamic module");
356 if (*ellcc_rev > EMODULES_REVISION) 356 if (*ellcc_rev > EMODULES_REVISION)
357 error ("Unsupported version `%ld(%ld)': Invalid dynamic module", 357 error ("Unsupported version `%ld(%ld)': Invalid dynamic module",
358 *ellcc_rev, EMODULES_REVISION); 358 *ellcc_rev, EMODULES_REVISION);
359 359
360 f = (CONST char **)dll_variable (dlhandle, "emodule_name"); 360 f = (const char **)dll_variable (dlhandle, "emodule_name");
361 if ((f == (CONST char **)0) || (*f == (CONST char *)0)) 361 if ((f == (const char **)0) || (*f == (const char *)0))
362 error ("Missing symbol `emodule_name': Invalid dynamic module"); 362 error ("Missing symbol `emodule_name': Invalid dynamic module");
363 363
364 mname = (char *)alloca (strlen (*f) + 1); 364 mname = (char *)alloca (strlen (*f) + 1);
365 strcpy (mname, *f); 365 strcpy (mname, *f);
366 if (mname[0] == '\0') 366 if (mname[0] == '\0')
367 error ("Empty value for `emodule_name': Invalid dynamic module"); 367 error ("Empty value for `emodule_name': Invalid dynamic module");
368 368
369 f = (CONST char **)dll_variable (dlhandle, "emodule_version"); 369 f = (const char **)dll_variable (dlhandle, "emodule_version");
370 if ((f == (CONST char **)0) || (*f == (CONST char *)0)) 370 if ((f == (const char **)0) || (*f == (const char *)0))
371 error ("Missing symbol `emodule_version': Invalid dynamic module"); 371 error ("Missing symbol `emodule_version': Invalid dynamic module");
372 372
373 mver = (char *)alloca (strlen (*f) + 1); 373 mver = (char *)alloca (strlen (*f) + 1);
374 strcpy (mver, *f); 374 strcpy (mver, *f);
375 375
376 f = (CONST char **)dll_variable (dlhandle, "emodule_title"); 376 f = (const char **)dll_variable (dlhandle, "emodule_title");
377 if ((f == (CONST char **)0) || (*f == (CONST char *)0)) 377 if ((f == (const char **)0) || (*f == (const char *)0))
378 error ("Missing symbol `emodule_title': Invalid dynamic module"); 378 error ("Missing symbol `emodule_title': Invalid dynamic module");
379 379
380 mtitle = (char *)alloca (strlen (*f) + 1); 380 mtitle = (char *)alloca (strlen (*f) + 1);
381 strcpy (mtitle, *f); 381 strcpy (mtitle, *f);
382 382
384 384
385 strcpy (symname, "modules_of_"); 385 strcpy (symname, "modules_of_");
386 strcat (symname, mname); 386 strcat (symname, mname);
387 modload = (void (*)(void))dll_function (dlhandle, symname); 387 modload = (void (*)(void))dll_function (dlhandle, symname);
388 /* 388 /*
389 * modload is optional. If the module doesnt require other modules it can 389 * modload is optional. If the module doesn't require other modules it can
390 * be left out. 390 * be left out.
391 */ 391 */
392 392
393 strcpy (symname, "syms_of_"); 393 strcpy (symname, "syms_of_");
394 strcat (symname, mname); 394 strcat (symname, mname);
416 416
417 /* 417 /*
418 * Attempt to make a new slot for this module. If this really is the 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. 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 420 * If that is non-zero, we know that we have a previously loaded module
421 * of the same name and version, and we dont need to go any further. 421 * of the same name and version, and we don't need to go any further.
422 */ 422 */
423 mpx = find_make_module (soname, mname, mver, 0); 423 mpx = find_make_module (soname, mname, mver, 0);
424 mp = &modules[mpx]; 424 mp = &modules[mpx];
425 if (mp->used > 0) 425 if (mp->used > 0)
426 { 426 {
476 modules[x].used = 1; 476 modules[x].used = 1;
477 } 477 }
478 } 478 }
479 479
480 void 480 void
481 emodules_doc_subr(CONST char *symname, CONST char *doc) 481 emodules_doc_subr(const char *symname, const char *doc)
482 { 482 {
483 Bytecount len = strlen (symname); 483 Bytecount len = strlen (symname);
484 Lisp_Object sym = oblookup (Vobarray, (CONST Bufbyte *)symname, len); 484 Lisp_Object sym = oblookup (Vobarray, (const Bufbyte *)symname, len);
485 Lisp_Subr *subr; 485 Lisp_Subr *subr;
486 486
487 if (SYMBOLP(sym)) 487 if (SYMBOLP(sym))
488 { 488 {
489 subr = XSUBR( XSYMBOL(sym)->function); 489 subr = XSUBR( XSYMBOL(sym)->function);
496 * look into this? 496 * look into this?
497 */ 497 */
498 } 498 }
499 499
500 void 500 void
501 emodules_doc_sym (CONST char *symname, CONST char *doc) 501 emodules_doc_sym (const char *symname, const char *doc)
502 { 502 {
503 Bytecount len = strlen (symname); 503 Bytecount len = strlen (symname);
504 Lisp_Object sym = oblookup (Vobarray, (CONST Bufbyte *)symname, len); 504 Lisp_Object sym = oblookup (Vobarray, (const Bufbyte *)symname, len);
505 Lisp_Object docstr; 505 Lisp_Object docstr;
506 struct gcpro gcpro1; 506 struct gcpro gcpro1;
507 507
508 if (SYMBOLP(sym)) 508 if (SYMBOLP(sym))
509 { 509 {
541 DEFVAR_LISP ("module-version", &Vmodule_version /* 541 DEFVAR_LISP ("module-version", &Vmodule_version /*
542 Emacs dynamic loading mechanism version, as a string. 542 Emacs dynamic loading mechanism version, as a string.
543 543
544 This string is in the form XX.YY.ppp, where XX is the major version 544 This string is in the form XX.YY.ppp, where XX is the major version
545 number, YY is the minor version number, and ppp is the patch level. 545 number, YY is the minor version number, and ppp is the patch level.
546 This variable can be used to distinquish between different versions of 546 This variable can be used to distinguish between different versions of
547 the dynamic loading technology used in Emacs, if required. It is not 547 the dynamic loading technology used in Emacs, if required. It is not
548 a given that this value will be the same as the Emacs version number. 548 a given that this value will be the same as the Emacs version number.
549 */ ); 549 */ );
550 Vmodule_version = build_string (EMODULES_VERSION); 550 Vmodule_version = build_string (EMODULES_VERSION);
551 551
562 DEFVAR_LISP ("module-load-path", &Vmodule_load_path /* 562 DEFVAR_LISP ("module-load-path", &Vmodule_load_path /*
563 *List of directories to search for dynamic modules to load. 563 *List of directories to search for dynamic modules to load.
564 Each element is a string (directory name) or nil (try default directory). 564 Each element is a string (directory name) or nil (try default directory).
565 565
566 Note that elements of this list *may not* begin with "~", so you must 566 Note that elements of this list *may not* begin with "~", so you must
567 call `expland-file-name' on them before adding them to this list. 567 call `expand-file-name' on them before adding them to this list.
568 568
569 Initialized based on EMACSMODULEPATH environment variable, if any, otherwise 569 Initialized based on EMACSMODULEPATH environment variable, if any, otherwise
570 to default specified the file `paths.h' when XEmacs was built. If there 570 to default specified the file `paths.h' when XEmacs was built. If there
571 were no paths specified in `paths.h', then XEmacs chooses a default 571 were no paths specified in `paths.h', then XEmacs chooses a default
572 value for this variable by looking around in the file-system near the 572 value for this variable by looking around in the file-system near the
573 directory in which the XEmacs executable resides. 573 directory in which the XEmacs executable resides.
574 574
575 Due to the nature of dynamic modules, the path names should almost always 575 Due to the nature of dynamic modules, the path names should almost always
576 refer to architecture-dependant directories. It is unwise to attempt to 576 refer to architecture-dependent directories. It is unwise to attempt to
577 store dynamic modules in a hetrogenous environment. Some environments 577 store dynamic modules in a heterogenous environment. Some environments
578 are similar enough to each other that XEmacs will be unable to determine 578 are similar enough to each other that XEmacs will be unable to determine
579 the correctness of a dynamic module, which can have unpredictable results 579 the correctness of a dynamic module, which can have unpredictable results
580 when a dynamic module is loaded. 580 when a dynamic module is loaded.
581 */); 581 */);
582 582