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