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