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