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