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