3 * Copyright (c) 1991-1999, 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 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
29 #define CALLOP this->*PL_op
32 static void *docatch_body _((va_list args));
33 static OP *docatch _((OP *o));
34 static OP *dofindlabel _((OP *o, char *label, OP **opstack, OP **oplimit));
35 static void doparseform _((SV *sv));
36 static I32 dopoptoeval _((I32 startingblock));
37 static I32 dopoptolabel _((char *label));
38 static I32 dopoptoloop _((I32 startingblock));
39 static I32 dopoptosub _((I32 startingblock));
40 static I32 dopoptosub_at _((PERL_CONTEXT *cxstk, I32 startingblock));
41 static void save_lines _((AV *array, SV *sv));
42 static I32 sortcv _((SV *a, SV *b));
43 static void qsortsv _((SV **array, size_t num_elts, I32 (*fun)(SV *a, SV *b)));
44 static OP *doeval _((int gimme, OP** startop));
45 static PerlIO *doopen_pmc _((const char *name, const char *mode));
46 static I32 sv_ncmp _((SV *a, SV *b));
47 static I32 sv_i_ncmp _((SV *a, SV *b));
48 static I32 amagic_ncmp _((SV *a, SV *b));
49 static I32 amagic_i_ncmp _((SV *a, SV *b));
50 static I32 amagic_cmp _((SV *str1, SV *str2));
51 static I32 amagic_cmp_locale _((SV *str1, SV *str2));
60 cxix = dopoptosub(cxstack_ix);
64 switch (cxstack[cxix].blk_gimme) {
81 /* XXXX Should store the old value to allow for tie/overload - and
82 restore in regcomp, where marked with XXXX. */
90 register PMOP *pm = (PMOP*)cLOGOP->op_other;
94 MAGIC *mg = Null(MAGIC*);
98 SV *sv = SvRV(tmpstr);
100 mg = mg_find(sv, 'r');
103 regexp *re = (regexp *)mg->mg_obj;
104 ReREFCNT_dec(pm->op_pmregexp);
105 pm->op_pmregexp = ReREFCNT_inc(re);
108 t = SvPV(tmpstr, len);
110 /* Check against the last compiled regexp. */
111 if (!pm->op_pmregexp || !pm->op_pmregexp->precomp ||
112 pm->op_pmregexp->prelen != len ||
113 memNE(pm->op_pmregexp->precomp, t, len))
115 if (pm->op_pmregexp) {
116 ReREFCNT_dec(pm->op_pmregexp);
117 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
119 if (PL_op->op_flags & OPf_SPECIAL)
120 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
122 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
123 pm->op_pmregexp = CALLREGCOMP(t, t + len, pm);
124 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
125 inside tie/overload accessors. */
129 #ifndef INCOMPLETE_TAINTS
132 pm->op_pmdynflags |= PMdf_TAINTED;
134 pm->op_pmdynflags &= ~PMdf_TAINTED;
138 if (!pm->op_pmregexp->prelen && PL_curpm)
140 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
141 pm->op_pmflags |= PMf_WHITE;
143 if (pm->op_pmflags & PMf_KEEP) {
144 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
145 cLOGOP->op_first->op_next = PL_op->op_next;
153 register PMOP *pm = (PMOP*) cLOGOP->op_other;
154 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
155 register SV *dstr = cx->sb_dstr;
156 register char *s = cx->sb_s;
157 register char *m = cx->sb_m;
158 char *orig = cx->sb_orig;
159 register REGEXP *rx = cx->sb_rx;
161 rxres_restore(&cx->sb_rxres, rx);
163 if (cx->sb_iters++) {
164 if (cx->sb_iters > cx->sb_maxiters)
165 DIE("Substitution loop");
167 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
168 cx->sb_rxtainted |= 2;
169 sv_catsv(dstr, POPs);
172 if (cx->sb_once || !CALLREGEXEC(rx, s, cx->sb_strend, orig,
173 s == m, cx->sb_targ, NULL,
174 ((cx->sb_rflags & REXEC_COPY_STR)
175 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
176 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
178 SV *targ = cx->sb_targ;
179 sv_catpvn(dstr, s, cx->sb_strend - s);
181 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
183 (void)SvOOK_off(targ);
184 Safefree(SvPVX(targ));
185 SvPVX(targ) = SvPVX(dstr);
186 SvCUR_set(targ, SvCUR(dstr));
187 SvLEN_set(targ, SvLEN(dstr));
191 TAINT_IF(cx->sb_rxtainted & 1);
192 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
194 (void)SvPOK_only(targ);
195 TAINT_IF(cx->sb_rxtainted);
199 LEAVE_SCOPE(cx->sb_oldsave);
201 RETURNOP(pm->op_next);
204 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
207 cx->sb_orig = orig = rx->subbeg;
209 cx->sb_strend = s + (cx->sb_strend - m);
211 cx->sb_m = m = rx->startp[0] + orig;
212 sv_catpvn(dstr, s, m-s);
213 cx->sb_s = rx->endp[0] + orig;
214 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
215 rxres_save(&cx->sb_rxres, rx);
216 RETURNOP(pm->op_pmreplstart);
220 rxres_save(void **rsp, REGEXP *rx)
225 if (!p || p[1] < rx->nparens) {
226 i = 6 + rx->nparens * 2;
234 *p++ = (UV)(RX_MATCH_COPIED(rx) ? rx->subbeg : Nullch);
235 RX_MATCH_COPIED_off(rx);
239 *p++ = (UV)rx->subbeg;
240 *p++ = (UV)rx->sublen;
241 for (i = 0; i <= rx->nparens; ++i) {
242 *p++ = (UV)rx->startp[i];
243 *p++ = (UV)rx->endp[i];
248 rxres_restore(void **rsp, REGEXP *rx)
253 if (RX_MATCH_COPIED(rx))
254 Safefree(rx->subbeg);
255 RX_MATCH_COPIED_set(rx, *p);
260 rx->subbeg = (char*)(*p++);
261 rx->sublen = (I32)(*p++);
262 for (i = 0; i <= rx->nparens; ++i) {
263 rx->startp[i] = (I32)(*p++);
264 rx->endp[i] = (I32)(*p++);
269 rxres_free(void **rsp)
274 Safefree((char*)(*p));
282 djSP; dMARK; dORIGMARK;
283 register SV *tmpForm = *++MARK;
295 bool chopspace = (strchr(PL_chopset, ' ') != Nullch);
301 STRLEN fudge = SvCUR(tmpForm) * (IN_UTF8 ? 3 : 1) + 1;
303 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
304 SvREADONLY_off(tmpForm);
305 doparseform(tmpForm);
308 SvPV_force(PL_formtarget, len);
309 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
311 f = SvPV(tmpForm, len);
312 /* need to jump to the next word */
313 s = f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN;
322 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
323 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
324 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
325 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
326 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
328 case FF_CHECKNL: name = "CHECKNL"; break;
329 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
330 case FF_SPACE: name = "SPACE"; break;
331 case FF_HALFSPACE: name = "HALFSPACE"; break;
332 case FF_ITEM: name = "ITEM"; break;
333 case FF_CHOP: name = "CHOP"; break;
334 case FF_LINEGLOB: name = "LINEGLOB"; break;
335 case FF_NEWLINE: name = "NEWLINE"; break;
336 case FF_MORE: name = "MORE"; break;
337 case FF_LINEMARK: name = "LINEMARK"; break;
338 case FF_END: name = "END"; break;
341 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
343 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
371 if (ckWARN(WARN_SYNTAX))
372 warner(WARN_SYNTAX, "Not enough format arguments");
377 item = s = SvPV(sv, len);
380 itemsize = sv_len_utf8(sv);
381 if (itemsize != len) {
383 if (itemsize > fieldsize) {
384 itemsize = fieldsize;
385 itembytes = itemsize;
386 sv_pos_u2b(sv, &itembytes, 0);
390 send = chophere = s + itembytes;
399 sv_pos_b2u(sv, &itemsize);
403 if (itemsize > fieldsize)
404 itemsize = fieldsize;
405 send = chophere = s + itemsize;
417 item = s = SvPV(sv, len);
420 itemsize = sv_len_utf8(sv);
421 if (itemsize != len) {
423 if (itemsize <= fieldsize) {
424 send = chophere = s + itemsize;
435 itemsize = fieldsize;
436 itembytes = itemsize;
437 sv_pos_u2b(sv, &itembytes, 0);
438 send = chophere = s + itembytes;
439 while (s < send || (s == send && isSPACE(*s))) {
449 if (strchr(PL_chopset, *s))
454 itemsize = chophere - item;
455 sv_pos_b2u(sv, &itemsize);
460 if (itemsize <= fieldsize) {
461 send = chophere = s + itemsize;
472 itemsize = fieldsize;
473 send = chophere = s + itemsize;
474 while (s < send || (s == send && isSPACE(*s))) {
484 if (strchr(PL_chopset, *s))
489 itemsize = chophere - item;
494 arg = fieldsize - itemsize;
503 arg = fieldsize - itemsize;
518 switch (UTF8SKIP(s)) {
529 if ( !((*t++ = *s++) & ~31) )
537 int ch = *t++ = *s++;
540 if ( !((*t++ = *s++) & ~31) )
549 while (*s && isSPACE(*s))
556 item = s = SvPV(sv, len);
569 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
570 sv_catpvn(PL_formtarget, item, itemsize);
571 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
572 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
577 /* If the field is marked with ^ and the value is undefined,
580 if ((arg & 512) && !SvOK(sv)) {
588 /* Formats aren't yet marked for locales, so assume "yes". */
591 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
593 sprintf(t, "%*.0f", (int) fieldsize, value);
600 while (t-- > linemark && *t == ' ') ;
608 if (arg) { /* repeat until fields exhausted? */
610 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
611 lines += FmLINES(PL_formtarget);
614 if (strnEQ(linemark, linemark - arg, arg))
615 DIE("Runaway format");
617 FmLINES(PL_formtarget) = lines;
619 RETURNOP(cLISTOP->op_first);
632 while (*s && isSPACE(*s) && s < send)
636 arg = fieldsize - itemsize;
643 if (strnEQ(s," ",3)) {
644 while (s > SvPVX(PL_formtarget) && isSPACE(s[-1]))
655 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
656 FmLINES(PL_formtarget) += lines;
668 if (PL_stack_base + *PL_markstack_ptr == SP) {
670 if (GIMME_V == G_SCALAR)
671 XPUSHs(sv_2mortal(newSViv(0)));
672 RETURNOP(PL_op->op_next->op_next);
674 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
675 pp_pushmark(ARGS); /* push dst */
676 pp_pushmark(ARGS); /* push src */
677 ENTER; /* enter outer scope */
680 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
682 ENTER; /* enter inner scope */
685 src = PL_stack_base[*PL_markstack_ptr];
690 if (PL_op->op_type == OP_MAPSTART)
691 pp_pushmark(ARGS); /* push top */
692 return ((LOGOP*)PL_op->op_next)->op_other;
697 DIE("panic: mapstart"); /* uses grepstart */
703 I32 diff = (SP - PL_stack_base) - *PL_markstack_ptr;
709 ++PL_markstack_ptr[-1];
711 if (diff > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
712 shift = diff - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
713 count = (SP - PL_stack_base) - PL_markstack_ptr[-1] + 2;
718 PL_markstack_ptr[-1] += shift;
719 *PL_markstack_ptr += shift;
723 dst = PL_stack_base + (PL_markstack_ptr[-2] += diff) - 1;
726 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
728 LEAVE; /* exit inner scope */
731 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
735 (void)POPMARK; /* pop top */
736 LEAVE; /* exit outer scope */
737 (void)POPMARK; /* pop src */
738 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
739 (void)POPMARK; /* pop dst */
740 SP = PL_stack_base + POPMARK; /* pop original mark */
741 if (gimme == G_SCALAR) {
745 else if (gimme == G_ARRAY)
752 ENTER; /* enter inner scope */
755 src = PL_stack_base[PL_markstack_ptr[-1]];
759 RETURNOP(cLOGOP->op_other);
764 sv_ncmp (SV *a, SV *b)
766 double nv1 = SvNV(a);
767 double nv2 = SvNV(b);
768 return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0;
771 sv_i_ncmp (SV *a, SV *b)
775 return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0;
777 #define tryCALL_AMAGICbin(left,right,meth,svp) STMT_START { \
779 if (PL_amagic_generation) { \
780 if (SvAMAGIC(left)||SvAMAGIC(right))\
781 *svp = amagic_call(left, \
789 amagic_ncmp(register SV *a, register SV *b)
792 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
797 I32 i = SvIVX(tmpsv);
807 return sv_ncmp(a, b);
811 amagic_i_ncmp(register SV *a, register SV *b)
814 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
819 I32 i = SvIVX(tmpsv);
829 return sv_i_ncmp(a, b);
833 amagic_cmp(register SV *str1, register SV *str2)
836 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
841 I32 i = SvIVX(tmpsv);
851 return sv_cmp(str1, str2);
855 amagic_cmp_locale(register SV *str1, register SV *str2)
858 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
863 I32 i = SvIVX(tmpsv);
873 return sv_cmp_locale(str1, str2);
878 djSP; dMARK; dORIGMARK;
880 SV **myorigmark = ORIGMARK;
886 OP* nextop = PL_op->op_next;
889 if (gimme != G_ARRAY) {
895 SAVEPPTR(PL_sortcop);
896 if (PL_op->op_flags & OPf_STACKED) {
897 if (PL_op->op_flags & OPf_SPECIAL) {
898 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
899 kid = kUNOP->op_first; /* pass rv2gv */
900 kid = kUNOP->op_first; /* pass leave */
901 PL_sortcop = kid->op_next;
902 stash = PL_curcop->cop_stash;
905 cv = sv_2cv(*++MARK, &stash, &gv, 0);
906 if (!(cv && CvROOT(cv))) {
908 SV *tmpstr = sv_newmortal();
909 gv_efullname3(tmpstr, gv, Nullch);
910 if (cv && CvXSUB(cv))
911 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
912 DIE("Undefined sort subroutine \"%s\" called",
917 DIE("Xsub called in sort");
918 DIE("Undefined subroutine in sort");
920 DIE("Not a CODE reference in sort");
922 PL_sortcop = CvSTART(cv);
923 SAVESPTR(CvROOT(cv)->op_ppaddr);
924 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
927 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
932 stash = PL_curcop->cop_stash;
936 while (MARK < SP) { /* This may or may not shift down one here. */
938 if (*up = *++MARK) { /* Weed out nulls. */
940 if (!PL_sortcop && !SvPOK(*up)) {
945 (void)sv_2pv(*up, &n_a);
950 max = --up - myorigmark;
955 bool oldcatch = CATCH_GET;
961 PUSHSTACKi(PERLSI_SORT);
962 if (PL_sortstash != stash) {
963 PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
964 PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
965 PL_sortstash = stash;
968 SAVESPTR(GvSV(PL_firstgv));
969 SAVESPTR(GvSV(PL_secondgv));
971 PUSHBLOCK(cx, CXt_NULL, PL_stack_base);
972 if (!(PL_op->op_flags & OPf_SPECIAL)) {
973 bool hasargs = FALSE;
974 cx->cx_type = CXt_SUB;
975 cx->blk_gimme = G_SCALAR;
978 (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
980 PL_sortcxix = cxstack_ix;
981 qsortsv((myorigmark+1), max, FUNC_NAME_TO_PTR(sortcv));
983 POPBLOCK(cx,PL_curpm);
991 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
992 qsortsv(ORIGMARK+1, max,
993 (PL_op->op_private & OPpSORT_NUMERIC)
994 ? ( (PL_op->op_private & OPpSORT_INTEGER)
996 ? FUNC_NAME_TO_PTR(amagic_i_ncmp)
997 : FUNC_NAME_TO_PTR(sv_i_ncmp))
999 ? FUNC_NAME_TO_PTR(amagic_ncmp)
1000 : FUNC_NAME_TO_PTR(sv_ncmp)))
1001 : ( (PL_op->op_private & OPpLOCALE)
1003 ? FUNC_NAME_TO_PTR(amagic_cmp_locale)
1004 : FUNC_NAME_TO_PTR(sv_cmp_locale))
1006 ? FUNC_NAME_TO_PTR(amagic_cmp)
1007 : FUNC_NAME_TO_PTR(sv_cmp) )));
1008 if (PL_op->op_private & OPpSORT_REVERSE) {
1009 SV **p = ORIGMARK+1;
1010 SV **q = ORIGMARK+max;
1020 PL_stack_sp = ORIGMARK + max;
1028 if (GIMME == G_ARRAY)
1029 return cCONDOP->op_true;
1030 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1031 return cCONDOP->op_false;
1033 return cCONDOP->op_true;
1040 if (GIMME == G_ARRAY) {
1041 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
1045 SV *targ = PAD_SV(PL_op->op_targ);
1047 if ((PL_op->op_private & OPpFLIP_LINENUM)
1048 ? (PL_last_in_gv && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
1050 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1051 if (PL_op->op_flags & OPf_SPECIAL) {
1059 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
1072 if (GIMME == G_ARRAY) {
1078 if (SvGMAGICAL(left))
1080 if (SvGMAGICAL(right))
1083 if (SvNIOKp(left) || !SvPOKp(left) ||
1084 (looks_like_number(left) && *SvPVX(left) != '0') )
1086 if (SvNV(left) < IV_MIN || SvNV(right) > IV_MAX)
1087 croak("Range iterator outside integer range");
1098 sv = sv_2mortal(newSViv(i++));
1103 SV *final = sv_mortalcopy(right);
1105 char *tmps = SvPV(final, len);
1107 sv = sv_mortalcopy(left);
1109 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1111 if (strEQ(SvPVX(sv),tmps))
1113 sv = sv_2mortal(newSVsv(sv));
1120 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
1122 if ((PL_op->op_private & OPpFLIP_LINENUM)
1123 ? (PL_last_in_gv && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
1125 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1126 sv_catpv(targ, "E0");
1137 dopoptolabel(char *label)
1141 register PERL_CONTEXT *cx;
1143 for (i = cxstack_ix; i >= 0; i--) {
1145 switch (CxTYPE(cx)) {
1147 if (ckWARN(WARN_UNSAFE))
1148 warner(WARN_UNSAFE, "Exiting substitution via %s",
1149 PL_op_name[PL_op->op_type]);
1152 if (ckWARN(WARN_UNSAFE))
1153 warner(WARN_UNSAFE, "Exiting subroutine via %s",
1154 PL_op_name[PL_op->op_type]);
1157 if (ckWARN(WARN_UNSAFE))
1158 warner(WARN_UNSAFE, "Exiting eval via %s",
1159 PL_op_name[PL_op->op_type]);
1162 if (ckWARN(WARN_UNSAFE))
1163 warner(WARN_UNSAFE, "Exiting pseudo-block via %s",
1164 PL_op_name[PL_op->op_type]);
1167 if (!cx->blk_loop.label ||
1168 strNE(label, cx->blk_loop.label) ) {
1169 DEBUG_l(deb("(Skipping label #%ld %s)\n",
1170 (long)i, cx->blk_loop.label));
1173 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
1183 I32 gimme = block_gimme();
1184 return (gimme == G_VOID) ? G_SCALAR : gimme;
1193 cxix = dopoptosub(cxstack_ix);
1197 switch (cxstack[cxix].blk_gimme) {
1205 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1212 dopoptosub(I32 startingblock)
1215 return dopoptosub_at(cxstack, startingblock);
1219 dopoptosub_at(PERL_CONTEXT *cxstk, I32 startingblock)
1223 register PERL_CONTEXT *cx;
1224 for (i = startingblock; i >= 0; i--) {
1226 switch (CxTYPE(cx)) {
1231 DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
1239 dopoptoeval(I32 startingblock)
1243 register PERL_CONTEXT *cx;
1244 for (i = startingblock; i >= 0; i--) {
1246 switch (CxTYPE(cx)) {
1250 DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
1258 dopoptoloop(I32 startingblock)
1262 register PERL_CONTEXT *cx;
1263 for (i = startingblock; i >= 0; i--) {
1265 switch (CxTYPE(cx)) {
1267 if (ckWARN(WARN_UNSAFE))
1268 warner(WARN_UNSAFE, "Exiting substitution via %s",
1269 PL_op_name[PL_op->op_type]);
1272 if (ckWARN(WARN_UNSAFE))
1273 warner(WARN_UNSAFE, "Exiting subroutine via %s",
1274 PL_op_name[PL_op->op_type]);
1277 if (ckWARN(WARN_UNSAFE))
1278 warner(WARN_UNSAFE, "Exiting eval via %s",
1279 PL_op_name[PL_op->op_type]);
1282 if (ckWARN(WARN_UNSAFE))
1283 warner(WARN_UNSAFE, "Exiting pseudo-block via %s",
1284 PL_op_name[PL_op->op_type]);
1287 DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
1298 register PERL_CONTEXT *cx;
1302 while (cxstack_ix > cxix) {
1303 cx = &cxstack[cxstack_ix];
1304 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1305 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
1306 /* Note: we don't need to restore the base context info till the end. */
1307 switch (CxTYPE(cx)) {
1310 continue; /* not break */
1328 die_where(char *message, STRLEN msglen)
1334 register PERL_CONTEXT *cx;
1339 if (PL_in_eval & EVAL_KEEPERR) {
1342 svp = hv_fetch(ERRHV, message, msglen, TRUE);
1345 static char prefix[] = "\t(in cleanup) ";
1347 sv_upgrade(*svp, SVt_IV);
1348 (void)SvIOK_only(*svp);
1351 SvGROW(err, SvCUR(err)+sizeof(prefix)+msglen);
1352 sv_catpvn(err, prefix, sizeof(prefix)-1);
1353 sv_catpvn(err, message, msglen);
1354 if (ckWARN(WARN_UNSAFE)) {
1355 STRLEN start = SvCUR(err)-msglen-sizeof(prefix)+1;
1356 warner(WARN_UNSAFE, SvPVX(err)+start);
1363 sv_setpvn(ERRSV, message, msglen);
1366 message = SvPVx(ERRSV, msglen);
1368 while ((cxix = dopoptoeval(cxstack_ix)) < 0 && PL_curstackinfo->si_prev) {
1376 if (cxix < cxstack_ix)
1379 POPBLOCK(cx,PL_curpm);
1380 if (CxTYPE(cx) != CXt_EVAL) {
1381 PerlIO_write(PerlIO_stderr(), "panic: die ", 11);
1382 PerlIO_write(PerlIO_stderr(), message, msglen);
1387 if (gimme == G_SCALAR)
1388 *++newsp = &PL_sv_undef;
1389 PL_stack_sp = newsp;
1393 if (optype == OP_REQUIRE) {
1394 char* msg = SvPVx(ERRSV, n_a);
1395 DIE("%s", *msg ? msg : "Compilation failed in require");
1397 return pop_return();
1401 message = SvPVx(ERRSV, msglen);
1404 /* SFIO can really mess with your errno */
1407 PerlIO_write(PerlIO_stderr(), message, msglen);
1408 (void)PerlIO_flush(PerlIO_stderr());
1421 if (SvTRUE(left) != SvTRUE(right))
1433 RETURNOP(cLOGOP->op_other);
1442 RETURNOP(cLOGOP->op_other);
1448 register I32 cxix = dopoptosub(cxstack_ix);
1449 register PERL_CONTEXT *cx;
1450 register PERL_CONTEXT *ccstack = cxstack;
1451 PERL_SI *top_si = PL_curstackinfo;
1462 /* we may be in a higher stacklevel, so dig down deeper */
1463 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1464 top_si = top_si->si_prev;
1465 ccstack = top_si->si_cxstack;
1466 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1469 if (GIMME != G_ARRAY)
1473 if (PL_DBsub && cxix >= 0 &&
1474 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1478 cxix = dopoptosub_at(ccstack, cxix - 1);
1481 cx = &ccstack[cxix];
1482 if (CxTYPE(cx) == CXt_SUB) {
1483 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1484 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1485 field below is defined for any cx. */
1486 if (PL_DBsub && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1487 cx = &ccstack[dbcxix];
1490 if (GIMME != G_ARRAY) {
1491 hv = cx->blk_oldcop->cop_stash;
1493 PUSHs(&PL_sv_undef);
1496 sv_setpv(TARG, HvNAME(hv));
1502 hv = cx->blk_oldcop->cop_stash;
1504 PUSHs(&PL_sv_undef);
1506 PUSHs(sv_2mortal(newSVpv(HvNAME(hv), 0)));
1507 PUSHs(sv_2mortal(newSVpvn(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)),
1508 SvCUR(GvSV(cx->blk_oldcop->cop_filegv)))));
1509 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1512 if (CxTYPE(cx) == CXt_SUB) { /* So is ccstack[dbcxix]. */
1514 gv_efullname3(sv, CvGV(ccstack[cxix].blk_sub.cv), Nullch);
1515 PUSHs(sv_2mortal(sv));
1516 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1519 PUSHs(sv_2mortal(newSVpvn("(eval)",6)));
1520 PUSHs(sv_2mortal(newSViv(0)));
1522 gimme = (I32)cx->blk_gimme;
1523 if (gimme == G_VOID)
1524 PUSHs(&PL_sv_undef);
1526 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1527 if (CxTYPE(cx) == CXt_EVAL) {
1528 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1529 PUSHs(cx->blk_eval.cur_text);
1532 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1533 /* Require, put the name. */
1534 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1538 else if (CxTYPE(cx) == CXt_SUB &&
1539 cx->blk_sub.hasargs &&
1540 PL_curcop->cop_stash == PL_debstash)
1542 AV *ary = cx->blk_sub.argarray;
1543 int off = AvARRAY(ary) - AvALLOC(ary);
1547 PL_dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1550 AvREAL_off(PL_dbargs); /* XXX Should be REIFY */
1553 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1554 av_extend(PL_dbargs, AvFILLp(ary) + off);
1555 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1556 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1562 sortcv(SV *a, SV *b)
1565 I32 oldsaveix = PL_savestack_ix;
1566 I32 oldscopeix = PL_scopestack_ix;
1568 GvSV(PL_firstgv) = a;
1569 GvSV(PL_secondgv) = b;
1570 PL_stack_sp = PL_stack_base;
1573 if (PL_stack_sp != PL_stack_base + 1)
1574 croak("Sort subroutine didn't return single value");
1575 if (!SvNIOKp(*PL_stack_sp))
1576 croak("Sort subroutine didn't return a numeric value");
1577 result = SvIV(*PL_stack_sp);
1578 while (PL_scopestack_ix > oldscopeix) {
1581 leave_scope(oldsaveix);
1595 sv_reset(tmps, PL_curcop->cop_stash);
1607 PL_curcop = (COP*)PL_op;
1608 TAINT_NOT; /* Each statement is presumed innocent */
1609 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1612 if (PL_op->op_private || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1616 register PERL_CONTEXT *cx;
1617 I32 gimme = G_ARRAY;
1624 DIE("No DB::DB routine defined");
1626 if (CvDEPTH(cv) >= 1 && !(PL_debug & (1<<30))) /* don't do recursive DB::DB call */
1638 push_return(PL_op->op_next);
1639 PUSHBLOCK(cx, CXt_SUB, SP);
1642 (void)SvREFCNT_inc(cv);
1643 SAVESPTR(PL_curpad);
1644 PL_curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1645 RETURNOP(CvSTART(cv));
1659 register PERL_CONTEXT *cx;
1660 I32 gimme = GIMME_V;
1667 if (PL_op->op_flags & OPf_SPECIAL) {
1669 svp = &THREADSV(PL_op->op_targ); /* per-thread variable */
1670 SAVEGENERICSV(*svp);
1674 #endif /* USE_THREADS */
1675 if (PL_op->op_targ) {
1676 svp = &PL_curpad[PL_op->op_targ]; /* "my" variable */
1680 svp = &GvSV((GV*)POPs); /* symbol table variable */
1681 SAVEGENERICSV(*svp);
1687 PUSHBLOCK(cx, CXt_LOOP, SP);
1688 PUSHLOOP(cx, svp, MARK);
1689 if (PL_op->op_flags & OPf_STACKED) {
1690 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1691 if (SvTYPE(cx->blk_loop.iterary) != SVt_PVAV) {
1693 if (SvNIOKp(sv) || !SvPOKp(sv) ||
1694 (looks_like_number(sv) && *SvPVX(sv) != '0')) {
1695 if (SvNV(sv) < IV_MIN ||
1696 SvNV((SV*)cx->blk_loop.iterary) >= IV_MAX)
1697 croak("Range iterator outside integer range");
1698 cx->blk_loop.iterix = SvIV(sv);
1699 cx->blk_loop.itermax = SvIV((SV*)cx->blk_loop.iterary);
1702 cx->blk_loop.iterlval = newSVsv(sv);
1706 cx->blk_loop.iterary = PL_curstack;
1707 AvFILLp(PL_curstack) = SP - PL_stack_base;
1708 cx->blk_loop.iterix = MARK - PL_stack_base;
1717 register PERL_CONTEXT *cx;
1718 I32 gimme = GIMME_V;
1724 PUSHBLOCK(cx, CXt_LOOP, SP);
1725 PUSHLOOP(cx, 0, SP);
1733 register PERL_CONTEXT *cx;
1734 struct block_loop cxloop;
1742 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1745 if (gimme == G_VOID)
1747 else if (gimme == G_SCALAR) {
1749 *++newsp = sv_mortalcopy(*SP);
1751 *++newsp = &PL_sv_undef;
1755 *++newsp = sv_mortalcopy(*++mark);
1756 TAINT_NOT; /* Each item is independent */
1762 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1763 PL_curpm = newpm; /* ... and pop $1 et al */
1775 register PERL_CONTEXT *cx;
1776 struct block_sub cxsub;
1777 bool popsub2 = FALSE;
1783 if (PL_curstackinfo->si_type == PERLSI_SORT) {
1784 if (cxstack_ix == PL_sortcxix || dopoptosub(cxstack_ix) <= PL_sortcxix) {
1785 if (cxstack_ix > PL_sortcxix)
1786 dounwind(PL_sortcxix);
1787 AvARRAY(PL_curstack)[1] = *SP;
1788 PL_stack_sp = PL_stack_base + 1;
1793 cxix = dopoptosub(cxstack_ix);
1795 DIE("Can't return outside a subroutine");
1796 if (cxix < cxstack_ix)
1800 switch (CxTYPE(cx)) {
1802 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1807 if (optype == OP_REQUIRE &&
1808 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1810 /* Unassume the success we assumed earlier. */
1811 char *name = cx->blk_eval.old_name;
1812 (void)hv_delete(GvHVn(PL_incgv), name, strlen(name), G_DISCARD);
1813 DIE("%s did not return a true value", name);
1817 DIE("panic: return");
1821 if (gimme == G_SCALAR) {
1824 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1826 *++newsp = SvREFCNT_inc(*SP);
1831 *++newsp = sv_mortalcopy(*SP);
1834 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
1836 *++newsp = sv_mortalcopy(*SP);
1838 *++newsp = &PL_sv_undef;
1840 else if (gimme == G_ARRAY) {
1841 while (++MARK <= SP) {
1842 *++newsp = (popsub2 && SvTEMP(*MARK))
1843 ? *MARK : sv_mortalcopy(*MARK);
1844 TAINT_NOT; /* Each item is independent */
1847 PL_stack_sp = newsp;
1849 /* Stack values are safe: */
1851 POPSUB2(); /* release CV and @_ ... */
1853 PL_curpm = newpm; /* ... and pop $1 et al */
1856 return pop_return();
1863 register PERL_CONTEXT *cx;
1864 struct block_loop cxloop;
1865 struct block_sub cxsub;
1872 SV **mark = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1874 if (PL_op->op_flags & OPf_SPECIAL) {
1875 cxix = dopoptoloop(cxstack_ix);
1877 DIE("Can't \"last\" outside a block");
1880 cxix = dopoptolabel(cPVOP->op_pv);
1882 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1884 if (cxix < cxstack_ix)
1888 switch (CxTYPE(cx)) {
1890 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1892 nextop = cxloop.last_op->op_next;
1895 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1897 nextop = pop_return();
1901 nextop = pop_return();
1908 if (gimme == G_SCALAR) {
1910 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1911 ? *SP : sv_mortalcopy(*SP);
1913 *++newsp = &PL_sv_undef;
1915 else if (gimme == G_ARRAY) {
1916 while (++MARK <= SP) {
1917 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1918 ? *MARK : sv_mortalcopy(*MARK);
1919 TAINT_NOT; /* Each item is independent */
1925 /* Stack values are safe: */
1928 POPLOOP2(); /* release loop vars ... */
1932 POPSUB2(); /* release CV and @_ ... */
1935 PL_curpm = newpm; /* ... and pop $1 et al */
1944 register PERL_CONTEXT *cx;
1947 if (PL_op->op_flags & OPf_SPECIAL) {
1948 cxix = dopoptoloop(cxstack_ix);
1950 DIE("Can't \"next\" outside a block");
1953 cxix = dopoptolabel(cPVOP->op_pv);
1955 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1957 if (cxix < cxstack_ix)
1961 oldsave = PL_scopestack[PL_scopestack_ix - 1];
1962 LEAVE_SCOPE(oldsave);
1963 return cx->blk_loop.next_op;
1969 register PERL_CONTEXT *cx;
1972 if (PL_op->op_flags & OPf_SPECIAL) {
1973 cxix = dopoptoloop(cxstack_ix);
1975 DIE("Can't \"redo\" outside a block");
1978 cxix = dopoptolabel(cPVOP->op_pv);
1980 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1982 if (cxix < cxstack_ix)
1986 oldsave = PL_scopestack[PL_scopestack_ix - 1];
1987 LEAVE_SCOPE(oldsave);
1988 return cx->blk_loop.redo_op;
1992 dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
1996 static char too_deep[] = "Target of goto is too deeply nested";
2000 if (o->op_type == OP_LEAVE ||
2001 o->op_type == OP_SCOPE ||
2002 o->op_type == OP_LEAVELOOP ||
2003 o->op_type == OP_LEAVETRY)
2005 *ops++ = cUNOPo->op_first;
2010 if (o->op_flags & OPf_KIDS) {
2012 /* First try all the kids at this level, since that's likeliest. */
2013 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2014 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2015 kCOP->cop_label && strEQ(kCOP->cop_label, label))
2018 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2019 if (kid == PL_lastgotoprobe)
2021 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2023 (ops[-1]->op_type != OP_NEXTSTATE &&
2024 ops[-1]->op_type != OP_DBSTATE)))
2026 if (o = dofindlabel(kid, label, ops, oplimit))
2036 return pp_goto(ARGS);
2045 register PERL_CONTEXT *cx;
2046 #define GOTO_DEPTH 64
2047 OP *enterops[GOTO_DEPTH];
2049 int do_dump = (PL_op->op_type == OP_DUMP);
2050 static char must_have_label[] = "goto must have label";
2053 if (PL_op->op_flags & OPf_STACKED) {
2057 /* This egregious kludge implements goto &subroutine */
2058 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2060 register PERL_CONTEXT *cx;
2061 CV* cv = (CV*)SvRV(sv);
2065 int arg_was_real = 0;
2068 if (!CvROOT(cv) && !CvXSUB(cv)) {
2073 /* autoloaded stub? */
2074 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2076 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2077 GvNAMELEN(gv), FALSE);
2078 if (autogv && (cv = GvCV(autogv)))
2080 tmpstr = sv_newmortal();
2081 gv_efullname3(tmpstr, gv, Nullch);
2082 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
2084 DIE("Goto undefined subroutine");
2087 /* First do some returnish stuff. */
2088 cxix = dopoptosub(cxstack_ix);
2090 DIE("Can't goto subroutine outside a subroutine");
2091 if (cxix < cxstack_ix)
2094 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
2095 DIE("Can't goto subroutine from an eval-string");
2097 if (CxTYPE(cx) == CXt_SUB &&
2098 cx->blk_sub.hasargs) { /* put @_ back onto stack */
2099 AV* av = cx->blk_sub.argarray;
2101 items = AvFILLp(av) + 1;
2103 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2104 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2105 PL_stack_sp += items;
2107 SvREFCNT_dec(GvAV(PL_defgv));
2108 GvAV(PL_defgv) = cx->blk_sub.savearray;
2109 #endif /* USE_THREADS */
2112 AvREAL_off(av); /* so av_clear() won't clobber elts */
2116 else if (CvXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2120 av = (AV*)PL_curpad[0];
2122 av = GvAV(PL_defgv);
2124 items = AvFILLp(av) + 1;
2126 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2127 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2128 PL_stack_sp += items;
2130 if (CxTYPE(cx) == CXt_SUB &&
2131 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2132 SvREFCNT_dec(cx->blk_sub.cv);
2133 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2134 LEAVE_SCOPE(oldsave);
2136 /* Now do some callish stuff. */
2139 #ifdef PERL_XSUB_OLDSTYLE
2140 if (CvOLDSTYLE(cv)) {
2141 I32 (*fp3)_((int,int,int));
2146 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2147 items = (*fp3)(CvXSUBANY(cv).any_i32,
2148 mark - PL_stack_base + 1,
2150 SP = PL_stack_base + items;
2153 #endif /* PERL_XSUB_OLDSTYLE */
2158 PL_stack_sp--; /* There is no cv arg. */
2159 /* Push a mark for the start of arglist */
2161 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
2162 /* Pop the current context like a decent sub should */
2163 POPBLOCK(cx, PL_curpm);
2164 /* Do _not_ use PUTBACK, keep the XSUB's return stack! */
2167 return pop_return();
2170 AV* padlist = CvPADLIST(cv);
2171 SV** svp = AvARRAY(padlist);
2172 if (CxTYPE(cx) == CXt_EVAL) {
2173 PL_in_eval = cx->blk_eval.old_in_eval;
2174 PL_eval_root = cx->blk_eval.old_eval_root;
2175 cx->cx_type = CXt_SUB;
2176 cx->blk_sub.hasargs = 0;
2178 cx->blk_sub.cv = cv;
2179 cx->blk_sub.olddepth = CvDEPTH(cv);
2181 if (CvDEPTH(cv) < 2)
2182 (void)SvREFCNT_inc(cv);
2183 else { /* save temporaries on recursion? */
2184 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION))
2185 sub_crush_depth(cv);
2186 if (CvDEPTH(cv) > AvFILLp(padlist)) {
2187 AV *newpad = newAV();
2188 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
2189 I32 ix = AvFILLp((AV*)svp[1]);
2190 svp = AvARRAY(svp[0]);
2191 for ( ;ix > 0; ix--) {
2192 if (svp[ix] != &PL_sv_undef) {
2193 char *name = SvPVX(svp[ix]);
2194 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
2197 /* outer lexical or anon code */
2198 av_store(newpad, ix,
2199 SvREFCNT_inc(oldpad[ix]) );
2201 else { /* our own lexical */
2203 av_store(newpad, ix, sv = (SV*)newAV());
2204 else if (*name == '%')
2205 av_store(newpad, ix, sv = (SV*)newHV());
2207 av_store(newpad, ix, sv = NEWSV(0,0));
2212 av_store(newpad, ix, sv = NEWSV(0,0));
2216 if (cx->blk_sub.hasargs) {
2219 av_store(newpad, 0, (SV*)av);
2220 AvFLAGS(av) = AVf_REIFY;
2222 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
2223 AvFILLp(padlist) = CvDEPTH(cv);
2224 svp = AvARRAY(padlist);
2228 if (!cx->blk_sub.hasargs) {
2229 AV* av = (AV*)PL_curpad[0];
2231 items = AvFILLp(av) + 1;
2233 /* Mark is at the end of the stack. */
2235 Copy(AvARRAY(av), SP + 1, items, SV*);
2240 #endif /* USE_THREADS */
2241 SAVESPTR(PL_curpad);
2242 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2244 if (cx->blk_sub.hasargs)
2245 #endif /* USE_THREADS */
2247 AV* av = (AV*)PL_curpad[0];
2251 cx->blk_sub.savearray = GvAV(PL_defgv);
2252 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2253 #endif /* USE_THREADS */
2254 cx->blk_sub.argarray = av;
2257 if (items >= AvMAX(av) + 1) {
2259 if (AvARRAY(av) != ary) {
2260 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2261 SvPVX(av) = (char*)ary;
2263 if (items >= AvMAX(av) + 1) {
2264 AvMAX(av) = items - 1;
2265 Renew(ary,items+1,SV*);
2267 SvPVX(av) = (char*)ary;
2270 Copy(mark,AvARRAY(av),items,SV*);
2271 AvFILLp(av) = items - 1;
2272 /* preserve @_ nature */
2283 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2285 * We do not care about using sv to call CV;
2286 * it's for informational purposes only.
2288 SV *sv = GvSV(PL_DBsub);
2291 if (PERLDB_SUB_NN) {
2292 SvIVX(sv) = (IV)cv; /* Already upgraded, saved */
2295 gv_efullname3(sv, CvGV(cv), Nullch);
2298 && (gotocv = perl_get_cv("DB::goto", FALSE)) ) {
2299 PUSHMARK( PL_stack_sp );
2300 perl_call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
2304 RETURNOP(CvSTART(cv));
2308 label = SvPV(sv,n_a);
2309 if (!(do_dump || *label))
2310 DIE(must_have_label);
2313 else if (PL_op->op_flags & OPf_SPECIAL) {
2315 DIE(must_have_label);
2318 label = cPVOP->op_pv;
2320 if (label && *label) {
2325 PL_lastgotoprobe = 0;
2327 for (ix = cxstack_ix; ix >= 0; ix--) {
2329 switch (CxTYPE(cx)) {
2331 gotoprobe = PL_eval_root; /* XXX not good for nested eval */
2334 gotoprobe = cx->blk_oldcop->op_sibling;
2340 gotoprobe = cx->blk_oldcop->op_sibling;
2342 gotoprobe = PL_main_root;
2345 if (CvDEPTH(cx->blk_sub.cv)) {
2346 gotoprobe = CvROOT(cx->blk_sub.cv);
2351 DIE("Can't \"goto\" outside a block");
2355 gotoprobe = PL_main_root;
2358 retop = dofindlabel(gotoprobe, label,
2359 enterops, enterops + GOTO_DEPTH);
2362 PL_lastgotoprobe = gotoprobe;
2365 DIE("Can't find label %s", label);
2367 /* pop unwanted frames */
2369 if (ix < cxstack_ix) {
2376 oldsave = PL_scopestack[PL_scopestack_ix];
2377 LEAVE_SCOPE(oldsave);
2380 /* push wanted frames */
2382 if (*enterops && enterops[1]) {
2384 for (ix = 1; enterops[ix]; ix++) {
2385 PL_op = enterops[ix];
2386 /* Eventually we may want to stack the needed arguments
2387 * for each op. For now, we punt on the hard ones. */
2388 if (PL_op->op_type == OP_ENTERITER)
2389 DIE("Can't \"goto\" into the middle of a foreach loop",
2391 (CALLOP->op_ppaddr)(ARGS);
2399 if (!retop) retop = PL_main_start;
2401 PL_restartop = retop;
2402 PL_do_undump = TRUE;
2406 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2407 PL_do_undump = FALSE;
2423 if (anum == 1 && VMSISH_EXIT)
2428 PUSHs(&PL_sv_undef);
2436 double value = SvNVx(GvSV(cCOP->cop_gv));
2437 register I32 match = I_32(value);
2440 if (((double)match) > value)
2441 --match; /* was fractional--truncate other way */
2443 match -= cCOP->uop.scop.scop_offset;
2446 else if (match > cCOP->uop.scop.scop_max)
2447 match = cCOP->uop.scop.scop_max;
2448 PL_op = cCOP->uop.scop.scop_next[match];
2458 PL_op = PL_op->op_next; /* can't assume anything */
2461 match = *(SvPVx(GvSV(cCOP->cop_gv), n_a)) & 255;
2462 match -= cCOP->uop.scop.scop_offset;
2465 else if (match > cCOP->uop.scop.scop_max)
2466 match = cCOP->uop.scop.scop_max;
2467 PL_op = cCOP->uop.scop.scop_next[match];
2476 save_lines(AV *array, SV *sv)
2478 register char *s = SvPVX(sv);
2479 register char *send = SvPVX(sv) + SvCUR(sv);
2481 register I32 line = 1;
2483 while (s && s < send) {
2484 SV *tmpstr = NEWSV(85,0);
2486 sv_upgrade(tmpstr, SVt_PVMG);
2487 t = strchr(s, '\n');
2493 sv_setpvn(tmpstr, s, t - s);
2494 av_store(array, line++, tmpstr);
2500 docatch_body(va_list args)
2514 assert(CATCH_GET == TRUE);
2518 CALLPROTECT(&ret, FUNC_NAME_TO_PTR(docatch_body));
2524 PL_op = PL_restartop;
2539 sv_compile_2op(SV *sv, OP** startop, char *code, AV** avp)
2540 /* sv Text to convert to OP tree. */
2541 /* startop op_free() this to undo. */
2542 /* code Short string id of the caller. */
2544 dSP; /* Make POPBLOCK work. */
2547 I32 gimme = 0; /* SUSPECT - INITIALZE TO WHAT? NI-S */
2550 OP *oop = PL_op, *rop;
2551 char tmpbuf[TYPE_DIGITS(long) + 12 + 10];
2557 /* switch to eval mode */
2559 if (PL_curcop == &PL_compiling) {
2560 SAVESPTR(PL_compiling.cop_stash);
2561 PL_compiling.cop_stash = PL_curstash;
2563 SAVESPTR(PL_compiling.cop_filegv);
2564 SAVEI16(PL_compiling.cop_line);
2565 sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++PL_evalseq);
2566 PL_compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2567 PL_compiling.cop_line = 1;
2568 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2569 deleting the eval's FILEGV from the stash before gv_check() runs
2570 (i.e. before run-time proper). To work around the coredump that
2571 ensues, we always turn GvMULTI_on for any globals that were
2572 introduced within evals. See force_ident(). GSAR 96-10-12 */
2573 safestr = savepv(tmpbuf);
2574 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
2576 #ifdef OP_IN_REGISTER
2584 PL_op->op_type = OP_ENTEREVAL;
2585 PL_op->op_flags = 0; /* Avoid uninit warning. */
2586 PUSHBLOCK(cx, CXt_EVAL, SP);
2587 PUSHEVAL(cx, 0, PL_compiling.cop_filegv);
2588 rop = doeval(G_SCALAR, startop);
2589 POPBLOCK(cx,PL_curpm);
2592 (*startop)->op_type = OP_NULL;
2593 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
2595 *avp = (AV*)SvREFCNT_inc(PL_comppad);
2597 if (PL_curcop == &PL_compiling)
2598 PL_compiling.op_private = PL_hints;
2599 #ifdef OP_IN_REGISTER
2605 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2607 doeval(int gimme, OP** startop)
2616 PL_in_eval = EVAL_INEVAL;
2620 /* set up a scratch pad */
2623 SAVESPTR(PL_curpad);
2624 SAVESPTR(PL_comppad);
2625 SAVESPTR(PL_comppad_name);
2626 SAVEI32(PL_comppad_name_fill);
2627 SAVEI32(PL_min_intro_pending);
2628 SAVEI32(PL_max_intro_pending);
2631 for (i = cxstack_ix - 1; i >= 0; i--) {
2632 PERL_CONTEXT *cx = &cxstack[i];
2633 if (CxTYPE(cx) == CXt_EVAL)
2635 else if (CxTYPE(cx) == CXt_SUB) {
2636 caller = cx->blk_sub.cv;
2641 SAVESPTR(PL_compcv);
2642 PL_compcv = (CV*)NEWSV(1104,0);
2643 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
2644 CvEVAL_on(PL_compcv);
2646 CvOWNER(PL_compcv) = 0;
2647 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
2648 MUTEX_INIT(CvMUTEXP(PL_compcv));
2649 #endif /* USE_THREADS */
2651 PL_comppad = newAV();
2652 av_push(PL_comppad, Nullsv);
2653 PL_curpad = AvARRAY(PL_comppad);
2654 PL_comppad_name = newAV();
2655 PL_comppad_name_fill = 0;
2656 PL_min_intro_pending = 0;
2659 av_store(PL_comppad_name, 0, newSVpvn("@_", 2));
2660 PL_curpad[0] = (SV*)newAV();
2661 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
2662 #endif /* USE_THREADS */
2664 comppadlist = newAV();
2665 AvREAL_off(comppadlist);
2666 av_store(comppadlist, 0, (SV*)PL_comppad_name);
2667 av_store(comppadlist, 1, (SV*)PL_comppad);
2668 CvPADLIST(PL_compcv) = comppadlist;
2670 if (!saveop || saveop->op_type != OP_REQUIRE)
2671 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc(caller);
2673 SAVEFREESV(PL_compcv);
2675 /* make sure we compile in the right package */
2677 newstash = PL_curcop->cop_stash;
2678 if (PL_curstash != newstash) {
2679 SAVESPTR(PL_curstash);
2680 PL_curstash = newstash;
2682 SAVESPTR(PL_beginav);
2683 PL_beginav = newAV();
2684 SAVEFREESV(PL_beginav);
2686 /* try to compile it */
2688 PL_eval_root = Nullop;
2690 PL_curcop = &PL_compiling;
2691 PL_curcop->cop_arybase = 0;
2692 SvREFCNT_dec(PL_rs);
2693 PL_rs = newSVpvn("\n", 1);
2694 if (saveop && saveop->op_flags & OPf_SPECIAL)
2695 PL_in_eval |= EVAL_KEEPERR;
2698 if (yyparse() || PL_error_count || !PL_eval_root) {
2702 I32 optype = 0; /* Might be reset by POPEVAL. */
2707 op_free(PL_eval_root);
2708 PL_eval_root = Nullop;
2710 SP = PL_stack_base + POPMARK; /* pop original mark */
2712 POPBLOCK(cx,PL_curpm);
2718 if (optype == OP_REQUIRE) {
2719 char* msg = SvPVx(ERRSV, n_a);
2720 DIE("%s", *msg ? msg : "Compilation failed in require");
2721 } else if (startop) {
2722 char* msg = SvPVx(ERRSV, n_a);
2724 POPBLOCK(cx,PL_curpm);
2726 croak("%sCompilation failed in regexp", (*msg ? msg : "Unknown error\n"));
2728 SvREFCNT_dec(PL_rs);
2729 PL_rs = SvREFCNT_inc(PL_nrs);
2731 MUTEX_LOCK(&PL_eval_mutex);
2733 COND_SIGNAL(&PL_eval_cond);
2734 MUTEX_UNLOCK(&PL_eval_mutex);
2735 #endif /* USE_THREADS */
2738 SvREFCNT_dec(PL_rs);
2739 PL_rs = SvREFCNT_inc(PL_nrs);
2740 PL_compiling.cop_line = 0;
2742 *startop = PL_eval_root;
2743 SvREFCNT_dec(CvOUTSIDE(PL_compcv));
2744 CvOUTSIDE(PL_compcv) = Nullcv;
2746 SAVEFREEOP(PL_eval_root);
2748 scalarvoid(PL_eval_root);
2749 else if (gimme & G_ARRAY)
2752 scalar(PL_eval_root);
2754 DEBUG_x(dump_eval());
2756 /* Register with debugger: */
2757 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2758 CV *cv = perl_get_cv("DB::postponed", FALSE);
2762 XPUSHs((SV*)PL_compiling.cop_filegv);
2764 perl_call_sv((SV*)cv, G_DISCARD);
2768 /* compiled okay, so do it */
2770 CvDEPTH(PL_compcv) = 1;
2771 SP = PL_stack_base + POPMARK; /* pop original mark */
2772 PL_op = saveop; /* The caller may need it. */
2774 MUTEX_LOCK(&PL_eval_mutex);
2776 COND_SIGNAL(&PL_eval_cond);
2777 MUTEX_UNLOCK(&PL_eval_mutex);
2778 #endif /* USE_THREADS */
2780 RETURNOP(PL_eval_start);
2784 doopen_pmc(const char *name, const char *mode)
2786 STRLEN namelen = strlen(name);
2789 if (namelen > 3 && strcmp(name + namelen - 3, ".pm") == 0) {
2790 SV *pmcsv = newSVpvf("%s%c", name, 'c');
2791 char *pmc = SvPV_nolen(pmcsv);
2794 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
2795 fp = PerlIO_open(name, mode);
2798 if (PerlLIO_stat(name, &pmstat) < 0 ||
2799 pmstat.st_mtime < pmcstat.st_mtime)
2801 fp = PerlIO_open(pmc, mode);
2804 fp = PerlIO_open(name, mode);
2807 SvREFCNT_dec(pmcsv);
2810 fp = PerlIO_open(name, mode);
2818 register PERL_CONTEXT *cx;
2823 SV *namesv = Nullsv;
2825 I32 gimme = G_SCALAR;
2826 PerlIO *tryrsfp = 0;
2830 if (SvNIOKp(sv) && !SvPOKp(sv)) {
2831 SET_NUMERIC_STANDARD();
2832 if (atof(PL_patchlevel) + 0.00000999 < SvNV(sv))
2833 DIE("Perl %s required--this is only version %s, stopped",
2834 SvPV(sv,n_a),PL_patchlevel);
2837 name = SvPV(sv, len);
2838 if (!(name && len > 0 && *name))
2839 DIE("Null filename used");
2840 TAINT_PROPER("require");
2841 if (PL_op->op_type == OP_REQUIRE &&
2842 (svp = hv_fetch(GvHVn(PL_incgv), name, len, 0)) &&
2843 *svp != &PL_sv_undef)
2846 /* prepare to compile file */
2851 (name[1] == '.' && name[2] == '/')))
2853 || (name[0] && name[1] == ':')
2856 || (name[0] == '\\' && name[1] == '\\') /* UNC path */
2859 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2860 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
2865 tryrsfp = doopen_pmc(name,PERL_SCRIPT_MODE);
2868 AV *ar = GvAVn(PL_incgv);
2872 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2875 namesv = NEWSV(806, 0);
2876 for (i = 0; i <= AvFILL(ar); i++) {
2877 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
2880 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2882 sv_setpv(namesv, unixdir);
2883 sv_catpv(namesv, unixname);
2885 sv_setpvf(namesv, "%s/%s", dir, name);
2887 TAINT_PROPER("require");
2888 tryname = SvPVX(namesv);
2889 tryrsfp = doopen_pmc(tryname, PERL_SCRIPT_MODE);
2891 if (tryname[0] == '.' && tryname[1] == '/')
2898 SAVESPTR(PL_compiling.cop_filegv);
2899 PL_compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2900 SvREFCNT_dec(namesv);
2902 if (PL_op->op_type == OP_REQUIRE) {
2903 char *msgstr = name;
2904 if (namesv) { /* did we lookup @INC? */
2905 SV *msg = sv_2mortal(newSVpv(msgstr,0));
2906 SV *dirmsgsv = NEWSV(0, 0);
2907 AV *ar = GvAVn(PL_incgv);
2909 sv_catpvn(msg, " in @INC", 8);
2910 if (instr(SvPVX(msg), ".h "))
2911 sv_catpv(msg, " (change .h to .ph maybe?)");
2912 if (instr(SvPVX(msg), ".ph "))
2913 sv_catpv(msg, " (did you run h2ph?)");
2914 sv_catpv(msg, " (@INC contains:");
2915 for (i = 0; i <= AvFILL(ar); i++) {
2916 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
2917 sv_setpvf(dirmsgsv, " %s", dir);
2918 sv_catsv(msg, dirmsgsv);
2920 sv_catpvn(msg, ")", 1);
2921 SvREFCNT_dec(dirmsgsv);
2922 msgstr = SvPV_nolen(msg);
2924 DIE("Can't locate %s", msgstr);
2930 SETERRNO(0, SS$_NORMAL);
2932 /* Assume success here to prevent recursive requirement. */
2933 (void)hv_store(GvHVn(PL_incgv), name, strlen(name),
2934 newSVsv(GvSV(PL_compiling.cop_filegv)), 0 );
2938 lex_start(sv_2mortal(newSVpvn("",0)));
2939 SAVEGENERICSV(PL_rsfp_filters);
2940 PL_rsfp_filters = Nullav;
2943 name = savepv(name);
2947 SAVEPPTR(PL_compiling.cop_warnings);
2948 PL_compiling.cop_warnings = ((PL_dowarn & G_WARN_ALL_ON) ? WARN_ALL
2951 /* switch to eval mode */
2953 push_return(PL_op->op_next);
2954 PUSHBLOCK(cx, CXt_EVAL, SP);
2955 PUSHEVAL(cx, name, PL_compiling.cop_filegv);
2957 SAVEI16(PL_compiling.cop_line);
2958 PL_compiling.cop_line = 0;
2962 MUTEX_LOCK(&PL_eval_mutex);
2963 if (PL_eval_owner && PL_eval_owner != thr)
2964 while (PL_eval_owner)
2965 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
2966 PL_eval_owner = thr;
2967 MUTEX_UNLOCK(&PL_eval_mutex);
2968 #endif /* USE_THREADS */
2969 return DOCATCH(doeval(G_SCALAR, NULL));
2974 return pp_require(ARGS);
2980 register PERL_CONTEXT *cx;
2982 I32 gimme = GIMME_V, was = PL_sub_generation;
2983 char tmpbuf[TYPE_DIGITS(long) + 12];
2988 if (!SvPV(sv,len) || !len)
2990 TAINT_PROPER("eval");
2996 /* switch to eval mode */
2998 SAVESPTR(PL_compiling.cop_filegv);
2999 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++PL_evalseq);
3000 PL_compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
3001 PL_compiling.cop_line = 1;
3002 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3003 deleting the eval's FILEGV from the stash before gv_check() runs
3004 (i.e. before run-time proper). To work around the coredump that
3005 ensues, we always turn GvMULTI_on for any globals that were
3006 introduced within evals. See force_ident(). GSAR 96-10-12 */
3007 safestr = savepv(tmpbuf);
3008 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
3010 PL_hints = PL_op->op_targ;
3011 SAVEPPTR(PL_compiling.cop_warnings);
3012 if (PL_compiling.cop_warnings != WARN_ALL
3013 && PL_compiling.cop_warnings != WARN_NONE){
3014 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
3015 SAVEFREESV(PL_compiling.cop_warnings) ;
3018 push_return(PL_op->op_next);
3019 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3020 PUSHEVAL(cx, 0, PL_compiling.cop_filegv);
3022 /* prepare to compile string */
3024 if (PERLDB_LINE && PL_curstash != PL_debstash)
3025 save_lines(GvAV(PL_compiling.cop_filegv), PL_linestr);
3028 MUTEX_LOCK(&PL_eval_mutex);
3029 if (PL_eval_owner && PL_eval_owner != thr)
3030 while (PL_eval_owner)
3031 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
3032 PL_eval_owner = thr;
3033 MUTEX_UNLOCK(&PL_eval_mutex);
3034 #endif /* USE_THREADS */
3035 ret = doeval(gimme, NULL);
3036 if (PERLDB_INTER && was != PL_sub_generation /* Some subs defined here. */
3037 && ret != PL_op->op_next) { /* Successive compilation. */
3038 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
3040 return DOCATCH(ret);
3050 register PERL_CONTEXT *cx;
3052 U8 save_flags = PL_op -> op_flags;
3057 retop = pop_return();
3060 if (gimme == G_VOID)
3062 else if (gimme == G_SCALAR) {
3065 if (SvFLAGS(TOPs) & SVs_TEMP)
3068 *MARK = sv_mortalcopy(TOPs);
3072 *MARK = &PL_sv_undef;
3076 /* in case LEAVE wipes old return values */
3077 for (mark = newsp + 1; mark <= SP; mark++) {
3078 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3079 *mark = sv_mortalcopy(*mark);
3080 TAINT_NOT; /* Each item is independent */
3084 PL_curpm = newpm; /* Don't pop $1 et al till now */
3087 * Closures mentioned at top level of eval cannot be referenced
3088 * again, and their presence indirectly causes a memory leak.
3089 * (Note that the fact that compcv and friends are still set here
3090 * is, AFAIK, an accident.) --Chip
3092 if (AvFILLp(PL_comppad_name) >= 0) {
3093 SV **svp = AvARRAY(PL_comppad_name);
3095 for (ix = AvFILLp(PL_comppad_name); ix >= 0; ix--) {
3097 if (sv && sv != &PL_sv_undef && *SvPVX(sv) == '&') {
3099 svp[ix] = &PL_sv_undef;
3103 SvREFCNT_dec(CvOUTSIDE(sv));
3104 CvOUTSIDE(sv) = Nullcv;
3117 assert(CvDEPTH(PL_compcv) == 1);
3119 CvDEPTH(PL_compcv) = 0;
3122 if (optype == OP_REQUIRE &&
3123 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
3125 /* Unassume the success we assumed earlier. */
3126 char *name = cx->blk_eval.old_name;
3127 (void)hv_delete(GvHVn(PL_incgv), name, strlen(name), G_DISCARD);
3128 retop = die("%s did not return a true value", name);
3129 /* die_where() did LEAVE, or we won't be here */
3133 if (!(save_flags & OPf_SPECIAL))
3143 register PERL_CONTEXT *cx;
3144 I32 gimme = GIMME_V;
3149 push_return(cLOGOP->op_other->op_next);
3150 PUSHBLOCK(cx, CXt_EVAL, SP);
3152 PL_eval_root = PL_op; /* Only needed so that goto works right. */
3154 PL_in_eval = EVAL_INEVAL;
3157 return DOCATCH(PL_op->op_next);
3167 register PERL_CONTEXT *cx;
3175 if (gimme == G_VOID)
3177 else if (gimme == G_SCALAR) {
3180 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
3183 *MARK = sv_mortalcopy(TOPs);
3187 *MARK = &PL_sv_undef;
3192 /* in case LEAVE wipes old return values */
3193 for (mark = newsp + 1; mark <= SP; mark++) {
3194 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
3195 *mark = sv_mortalcopy(*mark);
3196 TAINT_NOT; /* Each item is independent */
3200 PL_curpm = newpm; /* Don't pop $1 et al till now */
3211 register char *s = SvPV_force(sv, len);
3212 register char *send = s + len;
3213 register char *base;
3214 register I32 skipspaces = 0;
3217 bool postspace = FALSE;
3225 croak("Null picture in formline");
3227 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
3232 *fpc++ = FF_LINEMARK;
3233 noblank = repeat = FALSE;
3251 case ' ': case '\t':
3262 *fpc++ = FF_LITERAL;
3270 *fpc++ = skipspaces;
3274 *fpc++ = FF_NEWLINE;
3278 arg = fpc - linepc + 1;
3285 *fpc++ = FF_LINEMARK;
3286 noblank = repeat = FALSE;
3295 ischop = s[-1] == '^';
3301 arg = (s - base) - 1;
3303 *fpc++ = FF_LITERAL;
3312 *fpc++ = FF_LINEGLOB;
3314 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
3315 arg = ischop ? 512 : 0;
3325 arg |= 256 + (s - f);
3327 *fpc++ = s - base; /* fieldsize for FETCH */
3328 *fpc++ = FF_DECIMAL;
3333 bool ismore = FALSE;
3336 while (*++s == '>') ;
3337 prespace = FF_SPACE;
3339 else if (*s == '|') {
3340 while (*++s == '|') ;
3341 prespace = FF_HALFSPACE;
3346 while (*++s == '<') ;
3349 if (*s == '.' && s[1] == '.' && s[2] == '.') {
3353 *fpc++ = s - base; /* fieldsize for FETCH */
3355 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
3373 { /* need to jump to the next word */
3375 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
3376 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
3377 s = SvPVX(sv) + SvCUR(sv) + z;
3379 Copy(fops, s, arg, U16);
3381 sv_magic(sv, Nullsv, 'f', Nullch, 0);
3386 * The rest of this file was derived from source code contributed
3389 * NOTE: this code was derived from Tom Horsley's qsort replacement
3390 * and should not be confused with the original code.
3393 /* Copyright (C) Tom Horsley, 1997. All rights reserved.
3395 Permission granted to distribute under the same terms as perl which are
3398 This program is free software; you can redistribute it and/or modify
3399 it under the terms of either:
3401 a) the GNU General Public License as published by the Free
3402 Software Foundation; either version 1, or (at your option) any
3405 b) the "Artistic License" which comes with this Kit.
3407 Details on the perl license can be found in the perl source code which
3408 may be located via the www.perl.com web page.
3410 This is the most wonderfulest possible qsort I can come up with (and
3411 still be mostly portable) My (limited) tests indicate it consistently
3412 does about 20% fewer calls to compare than does the qsort in the Visual
3413 C++ library, other vendors may vary.
3415 Some of the ideas in here can be found in "Algorithms" by Sedgewick,
3416 others I invented myself (or more likely re-invented since they seemed
3417 pretty obvious once I watched the algorithm operate for a while).
3419 Most of this code was written while watching the Marlins sweep the Giants
3420 in the 1997 National League Playoffs - no Braves fans allowed to use this
3421 code (just kidding :-).
3423 I realize that if I wanted to be true to the perl tradition, the only
3424 comment in this file would be something like:
3426 ...they shuffled back towards the rear of the line. 'No, not at the
3427 rear!' the slave-driver shouted. 'Three files up. And stay there...
3429 However, I really needed to violate that tradition just so I could keep
3430 track of what happens myself, not to mention some poor fool trying to
3431 understand this years from now :-).
3434 /* ********************************************************** Configuration */
3436 #ifndef QSORT_ORDER_GUESS
3437 #define QSORT_ORDER_GUESS 2 /* Select doubling version of the netBSD trick */
3440 /* QSORT_MAX_STACK is the largest number of partitions that can be stacked up for
3441 future processing - a good max upper bound is log base 2 of memory size
3442 (32 on 32 bit machines, 64 on 64 bit machines, etc). In reality can
3443 safely be smaller than that since the program is taking up some space and
3444 most operating systems only let you grab some subset of contiguous
3445 memory (not to mention that you are normally sorting data larger than
3446 1 byte element size :-).
3448 #ifndef QSORT_MAX_STACK
3449 #define QSORT_MAX_STACK 32
3452 /* QSORT_BREAK_EVEN is the size of the largest partition we should insertion sort.
3453 Anything bigger and we use qsort. If you make this too small, the qsort
3454 will probably break (or become less efficient), because it doesn't expect
3455 the middle element of a partition to be the same as the right or left -
3456 you have been warned).
3458 #ifndef QSORT_BREAK_EVEN
3459 #define QSORT_BREAK_EVEN 6
3462 /* ************************************************************* Data Types */
3464 /* hold left and right index values of a partition waiting to be sorted (the
3465 partition includes both left and right - right is NOT one past the end or
3466 anything like that).
3468 struct partition_stack_entry {
3471 #ifdef QSORT_ORDER_GUESS
3472 int qsort_break_even;
3476 /* ******************************************************* Shorthand Macros */
3478 /* Note that these macros will be used from inside the qsort function where
3479 we happen to know that the variable 'elt_size' contains the size of an
3480 array element and the variable 'temp' points to enough space to hold a
3481 temp element and the variable 'array' points to the array being sorted
3482 and 'compare' is the pointer to the compare routine.
3484 Also note that there are very many highly architecture specific ways
3485 these might be sped up, but this is simply the most generally portable
3486 code I could think of.
3489 /* Return < 0 == 0 or > 0 as the value of elt1 is < elt2, == elt2, > elt2
3492 #define qsort_cmp(elt1, elt2) \
3493 ((this->*compare)(array[elt1], array[elt2]))
3495 #define qsort_cmp(elt1, elt2) \
3496 ((*compare)(array[elt1], array[elt2]))
3499 #ifdef QSORT_ORDER_GUESS
3500 #define QSORT_NOTICE_SWAP swapped++;
3502 #define QSORT_NOTICE_SWAP
3505 /* swaps contents of array elements elt1, elt2.
3507 #define qsort_swap(elt1, elt2) \
3510 temp = array[elt1]; \
3511 array[elt1] = array[elt2]; \
3512 array[elt2] = temp; \
3515 /* rotate contents of elt1, elt2, elt3 such that elt1 gets elt2, elt2 gets
3516 elt3 and elt3 gets elt1.
3518 #define qsort_rotate(elt1, elt2, elt3) \
3521 temp = array[elt1]; \
3522 array[elt1] = array[elt2]; \
3523 array[elt2] = array[elt3]; \
3524 array[elt3] = temp; \
3527 /* ************************************************************ Debug stuff */
3534 return; /* good place to set a breakpoint */
3537 #define qsort_assert(t) (void)( (t) || (break_here(), 0) )
3540 doqsort_all_asserts(
3544 int (*compare)(const void * elt1, const void * elt2),
3545 int pc_left, int pc_right, int u_left, int u_right)
3549 qsort_assert(pc_left <= pc_right);
3550 qsort_assert(u_right < pc_left);
3551 qsort_assert(pc_right < u_left);
3552 for (i = u_right + 1; i < pc_left; ++i) {
3553 qsort_assert(qsort_cmp(i, pc_left) < 0);
3555 for (i = pc_left; i < pc_right; ++i) {
3556 qsort_assert(qsort_cmp(i, pc_right) == 0);
3558 for (i = pc_right + 1; i < u_left; ++i) {
3559 qsort_assert(qsort_cmp(pc_right, i) < 0);
3563 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) \
3564 doqsort_all_asserts(array, num_elts, elt_size, compare, \
3565 PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT)
3569 #define qsort_assert(t) ((void)0)
3571 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) ((void)0)
3575 /* ****************************************************************** qsort */
3579 qsortsv(SV ** array, size_t num_elts, SVCOMPARE compare)
3584 I32 (*compare)(SV *a, SV *b))
3589 struct partition_stack_entry partition_stack[QSORT_MAX_STACK];
3590 int next_stack_entry = 0;
3594 #ifdef QSORT_ORDER_GUESS
3595 int qsort_break_even;
3599 /* Make sure we actually have work to do.
3601 if (num_elts <= 1) {
3605 /* Setup the initial partition definition and fall into the sorting loop
3608 part_right = (int)(num_elts - 1);
3609 #ifdef QSORT_ORDER_GUESS
3610 qsort_break_even = QSORT_BREAK_EVEN;
3612 #define qsort_break_even QSORT_BREAK_EVEN
3615 if ((part_right - part_left) >= qsort_break_even) {
3616 /* OK, this is gonna get hairy, so lets try to document all the
3617 concepts and abbreviations and variables and what they keep
3620 pc: pivot chunk - the set of array elements we accumulate in the
3621 middle of the partition, all equal in value to the original
3622 pivot element selected. The pc is defined by:
3624 pc_left - the leftmost array index of the pc
3625 pc_right - the rightmost array index of the pc
3627 we start with pc_left == pc_right and only one element
3628 in the pivot chunk (but it can grow during the scan).
3630 u: uncompared elements - the set of elements in the partition
3631 we have not yet compared to the pivot value. There are two
3632 uncompared sets during the scan - one to the left of the pc
3633 and one to the right.
3635 u_right - the rightmost index of the left side's uncompared set
3636 u_left - the leftmost index of the right side's uncompared set
3638 The leftmost index of the left sides's uncompared set
3639 doesn't need its own variable because it is always defined
3640 by the leftmost edge of the whole partition (part_left). The
3641 same goes for the rightmost edge of the right partition
3644 We know there are no uncompared elements on the left once we
3645 get u_right < part_left and no uncompared elements on the
3646 right once u_left > part_right. When both these conditions
3647 are met, we have completed the scan of the partition.
3649 Any elements which are between the pivot chunk and the
3650 uncompared elements should be less than the pivot value on
3651 the left side and greater than the pivot value on the right
3652 side (in fact, the goal of the whole algorithm is to arrange
3653 for that to be true and make the groups of less-than and
3654 greater-then elements into new partitions to sort again).
3656 As you marvel at the complexity of the code and wonder why it
3657 has to be so confusing. Consider some of the things this level
3658 of confusion brings:
3660 Once I do a compare, I squeeze every ounce of juice out of it. I
3661 never do compare calls I don't have to do, and I certainly never
3664 I also never swap any elements unless I can prove there is a
3665 good reason. Many sort algorithms will swap a known value with
3666 an uncompared value just to get things in the right place (or
3667 avoid complexity :-), but that uncompared value, once it gets
3668 compared, may then have to be swapped again. A lot of the
3669 complexity of this code is due to the fact that it never swaps
3670 anything except compared values, and it only swaps them when the
3671 compare shows they are out of position.
3673 int pc_left, pc_right;
3674 int u_right, u_left;
3678 pc_left = ((part_left + part_right) / 2);
3680 u_right = pc_left - 1;
3681 u_left = pc_right + 1;
3683 /* Qsort works best when the pivot value is also the median value
3684 in the partition (unfortunately you can't find the median value
3685 without first sorting :-), so to give the algorithm a helping
3686 hand, we pick 3 elements and sort them and use the median value
3687 of that tiny set as the pivot value.
3689 Some versions of qsort like to use the left middle and right as
3690 the 3 elements to sort so they can insure the ends of the
3691 partition will contain values which will stop the scan in the
3692 compare loop, but when you have to call an arbitrarily complex
3693 routine to do a compare, its really better to just keep track of
3694 array index values to know when you hit the edge of the
3695 partition and avoid the extra compare. An even better reason to
3696 avoid using a compare call is the fact that you can drop off the
3697 edge of the array if someone foolishly provides you with an
3698 unstable compare function that doesn't always provide consistent
3701 So, since it is simpler for us to compare the three adjacent
3702 elements in the middle of the partition, those are the ones we
3703 pick here (conveniently pointed at by u_right, pc_left, and
3704 u_left). The values of the left, center, and right elements
3705 are refered to as l c and r in the following comments.
3708 #ifdef QSORT_ORDER_GUESS
3711 s = qsort_cmp(u_right, pc_left);
3714 s = qsort_cmp(pc_left, u_left);
3715 /* if l < c, c < r - already in order - nothing to do */
3717 /* l < c, c == r - already in order, pc grows */
3719 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3721 /* l < c, c > r - need to know more */
3722 s = qsort_cmp(u_right, u_left);
3724 /* l < c, c > r, l < r - swap c & r to get ordered */
3725 qsort_swap(pc_left, u_left);
3726 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3727 } else if (s == 0) {
3728 /* l < c, c > r, l == r - swap c&r, grow pc */
3729 qsort_swap(pc_left, u_left);
3731 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3733 /* l < c, c > r, l > r - make lcr into rlc to get ordered */
3734 qsort_rotate(pc_left, u_right, u_left);
3735 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3738 } else if (s == 0) {
3740 s = qsort_cmp(pc_left, u_left);
3742 /* l == c, c < r - already in order, grow pc */
3744 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3745 } else if (s == 0) {
3746 /* l == c, c == r - already in order, grow pc both ways */
3749 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3751 /* l == c, c > r - swap l & r, grow pc */
3752 qsort_swap(u_right, u_left);
3754 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3758 s = qsort_cmp(pc_left, u_left);
3760 /* l > c, c < r - need to know more */
3761 s = qsort_cmp(u_right, u_left);
3763 /* l > c, c < r, l < r - swap l & c to get ordered */
3764 qsort_swap(u_right, pc_left);
3765 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3766 } else if (s == 0) {
3767 /* l > c, c < r, l == r - swap l & c, grow pc */
3768 qsort_swap(u_right, pc_left);
3770 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3772 /* l > c, c < r, l > r - rotate lcr into crl to order */
3773 qsort_rotate(u_right, pc_left, u_left);
3774 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3776 } else if (s == 0) {
3777 /* l > c, c == r - swap ends, grow pc */
3778 qsort_swap(u_right, u_left);
3780 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3782 /* l > c, c > r - swap ends to get in order */
3783 qsort_swap(u_right, u_left);
3784 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3787 /* We now know the 3 middle elements have been compared and
3788 arranged in the desired order, so we can shrink the uncompared
3793 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3795 /* The above massive nested if was the simple part :-). We now have
3796 the middle 3 elements ordered and we need to scan through the
3797 uncompared sets on either side, swapping elements that are on
3798 the wrong side or simply shuffling equal elements around to get
3799 all equal elements into the pivot chunk.
3803 int still_work_on_left;
3804 int still_work_on_right;
3806 /* Scan the uncompared values on the left. If I find a value
3807 equal to the pivot value, move it over so it is adjacent to
3808 the pivot chunk and expand the pivot chunk. If I find a value
3809 less than the pivot value, then just leave it - its already
3810 on the correct side of the partition. If I find a greater
3811 value, then stop the scan.
3813 while (still_work_on_left = (u_right >= part_left)) {
3814 s = qsort_cmp(u_right, pc_left);
3817 } else if (s == 0) {
3819 if (pc_left != u_right) {
3820 qsort_swap(u_right, pc_left);
3826 qsort_assert(u_right < pc_left);
3827 qsort_assert(pc_left <= pc_right);
3828 qsort_assert(qsort_cmp(u_right + 1, pc_left) <= 0);
3829 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3832 /* Do a mirror image scan of uncompared values on the right
3834 while (still_work_on_right = (u_left <= part_right)) {
3835 s = qsort_cmp(pc_right, u_left);
3838 } else if (s == 0) {
3840 if (pc_right != u_left) {
3841 qsort_swap(pc_right, u_left);
3847 qsort_assert(u_left > pc_right);
3848 qsort_assert(pc_left <= pc_right);
3849 qsort_assert(qsort_cmp(pc_right, u_left - 1) <= 0);
3850 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3853 if (still_work_on_left) {
3854 /* I know I have a value on the left side which needs to be
3855 on the right side, but I need to know more to decide
3856 exactly the best thing to do with it.
3858 if (still_work_on_right) {
3859 /* I know I have values on both side which are out of
3860 position. This is a big win because I kill two birds
3861 with one swap (so to speak). I can advance the
3862 uncompared pointers on both sides after swapping both
3863 of them into the right place.
3865 qsort_swap(u_right, u_left);
3868 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3870 /* I have an out of position value on the left, but the
3871 right is fully scanned, so I "slide" the pivot chunk
3872 and any less-than values left one to make room for the
3873 greater value over on the right. If the out of position
3874 value is immediately adjacent to the pivot chunk (there
3875 are no less-than values), I can do that with a swap,
3876 otherwise, I have to rotate one of the less than values
3877 into the former position of the out of position value
3878 and the right end of the pivot chunk into the left end
3882 if (pc_left == u_right) {
3883 qsort_swap(u_right, pc_right);
3884 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3886 qsort_rotate(u_right, pc_left, pc_right);
3887 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3892 } else if (still_work_on_right) {
3893 /* Mirror image of complex case above: I have an out of
3894 position value on the right, but the left is fully
3895 scanned, so I need to shuffle things around to make room
3896 for the right value on the left.
3899 if (pc_right == u_left) {
3900 qsort_swap(u_left, pc_left);
3901 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3903 qsort_rotate(pc_right, pc_left, u_left);
3904 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3909 /* No more scanning required on either side of partition,
3910 break out of loop and figure out next set of partitions
3916 /* The elements in the pivot chunk are now in the right place. They
3917 will never move or be compared again. All I have to do is decide
3918 what to do with the stuff to the left and right of the pivot
3921 Notes on the QSORT_ORDER_GUESS ifdef code:
3923 1. If I just built these partitions without swapping any (or
3924 very many) elements, there is a chance that the elements are
3925 already ordered properly (being properly ordered will
3926 certainly result in no swapping, but the converse can't be
3929 2. A (properly written) insertion sort will run faster on
3930 already ordered data than qsort will.
3932 3. Perhaps there is some way to make a good guess about
3933 switching to an insertion sort earlier than partition size 6
3934 (for instance - we could save the partition size on the stack
3935 and increase the size each time we find we didn't swap, thus
3936 switching to insertion sort earlier for partitions with a
3937 history of not swapping).
3939 4. Naturally, if I just switch right away, it will make
3940 artificial benchmarks with pure ascending (or descending)
3941 data look really good, but is that a good reason in general?
3945 #ifdef QSORT_ORDER_GUESS
3947 #if QSORT_ORDER_GUESS == 1
3948 qsort_break_even = (part_right - part_left) + 1;
3950 #if QSORT_ORDER_GUESS == 2
3951 qsort_break_even *= 2;
3953 #if QSORT_ORDER_GUESS == 3
3954 int prev_break = qsort_break_even;
3955 qsort_break_even *= qsort_break_even;
3956 if (qsort_break_even < prev_break) {
3957 qsort_break_even = (part_right - part_left) + 1;
3961 qsort_break_even = QSORT_BREAK_EVEN;
3965 if (part_left < pc_left) {
3966 /* There are elements on the left which need more processing.
3967 Check the right as well before deciding what to do.
3969 if (pc_right < part_right) {
3970 /* We have two partitions to be sorted. Stack the biggest one
3971 and process the smallest one on the next iteration. This
3972 minimizes the stack height by insuring that any additional
3973 stack entries must come from the smallest partition which
3974 (because it is smallest) will have the fewest
3975 opportunities to generate additional stack entries.
3977 if ((part_right - pc_right) > (pc_left - part_left)) {
3978 /* stack the right partition, process the left */
3979 partition_stack[next_stack_entry].left = pc_right + 1;
3980 partition_stack[next_stack_entry].right = part_right;
3981 #ifdef QSORT_ORDER_GUESS
3982 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3984 part_right = pc_left - 1;
3986 /* stack the left partition, process the right */
3987 partition_stack[next_stack_entry].left = part_left;
3988 partition_stack[next_stack_entry].right = pc_left - 1;
3989 #ifdef QSORT_ORDER_GUESS
3990 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3992 part_left = pc_right + 1;
3994 qsort_assert(next_stack_entry < QSORT_MAX_STACK);
3997 /* The elements on the left are the only remaining elements
3998 that need sorting, arrange for them to be processed as the
4001 part_right = pc_left - 1;
4003 } else if (pc_right < part_right) {
4004 /* There is only one chunk on the right to be sorted, make it
4005 the new partition and loop back around.
4007 part_left = pc_right + 1;
4009 /* This whole partition wound up in the pivot chunk, so
4010 we need to get a new partition off the stack.
4012 if (next_stack_entry == 0) {
4013 /* the stack is empty - we are done */
4017 part_left = partition_stack[next_stack_entry].left;
4018 part_right = partition_stack[next_stack_entry].right;
4019 #ifdef QSORT_ORDER_GUESS
4020 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
4024 /* This partition is too small to fool with qsort complexity, just
4025 do an ordinary insertion sort to minimize overhead.
4028 /* Assume 1st element is in right place already, and start checking
4029 at 2nd element to see where it should be inserted.
4031 for (i = part_left + 1; i <= part_right; ++i) {
4033 /* Scan (backwards - just in case 'i' is already in right place)
4034 through the elements already sorted to see if the ith element
4035 belongs ahead of one of them.
4037 for (j = i - 1; j >= part_left; --j) {
4038 if (qsort_cmp(i, j) >= 0) {
4039 /* i belongs right after j
4046 /* Looks like we really need to move some things
4050 for (k = i - 1; k >= j; --k)
4051 array[k + 1] = array[k];
4056 /* That partition is now sorted, grab the next one, or get out
4057 of the loop if there aren't any more.
4060 if (next_stack_entry == 0) {
4061 /* the stack is empty - we are done */
4065 part_left = partition_stack[next_stack_entry].left;
4066 part_right = partition_stack[next_stack_entry].right;
4067 #ifdef QSORT_ORDER_GUESS
4068 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
4073 /* Believe it or not, the array is sorted at this point! */