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