| 1 | /* pp_ctl.c |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others |
| 5 | * |
| 6 | * You may distribute under the terms of either the GNU General Public |
| 7 | * License or the Artistic License, as specified in the README file. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Now far ahead the Road has gone, |
| 13 | * And I must follow, if I can, |
| 14 | * Pursuing it with eager feet, |
| 15 | * Until it joins some larger way |
| 16 | * Where many paths and errands meet. |
| 17 | * And whither then? I cannot say. |
| 18 | * |
| 19 | * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] |
| 20 | */ |
| 21 | |
| 22 | /* This file contains control-oriented pp ("push/pop") functions that |
| 23 | * execute the opcodes that make up a perl program. A typical pp function |
| 24 | * expects to find its arguments on the stack, and usually pushes its |
| 25 | * results onto the stack, hence the 'pp' terminology. Each OP structure |
| 26 | * contains a pointer to the relevant pp_foo() function. |
| 27 | * |
| 28 | * Control-oriented means things like pp_enteriter() and pp_next(), which |
| 29 | * alter the flow of control of the program. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "EXTERN.h" |
| 34 | #define PERL_IN_PP_CTL_C |
| 35 | #include "perl.h" |
| 36 | |
| 37 | #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o)) |
| 38 | |
| 39 | #define dopoptosub(plop) dopoptosub_at(cxstack, (plop)) |
| 40 | |
| 41 | PP(pp_wantarray) |
| 42 | { |
| 43 | dVAR; |
| 44 | dSP; |
| 45 | I32 cxix; |
| 46 | EXTEND(SP, 1); |
| 47 | |
| 48 | cxix = dopoptosub(cxstack_ix); |
| 49 | if (cxix < 0) |
| 50 | RETPUSHUNDEF; |
| 51 | |
| 52 | switch (cxstack[cxix].blk_gimme) { |
| 53 | case G_ARRAY: |
| 54 | RETPUSHYES; |
| 55 | case G_SCALAR: |
| 56 | RETPUSHNO; |
| 57 | default: |
| 58 | RETPUSHUNDEF; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | PP(pp_regcreset) |
| 63 | { |
| 64 | dVAR; |
| 65 | /* XXXX Should store the old value to allow for tie/overload - and |
| 66 | restore in regcomp, where marked with XXXX. */ |
| 67 | PL_reginterp_cnt = 0; |
| 68 | TAINT_NOT; |
| 69 | return NORMAL; |
| 70 | } |
| 71 | |
| 72 | PP(pp_regcomp) |
| 73 | { |
| 74 | dVAR; |
| 75 | dSP; |
| 76 | register PMOP *pm = (PMOP*)cLOGOP->op_other; |
| 77 | SV *tmpstr; |
| 78 | REGEXP *re = NULL; |
| 79 | |
| 80 | /* prevent recompiling under /o and ithreads. */ |
| 81 | #if defined(USE_ITHREADS) |
| 82 | if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) { |
| 83 | if (PL_op->op_flags & OPf_STACKED) { |
| 84 | dMARK; |
| 85 | SP = MARK; |
| 86 | } |
| 87 | else |
| 88 | (void)POPs; |
| 89 | RETURN; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #define tryAMAGICregexp(rx) \ |
| 94 | STMT_START { \ |
| 95 | SvGETMAGIC(rx); \ |
| 96 | if (SvROK(rx) && SvAMAGIC(rx)) { \ |
| 97 | SV *sv = AMG_CALLunary(rx, regexp_amg); \ |
| 98 | if (sv) { \ |
| 99 | if (SvROK(sv)) \ |
| 100 | sv = SvRV(sv); \ |
| 101 | if (SvTYPE(sv) != SVt_REGEXP) \ |
| 102 | Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \ |
| 103 | rx = sv; \ |
| 104 | } \ |
| 105 | } \ |
| 106 | } STMT_END |
| 107 | |
| 108 | |
| 109 | if (PL_op->op_flags & OPf_STACKED) { |
| 110 | /* multiple args; concatenate them */ |
| 111 | dMARK; dORIGMARK; |
| 112 | tmpstr = PAD_SV(ARGTARG); |
| 113 | sv_setpvs(tmpstr, ""); |
| 114 | while (++MARK <= SP) { |
| 115 | SV *msv = *MARK; |
| 116 | SV *sv; |
| 117 | |
| 118 | tryAMAGICregexp(msv); |
| 119 | |
| 120 | if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) && |
| 121 | (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign))) |
| 122 | { |
| 123 | sv_setsv(tmpstr, sv); |
| 124 | continue; |
| 125 | } |
| 126 | sv_catsv_nomg(tmpstr, msv); |
| 127 | } |
| 128 | SvSETMAGIC(tmpstr); |
| 129 | SP = ORIGMARK; |
| 130 | } |
| 131 | else { |
| 132 | tmpstr = POPs; |
| 133 | tryAMAGICregexp(tmpstr); |
| 134 | } |
| 135 | |
| 136 | #undef tryAMAGICregexp |
| 137 | |
| 138 | if (SvROK(tmpstr)) { |
| 139 | SV * const sv = SvRV(tmpstr); |
| 140 | if (SvTYPE(sv) == SVt_REGEXP) |
| 141 | re = (REGEXP*) sv; |
| 142 | } |
| 143 | else if (SvTYPE(tmpstr) == SVt_REGEXP) |
| 144 | re = (REGEXP*) tmpstr; |
| 145 | |
| 146 | if (re) { |
| 147 | /* The match's LHS's get-magic might need to access this op's reg- |
| 148 | exp (as is sometimes the case with $'; see bug 70764). So we |
| 149 | must call get-magic now before we replace the regexp. Hopeful- |
| 150 | ly this hack can be replaced with the approach described at |
| 151 | http://www.nntp.perl.org/group/perl.perl5.porters/2007/03 |
| 152 | /msg122415.html some day. */ |
| 153 | if(pm->op_type == OP_MATCH) { |
| 154 | SV *lhs; |
| 155 | const bool was_tainted = PL_tainted; |
| 156 | if (pm->op_flags & OPf_STACKED) |
| 157 | lhs = TOPs; |
| 158 | else if (pm->op_private & OPpTARGET_MY) |
| 159 | lhs = PAD_SV(pm->op_targ); |
| 160 | else lhs = DEFSV; |
| 161 | SvGETMAGIC(lhs); |
| 162 | /* Restore the previous value of PL_tainted (which may have been |
| 163 | modified by get-magic), to avoid incorrectly setting the |
| 164 | RXf_TAINTED flag further down. */ |
| 165 | PL_tainted = was_tainted; |
| 166 | } |
| 167 | |
| 168 | re = reg_temp_copy(NULL, re); |
| 169 | ReREFCNT_dec(PM_GETRE(pm)); |
| 170 | PM_SETRE(pm, re); |
| 171 | } |
| 172 | else { |
| 173 | STRLEN len = 0; |
| 174 | const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : ""; |
| 175 | |
| 176 | re = PM_GETRE(pm); |
| 177 | assert (re != (REGEXP*) &PL_sv_undef); |
| 178 | |
| 179 | /* Check against the last compiled regexp. */ |
| 180 | if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len || |
| 181 | memNE(RX_PRECOMP(re), t, len)) |
| 182 | { |
| 183 | const regexp_engine *eng = re ? RX_ENGINE(re) : NULL; |
| 184 | U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME; |
| 185 | if (re) { |
| 186 | ReREFCNT_dec(re); |
| 187 | #ifdef USE_ITHREADS |
| 188 | PM_SETRE(pm, (REGEXP*) &PL_sv_undef); |
| 189 | #else |
| 190 | PM_SETRE(pm, NULL); /* crucial if regcomp aborts */ |
| 191 | #endif |
| 192 | } else if (PL_curcop->cop_hints_hash) { |
| 193 | SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0); |
| 194 | if (ptr && SvIOK(ptr) && SvIV(ptr)) |
| 195 | eng = INT2PTR(regexp_engine*,SvIV(ptr)); |
| 196 | } |
| 197 | |
| 198 | if (PL_op->op_flags & OPf_SPECIAL) |
| 199 | PL_reginterp_cnt = I32_MAX; /* Mark as safe. */ |
| 200 | |
| 201 | if (DO_UTF8(tmpstr)) { |
| 202 | assert (SvUTF8(tmpstr)); |
| 203 | } else if (SvUTF8(tmpstr)) { |
| 204 | /* Not doing UTF-8, despite what the SV says. Is this only if |
| 205 | we're trapped in use 'bytes'? */ |
| 206 | /* Make a copy of the octet sequence, but without the flag on, |
| 207 | as the compiler now honours the SvUTF8 flag on tmpstr. */ |
| 208 | STRLEN len; |
| 209 | const char *const p = SvPV(tmpstr, len); |
| 210 | tmpstr = newSVpvn_flags(p, len, SVs_TEMP); |
| 211 | } |
| 212 | else if (SvAMAGIC(tmpstr)) { |
| 213 | /* make a copy to avoid extra stringifies */ |
| 214 | tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr)); |
| 215 | } |
| 216 | |
| 217 | /* If it is gmagical, create a mortal copy, but without calling |
| 218 | get-magic, as we have already done that. */ |
| 219 | if(SvGMAGICAL(tmpstr)) { |
| 220 | SV *mortalcopy = sv_newmortal(); |
| 221 | sv_setsv_flags(mortalcopy, tmpstr, 0); |
| 222 | tmpstr = mortalcopy; |
| 223 | } |
| 224 | |
| 225 | if (eng) |
| 226 | PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags)); |
| 227 | else |
| 228 | PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags)); |
| 229 | |
| 230 | PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed |
| 231 | inside tie/overload accessors. */ |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | re = PM_GETRE(pm); |
| 236 | |
| 237 | #ifndef INCOMPLETE_TAINTS |
| 238 | if (PL_tainting) { |
| 239 | if (PL_tainted) { |
| 240 | SvTAINTED_on((SV*)re); |
| 241 | RX_EXTFLAGS(re) |= RXf_TAINTED; |
| 242 | } |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) |
| 247 | pm = PL_curpm; |
| 248 | |
| 249 | |
| 250 | #if !defined(USE_ITHREADS) |
| 251 | /* can't change the optree at runtime either */ |
| 252 | /* PMf_KEEP is handled differently under threads to avoid these problems */ |
| 253 | if (pm->op_pmflags & PMf_KEEP) { |
| 254 | pm->op_private &= ~OPpRUNTIME; /* no point compiling again */ |
| 255 | cLOGOP->op_first->op_next = PL_op->op_next; |
| 256 | } |
| 257 | #endif |
| 258 | RETURN; |
| 259 | } |
| 260 | |
| 261 | PP(pp_substcont) |
| 262 | { |
| 263 | dVAR; |
| 264 | dSP; |
| 265 | register PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
| 266 | register PMOP * const pm = (PMOP*) cLOGOP->op_other; |
| 267 | register SV * const dstr = cx->sb_dstr; |
| 268 | register char *s = cx->sb_s; |
| 269 | register char *m = cx->sb_m; |
| 270 | char *orig = cx->sb_orig; |
| 271 | register REGEXP * const rx = cx->sb_rx; |
| 272 | SV *nsv = NULL; |
| 273 | REGEXP *old = PM_GETRE(pm); |
| 274 | |
| 275 | PERL_ASYNC_CHECK(); |
| 276 | |
| 277 | if(old != rx) { |
| 278 | if(old) |
| 279 | ReREFCNT_dec(old); |
| 280 | PM_SETRE(pm,ReREFCNT_inc(rx)); |
| 281 | } |
| 282 | |
| 283 | rxres_restore(&cx->sb_rxres, rx); |
| 284 | RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ)); |
| 285 | |
| 286 | if (cx->sb_iters++) { |
| 287 | const I32 saviters = cx->sb_iters; |
| 288 | if (cx->sb_iters > cx->sb_maxiters) |
| 289 | DIE(aTHX_ "Substitution loop"); |
| 290 | |
| 291 | SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */ |
| 292 | |
| 293 | /* See "how taint works" above pp_subst() */ |
| 294 | if (SvTAINTED(TOPs)) |
| 295 | cx->sb_rxtainted |= SUBST_TAINT_REPL; |
| 296 | sv_catsv_nomg(dstr, POPs); |
| 297 | /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */ |
| 298 | s -= RX_GOFS(rx); |
| 299 | |
| 300 | /* Are we done */ |
| 301 | if (CxONCE(cx) || s < orig || |
| 302 | !CALLREGEXEC(rx, s, cx->sb_strend, orig, |
| 303 | (s == m) + RX_GOFS(rx), cx->sb_targ, NULL, |
| 304 | ((cx->sb_rflags & REXEC_COPY_STR) |
| 305 | ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST) |
| 306 | : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST)))) |
| 307 | { |
| 308 | SV * const targ = cx->sb_targ; |
| 309 | |
| 310 | assert(cx->sb_strend >= s); |
| 311 | if(cx->sb_strend > s) { |
| 312 | if (DO_UTF8(dstr) && !SvUTF8(targ)) |
| 313 | sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv); |
| 314 | else |
| 315 | sv_catpvn(dstr, s, cx->sb_strend - s); |
| 316 | } |
| 317 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
| 318 | cx->sb_rxtainted |= SUBST_TAINT_PAT; |
| 319 | |
| 320 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 321 | if (SvIsCOW(targ)) { |
| 322 | sv_force_normal_flags(targ, SV_COW_DROP_PV); |
| 323 | } else |
| 324 | #endif |
| 325 | { |
| 326 | SvPV_free(targ); |
| 327 | } |
| 328 | SvPV_set(targ, SvPVX(dstr)); |
| 329 | SvCUR_set(targ, SvCUR(dstr)); |
| 330 | SvLEN_set(targ, SvLEN(dstr)); |
| 331 | if (DO_UTF8(dstr)) |
| 332 | SvUTF8_on(targ); |
| 333 | SvPV_set(dstr, NULL); |
| 334 | |
| 335 | if (pm->op_pmflags & PMf_NONDESTRUCT) |
| 336 | PUSHs(targ); |
| 337 | else |
| 338 | mPUSHi(saviters - 1); |
| 339 | |
| 340 | (void)SvPOK_only_UTF8(targ); |
| 341 | |
| 342 | /* update the taint state of various various variables in |
| 343 | * preparation for final exit. |
| 344 | * See "how taint works" above pp_subst() */ |
| 345 | if (PL_tainting) { |
| 346 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || |
| 347 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) |
| 348 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) |
| 349 | ) |
| 350 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ |
| 351 | |
| 352 | if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET) |
| 353 | && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) |
| 354 | ) |
| 355 | SvTAINTED_on(TOPs); /* taint return value */ |
| 356 | /* needed for mg_set below */ |
| 357 | PL_tainted = cBOOL(cx->sb_rxtainted & |
| 358 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)); |
| 359 | SvTAINT(TARG); |
| 360 | } |
| 361 | /* PL_tainted must be correctly set for this mg_set */ |
| 362 | SvSETMAGIC(TARG); |
| 363 | TAINT_NOT; |
| 364 | LEAVE_SCOPE(cx->sb_oldsave); |
| 365 | POPSUBST(cx); |
| 366 | RETURNOP(pm->op_next); |
| 367 | /* NOTREACHED */ |
| 368 | } |
| 369 | cx->sb_iters = saviters; |
| 370 | } |
| 371 | if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) { |
| 372 | m = s; |
| 373 | s = orig; |
| 374 | cx->sb_orig = orig = RX_SUBBEG(rx); |
| 375 | s = orig + (m - s); |
| 376 | cx->sb_strend = s + (cx->sb_strend - m); |
| 377 | } |
| 378 | cx->sb_m = m = RX_OFFS(rx)[0].start + orig; |
| 379 | if (m > s) { |
| 380 | if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ)) |
| 381 | sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv); |
| 382 | else |
| 383 | sv_catpvn(dstr, s, m-s); |
| 384 | } |
| 385 | cx->sb_s = RX_OFFS(rx)[0].end + orig; |
| 386 | { /* Update the pos() information. */ |
| 387 | SV * const sv = cx->sb_targ; |
| 388 | MAGIC *mg; |
| 389 | SvUPGRADE(sv, SVt_PVMG); |
| 390 | if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) { |
| 391 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 392 | if (SvIsCOW(sv)) |
| 393 | sv_force_normal_flags(sv, 0); |
| 394 | #endif |
| 395 | mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob, |
| 396 | NULL, 0); |
| 397 | } |
| 398 | mg->mg_len = m - orig; |
| 399 | } |
| 400 | if (old != rx) |
| 401 | (void)ReREFCNT_inc(rx); |
| 402 | /* update the taint state of various various variables in preparation |
| 403 | * for calling the code block. |
| 404 | * See "how taint works" above pp_subst() */ |
| 405 | if (PL_tainting) { |
| 406 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
| 407 | cx->sb_rxtainted |= SUBST_TAINT_PAT; |
| 408 | |
| 409 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || |
| 410 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) |
| 411 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) |
| 412 | ) |
| 413 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ |
| 414 | |
| 415 | if (cx->sb_iters > 1 && (cx->sb_rxtainted & |
| 416 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))) |
| 417 | SvTAINTED_on(cx->sb_targ); |
| 418 | TAINT_NOT; |
| 419 | } |
| 420 | rxres_save(&cx->sb_rxres, rx); |
| 421 | PL_curpm = pm; |
| 422 | RETURNOP(pm->op_pmstashstartu.op_pmreplstart); |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx) |
| 427 | { |
| 428 | UV *p = (UV*)*rsp; |
| 429 | U32 i; |
| 430 | |
| 431 | PERL_ARGS_ASSERT_RXRES_SAVE; |
| 432 | PERL_UNUSED_CONTEXT; |
| 433 | |
| 434 | if (!p || p[1] < RX_NPARENS(rx)) { |
| 435 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 436 | i = 7 + RX_NPARENS(rx) * 2; |
| 437 | #else |
| 438 | i = 6 + RX_NPARENS(rx) * 2; |
| 439 | #endif |
| 440 | if (!p) |
| 441 | Newx(p, i, UV); |
| 442 | else |
| 443 | Renew(p, i, UV); |
| 444 | *rsp = (void*)p; |
| 445 | } |
| 446 | |
| 447 | *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL); |
| 448 | RX_MATCH_COPIED_off(rx); |
| 449 | |
| 450 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 451 | *p++ = PTR2UV(RX_SAVED_COPY(rx)); |
| 452 | RX_SAVED_COPY(rx) = NULL; |
| 453 | #endif |
| 454 | |
| 455 | *p++ = RX_NPARENS(rx); |
| 456 | |
| 457 | *p++ = PTR2UV(RX_SUBBEG(rx)); |
| 458 | *p++ = (UV)RX_SUBLEN(rx); |
| 459 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
| 460 | *p++ = (UV)RX_OFFS(rx)[i].start; |
| 461 | *p++ = (UV)RX_OFFS(rx)[i].end; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | S_rxres_restore(pTHX_ void **rsp, REGEXP *rx) |
| 467 | { |
| 468 | UV *p = (UV*)*rsp; |
| 469 | U32 i; |
| 470 | |
| 471 | PERL_ARGS_ASSERT_RXRES_RESTORE; |
| 472 | PERL_UNUSED_CONTEXT; |
| 473 | |
| 474 | RX_MATCH_COPY_FREE(rx); |
| 475 | RX_MATCH_COPIED_set(rx, *p); |
| 476 | *p++ = 0; |
| 477 | |
| 478 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 479 | if (RX_SAVED_COPY(rx)) |
| 480 | SvREFCNT_dec (RX_SAVED_COPY(rx)); |
| 481 | RX_SAVED_COPY(rx) = INT2PTR(SV*,*p); |
| 482 | *p++ = 0; |
| 483 | #endif |
| 484 | |
| 485 | RX_NPARENS(rx) = *p++; |
| 486 | |
| 487 | RX_SUBBEG(rx) = INT2PTR(char*,*p++); |
| 488 | RX_SUBLEN(rx) = (I32)(*p++); |
| 489 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
| 490 | RX_OFFS(rx)[i].start = (I32)(*p++); |
| 491 | RX_OFFS(rx)[i].end = (I32)(*p++); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | S_rxres_free(pTHX_ void **rsp) |
| 497 | { |
| 498 | UV * const p = (UV*)*rsp; |
| 499 | |
| 500 | PERL_ARGS_ASSERT_RXRES_FREE; |
| 501 | PERL_UNUSED_CONTEXT; |
| 502 | |
| 503 | if (p) { |
| 504 | #ifdef PERL_POISON |
| 505 | void *tmp = INT2PTR(char*,*p); |
| 506 | Safefree(tmp); |
| 507 | if (*p) |
| 508 | PoisonFree(*p, 1, sizeof(*p)); |
| 509 | #else |
| 510 | Safefree(INT2PTR(char*,*p)); |
| 511 | #endif |
| 512 | #ifdef PERL_OLD_COPY_ON_WRITE |
| 513 | if (p[1]) { |
| 514 | SvREFCNT_dec (INT2PTR(SV*,p[1])); |
| 515 | } |
| 516 | #endif |
| 517 | Safefree(p); |
| 518 | *rsp = NULL; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | #define FORM_NUM_BLANK (1<<30) |
| 523 | #define FORM_NUM_POINT (1<<29) |
| 524 | |
| 525 | PP(pp_formline) |
| 526 | { |
| 527 | dVAR; dSP; dMARK; dORIGMARK; |
| 528 | register SV * const tmpForm = *++MARK; |
| 529 | SV *formsv; /* contains text of original format */ |
| 530 | register U32 *fpc; /* format ops program counter */ |
| 531 | register char *t; /* current append position in target string */ |
| 532 | const char *f; /* current position in format string */ |
| 533 | register I32 arg; |
| 534 | register SV *sv = NULL; /* current item */ |
| 535 | const char *item = NULL;/* string value of current item */ |
| 536 | I32 itemsize = 0; /* length of current item, possibly truncated */ |
| 537 | I32 fieldsize = 0; /* width of current field */ |
| 538 | I32 lines = 0; /* number of lines that have been output */ |
| 539 | bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */ |
| 540 | const char *chophere = NULL; /* where to chop current item */ |
| 541 | STRLEN linemark = 0; /* pos of start of line in output */ |
| 542 | NV value; |
| 543 | bool gotsome = FALSE; /* seen at least one non-blank item on this line */ |
| 544 | STRLEN len; |
| 545 | STRLEN linemax; /* estimate of output size in bytes */ |
| 546 | bool item_is_utf8 = FALSE; |
| 547 | bool targ_is_utf8 = FALSE; |
| 548 | const char *fmt; |
| 549 | MAGIC *mg = NULL; |
| 550 | U8 *source; /* source of bytes to append */ |
| 551 | STRLEN to_copy; /* how may bytes to append */ |
| 552 | char trans; /* what chars to translate */ |
| 553 | |
| 554 | mg = doparseform(tmpForm); |
| 555 | |
| 556 | fpc = (U32*)mg->mg_ptr; |
| 557 | /* the actual string the format was compiled from. |
| 558 | * with overload etc, this may not match tmpForm */ |
| 559 | formsv = mg->mg_obj; |
| 560 | |
| 561 | |
| 562 | SvPV_force(PL_formtarget, len); |
| 563 | if (SvTAINTED(tmpForm) || SvTAINTED(formsv)) |
| 564 | SvTAINTED_on(PL_formtarget); |
| 565 | if (DO_UTF8(PL_formtarget)) |
| 566 | targ_is_utf8 = TRUE; |
| 567 | linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1); |
| 568 | t = SvGROW(PL_formtarget, len + linemax + 1); |
| 569 | /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */ |
| 570 | t += len; |
| 571 | f = SvPV_const(formsv, len); |
| 572 | |
| 573 | for (;;) { |
| 574 | DEBUG_f( { |
| 575 | const char *name = "???"; |
| 576 | arg = -1; |
| 577 | switch (*fpc) { |
| 578 | case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break; |
| 579 | case FF_BLANK: arg = fpc[1]; name = "BLANK"; break; |
| 580 | case FF_SKIP: arg = fpc[1]; name = "SKIP"; break; |
| 581 | case FF_FETCH: arg = fpc[1]; name = "FETCH"; break; |
| 582 | case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break; |
| 583 | |
| 584 | case FF_CHECKNL: name = "CHECKNL"; break; |
| 585 | case FF_CHECKCHOP: name = "CHECKCHOP"; break; |
| 586 | case FF_SPACE: name = "SPACE"; break; |
| 587 | case FF_HALFSPACE: name = "HALFSPACE"; break; |
| 588 | case FF_ITEM: name = "ITEM"; break; |
| 589 | case FF_CHOP: name = "CHOP"; break; |
| 590 | case FF_LINEGLOB: name = "LINEGLOB"; break; |
| 591 | case FF_NEWLINE: name = "NEWLINE"; break; |
| 592 | case FF_MORE: name = "MORE"; break; |
| 593 | case FF_LINEMARK: name = "LINEMARK"; break; |
| 594 | case FF_END: name = "END"; break; |
| 595 | case FF_0DECIMAL: name = "0DECIMAL"; break; |
| 596 | case FF_LINESNGL: name = "LINESNGL"; break; |
| 597 | } |
| 598 | if (arg >= 0) |
| 599 | PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg); |
| 600 | else |
| 601 | PerlIO_printf(Perl_debug_log, "%-16s\n", name); |
| 602 | } ); |
| 603 | switch (*fpc++) { |
| 604 | case FF_LINEMARK: |
| 605 | linemark = t - SvPVX(PL_formtarget); |
| 606 | lines++; |
| 607 | gotsome = FALSE; |
| 608 | break; |
| 609 | |
| 610 | case FF_LITERAL: |
| 611 | to_copy = *fpc++; |
| 612 | source = (U8 *)f; |
| 613 | f += to_copy; |
| 614 | trans = '~'; |
| 615 | item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv); |
| 616 | goto append; |
| 617 | |
| 618 | case FF_SKIP: |
| 619 | f += *fpc++; |
| 620 | break; |
| 621 | |
| 622 | case FF_FETCH: |
| 623 | arg = *fpc++; |
| 624 | f += arg; |
| 625 | fieldsize = arg; |
| 626 | |
| 627 | if (MARK < SP) |
| 628 | sv = *++MARK; |
| 629 | else { |
| 630 | sv = &PL_sv_no; |
| 631 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments"); |
| 632 | } |
| 633 | if (SvTAINTED(sv)) |
| 634 | SvTAINTED_on(PL_formtarget); |
| 635 | break; |
| 636 | |
| 637 | case FF_CHECKNL: |
| 638 | { |
| 639 | const char *send; |
| 640 | const char *s = item = SvPV_const(sv, len); |
| 641 | itemsize = len; |
| 642 | if (DO_UTF8(sv)) { |
| 643 | itemsize = sv_len_utf8(sv); |
| 644 | if (itemsize != (I32)len) { |
| 645 | I32 itembytes; |
| 646 | if (itemsize > fieldsize) { |
| 647 | itemsize = fieldsize; |
| 648 | itembytes = itemsize; |
| 649 | sv_pos_u2b(sv, &itembytes, 0); |
| 650 | } |
| 651 | else |
| 652 | itembytes = len; |
| 653 | send = chophere = s + itembytes; |
| 654 | while (s < send) { |
| 655 | if (*s & ~31) |
| 656 | gotsome = TRUE; |
| 657 | else if (*s == '\n') |
| 658 | break; |
| 659 | s++; |
| 660 | } |
| 661 | item_is_utf8 = TRUE; |
| 662 | itemsize = s - item; |
| 663 | sv_pos_b2u(sv, &itemsize); |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | item_is_utf8 = FALSE; |
| 668 | if (itemsize > fieldsize) |
| 669 | itemsize = fieldsize; |
| 670 | send = chophere = s + itemsize; |
| 671 | while (s < send) { |
| 672 | if (*s & ~31) |
| 673 | gotsome = TRUE; |
| 674 | else if (*s == '\n') |
| 675 | break; |
| 676 | s++; |
| 677 | } |
| 678 | itemsize = s - item; |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | case FF_CHECKCHOP: |
| 683 | { |
| 684 | const char *s = item = SvPV_const(sv, len); |
| 685 | itemsize = len; |
| 686 | if (DO_UTF8(sv)) { |
| 687 | itemsize = sv_len_utf8(sv); |
| 688 | if (itemsize != (I32)len) { |
| 689 | I32 itembytes; |
| 690 | if (itemsize <= fieldsize) { |
| 691 | const char *send = chophere = s + itemsize; |
| 692 | while (s < send) { |
| 693 | if (*s == '\r') { |
| 694 | itemsize = s - item; |
| 695 | chophere = s; |
| 696 | break; |
| 697 | } |
| 698 | if (*s++ & ~31) |
| 699 | gotsome = TRUE; |
| 700 | } |
| 701 | } |
| 702 | else { |
| 703 | const char *send; |
| 704 | itemsize = fieldsize; |
| 705 | itembytes = itemsize; |
| 706 | sv_pos_u2b(sv, &itembytes, 0); |
| 707 | send = chophere = s + itembytes; |
| 708 | while (s < send || (s == send && isSPACE(*s))) { |
| 709 | if (isSPACE(*s)) { |
| 710 | if (chopspace) |
| 711 | chophere = s; |
| 712 | if (*s == '\r') |
| 713 | break; |
| 714 | } |
| 715 | else { |
| 716 | if (*s & ~31) |
| 717 | gotsome = TRUE; |
| 718 | if (strchr(PL_chopset, *s)) |
| 719 | chophere = s + 1; |
| 720 | } |
| 721 | s++; |
| 722 | } |
| 723 | itemsize = chophere - item; |
| 724 | sv_pos_b2u(sv, &itemsize); |
| 725 | } |
| 726 | item_is_utf8 = TRUE; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | item_is_utf8 = FALSE; |
| 731 | if (itemsize <= fieldsize) { |
| 732 | const char *const send = chophere = s + itemsize; |
| 733 | while (s < send) { |
| 734 | if (*s == '\r') { |
| 735 | itemsize = s - item; |
| 736 | chophere = s; |
| 737 | break; |
| 738 | } |
| 739 | if (*s++ & ~31) |
| 740 | gotsome = TRUE; |
| 741 | } |
| 742 | } |
| 743 | else { |
| 744 | const char *send; |
| 745 | itemsize = fieldsize; |
| 746 | send = chophere = s + itemsize; |
| 747 | while (s < send || (s == send && isSPACE(*s))) { |
| 748 | if (isSPACE(*s)) { |
| 749 | if (chopspace) |
| 750 | chophere = s; |
| 751 | if (*s == '\r') |
| 752 | break; |
| 753 | } |
| 754 | else { |
| 755 | if (*s & ~31) |
| 756 | gotsome = TRUE; |
| 757 | if (strchr(PL_chopset, *s)) |
| 758 | chophere = s + 1; |
| 759 | } |
| 760 | s++; |
| 761 | } |
| 762 | itemsize = chophere - item; |
| 763 | } |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | case FF_SPACE: |
| 768 | arg = fieldsize - itemsize; |
| 769 | if (arg) { |
| 770 | fieldsize -= arg; |
| 771 | while (arg-- > 0) |
| 772 | *t++ = ' '; |
| 773 | } |
| 774 | break; |
| 775 | |
| 776 | case FF_HALFSPACE: |
| 777 | arg = fieldsize - itemsize; |
| 778 | if (arg) { |
| 779 | arg /= 2; |
| 780 | fieldsize -= arg; |
| 781 | while (arg-- > 0) |
| 782 | *t++ = ' '; |
| 783 | } |
| 784 | break; |
| 785 | |
| 786 | case FF_ITEM: |
| 787 | to_copy = itemsize; |
| 788 | source = (U8 *)item; |
| 789 | trans = 1; |
| 790 | if (item_is_utf8) { |
| 791 | /* convert to_copy from chars to bytes */ |
| 792 | U8 *s = source; |
| 793 | while (to_copy--) |
| 794 | s += UTF8SKIP(s); |
| 795 | to_copy = s - source; |
| 796 | } |
| 797 | goto append; |
| 798 | |
| 799 | case FF_CHOP: |
| 800 | { |
| 801 | const char *s = chophere; |
| 802 | if (chopspace) { |
| 803 | while (isSPACE(*s)) |
| 804 | s++; |
| 805 | } |
| 806 | sv_chop(sv,s); |
| 807 | SvSETMAGIC(sv); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | case FF_LINESNGL: |
| 812 | chopspace = 0; |
| 813 | case FF_LINEGLOB: |
| 814 | { |
| 815 | const bool oneline = fpc[-1] == FF_LINESNGL; |
| 816 | const char *s = item = SvPV_const(sv, len); |
| 817 | const char *const send = s + len; |
| 818 | |
| 819 | item_is_utf8 = DO_UTF8(sv); |
| 820 | if (!len) |
| 821 | break; |
| 822 | trans = 0; |
| 823 | gotsome = TRUE; |
| 824 | chophere = s + len; |
| 825 | source = (U8 *) s; |
| 826 | to_copy = len; |
| 827 | while (s < send) { |
| 828 | if (*s++ == '\n') { |
| 829 | if (oneline) { |
| 830 | to_copy = s - SvPVX_const(sv) - 1; |
| 831 | chophere = s; |
| 832 | break; |
| 833 | } else { |
| 834 | if (s == send) { |
| 835 | to_copy--; |
| 836 | } else |
| 837 | lines++; |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | append: |
| 844 | /* append to_copy bytes from source to PL_formstring. |
| 845 | * item_is_utf8 implies source is utf8. |
| 846 | * if trans, translate certain characters during the copy */ |
| 847 | { |
| 848 | U8 *tmp = NULL; |
| 849 | STRLEN grow = 0; |
| 850 | |
| 851 | SvCUR_set(PL_formtarget, |
| 852 | t - SvPVX_const(PL_formtarget)); |
| 853 | |
| 854 | if (targ_is_utf8 && !item_is_utf8) { |
| 855 | source = tmp = bytes_to_utf8(source, &to_copy); |
| 856 | } else { |
| 857 | if (item_is_utf8 && !targ_is_utf8) { |
| 858 | U8 *s; |
| 859 | /* Upgrade targ to UTF8, and then we reduce it to |
| 860 | a problem we have a simple solution for. |
| 861 | Don't need get magic. */ |
| 862 | sv_utf8_upgrade_nomg(PL_formtarget); |
| 863 | targ_is_utf8 = TRUE; |
| 864 | /* re-calculate linemark */ |
| 865 | s = (U8*)SvPVX(PL_formtarget); |
| 866 | /* the bytes we initially allocated to append the |
| 867 | * whole line may have been gobbled up during the |
| 868 | * upgrade, so allocate a whole new line's worth |
| 869 | * for safety */ |
| 870 | grow = linemax; |
| 871 | while (linemark--) |
| 872 | s += UTF8SKIP(s); |
| 873 | linemark = s - (U8*)SvPVX(PL_formtarget); |
| 874 | } |
| 875 | /* Easy. They agree. */ |
| 876 | assert (item_is_utf8 == targ_is_utf8); |
| 877 | } |
| 878 | if (!trans) |
| 879 | /* @* and ^* are the only things that can exceed |
| 880 | * the linemax, so grow by the output size, plus |
| 881 | * a whole new form's worth in case of any further |
| 882 | * output */ |
| 883 | grow = linemax + to_copy; |
| 884 | if (grow) |
| 885 | SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1); |
| 886 | t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget); |
| 887 | |
| 888 | Copy(source, t, to_copy, char); |
| 889 | if (trans) { |
| 890 | /* blank out ~ or control chars, depending on trans. |
| 891 | * works on bytes not chars, so relies on not |
| 892 | * matching utf8 continuation bytes */ |
| 893 | U8 *s = (U8*)t; |
| 894 | U8 *send = s + to_copy; |
| 895 | while (s < send) { |
| 896 | const int ch = *s; |
| 897 | if (trans == '~' ? (ch == '~') : |
| 898 | #ifdef EBCDIC |
| 899 | iscntrl(ch) |
| 900 | #else |
| 901 | (!(ch & ~31)) |
| 902 | #endif |
| 903 | ) |
| 904 | *s = ' '; |
| 905 | s++; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | t += to_copy; |
| 910 | SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy); |
| 911 | if (tmp) |
| 912 | Safefree(tmp); |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | case FF_0DECIMAL: |
| 917 | arg = *fpc++; |
| 918 | #if defined(USE_LONG_DOUBLE) |
| 919 | fmt = (const char *) |
| 920 | ((arg & FORM_NUM_POINT) ? |
| 921 | "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl); |
| 922 | #else |
| 923 | fmt = (const char *) |
| 924 | ((arg & FORM_NUM_POINT) ? |
| 925 | "%#0*.*f" : "%0*.*f"); |
| 926 | #endif |
| 927 | goto ff_dec; |
| 928 | case FF_DECIMAL: |
| 929 | arg = *fpc++; |
| 930 | #if defined(USE_LONG_DOUBLE) |
| 931 | fmt = (const char *) |
| 932 | ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl); |
| 933 | #else |
| 934 | fmt = (const char *) |
| 935 | ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f"); |
| 936 | #endif |
| 937 | ff_dec: |
| 938 | /* If the field is marked with ^ and the value is undefined, |
| 939 | blank it out. */ |
| 940 | if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) { |
| 941 | arg = fieldsize; |
| 942 | while (arg--) |
| 943 | *t++ = ' '; |
| 944 | break; |
| 945 | } |
| 946 | gotsome = TRUE; |
| 947 | value = SvNV(sv); |
| 948 | /* overflow evidence */ |
| 949 | if (num_overflow(value, fieldsize, arg)) { |
| 950 | arg = fieldsize; |
| 951 | while (arg--) |
| 952 | *t++ = '#'; |
| 953 | break; |
| 954 | } |
| 955 | /* Formats aren't yet marked for locales, so assume "yes". */ |
| 956 | { |
| 957 | STORE_NUMERIC_STANDARD_SET_LOCAL(); |
| 958 | arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
| 959 | my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value); |
| 960 | RESTORE_NUMERIC_STANDARD(); |
| 961 | } |
| 962 | t += fieldsize; |
| 963 | break; |
| 964 | |
| 965 | case FF_NEWLINE: |
| 966 | f++; |
| 967 | while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ; |
| 968 | t++; |
| 969 | *t++ = '\n'; |
| 970 | break; |
| 971 | |
| 972 | case FF_BLANK: |
| 973 | arg = *fpc++; |
| 974 | if (gotsome) { |
| 975 | if (arg) { /* repeat until fields exhausted? */ |
| 976 | fpc--; |
| 977 | goto end; |
| 978 | } |
| 979 | } |
| 980 | else { |
| 981 | t = SvPVX(PL_formtarget) + linemark; |
| 982 | lines--; |
| 983 | } |
| 984 | break; |
| 985 | |
| 986 | case FF_MORE: |
| 987 | { |
| 988 | const char *s = chophere; |
| 989 | const char *send = item + len; |
| 990 | if (chopspace) { |
| 991 | while (isSPACE(*s) && (s < send)) |
| 992 | s++; |
| 993 | } |
| 994 | if (s < send) { |
| 995 | char *s1; |
| 996 | arg = fieldsize - itemsize; |
| 997 | if (arg) { |
| 998 | fieldsize -= arg; |
| 999 | while (arg-- > 0) |
| 1000 | *t++ = ' '; |
| 1001 | } |
| 1002 | s1 = t - 3; |
| 1003 | if (strnEQ(s1," ",3)) { |
| 1004 | while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1])) |
| 1005 | s1--; |
| 1006 | } |
| 1007 | *s1++ = '.'; |
| 1008 | *s1++ = '.'; |
| 1009 | *s1++ = '.'; |
| 1010 | } |
| 1011 | break; |
| 1012 | } |
| 1013 | case FF_END: |
| 1014 | end: |
| 1015 | assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget)); |
| 1016 | *t = '\0'; |
| 1017 | SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget)); |
| 1018 | if (targ_is_utf8) |
| 1019 | SvUTF8_on(PL_formtarget); |
| 1020 | FmLINES(PL_formtarget) += lines; |
| 1021 | SP = ORIGMARK; |
| 1022 | if (fpc[-1] == FF_BLANK) |
| 1023 | RETURNOP(cLISTOP->op_first); |
| 1024 | else |
| 1025 | RETPUSHYES; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | PP(pp_grepstart) |
| 1031 | { |
| 1032 | dVAR; dSP; |
| 1033 | SV *src; |
| 1034 | |
| 1035 | if (PL_stack_base + *PL_markstack_ptr == SP) { |
| 1036 | (void)POPMARK; |
| 1037 | if (GIMME_V == G_SCALAR) |
| 1038 | mXPUSHi(0); |
| 1039 | RETURNOP(PL_op->op_next->op_next); |
| 1040 | } |
| 1041 | PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1; |
| 1042 | Perl_pp_pushmark(aTHX); /* push dst */ |
| 1043 | Perl_pp_pushmark(aTHX); /* push src */ |
| 1044 | ENTER_with_name("grep"); /* enter outer scope */ |
| 1045 | |
| 1046 | SAVETMPS; |
| 1047 | if (PL_op->op_private & OPpGREP_LEX) |
| 1048 | SAVESPTR(PAD_SVl(PL_op->op_targ)); |
| 1049 | else |
| 1050 | SAVE_DEFSV; |
| 1051 | ENTER_with_name("grep_item"); /* enter inner scope */ |
| 1052 | SAVEVPTR(PL_curpm); |
| 1053 | |
| 1054 | src = PL_stack_base[*PL_markstack_ptr]; |
| 1055 | SvTEMP_off(src); |
| 1056 | if (PL_op->op_private & OPpGREP_LEX) |
| 1057 | PAD_SVl(PL_op->op_targ) = src; |
| 1058 | else |
| 1059 | DEFSV_set(src); |
| 1060 | |
| 1061 | PUTBACK; |
| 1062 | if (PL_op->op_type == OP_MAPSTART) |
| 1063 | Perl_pp_pushmark(aTHX); /* push top */ |
| 1064 | return ((LOGOP*)PL_op->op_next)->op_other; |
| 1065 | } |
| 1066 | |
| 1067 | PP(pp_mapwhile) |
| 1068 | { |
| 1069 | dVAR; dSP; |
| 1070 | const I32 gimme = GIMME_V; |
| 1071 | I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */ |
| 1072 | I32 count; |
| 1073 | I32 shift; |
| 1074 | SV** src; |
| 1075 | SV** dst; |
| 1076 | |
| 1077 | /* first, move source pointer to the next item in the source list */ |
| 1078 | ++PL_markstack_ptr[-1]; |
| 1079 | |
| 1080 | /* if there are new items, push them into the destination list */ |
| 1081 | if (items && gimme != G_VOID) { |
| 1082 | /* might need to make room back there first */ |
| 1083 | if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) { |
| 1084 | /* XXX this implementation is very pessimal because the stack |
| 1085 | * is repeatedly extended for every set of items. Is possible |
| 1086 | * to do this without any stack extension or copying at all |
| 1087 | * by maintaining a separate list over which the map iterates |
| 1088 | * (like foreach does). --gsar */ |
| 1089 | |
| 1090 | /* everything in the stack after the destination list moves |
| 1091 | * towards the end the stack by the amount of room needed */ |
| 1092 | shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]); |
| 1093 | |
| 1094 | /* items to shift up (accounting for the moved source pointer) */ |
| 1095 | count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1); |
| 1096 | |
| 1097 | /* This optimization is by Ben Tilly and it does |
| 1098 | * things differently from what Sarathy (gsar) |
| 1099 | * is describing. The downside of this optimization is |
| 1100 | * that leaves "holes" (uninitialized and hopefully unused areas) |
| 1101 | * to the Perl stack, but on the other hand this |
| 1102 | * shouldn't be a problem. If Sarathy's idea gets |
| 1103 | * implemented, this optimization should become |
| 1104 | * irrelevant. --jhi */ |
| 1105 | if (shift < count) |
| 1106 | shift = count; /* Avoid shifting too often --Ben Tilly */ |
| 1107 | |
| 1108 | EXTEND(SP,shift); |
| 1109 | src = SP; |
| 1110 | dst = (SP += shift); |
| 1111 | PL_markstack_ptr[-1] += shift; |
| 1112 | *PL_markstack_ptr += shift; |
| 1113 | while (count--) |
| 1114 | *dst-- = *src--; |
| 1115 | } |
| 1116 | /* copy the new items down to the destination list */ |
| 1117 | dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1; |
| 1118 | if (gimme == G_ARRAY) { |
| 1119 | /* add returned items to the collection (making mortal copies |
| 1120 | * if necessary), then clear the current temps stack frame |
| 1121 | * *except* for those items. We do this splicing the items |
| 1122 | * into the start of the tmps frame (so some items may be on |
| 1123 | * the tmps stack twice), then moving PL_tmps_floor above |
| 1124 | * them, then freeing the frame. That way, the only tmps that |
| 1125 | * accumulate over iterations are the return values for map. |
| 1126 | * We have to do to this way so that everything gets correctly |
| 1127 | * freed if we die during the map. |
| 1128 | */ |
| 1129 | I32 tmpsbase; |
| 1130 | I32 i = items; |
| 1131 | /* make space for the slice */ |
| 1132 | EXTEND_MORTAL(items); |
| 1133 | tmpsbase = PL_tmps_floor + 1; |
| 1134 | Move(PL_tmps_stack + tmpsbase, |
| 1135 | PL_tmps_stack + tmpsbase + items, |
| 1136 | PL_tmps_ix - PL_tmps_floor, |
| 1137 | SV*); |
| 1138 | PL_tmps_ix += items; |
| 1139 | |
| 1140 | while (i-- > 0) { |
| 1141 | SV *sv = POPs; |
| 1142 | if (!SvTEMP(sv)) |
| 1143 | sv = sv_mortalcopy(sv); |
| 1144 | *dst-- = sv; |
| 1145 | PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv); |
| 1146 | } |
| 1147 | /* clear the stack frame except for the items */ |
| 1148 | PL_tmps_floor += items; |
| 1149 | FREETMPS; |
| 1150 | /* FREETMPS may have cleared the TEMP flag on some of the items */ |
| 1151 | i = items; |
| 1152 | while (i-- > 0) |
| 1153 | SvTEMP_on(PL_tmps_stack[--tmpsbase]); |
| 1154 | } |
| 1155 | else { |
| 1156 | /* scalar context: we don't care about which values map returns |
| 1157 | * (we use undef here). And so we certainly don't want to do mortal |
| 1158 | * copies of meaningless values. */ |
| 1159 | while (items-- > 0) { |
| 1160 | (void)POPs; |
| 1161 | *dst-- = &PL_sv_undef; |
| 1162 | } |
| 1163 | FREETMPS; |
| 1164 | } |
| 1165 | } |
| 1166 | else { |
| 1167 | FREETMPS; |
| 1168 | } |
| 1169 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
| 1170 | |
| 1171 | /* All done yet? */ |
| 1172 | if (PL_markstack_ptr[-1] > *PL_markstack_ptr) { |
| 1173 | |
| 1174 | (void)POPMARK; /* pop top */ |
| 1175 | LEAVE_with_name("grep"); /* exit outer scope */ |
| 1176 | (void)POPMARK; /* pop src */ |
| 1177 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; |
| 1178 | (void)POPMARK; /* pop dst */ |
| 1179 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
| 1180 | if (gimme == G_SCALAR) { |
| 1181 | if (PL_op->op_private & OPpGREP_LEX) { |
| 1182 | SV* sv = sv_newmortal(); |
| 1183 | sv_setiv(sv, items); |
| 1184 | PUSHs(sv); |
| 1185 | } |
| 1186 | else { |
| 1187 | dTARGET; |
| 1188 | XPUSHi(items); |
| 1189 | } |
| 1190 | } |
| 1191 | else if (gimme == G_ARRAY) |
| 1192 | SP += items; |
| 1193 | RETURN; |
| 1194 | } |
| 1195 | else { |
| 1196 | SV *src; |
| 1197 | |
| 1198 | ENTER_with_name("grep_item"); /* enter inner scope */ |
| 1199 | SAVEVPTR(PL_curpm); |
| 1200 | |
| 1201 | /* set $_ to the new source item */ |
| 1202 | src = PL_stack_base[PL_markstack_ptr[-1]]; |
| 1203 | SvTEMP_off(src); |
| 1204 | if (PL_op->op_private & OPpGREP_LEX) |
| 1205 | PAD_SVl(PL_op->op_targ) = src; |
| 1206 | else |
| 1207 | DEFSV_set(src); |
| 1208 | |
| 1209 | RETURNOP(cLOGOP->op_other); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /* Range stuff. */ |
| 1214 | |
| 1215 | PP(pp_range) |
| 1216 | { |
| 1217 | dVAR; |
| 1218 | if (GIMME == G_ARRAY) |
| 1219 | return NORMAL; |
| 1220 | if (SvTRUEx(PAD_SV(PL_op->op_targ))) |
| 1221 | return cLOGOP->op_other; |
| 1222 | else |
| 1223 | return NORMAL; |
| 1224 | } |
| 1225 | |
| 1226 | PP(pp_flip) |
| 1227 | { |
| 1228 | dVAR; |
| 1229 | dSP; |
| 1230 | |
| 1231 | if (GIMME == G_ARRAY) { |
| 1232 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
| 1233 | } |
| 1234 | else { |
| 1235 | dTOPss; |
| 1236 | SV * const targ = PAD_SV(PL_op->op_targ); |
| 1237 | int flip = 0; |
| 1238 | |
| 1239 | if (PL_op->op_private & OPpFLIP_LINENUM) { |
| 1240 | if (GvIO(PL_last_in_gv)) { |
| 1241 | flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); |
| 1242 | } |
| 1243 | else { |
| 1244 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
| 1245 | if (gv && GvSV(gv)) |
| 1246 | flip = SvIV(sv) == SvIV(GvSV(gv)); |
| 1247 | } |
| 1248 | } else { |
| 1249 | flip = SvTRUE(sv); |
| 1250 | } |
| 1251 | if (flip) { |
| 1252 | sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1); |
| 1253 | if (PL_op->op_flags & OPf_SPECIAL) { |
| 1254 | sv_setiv(targ, 1); |
| 1255 | SETs(targ); |
| 1256 | RETURN; |
| 1257 | } |
| 1258 | else { |
| 1259 | sv_setiv(targ, 0); |
| 1260 | SP--; |
| 1261 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
| 1262 | } |
| 1263 | } |
| 1264 | sv_setpvs(TARG, ""); |
| 1265 | SETs(targ); |
| 1266 | RETURN; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* This code tries to decide if "$left .. $right" should use the |
| 1271 | magical string increment, or if the range is numeric (we make |
| 1272 | an exception for .."0" [#18165]). AMS 20021031. */ |
| 1273 | |
| 1274 | #define RANGE_IS_NUMERIC(left,right) ( \ |
| 1275 | SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \ |
| 1276 | SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \ |
| 1277 | (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \ |
| 1278 | looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \ |
| 1279 | && (!SvOK(right) || looks_like_number(right)))) |
| 1280 | |
| 1281 | PP(pp_flop) |
| 1282 | { |
| 1283 | dVAR; dSP; |
| 1284 | |
| 1285 | if (GIMME == G_ARRAY) { |
| 1286 | dPOPPOPssrl; |
| 1287 | |
| 1288 | SvGETMAGIC(left); |
| 1289 | SvGETMAGIC(right); |
| 1290 | |
| 1291 | if (RANGE_IS_NUMERIC(left,right)) { |
| 1292 | register IV i, j; |
| 1293 | IV max; |
| 1294 | if ((SvOK(left) && SvNV(left) < IV_MIN) || |
| 1295 | (SvOK(right) && SvNV(right) > IV_MAX)) |
| 1296 | DIE(aTHX_ "Range iterator outside integer range"); |
| 1297 | i = SvIV(left); |
| 1298 | max = SvIV(right); |
| 1299 | if (max >= i) { |
| 1300 | j = max - i + 1; |
| 1301 | EXTEND_MORTAL(j); |
| 1302 | EXTEND(SP, j); |
| 1303 | } |
| 1304 | else |
| 1305 | j = 0; |
| 1306 | while (j--) { |
| 1307 | SV * const sv = sv_2mortal(newSViv(i++)); |
| 1308 | PUSHs(sv); |
| 1309 | } |
| 1310 | } |
| 1311 | else { |
| 1312 | SV * const final = sv_mortalcopy(right); |
| 1313 | STRLEN len; |
| 1314 | const char * const tmps = SvPV_const(final, len); |
| 1315 | |
| 1316 | SV *sv = sv_mortalcopy(left); |
| 1317 | SvPV_force_nolen(sv); |
| 1318 | while (!SvNIOKp(sv) && SvCUR(sv) <= len) { |
| 1319 | XPUSHs(sv); |
| 1320 | if (strEQ(SvPVX_const(sv),tmps)) |
| 1321 | break; |
| 1322 | sv = sv_2mortal(newSVsv(sv)); |
| 1323 | sv_inc(sv); |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | else { |
| 1328 | dTOPss; |
| 1329 | SV * const targ = PAD_SV(cUNOP->op_first->op_targ); |
| 1330 | int flop = 0; |
| 1331 | sv_inc(targ); |
| 1332 | |
| 1333 | if (PL_op->op_private & OPpFLIP_LINENUM) { |
| 1334 | if (GvIO(PL_last_in_gv)) { |
| 1335 | flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); |
| 1336 | } |
| 1337 | else { |
| 1338 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
| 1339 | if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv)); |
| 1340 | } |
| 1341 | } |
| 1342 | else { |
| 1343 | flop = SvTRUE(sv); |
| 1344 | } |
| 1345 | |
| 1346 | if (flop) { |
| 1347 | sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0); |
| 1348 | sv_catpvs(targ, "E0"); |
| 1349 | } |
| 1350 | SETs(targ); |
| 1351 | } |
| 1352 | |
| 1353 | RETURN; |
| 1354 | } |
| 1355 | |
| 1356 | /* Control. */ |
| 1357 | |
| 1358 | static const char * const context_name[] = { |
| 1359 | "pseudo-block", |
| 1360 | NULL, /* CXt_WHEN never actually needs "block" */ |
| 1361 | NULL, /* CXt_BLOCK never actually needs "block" */ |
| 1362 | NULL, /* CXt_GIVEN never actually needs "block" */ |
| 1363 | NULL, /* CXt_LOOP_FOR never actually needs "loop" */ |
| 1364 | NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */ |
| 1365 | NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */ |
| 1366 | NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */ |
| 1367 | "subroutine", |
| 1368 | "format", |
| 1369 | "eval", |
| 1370 | "substitution", |
| 1371 | }; |
| 1372 | |
| 1373 | STATIC I32 |
| 1374 | S_dopoptolabel(pTHX_ const char *label) |
| 1375 | { |
| 1376 | dVAR; |
| 1377 | register I32 i; |
| 1378 | |
| 1379 | PERL_ARGS_ASSERT_DOPOPTOLABEL; |
| 1380 | |
| 1381 | for (i = cxstack_ix; i >= 0; i--) { |
| 1382 | register const PERL_CONTEXT * const cx = &cxstack[i]; |
| 1383 | switch (CxTYPE(cx)) { |
| 1384 | case CXt_SUBST: |
| 1385 | case CXt_SUB: |
| 1386 | case CXt_FORMAT: |
| 1387 | case CXt_EVAL: |
| 1388 | case CXt_NULL: |
| 1389 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
| 1390 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); |
| 1391 | if (CxTYPE(cx) == CXt_NULL) |
| 1392 | return -1; |
| 1393 | break; |
| 1394 | case CXt_LOOP_LAZYIV: |
| 1395 | case CXt_LOOP_LAZYSV: |
| 1396 | case CXt_LOOP_FOR: |
| 1397 | case CXt_LOOP_PLAIN: |
| 1398 | { |
| 1399 | const char *cx_label = CxLABEL(cx); |
| 1400 | if (!cx_label || strNE(label, cx_label) ) { |
| 1401 | DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n", |
| 1402 | (long)i, cx_label)); |
| 1403 | continue; |
| 1404 | } |
| 1405 | DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label)); |
| 1406 | return i; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | return i; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | I32 |
| 1416 | Perl_dowantarray(pTHX) |
| 1417 | { |
| 1418 | dVAR; |
| 1419 | const I32 gimme = block_gimme(); |
| 1420 | return (gimme == G_VOID) ? G_SCALAR : gimme; |
| 1421 | } |
| 1422 | |
| 1423 | I32 |
| 1424 | Perl_block_gimme(pTHX) |
| 1425 | { |
| 1426 | dVAR; |
| 1427 | const I32 cxix = dopoptosub(cxstack_ix); |
| 1428 | if (cxix < 0) |
| 1429 | return G_VOID; |
| 1430 | |
| 1431 | switch (cxstack[cxix].blk_gimme) { |
| 1432 | case G_VOID: |
| 1433 | return G_VOID; |
| 1434 | case G_SCALAR: |
| 1435 | return G_SCALAR; |
| 1436 | case G_ARRAY: |
| 1437 | return G_ARRAY; |
| 1438 | default: |
| 1439 | Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme); |
| 1440 | /* NOTREACHED */ |
| 1441 | return 0; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | I32 |
| 1446 | Perl_is_lvalue_sub(pTHX) |
| 1447 | { |
| 1448 | dVAR; |
| 1449 | const I32 cxix = dopoptosub(cxstack_ix); |
| 1450 | assert(cxix >= 0); /* We should only be called from inside subs */ |
| 1451 | |
| 1452 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) |
| 1453 | return CxLVAL(cxstack + cxix); |
| 1454 | else |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | STATIC I32 |
| 1459 | S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock) |
| 1460 | { |
| 1461 | dVAR; |
| 1462 | I32 i; |
| 1463 | |
| 1464 | PERL_ARGS_ASSERT_DOPOPTOSUB_AT; |
| 1465 | |
| 1466 | for (i = startingblock; i >= 0; i--) { |
| 1467 | register const PERL_CONTEXT * const cx = &cxstk[i]; |
| 1468 | switch (CxTYPE(cx)) { |
| 1469 | default: |
| 1470 | continue; |
| 1471 | case CXt_EVAL: |
| 1472 | case CXt_SUB: |
| 1473 | case CXt_FORMAT: |
| 1474 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); |
| 1475 | return i; |
| 1476 | } |
| 1477 | } |
| 1478 | return i; |
| 1479 | } |
| 1480 | |
| 1481 | STATIC I32 |
| 1482 | S_dopoptoeval(pTHX_ I32 startingblock) |
| 1483 | { |
| 1484 | dVAR; |
| 1485 | I32 i; |
| 1486 | for (i = startingblock; i >= 0; i--) { |
| 1487 | register const PERL_CONTEXT *cx = &cxstack[i]; |
| 1488 | switch (CxTYPE(cx)) { |
| 1489 | default: |
| 1490 | continue; |
| 1491 | case CXt_EVAL: |
| 1492 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i)); |
| 1493 | return i; |
| 1494 | } |
| 1495 | } |
| 1496 | return i; |
| 1497 | } |
| 1498 | |
| 1499 | STATIC I32 |
| 1500 | S_dopoptoloop(pTHX_ I32 startingblock) |
| 1501 | { |
| 1502 | dVAR; |
| 1503 | I32 i; |
| 1504 | for (i = startingblock; i >= 0; i--) { |
| 1505 | register const PERL_CONTEXT * const cx = &cxstack[i]; |
| 1506 | switch (CxTYPE(cx)) { |
| 1507 | case CXt_SUBST: |
| 1508 | case CXt_SUB: |
| 1509 | case CXt_FORMAT: |
| 1510 | case CXt_EVAL: |
| 1511 | case CXt_NULL: |
| 1512 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
| 1513 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); |
| 1514 | if ((CxTYPE(cx)) == CXt_NULL) |
| 1515 | return -1; |
| 1516 | break; |
| 1517 | case CXt_LOOP_LAZYIV: |
| 1518 | case CXt_LOOP_LAZYSV: |
| 1519 | case CXt_LOOP_FOR: |
| 1520 | case CXt_LOOP_PLAIN: |
| 1521 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i)); |
| 1522 | return i; |
| 1523 | } |
| 1524 | } |
| 1525 | return i; |
| 1526 | } |
| 1527 | |
| 1528 | STATIC I32 |
| 1529 | S_dopoptogiven(pTHX_ I32 startingblock) |
| 1530 | { |
| 1531 | dVAR; |
| 1532 | I32 i; |
| 1533 | for (i = startingblock; i >= 0; i--) { |
| 1534 | register const PERL_CONTEXT *cx = &cxstack[i]; |
| 1535 | switch (CxTYPE(cx)) { |
| 1536 | default: |
| 1537 | continue; |
| 1538 | case CXt_GIVEN: |
| 1539 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i)); |
| 1540 | return i; |
| 1541 | case CXt_LOOP_PLAIN: |
| 1542 | assert(!CxFOREACHDEF(cx)); |
| 1543 | break; |
| 1544 | case CXt_LOOP_LAZYIV: |
| 1545 | case CXt_LOOP_LAZYSV: |
| 1546 | case CXt_LOOP_FOR: |
| 1547 | if (CxFOREACHDEF(cx)) { |
| 1548 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i)); |
| 1549 | return i; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | return i; |
| 1554 | } |
| 1555 | |
| 1556 | STATIC I32 |
| 1557 | S_dopoptowhen(pTHX_ I32 startingblock) |
| 1558 | { |
| 1559 | dVAR; |
| 1560 | I32 i; |
| 1561 | for (i = startingblock; i >= 0; i--) { |
| 1562 | register const PERL_CONTEXT *cx = &cxstack[i]; |
| 1563 | switch (CxTYPE(cx)) { |
| 1564 | default: |
| 1565 | continue; |
| 1566 | case CXt_WHEN: |
| 1567 | DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i)); |
| 1568 | return i; |
| 1569 | } |
| 1570 | } |
| 1571 | return i; |
| 1572 | } |
| 1573 | |
| 1574 | void |
| 1575 | Perl_dounwind(pTHX_ I32 cxix) |
| 1576 | { |
| 1577 | dVAR; |
| 1578 | I32 optype; |
| 1579 | |
| 1580 | while (cxstack_ix > cxix) { |
| 1581 | SV *sv; |
| 1582 | register PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
| 1583 | DEBUG_CX("UNWIND"); \ |
| 1584 | /* Note: we don't need to restore the base context info till the end. */ |
| 1585 | switch (CxTYPE(cx)) { |
| 1586 | case CXt_SUBST: |
| 1587 | POPSUBST(cx); |
| 1588 | continue; /* not break */ |
| 1589 | case CXt_SUB: |
| 1590 | POPSUB(cx,sv); |
| 1591 | LEAVESUB(sv); |
| 1592 | break; |
| 1593 | case CXt_EVAL: |
| 1594 | POPEVAL(cx); |
| 1595 | break; |
| 1596 | case CXt_LOOP_LAZYIV: |
| 1597 | case CXt_LOOP_LAZYSV: |
| 1598 | case CXt_LOOP_FOR: |
| 1599 | case CXt_LOOP_PLAIN: |
| 1600 | POPLOOP(cx); |
| 1601 | break; |
| 1602 | case CXt_NULL: |
| 1603 | break; |
| 1604 | case CXt_FORMAT: |
| 1605 | POPFORMAT(cx); |
| 1606 | break; |
| 1607 | } |
| 1608 | cxstack_ix--; |
| 1609 | } |
| 1610 | PERL_UNUSED_VAR(optype); |
| 1611 | } |
| 1612 | |
| 1613 | void |
| 1614 | Perl_qerror(pTHX_ SV *err) |
| 1615 | { |
| 1616 | dVAR; |
| 1617 | |
| 1618 | PERL_ARGS_ASSERT_QERROR; |
| 1619 | |
| 1620 | if (PL_in_eval) { |
| 1621 | if (PL_in_eval & EVAL_KEEPERR) { |
| 1622 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s", |
| 1623 | SvPV_nolen_const(err)); |
| 1624 | } |
| 1625 | else |
| 1626 | sv_catsv(ERRSV, err); |
| 1627 | } |
| 1628 | else if (PL_errors) |
| 1629 | sv_catsv(PL_errors, err); |
| 1630 | else |
| 1631 | Perl_warn(aTHX_ "%"SVf, SVfARG(err)); |
| 1632 | if (PL_parser) |
| 1633 | ++PL_parser->error_count; |
| 1634 | } |
| 1635 | |
| 1636 | void |
| 1637 | Perl_die_unwind(pTHX_ SV *msv) |
| 1638 | { |
| 1639 | dVAR; |
| 1640 | SV *exceptsv = sv_mortalcopy(msv); |
| 1641 | U8 in_eval = PL_in_eval; |
| 1642 | PERL_ARGS_ASSERT_DIE_UNWIND; |
| 1643 | |
| 1644 | if (in_eval) { |
| 1645 | I32 cxix; |
| 1646 | I32 gimme; |
| 1647 | |
| 1648 | /* |
| 1649 | * Historically, perl used to set ERRSV ($@) early in the die |
| 1650 | * process and rely on it not getting clobbered during unwinding. |
| 1651 | * That sucked, because it was liable to get clobbered, so the |
| 1652 | * setting of ERRSV used to emit the exception from eval{} has |
| 1653 | * been moved to much later, after unwinding (see just before |
| 1654 | * JMPENV_JUMP below). However, some modules were relying on the |
| 1655 | * early setting, by examining $@ during unwinding to use it as |
| 1656 | * a flag indicating whether the current unwinding was caused by |
| 1657 | * an exception. It was never a reliable flag for that purpose, |
| 1658 | * being totally open to false positives even without actual |
| 1659 | * clobberage, but was useful enough for production code to |
| 1660 | * semantically rely on it. |
| 1661 | * |
| 1662 | * We'd like to have a proper introspective interface that |
| 1663 | * explicitly describes the reason for whatever unwinding |
| 1664 | * operations are currently in progress, so that those modules |
| 1665 | * work reliably and $@ isn't further overloaded. But we don't |
| 1666 | * have one yet. In its absence, as a stopgap measure, ERRSV is |
| 1667 | * now *additionally* set here, before unwinding, to serve as the |
| 1668 | * (unreliable) flag that it used to. |
| 1669 | * |
| 1670 | * This behaviour is temporary, and should be removed when a |
| 1671 | * proper way to detect exceptional unwinding has been developed. |
| 1672 | * As of 2010-12, the authors of modules relying on the hack |
| 1673 | * are aware of the issue, because the modules failed on |
| 1674 | * perls 5.13.{1..7} which had late setting of $@ without this |
| 1675 | * early-setting hack. |
| 1676 | */ |
| 1677 | if (!(in_eval & EVAL_KEEPERR)) { |
| 1678 | SvTEMP_off(exceptsv); |
| 1679 | sv_setsv(ERRSV, exceptsv); |
| 1680 | } |
| 1681 | |
| 1682 | while ((cxix = dopoptoeval(cxstack_ix)) < 0 |
| 1683 | && PL_curstackinfo->si_prev) |
| 1684 | { |
| 1685 | dounwind(-1); |
| 1686 | POPSTACK; |
| 1687 | } |
| 1688 | |
| 1689 | if (cxix >= 0) { |
| 1690 | I32 optype; |
| 1691 | SV *namesv; |
| 1692 | register PERL_CONTEXT *cx; |
| 1693 | SV **newsp; |
| 1694 | COP *oldcop; |
| 1695 | JMPENV *restartjmpenv; |
| 1696 | OP *restartop; |
| 1697 | |
| 1698 | if (cxix < cxstack_ix) |
| 1699 | dounwind(cxix); |
| 1700 | |
| 1701 | POPBLOCK(cx,PL_curpm); |
| 1702 | if (CxTYPE(cx) != CXt_EVAL) { |
| 1703 | STRLEN msglen; |
| 1704 | const char* message = SvPVx_const(exceptsv, msglen); |
| 1705 | PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11); |
| 1706 | PerlIO_write(Perl_error_log, message, msglen); |
| 1707 | my_exit(1); |
| 1708 | } |
| 1709 | POPEVAL(cx); |
| 1710 | namesv = cx->blk_eval.old_namesv; |
| 1711 | oldcop = cx->blk_oldcop; |
| 1712 | restartjmpenv = cx->blk_eval.cur_top_env; |
| 1713 | restartop = cx->blk_eval.retop; |
| 1714 | |
| 1715 | if (gimme == G_SCALAR) |
| 1716 | *++newsp = &PL_sv_undef; |
| 1717 | PL_stack_sp = newsp; |
| 1718 | |
| 1719 | LEAVE; |
| 1720 | |
| 1721 | /* LEAVE could clobber PL_curcop (see save_re_context()) |
| 1722 | * XXX it might be better to find a way to avoid messing with |
| 1723 | * PL_curcop in save_re_context() instead, but this is a more |
| 1724 | * minimal fix --GSAR */ |
| 1725 | PL_curcop = oldcop; |
| 1726 | |
| 1727 | if (optype == OP_REQUIRE) { |
| 1728 | const char* const msg = SvPVx_nolen_const(exceptsv); |
| 1729 | (void)hv_store(GvHVn(PL_incgv), |
| 1730 | SvPVX_const(namesv), SvCUR(namesv), |
| 1731 | &PL_sv_undef, 0); |
| 1732 | /* note that unlike pp_entereval, pp_require isn't |
| 1733 | * supposed to trap errors. So now that we've popped the |
| 1734 | * EVAL that pp_require pushed, and processed the error |
| 1735 | * message, rethrow the error */ |
| 1736 | Perl_croak(aTHX_ "%sCompilation failed in require", |
| 1737 | *msg ? msg : "Unknown error\n"); |
| 1738 | } |
| 1739 | if (in_eval & EVAL_KEEPERR) { |
| 1740 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s", |
| 1741 | SvPV_nolen_const(exceptsv)); |
| 1742 | } |
| 1743 | else { |
| 1744 | sv_setsv(ERRSV, exceptsv); |
| 1745 | } |
| 1746 | PL_restartjmpenv = restartjmpenv; |
| 1747 | PL_restartop = restartop; |
| 1748 | JMPENV_JUMP(3); |
| 1749 | /* NOTREACHED */ |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | write_to_stderr(exceptsv); |
| 1754 | my_failure_exit(); |
| 1755 | /* NOTREACHED */ |
| 1756 | } |
| 1757 | |
| 1758 | PP(pp_xor) |
| 1759 | { |
| 1760 | dVAR; dSP; dPOPTOPssrl; |
| 1761 | if (SvTRUE(left) != SvTRUE(right)) |
| 1762 | RETSETYES; |
| 1763 | else |
| 1764 | RETSETNO; |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | =for apidoc caller_cx |
| 1769 | |
| 1770 | The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The |
| 1771 | returned C<PERL_CONTEXT> structure can be interrogated to find all the |
| 1772 | information returned to Perl by C<caller>. Note that XSUBs don't get a |
| 1773 | stack frame, so C<caller_cx(0, NULL)> will return information for the |
| 1774 | immediately-surrounding Perl code. |
| 1775 | |
| 1776 | This function skips over the automatic calls to C<&DB::sub> made on the |
| 1777 | behalf of the debugger. If the stack frame requested was a sub called by |
| 1778 | C<DB::sub>, the return value will be the frame for the call to |
| 1779 | C<DB::sub>, since that has the correct line number/etc. for the call |
| 1780 | site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the |
| 1781 | frame for the sub call itself. |
| 1782 | |
| 1783 | =cut |
| 1784 | */ |
| 1785 | |
| 1786 | const PERL_CONTEXT * |
| 1787 | Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp) |
| 1788 | { |
| 1789 | register I32 cxix = dopoptosub(cxstack_ix); |
| 1790 | register const PERL_CONTEXT *cx; |
| 1791 | register const PERL_CONTEXT *ccstack = cxstack; |
| 1792 | const PERL_SI *top_si = PL_curstackinfo; |
| 1793 | |
| 1794 | for (;;) { |
| 1795 | /* we may be in a higher stacklevel, so dig down deeper */ |
| 1796 | while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { |
| 1797 | top_si = top_si->si_prev; |
| 1798 | ccstack = top_si->si_cxstack; |
| 1799 | cxix = dopoptosub_at(ccstack, top_si->si_cxix); |
| 1800 | } |
| 1801 | if (cxix < 0) |
| 1802 | return NULL; |
| 1803 | /* caller() should not report the automatic calls to &DB::sub */ |
| 1804 | if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 && |
| 1805 | ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) |
| 1806 | count++; |
| 1807 | if (!count--) |
| 1808 | break; |
| 1809 | cxix = dopoptosub_at(ccstack, cxix - 1); |
| 1810 | } |
| 1811 | |
| 1812 | cx = &ccstack[cxix]; |
| 1813 | if (dbcxp) *dbcxp = cx; |
| 1814 | |
| 1815 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
| 1816 | const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1); |
| 1817 | /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the |
| 1818 | field below is defined for any cx. */ |
| 1819 | /* caller() should not report the automatic calls to &DB::sub */ |
| 1820 | if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) |
| 1821 | cx = &ccstack[dbcxix]; |
| 1822 | } |
| 1823 | |
| 1824 | return cx; |
| 1825 | } |
| 1826 | |
| 1827 | PP(pp_caller) |
| 1828 | { |
| 1829 | dVAR; |
| 1830 | dSP; |
| 1831 | register const PERL_CONTEXT *cx; |
| 1832 | const PERL_CONTEXT *dbcx; |
| 1833 | I32 gimme; |
| 1834 | const char *stashname; |
| 1835 | I32 count = 0; |
| 1836 | |
| 1837 | if (MAXARG) |
| 1838 | count = POPi; |
| 1839 | |
| 1840 | cx = caller_cx(count, &dbcx); |
| 1841 | if (!cx) { |
| 1842 | if (GIMME != G_ARRAY) { |
| 1843 | EXTEND(SP, 1); |
| 1844 | RETPUSHUNDEF; |
| 1845 | } |
| 1846 | RETURN; |
| 1847 | } |
| 1848 | |
| 1849 | stashname = CopSTASHPV(cx->blk_oldcop); |
| 1850 | if (GIMME != G_ARRAY) { |
| 1851 | EXTEND(SP, 1); |
| 1852 | if (!stashname) |
| 1853 | PUSHs(&PL_sv_undef); |
| 1854 | else { |
| 1855 | dTARGET; |
| 1856 | sv_setpv(TARG, stashname); |
| 1857 | PUSHs(TARG); |
| 1858 | } |
| 1859 | RETURN; |
| 1860 | } |
| 1861 | |
| 1862 | EXTEND(SP, 11); |
| 1863 | |
| 1864 | if (!stashname) |
| 1865 | PUSHs(&PL_sv_undef); |
| 1866 | else |
| 1867 | mPUSHs(newSVpv(stashname, 0)); |
| 1868 | mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0)); |
| 1869 | mPUSHi((I32)CopLINE(cx->blk_oldcop)); |
| 1870 | if (!MAXARG) |
| 1871 | RETURN; |
| 1872 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
| 1873 | GV * const cvgv = CvGV(dbcx->blk_sub.cv); |
| 1874 | /* So is ccstack[dbcxix]. */ |
| 1875 | if (isGV(cvgv)) { |
| 1876 | SV * const sv = newSV(0); |
| 1877 | gv_efullname3(sv, cvgv, NULL); |
| 1878 | mPUSHs(sv); |
| 1879 | PUSHs(boolSV(CxHASARGS(cx))); |
| 1880 | } |
| 1881 | else { |
| 1882 | PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP)); |
| 1883 | PUSHs(boolSV(CxHASARGS(cx))); |
| 1884 | } |
| 1885 | } |
| 1886 | else { |
| 1887 | PUSHs(newSVpvs_flags("(eval)", SVs_TEMP)); |
| 1888 | mPUSHi(0); |
| 1889 | } |
| 1890 | gimme = (I32)cx->blk_gimme; |
| 1891 | if (gimme == G_VOID) |
| 1892 | PUSHs(&PL_sv_undef); |
| 1893 | else |
| 1894 | PUSHs(boolSV((gimme & G_WANT) == G_ARRAY)); |
| 1895 | if (CxTYPE(cx) == CXt_EVAL) { |
| 1896 | /* eval STRING */ |
| 1897 | if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { |
| 1898 | PUSHs(cx->blk_eval.cur_text); |
| 1899 | PUSHs(&PL_sv_no); |
| 1900 | } |
| 1901 | /* require */ |
| 1902 | else if (cx->blk_eval.old_namesv) { |
| 1903 | mPUSHs(newSVsv(cx->blk_eval.old_namesv)); |
| 1904 | PUSHs(&PL_sv_yes); |
| 1905 | } |
| 1906 | /* eval BLOCK (try blocks have old_namesv == 0) */ |
| 1907 | else { |
| 1908 | PUSHs(&PL_sv_undef); |
| 1909 | PUSHs(&PL_sv_undef); |
| 1910 | } |
| 1911 | } |
| 1912 | else { |
| 1913 | PUSHs(&PL_sv_undef); |
| 1914 | PUSHs(&PL_sv_undef); |
| 1915 | } |
| 1916 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx) |
| 1917 | && CopSTASH_eq(PL_curcop, PL_debstash)) |
| 1918 | { |
| 1919 | AV * const ary = cx->blk_sub.argarray; |
| 1920 | const int off = AvARRAY(ary) - AvALLOC(ary); |
| 1921 | |
| 1922 | if (!PL_dbargs) |
| 1923 | Perl_init_dbargs(aTHX); |
| 1924 | |
| 1925 | if (AvMAX(PL_dbargs) < AvFILLp(ary) + off) |
| 1926 | av_extend(PL_dbargs, AvFILLp(ary) + off); |
| 1927 | Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*); |
| 1928 | AvFILLp(PL_dbargs) = AvFILLp(ary) + off; |
| 1929 | } |
| 1930 | /* XXX only hints propagated via op_private are currently |
| 1931 | * visible (others are not easily accessible, since they |
| 1932 | * use the global PL_hints) */ |
| 1933 | mPUSHi(CopHINTS_get(cx->blk_oldcop)); |
| 1934 | { |
| 1935 | SV * mask ; |
| 1936 | STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ; |
| 1937 | |
| 1938 | if (old_warnings == pWARN_NONE || |
| 1939 | (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0)) |
| 1940 | mask = newSVpvn(WARN_NONEstring, WARNsize) ; |
| 1941 | else if (old_warnings == pWARN_ALL || |
| 1942 | (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) { |
| 1943 | /* Get the bit mask for $warnings::Bits{all}, because |
| 1944 | * it could have been extended by warnings::register */ |
| 1945 | SV **bits_all; |
| 1946 | HV * const bits = get_hv("warnings::Bits", 0); |
| 1947 | if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) { |
| 1948 | mask = newSVsv(*bits_all); |
| 1949 | } |
| 1950 | else { |
| 1951 | mask = newSVpvn(WARN_ALLstring, WARNsize) ; |
| 1952 | } |
| 1953 | } |
| 1954 | else |
| 1955 | mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]); |
| 1956 | mPUSHs(mask); |
| 1957 | } |
| 1958 | |
| 1959 | PUSHs(cx->blk_oldcop->cop_hints_hash ? |
| 1960 | sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0)))) |
| 1961 | : &PL_sv_undef); |
| 1962 | RETURN; |
| 1963 | } |
| 1964 | |
| 1965 | PP(pp_reset) |
| 1966 | { |
| 1967 | dVAR; |
| 1968 | dSP; |
| 1969 | const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx; |
| 1970 | sv_reset(tmps, CopSTASH(PL_curcop)); |
| 1971 | PUSHs(&PL_sv_yes); |
| 1972 | RETURN; |
| 1973 | } |
| 1974 | |
| 1975 | /* like pp_nextstate, but used instead when the debugger is active */ |
| 1976 | |
| 1977 | PP(pp_dbstate) |
| 1978 | { |
| 1979 | dVAR; |
| 1980 | PL_curcop = (COP*)PL_op; |
| 1981 | TAINT_NOT; /* Each statement is presumed innocent */ |
| 1982 | PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp; |
| 1983 | FREETMPS; |
| 1984 | |
| 1985 | PERL_ASYNC_CHECK(); |
| 1986 | |
| 1987 | if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */ |
| 1988 | || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace)) |
| 1989 | { |
| 1990 | dSP; |
| 1991 | register PERL_CONTEXT *cx; |
| 1992 | const I32 gimme = G_ARRAY; |
| 1993 | U8 hasargs; |
| 1994 | GV * const gv = PL_DBgv; |
| 1995 | register CV * const cv = GvCV(gv); |
| 1996 | |
| 1997 | if (!cv) |
| 1998 | DIE(aTHX_ "No DB::DB routine defined"); |
| 1999 | |
| 2000 | if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG)) |
| 2001 | /* don't do recursive DB::DB call */ |
| 2002 | return NORMAL; |
| 2003 | |
| 2004 | ENTER; |
| 2005 | SAVETMPS; |
| 2006 | |
| 2007 | SAVEI32(PL_debug); |
| 2008 | SAVESTACK_POS(); |
| 2009 | PL_debug = 0; |
| 2010 | hasargs = 0; |
| 2011 | SPAGAIN; |
| 2012 | |
| 2013 | if (CvISXSUB(cv)) { |
| 2014 | CvDEPTH(cv)++; |
| 2015 | PUSHMARK(SP); |
| 2016 | (void)(*CvXSUB(cv))(aTHX_ cv); |
| 2017 | CvDEPTH(cv)--; |
| 2018 | FREETMPS; |
| 2019 | LEAVE; |
| 2020 | return NORMAL; |
| 2021 | } |
| 2022 | else { |
| 2023 | PUSHBLOCK(cx, CXt_SUB, SP); |
| 2024 | PUSHSUB_DB(cx); |
| 2025 | cx->blk_sub.retop = PL_op->op_next; |
| 2026 | CvDEPTH(cv)++; |
| 2027 | SAVECOMPPAD(); |
| 2028 | PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1); |
| 2029 | RETURNOP(CvSTART(cv)); |
| 2030 | } |
| 2031 | } |
| 2032 | else |
| 2033 | return NORMAL; |
| 2034 | } |
| 2035 | |
| 2036 | PP(pp_enteriter) |
| 2037 | { |
| 2038 | dVAR; dSP; dMARK; |
| 2039 | register PERL_CONTEXT *cx; |
| 2040 | const I32 gimme = GIMME_V; |
| 2041 | void *itervar; /* location of the iteration variable */ |
| 2042 | U8 cxtype = CXt_LOOP_FOR; |
| 2043 | |
| 2044 | ENTER_with_name("loop1"); |
| 2045 | SAVETMPS; |
| 2046 | |
| 2047 | if (PL_op->op_targ) { /* "my" variable */ |
| 2048 | if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */ |
| 2049 | SvPADSTALE_off(PAD_SVl(PL_op->op_targ)); |
| 2050 | SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ), |
| 2051 | SVs_PADSTALE, SVs_PADSTALE); |
| 2052 | } |
| 2053 | SAVEPADSVANDMORTALIZE(PL_op->op_targ); |
| 2054 | #ifdef USE_ITHREADS |
| 2055 | itervar = PL_comppad; |
| 2056 | #else |
| 2057 | itervar = &PAD_SVl(PL_op->op_targ); |
| 2058 | #endif |
| 2059 | } |
| 2060 | else { /* symbol table variable */ |
| 2061 | GV * const gv = MUTABLE_GV(POPs); |
| 2062 | SV** svp = &GvSV(gv); |
| 2063 | save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV); |
| 2064 | *svp = newSV(0); |
| 2065 | itervar = (void *)gv; |
| 2066 | } |
| 2067 | |
| 2068 | if (PL_op->op_private & OPpITER_DEF) |
| 2069 | cxtype |= CXp_FOR_DEF; |
| 2070 | |
| 2071 | ENTER_with_name("loop2"); |
| 2072 | |
| 2073 | PUSHBLOCK(cx, cxtype, SP); |
| 2074 | PUSHLOOP_FOR(cx, itervar, MARK); |
| 2075 | if (PL_op->op_flags & OPf_STACKED) { |
| 2076 | SV *maybe_ary = POPs; |
| 2077 | if (SvTYPE(maybe_ary) != SVt_PVAV) { |
| 2078 | dPOPss; |
| 2079 | SV * const right = maybe_ary; |
| 2080 | SvGETMAGIC(sv); |
| 2081 | SvGETMAGIC(right); |
| 2082 | if (RANGE_IS_NUMERIC(sv,right)) { |
| 2083 | cx->cx_type &= ~CXTYPEMASK; |
| 2084 | cx->cx_type |= CXt_LOOP_LAZYIV; |
| 2085 | /* Make sure that no-one re-orders cop.h and breaks our |
| 2086 | assumptions */ |
| 2087 | assert(CxTYPE(cx) == CXt_LOOP_LAZYIV); |
| 2088 | #ifdef NV_PRESERVES_UV |
| 2089 | if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) || |
| 2090 | (SvNV(sv) > (NV)IV_MAX))) |
| 2091 | || |
| 2092 | (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) || |
| 2093 | (SvNV(right) < (NV)IV_MIN)))) |
| 2094 | #else |
| 2095 | if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN) |
| 2096 | || |
| 2097 | ((SvNV(sv) > 0) && |
| 2098 | ((SvUV(sv) > (UV)IV_MAX) || |
| 2099 | (SvNV(sv) > (NV)UV_MAX))))) |
| 2100 | || |
| 2101 | (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN) |
| 2102 | || |
| 2103 | ((SvNV(right) > 0) && |
| 2104 | ((SvUV(right) > (UV)IV_MAX) || |
| 2105 | (SvNV(right) > (NV)UV_MAX)))))) |
| 2106 | #endif |
| 2107 | DIE(aTHX_ "Range iterator outside integer range"); |
| 2108 | cx->blk_loop.state_u.lazyiv.cur = SvIV(sv); |
| 2109 | cx->blk_loop.state_u.lazyiv.end = SvIV(right); |
| 2110 | #ifdef DEBUGGING |
| 2111 | /* for correct -Dstv display */ |
| 2112 | cx->blk_oldsp = sp - PL_stack_base; |
| 2113 | #endif |
| 2114 | } |
| 2115 | else { |
| 2116 | cx->cx_type &= ~CXTYPEMASK; |
| 2117 | cx->cx_type |= CXt_LOOP_LAZYSV; |
| 2118 | /* Make sure that no-one re-orders cop.h and breaks our |
| 2119 | assumptions */ |
| 2120 | assert(CxTYPE(cx) == CXt_LOOP_LAZYSV); |
| 2121 | cx->blk_loop.state_u.lazysv.cur = newSVsv(sv); |
| 2122 | cx->blk_loop.state_u.lazysv.end = right; |
| 2123 | SvREFCNT_inc(right); |
| 2124 | (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur); |
| 2125 | /* This will do the upgrade to SVt_PV, and warn if the value |
| 2126 | is uninitialised. */ |
| 2127 | (void) SvPV_nolen_const(right); |
| 2128 | /* Doing this avoids a check every time in pp_iter in pp_hot.c |
| 2129 | to replace !SvOK() with a pointer to "". */ |
| 2130 | if (!SvOK(right)) { |
| 2131 | SvREFCNT_dec(right); |
| 2132 | cx->blk_loop.state_u.lazysv.end = &PL_sv_no; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | else /* SvTYPE(maybe_ary) == SVt_PVAV */ { |
| 2137 | cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary); |
| 2138 | SvREFCNT_inc(maybe_ary); |
| 2139 | cx->blk_loop.state_u.ary.ix = |
| 2140 | (PL_op->op_private & OPpITER_REVERSED) ? |
| 2141 | AvFILL(cx->blk_loop.state_u.ary.ary) + 1 : |
| 2142 | -1; |
| 2143 | } |
| 2144 | } |
| 2145 | else { /* iterating over items on the stack */ |
| 2146 | cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */ |
| 2147 | if (PL_op->op_private & OPpITER_REVERSED) { |
| 2148 | cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1; |
| 2149 | } |
| 2150 | else { |
| 2151 | cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | RETURN; |
| 2156 | } |
| 2157 | |
| 2158 | PP(pp_enterloop) |
| 2159 | { |
| 2160 | dVAR; dSP; |
| 2161 | register PERL_CONTEXT *cx; |
| 2162 | const I32 gimme = GIMME_V; |
| 2163 | |
| 2164 | ENTER_with_name("loop1"); |
| 2165 | SAVETMPS; |
| 2166 | ENTER_with_name("loop2"); |
| 2167 | |
| 2168 | PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP); |
| 2169 | PUSHLOOP_PLAIN(cx, SP); |
| 2170 | |
| 2171 | RETURN; |
| 2172 | } |
| 2173 | |
| 2174 | PP(pp_leaveloop) |
| 2175 | { |
| 2176 | dVAR; dSP; |
| 2177 | register PERL_CONTEXT *cx; |
| 2178 | I32 gimme; |
| 2179 | SV **newsp; |
| 2180 | PMOP *newpm; |
| 2181 | SV **mark; |
| 2182 | |
| 2183 | POPBLOCK(cx,newpm); |
| 2184 | assert(CxTYPE_is_LOOP(cx)); |
| 2185 | mark = newsp; |
| 2186 | newsp = PL_stack_base + cx->blk_loop.resetsp; |
| 2187 | |
| 2188 | TAINT_NOT; |
| 2189 | if (gimme == G_VOID) |
| 2190 | NOOP; |
| 2191 | else if (gimme == G_SCALAR) { |
| 2192 | if (mark < SP) |
| 2193 | *++newsp = sv_mortalcopy(*SP); |
| 2194 | else |
| 2195 | *++newsp = &PL_sv_undef; |
| 2196 | } |
| 2197 | else { |
| 2198 | while (mark < SP) { |
| 2199 | *++newsp = sv_mortalcopy(*++mark); |
| 2200 | TAINT_NOT; /* Each item is independent */ |
| 2201 | } |
| 2202 | } |
| 2203 | SP = newsp; |
| 2204 | PUTBACK; |
| 2205 | |
| 2206 | POPLOOP(cx); /* Stack values are safe: release loop vars ... */ |
| 2207 | PL_curpm = newpm; /* ... and pop $1 et al */ |
| 2208 | |
| 2209 | LEAVE_with_name("loop2"); |
| 2210 | LEAVE_with_name("loop1"); |
| 2211 | |
| 2212 | return NORMAL; |
| 2213 | } |
| 2214 | |
| 2215 | STATIC void |
| 2216 | S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme, |
| 2217 | PERL_CONTEXT *cx) |
| 2218 | { |
| 2219 | if (gimme == G_SCALAR) { |
| 2220 | if (MARK < SP) { |
| 2221 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
| 2222 | *++newsp = SvREFCNT_inc(*SP); |
| 2223 | FREETMPS; |
| 2224 | sv_2mortal(*newsp); |
| 2225 | } |
| 2226 | else |
| 2227 | *++newsp = *SP; |
| 2228 | } |
| 2229 | else |
| 2230 | *++newsp = &PL_sv_undef; |
| 2231 | } |
| 2232 | else if (gimme == G_ARRAY) { |
| 2233 | while (++MARK <= SP) { |
| 2234 | *++newsp = *MARK; |
| 2235 | TAINT_NOT; /* Each item is independent */ |
| 2236 | } |
| 2237 | } |
| 2238 | PL_stack_sp = newsp; |
| 2239 | } |
| 2240 | |
| 2241 | PP(pp_return) |
| 2242 | { |
| 2243 | dVAR; dSP; dMARK; |
| 2244 | register PERL_CONTEXT *cx; |
| 2245 | bool popsub2 = FALSE; |
| 2246 | bool clear_errsv = FALSE; |
| 2247 | bool lval = FALSE; |
| 2248 | I32 gimme; |
| 2249 | SV **newsp; |
| 2250 | PMOP *newpm; |
| 2251 | I32 optype = 0; |
| 2252 | SV *namesv; |
| 2253 | SV *sv; |
| 2254 | OP *retop = NULL; |
| 2255 | |
| 2256 | const I32 cxix = dopoptosub(cxstack_ix); |
| 2257 | |
| 2258 | if (cxix < 0) { |
| 2259 | if (CxMULTICALL(cxstack)) { /* In this case we must be in a |
| 2260 | * sort block, which is a CXt_NULL |
| 2261 | * not a CXt_SUB */ |
| 2262 | dounwind(0); |
| 2263 | PL_stack_base[1] = *PL_stack_sp; |
| 2264 | PL_stack_sp = PL_stack_base + 1; |
| 2265 | return 0; |
| 2266 | } |
| 2267 | else |
| 2268 | DIE(aTHX_ "Can't return outside a subroutine"); |
| 2269 | } |
| 2270 | if (cxix < cxstack_ix) |
| 2271 | dounwind(cxix); |
| 2272 | |
| 2273 | if (CxMULTICALL(&cxstack[cxix])) { |
| 2274 | gimme = cxstack[cxix].blk_gimme; |
| 2275 | if (gimme == G_VOID) |
| 2276 | PL_stack_sp = PL_stack_base; |
| 2277 | else if (gimme == G_SCALAR) { |
| 2278 | PL_stack_base[1] = *PL_stack_sp; |
| 2279 | PL_stack_sp = PL_stack_base + 1; |
| 2280 | } |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | POPBLOCK(cx,newpm); |
| 2285 | switch (CxTYPE(cx)) { |
| 2286 | case CXt_SUB: |
| 2287 | popsub2 = TRUE; |
| 2288 | lval = !!CvLVALUE(cx->blk_sub.cv); |
| 2289 | retop = cx->blk_sub.retop; |
| 2290 | cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */ |
| 2291 | break; |
| 2292 | case CXt_EVAL: |
| 2293 | if (!(PL_in_eval & EVAL_KEEPERR)) |
| 2294 | clear_errsv = TRUE; |
| 2295 | POPEVAL(cx); |
| 2296 | namesv = cx->blk_eval.old_namesv; |
| 2297 | retop = cx->blk_eval.retop; |
| 2298 | if (CxTRYBLOCK(cx)) |
| 2299 | break; |
| 2300 | if (optype == OP_REQUIRE && |
| 2301 | (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) ) |
| 2302 | { |
| 2303 | /* Unassume the success we assumed earlier. */ |
| 2304 | (void)hv_delete(GvHVn(PL_incgv), |
| 2305 | SvPVX_const(namesv), SvCUR(namesv), |
| 2306 | G_DISCARD); |
| 2307 | DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv)); |
| 2308 | } |
| 2309 | break; |
| 2310 | case CXt_FORMAT: |
| 2311 | POPFORMAT(cx); |
| 2312 | retop = cx->blk_sub.retop; |
| 2313 | break; |
| 2314 | default: |
| 2315 | DIE(aTHX_ "panic: return"); |
| 2316 | } |
| 2317 | |
| 2318 | TAINT_NOT; |
| 2319 | if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx); |
| 2320 | else { |
| 2321 | if (gimme == G_SCALAR) { |
| 2322 | if (MARK < SP) { |
| 2323 | if (popsub2) { |
| 2324 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
| 2325 | if (SvTEMP(TOPs)) { |
| 2326 | *++newsp = SvREFCNT_inc(*SP); |
| 2327 | FREETMPS; |
| 2328 | sv_2mortal(*newsp); |
| 2329 | } |
| 2330 | else { |
| 2331 | sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */ |
| 2332 | FREETMPS; |
| 2333 | *++newsp = sv_mortalcopy(sv); |
| 2334 | SvREFCNT_dec(sv); |
| 2335 | } |
| 2336 | } |
| 2337 | else |
| 2338 | *++newsp = |
| 2339 | SvTEMP(*SP) ? *SP : sv_mortalcopy(*SP); |
| 2340 | } |
| 2341 | else |
| 2342 | *++newsp = sv_mortalcopy(*SP); |
| 2343 | } |
| 2344 | else |
| 2345 | *++newsp = &PL_sv_undef; |
| 2346 | } |
| 2347 | else if (gimme == G_ARRAY) { |
| 2348 | while (++MARK <= SP) { |
| 2349 | *++newsp = popsub2 && SvTEMP(*MARK) |
| 2350 | ? *MARK : sv_mortalcopy(*MARK); |
| 2351 | TAINT_NOT; /* Each item is independent */ |
| 2352 | } |
| 2353 | } |
| 2354 | PL_stack_sp = newsp; |
| 2355 | } |
| 2356 | |
| 2357 | LEAVE; |
| 2358 | /* Stack values are safe: */ |
| 2359 | if (popsub2) { |
| 2360 | cxstack_ix--; |
| 2361 | POPSUB(cx,sv); /* release CV and @_ ... */ |
| 2362 | } |
| 2363 | else |
| 2364 | sv = NULL; |
| 2365 | PL_curpm = newpm; /* ... and pop $1 et al */ |
| 2366 | |
| 2367 | LEAVESUB(sv); |
| 2368 | if (clear_errsv) { |
| 2369 | CLEAR_ERRSV(); |
| 2370 | } |
| 2371 | return retop; |
| 2372 | } |
| 2373 | |
| 2374 | PP(pp_last) |
| 2375 | { |
| 2376 | dVAR; dSP; |
| 2377 | I32 cxix; |
| 2378 | register PERL_CONTEXT *cx; |
| 2379 | I32 pop2 = 0; |
| 2380 | I32 gimme; |
| 2381 | I32 optype; |
| 2382 | OP *nextop = NULL; |
| 2383 | SV **newsp; |
| 2384 | PMOP *newpm; |
| 2385 | SV **mark; |
| 2386 | SV *sv = NULL; |
| 2387 | |
| 2388 | |
| 2389 | if (PL_op->op_flags & OPf_SPECIAL) { |
| 2390 | cxix = dopoptoloop(cxstack_ix); |
| 2391 | if (cxix < 0) |
| 2392 | DIE(aTHX_ "Can't \"last\" outside a loop block"); |
| 2393 | } |
| 2394 | else { |
| 2395 | cxix = dopoptolabel(cPVOP->op_pv); |
| 2396 | if (cxix < 0) |
| 2397 | DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv); |
| 2398 | } |
| 2399 | if (cxix < cxstack_ix) |
| 2400 | dounwind(cxix); |
| 2401 | |
| 2402 | POPBLOCK(cx,newpm); |
| 2403 | cxstack_ix++; /* temporarily protect top context */ |
| 2404 | mark = newsp; |
| 2405 | switch (CxTYPE(cx)) { |
| 2406 | case CXt_LOOP_LAZYIV: |
| 2407 | case CXt_LOOP_LAZYSV: |
| 2408 | case CXt_LOOP_FOR: |
| 2409 | case CXt_LOOP_PLAIN: |
| 2410 | pop2 = CxTYPE(cx); |
| 2411 | newsp = PL_stack_base + cx->blk_loop.resetsp; |
| 2412 | nextop = cx->blk_loop.my_op->op_lastop->op_next; |
| 2413 | break; |
| 2414 | case CXt_SUB: |
| 2415 | pop2 = CXt_SUB; |
| 2416 | nextop = cx->blk_sub.retop; |
| 2417 | break; |
| 2418 | case CXt_EVAL: |
| 2419 | POPEVAL(cx); |
| 2420 | nextop = cx->blk_eval.retop; |
| 2421 | break; |
| 2422 | case CXt_FORMAT: |
| 2423 | POPFORMAT(cx); |
| 2424 | nextop = cx->blk_sub.retop; |
| 2425 | break; |
| 2426 | default: |
| 2427 | DIE(aTHX_ "panic: last"); |
| 2428 | } |
| 2429 | |
| 2430 | TAINT_NOT; |
| 2431 | if (gimme == G_SCALAR) { |
| 2432 | if (MARK < SP) |
| 2433 | *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP)) |
| 2434 | ? *SP : sv_mortalcopy(*SP); |
| 2435 | else |
| 2436 | *++newsp = &PL_sv_undef; |
| 2437 | } |
| 2438 | else if (gimme == G_ARRAY) { |
| 2439 | while (++MARK <= SP) { |
| 2440 | *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK)) |
| 2441 | ? *MARK : sv_mortalcopy(*MARK); |
| 2442 | TAINT_NOT; /* Each item is independent */ |
| 2443 | } |
| 2444 | } |
| 2445 | SP = newsp; |
| 2446 | PUTBACK; |
| 2447 | |
| 2448 | LEAVE; |
| 2449 | cxstack_ix--; |
| 2450 | /* Stack values are safe: */ |
| 2451 | switch (pop2) { |
| 2452 | case CXt_LOOP_LAZYIV: |
| 2453 | case CXt_LOOP_PLAIN: |
| 2454 | case CXt_LOOP_LAZYSV: |
| 2455 | case CXt_LOOP_FOR: |
| 2456 | POPLOOP(cx); /* release loop vars ... */ |
| 2457 | LEAVE; |
| 2458 | break; |
| 2459 | case CXt_SUB: |
| 2460 | POPSUB(cx,sv); /* release CV and @_ ... */ |
| 2461 | break; |
| 2462 | } |
| 2463 | PL_curpm = newpm; /* ... and pop $1 et al */ |
| 2464 | |
| 2465 | LEAVESUB(sv); |
| 2466 | PERL_UNUSED_VAR(optype); |
| 2467 | PERL_UNUSED_VAR(gimme); |
| 2468 | return nextop; |
| 2469 | } |
| 2470 | |
| 2471 | PP(pp_next) |
| 2472 | { |
| 2473 | dVAR; |
| 2474 | I32 cxix; |
| 2475 | register PERL_CONTEXT *cx; |
| 2476 | I32 inner; |
| 2477 | |
| 2478 | if (PL_op->op_flags & OPf_SPECIAL) { |
| 2479 | cxix = dopoptoloop(cxstack_ix); |
| 2480 | if (cxix < 0) |
| 2481 | DIE(aTHX_ "Can't \"next\" outside a loop block"); |
| 2482 | } |
| 2483 | else { |
| 2484 | cxix = dopoptolabel(cPVOP->op_pv); |
| 2485 | if (cxix < 0) |
| 2486 | DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv); |
| 2487 | } |
| 2488 | if (cxix < cxstack_ix) |
| 2489 | dounwind(cxix); |
| 2490 | |
| 2491 | /* clear off anything above the scope we're re-entering, but |
| 2492 | * save the rest until after a possible continue block */ |
| 2493 | inner = PL_scopestack_ix; |
| 2494 | TOPBLOCK(cx); |
| 2495 | if (PL_scopestack_ix < inner) |
| 2496 | leave_scope(PL_scopestack[PL_scopestack_ix]); |
| 2497 | PL_curcop = cx->blk_oldcop; |
| 2498 | return (cx)->blk_loop.my_op->op_nextop; |
| 2499 | } |
| 2500 | |
| 2501 | PP(pp_redo) |
| 2502 | { |
| 2503 | dVAR; |
| 2504 | I32 cxix; |
| 2505 | register PERL_CONTEXT *cx; |
| 2506 | I32 oldsave; |
| 2507 | OP* redo_op; |
| 2508 | |
| 2509 | if (PL_op->op_flags & OPf_SPECIAL) { |
| 2510 | cxix = dopoptoloop(cxstack_ix); |
| 2511 | if (cxix < 0) |
| 2512 | DIE(aTHX_ "Can't \"redo\" outside a loop block"); |
| 2513 | } |
| 2514 | else { |
| 2515 | cxix = dopoptolabel(cPVOP->op_pv); |
| 2516 | if (cxix < 0) |
| 2517 | DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv); |
| 2518 | } |
| 2519 | if (cxix < cxstack_ix) |
| 2520 | dounwind(cxix); |
| 2521 | |
| 2522 | redo_op = cxstack[cxix].blk_loop.my_op->op_redoop; |
| 2523 | if (redo_op->op_type == OP_ENTER) { |
| 2524 | /* pop one less context to avoid $x being freed in while (my $x..) */ |
| 2525 | cxstack_ix++; |
| 2526 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK); |
| 2527 | redo_op = redo_op->op_next; |
| 2528 | } |
| 2529 | |
| 2530 | TOPBLOCK(cx); |
| 2531 | oldsave = PL_scopestack[PL_scopestack_ix - 1]; |
| 2532 | LEAVE_SCOPE(oldsave); |
| 2533 | FREETMPS; |
| 2534 | PL_curcop = cx->blk_oldcop; |
| 2535 | return redo_op; |
| 2536 | } |
| 2537 | |
| 2538 | STATIC OP * |
| 2539 | S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit) |
| 2540 | { |
| 2541 | dVAR; |
| 2542 | OP **ops = opstack; |
| 2543 | static const char too_deep[] = "Target of goto is too deeply nested"; |
| 2544 | |
| 2545 | PERL_ARGS_ASSERT_DOFINDLABEL; |
| 2546 | |
| 2547 | if (ops >= oplimit) |
| 2548 | Perl_croak(aTHX_ too_deep); |
| 2549 | if (o->op_type == OP_LEAVE || |
| 2550 | o->op_type == OP_SCOPE || |
| 2551 | o->op_type == OP_LEAVELOOP || |
| 2552 | o->op_type == OP_LEAVESUB || |
| 2553 | o->op_type == OP_LEAVETRY) |
| 2554 | { |
| 2555 | *ops++ = cUNOPo->op_first; |
| 2556 | if (ops >= oplimit) |
| 2557 | Perl_croak(aTHX_ too_deep); |
| 2558 | } |
| 2559 | *ops = 0; |
| 2560 | if (o->op_flags & OPf_KIDS) { |
| 2561 | OP *kid; |
| 2562 | /* First try all the kids at this level, since that's likeliest. */ |
| 2563 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
| 2564 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
| 2565 | const char *kid_label = CopLABEL(kCOP); |
| 2566 | if (kid_label && strEQ(kid_label, label)) |
| 2567 | return kid; |
| 2568 | } |
| 2569 | } |
| 2570 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
| 2571 | if (kid == PL_lastgotoprobe) |
| 2572 | continue; |
| 2573 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
| 2574 | if (ops == opstack) |
| 2575 | *ops++ = kid; |
| 2576 | else if (ops[-1]->op_type == OP_NEXTSTATE || |
| 2577 | ops[-1]->op_type == OP_DBSTATE) |
| 2578 | ops[-1] = kid; |
| 2579 | else |
| 2580 | *ops++ = kid; |
| 2581 | } |
| 2582 | if ((o = dofindlabel(kid, label, ops, oplimit))) |
| 2583 | return o; |
| 2584 | } |
| 2585 | } |
| 2586 | *ops = 0; |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | PP(pp_goto) |
| 2591 | { |
| 2592 | dVAR; dSP; |
| 2593 | OP *retop = NULL; |
| 2594 | I32 ix; |
| 2595 | register PERL_CONTEXT *cx; |
| 2596 | #define GOTO_DEPTH 64 |
| 2597 | OP *enterops[GOTO_DEPTH]; |
| 2598 | const char *label = NULL; |
| 2599 | const bool do_dump = (PL_op->op_type == OP_DUMP); |
| 2600 | static const char must_have_label[] = "goto must have label"; |
| 2601 | |
| 2602 | if (PL_op->op_flags & OPf_STACKED) { |
| 2603 | SV * const sv = POPs; |
| 2604 | |
| 2605 | /* This egregious kludge implements goto &subroutine */ |
| 2606 | if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) { |
| 2607 | I32 cxix; |
| 2608 | register PERL_CONTEXT *cx; |
| 2609 | CV *cv = MUTABLE_CV(SvRV(sv)); |
| 2610 | SV** mark; |
| 2611 | I32 items = 0; |
| 2612 | I32 oldsave; |
| 2613 | bool reified = 0; |
| 2614 | |
| 2615 | retry: |
| 2616 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
| 2617 | const GV * const gv = CvGV(cv); |
| 2618 | if (gv) { |
| 2619 | GV *autogv; |
| 2620 | SV *tmpstr; |
| 2621 | /* autoloaded stub? */ |
| 2622 | if (cv != GvCV(gv) && (cv = GvCV(gv))) |
| 2623 | goto retry; |
| 2624 | autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), |
| 2625 | GvNAMELEN(gv), FALSE); |
| 2626 | if (autogv && (cv = GvCV(autogv))) |
| 2627 | goto retry; |
| 2628 | tmpstr = sv_newmortal(); |
| 2629 | gv_efullname3(tmpstr, gv, NULL); |
| 2630 | DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr)); |
| 2631 | } |
| 2632 | DIE(aTHX_ "Goto undefined subroutine"); |
| 2633 | } |
| 2634 | |
| 2635 | /* First do some returnish stuff. */ |
| 2636 | SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */ |
| 2637 | FREETMPS; |
| 2638 | cxix = dopoptosub(cxstack_ix); |
| 2639 | if (cxix < 0) |
| 2640 | DIE(aTHX_ "Can't goto subroutine outside a subroutine"); |
| 2641 | if (cxix < cxstack_ix) |
| 2642 | dounwind(cxix); |
| 2643 | TOPBLOCK(cx); |
| 2644 | SPAGAIN; |
| 2645 | /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */ |
| 2646 | if (CxTYPE(cx) == CXt_EVAL) { |
| 2647 | if (CxREALEVAL(cx)) |
| 2648 | DIE(aTHX_ "Can't goto subroutine from an eval-string"); |
| 2649 | else |
| 2650 | DIE(aTHX_ "Can't goto subroutine from an eval-block"); |
| 2651 | } |
| 2652 | else if (CxMULTICALL(cx)) |
| 2653 | DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)"); |
| 2654 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { |
| 2655 | /* put @_ back onto stack */ |
| 2656 | AV* av = cx->blk_sub.argarray; |
| 2657 | |
| 2658 | items = AvFILLp(av) + 1; |
| 2659 | EXTEND(SP, items+1); /* @_ could have been extended. */ |
| 2660 | Copy(AvARRAY(av), SP + 1, items, SV*); |
| 2661 | SvREFCNT_dec(GvAV(PL_defgv)); |
| 2662 | GvAV(PL_defgv) = cx->blk_sub.savearray; |
| 2663 | CLEAR_ARGARRAY(av); |
| 2664 | /* abandon @_ if it got reified */ |
| 2665 | if (AvREAL(av)) { |
| 2666 | reified = 1; |
| 2667 | SvREFCNT_dec(av); |
| 2668 | av = newAV(); |
| 2669 | av_extend(av, items-1); |
| 2670 | AvREIFY_only(av); |
| 2671 | PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av); |
| 2672 | } |
| 2673 | } |
| 2674 | else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */ |
| 2675 | AV* const av = GvAV(PL_defgv); |
| 2676 | items = AvFILLp(av) + 1; |
| 2677 | EXTEND(SP, items+1); /* @_ could have been extended. */ |
| 2678 | Copy(AvARRAY(av), SP + 1, items, SV*); |
| 2679 | } |
| 2680 | mark = SP; |
| 2681 | SP += items; |
| 2682 | if (CxTYPE(cx) == CXt_SUB && |
| 2683 | !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) |
| 2684 | SvREFCNT_dec(cx->blk_sub.cv); |
| 2685 | oldsave = PL_scopestack[PL_scopestack_ix - 1]; |
| 2686 | LEAVE_SCOPE(oldsave); |
| 2687 | |
| 2688 | /* Now do some callish stuff. */ |
| 2689 | SAVETMPS; |
| 2690 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ |
| 2691 | if (CvISXSUB(cv)) { |
| 2692 | OP* const retop = cx->blk_sub.retop; |
| 2693 | SV **newsp __attribute__unused__; |
| 2694 | I32 gimme __attribute__unused__; |
| 2695 | if (reified) { |
| 2696 | I32 index; |
| 2697 | for (index=0; index<items; index++) |
| 2698 | sv_2mortal(SP[-index]); |
| 2699 | } |
| 2700 | |
| 2701 | /* XS subs don't have a CxSUB, so pop it */ |
| 2702 | POPBLOCK(cx, PL_curpm); |
| 2703 | /* Push a mark for the start of arglist */ |
| 2704 | PUSHMARK(mark); |
| 2705 | PUTBACK; |
| 2706 | (void)(*CvXSUB(cv))(aTHX_ cv); |
| 2707 | LEAVE; |
| 2708 | return retop; |
| 2709 | } |
| 2710 | else { |
| 2711 | AV* const padlist = CvPADLIST(cv); |
| 2712 | if (CxTYPE(cx) == CXt_EVAL) { |
| 2713 | PL_in_eval = CxOLD_IN_EVAL(cx); |
| 2714 | PL_eval_root = cx->blk_eval.old_eval_root; |
| 2715 | cx->cx_type = CXt_SUB; |
| 2716 | } |
| 2717 | cx->blk_sub.cv = cv; |
| 2718 | cx->blk_sub.olddepth = CvDEPTH(cv); |
| 2719 | |
| 2720 | CvDEPTH(cv)++; |
| 2721 | if (CvDEPTH(cv) < 2) |
| 2722 | SvREFCNT_inc_simple_void_NN(cv); |
| 2723 | else { |
| 2724 | if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION)) |
| 2725 | sub_crush_depth(cv); |
| 2726 | pad_push(padlist, CvDEPTH(cv)); |
| 2727 | } |
| 2728 | SAVECOMPPAD(); |
| 2729 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); |
| 2730 | if (CxHASARGS(cx)) |
| 2731 | { |
| 2732 | AV *const av = MUTABLE_AV(PAD_SVl(0)); |
| 2733 | |
| 2734 | cx->blk_sub.savearray = GvAV(PL_defgv); |
| 2735 | GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av)); |
| 2736 | CX_CURPAD_SAVE(cx->blk_sub); |
| 2737 | cx->blk_sub.argarray = av; |
| 2738 | |
| 2739 | if (items >= AvMAX(av) + 1) { |
| 2740 | SV **ary = AvALLOC(av); |
| 2741 | if (AvARRAY(av) != ary) { |
| 2742 | AvMAX(av) += AvARRAY(av) - AvALLOC(av); |
| 2743 | AvARRAY(av) = ary; |
| 2744 | } |
| 2745 | if (items >= AvMAX(av) + 1) { |
| 2746 | AvMAX(av) = items - 1; |
| 2747 | Renew(ary,items+1,SV*); |
| 2748 | AvALLOC(av) = ary; |
| 2749 | AvARRAY(av) = ary; |
| 2750 | } |
| 2751 | } |
| 2752 | ++mark; |
| 2753 | Copy(mark,AvARRAY(av),items,SV*); |
| 2754 | AvFILLp(av) = items - 1; |
| 2755 | assert(!AvREAL(av)); |
| 2756 | if (reified) { |
| 2757 | /* transfer 'ownership' of refcnts to new @_ */ |
| 2758 | AvREAL_on(av); |
| 2759 | AvREIFY_off(av); |
| 2760 | } |
| 2761 | while (items--) { |
| 2762 | if (*mark) |
| 2763 | SvTEMP_off(*mark); |
| 2764 | mark++; |
| 2765 | } |
| 2766 | } |
| 2767 | if (PERLDB_SUB) { /* Checking curstash breaks DProf. */ |
| 2768 | Perl_get_db_sub(aTHX_ NULL, cv); |
| 2769 | if (PERLDB_GOTO) { |
| 2770 | CV * const gotocv = get_cvs("DB::goto", 0); |
| 2771 | if (gotocv) { |
| 2772 | PUSHMARK( PL_stack_sp ); |
| 2773 | call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG); |
| 2774 | PL_stack_sp--; |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | RETURNOP(CvSTART(cv)); |
| 2779 | } |
| 2780 | } |
| 2781 | else { |
| 2782 | label = SvPV_nolen_const(sv); |
| 2783 | if (!(do_dump || *label)) |
| 2784 | DIE(aTHX_ must_have_label); |
| 2785 | } |
| 2786 | } |
| 2787 | else if (PL_op->op_flags & OPf_SPECIAL) { |
| 2788 | if (! do_dump) |
| 2789 | DIE(aTHX_ must_have_label); |
| 2790 | } |
| 2791 | else |
| 2792 | label = cPVOP->op_pv; |
| 2793 | |
| 2794 | PERL_ASYNC_CHECK(); |
| 2795 | |
| 2796 | if (label && *label) { |
| 2797 | OP *gotoprobe = NULL; |
| 2798 | bool leaving_eval = FALSE; |
| 2799 | bool in_block = FALSE; |
| 2800 | PERL_CONTEXT *last_eval_cx = NULL; |
| 2801 | |
| 2802 | /* find label */ |
| 2803 | |
| 2804 | PL_lastgotoprobe = NULL; |
| 2805 | *enterops = 0; |
| 2806 | for (ix = cxstack_ix; ix >= 0; ix--) { |
| 2807 | cx = &cxstack[ix]; |
| 2808 | switch (CxTYPE(cx)) { |
| 2809 | case CXt_EVAL: |
| 2810 | leaving_eval = TRUE; |
| 2811 | if (!CxTRYBLOCK(cx)) { |
| 2812 | gotoprobe = (last_eval_cx ? |
| 2813 | last_eval_cx->blk_eval.old_eval_root : |
| 2814 | PL_eval_root); |
| 2815 | last_eval_cx = cx; |
| 2816 | break; |
| 2817 | } |
| 2818 | /* else fall through */ |
| 2819 | case CXt_LOOP_LAZYIV: |
| 2820 | case CXt_LOOP_LAZYSV: |
| 2821 | case CXt_LOOP_FOR: |
| 2822 | case CXt_LOOP_PLAIN: |
| 2823 | case CXt_GIVEN: |
| 2824 | case CXt_WHEN: |
| 2825 | gotoprobe = cx->blk_oldcop->op_sibling; |
| 2826 | break; |
| 2827 | case CXt_SUBST: |
| 2828 | continue; |
| 2829 | case CXt_BLOCK: |
| 2830 | if (ix) { |
| 2831 | gotoprobe = cx->blk_oldcop->op_sibling; |
| 2832 | in_block = TRUE; |
| 2833 | } else |
| 2834 | gotoprobe = PL_main_root; |
| 2835 | break; |
| 2836 | case CXt_SUB: |
| 2837 | if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) { |
| 2838 | gotoprobe = CvROOT(cx->blk_sub.cv); |
| 2839 | break; |
| 2840 | } |
| 2841 | /* FALL THROUGH */ |
| 2842 | case CXt_FORMAT: |
| 2843 | case CXt_NULL: |
| 2844 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); |
| 2845 | default: |
| 2846 | if (ix) |
| 2847 | DIE(aTHX_ "panic: goto"); |
| 2848 | gotoprobe = PL_main_root; |
| 2849 | break; |
| 2850 | } |
| 2851 | if (gotoprobe) { |
| 2852 | retop = dofindlabel(gotoprobe, label, |
| 2853 | enterops, enterops + GOTO_DEPTH); |
| 2854 | if (retop) |
| 2855 | break; |
| 2856 | if (gotoprobe->op_sibling && |
| 2857 | gotoprobe->op_sibling->op_type == OP_UNSTACK && |
| 2858 | gotoprobe->op_sibling->op_sibling) { |
| 2859 | retop = dofindlabel(gotoprobe->op_sibling->op_sibling, |
| 2860 | label, enterops, enterops + GOTO_DEPTH); |
| 2861 | if (retop) |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | PL_lastgotoprobe = gotoprobe; |
| 2866 | } |
| 2867 | if (!retop) |
| 2868 | DIE(aTHX_ "Can't find label %s", label); |
| 2869 | |
| 2870 | /* if we're leaving an eval, check before we pop any frames |
| 2871 | that we're not going to punt, otherwise the error |
| 2872 | won't be caught */ |
| 2873 | |
| 2874 | if (leaving_eval && *enterops && enterops[1]) { |
| 2875 | I32 i; |
| 2876 | for (i = 1; enterops[i]; i++) |
| 2877 | if (enterops[i]->op_type == OP_ENTERITER) |
| 2878 | DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop"); |
| 2879 | } |
| 2880 | |
| 2881 | if (*enterops && enterops[1]) { |
| 2882 | I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1; |
| 2883 | if (enterops[i]) |
| 2884 | deprecate("\"goto\" to jump into a construct"); |
| 2885 | } |
| 2886 | |
| 2887 | /* pop unwanted frames */ |
| 2888 | |
| 2889 | if (ix < cxstack_ix) { |
| 2890 | I32 oldsave; |
| 2891 | |
| 2892 | if (ix < 0) |
| 2893 | ix = 0; |
| 2894 | dounwind(ix); |
| 2895 | TOPBLOCK(cx); |
| 2896 | oldsave = PL_scopestack[PL_scopestack_ix]; |
| 2897 | LEAVE_SCOPE(oldsave); |
| 2898 | } |
| 2899 | |
| 2900 | /* push wanted frames */ |
| 2901 | |
| 2902 | if (*enterops && enterops[1]) { |
| 2903 | OP * const oldop = PL_op; |
| 2904 | ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1; |
| 2905 | for (; enterops[ix]; ix++) { |
| 2906 | PL_op = enterops[ix]; |
| 2907 | /* Eventually we may want to stack the needed arguments |
| 2908 | * for each op. For now, we punt on the hard ones. */ |
| 2909 | if (PL_op->op_type == OP_ENTERITER) |
| 2910 | DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop"); |
| 2911 | PL_op->op_ppaddr(aTHX); |
| 2912 | } |
| 2913 | PL_op = oldop; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | if (do_dump) { |
| 2918 | #ifdef VMS |
| 2919 | if (!retop) retop = PL_main_start; |
| 2920 | #endif |
| 2921 | PL_restartop = retop; |
| 2922 | PL_do_undump = TRUE; |
| 2923 | |
| 2924 | my_unexec(); |
| 2925 | |
| 2926 | PL_restartop = 0; /* hmm, must be GNU unexec().. */ |
| 2927 | PL_do_undump = FALSE; |
| 2928 | } |
| 2929 | |
| 2930 | RETURNOP(retop); |
| 2931 | } |
| 2932 | |
| 2933 | PP(pp_exit) |
| 2934 | { |
| 2935 | dVAR; |
| 2936 | dSP; |
| 2937 | I32 anum; |
| 2938 | |
| 2939 | if (MAXARG < 1) |
| 2940 | anum = 0; |
| 2941 | else { |
| 2942 | anum = SvIVx(POPs); |
| 2943 | #ifdef VMS |
| 2944 | if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH)) |
| 2945 | anum = 0; |
| 2946 | VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH); |
| 2947 | #endif |
| 2948 | } |
| 2949 | PL_exit_flags |= PERL_EXIT_EXPECTED; |
| 2950 | #ifdef PERL_MAD |
| 2951 | /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */ |
| 2952 | if (anum || !(PL_minus_c && PL_madskills)) |
| 2953 | my_exit(anum); |
| 2954 | #else |
| 2955 | my_exit(anum); |
| 2956 | #endif |
| 2957 | PUSHs(&PL_sv_undef); |
| 2958 | RETURN; |
| 2959 | } |
| 2960 | |
| 2961 | /* Eval. */ |
| 2962 | |
| 2963 | STATIC void |
| 2964 | S_save_lines(pTHX_ AV *array, SV *sv) |
| 2965 | { |
| 2966 | const char *s = SvPVX_const(sv); |
| 2967 | const char * const send = SvPVX_const(sv) + SvCUR(sv); |
| 2968 | I32 line = 1; |
| 2969 | |
| 2970 | PERL_ARGS_ASSERT_SAVE_LINES; |
| 2971 | |
| 2972 | while (s && s < send) { |
| 2973 | const char *t; |
| 2974 | SV * const tmpstr = newSV_type(SVt_PVMG); |
| 2975 | |
| 2976 | t = (const char *)memchr(s, '\n', send - s); |
| 2977 | if (t) |
| 2978 | t++; |
| 2979 | else |
| 2980 | t = send; |
| 2981 | |
| 2982 | sv_setpvn(tmpstr, s, t - s); |
| 2983 | av_store(array, line++, tmpstr); |
| 2984 | s = t; |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | =for apidoc docatch |
| 2990 | |
| 2991 | Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context. |
| 2992 | |
| 2993 | 0 is used as continue inside eval, |
| 2994 | |
| 2995 | 3 is used for a die caught by an inner eval - continue inner loop |
| 2996 | |
| 2997 | See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must |
| 2998 | establish a local jmpenv to handle exception traps. |
| 2999 | |
| 3000 | =cut |
| 3001 | */ |
| 3002 | STATIC OP * |
| 3003 | S_docatch(pTHX_ OP *o) |
| 3004 | { |
| 3005 | dVAR; |
| 3006 | int ret; |
| 3007 | OP * const oldop = PL_op; |
| 3008 | dJMPENV; |
| 3009 | |
| 3010 | #ifdef DEBUGGING |
| 3011 | assert(CATCH_GET == TRUE); |
| 3012 | #endif |
| 3013 | PL_op = o; |
| 3014 | |
| 3015 | JMPENV_PUSH(ret); |
| 3016 | switch (ret) { |
| 3017 | case 0: |
| 3018 | assert(cxstack_ix >= 0); |
| 3019 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); |
| 3020 | cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env; |
| 3021 | redo_body: |
| 3022 | CALLRUNOPS(aTHX); |
| 3023 | break; |
| 3024 | case 3: |
| 3025 | /* die caught by an inner eval - continue inner loop */ |
| 3026 | if (PL_restartop && PL_restartjmpenv == PL_top_env) { |
| 3027 | PL_restartjmpenv = NULL; |
| 3028 | PL_op = PL_restartop; |
| 3029 | PL_restartop = 0; |
| 3030 | goto redo_body; |
| 3031 | } |
| 3032 | /* FALL THROUGH */ |
| 3033 | default: |
| 3034 | JMPENV_POP; |
| 3035 | PL_op = oldop; |
| 3036 | JMPENV_JUMP(ret); |
| 3037 | /* NOTREACHED */ |
| 3038 | } |
| 3039 | JMPENV_POP; |
| 3040 | PL_op = oldop; |
| 3041 | return NULL; |
| 3042 | } |
| 3043 | |
| 3044 | /* James Bond: Do you expect me to talk? |
| 3045 | Auric Goldfinger: No, Mr. Bond. I expect you to die. |
| 3046 | |
| 3047 | This code is an ugly hack, doesn't work with lexicals in subroutines that are |
| 3048 | called more than once, and is only used by regcomp.c, for (?{}) blocks. |
| 3049 | |
| 3050 | Currently it is not used outside the core code. Best if it stays that way. |
| 3051 | |
| 3052 | Hence it's now deprecated, and will be removed. |
| 3053 | */ |
| 3054 | OP * |
| 3055 | Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp) |
| 3056 | /* sv Text to convert to OP tree. */ |
| 3057 | /* startop op_free() this to undo. */ |
| 3058 | /* code Short string id of the caller. */ |
| 3059 | { |
| 3060 | PERL_ARGS_ASSERT_SV_COMPILE_2OP; |
| 3061 | return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp); |
| 3062 | } |
| 3063 | |
| 3064 | /* Don't use this. It will go away without warning once the regexp engine is |
| 3065 | refactored not to use it. */ |
| 3066 | OP * |
| 3067 | Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code, |
| 3068 | PAD **padp) |
| 3069 | { |
| 3070 | dVAR; dSP; /* Make POPBLOCK work. */ |
| 3071 | PERL_CONTEXT *cx; |
| 3072 | SV **newsp; |
| 3073 | I32 gimme = G_VOID; |
| 3074 | I32 optype; |
| 3075 | OP dummy; |
| 3076 | char tbuf[TYPE_DIGITS(long) + 12 + 10]; |
| 3077 | char *tmpbuf = tbuf; |
| 3078 | char *safestr; |
| 3079 | int runtime; |
| 3080 | CV* runcv = NULL; /* initialise to avoid compiler warnings */ |
| 3081 | STRLEN len; |
| 3082 | bool need_catch; |
| 3083 | |
| 3084 | PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN; |
| 3085 | |
| 3086 | ENTER_with_name("eval"); |
| 3087 | lex_start(sv, NULL, LEX_START_SAME_FILTER); |
| 3088 | SAVETMPS; |
| 3089 | /* switch to eval mode */ |
| 3090 | |
| 3091 | if (IN_PERL_COMPILETIME) { |
| 3092 | SAVECOPSTASH_FREE(&PL_compiling); |
| 3093 | CopSTASH_set(&PL_compiling, PL_curstash); |
| 3094 | } |
| 3095 | if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) { |
| 3096 | SV * const sv = sv_newmortal(); |
| 3097 | Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]", |
| 3098 | code, (unsigned long)++PL_evalseq, |
| 3099 | CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); |
| 3100 | tmpbuf = SvPVX(sv); |
| 3101 | len = SvCUR(sv); |
| 3102 | } |
| 3103 | else |
| 3104 | len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code, |
| 3105 | (unsigned long)++PL_evalseq); |
| 3106 | SAVECOPFILE_FREE(&PL_compiling); |
| 3107 | CopFILE_set(&PL_compiling, tmpbuf+2); |
| 3108 | SAVECOPLINE(&PL_compiling); |
| 3109 | CopLINE_set(&PL_compiling, 1); |
| 3110 | /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up |
| 3111 | deleting the eval's FILEGV from the stash before gv_check() runs |
| 3112 | (i.e. before run-time proper). To work around the coredump that |
| 3113 | ensues, we always turn GvMULTI_on for any globals that were |
| 3114 | introduced within evals. See force_ident(). GSAR 96-10-12 */ |
| 3115 | safestr = savepvn(tmpbuf, len); |
| 3116 | SAVEDELETE(PL_defstash, safestr, len); |
| 3117 | SAVEHINTS(); |
| 3118 | #ifdef OP_IN_REGISTER |
| 3119 | PL_opsave = op; |
| 3120 | #else |
| 3121 | SAVEVPTR(PL_op); |
| 3122 | #endif |
| 3123 | |
| 3124 | /* we get here either during compilation, or via pp_regcomp at runtime */ |
| 3125 | runtime = IN_PERL_RUNTIME; |
| 3126 | if (runtime) |
| 3127 | { |
| 3128 | runcv = find_runcv(NULL); |
| 3129 | |
| 3130 | /* At run time, we have to fetch the hints from PL_curcop. */ |
| 3131 | PL_hints = PL_curcop->cop_hints; |
| 3132 | if (PL_hints & HINT_LOCALIZE_HH) { |
| 3133 | /* SAVEHINTS created a new HV in PL_hintgv, which we |
| 3134 | need to GC */ |
| 3135 | SvREFCNT_dec(GvHV(PL_hintgv)); |
| 3136 | GvHV(PL_hintgv) = |
| 3137 | refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0); |
| 3138 | hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints); |
| 3139 | } |
| 3140 | SAVECOMPILEWARNINGS(); |
| 3141 | PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); |
| 3142 | cophh_free(CopHINTHASH_get(&PL_compiling)); |
| 3143 | /* XXX Does this need to avoid copying a label? */ |
| 3144 | PL_compiling.cop_hints_hash |
| 3145 | = cophh_copy(PL_curcop->cop_hints_hash); |
| 3146 | } |
| 3147 | |
| 3148 | PL_op = &dummy; |
| 3149 | PL_op->op_type = OP_ENTEREVAL; |
| 3150 | PL_op->op_flags = 0; /* Avoid uninit warning. */ |
| 3151 | PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP); |
| 3152 | PUSHEVAL(cx, 0); |
| 3153 | need_catch = CATCH_GET; |
| 3154 | CATCH_SET(TRUE); |
| 3155 | |
| 3156 | if (runtime) |
| 3157 | (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq); |
| 3158 | else |
| 3159 | (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax); |
| 3160 | CATCH_SET(need_catch); |
| 3161 | POPBLOCK(cx,PL_curpm); |
| 3162 | POPEVAL(cx); |
| 3163 | |
| 3164 | (*startop)->op_type = OP_NULL; |
| 3165 | (*startop)->op_ppaddr = PL_ppaddr[OP_NULL]; |
| 3166 | /* XXX DAPM do this properly one year */ |
| 3167 | *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad)); |
| 3168 | LEAVE_with_name("eval"); |
| 3169 | if (IN_PERL_COMPILETIME) |
| 3170 | CopHINTS_set(&PL_compiling, PL_hints); |
| 3171 | #ifdef OP_IN_REGISTER |
| 3172 | op = PL_opsave; |
| 3173 | #endif |
| 3174 | PERL_UNUSED_VAR(newsp); |
| 3175 | PERL_UNUSED_VAR(optype); |
| 3176 | |
| 3177 | return PL_eval_start; |
| 3178 | } |
| 3179 | |
| 3180 | |
| 3181 | /* |
| 3182 | =for apidoc find_runcv |
| 3183 | |
| 3184 | Locate the CV corresponding to the currently executing sub or eval. |
| 3185 | If db_seqp is non_null, skip CVs that are in the DB package and populate |
| 3186 | *db_seqp with the cop sequence number at the point that the DB:: code was |
| 3187 | entered. (allows debuggers to eval in the scope of the breakpoint rather |
| 3188 | than in the scope of the debugger itself). |
| 3189 | |
| 3190 | =cut |
| 3191 | */ |
| 3192 | |
| 3193 | CV* |
| 3194 | Perl_find_runcv(pTHX_ U32 *db_seqp) |
| 3195 | { |
| 3196 | dVAR; |
| 3197 | PERL_SI *si; |
| 3198 | |
| 3199 | if (db_seqp) |
| 3200 | *db_seqp = PL_curcop->cop_seq; |
| 3201 | for (si = PL_curstackinfo; si; si = si->si_prev) { |
| 3202 | I32 ix; |
| 3203 | for (ix = si->si_cxix; ix >= 0; ix--) { |
| 3204 | const PERL_CONTEXT *cx = &(si->si_cxstack[ix]); |
| 3205 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
| 3206 | CV * const cv = cx->blk_sub.cv; |
| 3207 | /* skip DB:: code */ |
| 3208 | if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) { |
| 3209 | *db_seqp = cx->blk_oldcop->cop_seq; |
| 3210 | continue; |
| 3211 | } |
| 3212 | return cv; |
| 3213 | } |
| 3214 | else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx)) |
| 3215 | return PL_compcv; |
| 3216 | } |
| 3217 | } |
| 3218 | return PL_main_cv; |
| 3219 | } |
| 3220 | |
| 3221 | |
| 3222 | /* Run yyparse() in a setjmp wrapper. Returns: |
| 3223 | * 0: yyparse() successful |
| 3224 | * 1: yyparse() failed |
| 3225 | * 3: yyparse() died |
| 3226 | */ |
| 3227 | STATIC int |
| 3228 | S_try_yyparse(pTHX_ int gramtype) |
| 3229 | { |
| 3230 | int ret; |
| 3231 | dJMPENV; |
| 3232 | |
| 3233 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); |
| 3234 | JMPENV_PUSH(ret); |
| 3235 | switch (ret) { |
| 3236 | case 0: |
| 3237 | ret = yyparse(gramtype) ? 1 : 0; |
| 3238 | break; |
| 3239 | case 3: |
| 3240 | break; |
| 3241 | default: |
| 3242 | JMPENV_POP; |
| 3243 | JMPENV_JUMP(ret); |
| 3244 | /* NOTREACHED */ |
| 3245 | } |
| 3246 | JMPENV_POP; |
| 3247 | return ret; |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | /* Compile a require/do, an eval '', or a /(?{...})/. |
| 3252 | * In the last case, startop is non-null, and contains the address of |
| 3253 | * a pointer that should be set to the just-compiled code. |
| 3254 | * outside is the lexically enclosing CV (if any) that invoked us. |
| 3255 | * Returns a bool indicating whether the compile was successful; if so, |
| 3256 | * PL_eval_start contains the first op of the compiled ocde; otherwise, |
| 3257 | * pushes undef (also croaks if startop != NULL). |
| 3258 | */ |
| 3259 | |
| 3260 | STATIC bool |
| 3261 | S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq) |
| 3262 | { |
| 3263 | dVAR; dSP; |
| 3264 | OP * const saveop = PL_op; |
| 3265 | bool in_require = (saveop && saveop->op_type == OP_REQUIRE); |
| 3266 | int yystatus; |
| 3267 | |
| 3268 | PL_in_eval = (in_require |
| 3269 | ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL)) |
| 3270 | : EVAL_INEVAL); |
| 3271 | |
| 3272 | PUSHMARK(SP); |
| 3273 | |
| 3274 | SAVESPTR(PL_compcv); |
| 3275 | PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV)); |
| 3276 | CvEVAL_on(PL_compcv); |
| 3277 | assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL); |
| 3278 | cxstack[cxstack_ix].blk_eval.cv = PL_compcv; |
| 3279 | |
| 3280 | CvOUTSIDE_SEQ(PL_compcv) = seq; |
| 3281 | CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); |
| 3282 | |
| 3283 | /* set up a scratch pad */ |
| 3284 | |
| 3285 | CvPADLIST(PL_compcv) = pad_new(padnew_SAVE); |
| 3286 | PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */ |
| 3287 | |
| 3288 | |
| 3289 | if (!PL_madskills) |
| 3290 | SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */ |
| 3291 | |
| 3292 | /* make sure we compile in the right package */ |
| 3293 | |
| 3294 | if (CopSTASH_ne(PL_curcop, PL_curstash)) { |
| 3295 | SAVESPTR(PL_curstash); |
| 3296 | PL_curstash = CopSTASH(PL_curcop); |
| 3297 | } |
| 3298 | /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */ |
| 3299 | SAVESPTR(PL_beginav); |
| 3300 | PL_beginav = newAV(); |
| 3301 | SAVEFREESV(PL_beginav); |
| 3302 | SAVESPTR(PL_unitcheckav); |
| 3303 | PL_unitcheckav = newAV(); |
| 3304 | SAVEFREESV(PL_unitcheckav); |
| 3305 | |
| 3306 | #ifdef PERL_MAD |
| 3307 | SAVEBOOL(PL_madskills); |
| 3308 | PL_madskills = 0; |
| 3309 | #endif |
| 3310 | |
| 3311 | /* try to compile it */ |
| 3312 | |
| 3313 | PL_eval_root = NULL; |
| 3314 | PL_curcop = &PL_compiling; |
| 3315 | CopARYBASE_set(PL_curcop, 0); |
| 3316 | if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL)) |
| 3317 | PL_in_eval |= EVAL_KEEPERR; |
| 3318 | else |
| 3319 | CLEAR_ERRSV(); |
| 3320 | |
| 3321 | CALL_BLOCK_HOOKS(bhk_eval, saveop); |
| 3322 | |
| 3323 | /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>, |
| 3324 | * so honour CATCH_GET and trap it here if necessary */ |
| 3325 | |
| 3326 | yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG); |
| 3327 | |
| 3328 | if (yystatus || PL_parser->error_count || !PL_eval_root) { |
| 3329 | SV **newsp; /* Used by POPBLOCK. */ |
| 3330 | PERL_CONTEXT *cx = NULL; |
| 3331 | I32 optype; /* Used by POPEVAL. */ |
| 3332 | SV *namesv = NULL; |
| 3333 | const char *msg; |
| 3334 | |
| 3335 | PERL_UNUSED_VAR(newsp); |
| 3336 | PERL_UNUSED_VAR(optype); |
| 3337 | |
| 3338 | /* note that if yystatus == 3, then the EVAL CX block has already |
| 3339 | * been popped, and various vars restored */ |
| 3340 | PL_op = saveop; |
| 3341 | if (yystatus != 3) { |
| 3342 | if (PL_eval_root) { |
| 3343 | op_free(PL_eval_root); |
| 3344 | PL_eval_root = NULL; |
| 3345 | } |
| 3346 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
| 3347 | if (!startop) { |
| 3348 | POPBLOCK(cx,PL_curpm); |
| 3349 | POPEVAL(cx); |
| 3350 | namesv = cx->blk_eval.old_namesv; |
| 3351 | } |
| 3352 | } |
| 3353 | if (yystatus != 3) |
| 3354 | LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */ |
| 3355 | |
| 3356 | msg = SvPVx_nolen_const(ERRSV); |
| 3357 | if (in_require) { |
| 3358 | if (!cx) { |
| 3359 | /* If cx is still NULL, it means that we didn't go in the |
| 3360 | * POPEVAL branch. */ |
| 3361 | cx = &cxstack[cxstack_ix]; |
| 3362 | assert(CxTYPE(cx) == CXt_EVAL); |
| 3363 | namesv = cx->blk_eval.old_namesv; |
| 3364 | } |
| 3365 | (void)hv_store(GvHVn(PL_incgv), |
| 3366 | SvPVX_const(namesv), SvCUR(namesv), |
| 3367 | &PL_sv_undef, 0); |
| 3368 | Perl_croak(aTHX_ "%sCompilation failed in require", |
| 3369 | *msg ? msg : "Unknown error\n"); |
| 3370 | } |
| 3371 | else if (startop) { |
| 3372 | if (yystatus != 3) { |
| 3373 | POPBLOCK(cx,PL_curpm); |
| 3374 | POPEVAL(cx); |
| 3375 | } |
| 3376 | Perl_croak(aTHX_ "%sCompilation failed in regexp", |
| 3377 | (*msg ? msg : "Unknown error\n")); |
| 3378 | } |
| 3379 | else { |
| 3380 | if (!*msg) { |
| 3381 | sv_setpvs(ERRSV, "Compilation error"); |
| 3382 | } |
| 3383 | } |
| 3384 | PUSHs(&PL_sv_undef); |
| 3385 | PUTBACK; |
| 3386 | return FALSE; |
| 3387 | } |
| 3388 | CopLINE_set(&PL_compiling, 0); |
| 3389 | if (startop) { |
| 3390 | *startop = PL_eval_root; |
| 3391 | } else |
| 3392 | SAVEFREEOP(PL_eval_root); |
| 3393 | |
| 3394 | /* Set the context for this new optree. |
| 3395 | * Propagate the context from the eval(). */ |
| 3396 | if ((gimme & G_WANT) == G_VOID) |
| 3397 | scalarvoid(PL_eval_root); |
| 3398 | else if ((gimme & G_WANT) == G_ARRAY) |
| 3399 | list(PL_eval_root); |
| 3400 | else |
| 3401 | scalar(PL_eval_root); |
| 3402 | |
| 3403 | DEBUG_x(dump_eval()); |
| 3404 | |
| 3405 | /* Register with debugger: */ |
| 3406 | if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) { |
| 3407 | CV * const cv = get_cvs("DB::postponed", 0); |
| 3408 | if (cv) { |
| 3409 | dSP; |
| 3410 | PUSHMARK(SP); |
| 3411 | XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); |
| 3412 | PUTBACK; |
| 3413 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | if (PL_unitcheckav) { |
| 3418 | OP *es = PL_eval_start; |
| 3419 | call_list(PL_scopestack_ix, PL_unitcheckav); |
| 3420 | PL_eval_start = es; |
| 3421 | } |
| 3422 | |
| 3423 | /* compiled okay, so do it */ |
| 3424 | |
| 3425 | CvDEPTH(PL_compcv) = 1; |
| 3426 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
| 3427 | PL_op = saveop; /* The caller may need it. */ |
| 3428 | PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */ |
| 3429 | |
| 3430 | PUTBACK; |
| 3431 | return TRUE; |
| 3432 | } |
| 3433 | |
| 3434 | STATIC PerlIO * |
| 3435 | S_check_type_and_open(pTHX_ SV *name) |
| 3436 | { |
| 3437 | Stat_t st; |
| 3438 | const char *p = SvPV_nolen_const(name); |
| 3439 | const int st_rc = PerlLIO_stat(p, &st); |
| 3440 | |
| 3441 | PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN; |
| 3442 | |
| 3443 | if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) { |
| 3444 | return NULL; |
| 3445 | } |
| 3446 | |
| 3447 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
| 3448 | return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name); |
| 3449 | #else |
| 3450 | return PerlIO_open(p, PERL_SCRIPT_MODE); |
| 3451 | #endif |
| 3452 | } |
| 3453 | |
| 3454 | #ifndef PERL_DISABLE_PMC |
| 3455 | STATIC PerlIO * |
| 3456 | S_doopen_pm(pTHX_ SV *name) |
| 3457 | { |
| 3458 | STRLEN namelen; |
| 3459 | const char *p = SvPV_const(name, namelen); |
| 3460 | |
| 3461 | PERL_ARGS_ASSERT_DOOPEN_PM; |
| 3462 | |
| 3463 | if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) { |
| 3464 | SV *const pmcsv = sv_newmortal(); |
| 3465 | Stat_t pmcstat; |
| 3466 | |
| 3467 | SvSetSV_nosteal(pmcsv,name); |
| 3468 | sv_catpvn(pmcsv, "c", 1); |
| 3469 | |
| 3470 | if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0) |
| 3471 | return check_type_and_open(pmcsv); |
| 3472 | } |
| 3473 | return check_type_and_open(name); |
| 3474 | } |
| 3475 | #else |
| 3476 | # define doopen_pm(name) check_type_and_open(name) |
| 3477 | #endif /* !PERL_DISABLE_PMC */ |
| 3478 | |
| 3479 | PP(pp_require) |
| 3480 | { |
| 3481 | dVAR; dSP; |
| 3482 | register PERL_CONTEXT *cx; |
| 3483 | SV *sv; |
| 3484 | const char *name; |
| 3485 | STRLEN len; |
| 3486 | char * unixname; |
| 3487 | STRLEN unixlen; |
| 3488 | #ifdef VMS |
| 3489 | int vms_unixname = 0; |
| 3490 | #endif |
| 3491 | const char *tryname = NULL; |
| 3492 | SV *namesv = NULL; |
| 3493 | const I32 gimme = GIMME_V; |
| 3494 | int filter_has_file = 0; |
| 3495 | PerlIO *tryrsfp = NULL; |
| 3496 | SV *filter_cache = NULL; |
| 3497 | SV *filter_state = NULL; |
| 3498 | SV *filter_sub = NULL; |
| 3499 | SV *hook_sv = NULL; |
| 3500 | SV *encoding; |
| 3501 | OP *op; |
| 3502 | |
| 3503 | sv = POPs; |
| 3504 | if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) { |
| 3505 | sv = sv_2mortal(new_version(sv)); |
| 3506 | if (!sv_derived_from(PL_patchlevel, "version")) |
| 3507 | upg_version(PL_patchlevel, TRUE); |
| 3508 | if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) { |
| 3509 | if ( vcmp(sv,PL_patchlevel) <= 0 ) |
| 3510 | DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped", |
| 3511 | SVfARG(sv_2mortal(vnormal(sv))), |
| 3512 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) |
| 3513 | ); |
| 3514 | } |
| 3515 | else { |
| 3516 | if ( vcmp(sv,PL_patchlevel) > 0 ) { |
| 3517 | I32 first = 0; |
| 3518 | AV *lav; |
| 3519 | SV * const req = SvRV(sv); |
| 3520 | SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE); |
| 3521 | |
| 3522 | /* get the left hand term */ |
| 3523 | lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE))); |
| 3524 | |
| 3525 | first = SvIV(*av_fetch(lav,0,0)); |
| 3526 | if ( first > (int)PERL_REVISION /* probably 'use 6.0' */ |
| 3527 | || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */ |
| 3528 | || av_len(lav) > 1 /* FP with > 3 digits */ |
| 3529 | || strstr(SvPVX(pv),".0") /* FP with leading 0 */ |
| 3530 | ) { |
| 3531 | DIE(aTHX_ "Perl %"SVf" required--this is only " |
| 3532 | "%"SVf", stopped", |
| 3533 | SVfARG(sv_2mortal(vnormal(req))), |
| 3534 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) |
| 3535 | ); |
| 3536 | } |
| 3537 | else { /* probably 'use 5.10' or 'use 5.8' */ |
| 3538 | SV *hintsv; |
| 3539 | I32 second = 0; |
| 3540 | |
| 3541 | if (av_len(lav)>=1) |
| 3542 | second = SvIV(*av_fetch(lav,1,0)); |
| 3543 | |
| 3544 | second /= second >= 600 ? 100 : 10; |
| 3545 | hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0", |
| 3546 | (int)first, (int)second); |
| 3547 | upg_version(hintsv, TRUE); |
| 3548 | |
| 3549 | DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)" |
| 3550 | "--this is only %"SVf", stopped", |
| 3551 | SVfARG(sv_2mortal(vnormal(req))), |
| 3552 | SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))), |
| 3553 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) |
| 3554 | ); |
| 3555 | } |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | RETPUSHYES; |
| 3560 | } |
| 3561 | name = SvPV_const(sv, len); |
| 3562 | if (!(name && len > 0 && *name)) |
| 3563 | DIE(aTHX_ "Null filename used"); |
| 3564 | TAINT_PROPER("require"); |
| 3565 | |
| 3566 | |
| 3567 | #ifdef VMS |
| 3568 | /* The key in the %ENV hash is in the syntax of file passed as the argument |
| 3569 | * usually this is in UNIX format, but sometimes in VMS format, which |
| 3570 | * can result in a module being pulled in more than once. |
| 3571 | * To prevent this, the key must be stored in UNIX format if the VMS |
| 3572 | * name can be translated to UNIX. |
| 3573 | */ |
| 3574 | if ((unixname = tounixspec(name, NULL)) != NULL) { |
| 3575 | unixlen = strlen(unixname); |
| 3576 | vms_unixname = 1; |
| 3577 | } |
| 3578 | else |
| 3579 | #endif |
| 3580 | { |
| 3581 | /* if not VMS or VMS name can not be translated to UNIX, pass it |
| 3582 | * through. |
| 3583 | */ |
| 3584 | unixname = (char *) name; |
| 3585 | unixlen = len; |
| 3586 | } |
| 3587 | if (PL_op->op_type == OP_REQUIRE) { |
| 3588 | SV * const * const svp = hv_fetch(GvHVn(PL_incgv), |
| 3589 | unixname, unixlen, 0); |
| 3590 | if ( svp ) { |
| 3591 | if (*svp != &PL_sv_undef) |
| 3592 | RETPUSHYES; |
| 3593 | else |
| 3594 | DIE(aTHX_ "Attempt to reload %s aborted.\n" |
| 3595 | "Compilation failed in require", unixname); |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | /* prepare to compile file */ |
| 3600 | |
| 3601 | if (path_is_absolute(name)) { |
| 3602 | /* At this point, name is SvPVX(sv) */ |
| 3603 | tryname = name; |
| 3604 | tryrsfp = doopen_pm(sv); |
| 3605 | } |
| 3606 | if (!tryrsfp) { |
| 3607 | AV * const ar = GvAVn(PL_incgv); |
| 3608 | I32 i; |
| 3609 | #ifdef VMS |
| 3610 | if (vms_unixname) |
| 3611 | #endif |
| 3612 | { |
| 3613 | namesv = newSV_type(SVt_PV); |
| 3614 | for (i = 0; i <= AvFILL(ar); i++) { |
| 3615 | SV * const dirsv = *av_fetch(ar, i, TRUE); |
| 3616 | |
| 3617 | if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied)) |
| 3618 | mg_get(dirsv); |
| 3619 | if (SvROK(dirsv)) { |
| 3620 | int count; |
| 3621 | SV **svp; |
| 3622 | SV *loader = dirsv; |
| 3623 | |
| 3624 | if (SvTYPE(SvRV(loader)) == SVt_PVAV |
| 3625 | && !sv_isobject(loader)) |
| 3626 | { |
| 3627 | loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE); |
| 3628 | } |
| 3629 | |
| 3630 | Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s", |
| 3631 | PTR2UV(SvRV(dirsv)), name); |
| 3632 | tryname = SvPVX_const(namesv); |
| 3633 | tryrsfp = NULL; |
| 3634 | |
| 3635 | ENTER_with_name("call_INC"); |
| 3636 | SAVETMPS; |
| 3637 | EXTEND(SP, 2); |
| 3638 | |
| 3639 | PUSHMARK(SP); |
| 3640 | PUSHs(dirsv); |
| 3641 | PUSHs(sv); |
| 3642 | PUTBACK; |
| 3643 | if (sv_isobject(loader)) |
| 3644 | count = call_method("INC", G_ARRAY); |
| 3645 | else |
| 3646 | count = call_sv(loader, G_ARRAY); |
| 3647 | SPAGAIN; |
| 3648 | |
| 3649 | if (count > 0) { |
| 3650 | int i = 0; |
| 3651 | SV *arg; |
| 3652 | |
| 3653 | SP -= count - 1; |
| 3654 | arg = SP[i++]; |
| 3655 | |
| 3656 | if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV) |
| 3657 | && !isGV_with_GP(SvRV(arg))) { |
| 3658 | filter_cache = SvRV(arg); |
| 3659 | SvREFCNT_inc_simple_void_NN(filter_cache); |
| 3660 | |
| 3661 | if (i < count) { |
| 3662 | arg = SP[i++]; |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | if (SvROK(arg) && isGV_with_GP(SvRV(arg))) { |
| 3667 | arg = SvRV(arg); |
| 3668 | } |
| 3669 | |
| 3670 | if (isGV_with_GP(arg)) { |
| 3671 | IO * const io = GvIO((const GV *)arg); |
| 3672 | |
| 3673 | ++filter_has_file; |
| 3674 | |
| 3675 | if (io) { |
| 3676 | tryrsfp = IoIFP(io); |
| 3677 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { |
| 3678 | PerlIO_close(IoOFP(io)); |
| 3679 | } |
| 3680 | IoIFP(io) = NULL; |
| 3681 | IoOFP(io) = NULL; |
| 3682 | } |
| 3683 | |
| 3684 | if (i < count) { |
| 3685 | arg = SP[i++]; |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) { |
| 3690 | filter_sub = arg; |
| 3691 | SvREFCNT_inc_simple_void_NN(filter_sub); |
| 3692 | |
| 3693 | if (i < count) { |
| 3694 | filter_state = SP[i]; |
| 3695 | SvREFCNT_inc_simple_void(filter_state); |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | if (!tryrsfp && (filter_cache || filter_sub)) { |
| 3700 | tryrsfp = PerlIO_open(BIT_BUCKET, |
| 3701 | PERL_SCRIPT_MODE); |
| 3702 | } |
| 3703 | SP--; |
| 3704 | } |
| 3705 | |
| 3706 | PUTBACK; |
| 3707 | FREETMPS; |
| 3708 | LEAVE_with_name("call_INC"); |
| 3709 | |
| 3710 | /* Adjust file name if the hook has set an %INC entry. |
| 3711 | This needs to happen after the FREETMPS above. */ |
| 3712 | svp = hv_fetch(GvHVn(PL_incgv), name, len, 0); |
| 3713 | if (svp) |
| 3714 | tryname = SvPV_nolen_const(*svp); |
| 3715 | |
| 3716 | if (tryrsfp) { |
| 3717 | hook_sv = dirsv; |
| 3718 | break; |
| 3719 | } |
| 3720 | |
| 3721 | filter_has_file = 0; |
| 3722 | if (filter_cache) { |
| 3723 | SvREFCNT_dec(filter_cache); |
| 3724 | filter_cache = NULL; |
| 3725 | } |
| 3726 | if (filter_state) { |
| 3727 | SvREFCNT_dec(filter_state); |
| 3728 | filter_state = NULL; |
| 3729 | } |
| 3730 | if (filter_sub) { |
| 3731 | SvREFCNT_dec(filter_sub); |
| 3732 | filter_sub = NULL; |
| 3733 | } |
| 3734 | } |
| 3735 | else { |
| 3736 | if (!path_is_absolute(name) |
| 3737 | ) { |
| 3738 | const char *dir; |
| 3739 | STRLEN dirlen; |
| 3740 | |
| 3741 | if (SvOK(dirsv)) { |
| 3742 | dir = SvPV_const(dirsv, dirlen); |
| 3743 | } else { |
| 3744 | dir = ""; |
| 3745 | dirlen = 0; |
| 3746 | } |
| 3747 | |
| 3748 | #ifdef VMS |
| 3749 | char *unixdir; |
| 3750 | if ((unixdir = tounixpath(dir, NULL)) == NULL) |
| 3751 | continue; |
| 3752 | sv_setpv(namesv, unixdir); |
| 3753 | sv_catpv(namesv, unixname); |
| 3754 | #else |
| 3755 | # ifdef __SYMBIAN32__ |
| 3756 | if (PL_origfilename[0] && |
| 3757 | PL_origfilename[1] == ':' && |
| 3758 | !(dir[0] && dir[1] == ':')) |
| 3759 | Perl_sv_setpvf(aTHX_ namesv, |
| 3760 | "%c:%s\\%s", |
| 3761 | PL_origfilename[0], |
| 3762 | dir, name); |
| 3763 | else |
| 3764 | Perl_sv_setpvf(aTHX_ namesv, |
| 3765 | "%s\\%s", |
| 3766 | dir, name); |
| 3767 | # else |
| 3768 | /* The equivalent of |
| 3769 | Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name); |
| 3770 | but without the need to parse the format string, or |
| 3771 | call strlen on either pointer, and with the correct |
| 3772 | allocation up front. */ |
| 3773 | { |
| 3774 | char *tmp = SvGROW(namesv, dirlen + len + 2); |
| 3775 | |
| 3776 | memcpy(tmp, dir, dirlen); |
| 3777 | tmp +=dirlen; |
| 3778 | *tmp++ = '/'; |
| 3779 | /* name came from an SV, so it will have a '\0' at the |
| 3780 | end that we can copy as part of this memcpy(). */ |
| 3781 | memcpy(tmp, name, len + 1); |
| 3782 | |
| 3783 | SvCUR_set(namesv, dirlen + len + 1); |
| 3784 | SvPOK_on(namesv); |
| 3785 | } |
| 3786 | # endif |
| 3787 | #endif |
| 3788 | TAINT_PROPER("require"); |
| 3789 | tryname = SvPVX_const(namesv); |
| 3790 | tryrsfp = doopen_pm(namesv); |
| 3791 | if (tryrsfp) { |
| 3792 | if (tryname[0] == '.' && tryname[1] == '/') { |
| 3793 | ++tryname; |
| 3794 | while (*++tryname == '/'); |
| 3795 | } |
| 3796 | break; |
| 3797 | } |
| 3798 | else if (errno == EMFILE) |
| 3799 | /* no point in trying other paths if out of handles */ |
| 3800 | break; |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | } |
| 3805 | } |
| 3806 | sv_2mortal(namesv); |
| 3807 | if (!tryrsfp) { |
| 3808 | if (PL_op->op_type == OP_REQUIRE) { |
| 3809 | if(errno == EMFILE) { |
| 3810 | /* diag_listed_as: Can't locate %s */ |
| 3811 | DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno)); |
| 3812 | } else { |
| 3813 | if (namesv) { /* did we lookup @INC? */ |
| 3814 | AV * const ar = GvAVn(PL_incgv); |
| 3815 | I32 i; |
| 3816 | SV *const inc = newSVpvs_flags("", SVs_TEMP); |
| 3817 | for (i = 0; i <= AvFILL(ar); i++) { |
| 3818 | sv_catpvs(inc, " "); |
| 3819 | sv_catsv(inc, *av_fetch(ar, i, TRUE)); |
| 3820 | } |
| 3821 | |
| 3822 | /* diag_listed_as: Can't locate %s */ |
| 3823 | DIE(aTHX_ |
| 3824 | "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")", |
| 3825 | name, |
| 3826 | (memEQ(name + len - 2, ".h", 3) |
| 3827 | ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""), |
| 3828 | (memEQ(name + len - 3, ".ph", 4) |
| 3829 | ? " (did you run h2ph?)" : ""), |
| 3830 | inc |
| 3831 | ); |
| 3832 | } |
| 3833 | } |
| 3834 | DIE(aTHX_ "Can't locate %s", name); |
| 3835 | } |
| 3836 | |
| 3837 | RETPUSHUNDEF; |
| 3838 | } |
| 3839 | else |
| 3840 | SETERRNO(0, SS_NORMAL); |
| 3841 | |
| 3842 | /* Assume success here to prevent recursive requirement. */ |
| 3843 | /* name is never assigned to again, so len is still strlen(name) */ |
| 3844 | /* Check whether a hook in @INC has already filled %INC */ |
| 3845 | if (!hook_sv) { |
| 3846 | (void)hv_store(GvHVn(PL_incgv), |
| 3847 | unixname, unixlen, newSVpv(tryname,0),0); |
| 3848 | } else { |
| 3849 | SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0); |
| 3850 | if (!svp) |
| 3851 | (void)hv_store(GvHVn(PL_incgv), |
| 3852 | unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 ); |
| 3853 | } |
| 3854 | |
| 3855 | ENTER_with_name("eval"); |
| 3856 | SAVETMPS; |
| 3857 | SAVECOPFILE_FREE(&PL_compiling); |
| 3858 | CopFILE_set(&PL_compiling, tryname); |
| 3859 | lex_start(NULL, tryrsfp, 0); |
| 3860 | |
| 3861 | SAVEHINTS(); |
| 3862 | PL_hints = 0; |
| 3863 | hv_clear(GvHV(PL_hintgv)); |
| 3864 | |
| 3865 | SAVECOMPILEWARNINGS(); |
| 3866 | if (PL_dowarn & G_WARN_ALL_ON) |
| 3867 | PL_compiling.cop_warnings = pWARN_ALL ; |
| 3868 | else if (PL_dowarn & G_WARN_ALL_OFF) |
| 3869 | PL_compiling.cop_warnings = pWARN_NONE ; |
| 3870 | else |
| 3871 | PL_compiling.cop_warnings = pWARN_STD ; |
| 3872 | |
| 3873 | if (filter_sub || filter_cache) { |
| 3874 | /* We can use the SvPV of the filter PVIO itself as our cache, rather |
| 3875 | than hanging another SV from it. In turn, filter_add() optionally |
| 3876 | takes the SV to use as the filter (or creates a new SV if passed |
| 3877 | NULL), so simply pass in whatever value filter_cache has. */ |
| 3878 | SV * const datasv = filter_add(S_run_user_filter, filter_cache); |
| 3879 | IoLINES(datasv) = filter_has_file; |
| 3880 | IoTOP_GV(datasv) = MUTABLE_GV(filter_state); |
| 3881 | IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub); |
| 3882 | } |
| 3883 | |
| 3884 | /* switch to eval mode */ |
| 3885 | PUSHBLOCK(cx, CXt_EVAL, SP); |
| 3886 | PUSHEVAL(cx, name); |
| 3887 | cx->blk_eval.retop = PL_op->op_next; |
| 3888 | |
| 3889 | SAVECOPLINE(&PL_compiling); |
| 3890 | CopLINE_set(&PL_compiling, 0); |
| 3891 | |
| 3892 | PUTBACK; |
| 3893 | |
| 3894 | /* Store and reset encoding. */ |
| 3895 | encoding = PL_encoding; |
| 3896 | PL_encoding = NULL; |
| 3897 | |
| 3898 | if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq)) |
| 3899 | op = DOCATCH(PL_eval_start); |
| 3900 | else |
| 3901 | op = PL_op->op_next; |
| 3902 | |
| 3903 | /* Restore encoding. */ |
| 3904 | PL_encoding = encoding; |
| 3905 | |
| 3906 | return op; |
| 3907 | } |
| 3908 | |
| 3909 | /* This is a op added to hold the hints hash for |
| 3910 | pp_entereval. The hash can be modified by the code |
| 3911 | being eval'ed, so we return a copy instead. */ |
| 3912 | |
| 3913 | PP(pp_hintseval) |
| 3914 | { |
| 3915 | dVAR; |
| 3916 | dSP; |
| 3917 | mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv)))); |
| 3918 | RETURN; |
| 3919 | } |
| 3920 | |
| 3921 | |
| 3922 | PP(pp_entereval) |
| 3923 | { |
| 3924 | dVAR; dSP; |
| 3925 | register PERL_CONTEXT *cx; |
| 3926 | SV *sv; |
| 3927 | const I32 gimme = GIMME_V; |
| 3928 | const U32 was = PL_breakable_sub_gen; |
| 3929 | char tbuf[TYPE_DIGITS(long) + 12]; |
| 3930 | bool saved_delete = FALSE; |
| 3931 | char *tmpbuf = tbuf; |
| 3932 | STRLEN len; |
| 3933 | CV* runcv; |
| 3934 | U32 seq; |
| 3935 | HV *saved_hh = NULL; |
| 3936 | |
| 3937 | if (PL_op->op_private & OPpEVAL_HAS_HH) { |
| 3938 | saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs)); |
| 3939 | } |
| 3940 | sv = POPs; |
| 3941 | if (!SvPOK(sv)) { |
| 3942 | /* make sure we've got a plain PV (no overload etc) before testing |
| 3943 | * for taint. Making a copy here is probably overkill, but better |
| 3944 | * safe than sorry */ |
| 3945 | STRLEN len; |
| 3946 | const char * const p = SvPV_const(sv, len); |
| 3947 | |
| 3948 | sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv)); |
| 3949 | } |
| 3950 | |
| 3951 | TAINT_IF(SvTAINTED(sv)); |
| 3952 | TAINT_PROPER("eval"); |
| 3953 | |
| 3954 | ENTER_with_name("eval"); |
| 3955 | lex_start(sv, NULL, LEX_START_SAME_FILTER); |
| 3956 | SAVETMPS; |
| 3957 | |
| 3958 | /* switch to eval mode */ |
| 3959 | |
| 3960 | if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) { |
| 3961 | SV * const temp_sv = sv_newmortal(); |
| 3962 | Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]", |
| 3963 | (unsigned long)++PL_evalseq, |
| 3964 | CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); |
| 3965 | tmpbuf = SvPVX(temp_sv); |
| 3966 | len = SvCUR(temp_sv); |
| 3967 | } |
| 3968 | else |
| 3969 | len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq); |
| 3970 | SAVECOPFILE_FREE(&PL_compiling); |
| 3971 | CopFILE_set(&PL_compiling, tmpbuf+2); |
| 3972 | SAVECOPLINE(&PL_compiling); |
| 3973 | CopLINE_set(&PL_compiling, 1); |
| 3974 | /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up |
| 3975 | deleting the eval's FILEGV from the stash before gv_check() runs |
| 3976 | (i.e. before run-time proper). To work around the coredump that |
| 3977 | ensues, we always turn GvMULTI_on for any globals that were |
| 3978 | introduced within evals. See force_ident(). GSAR 96-10-12 */ |
| 3979 | SAVEHINTS(); |
| 3980 | PL_hints = PL_op->op_targ; |
| 3981 | if (saved_hh) { |
| 3982 | /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */ |
| 3983 | SvREFCNT_dec(GvHV(PL_hintgv)); |
| 3984 | GvHV(PL_hintgv) = saved_hh; |
| 3985 | } |
| 3986 | SAVECOMPILEWARNINGS(); |
| 3987 | PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); |
| 3988 | cophh_free(CopHINTHASH_get(&PL_compiling)); |
| 3989 | if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) { |
| 3990 | /* The label, if present, is the first entry on the chain. So rather |
| 3991 | than writing a blank label in front of it (which involves an |
| 3992 | allocation), just use the next entry in the chain. */ |
| 3993 | PL_compiling.cop_hints_hash |
| 3994 | = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next); |
| 3995 | /* Check the assumption that this removed the label. */ |
| 3996 | assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL); |
| 3997 | } |
| 3998 | else |
| 3999 | PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash); |
| 4000 | /* special case: an eval '' executed within the DB package gets lexically |
| 4001 | * placed in the first non-DB CV rather than the current CV - this |
| 4002 | * allows the debugger to execute code, find lexicals etc, in the |
| 4003 | * scope of the code being debugged. Passing &seq gets find_runcv |
| 4004 | * to do the dirty work for us */ |
| 4005 | runcv = find_runcv(&seq); |
| 4006 | |
| 4007 | PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP); |
| 4008 | PUSHEVAL(cx, 0); |
| 4009 | cx->blk_eval.retop = PL_op->op_next; |
| 4010 | |
| 4011 | /* prepare to compile string */ |
| 4012 | |
| 4013 | if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash) |
| 4014 | save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr); |
| 4015 | else { |
| 4016 | char *const safestr = savepvn(tmpbuf, len); |
| 4017 | SAVEDELETE(PL_defstash, safestr, len); |
| 4018 | saved_delete = TRUE; |
| 4019 | } |
| 4020 | |
| 4021 | PUTBACK; |
| 4022 | |
| 4023 | if (doeval(gimme, NULL, runcv, seq)) { |
| 4024 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ |
| 4025 | ? (PERLDB_LINE || PERLDB_SAVESRC) |
| 4026 | : PERLDB_SAVESRC_NOSUBS) { |
| 4027 | /* Retain the filegv we created. */ |
| 4028 | } else if (!saved_delete) { |
| 4029 | char *const safestr = savepvn(tmpbuf, len); |
| 4030 | SAVEDELETE(PL_defstash, safestr, len); |
| 4031 | } |
| 4032 | return DOCATCH(PL_eval_start); |
| 4033 | } else { |
| 4034 | /* We have already left the scope set up earlier thanks to the LEAVE |
| 4035 | in doeval(). */ |
| 4036 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ |
| 4037 | ? (PERLDB_LINE || PERLDB_SAVESRC) |
| 4038 | : PERLDB_SAVESRC_INVALID) { |
| 4039 | /* Retain the filegv we created. */ |
| 4040 | } else if (!saved_delete) { |
| 4041 | (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD); |
| 4042 | } |
| 4043 | return PL_op->op_next; |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | PP(pp_leaveeval) |
| 4048 | { |
| 4049 | dVAR; dSP; |
| 4050 | register SV **mark; |
| 4051 | SV **newsp; |
| 4052 | PMOP *newpm; |
| 4053 | I32 gimme; |
| 4054 | register PERL_CONTEXT *cx; |
| 4055 | OP *retop; |
| 4056 | const U8 save_flags = PL_op -> op_flags; |
| 4057 | I32 optype; |
| 4058 | SV *namesv; |
| 4059 | |
| 4060 | PERL_ASYNC_CHECK(); |
| 4061 | POPBLOCK(cx,newpm); |
| 4062 | POPEVAL(cx); |
| 4063 | namesv = cx->blk_eval.old_namesv; |
| 4064 | retop = cx->blk_eval.retop; |
| 4065 | |
| 4066 | TAINT_NOT; |
| 4067 | if (gimme == G_VOID) |
| 4068 | MARK = newsp; |
| 4069 | else if (gimme == G_SCALAR) { |
| 4070 | MARK = newsp + 1; |
| 4071 | if (MARK <= SP) { |
| 4072 | if (SvFLAGS(TOPs) & SVs_TEMP) |
| 4073 | *MARK = TOPs; |
| 4074 | else |
| 4075 | *MARK = sv_mortalcopy(TOPs); |
| 4076 | } |
| 4077 | else { |
| 4078 | MEXTEND(mark,0); |
| 4079 | *MARK = &PL_sv_undef; |
| 4080 | } |
| 4081 | SP = MARK; |
| 4082 | } |
| 4083 | else { |
| 4084 | /* in case LEAVE wipes old return values */ |
| 4085 | for (mark = newsp + 1; mark <= SP; mark++) { |
| 4086 | if (!(SvFLAGS(*mark) & SVs_TEMP)) { |
| 4087 | *mark = sv_mortalcopy(*mark); |
| 4088 | TAINT_NOT; /* Each item is independent */ |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | PL_curpm = newpm; /* Don't pop $1 et al till now */ |
| 4093 | |
| 4094 | #ifdef DEBUGGING |
| 4095 | assert(CvDEPTH(PL_compcv) == 1); |
| 4096 | #endif |
| 4097 | CvDEPTH(PL_compcv) = 0; |
| 4098 | |
| 4099 | if (optype == OP_REQUIRE && |
| 4100 | !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp)) |
| 4101 | { |
| 4102 | /* Unassume the success we assumed earlier. */ |
| 4103 | (void)hv_delete(GvHVn(PL_incgv), |
| 4104 | SvPVX_const(namesv), SvCUR(namesv), |
| 4105 | G_DISCARD); |
| 4106 | retop = Perl_die(aTHX_ "%"SVf" did not return a true value", |
| 4107 | SVfARG(namesv)); |
| 4108 | /* die_unwind() did LEAVE, or we won't be here */ |
| 4109 | } |
| 4110 | else { |
| 4111 | LEAVE_with_name("eval"); |
| 4112 | if (!(save_flags & OPf_SPECIAL)) { |
| 4113 | CLEAR_ERRSV(); |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | RETURNOP(retop); |
| 4118 | } |
| 4119 | |
| 4120 | /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it |
| 4121 | close to the related Perl_create_eval_scope. */ |
| 4122 | void |
| 4123 | Perl_delete_eval_scope(pTHX) |
| 4124 | { |
| 4125 | SV **newsp; |
| 4126 | PMOP *newpm; |
| 4127 | I32 gimme; |
| 4128 | register PERL_CONTEXT *cx; |
| 4129 | I32 optype; |
| 4130 | |
| 4131 | POPBLOCK(cx,newpm); |
| 4132 | POPEVAL(cx); |
| 4133 | PL_curpm = newpm; |
| 4134 | LEAVE_with_name("eval_scope"); |
| 4135 | PERL_UNUSED_VAR(newsp); |
| 4136 | PERL_UNUSED_VAR(gimme); |
| 4137 | PERL_UNUSED_VAR(optype); |
| 4138 | } |
| 4139 | |
| 4140 | /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was |
| 4141 | also needed by Perl_fold_constants. */ |
| 4142 | PERL_CONTEXT * |
| 4143 | Perl_create_eval_scope(pTHX_ U32 flags) |
| 4144 | { |
| 4145 | PERL_CONTEXT *cx; |
| 4146 | const I32 gimme = GIMME_V; |
| 4147 | |
| 4148 | ENTER_with_name("eval_scope"); |
| 4149 | SAVETMPS; |
| 4150 | |
| 4151 | PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp); |
| 4152 | PUSHEVAL(cx, 0); |
| 4153 | |
| 4154 | PL_in_eval = EVAL_INEVAL; |
| 4155 | if (flags & G_KEEPERR) |
| 4156 | PL_in_eval |= EVAL_KEEPERR; |
| 4157 | else |
| 4158 | CLEAR_ERRSV(); |
| 4159 | if (flags & G_FAKINGEVAL) { |
| 4160 | PL_eval_root = PL_op; /* Only needed so that goto works right. */ |
| 4161 | } |
| 4162 | return cx; |
| 4163 | } |
| 4164 | |
| 4165 | PP(pp_entertry) |
| 4166 | { |
| 4167 | dVAR; |
| 4168 | PERL_CONTEXT * const cx = create_eval_scope(0); |
| 4169 | cx->blk_eval.retop = cLOGOP->op_other->op_next; |
| 4170 | return DOCATCH(PL_op->op_next); |
| 4171 | } |
| 4172 | |
| 4173 | PP(pp_leavetry) |
| 4174 | { |
| 4175 | dVAR; dSP; |
| 4176 | SV **newsp; |
| 4177 | PMOP *newpm; |
| 4178 | I32 gimme; |
| 4179 | register PERL_CONTEXT *cx; |
| 4180 | I32 optype; |
| 4181 | |
| 4182 | PERL_ASYNC_CHECK(); |
| 4183 | POPBLOCK(cx,newpm); |
| 4184 | POPEVAL(cx); |
| 4185 | PERL_UNUSED_VAR(optype); |
| 4186 | |
| 4187 | TAINT_NOT; |
| 4188 | if (gimme == G_VOID) |
| 4189 | SP = newsp; |
| 4190 | else if (gimme == G_SCALAR) { |
| 4191 | register SV **mark; |
| 4192 | MARK = newsp + 1; |
| 4193 | if (MARK <= SP) { |
| 4194 | if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)) |
| 4195 | *MARK = TOPs; |
| 4196 | else |
| 4197 | *MARK = sv_mortalcopy(TOPs); |
| 4198 | } |
| 4199 | else { |
| 4200 | MEXTEND(mark,0); |
| 4201 | *MARK = &PL_sv_undef; |
| 4202 | } |
| 4203 | SP = MARK; |
| 4204 | } |
| 4205 | else { |
| 4206 | /* in case LEAVE wipes old return values */ |
| 4207 | register SV **mark; |
| 4208 | for (mark = newsp + 1; mark <= SP; mark++) { |
| 4209 | if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) { |
| 4210 | *mark = sv_mortalcopy(*mark); |
| 4211 | TAINT_NOT; /* Each item is independent */ |
| 4212 | } |
| 4213 | } |
| 4214 | } |
| 4215 | PL_curpm = newpm; /* Don't pop $1 et al till now */ |
| 4216 | |
| 4217 | LEAVE_with_name("eval_scope"); |
| 4218 | CLEAR_ERRSV(); |
| 4219 | RETURN; |
| 4220 | } |
| 4221 | |
| 4222 | PP(pp_entergiven) |
| 4223 | { |
| 4224 | dVAR; dSP; |
| 4225 | register PERL_CONTEXT *cx; |
| 4226 | const I32 gimme = GIMME_V; |
| 4227 | |
| 4228 | ENTER_with_name("given"); |
| 4229 | SAVETMPS; |
| 4230 | |
| 4231 | sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs); |
| 4232 | |
| 4233 | PUSHBLOCK(cx, CXt_GIVEN, SP); |
| 4234 | PUSHGIVEN(cx); |
| 4235 | |
| 4236 | RETURN; |
| 4237 | } |
| 4238 | |
| 4239 | PP(pp_leavegiven) |
| 4240 | { |
| 4241 | dVAR; dSP; |
| 4242 | register PERL_CONTEXT *cx; |
| 4243 | I32 gimme; |
| 4244 | SV **newsp; |
| 4245 | PMOP *newpm; |
| 4246 | PERL_UNUSED_CONTEXT; |
| 4247 | |
| 4248 | POPBLOCK(cx,newpm); |
| 4249 | assert(CxTYPE(cx) == CXt_GIVEN); |
| 4250 | |
| 4251 | TAINT_NOT; |
| 4252 | if (gimme == G_VOID) |
| 4253 | SP = newsp; |
| 4254 | else if (gimme == G_SCALAR) { |
| 4255 | register SV **mark; |
| 4256 | MARK = newsp + 1; |
| 4257 | if (MARK <= SP) { |
| 4258 | if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)) |
| 4259 | *MARK = TOPs; |
| 4260 | else |
| 4261 | *MARK = sv_mortalcopy(TOPs); |
| 4262 | } |
| 4263 | else { |
| 4264 | MEXTEND(mark,0); |
| 4265 | *MARK = &PL_sv_undef; |
| 4266 | } |
| 4267 | SP = MARK; |
| 4268 | } |
| 4269 | else { |
| 4270 | /* in case LEAVE wipes old return values */ |
| 4271 | register SV **mark; |
| 4272 | for (mark = newsp + 1; mark <= SP; mark++) { |
| 4273 | if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) { |
| 4274 | *mark = sv_mortalcopy(*mark); |
| 4275 | TAINT_NOT; /* Each item is independent */ |
| 4276 | } |
| 4277 | } |
| 4278 | } |
| 4279 | PL_curpm = newpm; /* Don't pop $1 et al till now */ |
| 4280 | |
| 4281 | LEAVE_with_name("given"); |
| 4282 | RETURN; |
| 4283 | } |
| 4284 | |
| 4285 | /* Helper routines used by pp_smartmatch */ |
| 4286 | STATIC PMOP * |
| 4287 | S_make_matcher(pTHX_ REGEXP *re) |
| 4288 | { |
| 4289 | dVAR; |
| 4290 | PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED); |
| 4291 | |
| 4292 | PERL_ARGS_ASSERT_MAKE_MATCHER; |
| 4293 | |
| 4294 | PM_SETRE(matcher, ReREFCNT_inc(re)); |
| 4295 | |
| 4296 | SAVEFREEOP((OP *) matcher); |
| 4297 | ENTER_with_name("matcher"); SAVETMPS; |
| 4298 | SAVEOP(); |
| 4299 | return matcher; |
| 4300 | } |
| 4301 | |
| 4302 | STATIC bool |
| 4303 | S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv) |
| 4304 | { |
| 4305 | dVAR; |
| 4306 | dSP; |
| 4307 | |
| 4308 | PERL_ARGS_ASSERT_MATCHER_MATCHES_SV; |
| 4309 | |
| 4310 | PL_op = (OP *) matcher; |
| 4311 | XPUSHs(sv); |
| 4312 | PUTBACK; |
| 4313 | (void) Perl_pp_match(aTHX); |
| 4314 | SPAGAIN; |
| 4315 | return (SvTRUEx(POPs)); |
| 4316 | } |
| 4317 | |
| 4318 | STATIC void |
| 4319 | S_destroy_matcher(pTHX_ PMOP *matcher) |
| 4320 | { |
| 4321 | dVAR; |
| 4322 | |
| 4323 | PERL_ARGS_ASSERT_DESTROY_MATCHER; |
| 4324 | PERL_UNUSED_ARG(matcher); |
| 4325 | |
| 4326 | FREETMPS; |
| 4327 | LEAVE_with_name("matcher"); |
| 4328 | } |
| 4329 | |
| 4330 | /* Do a smart match */ |
| 4331 | PP(pp_smartmatch) |
| 4332 | { |
| 4333 | DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n")); |
| 4334 | return do_smartmatch(NULL, NULL); |
| 4335 | } |
| 4336 | |
| 4337 | /* This version of do_smartmatch() implements the |
| 4338 | * table of smart matches that is found in perlsyn. |
| 4339 | */ |
| 4340 | STATIC OP * |
| 4341 | S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other) |
| 4342 | { |
| 4343 | dVAR; |
| 4344 | dSP; |
| 4345 | |
| 4346 | bool object_on_left = FALSE; |
| 4347 | SV *e = TOPs; /* e is for 'expression' */ |
| 4348 | SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */ |
| 4349 | |
| 4350 | /* Take care only to invoke mg_get() once for each argument. |
| 4351 | * Currently we do this by copying the SV if it's magical. */ |
| 4352 | if (d) { |
| 4353 | if (SvGMAGICAL(d)) |
| 4354 | d = sv_mortalcopy(d); |
| 4355 | } |
| 4356 | else |
| 4357 | d = &PL_sv_undef; |
| 4358 | |
| 4359 | assert(e); |
| 4360 | if (SvGMAGICAL(e)) |
| 4361 | e = sv_mortalcopy(e); |
| 4362 | |
| 4363 | /* First of all, handle overload magic of the rightmost argument */ |
| 4364 | if (SvAMAGIC(e)) { |
| 4365 | SV * tmpsv; |
| 4366 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); |
| 4367 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); |
| 4368 | |
| 4369 | tmpsv = amagic_call(d, e, smart_amg, 0); |
| 4370 | if (tmpsv) { |
| 4371 | SPAGAIN; |
| 4372 | (void)POPs; |
| 4373 | SETs(tmpsv); |
| 4374 | RETURN; |
| 4375 | } |
| 4376 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n")); |
| 4377 | } |
| 4378 | |
| 4379 | SP -= 2; /* Pop the values */ |
| 4380 | |
| 4381 | |
| 4382 | /* ~~ undef */ |
| 4383 | if (!SvOK(e)) { |
| 4384 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n")); |
| 4385 | if (SvOK(d)) |
| 4386 | RETPUSHNO; |
| 4387 | else |
| 4388 | RETPUSHYES; |
| 4389 | } |
| 4390 | |
| 4391 | if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) { |
| 4392 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); |
| 4393 | Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation"); |
| 4394 | } |
| 4395 | if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP)) |
| 4396 | object_on_left = TRUE; |
| 4397 | |
| 4398 | /* ~~ sub */ |
| 4399 | if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) { |
| 4400 | I32 c; |
| 4401 | if (object_on_left) { |
| 4402 | goto sm_any_sub; /* Treat objects like scalars */ |
| 4403 | } |
| 4404 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { |
| 4405 | /* Test sub truth for each key */ |
| 4406 | HE *he; |
| 4407 | bool andedresults = TRUE; |
| 4408 | HV *hv = (HV*) SvRV(d); |
| 4409 | I32 numkeys = hv_iterinit(hv); |
| 4410 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n")); |
| 4411 | if (numkeys == 0) |
| 4412 | RETPUSHYES; |
| 4413 | while ( (he = hv_iternext(hv)) ) { |
| 4414 | DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n")); |
| 4415 | ENTER_with_name("smartmatch_hash_key_test"); |
| 4416 | SAVETMPS; |
| 4417 | PUSHMARK(SP); |
| 4418 | PUSHs(hv_iterkeysv(he)); |
| 4419 | PUTBACK; |
| 4420 | c = call_sv(e, G_SCALAR); |
| 4421 | SPAGAIN; |
| 4422 | if (c == 0) |
| 4423 | andedresults = FALSE; |
| 4424 | else |
| 4425 | andedresults = SvTRUEx(POPs) && andedresults; |
| 4426 | FREETMPS; |
| 4427 | LEAVE_with_name("smartmatch_hash_key_test"); |
| 4428 | } |
| 4429 | if (andedresults) |
| 4430 | RETPUSHYES; |
| 4431 | else |
| 4432 | RETPUSHNO; |
| 4433 | } |
| 4434 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { |
| 4435 | /* Test sub truth for each element */ |
| 4436 | I32 i; |
| 4437 | bool andedresults = TRUE; |
| 4438 | AV *av = (AV*) SvRV(d); |
| 4439 | const I32 len = av_len(av); |
| 4440 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n")); |
| 4441 | if (len == -1) |
| 4442 | RETPUSHYES; |
| 4443 | for (i = 0; i <= len; ++i) { |
| 4444 | SV * const * const svp = av_fetch(av, i, FALSE); |
| 4445 | DEBUG_M(Perl_deb(aTHX_ " testing array element...\n")); |
| 4446 | ENTER_with_name("smartmatch_array_elem_test"); |
| 4447 | SAVETMPS; |
| 4448 | PUSHMARK(SP); |
| 4449 | if (svp) |
| 4450 | PUSHs(*svp); |
| 4451 | PUTBACK; |
| 4452 | c = call_sv(e, G_SCALAR); |
| 4453 | SPAGAIN; |
| 4454 | if (c == 0) |
| 4455 | andedresults = FALSE; |
| 4456 | else |
| 4457 | andedresults = SvTRUEx(POPs) && andedresults; |
| 4458 | FREETMPS; |
| 4459 | LEAVE_with_name("smartmatch_array_elem_test"); |
| 4460 | } |
| 4461 | if (andedresults) |
| 4462 | RETPUSHYES; |
| 4463 | else |
| 4464 | RETPUSHNO; |
| 4465 | } |
| 4466 | else { |
| 4467 | sm_any_sub: |
| 4468 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n")); |
| 4469 | ENTER_with_name("smartmatch_coderef"); |
| 4470 | SAVETMPS; |
| 4471 | PUSHMARK(SP); |
| 4472 | PUSHs(d); |
| 4473 | PUTBACK; |
| 4474 | c = call_sv(e, G_SCALAR); |
| 4475 | SPAGAIN; |
| 4476 | if (c == 0) |
| 4477 | PUSHs(&PL_sv_no); |
| 4478 | else if (SvTEMP(TOPs)) |
| 4479 | SvREFCNT_inc_void(TOPs); |
| 4480 | FREETMPS; |
| 4481 | LEAVE_with_name("smartmatch_coderef"); |
| 4482 | RETURN; |
| 4483 | } |
| 4484 | } |
| 4485 | /* ~~ %hash */ |
| 4486 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { |
| 4487 | if (object_on_left) { |
| 4488 | goto sm_any_hash; /* Treat objects like scalars */ |
| 4489 | } |
| 4490 | else if (!SvOK(d)) { |
| 4491 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n")); |
| 4492 | RETPUSHNO; |
| 4493 | } |
| 4494 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { |
| 4495 | /* Check that the key-sets are identical */ |
| 4496 | HE *he; |
| 4497 | HV *other_hv = MUTABLE_HV(SvRV(d)); |
| 4498 | bool tied = FALSE; |
| 4499 | bool other_tied = FALSE; |
| 4500 | U32 this_key_count = 0, |
| 4501 | other_key_count = 0; |
| 4502 | HV *hv = MUTABLE_HV(SvRV(e)); |
| 4503 | |
| 4504 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n")); |
| 4505 | /* Tied hashes don't know how many keys they have. */ |
| 4506 | if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) { |
| 4507 | tied = TRUE; |
| 4508 | } |
| 4509 | else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) { |
| 4510 | HV * const temp = other_hv; |
| 4511 | other_hv = hv; |
| 4512 | hv = temp; |
| 4513 | tied = TRUE; |
| 4514 | } |
| 4515 | if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) |
| 4516 | other_tied = TRUE; |
| 4517 | |
| 4518 | if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv)) |
| 4519 | RETPUSHNO; |
| 4520 | |
| 4521 | /* The hashes have the same number of keys, so it suffices |
| 4522 | to check that one is a subset of the other. */ |
| 4523 | (void) hv_iterinit(hv); |
| 4524 | while ( (he = hv_iternext(hv)) ) { |
| 4525 | SV *key = hv_iterkeysv(he); |
| 4526 | |
| 4527 | DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n")); |
| 4528 | ++ this_key_count; |
| 4529 | |
| 4530 | if(!hv_exists_ent(other_hv, key, 0)) { |
| 4531 | (void) hv_iterinit(hv); /* reset iterator */ |
| 4532 | RETPUSHNO; |
| 4533 | } |
| 4534 | } |
| 4535 | |
| 4536 | if (other_tied) { |
| 4537 | (void) hv_iterinit(other_hv); |
| 4538 | while ( hv_iternext(other_hv) ) |
| 4539 | ++other_key_count; |
| 4540 | } |
| 4541 | else |
| 4542 | other_key_count = HvUSEDKEYS(other_hv); |
| 4543 | |
| 4544 | if (this_key_count != other_key_count) |
| 4545 | RETPUSHNO; |
| 4546 | else |
| 4547 | RETPUSHYES; |
| 4548 | } |
| 4549 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { |
| 4550 | AV * const other_av = MUTABLE_AV(SvRV(d)); |
| 4551 | const I32 other_len = av_len(other_av) + 1; |
| 4552 | I32 i; |
| 4553 | HV *hv = MUTABLE_HV(SvRV(e)); |
| 4554 | |
| 4555 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n")); |
| 4556 | for (i = 0; i < other_len; ++i) { |
| 4557 | SV ** const svp = av_fetch(other_av, i, FALSE); |
| 4558 | DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n")); |
| 4559 | if (svp) { /* ??? When can this not happen? */ |
| 4560 | if (hv_exists_ent(hv, *svp, 0)) |
| 4561 | RETPUSHYES; |
| 4562 | } |
| 4563 | } |
| 4564 | RETPUSHNO; |
| 4565 | } |
| 4566 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { |
| 4567 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n")); |
| 4568 | sm_regex_hash: |
| 4569 | { |
| 4570 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); |
| 4571 | HE *he; |
| 4572 | HV *hv = MUTABLE_HV(SvRV(e)); |
| 4573 | |
| 4574 | (void) hv_iterinit(hv); |
| 4575 | while ( (he = hv_iternext(hv)) ) { |
| 4576 | DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n")); |
| 4577 | if (matcher_matches_sv(matcher, hv_iterkeysv(he))) { |
| 4578 | (void) hv_iterinit(hv); |
| 4579 | destroy_matcher(matcher); |
| 4580 | RETPUSHYES; |
| 4581 | } |
| 4582 | } |
| 4583 | destroy_matcher(matcher); |
| 4584 | RETPUSHNO; |
| 4585 | } |
| 4586 | } |
| 4587 | else { |
| 4588 | sm_any_hash: |
| 4589 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n")); |
| 4590 | if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0)) |
| 4591 | RETPUSHYES; |
| 4592 | else |
| 4593 | RETPUSHNO; |
| 4594 | } |
| 4595 | } |
| 4596 | /* ~~ @array */ |
| 4597 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) { |
| 4598 | if (object_on_left) { |
| 4599 | goto sm_any_array; /* Treat objects like scalars */ |
| 4600 | } |
| 4601 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { |
| 4602 | AV * const other_av = MUTABLE_AV(SvRV(e)); |
| 4603 | const I32 other_len = av_len(other_av) + 1; |
| 4604 | I32 i; |
| 4605 | |
| 4606 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n")); |
| 4607 | for (i = 0; i < other_len; ++i) { |
| 4608 | SV ** const svp = av_fetch(other_av, i, FALSE); |
| 4609 | |
| 4610 | DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n")); |
| 4611 | if (svp) { /* ??? When can this not happen? */ |
| 4612 | if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0)) |
| 4613 | RETPUSHYES; |
| 4614 | } |
| 4615 | } |
| 4616 | RETPUSHNO; |
| 4617 | } |
| 4618 | if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { |
| 4619 | AV *other_av = MUTABLE_AV(SvRV(d)); |
| 4620 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n")); |
| 4621 | if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av)) |
| 4622 | RETPUSHNO; |
| 4623 | else { |
| 4624 | I32 i; |
| 4625 | const I32 other_len = av_len(other_av); |
| 4626 | |
| 4627 | if (NULL == seen_this) { |
| 4628 | seen_this = newHV(); |
| 4629 | (void) sv_2mortal(MUTABLE_SV(seen_this)); |
| 4630 | } |
| 4631 | if (NULL == seen_other) { |
| 4632 | seen_other = newHV(); |
| 4633 | (void) sv_2mortal(MUTABLE_SV(seen_other)); |
| 4634 | } |
| 4635 | for(i = 0; i <= other_len; ++i) { |
| 4636 | SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); |
| 4637 | SV * const * const other_elem = av_fetch(other_av, i, FALSE); |
| 4638 | |
| 4639 | if (!this_elem || !other_elem) { |
| 4640 | if ((this_elem && SvOK(*this_elem)) |
| 4641 | || (other_elem && SvOK(*other_elem))) |
| 4642 | RETPUSHNO; |
| 4643 | } |
| 4644 | else if (hv_exists_ent(seen_this, |
| 4645 | sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) || |
| 4646 | hv_exists_ent(seen_other, |
| 4647 | sv_2mortal(newSViv(PTR2IV(*other_elem))), 0)) |
| 4648 | { |
| 4649 | if (*this_elem != *other_elem) |
| 4650 | RETPUSHNO; |
| 4651 | } |
| 4652 | else { |
| 4653 | (void)hv_store_ent(seen_this, |
| 4654 | sv_2mortal(newSViv(PTR2IV(*this_elem))), |
| 4655 | &PL_sv_undef, 0); |
| 4656 | (void)hv_store_ent(seen_other, |
| 4657 | sv_2mortal(newSViv(PTR2IV(*other_elem))), |
| 4658 | &PL_sv_undef, 0); |
| 4659 | PUSHs(*other_elem); |
| 4660 | PUSHs(*this_elem); |
| 4661 | |
| 4662 | PUTBACK; |
| 4663 | DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n")); |
| 4664 | (void) do_smartmatch(seen_this, seen_other); |
| 4665 | SPAGAIN; |
| 4666 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); |
| 4667 | |
| 4668 | if (!SvTRUEx(POPs)) |
| 4669 | RETPUSHNO; |
| 4670 | } |
| 4671 | } |
| 4672 | RETPUSHYES; |
| 4673 | } |
| 4674 | } |
| 4675 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { |
| 4676 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n")); |
| 4677 | sm_regex_array: |
| 4678 | { |
| 4679 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); |
| 4680 | const I32 this_len = av_len(MUTABLE_AV(SvRV(e))); |
| 4681 | I32 i; |
| 4682 | |
| 4683 | for(i = 0; i <= this_len; ++i) { |
| 4684 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); |
| 4685 | DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n")); |
| 4686 | if (svp && matcher_matches_sv(matcher, *svp)) { |
| 4687 | destroy_matcher(matcher); |
| 4688 | RETPUSHYES; |
| 4689 | } |
| 4690 | } |
| 4691 | destroy_matcher(matcher); |
| 4692 | RETPUSHNO; |
| 4693 | } |
| 4694 | } |
| 4695 | else if (!SvOK(d)) { |
| 4696 | /* undef ~~ array */ |
| 4697 | const I32 this_len = av_len(MUTABLE_AV(SvRV(e))); |
| 4698 | I32 i; |
| 4699 | |
| 4700 | DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n")); |
| 4701 | for (i = 0; i <= this_len; ++i) { |
| 4702 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); |
| 4703 | DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n")); |
| 4704 | if (!svp || !SvOK(*svp)) |
| 4705 | RETPUSHYES; |
| 4706 | } |
| 4707 | RETPUSHNO; |
| 4708 | } |
| 4709 | else { |
| 4710 | sm_any_array: |
| 4711 | { |
| 4712 | I32 i; |
| 4713 | const I32 this_len = av_len(MUTABLE_AV(SvRV(e))); |
| 4714 | |
| 4715 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n")); |
| 4716 | for (i = 0; i <= this_len; ++i) { |
| 4717 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); |
| 4718 | if (!svp) |
| 4719 | continue; |
| 4720 | |
| 4721 | PUSHs(d); |
| 4722 | PUSHs(*svp); |
| 4723 | PUTBACK; |
| 4724 | /* infinite recursion isn't supposed to happen here */ |
| 4725 | DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n")); |
| 4726 | (void) do_smartmatch(NULL, NULL); |
| 4727 | SPAGAIN; |
| 4728 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); |
| 4729 | if (SvTRUEx(POPs)) |
| 4730 | RETPUSHYES; |
| 4731 | } |
| 4732 | RETPUSHNO; |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | /* ~~ qr// */ |
| 4737 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) { |
| 4738 | if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { |
| 4739 | SV *t = d; d = e; e = t; |
| 4740 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n")); |
| 4741 | goto sm_regex_hash; |
| 4742 | } |
| 4743 | else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { |
| 4744 | SV *t = d; d = e; e = t; |
| 4745 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n")); |
| 4746 | goto sm_regex_array; |
| 4747 | } |
| 4748 | else { |
| 4749 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(e)); |
| 4750 | |
| 4751 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n")); |
| 4752 | PUTBACK; |
| 4753 | PUSHs(matcher_matches_sv(matcher, d) |
| 4754 | ? &PL_sv_yes |
| 4755 | : &PL_sv_no); |
| 4756 | destroy_matcher(matcher); |
| 4757 | RETURN; |
| 4758 | } |
| 4759 | } |
| 4760 | /* ~~ scalar */ |
| 4761 | /* See if there is overload magic on left */ |
| 4762 | else if (object_on_left && SvAMAGIC(d)) { |
| 4763 | SV *tmpsv; |
| 4764 | DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n")); |
| 4765 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); |
| 4766 | PUSHs(d); PUSHs(e); |
| 4767 | PUTBACK; |
| 4768 | tmpsv = amagic_call(d, e, smart_amg, AMGf_noright); |
| 4769 | if (tmpsv) { |
| 4770 | SPAGAIN; |
| 4771 | (void)POPs; |
| 4772 | SETs(tmpsv); |
| 4773 | RETURN; |
| 4774 | } |
| 4775 | SP -= 2; |
| 4776 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n")); |
| 4777 | goto sm_any_scalar; |
| 4778 | } |
| 4779 | else if (!SvOK(d)) { |
| 4780 | /* undef ~~ scalar ; we already know that the scalar is SvOK */ |
| 4781 | DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n")); |
| 4782 | RETPUSHNO; |
| 4783 | } |
| 4784 | else |
| 4785 | sm_any_scalar: |
| 4786 | if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) { |
| 4787 | DEBUG_M(if (SvNIOK(e)) |
| 4788 | Perl_deb(aTHX_ " applying rule Any-Num\n"); |
| 4789 | else |
| 4790 | Perl_deb(aTHX_ " applying rule Num-numish\n"); |
| 4791 | ); |
| 4792 | /* numeric comparison */ |
| 4793 | PUSHs(d); PUSHs(e); |
| 4794 | PUTBACK; |
| 4795 | if (CopHINTS_get(PL_curcop) & HINT_INTEGER) |
| 4796 | (void) Perl_pp_i_eq(aTHX); |
| 4797 | else |
| 4798 | (void) Perl_pp_eq(aTHX); |
| 4799 | SPAGAIN; |
| 4800 | if (SvTRUEx(POPs)) |
| 4801 | RETPUSHYES; |
| 4802 | else |
| 4803 | RETPUSHNO; |
| 4804 | } |
| 4805 | |
| 4806 | /* As a last resort, use string comparison */ |
| 4807 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n")); |
| 4808 | PUSHs(d); PUSHs(e); |
| 4809 | PUTBACK; |
| 4810 | return Perl_pp_seq(aTHX); |
| 4811 | } |
| 4812 | |
| 4813 | PP(pp_enterwhen) |
| 4814 | { |
| 4815 | dVAR; dSP; |
| 4816 | register PERL_CONTEXT *cx; |
| 4817 | const I32 gimme = GIMME_V; |
| 4818 | |
| 4819 | /* This is essentially an optimization: if the match |
| 4820 | fails, we don't want to push a context and then |
| 4821 | pop it again right away, so we skip straight |
| 4822 | to the op that follows the leavewhen. |
| 4823 | RETURNOP calls PUTBACK which restores the stack pointer after the POPs. |
| 4824 | */ |
| 4825 | if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs)) |
| 4826 | RETURNOP(cLOGOP->op_other->op_next); |
| 4827 | |
| 4828 | ENTER_with_name("eval"); |
| 4829 | SAVETMPS; |
| 4830 | |
| 4831 | PUSHBLOCK(cx, CXt_WHEN, SP); |
| 4832 | PUSHWHEN(cx); |
| 4833 | |
| 4834 | RETURN; |
| 4835 | } |
| 4836 | |
| 4837 | PP(pp_leavewhen) |
| 4838 | { |
| 4839 | dVAR; dSP; |
| 4840 | register PERL_CONTEXT *cx; |
| 4841 | I32 gimme __attribute__unused__; |
| 4842 | SV **newsp; |
| 4843 | PMOP *newpm; |
| 4844 | |
| 4845 | POPBLOCK(cx,newpm); |
| 4846 | assert(CxTYPE(cx) == CXt_WHEN); |
| 4847 | |
| 4848 | SP = newsp; |
| 4849 | PUTBACK; |
| 4850 | |
| 4851 | PL_curpm = newpm; /* pop $1 et al */ |
| 4852 | |
| 4853 | LEAVE_with_name("eval"); |
| 4854 | return NORMAL; |
| 4855 | } |
| 4856 | |
| 4857 | PP(pp_continue) |
| 4858 | { |
| 4859 | dVAR; |
| 4860 | I32 cxix; |
| 4861 | register PERL_CONTEXT *cx; |
| 4862 | I32 inner; |
| 4863 | |
| 4864 | cxix = dopoptowhen(cxstack_ix); |
| 4865 | if (cxix < 0) |
| 4866 | DIE(aTHX_ "Can't \"continue\" outside a when block"); |
| 4867 | if (cxix < cxstack_ix) |
| 4868 | dounwind(cxix); |
| 4869 | |
| 4870 | /* clear off anything above the scope we're re-entering */ |
| 4871 | inner = PL_scopestack_ix; |
| 4872 | TOPBLOCK(cx); |
| 4873 | if (PL_scopestack_ix < inner) |
| 4874 | leave_scope(PL_scopestack[PL_scopestack_ix]); |
| 4875 | PL_curcop = cx->blk_oldcop; |
| 4876 | return cx->blk_givwhen.leave_op; |
| 4877 | } |
| 4878 | |
| 4879 | PP(pp_break) |
| 4880 | { |
| 4881 | dVAR; |
| 4882 | I32 cxix; |
| 4883 | register PERL_CONTEXT *cx; |
| 4884 | I32 inner; |
| 4885 | dSP; |
| 4886 | |
| 4887 | cxix = dopoptogiven(cxstack_ix); |
| 4888 | if (cxix < 0) { |
| 4889 | if (PL_op->op_flags & OPf_SPECIAL) |
| 4890 | DIE(aTHX_ "Can't use when() outside a topicalizer"); |
| 4891 | else |
| 4892 | DIE(aTHX_ "Can't \"break\" outside a given block"); |
| 4893 | } |
| 4894 | if (CxFOREACH(&cxstack[cxix]) && (0 == (PL_op->op_flags & OPf_SPECIAL))) |
| 4895 | DIE(aTHX_ "Can't \"break\" in a loop topicalizer"); |
| 4896 | |
| 4897 | if (cxix < cxstack_ix) |
| 4898 | dounwind(cxix); |
| 4899 | |
| 4900 | /* clear off anything above the scope we're re-entering */ |
| 4901 | inner = PL_scopestack_ix; |
| 4902 | TOPBLOCK(cx); |
| 4903 | if (PL_scopestack_ix < inner) |
| 4904 | leave_scope(PL_scopestack[PL_scopestack_ix]); |
| 4905 | PL_curcop = cx->blk_oldcop; |
| 4906 | |
| 4907 | if (CxFOREACH(cx)) |
| 4908 | return (cx)->blk_loop.my_op->op_nextop; |
| 4909 | else |
| 4910 | /* RETURNOP calls PUTBACK which restores the old old sp */ |
| 4911 | RETURNOP(cx->blk_givwhen.leave_op); |
| 4912 | } |
| 4913 | |
| 4914 | static MAGIC * |
| 4915 | S_doparseform(pTHX_ SV *sv) |
| 4916 | { |
| 4917 | STRLEN len; |
| 4918 | register char *s = SvPV(sv, len); |
| 4919 | register char *send; |
| 4920 | register char *base = NULL; /* start of current field */ |
| 4921 | register I32 skipspaces = 0; /* number of contiguous spaces seen */ |
| 4922 | bool noblank = FALSE; /* ~ or ~~ seen on this line */ |
| 4923 | bool repeat = FALSE; /* ~~ seen on this line */ |
| 4924 | bool postspace = FALSE; /* a text field may need right padding */ |
| 4925 | U32 *fops; |
| 4926 | register U32 *fpc; |
| 4927 | U32 *linepc = NULL; /* position of last FF_LINEMARK */ |
| 4928 | register I32 arg; |
| 4929 | bool ischop; /* it's a ^ rather than a @ */ |
| 4930 | bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */ |
| 4931 | int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */ |
| 4932 | MAGIC *mg = NULL; |
| 4933 | SV *sv_copy; |
| 4934 | |
| 4935 | PERL_ARGS_ASSERT_DOPARSEFORM; |
| 4936 | |
| 4937 | if (len == 0) |
| 4938 | Perl_croak(aTHX_ "Null picture in formline"); |
| 4939 | |
| 4940 | if (SvTYPE(sv) >= SVt_PVMG) { |
| 4941 | /* This might, of course, still return NULL. */ |
| 4942 | mg = mg_find(sv, PERL_MAGIC_fm); |
| 4943 | } else { |
| 4944 | sv_upgrade(sv, SVt_PVMG); |
| 4945 | } |
| 4946 | |
| 4947 | if (mg) { |
| 4948 | /* still the same as previously-compiled string? */ |
| 4949 | SV *old = mg->mg_obj; |
| 4950 | if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv)) |
| 4951 | && len == SvCUR(old) |
| 4952 | && strnEQ(SvPVX(old), SvPVX(sv), len) |
| 4953 | ) { |
| 4954 | DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n")); |
| 4955 | return mg; |
| 4956 | } |
| 4957 | |
| 4958 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n")); |
| 4959 | Safefree(mg->mg_ptr); |
| 4960 | mg->mg_ptr = NULL; |
| 4961 | SvREFCNT_dec(old); |
| 4962 | mg->mg_obj = NULL; |
| 4963 | } |
| 4964 | else { |
| 4965 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n")); |
| 4966 | mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0); |
| 4967 | } |
| 4968 | |
| 4969 | sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv)); |
| 4970 | s = SvPV(sv_copy, len); /* work on the copy, not the original */ |
| 4971 | send = s + len; |
| 4972 | |
| 4973 | |
| 4974 | /* estimate the buffer size needed */ |
| 4975 | for (base = s; s <= send; s++) { |
| 4976 | if (*s == '\n' || *s == '@' || *s == '^') |
| 4977 | maxops += 10; |
| 4978 | } |
| 4979 | s = base; |
| 4980 | base = NULL; |
| 4981 | |
| 4982 | Newx(fops, maxops, U32); |
| 4983 | fpc = fops; |
| 4984 | |
| 4985 | if (s < send) { |
| 4986 | linepc = fpc; |
| 4987 | *fpc++ = FF_LINEMARK; |
| 4988 | noblank = repeat = FALSE; |
| 4989 | base = s; |
| 4990 | } |
| 4991 | |
| 4992 | while (s <= send) { |
| 4993 | switch (*s++) { |
| 4994 | default: |
| 4995 | skipspaces = 0; |
| 4996 | continue; |
| 4997 | |
| 4998 | case '~': |
| 4999 | if (*s == '~') { |
| 5000 | repeat = TRUE; |
| 5001 | skipspaces++; |
| 5002 | s++; |
| 5003 | } |
| 5004 | noblank = TRUE; |
| 5005 | /* FALL THROUGH */ |
| 5006 | case ' ': case '\t': |
| 5007 | skipspaces++; |
| 5008 | continue; |
| 5009 | case 0: |
| 5010 | if (s < send) { |
| 5011 | skipspaces = 0; |
| 5012 | continue; |
| 5013 | } /* else FALL THROUGH */ |
| 5014 | case '\n': |
| 5015 | arg = s - base; |
| 5016 | skipspaces++; |
| 5017 | arg -= skipspaces; |
| 5018 | if (arg) { |
| 5019 | if (postspace) |
| 5020 | *fpc++ = FF_SPACE; |
| 5021 | *fpc++ = FF_LITERAL; |
| 5022 | *fpc++ = (U32)arg; |
| 5023 | } |
| 5024 | postspace = FALSE; |
| 5025 | if (s <= send) |
| 5026 | skipspaces--; |
| 5027 | if (skipspaces) { |
| 5028 | *fpc++ = FF_SKIP; |
| 5029 | *fpc++ = (U32)skipspaces; |
| 5030 | } |
| 5031 | skipspaces = 0; |
| 5032 | if (s <= send) |
| 5033 | *fpc++ = FF_NEWLINE; |
| 5034 | if (noblank) { |
| 5035 | *fpc++ = FF_BLANK; |
| 5036 | if (repeat) |
| 5037 | arg = fpc - linepc + 1; |
| 5038 | else |
| 5039 | arg = 0; |
| 5040 | *fpc++ = (U32)arg; |
| 5041 | } |
| 5042 | if (s < send) { |
| 5043 | linepc = fpc; |
| 5044 | *fpc++ = FF_LINEMARK; |
| 5045 | noblank = repeat = FALSE; |
| 5046 | base = s; |
| 5047 | } |
| 5048 | else |
| 5049 | s++; |
| 5050 | continue; |
| 5051 | |
| 5052 | case '@': |
| 5053 | case '^': |
| 5054 | ischop = s[-1] == '^'; |
| 5055 | |
| 5056 | if (postspace) { |
| 5057 | *fpc++ = FF_SPACE; |
| 5058 | postspace = FALSE; |
| 5059 | } |
| 5060 | arg = (s - base) - 1; |
| 5061 | if (arg) { |
| 5062 | *fpc++ = FF_LITERAL; |
| 5063 | *fpc++ = (U32)arg; |
| 5064 | } |
| 5065 | |
| 5066 | base = s - 1; |
| 5067 | *fpc++ = FF_FETCH; |
| 5068 | if (*s == '*') { /* @* or ^* */ |
| 5069 | s++; |
| 5070 | *fpc++ = 2; /* skip the @* or ^* */ |
| 5071 | if (ischop) { |
| 5072 | *fpc++ = FF_LINESNGL; |
| 5073 | *fpc++ = FF_CHOP; |
| 5074 | } else |
| 5075 | *fpc++ = FF_LINEGLOB; |
| 5076 | } |
| 5077 | else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */ |
| 5078 | arg = ischop ? FORM_NUM_BLANK : 0; |
| 5079 | base = s - 1; |
| 5080 | while (*s == '#') |
| 5081 | s++; |
| 5082 | if (*s == '.') { |
| 5083 | const char * const f = ++s; |
| 5084 | while (*s == '#') |
| 5085 | s++; |
| 5086 | arg |= FORM_NUM_POINT + (s - f); |
| 5087 | } |
| 5088 | *fpc++ = s - base; /* fieldsize for FETCH */ |
| 5089 | *fpc++ = FF_DECIMAL; |
| 5090 | *fpc++ = (U32)arg; |
| 5091 | unchopnum |= ! ischop; |
| 5092 | } |
| 5093 | else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */ |
| 5094 | arg = ischop ? FORM_NUM_BLANK : 0; |
| 5095 | base = s - 1; |
| 5096 | s++; /* skip the '0' first */ |
| 5097 | while (*s == '#') |
| 5098 | s++; |
| 5099 | if (*s == '.') { |
| 5100 | const char * const f = ++s; |
| 5101 | while (*s == '#') |
| 5102 | s++; |
| 5103 | arg |= FORM_NUM_POINT + (s - f); |
| 5104 | } |
| 5105 | *fpc++ = s - base; /* fieldsize for FETCH */ |
| 5106 | *fpc++ = FF_0DECIMAL; |
| 5107 | *fpc++ = (U32)arg; |
| 5108 | unchopnum |= ! ischop; |
| 5109 | } |
| 5110 | else { /* text field */ |
| 5111 | I32 prespace = 0; |
| 5112 | bool ismore = FALSE; |
| 5113 | |
| 5114 | if (*s == '>') { |
| 5115 | while (*++s == '>') ; |
| 5116 | prespace = FF_SPACE; |
| 5117 | } |
| 5118 | else if (*s == '|') { |
| 5119 | while (*++s == '|') ; |
| 5120 | prespace = FF_HALFSPACE; |
| 5121 | postspace = TRUE; |
| 5122 | } |
| 5123 | else { |
| 5124 | if (*s == '<') |
| 5125 | while (*++s == '<') ; |
| 5126 | postspace = TRUE; |
| 5127 | } |
| 5128 | if (*s == '.' && s[1] == '.' && s[2] == '.') { |
| 5129 | s += 3; |
| 5130 | ismore = TRUE; |
| 5131 | } |
| 5132 | *fpc++ = s - base; /* fieldsize for FETCH */ |
| 5133 | |
| 5134 | *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL; |
| 5135 | |
| 5136 | if (prespace) |
| 5137 | *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */ |
| 5138 | *fpc++ = FF_ITEM; |
| 5139 | if (ismore) |
| 5140 | *fpc++ = FF_MORE; |
| 5141 | if (ischop) |
| 5142 | *fpc++ = FF_CHOP; |
| 5143 | } |
| 5144 | base = s; |
| 5145 | skipspaces = 0; |
| 5146 | continue; |
| 5147 | } |
| 5148 | } |
| 5149 | *fpc++ = FF_END; |
| 5150 | |
| 5151 | assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */ |
| 5152 | arg = fpc - fops; |
| 5153 | |
| 5154 | mg->mg_ptr = (char *) fops; |
| 5155 | mg->mg_len = arg * sizeof(U32); |
| 5156 | mg->mg_obj = sv_copy; |
| 5157 | mg->mg_flags |= MGf_REFCOUNTED; |
| 5158 | |
| 5159 | if (unchopnum && repeat) |
| 5160 | Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)"); |
| 5161 | |
| 5162 | return mg; |
| 5163 | } |
| 5164 | |
| 5165 | |
| 5166 | STATIC bool |
| 5167 | S_num_overflow(NV value, I32 fldsize, I32 frcsize) |
| 5168 | { |
| 5169 | /* Can value be printed in fldsize chars, using %*.*f ? */ |
| 5170 | NV pwr = 1; |
| 5171 | NV eps = 0.5; |
| 5172 | bool res = FALSE; |
| 5173 | int intsize = fldsize - (value < 0 ? 1 : 0); |
| 5174 | |
| 5175 | if (frcsize & FORM_NUM_POINT) |
| 5176 | intsize--; |
| 5177 | frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
| 5178 | intsize -= frcsize; |
| 5179 | |
| 5180 | while (intsize--) pwr *= 10.0; |
| 5181 | while (frcsize--) eps /= 10.0; |
| 5182 | |
| 5183 | if( value >= 0 ){ |
| 5184 | if (value + eps >= pwr) |
| 5185 | res = TRUE; |
| 5186 | } else { |
| 5187 | if (value - eps <= -pwr) |
| 5188 | res = TRUE; |
| 5189 | } |
| 5190 | return res; |
| 5191 | } |
| 5192 | |
| 5193 | static I32 |
| 5194 | S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen) |
| 5195 | { |
| 5196 | dVAR; |
| 5197 | SV * const datasv = FILTER_DATA(idx); |
| 5198 | const int filter_has_file = IoLINES(datasv); |
| 5199 | SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv)); |
| 5200 | SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv)); |
| 5201 | int status = 0; |
| 5202 | SV *upstream; |
| 5203 | STRLEN got_len; |
| 5204 | char *got_p = NULL; |
| 5205 | char *prune_from = NULL; |
| 5206 | bool read_from_cache = FALSE; |
| 5207 | STRLEN umaxlen; |
| 5208 | |
| 5209 | PERL_ARGS_ASSERT_RUN_USER_FILTER; |
| 5210 | |
| 5211 | assert(maxlen >= 0); |
| 5212 | umaxlen = maxlen; |
| 5213 | |
| 5214 | /* I was having segfault trouble under Linux 2.2.5 after a |
| 5215 | parse error occured. (Had to hack around it with a test |
| 5216 | for PL_parser->error_count == 0.) Solaris doesn't segfault -- |
| 5217 | not sure where the trouble is yet. XXX */ |
| 5218 | |
| 5219 | { |
| 5220 | SV *const cache = datasv; |
| 5221 | if (SvOK(cache)) { |
| 5222 | STRLEN cache_len; |
| 5223 | const char *cache_p = SvPV(cache, cache_len); |
| 5224 | STRLEN take = 0; |
| 5225 | |
| 5226 | if (umaxlen) { |
| 5227 | /* Running in block mode and we have some cached data already. |
| 5228 | */ |
| 5229 | if (cache_len >= umaxlen) { |
| 5230 | /* In fact, so much data we don't even need to call |
| 5231 | filter_read. */ |
| 5232 | take = umaxlen; |
| 5233 | } |
| 5234 | } else { |
| 5235 | const char *const first_nl = |
| 5236 | (const char *)memchr(cache_p, '\n', cache_len); |
| 5237 | if (first_nl) { |
| 5238 | take = first_nl + 1 - cache_p; |
| 5239 | } |
| 5240 | } |
| 5241 | if (take) { |
| 5242 | sv_catpvn(buf_sv, cache_p, take); |
| 5243 | sv_chop(cache, cache_p + take); |
| 5244 | /* Definitely not EOF */ |
| 5245 | return 1; |
| 5246 | } |
| 5247 | |
| 5248 | sv_catsv(buf_sv, cache); |
| 5249 | if (umaxlen) { |
| 5250 | umaxlen -= cache_len; |
| 5251 | } |
| 5252 | SvOK_off(cache); |
| 5253 | read_from_cache = TRUE; |
| 5254 | } |
| 5255 | } |
| 5256 | |
| 5257 | /* Filter API says that the filter appends to the contents of the buffer. |
| 5258 | Usually the buffer is "", so the details don't matter. But if it's not, |
| 5259 | then clearly what it contains is already filtered by this filter, so we |
| 5260 | don't want to pass it in a second time. |
| 5261 | I'm going to use a mortal in case the upstream filter croaks. */ |
| 5262 | upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv)) |
| 5263 | ? sv_newmortal() : buf_sv; |
| 5264 | SvTEMP_off(upstream); |
| 5265 | SvUPGRADE(upstream, SVt_PV); |
| 5266 | |
| 5267 | if (filter_has_file) { |
| 5268 | status = FILTER_READ(idx+1, upstream, 0); |
| 5269 | } |
| 5270 | |
| 5271 | if (filter_sub && status >= 0) { |
| 5272 | dSP; |
| 5273 | int count; |
| 5274 | |
| 5275 | ENTER_with_name("call_filter_sub"); |
| 5276 | save_gp(PL_defgv, 0); |
| 5277 | GvINTRO_off(PL_defgv); |
| 5278 | SAVEGENERICSV(GvSV(PL_defgv)); |
| 5279 | SAVETMPS; |
| 5280 | EXTEND(SP, 2); |
| 5281 | |
| 5282 | DEFSV_set(upstream); |
| 5283 | SvREFCNT_inc_simple_void_NN(upstream); |
| 5284 | PUSHMARK(SP); |
| 5285 | mPUSHi(0); |
| 5286 | if (filter_state) { |
| 5287 | PUSHs(filter_state); |
| 5288 | } |
| 5289 | PUTBACK; |
| 5290 | count = call_sv(filter_sub, G_SCALAR); |
| 5291 | SPAGAIN; |
| 5292 | |
| 5293 | if (count > 0) { |
| 5294 | SV *out = POPs; |
| 5295 | if (SvOK(out)) { |
| 5296 | status = SvIV(out); |
| 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | PUTBACK; |
| 5301 | FREETMPS; |
| 5302 | LEAVE_with_name("call_filter_sub"); |
| 5303 | } |
| 5304 | |
| 5305 | if(SvOK(upstream)) { |
| 5306 | got_p = SvPV(upstream, got_len); |
| 5307 | if (umaxlen) { |
| 5308 | if (got_len > umaxlen) { |
| 5309 | prune_from = got_p + umaxlen; |
| 5310 | } |
| 5311 | } else { |
| 5312 | char *const first_nl = (char *)memchr(got_p, '\n', got_len); |
| 5313 | if (first_nl && first_nl + 1 < got_p + got_len) { |
| 5314 | /* There's a second line here... */ |
| 5315 | prune_from = first_nl + 1; |
| 5316 | } |
| 5317 | } |
| 5318 | } |
| 5319 | if (prune_from) { |
| 5320 | /* Oh. Too long. Stuff some in our cache. */ |
| 5321 | STRLEN cached_len = got_p + got_len - prune_from; |
| 5322 | SV *const cache = datasv; |
| 5323 | |
| 5324 | if (SvOK(cache)) { |
| 5325 | /* Cache should be empty. */ |
| 5326 | assert(!SvCUR(cache)); |
| 5327 | } |
| 5328 | |
| 5329 | sv_setpvn(cache, prune_from, cached_len); |
| 5330 | /* If you ask for block mode, you may well split UTF-8 characters. |
| 5331 | "If it breaks, you get to keep both parts" |
| 5332 | (Your code is broken if you don't put them back together again |
| 5333 | before something notices.) */ |
| 5334 | if (SvUTF8(upstream)) { |
| 5335 | SvUTF8_on(cache); |
| 5336 | } |
| 5337 | SvCUR_set(upstream, got_len - cached_len); |
| 5338 | *prune_from = 0; |
| 5339 | /* Can't yet be EOF */ |
| 5340 | if (status == 0) |
| 5341 | status = 1; |
| 5342 | } |
| 5343 | |
| 5344 | /* If they are at EOF but buf_sv has something in it, then they may never |
| 5345 | have touched the SV upstream, so it may be undefined. If we naively |
| 5346 | concatenate it then we get a warning about use of uninitialised value. |
| 5347 | */ |
| 5348 | if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) { |
| 5349 | sv_catsv(buf_sv, upstream); |
| 5350 | } |
| 5351 | |
| 5352 | if (status <= 0) { |
| 5353 | IoLINES(datasv) = 0; |
| 5354 | if (filter_state) { |
| 5355 | SvREFCNT_dec(filter_state); |
| 5356 | IoTOP_GV(datasv) = NULL; |
| 5357 | } |
| 5358 | if (filter_sub) { |
| 5359 | SvREFCNT_dec(filter_sub); |
| 5360 | IoBOTTOM_GV(datasv) = NULL; |
| 5361 | } |
| 5362 | filter_del(S_run_user_filter); |
| 5363 | } |
| 5364 | if (status == 0 && read_from_cache) { |
| 5365 | /* If we read some data from the cache (and by getting here it implies |
| 5366 | that we emptied the cache) then we aren't yet at EOF, and mustn't |
| 5367 | report that to our caller. */ |
| 5368 | return 1; |
| 5369 | } |
| 5370 | return status; |
| 5371 | } |
| 5372 | |
| 5373 | /* perhaps someone can come up with a better name for |
| 5374 | this? it is not really "absolute", per se ... */ |
| 5375 | static bool |
| 5376 | S_path_is_absolute(const char *name) |
| 5377 | { |
| 5378 | PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE; |
| 5379 | |
| 5380 | if (PERL_FILE_IS_ABSOLUTE(name) |
| 5381 | #ifdef WIN32 |
| 5382 | || (*name == '.' && ((name[1] == '/' || |
| 5383 | (name[1] == '.' && name[2] == '/')) |
| 5384 | || (name[1] == '\\' || |
| 5385 | ( name[1] == '.' && name[2] == '\\'))) |
| 5386 | ) |
| 5387 | #else |
| 5388 | || (*name == '.' && (name[1] == '/' || |
| 5389 | (name[1] == '.' && name[2] == '/'))) |
| 5390 | #endif |
| 5391 | ) |
| 5392 | { |
| 5393 | return TRUE; |
| 5394 | } |
| 5395 | else |
| 5396 | return FALSE; |
| 5397 | } |
| 5398 | |
| 5399 | /* |
| 5400 | * Local variables: |
| 5401 | * c-indentation-style: bsd |
| 5402 | * c-basic-offset: 4 |
| 5403 | * indent-tabs-mode: t |
| 5404 | * End: |
| 5405 | * |
| 5406 | * ex: set ts=8 sts=4 sw=4 noet: |
| 5407 | */ |