Fixup Win32
[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(ERRHV, 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(ERRSV, SvCUR(ERRSV)+sizeof(prefix)+klen);
1039                     sv_catpvn(ERRSV, prefix, sizeof(prefix)-1);
1040                     sv_catpvn(ERRSV, message, klen);
1041                 }
1042                 sv_inc(*svp);
1043             }
1044         }
1045         else
1046             sv_setpv(ERRSV, 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(ERRSV, 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 PP(pp_caller)
1110 {
1111     djSP;
1112     register I32 cxix = dopoptosub(cxstack_ix);
1113     register CONTEXT *cx;
1114     I32 dbcxix;
1115     I32 gimme;
1116     SV *sv;
1117     I32 count = 0;
1118
1119     if (MAXARG)
1120         count = POPi;
1121     EXTEND(SP, 6);
1122     for (;;) {
1123         if (cxix < 0) {
1124             if (GIMME != G_ARRAY)
1125                 RETPUSHUNDEF;
1126             RETURN;
1127         }
1128         if (DBsub && cxix >= 0 &&
1129                 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1130             count++;
1131         if (!count--)
1132             break;
1133         cxix = dopoptosub(cxix - 1);
1134     }
1135     cx = &cxstack[cxix];
1136     if (cxstack[cxix].cx_type == CXt_SUB) {
1137         dbcxix = dopoptosub(cxix - 1);
1138         /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1139            field below is defined for any cx. */
1140         if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1141             cx = &cxstack[dbcxix];
1142     }
1143
1144     if (GIMME != G_ARRAY) {
1145         dTARGET;
1146
1147         sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1148         PUSHs(TARG);
1149         RETURN;
1150     }
1151
1152     PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1153     PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1154     PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1155     if (!MAXARG)
1156         RETURN;
1157     if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
1158         sv = NEWSV(49, 0);
1159         gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
1160         PUSHs(sv_2mortal(sv));
1161         PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1162     }
1163     else {
1164         PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1165         PUSHs(sv_2mortal(newSViv(0)));
1166     }
1167     gimme = (I32)cx->blk_gimme;
1168     if (gimme == G_VOID)
1169         PUSHs(&sv_undef);
1170     else
1171         PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1172     if (cx->cx_type == CXt_EVAL) {
1173         if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1174             PUSHs(cx->blk_eval.cur_text);
1175             PUSHs(&sv_no);
1176         } 
1177         else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1178             /* Require, put the name. */
1179             PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1180             PUSHs(&sv_yes);
1181         }
1182     }
1183     else if (cx->cx_type == CXt_SUB &&
1184             cx->blk_sub.hasargs &&
1185             curcop->cop_stash == debstash)
1186     {
1187         AV *ary = cx->blk_sub.argarray;
1188         int off = AvARRAY(ary) - AvALLOC(ary);
1189
1190         if (!dbargs) {
1191             GV* tmpgv;
1192             dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1193                                 SVt_PVAV)));
1194             GvMULTI_on(tmpgv);
1195             AvREAL_off(dbargs);         /* XXX Should be REIFY */
1196         }
1197
1198         if (AvMAX(dbargs) < AvFILL(ary) + off)
1199             av_extend(dbargs, AvFILL(ary) + off);
1200         Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1201         AvFILL(dbargs) = AvFILL(ary) + off;
1202     }
1203     RETURN;
1204 }
1205
1206 static int
1207 sortcv(const void *a, const void *b)
1208 {
1209     dTHR;
1210     SV * const *str1 = (SV * const *)a;
1211     SV * const *str2 = (SV * const *)b;
1212     I32 oldsaveix = savestack_ix;
1213     I32 oldscopeix = scopestack_ix;
1214     I32 result;
1215     GvSV(firstgv) = *str1;
1216     GvSV(secondgv) = *str2;
1217     stack_sp = stack_base;
1218     op = sortcop;
1219     runops();
1220     if (stack_sp != stack_base + 1)
1221         croak("Sort subroutine didn't return single value");
1222     if (!SvNIOKp(*stack_sp))
1223         croak("Sort subroutine didn't return a numeric value");
1224     result = SvIV(*stack_sp);
1225     while (scopestack_ix > oldscopeix) {
1226         LEAVE;
1227     }
1228     leave_scope(oldsaveix);
1229     return result;
1230 }
1231
1232 static int
1233 sortcmp(const void *a, const void *b)
1234 {
1235     return sv_cmp(*(SV * const *)a, *(SV * const *)b);
1236 }
1237
1238 static int
1239 sortcmp_locale(const void *a, const void *b)
1240 {
1241     return sv_cmp_locale(*(SV * const *)a, *(SV * const *)b);
1242 }
1243
1244 PP(pp_reset)
1245 {
1246     djSP;
1247     char *tmps;
1248
1249     if (MAXARG < 1)
1250         tmps = "";
1251     else
1252         tmps = POPp;
1253     sv_reset(tmps, curcop->cop_stash);
1254     PUSHs(&sv_yes);
1255     RETURN;
1256 }
1257
1258 PP(pp_lineseq)
1259 {
1260     return NORMAL;
1261 }
1262
1263 PP(pp_dbstate)
1264 {
1265     curcop = (COP*)op;
1266     TAINT_NOT;          /* Each statement is presumed innocent */
1267     stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1268     FREETMPS;
1269
1270     if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1271     {
1272         SV **sp;
1273         register CV *cv;
1274         register CONTEXT *cx;
1275         I32 gimme = G_ARRAY;
1276         I32 hasargs;
1277         GV *gv;
1278
1279         gv = DBgv;
1280         cv = GvCV(gv);
1281         if (!cv)
1282             DIE("No DB::DB routine defined");
1283
1284         if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
1285             return NORMAL;
1286
1287         ENTER;
1288         SAVETMPS;
1289
1290         SAVEI32(debug);
1291         SAVESTACK_POS();
1292         debug = 0;
1293         hasargs = 0;
1294         sp = stack_sp;
1295
1296         push_return(op->op_next);
1297         PUSHBLOCK(cx, CXt_SUB, sp);
1298         PUSHSUB(cx);
1299         CvDEPTH(cv)++;
1300         (void)SvREFCNT_inc(cv);
1301         SAVESPTR(curpad);
1302         curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1303         RETURNOP(CvSTART(cv));
1304     }
1305     else
1306         return NORMAL;
1307 }
1308
1309 PP(pp_scope)
1310 {
1311     return NORMAL;
1312 }
1313
1314 PP(pp_enteriter)
1315 {
1316     djSP; dMARK;
1317     register CONTEXT *cx;
1318     I32 gimme = GIMME_V;
1319     SV **svp;
1320
1321     ENTER;
1322     SAVETMPS;
1323
1324     if (op->op_targ)
1325         svp = &curpad[op->op_targ];             /* "my" variable */
1326     else
1327         svp = &GvSV((GV*)POPs);                 /* symbol table variable */
1328
1329     SAVESPTR(*svp);
1330
1331     ENTER;
1332
1333     PUSHBLOCK(cx, CXt_LOOP, SP);
1334     PUSHLOOP(cx, svp, MARK);
1335     if (op->op_flags & OPf_STACKED)
1336         cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1337     else {
1338         cx->blk_loop.iterary = curstack;
1339         AvFILL(curstack) = sp - stack_base;
1340         cx->blk_loop.iterix = MARK - stack_base;
1341     }
1342
1343     RETURN;
1344 }
1345
1346 PP(pp_enterloop)
1347 {
1348     djSP;
1349     register CONTEXT *cx;
1350     I32 gimme = GIMME_V;
1351
1352     ENTER;
1353     SAVETMPS;
1354     ENTER;
1355
1356     PUSHBLOCK(cx, CXt_LOOP, SP);
1357     PUSHLOOP(cx, 0, SP);
1358
1359     RETURN;
1360 }
1361
1362 PP(pp_leaveloop)
1363 {
1364     djSP;
1365     register CONTEXT *cx;
1366     struct block_loop cxloop;
1367     I32 gimme;
1368     SV **newsp;
1369     PMOP *newpm;
1370     SV **mark;
1371
1372     POPBLOCK(cx,newpm);
1373     mark = newsp;
1374     POPLOOP1(cx);       /* Delay POPLOOP2 until stack values are safe */
1375
1376     TAINT_NOT;
1377     if (gimme == G_VOID)
1378         ; /* do nothing */
1379     else if (gimme == G_SCALAR) {
1380         if (mark < SP)
1381             *++newsp = sv_mortalcopy(*SP);
1382         else
1383             *++newsp = &sv_undef;
1384     }
1385     else {
1386         while (mark < SP) {
1387             *++newsp = sv_mortalcopy(*++mark);
1388             TAINT_NOT;          /* Each item is independent */
1389         }
1390     }
1391     SP = newsp;
1392     PUTBACK;
1393
1394     POPLOOP2();         /* Stack values are safe: release loop vars ... */
1395     curpm = newpm;      /* ... and pop $1 et al */
1396
1397     LEAVE;
1398     LEAVE;
1399
1400     return NORMAL;
1401 }
1402
1403 PP(pp_return)
1404 {
1405     djSP; dMARK;
1406     I32 cxix;
1407     register CONTEXT *cx;
1408     struct block_sub cxsub;
1409     bool popsub2 = FALSE;
1410     I32 gimme;
1411     SV **newsp;
1412     PMOP *newpm;
1413     I32 optype = 0;
1414
1415     if (curstack == sortstack) {
1416         if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) <= sortcxix) {
1417             if (cxstack_ix > sortcxix)
1418                 dounwind(sortcxix);
1419             AvARRAY(curstack)[1] = *SP;
1420             stack_sp = stack_base + 1;
1421             return 0;
1422         }
1423     }
1424
1425     cxix = dopoptosub(cxstack_ix);
1426     if (cxix < 0)
1427         DIE("Can't return outside a subroutine");
1428     if (cxix < cxstack_ix)
1429         dounwind(cxix);
1430
1431     POPBLOCK(cx,newpm);
1432     switch (cx->cx_type) {
1433     case CXt_SUB:
1434         POPSUB1(cx);    /* Delay POPSUB2 until stack values are safe */
1435         popsub2 = TRUE;
1436         break;
1437     case CXt_EVAL:
1438         POPEVAL(cx);
1439         if (optype == OP_REQUIRE &&
1440             (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1441         {
1442             /* Unassume the success we assumed earlier. */
1443             char *name = cx->blk_eval.old_name;
1444             (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1445             DIE("%s did not return a true value", name);
1446         }
1447         break;
1448     default:
1449         DIE("panic: return");
1450     }
1451
1452     TAINT_NOT;
1453     if (gimme == G_SCALAR) {
1454         if (MARK < SP)
1455             *++newsp = (popsub2 && SvTEMP(*SP))
1456                         ? *SP : sv_mortalcopy(*SP);
1457         else
1458             *++newsp = &sv_undef;
1459     }
1460     else if (gimme == G_ARRAY) {
1461         while (++MARK <= SP) {
1462             *++newsp = (popsub2 && SvTEMP(*MARK))
1463                         ? *MARK : sv_mortalcopy(*MARK);
1464             TAINT_NOT;          /* Each item is independent */
1465         }
1466     }
1467     stack_sp = newsp;
1468
1469     /* Stack values are safe: */
1470     if (popsub2) {
1471         POPSUB2();      /* release CV and @_ ... */
1472     }
1473     curpm = newpm;      /* ... and pop $1 et al */
1474
1475     LEAVE;
1476     return pop_return();
1477 }
1478
1479 PP(pp_last)
1480 {
1481     djSP;
1482     I32 cxix;
1483     register CONTEXT *cx;
1484     struct block_loop cxloop;
1485     struct block_sub cxsub;
1486     I32 pop2 = 0;
1487     I32 gimme;
1488     I32 optype;
1489     OP *nextop;
1490     SV **newsp;
1491     PMOP *newpm;
1492     SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
1493
1494     if (op->op_flags & OPf_SPECIAL) {
1495         cxix = dopoptoloop(cxstack_ix);
1496         if (cxix < 0)
1497             DIE("Can't \"last\" outside a block");
1498     }
1499     else {
1500         cxix = dopoptolabel(cPVOP->op_pv);
1501         if (cxix < 0)
1502             DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1503     }
1504     if (cxix < cxstack_ix)
1505         dounwind(cxix);
1506
1507     POPBLOCK(cx,newpm);
1508     switch (cx->cx_type) {
1509     case CXt_LOOP:
1510         POPLOOP1(cx);   /* Delay POPLOOP2 until stack values are safe */
1511         pop2 = CXt_LOOP;
1512         nextop = cxloop.last_op->op_next;
1513         break;
1514     case CXt_SUB:
1515         POPSUB1(cx);    /* Delay POPSUB2 until stack values are safe */
1516         pop2 = CXt_SUB;
1517         nextop = pop_return();
1518         break;
1519     case CXt_EVAL:
1520         POPEVAL(cx);
1521         nextop = pop_return();
1522         break;
1523     default:
1524         DIE("panic: last");
1525     }
1526
1527     TAINT_NOT;
1528     if (gimme == G_SCALAR) {
1529         if (MARK < SP)
1530             *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1531                         ? *SP : sv_mortalcopy(*SP);
1532         else
1533             *++newsp = &sv_undef;
1534     }
1535     else if (gimme == G_ARRAY) {
1536         while (++MARK <= SP) {
1537             *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1538                         ? *MARK : sv_mortalcopy(*MARK);
1539             TAINT_NOT;          /* Each item is independent */
1540         }
1541     }
1542     SP = newsp;
1543     PUTBACK;
1544
1545     /* Stack values are safe: */
1546     switch (pop2) {
1547     case CXt_LOOP:
1548         POPLOOP2();     /* release loop vars ... */
1549         LEAVE;
1550         break;
1551     case CXt_SUB:
1552         POPSUB2();      /* release CV and @_ ... */
1553         break;
1554     }
1555     curpm = newpm;      /* ... and pop $1 et al */
1556
1557     LEAVE;
1558     return nextop;
1559 }
1560
1561 PP(pp_next)
1562 {
1563     I32 cxix;
1564     register CONTEXT *cx;
1565     I32 oldsave;
1566
1567     if (op->op_flags & OPf_SPECIAL) {
1568         cxix = dopoptoloop(cxstack_ix);
1569         if (cxix < 0)
1570             DIE("Can't \"next\" outside a block");
1571     }
1572     else {
1573         cxix = dopoptolabel(cPVOP->op_pv);
1574         if (cxix < 0)
1575             DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1576     }
1577     if (cxix < cxstack_ix)
1578         dounwind(cxix);
1579
1580     TOPBLOCK(cx);
1581     oldsave = scopestack[scopestack_ix - 1];
1582     LEAVE_SCOPE(oldsave);
1583     return cx->blk_loop.next_op;
1584 }
1585
1586 PP(pp_redo)
1587 {
1588     I32 cxix;
1589     register CONTEXT *cx;
1590     I32 oldsave;
1591
1592     if (op->op_flags & OPf_SPECIAL) {
1593         cxix = dopoptoloop(cxstack_ix);
1594         if (cxix < 0)
1595             DIE("Can't \"redo\" outside a block");
1596     }
1597     else {
1598         cxix = dopoptolabel(cPVOP->op_pv);
1599         if (cxix < 0)
1600             DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1601     }
1602     if (cxix < cxstack_ix)
1603         dounwind(cxix);
1604
1605     TOPBLOCK(cx);
1606     oldsave = scopestack[scopestack_ix - 1];
1607     LEAVE_SCOPE(oldsave);
1608     return cx->blk_loop.redo_op;
1609 }
1610
1611 static OP* lastgotoprobe;
1612
1613 static OP *
1614 dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
1615 {
1616     OP *kid;
1617     OP **ops = opstack;
1618     static char too_deep[] = "Target of goto is too deeply nested";
1619
1620     if (ops >= oplimit)
1621         croak(too_deep);
1622     if (o->op_type == OP_LEAVE ||
1623         o->op_type == OP_SCOPE ||
1624         o->op_type == OP_LEAVELOOP ||
1625         o->op_type == OP_LEAVETRY)
1626     {
1627         *ops++ = cUNOPo->op_first;
1628         if (ops >= oplimit)
1629             croak(too_deep);
1630     }
1631     *ops = 0;
1632     if (o->op_flags & OPf_KIDS) {
1633         /* First try all the kids at this level, since that's likeliest. */
1634         for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1635             if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1636                     kCOP->cop_label && strEQ(kCOP->cop_label, label))
1637                 return kid;
1638         }
1639         for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1640             if (kid == lastgotoprobe)
1641                 continue;
1642             if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1643                 (ops == opstack ||
1644                  (ops[-1]->op_type != OP_NEXTSTATE &&
1645                   ops[-1]->op_type != OP_DBSTATE)))
1646                 *ops++ = kid;
1647             if (o = dofindlabel(kid, label, ops, oplimit))
1648                 return o;
1649         }
1650     }
1651     *ops = 0;
1652     return 0;
1653 }
1654
1655 PP(pp_dump)
1656 {
1657     return pp_goto(ARGS);
1658     /*NOTREACHED*/
1659 }
1660
1661 PP(pp_goto)
1662 {
1663     djSP;
1664     OP *retop = 0;
1665     I32 ix;
1666     register CONTEXT *cx;
1667 #define GOTO_DEPTH 64
1668     OP *enterops[GOTO_DEPTH];
1669     char *label;
1670     int do_dump = (op->op_type == OP_DUMP);
1671
1672     label = 0;
1673     if (op->op_flags & OPf_STACKED) {
1674         SV *sv = POPs;
1675
1676         /* This egregious kludge implements goto &subroutine */
1677         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1678             I32 cxix;
1679             register CONTEXT *cx;
1680             CV* cv = (CV*)SvRV(sv);
1681             SV** mark;
1682             I32 items = 0;
1683             I32 oldsave;
1684
1685             if (!CvROOT(cv) && !CvXSUB(cv)) {
1686                 if (CvGV(cv)) {
1687                     SV *tmpstr = sv_newmortal();
1688                     gv_efullname3(tmpstr, CvGV(cv), Nullch);
1689                     DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1690                 }
1691                 DIE("Goto undefined subroutine");
1692             }
1693
1694             /* First do some returnish stuff. */
1695             cxix = dopoptosub(cxstack_ix);
1696             if (cxix < 0)
1697                 DIE("Can't goto subroutine outside a subroutine");
1698             if (cxix < cxstack_ix)
1699                 dounwind(cxix);
1700             TOPBLOCK(cx);
1701             mark = stack_sp;
1702             if (cx->blk_sub.hasargs) {   /* put @_ back onto stack */
1703                 AV* av = cx->blk_sub.argarray;
1704                 
1705                 items = AvFILL(av) + 1;
1706                 stack_sp++;
1707                 EXTEND(stack_sp, items); /* @_ could have been extended. */
1708                 Copy(AvARRAY(av), stack_sp, items, SV*);
1709                 stack_sp += items;
1710 #ifndef USE_THREADS
1711                 SvREFCNT_dec(GvAV(defgv));
1712                 GvAV(defgv) = cx->blk_sub.savearray;
1713 #endif /* USE_THREADS */
1714                 AvREAL_off(av);
1715                 av_clear(av);
1716             }
1717             if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1718                 SvREFCNT_dec(cx->blk_sub.cv);
1719             oldsave = scopestack[scopestack_ix - 1];
1720             LEAVE_SCOPE(oldsave);
1721
1722             /* Now do some callish stuff. */
1723             SAVETMPS;
1724             if (CvXSUB(cv)) {
1725                 if (CvOLDSTYLE(cv)) {
1726                     I32 (*fp3)_((int,int,int));
1727                     while (sp > mark) {
1728                         sp[1] = sp[0];
1729                         sp--;
1730                     }
1731                     fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1732                     items = (*fp3)(CvXSUBANY(cv).any_i32,
1733                                    mark - stack_base + 1,
1734                                    items);
1735                     sp = stack_base + items;
1736                 }
1737                 else {
1738                     stack_sp--;         /* There is no cv arg. */
1739                     (void)(*CvXSUB(cv))(cv);
1740                 }
1741                 LEAVE;
1742                 return pop_return();
1743             }
1744             else {
1745                 AV* padlist = CvPADLIST(cv);
1746                 SV** svp = AvARRAY(padlist);
1747                 cx->blk_sub.cv = cv;
1748                 cx->blk_sub.olddepth = CvDEPTH(cv);
1749                 CvDEPTH(cv)++;
1750                 if (CvDEPTH(cv) < 2)
1751                     (void)SvREFCNT_inc(cv);
1752                 else {  /* save temporaries on recursion? */
1753                     if (CvDEPTH(cv) == 100 && dowarn)
1754                         sub_crush_depth(cv);
1755                     if (CvDEPTH(cv) > AvFILL(padlist)) {
1756                         AV *newpad = newAV();
1757                         SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
1758                         I32 ix = AvFILL((AV*)svp[1]);
1759                         svp = AvARRAY(svp[0]);
1760                         for ( ;ix > 0; ix--) {
1761                             if (svp[ix] != &sv_undef) {
1762                                 char *name = SvPVX(svp[ix]);
1763                                 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1764                                     || *name == '&')
1765                                 {
1766                                     /* outer lexical or anon code */
1767                                     av_store(newpad, ix,
1768                                         SvREFCNT_inc(oldpad[ix]) );
1769                                 }
1770                                 else {          /* our own lexical */
1771                                     if (*name == '@')
1772                                         av_store(newpad, ix, sv = (SV*)newAV());
1773                                     else if (*name == '%')
1774                                         av_store(newpad, ix, sv = (SV*)newHV());
1775                                     else
1776                                         av_store(newpad, ix, sv = NEWSV(0,0));
1777                                     SvPADMY_on(sv);
1778                                 }
1779                             }
1780                             else {
1781                                 av_store(newpad, ix, sv = NEWSV(0,0));
1782                                 SvPADTMP_on(sv);
1783                             }
1784                         }
1785                         if (cx->blk_sub.hasargs) {
1786                             AV* av = newAV();
1787                             av_extend(av, 0);
1788                             av_store(newpad, 0, (SV*)av);
1789                             AvFLAGS(av) = AVf_REIFY;
1790                         }
1791                         av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1792                         AvFILL(padlist) = CvDEPTH(cv);
1793                         svp = AvARRAY(padlist);
1794                     }
1795                 }
1796 #ifdef USE_THREADS
1797                 if (!cx->blk_sub.hasargs) {
1798                     AV* av = (AV*)curpad[0];
1799                     
1800                     items = AvFILL(av) + 1;
1801                     if (items) {
1802                         /* Mark is at the end of the stack. */
1803                         EXTEND(sp, items);
1804                         Copy(AvARRAY(av), sp + 1, items, SV*);
1805                         sp += items;
1806                         PUTBACK ;                   
1807                     }
1808                 }
1809 #endif /* USE_THREADS */                
1810                 SAVESPTR(curpad);
1811                 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1812 #ifndef USE_THREADS
1813                 if (cx->blk_sub.hasargs)
1814 #endif /* USE_THREADS */
1815                 {
1816                     AV* av = (AV*)curpad[0];
1817                     SV** ary;
1818
1819 #ifndef USE_THREADS
1820                     cx->blk_sub.savearray = GvAV(defgv);
1821                     GvAV(defgv) = (AV*)SvREFCNT_inc(av);
1822 #endif /* USE_THREADS */
1823                     cx->blk_sub.argarray = av;
1824                     ++mark;
1825
1826                     if (items >= AvMAX(av) + 1) {
1827                         ary = AvALLOC(av);
1828                         if (AvARRAY(av) != ary) {
1829                             AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1830                             SvPVX(av) = (char*)ary;
1831                         }
1832                         if (items >= AvMAX(av) + 1) {
1833                             AvMAX(av) = items - 1;
1834                             Renew(ary,items+1,SV*);
1835                             AvALLOC(av) = ary;
1836                             SvPVX(av) = (char*)ary;
1837                         }
1838                     }
1839                     Copy(mark,AvARRAY(av),items,SV*);
1840                     AvFILL(av) = items - 1;
1841                     
1842                     while (items--) {
1843                         if (*mark)
1844                             SvTEMP_off(*mark);
1845                         mark++;
1846                     }
1847                 }
1848                 if (PERLDB_SUB && curstash != debstash) {
1849                     /*
1850                      * We do not care about using sv to call CV;
1851                      * it's for informational purposes only.
1852                      */
1853                     SV *sv = GvSV(DBsub);
1854                     save_item(sv);
1855                     gv_efullname3(sv, CvGV(cv), Nullch);
1856                 }
1857                 RETURNOP(CvSTART(cv));
1858             }
1859         }
1860         else
1861             label = SvPV(sv,na);
1862     }
1863     else if (op->op_flags & OPf_SPECIAL) {
1864         if (! do_dump)
1865             DIE("goto must have label");
1866     }
1867     else
1868         label = cPVOP->op_pv;
1869
1870     if (label && *label) {
1871         OP *gotoprobe = 0;
1872
1873         /* find label */
1874
1875         lastgotoprobe = 0;
1876         *enterops = 0;
1877         for (ix = cxstack_ix; ix >= 0; ix--) {
1878             cx = &cxstack[ix];
1879             switch (cx->cx_type) {
1880             case CXt_EVAL:
1881                 gotoprobe = eval_root; /* XXX not good for nested eval */
1882                 break;
1883             case CXt_LOOP:
1884                 gotoprobe = cx->blk_oldcop->op_sibling;
1885                 break;
1886             case CXt_SUBST:
1887                 continue;
1888             case CXt_BLOCK:
1889                 if (ix)
1890                     gotoprobe = cx->blk_oldcop->op_sibling;
1891                 else
1892                     gotoprobe = main_root;
1893                 break;
1894             case CXt_SUB:
1895                 if (CvDEPTH(cx->blk_sub.cv)) {
1896                     gotoprobe = CvROOT(cx->blk_sub.cv);
1897                     break;
1898                 }
1899                 /* FALL THROUGH */
1900             case CXt_NULL:
1901                 DIE("Can't \"goto\" outside a block");
1902             default:
1903                 if (ix)
1904                     DIE("panic: goto");
1905                 gotoprobe = main_root;
1906                 break;
1907             }
1908             retop = dofindlabel(gotoprobe, label,
1909                                 enterops, enterops + GOTO_DEPTH);
1910             if (retop)
1911                 break;
1912             lastgotoprobe = gotoprobe;
1913         }
1914         if (!retop)
1915             DIE("Can't find label %s", label);
1916
1917         /* pop unwanted frames */
1918
1919         if (ix < cxstack_ix) {
1920             I32 oldsave;
1921
1922             if (ix < 0)
1923                 ix = 0;
1924             dounwind(ix);
1925             TOPBLOCK(cx);
1926             oldsave = scopestack[scopestack_ix];
1927             LEAVE_SCOPE(oldsave);
1928         }
1929
1930         /* push wanted frames */
1931
1932         if (*enterops && enterops[1]) {
1933             OP *oldop = op;
1934             for (ix = 1; enterops[ix]; ix++) {
1935                 op = enterops[ix];
1936                 /* Eventually we may want to stack the needed arguments
1937                  * for each op.  For now, we punt on the hard ones. */
1938                 if (op->op_type == OP_ENTERITER)
1939                     DIE("Can't \"goto\" into the middle of a foreach loop",
1940                         label);
1941                 (*op->op_ppaddr)(ARGS);
1942             }
1943             op = oldop;
1944         }
1945     }
1946
1947     if (do_dump) {
1948 #ifdef VMS
1949         if (!retop) retop = main_start;
1950 #endif
1951         restartop = retop;
1952         do_undump = TRUE;
1953
1954         my_unexec();
1955
1956         restartop = 0;          /* hmm, must be GNU unexec().. */
1957         do_undump = FALSE;
1958     }
1959
1960     if (curstack == signalstack) {
1961         restartop = retop;
1962         JMPENV_JUMP(3);
1963     }
1964
1965     RETURNOP(retop);
1966 }
1967
1968 PP(pp_exit)
1969 {
1970     djSP;
1971     I32 anum;
1972
1973     if (MAXARG < 1)
1974         anum = 0;
1975     else {
1976         anum = SvIVx(POPs);
1977 #ifdef VMSISH_EXIT
1978         if (anum == 1 && VMSISH_EXIT)
1979             anum = 0;
1980 #endif
1981     }
1982     my_exit(anum);
1983     PUSHs(&sv_undef);
1984     RETURN;
1985 }
1986
1987 #ifdef NOTYET
1988 PP(pp_nswitch)
1989 {
1990     djSP;
1991     double value = SvNVx(GvSV(cCOP->cop_gv));
1992     register I32 match = I_32(value);
1993
1994     if (value < 0.0) {
1995         if (((double)match) > value)
1996             --match;            /* was fractional--truncate other way */
1997     }
1998     match -= cCOP->uop.scop.scop_offset;
1999     if (match < 0)
2000         match = 0;
2001     else if (match > cCOP->uop.scop.scop_max)
2002         match = cCOP->uop.scop.scop_max;
2003     op = cCOP->uop.scop.scop_next[match];
2004     RETURNOP(op);
2005 }
2006
2007 PP(pp_cswitch)
2008 {
2009     djSP;
2010     register I32 match;
2011
2012     if (multiline)
2013         op = op->op_next;                       /* can't assume anything */
2014     else {
2015         match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
2016         match -= cCOP->uop.scop.scop_offset;
2017         if (match < 0)
2018             match = 0;
2019         else if (match > cCOP->uop.scop.scop_max)
2020             match = cCOP->uop.scop.scop_max;
2021         op = cCOP->uop.scop.scop_next[match];
2022     }
2023     RETURNOP(op);
2024 }
2025 #endif
2026
2027 /* Eval. */
2028
2029 static void
2030 save_lines(AV *array, SV *sv)
2031 {
2032     register char *s = SvPVX(sv);
2033     register char *send = SvPVX(sv) + SvCUR(sv);
2034     register char *t;
2035     register I32 line = 1;
2036
2037     while (s && s < send) {
2038         SV *tmpstr = NEWSV(85,0);
2039
2040         sv_upgrade(tmpstr, SVt_PVMG);
2041         t = strchr(s, '\n');
2042         if (t)
2043             t++;
2044         else
2045             t = send;
2046
2047         sv_setpvn(tmpstr, s, t - s);
2048         av_store(array, line++, tmpstr);
2049         s = t;
2050     }
2051 }
2052
2053 static OP *
2054 docatch(OP *o)
2055 {
2056     dTHR;
2057     int ret;
2058     I32 oldrunlevel = runlevel;
2059     OP *oldop = op;
2060     dJMPENV;
2061
2062     op = o;
2063 #ifdef DEBUGGING
2064     assert(CATCH_GET == TRUE);
2065     DEBUG_l(deb("(Setting up local jumplevel, runlevel = %ld)\n", (long)runlevel+1));
2066 #endif
2067     JMPENV_PUSH(ret);
2068     switch (ret) {
2069     default:                            /* topmost level handles it */
2070         JMPENV_POP;
2071         runlevel = oldrunlevel;
2072         op = oldop;
2073         JMPENV_JUMP(ret);
2074         /* NOTREACHED */
2075     case 3:
2076         if (!restartop) {
2077             PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2078             break;
2079         }
2080         op = restartop;
2081         restartop = 0;
2082         /* FALL THROUGH */
2083     case 0:
2084         runops();
2085         break;
2086     }
2087     JMPENV_POP;
2088     runlevel = oldrunlevel;
2089     op = oldop;
2090     return Nullop;
2091 }
2092
2093 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2094 static OP *
2095 doeval(int gimme)
2096 {
2097     dSP;
2098     OP *saveop = op;
2099     HV *newstash;
2100     CV *caller;
2101     AV* comppadlist;
2102
2103     in_eval = 1;
2104
2105     PUSHMARK(SP);
2106
2107     /* set up a scratch pad */
2108
2109     SAVEI32(padix);
2110     SAVESPTR(curpad);
2111     SAVESPTR(comppad);
2112     SAVESPTR(comppad_name);
2113     SAVEI32(comppad_name_fill);
2114     SAVEI32(min_intro_pending);
2115     SAVEI32(max_intro_pending);
2116
2117     caller = compcv;
2118     SAVESPTR(compcv);
2119     compcv = (CV*)NEWSV(1104,0);
2120     sv_upgrade((SV *)compcv, SVt_PVCV);
2121     CvUNIQUE_on(compcv);
2122 #ifdef USE_THREADS
2123     CvOWNER(compcv) = 0;
2124     New(666, CvMUTEXP(compcv), 1, perl_mutex);
2125     MUTEX_INIT(CvMUTEXP(compcv));
2126 #endif /* USE_THREADS */
2127
2128     comppad = newAV();
2129     av_push(comppad, Nullsv);
2130     curpad = AvARRAY(comppad);
2131     comppad_name = newAV();
2132     comppad_name_fill = 0;
2133     min_intro_pending = 0;
2134     padix = 0;
2135 #ifdef USE_THREADS
2136     av_store(comppad_name, 0, newSVpv("@_", 2));
2137     curpad[0] = (SV*)newAV();
2138     SvPADMY_on(curpad[0]);      /* XXX Needed? */
2139 #endif /* USE_THREADS */
2140
2141     comppadlist = newAV();
2142     AvREAL_off(comppadlist);
2143     av_store(comppadlist, 0, (SV*)comppad_name);
2144     av_store(comppadlist, 1, (SV*)comppad);
2145     CvPADLIST(compcv) = comppadlist;
2146
2147     if (saveop->op_type != OP_REQUIRE)
2148         CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
2149
2150     SAVEFREESV(compcv);
2151
2152     /* make sure we compile in the right package */
2153
2154     newstash = curcop->cop_stash;
2155     if (curstash != newstash) {
2156         SAVESPTR(curstash);
2157         curstash = newstash;
2158     }
2159     SAVESPTR(beginav);
2160     beginav = newAV();
2161     SAVEFREESV(beginav);
2162
2163     /* try to compile it */
2164
2165     eval_root = Nullop;
2166     error_count = 0;
2167     curcop = &compiling;
2168     curcop->cop_arybase = 0;
2169     SvREFCNT_dec(rs);
2170     rs = newSVpv("\n", 1);
2171     if (saveop->op_flags & OPf_SPECIAL)
2172         in_eval |= 4;
2173     else
2174         sv_setpv(ERRSV,"");
2175     if (yyparse() || error_count || !eval_root) {
2176         SV **newsp;
2177         I32 gimme;
2178         CONTEXT *cx;
2179         I32 optype;
2180
2181         op = saveop;
2182         if (eval_root) {
2183             op_free(eval_root);
2184             eval_root = Nullop;
2185         }
2186         SP = stack_base + POPMARK;              /* pop original mark */
2187         POPBLOCK(cx,curpm);
2188         POPEVAL(cx);
2189         pop_return();
2190         lex_end();
2191         LEAVE;
2192         if (optype == OP_REQUIRE) {
2193             char* msg = SvPVx(ERRSV, na);
2194             DIE("%s", *msg ? msg : "Compilation failed in require");
2195         }
2196         SvREFCNT_dec(rs);
2197         rs = SvREFCNT_inc(nrs);
2198 #ifdef USE_THREADS
2199         MUTEX_LOCK(&eval_mutex);
2200         eval_owner = 0;
2201         COND_SIGNAL(&eval_cond);
2202         MUTEX_UNLOCK(&eval_mutex);
2203 #endif /* USE_THREADS */
2204         RETPUSHUNDEF;
2205     }
2206     SvREFCNT_dec(rs);
2207     rs = SvREFCNT_inc(nrs);
2208     compiling.cop_line = 0;
2209     SAVEFREEOP(eval_root);
2210     if (gimme & G_VOID)
2211         scalarvoid(eval_root);
2212     else if (gimme & G_ARRAY)
2213         list(eval_root);
2214     else
2215         scalar(eval_root);
2216
2217     DEBUG_x(dump_eval());
2218
2219     /* Register with debugger: */
2220     if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2221         CV *cv = perl_get_cv("DB::postponed", FALSE);
2222         if (cv) {
2223             dSP;
2224             PUSHMARK(sp);
2225             XPUSHs((SV*)compiling.cop_filegv);
2226             PUTBACK;
2227             perl_call_sv((SV*)cv, G_DISCARD);
2228         }
2229     }
2230
2231     /* compiled okay, so do it */
2232
2233     CvDEPTH(compcv) = 1;
2234     SP = stack_base + POPMARK;          /* pop original mark */
2235     op = saveop;                                        /* The caller may need it. */
2236 #ifdef USE_THREADS
2237     MUTEX_LOCK(&eval_mutex);
2238     eval_owner = 0;
2239     COND_SIGNAL(&eval_cond);
2240     MUTEX_UNLOCK(&eval_mutex);
2241 #endif /* USE_THREADS */
2242
2243     RETURNOP(eval_start);
2244 }
2245
2246 PP(pp_require)
2247 {
2248     djSP;
2249     register CONTEXT *cx;
2250     SV *sv;
2251     char *name;
2252     char *tryname;
2253     SV *namesv = Nullsv;
2254     SV** svp;
2255     I32 gimme = G_SCALAR;
2256     PerlIO *tryrsfp = 0;
2257
2258     sv = POPs;
2259     if (SvNIOKp(sv) && !SvPOKp(sv)) {
2260         SET_NUMERIC_STANDARD();
2261         if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2262             DIE("Perl %s required--this is only version %s, stopped",
2263                 SvPV(sv,na),patchlevel);
2264         RETPUSHYES;
2265     }
2266     name = SvPV(sv, na);
2267     if (!*name)
2268         DIE("Null filename used");
2269     TAINT_PROPER("require");
2270     if (op->op_type == OP_REQUIRE &&
2271       (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2272       *svp != &sv_undef)
2273         RETPUSHYES;
2274
2275     /* prepare to compile file */
2276
2277     if (*name == '/' ||
2278         (*name == '.' && 
2279             (name[1] == '/' ||
2280              (name[1] == '.' && name[2] == '/')))
2281 #ifdef DOSISH
2282       || (name[0] && name[1] == ':')
2283 #endif
2284 #ifdef WIN32
2285       || (name[0] == '\\' && name[1] == '\\')   /* UNC path */
2286 #endif
2287 #ifdef VMS
2288         || (strchr(name,':')  || ((*name == '[' || *name == '<') &&
2289             (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
2290 #endif
2291     )
2292     {
2293         tryname = name;
2294         tryrsfp = PerlIO_open(name,"r");
2295     }
2296     else {
2297         AV *ar = GvAVn(incgv);
2298         I32 i;
2299 #ifdef VMS
2300         char *unixname;
2301         if ((unixname = tounixspec(name, Nullch)) != Nullch)
2302 #endif
2303         {
2304             namesv = NEWSV(806, 0);
2305             for (i = 0; i <= AvFILL(ar); i++) {
2306                 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2307 #ifdef VMS
2308                 char *unixdir;
2309                 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2310                     continue;
2311                 sv_setpv(namesv, unixdir);
2312                 sv_catpv(namesv, unixname);
2313 #else
2314                 sv_setpvf(namesv, "%s/%s", dir, name);
2315 #endif
2316                 tryname = SvPVX(namesv);
2317                 tryrsfp = PerlIO_open(tryname, "r");
2318                 if (tryrsfp) {
2319                     if (tryname[0] == '.' && tryname[1] == '/')
2320                         tryname += 2;
2321                     break;
2322                 }
2323             }
2324         }
2325     }
2326     SAVESPTR(compiling.cop_filegv);
2327     compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2328     SvREFCNT_dec(namesv);
2329     if (!tryrsfp) {
2330         if (op->op_type == OP_REQUIRE) {
2331             SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2332             SV *dirmsgsv = NEWSV(0, 0);
2333             AV *ar = GvAVn(incgv);
2334             I32 i;
2335             if (instr(SvPVX(msg), ".h "))
2336                 sv_catpv(msg, " (change .h to .ph maybe?)");
2337             if (instr(SvPVX(msg), ".ph "))
2338                 sv_catpv(msg, " (did you run h2ph?)");
2339             sv_catpv(msg, " (@INC contains:");
2340             for (i = 0; i <= AvFILL(ar); i++) {
2341                 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2342                 sv_setpvf(dirmsgsv, " %s", dir);
2343                 sv_catsv(msg, dirmsgsv);
2344             }
2345             sv_catpvn(msg, ")", 1);
2346             SvREFCNT_dec(dirmsgsv);
2347             DIE("%_", msg);
2348         }
2349
2350         RETPUSHUNDEF;
2351     }
2352
2353     /* Assume success here to prevent recursive requirement. */
2354     (void)hv_store(GvHVn(incgv), name, strlen(name),
2355         newSVsv(GvSV(compiling.cop_filegv)), 0 );
2356
2357     ENTER;
2358     SAVETMPS;
2359     lex_start(sv_2mortal(newSVpv("",0)));
2360     if (rsfp_filters){
2361         save_aptr(&rsfp_filters);
2362         rsfp_filters = NULL;
2363     }
2364
2365     rsfp = tryrsfp;
2366     name = savepv(name);
2367     SAVEFREEPV(name);
2368     SAVEI32(hints);
2369     hints = 0;
2370  
2371     /* switch to eval mode */
2372
2373     push_return(op->op_next);
2374     PUSHBLOCK(cx, CXt_EVAL, SP);
2375     PUSHEVAL(cx, name, compiling.cop_filegv);
2376
2377     compiling.cop_line = 0;
2378
2379     PUTBACK;
2380 #ifdef USE_THREADS
2381     MUTEX_LOCK(&eval_mutex);
2382     if (eval_owner && eval_owner != thr)
2383         while (eval_owner)
2384             COND_WAIT(&eval_cond, &eval_mutex);
2385     eval_owner = thr;
2386     MUTEX_UNLOCK(&eval_mutex);
2387 #endif /* USE_THREADS */
2388     return DOCATCH(doeval(G_SCALAR));
2389 }
2390
2391 PP(pp_dofile)
2392 {
2393     return pp_require(ARGS);
2394 }
2395
2396 PP(pp_entereval)
2397 {
2398     djSP;
2399     register CONTEXT *cx;
2400     dPOPss;
2401     I32 gimme = GIMME_V, was = sub_generation;
2402     char tmpbuf[TYPE_DIGITS(long) + 12];
2403     char *safestr;
2404     STRLEN len;
2405     OP *ret;
2406
2407     if (!SvPV(sv,len) || !len)
2408         RETPUSHUNDEF;
2409     TAINT_PROPER("eval");
2410
2411     ENTER;
2412     lex_start(sv);
2413     SAVETMPS;
2414  
2415     /* switch to eval mode */
2416
2417     SAVESPTR(compiling.cop_filegv);
2418     sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
2419     compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2420     compiling.cop_line = 1;
2421     /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2422        deleting the eval's FILEGV from the stash before gv_check() runs
2423        (i.e. before run-time proper). To work around the coredump that
2424        ensues, we always turn GvMULTI_on for any globals that were
2425        introduced within evals. See force_ident(). GSAR 96-10-12 */
2426     safestr = savepv(tmpbuf);
2427     SAVEDELETE(defstash, safestr, strlen(safestr));
2428     SAVEI32(hints);
2429     hints = op->op_targ;
2430
2431     push_return(op->op_next);
2432     PUSHBLOCK(cx, CXt_EVAL, SP);
2433     PUSHEVAL(cx, 0, compiling.cop_filegv);
2434
2435     /* prepare to compile string */
2436
2437     if (PERLDB_LINE && curstash != debstash)
2438         save_lines(GvAV(compiling.cop_filegv), linestr);
2439     PUTBACK;
2440 #ifdef USE_THREADS
2441     MUTEX_LOCK(&eval_mutex);
2442     if (eval_owner && eval_owner != thr)
2443         while (eval_owner)
2444             COND_WAIT(&eval_cond, &eval_mutex);
2445     eval_owner = thr;
2446     MUTEX_UNLOCK(&eval_mutex);
2447 #endif /* USE_THREADS */
2448     ret = doeval(gimme);
2449     if (PERLDB_INTER && was != sub_generation /* Some subs defined here. */
2450         && ret != op->op_next) {        /* Successive compilation. */
2451         strcpy(safestr, "_<(eval )");   /* Anything fake and short. */
2452     }
2453     return DOCATCH(ret);
2454 }
2455
2456 PP(pp_leaveeval)
2457 {
2458     djSP;
2459     register SV **mark;
2460     SV **newsp;
2461     PMOP *newpm;
2462     I32 gimme;
2463     register CONTEXT *cx;
2464     OP *retop;
2465     U8 save_flags = op -> op_flags;
2466     I32 optype;
2467
2468     POPBLOCK(cx,newpm);
2469     POPEVAL(cx);
2470     retop = pop_return();
2471
2472     TAINT_NOT;
2473     if (gimme == G_VOID)
2474         MARK = newsp;
2475     else if (gimme == G_SCALAR) {
2476         MARK = newsp + 1;
2477         if (MARK <= SP) {
2478             if (SvFLAGS(TOPs) & SVs_TEMP)
2479                 *MARK = TOPs;
2480             else
2481                 *MARK = sv_mortalcopy(TOPs);
2482         }
2483         else {
2484             MEXTEND(mark,0);
2485             *MARK = &sv_undef;
2486         }
2487     }
2488     else {
2489         /* in case LEAVE wipes old return values */
2490         for (mark = newsp + 1; mark <= SP; mark++) {
2491             if (!(SvFLAGS(*mark) & SVs_TEMP)) {
2492                 *mark = sv_mortalcopy(*mark);
2493                 TAINT_NOT;      /* Each item is independent */
2494             }
2495         }
2496     }
2497     curpm = newpm;      /* Don't pop $1 et al till now */
2498
2499     /*
2500      * Closures mentioned at top level of eval cannot be referenced
2501      * again, and their presence indirectly causes a memory leak.
2502      * (Note that the fact that compcv and friends are still set here
2503      * is, AFAIK, an accident.)  --Chip
2504      */
2505     if (AvFILL(comppad_name) >= 0) {
2506         SV **svp = AvARRAY(comppad_name);
2507         I32 ix;
2508         for (ix = AvFILL(comppad_name); ix >= 0; ix--) {
2509             SV *sv = svp[ix];
2510             if (sv && sv != &sv_undef && *SvPVX(sv) == '&') {
2511                 SvREFCNT_dec(sv);
2512                 svp[ix] = &sv_undef;
2513
2514                 sv = curpad[ix];
2515                 if (CvCLONE(sv)) {
2516                     SvREFCNT_dec(CvOUTSIDE(sv));
2517                     CvOUTSIDE(sv) = Nullcv;
2518                 }
2519                 else {
2520                     SvREFCNT_dec(sv);
2521                     sv = NEWSV(0,0);
2522                     SvPADTMP_on(sv);
2523                     curpad[ix] = sv;
2524                 }
2525             }
2526         }
2527     }
2528
2529 #ifdef DEBUGGING
2530     assert(CvDEPTH(compcv) == 1);
2531 #endif
2532     CvDEPTH(compcv) = 0;
2533
2534     if (optype == OP_REQUIRE &&
2535         !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp))
2536     {
2537         /* Unassume the success we assumed earlier. */
2538         char *name = cx->blk_eval.old_name;
2539         (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2540         retop = die("%s did not return a true value", name);
2541     }
2542
2543     lex_end();
2544     LEAVE;
2545
2546     if (!(save_flags & OPf_SPECIAL))
2547         sv_setpv(ERRSV,"");
2548
2549     RETURNOP(retop);
2550 }
2551
2552 PP(pp_entertry)
2553 {
2554     djSP;
2555     register CONTEXT *cx;
2556     I32 gimme = GIMME_V;
2557
2558     ENTER;
2559     SAVETMPS;
2560
2561     push_return(cLOGOP->op_other->op_next);
2562     PUSHBLOCK(cx, CXt_EVAL, SP);
2563     PUSHEVAL(cx, 0, 0);
2564     eval_root = op;             /* Only needed so that goto works right. */
2565
2566     in_eval = 1;
2567     sv_setpv(ERRSV,"");
2568     PUTBACK;
2569     return DOCATCH(op->op_next);
2570 }
2571
2572 PP(pp_leavetry)
2573 {
2574     djSP;
2575     register SV **mark;
2576     SV **newsp;
2577     PMOP *newpm;
2578     I32 gimme;
2579     register CONTEXT *cx;
2580     I32 optype;
2581
2582     POPBLOCK(cx,newpm);
2583     POPEVAL(cx);
2584     pop_return();
2585
2586     TAINT_NOT;
2587     if (gimme == G_VOID)
2588         SP = newsp;
2589     else if (gimme == G_SCALAR) {
2590         MARK = newsp + 1;
2591         if (MARK <= SP) {
2592             if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2593                 *MARK = TOPs;
2594             else
2595                 *MARK = sv_mortalcopy(TOPs);
2596         }
2597         else {
2598             MEXTEND(mark,0);
2599             *MARK = &sv_undef;
2600         }
2601         SP = MARK;
2602     }
2603     else {
2604         /* in case LEAVE wipes old return values */
2605         for (mark = newsp + 1; mark <= SP; mark++) {
2606             if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
2607                 *mark = sv_mortalcopy(*mark);
2608                 TAINT_NOT;      /* Each item is independent */
2609             }
2610         }
2611     }
2612     curpm = newpm;      /* Don't pop $1 et al till now */
2613
2614     LEAVE;
2615     sv_setpv(ERRSV,"");
2616     RETURN;
2617 }
2618
2619 static void
2620 doparseform(SV *sv)
2621 {
2622     STRLEN len;
2623     register char *s = SvPV_force(sv, len);
2624     register char *send = s + len;
2625     register char *base;
2626     register I32 skipspaces = 0;
2627     bool noblank;
2628     bool repeat;
2629     bool postspace = FALSE;
2630     U16 *fops;
2631     register U16 *fpc;
2632     U16 *linepc;
2633     register I32 arg;
2634     bool ischop;
2635
2636     if (len == 0)
2637         croak("Null picture in formline");
2638     
2639     New(804, fops, (send - s)*3+10, U16);    /* Almost certainly too long... */
2640     fpc = fops;
2641
2642     if (s < send) {
2643         linepc = fpc;
2644         *fpc++ = FF_LINEMARK;
2645         noblank = repeat = FALSE;
2646         base = s;
2647     }
2648
2649     while (s <= send) {
2650         switch (*s++) {
2651         default:
2652             skipspaces = 0;
2653             continue;
2654
2655         case '~':
2656             if (*s == '~') {
2657                 repeat = TRUE;
2658                 *s = ' ';
2659             }
2660             noblank = TRUE;
2661             s[-1] = ' ';
2662             /* FALL THROUGH */
2663         case ' ': case '\t':
2664             skipspaces++;
2665             continue;
2666             
2667         case '\n': case 0:
2668             arg = s - base;
2669             skipspaces++;
2670             arg -= skipspaces;
2671             if (arg) {
2672                 if (postspace)
2673                     *fpc++ = FF_SPACE;
2674                 *fpc++ = FF_LITERAL;
2675                 *fpc++ = arg;
2676             }
2677             postspace = FALSE;
2678             if (s <= send)
2679                 skipspaces--;
2680             if (skipspaces) {
2681                 *fpc++ = FF_SKIP;
2682                 *fpc++ = skipspaces;
2683             }
2684             skipspaces = 0;
2685             if (s <= send)
2686                 *fpc++ = FF_NEWLINE;
2687             if (noblank) {
2688                 *fpc++ = FF_BLANK;
2689                 if (repeat)
2690                     arg = fpc - linepc + 1;
2691                 else
2692                     arg = 0;
2693                 *fpc++ = arg;
2694             }
2695             if (s < send) {
2696                 linepc = fpc;
2697                 *fpc++ = FF_LINEMARK;
2698                 noblank = repeat = FALSE;
2699                 base = s;
2700             }
2701             else
2702                 s++;
2703             continue;
2704
2705         case '@':
2706         case '^':
2707             ischop = s[-1] == '^';
2708
2709             if (postspace) {
2710                 *fpc++ = FF_SPACE;
2711                 postspace = FALSE;
2712             }
2713             arg = (s - base) - 1;
2714             if (arg) {
2715                 *fpc++ = FF_LITERAL;
2716                 *fpc++ = arg;
2717             }
2718
2719             base = s - 1;
2720             *fpc++ = FF_FETCH;
2721             if (*s == '*') {
2722                 s++;
2723                 *fpc++ = 0;
2724                 *fpc++ = FF_LINEGLOB;
2725             }
2726             else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2727                 arg = ischop ? 512 : 0;
2728                 base = s - 1;
2729                 while (*s == '#')
2730                     s++;
2731                 if (*s == '.') {
2732                     char *f;
2733                     s++;
2734                     f = s;
2735                     while (*s == '#')
2736                         s++;
2737                     arg |= 256 + (s - f);
2738                 }
2739                 *fpc++ = s - base;              /* fieldsize for FETCH */
2740                 *fpc++ = FF_DECIMAL;
2741                 *fpc++ = arg;
2742             }
2743             else {
2744                 I32 prespace = 0;
2745                 bool ismore = FALSE;
2746
2747                 if (*s == '>') {
2748                     while (*++s == '>') ;
2749                     prespace = FF_SPACE;
2750                 }
2751                 else if (*s == '|') {
2752                     while (*++s == '|') ;
2753                     prespace = FF_HALFSPACE;
2754                     postspace = TRUE;
2755                 }
2756                 else {
2757                     if (*s == '<')
2758                         while (*++s == '<') ;
2759                     postspace = TRUE;
2760                 }
2761                 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2762                     s += 3;
2763                     ismore = TRUE;
2764                 }
2765                 *fpc++ = s - base;              /* fieldsize for FETCH */
2766
2767                 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2768
2769                 if (prespace)
2770                     *fpc++ = prespace;
2771                 *fpc++ = FF_ITEM;
2772                 if (ismore)
2773                     *fpc++ = FF_MORE;
2774                 if (ischop)
2775                     *fpc++ = FF_CHOP;
2776             }
2777             base = s;
2778             skipspaces = 0;
2779             continue;
2780         }
2781     }
2782     *fpc++ = FF_END;
2783
2784     arg = fpc - fops;
2785     { /* need to jump to the next word */
2786         int z;
2787         z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2788         SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2789         s = SvPVX(sv) + SvCUR(sv) + z;
2790     }
2791     Copy(fops, s, arg, U16);
2792     Safefree(fops);
2793     sv_magic(sv, Nullsv, 'f', Nullch, 0);
2794     SvCOMPILED_on(sv);
2795 }
2796