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