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