17 extern ULONG _emx_exception ( EXCEPTIONREPORTRECORD *,
18 EXCEPTIONREGISTRATIONRECORD *,
22 static RXSTRING * strs;
24 static SHVBLOCK * vars;
29 static RXSTRING rxcommand = { 9, "RXCOMMAND" };
30 static RXSTRING rxsubroutine = { 12, "RXSUBROUTINE" };
31 static RXSTRING rxfunction = { 11, "RXFUNCTION" };
34 static ULONG PERLCALL(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret);
35 static ULONG PERLCALLcv(PCSZ name, SV *cv, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret);
36 static ULONG PERLSTART(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret);
37 static RexxSubcomHandler SubCommandPerlEval;
41 #define Fetch RXSHV_FETCH
42 #define Drop RXSHV_DROPV
44 #define Set RXSHV_SYSET
45 #define Fetch RXSHV_SYFET
46 #define Drop RXSHV_SYDRO
49 static long incompartment; /* May be used to unload the REXX */
51 static LONG APIENTRY (*pRexxStart) (LONG, PRXSTRING, PSZ, PRXSTRING,
52 PSZ, LONG, PRXSYSEXIT, PSHORT, PRXSTRING);
53 static APIRET APIENTRY (*pRexxRegisterFunctionExe) (PSZ,
54 RexxFunctionHandler *);
55 static APIRET APIENTRY (*pRexxDeregisterFunction) (PSZ);
57 static ULONG (*pRexxVariablePool) (PSHVBLOCK pRequest);
61 /* Create a REXX compartment,
62 register `n' callbacks `handlers' with the REXX names `handlerNames',
63 evaluate the REXX expression `cmd'.
66 exec_in_REXX_with(pTHX_ char *cmd, int c, char **handlerNames, RexxFunctionHandler **handlers)
80 Newz(728, subs, c, char);
82 rc = pRexxRegisterFunctionExe(handlerNames[n], handlers[n]);
83 if (rc == RXFUNC_DEFINED)
87 MAKERXSTRING(args[0], NULL, 0);
88 MAKERXSTRING(inst[0], cmd, strlen(cmd));
89 MAKERXSTRING(inst[1], NULL, 0);
90 MAKERXSTRING(result, NULL, 0);
91 rc = pRexxStart(0, args, /* No arguments */
92 "REXX_in_Perl", /* Returned on REXX' PARSE SOURCE,
93 and the "macrospace function name" */
94 inst, /* inst[0] - the code to execute,
95 inst[1] will contain tokens. */
96 "Perl", /* Pass string-cmds to this callback */
97 RXSUBROUTINE, /* Many arguments, maybe result */
98 NULL, /* No callbacks/exits to register */
105 pRexxDeregisterFunction(handlerNames[n]);
108 #if 0 /* Do we want to restore these? */
109 DosFreeModule(hRexxAPI);
110 DosFreeModule(hRexx);
113 if (RXSTRPTR(inst[1])) /* Free the tokenized version */
114 DosFreeMem(RXSTRPTR(inst[1]));
115 if (!RXNULLSTRING(result)) {
116 res = newSVpv(RXSTRPTR(result), RXSTRLEN(result));
117 DosFreeMem(RXSTRPTR(result));
121 if (rc || SvTRUE(GvSV(PL_errgv))) {
122 if (SvTRUE(GvSV(PL_errgv))) {
124 Perl_croak(aTHX_ "Error inside perl function called from REXX compartment:\n%s", SvPV(GvSV(PL_errgv), n_a)) ;
126 Perl_croak(aTHX_ "REXX compartment returned non-zero status %li", rc);
132 /* Call the Perl function given by name, or if name=0, by cv,
133 with the given arguments. Return the stringified result to REXX. */
135 PERLCALLcv(PCSZ name, SV *cv, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret)
138 EXCEPTIONREGISTRATIONRECORD xreg = { NULL, _emx_exception };
145 DosSetExceptionHandler(&xreg);
153 DosUnsetExceptionHandler(&xreg);
158 for (i = 0; i < argc; ++i)
159 XPUSHs(sv_2mortal(newSVpvn(argv[i].strptr, argv[i].strlength)));
162 rc = perl_call_pv(name, G_SCALAR | G_EVAL);
164 rc = perl_call_sv(cv, G_SCALAR | G_EVAL);
170 if (rc == 1) /* must be! */
172 if (rc == 1 && SvOK(res)) {
173 str = SvPVx(res, len);
174 if (len <= 256 /* Default buffer is 256-char long */
175 || !CheckOSError(DosAllocMem((PPVOID)&ret->strptr, len,
176 PAG_READ|PAG_WRITE|PAG_COMMIT))) {
177 memcpy(ret->strptr, str, len);
178 ret->strlength = len;
188 DosUnsetExceptionHandler(&xreg);
189 return rc == 1 ? 0 : 1; /* 0 means SUCCESS */
193 PERLSTART(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret)
198 return PERLCALLcv(NULL, cv, argc, argv, queue, ret);
202 PERLCALL(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret)
204 return PERLCALLcv(name, Nullsv, argc, argv, queue, ret);
207 RexxFunctionHandler* PF = &PERLSTART;
208 char* PF_name = "StartPerl";
210 #define REXX_eval_with(cmd,name,cv) \
211 ( exec_cv = cv, exec_in_REXX_with(aTHX_ (cmd),1, &(name), &PF))
212 #define REXX_call(cv) REXX_eval_with("return StartPerl()\r\n", PF_name, (cv))
213 #define REXX_eval(cmd) ( exec_in_REXX_with(aTHX_ (cmd), 0, NULL, NULL))
217 PRXSTRING command, /* command to issue */
218 PUSHORT flags, /* error/failure flags */
219 PRXSTRING retstr ) /* return code */
231 in = sv_2mortal(newSVpvn(command->strptr, command->strlength));
232 eval_sv(in, G_SCALAR);
239 *flags = RXSUBCOM_ERROR; /* raise error condition */
240 str = SvPV(ERRSV, len);
241 } else if (!SvOK(res)) {
242 *flags = RXSUBCOM_ERROR; /* raise error condition */
243 str = "undefined value returned by Perl-in-REXX";
246 str = SvPV(res, len);
247 if (len <= 256 /* Default buffer is 256-char long */
248 || !DosAllocMem((PPVOID)&retstr->strptr, len,
249 PAG_READ|PAG_WRITE|PAG_COMMIT)) {
250 memcpy(retstr->strptr, str, len);
251 retstr->strlength = len;
253 *flags = RXSUBCOM_ERROR; /* raise error condition */
254 strcpy(retstr->strptr, "Not enough memory for the return string of Perl-in-REXX");
255 retstr->strlength = strlen(retstr->strptr);
261 return 0; /* finished */
271 strs = malloc(nstrs * sizeof(RXSTRING));
282 vars = malloc(nvars * sizeof(SHVBLOCK));
290 *(PFN *)&pRexxStart = loadByOrdinal(ORD_RexxStart, 1);
291 *(PFN *)&pRexxRegisterFunctionExe
292 = loadByOrdinal(ORD_RexxRegisterFunctionExe, 1);
293 *(PFN *)&pRexxDeregisterFunction
294 = loadByOrdinal(ORD_RexxDeregisterFunction, 1);
295 *(PFN *)&pRexxVariablePool = loadByOrdinal(ORD_RexxVariablePool, 1);
298 trace = getenv("PERL_REXX_DEBUG");
300 rc = RexxRegisterSubcomExe("PERLEVAL", (PFN)&SubCommandPerlEval, NULL);
304 constant(char *name, int arg)
311 MODULE = OS2::REXX PACKAGE = OS2::REXX
328 int n = (items + 1) / 2;
332 fprintf(stderr, "REXXCALL::_set");
333 for (i = 0; i < n; ++i) {
334 SHVBLOCK * var = &vars[i];
337 name = SvPV(ST(2*i+0),namelen);
339 value = SvPV(ST(2*i+1),valuelen);
345 var->shvcode = RXSHV_SET;
346 var->shvnext = &vars[i+1];
347 var->shvnamelen = namelen;
348 var->shvvaluelen = valuelen;
349 MAKERXSTRING(var->shvname, name, namelen);
350 MAKERXSTRING(var->shvvalue, value, valuelen);
352 fprintf(stderr, " %.*s='%.*s'",
353 (int)var->shvname.strlength, var->shvname.strptr,
354 (int)var->shvvalue.strlength, var->shvvalue.strptr);
357 fprintf(stderr, "\n");
358 vars[n-1].shvnext = NULL;
359 rc = pRexxVariablePool(vars);
361 fprintf(stderr, " rc=%#lX\n", rc);
362 RETVAL = (rc & ~RXSHV_NEWV) ? FALSE : TRUE;
377 fprintf(stderr, "REXXCALL::_fetch");
378 for (i = 0; i < items; ++i) {
379 SHVBLOCK * var = &vars[i];
381 name = SvPV(ST(i),namelen);
382 var->shvcode = RXSHV_FETCH;
383 var->shvnext = &vars[i+1];
384 var->shvnamelen = namelen;
385 var->shvvaluelen = 0;
386 MAKERXSTRING(var->shvname, name, namelen);
387 MAKERXSTRING(var->shvvalue, NULL, 0);
389 fprintf(stderr, " '%s'", name);
392 fprintf(stderr, "\n");
393 vars[items-1].shvnext = NULL;
394 rc = pRexxVariablePool(vars);
395 if (!(rc & ~RXSHV_NEWV)) {
396 for (i = 0; i < items; ++i) {
398 SHVBLOCK * var = &vars[i];
399 /* returned lengths appear to be swapped */
400 /* but beware of "future bug fixes" */
401 namelen = var->shvvalue.strlength; /* should be */
402 if (var->shvvaluelen < var->shvvalue.strlength)
403 namelen = var->shvvaluelen; /* is */
405 fprintf(stderr, " %.*s='%.*s'\n",
406 (int)var->shvname.strlength, var->shvname.strptr,
407 namelen, var->shvvalue.strptr);
408 if (var->shvret & RXSHV_NEWV || !var->shvvalue.strptr)
411 PUSHs(sv_2mortal(newSVpv(var->shvvalue.strptr,
416 fprintf(stderr, " rc=%#lX\n", rc);
428 int len = strlen(stem), namelen, valuelen;
430 fprintf(stderr, "REXXCALL::_next stem='%s'\n", stem);
431 sv.shvcode = RXSHV_NEXTV;
433 MAKERXSTRING(sv.shvvalue, NULL, 0);
435 sv.shvnamelen = sizeof name;
437 MAKERXSTRING(sv.shvname, name, sizeof name);
438 if (sv.shvvalue.strptr) {
439 DosFreeMem(sv.shvvalue.strptr);
440 MAKERXSTRING(sv.shvvalue, NULL, 0);
442 rc = pRexxVariablePool(&sv);
443 } while (!rc && memcmp(stem, sv.shvname.strptr, len) != 0);
446 /* returned lengths appear to be swapped */
447 /* but beware of "future bug fixes" */
448 namelen = sv.shvname.strlength; /* should be */
449 if (sv.shvnamelen < sv.shvname.strlength)
450 namelen = sv.shvnamelen; /* is */
451 valuelen = sv.shvvalue.strlength; /* should be */
452 if (sv.shvvaluelen < sv.shvvalue.strlength)
453 valuelen = sv.shvvaluelen; /* is */
455 fprintf(stderr, " %.*s='%.*s'\n",
456 namelen, sv.shvname.strptr,
457 valuelen, sv.shvvalue.strptr);
458 PUSHs(sv_2mortal(newSVpv(sv.shvname.strptr+len, namelen-len)));
459 if (sv.shvvalue.strptr) {
460 PUSHs(sv_2mortal(newSVpv(sv.shvvalue.strptr, valuelen)));
461 DosFreeMem(sv.shvvalue.strptr);
464 } else if (rc != RXSHV_LVAR) {
465 die("Error %i when in _next", rc);
468 fprintf(stderr, " rc=%#lX\n", rc);
479 for (i = 0; i < items; ++i) {
480 SHVBLOCK * var = &vars[i];
482 name = SvPV(ST(i),namelen);
483 var->shvcode = RXSHV_DROPV;
484 var->shvnext = &vars[i+1];
485 var->shvnamelen = namelen;
486 var->shvvaluelen = 0;
487 MAKERXSTRING(var->shvname, name, var->shvnamelen);
488 MAKERXSTRING(var->shvvalue, NULL, 0);
490 vars[items-1].shvnext = NULL;
491 RETVAL = (pRexxVariablePool(vars) & ~RXSHV_NEWV) ? FALSE : TRUE;
500 RETVAL = pRexxRegisterFunctionExe(name, PERLCALL);
514 REXX_eval_with(cmd,name,cv)
519 #ifdef THIS_IS_NOT_FINISHED
522 _REXX_eval_with(cmd,...)
526 int n = (items - 1)/2;
530 if ((items % 2) == 0)
531 Perl_croak(aTHX_ "Name/values should come in pairs in REXX_eval_with()");
532 New(730, names, n, char*);
533 New(730, cvs, n, SV*);
534 /* XXX Unfinished... */