dynaload.c

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 71 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ICO/t/t60writefail.t ↑ Summary feat.c »
1 #define IMAGER_NO_CONTEXT
2 #if defined(OS_hpux)
3 #include <dl.h>
4 typedef shl_t minthandle_t;
5 #elif defined(WIN32)
6 #define WIN32_LEAN_AND_MEAN
7 #include <windows.h>
8 typedef HMODULE minthandle_t;
9 #undef WIN32_LEAN_AND_MEAN
10 #else
11 #include <dlfcn.h>
12 typedef void *minthandle_t;
13 #endif
14  
15 #include "ext.h"
16  
17 struct DSO_handle_tag {
18 minthandle_t handle;
19 char *filename;
20 func_ptr *function_list;
21 };
22  
23 #include "imager.h"
24 #include "dynaload.h"
25 /* #include "XSUB.h" so we can compile on threaded perls */
26 #include "imageri.h"
27  
28 static im_context_t
29 0 do_get_context(void) {
30 0 return im_get_context();
31 }
32  
33 static i_img *
34 0 my_img_empty_ch(pIMCTX, i_img *im, i_img_dim x, i_img_dim y, int ch) {
35 0 ▶ if (im != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (im != NULL) 0 0 0
36 0 return NULL;
37  
38 0 return im_img_8_new(aIMCTX, x, y, ch);
39 }
40  
41 static symbol_table_t symbol_table=
42 {
43 i_has_format,
44 ICL_set_internal,
45 ICL_info,
46 do_get_context,
47 my_img_empty_ch,
48 i_img_exorcise,
49 i_img_info,
50 i_img_setmask,
51 i_img_getmask,
52 i_box,
53 i_line,
54 i_arc,
55 i_copyto,
56 i_copyto_trans,
57 i_rubthru
58 };
59  
60  
61 /*
62 Dynamic loading works like this:
63 dynaload opens the shared object and
64 loads all the functions into an array of functions
65 it returns a string from the dynamic function that
66 can be supplied to the parser for evaling.
67 */
68  
69 void
70 0 DSO_call(DSO_handle *handle,int func_index,HV* hv) {
71 0 dIMCTX;
72 0 im_log((aIMCTX, 1,"DSO_call(handle %p, func_index %d, hv %p)\n",
73 handle, func_index, hv));
74 0 (handle->function_list[func_index].iptr)((void*)hv);
75 0 }
76  
77 func_ptr *
78 0 DSO_funclist(DSO_handle *handle) {
79 0 return handle->function_list;
80 }
81  
82  
83 #if defined( OS_hpux )
84  
85 void*
86 DSO_open(char* file,char** evalstring) {
87 shl_t tt_handle;
88 void *d_handle,**plugin_symtab,**plugin_utiltab;
89 int rc,*iptr, (*fptr)(int);
90 func_ptr *function_list;
91 DSO_handle *dso_handle;
92 void (*f)(void *s,void *u); /* these will just have to be void for now */
93 int i;
94 dIMCTX;
95  
96 *evalstring=NULL;
97  
98 im_log( (aIMCTX, 1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
99  
100 if ( (tt_handle = shl_load(file, BIND_DEFERRED,0L)) == NULL) return NULL;
101 if ( (shl_findsym(&tt_handle, I_EVALSTR,TYPE_UNDEFINED,(void*)evalstring))) return NULL;
102  
103 /*
104 if ( (shl_findsym(&tt_handle, "symbol_table",TYPE_UNDEFINED,(void*)&plugin_symtab))) return NULL;
105 if ( (shl_findsym(&tt_handle, "util_table",TYPE_UNDEFINED,&plugin_utiltab))) return NULL;
106 (*plugin_symtab)=&symbol_table;
107 (*plugin_utiltab)=&i_UTIL_table;
108 */
109  
110 if ( (shl_findsym(&tt_handle, I_INSTALL_TABLES ,TYPE_UNDEFINED, &f ))) return NULL;
111
112 im_log( (aIMCTX, 1,"Calling install_tables\n") );
113 f(&symbol_table,&i_UTIL_table);
114 im_log( (aIMCTX, 1,"Call ok.\n") );
115
116 if ( (shl_findsym(&tt_handle, I_FUNCTION_LIST ,TYPE_UNDEFINED,(func_ptr*)&function_list))) return NULL;
117 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) /* checked 17jul05 tonyc */
118 return NULL;
119  
120 dso_handle->handle=tt_handle; /* needed to close again */
121 dso_handle->function_list=function_list;
122 if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
123 free(dso_handle); return NULL;
124 }
125 strcpy(dso_handle->filename,file);
126  
127 im_log((aIMCTX, 1,"DSO_open <- (0x%X)\n",dso_handle));
128 return (void*)dso_handle;
129 }
130  
131 undef_int
132 DSO_close(void *ptr) {
133 DSO_handle *handle=(DSO_handle*) ptr;
134 dIMCTX;
135 im_log((1,"DSO_close(ptr 0x%X)\n",ptr));
136 return !shl_unload((handle->handle));
137 }
138  
139 #elif defined(WIN32)
140  
141 void *
142 DSO_open(char *file, char **evalstring) {
143 HMODULE d_handle;
144 func_ptr *function_list;
145 DSO_handle *dso_handle;
146 dIMCTX;
147
148 void (*f)(void *s,void *u); /* these will just have to be void for now */
149  
150 im_log( (aIMCTX, 1,"DSO_open(file '%s' (%p), evalstring %p)\n",file,file,evalstring) );
151  
152 *evalstring = NULL;
153 if ((d_handle = LoadLibrary(file)) == NULL) {
154 im_log((aIMCTX, 1, "DSO_open: LoadLibrary(%s) failed: %lu\n", file, GetLastError()));
155 return NULL;
156 }
157 if ( (*evalstring = (char *)GetProcAddress(d_handle, I_EVALSTR)) == NULL) {
158 im_log((aIMCTX, 1,"DSO_open: GetProcAddress didn't fine '%s': %lu\n", I_EVALSTR, GetLastError()));
159 FreeLibrary(d_handle);
160 return NULL;
161 }
162 if ((f = (void (*)(void *, void*))(void (*)(void))GetProcAddress(d_handle, I_INSTALL_TABLES)) == NULL) {
163 im_log((aIMCTX, 1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_INSTALL_TABLES, GetLastError()));
164 FreeLibrary(d_handle);
165 return NULL;
166 }
167 im_log((aIMCTX, 1, "Calling install tables\n"));
168 f(&symbol_table, &i_UTIL_table);
169 im_log((aIMCTX, 1, "Call ok\n"));
170
171 if ( (function_list = (func_ptr *)GetProcAddress(d_handle, I_FUNCTION_LIST)) == NULL) {
172 im_log((aIMCTX, 1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_FUNCTION_LIST, GetLastError()));
173 FreeLibrary(d_handle);
174 return NULL;
175 }
176 if ( (dso_handle = (DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) { /* checked 17jul05 tonyc */
177 im_log( (aIMCTX, 1, "DSO_Open: out of memory\n") );
178 FreeLibrary(d_handle);
179 return NULL;
180 }
181 dso_handle->handle=d_handle; /* needed to close again */
182 dso_handle->function_list=function_list;
183 if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
184 free(dso_handle);
185 FreeLibrary(d_handle);
186 return NULL;
187 }
188 strcpy(dso_handle->filename,file);
189  
190 im_log( (aIMCTX, 1,"DSO_open <- %p\n",dso_handle) );
191 return (void*)dso_handle;
192  
193 }
194  
195 undef_int
196 DSO_close(void *ptr) {
197 DSO_handle *handle = (DSO_handle *)ptr;
198 BOOL result = FreeLibrary(handle->handle);
199 free(handle->filename);
200 free(handle);
201  
202 return result;
203 }
204  
205 #else
206  
207 /* OS/2 has no dlclose; Perl doesn't provide one. */
208 #ifdef __EMX__ /* OS/2 */
209 int
210 dlclose(minthandle_t h) {
211 return DosFreeModule(h) ? -1 : 0;
212 }
213 #endif /* __EMX__ */
214  
215 void*
216 0 DSO_open(char* file,char** evalstring) {
217 void *d_handle;
218 func_ptr *function_list;
219 DSO_handle *dso_handle;
220 0 dIMCTX;
221  
222 void (*f)(void *s,void *u); /* these will just have to be void for now */
223
224 0 *evalstring=NULL;
225  
226 0 im_log( (aIMCTX, 1,"DSO_open(file '%s' (%p), evalstring %p)\n",
227 file, file, evalstring) );
228  
229 0 ▶ if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) { 0 0 0
230 0 im_log( (aIMCTX, 1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
231 0 return NULL;
232 }
233  
234 0 ▶ if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) { 0 0 0
235 0 im_log( (aIMCTX, 1,"DSO_open: dlsym didn't find '%s': %s.\n",I_EVALSTR,dlerror()) );
236 0 return NULL;
237 }
238  
239 /*
240  
241 I'll just leave this thing in here for now if I need it real soon
242  
243 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_SYMBOL_TABLE ));
244 if ( (plugin_symtab = dlsym(d_handle, I_SYMBOL_TABLE)) == NULL) {
245 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_SYMBOL_TABLE,dlerror()) );
246 return NULL;
247 }
248
249 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_UTIL_TABLE ));
250 if ( (plugin_utiltab = dlsym(d_handle, I_UTIL_TABLE)) == NULL) {
251 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_UTIL_TABLE,dlerror()) );
252 return NULL;
253 }
254  
255 */
256  
257 0 f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES);
258 0 im_log( (aIMCTX, 1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
259 0 ▶ if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) { 0 0 0
260 0 im_log( (aIMCTX, 1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
261 0 dlclose(d_handle);
262 0 return NULL;
263 }
264  
265 0 im_log( (aIMCTX, 1,"Calling install_tables\n") );
266 0 f(&symbol_table,&i_UTIL_table);
267 0 im_log( (aIMCTX, 1,"Call ok.\n") );
268  
269 /* (*plugin_symtab)=&symbol_table;
270 (*plugin_utiltab)=&i_UTIL_table; */
271
272 0 im_log( (aIMCTX, 1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
273 0 ▶ if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) { 0 0 0
274 0 im_log( (aIMCTX, 1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
275 0 dlclose(d_handle);
276 0 return NULL;
277 }
278  
279 /* checked 17jul05 tonyc */
280 0 ▶ if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) { 0 0 0
281 0 dlclose(d_handle);
282 0 return NULL;
283 }
284
285 0 dso_handle->handle=d_handle; /* needed to close again */
286 0 dso_handle->function_list=function_list;
287 0 ▶ if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
Branch: 1 branch
BranchTrueFalseTotal
if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */ 0 0 0
288 0 free(dso_handle);
289 0 dlclose(d_handle);
290 0 return NULL;
291 }
292 0 strcpy(dso_handle->filename,file);
293  
294 0 im_log( (aIMCTX, 1,"DSO_open <- %p\n",dso_handle) );
295 0 return (void*)dso_handle;
296 }
297  
298 undef_int
299 0 DSO_close(void *ptr) {
300 DSO_handle *handle;
301 undef_int result;
302 0 dIMCTX;
303  
304 0 im_log((aIMCTX, 1,"DSO_close(ptr %p)\n",ptr));
305 0 handle=(DSO_handle*) ptr;
306 0 result = !dlclose(handle->handle);
307 0 free(handle->filename);
308 0 free(handle);
309 0 return result;
310 }
311  
312 #endif
313  
« ICO/t/t60writefail.t ↑ Summary feat.c »