17 extern ULONG _emx_exception ( EXCEPTIONREPORTRECORD *,
18 EXCEPTIONREGISTRATIONRECORD *,
22 static RXSTRING * strs;
24 static SHVBLOCK * vars;
28 static RXSTRING rxcommand = { 9, "RXCOMMAND" };
29 static RXSTRING rxsubroutine = { 12, "RXSUBROUTINE" };
30 static RXSTRING rxfunction = { 11, "RXFUNCTION" };
32 static ULONG PERLCALL(PSZ name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret);
36 #define Fetch RXSHV_FETCH
37 #define Drop RXSHV_DROPV
39 #define Set RXSHV_SYSET
40 #define Fetch RXSHV_SYFET
41 #define Drop RXSHV_SYDRO
44 static long incompartment;
47 exec_in_REXX(char *cmd, char * handlerName, RexxFunctionHandler *handler)
50 HMODULE hRexx, hRexxAPI;
52 LONG APIENTRY (*pRexxStart) (LONG, PRXSTRING, PSZ, PRXSTRING,
53 PSZ, LONG, PRXSYSEXIT, PSHORT, PRXSTRING);
54 APIRET APIENTRY (*pRexxRegisterFunctionExe) (PSZ,
55 RexxFunctionHandler *);
56 APIRET APIENTRY (*pRexxDeregisterFunction) (PSZ);
64 if (incompartment) die ("Attempt to reenter into REXX compartment");
67 if (DosLoadModule(buf, sizeof buf, "REXX", &hRexx)
68 || DosLoadModule(buf, sizeof buf, "REXXAPI", &hRexxAPI)
69 || DosQueryProcAddr(hRexx, 0, "RexxStart", (PFN *)&pRexxStart)
70 || DosQueryProcAddr(hRexxAPI, 0, "RexxRegisterFunctionExe",
71 (PFN *)&pRexxRegisterFunctionExe)
72 || DosQueryProcAddr(hRexxAPI, 0, "RexxDeregisterFunction",
73 (PFN *)&pRexxDeregisterFunction)) {
74 die("REXX not available\n");
78 pRexxRegisterFunctionExe(handlerName, handler);
80 MAKERXSTRING(args[0], NULL, 0);
81 MAKERXSTRING(inst[0], cmd, strlen(cmd));
82 MAKERXSTRING(inst[1], NULL, 0);
83 MAKERXSTRING(result, NULL, 0);
84 rc = pRexxStart(0, args, "StartPerl", inst, "Perl", RXSUBROUTINE, NULL,
88 pRexxDeregisterFunction("StartPerl");
89 DosFreeModule(hRexxAPI);
91 if (!RXNULLSTRING(result)) {
92 res = newSVpv(RXSTRPTR(result), RXSTRLEN(result));
93 DosFreeMem(RXSTRPTR(result));
97 if (rc || SvTRUE(GvSV(PL_errgv))) {
98 if (SvTRUE(GvSV(PL_errgv))) {
100 die ("Error inside perl function called from REXX compartment.\n%s", SvPV(GvSV(PL_errgv), n_a)) ;
102 die ("REXX compartment returned non-zero status %li", rc);
111 PERLSTART(PSZ name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
113 return PERLCALL(NULL, argc, argv, queue, ret);
116 #define in_rexx_compartment() exec_in_REXX("return StartPerl()\r\n", \
117 "StartPerl", PERLSTART)
118 #define REXX_call(cv) ( exec_cv = (cv), in_rexx_compartment())
119 #define REXX_eval_with(cmd,name,cv) ( exec_cv = (cv), \
120 exec_in_REXX(cmd,name,PERLSTART))
121 #define REXX_eval(cmd) REXX_eval_with(cmd,NULL,NULL)
124 PERLCALL(PSZ name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
126 EXCEPTIONREGISTRATIONRECORD xreg = { NULL, _emx_exception };
133 DosSetExceptionHandler(&xreg);
141 DosUnsetExceptionHandler(&xreg);
148 char **arr = alloca((argc + 1) * sizeof(char *));
150 for (i = 0; i < argc; ++i)
151 arr[ac++] = argv[i].strptr;
154 rc = perl_call_argv(name, G_SCALAR | G_EVAL, arr);
155 } else if (exec_cv) {
159 rc = perl_call_sv(cv, G_SCALAR | G_EVAL);
164 if (rc == 1 && SvOK(TOPs)) {
165 str = SvPVx(POPs, len);
167 if (DosAllocMem((PPVOID)&ret->strptr, len, PAG_READ|PAG_WRITE|PAG_COMMIT)) {
168 DosUnsetExceptionHandler(&xreg);
171 memcpy(ret->strptr, str, len);
172 ret->strlength = len;
180 DosUnsetExceptionHandler(&xreg);
185 DosUnsetExceptionHandler(&xreg);
196 strs = malloc(nstrs * sizeof(RXSTRING));
207 vars = malloc(nvars * sizeof(SHVBLOCK));
216 trace = getenv("PERL_REXX_DEBUG");
223 croak("%s not implemented on this architecture", s);
237 MODULE = OS2::REXX PACKAGE = OS2::REXX
248 _call(name, address, queue="SESSION", ...)
258 RexxFunctionHandler *fcn = address;
262 fprintf(stderr, "REXXCALL::_call name: '%s' args:", name);
263 for (i = 0; i < argc; ++i) {
265 char *ptr = SvPV(ST(3+i), len);
266 MAKERXSTRING(strs[i], ptr, len);
268 fprintf(stderr, " '%.*s'", len, ptr);
273 fprintf(stderr, "\n");
274 MAKERXSTRING(result, resbuf, sizeof resbuf);
275 rc = fcn(name, argc, strs, queue, &result);
277 fprintf(stderr, " rc=%X, result='%.*s'\n", rc,
278 result.strlength, result.strptr);
279 ST(0) = sv_newmortal();
282 sv_setpvn(ST(0), result.strptr, result.strlength);
284 sv_setpvn(ST(0), "", 0);
286 if (result.strptr && result.strptr != resbuf)
287 DosFreeMem(result.strptr);
297 int n = (items + 1) / 2;
301 fprintf(stderr, "REXXCALL::_set");
302 for (i = 0; i < n; ++i) {
303 SHVBLOCK * var = &vars[i];
306 name = SvPV(ST(2*i+0),namelen);
308 value = SvPV(ST(2*i+1),valuelen);
314 var->shvcode = RXSHV_SET;
315 var->shvnext = &vars[i+1];
316 var->shvnamelen = namelen;
317 var->shvvaluelen = valuelen;
318 MAKERXSTRING(var->shvname, name, namelen);
319 MAKERXSTRING(var->shvvalue, value, valuelen);
321 fprintf(stderr, " %.*s='%.*s'",
322 var->shvname.strlength, var->shvname.strptr,
323 var->shvvalue.strlength, var->shvvalue.strptr);
326 fprintf(stderr, "\n");
327 vars[n-1].shvnext = NULL;
328 rc = RexxVariablePool(vars);
330 fprintf(stderr, " rc=%X\n", rc);
331 RETVAL = (rc & ~RXSHV_NEWV) ? FALSE : TRUE;
346 fprintf(stderr, "REXXCALL::_fetch");
347 for (i = 0; i < items; ++i) {
348 SHVBLOCK * var = &vars[i];
350 name = SvPV(ST(i),namelen);
351 var->shvcode = RXSHV_FETCH;
352 var->shvnext = &vars[i+1];
353 var->shvnamelen = namelen;
354 var->shvvaluelen = 0;
355 MAKERXSTRING(var->shvname, name, namelen);
356 MAKERXSTRING(var->shvvalue, NULL, 0);
358 fprintf(stderr, " '%s'", name);
361 fprintf(stderr, "\n");
362 vars[items-1].shvnext = NULL;
363 rc = RexxVariablePool(vars);
364 if (!(rc & ~RXSHV_NEWV)) {
365 for (i = 0; i < items; ++i) {
367 SHVBLOCK * var = &vars[i];
368 /* returned lengths appear to be swapped */
369 /* but beware of "future bug fixes" */
370 namelen = var->shvvalue.strlength; /* should be */
371 if (var->shvvaluelen < var->shvvalue.strlength)
372 namelen = var->shvvaluelen; /* is */
374 fprintf(stderr, " %.*s='%.*s'\n",
375 var->shvname.strlength, var->shvname.strptr,
376 namelen, var->shvvalue.strptr);
377 if (var->shvret & RXSHV_NEWV || !var->shvvalue.strptr)
380 PUSHs(sv_2mortal(newSVpv(var->shvvalue.strptr,
385 fprintf(stderr, " rc=%X\n", rc);
397 int len = strlen(stem), namelen, valuelen;
399 fprintf(stderr, "REXXCALL::_next stem='%s'\n", stem);
400 sv.shvcode = RXSHV_NEXTV;
402 MAKERXSTRING(sv.shvvalue, NULL, 0);
404 sv.shvnamelen = sizeof name;
406 MAKERXSTRING(sv.shvname, name, sizeof name);
407 if (sv.shvvalue.strptr) {
408 DosFreeMem(sv.shvvalue.strptr);
409 MAKERXSTRING(sv.shvvalue, NULL, 0);
411 rc = RexxVariablePool(&sv);
412 } while (!rc && memcmp(stem, sv.shvname.strptr, len) != 0);
415 /* returned lengths appear to be swapped */
416 /* but beware of "future bug fixes" */
417 namelen = sv.shvname.strlength; /* should be */
418 if (sv.shvnamelen < sv.shvname.strlength)
419 namelen = sv.shvnamelen; /* is */
420 valuelen = sv.shvvalue.strlength; /* should be */
421 if (sv.shvvaluelen < sv.shvvalue.strlength)
422 valuelen = sv.shvvaluelen; /* is */
424 fprintf(stderr, " %.*s='%.*s'\n",
425 namelen, sv.shvname.strptr,
426 valuelen, sv.shvvalue.strptr);
427 PUSHs(sv_2mortal(newSVpv(sv.shvname.strptr+len, namelen-len)));
428 if (sv.shvvalue.strptr) {
429 PUSHs(sv_2mortal(newSVpv(sv.shvvalue.strptr, valuelen)));
430 DosFreeMem(sv.shvvalue.strptr);
433 } else if (rc != RXSHV_LVAR) {
434 die("Error %i when in _next", rc);
437 fprintf(stderr, " rc=%X\n", rc);
448 for (i = 0; i < items; ++i) {
449 SHVBLOCK * var = &vars[i];
451 name = SvPV(ST(i),namelen);
452 var->shvcode = RXSHV_DROPV;
453 var->shvnext = &vars[i+1];
454 var->shvnamelen = namelen;
455 var->shvvaluelen = 0;
456 MAKERXSTRING(var->shvname, name, var->shvnamelen);
457 MAKERXSTRING(var->shvvalue, NULL, 0);
459 vars[items-1].shvnext = NULL;
460 RETVAL = (RexxVariablePool(vars) & ~RXSHV_NEWV) ? FALSE : TRUE;
469 RETVAL = RexxRegisterFunctionExe(name, PERLCALL);
483 REXX_eval_with(cmd,name,cv)