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