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 sortcmp _((const void *, const void *));
35 static int sortcv _((const void *, const void *));
45 cxix = dopoptosub(cxstack_ix);
49 if (cxstack[cxix].blk_gimme == G_ARRAY)
62 register PMOP *pm = (PMOP*)cLOGOP->op_other;
68 t = SvPV(tmpstr, len);
70 if (pm->op_pmregexp) {
71 regfree(pm->op_pmregexp);
72 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
75 pm->op_pmregexp = regcomp(t, t + len, pm);
77 if (!pm->op_pmregexp->prelen && curpm)
79 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
80 pm->op_pmflags |= PMf_WHITE;
82 if (pm->op_pmflags & PMf_KEEP) {
83 pm->op_pmflags &= ~PMf_RUNTIME; /* no point compiling again */
85 cLOGOP->op_first->op_next = op->op_next;
93 register PMOP *pm = (PMOP*) cLOGOP->op_other;
94 register CONTEXT *cx = &cxstack[cxstack_ix];
95 register SV *dstr = cx->sb_dstr;
96 register char *s = cx->sb_s;
97 register char *m = cx->sb_m;
98 char *orig = cx->sb_orig;
99 register REGEXP *rx = pm->op_pmregexp;
101 if (cx->sb_iters++) {
102 if (cx->sb_iters > cx->sb_maxiters)
103 DIE("Substitution loop");
105 sv_catsv(dstr, POPs);
107 Safefree(rx->subbase);
108 rx->subbase = cx->sb_subbase;
111 if (cx->sb_once || !regexec(rx, s, cx->sb_strend, orig,
112 s == m, Nullsv, cx->sb_safebase))
114 SV *targ = cx->sb_targ;
115 sv_catpvn(dstr, s, cx->sb_strend - s);
117 Safefree(SvPVX(targ));
118 SvPVX(targ) = SvPVX(dstr);
119 SvCUR_set(targ, SvCUR(dstr));
120 SvLEN_set(targ, SvLEN(dstr));
124 (void)SvPOK_only(targ);
126 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
128 RETURNOP(pm->op_next);
131 if (rx->subbase && rx->subbase != orig) {
134 cx->sb_orig = orig = rx->subbase;
136 cx->sb_strend = s + (cx->sb_strend - m);
138 cx->sb_m = m = rx->startp[0];
139 sv_catpvn(dstr, s, m-s);
140 cx->sb_s = rx->endp[0];
141 cx->sb_subbase = rx->subbase;
143 rx->subbase = Nullch; /* so recursion works */
144 RETURNOP(pm->op_pmreplstart);
149 dSP; dMARK; dORIGMARK;
150 register SV *form = *++MARK;
162 bool chopspace = (strchr(chopset, ' ') != Nullch);
169 if (!SvCOMPILED(form)) {
170 SvREADONLY_off(form);
174 SvPV_force(formtarget, len);
175 t = SvGROW(formtarget, len + SvCUR(form) + 1); /* XXX SvCUR bad */
178 /* need to jump to the next word */
179 s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
188 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
189 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
190 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
191 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
192 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
194 case FF_CHECKNL: name = "CHECKNL"; break;
195 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
196 case FF_SPACE: name = "SPACE"; break;
197 case FF_HALFSPACE: name = "HALFSPACE"; break;
198 case FF_ITEM: name = "ITEM"; break;
199 case FF_CHOP: name = "CHOP"; break;
200 case FF_LINEGLOB: name = "LINEGLOB"; break;
201 case FF_NEWLINE: name = "NEWLINE"; break;
202 case FF_MORE: name = "MORE"; break;
203 case FF_LINEMARK: name = "LINEMARK"; break;
204 case FF_END: name = "END"; break;
207 fprintf(stderr, "%-16s%ld\n", name, (long) arg);
209 fprintf(stderr, "%-16s\n", name);
238 warn("Not enough format arguments");
243 item = s = SvPV(sv, len);
245 if (itemsize > fieldsize)
246 itemsize = fieldsize;
247 send = chophere = s + itemsize;
259 item = s = SvPV(sv, len);
261 if (itemsize <= fieldsize) {
262 send = chophere = s + itemsize;
273 itemsize = fieldsize;
274 send = chophere = s + itemsize;
275 while (s < send || (s == send && isSPACE(*s))) {
285 if (strchr(chopset, *s))
290 itemsize = chophere - item;
295 arg = fieldsize - itemsize;
304 arg = fieldsize - itemsize;
318 int ch = *t++ = *s++;
322 if ( !((*t++ = *s++) & ~31) )
332 while (*s && isSPACE(*s))
339 item = s = SvPV(sv, len);
352 SvCUR_set(formtarget, t - SvPVX(formtarget));
353 sv_catpvn(formtarget, item, itemsize);
354 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
355 t = SvPVX(formtarget) + SvCUR(formtarget);
360 /* If the field is marked with ^ and the value is undefined,
363 if ((arg & 512) && !SvOK(sv)) {
372 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
374 sprintf(t, "%*.0f", (int) fieldsize, value);
381 while (t-- > linemark && *t == ' ') ;
389 if (arg) { /* repeat until fields exhausted? */
391 SvCUR_set(formtarget, t - SvPVX(formtarget));
392 lines += FmLINES(formtarget);
395 if (strnEQ(linemark, linemark - arg, arg))
396 DIE("Runaway format");
398 FmLINES(formtarget) = lines;
400 RETURNOP(cLISTOP->op_first);
411 arg = fieldsize - itemsize;
418 if (strnEQ(s," ",3)) {
419 while (s > SvPVX(formtarget) && isSPACE(s[-1]))
430 SvCUR_set(formtarget, t - SvPVX(formtarget));
431 FmLINES(formtarget) += lines;
443 if (stack_base + *markstack_ptr == sp) {
445 if (GIMME != G_ARRAY)
447 RETURNOP(op->op_next->op_next);
449 stack_sp = stack_base + *markstack_ptr + 1;
450 pp_pushmark(); /* push dst */
451 pp_pushmark(); /* push src */
452 ENTER; /* enter outer scope */
455 SAVESPTR(GvSV(defgv));
457 ENTER; /* enter inner scope */
460 src = stack_base[*markstack_ptr];
465 if (op->op_type == OP_MAPSTART)
466 pp_pushmark(); /* push top */
467 return ((LOGOP*)op->op_next)->op_other;
472 DIE("panic: mapstart"); /* uses grepstart */
478 I32 diff = (sp - stack_base) - *markstack_ptr;
486 if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
487 shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
488 count = (sp - stack_base) - markstack_ptr[-1] + 2;
493 markstack_ptr[-1] += shift;
494 *markstack_ptr += shift;
498 dst = stack_base + (markstack_ptr[-2] += diff) - 1;
501 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
503 LEAVE; /* exit inner scope */
506 if (markstack_ptr[-1] > *markstack_ptr) {
509 (void)POPMARK; /* pop top */
510 LEAVE; /* exit outer scope */
511 (void)POPMARK; /* pop src */
512 items = --*markstack_ptr - markstack_ptr[-1];
513 (void)POPMARK; /* pop dst */
514 SP = stack_base + POPMARK; /* pop original mark */
515 if (GIMME != G_ARRAY) {
526 ENTER; /* enter inner scope */
529 src = stack_base[markstack_ptr[-1]];
533 RETURNOP(cLOGOP->op_other);
540 dSP; dMARK; dORIGMARK;
542 SV **myorigmark = ORIGMARK;
548 OP* nextop = op->op_next;
550 if (gimme != G_ARRAY) {
555 if (op->op_flags & OPf_STACKED) {
557 if (op->op_flags & OPf_SPECIAL) {
558 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
559 kid = kUNOP->op_first; /* pass rv2gv */
560 kid = kUNOP->op_first; /* pass leave */
561 sortcop = kid->op_next;
562 stash = curcop->cop_stash;
565 cv = sv_2cv(*++MARK, &stash, &gv, 0);
566 if (!(cv && CvROOT(cv))) {
568 SV *tmpstr = sv_newmortal();
569 gv_efullname(tmpstr, gv);
570 if (cv && CvXSUB(cv))
571 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
572 DIE("Undefined sort subroutine \"%s\" called",
577 DIE("Xsub called in sort");
578 DIE("Undefined subroutine in sort");
580 DIE("Not a CODE reference in sort");
582 sortcop = CvSTART(cv);
583 SAVESPTR(CvROOT(cv)->op_ppaddr);
584 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
587 curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
592 stash = curcop->cop_stash;
596 while (MARK < SP) { /* This may or may not shift down one here. */
598 if (*up = *++MARK) { /* Weed out nulls. */
600 (void)sv_2pv(*up, &na);
606 max = --up - myorigmark;
619 AvREAL_off(sortstack);
620 av_extend(sortstack, 32);
622 SWITCHSTACK(stack, sortstack);
623 if (sortstash != stash) {
624 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
625 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
629 SAVESPTR(GvSV(firstgv));
630 SAVESPTR(GvSV(secondgv));
631 PUSHBLOCK(cx, CXt_LOOP, stack_base);
632 sortcxix = cxstack_ix;
634 qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
637 SWITCHSTACK(sortstack, oldstack);
643 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
644 qsort((char*)(ORIGMARK+1), max, sizeof(SV*), sortcmp);
647 stack_sp = ORIGMARK + max;
655 if (GIMME == G_ARRAY)
656 return cCONDOP->op_true;
657 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
664 if (GIMME == G_ARRAY) {
665 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
669 SV *targ = PAD_SV(op->op_targ);
671 if ((op->op_private & OPpFLIP_LINENUM)
672 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
674 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
675 if (op->op_flags & OPf_SPECIAL) {
682 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
695 if (GIMME == G_ARRAY) {
701 if (SvNIOK(left) || !SvPOK(left) ||
702 (looks_like_number(left) && *SvPVX(left) != '0') ) {
706 EXTEND(SP, max - i + 1);
708 sv = sv_mortalcopy(&sv_no);
714 SV *final = sv_mortalcopy(right);
716 char *tmps = SvPV(final, len);
718 sv = sv_mortalcopy(left);
719 while (!SvNIOK(sv) && SvCUR(sv) <= len &&
720 strNE(SvPVX(sv),tmps) ) {
722 sv = sv_2mortal(newSVsv(sv));
725 if (strEQ(SvPVX(sv),tmps))
731 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
733 if ((op->op_private & OPpFLIP_LINENUM)
734 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
736 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
737 sv_catpv(targ, "E0");
752 register CONTEXT *cx;
754 for (i = cxstack_ix; i >= 0; i--) {
756 switch (cx->cx_type) {
759 warn("Exiting substitution via %s", op_name[op->op_type]);
763 warn("Exiting subroutine via %s", op_name[op->op_type]);
767 warn("Exiting eval via %s", op_name[op->op_type]);
770 if (!cx->blk_loop.label ||
771 strNE(label, cx->blk_loop.label) ) {
772 DEBUG_l(deb("(Skipping label #%d %s)\n",
773 i, cx->blk_loop.label));
776 DEBUG_l( deb("(Found label #%d %s)\n", i, label));
784 dopoptosub(startingblock)
788 register CONTEXT *cx;
789 for (i = startingblock; i >= 0; i--) {
791 switch (cx->cx_type) {
796 DEBUG_l( deb("(Found sub #%d)\n", i));
804 dopoptoeval(startingblock)
808 register CONTEXT *cx;
809 for (i = startingblock; i >= 0; i--) {
811 switch (cx->cx_type) {
815 DEBUG_l( deb("(Found eval #%d)\n", i));
823 dopoptoloop(startingblock)
827 register CONTEXT *cx;
828 for (i = startingblock; i >= 0; i--) {
830 switch (cx->cx_type) {
833 warn("Exiting substitition via %s", op_name[op->op_type]);
837 warn("Exiting subroutine via %s", op_name[op->op_type]);
841 warn("Exiting eval via %s", op_name[op->op_type]);
844 DEBUG_l( deb("(Found loop #%d)\n", i));
855 register CONTEXT *cx;
859 while (cxstack_ix > cxix) {
860 cx = &cxstack[cxstack_ix--];
861 DEBUG_l(fprintf(stderr, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
862 block_type[cx->cx_type]));
863 /* Note: we don't need to restore the base context info till the end. */
864 switch (cx->cx_type) {
893 int oldrunlevel = runlevel;
894 int was_in_eval = in_eval;
904 message = mess(pat, &args);
906 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
911 PUSHs(sv_2mortal(newSVpv(message,0)));
913 perl_call_sv((SV*)cv, G_DISCARD);
915 restartop = die_where(message);
916 if ((!restartop && was_in_eval) || oldrunlevel > 1)
927 register CONTEXT *cx;
932 errsv = GvSV(gv_fetchpv("@",TRUE, SVt_PV));
933 /* As destructors may produce errors we set $@ at the last moment */
934 sv_setpv(errsv, ""); /* clear $@ before destroying */
936 cxix = dopoptoeval(cxstack_ix);
940 if (cxix < cxstack_ix)
944 if (cx->cx_type != CXt_EVAL) {
945 fprintf(stderr, "panic: die %s", message);
950 if (gimme == G_SCALAR)
951 *++newsp = &sv_undef;
956 sv_insert(errsv, 0, 0, message, strlen(message));
957 if (optype == OP_REQUIRE)
958 DIE("%s", SvPVx(GvSV(gv_fetchpv("@",TRUE, SVt_PV)), na));
962 fputs(message, stderr);
963 (void)fflush(stderr);
965 (void)UNLINK(e_tmpname);
966 statusvalue = SHIFTSTATUS(statusvalue);
968 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
970 my_exit((I32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
978 if (SvTRUE(left) != SvTRUE(right))
990 RETURNOP(cLOGOP->op_other);
999 RETURNOP(cLOGOP->op_other);
1006 SV** mark = (stack_base + *markstack_ptr + 1);
1008 while (mark < sp) { /* emulate old interface */
1013 return pp_entersub();
1020 register I32 cxix = dopoptosub(cxstack_ix);
1021 register CONTEXT *cx;
1031 if (GIMME != G_ARRAY)
1035 if (DBsub && cxix >= 0 &&
1036 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1040 cxix = dopoptosub(cxix - 1);
1042 cx = &cxstack[cxix];
1043 if (GIMME != G_ARRAY) {
1046 sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
1050 dbcxix = dopoptosub(cxix - 1);
1051 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1052 cx = &cxstack[dbcxix];
1054 PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
1055 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1056 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1059 if (cx->cx_type == CXt_SUB) {
1061 gv_efullname(sv, CvGV(cxstack[cxix].blk_sub.cv));
1062 PUSHs(sv_2mortal(sv));
1063 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1066 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1067 PUSHs(sv_2mortal(newSViv(0)));
1069 PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
1070 if (cx->cx_type == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
1071 PUSHs(cx->blk_eval.cur_text);
1073 if (cx->blk_sub.hasargs && curcop->cop_stash == debstash) {
1074 AV *ary = cx->blk_sub.argarray;
1075 int off = AvARRAY(ary) - AvALLOC(ary);
1079 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1082 AvREAL_off(dbargs); /* XXX Should be REIFY */
1085 if (AvMAX(dbargs) < AvFILL(ary) + off)
1086 av_extend(dbargs, AvFILL(ary) + off);
1087 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
1088 AvFILL(dbargs) = AvFILL(ary) + off;
1098 SV **str1 = (SV **) a;
1099 SV **str2 = (SV **) b;
1100 I32 oldsaveix = savestack_ix;
1101 I32 oldscopeix = scopestack_ix;
1103 GvSV(firstgv) = *str1;
1104 GvSV(secondgv) = *str2;
1105 stack_sp = stack_base;
1108 if (stack_sp != stack_base + 1)
1109 croak("Sort subroutine didn't return single value");
1110 if (!SvNIOKp(*stack_sp))
1111 croak("Sort subroutine didn't return a numeric value");
1112 result = SvIV(*stack_sp);
1113 while (scopestack_ix > oldscopeix) {
1116 leave_scope(oldsaveix);
1125 register SV *str1 = *(SV **) a;
1126 register SV *str2 = *(SV **) b;
1129 if (SvCUR(str1) < SvCUR(str2)) {
1131 if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str1)))
1137 else if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str2)))
1139 else if (SvCUR(str1) == SvCUR(str2))
1154 sv_reset(tmps, curcop->cop_stash);
1167 TAINT_NOT; /* Each statement is presumed innocent */
1168 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1171 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1175 register CONTEXT *cx;
1176 I32 gimme = G_ARRAY;
1186 DIE("No DB::DB routine defined");
1188 if (CvDEPTH(cv) >= 1) /* don't do recursive DB::DB call */
1197 push_return(op->op_next);
1198 PUSHBLOCK(cx, CXt_SUB, sp);
1201 (void)SvREFCNT_inc(cv);
1203 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1204 RETURNOP(CvSTART(cv));
1218 register CONTEXT *cx;
1223 svp = &curpad[op->op_targ]; /* "my" variable */
1225 svp = &GvSV((GV*)POPs); /* symbol table variable */
1231 PUSHBLOCK(cx, CXt_LOOP, SP);
1232 PUSHLOOP(cx, svp, MARK);
1233 cx->blk_loop.iterary = stack;
1234 cx->blk_loop.iterix = MARK - stack_base;
1242 register CONTEXT *cx;
1249 PUSHBLOCK(cx, CXt_LOOP, SP);
1250 PUSHLOOP(cx, 0, SP);
1258 register CONTEXT *cx;
1267 if (gimme == G_SCALAR) {
1268 if (op->op_private & OPpLEAVE_VOID)
1272 *++newsp = sv_mortalcopy(*SP);
1274 *++newsp = &sv_undef;
1279 *++newsp = sv_mortalcopy(*++mark);
1281 curpm = newpm; /* Don't pop $1 et al till now */
1293 register CONTEXT *cx;
1299 if (stack == sortstack) {
1300 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
1301 if (cxstack_ix > sortcxix)
1303 AvARRAY(stack)[1] = *SP;
1304 stack_sp = stack_base + 1;
1309 cxix = dopoptosub(cxstack_ix);
1311 DIE("Can't return outside a subroutine");
1312 if (cxix < cxstack_ix)
1316 switch (cx->cx_type) {
1322 if (optype == OP_REQUIRE &&
1323 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1325 char *name = cx->blk_eval.old_name;
1326 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1327 DIE("%s did not return a true value", name);
1331 DIE("panic: return");
1335 if (gimme == G_SCALAR) {
1337 *++newsp = sv_mortalcopy(*SP);
1339 *++newsp = &sv_undef;
1343 *++newsp = sv_mortalcopy(*++MARK);
1345 curpm = newpm; /* Don't pop $1 et al till now */
1349 return pop_return();
1356 register CONTEXT *cx;
1362 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
1364 if (op->op_flags & OPf_SPECIAL) {
1365 cxix = dopoptoloop(cxstack_ix);
1367 DIE("Can't \"last\" outside a block");
1370 cxix = dopoptolabel(cPVOP->op_pv);
1372 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1374 if (cxix < cxstack_ix)
1378 switch (cx->cx_type) {
1381 nextop = cx->blk_loop.last_op->op_next;
1386 nextop = pop_return();
1390 nextop = pop_return();
1397 if (gimme == G_SCALAR) {
1399 *++newsp = sv_mortalcopy(*SP);
1401 *++newsp = &sv_undef;
1405 *++newsp = sv_mortalcopy(*++mark);
1407 curpm = newpm; /* Don't pop $1 et al till now */
1417 register CONTEXT *cx;
1420 if (op->op_flags & OPf_SPECIAL) {
1421 cxix = dopoptoloop(cxstack_ix);
1423 DIE("Can't \"next\" outside a block");
1426 cxix = dopoptolabel(cPVOP->op_pv);
1428 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1430 if (cxix < cxstack_ix)
1434 oldsave = scopestack[scopestack_ix - 1];
1435 LEAVE_SCOPE(oldsave);
1436 return cx->blk_loop.next_op;
1442 register CONTEXT *cx;
1445 if (op->op_flags & OPf_SPECIAL) {
1446 cxix = dopoptoloop(cxstack_ix);
1448 DIE("Can't \"redo\" outside a block");
1451 cxix = dopoptolabel(cPVOP->op_pv);
1453 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1455 if (cxix < cxstack_ix)
1459 oldsave = scopestack[scopestack_ix - 1];
1460 LEAVE_SCOPE(oldsave);
1461 return cx->blk_loop.redo_op;
1464 static OP* lastgotoprobe;
1467 dofindlabel(op,label,opstack)
1475 if (op->op_type == OP_LEAVE ||
1476 op->op_type == OP_SCOPE ||
1477 op->op_type == OP_LEAVELOOP ||
1478 op->op_type == OP_LEAVETRY)
1479 *ops++ = cUNOP->op_first;
1481 if (op->op_flags & OPf_KIDS) {
1482 /* First try all the kids at this level, since that's likeliest. */
1483 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1484 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1485 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1488 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
1489 if (kid == lastgotoprobe)
1491 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1492 if (ops > opstack &&
1493 (ops[-1]->op_type == OP_NEXTSTATE ||
1494 ops[-1]->op_type == OP_DBSTATE))
1499 if (op = dofindlabel(kid,label,ops))
1509 return pp_goto(ARGS);
1518 register CONTEXT *cx;
1521 int do_dump = (op->op_type == OP_DUMP);
1524 if (op->op_flags & OPf_STACKED) {
1527 /* This egregious kludge implements goto &subroutine */
1528 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1530 register CONTEXT *cx;
1531 CV* cv = (CV*)SvRV(sv);
1536 /* First do some returnish stuff. */
1537 cxix = dopoptosub(cxstack_ix);
1539 DIE("Can't goto subroutine outside a subroutine");
1540 if (cxix < cxstack_ix)
1544 if (cx->blk_sub.hasargs) { /* put @_ back onto stack */
1545 AV* av = cx->blk_sub.argarray;
1547 items = AvFILL(av) + 1;
1548 Copy(AvARRAY(av), ++stack_sp, items, SV*);
1550 GvAV(defgv) = cx->blk_sub.savearray;
1554 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1555 SvREFCNT_dec(cx->blk_sub.cv);
1556 oldsave = scopestack[scopestack_ix - 1];
1557 LEAVE_SCOPE(oldsave);
1559 /* Now do some callish stuff. */
1562 if (CvOLDSTYLE(cv)) {
1563 I32 (*fp3)_((int,int,int));
1568 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1569 items = (*fp3)(CvXSUBANY(cv).any_i32,
1570 mark - stack_base + 1,
1572 sp = stack_base + items;
1575 (void)(*CvXSUB(cv))(cv);
1578 return pop_return();
1581 AV* padlist = CvPADLIST(cv);
1582 SV** svp = AvARRAY(padlist);
1583 cx->blk_sub.cv = cv;
1584 cx->blk_sub.olddepth = CvDEPTH(cv);
1586 if (CvDEPTH(cv) < 2)
1587 (void)SvREFCNT_inc(cv);
1588 else { /* save temporaries on recursion? */
1589 if (CvDEPTH(cv) == 100 && dowarn)
1590 warn("Deep recursion on subroutine \"%s\"",
1592 if (CvDEPTH(cv) > AvFILL(padlist)) {
1593 AV *newpad = newAV();
1594 AV *oldpad = (AV*)AvARRAY(svp[CvDEPTH(cv)-1]);
1595 I32 ix = AvFILL((AV*)svp[1]);
1596 svp = AvARRAY(svp[0]);
1597 for ( ;ix > 0; ix--) {
1598 if (svp[ix] != &sv_undef) {
1599 char *name = SvPVX(svp[ix]);
1600 if (SvFLAGS(svp[ix]) & SVf_FAKE) {
1601 /* outer lexical? */
1602 av_store(newpad, ix,
1603 SvREFCNT_inc(AvARRAY(oldpad)[ix]) );
1605 else { /* our own lexical */
1607 av_store(newpad, ix, sv = (SV*)newAV());
1608 else if (*name == '%')
1609 av_store(newpad, ix, sv = (SV*)newHV());
1611 av_store(newpad, ix, sv = NEWSV(0,0));
1616 av_store(newpad, ix, sv = NEWSV(0,0));
1620 if (cx->blk_sub.hasargs) {
1623 av_store(newpad, 0, (SV*)av);
1624 AvFLAGS(av) = AVf_REIFY;
1626 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1627 AvFILL(padlist) = CvDEPTH(cv);
1628 svp = AvARRAY(padlist);
1632 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1633 if (cx->blk_sub.hasargs) {
1634 AV* av = (AV*)curpad[0];
1637 cx->blk_sub.savearray = GvAV(defgv);
1638 cx->blk_sub.argarray = av;
1639 GvAV(defgv) = cx->blk_sub.argarray;
1642 if (items >= AvMAX(av) + 1) {
1644 if (AvARRAY(av) != ary) {
1645 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1646 SvPVX(av) = (char*)ary;
1648 if (items >= AvMAX(av) + 1) {
1649 AvMAX(av) = items - 1;
1650 Renew(ary,items+1,SV*);
1652 SvPVX(av) = (char*)ary;
1655 Copy(mark,AvARRAY(av),items,SV*);
1656 AvFILL(av) = items - 1;
1664 RETURNOP(CvSTART(cv));
1668 label = SvPV(sv,na);
1670 else if (op->op_flags & OPf_SPECIAL) {
1672 DIE("goto must have label");
1675 label = cPVOP->op_pv;
1677 if (label && *label) {
1684 for (ix = cxstack_ix; ix >= 0; ix--) {
1686 switch (cx->cx_type) {
1688 gotoprobe = CvROOT(cx->blk_sub.cv);
1691 gotoprobe = eval_root; /* XXX not good for nested eval */
1694 gotoprobe = cx->blk_oldcop->op_sibling;
1700 gotoprobe = cx->blk_oldcop->op_sibling;
1702 gotoprobe = main_root;
1708 gotoprobe = main_root;
1711 retop = dofindlabel(gotoprobe, label, enterops);
1714 lastgotoprobe = gotoprobe;
1717 DIE("Can't find label %s", label);
1719 /* pop unwanted frames */
1721 if (ix < cxstack_ix) {
1728 oldsave = scopestack[scopestack_ix];
1729 LEAVE_SCOPE(oldsave);
1732 /* push wanted frames */
1734 if (*enterops && enterops[1]) {
1736 for (ix = 1; enterops[ix]; ix++) {
1750 restartop = 0; /* hmm, must be GNU unexec().. */
1754 if (stack == signalstack) {
1756 longjmp(top_env, 3);
1780 double value = SvNVx(GvSV(cCOP->cop_gv));
1781 register I32 match = I_32(value);
1784 if (((double)match) > value)
1785 --match; /* was fractional--truncate other way */
1787 match -= cCOP->uop.scop.scop_offset;
1790 else if (match > cCOP->uop.scop.scop_max)
1791 match = cCOP->uop.scop.scop_max;
1792 op = cCOP->uop.scop.scop_next[match];
1802 op = op->op_next; /* can't assume anything */
1804 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
1805 match -= cCOP->uop.scop.scop_offset;
1808 else if (match > cCOP->uop.scop.scop_max)
1809 match = cCOP->uop.scop.scop_max;
1810 op = cCOP->uop.scop.scop_next[match];
1819 save_lines(array, sv)
1823 register char *s = SvPVX(sv);
1824 register char *send = SvPVX(sv) + SvCUR(sv);
1826 register I32 line = 1;
1828 while (s && s < send) {
1829 SV *tmpstr = NEWSV(85,0);
1831 sv_upgrade(tmpstr, SVt_PVMG);
1832 t = strchr(s, '\n');
1838 sv_setpvn(tmpstr, s, t - s);
1839 av_store(array, line++, tmpstr);
1855 /* set up a scratch pad */
1860 SAVESPTR(comppad_name);
1861 SAVEINT(comppad_name_fill);
1862 SAVEINT(min_intro_pending);
1863 SAVEINT(max_intro_pending);
1866 compcv = (CV*)NEWSV(1104,0);
1867 sv_upgrade((SV *)compcv, SVt_PVCV);
1870 comppad_name = newAV();
1871 comppad_name_fill = 0;
1872 min_intro_pending = 0;
1873 av_push(comppad, Nullsv);
1874 curpad = AvARRAY(comppad);
1877 comppadlist = newAV();
1878 AvREAL_off(comppadlist);
1879 av_store(comppadlist, 0, SvREFCNT_inc((SV*)comppad_name));
1880 av_store(comppadlist, 1, SvREFCNT_inc((SV*)comppad));
1881 CvPADLIST(compcv) = comppadlist;
1883 /* make sure we compile in the right package */
1885 newstash = curcop->cop_stash;
1886 if (curstash != newstash) {
1888 curstash = newstash;
1892 SAVEFREESV(beginav);
1894 /* try to compile it */
1898 curcop = &compiling;
1899 curcop->cop_arybase = 0;
1904 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
1905 if (yyparse() || error_count || !eval_root) {
1921 if (optype == OP_REQUIRE)
1922 DIE("%s", SvPVx(GvSV(gv_fetchpv("@",TRUE, SVt_PV)), na));
1926 rspara = (nrslen == 2);
1932 rspara = (nrslen == 2);
1933 compiling.cop_line = 0;
1935 SAVEFREEOP(eval_root);
1936 if (gimme & G_ARRAY)
1941 DEBUG_x(dump_eval());
1943 /* compiled okay, so do it */
1945 RETURNOP(eval_start);
1951 register CONTEXT *cx;
1956 I32 gimme = G_SCALAR;
1960 if (SvNIOK(sv) && !SvPOKp(sv)) {
1961 if (atof(patchlevel) + 0.000999 < SvNV(sv))
1962 DIE("Perl %3.3f required--this is only version %s, stopped",
1963 SvNV(sv),patchlevel);
1966 name = SvPV(sv, na);
1968 DIE("Null filename used");
1969 if (op->op_type == OP_REQUIRE &&
1970 (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
1974 /* prepare to compile file */
1976 tmpname = savepv(name);
1977 if (*tmpname == '/' ||
1979 (tmpname[1] == '/' ||
1980 (tmpname[1] == '.' && tmpname[2] == '/')))
1982 || ((*tmpname == '[' || *tmpname == '<') &&
1983 (tmpname[1] == '-' || tmpname[1] == ']' || tmpname[1] == '>'))
1987 tryrsfp = fopen(tmpname,"r");
1990 AV *ar = GvAVn(incgv);
1993 for (i = 0; i <= AvFILL(ar); i++) {
1995 if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
1996 croak("Error converting file specification %s",
1997 SvPVx(*av_fetch(ar, i, TRUE), na));
2000 (void)sprintf(buf, "%s/%s",
2001 SvPVx(*av_fetch(ar, i, TRUE), na), name);
2003 tryrsfp = fopen(buf, "r");
2007 if (*s == '.' && s[1] == '/')
2010 tmpname = savepv(s);
2015 SAVESPTR(compiling.cop_filegv);
2016 compiling.cop_filegv = gv_fetchfile(tmpname);
2020 if (op->op_type == OP_REQUIRE) {
2021 sprintf(tokenbuf,"Can't locate %s in @INC", name);
2022 if (instr(tokenbuf,".h "))
2023 strcat(tokenbuf," (change .h to .ph maybe?)");
2024 if (instr(tokenbuf,".ph "))
2025 strcat(tokenbuf," (did you run h2ph?)");
2032 /* Assume success here to prevent recursive requirement. */
2033 (void)hv_store(GvHVn(incgv), name, strlen(name),
2034 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2038 lex_start(sv_2mortal(newSVpv("",0)));
2040 name = savepv(name);
2045 /* switch to eval mode */
2047 push_return(op->op_next);
2048 PUSHBLOCK(cx, CXt_EVAL, SP);
2049 PUSHEVAL(cx, name, compiling.cop_filegv);
2051 compiling.cop_line = 0;
2054 return doeval(G_SCALAR);
2059 return pp_require(ARGS);
2065 register CONTEXT *cx;
2071 if (!SvPV(sv,len) || !len)
2073 TAINT_PROPER("eval");
2079 /* switch to eval mode */
2081 SAVESPTR(compiling.cop_filegv);
2082 sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
2083 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2084 compiling.cop_line = 1;
2085 SAVEDELETE(defstash, savepv(tmpbuf), strlen(tmpbuf));
2087 hints = op->op_targ;
2089 push_return(op->op_next);
2090 PUSHBLOCK(cx, CXt_EVAL, SP);
2091 PUSHEVAL(cx, 0, compiling.cop_filegv);
2093 /* prepare to compile string */
2095 if (perldb && curstash != debstash)
2096 save_lines(GvAV(compiling.cop_filegv), linestr);
2098 return doeval(gimme);
2108 register CONTEXT *cx;
2114 retop = pop_return();
2116 if (gimme == G_SCALAR) {
2117 if (op->op_private & OPpLEAVE_VOID)
2122 if (SvFLAGS(TOPs) & SVs_TEMP)
2125 *MARK = sv_mortalcopy(TOPs);
2135 for (mark = newsp + 1; mark <= SP; mark++)
2136 if (!(SvFLAGS(TOPs) & SVs_TEMP))
2137 *mark = sv_mortalcopy(*mark);
2138 /* in case LEAVE wipes old return values */
2140 curpm = newpm; /* Don't pop $1 et al till now */
2142 if (optype != OP_ENTEREVAL) {
2143 char *name = cx->blk_eval.old_name;
2145 if (!(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
2146 /* Unassume the success we assumed earlier. */
2147 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2149 if (optype == OP_REQUIRE)
2150 retop = die("%s did not return a true value", name);
2156 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2164 register CONTEXT *cx;
2170 push_return(cLOGOP->op_other->op_next);
2171 PUSHBLOCK(cx, CXt_EVAL, SP);
2173 eval_root = op; /* Only needed so that goto works right. */
2176 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2187 register CONTEXT *cx;
2194 if (gimme == G_SCALAR) {
2195 if (op->op_private & OPpLEAVE_VOID)
2200 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2203 *MARK = sv_mortalcopy(TOPs);
2213 for (mark = newsp + 1; mark <= SP; mark++)
2214 if (!(SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)))
2215 *mark = sv_mortalcopy(*mark);
2216 /* in case LEAVE wipes old return values */
2218 curpm = newpm; /* Don't pop $1 et al till now */
2221 sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
2230 register char *s = SvPV_force(sv, len);
2231 register char *send = s + len;
2232 register char *base;
2233 register I32 skipspaces = 0;
2236 bool postspace = FALSE;
2243 New(804, fops, (send - s)*3+2, U16); /* Almost certainly too long... */
2248 *fpc++ = FF_LINEMARK;
2249 noblank = repeat = FALSE;
2267 case ' ': case '\t':
2280 *fpc++ = FF_LITERAL;
2287 *fpc++ = skipspaces;
2291 *fpc++ = FF_NEWLINE;
2295 arg = fpc - linepc + 1;
2302 *fpc++ = FF_LINEMARK;
2303 noblank = repeat = FALSE;
2312 ischop = s[-1] == '^';
2318 arg = (s - base) - 1;
2320 *fpc++ = FF_LITERAL;
2329 *fpc++ = FF_LINEGLOB;
2331 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2332 arg = ischop ? 512 : 0;
2342 arg |= 256 + (s - f);
2344 *fpc++ = s - base; /* fieldsize for FETCH */
2345 *fpc++ = FF_DECIMAL;
2350 bool ismore = FALSE;
2353 while (*++s == '>') ;
2354 prespace = FF_SPACE;
2356 else if (*s == '|') {
2357 while (*++s == '|') ;
2358 prespace = FF_HALFSPACE;
2363 while (*++s == '<') ;
2366 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2370 *fpc++ = s - base; /* fieldsize for FETCH */
2372 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2390 { /* need to jump to the next word */
2392 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2393 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2394 s = SvPVX(sv) + SvCUR(sv) + z;
2396 Copy(fops, s, arg, U16);