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