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(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ 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(pTHX_ char *cmd, char * handlerName, RexxFunctionHandler *handler)
49 HMODULE hRexx, hRexxAPI;
51 LONG APIENTRY (*pRexxStart) (LONG, PRXSTRING, PSZ, PRXSTRING,
52 PSZ, LONG, PRXSYSEXIT, PSHORT, PRXSTRING);
53 APIRET APIENTRY (*pRexxRegisterFunctionExe) (PSZ,
54 RexxFunctionHandler *);
55 APIRET APIENTRY (*pRexxDeregisterFunction) (PSZ);
64 Perl_die(aTHX_ "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 Perl_die(aTHX_ "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 Perl_die(aTHX_ "Error inside perl function called from REXX compartment.\n%s", SvPV(GvSV(PL_errgv), n_a)) ;
102 Perl_die(aTHX_ "REXX compartment returned non-zero status %li", rc);
111 PERLSTART(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret)
113 return PERLCALL(NULL, argc, argv, queue, ret);
116 #define in_rexx_compartment() exec_in_REXX(aTHX_ "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(aTHX_ cmd,name,PERLSTART))
121 #define REXX_eval(cmd) REXX_eval_with(cmd,NULL,NULL)
124 PERLCALL(PCSZ name, ULONG argc, PRXSTRING argv, PCSZ queue, PRXSTRING ret)
127 EXCEPTIONREGISTRATIONRECORD xreg = { NULL, _emx_exception };
134 DosSetExceptionHandler(&xreg);
142 DosUnsetExceptionHandler(&xreg);
149 char **arr = alloca((argc + 1) * sizeof(char *));
151 for (i = 0; i < argc; ++i)
152 arr[ac++] = argv[i].strptr;
155 rc = perl_call_argv(name, G_SCALAR | G_EVAL, arr);
156 } else if (exec_cv) {
160 rc = perl_call_sv(cv, G_SCALAR | G_EVAL);
165 if (rc == 1 && SvOK(TOPs)) {
166 str = SvPVx(POPs, len);
168 if (DosAllocMem((PPVOID)&ret->strptr, len, PAG_READ|PAG_WRITE|PAG_COMMIT)) {
169 DosUnsetExceptionHandler(&xreg);
172 memcpy(ret->strptr, str, len);
173 ret->strlength = len;
181 DosUnsetExceptionHandler(&xreg);
186 DosUnsetExceptionHandler(&xreg);
197 strs = malloc(nstrs * sizeof(RXSTRING));
208 vars = malloc(nvars * sizeof(SHVBLOCK));
217 trace = getenv("PERL_REXX_DEBUG");
221 constant(char *name, int arg)
228 MODULE = OS2::REXX PACKAGE = OS2::REXX
245 int n = (items + 1) / 2;
249 fprintf(stderr, "REXXCALL::_set");
250 for (i = 0; i < n; ++i) {
251 SHVBLOCK * var = &vars[i];
254 name = SvPV(ST(2*i+0),namelen);
256 value = SvPV(ST(2*i+1),valuelen);
262 var->shvcode = RXSHV_SET;
263 var->shvnext = &vars[i+1];
264 var->shvnamelen = namelen;
265 var->shvvaluelen = valuelen;
266 MAKERXSTRING(var->shvname, name, namelen);
267 MAKERXSTRING(var->shvvalue, value, valuelen);
269 fprintf(stderr, " %.*s='%.*s'",
270 var->shvname.strlength, var->shvname.strptr,
271 var->shvvalue.strlength, var->shvvalue.strptr);
274 fprintf(stderr, "\n");
275 vars[n-1].shvnext = NULL;
276 rc = RexxVariablePool(vars);
278 fprintf(stderr, " rc=%X\n", rc);
279 RETVAL = (rc & ~RXSHV_NEWV) ? FALSE : TRUE;
294 fprintf(stderr, "REXXCALL::_fetch");
295 for (i = 0; i < items; ++i) {
296 SHVBLOCK * var = &vars[i];
298 name = SvPV(ST(i),namelen);
299 var->shvcode = RXSHV_FETCH;
300 var->shvnext = &vars[i+1];
301 var->shvnamelen = namelen;
302 var->shvvaluelen = 0;
303 MAKERXSTRING(var->shvname, name, namelen);
304 MAKERXSTRING(var->shvvalue, NULL, 0);
306 fprintf(stderr, " '%s'", name);
309 fprintf(stderr, "\n");
310 vars[items-1].shvnext = NULL;
311 rc = RexxVariablePool(vars);
312 if (!(rc & ~RXSHV_NEWV)) {
313 for (i = 0; i < items; ++i) {
315 SHVBLOCK * var = &vars[i];
316 /* returned lengths appear to be swapped */
317 /* but beware of "future bug fixes" */
318 namelen = var->shvvalue.strlength; /* should be */
319 if (var->shvvaluelen < var->shvvalue.strlength)
320 namelen = var->shvvaluelen; /* is */
322 fprintf(stderr, " %.*s='%.*s'\n",
323 var->shvname.strlength, var->shvname.strptr,
324 namelen, var->shvvalue.strptr);
325 if (var->shvret & RXSHV_NEWV || !var->shvvalue.strptr)
328 PUSHs(sv_2mortal(newSVpv(var->shvvalue.strptr,
333 fprintf(stderr, " rc=%X\n", rc);
345 int len = strlen(stem), namelen, valuelen;
347 fprintf(stderr, "REXXCALL::_next stem='%s'\n", stem);
348 sv.shvcode = RXSHV_NEXTV;
350 MAKERXSTRING(sv.shvvalue, NULL, 0);
352 sv.shvnamelen = sizeof name;
354 MAKERXSTRING(sv.shvname, name, sizeof name);
355 if (sv.shvvalue.strptr) {
356 DosFreeMem(sv.shvvalue.strptr);
357 MAKERXSTRING(sv.shvvalue, NULL, 0);
359 rc = RexxVariablePool(&sv);
360 } while (!rc && memcmp(stem, sv.shvname.strptr, len) != 0);
363 /* returned lengths appear to be swapped */
364 /* but beware of "future bug fixes" */
365 namelen = sv.shvname.strlength; /* should be */
366 if (sv.shvnamelen < sv.shvname.strlength)
367 namelen = sv.shvnamelen; /* is */
368 valuelen = sv.shvvalue.strlength; /* should be */
369 if (sv.shvvaluelen < sv.shvvalue.strlength)
370 valuelen = sv.shvvaluelen; /* is */
372 fprintf(stderr, " %.*s='%.*s'\n",
373 namelen, sv.shvname.strptr,
374 valuelen, sv.shvvalue.strptr);
375 PUSHs(sv_2mortal(newSVpv(sv.shvname.strptr+len, namelen-len)));
376 if (sv.shvvalue.strptr) {
377 PUSHs(sv_2mortal(newSVpv(sv.shvvalue.strptr, valuelen)));
378 DosFreeMem(sv.shvvalue.strptr);
381 } else if (rc != RXSHV_LVAR) {
382 die("Error %i when in _next", rc);
385 fprintf(stderr, " rc=%X\n", rc);
396 for (i = 0; i < items; ++i) {
397 SHVBLOCK * var = &vars[i];
399 name = SvPV(ST(i),namelen);
400 var->shvcode = RXSHV_DROPV;
401 var->shvnext = &vars[i+1];
402 var->shvnamelen = namelen;
403 var->shvvaluelen = 0;
404 MAKERXSTRING(var->shvname, name, var->shvnamelen);
405 MAKERXSTRING(var->shvvalue, NULL, 0);
407 vars[items-1].shvnext = NULL;
408 RETVAL = (RexxVariablePool(vars) & ~RXSHV_NEWV) ? FALSE : TRUE;
417 RETVAL = RexxRegisterFunctionExe(name, PERLCALL);
431 REXX_eval_with(cmd,name,cv)