perl 5.002
[p5sagit/p5-mst-13.2.git] / os2 / os2.c
index fa41298..9b88b7f 100644 (file)
--- a/os2/os2.c
+++ b/os2/os2.c
@@ -1,5 +1,9 @@
 #define INCL_DOS
 #define INCL_NOPM
+#ifndef NO_SYS_ALLOC 
+#  define INCL_DOSMEMMGR
+#  define INCL_DOSERRORS
+#endif /* ! defined NO_SYS_ALLOC */
 #include <os2.h>
 
 /*
@@ -41,12 +45,14 @@ result(int flag, int pid)
        Signal_t (*ihand)();     /* place to save signal during system() */
        Signal_t (*qhand)();     /* place to save signal during system() */
 
-       if (pid < 0 || flag != 0)
+       if (pid < 0 || flag != 0) 
                return pid;
 
        ihand = signal(SIGINT, SIG_IGN);
        qhand = signal(SIGQUIT, SIG_IGN);
-       r = waitpid(pid, &status, 0);
+       do {
+           r = wait4pid(pid, &status, 0);
+       } while (r == -1 && errno == EINTR);
        signal(SIGINT, ihand);
        signal(SIGQUIT, qhand);
 
@@ -88,6 +94,8 @@ register SV **sp;
        if (flag == P_WAIT)
                flag = P_NOWAIT;
 
+       if (*Argv[0] != '/' && *Argv[0] != '\\') /* will swawnvp use PATH? */
+           TAINT_ENV();        /* testing IFS here is overkill, probably */
        if (really && *(tmps = SvPV(really, na)))
            rc = result(trueflag, spawnvp(flag,tmps,Argv));
        else
@@ -95,6 +103,7 @@ register SV **sp;
 
        if (rc < 0 && dowarn)
            warn("Can't spawn \"%s\": %s", Argv[0], Strerror(errno));
+       if (rc < 0) rc = 255 << 8; /* Emulate the fork(). */
     } else
        rc = -1;
     do_execfree();
@@ -111,30 +120,56 @@ char *cmd;
     char *shell, *copt;
     int rc;
 
-    if ((shell = getenv("SHELL")) != NULL)
+#ifdef TRYSHELL
+    if ((shell = getenv("EMXSHELL")) != NULL)
+       copt = "-c";
+    else if ((shell = getenv("SHELL")) != NULL)
        copt = "-c";
     else if ((shell = getenv("COMSPEC")) != NULL)
        copt = "/C";
     else
        shell = "cmd.exe";
+#else
+    /* Consensus on perl5-porters is that it is _very_ important to
+       have a shell which will not change between computers with the
+       same architecture, to avoid "action on a distance". 
+       And to have simple build, this shell should be sh. */
+    shell = "sh.exe";
+    copt = "-c";
+#endif 
+
+    while (*cmd && isSPACE(*cmd))
+       cmd++;
 
     /* save an extra exec if possible */
     /* see if there are shell metacharacters in it */
 
-    /*SUPPRESS 530*/
-    if (*cmd == '@') {
-       ++cmd;
-       goto shell_cmd;
-    }
+    if (*cmd == '.' && isSPACE(cmd[1]))
+       goto doshell;
+
+    if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
+       goto doshell;
+
+    for (s = cmd; *s && isALPHA(*s); s++) ;    /* catch VAR=val gizmo */
+    if (*s == '=')
+       goto doshell;
+
     for (s = cmd; *s; s++) {
-       if (*s != ' ' && !isALPHA(*s) && strchr("%&|<>\n",*s)) {
+       if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
            if (*s == '\n' && !s[1]) {
                *s = '\0';
                break;
            }
-shell_cmd:  return result(P_WAIT, spawnl(P_NOWAIT,shell,shell,copt,cmd,(char*)0));
+         doshell:
+           rc = result(P_WAIT,
+                         spawnl(P_NOWAIT,shell,shell,copt,cmd,(char*)0));
+           if (rc < 0 && dowarn)
+               warn("Can't spawn \"%s\": %s", shell, Strerror(errno));
+           if (rc < 0) rc = 255 << 8; /* Emulate the fork(). */
+           return rc;
        }
     }
+
     New(402,Argv, (s - cmd) / 2 + 2, char*);
     Cmd = savepvn(cmd, s-cmd);
     a = Argv;
@@ -151,12 +186,27 @@ shell_cmd:  return result(P_WAIT, spawnl(P_NOWAIT,shell,shell,copt,cmd,(char*)0)
        rc = result(P_WAIT, spawnvp(P_NOWAIT,Argv[0],Argv));
        if (rc < 0 && dowarn)
            warn("Can't spawn \"%s\": %s", Argv[0], Strerror(errno));
+       if (rc < 0) rc = 255 << 8; /* Emulate the fork(). */
     } else
        rc = -1;
     do_execfree();
     return rc;
 }
 
+FILE *
+my_popen(cmd,mode)
+char   *cmd;
+char   *mode;
+{
+    char *shell = getenv("EMXSHELL");
+    FILE *res;
+    
+    my_setenv("EMXSHELL", "sh.exe");
+    res = popen(cmd, mode);
+    my_setenv("EMXSHELL", shell);
+    return res;
+}
+
 /*****************************************************************************/
 
 #ifndef HAS_FORK
@@ -216,3 +266,62 @@ os2_stat(char *name, struct stat *st)
 }
 
 #endif
+
+#ifndef NO_SYS_ALLOC
+
+static char *old2K;
+
+#define ONE_K (1<<10)
+#define TWO_K (1<<11)
+#define FOUR_K (1<<12)
+#define FOUR_K_FLAG (FOUR_K - 1)
+
+
+void *
+sbrk(int size)
+{
+    char *got;
+    APIRET rc;
+    int is2K = 0;
+
+    if (!size) return 0;
+    else if (size == TWO_K) {
+       is2K = 1;
+       if (old2K) {
+           char *ret = old2K;
+
+           old2K = 0;
+           return (void *)ret;
+       }
+       size = FOUR_K;
+    } else if (size & FOUR_K_FLAG) {
+       croak("Memory allocation in units %li not multiple to 4K", size);
+    }
+    rc = DosAllocMem((void **)&got, size, PAG_COMMIT | PAG_WRITE);
+    if (rc == ERROR_NOT_ENOUGH_MEMORY) {
+       return (void *) -1;
+    } else if ( rc ) die("Got an error from DosAllocMem: %li", (long)rc);
+    if (is2K) old2K = got + TWO_K;
+    return (void *)got;
+}
+#endif /* ! defined NO_SYS_ALLOC */
+
+/* tmp path */
+
+char *tmppath = TMPPATH1;
+
+void
+settmppath()
+{
+    char *p = getenv("TMP"), *tpath;
+    int len;
+
+    if (!p) p = getenv("TEMP");
+    if (!p) return;
+    len = strlen(p);
+    tpath = (char *)malloc(len + strlen(TMPPATH1) + 2);
+    strcpy(tpath, p);
+    tpath[len] = '/';
+    strcpy(tpath + len + 1, TMPPATH1);
+    tmppath = tpath;
+}