dynload.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00010 #define _SCHEME_SOURCE
00011 #include "dynload.h"
00012 #include "gerb_file.h"
00013 #include <string.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016
00017 #ifndef MAXPATHLEN
00018 # define MAXPATHLEN 1024
00019 #endif
00020
00021 static void make_filename(const char *name, char *filename);
00022 static void make_init_fn(const char *name, char *init_fn);
00023
00024 #ifdef _WIN32
00025 # include <windows.h>
00026 #else
00027 typedef void *HMODULE;
00028 typedef void (*FARPROC)();
00029 #ifndef SUN_DL
00030 #define SUN_DL
00031 #endif
00032 #include <dlfcn.h>
00033 #endif
00034
00035 #ifdef _WIN32
00036
00037 #define PREFIX ""
00038 #define SUFFIX ".dll"
00039
00040 static void display_w32_error_msg(const char *additional_message)
00041 {
00042 LPVOID msg_buf;
00043
00044 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
00045 NULL, GetLastError(), 0,
00046 (LPTSTR)&msg_buf, 0, NULL);
00047 fprintf(stderr, "scheme load-extension: %s: %s", additional_message, (char *) msg_buf);
00048 LocalFree(msg_buf);
00049 }
00050
00051 static HMODULE dl_attach(const char *module) {
00052 HMODULE dll = LoadLibrary(module);
00053 if (!dll) display_w32_error_msg(module);
00054 return dll;
00055 }
00056
00057 static FARPROC dl_proc(HMODULE mo, const char *proc) {
00058 FARPROC procedure = GetProcAddress(mo,proc);
00059 if (!procedure) display_w32_error_msg(proc);
00060 return procedure;
00061 }
00062 #if 0
00063 static void dl_detach(HMODULE mo) {
00064 (void)FreeLibrary(mo);
00065 }
00066 #endif
00067 #elif defined(SUN_DL)
00068
00069 #include <dlfcn.h>
00070
00071 #define PREFIX "lib"
00072 #define SUFFIX ".so"
00073
00074 static HMODULE dl_attach(const char *module) {
00075 HMODULE so=dlopen(module,RTLD_LAZY);
00076 if(!so) {
00077 fprintf(stderr, "Error loading scheme extension \"%s\": %s\n", module, dlerror());
00078 }
00079 return so;
00080 }
00081
00082 static FARPROC dl_proc(HMODULE mo, const char *proc) {
00083 const char *errmsg;
00084 FARPROC fp=(FARPROC)dlsym(mo,proc);
00085 if ((errmsg = dlerror()) == 0) {
00086 return fp;
00087 }
00088 fprintf(stderr, "Error initializing scheme module \"%s\": %s\n", proc, errmsg);
00089 return 0;
00090 }
00091 #if 0
00092 static void dl_detach(HMODULE mo) {
00093 (void)dlclose(mo);
00094 }
00095 #endif
00096 #endif
00097
00098 pointer scm_load_ext(scheme *sc, pointer args)
00099 {
00100 pointer first_arg;
00101 pointer retval;
00102 char filename[MAXPATHLEN], init_fn[MAXPATHLEN+6];
00103 char *name;
00104 HMODULE dll_handle;
00105 void (*module_init)(scheme *sc);
00106
00107 if ((args != sc->NIL) && is_string((first_arg = pair_car(args)))) {
00108 name = string_value(first_arg);
00109 make_filename(name,filename);
00110 make_init_fn(name,init_fn);
00111 dll_handle = dl_attach(filename);
00112 if (dll_handle == 0) {
00113 retval = sc -> F;
00114 }
00115 else {
00116 module_init = (void(*)(scheme *))dl_proc(dll_handle, init_fn);
00117 if (module_init != 0) {
00118 (*module_init)(sc);
00119 retval = sc -> T;
00120 }
00121 else {
00122 retval = sc->F;
00123 }
00124 }
00125 }
00126 else {
00127 retval = sc -> F;
00128 }
00129
00130 return(retval);
00131 }
00132
00133 static void make_filename(const char *name, char *filename) {
00134 strcpy(filename,name);
00135 strcat(filename,SUFFIX);
00136 }
00137
00138 static void make_init_fn(const char *name, char *init_fn) {
00139 const char *p=strrchr(name,'/');
00140 if(p==0) {
00141 p=name;
00142 } else {
00143 p++;
00144 }
00145 strcpy(init_fn,"init_");
00146 strcat(init_fn,p);
00147 }
00148
00149
00150
00151
00152
00153