[go: up one dir, main page]

File: wrappers.c

package info (click to toggle)
regina 0.08h-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,724 kB
  • ctags: 4,101
  • sloc: ansic: 33,311; lex: 1,881; sh: 1,565; yacc: 1,129; makefile: 624
file content (323 lines) | stat: -rw-r--r-- 8,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 *  The Regina Rexx Interpreter
 *  Copyright (C) 1993-1994  Anders Christensen <anders@pvv.unit.no>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * $Id: wrappers.c,v 1.3 1999/12/24 04:42:49 mark Exp $
 */

/*
 * This file is 'a hell of a file'. It contain _anything_ that is neither
 * POSIX nor ANSI. The meaning is that when these things are needed in
 * the code, they are located in a wrapper in this file. Thus, if you need
 * to hack the code, it's likely that you only need to hack this file.
 *
 * At least, that's the theory ...
 */

#if defined(WIN32)
# ifdef _MSC_VER
#  if _MSC_VER >= 1100
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
#   pragma warning(disable: 4115 4201 4214)
#  endif
# endif
# include <windows.h>
# ifdef _MSC_VER
#  if _MSC_VER >= 1100
#   pragma warning(default: 4115 4201 4214)
#  endif
# endif
#endif

#if defined(OS2)
# define INCL_DOSMODULEMGR
# include <os2.h>
# define DONT_TYPEDEF_PFN
#endif

#ifdef _POSIX_SOURCE
# undef _POSIX_SOURCE
#endif

#include "rexx.h"


#ifdef DYNAMIC

/*
 * Most Unix systems have dlopen(), so set this as the default and
 * unset it for platforms that don't have it
 */
# if defined(HAVE_DLFCN_H)
#  define DYNAMIC_DLOPEN
# endif

# if defined(__hpux)
#  define DYNAMIC_HPSHLOAD
# endif

# if defined(DYNAMIC_DLOPEN)
#  include <dlfcn.h>
   typedef void *handle_type ;
#  ifndef RTLD_LAZY
#   define RTLD_LAZY 1
#  endif
#  define DYNLIBPRE "lib"
#  if defined(AIX)
#    define DYNLIBPST ".a"
#    define DYNLIBLEN 5
#  else
#    define DYNLIBPST ".so"
#    define DYNLIBLEN 6
#  endif
# elif defined(DYNAMIC_AIXLOAD)
#  include <sys/types.h>
#  include <sys/ldr.h>
   typedef int (*handle_type) ;
#  define DYNLIBPRE "lib"
#  define DYNLIBPST ".a"
#  define DYNLIBLEN 5

# elif defined(DYNAMIC_HPSHLOAD)
#  include <dl.h>
   typedef shl_t handle_type ;
#  define DYNLIBPRE "lib"
#  define DYNLIBPST ".sl"
#  define DYNLIBLEN 6

# elif defined(DYNAMIC_OS2)
   typedef HMODULE handle_type ;
/*  typedef PHMODULE handle_type ; */

# elif defined(DYNAMIC_WIN32)
   typedef HINSTANCE handle_type ;
# elif defined(DYNAMIC_VMS)
#  define DYNLIBPRE "LIB"
#  define DYNLIBPST ".EXE"
#  define DYNLIBLEN 7
#endif /* DYNAMIC */

#include <errno.h>
#include <string.h>

#ifdef DYNAMIC_HPSHLOAD_NOT_NEEDED
/*
 * HP/UX doesn't find symbols unless they are 'true data'; entrypoints
 * for functions are not sufficient! Thus, we need to force it to
 * 'release' that these symbols are defined. There's no need to do
 * anything more, once it sets some flag somewhere ... it'll find'em
 * later. Weird!
 *
 * Datatype char only used to save space ... this is never used, so
 * there is no reason to waste space.
 *
 * Removed by MH 0.08h. Causes problems on HPUX 10.20 linker, and may
 * not be needed anymore.
 */

char Food_For_Fuzzy_HP_Linkers[] = {
   (char)exiterror, (char)getonechar,
   (char)nullstringptr,
   (char)atozpos, (char)checkparam, (char)loadrxfunc } ;
#endif

#if defined(DYNAMIC_AIXLOAD)
void *wrapper_dummy_for_aix( void )
{
}
#endif

void *wrapper_load( streng *module )
{
   streng *new_module=NULL ;
   handle_type handle=(handle_type)NULL ;
   char *msg=NULL;
#if defined(DYNAMIC_OS2)
   CHAR LoadError[256];
   APIRET rc=0L;
#endif
#if defined(DYNAMIC_WIN32)
   char LoadError[256];
#endif
#if defined(DYNAMIC_HPSHLOAD)
   char libname[1024];
   char modname[256];
#endif

#if !defined(DYNLIBLEN)
   new_module = Str_ify( module ) ;
#else
# if defined(DYNAMIC_HPSHLOAD)
   strcpy( modname, DYNLIBPRE );
   strcat( modname, module->value );
   strcat( modname, DYNLIBPST );
   find_shared_library(modname,"SHLIB_PATH",libname);
   new_module = Str_make( strlen( libname ) + 1 ) ;
   ( void* )Str_catstr( new_module, libname );
# else
   new_module = Str_make( Str_len( module ) + DYNLIBLEN + 1 ) ;
   ( void* )Str_catstr( new_module, DYNLIBPRE );
   ( void* )Str_cat( new_module, module );
   ( void* )Str_catstr( new_module, DYNLIBPST );
# endif
   new_module = Str_ify( new_module ) ;
#endif

#if defined(DYNAMIC_DLOPEN)
   handle = dlopen( new_module->value, RTLD_LAZY ) ;
   if (handle==NULL)
     {
      msg = dlerror();
      if (msg)
        {
         set_err_message( msg ) ;
        }
      else
         set_err_message("");
     }
#elif defined(DYNAMIC_HPSHLOAD)
   handle = shl_load( new_module->value, BIND_IMMEDIATE | BIND_VERBOSE | DYNAMIC_PATH ,0L ) ;
   if (handle==NULL)
      set_err_message( strerror(errno)) ;
#elif defined(DYNAMIC_AIXLOAD)
   handle = load( new_module->value, 1, NULL ) ;
   if (handle)
     {
      int rc=loadbind( 0, (void *)wrapper_dummy_for_aix, (void *)handle ) ;
      if (rc)
         handle = NULL ;
     }
   else
      set_err_message( strerror( errno )) ;
#elif defined(DYNAMIC_OS2)
   rc = DosLoadModule( LoadError, sizeof(LoadError),
                       new_module->value, &handle ) ;
   if (rc)
     {
      set_err_message(LoadError);
      handle = (handle_type)NULL;
     }
#elif defined(DYNAMIC_WIN32)
   handle = LoadLibrary( new_module->value ) ;
   if (handle==NULL)
     {
      FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
      set_err_message(LoadError);
     }
#endif

#if !defined(DYNLIBLEN)
   if (new_module != module)
      Free_string( new_module ) ;
#else
   Free_string( new_module ) ;
#endif

   return (void *)handle ;
}

#ifdef REGINA_0_07A
void *wrapper_getentries( handle_type handle )
{
   void *addr ;

#if defined(DYNAMIC_DLOPEN)
   addr = (void *)dlsym( handle, "initial" ) ;
   if (addr==NULL)
      set_err_message( dlerror()) ;
#elif defined(DYNAMIC_HPSHLOAD)
   {
      long eaddr ;
      if (shl_findsym( &handle, "initial", TYPE_PROCEDURE, &eaddr ))
      {
         addr = NULL ;
         set_err_message( strerror(errno)) ;
      }
      else
         addr = (void *)eaddr ;
   }
#elif defined(DYNAMIC_AIXLOAD)
   addr = handle ;
#elif defined(DYNAMIC_WIN32)
   addr = handle;
#endif
   if (addr)
      return (void *)addr ;
   else
      return NULL ;
}
#endif /* REGINA_0_07A */

PFN wrapper_get_addr( struct library *lptr, streng *name)
{
   PFN addr;
   handle_type handle=(handle_type)lptr->handle;
   streng *newname=NULL ;
#if defined(DYNAMIC_OS2)
   APIRET rc=0L;
#endif

   newname = Str_ify( name ) ;

#if defined(DYNAMIC_DLOPEN)
   /*
    * Note, the following assignment is not allowed by ANSI, but SVR4.2
    * includes it as an example, so it is probably safe in this context
    */
   addr = (PFN)(dlsym( handle, newname->value )) ;
   if (addr==NULL)
      set_err_message( dlerror()) ;
#elif defined(DYNAMIC_HPSHLOAD)
   {
      long eaddr ;
      if (shl_findsym( &handle, newname->value, TYPE_PROCEDURE, &eaddr ))
      {
         addr = NULL ;
         set_err_message( strerror(errno)) ;
      }
      else
         addr = (PFN)eaddr ;
   }
#elif defined(DYNAMIC_AIXLOAD)
   addr = (PFN)handle ;
#elif defined(DYNAMIC_OS2)
   rc = DosQueryProcAddr(handle,0L,newname->value,&addr);
   if (rc)
   {
      char buf[150];
      sprintf(buf,"DosQueryProcAddr() failed with %d looking for %s", rc, newname->value );
      set_err_message( buf ) ;
   }
#elif defined(DYNAMIC_WIN32)
   /*  13/12/1999 JH moved cast, (HMODULE), from second parm to first.  Removed
    * a compiler warning,
    */
   addr = (PFN)GetProcAddress((HMODULE)handle,newname->value);
   if (addr == NULL)
      set_err_message( strerror(errno)) ;
#endif
   if (newname != name)
      Free_string( newname ) ;
   if (addr)
      return (PFN)addr ;
   else
      return NULL ;
}

#endif /* DYNAMIC */