import -ko -b 1.1.3 XEmacs XEmacs-21_2 r21-2-35
[chise/xemacs-chise.git.1] / src / sysdll.c
1 /* sysdll.c --- system dependent support for dynamic linked libraries
2    Copyright (C) 1998 Free Software Foundation, Inc.
3    Author:  William Perry <wmperry@aventail.com>
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include "sysdll.h"
28
29 /* This whole file is conditional upon HAVE_SHLIB */
30 #ifdef HAVE_SHLIB
31
32 /* Thankfully, most systems follow the ELFish dlopen() method.
33 ** HAVE__DLOPEN is lame, but SCO has their dl* functions as _dl*, and
34 ** unless you include dlfcn.h you don't get the macros to mask them, and
35 ** autoconf fails to find them. No longer true as of 5.0.5.
36 **
37 ** Anybody who wants to use this on SCO needs to have their configure.in
38 ** look for _dlopen() as well as dlopen()
39 */
40 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN) || defined(HAVE_DLFCN_H)
41 #include <dlfcn.h>
42
43 #ifndef RTLD_LAZY
44 # define RTLD_LAZY 1
45 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
46
47 #ifndef RTLD_GLOBAL
48 # define RTLD_GLOBAL 0
49 #endif
50
51 int
52 dll_init (const char *arg)
53 {
54   return 0;
55 }
56
57 dll_handle
58 dll_open (const char *fname)
59 {
60   return (dll_handle)dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
61 }
62
63 int
64 dll_close (dll_handle h)
65 {
66   return dlclose((void *)h);
67 }
68
69 dll_func
70 dll_function (dll_handle h, const char *n)
71 {
72 #ifdef DLSYM_NEEDS_UNDERSCORE
73   char *buf = alloca_array (char, strlen (n) + 2);
74   *buf = '_';
75   (void)strcpy(buf + 1, n);
76   n = buf;
77 #endif
78   return (dll_func)dlsym ((void *)h, n);
79 }
80
81 dll_var
82 dll_variable (dll_handle h, const char *n)
83 {
84 #ifdef DLSYM_NEEDS_UNDERSCORE
85   char *buf = alloca_array (char, strlen (n) + 2);
86   *buf = '_';
87   (void)strcpy(buf + 1, n);
88   n = buf;
89 #endif
90   return (dll_var)dlsym ((void *)h, n);
91 }
92
93 const char *
94 dll_error (dll_handle h)
95 {
96 #if defined(HAVE_DLERROR) || defined(dlerror)
97   return (const char *)dlerror ();
98 #elif defined(HAVE__DLERROR)
99   return (const char *)_dlerror();
100 #else
101   return "Shared library error";
102 #endif
103 }
104
105 #elif defined(HAVE_SHL_LOAD)
106 /* This is the HP/UX version */
107 #include <dl.h>
108 int
109 dll_init (const char *arg)
110 {
111   return 0;
112 }
113
114 dll_handle
115 dll_open (const char *fname)
116 {
117   shl_t h = shl_load (fname, BIND_DEFERRED,0L);
118   shl_t *hp = NULL;
119
120   if (h)
121     {
122       hp = (shl_t *)malloc (sizeof (shl_t));
123       if (!hp)
124         shl_unload(h);
125       else
126         *hp = h;
127     }
128   return (dll_handle)hp;
129 }
130
131 int
132 dll_close (dll_handle h)
133 {
134   shl_t hp = *((shl_t *)h);
135   free (hp);
136   return shl_unload(h);
137 }
138
139 dll_func
140 dll_function (dll_handle h, const char *n)
141 {
142   long handle = 0L;
143
144   if (shl_findsym ((shl_t *)h, n, TYPE_PROCEDURE, &handle))
145     return NULL;
146
147   return (dll_func)handle;
148 }
149
150 dll_var
151 dll_variable (dll_handle h, const char *n)
152 {
153   long handle = 0L;
154
155   if (shl_findsym ((shl_t *)h, n, TYPE_DATA, &handle))
156     return NULL;
157
158   return (dll_var)handle;
159 }
160
161 const char *
162 dll_error (dll_handle h)
163 {
164   /* #### WTF?!  Shouldn't this at least attempt to get strerror or
165      something?  --hniksic */
166   return "Generic shared library error";
167 }
168
169 #elif defined(HAVE_INIT_DLD)
170 #include <dld.h>
171 int
172 dll_init (const char *arg)
173 {
174   char *real_exe = dld_find_executable (arg);
175   int rc;
176
177   rc = dld_init (real_exe);
178   if (rc)
179     {
180       dld_perror (exe);
181       return -1;
182     }
183   return 0;
184 }
185
186 dll_handle
187 dll_open (const char *fname)
188 {
189   rc = dld_link (fname);
190   if (rc)
191     return NULL;
192
193   return (dll_handle)1;
194 }
195
196 int
197 dll_close (dll_handle h)
198 {
199   /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
200   ** later on to free the file - you have to remember the filename and
201   ** use that as the unlinker.  We should eventually keep a linked list
202   ** of loaded modules and then use the node pointer as the unique id
203   ** for the shared library.  Wheeee.  But not now.
204   */
205   return 1;
206 }
207
208 DLL_FUNC
209 dll_function (dll_handle h, const char *n)
210 {
211   return dld_get_func(n);
212 }
213
214 DLL_FUNC
215 dll_variable (dll_handle h, const char *n)
216 {
217   return dld_get_symbol(n);
218 }
219 #elif defined (WIN32_NATIVE)
220
221 #define WIN32_LEAN_AND_MEAN
222 #include <windows.h>
223 #undef WIN32_LEAN_AND_MEAN
224
225 int
226 dll_init (const char *arg)
227 {
228   return 0;
229 }
230
231 dll_handle
232 dll_open (const char *fname)
233 {
234   return (dll_handle)LoadLibrary (fname);
235 }
236
237 int
238 dll_close (dll_handle h)
239 {
240   return FreeLibrary (h);
241 }
242
243 dll_func
244 dll_function (dll_handle h, const char *n)
245 {
246   return (dll_func)GetProcAddress (h,n);
247 }
248
249 dll_func
250 dll_variable (dll_handle h, const char *n)
251 {
252   return (dll_func)GetProcAddress (h,n);
253 }
254
255 const char *
256 dll_error (dll_handle h)
257 {
258   return "Windows DLL Error";
259 }
260 #else
261 /* Catchall if we don't know about this systems method of dynamic loading */
262 int
263 dll_init (const char *arg)
264 {
265   return -1;
266 }
267
268 dll_handle
269 dll_open (const char *fname)
270 {
271   return NULL;
272 }
273
274 int
275 dll_close (dll_handle h)
276 {
277   return 0;
278 }
279
280 dll_func
281 dll_function (dll_handle h, const char *n)
282 {
283   return NULL;
284 }
285
286 dll_func
287 dll_variable (dll_handle h, const char *n)
288 {
289   return NULL;
290 }
291
292 const char *
293 dll_error (dll_handle h)
294 {
295   return "Shared libraries not implemented on this system";
296 }
297 #endif /* System conditionals */
298
299 #endif /* HAVE_SHLIB */