3 * Copyright (c) 1991-1994, Larry Wall
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.
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.
23 #define WORD_ALIGN sizeof(U16)
26 static OP *doeval _((int gimme));
27 static OP *dofindlabel _((OP *op, char *label, OP **opstack));
28 static void doparseform _((SV *sv));
29 static I32 dopoptoeval _((I32 startingblock));
30 static I32 dopoptolabel _((char *label));
31 static I32 dopoptoloop _((I32 startingblock));
32 static I32 dopoptosub _((I32 startingblock));
33 static void save_lines _((AV *array, SV *sv));
34 static int sortcv _((const void *, const void *));
35 static int sortcmp _((const void *, const void *));
36 static int sortcmp_locale _((const void *, const void *));
46 cxix = dopoptosub(cxstack_ix);
50 if (cxstack[cxix].blk_gimme == G_ARRAY)
63 register PMOP *pm = (PMOP*)cLOGOP->op_other;
69 t = SvPV(tmpstr, len);
71 /* JMR: Check against the last compiled regexp */
72 if ( ! pm->op_pmregexp || ! pm->op_pmregexp->precomp
73 || strnNE(pm->op_pmregexp->precomp, t, len)
74 || pm->op_pmregexp->precomp[len]) {
75 if (pm->op_pmregexp) {
76 pregfree(pm->op_pmregexp);
77 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
80 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
81 pm->op_pmregexp = pregcomp(t, t + len, pm);
84 if (!pm->op_pmregexp->prelen && curpm)
86 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
87 pm->op_pmflags |= PMf_WHITE;
89 if (pm->op_pmflags & PMf_KEEP) {
90 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
92 cLOGOP->op_first->op_next = op->op_next;
100 register PMOP *pm = (PMOP*) cLOGOP->op_other;
101 register CONTEXT *cx = &cxstack[cxstack_ix];
102 register SV *dstr = cx->sb_dstr;
103 register char *s = cx->sb_s;
104 register char *m = cx->sb_m;
105 char *orig = cx->sb_orig;
106 register REGEXP *rx = cx->sb_rx;
108 if (cx->sb_iters++) {
109 if (cx->sb_iters > cx->sb_maxiters)
110 DIE("Substitution loop");
112 if (!cx->sb_rxtainted)
113 cx->sb_rxtainted = SvTAINTED(TOPs);
114 sv_catsv(dstr, POPs);
116 Safefree(rx->subbase);
117 rx->subbase = cx->sb_subbase;
120 if (cx->sb_once || !pregexec(rx, s, cx->sb_strend, orig,
121 s == m, Nullsv, cx->sb_safebase))
123 SV *targ = cx->sb_targ;
124 sv_catpvn(dstr, s, cx->sb_strend - s);
126 (void)SvOOK_off(targ);
127 Safefree(SvPVX(targ));
128 SvPVX(targ) = SvPVX(dstr);
129 SvCUR_set(targ, SvCUR(dstr));
130 SvLEN_set(targ, SvLEN(dstr));
134 (void)SvPOK_only(targ);
136 if (cx->sb_rxtainted)
138 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
139 LEAVE_SCOPE(cx->sb_oldsave);
141 RETURNOP(pm->op_next);
144 if (rx->subbase && rx->subbase != orig) {
147 cx->sb_orig = orig = rx->subbase;
149 cx->sb_strend = s + (cx->sb_strend - m);
151 cx->sb_m = m = rx->startp[0];
152 sv_catpvn(dstr, s, m-s);
153 cx->sb_s = rx->endp[0];
154 cx->sb_subbase = rx->subbase;
155 cx->sb_rxtainted |= rx->exec_tainted;
157 rx->subbase = Nullch; /* so recursion works */
158 RETURNOP(pm->op_pmreplstart);
163 dSP; dMARK; dORIGMARK;
164 register SV *form = *++MARK;
176 bool chopspace = (strchr(chopset, ' ') != Nullch);
183 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
184 SvREADONLY_off(form);
188 SvPV_force(formtarget, len);
189 t = SvGROW(formtarget, len + SvCUR(form) + 1); /* XXX SvCUR bad */
192 /* need to jump to the next word */
193 s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
202 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
203 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
204 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
205 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
206 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
208 case FF_CHECKNL: name = "CHECKNL"; break;
209 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
210 case FF_SPACE: name = "SPACE"; break;
211 case FF_HALFSPACE: name = "HALFSPACE"; break;
212 case FF_ITEM: name = "ITEM"; break;
213 case FF_CHOP: name = "CHOP"; break;
214 case FF_LINEGLOB: name = "LINEGLOB"; break;
215 case FF_NEWLINE: name = "NEWLINE"; break;
216 case FF_MORE: name = "MORE"; break;
217 case FF_LINEMARK: name = "LINEMARK"; break;
218 case FF_END: name = "END"; break;
221 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
223 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
252 warn("Not enough format arguments");
257 item = s = SvPV(sv, len);
259 if (itemsize > fieldsize)
260 itemsize = fieldsize;
261 send = chophere = s + itemsize;
273 item = s = SvPV(sv, len);
275 if (itemsize <= fieldsize) {
276 send = chophere = s + itemsize;
287 itemsize = fieldsize;
288 send = chophere = s + itemsize;
289 while (s < send || (s == send && isSPACE(*s))) {
299 if (strchr(chopset, *s))
304 itemsize = chophere - item;
309 arg = fieldsize - itemsize;
318 arg = fieldsize - itemsize;
332 int ch = *t++ = *s++;
336 if ( !((*t++ = *s++) & ~31) )
346 while (*s && isSPACE(*s))
353 item = s = SvPV(sv, len);
366 SvCUR_set(formtarget, t - SvPVX(formtarget));
367 sv_catpvn(formtarget, item, itemsize);
368 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
369 t = SvPVX(formtarget) + SvCUR(formtarget);
374 /* If the field is marked with ^ and the value is undefined,
377 if ((arg & 512) && !SvOK(sv)) {
385 /* Formats aren't yet marked for locales, so assume "yes". */
388 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
390 sprintf(t, "%*.0f", (int) fieldsize, value);
397 while (t-- > linemark && *t == ' ') ;
405 if (arg) { /* repeat until fields exhausted? */
407 SvCUR_set(formtarget, t - SvPVX(formtarget));
408 lines += FmLINES(formtarget);
411 if (strnEQ(linemark, linemark - arg, arg))
412 DIE("Runaway format");
414 FmLINES(formtarget) = lines;
416 RETURNOP(cLISTOP->op_first);
427 arg = fieldsize - itemsize;
434 if (strnEQ(s," ",3)) {
435 while (s > SvPVX(formtarget) && isSPACE(s[-1]))
446 SvCUR_set(formtarget, t - SvPVX(formtarget));
447 FmLINES(formtarget) += lines;
459 if (stack_base + *markstack_ptr == sp) {
461 if (GIMME != G_ARRAY)
463 RETURNOP(op->op_next->op_next);
465 stack_sp = stack_base + *markstack_ptr + 1;
466 pp_pushmark(); /* push dst */
467 pp_pushmark(); /* push src */
468 ENTER; /* enter outer scope */
471 SAVESPTR(GvSV(defgv));
473 ENTER; /* enter inner scope */
476 src = stack_base[*markstack_ptr];
481 if (op->op_type == OP_MAPSTART)
482 pp_pushmark(); /* push top */
483 return ((LOGOP*)op->op_next)->op_other;
488 DIE("panic: mapstart"); /* uses grepstart */
494 I32 diff = (sp - stack_base) - *markstack_ptr;
502 if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
503 shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
504 count = (sp - stack_base) - markstack_ptr[-1] + 2;
509 markstack_ptr[-1] += shift;
510 *markstack_ptr += shift;
514 dst = stack_base + (markstack_ptr[-2] += diff) - 1;
517 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
519 LEAVE; /* exit inner scope */
522 if (markstack_ptr[-1] > *markstack_ptr) {
525 (void)POPMARK; /* pop top */
526 LEAVE; /* exit outer scope */
527 (void)POPMARK; /* pop src */
528 items = --*markstack_ptr - markstack_ptr[-1];
529 (void)POPMARK; /* pop dst */
530 SP = stack_base + POPMARK; /* pop original mark */
531 if (GIMME != G_ARRAY) {
542 ENTER; /* enter inner scope */
545 src = stack_base[markstack_ptr[-1]];
549 RETURNOP(cLOGOP->op_other);
556 dSP; dMARK; dORIGMARK;
558 SV **myorigmark = ORIGMARK;
564 OP* nextop = op->op_next;
566 if (gimme != G_ARRAY) {
571 if (op->op_flags & OPf_STACKED) {
573 if (op->op_flags & OPf_SPECIAL) {
574 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
575 kid = kUNOP->op_first; /* pass rv2gv */
576 kid = kUNOP->op_first; /* pass leave */
577 sortcop = kid->op_next;
578 stash = curcop->cop_stash;
581 cv = sv_2cv(*++MARK, &stash, &gv, 0);
582 if (!(cv && CvROOT(cv))) {
584 SV *tmpstr = sv_newmortal();
585 gv_efullname3(tmpstr, gv, Nullch);
586 if (cv && CvXSUB(cv))
587 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
588 DIE("Undefined sort subroutine \"%s\" called",
593 DIE("Xsub called in sort");
594 DIE("Undefined subroutine in sort");
596 DIE("Not a CODE reference in sort");
598 sortcop = CvSTART(cv);
599 SAVESPTR(CvROOT(cv)->op_ppaddr);
600 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
603 curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
608 stash = curcop->cop_stash;
612 while (MARK < SP) { /* This may or may not shift down one here. */
614 if (*up = *++MARK) { /* Weed out nulls. */
616 if (!sortcop && !SvPOK(*up))
617 (void)sv_2pv(*up, &na);
621 max = --up - myorigmark;
634 AvREAL_off(sortstack);
635 av_extend(sortstack, 32);
637 SWITCHSTACK(curstack, sortstack);
638 if (sortstash != stash) {
639 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
640 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
644 SAVESPTR(GvSV(firstgv));
645 SAVESPTR(GvSV(secondgv));
646 PUSHBLOCK(cx, CXt_NULL, stack_base);
647 sortcxix = cxstack_ix;
649 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
652 SWITCHSTACK(sortstack, oldstack);
658 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
659 qsort((char*)(ORIGMARK+1), max, sizeof(SV*),
660 (op->op_private & OPpLOCALE) ? sortcmp_locale : sortcmp);
663 stack_sp = ORIGMARK + max;
671 if (GIMME == G_ARRAY)
672 return cCONDOP->op_true;
673 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
680 if (GIMME == G_ARRAY) {
681 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
685 SV *targ = PAD_SV(op->op_targ);
687 if ((op->op_private & OPpFLIP_LINENUM)
688 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
690 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
691 if (op->op_flags & OPf_SPECIAL) {
698 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
711 if (GIMME == G_ARRAY) {
717 if (SvNIOKp(left) || !SvPOKp(left) ||
718 (looks_like_number(left) && *SvPVX(left) != '0') )
723 EXTEND_MORTAL(max - i + 1);
724 EXTEND(SP, max - i + 1);
727 sv = sv_2mortal(newSViv(i++));
732 SV *final = sv_mortalcopy(right);
734 char *tmps = SvPV(final, len);
736 sv = sv_mortalcopy(left);
737 while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
738 strNE(SvPVX(sv),tmps) ) {
740 sv = sv_2mortal(newSVsv(sv));
743 if (strEQ(SvPVX(sv),tmps))
749 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
751 if ((op->op_private & OPpFLIP_LINENUM)
752 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
754 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
755 sv_catpv(targ, "E0");
770 register CONTEXT *cx;
772 for (i = cxstack_ix; i >= 0; i--) {
774 switch (cx->cx_type) {
777 warn("Exiting substitution via %s", op_name[op->op_type]);
781 warn("Exiting subroutine via %s", op_name[op->op_type]);
785 warn("Exiting eval via %s", op_name[op->op_type]);
789 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
792 if (!cx->blk_loop.label ||
793 strNE(label, cx->blk_loop.label) ) {
794 DEBUG_l(deb("(Skipping label #%d %s)\n",
795 i, cx->blk_loop.label));
798 DEBUG_l( deb("(Found label #%d %s)\n", i, label));
810 cxix = dopoptosub(cxstack_ix);
814 if (cxstack[cxix].blk_gimme == G_ARRAY)
821 dopoptosub(startingblock)
825 register CONTEXT *cx;
826 for (i = startingblock; i >= 0; i--) {
828 switch (cx->cx_type) {
833 DEBUG_l( deb("(Found sub #%d)\n", i));
841 dopoptoeval(startingblock)
845 register CONTEXT *cx;
846 for (i = startingblock; i >= 0; i--) {
848 switch (cx->cx_type) {
852 DEBUG_l( deb("(Found eval #%d)\n", i));
860 dopoptoloop(startingblock)
864 register CONTEXT *cx;
865 for (i = startingblock; i >= 0; i--) {
867 switch (cx->cx_type) {
870 warn("Exiting substitution via %s", op_name[op->op_type]);
874 warn("Exiting subroutine via %s", op_name[op->op_type]);
878 warn("Exiting eval via %s", op_name[op->op_type]);
882 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
885 DEBUG_l( deb("(Found loop #%d)\n", i));
896 register CONTEXT *cx;
900 while (cxstack_ix > cxix) {
901 cx = &cxstack[cxstack_ix--];
902 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
903 block_type[cx->cx_type]));
904 /* Note: we don't need to restore the base context info till the end. */
905 switch (cx->cx_type) {
928 register CONTEXT *cx;
934 STRLEN klen = strlen(message);
936 svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
939 static char prefix[] = "\t(in cleanup) ";
940 sv_upgrade(*svp, SVt_IV);
941 (void)SvIOK_only(*svp);
942 SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
943 sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
944 sv_catpvn(GvSV(errgv), message, klen);
950 sv_setpv(GvSV(errgv), message);
952 cxix = dopoptoeval(cxstack_ix);
956 if (cxix < cxstack_ix)
960 if (cx->cx_type != CXt_EVAL) {
961 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
966 if (gimme == G_SCALAR)
967 *++newsp = &sv_undef;
972 if (optype == OP_REQUIRE)
973 DIE("%s", SvPVx(GvSV(errgv), na));
977 PerlIO_printf(PerlIO_stderr(), "%s",message);
978 PerlIO_flush(PerlIO_stderr());
987 if (SvTRUE(left) != SvTRUE(right))
999 RETURNOP(cLOGOP->op_other);
1008 RETURNOP(cLOGOP->op_other);
1015 SV** mark = (stack_base + *markstack_ptr + 1);
1017 while (mark < sp) { /* emulate old interface */
1022 return pp_entersub();
1029 register I32 cxix = dopoptosub(cxstack_ix);
1030 register CONTEXT *cx;
1040 if (GIMME != G_ARRAY)
1044 if (DBsub && cxix >= 0 &&
1045 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1049 cxix = dopoptosub(cxix - 1);
1051 cx = &cxstack[cxix];
1052 if (cxstack[cxix].cx_type == CXt_SUB) {
1053 dbcxix = dopoptosub(cxix - 1);
1054 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1055 field below is defined for any cx. */
1056 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1057 cx = &cxstack[dbcxix];
1060 if (GIMME != G_ARRAY) {
1063 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1068 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1069 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1070 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1073 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
1075 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
1076 PUSHs(sv_2mortal(sv));
1077 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1080 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1081 PUSHs(sv_2mortal(newSViv(0)));
1083 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
1084 if (cx->cx_type == CXt_EVAL) {
1085 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1086 PUSHs(cx->blk_eval.cur_text);
1089 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1090 /* Require, put the name. */
1091 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1095 else if (cx->cx_type == CXt_SUB &&
1096 cx->blk_sub.hasargs &&
1097 curcop->cop_stash == debstash)
1099 AV *ary = cx->blk_sub.argarray;
1100 int off = AvARRAY(ary) - AvALLOC(ary);
1104 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1107 AvREAL_off(dbargs); /* XXX Should be REIFY */
1110 if (AvMAX(dbargs) < AvFILL(ary) + off)
1111 av_extend(dbargs, AvFILL(ary) + off);
1112 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1113 AvFILL(dbargs) = AvFILL(ary) + off;
1123 SV **str1 = (SV **) a;
1124 SV **str2 = (SV **) b;
1125 I32 oldsaveix = savestack_ix;
1126 I32 oldscopeix = scopestack_ix;
1128 GvSV(firstgv) = *str1;
1129 GvSV(secondgv) = *str2;
1130 stack_sp = stack_base;
1133 if (stack_sp != stack_base + 1)
1134 croak("Sort subroutine didn't return single value");
1135 if (!SvNIOKp(*stack_sp))
1136 croak("Sort subroutine didn't return a numeric value");
1137 result = SvIV(*stack_sp);
1138 while (scopestack_ix > oldscopeix) {
1141 leave_scope(oldsaveix);
1150 return sv_cmp(*(SV **)a, *(SV **)b);
1154 sortcmp_locale(a, b)
1158 return sv_cmp_locale(*(SV **)a, *(SV **)b);
1170 sv_reset(tmps, curcop->cop_stash);
1183 TAINT_NOT; /* Each statement is presumed innocent */
1184 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1187 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1191 register CONTEXT *cx;
1192 I32 gimme = G_ARRAY;
1199 DIE("No DB::DB routine defined");
1201 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
1213 push_return(op->op_next);
1214 PUSHBLOCK(cx, CXt_SUB, sp);
1217 (void)SvREFCNT_inc(cv);
1219 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1220 RETURNOP(CvSTART(cv));
1234 register CONTEXT *cx;
1242 svp = &curpad[op->op_targ]; /* "my" variable */
1244 svp = &GvSV((GV*)POPs); /* symbol table variable */
1250 PUSHBLOCK(cx, CXt_LOOP, SP);
1251 PUSHLOOP(cx, svp, MARK);
1252 if (op->op_flags & OPf_STACKED)
1253 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1255 cx->blk_loop.iterary = curstack;
1256 AvFILL(curstack) = sp - stack_base;
1257 cx->blk_loop.iterix = MARK - stack_base;
1266 register CONTEXT *cx;
1273 PUSHBLOCK(cx, CXt_LOOP, SP);
1274 PUSHLOOP(cx, 0, SP);
1282 register CONTEXT *cx;
1283 struct block_loop cxloop;
1291 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1293 if (gimme == G_SCALAR) {
1294 if (op->op_private & OPpLEAVE_VOID)
1298 *++newsp = sv_mortalcopy(*SP);
1300 *++newsp = &sv_undef;
1305 *++newsp = sv_mortalcopy(*++mark);
1310 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1311 curpm = newpm; /* ... and pop $1 et al */
1323 register CONTEXT *cx;
1324 struct block_sub cxsub;
1325 bool popsub2 = FALSE;
1331 if (curstack == sortstack) {
1332 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
1333 if (cxstack_ix > sortcxix)
1335 AvARRAY(curstack)[1] = *SP;
1336 stack_sp = stack_base + 1;
1341 cxix = dopoptosub(cxstack_ix);
1343 DIE("Can't return outside a subroutine");
1344 if (cxix < cxstack_ix)
1348 switch (cx->cx_type) {
1350 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1355 if (optype == OP_REQUIRE &&
1356 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1358 char *name = cx->blk_eval.old_name;
1359 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1360 DIE("%s did not return a true value", name);
1364 DIE("panic: return");
1368 if (gimme == G_SCALAR) {
1370 *++newsp = (popsub2 && SvTEMP(*SP))
1371 ? *SP : sv_mortalcopy(*SP);
1373 *++newsp = &sv_undef;
1376 while (++MARK <= SP)
1377 *++newsp = (popsub2 && SvTEMP(*MARK))
1378 ? *MARK : sv_mortalcopy(*MARK);
1382 /* Stack values are safe: */
1384 POPSUB2(); /* release CV and @_ ... */
1386 curpm = newpm; /* ... and pop $1 et al */
1389 return pop_return();
1396 register CONTEXT *cx;
1397 struct block_loop cxloop;
1398 struct block_sub cxsub;
1405 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
1407 if (op->op_flags & OPf_SPECIAL) {
1408 cxix = dopoptoloop(cxstack_ix);
1410 DIE("Can't \"last\" outside a block");
1413 cxix = dopoptolabel(cPVOP->op_pv);
1415 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1417 if (cxix < cxstack_ix)
1421 switch (cx->cx_type) {
1423 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1425 nextop = cxloop.last_op->op_next;
1428 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1430 nextop = pop_return();
1434 nextop = pop_return();
1441 if (gimme == G_SCALAR) {
1443 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1444 ? *SP : sv_mortalcopy(*SP);
1446 *++newsp = &sv_undef;
1449 while (++MARK <= SP)
1450 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1451 ? *MARK : sv_mortalcopy(*MARK);
1456 /* Stack values are safe: */
1459 POPLOOP2(); /* release loop vars ... */
1463 POPSUB2(); /* release CV and @_ ... */
1466 curpm = newpm; /* ... and pop $1 et al */
1475 register CONTEXT *cx;
1478 if (op->op_flags & OPf_SPECIAL) {
1479 cxix = dopoptoloop(cxstack_ix);
1481 DIE("Can't \"next\" outside a block");
1484 cxix = dopoptolabel(cPVOP->op_pv);
1486 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1488 if (cxix < cxstack_ix)
1492 oldsave = scopestack[scopestack_ix - 1];
1493 LEAVE_SCOPE(oldsave);
1494 return cx->blk_loop.next_op;
1500 register CONTEXT *cx;
1503 if (op->op_flags & OPf_SPECIAL) {
1504 cxix = dopoptoloop(cxstack_ix);
1506 DIE("Can't \"redo\" outside a block");
1509 cxix = dopoptolabel(cPVOP->op_pv);
1511 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1513 if (cxix < cxstack_ix)
1517 oldsave = scopestack[scopestack_ix - 1];
1518 LEAVE_SCOPE(oldsave);
1519 return cx->blk_loop.redo_op;
1522 static OP* lastgotoprobe;
1525 dofindlabel(op,label,opstack)
1533 if (op->op_type == OP_LEAVE ||
1534 op->op_type == OP_SCOPE ||
1535 op->op_type == OP_LEAVELOOP ||
1536 op->op_type == OP_LEAVETRY)
1537 *ops++ = cUNOP->op_first;
1539 if (op->op_flags & OPf_KIDS) {
1540 /* First try all the kids at this level, since that's likeliest. */
1541 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1542 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1543 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1546 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1547 if (kid == lastgotoprobe)
1549 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1550 if (ops > opstack &&
1551 (ops[-1]->op_type == OP_NEXTSTATE ||
1552 ops[-1]->op_type == OP_DBSTATE))
1557 if (op = dofindlabel(kid,label,ops))
1567 return pp_goto(ARGS);
1576 register CONTEXT *cx;
1579 int do_dump = (op->op_type == OP_DUMP);
1582 if (op->op_flags & OPf_STACKED) {
1585 /* This egregious kludge implements goto &subroutine */
1586 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1588 register CONTEXT *cx;
1589 CV* cv = (CV*)SvRV(sv);
1594 if (!CvROOT(cv) && !CvXSUB(cv)) {
1596 SV *tmpstr = sv_newmortal();
1597 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1598 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1600 DIE("Goto undefined subroutine");
1603 /* First do some returnish stuff. */
1604 cxix = dopoptosub(cxstack_ix);
1606 DIE("Can't goto subroutine outside a subroutine");
1607 if (cxix < cxstack_ix)
1611 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1612 AV* av = cx->blk_sub.argarray;
1614 items = AvFILL(av) + 1;
1616 EXTEND(stack_sp, items); /* @_ could have been extended. */
1617 Copy(AvARRAY(av), stack_sp, items, SV*);
1619 SvREFCNT_dec(GvAV(defgv));
1620 GvAV(defgv) = cx->blk_sub.savearray;
1624 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1625 SvREFCNT_dec(cx->blk_sub.cv);
1626 oldsave = scopestack[scopestack_ix - 1];
1627 LEAVE_SCOPE(oldsave);
1629 /* Now do some callish stuff. */
1632 if (CvOLDSTYLE(cv)) {
1633 I32 (*fp3)_((int,int,int));
1638 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1639 items = (*fp3)(CvXSUBANY(cv).any_i32,
1640 mark - stack_base + 1,
1642 sp = stack_base + items;
1645 stack_sp--; /* There is no cv arg. */
1646 (void)(*CvXSUB(cv))(cv);
1649 return pop_return();
1652 AV* padlist = CvPADLIST(cv);
1653 SV** svp = AvARRAY(padlist);
1654 cx->blk_sub.cv = cv;
1655 cx->blk_sub.olddepth = CvDEPTH(cv);
1657 if (CvDEPTH(cv) < 2)
1658 (void)SvREFCNT_inc(cv);
1659 else { /* save temporaries on recursion? */
1660 if (CvDEPTH(cv) == 100 && dowarn)
1661 sub_crush_depth(cv);
1662 if (CvDEPTH(cv) > AvFILL(padlist)) {
1663 AV *newpad = newAV();
1664 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
1665 I32 ix = AvFILL((AV*)svp[1]);
1666 svp = AvARRAY(svp[0]);
1667 for ( ;ix > 0; ix--) {
1668 if (svp[ix] != &sv_undef) {
1669 char *name = SvPVX(svp[ix]);
1670 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1673 /* outer lexical or anon code */
1674 av_store(newpad, ix,
1675 SvREFCNT_inc(oldpad[ix]) );
1677 else { /* our own lexical */
1679 av_store(newpad, ix, sv = (SV*)newAV());
1680 else if (*name == '%')
1681 av_store(newpad, ix, sv = (SV*)newHV());
1683 av_store(newpad, ix, sv = NEWSV(0,0));
1688 av_store(newpad, ix, sv = NEWSV(0,0));
1692 if (cx->blk_sub.hasargs) {
1695 av_store(newpad, 0, (SV*)av);
1696 AvFLAGS(av) = AVf_REIFY;
1698 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1699 AvFILL(padlist) = CvDEPTH(cv);
1700 svp = AvARRAY(padlist);
1704 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1705 if (cx->blk_sub.hasargs) {
1706 AV* av = (AV*)curpad[0];
1709 cx->blk_sub.savearray = GvAV(defgv);
1710 cx->blk_sub.argarray = av;
1711 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
1714 if (items >= AvMAX(av) + 1) {
1716 if (AvARRAY(av) != ary) {
1717 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1718 SvPVX(av) = (char*)ary;
1720 if (items >= AvMAX(av) + 1) {
1721 AvMAX(av) = items - 1;
1722 Renew(ary,items+1,SV*);
1724 SvPVX(av) = (char*)ary;
1727 Copy(mark,AvARRAY(av),items,SV*);
1728 AvFILL(av) = items - 1;
1736 if (perldb && curstash != debstash) {
1738 * We do not care about using sv to call CV;
1739 * it's for informational purposes only.
1741 SV *sv = GvSV(DBsub);
1743 gv_efullname3(sv, CvGV(cv), Nullch);
1745 RETURNOP(CvSTART(cv));
1749 label = SvPV(sv,na);
1751 else if (op->op_flags & OPf_SPECIAL) {
1753 DIE("goto must have label");
1756 label = cPVOP->op_pv;
1758 if (label && *label) {
1765 for (ix = cxstack_ix; ix >= 0; ix--) {
1767 switch (cx->cx_type) {
1769 gotoprobe = CvROOT(cx->blk_sub.cv);
1772 gotoprobe = eval_root; /* XXX not good for nested eval */
1775 gotoprobe = cx->blk_oldcop->op_sibling;
1781 gotoprobe = cx->blk_oldcop->op_sibling;
1783 gotoprobe = main_root;
1786 DIE("Can't \"goto\" outside a block");
1792 gotoprobe = main_root;
1795 retop = dofindlabel(gotoprobe, label, enterops);
1798 lastgotoprobe = gotoprobe;
1801 DIE("Can't find label %s", label);
1803 /* pop unwanted frames */
1805 if (ix < cxstack_ix) {
1812 oldsave = scopestack[scopestack_ix];
1813 LEAVE_SCOPE(oldsave);
1816 /* push wanted frames */
1818 if (*enterops && enterops[1]) {
1820 for (ix = 1; enterops[ix]; ix++) {
1830 if (!retop) retop = main_start;
1837 restartop = 0; /* hmm, must be GNU unexec().. */
1841 if (curstack == signalstack) {
1843 Siglongjmp(top_env, 3);
1867 double value = SvNVx(GvSV(cCOP->cop_gv));
1868 register I32 match = I_32(value);
1871 if (((double)match) > value)
1872 --match; /* was fractional--truncate other way */
1874 match -= cCOP->uop.scop.scop_offset;
1877 else if (match > cCOP->uop.scop.scop_max)
1878 match = cCOP->uop.scop.scop_max;
1879 op = cCOP->uop.scop.scop_next[match];
1889 op = op->op_next; /* can't assume anything */
1891 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1892 match -= cCOP->uop.scop.scop_offset;
1895 else if (match > cCOP->uop.scop.scop_max)
1896 match = cCOP->uop.scop.scop_max;
1897 op = cCOP->uop.scop.scop_next[match];
1906 save_lines(array, sv)
1910 register char *s = SvPVX(sv);
1911 register char *send = SvPVX(sv) + SvCUR(sv);
1913 register I32 line = 1;
1915 while (s && s < send) {
1916 SV *tmpstr = NEWSV(85,0);
1918 sv_upgrade(tmpstr, SVt_PVMG);
1919 t = strchr(s, '\n');
1925 sv_setpvn(tmpstr, s, t - s);
1926 av_store(array, line++, tmpstr);
1945 /* set up a scratch pad */
1950 SAVESPTR(comppad_name);
1951 SAVEI32(comppad_name_fill);
1952 SAVEI32(min_intro_pending);
1953 SAVEI32(max_intro_pending);
1957 compcv = (CV*)NEWSV(1104,0);
1958 sv_upgrade((SV *)compcv, SVt_PVCV);
1959 CvUNIQUE_on(compcv);
1962 comppad_name = newAV();
1963 comppad_name_fill = 0;
1964 min_intro_pending = 0;
1965 av_push(comppad, Nullsv);
1966 curpad = AvARRAY(comppad);
1969 comppadlist = newAV();
1970 AvREAL_off(comppadlist);
1971 av_store(comppadlist, 0, (SV*)comppad_name);
1972 av_store(comppadlist, 1, (SV*)comppad);
1973 CvPADLIST(compcv) = comppadlist;
1975 if (saveop->op_type != OP_REQUIRE)
1976 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
1980 /* make sure we compile in the right package */
1982 newstash = curcop->cop_stash;
1983 if (curstash != newstash) {
1985 curstash = newstash;
1989 SAVEFREESV(beginav);
1991 /* try to compile it */
1995 curcop = &compiling;
1996 curcop->cop_arybase = 0;
1998 rs = newSVpv("\n", 1);
1999 if (saveop->op_flags & OPf_SPECIAL)
2002 sv_setpv(GvSV(errgv),"");
2003 if (yyparse() || error_count || !eval_root) {
2014 SP = stack_base + POPMARK; /* pop original mark */
2020 if (optype == OP_REQUIRE)
2021 DIE("%s", SvPVx(GvSV(errgv), na));
2023 rs = SvREFCNT_inc(nrs);
2027 rs = SvREFCNT_inc(nrs);
2028 compiling.cop_line = 0;
2029 SAVEFREEOP(eval_root);
2030 if (gimme & G_ARRAY)
2035 DEBUG_x(dump_eval());
2037 /* Register with debugger: */
2038 if (perldb && saveop->op_type == OP_REQUIRE) {
2039 CV *cv = perl_get_cv("DB::postponed", FALSE);
2043 XPUSHs((SV*)compiling.cop_filegv);
2045 perl_call_sv((SV*)cv, G_DISCARD);
2049 /* compiled okay, so do it */
2051 CvDEPTH(compcv) = 1;
2053 SP = stack_base + POPMARK; /* pop original mark */
2054 RETURNOP(eval_start);
2060 register CONTEXT *cx;
2065 I32 gimme = G_SCALAR;
2066 PerlIO *tryrsfp = 0;
2069 if (SvNIOKp(sv) && !SvPOKp(sv)) {
2070 SET_NUMERIC_STANDARD();
2071 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2072 DIE("Perl %s required--this is only version %s, stopped",
2073 SvPV(sv,na),patchlevel);
2076 name = SvPV(sv, na);
2078 DIE("Null filename used");
2079 TAINT_PROPER("require");
2080 if (op->op_type == OP_REQUIRE &&
2081 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
2085 /* prepare to compile file */
2087 tmpname = savepv(name);
2088 if (*tmpname == '/' ||
2090 (tmpname[1] == '/' ||
2091 (tmpname[1] == '.' && tmpname[2] == '/')))
2093 || (tmpname[0] && tmpname[1] == ':')
2096 || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
2097 (isALNUM(tmpname[1]) || strchr("$-_]>",tmpname[1]))))
2101 tryrsfp = PerlIO_open(tmpname,"r");
2104 AV *ar = GvAVn(incgv);
2107 char unixified[256];
2108 if (tounixspec_ts(tmpname,unixified) != NULL)
2109 for (i = 0; i <= AvFILL(ar); i++) {
2110 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
2112 strcat(buf,unixified);
2114 for (i = 0; i <= AvFILL(ar); i++) {
2115 (void)sprintf(buf, "%s/%s",
2116 SvPVx(*av_fetch(ar, i, TRUE), na), name);
2118 tryrsfp = PerlIO_open(buf, "r");
2122 if (*s == '.' && s[1] == '/')
2125 tmpname = savepv(s);
2130 SAVESPTR(compiling.cop_filegv);
2131 compiling.cop_filegv = gv_fetchfile(tmpname);
2135 if (op->op_type == OP_REQUIRE) {
2136 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2137 if (instr(tokenbuf,".h "))
2138 strcat(tokenbuf," (change .h to .ph maybe?)");
2139 if (instr(tokenbuf,".ph "))
2140 strcat(tokenbuf," (did you run h2ph?)");
2147 /* Assume success here to prevent recursive requirement. */
2148 (void)hv_store(GvHVn(incgv), name, strlen(name),
2149 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2153 lex_start(sv_2mortal(newSVpv("",0)));
2155 save_aptr(&rsfp_filters);
2156 rsfp_filters = NULL;
2160 name = savepv(name);
2165 /* switch to eval mode */
2167 push_return(op->op_next);
2168 PUSHBLOCK(cx, CXt_EVAL, SP);
2169 PUSHEVAL(cx, name, compiling.cop_filegv);
2171 compiling.cop_line = 0;
2174 return doeval(G_SCALAR);
2179 return pp_require(ARGS);
2185 register CONTEXT *cx;
2187 I32 gimme = GIMME, was = sub_generation;
2188 char tmpbuf[32], *safestr;
2192 if (!SvPV(sv,len) || !len)
2194 TAINT_PROPER("eval");
2200 /* switch to eval mode */
2202 SAVESPTR(compiling.cop_filegv);
2203 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2204 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2205 compiling.cop_line = 1;
2206 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2207 deleting the eval's FILEGV from the stash before gv_check() runs
2208 (i.e. before run-time proper). To work around the coredump that
2209 ensues, we always turn GvMULTI_on for any globals that were
2210 introduced within evals. See force_ident(). GSAR 96-10-12 */
2211 safestr = savepv(tmpbuf);
2212 SAVEDELETE(defstash, safestr, strlen(safestr));
2214 hints = op->op_targ;
2216 push_return(op->op_next);
2217 PUSHBLOCK(cx, CXt_EVAL, SP);
2218 PUSHEVAL(cx, 0, compiling.cop_filegv);
2220 /* prepare to compile string */
2222 if (perldb && curstash != debstash)
2223 save_lines(GvAV(compiling.cop_filegv), linestr);
2225 ret = doeval(gimme);
2226 if (perldb && was != sub_generation) { /* Some subs defined here. */
2227 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2239 register CONTEXT *cx;
2241 U8 save_flags = op -> op_flags;
2246 retop = pop_return();
2248 if (gimme == G_SCALAR) {
2249 if (op->op_private & OPpLEAVE_VOID)
2254 if (SvFLAGS(TOPs) & SVs_TEMP)
2257 *MARK = sv_mortalcopy(TOPs);
2267 for (mark = newsp + 1; mark <= SP; mark++)
2268 if (!(SvFLAGS(*mark) & SVs_TEMP))
2269 *mark = sv_mortalcopy(*mark);
2270 /* in case LEAVE wipes old return values */
2272 curpm = newpm; /* Don't pop $1 et al till now */
2275 assert(CvDEPTH(compcv) == 1);
2277 CvDEPTH(compcv) = 0;
2279 if (optype == OP_REQUIRE &&
2280 !(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
2281 char *name = cx->blk_eval.old_name;
2283 /* Unassume the success we assumed earlier. */
2284 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2285 retop = die("%s did not return a true value", name);
2291 if (!(save_flags & OPf_SPECIAL))
2292 sv_setpv(GvSV(errgv),"");
2300 register CONTEXT *cx;
2306 push_return(cLOGOP->op_other->op_next);
2307 PUSHBLOCK(cx, CXt_EVAL, SP);
2309 eval_root = op; /* Only needed so that goto works right. */
2312 sv_setpv(GvSV(errgv),"");
2323 register CONTEXT *cx;
2330 if (gimme == G_SCALAR) {
2331 if (op->op_private & OPpLEAVE_VOID)
2336 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2339 *MARK = sv_mortalcopy(TOPs);
2349 for (mark = newsp + 1; mark <= SP; mark++)
2350 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
2351 *mark = sv_mortalcopy(*mark);
2352 /* in case LEAVE wipes old return values */
2354 curpm = newpm; /* Don't pop $1 et al till now */
2357 sv_setpv(GvSV(errgv),"");
2366 register char *s = SvPV_force(sv, len);
2367 register char *send = s + len;
2368 register char *base;
2369 register I32 skipspaces = 0;
2372 bool postspace = FALSE;
2380 croak("Null picture in formline");
2382 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
2387 *fpc++ = FF_LINEMARK;
2388 noblank = repeat = FALSE;
2406 case ' ': case '\t':
2417 *fpc++ = FF_LITERAL;
2425 *fpc++ = skipspaces;
2429 *fpc++ = FF_NEWLINE;
2433 arg = fpc - linepc + 1;
2440 *fpc++ = FF_LINEMARK;
2441 noblank = repeat = FALSE;
2450 ischop = s[-1] == '^';
2456 arg = (s - base) - 1;
2458 *fpc++ = FF_LITERAL;
2467 *fpc++ = FF_LINEGLOB;
2469 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2470 arg = ischop ? 512 : 0;
2480 arg |= 256 + (s - f);
2482 *fpc++ = s - base; /* fieldsize for FETCH */
2483 *fpc++ = FF_DECIMAL;
2488 bool ismore = FALSE;
2491 while (*++s == '>') ;
2492 prespace = FF_SPACE;
2494 else if (*s == '|') {
2495 while (*++s == '|') ;
2496 prespace = FF_HALFSPACE;
2501 while (*++s == '<') ;
2504 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2508 *fpc++ = s - base; /* fieldsize for FETCH */
2510 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2528 { /* need to jump to the next word */
2530 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2531 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2532 s = SvPVX(sv) + SvCUR(sv) + z;
2534 Copy(fops, s, arg, U16);
2536 sv_magic(sv, Nullsv, 'f', Nullch, 0);