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