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