Commit | Line | Data |
a0d0e21e |
1 | /* dl_vms.xs |
2 | * |
a491bdcd |
3 | * Platform: OpenVMS, VAX or AXP or IA64 |
bd3fa61c |
4 | * Author: Charles Bailey bailey@newman.upenn.edu |
a491bdcd |
5 | * Revised: See http://public.activestate.com/cgi-bin/perlbrowse |
a0d0e21e |
6 | * |
7 | * Implementation Note |
8 | * This section is added as an aid to users and DynaLoader developers, in |
9 | * order to clarify the process of dynamic linking under VMS. |
10 | * dl_vms.xs uses the supported VMS dynamic linking call, which allows |
11 | * a running program to map an arbitrary file of executable code and call |
12 | * routines within that file. This is done via the VMS RTL routine |
13 | * lib$find_image_symbol, whose calling sequence is as follows: |
14 | * status = lib$find_image_symbol(imgname,symname,symval,defspec); |
15 | * where |
16 | * status = a standard VMS status value (unsigned long int) |
17 | * imgname = a fixed-length string descriptor, passed by |
18 | * reference, containing the NAME ONLY of the image |
19 | * file to be mapped. An attempt will be made to |
20 | * translate this string as a logical name, so it may |
21 | * not contain any characters which are not allowed in |
22 | * logical names. If no translation is found, imgname |
23 | * is used directly as the name of the image file. |
24 | * symname = a fixed-length string descriptor, passed by |
25 | * reference, containing the name of the routine |
26 | * to be located. |
27 | * symval = an unsigned long int, passed by reference, into |
28 | * which is written the entry point address of the |
29 | * routine whose name is specified in symname. |
30 | * defspec = a fixed-length string descriptor, passed by |
31 | * reference, containing a default file specification |
32 | * whichis used to fill in any missing parts of the |
33 | * image file specification after the imgname argument |
34 | * is processed. |
35 | * In order to accommodate the handling of the imgname argument, the routine |
36 | * dl_expandspec() is provided for use by perl code (e.g. dl_findfile) |
37 | * which wants to see what image file lib$find_image_symbol would use if |
38 | * it were passed a given file specification. The file specification passed |
39 | * to dl_expandspec() and dl_load_file() can be partial or complete, and can |
40 | * use VMS or Unix syntax; these routines perform the necessary conversions. |
41 | * In general, writers of perl extensions need only conform to the |
42 | * procedures set out in the DynaLoader documentation, and let the details |
43 | * be taken care of by the routines here and in DynaLoader.pm. If anyone |
44 | * comes across any incompatibilities, please let me know. Thanks. |
45 | * |
46 | */ |
47 | |
48 | #include "EXTERN.h" |
49 | #include "perl.h" |
50 | #include "XSUB.h" |
51 | |
a0d0e21e |
52 | #include <descrip.h> |
53 | #include <fscndef.h> |
54 | #include <lib$routines.h> |
55 | #include <rms.h> |
56 | #include <ssdef.h> |
748a9306 |
57 | #include <starlet.h> |
a0d0e21e |
58 | |
b6837a3b |
59 | #if defined(VMS_WE_ARE_CASE_SENSITIVE) |
60 | #define DL_CASE_SENSITIVE 1<<4 |
61 | #else |
62 | #define DL_CASE_SENSITIVE 0 |
63 | #endif |
64 | |
a0d0e21e |
65 | typedef unsigned long int vmssts; |
66 | |
67 | struct libref { |
68 | struct dsc$descriptor_s name; |
69 | struct dsc$descriptor_s defspec; |
70 | }; |
71 | |
cdc73a10 |
72 | typedef struct { |
73 | AV * x_require_symbols; |
74 | /* "Static" data for dl_expand_filespec() - This is static to save |
a0d0e21e |
75 | * initialization on each call; if you need context-independence, |
76 | * just make these auto variables in dl_expandspec() and dl_load_file() |
77 | */ |
cdc73a10 |
78 | char x_esa[NAM$C_MAXRSS]; |
79 | char x_rsa[NAM$C_MAXRSS]; |
80 | struct FAB x_fab; |
81 | struct NAM x_nam; |
82 | } my_cxtx_t; /* this *must* be named my_cxtx_t */ |
83 | |
84 | #define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */ |
85 | #include "dlutils.c" /* dl_debug, dl_last_error; SaveError not used */ |
86 | |
87 | #define dl_require_symbols (dl_cxtx.x_require_symbols) |
88 | #define dl_esa (dl_cxtx.x_esa) |
89 | #define dl_rsa (dl_cxtx.x_rsa) |
90 | #define dl_fab (dl_cxtx.x_fab) |
91 | #define dl_nam (dl_cxtx.x_nam) |
92 | |
93 | /* $PutMsg action routine - records error message in dl_last_error */ |
a0d0e21e |
94 | static vmssts |
95 | copy_errmsg(msg,unused) |
96 | struct dsc$descriptor_s * msg; |
97 | vmssts unused; |
98 | { |
ecdc15d9 |
99 | dTHX; |
cdc73a10 |
100 | dMY_CXT; |
748a9306 |
101 | if (*(msg->dsc$a_pointer) == '%') { /* first line */ |
cdc73a10 |
102 | if (dl_last_error) |
103 | strncpy((dl_last_error = saferealloc(dl_last_error,msg->dsc$w_length+1)), |
a0d0e21e |
104 | msg->dsc$a_pointer, msg->dsc$w_length); |
105 | else |
cdc73a10 |
106 | strncpy((dl_last_error = safemalloc(msg->dsc$w_length+1)), |
a0d0e21e |
107 | msg->dsc$a_pointer, msg->dsc$w_length); |
cdc73a10 |
108 | dl_last_error[msg->dsc$w_length] = '\0'; |
a0d0e21e |
109 | } |
110 | else { /* continuation line */ |
cdc73a10 |
111 | int errlen = strlen(dl_last_error); |
112 | dl_last_error = saferealloc(dl_last_error, errlen + msg->dsc$w_length + 2); |
113 | dl_last_error[errlen] = '\n'; dl_last_error[errlen+1] = '\0'; |
114 | strncat(dl_last_error, msg->dsc$a_pointer, msg->dsc$w_length); |
115 | dl_last_error[errlen+msg->dsc$w_length+1] = '\0'; |
a0d0e21e |
116 | } |
8381071f |
117 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "Saved error message: %s\n", dl_last_error)); |
2708a6a3 |
118 | return 0; |
a0d0e21e |
119 | } |
120 | |
121 | /* Use $PutMsg to retrieve error message for failure status code */ |
122 | static void |
123 | dl_set_error(sts,stv) |
124 | vmssts sts; |
125 | vmssts stv; |
126 | { |
748a9306 |
127 | vmssts vec[3]; |
5c84aa53 |
128 | dTHX; |
a0d0e21e |
129 | |
130 | vec[0] = stv ? 2 : 1; |
131 | vec[1] = sts; vec[2] = stv; |
748a9306 |
132 | _ckvmssts(sys$putmsg(vec,copy_errmsg,0,0)); |
a0d0e21e |
133 | } |
134 | |
2708a6a3 |
135 | /* wrapper for lib$find_image_symbol, so signalled errors can be saved |
136 | * for dl_error and then returned */ |
137 | static unsigned long int |
138 | my_find_image_symbol(struct dsc$descriptor_s *imgname, |
139 | struct dsc$descriptor_s *symname, |
140 | void (**entry)(), |
141 | struct dsc$descriptor_s *defspec) |
142 | { |
143 | unsigned long int retsts; |
8381071f |
144 | VAXC$ESTABLISH(lib$sig_to_ret); |
b6837a3b |
145 | retsts = lib$find_image_symbol(imgname,symname,entry,defspec,DL_CASE_SENSITIVE); |
2708a6a3 |
146 | return retsts; |
147 | } |
148 | |
149 | |
a0d0e21e |
150 | static void |
cea2e8a9 |
151 | dl_private_init(pTHX) |
a0d0e21e |
152 | { |
cea2e8a9 |
153 | dl_generic_private_init(aTHX); |
cdc73a10 |
154 | { |
155 | dMY_CXT; |
156 | dl_require_symbols = get_av("DynaLoader::dl_require_symbols", 0x4); |
157 | /* Set up the static control blocks for dl_expand_filespec() */ |
158 | dl_fab = cc$rms_fab; |
159 | dl_nam = cc$rms_nam; |
160 | dl_fab.fab$l_nam = &dl_nam; |
161 | dl_nam.nam$l_esa = dl_esa; |
162 | dl_nam.nam$b_ess = sizeof dl_esa; |
163 | dl_nam.nam$l_rsa = dl_rsa; |
164 | dl_nam.nam$b_rss = sizeof dl_rsa; |
165 | } |
a0d0e21e |
166 | } |
167 | MODULE = DynaLoader PACKAGE = DynaLoader |
168 | |
169 | BOOT: |
cea2e8a9 |
170 | (void)dl_private_init(aTHX); |
a0d0e21e |
171 | |
748a9306 |
172 | void |
a0d0e21e |
173 | dl_expandspec(filespec) |
174 | char * filespec |
175 | CODE: |
176 | char vmsspec[NAM$C_MAXRSS], defspec[NAM$C_MAXRSS]; |
177 | size_t deflen; |
178 | vmssts sts; |
cdc73a10 |
179 | dMY_CXT; |
a0d0e21e |
180 | |
181 | tovmsspec(filespec,vmsspec); |
cdc73a10 |
182 | dl_fab.fab$l_fna = vmsspec; |
183 | dl_fab.fab$b_fns = strlen(vmsspec); |
184 | dl_fab.fab$l_dna = 0; |
185 | dl_fab.fab$b_dns = 0; |
146174a9 |
186 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_expand_filespec(%s):\n",vmsspec)); |
a0d0e21e |
187 | /* On the first pass, just parse the specification string */ |
cdc73a10 |
188 | dl_nam.nam$b_nop = NAM$M_SYNCHK; |
189 | sts = sys$parse(&dl_fab); |
146174a9 |
190 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tSYNCHK sys$parse = %d\n",sts)); |
a0d0e21e |
191 | if (!(sts & 1)) { |
cdc73a10 |
192 | dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv); |
6b88bc9c |
193 | ST(0) = &PL_sv_undef; |
a0d0e21e |
194 | } |
195 | else { |
196 | /* Now set up a default spec - everything but the name */ |
cdc73a10 |
197 | deflen = dl_nam.nam$l_name - dl_esa; |
198 | memcpy(defspec,dl_esa,deflen); |
199 | memcpy(defspec+deflen,dl_nam.nam$l_type, |
200 | dl_nam.nam$b_type + dl_nam.nam$b_ver); |
201 | deflen += dl_nam.nam$b_type + dl_nam.nam$b_ver; |
202 | memcpy(vmsspec,dl_nam.nam$l_name,dl_nam.nam$b_name); |
146174a9 |
203 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsplit filespec: name = %.*s, default = %.*s\n", |
cdc73a10 |
204 | dl_nam.nam$b_name,vmsspec,deflen,defspec)); |
a0d0e21e |
205 | /* . . . and go back to expand it */ |
cdc73a10 |
206 | dl_nam.nam$b_nop = 0; |
207 | dl_fab.fab$l_dna = defspec; |
208 | dl_fab.fab$b_dns = deflen; |
209 | dl_fab.fab$b_fns = dl_nam.nam$b_name; |
210 | sts = sys$parse(&dl_fab); |
146174a9 |
211 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tname/default sys$parse = %d\n",sts)); |
a0d0e21e |
212 | if (!(sts & 1)) { |
cdc73a10 |
213 | dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv); |
6b88bc9c |
214 | ST(0) = &PL_sv_undef; |
a0d0e21e |
215 | } |
216 | else { |
217 | /* Now find the actual file */ |
cdc73a10 |
218 | sts = sys$search(&dl_fab); |
146174a9 |
219 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$search = %d\n",sts)); |
2708a6a3 |
220 | if (!(sts & 1)) { |
cdc73a10 |
221 | dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv); |
6b88bc9c |
222 | ST(0) = &PL_sv_undef; |
a0d0e21e |
223 | } |
224 | else { |
cdc73a10 |
225 | ST(0) = sv_2mortal(newSVpvn(dl_nam.nam$l_rsa,dl_nam.nam$b_rsl)); |
146174a9 |
226 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "\tresult = \\%.*s\\\n", |
cdc73a10 |
227 | dl_nam.nam$b_rsl,dl_nam.nam$l_rsa)); |
a0d0e21e |
228 | } |
229 | } |
230 | } |
231 | |
748a9306 |
232 | void |
a491bdcd |
233 | dl_load_file(filename, flags=0) |
234 | char * filename |
ff7f3c60 |
235 | int flags |
236 | PREINIT: |
626727d5 |
237 | dTHX; |
ecdc15d9 |
238 | dMY_CXT; |
a0d0e21e |
239 | char vmsspec[NAM$C_MAXRSS]; |
a0d0e21e |
240 | SV *reqSV, **reqSVhndl; |
241 | STRLEN deflen; |
242 | struct dsc$descriptor_s |
243 | specdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}, |
244 | symdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}; |
245 | struct fscnlst { |
246 | unsigned short int len; |
247 | unsigned short int code; |
248 | char *string; |
748a9306 |
249 | } namlst[2] = {{0,FSCN$_NAME,0},{0,0,0}}; |
a0d0e21e |
250 | struct libref *dlptr; |
251 | vmssts sts, failed = 0; |
2708a6a3 |
252 | void (*entry)(); |
ff7f3c60 |
253 | CODE: |
a0d0e21e |
254 | |
a491bdcd |
255 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags)); |
256 | specdsc.dsc$a_pointer = tovmsspec(filename,vmsspec); |
a0d0e21e |
257 | specdsc.dsc$w_length = strlen(specdsc.dsc$a_pointer); |
a491bdcd |
258 | if (specdsc.dsc$w_length == 0) { /* undef in, empty out */ |
259 | XSRETURN_EMPTY; |
260 | } |
261 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tVMS-ified filename is %s\n", |
a0d0e21e |
262 | specdsc.dsc$a_pointer)); |
a02a5408 |
263 | Newx(dlptr,1,struct libref); |
a0d0e21e |
264 | dlptr->name.dsc$b_dtype = dlptr->defspec.dsc$b_dtype = DSC$K_DTYPE_T; |
265 | dlptr->name.dsc$b_class = dlptr->defspec.dsc$b_class = DSC$K_CLASS_S; |
266 | sts = sys$filescan(&specdsc,namlst,0); |
146174a9 |
267 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$filescan: returns %d, name is %.*s\n", |
a0d0e21e |
268 | sts,namlst[0].len,namlst[0].string)); |
269 | if (!(sts & 1)) { |
270 | failed = 1; |
271 | dl_set_error(sts,0); |
272 | } |
273 | else { |
274 | dlptr->name.dsc$w_length = namlst[0].len; |
275 | dlptr->name.dsc$a_pointer = savepvn(namlst[0].string,namlst[0].len); |
276 | dlptr->defspec.dsc$w_length = specdsc.dsc$w_length - namlst[0].len; |
a02a5408 |
277 | Newx(dlptr->defspec.dsc$a_pointer, dlptr->defspec.dsc$w_length + 1, char); |
a0d0e21e |
278 | deflen = namlst[0].string - specdsc.dsc$a_pointer; |
279 | memcpy(dlptr->defspec.dsc$a_pointer,specdsc.dsc$a_pointer,deflen); |
280 | memcpy(dlptr->defspec.dsc$a_pointer + deflen, |
281 | namlst[0].string + namlst[0].len, |
282 | dlptr->defspec.dsc$w_length - deflen); |
146174a9 |
283 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlibref = name: %s, defspec: %.*s\n", |
a0d0e21e |
284 | dlptr->name.dsc$a_pointer, |
285 | dlptr->defspec.dsc$w_length, |
286 | dlptr->defspec.dsc$a_pointer)); |
8e07c86e |
287 | if (!(reqSVhndl = av_fetch(dl_require_symbols,0,FALSE)) || !(reqSV = *reqSVhndl)) { |
146174a9 |
288 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t@dl_require_symbols empty, returning untested libref\n")); |
a0d0e21e |
289 | } |
290 | else { |
291 | symdsc.dsc$w_length = SvCUR(reqSV); |
292 | symdsc.dsc$a_pointer = SvPVX(reqSV); |
146174a9 |
293 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t$dl_require_symbols[0] = %.*s\n", |
a0d0e21e |
294 | symdsc.dsc$w_length, symdsc.dsc$a_pointer)); |
2708a6a3 |
295 | sts = my_find_image_symbol(&(dlptr->name),&symdsc, |
a0d0e21e |
296 | &entry,&(dlptr->defspec)); |
146174a9 |
297 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts)); |
a0d0e21e |
298 | if (!(sts&1)) { |
299 | failed = 1; |
300 | dl_set_error(sts,0); |
301 | } |
302 | } |
303 | } |
304 | |
305 | if (failed) { |
306 | Safefree(dlptr->name.dsc$a_pointer); |
307 | Safefree(dlptr->defspec.dsc$a_pointer); |
308 | Safefree(dlptr); |
6b88bc9c |
309 | ST(0) = &PL_sv_undef; |
a0d0e21e |
310 | } |
311 | else { |
c529f79d |
312 | ST(0) = sv_2mortal(newSViv(PTR2IV(dlptr))); |
a0d0e21e |
313 | } |
314 | |
315 | |
748a9306 |
316 | void |
a0d0e21e |
317 | dl_find_symbol(librefptr,symname) |
318 | void * librefptr |
319 | SV * symname |
320 | CODE: |
321 | struct libref thislib = *((struct libref *)librefptr); |
322 | struct dsc$descriptor_s |
323 | symdsc = {SvCUR(symname),DSC$K_DTYPE_T,DSC$K_CLASS_S,SvPVX(symname)}; |
324 | void (*entry)(); |
325 | vmssts sts; |
326 | |
a491bdcd |
327 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_find_symbol(%.*s,%.*s):\n", |
a0d0e21e |
328 | thislib.name.dsc$w_length, thislib.name.dsc$a_pointer, |
329 | symdsc.dsc$w_length,symdsc.dsc$a_pointer)); |
2708a6a3 |
330 | sts = my_find_image_symbol(&(thislib.name),&symdsc, |
331 | &entry,&(thislib.defspec)); |
146174a9 |
332 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts)); |
333 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tentry point is %d\n", |
a0d0e21e |
334 | (unsigned long int) entry)); |
335 | if (!(sts & 1)) { |
8381071f |
336 | dl_set_error(sts,0); |
6b88bc9c |
337 | ST(0) = &PL_sv_undef; |
a0d0e21e |
338 | } |
c529f79d |
339 | else ST(0) = sv_2mortal(newSViv(PTR2IV(entry))); |
a0d0e21e |
340 | |
341 | |
342 | void |
343 | dl_undef_symbols() |
344 | PPCODE: |
345 | |
346 | |
347 | # These functions should not need changing on any platform: |
348 | |
349 | void |
350 | dl_install_xsub(perl_name, symref, filename="$Package") |
351 | char * perl_name |
352 | void * symref |
d3f5e399 |
353 | const char * filename |
a0d0e21e |
354 | CODE: |
146174a9 |
355 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n", |
a0d0e21e |
356 | perl_name, symref)); |
77004dee |
357 | ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name, |
358 | (void(*)(pTHX_ CV *))symref, |
359 | filename, NULL, |
360 | XS_DYNAMIC_FILENAME))); |
a0d0e21e |
361 | |
362 | |
363 | char * |
364 | dl_error() |
365 | CODE: |
cdc73a10 |
366 | dMY_CXT; |
367 | RETVAL = dl_last_error ; |
a0d0e21e |
368 | OUTPUT: |
369 | RETVAL |
370 | |
371 | # end. |