Threading patches for OS/2 (missing files taken from previous patch):
[p5sagit/p5-mst-13.2.git] / pp_ctl.c
1 /*    pp_ctl.c
2  *
3  *    Copyright (c) 1991-1997, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /*
11  * Now far ahead the Road has gone,
12  * And I must follow, if I can,
13  * Pursuing it with eager feet,
14  * Until it joins some larger way
15  * Where many paths and errands meet.
16  * And whither then?  I cannot say.
17  */
18
19 #include "EXTERN.h"
20 #include "perl.h"
21
22 #ifndef WORD_ALIGN
23 #define WORD_ALIGN sizeof(U16)
24 #endif
25
26 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
27
28 static OP *docatch _((OP *o));
29 static OP *dofindlabel _((OP *o, char *label, OP **opstack, OP **oplimit));
30 static void doparseform _((SV *sv));
31 static I32 dopoptoeval _((I32 startingblock));
32 static I32 dopoptolabel _((char *label));
33 static I32 dopoptoloop _((I32 startingblock));
34 static I32 dopoptosub _((I32 startingblock));
35 static void save_lines _((AV *array, SV *sv));
36 static int sortcv _((const void *, const void *));
37 static int sortcmp _((const void *, const void *));
38 static int sortcmp_locale _((const void *, const void *));
39 static OP *doeval _((int gimme, OP** startop));
40
41 static I32 sortcxix;
42
43 PP(pp_wantarray)
44 {
45     djSP;
46     I32 cxix;
47     EXTEND(SP, 1);
48
49     cxix = dopoptosub(cxstack_ix);
50     if (cxix < 0)
51         RETPUSHUNDEF;
52
53     switch (cxstack[cxix].blk_gimme) {
54     case G_ARRAY:
55         RETPUSHYES;
56     case G_SCALAR:
57         RETPUSHNO;
58     default:
59         RETPUSHUNDEF;
60     }
61 }
62
63 PP(pp_regcmaybe)
64 {
65     return NORMAL;
66 }
67
68 PP(pp_regcomp) {
69     djSP;
70     register PMOP *pm = (PMOP*)cLOGOP->op_other;
71     register char *t;
72     SV *tmpstr;
73     STRLEN len;
74     MAGIC *mg = Null(MAGIC*);
75
76     tmpstr = POPs;
77     if(SvROK(tmpstr)) {
78         SV *sv = SvRV(tmpstr);
79         if(SvMAGICAL(sv))
80             mg = mg_find(sv, 'r');
81     }
82     if(mg) {
83         regexp *re = (regexp *)mg->mg_obj;
84         ReREFCNT_dec(pm->op_pmregexp);
85         pm->op_pmregexp = ReREFCNT_inc(re);
86     }
87     else {
88         t = SvPV(tmpstr, len);
89
90         /* JMR: Check against the last compiled regexp */
91         if ( ! pm->op_pmregexp  || ! pm->op_pmregexp->precomp
92             || strnNE(pm->op_pmregexp->precomp, t, len) 
93             || pm->op_pmregexp->precomp[len]) {
94             if (pm->op_pmregexp) {
95                 ReREFCNT_dec(pm->op_pmregexp);
96                 pm->op_pmregexp = Null(REGEXP*);        /* crucial if regcomp aborts */
97             }
98
99             pm->op_pmflags = pm->op_pmpermflags;        /* reset case sensitivity */
100             pm->op_pmregexp = pregcomp(t, t + len, pm);
101         }
102     }
103
104     if (!pm->op_pmregexp->prelen && curpm)
105         pm = curpm;
106     else if (strEQ("\\s+", pm->op_pmregexp->precomp))
107         pm->op_pmflags |= PMf_WHITE;
108
109     if (pm->op_pmflags & PMf_KEEP) {
110         pm->op_private &= ~OPpRUNTIME;  /* no point compiling again */
111         cLOGOP->op_first->op_next = op->op_next;
112     }
113     RETURN;
114 }
115
116 PP(pp_substcont)
117 {
118     djSP;
119     register PMOP *pm = (PMOP*) cLOGOP->op_other;
120     register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
121     register SV *dstr = cx->sb_dstr;
122     register char *s = cx->sb_s;
123     register char *m = cx->sb_m;
124     char *orig = cx->sb_orig;
125     register REGEXP *rx = cx->sb_rx;
126
127     rxres_restore(&cx->sb_rxres, rx);
128
129     if (cx->sb_iters++) {
130         if (cx->sb_iters > cx->sb_maxiters)
131             DIE("Substitution loop");
132
133         if (!cx->sb_rxtainted)
134             cx->sb_rxtainted = SvTAINTED(TOPs);
135         sv_catsv(dstr, POPs);
136
137         /* Are we done */
138         if (cx->sb_once || !regexec_flags(rx, s, cx->sb_strend, orig,
139                                      s == m, Nullsv, NULL,
140                                      cx->sb_safebase ? 0 : REXEC_COPY_STR))
141         {
142             SV *targ = cx->sb_targ;
143             sv_catpvn(dstr, s, cx->sb_strend - s);
144
145             TAINT_IF(cx->sb_rxtainted || RX_MATCH_TAINTED(rx));
146
147             (void)SvOOK_off(targ);
148             Safefree(SvPVX(targ));
149             SvPVX(targ) = SvPVX(dstr);
150             SvCUR_set(targ, SvCUR(dstr));
151             SvLEN_set(targ, SvLEN(dstr));
152             SvPVX(dstr) = 0;
153             sv_free(dstr);
154             (void)SvPOK_only(targ);
155             SvSETMAGIC(targ);
156             SvTAINT(targ);
157
158             PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
159             LEAVE_SCOPE(cx->sb_oldsave);
160             POPSUBST(cx);
161             RETURNOP(pm->op_next);
162         }
163     }
164     if (rx->subbase && rx->subbase != orig) {
165         m = s;
166         s = orig;
167         cx->sb_orig = orig = rx->subbase;
168         s = orig + (m - s);
169         cx->sb_strend = s + (cx->sb_strend - m);
170     }
171     cx->sb_m = m = rx->startp[0];
172     sv_catpvn(dstr, s, m-s);
173     cx->sb_s = rx->endp[0];
174     cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
175     rxres_save(&cx->sb_rxres, rx);
176     RETURNOP(pm->op_pmreplstart);
177 }
178
179 void
180 rxres_save(void **rsp, REGEXP *rx)
181 {
182     UV *p = (UV*)*rsp;
183     U32 i;
184
185     if (!p || p[1] < rx->nparens) {
186         i = 6 + rx->nparens * 2;
187         if (!p)
188             New(501, p, i, UV);
189         else
190             Renew(p, i, UV);
191         *rsp = (void*)p;
192     }
193
194     *p++ = (UV)rx->subbase;
195     rx->subbase = Nullch;
196
197     *p++ = rx->nparens;
198
199     *p++ = (UV)rx->subbeg;
200     *p++ = (UV)rx->subend;
201     for (i = 0; i <= rx->nparens; ++i) {
202         *p++ = (UV)rx->startp[i];
203         *p++ = (UV)rx->endp[i];
204     }
205 }
206
207 void
208 rxres_restore(void **rsp, REGEXP *rx)
209 {
210     UV *p = (UV*)*rsp;
211     U32 i;
212
213     Safefree(rx->subbase);
214     rx->subbase = (char*)(*p);
215     *p++ = 0;
216
217     rx->nparens = *p++;
218
219     rx->subbeg = (char*)(*p++);
220     rx->subend = (char*)(*p++);
221     for (i = 0; i <= rx->nparens; ++i) {
222         rx->startp[i] = (char*)(*p++);
223         rx->endp[i] = (char*)(*p++);
224     }
225 }
226
227 void
228 rxres_free(void **rsp)
229 {
230     UV *p = (UV*)*rsp;
231
232     if (p) {
233         Safefree((char*)(*p));
234         Safefree(p);
235         *rsp = Null(void*);
236     }
237 }
238
239 PP(pp_formline)
240 {
241     djSP; dMARK; dORIGMARK;
242     register SV *form = *++MARK;
243     register U16 *fpc;
244     register char *t;
245     register char *f;
246     register char *s;
247     register char *send;
248     register I32 arg;
249     register SV *sv;
250     char *item;
251     I32 itemsize;
252     I32 fieldsize;
253     I32 lines = 0;
254     bool chopspace = (strchr(chopset, ' ') != Nullch);
255     char *chophere;
256     char *linemark;
257     double value;
258     bool gotsome;
259     STRLEN len;
260
261     if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
262         SvREADONLY_off(form);
263         doparseform(form);
264     }
265
266     SvPV_force(formtarget, len);
267     t = SvGROW(formtarget, len + SvCUR(form) + 1);  /* XXX SvCUR bad */
268     t += len;
269     f = SvPV(form, len);
270     /* need to jump to the next word */
271     s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
272
273     fpc = (U16*)s;
274
275     for (;;) {
276         DEBUG_f( {
277             char *name = "???";
278             arg = -1;
279             switch (*fpc) {
280             case FF_LITERAL:    arg = fpc[1]; name = "LITERAL"; break;
281             case FF_BLANK:      arg = fpc[1]; name = "BLANK";   break;
282             case FF_SKIP:       arg = fpc[1]; name = "SKIP";    break;
283             case FF_FETCH:      arg = fpc[1]; name = "FETCH";   break;
284             case FF_DECIMAL:    arg = fpc[1]; name = "DECIMAL"; break;
285
286             case FF_CHECKNL:    name = "CHECKNL";       break;
287             case FF_CHECKCHOP:  name = "CHECKCHOP";     break;
288             case FF_SPACE:      name = "SPACE";         break;
289             case FF_HALFSPACE:  name = "HALFSPACE";     break;
290             case FF_ITEM:       name = "ITEM";          break;
291             case FF_CHOP:       name = "CHOP";          break;
292             case FF_LINEGLOB:   name = "LINEGLOB";      break;
293             case FF_NEWLINE:    name = "NEWLINE";       break;
294             case FF_MORE:       name = "MORE";          break;
295             case FF_LINEMARK:   name = "LINEMARK";      break;
296             case FF_END:        name = "END";           break;
297             }
298             if (arg >= 0)
299                 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
300             else
301                 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
302         } )
303         switch (*fpc++) {
304         case FF_LINEMARK:
305             linemark = t;
306             lines++;
307             gotsome = FALSE;
308             break;
309
310         case FF_LITERAL:
311             arg = *fpc++;
312             while (arg--)
313                 *t++ = *f++;
314             break;
315
316         case FF_SKIP:
317             f += *fpc++;
318             break;
319
320         case FF_FETCH:
321             arg = *fpc++;
322             f += arg;
323             fieldsize = arg;
324
325             if (MARK < SP)
326                 sv = *++MARK;
327             else {
328                 sv = &sv_no;
329                 if (dowarn)
330                     warn("Not enough format arguments");
331             }
332             break;
333
334         case FF_CHECKNL:
335             item = s = SvPV(sv, len);
336             itemsize = len;
337             if (itemsize > fieldsize)
338                 itemsize = fieldsize;
339             send = chophere = s + itemsize;
340             while (s < send) {
341                 if (*s & ~31)
342                     gotsome = TRUE;
343                 else if (*s == '\n')
344                     break;
345                 s++;
346             }
347             itemsize = s - item;
348             break;
349
350         case FF_CHECKCHOP:
351             item = s = SvPV(sv, len);
352             itemsize = len;
353             if (itemsize <= fieldsize) {
354                 send = chophere = s + itemsize;
355                 while (s < send) {
356                     if (*s == '\r') {
357                         itemsize = s - item;
358                         break;
359                     }
360                     if (*s++ & ~31)
361                         gotsome = TRUE;
362                 }
363             }
364             else {
365                 itemsize = fieldsize;
366                 send = chophere = s + itemsize;
367                 while (s < send || (s == send && isSPACE(*s))) {
368                     if (isSPACE(*s)) {
369                         if (chopspace)
370                             chophere = s;
371                         if (*s == '\r')
372                             break;
373                     }
374                     else {
375                         if (*s & ~31)
376                             gotsome = TRUE;
377                         if (strchr(chopset, *s))
378                             chophere = s + 1;
379                     }
380                     s++;
381                 }
382                 itemsize = chophere - item;
383             }
384             break;
385
386         case FF_SPACE:
387             arg = fieldsize - itemsize;
388             if (arg) {
389                 fieldsize -= arg;
390                 while (arg-- > 0)
391                     *t++ = ' ';
392             }
393             break;
394
395         case FF_HALFSPACE:
396             arg = fieldsize - itemsize;
397             if (arg) {
398                 arg /= 2;
399                 fieldsize -= arg;
400                 while (arg-- > 0)
401                     *t++ = ' ';
402             }
403             break;
404
405         case FF_ITEM:
406             arg = itemsize;
407             s = item;
408             while (arg--) {
409 #if 'z' - 'a' != 25
410                 int ch = *t++ = *s++;
411                 if (!iscntrl(ch))
412                     t[-1] = ' ';
413 #else
414                 if ( !((*t++ = *s++) & ~31) )
415                     t[-1] = ' ';
416 #endif
417
418             }
419             break;
420
421         case FF_CHOP:
422             s = chophere;
423             if (chopspace) {
424                 while (*s && isSPACE(*s))
425                     s++;
426             }
427             sv_chop(sv,s);
428             break;
429
430         case FF_LINEGLOB:
431             item = s = SvPV(sv, len);
432             itemsize = len;
433             if (itemsize) {
434                 gotsome = TRUE;
435                 send = s + itemsize;
436                 while (s < send) {
437                     if (*s++ == '\n') {
438                         if (s == send)
439                             itemsize--;
440                         else
441                             lines++;
442                     }
443                 }
444                 SvCUR_set(formtarget, t - SvPVX(formtarget));
445                 sv_catpvn(formtarget, item, itemsize);
446                 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
447                 t = SvPVX(formtarget) + SvCUR(formtarget);
448             }
449             break;
450
451         case FF_DECIMAL:
452             /* If the field is marked with ^ and the value is undefined,
453                blank it out. */
454             arg = *fpc++;
455             if ((arg & 512) && !SvOK(sv)) {
456                 arg = fieldsize;
457                 while (arg--)
458                     *t++ = ' ';
459                 break;
460             }
461             gotsome = TRUE;
462             value = SvNV(sv);
463             /* Formats aren't yet marked for locales, so assume "yes". */
464             SET_NUMERIC_LOCAL();
465             if (arg & 256) {
466                 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
467             } else {
468                 sprintf(t, "%*.0f", (int) fieldsize, value);
469             }
470             t += fieldsize;
471             break;
472
473         case FF_NEWLINE:
474             f++;
475             while (t-- > linemark && *t == ' ') ;
476             t++;
477             *t++ = '\n';
478             break;
479
480         case FF_BLANK:
481             arg = *fpc++;
482             if (gotsome) {
483                 if (arg) {              /* repeat until fields exhausted? */
484                     *t = '\0';
485                     SvCUR_set(formtarget, t - SvPVX(formtarget));
486                     lines += FmLINES(formtarget);
487                     if (lines == 200) {
488                         arg = t - linemark;
489                         if (strnEQ(linemark, linemark - arg, arg))
490                             DIE("Runaway format");
491                     }
492                     FmLINES(formtarget) = lines;
493                     SP = ORIGMARK;
494                     RETURNOP(cLISTOP->op_first);
495                 }
496             }
497             else {
498                 t = linemark;
499                 lines--;
500             }
501             break;
502
503         case FF_MORE:
504             if (itemsize) {
505                 arg = fieldsize - itemsize;
506                 if (arg) {
507                     fieldsize -= arg;
508                     while (arg-- > 0)
509                         *t++ = ' ';
510                 }
511                 s = t - 3;
512                 if (strnEQ(s,"   ",3)) {
513                     while (s > SvPVX(formtarget) && isSPACE(s[-1]))
514                         s--;
515                 }
516                 *s++ = '.';
517                 *s++ = '.';
518                 *s++ = '.';
519             }
520             break;
521
522         case FF_END:
523             *t = '\0';
524             SvCUR_set(formtarget, t - SvPVX(formtarget));
525             FmLINES(formtarget) += lines;
526             SP = ORIGMARK;
527             RETPUSHYES;
528         }
529     }
530 }
531
532 PP(pp_grepstart)
533 {
534     djSP;
535     SV *src;
536
537     if (stack_base + *markstack_ptr == sp) {
538         (void)POPMARK;
539         if (GIMME_V == G_SCALAR)
540             XPUSHs(&sv_no);
541         RETURNOP(op->op_next->op_next);
542     }
543     stack_sp = stack_base + *markstack_ptr + 1;
544     pp_pushmark(ARGS);                          /* push dst */
545     pp_pushmark(ARGS);                          /* push src */
546     ENTER;                                      /* enter outer scope */
547
548     SAVETMPS;
549 #ifdef USE_THREADS
550     /* SAVE_DEFSV does *not* suffice here */
551     save_sptr(av_fetch(thr->threadsv, find_threadsv("_"), FALSE));
552 #else
553     SAVESPTR(GvSV(defgv));
554 #endif /* USE_THREADS */
555     ENTER;                                      /* enter inner scope */
556     SAVESPTR(curpm);
557
558     src = stack_base[*markstack_ptr];
559     SvTEMP_off(src);
560     DEFSV = src;
561
562     PUTBACK;
563     if (op->op_type == OP_MAPSTART)
564         pp_pushmark(ARGS);                      /* push top */
565     return ((LOGOP*)op->op_next)->op_other;
566 }
567
568 PP(pp_mapstart)
569 {
570     DIE("panic: mapstart");     /* uses grepstart */
571 }
572
573 PP(pp_mapwhile)
574 {
575     djSP;
576     I32 diff = (sp - stack_base) - *markstack_ptr;
577     I32 count;
578     I32 shift;
579     SV** src;
580     SV** dst; 
581
582     ++markstack_ptr[-1];
583     if (diff) {
584         if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
585             shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
586             count = (sp - stack_base) - markstack_ptr[-1] + 2;
587             
588             EXTEND(sp,shift);
589             src = sp;
590             dst = (sp += shift);
591             markstack_ptr[-1] += shift;
592             *markstack_ptr += shift;
593             while (--count)
594                 *dst-- = *src--;
595         }
596         dst = stack_base + (markstack_ptr[-2] += diff) - 1; 
597         ++diff;
598         while (--diff)
599             *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs); 
600     }
601     LEAVE;                                      /* exit inner scope */
602
603     /* All done yet? */
604     if (markstack_ptr[-1] > *markstack_ptr) {
605         I32 items;
606         I32 gimme = GIMME_V;
607
608         (void)POPMARK;                          /* pop top */
609         LEAVE;                                  /* exit outer scope */
610         (void)POPMARK;                          /* pop src */
611         items = --*markstack_ptr - markstack_ptr[-1];
612         (void)POPMARK;                          /* pop dst */
613         SP = stack_base + POPMARK;              /* pop original mark */
614         if (gimme == G_SCALAR) {
615             dTARGET;
616             XPUSHi(items);
617         }
618         else if (gimme == G_ARRAY)
619             SP += items;
620         RETURN;
621     }
622     else {
623         SV *src;
624
625         ENTER;                                  /* enter inner scope */
626         SAVESPTR(curpm);
627
628         src = stack_base[markstack_ptr[-1]];
629         SvTEMP_off(src);
630         DEFSV = src;
631
632         RETURNOP(cLOGOP->op_other);
633     }
634 }
635
636
637 PP(pp_sort)
638 {
639     djSP; dMARK; dORIGMARK;
640     register SV **up;
641     SV **myorigmark = ORIGMARK;
642     register I32 max;
643     HV *stash;
644     GV *gv;
645     CV *cv;
646     I32 gimme = GIMME;
647     OP* nextop = op->op_next;
648
649     if (gimme != G_ARRAY) {
650         SP = MARK;
651         RETPUSHUNDEF;
652     }
653
654     if (op->op_flags & OPf_STACKED) {
655         ENTER;
656         if (op->op_flags & OPf_SPECIAL) {
657             OP *kid = cLISTOP->op_first->op_sibling;    /* pass pushmark */
658             kid = kUNOP->op_first;                      /* pass rv2gv */
659             kid = kUNOP->op_first;                      /* pass leave */
660             sortcop = kid->op_next;
661             stash = curcop->cop_stash;
662         }
663         else {
664             cv = sv_2cv(*++MARK, &stash, &gv, 0);
665             if (!(cv && CvROOT(cv))) {
666                 if (gv) {
667                     SV *tmpstr = sv_newmortal();
668                     gv_efullname3(tmpstr, gv, Nullch);
669                     if (cv && CvXSUB(cv))
670                         DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
671                     DIE("Undefined sort subroutine \"%s\" called",
672                         SvPVX(tmpstr));
673                 }
674                 if (cv) {
675                     if (CvXSUB(cv))
676                         DIE("Xsub called in sort");
677                     DIE("Undefined subroutine in sort");
678                 }
679                 DIE("Not a CODE reference in sort");
680             }
681             sortcop = CvSTART(cv);
682             SAVESPTR(CvROOT(cv)->op_ppaddr);
683             CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
684
685             SAVESPTR(curpad);
686             curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
687         }
688     }
689     else {
690         sortcop = Nullop;
691         stash = curcop->cop_stash;
692     }
693
694     up = myorigmark + 1;
695     while (MARK < SP) { /* This may or may not shift down one here. */
696         /*SUPPRESS 560*/
697         if (*up = *++MARK) {                    /* Weed out nulls. */
698             SvTEMP_off(*up);
699             if (!sortcop && !SvPOK(*up))
700                 (void)sv_2pv(*up, &na);
701             up++;
702         }
703     }
704     max = --up - myorigmark;
705     if (sortcop) {
706         if (max > 1) {
707             AV *oldstack;
708             PERL_CONTEXT *cx;
709             SV** newsp;
710             bool oldcatch = CATCH_GET;
711
712             SAVETMPS;
713             SAVEOP();
714
715             oldstack = curstack;
716             if (!sortstack) {
717                 sortstack = newAV();
718                 AvREAL_off(sortstack);
719                 av_extend(sortstack, 32);
720             }
721             CATCH_SET(TRUE);
722             SWITCHSTACK(curstack, sortstack);
723             if (sortstash != stash) {
724                 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
725                 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
726                 sortstash = stash;
727             }
728
729             SAVESPTR(GvSV(firstgv));
730             SAVESPTR(GvSV(secondgv));
731
732             PUSHBLOCK(cx, CXt_NULL, stack_base);
733             if (!(op->op_flags & OPf_SPECIAL)) {
734                 bool hasargs = FALSE;
735                 cx->cx_type = CXt_SUB;
736                 cx->blk_gimme = G_SCALAR;
737                 PUSHSUB(cx);
738                 if (!CvDEPTH(cv))
739                     (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
740             }
741             sortcxix = cxstack_ix;
742
743             qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
744
745             POPBLOCK(cx,curpm);
746             SWITCHSTACK(sortstack, oldstack);
747             CATCH_SET(oldcatch);
748         }
749         LEAVE;
750     }
751     else {
752         if (max > 1) {
753             MEXTEND(SP, 20);    /* Can't afford stack realloc on signal. */
754             qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
755                   (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
756         }
757     }
758     stack_sp = ORIGMARK + max;
759     return nextop;
760 }
761
762 /* Range stuff. */
763
764 PP(pp_range)
765 {
766     if (GIMME == G_ARRAY)
767         return cCONDOP->op_true;
768     return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
769 }
770
771 PP(pp_flip)
772 {
773     djSP;
774
775     if (GIMME == G_ARRAY) {
776         RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
777     }
778     else {
779         dTOPss;
780         SV *targ = PAD_SV(op->op_targ);
781
782         if ((op->op_private & OPpFLIP_LINENUM)
783           ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
784           : SvTRUE(sv) ) {
785             sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
786             if (op->op_flags & OPf_SPECIAL) {
787                 sv_setiv(targ, 1);
788                 SETs(targ);
789                 RETURN;
790             }
791             else {
792                 sv_setiv(targ, 0);
793                 sp--;
794                 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
795             }
796         }
797         sv_setpv(TARG, "");
798         SETs(targ);
799         RETURN;
800     }
801 }
802
803 PP(pp_flop)
804 {
805     djSP;
806
807     if (GIMME == G_ARRAY) {
808         dPOPPOPssrl;
809         register I32 i;
810         register SV *sv;
811         I32 max;
812
813         if (SvNIOKp(left) || !SvPOKp(left) ||
814           (looks_like_number(left) && *SvPVX(left) != '0') )
815         {
816             i = SvIV(left);
817             max = SvIV(right);
818             if (max >= i) {
819                 EXTEND_MORTAL(max - i + 1);
820                 EXTEND(SP, max - i + 1);
821             }
822             while (i <= max) {
823                 sv = sv_2mortal(newSViv(i++));
824                 PUSHs(sv);
825             }
826         }
827         else {
828             SV *final = sv_mortalcopy(right);
829             STRLEN len;
830             char *tmps = SvPV(final, len);
831
832             sv = sv_mortalcopy(left);
833             while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
834                 strNE(SvPVX(sv),tmps) ) {
835                 XPUSHs(sv);
836                 sv = sv_2mortal(newSVsv(sv));
837                 sv_inc(sv);
838             }
839             if (strEQ(SvPVX(sv),tmps))
840                 XPUSHs(sv);
841         }
842     }
843     else {
844         dTOPss;
845         SV *targ = PAD_SV(cUNOP->op_first->op_targ);
846         sv_inc(targ);
847         if ((op->op_private & OPpFLIP_LINENUM)
848           ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
849           : SvTRUE(sv) ) {
850             sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
851             sv_catpv(targ, "E0");
852         }
853         SETs(targ);
854     }
855
856     RETURN;
857 }
858
859 /* Control. */
860
861 static I32
862 dopoptolabel(char *label)
863 {
864     dTHR;
865     register I32 i;
866     register PERL_CONTEXT *cx;
867
868     for (i = cxstack_ix; i >= 0; i--) {
869         cx = &cxstack[i];
870         switch (cx->cx_type) {
871         case CXt_SUBST:
872             if (dowarn)
873                 warn("Exiting substitution via %s", op_name[op->op_type]);
874             break;
875         case CXt_SUB:
876             if (dowarn)
877                 warn("Exiting subroutine via %s", op_name[op->op_type]);
878             break;
879         case CXt_EVAL:
880             if (dowarn)
881                 warn("Exiting eval via %s", op_name[op->op_type]);
882             break;
883         case CXt_NULL:
884             if (dowarn)
885                 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
886             return -1;
887         case CXt_LOOP:
888             if (!cx->blk_loop.label ||
889               strNE(label, cx->blk_loop.label) ) {
890                 DEBUG_l(deb("(Skipping label #%ld %s)\n",
891                         (long)i, cx->blk_loop.label));
892                 continue;
893             }
894             DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
895             return i;
896         }
897     }
898     return i;
899 }
900
901 I32
902 dowantarray(void)
903 {
904     I32 gimme = block_gimme();
905     return (gimme == G_VOID) ? G_SCALAR : gimme;
906 }
907
908 I32
909 block_gimme(void)
910 {
911     dTHR;
912     I32 cxix;
913
914     cxix = dopoptosub(cxstack_ix);
915     if (cxix < 0)
916         return G_VOID;
917
918     switch (cxstack[cxix].blk_gimme) {
919     case G_SCALAR:
920         return G_SCALAR;
921     case G_ARRAY:
922         return G_ARRAY;
923     default:
924         croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
925     case G_VOID:
926         return G_VOID;
927     }
928 }
929
930 static I32
931 dopoptosub(I32 startingblock)
932 {
933     dTHR;
934     I32 i;
935     register PERL_CONTEXT *cx;
936     for (i = startingblock; i >= 0; i--) {
937         cx = &cxstack[i];
938         switch (cx->cx_type) {
939         default:
940             continue;
941         case CXt_EVAL:
942         case CXt_SUB:
943             DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
944             return i;
945         }
946     }
947     return i;
948 }
949
950 static I32
951 dopoptoeval(I32 startingblock)
952 {
953     dTHR;
954     I32 i;
955     register PERL_CONTEXT *cx;
956     for (i = startingblock; i >= 0; i--) {
957         cx = &cxstack[i];
958         switch (cx->cx_type) {
959         default:
960             continue;
961         case CXt_EVAL:
962             DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
963             return i;
964         }
965     }
966     return i;
967 }
968
969 static I32
970 dopoptoloop(I32 startingblock)
971 {
972     dTHR;
973     I32 i;
974     register PERL_CONTEXT *cx;
975     for (i = startingblock; i >= 0; i--) {
976         cx = &cxstack[i];
977         switch (cx->cx_type) {
978         case CXt_SUBST:
979             if (dowarn)
980                 warn("Exiting substitution via %s", op_name[op->op_type]);
981             break;
982         case CXt_SUB:
983             if (dowarn)
984                 warn("Exiting subroutine via %s", op_name[op->op_type]);
985             break;
986         case CXt_EVAL:
987             if (dowarn)
988                 warn("Exiting eval via %s", op_name[op->op_type]);
989             break;
990         case CXt_NULL:
991             if (dowarn)
992                 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
993             return -1;
994         case CXt_LOOP:
995             DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
996             return i;
997         }
998     }
999     return i;
1000 }
1001
1002 void
1003 dounwind(I32 cxix)
1004 {
1005     dTHR;
1006     register PERL_CONTEXT *cx;
1007     SV **newsp;
1008     I32 optype;
1009
1010     while (cxstack_ix > cxix) {
1011         cx = &cxstack[cxstack_ix];
1012         DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1013                               (long) cxstack_ix+1, block_type[cx->cx_type]));
1014         /* Note: we don't need to restore the base context info till the end. */
1015         switch (cx->cx_type) {
1016         case CXt_SUBST:
1017             POPSUBST(cx);
1018             continue;  /* not break */
1019         case CXt_SUB:
1020             POPSUB(cx);
1021             break;
1022         case CXt_EVAL:
1023             POPEVAL(cx);
1024             break;
1025         case CXt_LOOP:
1026             POPLOOP(cx);
1027             break;
1028         case CXt_NULL:
1029             break;
1030         }
1031         cxstack_ix--;
1032     }
1033 }
1034
1035 OP *
1036 die_where(char *message)
1037 {
1038     dTHR;
1039     if (in_eval) {
1040         I32 cxix;
1041         register PERL_CONTEXT *cx;
1042         I32 gimme;
1043         SV **newsp;
1044
1045         if (in_eval & 4) {
1046             SV **svp;
1047             STRLEN klen = strlen(message);
1048             
1049             svp = hv_fetch(ERRHV, message, klen, TRUE);
1050             if (svp) {
1051                 if (!SvIOK(*svp)) {
1052                     static char prefix[] = "\t(in cleanup) ";
1053                     SV *err = ERRSV;
1054                     sv_upgrade(*svp, SVt_IV);
1055                     (void)SvIOK_only(*svp);
1056                     if (!SvPOK(err))
1057                         sv_setpv(err,"");
1058                     SvGROW(err, SvCUR(err)+sizeof(prefix)+klen);
1059                     sv_catpvn(err, prefix, sizeof(prefix)-1);
1060                     sv_catpvn(err, message, klen);
1061                 }
1062                 sv_inc(*svp);
1063             }
1064         }
1065         else
1066             sv_setpv(ERRSV, message);
1067         
1068         cxix = dopoptoeval(cxstack_ix);
1069         if (cxix >= 0) {
1070             I32 optype;
1071
1072             if (cxix < cxstack_ix)
1073                 dounwind(cxix);
1074
1075             POPBLOCK(cx,curpm);
1076             if (cx->cx_type != CXt_EVAL) {
1077                 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
1078                 my_exit(1);
1079             }
1080             POPEVAL(cx);
1081
1082             if (gimme == G_SCALAR)
1083                 *++newsp = &sv_undef;
1084             stack_sp = newsp;
1085
1086             LEAVE;
1087
1088             if (optype == OP_REQUIRE) {
1089                 char* msg = SvPVx(ERRSV, na);
1090                 DIE("%s", *msg ? msg : "Compilation failed in require");
1091             }
1092             return pop_return();
1093         }
1094     }
1095     PerlIO_printf(PerlIO_stderr(), "%s",message);
1096     PerlIO_flush(PerlIO_stderr());
1097     my_failure_exit();
1098     /* NOTREACHED */
1099     return 0;
1100 }
1101
1102 PP(pp_xor)
1103 {
1104     djSP; dPOPTOPssrl;
1105     if (SvTRUE(left) != SvTRUE(right))
1106         RETSETYES;
1107     else
1108         RETSETNO;
1109 }
1110
1111 PP(pp_andassign)
1112 {
1113     djSP;
1114     if (!SvTRUE(TOPs))
1115         RETURN;
1116     else
1117         RETURNOP(cLOGOP->op_other);
1118 }
1119
1120 PP(pp_orassign)
1121 {
1122     djSP;
1123     if (SvTRUE(TOPs))
1124         RETURN;
1125     else
1126         RETURNOP(cLOGOP->op_other);
1127 }
1128         
1129 PP(pp_caller)
1130 {
1131     djSP;
1132     register I32 cxix = dopoptosub(cxstack_ix);
1133     register PERL_CONTEXT *cx;
1134     I32 dbcxix;
1135     I32 gimme;
1136     SV *sv;
1137     I32 count = 0;
1138
1139     if (MAXARG)
1140         count = POPi;
1141     EXTEND(SP, 6);
1142     for (;;) {
1143         if (cxix < 0) {
1144             if (GIMME != G_ARRAY)
1145                 RETPUSHUNDEF;
1146             RETURN;
1147         }
1148         if (DBsub && cxix >= 0 &&
1149                 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1150             count++;
1151         if (!count--)
1152             break;
1153         cxix = dopoptosub(cxix - 1);
1154     }
1155     cx = &cxstack[cxix];
1156     if (cxstack[cxix].cx_type == CXt_SUB) {
1157         dbcxix = dopoptosub(cxix - 1);
1158         /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1159            field below is defined for any cx. */
1160         if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1161             cx = &cxstack[dbcxix];
1162     }
1163
1164     if (GIMME != G_ARRAY) {
1165         dTARGET;
1166
1167         sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1168         PUSHs(TARG);
1169         RETURN;
1170     }
1171
1172     PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1173     PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1174     PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1175     if (!MAXARG)
1176         RETURN;
1177     if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
1178         sv = NEWSV(49, 0);
1179         gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
1180         PUSHs(sv_2mortal(sv));
1181         PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1182     }
1183     else {
1184         PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1185         PUSHs(sv_2mortal(newSViv(0)));
1186     }
1187     gimme = (I32)cx->blk_gimme;
1188     if (gimme == G_VOID)
1189         PUSHs(&sv_undef);
1190     else
1191         PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1192     if (cx->cx_type == CXt_EVAL) {
1193         if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1194             PUSHs(cx->blk_eval.cur_text);
1195             PUSHs(&sv_no);
1196         } 
1197         else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1198             /* Require, put the name. */
1199             PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1200             PUSHs(&sv_yes);
1201         }
1202     }
1203     else if (cx->cx_type == CXt_SUB &&
1204             cx->blk_sub.hasargs &&
1205             curcop->cop_stash == debstash)
1206     {
1207         AV *ary = cx->blk_sub.argarray;
1208         int off = AvARRAY(ary) - AvALLOC(ary);
1209
1210         if (!dbargs) {
1211             GV* tmpgv;
1212             dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1213                                 SVt_PVAV)));
1214             GvMULTI_on(tmpgv);
1215             AvREAL_off(dbargs);         /* XXX Should be REIFY */
1216         }
1217
1218         if (AvMAX(dbargs) < AvFILL(ary) + off)
1219             av_extend(dbargs, AvFILL(ary) + off);
1220         Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1221         AvFILL(dbargs) = AvFILL(ary) + off;
1222     }
1223     RETURN;
1224 }
1225
1226 static int
1227 sortcv(const void *a, const void *b)
1228 {
1229     dTHR;
1230     SV * const *str1 = (SV * const *)a;
1231     SV * const *str2 = (SV * const *)b;
1232     I32 oldsaveix = savestack_ix;
1233     I32 oldscopeix = scopestack_ix;
1234     I32 result;
1235     GvSV(firstgv) = *str1;
1236     GvSV(secondgv) = *str2;
1237     stack_sp = stack_base;
1238     op = sortcop;
1239     runops();
1240     if (stack_sp != stack_base + 1)
1241         croak("Sort subroutine didn't return single value");
1242     if (!SvNIOKp(*stack_sp))
1243         croak("Sort subroutine didn't return a numeric value");
1244     result = SvIV(*stack_sp);
1245     while (scopestack_ix > oldscopeix) {
1246         LEAVE;
1247     }
1248     leave_scope(oldsaveix);
1249     return result;
1250 }
1251
1252 static int
1253 sortcmp(const void *a, const void *b)
1254 {
1255     return sv_cmp(*(SV * const *)a, *(SV * const *)b);
1256 }
1257
1258 static int
1259 sortcmp_locale(const void *a, const void *b)
1260 {
1261     return sv_cmp_locale(*(SV * const *)a, *(SV * const *)b);
1262 }
1263
1264 PP(pp_reset)
1265 {
1266     djSP;
1267     char *tmps;
1268
1269     if (MAXARG < 1)
1270         tmps = "";
1271     else
1272         tmps = POPp;
1273     sv_reset(tmps, curcop->cop_stash);
1274     PUSHs(&sv_yes);
1275     RETURN;
1276 }
1277
1278 PP(pp_lineseq)
1279 {
1280     return NORMAL;
1281 }
1282
1283 PP(pp_dbstate)
1284 {
1285     curcop = (COP*)op;
1286     TAINT_NOT;          /* Each statement is presumed innocent */
1287     stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1288     FREETMPS;
1289
1290     if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1291     {
1292         SV **sp;
1293         register CV *cv;
1294         register PERL_CONTEXT *cx;
1295         I32 gimme = G_ARRAY;
1296         I32 hasargs;
1297         GV *gv;
1298
1299         gv = DBgv;
1300         cv = GvCV(gv);
1301         if (!cv)
1302             DIE("No DB::DB routine defined");
1303
1304         if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
1305             return NORMAL;
1306
1307         ENTER;
1308         SAVETMPS;
1309
1310         SAVEI32(debug);
1311         SAVESTACK_POS();
1312         debug = 0;
1313         hasargs = 0;
1314         sp = stack_sp;
1315
1316         push_return(op->op_next);
1317         PUSHBLOCK(cx, CXt_SUB, sp);
1318         PUSHSUB(cx);
1319         CvDEPTH(cv)++;
1320         (void)SvREFCNT_inc(cv);
1321         SAVESPTR(curpad);
1322         curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1323         RETURNOP(CvSTART(cv));
1324     }
1325     else
1326         return NORMAL;
1327 }
1328
1329 PP(pp_scope)
1330 {
1331     return NORMAL;
1332 }
1333
1334 PP(pp_enteriter)
1335 {
1336     djSP; dMARK;
1337     register PERL_CONTEXT *cx;
1338     I32 gimme = GIMME_V;
1339     SV **svp;
1340
1341     ENTER;
1342     SAVETMPS;
1343
1344 #ifdef USE_THREADS
1345     if (op->op_flags & OPf_SPECIAL)
1346         svp = save_threadsv(op->op_targ);       /* per-thread variable */
1347     else
1348 #endif /* USE_THREADS */
1349     if (op->op_targ) {
1350         svp = &curpad[op->op_targ];             /* "my" variable */
1351         SAVESPTR(*svp);
1352     }
1353     else {
1354         svp = &GvSV((GV*)POPs);                 /* symbol table variable */
1355         SAVESPTR(*svp);
1356     }
1357
1358     ENTER;
1359
1360     PUSHBLOCK(cx, CXt_LOOP, SP);
1361     PUSHLOOP(cx, svp, MARK);
1362     if (op->op_flags & OPf_STACKED)
1363         cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1364     else {
1365         cx->blk_loop.iterary = curstack;
1366         AvFILL(curstack) = sp - stack_base;
1367         cx->blk_loop.iterix = MARK - stack_base;
1368     }
1369
1370     RETURN;
1371 }
1372
1373 PP(pp_enterloop)
1374 {
1375     djSP;
1376     register PERL_CONTEXT *cx;
1377     I32 gimme = GIMME_V;
1378
1379     ENTER;
1380     SAVETMPS;
1381     ENTER;
1382
1383     PUSHBLOCK(cx, CXt_LOOP, SP);
1384     PUSHLOOP(cx, 0, SP);
1385
1386     RETURN;
1387 }
1388
1389 PP(pp_leaveloop)
1390 {
1391     djSP;
1392     register PERL_CONTEXT *cx;
1393     struct block_loop cxloop;
1394     I32 gimme;
1395     SV **newsp;
1396     PMOP *newpm;
1397     SV **mark;
1398
1399     POPBLOCK(cx,newpm);
1400     mark = newsp;
1401     POPLOOP1(cx);       /* Delay POPLOOP2 until stack values are safe */
1402
1403     TAINT_NOT;
1404     if (gimme == G_VOID)
1405         ; /* do nothing */
1406     else if (gimme == G_SCALAR) {
1407         if (mark < SP)
1408             *++newsp = sv_mortalcopy(*SP);
1409         else
1410             *++newsp = &sv_undef;
1411     }
1412     else {
1413         while (mark < SP) {
1414             *++newsp = sv_mortalcopy(*++mark);
1415             TAINT_NOT;          /* Each item is independent */
1416         }
1417     }
1418     SP = newsp;
1419     PUTBACK;
1420
1421     POPLOOP2();         /* Stack values are safe: release loop vars ... */
1422     curpm = newpm;      /* ... and pop $1 et al */
1423
1424     LEAVE;
1425     LEAVE;
1426
1427     return NORMAL;
1428 }
1429
1430 PP(pp_return)
1431 {
1432     djSP; dMARK;
1433     I32 cxix;
1434     register PERL_CONTEXT *cx;
1435     struct block_sub cxsub;
1436     bool popsub2 = FALSE;
1437     I32 gimme;
1438     SV **newsp;
1439     PMOP *newpm;
1440     I32 optype = 0;
1441
1442     if (curstack == sortstack) {
1443         if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) <= sortcxix) {
1444             if (cxstack_ix > sortcxix)
1445                 dounwind(sortcxix);
1446             AvARRAY(curstack)[1] = *SP;
1447             stack_sp = stack_base + 1;
1448             return 0;
1449         }
1450     }
1451
1452     cxix = dopoptosub(cxstack_ix);
1453     if (cxix < 0)
1454         DIE("Can't return outside a subroutine");
1455     if (cxix < cxstack_ix)
1456         dounwind(cxix);
1457
1458     POPBLOCK(cx,newpm);
1459     switch (cx->cx_type) {
1460     case CXt_SUB:
1461         POPSUB1(cx);    /* Delay POPSUB2 until stack values are safe */
1462         popsub2 = TRUE;
1463         break;
1464     case CXt_EVAL:
1465         POPEVAL(cx);
1466         if (optype == OP_REQUIRE &&
1467             (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1468         {
1469             /* Unassume the success we assumed earlier. */
1470             char *name = cx->blk_eval.old_name;
1471             (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1472             DIE("%s did not return a true value", name);
1473         }
1474         break;
1475     default:
1476         DIE("panic: return");
1477     }
1478
1479     TAINT_NOT;
1480     if (gimme == G_SCALAR) {
1481         if (MARK < SP)
1482             *++newsp = (popsub2 && SvTEMP(*SP))
1483                         ? *SP : sv_mortalcopy(*SP);
1484         else
1485             *++newsp = &sv_undef;
1486     }
1487     else if (gimme == G_ARRAY) {
1488         while (++MARK <= SP) {
1489             *++newsp = (popsub2 && SvTEMP(*MARK))
1490                         ? *MARK : sv_mortalcopy(*MARK);
1491             TAINT_NOT;          /* Each item is independent */
1492         }
1493     }
1494     stack_sp = newsp;
1495
1496     /* Stack values are safe: */
1497     if (popsub2) {
1498         POPSUB2();      /* release CV and @_ ... */
1499     }
1500     curpm = newpm;      /* ... and pop $1 et al */
1501
1502     LEAVE;
1503     return pop_return();
1504 }
1505
1506 PP(pp_last)
1507 {
1508     djSP;
1509     I32 cxix;
1510     register PERL_CONTEXT *cx;
1511     struct block_loop cxloop;
1512     struct block_sub cxsub;
1513     I32 pop2 = 0;
1514     I32 gimme;
1515     I32 optype;
1516     OP *nextop;
1517     SV **newsp;
1518     PMOP *newpm;
1519     SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
1520
1521     if (op->op_flags & OPf_SPECIAL) {
1522         cxix = dopoptoloop(cxstack_ix);
1523         if (cxix < 0)
1524             DIE("Can't \"last\" outside a block");
1525     }
1526     else {
1527         cxix = dopoptolabel(cPVOP->op_pv);
1528         if (cxix < 0)
1529             DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1530     }
1531     if (cxix < cxstack_ix)
1532         dounwind(cxix);
1533
1534     POPBLOCK(cx,newpm);
1535     switch (cx->cx_type) {
1536     case CXt_LOOP:
1537         POPLOOP1(cx);   /* Delay POPLOOP2 until stack values are safe */
1538         pop2 = CXt_LOOP;
1539         nextop = cxloop.last_op->op_next;
1540         break;
1541     case CXt_SUB:
1542         POPSUB1(cx);    /* Delay POPSUB2 until stack values are safe */
1543         pop2 = CXt_SUB;
1544         nextop = pop_return();
1545         break;
1546     case CXt_EVAL:
1547         POPEVAL(cx);
1548         nextop = pop_return();
1549         break;
1550     default:
1551         DIE("panic: last");
1552     }
1553
1554     TAINT_NOT;
1555     if (gimme == G_SCALAR) {
1556         if (MARK < SP)
1557             *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1558                         ? *SP : sv_mortalcopy(*SP);
1559         else
1560             *++newsp = &sv_undef;
1561     }
1562     else if (gimme == G_ARRAY) {
1563         while (++MARK <= SP) {
1564             *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1565                         ? *MARK : sv_mortalcopy(*MARK);
1566             TAINT_NOT;          /* Each item is independent */
1567         }
1568     }
1569     SP = newsp;
1570     PUTBACK;
1571
1572     /* Stack values are safe: */
1573     switch (pop2) {
1574     case CXt_LOOP:
1575         POPLOOP2();     /* release loop vars ... */
1576         LEAVE;
1577         break;
1578     case CXt_SUB:
1579         POPSUB2();      /* release CV and @_ ... */
1580         break;
1581     }
1582     curpm = newpm;      /* ... and pop $1 et al */
1583
1584     LEAVE;
1585     return nextop;
1586 }
1587
1588 PP(pp_next)
1589 {
1590     I32 cxix;
1591     register PERL_CONTEXT *cx;
1592     I32 oldsave;
1593
1594     if (op->op_flags & OPf_SPECIAL) {
1595         cxix = dopoptoloop(cxstack_ix);
1596         if (cxix < 0)
1597             DIE("Can't \"next\" outside a block");
1598     }
1599     else {
1600         cxix = dopoptolabel(cPVOP->op_pv);
1601         if (cxix < 0)
1602             DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1603     }
1604     if (cxix < cxstack_ix)
1605         dounwind(cxix);
1606
1607     TOPBLOCK(cx);
1608     oldsave = scopestack[scopestack_ix - 1];
1609     LEAVE_SCOPE(oldsave);
1610     return cx->blk_loop.next_op;
1611 }
1612
1613 PP(pp_redo)
1614 {
1615     I32 cxix;
1616     register PERL_CONTEXT *cx;
1617     I32 oldsave;
1618
1619     if (op->op_flags & OPf_SPECIAL) {
1620         cxix = dopoptoloop(cxstack_ix);
1621         if (cxix < 0)
1622             DIE("Can't \"redo\" outside a block");
1623     }
1624     else {
1625         cxix = dopoptolabel(cPVOP->op_pv);
1626         if (cxix < 0)
1627             DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1628     }
1629     if (cxix < cxstack_ix)
1630         dounwind(cxix);
1631
1632     TOPBLOCK(cx);
1633     oldsave = scopestack[scopestack_ix - 1];
1634     LEAVE_SCOPE(oldsave);
1635     return cx->blk_loop.redo_op;
1636 }
1637
1638 static OP* lastgotoprobe;
1639
1640 static OP *
1641 dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
1642 {
1643     OP *kid;
1644     OP **ops = opstack;
1645     static char too_deep[] = "Target of goto is too deeply nested";
1646
1647     if (ops >= oplimit)
1648         croak(too_deep);
1649     if (o->op_type == OP_LEAVE ||
1650         o->op_type == OP_SCOPE ||
1651         o->op_type == OP_LEAVELOOP ||
1652         o->op_type == OP_LEAVETRY)
1653     {
1654         *ops++ = cUNOPo->op_first;
1655         if (ops >= oplimit)
1656             croak(too_deep);
1657     }
1658     *ops = 0;
1659     if (o->op_flags & OPf_KIDS) {
1660         /* First try all the kids at this level, since that's likeliest. */
1661         for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1662             if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1663                     kCOP->cop_label && strEQ(kCOP->cop_label, label))
1664                 return kid;
1665         }
1666         for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1667             if (kid == lastgotoprobe)
1668                 continue;
1669             if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1670                 (ops == opstack ||
1671                  (ops[-1]->op_type != OP_NEXTSTATE &&
1672                   ops[-1]->op_type != OP_DBSTATE)))
1673                 *ops++ = kid;
1674             if (o = dofindlabel(kid, label, ops, oplimit))
1675                 return o;
1676         }
1677     }
1678     *ops = 0;
1679     return 0;
1680 }
1681
1682 PP(pp_dump)
1683 {
1684     return pp_goto(ARGS);
1685     /*NOTREACHED*/
1686 }
1687
1688 PP(pp_goto)
1689 {
1690     djSP;
1691     OP *retop = 0;
1692     I32 ix;
1693     register PERL_CONTEXT *cx;
1694 #define GOTO_DEPTH 64
1695     OP *enterops[GOTO_DEPTH];
1696     char *label;
1697     int do_dump = (op->op_type == OP_DUMP);
1698
1699     label = 0;
1700     if (op->op_flags & OPf_STACKED) {
1701         SV *sv = POPs;
1702
1703         /* This egregious kludge implements goto &subroutine */
1704         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1705             I32 cxix;
1706             register PERL_CONTEXT *cx;
1707             CV* cv = (CV*)SvRV(sv);
1708             SV** mark;
1709             I32 items = 0;
1710             I32 oldsave;
1711
1712             if (!CvROOT(cv) && !CvXSUB(cv)) {
1713                 if (CvGV(cv)) {
1714                     SV *tmpstr = sv_newmortal();
1715                     gv_efullname3(tmpstr, CvGV(cv), Nullch);
1716                     DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1717                 }
1718                 DIE("Goto undefined subroutine");
1719             }
1720
1721             /* First do some returnish stuff. */
1722             cxix = dopoptosub(cxstack_ix);
1723             if (cxix < 0)
1724                 DIE("Can't goto subroutine outside a subroutine");
1725             if (cxix < cxstack_ix)
1726                 dounwind(cxix);
1727             TOPBLOCK(cx);
1728             mark = stack_sp;
1729             if (cx->blk_sub.hasargs) {   /* put @_ back onto stack */
1730                 AV* av = cx->blk_sub.argarray;
1731                 
1732                 items = AvFILL(av) + 1;
1733                 stack_sp++;
1734                 EXTEND(stack_sp, items); /* @_ could have been extended. */
1735                 Copy(AvARRAY(av), stack_sp, items, SV*);
1736                 stack_sp += items;
1737 #ifndef USE_THREADS
1738                 SvREFCNT_dec(GvAV(defgv));
1739                 GvAV(defgv) = cx->blk_sub.savearray;
1740 #endif /* USE_THREADS */
1741                 AvREAL_off(av);
1742                 av_clear(av);
1743             }
1744             if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1745                 SvREFCNT_dec(cx->blk_sub.cv);
1746             oldsave = scopestack[scopestack_ix - 1];
1747             LEAVE_SCOPE(oldsave);
1748
1749             /* Now do some callish stuff. */
1750             SAVETMPS;
1751             if (CvXSUB(cv)) {
1752                 if (CvOLDSTYLE(cv)) {
1753                     I32 (*fp3)_((int,int,int));
1754                     while (sp > mark) {
1755                         sp[1] = sp[0];
1756                         sp--;
1757                     }
1758                     fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1759                     items = (*fp3)(CvXSUBANY(cv).any_i32,
1760                                    mark - stack_base + 1,
1761                                    items);
1762                     sp = stack_base + items;
1763                 }
1764                 else {
1765                     stack_sp--;         /* There is no cv arg. */
1766                     (void)(*CvXSUB(cv))(cv);
1767                 }
1768                 LEAVE;
1769                 return pop_return();
1770             }
1771             else {
1772                 AV* padlist = CvPADLIST(cv);
1773                 SV** svp = AvARRAY(padlist);
1774                 cx->blk_sub.cv = cv;
1775                 cx->blk_sub.olddepth = CvDEPTH(cv);
1776                 CvDEPTH(cv)++;
1777                 if (CvDEPTH(cv) < 2)
1778                     (void)SvREFCNT_inc(cv);
1779                 else {  /* save temporaries on recursion? */
1780                     if (CvDEPTH(cv) == 100 && dowarn)
1781                         sub_crush_depth(cv);
1782                     if (CvDEPTH(cv) > AvFILL(padlist)) {
1783                         AV *newpad = newAV();
1784                         SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
1785                         I32 ix = AvFILL((AV*)svp[1]);
1786                         svp = AvARRAY(svp[0]);
1787                         for ( ;ix > 0; ix--) {
1788                             if (svp[ix] != &sv_undef) {
1789                                 char *name = SvPVX(svp[ix]);
1790                                 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1791                                     || *name == '&')
1792                                 {
1793                                     /* outer lexical or anon code */
1794                                     av_store(newpad, ix,
1795                                         SvREFCNT_inc(oldpad[ix]) );
1796                                 }
1797                                 else {          /* our own lexical */
1798                                     if (*name == '@')
1799                                         av_store(newpad, ix, sv = (SV*)newAV());
1800                                     else if (*name == '%')
1801                                         av_store(newpad, ix, sv = (SV*)newHV());
1802                                     else
1803                                         av_store(newpad, ix, sv = NEWSV(0,0));
1804                                     SvPADMY_on(sv);
1805                                 }
1806                             }
1807                             else {
1808                                 av_store(newpad, ix, sv = NEWSV(0,0));
1809                                 SvPADTMP_on(sv);
1810                             }
1811                         }
1812                         if (cx->blk_sub.hasargs) {
1813                             AV* av = newAV();
1814                             av_extend(av, 0);
1815                             av_store(newpad, 0, (SV*)av);
1816                             AvFLAGS(av) = AVf_REIFY;
1817                         }
1818                         av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1819                         AvFILL(padlist) = CvDEPTH(cv);
1820                         svp = AvARRAY(padlist);
1821                     }
1822                 }
1823 #ifdef USE_THREADS
1824                 if (!cx->blk_sub.hasargs) {
1825                     AV* av = (AV*)curpad[0];
1826                     
1827                     items = AvFILL(av) + 1;
1828                     if (items) {
1829                         /* Mark is at the end of the stack. */
1830                         EXTEND(sp, items);
1831                         Copy(AvARRAY(av), sp + 1, items, SV*);
1832                         sp += items;
1833                         PUTBACK ;                   
1834                     }
1835                 }
1836 #endif /* USE_THREADS */                
1837                 SAVESPTR(curpad);
1838                 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1839 #ifndef USE_THREADS
1840                 if (cx->blk_sub.hasargs)
1841 #endif /* USE_THREADS */
1842                 {
1843                     AV* av = (AV*)curpad[0];
1844                     SV** ary;
1845
1846 #ifndef USE_THREADS
1847                     cx->blk_sub.savearray = GvAV(defgv);
1848                     GvAV(defgv) = (AV*)SvREFCNT_inc(av);
1849 #endif /* USE_THREADS */
1850                     cx->blk_sub.argarray = av;
1851                     ++mark;
1852
1853                     if (items >= AvMAX(av) + 1) {
1854                         ary = AvALLOC(av);
1855                         if (AvARRAY(av) != ary) {
1856                             AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1857                             SvPVX(av) = (char*)ary;
1858                         }
1859                         if (items >= AvMAX(av) + 1) {
1860                             AvMAX(av) = items - 1;
1861                             Renew(ary,items+1,SV*);
1862                             AvALLOC(av) = ary;
1863                             SvPVX(av) = (char*)ary;
1864                         }
1865                     }
1866                     Copy(mark,AvARRAY(av),items,SV*);
1867                     AvFILL(av) = items - 1;
1868                     
1869                     while (items--) {
1870                         if (*mark)
1871                             SvTEMP_off(*mark);
1872                         mark++;
1873                     }
1874                 }
1875                 if (PERLDB_SUB && curstash != debstash) {
1876                     /*
1877                      * We do not care about using sv to call CV;
1878                      * it's for informational purposes only.
1879                      */
1880                     SV *sv = GvSV(DBsub);
1881                     save_item(sv);
1882                     gv_efullname3(sv, CvGV(cv), Nullch);
1883                 }
1884                 RETURNOP(CvSTART(cv));
1885             }
1886         }
1887         else
1888             label = SvPV(sv,na);
1889     }
1890     else if (op->op_flags & OPf_SPECIAL) {
1891         if (! do_dump)
1892             DIE("goto must have label");
1893     }
1894     else
1895         label = cPVOP->op_pv;
1896
1897     if (label && *label) {
1898         OP *gotoprobe = 0;
1899
1900         /* find label */
1901
1902         lastgotoprobe = 0;
1903         *enterops = 0;
1904         for (ix = cxstack_ix; ix >= 0; ix--) {
1905             cx = &cxstack[ix];
1906             switch (cx->cx_type) {
1907             case CXt_EVAL:
1908                 gotoprobe = eval_root; /* XXX not good for nested eval */
1909                 break;
1910             case CXt_LOOP:
1911                 gotoprobe = cx->blk_oldcop->op_sibling;
1912                 break;
1913             case CXt_SUBST:
1914                 continue;
1915             case CXt_BLOCK:
1916                 if (ix)
1917                     gotoprobe = cx->blk_oldcop->op_sibling;
1918                 else
1919                     gotoprobe = main_root;
1920                 break;
1921             case CXt_SUB:
1922                 if (CvDEPTH(cx->blk_sub.cv)) {
1923                     gotoprobe = CvROOT(cx->blk_sub.cv);
1924                     break;
1925                 }
1926                 /* FALL THROUGH */
1927             case CXt_NULL:
1928                 DIE("Can't \"goto\" outside a block");
1929             default:
1930                 if (ix)
1931                     DIE("panic: goto");
1932                 gotoprobe = main_root;
1933                 break;
1934             }
1935             retop = dofindlabel(gotoprobe, label,
1936                                 enterops, enterops + GOTO_DEPTH);
1937             if (retop)
1938                 break;
1939             lastgotoprobe = gotoprobe;
1940         }
1941         if (!retop)
1942             DIE("Can't find label %s", label);
1943
1944         /* pop unwanted frames */
1945
1946         if (ix < cxstack_ix) {
1947             I32 oldsave;
1948
1949             if (ix < 0)
1950                 ix = 0;
1951             dounwind(ix);
1952             TOPBLOCK(cx);
1953             oldsave = scopestack[scopestack_ix];
1954             LEAVE_SCOPE(oldsave);
1955         }
1956
1957         /* push wanted frames */
1958
1959         if (*enterops && enterops[1]) {
1960             OP *oldop = op;
1961             for (ix = 1; enterops[ix]; ix++) {
1962                 op = enterops[ix];
1963                 /* Eventually we may want to stack the needed arguments
1964                  * for each op.  For now, we punt on the hard ones. */
1965                 if (op->op_type == OP_ENTERITER)
1966                     DIE("Can't \"goto\" into the middle of a foreach loop",
1967                         label);
1968                 (*op->op_ppaddr)(ARGS);
1969             }
1970             op = oldop;
1971         }
1972     }
1973
1974     if (do_dump) {
1975 #ifdef VMS
1976         if (!retop) retop = main_start;
1977 #endif
1978         restartop = retop;
1979         do_undump = TRUE;
1980
1981         my_unexec();
1982
1983         restartop = 0;          /* hmm, must be GNU unexec().. */
1984         do_undump = FALSE;
1985     }
1986
1987     if (curstack == signalstack) {
1988         restartop = retop;
1989         JMPENV_JUMP(3);
1990     }
1991
1992     RETURNOP(retop);
1993 }
1994
1995 PP(pp_exit)
1996 {
1997     djSP;
1998     I32 anum;
1999
2000     if (MAXARG < 1)
2001         anum = 0;
2002     else {
2003         anum = SvIVx(POPs);
2004 #ifdef VMSISH_EXIT
2005         if (anum == 1 && VMSISH_EXIT)
2006             anum = 0;
2007 #endif
2008     }
2009     my_exit(anum);
2010     PUSHs(&sv_undef);
2011     RETURN;
2012 }
2013
2014 #ifdef NOTYET
2015 PP(pp_nswitch)
2016 {
2017     djSP;
2018     double value = SvNVx(GvSV(cCOP->cop_gv));
2019     register I32 match = I_32(value);
2020
2021     if (value < 0.0) {
2022         if (((double)match) > value)
2023             --match;            /* was fractional--truncate other way */
2024     }
2025     match -= cCOP->uop.scop.scop_offset;
2026     if (match < 0)
2027         match = 0;
2028     else if (match > cCOP->uop.scop.scop_max)
2029         match = cCOP->uop.scop.scop_max;
2030     op = cCOP->uop.scop.scop_next[match];
2031     RETURNOP(op);
2032 }
2033
2034 PP(pp_cswitch)
2035 {
2036     djSP;
2037     register I32 match;
2038
2039     if (multiline)
2040         op = op->op_next;                       /* can't assume anything */
2041     else {
2042         match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
2043         match -= cCOP->uop.scop.scop_offset;
2044         if (match < 0)
2045             match = 0;
2046         else if (match > cCOP->uop.scop.scop_max)
2047             match = cCOP->uop.scop.scop_max;
2048         op = cCOP->uop.scop.scop_next[match];
2049     }
2050     RETURNOP(op);
2051 }
2052 #endif
2053
2054 /* Eval. */
2055
2056 static void
2057 save_lines(AV *array, SV *sv)
2058 {
2059     register char *s = SvPVX(sv);
2060     register char *send = SvPVX(sv) + SvCUR(sv);
2061     register char *t;
2062     register I32 line = 1;
2063
2064     while (s && s < send) {
2065         SV *tmpstr = NEWSV(85,0);
2066
2067         sv_upgrade(tmpstr, SVt_PVMG);
2068         t = strchr(s, '\n');
2069         if (t)
2070             t++;
2071         else
2072             t = send;
2073
2074         sv_setpvn(tmpstr, s, t - s);
2075         av_store(array, line++, tmpstr);
2076         s = t;
2077     }
2078 }
2079
2080 static OP *
2081 docatch(OP *o)
2082 {
2083     dTHR;
2084     int ret;
2085     OP *oldop = op;
2086     dJMPENV;
2087
2088     op = o;
2089 #ifdef DEBUGGING
2090     assert(CATCH_GET == TRUE);
2091     DEBUG_l(deb("Setting up local jumplevel %p, was %p\n", &cur_env, top_env));
2092 #endif
2093     JMPENV_PUSH(ret);
2094     switch (ret) {
2095     default:                            /* topmost level handles it */
2096         JMPENV_POP;
2097         op = oldop;
2098         JMPENV_JUMP(ret);
2099         /* NOTREACHED */
2100     case 3:
2101         if (!restartop) {
2102             PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2103             break;
2104         }
2105         op = restartop;
2106         restartop = 0;
2107         /* FALL THROUGH */
2108     case 0:
2109         runops();
2110         break;
2111     }
2112     JMPENV_POP;
2113     op = oldop;
2114     return Nullop;
2115 }
2116
2117 OP *
2118 sv_compile_2op(SV *sv, OP** startop, char *code, AV** avp)
2119 /* sv Text to convert to OP tree. */
2120 /* startop op_free() this to undo. */
2121 /* code Short string id of the caller. */
2122 {
2123     dSP;                                /* Make POPBLOCK work. */
2124     PERL_CONTEXT *cx;
2125     SV **newsp;
2126     I32 gimme = 0;   /* SUSPECT - INITIALZE TO WHAT?  NI-S */
2127     I32 optype;
2128     OP dummy;
2129     OP *oop = op, *rop;
2130     char tmpbuf[TYPE_DIGITS(long) + 12 + 10];
2131     char *safestr;
2132
2133     ENTER;
2134     lex_start(sv);
2135     SAVETMPS;
2136     /* switch to eval mode */
2137
2138     SAVESPTR(compiling.cop_filegv);
2139     SAVEI16(compiling.cop_line);
2140     sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++evalseq);
2141     compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2142     compiling.cop_line = 1;
2143     /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2144        deleting the eval's FILEGV from the stash before gv_check() runs
2145        (i.e. before run-time proper). To work around the coredump that
2146        ensues, we always turn GvMULTI_on for any globals that were
2147        introduced within evals. See force_ident(). GSAR 96-10-12 */
2148     safestr = savepv(tmpbuf);
2149     SAVEDELETE(defstash, safestr, strlen(safestr));
2150     SAVEI32(hints);
2151     SAVEPPTR(op);
2152     hints = 0;
2153
2154     op = &dummy;
2155     op->op_type = 0;                    /* Avoid uninit warning. */
2156     op->op_flags = 0;                   /* Avoid uninit warning. */
2157     PUSHBLOCK(cx, CXt_EVAL, SP);
2158     PUSHEVAL(cx, 0, compiling.cop_filegv);
2159     rop = doeval(G_SCALAR, startop);
2160     POPBLOCK(cx,curpm);
2161     POPEVAL(cx);
2162
2163     (*startop)->op_type = OP_NULL;
2164     (*startop)->op_ppaddr = ppaddr[OP_NULL];
2165     lex_end();
2166     *avp = (AV*)SvREFCNT_inc(comppad);
2167     LEAVE;
2168     return rop;
2169 }
2170
2171 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2172 static OP *
2173 doeval(int gimme, OP** startop)
2174 {
2175     dSP;
2176     OP *saveop = op;
2177     HV *newstash;
2178     CV *caller;
2179     AV* comppadlist;
2180
2181     in_eval = 1;
2182
2183     PUSHMARK(SP);
2184
2185     /* set up a scratch pad */
2186
2187     SAVEI32(padix);
2188     SAVESPTR(curpad);
2189     SAVESPTR(comppad);
2190     SAVESPTR(comppad_name);
2191     SAVEI32(comppad_name_fill);
2192     SAVEI32(min_intro_pending);
2193     SAVEI32(max_intro_pending);
2194
2195     caller = compcv;
2196     SAVESPTR(compcv);
2197     compcv = (CV*)NEWSV(1104,0);
2198     sv_upgrade((SV *)compcv, SVt_PVCV);
2199     CvUNIQUE_on(compcv);
2200 #ifdef USE_THREADS
2201     CvOWNER(compcv) = 0;
2202     New(666, CvMUTEXP(compcv), 1, perl_mutex);
2203     MUTEX_INIT(CvMUTEXP(compcv));
2204 #endif /* USE_THREADS */
2205
2206     comppad = newAV();
2207     av_push(comppad, Nullsv);
2208     curpad = AvARRAY(comppad);
2209     comppad_name = newAV();
2210     comppad_name_fill = 0;
2211     min_intro_pending = 0;
2212     padix = 0;
2213 #ifdef USE_THREADS
2214     av_store(comppad_name, 0, newSVpv("@_", 2));
2215     curpad[0] = (SV*)newAV();
2216     SvPADMY_on(curpad[0]);      /* XXX Needed? */
2217 #endif /* USE_THREADS */
2218
2219     comppadlist = newAV();
2220     AvREAL_off(comppadlist);
2221     av_store(comppadlist, 0, (SV*)comppad_name);
2222     av_store(comppadlist, 1, (SV*)comppad);
2223     CvPADLIST(compcv) = comppadlist;
2224
2225     if (!saveop || saveop->op_type != OP_REQUIRE)
2226         CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
2227
2228     SAVEFREESV(compcv);
2229
2230     /* make sure we compile in the right package */
2231
2232     newstash = curcop->cop_stash;
2233     if (curstash != newstash) {
2234         SAVESPTR(curstash);
2235         curstash = newstash;
2236     }
2237     SAVESPTR(beginav);
2238     beginav = newAV();
2239     SAVEFREESV(beginav);
2240
2241     /* try to compile it */
2242
2243     eval_root = Nullop;
2244     error_count = 0;
2245     curcop = &compiling;
2246     curcop->cop_arybase = 0;
2247     SvREFCNT_dec(rs);
2248     rs = newSVpv("\n", 1);
2249     if (saveop && saveop->op_flags & OPf_SPECIAL)
2250         in_eval |= 4;
2251     else
2252         sv_setpv(ERRSV,"");
2253     if (yyparse() || error_count || !eval_root) {
2254         SV **newsp;
2255         I32 gimme;
2256         PERL_CONTEXT *cx;
2257         I32 optype = 0;                 /* Might be reset by POPEVAL. */
2258
2259         op = saveop;
2260         if (eval_root) {
2261             op_free(eval_root);
2262             eval_root = Nullop;
2263         }
2264         SP = stack_base + POPMARK;              /* pop original mark */
2265         if (!startop) {
2266             POPBLOCK(cx,curpm);
2267             POPEVAL(cx);
2268             pop_return();
2269         }
2270         lex_end();
2271         LEAVE;
2272         if (optype == OP_REQUIRE) {
2273             char* msg = SvPVx(ERRSV, na);
2274             DIE("%s", *msg ? msg : "Compilation failed in require");
2275         } else if (startop) {
2276             char* msg = SvPVx(ERRSV, na);
2277
2278             POPBLOCK(cx,curpm);
2279             POPEVAL(cx);
2280             croak("%sCompilation failed in regexp", (*msg ? msg : "Unknown error\n"));
2281         }
2282         SvREFCNT_dec(rs);
2283         rs = SvREFCNT_inc(nrs);
2284 #ifdef USE_THREADS
2285         MUTEX_LOCK(&eval_mutex);
2286         eval_owner = 0;
2287         COND_SIGNAL(&eval_cond);
2288         MUTEX_UNLOCK(&eval_mutex);
2289 #endif /* USE_THREADS */
2290         RETPUSHUNDEF;
2291     }
2292     SvREFCNT_dec(rs);
2293     rs = SvREFCNT_inc(nrs);
2294     compiling.cop_line = 0;
2295     if (startop) {
2296         *startop = eval_root;
2297         SvREFCNT_dec(CvOUTSIDE(compcv));
2298         CvOUTSIDE(compcv) = Nullcv;
2299     } else
2300         SAVEFREEOP(eval_root);
2301     if (gimme & G_VOID)
2302         scalarvoid(eval_root);
2303     else if (gimme & G_ARRAY)
2304         list(eval_root);
2305     else
2306         scalar(eval_root);
2307
2308     DEBUG_x(dump_eval());
2309
2310     /* Register with debugger: */
2311     if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2312         CV *cv = perl_get_cv("DB::postponed", FALSE);
2313         if (cv) {
2314             dSP;
2315             PUSHMARK(sp);
2316             XPUSHs((SV*)compiling.cop_filegv);
2317             PUTBACK;
2318             perl_call_sv((SV*)cv, G_DISCARD);
2319         }
2320     }
2321
2322     /* compiled okay, so do it */
2323
2324     CvDEPTH(compcv) = 1;
2325     SP = stack_base + POPMARK;          /* pop original mark */
2326     op = saveop;                        /* The caller may need it. */
2327 #ifdef USE_THREADS
2328     MUTEX_LOCK(&eval_mutex);
2329     eval_owner = 0;
2330     COND_SIGNAL(&eval_cond);
2331     MUTEX_UNLOCK(&eval_mutex);
2332 #endif /* USE_THREADS */
2333
2334     RETURNOP(eval_start);
2335 }
2336
2337 PP(pp_require)
2338 {
2339     djSP;
2340     register PERL_CONTEXT *cx;
2341     SV *sv;
2342     char *name;
2343     char *tryname;
2344     SV *namesv = Nullsv;
2345     SV** svp;
2346     I32 gimme = G_SCALAR;
2347     PerlIO *tryrsfp = 0;
2348
2349     sv = POPs;
2350     if (SvNIOKp(sv) && !SvPOKp(sv)) {
2351         SET_NUMERIC_STANDARD();
2352         if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2353             DIE("Perl %s required--this is only version %s, stopped",
2354                 SvPV(sv,na),patchlevel);
2355         RETPUSHYES;
2356     }
2357     name = SvPV(sv, na);
2358     if (!*name)
2359         DIE("Null filename used");
2360     TAINT_PROPER("require");
2361     if (op->op_type == OP_REQUIRE &&
2362       (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2363       *svp != &sv_undef)
2364         RETPUSHYES;
2365
2366     /* prepare to compile file */
2367
2368     if (*name == '/' ||
2369         (*name == '.' && 
2370             (name[1] == '/' ||
2371              (name[1] == '.' && name[2] == '/')))
2372 #ifdef DOSISH
2373       || (name[0] && name[1] == ':')
2374 #endif
2375 #ifdef WIN32
2376       || (name[0] == '\\' && name[1] == '\\')   /* UNC path */
2377 #endif
2378 #ifdef VMS
2379         || (strchr(name,':')  || ((*name == '[' || *name == '<') &&
2380             (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
2381 #endif
2382     )
2383     {
2384         tryname = name;
2385         tryrsfp = PerlIO_open(name,PERL_SCRIPT_MODE);
2386     }
2387     else {
2388         AV *ar = GvAVn(incgv);
2389         I32 i;
2390 #ifdef VMS
2391         char *unixname;
2392         if ((unixname = tounixspec(name, Nullch)) != Nullch)
2393 #endif
2394         {
2395             namesv = NEWSV(806, 0);
2396             for (i = 0; i <= AvFILL(ar); i++) {
2397                 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2398 #ifdef VMS
2399                 char *unixdir;
2400                 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2401                     continue;
2402                 sv_setpv(namesv, unixdir);
2403                 sv_catpv(namesv, unixname);
2404 #else
2405                 sv_setpvf(namesv, "%s/%s", dir, name);
2406 #endif
2407                 tryname = SvPVX(namesv);
2408                 tryrsfp = PerlIO_open(tryname, PERL_SCRIPT_MODE);
2409                 if (tryrsfp) {
2410                     if (tryname[0] == '.' && tryname[1] == '/')
2411                         tryname += 2;
2412                     break;
2413                 }
2414             }
2415         }
2416     }
2417     SAVESPTR(compiling.cop_filegv);
2418     compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2419     SvREFCNT_dec(namesv);
2420     if (!tryrsfp) {
2421         if (op->op_type == OP_REQUIRE) {
2422             SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2423             SV *dirmsgsv = NEWSV(0, 0);
2424             AV *ar = GvAVn(incgv);
2425             I32 i;
2426             if (instr(SvPVX(msg), ".h "))
2427                 sv_catpv(msg, " (change .h to .ph maybe?)");
2428             if (instr(SvPVX(msg), ".ph "))
2429                 sv_catpv(msg, " (did you run h2ph?)");
2430             sv_catpv(msg, " (@INC contains:");
2431             for (i = 0; i <= AvFILL(ar); i++) {
2432                 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2433                 sv_setpvf(dirmsgsv, " %s", dir);
2434                 sv_catsv(msg, dirmsgsv);
2435             }
2436             sv_catpvn(msg, ")", 1);
2437             SvREFCNT_dec(dirmsgsv);
2438             DIE("%_", msg);
2439         }
2440
2441         RETPUSHUNDEF;
2442     }
2443
2444     /* Assume success here to prevent recursive requirement. */
2445     (void)hv_store(GvHVn(incgv), name, strlen(name),
2446         newSVsv(GvSV(compiling.cop_filegv)), 0 );
2447
2448     ENTER;
2449     SAVETMPS;
2450     lex_start(sv_2mortal(newSVpv("",0)));
2451     if (rsfp_filters){
2452         save_aptr(&rsfp_filters);
2453         rsfp_filters = NULL;
2454     }
2455
2456     rsfp = tryrsfp;
2457     name = savepv(name);
2458     SAVEFREEPV(name);
2459     SAVEI32(hints);
2460     hints = 0;
2461  
2462     /* switch to eval mode */
2463
2464     push_return(op->op_next);
2465     PUSHBLOCK(cx, CXt_EVAL, SP);
2466     PUSHEVAL(cx, name, compiling.cop_filegv);
2467
2468     compiling.cop_line = 0;
2469
2470     PUTBACK;
2471 #ifdef USE_THREADS
2472     MUTEX_LOCK(&eval_mutex);
2473     if (eval_owner && eval_owner != thr)
2474         while (eval_owner)
2475             COND_WAIT(&eval_cond, &eval_mutex);
2476     eval_owner = thr;
2477     MUTEX_UNLOCK(&eval_mutex);
2478 #endif /* USE_THREADS */
2479     return DOCATCH(doeval(G_SCALAR, NULL));
2480 }
2481
2482 PP(pp_dofile)
2483 {
2484     return pp_require(ARGS);
2485 }
2486
2487 PP(pp_entereval)
2488 {
2489     djSP;
2490     register PERL_CONTEXT *cx;
2491     dPOPss;
2492     I32 gimme = GIMME_V, was = sub_generation;
2493     char tmpbuf[TYPE_DIGITS(long) + 12];
2494     char *safestr;
2495     STRLEN len;
2496     OP *ret;
2497
2498     if (!SvPV(sv,len) || !len)
2499         RETPUSHUNDEF;
2500     TAINT_PROPER("eval");
2501
2502     ENTER;
2503     lex_start(sv);
2504     SAVETMPS;
2505  
2506     /* switch to eval mode */
2507
2508     SAVESPTR(compiling.cop_filegv);
2509     sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
2510     compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2511     compiling.cop_line = 1;
2512     /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2513        deleting the eval's FILEGV from the stash before gv_check() runs
2514        (i.e. before run-time proper). To work around the coredump that
2515        ensues, we always turn GvMULTI_on for any globals that were
2516        introduced within evals. See force_ident(). GSAR 96-10-12 */
2517     safestr = savepv(tmpbuf);
2518     SAVEDELETE(defstash, safestr, strlen(safestr));
2519     SAVEI32(hints);
2520     hints = op->op_targ;
2521
2522     push_return(op->op_next);
2523     PUSHBLOCK(cx, CXt_EVAL, SP);
2524     PUSHEVAL(cx, 0, compiling.cop_filegv);
2525
2526     /* prepare to compile string */
2527
2528     if (PERLDB_LINE && curstash != debstash)
2529         save_lines(GvAV(compiling.cop_filegv), linestr);
2530     PUTBACK;
2531 #ifdef USE_THREADS
2532     MUTEX_LOCK(&eval_mutex);
2533     if (eval_owner && eval_owner != thr)
2534         while (eval_owner)
2535             COND_WAIT(&eval_cond, &eval_mutex);
2536     eval_owner = thr;
2537     MUTEX_UNLOCK(&eval_mutex);
2538 #endif /* USE_THREADS */
2539     ret = doeval(gimme, NULL);
2540     if (PERLDB_INTER && was != sub_generation /* Some subs defined here. */
2541         && ret != op->op_next) {        /* Successive compilation. */
2542         strcpy(safestr, "_<(eval )");   /* Anything fake and short. */
2543     }
2544     return DOCATCH(ret);
2545 }
2546
2547 PP(pp_leaveeval)
2548 {
2549     djSP;
2550     register SV **mark;
2551     SV **newsp;
2552     PMOP *newpm;
2553     I32 gimme;
2554     register PERL_CONTEXT *cx;
2555     OP *retop;
2556     U8 save_flags = op -> op_flags;
2557     I32 optype;
2558
2559     POPBLOCK(cx,newpm);
2560     POPEVAL(cx);
2561     retop = pop_return();
2562
2563     TAINT_NOT;
2564     if (gimme == G_VOID)
2565         MARK = newsp;
2566     else if (gimme == G_SCALAR) {
2567         MARK = newsp + 1;
2568         if (MARK <= SP) {
2569             if (SvFLAGS(TOPs) & SVs_TEMP)
2570                 *MARK = TOPs;
2571             else
2572                 *MARK = sv_mortalcopy(TOPs);
2573         }
2574         else {
2575             MEXTEND(mark,0);
2576             *MARK = &sv_undef;
2577         }
2578     }
2579     else {
2580         /* in case LEAVE wipes old return values */
2581         for (mark = newsp + 1; mark <= SP; mark++) {
2582             if (!(SvFLAGS(*mark) & SVs_TEMP)) {
2583                 *mark = sv_mortalcopy(*mark);
2584                 TAINT_NOT;      /* Each item is independent */
2585             }
2586         }
2587     }
2588     curpm = newpm;      /* Don't pop $1 et al till now */
2589
2590     /*
2591      * Closures mentioned at top level of eval cannot be referenced
2592      * again, and their presence indirectly causes a memory leak.
2593      * (Note that the fact that compcv and friends are still set here
2594      * is, AFAIK, an accident.)  --Chip
2595      */
2596     if (AvFILL(comppad_name) >= 0) {
2597         SV **svp = AvARRAY(comppad_name);
2598         I32 ix;
2599         for (ix = AvFILL(comppad_name); ix >= 0; ix--) {
2600             SV *sv = svp[ix];
2601             if (sv && sv != &sv_undef && *SvPVX(sv) == '&') {
2602                 SvREFCNT_dec(sv);
2603                 svp[ix] = &sv_undef;
2604
2605                 sv = curpad[ix];
2606                 if (CvCLONE(sv)) {
2607                     SvREFCNT_dec(CvOUTSIDE(sv));
2608                     CvOUTSIDE(sv) = Nullcv;
2609                 }
2610                 else {
2611                     SvREFCNT_dec(sv);
2612                     sv = NEWSV(0,0);
2613                     SvPADTMP_on(sv);
2614                     curpad[ix] = sv;
2615                 }
2616             }
2617         }
2618     }
2619
2620 #ifdef DEBUGGING
2621     assert(CvDEPTH(compcv) == 1);
2622 #endif
2623     CvDEPTH(compcv) = 0;
2624
2625     if (optype == OP_REQUIRE &&
2626         !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp))
2627     {
2628         /* Unassume the success we assumed earlier. */
2629         char *name = cx->blk_eval.old_name;
2630         (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2631         retop = die("%s did not return a true value", name);
2632     }
2633
2634     lex_end();
2635     LEAVE;
2636
2637     if (!(save_flags & OPf_SPECIAL))
2638         sv_setpv(ERRSV,"");
2639
2640     RETURNOP(retop);
2641 }
2642
2643 PP(pp_entertry)
2644 {
2645     djSP;
2646     register PERL_CONTEXT *cx;
2647     I32 gimme = GIMME_V;
2648
2649     ENTER;
2650     SAVETMPS;
2651
2652     push_return(cLOGOP->op_other->op_next);
2653     PUSHBLOCK(cx, CXt_EVAL, SP);
2654     PUSHEVAL(cx, 0, 0);
2655     eval_root = op;             /* Only needed so that goto works right. */
2656
2657     in_eval = 1;
2658     sv_setpv(ERRSV,"");
2659     PUTBACK;
2660     return DOCATCH(op->op_next);
2661 }
2662
2663 PP(pp_leavetry)
2664 {
2665     djSP;
2666     register SV **mark;
2667     SV **newsp;
2668     PMOP *newpm;
2669     I32 gimme;
2670     register PERL_CONTEXT *cx;
2671     I32 optype;
2672
2673     POPBLOCK(cx,newpm);
2674     POPEVAL(cx);
2675     pop_return();
2676
2677     TAINT_NOT;
2678     if (gimme == G_VOID)
2679         SP = newsp;
2680     else if (gimme == G_SCALAR) {
2681         MARK = newsp + 1;
2682         if (MARK <= SP) {
2683             if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2684                 *MARK = TOPs;
2685             else
2686                 *MARK = sv_mortalcopy(TOPs);
2687         }
2688         else {
2689             MEXTEND(mark,0);
2690             *MARK = &sv_undef;
2691         }
2692         SP = MARK;
2693     }
2694     else {
2695         /* in case LEAVE wipes old return values */
2696         for (mark = newsp + 1; mark <= SP; mark++) {
2697             if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
2698                 *mark = sv_mortalcopy(*mark);
2699                 TAINT_NOT;      /* Each item is independent */
2700             }
2701         }
2702     }
2703     curpm = newpm;      /* Don't pop $1 et al till now */
2704
2705     LEAVE;
2706     sv_setpv(ERRSV,"");
2707     RETURN;
2708 }
2709
2710 static void
2711 doparseform(SV *sv)
2712 {
2713     STRLEN len;
2714     register char *s = SvPV_force(sv, len);
2715     register char *send = s + len;
2716     register char *base;
2717     register I32 skipspaces = 0;
2718     bool noblank;
2719     bool repeat;
2720     bool postspace = FALSE;
2721     U16 *fops;
2722     register U16 *fpc;
2723     U16 *linepc;
2724     register I32 arg;
2725     bool ischop;
2726
2727     if (len == 0)
2728         croak("Null picture in formline");
2729     
2730     New(804, fops, (send - s)*3+10, U16);    /* Almost certainly too long... */
2731     fpc = fops;
2732
2733     if (s < send) {
2734         linepc = fpc;
2735         *fpc++ = FF_LINEMARK;
2736         noblank = repeat = FALSE;
2737         base = s;
2738     }
2739
2740     while (s <= send) {
2741         switch (*s++) {
2742         default:
2743             skipspaces = 0;
2744             continue;
2745
2746         case '~':
2747             if (*s == '~') {
2748                 repeat = TRUE;
2749                 *s = ' ';
2750             }
2751             noblank = TRUE;
2752             s[-1] = ' ';
2753             /* FALL THROUGH */
2754         case ' ': case '\t':
2755             skipspaces++;
2756             continue;
2757             
2758         case '\n': case 0:
2759             arg = s - base;
2760             skipspaces++;
2761             arg -= skipspaces;
2762             if (arg) {
2763                 if (postspace)
2764                     *fpc++ = FF_SPACE;
2765                 *fpc++ = FF_LITERAL;
2766                 *fpc++ = arg;
2767             }
2768             postspace = FALSE;
2769             if (s <= send)
2770                 skipspaces--;
2771             if (skipspaces) {
2772                 *fpc++ = FF_SKIP;
2773                 *fpc++ = skipspaces;
2774             }
2775             skipspaces = 0;
2776             if (s <= send)
2777                 *fpc++ = FF_NEWLINE;
2778             if (noblank) {
2779                 *fpc++ = FF_BLANK;
2780                 if (repeat)
2781                     arg = fpc - linepc + 1;
2782                 else
2783                     arg = 0;
2784                 *fpc++ = arg;
2785             }
2786             if (s < send) {
2787                 linepc = fpc;
2788                 *fpc++ = FF_LINEMARK;
2789                 noblank = repeat = FALSE;
2790                 base = s;
2791             }
2792             else
2793                 s++;
2794             continue;
2795
2796         case '@':
2797         case '^':
2798             ischop = s[-1] == '^';
2799
2800             if (postspace) {
2801                 *fpc++ = FF_SPACE;
2802                 postspace = FALSE;
2803             }
2804             arg = (s - base) - 1;
2805             if (arg) {
2806                 *fpc++ = FF_LITERAL;
2807                 *fpc++ = arg;
2808             }
2809
2810             base = s - 1;
2811             *fpc++ = FF_FETCH;
2812             if (*s == '*') {
2813                 s++;
2814                 *fpc++ = 0;
2815                 *fpc++ = FF_LINEGLOB;
2816             }
2817             else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2818                 arg = ischop ? 512 : 0;
2819                 base = s - 1;
2820                 while (*s == '#')
2821                     s++;
2822                 if (*s == '.') {
2823                     char *f;
2824                     s++;
2825                     f = s;
2826                     while (*s == '#')
2827                         s++;
2828                     arg |= 256 + (s - f);
2829                 }
2830                 *fpc++ = s - base;              /* fieldsize for FETCH */
2831                 *fpc++ = FF_DECIMAL;
2832                 *fpc++ = arg;
2833             }
2834             else {
2835                 I32 prespace = 0;
2836                 bool ismore = FALSE;
2837
2838                 if (*s == '>') {
2839                     while (*++s == '>') ;
2840                     prespace = FF_SPACE;
2841                 }
2842                 else if (*s == '|') {
2843                     while (*++s == '|') ;
2844                     prespace = FF_HALFSPACE;
2845                     postspace = TRUE;
2846                 }
2847                 else {
2848                     if (*s == '<')
2849                         while (*++s == '<') ;
2850                     postspace = TRUE;
2851                 }
2852                 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2853                     s += 3;
2854                     ismore = TRUE;
2855                 }
2856                 *fpc++ = s - base;              /* fieldsize for FETCH */
2857
2858                 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2859
2860                 if (prespace)
2861                     *fpc++ = prespace;
2862                 *fpc++ = FF_ITEM;
2863                 if (ismore)
2864                     *fpc++ = FF_MORE;
2865                 if (ischop)
2866                     *fpc++ = FF_CHOP;
2867             }
2868             base = s;
2869             skipspaces = 0;
2870             continue;
2871         }
2872     }
2873     *fpc++ = FF_END;
2874
2875     arg = fpc - fops;
2876     { /* need to jump to the next word */
2877         int z;
2878         z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2879         SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2880         s = SvPVX(sv) + SvCUR(sv) + z;
2881     }
2882     Copy(fops, s, arg, U16);
2883     Safefree(fops);
2884     sv_magic(sv, Nullsv, 'f', Nullch, 0);
2885     SvCOMPILED_on(sv);
2886 }
2887
2888