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