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