gerbv  2.6A
dynload.c
Go to the documentation of this file.
1 /* dynload.c Dynamic Loader for TinyScheme */
2 /* Original Copyright (c) 1999 Alexander Shendi */
3 /* Modifications for NT and dl_* interface, scm_load_ext: D. Souflis */
4 /* Refurbished by Stephen Gildea */
5 
10 #define _SCHEME_SOURCE
11 #include "dynload.h"
12 #include "gerb_file.h"
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include "common.h"
18 
19 #ifndef MAXPATHLEN
20 # define MAXPATHLEN 1024
21 #endif
22 
23 static void make_filename(const char *name, char *filename);
24 static void make_init_fn(const char *name, char *init_fn);
25 
26 #ifdef _WIN32
27 # include <windows.h>
28 #else
29 typedef void *HMODULE;
30 typedef void (*FARPROC)();
31 #ifndef SUN_DL
32 #define SUN_DL
33 #endif
34 #include <dlfcn.h>
35 #endif
36 
37 #ifdef _WIN32
38 
39 #define PREFIX ""
40 #define SUFFIX ".dll"
41 
42  static void display_w32_error_msg(const char *additional_message)
43  {
44  LPVOID msg_buf;
45 
46  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
47  NULL, GetLastError(), 0,
48  (LPTSTR)&msg_buf, 0, NULL);
49  fprintf(stderr, _("scheme load-extension: %s: %s"), additional_message, (char *) msg_buf);
50  LocalFree(msg_buf);
51  }
52 
53 static HMODULE dl_attach(const char *module) {
54  HMODULE dll = LoadLibrary(module);
55  if (!dll) display_w32_error_msg(module);
56  return dll;
57 }
58 
59 static FARPROC dl_proc(HMODULE mo, const char *proc) {
60  FARPROC procedure = GetProcAddress(mo,proc);
61  if (!procedure) display_w32_error_msg(proc);
62  return procedure;
63 }
64 #if 0
65 static void dl_detach(HMODULE mo) {
66  (void)FreeLibrary(mo);
67 }
68 #endif
69 #elif defined(SUN_DL)
70 
71 #include <dlfcn.h>
72 
73 #define PREFIX "lib"
74 #define SUFFIX ".so"
75 
76 static HMODULE dl_attach(const char *module) {
77  HMODULE so=dlopen(module,RTLD_LAZY);
78  if(!so) {
79  fprintf(stderr, _("Error loading scheme extension \"%s\": %s\n"), module, dlerror());
80  }
81  return so;
82 }
83 
84 static FARPROC dl_proc(HMODULE mo, const char *proc) {
85  const char *errmsg;
86  FARPROC fp=(FARPROC)dlsym(mo,proc);
87  if ((errmsg = dlerror()) == 0) {
88  return fp;
89  }
90  fprintf(stderr, _("Error initializing scheme module \"%s\": %s\n"), proc, errmsg);
91  return 0;
92 }
93 #if 0
94 static void dl_detach(HMODULE mo) {
95  (void)dlclose(mo);
96 }
97 #endif
98 #endif
99 
100 pointer scm_load_ext(scheme *sc, pointer args)
101 {
102  pointer first_arg;
103  pointer retval;
104  char filename[MAXPATHLEN], init_fn[MAXPATHLEN+6];
105  char *name;
106  HMODULE dll_handle;
107  void (*module_init)(scheme *sc);
108 
109  if ((args != sc->NIL) && is_string((first_arg = pair_car(args)))) {
110  name = string_value(first_arg);
111  make_filename(name,filename);
112  make_init_fn(name,init_fn);
113  dll_handle = dl_attach(filename);
114  if (dll_handle == 0) {
115  retval = sc -> F;
116  }
117  else {
118  module_init = (void(*)(scheme *))dl_proc(dll_handle, init_fn);
119  if (module_init != 0) {
120  (*module_init)(sc);
121  retval = sc -> T;
122  }
123  else {
124  retval = sc->F;
125  }
126  }
127  }
128  else {
129  retval = sc -> F;
130  }
131 
132  return(retval);
133 }
134 
135 static void make_filename(const char *name, char *filename) {
136  strcpy(filename,name);
137  strcat(filename,SUFFIX);
138 }
139 
140 static void make_init_fn(const char *name, char *init_fn) {
141  const char *p=strrchr(name,'/');/*CHECK ME MINGW PATH SEPARATOR*/
142  if(p==0) {
143  p=name;
144  } else {
145  p++;
146  }
147  strcpy(init_fn,"init_");
148  strcat(init_fn,p);
149 }
150 
151 
152 
153 
154 
155