2089826bc1540c76d610c74b1ce7c7e867f1e5ac
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / dl_vms.xs
1 /* dl_vms.xs
2  * 
3  * Platform:  OpenVMS, VAX or AXP
4  * Author:    Charles Bailey  bailey@newman.upenn.edu
5  * Revised:   12-Dec-1994
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
52 /* N.B.:
53  * dl_debug and dl_last_error are static vars; you'll need to deal
54  * with them appropriately if you need context independence
55  */
56
57 #include <descrip.h>
58 #include <fscndef.h>
59 #include <lib$routines.h>
60 #include <rms.h>
61 #include <ssdef.h>
62 #include <starlet.h>
63
64 #if defined(VMS_WE_ARE_CASE_SENSITIVE)
65 #define DL_CASE_SENSITIVE 1<<4
66 #else
67 #define DL_CASE_SENSITIVE 0
68 #endif
69
70 typedef unsigned long int vmssts;
71
72 struct libref {
73   struct dsc$descriptor_s name;
74   struct dsc$descriptor_s defspec;
75 };
76
77 typedef struct {
78     AV *        x_require_symbols;
79 /* "Static" data for dl_expand_filespec() - This is static to save
80  * initialization on each call; if you need context-independence,
81  * just make these auto variables in dl_expandspec() and dl_load_file()
82  */
83     char        x_esa[NAM$C_MAXRSS];
84     char        x_rsa[NAM$C_MAXRSS];
85     struct FAB  x_fab;
86     struct NAM  x_nam;
87 } my_cxtx_t;            /* this *must* be named my_cxtx_t */
88
89 #define DL_CXT_EXTRA    /* ask for dl_cxtx to be defined in dlutils.c */
90 #include "dlutils.c"    /* dl_debug, dl_last_error; SaveError not used  */
91
92 #define dl_require_symbols      (dl_cxtx.x_require_symbols)
93 #define dl_esa                  (dl_cxtx.x_esa)
94 #define dl_rsa                  (dl_cxtx.x_rsa)
95 #define dl_fab                  (dl_cxtx.x_fab)
96 #define dl_nam                  (dl_cxtx.x_nam)
97
98 /* $PutMsg action routine - records error message in dl_last_error */
99 static vmssts
100 copy_errmsg(msg,unused)
101     struct dsc$descriptor_s *   msg;
102     vmssts  unused;
103 {
104     dMY_CXT;
105     if (*(msg->dsc$a_pointer) == '%') { /* first line */
106       if (dl_last_error)
107         strncpy((dl_last_error = saferealloc(dl_last_error,msg->dsc$w_length+1)),
108                  msg->dsc$a_pointer, msg->dsc$w_length);
109       else
110         strncpy((dl_last_error = safemalloc(msg->dsc$w_length+1)),
111                  msg->dsc$a_pointer, msg->dsc$w_length);
112       dl_last_error[msg->dsc$w_length] = '\0';
113     }
114     else { /* continuation line */
115       int errlen = strlen(dl_last_error);
116       dl_last_error = saferealloc(dl_last_error, errlen + msg->dsc$w_length + 2);
117       dl_last_error[errlen] = '\n';  dl_last_error[errlen+1] = '\0';
118       strncat(dl_last_error, msg->dsc$a_pointer, msg->dsc$w_length);
119       dl_last_error[errlen+msg->dsc$w_length+1] = '\0';
120     }
121     return 0;
122 }
123
124 /* Use $PutMsg to retrieve error message for failure status code */
125 static void
126 dl_set_error(sts,stv)
127     vmssts  sts;
128     vmssts  stv;
129 {
130     vmssts vec[3];
131     dTHX;
132
133     vec[0] = stv ? 2 : 1;
134     vec[1] = sts;  vec[2] = stv;
135     _ckvmssts(sys$putmsg(vec,copy_errmsg,0,0));
136 }
137
138 static unsigned int
139 findsym_handler(void *sig, void *mech)
140 {
141     dTHX;
142     unsigned long int myvec[8],args, *usig = (unsigned long int *) sig;
143     /* Be paranoid and assume signal vector passed in might be readonly */
144     myvec[0] = args = usig[0] > 10 ? 9 : usig[0] - 1;
145     while (--args) myvec[args] = usig[args];
146     _ckvmssts(sys$putmsg(myvec,copy_errmsg,0,0));
147     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "findsym_handler: received\n\t%s\n",dl_last_error));
148     return SS$_CONTINUE;
149 }
150
151 /* wrapper for lib$find_image_symbol, so signalled errors can be saved
152  * for dl_error and then returned */
153 static unsigned long int
154 my_find_image_symbol(struct dsc$descriptor_s *imgname,
155                      struct dsc$descriptor_s *symname,
156                      void (**entry)(),
157                      struct dsc$descriptor_s *defspec)
158 {
159   unsigned long int retsts;
160   VAXC$ESTABLISH(findsym_handler);
161   retsts = lib$find_image_symbol(imgname,symname,entry,defspec,DL_CASE_SENSITIVE);
162   return retsts;
163 }
164
165
166 static void
167 dl_private_init(pTHX)
168 {
169     dl_generic_private_init(aTHX);
170     {
171         dMY_CXT;
172         dl_require_symbols = get_av("DynaLoader::dl_require_symbols", 0x4);
173         /* Set up the static control blocks for dl_expand_filespec() */
174         dl_fab = cc$rms_fab;
175         dl_nam = cc$rms_nam;
176         dl_fab.fab$l_nam = &dl_nam;
177         dl_nam.nam$l_esa = dl_esa;
178         dl_nam.nam$b_ess = sizeof dl_esa;
179         dl_nam.nam$l_rsa = dl_rsa;
180         dl_nam.nam$b_rss = sizeof dl_rsa;
181     }
182 }
183 MODULE = DynaLoader PACKAGE = DynaLoader
184
185 BOOT:
186     (void)dl_private_init(aTHX);
187
188 void
189 dl_expandspec(filespec)
190     char *      filespec
191     CODE:
192     char vmsspec[NAM$C_MAXRSS], defspec[NAM$C_MAXRSS];
193     size_t deflen;
194     vmssts sts;
195     dMY_CXT;
196
197     tovmsspec(filespec,vmsspec);
198     dl_fab.fab$l_fna = vmsspec;
199     dl_fab.fab$b_fns = strlen(vmsspec);
200     dl_fab.fab$l_dna = 0;
201     dl_fab.fab$b_dns = 0;
202     DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_expand_filespec(%s):\n",vmsspec));
203     /* On the first pass, just parse the specification string */
204     dl_nam.nam$b_nop = NAM$M_SYNCHK;
205     sts = sys$parse(&dl_fab);
206     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tSYNCHK sys$parse = %d\n",sts));
207     if (!(sts & 1)) {
208       dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
209       ST(0) = &PL_sv_undef;
210     }
211     else {
212       /* Now set up a default spec - everything but the name */
213       deflen = dl_nam.nam$l_name - dl_esa;
214       memcpy(defspec,dl_esa,deflen);
215       memcpy(defspec+deflen,dl_nam.nam$l_type,
216              dl_nam.nam$b_type + dl_nam.nam$b_ver);
217       deflen += dl_nam.nam$b_type + dl_nam.nam$b_ver;
218       memcpy(vmsspec,dl_nam.nam$l_name,dl_nam.nam$b_name);
219       DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsplit filespec: name = %.*s, default = %.*s\n",
220                         dl_nam.nam$b_name,vmsspec,deflen,defspec));
221       /* . . . and go back to expand it */
222       dl_nam.nam$b_nop = 0;
223       dl_fab.fab$l_dna = defspec;
224       dl_fab.fab$b_dns = deflen;
225       dl_fab.fab$b_fns = dl_nam.nam$b_name;
226       sts = sys$parse(&dl_fab);
227       DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tname/default sys$parse = %d\n",sts));
228       if (!(sts & 1)) {
229         dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
230         ST(0) = &PL_sv_undef;
231       }
232       else {
233         /* Now find the actual file */
234         sts = sys$search(&dl_fab);
235         DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$search = %d\n",sts));
236         if (!(sts & 1)) {
237           dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
238           ST(0) = &PL_sv_undef;
239         }
240         else {
241           ST(0) = sv_2mortal(newSVpvn(dl_nam.nam$l_rsa,dl_nam.nam$b_rsl));
242           DLDEBUG(1,PerlIO_printf(Perl_debug_log, "\tresult = \\%.*s\\\n",
243                             dl_nam.nam$b_rsl,dl_nam.nam$l_rsa));
244         }
245       }
246     }
247
248 void
249 dl_load_file(filespec, flags)
250     char *      filespec
251     int         flags
252     PREINIT:
253     dTHX;
254     char vmsspec[NAM$C_MAXRSS];
255     SV *reqSV, **reqSVhndl;
256     STRLEN deflen;
257     struct dsc$descriptor_s
258       specdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
259       symdsc  = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
260     struct fscnlst {
261       unsigned short int len;
262       unsigned short int code;
263       char *string;
264     }  namlst[2] = {{0,FSCN$_NAME,0},{0,0,0}};
265     struct libref *dlptr;
266     vmssts sts, failed = 0;
267     void (*entry)();
268     dMY_CXT;
269     CODE:
270
271     DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filespec,flags));
272     specdsc.dsc$a_pointer = tovmsspec(filespec,vmsspec);
273     specdsc.dsc$w_length = strlen(specdsc.dsc$a_pointer);
274     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tVMS-ified filespec is %s\n",
275                       specdsc.dsc$a_pointer));
276     New(1399,dlptr,1,struct libref);
277     dlptr->name.dsc$b_dtype = dlptr->defspec.dsc$b_dtype = DSC$K_DTYPE_T;
278     dlptr->name.dsc$b_class = dlptr->defspec.dsc$b_class = DSC$K_CLASS_S;
279     sts = sys$filescan(&specdsc,namlst,0);
280     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$filescan: returns %d, name is %.*s\n",
281                       sts,namlst[0].len,namlst[0].string));
282     if (!(sts & 1)) {
283       failed = 1;
284       dl_set_error(sts,0);
285     }
286     else {
287       dlptr->name.dsc$w_length = namlst[0].len;
288       dlptr->name.dsc$a_pointer = savepvn(namlst[0].string,namlst[0].len);
289       dlptr->defspec.dsc$w_length = specdsc.dsc$w_length - namlst[0].len;
290       New(1097, dlptr->defspec.dsc$a_pointer, dlptr->defspec.dsc$w_length + 1, char);
291       deflen = namlst[0].string - specdsc.dsc$a_pointer; 
292       memcpy(dlptr->defspec.dsc$a_pointer,specdsc.dsc$a_pointer,deflen);
293       memcpy(dlptr->defspec.dsc$a_pointer + deflen,
294              namlst[0].string + namlst[0].len,
295              dlptr->defspec.dsc$w_length - deflen);
296       DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlibref = name: %s, defspec: %.*s\n",
297                         dlptr->name.dsc$a_pointer,
298                         dlptr->defspec.dsc$w_length,
299                         dlptr->defspec.dsc$a_pointer));
300       if (!(reqSVhndl = av_fetch(dl_require_symbols,0,FALSE)) || !(reqSV = *reqSVhndl)) {
301         DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t@dl_require_symbols empty, returning untested libref\n"));
302       }
303       else {
304         symdsc.dsc$w_length = SvCUR(reqSV);
305         symdsc.dsc$a_pointer = SvPVX(reqSV);
306         DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t$dl_require_symbols[0] = %.*s\n",
307                           symdsc.dsc$w_length, symdsc.dsc$a_pointer));
308         sts = my_find_image_symbol(&(dlptr->name),&symdsc,
309                                     &entry,&(dlptr->defspec));
310         DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts));
311         if (!(sts&1)) {
312           failed = 1;
313           dl_set_error(sts,0);
314         }
315       }
316     }
317
318     if (failed) {
319       Safefree(dlptr->name.dsc$a_pointer);
320       Safefree(dlptr->defspec.dsc$a_pointer);
321       Safefree(dlptr);
322       ST(0) = &PL_sv_undef;
323     }
324     else {
325       ST(0) = sv_2mortal(newSViv(PTR2IV(dlptr)));
326     }
327
328
329 void
330 dl_find_symbol(librefptr,symname)
331     void *      librefptr
332     SV *        symname
333     CODE:
334     struct libref thislib = *((struct libref *)librefptr);
335     struct dsc$descriptor_s
336       symdsc = {SvCUR(symname),DSC$K_DTYPE_T,DSC$K_CLASS_S,SvPVX(symname)};
337     void (*entry)();
338     vmssts sts;
339
340     DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_find_dymbol(%.*s,%.*s):\n",
341                       thislib.name.dsc$w_length, thislib.name.dsc$a_pointer,
342                       symdsc.dsc$w_length,symdsc.dsc$a_pointer));
343     sts = my_find_image_symbol(&(thislib.name),&symdsc,
344                                &entry,&(thislib.defspec));
345     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts));
346     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tentry point is %d\n",
347                       (unsigned long int) entry));
348     if (!(sts & 1)) {
349       /* error message already saved by findsym_handler */
350       ST(0) = &PL_sv_undef;
351     }
352     else ST(0) = sv_2mortal(newSViv(PTR2IV(entry)));
353
354
355 void
356 dl_undef_symbols()
357     PPCODE:
358
359
360 # These functions should not need changing on any platform:
361
362 void
363 dl_install_xsub(perl_name, symref, filename="$Package")
364     char *      perl_name
365     void *      symref 
366     char *      filename
367     CODE:
368     DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
369         perl_name, symref));
370     ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
371                                       (void(*)(pTHX_ CV *))symref,
372                                       filename)));
373
374
375 char *
376 dl_error()
377     CODE:
378     dMY_CXT;
379     RETVAL = dl_last_error ;
380     OUTPUT:
381       RETVAL
382
383 # end.