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