ansiperl builds with Borland C++ again
[p5sagit/p5-mst-13.2.git] / util.c
diff --git a/util.c b/util.c
index e3233e5..ab6ddd7 100644 (file)
--- a/util.c
+++ b/util.c
 static void xstat _((void));
 #endif
 
+#ifdef USE_THREADS
+static U32 threadnum = 0;
+#endif /* USE_THREADS */
+
 #ifndef MYMALLOC
 
 /* paranoid version of malloc */
@@ -93,6 +97,7 @@ safemalloc(MEM_SIZE size)
     else {
        PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
        my_exit(1);
+        return Nullch;
     }
     /*NOTREACHED*/
 }
@@ -141,6 +146,7 @@ saferealloc(Malloc_t where,MEM_SIZE size)
     else {
        PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
        my_exit(1);
+       return Nullch;
     }
     /*NOTREACHED*/
 }
@@ -195,6 +201,7 @@ safecalloc(MEM_SIZE count, MEM_SIZE size)
     else {
        PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
        my_exit(1);
+       return Nullch;
     }
     /*NOTREACHED*/
 }
@@ -812,7 +819,8 @@ fbm_compile(SV *sv)
     I32 rarest = 0;
     U32 frequency = 256;
 
-    if (len > 255)
+    sv_upgrade(sv, SVt_PVBM);
+    if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
        return;                 /* can't have offsets that big */
     Sv_Grow(sv,len+258);
     table = (unsigned char*)(SvPVX(sv) + len + 1);
@@ -827,7 +835,6 @@ fbm_compile(SV *sv)
            table[*s] = i;
        s--,i++;
     }
-    sv_upgrade(sv, SVt_PVBM);
     sv_magic(sv, Nullsv, 'B', Nullch, 0);      /* deep magic */
     SvVALID_on(sv);
 
@@ -857,8 +864,15 @@ fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
     if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
        STRLEN len;
        char *l = SvPV(littlestr,len);
-       if (!len)
+       if (!len) {
+           if (SvTAIL(littlestr)) {
+               if (bigend > big && bigend[-1] == '\n')
+                   return (char *)(bigend - 1);
+               else
+                   return (char *) bigend;
+           }
            return (char*)big;
+       }
        return ninstr((char*)big,(char*)bigend, l, l + len);
     }
 
@@ -904,20 +918,35 @@ fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
            while (tmp--) {
                if (*--s == *--little)
                    continue;
+             differ:
                s = olds + 1;   /* here we pay the price for failure */
                little = oldlittle;
                if (s < bigend) /* fake up continue to outer loop */
                    goto top2;
                return Nullch;
            }
+           if (SvTAIL(littlestr)       /* automatically multiline */
+               && olds + 1 != bigend
+               && olds[1] != '\n') 
+               goto differ;
            return (char *)s;
        }
     }
     return Nullch;
 }
 
+/* start_shift, end_shift are positive quantities which give offsets
+   of ends of some substring of bigstr.
+   If `last' we want the last occurence.
+   old_posp is the way of communication between consequent calls if
+   the next call needs to find the . 
+   The initial *old_posp should be -1.
+   Note that we do not take into account SvTAIL, so it may give wrong
+   positives if _ALL flag is set.
+ */
+
 char *
-screaminstr(SV *bigstr, SV *littlestr)
+screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
 {
     register unsigned char *s, *x;
     register unsigned char *big;
@@ -925,54 +954,65 @@ screaminstr(SV *bigstr, SV *littlestr)
     register I32 previous;
     register I32 first;
     register unsigned char *little;
-    register unsigned char *bigend;
+    register I32 stop_pos;
     register unsigned char *littleend;
+    I32 found = 0;
 
-    if ((pos = screamfirst[BmRARE(littlestr)]) < 0) 
+    if (*old_posp == -1
+       ? (pos = screamfirst[BmRARE(littlestr)]) < 0
+       : (((pos = *old_posp), pos += screamnext[pos]) == 0))
        return Nullch;
     little = (unsigned char *)(SvPVX(littlestr));
     littleend = little + SvCUR(littlestr);
     first = *little++;
+    /* The value of pos we can start at: */
     previous = BmPREVIOUS(littlestr);
     big = (unsigned char *)(SvPVX(bigstr));
-    bigend = big + SvCUR(bigstr);
-    while (pos < previous) {
+    /* The value of pos we can stop at: */
+    stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
+    if (previous + start_shift > stop_pos) return Nullch;
+    while (pos < previous + start_shift) {
        if (!(pos += screamnext[pos]))
            return Nullch;
     }
 #ifdef POINTERRIGOR
     do {
+       if (pos >= stop_pos) return Nullch;
        if (big[pos-previous] != first)
            continue;
        for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
-           if (x >= bigend)
-               return Nullch;
            if (*s++ != *x++) {
                s--;
                break;
            }
        }
-       if (s == littleend)
-           return (char *)(big+pos-previous);
+       if (s == littleend) {
+           *old_posp = pos;
+           if (!last) return (char *)(big+pos-previous);
+           found = 1;
+       }
     } while ( pos += screamnext[pos] );
+    return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
 #else /* !POINTERRIGOR */
     big -= previous;
     do {
+       if (pos >= stop_pos) return Nullch;
        if (big[pos] != first)
            continue;
        for (x=big+pos+1,s=little; s < littleend; /**/ ) {
-           if (x >= bigend)
-               return Nullch;
            if (*s++ != *x++) {
                s--;
                break;
            }
        }
-       if (s == littleend)
-           return (char *)(big+pos);
+       if (s == littleend) {
+           *old_posp = pos;
+           if (!last) return (char *)(big+pos);
+           found = 1;
+       }
     } while ( pos += screamnext[pos] );
+    return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
 #endif /* POINTERRIGOR */
-    return Nullch;
 }
 
 I32
@@ -1113,14 +1153,16 @@ die(pat, va_alist)
     dTHR;
     va_list args;
     char *message;
-    I32 oldrunlevel = runlevel;
     int was_in_eval = in_eval;
     HV *stash;
     GV *gv;
     CV *cv;
 
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n",
-                   curstack, mainstack));/*debug*/
+#ifdef USE_THREADS
+    DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+                         "%p: die: curstack = %p, mainstack = %p\n",
+                         thr, curstack, mainstack));
+#endif /* USE_THREADS */
     /* We have to switch back to mainstack or die_where may try to pop
      * the eval block from the wrong stack if die is being called from a
      * signal handler.  - dkindred@cs.cmu.edu */
@@ -1137,8 +1179,11 @@ die(pat, va_alist)
     message = mess(pat, &args);
     va_end(args);
 
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n",
-                  message, diehook));/*debug*/
+#ifdef USE_THREADS
+    DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+                         "%p: die: message = %s\ndiehook = %p\n",
+                         thr, message, diehook));
+#endif /* USE_THREADS */
     if (diehook) {
        /* sv_2cv might call croak() */
        SV *olddiehook = diehook;
@@ -1166,10 +1211,12 @@ die(pat, va_alist)
     }
 
     restartop = die_where(message);
+#ifdef USE_THREADS
     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
-                   "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
-                   restartop, was_in_eval, oldrunlevel));/*debug*/
-    if ((!restartop && was_in_eval) || oldrunlevel > 1)
+         "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
+         thr, restartop, was_in_eval, top_env));
+#endif /* USE_THREADS */
+    if ((!restartop && was_in_eval) || top_env->je_prev)
        JMPENV_JUMP(3);
     return restartop;
 }
@@ -1345,8 +1392,7 @@ my_setenv(char *nam, char *val)
 #else /* if WIN32 */
 
 void
-my_setenv(nam,val)
-char *nam, *val;
+my_setenv(char *nam,char *val)
 {
 
 #ifdef USE_WIN32_RTL_ENV
@@ -1444,10 +1490,7 @@ char *f;
 
 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
 char *
-my_bcopy(from,to,len)
-register char *from;
-register char *to;
-register I32 len;
+my_bcopy(register char *from,register char *to,register I32 len)
 {
     char *retval = to;
 
@@ -1896,9 +1939,7 @@ rsignal_restore(int signo, Sigsave_t *save)
 #else /* !HAS_SIGACTION */
 
 Sighandler_t
-rsignal(signo, handler)
-int signo;
-Sighandler_t handler;
+rsignal(int signo, Sighandler_t handler)
 {
     return signal(signo, handler);
 }
@@ -1907,15 +1948,13 @@ static int sig_trapped;
 
 static
 Signal_t
-sig_trap(signo)
-int signo;
+sig_trap(int signo)
 {
     sig_trapped++;
 }
 
 Sighandler_t
-rsignal_state(signo)
-int signo;
+rsignal_state(int signo)
 {
     Sighandler_t oldsig;
 
@@ -1928,19 +1967,14 @@ int signo;
 }
 
 int
-rsignal_save(signo, handler, save)
-int signo;
-Sighandler_t handler;
-Sigsave_t *save;
+rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
 {
     *save = signal(signo, handler);
     return (*save == SIG_ERR) ? -1 : 0;
 }
 
 int
-rsignal_restore(signo, save)
-int signo;
-Sigsave_t *save;
+rsignal_restore(int signo, Sigsave_t *save)
 {
     return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
 }
@@ -2255,13 +2289,13 @@ scan_hex(char *start, I32 len, I32 *retlen)
     bool overflowed = FALSE;
     char *tmp;
 
-    while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
+    while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
        register UV n = retval << 4;
        if (!overflowed && (n >> 4) != retval) {
            warn("Integer overflow in hex number");
            overflowed = TRUE;
        }
-       retval = n | (tmp - hexdigit) & 15;
+       retval = n | ((tmp - hexdigit) & 15);
        s++;
     }
     *retlen = s - start;
@@ -2396,6 +2430,108 @@ condpair_magic(SV *sv)
     }
     return mg;
 }
+
+/*
+ * Make a new perl thread structure using t as a prototype. Some of the
+ * fields for the new thread are copied from the prototype thread, t,
+ * so t should not be running in perl at the time this function is
+ * called. The use by ext/Thread/Thread.xs in core perl (where t is the
+ * thread calling new_struct_thread) clearly satisfies this constraint.
+ */
+struct thread *
+new_struct_thread(struct thread *t)
+{
+    struct thread *thr;
+    SV *sv;
+    SV **svp;
+    I32 i;
+
+    sv = newSVpv("", 0);
+    SvGROW(sv, sizeof(struct thread) + 1);
+    SvCUR_set(sv, sizeof(struct thread));
+    thr = (Thread) SvPVX(sv);
+    /* debug */
+    memset(thr, 0xab, sizeof(struct thread));
+    markstack = 0;
+    scopestack = 0;
+    savestack = 0;
+    retstack = 0;
+    dirty = 0;
+    localizing = 0;
+    /* end debug */
+
+    thr->oursv = sv;
+    init_stacks(ARGS);
+
+    curcop = &compiling;
+    thr->cvcache = newHV();
+    thr->threadsv = newAV();
+    thr->specific = newAV();
+    thr->errsv = newSVpv("", 0);
+    thr->errhv = newHV();
+    thr->flags = THRf_R_JOINABLE;
+    MUTEX_INIT(&thr->mutex);
+
+    curcop = t->Tcurcop;       /* XXX As good a guess as any? */
+    defstash = t->Tdefstash;   /* XXX maybe these should */
+    curstash = t->Tcurstash;   /* always be set to main? */
+
+
+    /* top_env needs to be non-zero. It points to an area
+       in which longjmp() stuff is stored, as C callstack
+       info there at least is thread specific this has to
+       be per-thread. Otherwise a 'die' in a thread gives
+       that thread the C stack of last thread to do an eval {}!
+       See comments in scope.h    
+       Initialize top entry (as in perl.c for main thread)
+     */
+    start_env.je_prev = NULL;
+    start_env.je_ret = -1;
+    start_env.je_mustcatch = TRUE;
+    top_env  = &start_env;
+
+    in_eval = FALSE;
+    restartop = 0;
+
+    tainted = t->Ttainted;
+    curpm = t->Tcurpm;         /* XXX No PMOP ref count */
+    nrs = newSVsv(t->Tnrs);
+    rs = newSVsv(t->Trs);
+    last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
+    ofslen = t->Tofslen;
+    ofs = savepvn(t->Tofs, ofslen);
+    defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
+    chopset = t->Tchopset;
+    formtarget = newSVsv(t->Tformtarget);
+    bodytarget = newSVsv(t->Tbodytarget);
+    toptarget = newSVsv(t->Ttoptarget);
+    
+    /* Initialise all per-thread SVs that the template thread used */
+    svp = AvARRAY(t->threadsv);
+    for (i = 0; i <= AvFILL(t->threadsv); i++, svp++) {
+       if (*svp && *svp != &sv_undef) {
+           SV *sv = newSVsv(*svp);
+           av_store(thr->threadsv, i, sv);
+           sv_magic(sv, 0, 0, &threadsv_names[i], 1);
+           DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+               "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
+       }
+    } 
+
+    MUTEX_LOCK(&threads_mutex);
+    nthreads++;
+    thr->tid = ++threadnum;
+    thr->next = t->next;
+    thr->prev = t;
+    t->next = thr;
+    thr->next->prev = thr;
+    MUTEX_UNLOCK(&threads_mutex);
+
+#ifdef HAVE_THREAD_INTERN
+    init_thread_intern(thr);
+#endif /* HAVE_THREAD_INTERN */
+    return thr;
+}
 #endif /* USE_THREADS */
 
 #ifdef HUGE_VAL
@@ -2410,3 +2546,5 @@ Perl_huge(void)
  return HUGE_VAL;
 }
 #endif
+
+