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(errgv))) {
98 if (SvTRUE(GvSV(errgv))) {
99 die ("Error inside perl function called from REXX compartment.\n%s", SvPV(GvSV(errgv), na)) ;
101 die ("REXX compartment returned non-zero status %li", rc);
110 PERLSTART(PSZ name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
112 return PERLCALL(NULL, argc, argv, queue, ret);
115 #define in_rexx_compartment() exec_in_REXX("return StartPerl()\r\n", \
116 "StartPerl", PERLSTART)
117 #define REXX_call(cv) ( exec_cv = (cv), in_rexx_compartment())
118 #define REXX_eval_with(cmd,name,cv) ( exec_cv = (cv), \
119 exec_in_REXX(cmd,name,PERLSTART))
120 #define REXX_eval(cmd) REXX_eval_with(cmd,NULL,NULL)
123 PERLCALL(PSZ name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
125 EXCEPTIONREGISTRATIONRECORD xreg = { NULL, _emx_exception };
132 DosSetExceptionHandler(&xreg);
140 DosUnsetExceptionHandler(&xreg);
147 char **arr = alloca((argc + 1) * sizeof(char *));
149 for (i = 0; i < argc; ++i)
150 arr[ac++] = argv[i].strptr;
153 rc = perl_call_argv(name, G_SCALAR | G_EVAL, arr);
154 } else if (exec_cv) {
158 rc = perl_call_sv(cv, G_SCALAR | G_EVAL);
163 if (rc == 1 && SvOK(TOPs)) {
164 str = SvPVx(POPs, len);
166 if (DosAllocMem((PPVOID)&ret->strptr, len, PAG_READ|PAG_WRITE|PAG_COMMIT)) {
167 DosUnsetExceptionHandler(&xreg);
170 memcpy(ret->strptr, str, len);
171 ret->strlength = len;
179 DosUnsetExceptionHandler(&xreg);
184 DosUnsetExceptionHandler(&xreg);
195 strs = malloc(nstrs * sizeof(RXSTRING));
206 vars = malloc(nvars * sizeof(SHVBLOCK));
215 trace = getenv("PERL_REXX_DEBUG");
222 croak("%s not implemented on this architecture", s);
236 MODULE = OS2::REXX PACKAGE = OS2::REXX
247 _call(name, address, queue="SESSION", ...)
257 RexxFunctionHandler *fcn = address;
261 fprintf(stderr, "REXXCALL::_call name: '%s' args:", name);
262 for (i = 0; i < argc; ++i) {
264 char *ptr = SvPV(ST(3+i), len);
265 MAKERXSTRING(strs[i], ptr, len);
267 fprintf(stderr, " '%.*s'", len, ptr);
272 fprintf(stderr, "\n");
273 MAKERXSTRING(result, resbuf, sizeof resbuf);
274 rc = fcn(name, argc, strs, queue, &result);
276 fprintf(stderr, " rc=%X, result='%.*s'\n", rc,
277 result.strlength, result.strptr);
278 ST(0) = sv_newmortal();
281 sv_setpvn(ST(0), result.strptr, result.strlength);
283 sv_setpvn(ST(0), "", 0);
285 if (result.strptr && result.strptr != resbuf)
286 DosFreeMem(result.strptr);
296 int n = (items + 1) / 2;
300 fprintf(stderr, "REXXCALL::_set");
301 for (i = 0; i < n; ++i) {
302 SHVBLOCK * var = &vars[i];
305 name = SvPV(ST(2*i+0),namelen);
307 value = SvPV(ST(2*i+1),valuelen);
313 var->shvcode = RXSHV_SET;
314 var->shvnext = &vars[i+1];
315 var->shvnamelen = namelen;
316 var->shvvaluelen = valuelen;
317 MAKERXSTRING(var->shvname, name, namelen);
318 MAKERXSTRING(var->shvvalue, value, valuelen);
320 fprintf(stderr, " %.*s='%.*s'",
321 var->shvname.strlength, var->shvname.strptr,
322 var->shvvalue.strlength, var->shvvalue.strptr);
325 fprintf(stderr, "\n");
326 vars[n-1].shvnext = NULL;
327 rc = RexxVariablePool(vars);
329 fprintf(stderr, " rc=%X\n", rc);
330 RETVAL = (rc & ~RXSHV_NEWV) ? FALSE : TRUE;
345 fprintf(stderr, "REXXCALL::_fetch");
346 for (i = 0; i < items; ++i) {
347 SHVBLOCK * var = &vars[i];
349 name = SvPV(ST(i),namelen);
350 var->shvcode = RXSHV_FETCH;
351 var->shvnext = &vars[i+1];
352 var->shvnamelen = namelen;
353 var->shvvaluelen = 0;
354 MAKERXSTRING(var->shvname, name, namelen);
355 MAKERXSTRING(var->shvvalue, NULL, 0);
357 fprintf(stderr, " '%s'", name);
360 fprintf(stderr, "\n");
361 vars[items-1].shvnext = NULL;
362 rc = RexxVariablePool(vars);
363 if (!(rc & ~RXSHV_NEWV)) {
364 for (i = 0; i < items; ++i) {
366 SHVBLOCK * var = &vars[i];
367 /* returned lengths appear to be swapped */
368 /* but beware of "future bug fixes" */
369 namelen = var->shvvalue.strlength; /* should be */
370 if (var->shvvaluelen < var->shvvalue.strlength)
371 namelen = var->shvvaluelen; /* is */
373 fprintf(stderr, " %.*s='%.*s'\n",
374 var->shvname.strlength, var->shvname.strptr,
375 namelen, var->shvvalue.strptr);
376 if (var->shvret & RXSHV_NEWV || !var->shvvalue.strptr)
379 PUSHs(sv_2mortal(newSVpv(var->shvvalue.strptr,
384 fprintf(stderr, " rc=%X\n", rc);
396 int len = strlen(stem), namelen, valuelen;
398 fprintf(stderr, "REXXCALL::_next stem='%s'\n", stem);
399 sv.shvcode = RXSHV_NEXTV;
401 MAKERXSTRING(sv.shvvalue, NULL, 0);
403 sv.shvnamelen = sizeof name;
405 MAKERXSTRING(sv.shvname, name, sizeof name);
406 if (sv.shvvalue.strptr) {
407 DosFreeMem(sv.shvvalue.strptr);
408 MAKERXSTRING(sv.shvvalue, NULL, 0);
410 rc = RexxVariablePool(&sv);
411 } while (!rc && memcmp(stem, sv.shvname.strptr, len) != 0);
414 /* returned lengths appear to be swapped */
415 /* but beware of "future bug fixes" */
416 namelen = sv.shvname.strlength; /* should be */
417 if (sv.shvnamelen < sv.shvname.strlength)
418 namelen = sv.shvnamelen; /* is */
419 valuelen = sv.shvvalue.strlength; /* should be */
420 if (sv.shvvaluelen < sv.shvvalue.strlength)
421 valuelen = sv.shvvaluelen; /* is */
423 fprintf(stderr, " %.*s='%.*s'\n",
424 namelen, sv.shvname.strptr,
425 valuelen, sv.shvvalue.strptr);
426 PUSHs(sv_2mortal(newSVpv(sv.shvname.strptr+len, namelen-len)));
427 if (sv.shvvalue.strptr) {
428 PUSHs(sv_2mortal(newSVpv(sv.shvvalue.strptr, valuelen)));
429 DosFreeMem(sv.shvvalue.strptr);
432 } else if (rc != RXSHV_LVAR) {
433 die("Error %i when in _next", rc);
436 fprintf(stderr, " rc=%X\n", rc);
447 for (i = 0; i < items; ++i) {
448 SHVBLOCK * var = &vars[i];
450 name = SvPV(ST(i),namelen);
451 var->shvcode = RXSHV_DROPV;
452 var->shvnext = &vars[i+1];
453 var->shvnamelen = namelen;
454 var->shvvaluelen = 0;
455 MAKERXSTRING(var->shvname, name, var->shvnamelen);
456 MAKERXSTRING(var->shvvalue, NULL, 0);
458 vars[items-1].shvnext = NULL;
459 RETVAL = (RexxVariablePool(vars) & ~RXSHV_NEWV) ? FALSE : TRUE;
468 RETVAL = RexxRegisterFunctionExe(name, PERLCALL);
482 REXX_eval_with(cmd,name,cv)