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