Debian Codegraph @

wrappers.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1993-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * This file is 'a hell of a file'. It contain _anything_ that is neither
21 * POSIX nor ANSI. The meaning is that when these things are needed in
22 * the code, they are located in a wrapper in this file. Thus, if you need
23 * to hack the code, it's likely that you only need to hack this file.
24 *
25 * At least, that's the theory ...
26 */
27
28 #include "regina_c.h"
29
30 #if defined(WIN32)
31 # ifdef _MSC_VER
32 # if _MSC_VER >= 1100
33 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
34 # pragma warning(disable: 4115 4201 4214 4514)
35 # endif
36 # endif
37 # include <windows.h>
38 # ifdef _MSC_VER
39 # if _MSC_VER >= 1100
40 # pragma warning(default: 4115 4201 4214)
41 # endif
42 # endif
43 #endif
44
45 #if defined(OS2)
46 # define INCL_DOSMODULEMGR
47 # include <os2.h>
48 # define DONT_TYPEDEF_PFN
49 #endif
50
51 #ifdef _POSIX_SOURCE
52 # undef _POSIX_SOURCE
53 #endif
54
55 #include "rexx.h"
56
57 #ifdef DYNAMIC
58
59 /*
60 * Most Unix systems have dlopen(), so set this as the default and
61 * unset it for platforms that don't have it - except for HPUX
62 */
63 # if defined(__hpux) && !defined(HAVE_DLFCN_H)
64 # define DYNAMIC_HPSHLOAD
65 # endif
66
67 # if defined(HAVE_DLFCN_H) && !defined(DYNAMIC_HPSHLOAD)
68 # define DYNAMIC_DLOPEN
69 # endif
70
71 # if defined(DYNAMIC_STATIC)
72 typedef void * handle_type;
73 # define DYNLIBPRE ""
74 # define DYNLIBPST ""
75 # elif defined(DYNAMIC_DLOPEN)
76 # include <dlfcn.h>
77 typedef void *handle_type ;
78 # ifndef RTLD_LAZY
79 # define RTLD_LAZY 1
80 # endif
81 # if defined(__CYGWIN__)
82 # define DYNLIBPRE ""
83 # define DYNLIBPST ".dll"
84 # define DYNLIBLEN 4
85 # elif defined(__APPLE__) && defined(__MACH__)
86 # define DYNLIBPRE "lib"
87 # define DYNLIBPST ".dylib"
88 # define DYNLIBLEN 9
89 # elif defined(AIX)
90 # define DYNLIBPRE "lib"
91 # define DYNLIBPST ".a"
92 # define DYNLIBLEN 5
93 # else
94 # define DYNLIBPRE "lib"
95 # define DYNLIBPST ".so"
96 # define DYNLIBLEN 6
97 # endif
98 # elif defined(DYNAMIC_AIXLOAD)
99 # include <sys/types.h>
100 # include <sys/ldr.h>
101 typedef int (*handle_type) ;
102 # define DYNLIBPRE "lib"
103 # define DYNLIBPST ".a"
104 # define DYNLIBLEN 5
105
106 # elif defined(DYNAMIC_HPSHLOAD)
107 # include <dl.h>
108 typedef shl_t handle_type ;
109 # define DYNLIBPRE "lib"
110 # define DYNLIBPST ".sl"
111 # define DYNLIBLEN 6
112
113 # elif defined(__BEOS__) || defined(__HAIKU__)
114 # include <be/kernel/image.h>
115 # define DYNAMIC_BEOS 1
116 typedef image_id handle_type ;
117 /*
118 * BeOS/HAIKU add-ons are all in a directory specified in $ADDON_PATH
119 */
120 # define DYNLIBPRE "lib"
121 # define DYNLIBPST ".so"
122 # define DYNLIBLEN 6
123
124 # elif defined(DYNAMIC_SKYOS)
125 typedef sDllHandle * handle_type ;
126 # define DYNLIBPRE "/boot/programs/rexx/extensions/lib"
127 # define DYNLIBPST ".dll"
128 # define DYNLIBLEN 40
129
130 # elif defined(DYNAMIC_OS2)
131 typedef HMODULE handle_type ;
132 /* typedef PHMODULE handle_type ; */
133
134 # elif defined(DYNAMIC_WIN32)
135 typedef HINSTANCE handle_type ;
136
137 # elif defined(DYNAMIC_VMS)
138 # define DYNLIBPRE "LIB"
139 # define DYNLIBPST ".EXE"
140 # define DYNLIBLEN 7
141 #endif /* DYNAMIC */
142
143 #include <errno.h>
144 #include <string.h>
145
146 #ifdef DYNAMIC_HPSHLOAD_NOT_NEEDED
147 /*
148 * HP/UX doesn't find symbols unless they are 'true data'; entrypoints
149 * for functions are not sufficient! Thus, we need to force it to
150 * 'release' that these symbols are defined. There's no need to do
151 * anything more, once it sets some flag somewhere ... it'll find'em
152 * later. Weird!
153 *
154 * Datatype char only used to save space ... this is never used, so
155 * there is no reason to waste space.
156 *
157 * Removed by MH 0.08h. Causes problems on HPUX 10.20 linker, and may
158 * not be needed anymore.
159 */
160
161 char Food_For_Fuzzy_HP_Linkers[] = {
162 (char)exiterror, (char)getonechar,
163 (char)nullstringptr,
164 (char)atozpos, (char)checkparam, (char)find_library } ;
165 #endif
166
167 #if defined(DYNAMIC_AIXLOAD)
168 void *wrapper_dummy_for_aix( void )
169 {
170 }
171 #endif
172
173 void *wrapper_load( const tsd_t *TSD, const streng *module )
174 {
175 handle_type handle=(handle_type)NULL ;
176 #if defined(DYNAMIC_OS2)
177 CHAR LoadError[256];
178 APIRET rc=0L;
179 #endif
180 #if defined(DYNAMIC_WIN32)
181 char LoadError[256];
182 #endif
183 #if defined(DYNAMIC_HPSHLOAD) || defined(DYNAMIC_BEOS)
184 char buf[1024];
185 #endif
186 #ifdef DYNAMIC_SKYOS
187 sDllHandle tmp_handle;
188 #endif
189 #ifdef DYNAMIC_DLOPEN
190 char *addon_dir;
191 #endif
192 char *file_name, *module_name, *udpart, *orig_module;
193 char *addon_name, *ptmp;
194 int addon_len=0;
195
196 orig_module = str_ofTSD( module );
197 #ifdef DYNLIBLEN
198 module_name = (char *)MallocTSD( Str_len( module ) + strlen(DYNLIBPRE) +
199 strlen(DYNLIBPST) + 1 ) ;
200 strcpy(module_name, DYNLIBPRE );
201 udpart = module_name + strlen(DYNLIBPRE);
202 memcpy(udpart, module->value, Str_len(module) );
203 strcpy(udpart + Str_len(module), DYNLIBPST );
204 file_name = module_name;
205 # if defined(DYNAMIC_HPSHLOAD)
206 file_name = buf;
207 # endif
208 #else
209 file_name = module_name = str_ofTSD(module);
210 #endif
211
212 #if defined(DYNAMIC_STATIC)
213 handle = static_dlopen( file_name );
214 if (handle == NULL)
215 {
216 set_err_message(TSD, "static_dlopen() failed loading:", file_name );
217 handle = (handle_type)NULL;
218 }
219 #elif defined(DYNAMIC_DLOPEN)
220 /*
221 * If the REGINA_ADDON_DIR environment variable is set, use it to explicitly specify
222 * the location of Regina external function packages
223 */
224 addon_dir = getenv( "REGINA_ADDON_DIR" );
225 if ( addon_dir )
226 {
227 addon_len = strlen(addon_dir);
228 addon_name = (char *)MallocTSD( Str_len( module ) + strlen(DYNLIBPRE) + strlen(DYNLIBPST) + addon_len + 2 ) ;
229 strcpy(addon_name, addon_dir );
230 if ( addon_name[addon_len-1] != '/' )
231 {
232 strcat( addon_name, "/" );
233 addon_len++;
234 }
235 strcat(addon_name, DYNLIBPRE );
236 ptmp = addon_name + strlen(DYNLIBPRE) + addon_len;
237 memcpy(ptmp, module->value, Str_len(module) );
238 strcpy(ptmp + Str_len(module), DYNLIBPST );
239 handle = dlopen(addon_name, RTLD_LAZY );
240 FreeTSD( addon_name );
241 }
242 # if defined(HAVE_REGINA_ADDON_DIR)
243 /*
244 * Use the compiled in location of Regina addons to locate the external function package
245 */
246 if ( handle == NULL )
247 {
248 addon_len = strlen(HAVE_REGINA_ADDON_DIR);
249 addon_name = (char *)MallocTSD( Str_len( module ) + strlen(DYNLIBPRE) + strlen(DYNLIBPST) + addon_len + 2 ) ;
250 strcpy(addon_name, HAVE_REGINA_ADDON_DIR );
251 if ( addon_name[addon_len-1] != '/' )
252 {
253 strcat( addon_name, "/" );
254 addon_len++;
255 }
256 strcat(addon_name, DYNLIBPRE );
257 ptmp = addon_name + strlen(DYNLIBPRE) + addon_len;
258 memcpy(ptmp, module->value, Str_len(module) );
259 strcpy(ptmp + Str_len(module), DYNLIBPST );
260 handle = dlopen(addon_name, RTLD_LAZY );
261 FreeTSD( addon_name );
262 }
263 # endif
264 if ( handle == NULL )
265 {
266 /*
267 * Try and load the module name exactly as specified (before wrapping it
268 * in lib*.so)
269 */
270 handle = dlopen( orig_module, RTLD_LAZY ) ;
271 if (handle == NULL)
272 {
273 handle = dlopen( file_name, RTLD_LAZY ) ;
274 /* deal with incorrect case in call */
275 if (handle == NULL)
276 {
277 mem_lower( udpart, Str_len( module ) );
278 handle = dlopen(module_name, RTLD_LAZY);
279
280 if (handle == NULL)
281 {
282 mem_upper( udpart, Str_len( module ) );
283 handle = dlopen(module_name, RTLD_LAZY);
284 if ( handle == NULL )
285 {
286 /*
287 * Reset the original module portion of the filename to be
288 * searched again so that any error message returned uses the
289 * original module name
290 */
291 memcpy(udpart, module->value, Str_len(module) );
292 handle = dlopen(module_name, RTLD_LAZY);
293 }
294 }
295 }
296 }
297 }
298
299 /* or maybe it's just not there */
300 if (handle==NULL)
301 {
302 char *msg=NULL;
303 msg = dlerror();
304 if (msg)
305 set_err_message(TSD, "dlopen() failed: ", msg ) ;
306 else
307 set_err_message(TSD, "", "");
308 }
309 #elif defined(DYNAMIC_HPSHLOAD)
310 /*
311 * Try and load the module name exactly as specified (before wrapping it
312 * in lib*.sl)
313 */
314 find_shared_library( TSD, orig_module, "SHLIB_PATH", buf );
315 handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH, 0L ) ;
316 if (handle == NULL)
317 {
318 find_shared_library(TSD,module_name,"SHLIB_PATH",buf);
319 handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH, 0L ) ;
320 if (handle == NULL)
321 {
322 mem_lower( udpart, Str_len( module ) );
323 find_shared_library( TSD, module_name, "SHLIB_PATH", buf );
324 handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH ,0L ) ;
325
326 if (handle == NULL)
327 {
328 mem_upper( udpart, Str_len( module ) );
329 find_shared_library( TSD, module_name, "SHLIB_PATH", buf );
330 handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH ,0L ) ;
331 }
332 }
333 }
334
335 if (handle==NULL)
336 set_err_message(TSD, "shl_load() failed: ", strerror(errno)) ;
337 #elif defined(DYNAMIC_AIXLOAD)
338 /*
339 * Try and load the module name exactly as specified (before wrapping it
340 * in lib*.a)
341 */
342 handle = load( orig_module, 1, NULL ) ;
343 if ( handle == NULL )
344 {
345 handle = load( file_name, 1, NULL ) ;
346 if ( handle == NULL )
347 {
348 set_err_message(TSD, "load() failed: ", strerror( errno )) ;
349 }
350 else
351 {
352 int rc=loadbind( 0, (void *)wrapper_dummy_for_aix, (void *)handle ) ;
353 if (rc)
354 {
355 handle = NULL ;
356 set_err_message(TSD, "loadbind() failed: ", strerror( errno )) ;
357 }
358 }
359 #elif defined(DYNAMIC_OS2)
360 rc = DosLoadModule( LoadError, sizeof(LoadError),
361 file_name, &handle ) ;
362 if (rc)
363 {
364 set_err_message(TSD, "DosLoadModule() unable to load DLL: ", LoadError);
365 handle = (handle_type)NULL;
366 }
367 #elif defined(DYNAMIC_WIN32)
368 handle = LoadLibrary( file_name ) ;
369 if (handle==NULL)
370 {
371 char buf[150];
372 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
373 sprintf( buf, "Failed to load \"%s\" library: LoadLibrary() failed: ", file_name );
374 set_err_message(TSD, buf, LoadError);
375 }
376 #elif defined(DYNAMIC_BEOS)
377 handle = load_add_on( orig_module );
378 if (handle < B_NO_ERROR)
379 {
380 handle = load_add_on( file_name );
381 if (handle < B_NO_ERROR)
382 {
383 sprintf( buf, "load_add_on() failed loading \"%s\" with error:", file_name );
384 set_err_message(TSD, buf, strerror( handle ) );
385 handle = (handle_type)NULL;
386 }
387 }
388 #elif defined(DYNAMIC_SKYOS)
389 handle = (handle_type)MallocTSD( sizeof( sDllHandle ) );
390 /*
391 * Don't try and load the module without the trimmings.
392 * You get a binary popup window otherwise!
393 */
394 fprintf(stderr,"%s %d %x %s:\n",__FILE__,__LINE__,handle,file_name);
395 if ( DllLoad( file_name, &tmp_handle ) != 0 )
396 {
397 char buf[150];
398 sprintf( buf, "Failed to load \"%s\" library: DllLoad() failed: ", file_name );
399 set_err_message(TSD, buf, strerror( errno ) );
400 FreeTSD( handle );
401 handle = (handle_type)NULL;
402 }
403 fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
404 memcpy( handle, &tmp_handle, sizeof( sDllHandle ) );
405 fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
406 #endif
407
408 FreeTSD( module_name );
409 FreeTSD( orig_module );
410
411 return (void *)handle ;
412 }
413
414 void wrapper_unload( const tsd_t *TSD, void *libhandle )
415 {
416 #ifdef DYNAMIC_STATIC
417
418 libhandle = libhandle;
419
420 #elif defined(DYNAMIC_DLOPEN)
421
422 dlclose((handle_type) libhandle);
423
424 #elif defined(DYNAMIC_HPSHLOAD)
425
426 shl_unload((handle_type) libhandle);
427
428 #elif defined(DYNAMIC_AIXLOAD)
429
430 unload((handle_type) libhandle);
431
432 #elif defined(DYNAMIC_OS2)
433
434 DosFreeModule((handle_type) libhandle);
435
436 #elif defined(DYNAMIC_WIN32)
437
438 FreeLibrary((handle_type) libhandle);
439
440 #elif defined(DYNAMIC_BEOS)
441
442 unload_add_on((handle_type) libhandle);
443
444 #elif defined(DYNAMIC_SKYOS)
445
446 fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
447 FreeTSD( libhandle );
448
449 #else
450 (libhandle = libhandle);
451 #endif
452 }
453
454 PFN wrapper_get_addr( const tsd_t *TSD, const struct library *lptr, const streng *name )
455 {
456 PFN addr;
457 handle_type handle=(handle_type)lptr->handle;
458 char *funcname ;
459 #if defined(DYNAMIC_WIN32)
460 char LoadError[256];
461 char *entryname;
462 unsigned u;
463 char c;
464 #endif
465 #if defined(DYNAMIC_OS2)
466 char *entryname;
467 unsigned u;
468 char c;
469 ULONG ordinal;
470 APIRET rc=0L;
471 #endif
472 #if defined(DYNAMIC_BEOS)
473 status_t rc=0;
474 #endif
475 #if defined(DYNAMIC_STATIC)
476 int rc=0;
477 #endif
478 #if defined(MODULES_NEED_USCORE)
479 streng *us_func;
480 #endif
481
482 funcname = str_of( TSD, name ) ;
483
484 #if defined(DYNAMIC_STATIC)
485 rc = static_dlsym( handle, funcname,(void **)&addr );
486 if ( rc != 0 )
487 {
488 char buf[150];
489 sprintf(buf,"static_dlsym() failed with %d looking for %s", rc, funcname );
490 set_err_message(TSD, buf, "" ) ;
491 addr = NULL;
492 }
493 #elif defined(DYNAMIC_DLOPEN)
494 # if defined(MODULES_NEED_USCORE)
495 /*
496 * Some platforms need to have an underscore prepended to the function
497 * name to be found in a loadable module.
498 */
499 FreeTSD( funcname );
500 us_func = Str_makeTSD( Str_len( name ) + 1 );
501 memcpy( us_func->value, "_", 1 );
502 us_func->len = 1;
503 Str_catTSD( us_func, name );
504 funcname = str_of( TSD, us_func );
505 Free_stringTSD( us_func );
506 # endif
507 /*
508 * Note, the following assignment is not allowed by ANSI, but SVR4.2
509 * includes it as an example, so it is probably safe in this context
510 */
511 addr = (PFN)(dlsym( handle, funcname )) ;
512
513 /* deal with, eg 'SysLoadFuncs' when the function is 'sysloadfuncs' or 'SYSLOADFUNCS' */
514 if (addr == NULL)
515 {
516 mem_upper( funcname, strlen( funcname ) );
517 addr = (PFN)(dlsym( handle, funcname )) ;
518
519 if (addr == NULL)
520 {
521 mem_lower( funcname, strlen( funcname ) );
522 addr = (PFN)(dlsym( handle, funcname )) ;
523
524 if (addr==NULL)
525 set_err_message( TSD, "dlsym() failed: ", dlerror() );
526 }
527 }
528
529 #elif defined(DYNAMIC_HPSHLOAD)
530 {
531 long eaddr ;
532 int rc;
533
534 if (rc = shl_findsym( &handle, funcname, TYPE_PROCEDURE, &eaddr ))
535 {
536 mem_upper( funcname, strlen( funcname ) );
537 if (rc = shl_findsym( &handle, funcname, TYPE_PROCEDURE, &eaddr ))
538 {
539 mem_lower( funcname, strlen( funcname ) );
540 if (rc = shl_findsym( &handle, funcname, TYPE_PROCEDURE, &eaddr ))
541 {
542 addr = NULL ;
543 set_err_message( TSD, "shl_findsym() failed: ", strerror(errno) );
544 }
545 }
546 }
547
548 if (!rc)
549 addr = (PFN)eaddr ;
550 }
551
552 #elif defined(DYNAMIC_AIXLOAD)
553 addr = (PFN)handle ;
554
555 #elif defined(DYNAMIC_OS2)
556 if ( ( sscanf( funcname, "#%u%c", &u, &c ) == 1 ) && ( u != 0 ) )
557 {
558 ordinal = (ULONG) u;
559 entryname = NULL;
560 }
561 else
562 {
563 ordinal = 0L;
564 entryname = funcname;
565 }
566 rc = DosQueryProcAddr(handle,ordinal,entryname,&addr);
567 if (rc)
568 {
569 mem_upper( entryname, strlen( entryname ) );
570 rc = DosQueryProcAddr(handle,ordinal,entryname,&addr);
571 if (rc)
572 {
573 mem_lower( entryname, strlen( entryname ) );
574 rc = DosQueryProcAddr(handle,ordinal,entryname,&addr);
575 if (rc)
576 {
577 char buf[150];
578 sprintf(buf,"DosQueryProcAddr() failed with %lu looking for %.90s", (long) rc, funcname );
579 set_err_message(TSD, buf, "" ) ;
580 }
581 }
582 }
583
584 #elif defined(DYNAMIC_WIN32)
585 /* 13/12/1999 JH moved cast, (HMODULE), from second parm to first. Removed
586 * a compiler warning,
587 */
588 if ( ( sscanf( funcname, "#%u%c", &u, &c ) == 1 ) && ( u != 0 ) &&
589 ( u <= 0xFFFF ) )
590 entryname = (char *) u;
591 else
592 entryname = funcname;
593
594 addr = (PFN) GetProcAddress( (HMODULE) handle, entryname );
595
596 if ( ( addr == NULL ) && ( funcname == entryname ) )
597 {
598 strlwr(funcname);
599 addr = (PFN)GetProcAddress((HMODULE)handle,funcname);
600
601 if (addr == NULL)
602 {
603 strupr(funcname);
604 addr = (PFN)GetProcAddress((HMODULE)handle, funcname);
605 }
606 }
607 if (addr == NULL)
608 {
609 char buf[150];
610 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL );
611 sprintf( buf, "Failed to find \"%s\" in external library: GetProcAddress() failed: ", funcname );
612 set_err_message( TSD, buf, LoadError );
613 }
614
615 #elif defined(DYNAMIC_BEOS)
616 rc = get_image_symbol(handle,funcname,B_SYMBOL_TYPE_TEXT,(void **)&addr);
617 if (rc == B_BAD_IMAGE_ID)
618 {
619 char buf[150];
620 sprintf(buf,"get_image_symbol() failed with %d looking for %s", rc, funcname );
621 set_err_message( TSD, buf, "" );
622 addr = NULL;
623 }
624
625 #elif defined(DYNAMIC_SKYOS)
626 fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
627 addr = (PFN)GetDllFunction( handle, funcname );
628 if ( addr == NULL )
629 {
630 char buf[150];
631 sprintf(buf,"GetDllFunction() failed looking for %s", funcname );
632 set_err_message( TSD, buf, "" );
633 addr = NULL;
634 }
635 fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
636 #endif
637
638 FreeTSD( funcname );
639
640 if (addr)
641 return (PFN)addr ;
642 else
643 return NULL ;
644 }
645 # ifdef SKYOS
646 /*
647 * Required as entry point for DLL under SkyOS
648 */
649 int DllMain( void )
650 {
651 return 0;
652 }
653 # endif
654
655 #endif /* DYNAMIC */