Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* pp_ctl.c |
2 | * | |
1129b882 NC |
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 | |
a0d0e21e LW |
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 | /* | |
4ac71550 TC |
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"] | |
a0d0e21e LW |
20 | */ |
21 | ||
166f8a29 DM |
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 | ||
a0d0e21e | 33 | #include "EXTERN.h" |
864dbfa3 | 34 | #define PERL_IN_PP_CTL_C |
a0d0e21e LW |
35 | #include "perl.h" |
36 | ||
d7e3f70f Z |
37 | #define RUN_PP_CATCHABLY(thispp) \ |
38 | STMT_START { if (CATCH_GET) return docatch(thispp); } STMT_END | |
1e422769 | 39 | |
5b6f7443 DM |
40 | #define dopopto_cursub() \ |
41 | (PL_curstackinfo->si_cxsubix >= 0 \ | |
42 | ? PL_curstackinfo->si_cxsubix \ | |
43 | : dopoptosub_at(cxstack, cxstack_ix)) | |
44 | ||
94fcd414 NC |
45 | #define dopoptosub(plop) dopoptosub_at(cxstack, (plop)) |
46 | ||
a0d0e21e LW |
47 | PP(pp_wantarray) |
48 | { | |
39644a26 | 49 | dSP; |
a0d0e21e | 50 | I32 cxix; |
93f0bc49 | 51 | const PERL_CONTEXT *cx; |
a0d0e21e LW |
52 | EXTEND(SP, 1); |
53 | ||
93f0bc49 FC |
54 | if (PL_op->op_private & OPpOFFBYONE) { |
55 | if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF; | |
56 | } | |
57 | else { | |
5b6f7443 | 58 | cxix = dopopto_cursub(); |
93f0bc49 | 59 | if (cxix < 0) |
a0d0e21e | 60 | RETPUSHUNDEF; |
93f0bc49 FC |
61 | cx = &cxstack[cxix]; |
62 | } | |
a0d0e21e | 63 | |
93f0bc49 | 64 | switch (cx->blk_gimme) { |
54310121 | 65 | case G_ARRAY: |
a0d0e21e | 66 | RETPUSHYES; |
54310121 | 67 | case G_SCALAR: |
a0d0e21e | 68 | RETPUSHNO; |
54310121 | 69 | default: |
70 | RETPUSHUNDEF; | |
71 | } | |
a0d0e21e LW |
72 | } |
73 | ||
2cd61cdb IZ |
74 | PP(pp_regcreset) |
75 | { | |
0b4182de | 76 | TAINT_NOT; |
2cd61cdb IZ |
77 | return NORMAL; |
78 | } | |
79 | ||
b3eb6a9b GS |
80 | PP(pp_regcomp) |
81 | { | |
39644a26 | 82 | dSP; |
eb578fdb | 83 | PMOP *pm = (PMOP*)cLOGOP->op_other; |
9f141731 | 84 | SV **args; |
df787a7b | 85 | int nargs; |
84679df5 | 86 | REGEXP *re = NULL; |
9f141731 DM |
87 | REGEXP *new_re; |
88 | const regexp_engine *eng; | |
dbc200c5 | 89 | bool is_bare_re= FALSE; |
bfed75c6 | 90 | |
df787a7b DM |
91 | if (PL_op->op_flags & OPf_STACKED) { |
92 | dMARK; | |
93 | nargs = SP - MARK; | |
94 | args = ++MARK; | |
95 | } | |
96 | else { | |
97 | nargs = 1; | |
98 | args = SP; | |
99 | } | |
100 | ||
4b5a0d1c | 101 | /* prevent recompiling under /o and ithreads. */ |
3db8f154 | 102 | #if defined(USE_ITHREADS) |
131b3ad0 | 103 | if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) { |
df787a7b | 104 | SP = args-1; |
131b3ad0 DM |
105 | RETURN; |
106 | } | |
513629ba | 107 | #endif |
d4b87e75 | 108 | |
9f141731 DM |
109 | re = PM_GETRE(pm); |
110 | assert (re != (REGEXP*) &PL_sv_undef); | |
111 | eng = re ? RX_ENGINE(re) : current_re_engine(); | |
112 | ||
3c13cae6 DM |
113 | new_re = (eng->op_comp |
114 | ? eng->op_comp | |
115 | : &Perl_re_op_compile | |
116 | )(aTHX_ args, nargs, pm->op_code_list, eng, re, | |
346d3070 | 117 | &is_bare_re, |
dbc200c5 | 118 | (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK), |
a5ae69f0 DM |
119 | pm->op_pmflags | |
120 | (PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0)); | |
dbc200c5 | 121 | |
346d3070 | 122 | if (pm->op_pmflags & PMf_HAS_CV) |
8d919b0a | 123 | ReANY(new_re)->qr_anoncv |
9fe3265f | 124 | = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ)); |
9f141731 DM |
125 | |
126 | if (is_bare_re) { | |
127 | REGEXP *tmp; | |
128 | /* The match's LHS's get-magic might need to access this op's regexp | |
129 | (e.g. $' =~ /$re/ while foo; see bug 70764). So we must call | |
130 | get-magic now before we replace the regexp. Hopefully this hack can | |
131 | be replaced with the approach described at | |
132 | http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html | |
133 | some day. */ | |
134 | if (pm->op_type == OP_MATCH) { | |
135 | SV *lhs; | |
284167a5 | 136 | const bool was_tainted = TAINT_get; |
9f141731 DM |
137 | if (pm->op_flags & OPf_STACKED) |
138 | lhs = args[-1]; | |
6ffceeb7 | 139 | else if (pm->op_targ) |
9f141731 DM |
140 | lhs = PAD_SV(pm->op_targ); |
141 | else lhs = DEFSV; | |
142 | SvGETMAGIC(lhs); | |
143 | /* Restore the previous value of PL_tainted (which may have been | |
144 | modified by get-magic), to avoid incorrectly setting the | |
284167a5 S |
145 | RXf_TAINTED flag with RX_TAINT_on further down. */ |
146 | TAINT_set(was_tainted); | |
dc6d7f5c | 147 | #ifdef NO_TAINT_SUPPORT |
9a9b5ec9 DM |
148 | PERL_UNUSED_VAR(was_tainted); |
149 | #endif | |
df787a7b | 150 | } |
9f141731 DM |
151 | tmp = reg_temp_copy(NULL, new_re); |
152 | ReREFCNT_dec(new_re); | |
153 | new_re = tmp; | |
df787a7b | 154 | } |
dbc200c5 | 155 | |
9f141731 DM |
156 | if (re != new_re) { |
157 | ReREFCNT_dec(re); | |
158 | PM_SETRE(pm, new_re); | |
c277df42 | 159 | } |
d4b87e75 | 160 | |
dbc200c5 | 161 | |
d48c660d DM |
162 | assert(TAINTING_get || !TAINT_get); |
163 | if (TAINT_get) { | |
9f141731 | 164 | SvTAINTED_on((SV*)new_re); |
284167a5 | 165 | RX_TAINT_on(new_re); |
72311751 | 166 | } |
72311751 | 167 | |
5585e758 YO |
168 | /* handle the empty pattern */ |
169 | if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) { | |
170 | if (PL_curpm == PL_reg_curpm) { | |
3cb4cde3 TC |
171 | if (PL_curpm_under && PL_curpm_under == PL_reg_curpm) { |
172 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); | |
5585e758 | 173 | } |
5585e758 YO |
174 | } |
175 | } | |
176 | ||
c737faaf YO |
177 | #if !defined(USE_ITHREADS) |
178 | /* can't change the optree at runtime either */ | |
179 | /* PMf_KEEP is handled differently under threads to avoid these problems */ | |
a0d0e21e | 180 | if (pm->op_pmflags & PMf_KEEP) { |
533c011a | 181 | cLOGOP->op_first->op_next = PL_op->op_next; |
a0d0e21e | 182 | } |
c737faaf | 183 | #endif |
9f141731 | 184 | |
df787a7b | 185 | SP = args-1; |
a0d0e21e LW |
186 | RETURN; |
187 | } | |
188 | ||
9f141731 | 189 | |
a0d0e21e LW |
190 | PP(pp_substcont) |
191 | { | |
39644a26 | 192 | dSP; |
4ebe6e95 | 193 | PERL_CONTEXT *cx = CX_CUR(); |
eb578fdb KW |
194 | PMOP * const pm = (PMOP*) cLOGOP->op_other; |
195 | SV * const dstr = cx->sb_dstr; | |
196 | char *s = cx->sb_s; | |
197 | char *m = cx->sb_m; | |
a0d0e21e | 198 | char *orig = cx->sb_orig; |
eb578fdb | 199 | REGEXP * const rx = cx->sb_rx; |
c445ea15 | 200 | SV *nsv = NULL; |
988e6e7e | 201 | REGEXP *old = PM_GETRE(pm); |
f410a211 NC |
202 | |
203 | PERL_ASYNC_CHECK(); | |
204 | ||
988e6e7e | 205 | if(old != rx) { |
bfed75c6 | 206 | if(old) |
988e6e7e | 207 | ReREFCNT_dec(old); |
d6106309 | 208 | PM_SETRE(pm,ReREFCNT_inc(rx)); |
d8f2cf8a AB |
209 | } |
210 | ||
d9f97599 | 211 | rxres_restore(&cx->sb_rxres, rx); |
c90c0ff4 | 212 | |
a0d0e21e | 213 | if (cx->sb_iters++) { |
3c6ef0a5 | 214 | const SSize_t saviters = cx->sb_iters; |
a0d0e21e | 215 | if (cx->sb_iters > cx->sb_maxiters) |
cea2e8a9 | 216 | DIE(aTHX_ "Substitution loop"); |
a0d0e21e | 217 | |
447ee134 DM |
218 | SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */ |
219 | ||
ef07e810 | 220 | /* See "how taint works" above pp_subst() */ |
447ee134 | 221 | sv_catsv_nomg(dstr, POPs); |
c4f4b223 Z |
222 | if (UNLIKELY(TAINT_get)) |
223 | cx->sb_rxtainted |= SUBST_TAINT_REPL; | |
2c296965 | 224 | if (CxONCE(cx) || s < orig || |
03c83e26 DM |
225 | !CALLREGEXEC(rx, s, cx->sb_strend, orig, |
226 | (s == m), cx->sb_targ, NULL, | |
d5e7783a | 227 | (REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW))) |
a0d0e21e | 228 | { |
8ca8a454 | 229 | SV *targ = cx->sb_targ; |
748a9306 | 230 | |
078c425b JH |
231 | assert(cx->sb_strend >= s); |
232 | if(cx->sb_strend > s) { | |
233 | if (DO_UTF8(dstr) && !SvUTF8(targ)) | |
4bac9ae4 | 234 | sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv); |
078c425b | 235 | else |
4bac9ae4 | 236 | sv_catpvn_nomg(dstr, s, cx->sb_strend - s); |
078c425b | 237 | } |
20be6587 DM |
238 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
239 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
9212bbba | 240 | |
8ca8a454 NC |
241 | if (pm->op_pmflags & PMf_NONDESTRUCT) { |
242 | PUSHs(dstr); | |
243 | /* From here on down we're using the copy, and leaving the | |
244 | original untouched. */ | |
245 | targ = dstr; | |
246 | } | |
247 | else { | |
9e0ea7f3 FC |
248 | SV_CHECK_THINKFIRST_COW_DROP(targ); |
249 | if (isGV(targ)) Perl_croak_no_modify(); | |
250 | SvPV_free(targ); | |
8ca8a454 NC |
251 | SvPV_set(targ, SvPVX(dstr)); |
252 | SvCUR_set(targ, SvCUR(dstr)); | |
253 | SvLEN_set(targ, SvLEN(dstr)); | |
254 | if (DO_UTF8(dstr)) | |
255 | SvUTF8_on(targ); | |
256 | SvPV_set(dstr, NULL); | |
257 | ||
52c47e16 | 258 | PL_tainted = 0; |
4f4d7508 | 259 | mPUSHi(saviters - 1); |
48c036b1 | 260 | |
8ca8a454 NC |
261 | (void)SvPOK_only_UTF8(targ); |
262 | } | |
5cd24f17 | 263 | |
20be6587 | 264 | /* update the taint state of various various variables in |
ef07e810 DM |
265 | * preparation for final exit. |
266 | * See "how taint works" above pp_subst() */ | |
284167a5 | 267 | if (TAINTING_get) { |
20be6587 DM |
268 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || |
269 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
270 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
271 | ) | |
272 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
273 | ||
274 | if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET) | |
275 | && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) | |
276 | ) | |
277 | SvTAINTED_on(TOPs); /* taint return value */ | |
278 | /* needed for mg_set below */ | |
284167a5 S |
279 | TAINT_set( |
280 | cBOOL(cx->sb_rxtainted & | |
281 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)) | |
282 | ); | |
4a69216a DM |
283 | |
284 | /* sv_magic(), when adding magic (e.g.taint magic), also | |
285 | * recalculates any pos() magic, converting any byte offset | |
286 | * to utf8 offset. Make sure pos() is reset before this | |
287 | * happens rather than using the now invalid value (since | |
288 | * we've just replaced targ's pvx buffer with the | |
289 | * potentially shorter dstr buffer). Normally (i.e. in | |
290 | * non-taint cases), pos() gets removed a few lines later | |
291 | * with the SvSETMAGIC(). | |
292 | */ | |
293 | { | |
294 | MAGIC *mg; | |
295 | mg = mg_find_mglob(targ); | |
296 | if (mg) { | |
297 | MgBYTEPOS_set(mg, targ, SvPVX(targ), -1); | |
298 | } | |
299 | } | |
300 | ||
20be6587 DM |
301 | SvTAINT(TARG); |
302 | } | |
303 | /* PL_tainted must be correctly set for this mg_set */ | |
304 | SvSETMAGIC(TARG); | |
305 | TAINT_NOT; | |
80d88db0 | 306 | |
8e7e33ef | 307 | CX_LEAVE_SCOPE(cx); |
b405d38b | 308 | CX_POPSUBST(cx); |
5da525e9 | 309 | CX_POP(cx); |
80d88db0 | 310 | |
47c9d59f | 311 | PERL_ASYNC_CHECK(); |
a0d0e21e | 312 | RETURNOP(pm->op_next); |
e5964223 | 313 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e | 314 | } |
8e5e9ebe | 315 | cx->sb_iters = saviters; |
a0d0e21e | 316 | } |
07bc277f | 317 | if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) { |
a0d0e21e LW |
318 | m = s; |
319 | s = orig; | |
6502e081 | 320 | assert(!RX_SUBOFFSET(rx)); |
07bc277f | 321 | cx->sb_orig = orig = RX_SUBBEG(rx); |
a0d0e21e LW |
322 | s = orig + (m - s); |
323 | cx->sb_strend = s + (cx->sb_strend - m); | |
324 | } | |
07bc277f | 325 | cx->sb_m = m = RX_OFFS(rx)[0].start + orig; |
db79b45b | 326 | if (m > s) { |
bfed75c6 | 327 | if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ)) |
4bac9ae4 | 328 | sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv); |
db79b45b | 329 | else |
4bac9ae4 | 330 | sv_catpvn_nomg(dstr, s, m-s); |
db79b45b | 331 | } |
07bc277f | 332 | cx->sb_s = RX_OFFS(rx)[0].end + orig; |
084916e3 | 333 | { /* Update the pos() information. */ |
8ca8a454 NC |
334 | SV * const sv |
335 | = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ; | |
084916e3 | 336 | MAGIC *mg; |
a911bb25 DM |
337 | |
338 | /* the string being matched against may no longer be a string, | |
339 | * e.g. $_=0; s/.../$_++/ge */ | |
340 | ||
341 | if (!SvPOK(sv)) | |
342 | SvPV_force_nomg_nolen(sv); | |
343 | ||
96c2a8ff FC |
344 | if (!(mg = mg_find_mglob(sv))) { |
345 | mg = sv_magicext_mglob(sv); | |
084916e3 | 346 | } |
cda67c99 | 347 | MgBYTEPOS_set(mg, sv, SvPVX(sv), m - orig); |
084916e3 | 348 | } |
988e6e7e | 349 | if (old != rx) |
d6106309 | 350 | (void)ReREFCNT_inc(rx); |
20be6587 | 351 | /* update the taint state of various various variables in preparation |
ef07e810 DM |
352 | * for calling the code block. |
353 | * See "how taint works" above pp_subst() */ | |
284167a5 | 354 | if (TAINTING_get) { |
20be6587 DM |
355 | if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */ |
356 | cx->sb_rxtainted |= SUBST_TAINT_PAT; | |
357 | ||
358 | if ((cx->sb_rxtainted & SUBST_TAINT_PAT) || | |
359 | ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
360 | == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
361 | ) | |
362 | (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */ | |
363 | ||
364 | if (cx->sb_iters > 1 && (cx->sb_rxtainted & | |
365 | (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))) | |
8ca8a454 NC |
366 | SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT) |
367 | ? cx->sb_dstr : cx->sb_targ); | |
20be6587 DM |
368 | TAINT_NOT; |
369 | } | |
d9f97599 | 370 | rxres_save(&cx->sb_rxres, rx); |
af9838cc | 371 | PL_curpm = pm; |
29f2e912 | 372 | RETURNOP(pm->op_pmstashstartu.op_pmreplstart); |
a0d0e21e LW |
373 | } |
374 | ||
c90c0ff4 | 375 | void |
864dbfa3 | 376 | Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx) |
c90c0ff4 | 377 | { |
378 | UV *p = (UV*)*rsp; | |
379 | U32 i; | |
7918f24d NC |
380 | |
381 | PERL_ARGS_ASSERT_RXRES_SAVE; | |
96a5add6 | 382 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 383 | |
07bc277f | 384 | if (!p || p[1] < RX_NPARENS(rx)) { |
db2c6cb3 | 385 | #ifdef PERL_ANY_COW |
6502e081 | 386 | i = 7 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 387 | #else |
6502e081 | 388 | i = 6 + (RX_NPARENS(rx)+1) * 2; |
ed252734 | 389 | #endif |
c90c0ff4 | 390 | if (!p) |
a02a5408 | 391 | Newx(p, i, UV); |
c90c0ff4 | 392 | else |
393 | Renew(p, i, UV); | |
394 | *rsp = (void*)p; | |
395 | } | |
396 | ||
5eabab15 DM |
397 | /* what (if anything) to free on croak */ |
398 | *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL); | |
cf93c79d | 399 | RX_MATCH_COPIED_off(rx); |
6c31ff74 | 400 | *p++ = RX_NPARENS(rx); |
c90c0ff4 | 401 | |
db2c6cb3 | 402 | #ifdef PERL_ANY_COW |
bdd9a1b1 NC |
403 | *p++ = PTR2UV(RX_SAVED_COPY(rx)); |
404 | RX_SAVED_COPY(rx) = NULL; | |
ed252734 NC |
405 | #endif |
406 | ||
07bc277f NC |
407 | *p++ = PTR2UV(RX_SUBBEG(rx)); |
408 | *p++ = (UV)RX_SUBLEN(rx); | |
6502e081 DM |
409 | *p++ = (UV)RX_SUBOFFSET(rx); |
410 | *p++ = (UV)RX_SUBCOFFSET(rx); | |
07bc277f NC |
411 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
412 | *p++ = (UV)RX_OFFS(rx)[i].start; | |
413 | *p++ = (UV)RX_OFFS(rx)[i].end; | |
c90c0ff4 | 414 | } |
415 | } | |
416 | ||
9c105995 NC |
417 | static void |
418 | S_rxres_restore(pTHX_ void **rsp, REGEXP *rx) | |
c90c0ff4 | 419 | { |
420 | UV *p = (UV*)*rsp; | |
421 | U32 i; | |
7918f24d NC |
422 | |
423 | PERL_ARGS_ASSERT_RXRES_RESTORE; | |
96a5add6 | 424 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 425 | |
ed252734 | 426 | RX_MATCH_COPY_FREE(rx); |
cf93c79d | 427 | RX_MATCH_COPIED_set(rx, *p); |
c90c0ff4 | 428 | *p++ = 0; |
6c31ff74 | 429 | RX_NPARENS(rx) = *p++; |
c90c0ff4 | 430 | |
db2c6cb3 | 431 | #ifdef PERL_ANY_COW |
bdd9a1b1 NC |
432 | if (RX_SAVED_COPY(rx)) |
433 | SvREFCNT_dec (RX_SAVED_COPY(rx)); | |
434 | RX_SAVED_COPY(rx) = INT2PTR(SV*,*p); | |
ed252734 NC |
435 | *p++ = 0; |
436 | #endif | |
437 | ||
07bc277f NC |
438 | RX_SUBBEG(rx) = INT2PTR(char*,*p++); |
439 | RX_SUBLEN(rx) = (I32)(*p++); | |
6502e081 DM |
440 | RX_SUBOFFSET(rx) = (I32)*p++; |
441 | RX_SUBCOFFSET(rx) = (I32)*p++; | |
07bc277f NC |
442 | for (i = 0; i <= RX_NPARENS(rx); ++i) { |
443 | RX_OFFS(rx)[i].start = (I32)(*p++); | |
444 | RX_OFFS(rx)[i].end = (I32)(*p++); | |
c90c0ff4 | 445 | } |
446 | } | |
447 | ||
9c105995 NC |
448 | static void |
449 | S_rxres_free(pTHX_ void **rsp) | |
c90c0ff4 | 450 | { |
44f8325f | 451 | UV * const p = (UV*)*rsp; |
7918f24d NC |
452 | |
453 | PERL_ARGS_ASSERT_RXRES_FREE; | |
96a5add6 | 454 | PERL_UNUSED_CONTEXT; |
c90c0ff4 | 455 | |
456 | if (p) { | |
94010e71 | 457 | void *tmp = INT2PTR(char*,*p); |
6c31ff74 | 458 | #ifdef PERL_POISON |
db2c6cb3 | 459 | #ifdef PERL_ANY_COW |
6c31ff74 | 460 | U32 i = 9 + p[1] * 2; |
94010e71 | 461 | #else |
6c31ff74 | 462 | U32 i = 8 + p[1] * 2; |
94010e71 | 463 | #endif |
6c31ff74 NC |
464 | #endif |
465 | ||
db2c6cb3 | 466 | #ifdef PERL_ANY_COW |
6c31ff74 | 467 | SvREFCNT_dec (INT2PTR(SV*,p[2])); |
ed252734 | 468 | #endif |
6c31ff74 NC |
469 | #ifdef PERL_POISON |
470 | PoisonFree(p, i, sizeof(UV)); | |
471 | #endif | |
472 | ||
473 | Safefree(tmp); | |
c90c0ff4 | 474 | Safefree(p); |
4608196e | 475 | *rsp = NULL; |
c90c0ff4 | 476 | } |
477 | } | |
478 | ||
a701009a DM |
479 | #define FORM_NUM_BLANK (1<<30) |
480 | #define FORM_NUM_POINT (1<<29) | |
481 | ||
a0d0e21e LW |
482 | PP(pp_formline) |
483 | { | |
20b7effb | 484 | dSP; dMARK; dORIGMARK; |
eb578fdb | 485 | SV * const tmpForm = *++MARK; |
086b26f3 | 486 | SV *formsv; /* contains text of original format */ |
eb578fdb KW |
487 | U32 *fpc; /* format ops program counter */ |
488 | char *t; /* current append position in target string */ | |
086b26f3 | 489 | const char *f; /* current position in format string */ |
eb578fdb KW |
490 | I32 arg; |
491 | SV *sv = NULL; /* current item */ | |
086b26f3 | 492 | const char *item = NULL;/* string value of current item */ |
9b4bdfd4 DM |
493 | I32 itemsize = 0; /* length (chars) of item, possibly truncated */ |
494 | I32 itembytes = 0; /* as itemsize, but length in bytes */ | |
086b26f3 DM |
495 | I32 fieldsize = 0; /* width of current field */ |
496 | I32 lines = 0; /* number of lines that have been output */ | |
497 | bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */ | |
498 | const char *chophere = NULL; /* where to chop current item */ | |
f5ada144 | 499 | STRLEN linemark = 0; /* pos of start of line in output */ |
65202027 | 500 | NV value; |
086b26f3 | 501 | bool gotsome = FALSE; /* seen at least one non-blank item on this line */ |
9b4bdfd4 | 502 | STRLEN len; /* length of current sv */ |
732f08da | 503 | STRLEN linemax; /* estimate of output size in bytes */ |
1bd51a4c IH |
504 | bool item_is_utf8 = FALSE; |
505 | bool targ_is_utf8 = FALSE; | |
bd7084a6 | 506 | const char *fmt; |
74e0ddf7 | 507 | MAGIC *mg = NULL; |
4ff700b9 DM |
508 | U8 *source; /* source of bytes to append */ |
509 | STRLEN to_copy; /* how may bytes to append */ | |
ea60cfe8 | 510 | char trans; /* what chars to translate */ |
92ff660b | 511 | bool copied_form = FALSE; /* have we duplicated the form? */ |
74e0ddf7 | 512 | |
3808a683 | 513 | mg = doparseform(tmpForm); |
a0d0e21e | 514 | |
74e0ddf7 | 515 | fpc = (U32*)mg->mg_ptr; |
3808a683 DM |
516 | /* the actual string the format was compiled from. |
517 | * with overload etc, this may not match tmpForm */ | |
518 | formsv = mg->mg_obj; | |
519 | ||
74e0ddf7 | 520 | |
3280af22 | 521 | SvPV_force(PL_formtarget, len); |
3808a683 | 522 | if (SvTAINTED(tmpForm) || SvTAINTED(formsv)) |
125b9982 | 523 | SvTAINTED_on(PL_formtarget); |
1bd51a4c IH |
524 | if (DO_UTF8(PL_formtarget)) |
525 | targ_is_utf8 = TRUE; | |
732f08da DM |
526 | /* this is an initial estimate of how much output buffer space |
527 | * to allocate. It may be exceeded later */ | |
528 | linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1); | |
529 | t = SvGROW(PL_formtarget, len + linemax + 1); | |
26e935cf | 530 | /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */ |
a0d0e21e | 531 | t += len; |
3808a683 | 532 | f = SvPV_const(formsv, len); |
a0d0e21e LW |
533 | |
534 | for (;;) { | |
535 | DEBUG_f( { | |
bfed75c6 | 536 | const char *name = "???"; |
a0d0e21e LW |
537 | arg = -1; |
538 | switch (*fpc) { | |
539 | case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break; | |
540 | case FF_BLANK: arg = fpc[1]; name = "BLANK"; break; | |
541 | case FF_SKIP: arg = fpc[1]; name = "SKIP"; break; | |
542 | case FF_FETCH: arg = fpc[1]; name = "FETCH"; break; | |
543 | case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break; | |
544 | ||
545 | case FF_CHECKNL: name = "CHECKNL"; break; | |
546 | case FF_CHECKCHOP: name = "CHECKCHOP"; break; | |
547 | case FF_SPACE: name = "SPACE"; break; | |
548 | case FF_HALFSPACE: name = "HALFSPACE"; break; | |
549 | case FF_ITEM: name = "ITEM"; break; | |
550 | case FF_CHOP: name = "CHOP"; break; | |
551 | case FF_LINEGLOB: name = "LINEGLOB"; break; | |
552 | case FF_NEWLINE: name = "NEWLINE"; break; | |
553 | case FF_MORE: name = "MORE"; break; | |
554 | case FF_LINEMARK: name = "LINEMARK"; break; | |
555 | case FF_END: name = "END"; break; | |
bfed75c6 | 556 | case FF_0DECIMAL: name = "0DECIMAL"; break; |
a1b95068 | 557 | case FF_LINESNGL: name = "LINESNGL"; break; |
a0d0e21e LW |
558 | } |
559 | if (arg >= 0) | |
bf49b057 | 560 | PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg); |
a0d0e21e | 561 | else |
bf49b057 | 562 | PerlIO_printf(Perl_debug_log, "%-16s\n", name); |
5f80b19c | 563 | } ); |
a0d0e21e | 564 | switch (*fpc++) { |
4a73dc0b | 565 | case FF_LINEMARK: /* start (or end) of a line */ |
f5ada144 | 566 | linemark = t - SvPVX(PL_formtarget); |
a0d0e21e LW |
567 | lines++; |
568 | gotsome = FALSE; | |
569 | break; | |
570 | ||
4a73dc0b | 571 | case FF_LITERAL: /* append <arg> literal chars */ |
ea60cfe8 DM |
572 | to_copy = *fpc++; |
573 | source = (U8 *)f; | |
574 | f += to_copy; | |
575 | trans = '~'; | |
75645721 | 576 | item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv); |
ea60cfe8 | 577 | goto append; |
a0d0e21e | 578 | |
4a73dc0b | 579 | case FF_SKIP: /* skip <arg> chars in format */ |
a0d0e21e LW |
580 | f += *fpc++; |
581 | break; | |
582 | ||
4a73dc0b | 583 | case FF_FETCH: /* get next item and set field size to <arg> */ |
a0d0e21e LW |
584 | arg = *fpc++; |
585 | f += arg; | |
586 | fieldsize = arg; | |
587 | ||
588 | if (MARK < SP) | |
589 | sv = *++MARK; | |
590 | else { | |
3280af22 | 591 | sv = &PL_sv_no; |
a2a5de95 | 592 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments"); |
a0d0e21e | 593 | } |
125b9982 NT |
594 | if (SvTAINTED(sv)) |
595 | SvTAINTED_on(PL_formtarget); | |
a0d0e21e LW |
596 | break; |
597 | ||
4a73dc0b | 598 | case FF_CHECKNL: /* find max len of item (up to \n) that fits field */ |
5a34cab7 | 599 | { |
5a34cab7 | 600 | const char *s = item = SvPV_const(sv, len); |
9b4bdfd4 DM |
601 | const char *send = s + len; |
602 | ||
603 | itemsize = 0; | |
604 | item_is_utf8 = DO_UTF8(sv); | |
605 | while (s < send) { | |
606 | if (!isCNTRL(*s)) | |
607 | gotsome = TRUE; | |
608 | else if (*s == '\n') | |
609 | break; | |
610 | ||
611 | if (item_is_utf8) | |
612 | s += UTF8SKIP(s); | |
613 | else | |
614 | s++; | |
615 | itemsize++; | |
616 | if (itemsize == fieldsize) | |
617 | break; | |
618 | } | |
619 | itembytes = s - item; | |
62db6ea5 | 620 | chophere = s; |
5a34cab7 | 621 | break; |
a0ed51b3 | 622 | } |
a0d0e21e | 623 | |
4a73dc0b | 624 | case FF_CHECKCHOP: /* like CHECKNL, but up to highest split point */ |
5a34cab7 NC |
625 | { |
626 | const char *s = item = SvPV_const(sv, len); | |
9b4bdfd4 DM |
627 | const char *send = s + len; |
628 | I32 size = 0; | |
629 | ||
630 | chophere = NULL; | |
631 | item_is_utf8 = DO_UTF8(sv); | |
632 | while (s < send) { | |
633 | /* look for a legal split position */ | |
634 | if (isSPACE(*s)) { | |
635 | if (*s == '\r') { | |
636 | chophere = s; | |
637 | itemsize = size; | |
638 | break; | |
639 | } | |
640 | if (chopspace) { | |
641 | /* provisional split point */ | |
642 | chophere = s; | |
643 | itemsize = size; | |
644 | } | |
645 | /* we delay testing fieldsize until after we've | |
646 | * processed the possible split char directly | |
647 | * following the last field char; so if fieldsize=3 | |
648 | * and item="a b cdef", we consume "a b", not "a". | |
649 | * Ditto further down. | |
650 | */ | |
651 | if (size == fieldsize) | |
652 | break; | |
653 | } | |
654 | else { | |
655 | if (strchr(PL_chopset, *s)) { | |
656 | /* provisional split point */ | |
657 | /* for a non-space split char, we include | |
658 | * the split char; hence the '+1' */ | |
659 | chophere = s + 1; | |
660 | itemsize = size; | |
661 | } | |
662 | if (size == fieldsize) | |
663 | break; | |
664 | if (!isCNTRL(*s)) | |
665 | gotsome = TRUE; | |
666 | } | |
667 | ||
668 | if (item_is_utf8) | |
669 | s += UTF8SKIP(s); | |
670 | else | |
077dbbf3 | 671 | s++; |
9b4bdfd4 DM |
672 | size++; |
673 | } | |
674 | if (!chophere || s == send) { | |
675 | chophere = s; | |
676 | itemsize = size; | |
677 | } | |
678 | itembytes = chophere - item; | |
679 | ||
5a34cab7 | 680 | break; |
a0d0e21e | 681 | } |
a0d0e21e | 682 | |
4a73dc0b | 683 | case FF_SPACE: /* append padding space (diff of field, item size) */ |
a0d0e21e LW |
684 | arg = fieldsize - itemsize; |
685 | if (arg) { | |
686 | fieldsize -= arg; | |
687 | while (arg-- > 0) | |
688 | *t++ = ' '; | |
689 | } | |
690 | break; | |
691 | ||
4a73dc0b | 692 | case FF_HALFSPACE: /* like FF_SPACE, but only append half as many */ |
a0d0e21e LW |
693 | arg = fieldsize - itemsize; |
694 | if (arg) { | |
695 | arg /= 2; | |
696 | fieldsize -= arg; | |
697 | while (arg-- > 0) | |
698 | *t++ = ' '; | |
699 | } | |
700 | break; | |
701 | ||
4a73dc0b | 702 | case FF_ITEM: /* append a text item, while blanking ctrl chars */ |
9b4bdfd4 | 703 | to_copy = itembytes; |
8aa7beb6 DM |
704 | source = (U8 *)item; |
705 | trans = 1; | |
8aa7beb6 | 706 | goto append; |
a0d0e21e | 707 | |
4a73dc0b | 708 | case FF_CHOP: /* (for ^*) chop the current item */ |
fb9282c3 | 709 | if (sv != &PL_sv_no) { |
5a34cab7 | 710 | const char *s = chophere; |
86191aed TC |
711 | if (!copied_form && |
712 | ((sv == tmpForm || SvSMAGICAL(sv)) | |
713 | || (SvGMAGICAL(tmpForm) && !sv_only_taint_gmagic(tmpForm))) ) { | |
714 | /* sv and tmpForm are either the same SV, or magic might allow modification | |
715 | of tmpForm when sv is modified, so copy */ | |
716 | SV *newformsv = sv_mortalcopy(formsv); | |
717 | U32 *new_compiled; | |
718 | ||
719 | f = SvPV_nolen(newformsv) + (f - SvPV_nolen(formsv)); | |
720 | Newx(new_compiled, mg->mg_len / sizeof(U32), U32); | |
721 | memcpy(new_compiled, mg->mg_ptr, mg->mg_len); | |
722 | SAVEFREEPV(new_compiled); | |
723 | fpc = new_compiled + (fpc - (U32*)mg->mg_ptr); | |
724 | formsv = newformsv; | |
725 | ||
92ff660b | 726 | copied_form = TRUE; |
86191aed | 727 | } |
5a34cab7 | 728 | if (chopspace) { |
af68e756 | 729 | while (isSPACE(*s)) |
5a34cab7 NC |
730 | s++; |
731 | } | |
9b4bdfd4 DM |
732 | if (SvPOKp(sv)) |
733 | sv_chop(sv,s); | |
734 | else | |
735 | /* tied, overloaded or similar strangeness. | |
736 | * Do it the hard way */ | |
737 | sv_setpvn(sv, s, len - (s-item)); | |
5a34cab7 NC |
738 | SvSETMAGIC(sv); |
739 | break; | |
a0d0e21e | 740 | } |
2165bd23 | 741 | /* FALLTHROUGH */ |
a0d0e21e | 742 | |
4a73dc0b | 743 | case FF_LINESNGL: /* process ^* */ |
a1b95068 | 744 | chopspace = 0; |
c67159e1 | 745 | /* FALLTHROUGH */ |
4a73dc0b DM |
746 | |
747 | case FF_LINEGLOB: /* process @* */ | |
5a34cab7 | 748 | { |
e32383e2 | 749 | const bool oneline = fpc[-1] == FF_LINESNGL; |
5a34cab7 | 750 | const char *s = item = SvPV_const(sv, len); |
7440a75b | 751 | const char *const send = s + len; |
7440a75b | 752 | |
f3f2f1a3 | 753 | item_is_utf8 = DO_UTF8(sv); |
fb9282c3 | 754 | chophere = s + len; |
a1137ee5 | 755 | if (!len) |
7440a75b | 756 | break; |
ea60cfe8 | 757 | trans = 0; |
0d21cefe | 758 | gotsome = TRUE; |
4ff700b9 DM |
759 | source = (U8 *) s; |
760 | to_copy = len; | |
0d21cefe DM |
761 | while (s < send) { |
762 | if (*s++ == '\n') { | |
763 | if (oneline) { | |
9b4bdfd4 | 764 | to_copy = s - item - 1; |
0d21cefe DM |
765 | chophere = s; |
766 | break; | |
767 | } else { | |
768 | if (s == send) { | |
0d21cefe DM |
769 | to_copy--; |
770 | } else | |
771 | lines++; | |
1bd51a4c | 772 | } |
a0d0e21e | 773 | } |
0d21cefe | 774 | } |
a2c0032b DM |
775 | } |
776 | ||
ea60cfe8 DM |
777 | append: |
778 | /* append to_copy bytes from source to PL_formstring. | |
779 | * item_is_utf8 implies source is utf8. | |
780 | * if trans, translate certain characters during the copy */ | |
a2c0032b DM |
781 | { |
782 | U8 *tmp = NULL; | |
732f08da | 783 | STRLEN grow = 0; |
0325ce87 | 784 | |
732f08da DM |
785 | SvCUR_set(PL_formtarget, |
786 | t - SvPVX_const(PL_formtarget)); | |
0325ce87 | 787 | |
0d21cefe DM |
788 | if (targ_is_utf8 && !item_is_utf8) { |
789 | source = tmp = bytes_to_utf8(source, &to_copy); | |
732f08da | 790 | grow = to_copy; |
0d21cefe DM |
791 | } else { |
792 | if (item_is_utf8 && !targ_is_utf8) { | |
f5ada144 | 793 | U8 *s; |
0d21cefe | 794 | /* Upgrade targ to UTF8, and then we reduce it to |
0325ce87 DM |
795 | a problem we have a simple solution for. |
796 | Don't need get magic. */ | |
0d21cefe | 797 | sv_utf8_upgrade_nomg(PL_formtarget); |
0325ce87 | 798 | targ_is_utf8 = TRUE; |
f5ada144 DM |
799 | /* re-calculate linemark */ |
800 | s = (U8*)SvPVX(PL_formtarget); | |
732f08da DM |
801 | /* the bytes we initially allocated to append the |
802 | * whole line may have been gobbled up during the | |
803 | * upgrade, so allocate a whole new line's worth | |
804 | * for safety */ | |
805 | grow = linemax; | |
f5ada144 | 806 | while (linemark--) |
40c725d1 KW |
807 | s += UTF8_SAFE_SKIP(s, |
808 | (U8 *) SvEND(PL_formtarget)); | |
f5ada144 | 809 | linemark = s - (U8*)SvPVX(PL_formtarget); |
e8e72d41 | 810 | } |
0d21cefe DM |
811 | /* Easy. They agree. */ |
812 | assert (item_is_utf8 == targ_is_utf8); | |
813 | } | |
732f08da DM |
814 | if (!trans) |
815 | /* @* and ^* are the only things that can exceed | |
816 | * the linemax, so grow by the output size, plus | |
817 | * a whole new form's worth in case of any further | |
818 | * output */ | |
819 | grow = linemax + to_copy; | |
820 | if (grow) | |
821 | SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1); | |
822 | t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget); | |
0d21cefe DM |
823 | |
824 | Copy(source, t, to_copy, char); | |
ea60cfe8 | 825 | if (trans) { |
8aa7beb6 DM |
826 | /* blank out ~ or control chars, depending on trans. |
827 | * works on bytes not chars, so relies on not | |
828 | * matching utf8 continuation bytes */ | |
ea60cfe8 DM |
829 | U8 *s = (U8*)t; |
830 | U8 *send = s + to_copy; | |
831 | while (s < send) { | |
8aa7beb6 | 832 | const int ch = *s; |
077dbbf3 | 833 | if (trans == '~' ? (ch == '~') : isCNTRL(ch)) |
ea60cfe8 DM |
834 | *s = ' '; |
835 | s++; | |
836 | } | |
837 | } | |
838 | ||
0d21cefe | 839 | t += to_copy; |
732f08da | 840 | SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy); |
a1137ee5 | 841 | if (tmp) |
0d21cefe | 842 | Safefree(tmp); |
5a34cab7 | 843 | break; |
a0d0e21e | 844 | } |
a0d0e21e | 845 | |
4a73dc0b | 846 | case FF_0DECIMAL: /* like FF_DECIMAL but for 0### */ |
a0d0e21e | 847 | arg = *fpc++; |
bd7084a6 | 848 | fmt = (const char *) |
a029fa42 | 849 | ((arg & FORM_NUM_POINT) ? "%#0*.*" NVff : "%0*.*" NVff); |
bd7084a6 | 850 | goto ff_dec; |
5d37acd6 | 851 | |
bd7084a6 DM |
852 | case FF_DECIMAL: /* do @##, ^##, where <arg>=(precision|flags) */ |
853 | arg = *fpc++; | |
bd7084a6 | 854 | fmt = (const char *) |
a029fa42 | 855 | ((arg & FORM_NUM_POINT) ? "%#*.*" NVff : "%*.*" NVff); |
bd7084a6 | 856 | ff_dec: |
784707d5 JP |
857 | /* If the field is marked with ^ and the value is undefined, |
858 | blank it out. */ | |
a701009a | 859 | if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) { |
784707d5 JP |
860 | arg = fieldsize; |
861 | while (arg--) | |
862 | *t++ = ' '; | |
863 | break; | |
864 | } | |
865 | gotsome = TRUE; | |
866 | value = SvNV(sv); | |
a1b95068 | 867 | /* overflow evidence */ |
bfed75c6 | 868 | if (num_overflow(value, fieldsize, arg)) { |
a1b95068 WL |
869 | arg = fieldsize; |
870 | while (arg--) | |
871 | *t++ = '#'; | |
872 | break; | |
873 | } | |
784707d5 JP |
874 | /* Formats aren't yet marked for locales, so assume "yes". */ |
875 | { | |
e8549682 JH |
876 | Size_t max = SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)); |
877 | int len; | |
67d796ae KW |
878 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
879 | STORE_LC_NUMERIC_SET_TO_NEEDED(); | |
51f14a05 | 880 | arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
a4eca1d4 JH |
881 | #ifdef USE_QUADMATH |
882 | { | |
883 | const char* qfmt = quadmath_format_single(fmt); | |
884 | int len; | |
885 | if (!qfmt) | |
886 | Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", fmt); | |
887 | len = quadmath_snprintf(t, max, qfmt, (int) fieldsize, (int) arg, value); | |
888 | if (len == -1) | |
889 | Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", qfmt); | |
890 | if (qfmt != fmt) | |
891 | Safefree(fmt); | |
892 | } | |
893 | #else | |
b587c0e8 | 894 | /* we generate fmt ourselves so it is safe */ |
7347ee54 | 895 | GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); |
e8549682 | 896 | len = my_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value); |
7347ee54 | 897 | GCC_DIAG_RESTORE_STMT; |
a4eca1d4 JH |
898 | #endif |
899 | PERL_MY_SNPRINTF_POST_GUARD(len, max); | |
a2287a13 | 900 | RESTORE_LC_NUMERIC(); |
784707d5 JP |
901 | } |
902 | t += fieldsize; | |
903 | break; | |
a1b95068 | 904 | |
4a73dc0b | 905 | case FF_NEWLINE: /* delete trailing spaces, then append \n */ |
a0d0e21e | 906 | f++; |
732f08da | 907 | while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ; |
a0d0e21e LW |
908 | t++; |
909 | *t++ = '\n'; | |
910 | break; | |
911 | ||
4a73dc0b | 912 | case FF_BLANK: /* for arg==0: do '~'; for arg>0 : do '~~' */ |
a0d0e21e LW |
913 | arg = *fpc++; |
914 | if (gotsome) { | |
915 | if (arg) { /* repeat until fields exhausted? */ | |
11f9eeaf DM |
916 | fpc--; |
917 | goto end; | |
a0d0e21e LW |
918 | } |
919 | } | |
920 | else { | |
f5ada144 | 921 | t = SvPVX(PL_formtarget) + linemark; |
a0d0e21e LW |
922 | lines--; |
923 | } | |
924 | break; | |
925 | ||
4a73dc0b | 926 | case FF_MORE: /* replace long end of string with '...' */ |
5a34cab7 NC |
927 | { |
928 | const char *s = chophere; | |
929 | const char *send = item + len; | |
930 | if (chopspace) { | |
af68e756 | 931 | while (isSPACE(*s) && (s < send)) |
5a34cab7 | 932 | s++; |
a0d0e21e | 933 | } |
5a34cab7 NC |
934 | if (s < send) { |
935 | char *s1; | |
936 | arg = fieldsize - itemsize; | |
937 | if (arg) { | |
938 | fieldsize -= arg; | |
939 | while (arg-- > 0) | |
940 | *t++ = ' '; | |
941 | } | |
942 | s1 = t - 3; | |
f55ac4a4 | 943 | if (strBEGINs(s1," ")) { |
5a34cab7 NC |
944 | while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1])) |
945 | s1--; | |
946 | } | |
947 | *s1++ = '.'; | |
948 | *s1++ = '.'; | |
949 | *s1++ = '.'; | |
a0d0e21e | 950 | } |
5a34cab7 | 951 | break; |
a0d0e21e | 952 | } |
4a73dc0b DM |
953 | |
954 | case FF_END: /* tidy up, then return */ | |
11f9eeaf | 955 | end: |
bf2bec63 | 956 | assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget)); |
a0d0e21e | 957 | *t = '\0'; |
b15aece3 | 958 | SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget)); |
1bd51a4c IH |
959 | if (targ_is_utf8) |
960 | SvUTF8_on(PL_formtarget); | |
3280af22 | 961 | FmLINES(PL_formtarget) += lines; |
a0d0e21e | 962 | SP = ORIGMARK; |
11f9eeaf DM |
963 | if (fpc[-1] == FF_BLANK) |
964 | RETURNOP(cLISTOP->op_first); | |
965 | else | |
966 | RETPUSHYES; | |
a0d0e21e LW |
967 | } |
968 | } | |
969 | } | |
970 | ||
10088f56 | 971 | /* also used for: pp_mapstart() */ |
a0d0e21e LW |
972 | PP(pp_grepstart) |
973 | { | |
20b7effb | 974 | dSP; |
a0d0e21e LW |
975 | SV *src; |
976 | ||
6cae08a8 | 977 | if (PL_stack_base + TOPMARK == SP) { |
a0d0e21e | 978 | (void)POPMARK; |
54310121 | 979 | if (GIMME_V == G_SCALAR) |
725c44f9 | 980 | XPUSHs(&PL_sv_zero); |
533c011a | 981 | RETURNOP(PL_op->op_next->op_next); |
a0d0e21e | 982 | } |
6cae08a8 | 983 | PL_stack_sp = PL_stack_base + TOPMARK + 1; |
897d3989 NC |
984 | Perl_pp_pushmark(aTHX); /* push dst */ |
985 | Perl_pp_pushmark(aTHX); /* push src */ | |
d343c3ef | 986 | ENTER_with_name("grep"); /* enter outer scope */ |
a0d0e21e LW |
987 | |
988 | SAVETMPS; | |
ffd49c98 | 989 | SAVE_DEFSV; |
d343c3ef | 990 | ENTER_with_name("grep_item"); /* enter inner scope */ |
7766f137 | 991 | SAVEVPTR(PL_curpm); |
a0d0e21e | 992 | |
6cae08a8 | 993 | src = PL_stack_base[TOPMARK]; |
60779a30 | 994 | if (SvPADTMP(src)) { |
6cae08a8 | 995 | src = PL_stack_base[TOPMARK] = sv_mortalcopy(src); |
a0ed822e FC |
996 | PL_tmps_floor++; |
997 | } | |
a0d0e21e | 998 | SvTEMP_off(src); |
ffd49c98 | 999 | DEFSV_set(src); |
a0d0e21e LW |
1000 | |
1001 | PUTBACK; | |
533c011a | 1002 | if (PL_op->op_type == OP_MAPSTART) |
897d3989 | 1003 | Perl_pp_pushmark(aTHX); /* push top */ |
533c011a | 1004 | return ((LOGOP*)PL_op->op_next)->op_other; |
a0d0e21e LW |
1005 | } |
1006 | ||
a0d0e21e LW |
1007 | PP(pp_mapwhile) |
1008 | { | |
20b7effb | 1009 | dSP; |
1c23e2bd | 1010 | const U8 gimme = GIMME_V; |
6cae08a8 | 1011 | I32 items = (SP - PL_stack_base) - TOPMARK; /* how many new items */ |
a0d0e21e LW |
1012 | I32 count; |
1013 | I32 shift; | |
1014 | SV** src; | |
ac27b0f5 | 1015 | SV** dst; |
a0d0e21e | 1016 | |
544f3153 | 1017 | /* first, move source pointer to the next item in the source list */ |
3280af22 | 1018 | ++PL_markstack_ptr[-1]; |
544f3153 GS |
1019 | |
1020 | /* if there are new items, push them into the destination list */ | |
4c90a460 | 1021 | if (items && gimme != G_VOID) { |
544f3153 GS |
1022 | /* might need to make room back there first */ |
1023 | if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) { | |
1024 | /* XXX this implementation is very pessimal because the stack | |
1025 | * is repeatedly extended for every set of items. Is possible | |
1026 | * to do this without any stack extension or copying at all | |
1027 | * by maintaining a separate list over which the map iterates | |
18ef8bea | 1028 | * (like foreach does). --gsar */ |
544f3153 GS |
1029 | |
1030 | /* everything in the stack after the destination list moves | |
1031 | * towards the end the stack by the amount of room needed */ | |
1032 | shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]); | |
1033 | ||
1034 | /* items to shift up (accounting for the moved source pointer) */ | |
1035 | count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1); | |
18ef8bea BT |
1036 | |
1037 | /* This optimization is by Ben Tilly and it does | |
1038 | * things differently from what Sarathy (gsar) | |
1039 | * is describing. The downside of this optimization is | |
1040 | * that leaves "holes" (uninitialized and hopefully unused areas) | |
1041 | * to the Perl stack, but on the other hand this | |
1042 | * shouldn't be a problem. If Sarathy's idea gets | |
1043 | * implemented, this optimization should become | |
1044 | * irrelevant. --jhi */ | |
1045 | if (shift < count) | |
1046 | shift = count; /* Avoid shifting too often --Ben Tilly */ | |
bfed75c6 | 1047 | |
924508f0 GS |
1048 | EXTEND(SP,shift); |
1049 | src = SP; | |
1050 | dst = (SP += shift); | |
3280af22 NIS |
1051 | PL_markstack_ptr[-1] += shift; |
1052 | *PL_markstack_ptr += shift; | |
544f3153 | 1053 | while (count--) |
a0d0e21e LW |
1054 | *dst-- = *src--; |
1055 | } | |
544f3153 | 1056 | /* copy the new items down to the destination list */ |
ac27b0f5 | 1057 | dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1; |
22023b26 | 1058 | if (gimme == G_ARRAY) { |
b2a2a901 DM |
1059 | /* add returned items to the collection (making mortal copies |
1060 | * if necessary), then clear the current temps stack frame | |
1061 | * *except* for those items. We do this splicing the items | |
1062 | * into the start of the tmps frame (so some items may be on | |
59d53fd6 | 1063 | * the tmps stack twice), then moving PL_tmps_floor above |
b2a2a901 DM |
1064 | * them, then freeing the frame. That way, the only tmps that |
1065 | * accumulate over iterations are the return values for map. | |
1066 | * We have to do to this way so that everything gets correctly | |
1067 | * freed if we die during the map. | |
1068 | */ | |
1069 | I32 tmpsbase; | |
1070 | I32 i = items; | |
1071 | /* make space for the slice */ | |
1072 | EXTEND_MORTAL(items); | |
1073 | tmpsbase = PL_tmps_floor + 1; | |
1074 | Move(PL_tmps_stack + tmpsbase, | |
1075 | PL_tmps_stack + tmpsbase + items, | |
1076 | PL_tmps_ix - PL_tmps_floor, | |
1077 | SV*); | |
1078 | PL_tmps_ix += items; | |
1079 | ||
1080 | while (i-- > 0) { | |
1081 | SV *sv = POPs; | |
1082 | if (!SvTEMP(sv)) | |
1083 | sv = sv_mortalcopy(sv); | |
1084 | *dst-- = sv; | |
1085 | PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv); | |
1086 | } | |
1087 | /* clear the stack frame except for the items */ | |
1088 | PL_tmps_floor += items; | |
1089 | FREETMPS; | |
1090 | /* FREETMPS may have cleared the TEMP flag on some of the items */ | |
1091 | i = items; | |
1092 | while (i-- > 0) | |
1093 | SvTEMP_on(PL_tmps_stack[--tmpsbase]); | |
22023b26 | 1094 | } |
bfed75c6 | 1095 | else { |
22023b26 TP |
1096 | /* scalar context: we don't care about which values map returns |
1097 | * (we use undef here). And so we certainly don't want to do mortal | |
1098 | * copies of meaningless values. */ | |
1099 | while (items-- > 0) { | |
b988aa42 | 1100 | (void)POPs; |
22023b26 TP |
1101 | *dst-- = &PL_sv_undef; |
1102 | } | |
b2a2a901 | 1103 | FREETMPS; |
22023b26 | 1104 | } |
a0d0e21e | 1105 | } |
b2a2a901 DM |
1106 | else { |
1107 | FREETMPS; | |
1108 | } | |
d343c3ef | 1109 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
a0d0e21e LW |
1110 | |
1111 | /* All done yet? */ | |
6cae08a8 | 1112 | if (PL_markstack_ptr[-1] > TOPMARK) { |
a0d0e21e LW |
1113 | |
1114 | (void)POPMARK; /* pop top */ | |
d343c3ef | 1115 | LEAVE_with_name("grep"); /* exit outer scope */ |
a0d0e21e | 1116 | (void)POPMARK; /* pop src */ |
3280af22 | 1117 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; |
a0d0e21e | 1118 | (void)POPMARK; /* pop dst */ |
3280af22 | 1119 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
54310121 | 1120 | if (gimme == G_SCALAR) { |
7cc47870 RGS |
1121 | dTARGET; |
1122 | XPUSHi(items); | |
a0d0e21e | 1123 | } |
54310121 | 1124 | else if (gimme == G_ARRAY) |
1125 | SP += items; | |
a0d0e21e LW |
1126 | RETURN; |
1127 | } | |
1128 | else { | |
1129 | SV *src; | |
1130 | ||
d343c3ef | 1131 | ENTER_with_name("grep_item"); /* enter inner scope */ |
7766f137 | 1132 | SAVEVPTR(PL_curpm); |
a0d0e21e | 1133 | |
544f3153 | 1134 | /* set $_ to the new source item */ |
3280af22 | 1135 | src = PL_stack_base[PL_markstack_ptr[-1]]; |
60779a30 | 1136 | if (SvPADTMP(src)) { |
60779a30 DM |
1137 | src = sv_mortalcopy(src); |
1138 | } | |
a0d0e21e | 1139 | SvTEMP_off(src); |
ffd49c98 | 1140 | DEFSV_set(src); |
a0d0e21e LW |
1141 | |
1142 | RETURNOP(cLOGOP->op_other); | |
1143 | } | |
1144 | } | |
1145 | ||
a0d0e21e LW |
1146 | /* Range stuff. */ |
1147 | ||
1148 | PP(pp_range) | |
1149 | { | |
f4c975aa | 1150 | dTARG; |
82334630 | 1151 | if (GIMME_V == G_ARRAY) |
1a67a97c | 1152 | return NORMAL; |
f4c975aa DM |
1153 | GETTARGET; |
1154 | if (SvTRUE_NN(targ)) | |
1a67a97c | 1155 | return cLOGOP->op_other; |
538573f7 | 1156 | else |
1a67a97c | 1157 | return NORMAL; |
a0d0e21e LW |
1158 | } |
1159 | ||
1160 | PP(pp_flip) | |
1161 | { | |
39644a26 | 1162 | dSP; |
a0d0e21e | 1163 | |
82334630 | 1164 | if (GIMME_V == G_ARRAY) { |
1a67a97c | 1165 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
a0d0e21e LW |
1166 | } |
1167 | else { | |
1168 | dTOPss; | |
44f8325f | 1169 | SV * const targ = PAD_SV(PL_op->op_targ); |
bfed75c6 | 1170 | int flip = 0; |
790090df | 1171 | |
bfed75c6 | 1172 | if (PL_op->op_private & OPpFLIP_LINENUM) { |
4e3399f9 YST |
1173 | if (GvIO(PL_last_in_gv)) { |
1174 | flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1175 | } | |
1176 | else { | |
fafc274c | 1177 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
44f8325f AL |
1178 | if (gv && GvSV(gv)) |
1179 | flip = SvIV(sv) == SvIV(GvSV(gv)); | |
4e3399f9 | 1180 | } |
bfed75c6 | 1181 | } else { |
f4c975aa | 1182 | flip = SvTRUE_NN(sv); |
bfed75c6 AL |
1183 | } |
1184 | if (flip) { | |
a0d0e21e | 1185 | sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1); |
533c011a | 1186 | if (PL_op->op_flags & OPf_SPECIAL) { |
a0d0e21e | 1187 | sv_setiv(targ, 1); |
3e3baf6d | 1188 | SETs(targ); |
a0d0e21e LW |
1189 | RETURN; |
1190 | } | |
1191 | else { | |
1192 | sv_setiv(targ, 0); | |
924508f0 | 1193 | SP--; |
1a67a97c | 1194 | RETURNOP(((LOGOP*)cUNOP->op_first)->op_other); |
a0d0e21e LW |
1195 | } |
1196 | } | |
9aa3b533 | 1197 | SvPVCLEAR(TARG); |
a0d0e21e LW |
1198 | SETs(targ); |
1199 | RETURN; | |
1200 | } | |
1201 | } | |
1202 | ||
8e9bbdb9 | 1203 | /* This code tries to decide if "$left .. $right" should use the |
d1bc97fe H |
1204 | magical string increment, or if the range is numeric. Initially, |
1205 | an exception was made for *any* string beginning with "0" (see | |
1206 | [#18165], AMS 20021031), but now that is only applied when the | |
1207 | string's length is also >1 - see the rules now documented in | |
1208 | perlop [#133695] */ | |
8e9bbdb9 RGS |
1209 | |
1210 | #define RANGE_IS_NUMERIC(left,right) ( \ | |
b0e74086 RGS |
1211 | SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \ |
1212 | SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \ | |
e0ab1c0e | 1213 | (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \ |
d1bc97fe H |
1214 | looks_like_number(left)) && SvPOKp(left) \ |
1215 | && !(*SvPVX_const(left) == '0' && SvCUR(left)>1 ) )) \ | |
e0ab1c0e | 1216 | && (!SvOK(right) || looks_like_number(right)))) |
8e9bbdb9 | 1217 | |
a0d0e21e LW |
1218 | PP(pp_flop) |
1219 | { | |
20b7effb | 1220 | dSP; |
a0d0e21e | 1221 | |
82334630 | 1222 | if (GIMME_V == G_ARRAY) { |
a0d0e21e | 1223 | dPOPPOPssrl; |
86cb7173 | 1224 | |
5b295bef RD |
1225 | SvGETMAGIC(left); |
1226 | SvGETMAGIC(right); | |
a0d0e21e | 1227 | |
8e9bbdb9 | 1228 | if (RANGE_IS_NUMERIC(left,right)) { |
b262c4c9 | 1229 | IV i, j, n; |
4d91eccc FC |
1230 | if ((SvOK(left) && !SvIOK(left) && SvNV_nomg(left) < IV_MIN) || |
1231 | (SvOK(right) && (SvIOK(right) | |
1232 | ? SvIsUV(right) && SvUV(right) > IV_MAX | |
1233 | : SvNV_nomg(right) > IV_MAX))) | |
d470f89e | 1234 | DIE(aTHX_ "Range iterator outside integer range"); |
f52e41ad | 1235 | i = SvIV_nomg(left); |
b262c4c9 JH |
1236 | j = SvIV_nomg(right); |
1237 | if (j >= i) { | |
1238 | /* Dance carefully around signed max. */ | |
1239 | bool overflow = (i <= 0 && j > SSize_t_MAX + i - 1); | |
1240 | if (!overflow) { | |
1241 | n = j - i + 1; | |
1242 | /* The wraparound of signed integers is undefined | |
1243 | * behavior, but here we aim for count >=1, and | |
1244 | * negative count is just wrong. */ | |
a1e27170 TC |
1245 | if (n < 1 |
1246 | #if IVSIZE > Size_t_size | |
1247 | || n > SSize_t_MAX | |
1248 | #endif | |
1249 | ) | |
b262c4c9 JH |
1250 | overflow = TRUE; |
1251 | } | |
1252 | if (overflow) | |
1253 | Perl_croak(aTHX_ "Out of memory during list extend"); | |
1254 | EXTEND_MORTAL(n); | |
1255 | EXTEND(SP, n); | |
bbce6d69 | 1256 | } |
c1ab3db2 | 1257 | else |
b262c4c9 JH |
1258 | n = 0; |
1259 | while (n--) { | |
fc01cab4 | 1260 | SV * const sv = sv_2mortal(newSViv(i)); |
a0d0e21e | 1261 | PUSHs(sv); |
fc01cab4 DM |
1262 | if (n) /* avoid incrementing above IV_MAX */ |
1263 | i++; | |
a0d0e21e LW |
1264 | } |
1265 | } | |
1266 | else { | |
3c323193 FC |
1267 | STRLEN len, llen; |
1268 | const char * const lpv = SvPV_nomg_const(left, llen); | |
f52e41ad | 1269 | const char * const tmps = SvPV_nomg_const(right, len); |
a0d0e21e | 1270 | |
3c323193 | 1271 | SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP); |
d6c970c7 AC |
1272 | if (DO_UTF8(right) && IN_UNI_8_BIT) |
1273 | len = sv_len_utf8_nomg(right); | |
89ea2908 | 1274 | while (!SvNIOKp(sv) && SvCUR(sv) <= len) { |
a0d0e21e | 1275 | XPUSHs(sv); |
b15aece3 | 1276 | if (strEQ(SvPVX_const(sv),tmps)) |
89ea2908 | 1277 | break; |
a0d0e21e LW |
1278 | sv = sv_2mortal(newSVsv(sv)); |
1279 | sv_inc(sv); | |
1280 | } | |
a0d0e21e LW |
1281 | } |
1282 | } | |
1283 | else { | |
1284 | dTOPss; | |
901017d6 | 1285 | SV * const targ = PAD_SV(cUNOP->op_first->op_targ); |
4e3399f9 | 1286 | int flop = 0; |
a0d0e21e | 1287 | sv_inc(targ); |
4e3399f9 YST |
1288 | |
1289 | if (PL_op->op_private & OPpFLIP_LINENUM) { | |
1290 | if (GvIO(PL_last_in_gv)) { | |
1291 | flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)); | |
1292 | } | |
1293 | else { | |
fafc274c | 1294 | GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV); |
4e3399f9 YST |
1295 | if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv)); |
1296 | } | |
1297 | } | |
1298 | else { | |
f4c975aa | 1299 | flop = SvTRUE_NN(sv); |
4e3399f9 YST |
1300 | } |
1301 | ||
1302 | if (flop) { | |
a0d0e21e | 1303 | sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0); |
396482e1 | 1304 | sv_catpvs(targ, "E0"); |
a0d0e21e LW |
1305 | } |
1306 | SETs(targ); | |
1307 | } | |
1308 | ||
1309 | RETURN; | |
1310 | } | |
1311 | ||
1312 | /* Control. */ | |
1313 | ||
27da23d5 | 1314 | static const char * const context_name[] = { |
515afda2 | 1315 | "pseudo-block", |
7896dde7 | 1316 | NULL, /* CXt_WHEN never actually needs "block" */ |
76753e7f | 1317 | NULL, /* CXt_BLOCK never actually needs "block" */ |
7896dde7 | 1318 | NULL, /* CXt_GIVEN never actually needs "block" */ |
76753e7f | 1319 | NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */ |
76753e7f | 1320 | NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */ |
93661e56 DM |
1321 | NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */ |
1322 | NULL, /* CXt_LOOP_LIST never actually needs "loop" */ | |
1323 | NULL, /* CXt_LOOP_ARY never actually needs "loop" */ | |
515afda2 | 1324 | "subroutine", |
76753e7f | 1325 | "format", |
515afda2 | 1326 | "eval", |
515afda2 | 1327 | "substitution", |
515afda2 NC |
1328 | }; |
1329 | ||
76e3520e | 1330 | STATIC I32 |
5db1eb8d | 1331 | S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags) |
a0d0e21e | 1332 | { |
eb578fdb | 1333 | I32 i; |
a0d0e21e | 1334 | |
7918f24d NC |
1335 | PERL_ARGS_ASSERT_DOPOPTOLABEL; |
1336 | ||
a0d0e21e | 1337 | for (i = cxstack_ix; i >= 0; i--) { |
eb578fdb | 1338 | const PERL_CONTEXT * const cx = &cxstack[i]; |
6b35e009 | 1339 | switch (CxTYPE(cx)) { |
a0d0e21e | 1340 | case CXt_SUBST: |
a0d0e21e | 1341 | case CXt_SUB: |
7766f137 | 1342 | case CXt_FORMAT: |
a0d0e21e | 1343 | case CXt_EVAL: |
0a753a76 | 1344 | case CXt_NULL: |
dcbac5bb | 1345 | /* diag_listed_as: Exiting subroutine via %s */ |
a2a5de95 NC |
1346 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
1347 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
79646418 | 1348 | if (CxTYPE(cx) == CXt_NULL) /* sort BLOCK */ |
515afda2 NC |
1349 | return -1; |
1350 | break; | |
93661e56 | 1351 | case CXt_LOOP_PLAIN: |
c6fdafd0 | 1352 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1353 | case CXt_LOOP_LAZYSV: |
93661e56 DM |
1354 | case CXt_LOOP_LIST: |
1355 | case CXt_LOOP_ARY: | |
7e8f1eac | 1356 | { |
5db1eb8d BF |
1357 | STRLEN cx_label_len = 0; |
1358 | U32 cx_label_flags = 0; | |
1359 | const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags); | |
1360 | if (!cx_label || !( | |
1361 | ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? | |
1362 | (flags & SVf_UTF8) | |
1363 | ? (bytes_cmp_utf8( | |
1364 | (const U8*)cx_label, cx_label_len, | |
1365 | (const U8*)label, len) == 0) | |
1366 | : (bytes_cmp_utf8( | |
1367 | (const U8*)label, len, | |
1368 | (const U8*)cx_label, cx_label_len) == 0) | |
eade7155 BF |
1369 | : (len == cx_label_len && ((cx_label == label) |
1370 | || memEQ(cx_label, label, len))) )) { | |
1c98cc53 | 1371 | DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n", |
7e8f1eac | 1372 | (long)i, cx_label)); |
a0d0e21e LW |
1373 | continue; |
1374 | } | |
1c98cc53 | 1375 | DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label)); |
a0d0e21e | 1376 | return i; |
7e8f1eac | 1377 | } |
a0d0e21e LW |
1378 | } |
1379 | } | |
1380 | return i; | |
1381 | } | |
1382 | ||
0d863452 RH |
1383 | |
1384 | ||
1c23e2bd | 1385 | U8 |
864dbfa3 | 1386 | Perl_dowantarray(pTHX) |
e50aee73 | 1387 | { |
1c23e2bd | 1388 | const U8 gimme = block_gimme(); |
54310121 | 1389 | return (gimme == G_VOID) ? G_SCALAR : gimme; |
1390 | } | |
1391 | ||
5b6f7443 DM |
1392 | /* note that this function has mostly been superseded by Perl_gimme_V */ |
1393 | ||
1c23e2bd | 1394 | U8 |
864dbfa3 | 1395 | Perl_block_gimme(pTHX) |
54310121 | 1396 | { |
5b6f7443 | 1397 | const I32 cxix = dopopto_cursub(); |
a05700a8 | 1398 | U8 gimme; |
e50aee73 | 1399 | if (cxix < 0) |
46fc3d4c | 1400 | return G_VOID; |
e50aee73 | 1401 | |
a05700a8 DM |
1402 | gimme = (cxstack[cxix].blk_gimme & G_WANT); |
1403 | if (!gimme) | |
1404 | Perl_croak(aTHX_ "panic: bad gimme: %d\n", gimme); | |
1405 | return gimme; | |
e50aee73 AD |
1406 | } |
1407 | ||
a05700a8 | 1408 | |
78f9721b SM |
1409 | I32 |
1410 | Perl_is_lvalue_sub(pTHX) | |
1411 | { | |
5b6f7443 | 1412 | const I32 cxix = dopopto_cursub(); |
78f9721b SM |
1413 | assert(cxix >= 0); /* We should only be called from inside subs */ |
1414 | ||
bafb2adc NC |
1415 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) |
1416 | return CxLVAL(cxstack + cxix); | |
78f9721b SM |
1417 | else |
1418 | return 0; | |
1419 | } | |
1420 | ||
a73d8813 | 1421 | /* only used by cx_pushsub() */ |
777d9014 FC |
1422 | I32 |
1423 | Perl_was_lvalue_sub(pTHX) | |
1424 | { | |
777d9014 FC |
1425 | const I32 cxix = dopoptosub(cxstack_ix-1); |
1426 | assert(cxix >= 0); /* We should only be called from inside subs */ | |
1427 | ||
1428 | if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv)) | |
1429 | return CxLVAL(cxstack + cxix); | |
1430 | else | |
1431 | return 0; | |
1432 | } | |
1433 | ||
76e3520e | 1434 | STATIC I32 |
901017d6 | 1435 | S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock) |
2c375eb9 | 1436 | { |
a0d0e21e | 1437 | I32 i; |
7918f24d NC |
1438 | |
1439 | PERL_ARGS_ASSERT_DOPOPTOSUB_AT; | |
81611534 JH |
1440 | #ifndef DEBUGGING |
1441 | PERL_UNUSED_CONTEXT; | |
1442 | #endif | |
7918f24d | 1443 | |
a0d0e21e | 1444 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1445 | const PERL_CONTEXT * const cx = &cxstk[i]; |
6b35e009 | 1446 | switch (CxTYPE(cx)) { |
a0d0e21e LW |
1447 | default: |
1448 | continue; | |
a0d0e21e | 1449 | case CXt_SUB: |
5fbe8311 DM |
1450 | /* in sub foo { /(?{...})/ }, foo ends up on the CX stack |
1451 | * twice; the first for the normal foo() call, and the second | |
1452 | * for a faked up re-entry into the sub to execute the | |
1453 | * code block. Hide this faked entry from the world. */ | |
1454 | if (cx->cx_type & CXp_SUB_RE_FAKE) | |
1455 | continue; | |
c67159e1 | 1456 | /* FALLTHROUGH */ |
5fbe8311 | 1457 | case CXt_EVAL: |
7766f137 | 1458 | case CXt_FORMAT: |
1c98cc53 | 1459 | DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1460 | return i; |
1461 | } | |
1462 | } | |
1463 | return i; | |
1464 | } | |
1465 | ||
76e3520e | 1466 | STATIC I32 |
cea2e8a9 | 1467 | S_dopoptoeval(pTHX_ I32 startingblock) |
a0d0e21e LW |
1468 | { |
1469 | I32 i; | |
a0d0e21e | 1470 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1471 | const PERL_CONTEXT *cx = &cxstack[i]; |
6b35e009 | 1472 | switch (CxTYPE(cx)) { |
a0d0e21e LW |
1473 | default: |
1474 | continue; | |
1475 | case CXt_EVAL: | |
1c98cc53 | 1476 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1477 | return i; |
1478 | } | |
1479 | } | |
1480 | return i; | |
1481 | } | |
1482 | ||
76e3520e | 1483 | STATIC I32 |
cea2e8a9 | 1484 | S_dopoptoloop(pTHX_ I32 startingblock) |
a0d0e21e LW |
1485 | { |
1486 | I32 i; | |
a0d0e21e | 1487 | for (i = startingblock; i >= 0; i--) { |
eb578fdb | 1488 | const PERL_CONTEXT * const cx = &cxstack[i]; |
6b35e009 | 1489 | switch (CxTYPE(cx)) { |
a0d0e21e | 1490 | case CXt_SUBST: |
a0d0e21e | 1491 | case CXt_SUB: |
7766f137 | 1492 | case CXt_FORMAT: |
a0d0e21e | 1493 | case CXt_EVAL: |
0a753a76 | 1494 | case CXt_NULL: |
dcbac5bb | 1495 | /* diag_listed_as: Exiting subroutine via %s */ |
a2a5de95 NC |
1496 | Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s", |
1497 | context_name[CxTYPE(cx)], OP_NAME(PL_op)); | |
79646418 | 1498 | if ((CxTYPE(cx)) == CXt_NULL) /* sort BLOCK */ |
515afda2 NC |
1499 | return -1; |
1500 | break; | |
93661e56 | 1501 | case CXt_LOOP_PLAIN: |
c6fdafd0 | 1502 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1503 | case CXt_LOOP_LAZYSV: |
93661e56 DM |
1504 | case CXt_LOOP_LIST: |
1505 | case CXt_LOOP_ARY: | |
1c98cc53 | 1506 | DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i)); |
a0d0e21e LW |
1507 | return i; |
1508 | } | |
1509 | } | |
1510 | return i; | |
1511 | } | |
1512 | ||
7896dde7 Z |
1513 | /* find the next GIVEN or FOR (with implicit $_) loop context block */ |
1514 | ||
1515 | STATIC I32 | |
1516 | S_dopoptogivenfor(pTHX_ I32 startingblock) | |
1517 | { | |
1518 | I32 i; | |
1519 | for (i = startingblock; i >= 0; i--) { | |
1520 | const PERL_CONTEXT *cx = &cxstack[i]; | |
1521 | switch (CxTYPE(cx)) { | |
1522 | default: | |
1523 | continue; | |
1524 | case CXt_GIVEN: | |
1525 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found given at cx=%ld)\n", (long)i)); | |
1526 | return i; | |
1527 | case CXt_LOOP_PLAIN: | |
1528 | assert(!(cx->cx_type & CXp_FOR_DEF)); | |
1529 | break; | |
1530 | case CXt_LOOP_LAZYIV: | |
1531 | case CXt_LOOP_LAZYSV: | |
1532 | case CXt_LOOP_LIST: | |
1533 | case CXt_LOOP_ARY: | |
1534 | if (cx->cx_type & CXp_FOR_DEF) { | |
1535 | DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found foreach at cx=%ld)\n", (long)i)); | |
1536 | return i; | |
1537 | } | |
1538 | } | |
1539 | } | |
1540 | return i; | |
1541 | } | |
1542 | ||
0d863452 | 1543 | STATIC I32 |
7896dde7 | 1544 | S_dopoptowhen(pTHX_ I32 startingblock) |
0d863452 RH |
1545 | { |
1546 | I32 i; | |
1547 | for (i = startingblock; i >= 0; i--) { | |
eb578fdb | 1548 | const PERL_CONTEXT *cx = &cxstack[i]; |
0d863452 RH |
1549 | switch (CxTYPE(cx)) { |
1550 | default: | |
1551 | continue; | |
7896dde7 Z |
1552 | case CXt_WHEN: |
1553 | DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i)); | |
0d863452 RH |
1554 | return i; |
1555 | } | |
1556 | } | |
1557 | return i; | |
1558 | } | |
1559 | ||
abaf5d5a | 1560 | /* dounwind(): pop all contexts above (but not including) cxix. |
fc6e609e DM |
1561 | * Note that it clears the savestack frame associated with each popped |
1562 | * context entry, but doesn't free any temps. | |
ed8ff0f3 | 1563 | * It does a cx_popblock() of the last frame that it pops, and leaves |
fc6e609e | 1564 | * cxstack_ix equal to cxix. |
abaf5d5a DM |
1565 | */ |
1566 | ||
a0d0e21e | 1567 | void |
864dbfa3 | 1568 | Perl_dounwind(pTHX_ I32 cxix) |
a0d0e21e | 1569 | { |
f144f1e3 DM |
1570 | if (!PL_curstackinfo) /* can happen if die during thread cloning */ |
1571 | return; | |
1572 | ||
a0d0e21e | 1573 | while (cxstack_ix > cxix) { |
4ebe6e95 | 1574 | PERL_CONTEXT *cx = CX_CUR(); |
5e691bc6 DM |
1575 | |
1576 | CX_DEBUG(cx, "UNWIND"); | |
a0d0e21e | 1577 | /* Note: we don't need to restore the base context info till the end. */ |
76d10131 DM |
1578 | |
1579 | CX_LEAVE_SCOPE(cx); | |
1580 | ||
6b35e009 | 1581 | switch (CxTYPE(cx)) { |
c90c0ff4 | 1582 | case CXt_SUBST: |
b405d38b | 1583 | CX_POPSUBST(cx); |
7332835e DM |
1584 | /* CXt_SUBST is not a block context type, so skip the |
1585 | * cx_popblock(cx) below */ | |
1586 | if (cxstack_ix == cxix + 1) { | |
1587 | cxstack_ix--; | |
1588 | return; | |
1589 | } | |
80d88db0 | 1590 | break; |
a0d0e21e | 1591 | case CXt_SUB: |
a73d8813 | 1592 | cx_popsub(cx); |
a0d0e21e LW |
1593 | break; |
1594 | case CXt_EVAL: | |
13febba5 | 1595 | cx_popeval(cx); |
03e489bc | 1596 | break; |
93661e56 | 1597 | case CXt_LOOP_PLAIN: |
c6fdafd0 | 1598 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1599 | case CXt_LOOP_LAZYSV: |
93661e56 DM |
1600 | case CXt_LOOP_LIST: |
1601 | case CXt_LOOP_ARY: | |
d1b6bf72 | 1602 | cx_poploop(cx); |
a0d0e21e | 1603 | break; |
7896dde7 Z |
1604 | case CXt_WHEN: |
1605 | cx_popwhen(cx); | |
1606 | break; | |
1607 | case CXt_GIVEN: | |
1608 | cx_popgiven(cx); | |
b95eccd3 | 1609 | break; |
f217cb3d | 1610 | case CXt_BLOCK: |
0a753a76 | 1611 | case CXt_NULL: |
f217cb3d | 1612 | /* these two don't have a POPFOO() */ |
a0d0e21e | 1613 | break; |
7766f137 | 1614 | case CXt_FORMAT: |
6a7d52cc | 1615 | cx_popformat(cx); |
7766f137 | 1616 | break; |
a0d0e21e | 1617 | } |
fc6e609e | 1618 | if (cxstack_ix == cxix + 1) { |
ed8ff0f3 | 1619 | cx_popblock(cx); |
fc6e609e | 1620 | } |
c90c0ff4 | 1621 | cxstack_ix--; |
a0d0e21e | 1622 | } |
fc6e609e | 1623 | |
a0d0e21e LW |
1624 | } |
1625 | ||
5a844595 GS |
1626 | void |
1627 | Perl_qerror(pTHX_ SV *err) | |
1628 | { | |
7918f24d NC |
1629 | PERL_ARGS_ASSERT_QERROR; |
1630 | ||
6b2fb389 DM |
1631 | if (PL_in_eval) { |
1632 | if (PL_in_eval & EVAL_KEEPERR) { | |
147e3846 | 1633 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf, |
ecad31f0 | 1634 | SVfARG(err)); |
6b2fb389 DM |
1635 | } |
1636 | else | |
1637 | sv_catsv(ERRSV, err); | |
1638 | } | |
5a844595 GS |
1639 | else if (PL_errors) |
1640 | sv_catsv(PL_errors, err); | |
1641 | else | |
147e3846 | 1642 | Perl_warn(aTHX_ "%" SVf, SVfARG(err)); |
13765c85 DM |
1643 | if (PL_parser) |
1644 | ++PL_parser->error_count; | |
5a844595 GS |
1645 | } |
1646 | ||
d308a779 DM |
1647 | |
1648 | ||
06a7bc17 DM |
1649 | /* pop a CXt_EVAL context and in addition, if it was a require then |
1650 | * based on action: | |
1651 | * 0: do nothing extra; | |
1652 | * 1: undef $INC{$name}; croak "$name did not return a true value"; | |
1653 | * 2: delete $INC{$name}; croak "$errsv: Compilation failed in require" | |
1654 | */ | |
d308a779 | 1655 | |
8a1fd305 | 1656 | static void |
06a7bc17 | 1657 | S_pop_eval_context_maybe_croak(pTHX_ PERL_CONTEXT *cx, SV *errsv, int action) |
d308a779 | 1658 | { |
51787acf | 1659 | SV *namesv = NULL; /* init to avoid dumb compiler warning */ |
06a7bc17 DM |
1660 | bool do_croak; |
1661 | ||
1662 | CX_LEAVE_SCOPE(cx); | |
1663 | do_croak = action && (CxOLD_OP_TYPE(cx) == OP_REQUIRE); | |
2a1e0dfe DM |
1664 | if (do_croak) { |
1665 | /* keep namesv alive after cx_popeval() */ | |
1666 | namesv = cx->blk_eval.old_namesv; | |
1667 | cx->blk_eval.old_namesv = NULL; | |
1668 | sv_2mortal(namesv); | |
1669 | } | |
06a7bc17 DM |
1670 | cx_popeval(cx); |
1671 | cx_popblock(cx); | |
1672 | CX_POP(cx); | |
1673 | ||
1674 | if (do_croak) { | |
1675 | const char *fmt; | |
1676 | HV *inc_hv = GvHVn(PL_incgv); | |
1677 | I32 klen = SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv); | |
1678 | const char *key = SvPVX_const(namesv); | |
d308a779 | 1679 | |
06a7bc17 DM |
1680 | if (action == 1) { |
1681 | (void)hv_delete(inc_hv, key, klen, G_DISCARD); | |
147e3846 | 1682 | fmt = "%" SVf " did not return a true value"; |
06a7bc17 DM |
1683 | errsv = namesv; |
1684 | } | |
1685 | else { | |
1686 | (void)hv_store(inc_hv, key, klen, &PL_sv_undef, 0); | |
147e3846 | 1687 | fmt = "%" SVf "Compilation failed in require"; |
06a7bc17 DM |
1688 | if (!errsv) |
1689 | errsv = newSVpvs_flags("Unknown error\n", SVs_TEMP); | |
1690 | } | |
1691 | ||
1692 | Perl_croak(aTHX_ fmt, SVfARG(errsv)); | |
1693 | } | |
d308a779 DM |
1694 | } |
1695 | ||
1696 | ||
8c86f023 DM |
1697 | /* die_unwind(): this is the final destination for the various croak() |
1698 | * functions. If we're in an eval, unwind the context and other stacks | |
1699 | * back to the top-most CXt_EVAL and set $@ to msv; otherwise print msv | |
1700 | * to STDERR and initiate an exit. Note that if the CXt_EVAL popped back | |
1701 | * to is a require the exception will be rethrown, as requires don't | |
1702 | * actually trap exceptions. | |
1703 | */ | |
06a7bc17 | 1704 | |
bb4c52e0 | 1705 | void |
c5df3096 | 1706 | Perl_die_unwind(pTHX_ SV *msv) |
a0d0e21e | 1707 | { |
8c86f023 | 1708 | SV *exceptsv = msv; |
96d9b9cd | 1709 | U8 in_eval = PL_in_eval; |
c5df3096 | 1710 | PERL_ARGS_ASSERT_DIE_UNWIND; |
87582a92 | 1711 | |
96d9b9cd | 1712 | if (in_eval) { |
a0d0e21e | 1713 | I32 cxix; |
a0d0e21e | 1714 | |
b66d79a6 DM |
1715 | /* We need to keep this SV alive through all the stack unwinding |
1716 | * and FREETMPSing below, while ensuing that it doesn't leak | |
1717 | * if we call out to something which then dies (e.g. sub STORE{die} | |
1718 | * when unlocalising a tied var). So we do a dance with | |
1719 | * mortalising and SAVEFREEing. | |
1720 | */ | |
1721 | sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv)); | |
8c86f023 | 1722 | |
22a30693 Z |
1723 | /* |
1724 | * Historically, perl used to set ERRSV ($@) early in the die | |
1725 | * process and rely on it not getting clobbered during unwinding. | |
1726 | * That sucked, because it was liable to get clobbered, so the | |
1727 | * setting of ERRSV used to emit the exception from eval{} has | |
1728 | * been moved to much later, after unwinding (see just before | |
1729 | * JMPENV_JUMP below). However, some modules were relying on the | |
1730 | * early setting, by examining $@ during unwinding to use it as | |
1731 | * a flag indicating whether the current unwinding was caused by | |
1732 | * an exception. It was never a reliable flag for that purpose, | |
1733 | * being totally open to false positives even without actual | |
1734 | * clobberage, but was useful enough for production code to | |
1735 | * semantically rely on it. | |
1736 | * | |
1737 | * We'd like to have a proper introspective interface that | |
1738 | * explicitly describes the reason for whatever unwinding | |
1739 | * operations are currently in progress, so that those modules | |
1740 | * work reliably and $@ isn't further overloaded. But we don't | |
1741 | * have one yet. In its absence, as a stopgap measure, ERRSV is | |
1742 | * now *additionally* set here, before unwinding, to serve as the | |
1743 | * (unreliable) flag that it used to. | |
1744 | * | |
1745 | * This behaviour is temporary, and should be removed when a | |
1746 | * proper way to detect exceptional unwinding has been developed. | |
1747 | * As of 2010-12, the authors of modules relying on the hack | |
1748 | * are aware of the issue, because the modules failed on | |
1749 | * perls 5.13.{1..7} which had late setting of $@ without this | |
1750 | * early-setting hack. | |
1751 | */ | |
933e3e63 TC |
1752 | if (!(in_eval & EVAL_KEEPERR)) { |
1753 | /* remove any read-only/magic from the SV, so we don't | |
1754 | get infinite recursion when setting ERRSV */ | |
1755 | SANE_ERRSV(); | |
8c86f023 DM |
1756 | sv_setsv_flags(ERRSV, exceptsv, |
1757 | (SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL)); | |
933e3e63 | 1758 | } |
22a30693 | 1759 | |
fc941f37 | 1760 | if (in_eval & EVAL_KEEPERR) { |
147e3846 | 1761 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf, |
fc941f37 Z |
1762 | SVfARG(exceptsv)); |
1763 | } | |
1764 | ||
5a844595 GS |
1765 | while ((cxix = dopoptoeval(cxstack_ix)) < 0 |
1766 | && PL_curstackinfo->si_prev) | |
1767 | { | |
bac4b2ad | 1768 | dounwind(-1); |
d3acc0f7 | 1769 | POPSTACK; |
bac4b2ad | 1770 | } |
e336de0d | 1771 | |
a0d0e21e | 1772 | if (cxix >= 0) { |
eb578fdb | 1773 | PERL_CONTEXT *cx; |
f5ddd604 | 1774 | SV **oldsp; |
1c23e2bd | 1775 | U8 gimme; |
8f89e5a9 Z |
1776 | JMPENV *restartjmpenv; |
1777 | OP *restartop; | |
a0d0e21e LW |
1778 | |
1779 | if (cxix < cxstack_ix) | |
1780 | dounwind(cxix); | |
1781 | ||
4ebe6e95 | 1782 | cx = CX_CUR(); |
f7d0774b | 1783 | assert(CxTYPE(cx) == CXt_EVAL); |
e17c1e7c DM |
1784 | |
1785 | /* return false to the caller of eval */ | |
f5ddd604 | 1786 | oldsp = PL_stack_base + cx->blk_oldsp; |
f7d0774b | 1787 | gimme = cx->blk_gimme; |
f7d0774b | 1788 | if (gimme == G_SCALAR) |
f5ddd604 DM |
1789 | *++oldsp = &PL_sv_undef; |
1790 | PL_stack_sp = oldsp; | |
f7d0774b | 1791 | |
7d140242 DM |
1792 | restartjmpenv = cx->blk_eval.cur_top_env; |
1793 | restartop = cx->blk_eval.retop; | |
b66d79a6 DM |
1794 | |
1795 | /* We need a FREETMPS here to avoid late-called destructors | |
1796 | * clobbering $@ *after* we set it below, e.g. | |
1797 | * sub DESTROY { eval { die "X" } } | |
1798 | * eval { my $x = bless []; die $x = 0, "Y" }; | |
1799 | * is($@, "Y") | |
1800 | * Here the clearing of the $x ref mortalises the anon array, | |
1801 | * which needs to be freed *before* $& is set to "Y", | |
1802 | * otherwise it gets overwritten with "X". | |
1803 | * | |
1804 | * However, the FREETMPS will clobber exceptsv, so preserve it | |
1805 | * on the savestack for now. | |
1806 | */ | |
1807 | SAVEFREESV(SvREFCNT_inc_simple_NN(exceptsv)); | |
1808 | FREETMPS; | |
1809 | /* now we're about to pop the savestack, so re-mortalise it */ | |
1810 | sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv)); | |
1811 | ||
06a7bc17 DM |
1812 | /* Note that unlike pp_entereval, pp_require isn't supposed to |
1813 | * trap errors. So if we're a require, after we pop the | |
1814 | * CXt_EVAL that pp_require pushed, rethrow the error with | |
1815 | * croak(exceptsv). This is all handled by the call below when | |
1816 | * action == 2. | |
1817 | */ | |
1818 | S_pop_eval_context_maybe_croak(aTHX_ cx, exceptsv, 2); | |
a0d0e21e | 1819 | |
933e3e63 TC |
1820 | if (!(in_eval & EVAL_KEEPERR)) { |
1821 | SANE_ERRSV(); | |
96d9b9cd | 1822 | sv_setsv(ERRSV, exceptsv); |
933e3e63 | 1823 | } |
8f89e5a9 Z |
1824 | PL_restartjmpenv = restartjmpenv; |
1825 | PL_restartop = restartop; | |
bb4c52e0 | 1826 | JMPENV_JUMP(3); |
e5964223 | 1827 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e LW |
1828 | } |
1829 | } | |
87582a92 | 1830 | |
96d9b9cd | 1831 | write_to_stderr(exceptsv); |
f86702cc | 1832 | my_failure_exit(); |
e5964223 | 1833 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e LW |
1834 | } |
1835 | ||
1836 | PP(pp_xor) | |
1837 | { | |
20b7effb | 1838 | dSP; dPOPTOPssrl; |
f4c975aa | 1839 | if (SvTRUE_NN(left) != SvTRUE_NN(right)) |
a0d0e21e LW |
1840 | RETSETYES; |
1841 | else | |
1842 | RETSETNO; | |
1843 | } | |
1844 | ||
8dff4fc5 | 1845 | /* |
dcccc8ff KW |
1846 | |
1847 | =head1 CV Manipulation Functions | |
1848 | ||
8dff4fc5 BM |
1849 | =for apidoc caller_cx |
1850 | ||
72d33970 | 1851 | The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The |
8dff4fc5 | 1852 | returned C<PERL_CONTEXT> structure can be interrogated to find all the |
72d33970 | 1853 | information returned to Perl by C<caller>. Note that XSUBs don't get a |
8dff4fc5 BM |
1854 | stack frame, so C<caller_cx(0, NULL)> will return information for the |
1855 | immediately-surrounding Perl code. | |
1856 | ||
1857 | This function skips over the automatic calls to C<&DB::sub> made on the | |
72d33970 | 1858 | behalf of the debugger. If the stack frame requested was a sub called by |
8dff4fc5 BM |
1859 | C<DB::sub>, the return value will be the frame for the call to |
1860 | C<DB::sub>, since that has the correct line number/etc. for the call | |
72d33970 | 1861 | site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the |
8dff4fc5 BM |
1862 | frame for the sub call itself. |
1863 | ||
1864 | =cut | |
1865 | */ | |
1866 | ||
1867 | const PERL_CONTEXT * | |
1868 | Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp) | |
a0d0e21e | 1869 | { |
5b6f7443 | 1870 | I32 cxix = dopopto_cursub(); |
eb578fdb KW |
1871 | const PERL_CONTEXT *cx; |
1872 | const PERL_CONTEXT *ccstack = cxstack; | |
901017d6 | 1873 | const PERL_SI *top_si = PL_curstackinfo; |
27d41816 | 1874 | |
a0d0e21e | 1875 | for (;;) { |
2c375eb9 GS |
1876 | /* we may be in a higher stacklevel, so dig down deeper */ |
1877 | while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { | |
1878 | top_si = top_si->si_prev; | |
1879 | ccstack = top_si->si_cxstack; | |
1880 | cxix = dopoptosub_at(ccstack, top_si->si_cxix); | |
1881 | } | |
8dff4fc5 BM |
1882 | if (cxix < 0) |
1883 | return NULL; | |
f2a7f298 DG |
1884 | /* caller() should not report the automatic calls to &DB::sub */ |
1885 | if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 && | |
3280af22 | 1886 | ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) |
a0d0e21e LW |
1887 | count++; |
1888 | if (!count--) | |
1889 | break; | |
2c375eb9 | 1890 | cxix = dopoptosub_at(ccstack, cxix - 1); |
a0d0e21e | 1891 | } |
2c375eb9 GS |
1892 | |
1893 | cx = &ccstack[cxix]; | |
8dff4fc5 BM |
1894 | if (dbcxp) *dbcxp = cx; |
1895 | ||
7766f137 | 1896 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
f54cb97a | 1897 | const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1); |
2c375eb9 | 1898 | /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the |
06a5b730 | 1899 | field below is defined for any cx. */ |
f2a7f298 DG |
1900 | /* caller() should not report the automatic calls to &DB::sub */ |
1901 | if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) | |
2c375eb9 | 1902 | cx = &ccstack[dbcxix]; |
06a5b730 | 1903 | } |
1904 | ||
8dff4fc5 BM |
1905 | return cx; |
1906 | } | |
1907 | ||
1908 | PP(pp_caller) | |
1909 | { | |
8dff4fc5 | 1910 | dSP; |
eb578fdb | 1911 | const PERL_CONTEXT *cx; |
8dff4fc5 | 1912 | const PERL_CONTEXT *dbcx; |
1c23e2bd | 1913 | U8 gimme = GIMME_V; |
d527ce7c | 1914 | const HEK *stash_hek; |
8dff4fc5 | 1915 | I32 count = 0; |
ce0b554b | 1916 | bool has_arg = MAXARG && TOPs; |
25502127 | 1917 | const COP *lcop; |
8dff4fc5 | 1918 | |
ce0b554b FC |
1919 | if (MAXARG) { |
1920 | if (has_arg) | |
8dff4fc5 | 1921 | count = POPi; |
ce0b554b FC |
1922 | else (void)POPs; |
1923 | } | |
8dff4fc5 | 1924 | |
ce0b554b | 1925 | cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx); |
8dff4fc5 | 1926 | if (!cx) { |
48ebc325 | 1927 | if (gimme != G_ARRAY) { |
8dff4fc5 BM |
1928 | EXTEND(SP, 1); |
1929 | RETPUSHUNDEF; | |
1930 | } | |
1931 | RETURN; | |
1932 | } | |
1933 | ||
5e691bc6 | 1934 | CX_DEBUG(cx, "CALLER"); |
d0279c7c | 1935 | assert(CopSTASH(cx->blk_oldcop)); |
e7886211 FC |
1936 | stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV |
1937 | ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop)) | |
1938 | : NULL; | |
48ebc325 | 1939 | if (gimme != G_ARRAY) { |
27d41816 | 1940 | EXTEND(SP, 1); |
d527ce7c | 1941 | if (!stash_hek) |
3280af22 | 1942 | PUSHs(&PL_sv_undef); |
49d8d3a1 MB |
1943 | else { |
1944 | dTARGET; | |
d527ce7c | 1945 | sv_sethek(TARG, stash_hek); |
49d8d3a1 MB |
1946 | PUSHs(TARG); |
1947 | } | |
a0d0e21e LW |
1948 | RETURN; |
1949 | } | |
a0d0e21e | 1950 | |
b3ca2e83 | 1951 | EXTEND(SP, 11); |
27d41816 | 1952 | |
d527ce7c | 1953 | if (!stash_hek) |
3280af22 | 1954 | PUSHs(&PL_sv_undef); |
d527ce7c BF |
1955 | else { |
1956 | dTARGET; | |
1957 | sv_sethek(TARG, stash_hek); | |
1958 | PUSHTARG; | |
1959 | } | |
6e449a3a | 1960 | mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0)); |
e6dae479 | 1961 | lcop = closest_cop(cx->blk_oldcop, OpSIBLING(cx->blk_oldcop), |
25502127 FC |
1962 | cx->blk_sub.retop, TRUE); |
1963 | if (!lcop) | |
1964 | lcop = cx->blk_oldcop; | |
e9e9e546 | 1965 | mPUSHu(CopLINE(lcop)); |
ce0b554b | 1966 | if (!has_arg) |
a0d0e21e | 1967 | RETURN; |
7766f137 GS |
1968 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
1969 | /* So is ccstack[dbcxix]. */ | |
a5f47741 | 1970 | if (CvHASGV(dbcx->blk_sub.cv)) { |
ecf05a58 | 1971 | PUSHs(cv_name(dbcx->blk_sub.cv, 0, 0)); |
bf38a478 | 1972 | PUSHs(boolSV(CxHASARGS(cx))); |
07b8c804 RGS |
1973 | } |
1974 | else { | |
84bafc02 | 1975 | PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP)); |
bf38a478 | 1976 | PUSHs(boolSV(CxHASARGS(cx))); |
07b8c804 | 1977 | } |
a0d0e21e LW |
1978 | } |
1979 | else { | |
84bafc02 | 1980 | PUSHs(newSVpvs_flags("(eval)", SVs_TEMP)); |
725c44f9 | 1981 | PUSHs(&PL_sv_zero); |
a0d0e21e | 1982 | } |
1c23e2bd | 1983 | gimme = cx->blk_gimme; |
54310121 | 1984 | if (gimme == G_VOID) |
3280af22 | 1985 | PUSHs(&PL_sv_undef); |
54310121 | 1986 | else |
98625aca | 1987 | PUSHs(boolSV((gimme & G_WANT) == G_ARRAY)); |
6b35e009 | 1988 | if (CxTYPE(cx) == CXt_EVAL) { |
811a4de9 | 1989 | /* eval STRING */ |
85a64632 | 1990 | if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { |
78beb4ca TC |
1991 | SV *cur_text = cx->blk_eval.cur_text; |
1992 | if (SvCUR(cur_text) >= 2) { | |
1993 | PUSHs(newSVpvn_flags(SvPVX(cur_text), SvCUR(cur_text)-2, | |
1994 | SvUTF8(cur_text)|SVs_TEMP)); | |
1995 | } | |
1996 | else { | |
1997 | /* I think this is will always be "", but be sure */ | |
1998 | PUSHs(sv_2mortal(newSVsv(cur_text))); | |
1999 | } | |
2000 | ||
3280af22 | 2001 | PUSHs(&PL_sv_no); |
0f79a09d | 2002 | } |
811a4de9 | 2003 | /* require */ |
0f79a09d | 2004 | else if (cx->blk_eval.old_namesv) { |
6e449a3a | 2005 | mPUSHs(newSVsv(cx->blk_eval.old_namesv)); |
3280af22 | 2006 | PUSHs(&PL_sv_yes); |
06a5b730 | 2007 | } |
811a4de9 GS |
2008 | /* eval BLOCK (try blocks have old_namesv == 0) */ |
2009 | else { | |
2010 | PUSHs(&PL_sv_undef); | |
2011 | PUSHs(&PL_sv_undef); | |
2012 | } | |
4633a7c4 | 2013 | } |
a682de96 GS |
2014 | else { |
2015 | PUSHs(&PL_sv_undef); | |
2016 | PUSHs(&PL_sv_undef); | |
2017 | } | |
bafb2adc | 2018 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx) |
ed094faf | 2019 | && CopSTASH_eq(PL_curcop, PL_debstash)) |
4633a7c4 | 2020 | { |
9513529b DM |
2021 | /* slot 0 of the pad contains the original @_ */ |
2022 | AV * const ary = MUTABLE_AV(AvARRAY(MUTABLE_AV( | |
2023 | PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[ | |
2024 | cx->blk_sub.olddepth+1]))[0]); | |
c70927a6 | 2025 | const SSize_t off = AvARRAY(ary) - AvALLOC(ary); |
a0d0e21e | 2026 | |
e1a80902 | 2027 | Perl_init_dbargs(aTHX); |
a0d0e21e | 2028 | |
3280af22 NIS |
2029 | if (AvMAX(PL_dbargs) < AvFILLp(ary) + off) |
2030 | av_extend(PL_dbargs, AvFILLp(ary) + off); | |
f14cf363 TC |
2031 | if (AvFILLp(ary) + 1 + off) |
2032 | Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*); | |
3280af22 | 2033 | AvFILLp(PL_dbargs) = AvFILLp(ary) + off; |
a0d0e21e | 2034 | } |
6e449a3a | 2035 | mPUSHi(CopHINTS_get(cx->blk_oldcop)); |
e476b1b5 GS |
2036 | { |
2037 | SV * mask ; | |
72dc9ed5 | 2038 | STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ; |
114bafba | 2039 | |
f07626ad | 2040 | if (old_warnings == pWARN_NONE) |
e476b1b5 | 2041 | mask = newSVpvn(WARN_NONEstring, WARNsize) ; |
f07626ad FC |
2042 | else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0) |
2043 | mask = &PL_sv_undef ; | |
ac27b0f5 | 2044 | else if (old_warnings == pWARN_ALL || |
75b6c4ca | 2045 | (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) { |
006c1a1d | 2046 | mask = newSVpvn(WARN_ALLstring, WARNsize) ; |
75b6c4ca | 2047 | } |
e476b1b5 | 2048 | else |
72dc9ed5 | 2049 | mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]); |
6e449a3a | 2050 | mPUSHs(mask); |
e476b1b5 | 2051 | } |
b3ca2e83 | 2052 | |
c28fe1ec | 2053 | PUSHs(cx->blk_oldcop->cop_hints_hash ? |
20439bc7 | 2054 | sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0)))) |
b3ca2e83 | 2055 | : &PL_sv_undef); |
a0d0e21e LW |
2056 | RETURN; |
2057 | } | |
2058 | ||
a0d0e21e LW |
2059 | PP(pp_reset) |
2060 | { | |
39644a26 | 2061 | dSP; |
ca826051 FC |
2062 | const char * tmps; |
2063 | STRLEN len = 0; | |
2372d073 DM |
2064 | if (MAXARG < 1 || (!TOPs && !POPs)) { |
2065 | EXTEND(SP, 1); | |
ca826051 | 2066 | tmps = NULL, len = 0; |
2372d073 | 2067 | } |
ca826051 FC |
2068 | else |
2069 | tmps = SvPVx_const(POPs, len); | |
2070 | sv_resetpvn(tmps, len, CopSTASH(PL_curcop)); | |
3280af22 | 2071 | PUSHs(&PL_sv_yes); |
a0d0e21e LW |
2072 | RETURN; |
2073 | } | |
2074 | ||
dd2155a4 DM |
2075 | /* like pp_nextstate, but used instead when the debugger is active */ |
2076 | ||
a0d0e21e LW |
2077 | PP(pp_dbstate) |
2078 | { | |
533c011a | 2079 | PL_curcop = (COP*)PL_op; |
a0d0e21e | 2080 | TAINT_NOT; /* Each statement is presumed innocent */ |
4ebe6e95 | 2081 | PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp; |
a0d0e21e LW |
2082 | FREETMPS; |
2083 | ||
f410a211 NC |
2084 | PERL_ASYNC_CHECK(); |
2085 | ||
88df5f01 | 2086 | if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */ |
a6d69523 | 2087 | || PL_DBsingle_iv || PL_DBsignal_iv || PL_DBtrace_iv) |
a0d0e21e | 2088 | { |
39644a26 | 2089 | dSP; |
eb578fdb | 2090 | PERL_CONTEXT *cx; |
1c23e2bd | 2091 | const U8 gimme = G_ARRAY; |
0bd48802 | 2092 | GV * const gv = PL_DBgv; |
432d4561 JL |
2093 | CV * cv = NULL; |
2094 | ||
2095 | if (gv && isGV_with_GP(gv)) | |
2096 | cv = GvCV(gv); | |
a0d0e21e | 2097 | |
c2cb6f77 | 2098 | if (!cv || (!CvROOT(cv) && !CvXSUB(cv))) |
cea2e8a9 | 2099 | DIE(aTHX_ "No DB::DB routine defined"); |
a0d0e21e | 2100 | |
aea4f609 DM |
2101 | if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG)) |
2102 | /* don't do recursive DB::DB call */ | |
a0d0e21e | 2103 | return NORMAL; |
748a9306 | 2104 | |
aed2304a | 2105 | if (CvISXSUB(cv)) { |
8ae997c5 DM |
2106 | ENTER; |
2107 | SAVEI32(PL_debug); | |
2108 | PL_debug = 0; | |
2109 | SAVESTACK_POS(); | |
8a44b450 | 2110 | SAVETMPS; |
c127bd3a SF |
2111 | PUSHMARK(SP); |
2112 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
c127bd3a | 2113 | FREETMPS; |
a57c6685 | 2114 | LEAVE; |
c127bd3a SF |
2115 | return NORMAL; |
2116 | } | |
2117 | else { | |
ed8ff0f3 | 2118 | cx = cx_pushblock(CXt_SUB, gimme, SP, PL_savestack_ix); |
a73d8813 DM |
2119 | cx_pushsub(cx, cv, PL_op->op_next, 0); |
2120 | /* OP_DBSTATE's op_private holds hint bits rather than | |
2121 | * the lvalue-ish flags seen in OP_ENTERSUB. So cancel | |
2122 | * any CxLVAL() flags that have now been mis-calculated */ | |
2123 | cx->blk_u16 = 0; | |
8ae997c5 DM |
2124 | |
2125 | SAVEI32(PL_debug); | |
2126 | PL_debug = 0; | |
2127 | SAVESTACK_POS(); | |
c127bd3a | 2128 | CvDEPTH(cv)++; |
d2af2719 | 2129 | if (CvDEPTH(cv) >= 2) |
9d976ff5 | 2130 | pad_push(CvPADLIST(cv), CvDEPTH(cv)); |
9d976ff5 | 2131 | PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv)); |
c127bd3a SF |
2132 | RETURNOP(CvSTART(cv)); |
2133 | } | |
a0d0e21e LW |
2134 | } |
2135 | else | |
2136 | return NORMAL; | |
2137 | } | |
2138 | ||
0663a8f8 | 2139 | |
2b9a6457 VP |
2140 | PP(pp_enter) |
2141 | { | |
1c23e2bd | 2142 | U8 gimme = GIMME_V; |
2b9a6457 | 2143 | |
d4ce7588 DM |
2144 | (void)cx_pushblock(CXt_BLOCK, gimme, PL_stack_sp, PL_savestack_ix); |
2145 | return NORMAL; | |
2b9a6457 VP |
2146 | } |
2147 | ||
d4ce7588 | 2148 | |
2b9a6457 VP |
2149 | PP(pp_leave) |
2150 | { | |
eb578fdb | 2151 | PERL_CONTEXT *cx; |
f5ddd604 | 2152 | SV **oldsp; |
1c23e2bd | 2153 | U8 gimme; |
2b9a6457 | 2154 | |
4ebe6e95 | 2155 | cx = CX_CUR(); |
61d3b95a | 2156 | assert(CxTYPE(cx) == CXt_BLOCK); |
4df352a8 DM |
2157 | |
2158 | if (PL_op->op_flags & OPf_SPECIAL) | |
c349b9a0 DM |
2159 | /* fake block should preserve $1 et al; e.g. /(...)/ while ...; */ |
2160 | cx->blk_oldpm = PL_curpm; | |
4df352a8 | 2161 | |
f5ddd604 | 2162 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a | 2163 | gimme = cx->blk_gimme; |
2b9a6457 | 2164 | |
0663a8f8 | 2165 | if (gimme == G_VOID) |
f5ddd604 | 2166 | PL_stack_sp = oldsp; |
0663a8f8 | 2167 | else |
f5ddd604 | 2168 | leave_adjust_stacks(oldsp, oldsp, gimme, |
75bc488d | 2169 | PL_op->op_private & OPpLVALUE ? 3 : 1); |
67f63db7 | 2170 | |
2f450c1b | 2171 | CX_LEAVE_SCOPE(cx); |
ed8ff0f3 | 2172 | cx_popblock(cx); |
5da525e9 | 2173 | CX_POP(cx); |
2b9a6457 | 2174 | |
0663a8f8 | 2175 | return NORMAL; |
2b9a6457 VP |
2176 | } |
2177 | ||
eaa9f768 JH |
2178 | static bool |
2179 | S_outside_integer(pTHX_ SV *sv) | |
2180 | { | |
2181 | if (SvOK(sv)) { | |
2182 | const NV nv = SvNV_nomg(sv); | |
415b66b2 JH |
2183 | if (Perl_isinfnan(nv)) |
2184 | return TRUE; | |
eaa9f768 JH |
2185 | #ifdef NV_PRESERVES_UV |
2186 | if (nv < (NV)IV_MIN || nv > (NV)IV_MAX) | |
2187 | return TRUE; | |
2188 | #else | |
2189 | if (nv <= (NV)IV_MIN) | |
2190 | return TRUE; | |
2191 | if ((nv > 0) && | |
2192 | ((nv > (NV)UV_MAX || | |
2193 | SvUV_nomg(sv) > (UV)IV_MAX))) | |
2194 | return TRUE; | |
2195 | #endif | |
2196 | } | |
2197 | return FALSE; | |
2198 | } | |
2199 | ||
a0d0e21e LW |
2200 | PP(pp_enteriter) |
2201 | { | |
20b7effb | 2202 | dSP; dMARK; |
eb578fdb | 2203 | PERL_CONTEXT *cx; |
1c23e2bd | 2204 | const U8 gimme = GIMME_V; |
4ad63d70 | 2205 | void *itervarp; /* GV or pad slot of the iteration variable */ |
f0bb9bf7 | 2206 | SV *itersave; /* the old var in the iterator var slot */ |
93661e56 | 2207 | U8 cxflags = 0; |
a0d0e21e | 2208 | |
aafca525 | 2209 | if (PL_op->op_targ) { /* "my" variable */ |
4ad63d70 DM |
2210 | itervarp = &PAD_SVl(PL_op->op_targ); |
2211 | itersave = *(SV**)itervarp; | |
2212 | assert(itersave); | |
aafca525 | 2213 | if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */ |
fdb8b82b DM |
2214 | /* the SV currently in the pad slot is never live during |
2215 | * iteration (the slot is always aliased to one of the items) | |
2216 | * so it's always stale */ | |
4ad63d70 | 2217 | SvPADSTALE_on(itersave); |
14f338dc | 2218 | } |
4ad63d70 | 2219 | SvREFCNT_inc_simple_void_NN(itersave); |
93661e56 | 2220 | cxflags = CXp_FOR_PAD; |
54b9620d | 2221 | } |
d39c26a6 FC |
2222 | else { |
2223 | SV * const sv = POPs; | |
4ad63d70 | 2224 | itervarp = (void *)sv; |
d4e2b4d6 | 2225 | if (LIKELY(isGV(sv))) { /* symbol table variable */ |
6d3ca00e DM |
2226 | itersave = GvSV(sv); |
2227 | SvREFCNT_inc_simple_void(itersave); | |
93661e56 | 2228 | cxflags = CXp_FOR_GV; |
7896dde7 Z |
2229 | if (PL_op->op_private & OPpITER_DEF) |
2230 | cxflags |= CXp_FOR_DEF; | |
d4e2b4d6 DM |
2231 | } |
2232 | else { /* LV ref: for \$foo (...) */ | |
2233 | assert(SvTYPE(sv) == SVt_PVMG); | |
2234 | assert(SvMAGIC(sv)); | |
2235 | assert(SvMAGIC(sv)->mg_type == PERL_MAGIC_lvref); | |
2236 | itersave = NULL; | |
93661e56 | 2237 | cxflags = CXp_FOR_LVREF; |
d4e2b4d6 | 2238 | } |
d39c26a6 | 2239 | } |
7896dde7 Z |
2240 | /* OPpITER_DEF (implicit $_) should only occur with a GV iter var */ |
2241 | assert((cxflags & CXp_FOR_GV) || !(PL_op->op_private & OPpITER_DEF)); | |
0d863452 | 2242 | |
75fbe096 DM |
2243 | /* Note that this context is initially set as CXt_NULL. Further on |
2244 | * down it's changed to one of the CXt_LOOP_*. Before it's changed, | |
2245 | * there mustn't be anything in the blk_loop substruct that requires | |
2246 | * freeing or undoing, in case we die in the meantime. And vice-versa. | |
2247 | */ | |
ed8ff0f3 | 2248 | cx = cx_pushblock(cxflags, gimme, MARK, PL_savestack_ix); |
d1b6bf72 | 2249 | cx_pushloop_for(cx, itervarp, itersave); |
2c49879e | 2250 | |
533c011a | 2251 | if (PL_op->op_flags & OPf_STACKED) { |
2c49879e DM |
2252 | /* OPf_STACKED implies either a single array: for(@), with a |
2253 | * single AV on the stack, or a range: for (1..5), with 1 and 5 on | |
2254 | * the stack */ | |
d01136d6 BS |
2255 | SV *maybe_ary = POPs; |
2256 | if (SvTYPE(maybe_ary) != SVt_PVAV) { | |
2c49879e | 2257 | /* range */ |
89ea2908 | 2258 | dPOPss; |
d01136d6 | 2259 | SV * const right = maybe_ary; |
93661e56 | 2260 | if (UNLIKELY(cxflags & CXp_FOR_LVREF)) |
d39c26a6 | 2261 | DIE(aTHX_ "Assigned value is not a reference"); |
984a4bea RD |
2262 | SvGETMAGIC(sv); |
2263 | SvGETMAGIC(right); | |
4fe3f0fa | 2264 | if (RANGE_IS_NUMERIC(sv,right)) { |
c6fdafd0 | 2265 | cx->cx_type |= CXt_LOOP_LAZYIV; |
eaa9f768 JH |
2266 | if (S_outside_integer(aTHX_ sv) || |
2267 | S_outside_integer(aTHX_ right)) | |
076d9a11 | 2268 | DIE(aTHX_ "Range iterator outside integer range"); |
f52e41ad FC |
2269 | cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv); |
2270 | cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right); | |
89ea2908 | 2271 | } |
3f63a782 | 2272 | else { |
d01136d6 | 2273 | cx->cx_type |= CXt_LOOP_LAZYSV; |
d01136d6 BS |
2274 | cx->blk_loop.state_u.lazysv.cur = newSVsv(sv); |
2275 | cx->blk_loop.state_u.lazysv.end = right; | |
2c49879e | 2276 | SvREFCNT_inc_simple_void_NN(right); |
d01136d6 | 2277 | (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur); |
267cc4a8 NC |
2278 | /* This will do the upgrade to SVt_PV, and warn if the value |
2279 | is uninitialised. */ | |
10516c54 | 2280 | (void) SvPV_nolen_const(right); |
267cc4a8 NC |
2281 | /* Doing this avoids a check every time in pp_iter in pp_hot.c |
2282 | to replace !SvOK() with a pointer to "". */ | |
2283 | if (!SvOK(right)) { | |
2284 | SvREFCNT_dec(right); | |
d01136d6 | 2285 | cx->blk_loop.state_u.lazysv.end = &PL_sv_no; |
267cc4a8 | 2286 | } |
3f63a782 | 2287 | } |
89ea2908 | 2288 | } |
d01136d6 | 2289 | else /* SvTYPE(maybe_ary) == SVt_PVAV */ { |
2c49879e | 2290 | /* for (@array) {} */ |
93661e56 | 2291 | cx->cx_type |= CXt_LOOP_ARY; |
502c6561 | 2292 | cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary); |
2c49879e | 2293 | SvREFCNT_inc_simple_void_NN(maybe_ary); |
d01136d6 BS |
2294 | cx->blk_loop.state_u.ary.ix = |
2295 | (PL_op->op_private & OPpITER_REVERSED) ? | |
2296 | AvFILL(cx->blk_loop.state_u.ary.ary) + 1 : | |
2297 | -1; | |
ef3e5ea9 | 2298 | } |
8a1f10dd | 2299 | /* EXTEND(SP, 1) not needed in this branch because we just did POPs */ |
89ea2908 | 2300 | } |
d01136d6 | 2301 | else { /* iterating over items on the stack */ |
93661e56 | 2302 | cx->cx_type |= CXt_LOOP_LIST; |
1bef65a2 | 2303 | cx->blk_oldsp = SP - PL_stack_base; |
93661e56 DM |
2304 | cx->blk_loop.state_u.stack.basesp = MARK - PL_stack_base; |
2305 | cx->blk_loop.state_u.stack.ix = | |
2306 | (PL_op->op_private & OPpITER_REVERSED) | |
2307 | ? cx->blk_oldsp + 1 | |
2308 | : cx->blk_loop.state_u.stack.basesp; | |
8a1f10dd DM |
2309 | /* pre-extend stack so pp_iter doesn't have to check every time |
2310 | * it pushes yes/no */ | |
2311 | EXTEND(SP, 1); | |
4633a7c4 | 2312 | } |
a0d0e21e LW |
2313 | |
2314 | RETURN; | |
2315 | } | |
2316 | ||
2317 | PP(pp_enterloop) | |
2318 | { | |
eb578fdb | 2319 | PERL_CONTEXT *cx; |
1c23e2bd | 2320 | const U8 gimme = GIMME_V; |
a0d0e21e | 2321 | |
d4ce7588 | 2322 | cx = cx_pushblock(CXt_LOOP_PLAIN, gimme, PL_stack_sp, PL_savestack_ix); |
d1b6bf72 | 2323 | cx_pushloop_plain(cx); |
d4ce7588 | 2324 | return NORMAL; |
a0d0e21e LW |
2325 | } |
2326 | ||
d4ce7588 | 2327 | |
a0d0e21e LW |
2328 | PP(pp_leaveloop) |
2329 | { | |
eb578fdb | 2330 | PERL_CONTEXT *cx; |
1c23e2bd | 2331 | U8 gimme; |
032736ab | 2332 | SV **base; |
f5ddd604 | 2333 | SV **oldsp; |
a0d0e21e | 2334 | |
4ebe6e95 | 2335 | cx = CX_CUR(); |
3b719c58 | 2336 | assert(CxTYPE_is_LOOP(cx)); |
032736ab DM |
2337 | oldsp = PL_stack_base + cx->blk_oldsp; |
2338 | base = CxTYPE(cx) == CXt_LOOP_LIST | |
1bef65a2 | 2339 | ? PL_stack_base + cx->blk_loop.state_u.stack.basesp |
032736ab | 2340 | : oldsp; |
61d3b95a | 2341 | gimme = cx->blk_gimme; |
f86702cc | 2342 | |
0663a8f8 | 2343 | if (gimme == G_VOID) |
032736ab | 2344 | PL_stack_sp = base; |
0663a8f8 | 2345 | else |
032736ab | 2346 | leave_adjust_stacks(oldsp, base, gimme, |
75bc488d | 2347 | PL_op->op_private & OPpLVALUE ? 3 : 1); |
f86702cc | 2348 | |
2f450c1b | 2349 | CX_LEAVE_SCOPE(cx); |
d1b6bf72 | 2350 | cx_poploop(cx); /* Stack values are safe: release loop vars ... */ |
ed8ff0f3 | 2351 | cx_popblock(cx); |
5da525e9 | 2352 | CX_POP(cx); |
f86702cc | 2353 | |
f86702cc | 2354 | return NORMAL; |
a0d0e21e LW |
2355 | } |
2356 | ||
31ccb4f5 DM |
2357 | |
2358 | /* This duplicates most of pp_leavesub, but with additional code to handle | |
2359 | * return args in lvalue context. It was forked from pp_leavesub to | |
2360 | * avoid slowing down that function any further. | |
2361 | * | |
2362 | * Any changes made to this function may need to be copied to pp_leavesub | |
2363 | * and vice-versa. | |
c349b9a0 DM |
2364 | * |
2365 | * also tail-called by pp_return | |
57486a97 DM |
2366 | */ |
2367 | ||
31ccb4f5 | 2368 | PP(pp_leavesublv) |
3bdf583b | 2369 | { |
1c23e2bd | 2370 | U8 gimme; |
57486a97 | 2371 | PERL_CONTEXT *cx; |
799da9d7 | 2372 | SV **oldsp; |
5da525e9 | 2373 | OP *retop; |
57486a97 | 2374 | |
4ebe6e95 | 2375 | cx = CX_CUR(); |
61d3b95a DM |
2376 | assert(CxTYPE(cx) == CXt_SUB); |
2377 | ||
2378 | if (CxMULTICALL(cx)) { | |
1f0ba93b DM |
2379 | /* entry zero of a stack is always PL_sv_undef, which |
2380 | * simplifies converting a '()' return into undef in scalar context */ | |
2381 | assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef); | |
85ecf147 | 2382 | return 0; |
1f0ba93b | 2383 | } |
85ecf147 | 2384 | |
61d3b95a | 2385 | gimme = cx->blk_gimme; |
799da9d7 | 2386 | oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */ |
57486a97 | 2387 | |
799da9d7 DM |
2388 | if (gimme == G_VOID) |
2389 | PL_stack_sp = oldsp; | |
2390 | else { | |
2391 | U8 lval = CxLVAL(cx); | |
2392 | bool is_lval = (lval && !(lval & OPpENTERSUB_INARGS)); | |
2393 | const char *what = NULL; | |
2394 | ||
2395 | if (gimme == G_SCALAR) { | |
2396 | if (is_lval) { | |
2397 | /* check for bad return arg */ | |
2398 | if (oldsp < PL_stack_sp) { | |
2399 | SV *sv = *PL_stack_sp; | |
2400 | if ((SvPADTMP(sv) || SvREADONLY(sv))) { | |
2401 | what = | |
2402 | SvREADONLY(sv) ? (sv == &PL_sv_undef) ? "undef" | |
2403 | : "a readonly value" : "a temporary"; | |
2404 | } | |
2405 | else goto ok; | |
2406 | } | |
2407 | else { | |
2408 | /* sub:lvalue{} will take us here. */ | |
2409 | what = "undef"; | |
2410 | } | |
2411 | croak: | |
2412 | Perl_croak(aTHX_ | |
2413 | "Can't return %s from lvalue subroutine", what); | |
2414 | } | |
57486a97 | 2415 | |
799da9d7 | 2416 | ok: |
e02ce34b | 2417 | leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2); |
e80c4acf | 2418 | |
799da9d7 DM |
2419 | if (lval & OPpDEREF) { |
2420 | /* lval_sub()->{...} and similar */ | |
2421 | dSP; | |
2422 | SvGETMAGIC(TOPs); | |
2423 | if (!SvOK(TOPs)) { | |
2424 | TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF); | |
2425 | } | |
2426 | PUTBACK; | |
2427 | } | |
2428 | } | |
2429 | else { | |
2430 | assert(gimme == G_ARRAY); | |
2431 | assert (!(lval & OPpDEREF)); | |
2432 | ||
2433 | if (is_lval) { | |
2434 | /* scan for bad return args */ | |
2435 | SV **p; | |
2436 | for (p = PL_stack_sp; p > oldsp; p--) { | |
2437 | SV *sv = *p; | |
2438 | /* the PL_sv_undef exception is to allow things like | |
2439 | * this to work, where PL_sv_undef acts as 'skip' | |
2440 | * placeholder on the LHS of list assigns: | |
2441 | * sub foo :lvalue { undef } | |
2442 | * ($a, undef, foo(), $b) = 1..4; | |
2443 | */ | |
2444 | if (sv != &PL_sv_undef && (SvPADTMP(sv) || SvREADONLY(sv))) | |
2445 | { | |
2446 | /* Might be flattened array after $#array = */ | |
2447 | what = SvREADONLY(sv) | |
2448 | ? "a readonly value" : "a temporary"; | |
2449 | goto croak; | |
2450 | } | |
2451 | } | |
2452 | } | |
2453 | ||
e02ce34b | 2454 | leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2); |
799da9d7 | 2455 | } |
3bdf583b | 2456 | } |
57486a97 | 2457 | |
2f450c1b | 2458 | CX_LEAVE_SCOPE(cx); |
a73d8813 | 2459 | cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */ |
ed8ff0f3 | 2460 | cx_popblock(cx); |
5da525e9 DM |
2461 | retop = cx->blk_sub.retop; |
2462 | CX_POP(cx); | |
57486a97 | 2463 | |
5da525e9 | 2464 | return retop; |
3bdf583b FC |
2465 | } |
2466 | ||
57486a97 | 2467 | |
a0d0e21e LW |
2468 | PP(pp_return) |
2469 | { | |
20b7effb | 2470 | dSP; dMARK; |
eb578fdb | 2471 | PERL_CONTEXT *cx; |
5b6f7443 | 2472 | const I32 cxix = dopopto_cursub(); |
0bd48802 | 2473 | |
d40dc6b1 DM |
2474 | assert(cxstack_ix >= 0); |
2475 | if (cxix < cxstack_ix) { | |
2476 | if (cxix < 0) { | |
79646418 DM |
2477 | if (!( PL_curstackinfo->si_type == PERLSI_SORT |
2478 | || ( PL_curstackinfo->si_type == PERLSI_MULTICALL | |
2479 | && (cxstack[0].cx_type & CXp_SUB_RE_FAKE)) | |
2480 | ) | |
2481 | ) | |
3089a108 | 2482 | DIE(aTHX_ "Can't return outside a subroutine"); |
79646418 DM |
2483 | /* We must be in: |
2484 | * a sort block, which is a CXt_NULL not a CXt_SUB; | |
2485 | * or a /(?{...})/ block. | |
2486 | * Handle specially. */ | |
2487 | assert(CxTYPE(&cxstack[0]) == CXt_NULL | |
2488 | || ( CxTYPE(&cxstack[0]) == CXt_SUB | |
2489 | && (cxstack[0].cx_type & CXp_SUB_RE_FAKE))); | |
3089a108 DM |
2490 | if (cxstack_ix > 0) { |
2491 | /* See comment below about context popping. Since we know | |
2492 | * we're scalar and not lvalue, we can preserve the return | |
2493 | * value in a simpler fashion than there. */ | |
2494 | SV *sv = *SP; | |
d40dc6b1 | 2495 | assert(cxstack[0].blk_gimme == G_SCALAR); |
3089a108 DM |
2496 | if ( (sp != PL_stack_base) |
2497 | && !(SvFLAGS(sv) & (SVs_TEMP|SVs_PADTMP)) | |
2498 | ) | |
2499 | *SP = sv_mortalcopy(sv); | |
2500 | dounwind(0); | |
d40dc6b1 | 2501 | } |
3089a108 DM |
2502 | /* caller responsible for popping cxstack[0] */ |
2503 | return 0; | |
d40dc6b1 | 2504 | } |
3089a108 DM |
2505 | |
2506 | /* There are contexts that need popping. Doing this may free the | |
c349b9a0 | 2507 | * return value(s), so preserve them first: e.g. popping the plain |
3089a108 DM |
2508 | * loop here would free $x: |
2509 | * sub f { { my $x = 1; return $x } } | |
2510 | * We may also need to shift the args down; for example, | |
2511 | * for (1,2) { return 3,4 } | |
c349b9a0 DM |
2512 | * leaves 1,2,3,4 on the stack. Both these actions will be done by |
2513 | * leave_adjust_stacks(), along with freeing any temps. Note that | |
2514 | * whoever we tail-call (e.g. pp_leaveeval) will also call | |
2515 | * leave_adjust_stacks(); however, the second call is likely to | |
2516 | * just see a bunch of SvTEMPs with a ref count of 1, and so just | |
2517 | * pass them through, rather than copying them again. So this | |
2518 | * isn't as inefficient as it sounds. | |
3089a108 DM |
2519 | */ |
2520 | cx = &cxstack[cxix]; | |
3089a108 | 2521 | PUTBACK; |
e02ce34b DM |
2522 | if (cx->blk_gimme != G_VOID) |
2523 | leave_adjust_stacks(MARK, PL_stack_base + cx->blk_oldsp, | |
fc6e609e DM |
2524 | cx->blk_gimme, |
2525 | CxTYPE(cx) == CXt_SUB && CvLVALUE(cx->blk_sub.cv) | |
2526 | ? 3 : 0); | |
0663a8f8 | 2527 | SPAGAIN; |
a0d0e21e | 2528 | dounwind(cxix); |
42831ce4 | 2529 | cx = &cxstack[cxix]; /* CX stack may have been realloced */ |
d40dc6b1 | 2530 | } |
3089a108 DM |
2531 | else { |
2532 | /* Like in the branch above, we need to handle any extra junk on | |
2533 | * the stack. But because we're not also popping extra contexts, we | |
2534 | * don't have to worry about prematurely freeing args. So we just | |
2535 | * need to do the bare minimum to handle junk, and leave the main | |
2536 | * arg processing in the function we tail call, e.g. pp_leavesub. | |
2537 | * In list context we have to splice out the junk; in scalar | |
2538 | * context we can leave as-is (pp_leavesub will later return the | |
2539 | * top stack element). But for an empty arg list, e.g. | |
2540 | * for (1,2) { return } | |
2541 | * we need to set sp = oldsp so that pp_leavesub knows to push | |
2542 | * &PL_sv_undef onto the stack. | |
2543 | */ | |
3ebe7c5a DM |
2544 | SV **oldsp; |
2545 | cx = &cxstack[cxix]; | |
2546 | oldsp = PL_stack_base + cx->blk_oldsp; | |
2547 | if (oldsp != MARK) { | |
2548 | SSize_t nargs = SP - MARK; | |
2549 | if (nargs) { | |
2550 | if (cx->blk_gimme == G_ARRAY) { | |
2551 | /* shift return args to base of call stack frame */ | |
2552 | Move(MARK + 1, oldsp + 1, nargs, SV*); | |
2553 | PL_stack_sp = oldsp + nargs; | |
2554 | } | |
6228a1e1 | 2555 | } |
3ebe7c5a DM |
2556 | else |
2557 | PL_stack_sp = oldsp; | |
13929c4c | 2558 | } |
3089a108 | 2559 | } |
617a4f41 DM |
2560 | |
2561 | /* fall through to a normal exit */ | |
2562 | switch (CxTYPE(cx)) { | |
2563 | case CXt_EVAL: | |
2564 | return CxTRYBLOCK(cx) | |
2565 | ? Perl_pp_leavetry(aTHX) | |
2566 | : Perl_pp_leaveeval(aTHX); | |
2567 | case CXt_SUB: | |
13929c4c | 2568 | return CvLVALUE(cx->blk_sub.cv) |
31ccb4f5 | 2569 | ? Perl_pp_leavesublv(aTHX) |
13929c4c | 2570 | : Perl_pp_leavesub(aTHX); |
7766f137 | 2571 | case CXt_FORMAT: |
617a4f41 | 2572 | return Perl_pp_leavewrite(aTHX); |
a0d0e21e | 2573 | default: |
5637ef5b | 2574 | DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx)); |
a0d0e21e | 2575 | } |
a0d0e21e LW |
2576 | } |
2577 | ||
42b5eca0 | 2578 | /* find the enclosing loop or labelled loop and dounwind() back to it. */ |
4f443c3d | 2579 | |
31705cda | 2580 | static PERL_CONTEXT * |
42b5eca0 | 2581 | S_unwind_loop(pTHX) |
a0d0e21e | 2582 | { |
a0d0e21e | 2583 | I32 cxix; |
1f039d60 FC |
2584 | if (PL_op->op_flags & OPf_SPECIAL) { |
2585 | cxix = dopoptoloop(cxstack_ix); | |
2586 | if (cxix < 0) | |
2587 | /* diag_listed_as: Can't "last" outside a loop block */ | |
42b5eca0 DM |
2588 | Perl_croak(aTHX_ "Can't \"%s\" outside a loop block", |
2589 | OP_NAME(PL_op)); | |
1f039d60 FC |
2590 | } |
2591 | else { | |
2592 | dSP; | |
2593 | STRLEN label_len; | |
2594 | const char * const label = | |
2595 | PL_op->op_flags & OPf_STACKED | |
2596 | ? SvPV(TOPs,label_len) | |
2597 | : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv); | |
2598 | const U32 label_flags = | |
2599 | PL_op->op_flags & OPf_STACKED | |
2600 | ? SvUTF8(POPs) | |
2601 | : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; | |
2602 | PUTBACK; | |
2603 | cxix = dopoptolabel(label, label_len, label_flags); | |
2604 | if (cxix < 0) | |
2605 | /* diag_listed_as: Label not found for "last %s" */ | |
147e3846 | 2606 | Perl_croak(aTHX_ "Label not found for \"%s %" SVf "\"", |
42b5eca0 | 2607 | OP_NAME(PL_op), |
1f039d60 FC |
2608 | SVfARG(PL_op->op_flags & OPf_STACKED |
2609 | && !SvGMAGICAL(TOPp1s) | |
2610 | ? TOPp1s | |
2611 | : newSVpvn_flags(label, | |
2612 | label_len, | |
2613 | label_flags | SVs_TEMP))); | |
2614 | } | |
2615 | if (cxix < cxstack_ix) | |
2616 | dounwind(cxix); | |
dc7f00ca | 2617 | return &cxstack[cxix]; |
1f039d60 FC |
2618 | } |
2619 | ||
dc7f00ca | 2620 | |
1f039d60 FC |
2621 | PP(pp_last) |
2622 | { | |
eb578fdb | 2623 | PERL_CONTEXT *cx; |
5da525e9 | 2624 | OP* nextop; |
9d4ba2ae | 2625 | |
42b5eca0 | 2626 | cx = S_unwind_loop(aTHX); |
4df352a8 | 2627 | |
93661e56 | 2628 | assert(CxTYPE_is_LOOP(cx)); |
1bef65a2 DM |
2629 | PL_stack_sp = PL_stack_base |
2630 | + (CxTYPE(cx) == CXt_LOOP_LIST | |
2631 | ? cx->blk_loop.state_u.stack.basesp | |
2632 | : cx->blk_oldsp | |
2633 | ); | |
a0d0e21e | 2634 | |
a1f49e72 | 2635 | TAINT_NOT; |
f86702cc | 2636 | |
2637 | /* Stack values are safe: */ | |
2f450c1b | 2638 | CX_LEAVE_SCOPE(cx); |
d1b6bf72 | 2639 | cx_poploop(cx); /* release loop vars ... */ |
ed8ff0f3 | 2640 | cx_popblock(cx); |
5da525e9 DM |
2641 | nextop = cx->blk_loop.my_op->op_lastop->op_next; |
2642 | CX_POP(cx); | |
a0d0e21e | 2643 | |
5da525e9 | 2644 | return nextop; |
a0d0e21e LW |
2645 | } |
2646 | ||
2647 | PP(pp_next) | |
2648 | { | |
eb578fdb | 2649 | PERL_CONTEXT *cx; |
a0d0e21e | 2650 | |
cd97dc8d DM |
2651 | /* if not a bare 'next' in the main scope, search for it */ |
2652 | cx = CX_CUR(); | |
2653 | if (!((PL_op->op_flags & OPf_SPECIAL) && CxTYPE_is_LOOP(cx))) | |
2654 | cx = S_unwind_loop(aTHX); | |
a0d0e21e | 2655 | |
ed8ff0f3 | 2656 | cx_topblock(cx); |
3a1b2b9e | 2657 | PL_curcop = cx->blk_oldcop; |
47c9d59f | 2658 | PERL_ASYNC_CHECK(); |
d57ce4df | 2659 | return (cx)->blk_loop.my_op->op_nextop; |
a0d0e21e LW |
2660 | } |
2661 | ||
2662 | PP(pp_redo) | |
2663 | { | |
42b5eca0 | 2664 | PERL_CONTEXT *cx = S_unwind_loop(aTHX); |
dc7f00ca | 2665 | OP* redo_op = cx->blk_loop.my_op->op_redoop; |
a0d0e21e | 2666 | |
a034e688 DM |
2667 | if (redo_op->op_type == OP_ENTER) { |
2668 | /* pop one less context to avoid $x being freed in while (my $x..) */ | |
2669 | cxstack_ix++; | |
dc7f00ca DM |
2670 | cx = CX_CUR(); |
2671 | assert(CxTYPE(cx) == CXt_BLOCK); | |
a034e688 DM |
2672 | redo_op = redo_op->op_next; |
2673 | } | |
2674 | ||
936c78b5 | 2675 | FREETMPS; |
ef588991 | 2676 | CX_LEAVE_SCOPE(cx); |
ed8ff0f3 | 2677 | cx_topblock(cx); |
3a1b2b9e | 2678 | PL_curcop = cx->blk_oldcop; |
47c9d59f | 2679 | PERL_ASYNC_CHECK(); |
a034e688 | 2680 | return redo_op; |
a0d0e21e LW |
2681 | } |
2682 | ||
6d90e983 | 2683 | #define UNENTERABLE (OP *)1 |
4bfb5532 | 2684 | #define GOTO_DEPTH 64 |
6d90e983 | 2685 | |
0824fdcb | 2686 | STATIC OP * |
5db1eb8d | 2687 | S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit) |
a0d0e21e | 2688 | { |
a0d0e21e | 2689 | OP **ops = opstack; |
a1894d81 | 2690 | static const char* const too_deep = "Target of goto is too deeply nested"; |
a0d0e21e | 2691 | |
7918f24d NC |
2692 | PERL_ARGS_ASSERT_DOFINDLABEL; |
2693 | ||
fc36a67e | 2694 | if (ops >= oplimit) |
0157ef98 | 2695 | Perl_croak(aTHX_ "%s", too_deep); |
11343788 MB |
2696 | if (o->op_type == OP_LEAVE || |
2697 | o->op_type == OP_SCOPE || | |
2698 | o->op_type == OP_LEAVELOOP || | |
33d34e4c | 2699 | o->op_type == OP_LEAVESUB || |
7896dde7 Z |
2700 | o->op_type == OP_LEAVETRY || |
2701 | o->op_type == OP_LEAVEGIVEN) | |
fc36a67e | 2702 | { |
5dc0d613 | 2703 | *ops++ = cUNOPo->op_first; |
fc36a67e | 2704 | } |
4bfb5532 FC |
2705 | else if (oplimit - opstack < GOTO_DEPTH) { |
2706 | if (o->op_flags & OPf_KIDS | |
6d90e983 FC |
2707 | && cUNOPo->op_first->op_type == OP_PUSHMARK) { |
2708 | *ops++ = UNENTERABLE; | |
4bfb5532 FC |
2709 | } |
2710 | else if (o->op_flags & OPf_KIDS && PL_opargs[o->op_type] | |
6d90e983 FC |
2711 | && OP_CLASS(o) != OA_LOGOP |
2712 | && o->op_type != OP_LINESEQ | |
2713 | && o->op_type != OP_SREFGEN | |
4d7e83bb | 2714 | && o->op_type != OP_ENTEREVAL |
c1cc29fd | 2715 | && o->op_type != OP_GLOB |
6d90e983 FC |
2716 | && o->op_type != OP_RV2CV) { |
2717 | OP * const kid = cUNOPo->op_first; | |
2718 | if (OP_GIMME(kid, 0) != G_SCALAR || OpHAS_SIBLING(kid)) | |
2719 | *ops++ = UNENTERABLE; | |
4bfb5532 | 2720 | } |
6d90e983 FC |
2721 | } |
2722 | if (ops >= oplimit) | |
2723 | Perl_croak(aTHX_ "%s", too_deep); | |
c4aa4e48 | 2724 | *ops = 0; |
11343788 | 2725 | if (o->op_flags & OPf_KIDS) { |
aec46f14 | 2726 | OP *kid; |
b4dcd72d | 2727 | OP * const kid1 = cUNOPo->op_first; |
a0d0e21e | 2728 | /* First try all the kids at this level, since that's likeliest. */ |
e6dae479 | 2729 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { |
7e8f1eac | 2730 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
5db1eb8d BF |
2731 | STRLEN kid_label_len; |
2732 | U32 kid_label_flags; | |
2733 | const char *kid_label = CopLABEL_len_flags(kCOP, | |
2734 | &kid_label_len, &kid_label_flags); | |
2735 | if (kid_label && ( | |
2736 | ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ? | |
2737 | (flags & SVf_UTF8) | |
2738 | ? (bytes_cmp_utf8( | |
2739 | (const U8*)kid_label, kid_label_len, | |
2740 | (const U8*)label, len) == 0) | |
2741 | : (bytes_cmp_utf8( | |
2742 | (const U8*)label, len, | |
2743 | (const U8*)kid_label, kid_label_len) == 0) | |
eade7155 BF |
2744 | : ( len == kid_label_len && ((kid_label == label) |
2745 | || memEQ(kid_label, label, len))))) | |
7e8f1eac AD |
2746 | return kid; |
2747 | } | |
a0d0e21e | 2748 | } |
e6dae479 | 2749 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { |
b4dcd72d | 2750 | bool first_kid_of_binary = FALSE; |
3280af22 | 2751 | if (kid == PL_lastgotoprobe) |
a0d0e21e | 2752 | continue; |
ed8d0fe2 SM |
2753 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { |
2754 | if (ops == opstack) | |
2755 | *ops++ = kid; | |
6d90e983 FC |
2756 | else if (ops[-1] != UNENTERABLE |
2757 | && (ops[-1]->op_type == OP_NEXTSTATE || | |
2758 | ops[-1]->op_type == OP_DBSTATE)) | |
ed8d0fe2 SM |
2759 | ops[-1] = kid; |
2760 | else | |
2761 | *ops++ = kid; | |
2762 | } | |
b4dcd72d FC |
2763 | if (kid == kid1 && ops != opstack && ops[-1] == UNENTERABLE) { |
2764 | first_kid_of_binary = TRUE; | |
2765 | ops--; | |
2766 | } | |
5db1eb8d | 2767 | if ((o = dofindlabel(kid, label, len, flags, ops, oplimit))) |
11343788 | 2768 | return o; |
b4dcd72d FC |
2769 | if (first_kid_of_binary) |
2770 | *ops++ = UNENTERABLE; | |
a0d0e21e LW |
2771 | } |
2772 | } | |
c4aa4e48 | 2773 | *ops = 0; |
a0d0e21e LW |
2774 | return 0; |
2775 | } | |
2776 | ||
b1c05ba5 | 2777 | |
b5377742 FC |
2778 | static void |
2779 | S_check_op_type(pTHX_ OP * const o) | |
2780 | { | |
2781 | /* Eventually we may want to stack the needed arguments | |
2782 | * for each op. For now, we punt on the hard ones. */ | |
2783 | /* XXX This comment seems to me like wishful thinking. --sprout */ | |
6d90e983 FC |
2784 | if (o == UNENTERABLE) |
2785 | Perl_croak(aTHX_ | |
2786 | "Can't \"goto\" into a binary or list expression"); | |
b5377742 FC |
2787 | if (o->op_type == OP_ENTERITER) |
2788 | Perl_croak(aTHX_ | |
2789 | "Can't \"goto\" into the middle of a foreach loop"); | |
a01f4640 FC |
2790 | if (o->op_type == OP_ENTERGIVEN) |
2791 | Perl_croak(aTHX_ | |
2792 | "Can't \"goto\" into a \"given\" block"); | |
b5377742 FC |
2793 | } |
2794 | ||
b1c05ba5 DM |
2795 | /* also used for: pp_dump() */ |
2796 | ||
2797 | PP(pp_goto) | |
a0d0e21e | 2798 | { |
27da23d5 | 2799 | dVAR; dSP; |
cbbf8932 | 2800 | OP *retop = NULL; |
a0d0e21e | 2801 | I32 ix; |
eb578fdb | 2802 | PERL_CONTEXT *cx; |
fc36a67e | 2803 | OP *enterops[GOTO_DEPTH]; |
cbbf8932 | 2804 | const char *label = NULL; |
5db1eb8d BF |
2805 | STRLEN label_len = 0; |
2806 | U32 label_flags = 0; | |
bfed75c6 | 2807 | const bool do_dump = (PL_op->op_type == OP_DUMP); |
a1894d81 | 2808 | static const char* const must_have_label = "goto must have label"; |
a0d0e21e | 2809 | |
533c011a | 2810 | if (PL_op->op_flags & OPf_STACKED) { |
7d1d69cb DM |
2811 | /* goto EXPR or goto &foo */ |
2812 | ||
9d4ba2ae | 2813 | SV * const sv = POPs; |
55b37f1c | 2814 | SvGETMAGIC(sv); |
a0d0e21e | 2815 | |
a0d0e21e | 2816 | if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) { |
0fa3d31d | 2817 | /* This egregious kludge implements goto &subroutine */ |
a0d0e21e | 2818 | I32 cxix; |
eb578fdb | 2819 | PERL_CONTEXT *cx; |
ea726b52 | 2820 | CV *cv = MUTABLE_CV(SvRV(sv)); |
049bd5ff | 2821 | AV *arg = GvAV(PL_defgv); |
a0d0e21e | 2822 | |
5d52e310 | 2823 | while (!CvROOT(cv) && !CvXSUB(cv)) { |
7fc63493 | 2824 | const GV * const gv = CvGV(cv); |
e8f7dd13 | 2825 | if (gv) { |
7fc63493 | 2826 | GV *autogv; |
e8f7dd13 GS |
2827 | SV *tmpstr; |
2828 | /* autoloaded stub? */ | |
2829 | if (cv != GvCV(gv) && (cv = GvCV(gv))) | |
5d52e310 | 2830 | continue; |
c271df94 BF |
2831 | autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), |
2832 | GvNAMELEN(gv), | |
2833 | GvNAMEUTF8(gv) ? SVf_UTF8 : 0); | |
e8f7dd13 | 2834 | if (autogv && (cv = GvCV(autogv))) |
5d52e310 | 2835 | continue; |
e8f7dd13 | 2836 | tmpstr = sv_newmortal(); |
c445ea15 | 2837 | gv_efullname3(tmpstr, gv, NULL); |
147e3846 | 2838 | DIE(aTHX_ "Goto undefined subroutine &%" SVf, SVfARG(tmpstr)); |
4aa0a1f7 | 2839 | } |
cea2e8a9 | 2840 | DIE(aTHX_ "Goto undefined subroutine"); |
4aa0a1f7 AD |
2841 | } |
2842 | ||
5b6f7443 | 2843 | cxix = dopopto_cursub(); |
d338c0c2 DM |
2844 | if (cxix < 0) { |
2845 | DIE(aTHX_ "Can't goto subroutine outside a subroutine"); | |
8da3792e | 2846 | } |
d338c0c2 | 2847 | cx = &cxstack[cxix]; |
564abe23 | 2848 | /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */ |
2d43a17f | 2849 | if (CxTYPE(cx) == CXt_EVAL) { |
c74ace89 | 2850 | if (CxREALEVAL(cx)) |
00455a92 | 2851 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ |
c74ace89 DM |
2852 | DIE(aTHX_ "Can't goto subroutine from an eval-string"); |
2853 | else | |
00455a92 | 2854 | /* diag_listed_as: Can't goto subroutine from an eval-%s */ |
c74ace89 | 2855 | DIE(aTHX_ "Can't goto subroutine from an eval-block"); |
2d43a17f | 2856 | } |
9850bf21 RH |
2857 | else if (CxMULTICALL(cx)) |
2858 | DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)"); | |
d338c0c2 DM |
2859 | |
2860 | /* First do some returnish stuff. */ | |
2861 | ||
2862 | SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */ | |
2863 | FREETMPS; | |
2864 | if (cxix < cxstack_ix) { | |
2865 | dounwind(cxix); | |
2866 | } | |
7e637ba4 | 2867 | cx = CX_CUR(); |
ed8ff0f3 | 2868 | cx_topblock(cx); |
d338c0c2 | 2869 | SPAGAIN; |
39de75fd | 2870 | |
8ae997c5 DM |
2871 | /* protect @_ during save stack unwind. */ |
2872 | if (arg) | |
2873 | SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg))); | |
2874 | ||
2875 | assert(PL_scopestack_ix == cx->blk_oldscopesp); | |
dfe0f39b | 2876 | CX_LEAVE_SCOPE(cx); |
8ae997c5 | 2877 | |
bafb2adc | 2878 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { |
a73d8813 | 2879 | /* this is part of cx_popsub_args() */ |
9513529b | 2880 | AV* av = MUTABLE_AV(PAD_SVl(0)); |
e2657e18 DM |
2881 | assert(AvARRAY(MUTABLE_AV( |
2882 | PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[ | |
2883 | CvDEPTH(cx->blk_sub.cv)])) == PL_curpad); | |
2884 | ||
f72bdec3 DM |
2885 | /* we are going to donate the current @_ from the old sub |
2886 | * to the new sub. This first part of the donation puts a | |
2887 | * new empty AV in the pad[0] slot of the old sub, | |
2888 | * unless pad[0] and @_ differ (e.g. if the old sub did | |
2889 | * local *_ = []); in which case clear the old pad[0] | |
2890 | * array in the usual way */ | |
95b2f486 DM |
2891 | if (av == arg || AvREAL(av)) |
2892 | clear_defarray(av, av == arg); | |
049bd5ff | 2893 | else CLEAR_ARGARRAY(av); |
a0d0e21e | 2894 | } |
88c11d84 | 2895 | |
b1e25d05 DM |
2896 | /* don't restore PL_comppad here. It won't be needed if the |
2897 | * sub we're going to is non-XS, but restoring it early then | |
2898 | * croaking (e.g. the "Goto undefined subroutine" below) | |
2899 | * means the CX block gets processed again in dounwind, | |
2900 | * but this time with the wrong PL_comppad */ | |
88c11d84 | 2901 | |
1d59c038 FC |
2902 | /* A destructor called during LEAVE_SCOPE could have undefined |
2903 | * our precious cv. See bug #99850. */ | |
2904 | if (!CvROOT(cv) && !CvXSUB(cv)) { | |
2905 | const GV * const gv = CvGV(cv); | |
2906 | if (gv) { | |
2907 | SV * const tmpstr = sv_newmortal(); | |
2908 | gv_efullname3(tmpstr, gv, NULL); | |
147e3846 | 2909 | DIE(aTHX_ "Goto undefined subroutine &%" SVf, |
1d59c038 FC |
2910 | SVfARG(tmpstr)); |
2911 | } | |
2912 | DIE(aTHX_ "Goto undefined subroutine"); | |
2913 | } | |
2914 | ||
cd17cc2e DM |
2915 | if (CxTYPE(cx) == CXt_SUB) { |
2916 | CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth; | |
2917 | SvREFCNT_dec_NN(cx->blk_sub.cv); | |
2918 | } | |
2919 | ||
a0d0e21e | 2920 | /* Now do some callish stuff. */ |
aed2304a | 2921 | if (CvISXSUB(cv)) { |
ad39f3a2 | 2922 | const SSize_t items = arg ? AvFILL(arg) + 1 : 0; |
cd313eb4 | 2923 | const bool m = arg ? cBOOL(SvRMAGICAL(arg)) : 0; |
049bd5ff FC |
2924 | SV** mark; |
2925 | ||
8ae997c5 | 2926 | ENTER; |
80774f05 DM |
2927 | SAVETMPS; |
2928 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ | |
2929 | ||
049bd5ff | 2930 | /* put GvAV(defgv) back onto stack */ |
8c9d3376 FC |
2931 | if (items) { |
2932 | EXTEND(SP, items+1); /* @_ could have been extended. */ | |
8c9d3376 | 2933 | } |
049bd5ff | 2934 | mark = SP; |
ad39f3a2 | 2935 | if (items) { |
de935cc9 | 2936 | SSize_t index; |
ad39f3a2 | 2937 | bool r = cBOOL(AvREAL(arg)); |
b1464ded | 2938 | for (index=0; index<items; index++) |
ad39f3a2 FC |
2939 | { |
2940 | SV *sv; | |
2941 | if (m) { | |
2942 | SV ** const svp = av_fetch(arg, index, 0); | |
2943 | sv = svp ? *svp : NULL; | |
dd2a7f90 | 2944 | } |
ad39f3a2 FC |
2945 | else sv = AvARRAY(arg)[index]; |
2946 | SP[index+1] = sv | |
2947 | ? r ? SvREFCNT_inc_NN(sv_2mortal(sv)) : sv | |
2948 | : sv_2mortal(newSVavdefelem(arg, index, 1)); | |
2949 | } | |
049bd5ff | 2950 | } |
ad39f3a2 | 2951 | SP += items; |
049bd5ff FC |
2952 | if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) { |
2953 | /* Restore old @_ */ | |
b405d38b | 2954 | CX_POP_SAVEARRAY(cx); |
b1464ded | 2955 | } |
1fa4e549 | 2956 | |
51eb35b5 | 2957 | retop = cx->blk_sub.retop; |
b1e25d05 DM |
2958 | PL_comppad = cx->blk_sub.prevcomppad; |
2959 | PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL; | |
8ae997c5 DM |
2960 | |
2961 | /* XS subs don't have a CXt_SUB, so pop it; | |
ed8ff0f3 DM |
2962 | * this is a cx_popblock(), less all the stuff we already did |
2963 | * for cx_topblock() earlier */ | |
8ae997c5 | 2964 | PL_curcop = cx->blk_oldcop; |
843fe1ca DM |
2965 | /* this is cx_popsub, less all the stuff we already did */ |
2966 | PL_curstackinfo->si_cxsubix = cx->blk_sub.old_cxsubix; | |
2967 | ||
5da525e9 | 2968 | CX_POP(cx); |
8ae997c5 | 2969 | |
b37c2d43 AL |
2970 | /* Push a mark for the start of arglist */ |
2971 | PUSHMARK(mark); | |
2972 | PUTBACK; | |
2973 | (void)(*CvXSUB(cv))(aTHX_ cv); | |
a57c6685 | 2974 | LEAVE; |
51eb35b5 | 2975 | goto _return; |
a0d0e21e LW |
2976 | } |
2977 | else { | |
b70d5558 | 2978 | PADLIST * const padlist = CvPADLIST(cv); |
39de75fd | 2979 | |
80774f05 DM |
2980 | SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */ |
2981 | ||
a73d8813 | 2982 | /* partial unrolled cx_pushsub(): */ |
39de75fd | 2983 | |
a0d0e21e | 2984 | cx->blk_sub.cv = cv; |
1a5b3db4 | 2985 | cx->blk_sub.olddepth = CvDEPTH(cv); |
dd2155a4 | 2986 | |
a0d0e21e | 2987 | CvDEPTH(cv)++; |
2c50b7ed DM |
2988 | SvREFCNT_inc_simple_void_NN(cv); |
2989 | if (CvDEPTH(cv) > 1) { | |
2b9dff67 | 2990 | if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION)) |
44a8e56a | 2991 | sub_crush_depth(cv); |
26019298 | 2992 | pad_push(padlist, CvDEPTH(cv)); |
a0d0e21e | 2993 | } |
426a09cd | 2994 | PL_curcop = cx->blk_oldcop; |
fd617465 | 2995 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); |
bafb2adc | 2996 | if (CxHASARGS(cx)) |
6d4ff0d2 | 2997 | { |
f72bdec3 DM |
2998 | /* second half of donating @_ from the old sub to the |
2999 | * new sub: abandon the original pad[0] AV in the | |
3000 | * new sub, and replace it with the donated @_. | |
3001 | * pad[0] takes ownership of the extra refcount | |
3002 | * we gave arg earlier */ | |
bfa371b6 FC |
3003 | if (arg) { |
3004 | SvREFCNT_dec(PAD_SVl(0)); | |
fed4514a | 3005 | PAD_SVl(0) = (SV *)arg; |
13122036 | 3006 | SvREFCNT_inc_simple_void_NN(arg); |
bfa371b6 | 3007 | } |
049bd5ff FC |
3008 | |
3009 | /* GvAV(PL_defgv) might have been modified on scope | |
f72bdec3 | 3010 | exit, so point it at arg again. */ |
049bd5ff FC |
3011 | if (arg != GvAV(PL_defgv)) { |
3012 | AV * const av = GvAV(PL_defgv); | |
3013 | GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg); | |
3014 | SvREFCNT_dec(av); | |
a0d0e21e LW |
3015 | } |
3016 | } | |
13122036 | 3017 | |
491527d0 | 3018 | if (PERLDB_SUB) { /* Checking curstash breaks DProf. */ |
005a8a35 | 3019 | Perl_get_db_sub(aTHX_ NULL, cv); |
b37c2d43 | 3020 | if (PERLDB_GOTO) { |
b96d8cd9 | 3021 | CV * const gotocv = get_cvs("DB::goto", 0); |
b37c2d43 AL |
3022 | if (gotocv) { |
3023 | PUSHMARK( PL_stack_sp ); | |
ad64d0ec | 3024 | call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG); |
b37c2d43 AL |
3025 | PL_stack_sp--; |
3026 | } | |
491527d0 | 3027 | } |
1ce6579f | 3028 | } |
51eb35b5 DD |
3029 | retop = CvSTART(cv); |
3030 | goto putback_return; | |
a0d0e21e LW |
3031 | } |
3032 | } | |
1614b0e3 | 3033 | else { |
7d1d69cb | 3034 | /* goto EXPR */ |
55b37f1c | 3035 | label = SvPV_nomg_const(sv, label_len); |
5db1eb8d | 3036 | label_flags = SvUTF8(sv); |
1614b0e3 | 3037 | } |
a0d0e21e | 3038 | } |
2fc690dc | 3039 | else if (!(PL_op->op_flags & OPf_SPECIAL)) { |
7d1d69cb | 3040 | /* goto LABEL or dump LABEL */ |
5db1eb8d BF |
3041 | label = cPVOP->op_pv; |
3042 | label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0; | |
3043 | label_len = strlen(label); | |
3044 | } | |
0157ef98 | 3045 | if (!(do_dump || label_len)) DIE(aTHX_ "%s", must_have_label); |
a0d0e21e | 3046 | |
f410a211 NC |
3047 | PERL_ASYNC_CHECK(); |
3048 | ||
3532f34a | 3049 | if (label_len) { |
cbbf8932 | 3050 | OP *gotoprobe = NULL; |
3b2447bc | 3051 | bool leaving_eval = FALSE; |
33d34e4c | 3052 | bool in_block = FALSE; |
3c157b3c | 3053 | bool pseudo_block = FALSE; |
cbbf8932 | 3054 | PERL_CONTEXT *last_eval_cx = NULL; |
a0d0e21e LW |
3055 | |
3056 | /* find label */ | |
3057 | ||
d4c19fe8 | 3058 | PL_lastgotoprobe = NULL; |
a0d0e21e LW |
3059 | *enterops = 0; |
3060 | for (ix = cxstack_ix; ix >= 0; ix--) { | |
3061 | cx = &cxstack[ix]; | |
6b35e009 | 3062 | switch (CxTYPE(cx)) { |
a0d0e21e | 3063 | case CXt_EVAL: |
3b2447bc | 3064 | leaving_eval = TRUE; |
971ecbe6 | 3065 | if (!CxTRYBLOCK(cx)) { |
a4f3a277 RH |
3066 | gotoprobe = (last_eval_cx ? |
3067 | last_eval_cx->blk_eval.old_eval_root : | |
3068 | PL_eval_root); | |
3069 | last_eval_cx = cx; | |
9c5794fe RH |
3070 | break; |
3071 | } | |
3072 | /* else fall through */ | |
93661e56 DM |
3073 | case CXt_LOOP_PLAIN: |
3074 | case CXt_LOOP_LAZYIV: | |
3075 | case CXt_LOOP_LAZYSV: | |
3076 | case CXt_LOOP_LIST: | |
3077 | case CXt_LOOP_ARY: | |
7896dde7 Z |
3078 | case CXt_GIVEN: |
3079 | case CXt_WHEN: | |
e6dae479 | 3080 | gotoprobe = OpSIBLING(cx->blk_oldcop); |
a0d0e21e LW |
3081 | break; |
3082 | case CXt_SUBST: | |
3083 | continue; | |
3084 | case CXt_BLOCK: | |
33d34e4c | 3085 | if (ix) { |
e6dae479 | 3086 | gotoprobe = OpSIBLING(cx->blk_oldcop); |
33d34e4c AE |
3087 | in_block = TRUE; |
3088 | } else | |
3280af22 | 3089 | gotoprobe = PL_main_root; |
a0d0e21e | 3090 | break; |
b3933176 | 3091 | case CXt_SUB: |
3c157b3c Z |
3092 | gotoprobe = CvROOT(cx->blk_sub.cv); |
3093 | pseudo_block = cBOOL(CxMULTICALL(cx)); | |
3094 | break; | |
7766f137 | 3095 | case CXt_FORMAT: |
0a753a76 | 3096 | case CXt_NULL: |
a651a37d | 3097 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); |
a0d0e21e LW |
3098 | default: |
3099 | if (ix) | |
5637ef5b NC |
3100 | DIE(aTHX_ "panic: goto, type=%u, ix=%ld", |
3101 | CxTYPE(cx), (long) ix); | |
3280af22 | 3102 | gotoprobe = PL_main_root; |
a0d0e21e LW |
3103 | break; |
3104 | } | |
2b597662 | 3105 | if (gotoprobe) { |
29e61fd9 DM |
3106 | OP *sibl1, *sibl2; |
3107 | ||
5db1eb8d | 3108 | retop = dofindlabel(gotoprobe, label, label_len, label_flags, |
2b597662 GS |
3109 | enterops, enterops + GOTO_DEPTH); |
3110 | if (retop) | |
3111 | break; | |
e6dae479 | 3112 | if ( (sibl1 = OpSIBLING(gotoprobe)) && |
29e61fd9 | 3113 | sibl1->op_type == OP_UNSTACK && |
e6dae479 | 3114 | (sibl2 = OpSIBLING(sibl1))) |
29e61fd9 DM |
3115 | { |
3116 | retop = dofindlabel(sibl2, | |
5db1eb8d BF |
3117 | label, label_len, label_flags, enterops, |
3118 | enterops + GOTO_DEPTH); | |
eae48c89 Z |
3119 | if (retop) |
3120 | break; | |
3121 | } | |
2b597662 | 3122 | } |
3c157b3c Z |
3123 | if (pseudo_block) |
3124 | DIE(aTHX_ "Can't \"goto\" out of a pseudo block"); | |
3280af22 | 3125 | PL_lastgotoprobe = gotoprobe; |
a0d0e21e LW |
3126 | } |
3127 | if (!retop) | |
147e3846 | 3128 | DIE(aTHX_ "Can't find label %" UTF8f, |
b17a0679 | 3129 | UTF8fARG(label_flags, label_len, label)); |
a0d0e21e | 3130 | |
3b2447bc RH |
3131 | /* if we're leaving an eval, check before we pop any frames |
3132 | that we're not going to punt, otherwise the error | |
3133 | won't be caught */ | |
3134 | ||
3135 | if (leaving_eval && *enterops && enterops[1]) { | |
3136 | I32 i; | |
3137 | for (i = 1; enterops[i]; i++) | |
b5377742 | 3138 | S_check_op_type(aTHX_ enterops[i]); |
3b2447bc RH |
3139 | } |
3140 | ||
b500e03b | 3141 | if (*enterops && enterops[1]) { |
6d90e983 FC |
3142 | I32 i = enterops[1] != UNENTERABLE |
3143 | && enterops[1]->op_type == OP_ENTER && in_block | |
3144 | ? 2 | |
3145 | : 1; | |
b500e03b | 3146 | if (enterops[i]) |
dc6e8de0 | 3147 | deprecate("\"goto\" to jump into a construct"); |
b500e03b GG |
3148 | } |
3149 | ||
a0d0e21e LW |
3150 | /* pop unwanted frames */ |
3151 | ||
3152 | if (ix < cxstack_ix) { | |
a0d0e21e | 3153 | if (ix < 0) |
5edb7975 | 3154 | DIE(aTHX_ "panic: docatch: illegal ix=%ld", (long)ix); |
a0d0e21e | 3155 | dounwind(ix); |
7e637ba4 | 3156 | cx = CX_CUR(); |
ed8ff0f3 | 3157 | cx_topblock(cx); |
a0d0e21e LW |
3158 | } |
3159 | ||
3160 | /* push wanted frames */ | |
3161 | ||
748a9306 | 3162 | if (*enterops && enterops[1]) { |
0bd48802 | 3163 | OP * const oldop = PL_op; |
6d90e983 FC |
3164 | ix = enterops[1] != UNENTERABLE |
3165 | && enterops[1]->op_type == OP_ENTER && in_block | |
3166 | ? 2 | |
3167 | : 1; | |
33d34e4c | 3168 | for (; enterops[ix]; ix++) { |
533c011a | 3169 | PL_op = enterops[ix]; |
b5377742 | 3170 | S_check_op_type(aTHX_ PL_op); |
e57923a2 FC |
3171 | DEBUG_l( Perl_deb(aTHX_ "pp_goto: Entering %s\n", |
3172 | OP_NAME(PL_op))); | |
16c91539 | 3173 | PL_op->op_ppaddr(aTHX); |
a0d0e21e | 3174 | } |
533c011a | 3175 | PL_op = oldop; |
a0d0e21e LW |
3176 | } |
3177 | } | |
3178 | ||
2631bbca | 3179 | if (do_dump) { |
a5f75d66 | 3180 | #ifdef VMS |
6b88bc9c | 3181 | if (!retop) retop = PL_main_start; |
a5f75d66 | 3182 | #endif |
3280af22 NIS |
3183 | PL_restartop = retop; |
3184 | PL_do_undump = TRUE; | |
a0d0e21e LW |
3185 | |
3186 | my_unexec(); | |
3187 | ||
3280af22 NIS |
3188 | PL_restartop = 0; /* hmm, must be GNU unexec().. */ |
3189 | PL_do_undump = FALSE; | |
a0d0e21e LW |
3190 | } |
3191 | ||
51eb35b5 DD |
3192 | putback_return: |
3193 | PL_stack_sp = sp; | |
3194 | _return: | |
47c9d59f | 3195 | PERL_ASYNC_CHECK(); |
51eb35b5 | 3196 | return retop; |
a0d0e21e LW |
3197 | } |
3198 | ||
3199 | PP(pp_exit) | |
3200 | { | |
39644a26 | 3201 | dSP; |
a0d0e21e LW |
3202 | I32 anum; |
3203 | ||
3204 | if (MAXARG < 1) | |
3205 | anum = 0; | |
9d3c658e FC |
3206 | else if (!TOPs) { |
3207 | anum = 0; (void)POPs; | |
3208 | } | |
ff0cee69 | 3209 | else { |
a0d0e21e | 3210 | anum = SvIVx(POPs); |
d98f61e7 | 3211 | #ifdef VMS |
5450b4d8 FC |
3212 | if (anum == 1 |
3213 | && SvTRUE(cop_hints_fetch_pvs(PL_curcop, "vmsish_exit", 0))) | |
ff0cee69 | 3214 | anum = 0; |
97124ef6 FC |
3215 | VMSISH_HUSHED = |
3216 | VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH); | |
ff0cee69 | 3217 | #endif |
3218 | } | |
cc3604b1 | 3219 | PL_exit_flags |= PERL_EXIT_EXPECTED; |
a0d0e21e | 3220 | my_exit(anum); |
3280af22 | 3221 | PUSHs(&PL_sv_undef); |
a0d0e21e LW |
3222 | RETURN; |
3223 | } | |
3224 | ||
a0d0e21e LW |
3225 | /* Eval. */ |
3226 | ||
0824fdcb | 3227 | STATIC void |
cea2e8a9 | 3228 | S_save_lines(pTHX_ AV *array, SV *sv) |
a0d0e21e | 3229 | { |
504618e9 | 3230 | const char *s = SvPVX_const(sv); |
890ce7af | 3231 | const char * const send = SvPVX_const(sv) + SvCUR(sv); |
504618e9 | 3232 | I32 line = 1; |
a0d0e21e | 3233 | |
7918f24d NC |
3234 | PERL_ARGS_ASSERT_SAVE_LINES; |
3235 | ||
a0d0e21e | 3236 | while (s && s < send) { |
f54cb97a | 3237 | const char *t; |
b9f83d2f | 3238 | SV * const tmpstr = newSV_type(SVt_PVMG); |
a0d0e21e | 3239 | |
1d963ff3 | 3240 | t = (const char *)memchr(s, '\n', send - s); |
a0d0e21e LW |
3241 | if (t) |
3242 | t++; | |
3243 | else | |
3244 | t = send; | |
3245 | ||
3246 | sv_setpvn(tmpstr, s, t - s); | |
3247 | av_store(array, line++, tmpstr); | |
3248 | s = t; | |
3249 | } | |
3250 | } | |
3251 | ||
22f16304 RU |
3252 | /* |
3253 | =for apidoc docatch | |
3254 | ||
3255 | Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context. | |
3256 | ||
3257 | 0 is used as continue inside eval, | |
3258 | ||
3259 | 3 is used for a die caught by an inner eval - continue inner loop | |
3260 | ||
75af9d73 | 3261 | See F<cop.h>: je_mustcatch, when set at any runlevel to TRUE, means eval ops must |
22f16304 RU |
3262 | establish a local jmpenv to handle exception traps. |
3263 | ||
3264 | =cut | |
3265 | */ | |
0824fdcb | 3266 | STATIC OP * |
d7e3f70f | 3267 | S_docatch(pTHX_ Perl_ppaddr_t firstpp) |
1e422769 | 3268 | { |
6224f72b | 3269 | int ret; |
06b5626a | 3270 | OP * const oldop = PL_op; |
db36c5a1 | 3271 | dJMPENV; |
1e422769 | 3272 | |
54310121 | 3273 | assert(CATCH_GET == TRUE); |
8bffa5f8 | 3274 | |
14dd3ad8 | 3275 | JMPENV_PUSH(ret); |
6224f72b | 3276 | switch (ret) { |
312caa8e | 3277 | case 0: |
d7e3f70f | 3278 | PL_op = firstpp(aTHX); |
14dd3ad8 | 3279 | redo_body: |
85aaa934 | 3280 | CALLRUNOPS(aTHX); |
312caa8e CS |
3281 | break; |
3282 | case 3: | |
8bffa5f8 | 3283 | /* die caught by an inner eval - continue inner loop */ |
febb3a6d Z |
3284 | if (PL_restartop && PL_restartjmpenv == PL_top_env) { |
3285 | PL_restartjmpenv = NULL; | |
312caa8e CS |
3286 | PL_op = PL_restartop; |
3287 | PL_restartop = 0; | |
3288 | goto redo_body; | |
3289 | } | |
924ba076 | 3290 | /* FALLTHROUGH */ |
312caa8e | 3291 | default: |
14dd3ad8 | 3292 | JMPENV_POP; |
533c011a | 3293 | PL_op = oldop; |
6224f72b | 3294 | JMPENV_JUMP(ret); |
e5964223 | 3295 | NOT_REACHED; /* NOTREACHED */ |
1e422769 | 3296 | } |
14dd3ad8 | 3297 | JMPENV_POP; |
533c011a | 3298 | PL_op = oldop; |
5f66b61c | 3299 | return NULL; |
1e422769 | 3300 | } |
3301 | ||
a3985cdc DM |
3302 | |
3303 | /* | |
3304 | =for apidoc find_runcv | |
3305 | ||
3306 | Locate the CV corresponding to the currently executing sub or eval. | |
796b6530 KW |
3307 | If C<db_seqp> is non_null, skip CVs that are in the DB package and populate |
3308 | C<*db_seqp> with the cop sequence number at the point that the DB:: code was | |
72d33970 FC |
3309 | entered. (This allows debuggers to eval in the scope of the breakpoint |
3310 | rather than in the scope of the debugger itself.) | |
a3985cdc DM |
3311 | |
3312 | =cut | |
3313 | */ | |
3314 | ||
3315 | CV* | |
d819b83a | 3316 | Perl_find_runcv(pTHX_ U32 *db_seqp) |
a3985cdc | 3317 | { |
db4cf31d | 3318 | return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp); |
70794f7b FC |
3319 | } |
3320 | ||
3321 | /* If this becomes part of the API, it might need a better name. */ | |
3322 | CV * | |
db4cf31d | 3323 | Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp) |
70794f7b | 3324 | { |
a3985cdc | 3325 | PERL_SI *si; |
b4b0692a | 3326 | int level = 0; |
a3985cdc | 3327 | |
d819b83a | 3328 | if (db_seqp) |
c3923c33 DM |
3329 | *db_seqp = |
3330 | PL_curcop == &PL_compiling | |
3331 | ? PL_cop_seqmax | |
3332 | : PL_curcop->cop_seq; | |
3333 | ||
a3985cdc | 3334 | for (si = PL_curstackinfo; si; si = si->si_prev) { |
06b5626a | 3335 | I32 ix; |
a3985cdc | 3336 | for (ix = si->si_cxix; ix >= 0; ix--) { |
06b5626a | 3337 | const PERL_CONTEXT *cx = &(si->si_cxstack[ix]); |
70794f7b | 3338 | CV *cv = NULL; |
d819b83a | 3339 | if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) { |
70794f7b | 3340 | cv = cx->blk_sub.cv; |
d819b83a DM |
3341 | /* skip DB:: code */ |
3342 | if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) { | |
3343 | *db_seqp = cx->blk_oldcop->cop_seq; | |
3344 | continue; | |
3345 | } | |
a453e28a DM |
3346 | if (cx->cx_type & CXp_SUB_RE) |
3347 | continue; | |
d819b83a | 3348 | } |
a3985cdc | 3349 | else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx)) |
70794f7b FC |
3350 | cv = cx->blk_eval.cv; |
3351 | if (cv) { | |
3352 | switch (cond) { | |
db4cf31d FC |
3353 | case FIND_RUNCV_padid_eq: |
3354 | if (!CvPADLIST(cv) | |
b4db5868 | 3355 | || CvPADLIST(cv)->xpadl_id != (U32)arg) |
8771da69 | 3356 | continue; |
b4b0692a FC |
3357 | return cv; |
3358 | case FIND_RUNCV_level_eq: | |
db4cf31d | 3359 | if (level++ != arg) continue; |
2165bd23 | 3360 | /* FALLTHROUGH */ |
70794f7b FC |
3361 | default: |
3362 | return cv; | |
3363 | } | |
3364 | } | |
a3985cdc DM |
3365 | } |
3366 | } | |
db4cf31d | 3367 | return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv; |
a3985cdc DM |
3368 | } |
3369 | ||
3370 | ||
27e90453 DM |
3371 | /* Run yyparse() in a setjmp wrapper. Returns: |
3372 | * 0: yyparse() successful | |
3373 | * 1: yyparse() failed | |
3374 | * 3: yyparse() died | |
3375 | */ | |
3376 | STATIC int | |
28ac2b49 | 3377 | S_try_yyparse(pTHX_ int gramtype) |
27e90453 DM |
3378 | { |
3379 | int ret; | |
3380 | dJMPENV; | |
3381 | ||
4ebe6e95 | 3382 | assert(CxTYPE(CX_CUR()) == CXt_EVAL); |
27e90453 DM |
3383 | JMPENV_PUSH(ret); |
3384 | switch (ret) { | |
3385 | case 0: | |
28ac2b49 | 3386 | ret = yyparse(gramtype) ? 1 : 0; |
27e90453 DM |
3387 | break; |
3388 | case 3: | |
3389 | break; | |
3390 | default: | |
3391 | JMPENV_POP; | |
3392 | JMPENV_JUMP(ret); | |
e5964223 | 3393 | NOT_REACHED; /* NOTREACHED */ |
27e90453 DM |
3394 | } |
3395 | JMPENV_POP; | |
3396 | return ret; | |
3397 | } | |
3398 | ||
3399 | ||
104a8185 DM |
3400 | /* Compile a require/do or an eval ''. |
3401 | * | |
a3985cdc | 3402 | * outside is the lexically enclosing CV (if any) that invoked us. |
104a8185 DM |
3403 | * seq is the current COP scope value. |
3404 | * hh is the saved hints hash, if any. | |
3405 | * | |
410be5db | 3406 | * Returns a bool indicating whether the compile was successful; if so, |
104a8185 DM |
3407 | * PL_eval_start contains the first op of the compiled code; otherwise, |
3408 | * pushes undef. | |
3409 | * | |
3410 | * This function is called from two places: pp_require and pp_entereval. | |
3411 | * These can be distinguished by whether PL_op is entereval. | |
7d116edc FC |
3412 | */ |
3413 | ||
410be5db | 3414 | STATIC bool |
1c23e2bd | 3415 | S_doeval_compile(pTHX_ U8 gimme, CV* outside, U32 seq, HV *hh) |
a0d0e21e | 3416 | { |
20b7effb | 3417 | dSP; |
46c461b5 | 3418 | OP * const saveop = PL_op; |
104a8185 | 3419 | bool clear_hints = saveop->op_type != OP_ENTEREVAL; |
f45b078d | 3420 | COP * const oldcurcop = PL_curcop; |
26c9400e | 3421 | bool in_require = (saveop->op_type == OP_REQUIRE); |
27e90453 | 3422 | int yystatus; |
676a678a | 3423 | CV *evalcv; |
a0d0e21e | 3424 | |
27e90453 | 3425 | PL_in_eval = (in_require |
6dc8a9e4 | 3426 | ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL)) |
a1941760 DM |
3427 | : (EVAL_INEVAL | |
3428 | ((PL_op->op_private & OPpEVAL_RE_REPARSING) | |
3429 | ? EVAL_RE_REPARSING : 0))); | |
a0d0e21e | 3430 | |
1ce6579f | 3431 | PUSHMARK(SP); |
3432 | ||
676a678a Z |
3433 | evalcv = MUTABLE_CV(newSV_type(SVt_PVCV)); |
3434 | CvEVAL_on(evalcv); | |
4ebe6e95 DM |
3435 | assert(CxTYPE(CX_CUR()) == CXt_EVAL); |
3436 | CX_CUR()->blk_eval.cv = evalcv; | |
3437 | CX_CUR()->blk_gimme = gimme; | |
2090ab20 | 3438 | |
676a678a Z |
3439 | CvOUTSIDE_SEQ(evalcv) = seq; |
3440 | CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); | |
a3985cdc | 3441 | |
dd2155a4 | 3442 | /* set up a scratch pad */ |
a0d0e21e | 3443 | |
eacbb379 | 3444 | CvPADLIST_set(evalcv, pad_new(padnew_SAVE)); |
cecbe010 | 3445 | PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */ |
2c05e328 | 3446 | |
07055b4c | 3447 | |
b5bbe64a | 3448 | SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */ |
748a9306 | 3449 | |
a0d0e21e LW |
3450 | /* make sure we compile in the right package */ |
3451 | ||
ed094faf | 3452 | if (CopSTASH_ne(PL_curcop, PL_curstash)) { |
03d9f026 | 3453 | SAVEGENERICSV(PL_curstash); |
cb1ad50e FC |
3454 | PL_curstash = (HV *)CopSTASH(PL_curcop); |
3455 | if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL; | |
04680144 FC |
3456 | else { |
3457 | SvREFCNT_inc_simple_void(PL_curstash); | |
3458 | save_item(PL_curstname); | |
3459 | sv_sethek(PL_curstname, HvNAME_HEK(PL_curstash)); | |
3460 | } | |
a0d0e21e | 3461 | } |
3c10abe3 | 3462 | /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */ |
3280af22 NIS |
3463 | SAVESPTR(PL_beginav); |
3464 | PL_beginav = newAV(); | |
3465 | SAVEFREESV(PL_beginav); | |
3c10abe3 AG |
3466 | SAVESPTR(PL_unitcheckav); |
3467 | PL_unitcheckav = newAV(); | |
3468 | SAVEFREESV(PL_unitcheckav); | |
a0d0e21e | 3469 | |
81d86705 | 3470 | |
104a8185 | 3471 | ENTER_with_name("evalcomp"); |
676a678a Z |
3472 | SAVESPTR(PL_compcv); |
3473 | PL_compcv = evalcv; | |
3474 | ||
a0d0e21e LW |
3475 | /* try to compile it */ |
3476 | ||
5f66b61c | 3477 | PL_eval_root = NULL; |
3280af22 | 3478 | PL_curcop = &PL_compiling; |
26c9400e | 3479 | if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL)) |
faef0170 | 3480 | PL_in_eval |= EVAL_KEEPERR; |
ab69dbc2 RGS |
3481 | else |
3482 | CLEAR_ERRSV(); | |
27e90453 | 3483 | |
377b5421 DM |
3484 | SAVEHINTS(); |
3485 | if (clear_hints) { | |
3486 | PL_hints = 0; | |
3487 | hv_clear(GvHV(PL_hintgv)); | |
3488 | } | |
3489 | else { | |
3490 | PL_hints = saveop->op_private & OPpEVAL_COPHH | |
f734918a | 3491 | ? oldcurcop->cop_hints : (U32)saveop->op_targ; |
4f3e2518 DM |
3492 | |
3493 | /* making 'use re eval' not be in scope when compiling the | |
3494 | * qr/mabye_has_runtime_code_block/ ensures that we don't get | |
3495 | * infinite recursion when S_has_runtime_code() gives a false | |
3496 | * positive: the second time round, HINT_RE_EVAL isn't set so we | |
3497 | * don't bother calling S_has_runtime_code() */ | |
3498 | if (PL_in_eval & EVAL_RE_REPARSING) | |
3499 | PL_hints &= ~HINT_RE_EVAL; | |
3500 | ||
377b5421 DM |
3501 | if (hh) { |
3502 | /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */ | |
3503 | SvREFCNT_dec(GvHV(PL_hintgv)); | |
3504 | GvHV(PL_hintgv) = hh; | |
3505 | } | |
3506 | } | |
3507 | SAVECOMPILEWARNINGS(); | |
3508 | if (clear_hints) { | |
3509 | if (PL_dowarn & G_WARN_ALL_ON) | |
3510 | PL_compiling.cop_warnings = pWARN_ALL ; | |
3511 | else if (PL_dowarn & G_WARN_ALL_OFF) | |
3512 | PL_compiling.cop_warnings = pWARN_NONE ; | |
3513 | else | |
3514 | PL_compiling.cop_warnings = pWARN_STD ; | |
3515 | } | |
3516 | else { | |
3517 | PL_compiling.cop_warnings = | |
3518 | DUP_WARNINGS(oldcurcop->cop_warnings); | |
3519 | cophh_free(CopHINTHASH_get(&PL_compiling)); | |
3520 | if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) { | |
3521 | /* The label, if present, is the first entry on the chain. So rather | |
3522 | than writing a blank label in front of it (which involves an | |
3523 | allocation), just use the next entry in the chain. */ | |
3524 | PL_compiling.cop_hints_hash | |
3525 | = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next); | |
3526 | /* Check the assumption that this removed the label. */ | |
3527 | assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL); | |
f45b078d | 3528 | } |
377b5421 DM |
3529 | else |
3530 | PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash); | |
3531 | } | |
f45b078d | 3532 | |
a88d97bf | 3533 | CALL_BLOCK_HOOKS(bhk_eval, saveop); |
52db365a | 3534 | |
27e90453 DM |
3535 | /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>, |
3536 | * so honour CATCH_GET and trap it here if necessary */ | |
3537 | ||
fc69996c DM |
3538 | |
3539 | /* compile the code */ | |
28ac2b49 | 3540 | yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG); |
27e90453 DM |
3541 | |
3542 | if (yystatus || PL_parser->error_count || !PL_eval_root) { | |
d164302a | 3543 | PERL_CONTEXT *cx; |
d308a779 | 3544 | SV *errsv; |
bfed75c6 | 3545 | |
d308a779 | 3546 | PL_op = saveop; |
fc69996c DM |
3547 | /* note that if yystatus == 3, then the require/eval died during |
3548 | * compilation, so the EVAL CX block has already been popped, and | |
3549 | * various vars restored */ | |
27e90453 | 3550 | if (yystatus != 3) { |
c86ffc32 DM |
3551 | if (PL_eval_root) { |
3552 | op_free(PL_eval_root); | |
3553 | PL_eval_root = NULL; | |
3554 | } | |
27e90453 | 3555 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
4ebe6e95 | 3556 | cx = CX_CUR(); |
06a7bc17 DM |
3557 | assert(CxTYPE(cx) == CXt_EVAL); |
3558 | /* pop the CXt_EVAL, and if was a require, croak */ | |
3559 | S_pop_eval_context_maybe_croak(aTHX_ cx, ERRSV, 2); | |
9d7f88dd | 3560 | } |
d308a779 | 3561 | |
03e81cd3 DM |
3562 | /* die_unwind() re-croaks when in require, having popped the |
3563 | * require EVAL context. So we should never catch a require | |
3564 | * exception here */ | |
3565 | assert(!in_require); | |
3566 | ||
3567 | errsv = ERRSV; | |
d308a779 DM |
3568 | if (!*(SvPV_nolen_const(errsv))) |
3569 | sv_setpvs(errsv, "Compilation error"); | |
3570 | ||
2bf54cc6 | 3571 | if (gimme != G_ARRAY) PUSHs(&PL_sv_undef); |
410be5db DM |
3572 | PUTBACK; |
3573 | return FALSE; | |
a0d0e21e | 3574 | } |
fc69996c DM |
3575 | |
3576 | /* Compilation successful. Now clean up */ | |
3577 | ||
3578 | LEAVE_with_name("evalcomp"); | |
104a8185 | 3579 | |
57843af0 | 3580 | CopLINE_set(&PL_compiling, 0); |
104a8185 | 3581 | SAVEFREEOP(PL_eval_root); |
8be227ab | 3582 | cv_forget_slab(evalcv); |
0c58d367 | 3583 | |
a0d0e21e LW |
3584 | DEBUG_x(dump_eval()); |
3585 | ||
55497cff | 3586 | /* Register with debugger: */ |
26c9400e | 3587 | if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) { |
b96d8cd9 | 3588 | CV * const cv = get_cvs("DB::postponed", 0); |
55497cff | 3589 | if (cv) { |
3590 | dSP; | |
924508f0 | 3591 | PUSHMARK(SP); |
ad64d0ec | 3592 | XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); |
55497cff | 3593 | PUTBACK; |
ad64d0ec | 3594 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
55497cff | 3595 | } |
3596 | } | |
3597 | ||
8ed49485 FC |
3598 | if (PL_unitcheckav) { |
3599 | OP *es = PL_eval_start; | |
3c10abe3 | 3600 | call_list(PL_scopestack_ix, PL_unitcheckav); |
8ed49485 FC |
3601 | PL_eval_start = es; |
3602 | } | |
3c10abe3 | 3603 | |
676a678a | 3604 | CvDEPTH(evalcv) = 1; |
3280af22 | 3605 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
533c011a | 3606 | PL_op = saveop; /* The caller may need it. */ |
bc177e6b | 3607 | PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */ |
5dc0d613 | 3608 | |
410be5db DM |
3609 | PUTBACK; |
3610 | return TRUE; | |
a0d0e21e LW |
3611 | } |
3612 | ||
f0dea69c DM |
3613 | /* Return NULL if the file doesn't exist or isn't a file; |
3614 | * else return PerlIO_openn(). | |
3615 | */ | |
fc69996c | 3616 | |
a6c40364 | 3617 | STATIC PerlIO * |
282b29ee | 3618 | S_check_type_and_open(pTHX_ SV *name) |
ce8abf5f SP |
3619 | { |
3620 | Stat_t st; | |
41188aa0 | 3621 | STRLEN len; |
d345f487 | 3622 | PerlIO * retio; |
41188aa0 | 3623 | const char *p = SvPV_const(name, len); |
c8028aa6 | 3624 | int st_rc; |
df528165 | 3625 | |
7918f24d NC |
3626 | PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN; |
3627 | ||
c8028aa6 TC |
3628 | /* checking here captures a reasonable error message when |
3629 | * PERL_DISABLE_PMC is true, but when PMC checks are enabled, the | |
3630 | * user gets a confusing message about looking for the .pmc file | |
1e777496 DD |
3631 | * rather than for the .pm file so do the check in S_doopen_pm when |
3632 | * PMC is on instead of here. S_doopen_pm calls this func. | |
c8028aa6 TC |
3633 | * This check prevents a \0 in @INC causing problems. |
3634 | */ | |
1e777496 | 3635 | #ifdef PERL_DISABLE_PMC |
41188aa0 | 3636 | if (!IS_SAFE_PATHNAME(p, len, "require")) |
c8028aa6 | 3637 | return NULL; |
1e777496 | 3638 | #endif |
c8028aa6 | 3639 | |
d345f487 DD |
3640 | /* on Win32 stat is expensive (it does an open() and close() twice and |
3641 | a couple other IO calls), the open will fail with a dir on its own with | |
3642 | errno EACCES, so only do a stat to separate a dir from a real EACCES | |
3643 | caused by user perms */ | |
3644 | #ifndef WIN32 | |
c8028aa6 TC |
3645 | st_rc = PerlLIO_stat(p, &st); |
3646 | ||
d1ac83c4 | 3647 | if (st_rc < 0) |
4608196e | 3648 | return NULL; |
d1ac83c4 DD |
3649 | else { |
3650 | int eno; | |
3651 | if(S_ISBLK(st.st_mode)) { | |
3652 | eno = EINVAL; | |
3653 | goto not_file; | |
3654 | } | |
3655 | else if(S_ISDIR(st.st_mode)) { | |
3656 | eno = EISDIR; | |
3657 | not_file: | |
3658 | errno = eno; | |
3659 | return NULL; | |
3660 | } | |
ce8abf5f | 3661 | } |
d345f487 | 3662 | #endif |
ce8abf5f | 3663 | |
d345f487 | 3664 | retio = PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name); |
d345f487 DD |
3665 | #ifdef WIN32 |
3666 | /* EACCES stops the INC search early in pp_require to implement | |
3667 | feature RT #113422 */ | |
3668 | if(!retio && errno == EACCES) { /* exists but probably a directory */ | |
3669 | int eno; | |
3670 | st_rc = PerlLIO_stat(p, &st); | |
3671 | if (st_rc >= 0) { | |
d1ac83c4 DD |
3672 | if(S_ISDIR(st.st_mode)) |
3673 | eno = EISDIR; | |
3674 | else if(S_ISBLK(st.st_mode)) | |
3675 | eno = EINVAL; | |
d345f487 DD |
3676 | else |
3677 | eno = EACCES; | |
3678 | errno = eno; | |
3679 | } | |
3680 | } | |
ccb84406 | 3681 | #endif |
d345f487 | 3682 | return retio; |
ce8abf5f SP |
3683 | } |
3684 | ||
f0dea69c DM |
3685 | /* doopen_pm(): return the equivalent of PerlIO_openn() on the given name, |
3686 | * but first check for bad names (\0) and non-files. | |
3687 | * Also if the filename ends in .pm and unless PERL_DISABLE_PMC, | |
3688 | * try loading Foo.pmc first. | |
3689 | */ | |
75c20bac | 3690 | #ifndef PERL_DISABLE_PMC |
ce8abf5f | 3691 | STATIC PerlIO * |
282b29ee | 3692 | S_doopen_pm(pTHX_ SV *name) |
b295d113 | 3693 | { |
282b29ee NC |
3694 | STRLEN namelen; |
3695 | const char *p = SvPV_const(name, namelen); | |
b295d113 | 3696 | |
7918f24d NC |
3697 | PERL_ARGS_ASSERT_DOOPEN_PM; |
3698 | ||
c8028aa6 TC |
3699 | /* check the name before trying for the .pmc name to avoid the |
3700 | * warning referring to the .pmc which the user probably doesn't | |
3701 | * know or care about | |
3702 | */ | |
41188aa0 | 3703 | if (!IS_SAFE_PATHNAME(p, namelen, "require")) |
c8028aa6 TC |
3704 | return NULL; |
3705 | ||
b80f8424 | 3706 | if (memENDPs(p, namelen, ".pm")) { |
eb70bb4a | 3707 | SV *const pmcsv = sv_newmortal(); |
1e777496 | 3708 | PerlIO * pmcio; |
50b8ed39 | 3709 | |
eb70bb4a | 3710 | SvSetSV_nosteal(pmcsv,name); |
46e2868e | 3711 | sv_catpvs(pmcsv, "c"); |
50b8ed39 | 3712 | |
1e777496 DD |
3713 | pmcio = check_type_and_open(pmcsv); |
3714 | if (pmcio) | |
3715 | return pmcio; | |
a6c40364 | 3716 | } |
282b29ee | 3717 | return check_type_and_open(name); |
75c20bac | 3718 | } |
7925835c | 3719 | #else |
282b29ee | 3720 | # define doopen_pm(name) check_type_and_open(name) |
7925835c | 3721 | #endif /* !PERL_DISABLE_PMC */ |
b295d113 | 3722 | |
f0dea69c DM |
3723 | /* require doesn't search in @INC for absolute names, or when the name is |
3724 | explicitly relative the current directory: i.e. ./, ../ */ | |
511712dc TC |
3725 | PERL_STATIC_INLINE bool |
3726 | S_path_is_searchable(const char *name) | |
3727 | { | |
3728 | PERL_ARGS_ASSERT_PATH_IS_SEARCHABLE; | |
3729 | ||
3730 | if (PERL_FILE_IS_ABSOLUTE(name) | |
3731 | #ifdef WIN32 | |
3732 | || (*name == '.' && ((name[1] == '/' || | |
3733 | (name[1] == '.' && name[2] == '/')) | |
3734 | || (name[1] == '\\' || | |
3735 | ( name[1] == '.' && name[2] == '\\'))) | |
3736 | ) | |
3737 | #else | |
3738 | || (*name == '.' && (name[1] == '/' || | |
3739 | (name[1] == '.' && name[2] == '/'))) | |
3740 | #endif | |
3741 | ) | |
3742 | { | |
3743 | return FALSE; | |
3744 | } | |
3745 | else | |
3746 | return TRUE; | |
3747 | } | |
3748 | ||
b1c05ba5 | 3749 | |
5fb41388 | 3750 | /* implement 'require 5.010001' */ |
b1c05ba5 | 3751 | |
5fb41388 DM |
3752 | static OP * |
3753 | S_require_version(pTHX_ SV *sv) | |
a0d0e21e | 3754 | { |
5fb41388 | 3755 | dVAR; dSP; |
a0d0e21e | 3756 | |
9cdec136 DM |
3757 | sv = sv_2mortal(new_version(sv)); |
3758 | if (!Perl_sv_derived_from_pvn(aTHX_ PL_patchlevel, STR_WITH_LEN("version"), 0)) | |
3759 | upg_version(PL_patchlevel, TRUE); | |
3760 | if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) { | |
3761 | if ( vcmp(sv,PL_patchlevel) <= 0 ) | |
147e3846 | 3762 | DIE(aTHX_ "Perls since %" SVf " too modern--this is %" SVf ", stopped", |
9cdec136 DM |
3763 | SVfARG(sv_2mortal(vnormal(sv))), |
3764 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
3765 | ); | |
3766 | } | |
3767 | else { | |
3768 | if ( vcmp(sv,PL_patchlevel) > 0 ) { | |
3769 | I32 first = 0; | |
3770 | AV *lav; | |
3771 | SV * const req = SvRV(sv); | |
3772 | SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE); | |
3773 | ||
3774 | /* get the left hand term */ | |
3775 | lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE))); | |
3776 | ||
3777 | first = SvIV(*av_fetch(lav,0,0)); | |
3778 | if ( first > (int)PERL_REVISION /* probably 'use 6.0' */ | |
3779 | || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */ | |
3780 | || av_tindex(lav) > 1 /* FP with > 3 digits */ | |
3781 | || strstr(SvPVX(pv),".0") /* FP with leading 0 */ | |
3782 | ) { | |
147e3846 KW |
3783 | DIE(aTHX_ "Perl %" SVf " required--this is only " |
3784 | "%" SVf ", stopped", | |
9cdec136 DM |
3785 | SVfARG(sv_2mortal(vnormal(req))), |
3786 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
3787 | ); | |
3788 | } | |
3789 | else { /* probably 'use 5.10' or 'use 5.8' */ | |
3790 | SV *hintsv; | |
3791 | I32 second = 0; | |
3792 | ||
3793 | if (av_tindex(lav)>=1) | |
3794 | second = SvIV(*av_fetch(lav,1,0)); | |
3795 | ||
3796 | second /= second >= 600 ? 100 : 10; | |
3797 | hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0", | |
3798 | (int)first, (int)second); | |
3799 | upg_version(hintsv, TRUE); | |
3800 | ||
147e3846 KW |
3801 | DIE(aTHX_ "Perl %" SVf " required (did you mean %" SVf "?)" |
3802 | "--this is only %" SVf ", stopped", | |
9cdec136 DM |
3803 | SVfARG(sv_2mortal(vnormal(req))), |
3804 | SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))), | |
3805 | SVfARG(sv_2mortal(vnormal(PL_patchlevel))) | |
3806 | ); | |
3807 | } | |
3808 | } | |
3809 | } | |
d7aa5382 | 3810 | |
9cdec136 | 3811 | RETPUSHYES; |
5fb41388 DM |
3812 | } |
3813 | ||
3814 | /* Handle C<require Foo::Bar>, C<require "Foo/Bar.pm"> and C<do "Foo.pm">. | |
3815 | * The first form will have already been converted at compile time to | |
3816 | * the second form */ | |
3817 | ||
3818 | static OP * | |
2a0461a3 | 3819 | S_require_file(pTHX_ SV *sv) |
5fb41388 DM |
3820 | { |
3821 | dVAR; dSP; | |
3822 | ||
3823 | PERL_CONTEXT *cx; | |
3824 | const char *name; | |
3825 | STRLEN len; | |
3826 | char * unixname; | |
3827 | STRLEN unixlen; | |
3828 | #ifdef VMS | |
3829 | int vms_unixname = 0; | |
3830 | char *unixdir; | |
3831 | #endif | |
f0dea69c DM |
3832 | /* tryname is the actual pathname (with @INC prefix) which was loaded. |
3833 | * It's stored as a value in %INC, and used for error messages */ | |
5fb41388 | 3834 | const char *tryname = NULL; |
f0dea69c | 3835 | SV *namesv = NULL; /* SV equivalent of tryname */ |
5fb41388 DM |
3836 | const U8 gimme = GIMME_V; |
3837 | int filter_has_file = 0; | |
3838 | PerlIO *tryrsfp = NULL; | |
3839 | SV *filter_cache = NULL; | |
3840 | SV *filter_state = NULL; | |
3841 | SV *filter_sub = NULL; | |
3842 | SV *hook_sv = NULL; | |
3843 | OP *op; | |
3844 | int saved_errno; | |
3845 | bool path_searchable; | |
3846 | I32 old_savestack_ix; | |
33fe1955 LM |
3847 | const bool op_is_require = PL_op->op_type == OP_REQUIRE; |
3848 | const char *const op_name = op_is_require ? "require" : "do"; | |
0cbfaef6 | 3849 | SV ** svp_cached = NULL; |
33fe1955 LM |
3850 | |
3851 | assert(op_is_require || PL_op->op_type == OP_DOFILE); | |
5fb41388 | 3852 | |
f04d2c34 | 3853 | if (!SvOK(sv)) |
33fe1955 | 3854 | DIE(aTHX_ "Missing or undefined argument to %s", op_name); |
672794ca | 3855 | name = SvPV_nomg_const(sv, len); |
6132ea6c | 3856 | if (!(name && len > 0 && *name)) |
33fe1955 | 3857 | DIE(aTHX_ "Missing or undefined argument to %s", op_name); |
f04d2c34 | 3858 | |
0cbfaef6 N |
3859 | #ifndef VMS |
3860 | /* try to return earlier (save the SAFE_PATHNAME check) if INC already got the name */ | |
3861 | if (op_is_require) { | |
3862 | /* can optimize to only perform one single lookup */ | |
3863 | svp_cached = hv_fetch(GvHVn(PL_incgv), (char*) name, len, 0); | |
3864 | if ( svp_cached && *svp_cached != &PL_sv_undef ) RETPUSHYES; | |
3865 | } | |
3866 | #endif | |
3867 | ||
33fe1955 LM |
3868 | if (!IS_SAFE_PATHNAME(name, len, op_name)) { |
3869 | if (!op_is_require) { | |
a1b60c8d LM |
3870 | CLEAR_ERRSV(); |
3871 | RETPUSHUNDEF; | |
3872 | } | |
c8028aa6 | 3873 | DIE(aTHX_ "Can't locate %s: %s", |
08f800f8 FC |
3874 | pv_escape(newSVpvs_flags("",SVs_TEMP),name,len,len*2, |
3875 | NULL, SvUTF8(sv)?PERL_PV_ESCAPE_UNI:0), | |
c8028aa6 TC |
3876 | Strerror(ENOENT)); |
3877 | } | |
33fe1955 | 3878 | TAINT_PROPER(op_name); |
4492be7a | 3879 | |
511712dc | 3880 | path_searchable = path_is_searchable(name); |
4492be7a JM |
3881 | |
3882 | #ifdef VMS | |
3883 | /* The key in the %ENV hash is in the syntax of file passed as the argument | |
3884 | * usually this is in UNIX format, but sometimes in VMS format, which | |
3885 | * can result in a module being pulled in more than once. | |
3886 | * To prevent this, the key must be stored in UNIX format if the VMS | |
3887 | * name can be translated to UNIX. | |
3888 | */ | |
155f4c25 | 3889 | |
8de90695 FC |
3890 | if ((unixname = |
3891 | tounixspec(name, SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))) | |
3892 | != NULL) { | |
4492be7a JM |
3893 | unixlen = strlen(unixname); |
3894 | vms_unixname = 1; | |
3895 | } | |
3896 | else | |
3897 | #endif | |
3898 | { | |
3899 | /* if not VMS or VMS name can not be translated to UNIX, pass it | |
3900 | * through. | |
3901 | */ | |
3902 | unixname = (char *) name; | |
3903 | unixlen = len; | |
3904 | } | |
33fe1955 | 3905 | if (op_is_require) { |
0cbfaef6 N |
3906 | /* reuse the previous hv_fetch result if possible */ |
3907 | SV * const * const svp = svp_cached ? svp_cached : hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0); | |
44f8325f AL |
3908 | if ( svp ) { |
3909 | if (*svp != &PL_sv_undef) | |
3910 | RETPUSHYES; | |
3911 | else | |
087b5369 RD |
3912 | DIE(aTHX_ "Attempt to reload %s aborted.\n" |
3913 | "Compilation failed in require", unixname); | |
44f8325f | 3914 | } |
a52f2cce | 3915 | |
f0dea69c | 3916 | /*XXX OPf_KIDS should always be true? -dapm 4/2017 */ |
a52f2cce NC |
3917 | if (PL_op->op_flags & OPf_KIDS) { |
3918 | SVOP * const kid = (SVOP*)cUNOP->op_first; | |
3919 | ||
3920 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
f0dea69c DM |
3921 | /* Make sure that a bareword module name (e.g. ::Foo::Bar) |
3922 | * doesn't map to a naughty pathname like /Foo/Bar.pm. | |
3923 | * Note that the parser will normally detect such errors | |
3924 | * at compile time before we reach here, but | |
3925 | * Perl_load_module() can fake up an identical optree | |
3926 | * without going near the parser, and being able to put | |
3927 | * anything as the bareword. So we include a duplicate set | |
3928 | * of checks here at runtime. | |
3929 | */ | |
a52f2cce NC |
3930 | const STRLEN package_len = len - 3; |
3931 | const char slashdot[2] = {'/', '.'}; | |
3932 | #ifdef DOSISH | |
3933 | const char backslashdot[2] = {'\\', '.'}; | |
3934 | #endif | |
3935 | ||
3936 | /* Disallow *purported* barewords that map to absolute | |
3937 | filenames, filenames relative to the current or parent | |
3938 | directory, or (*nix) hidden filenames. Also sanity check | |
3939 | that the generated filename ends .pm */ | |
3940 | if (!path_searchable || len < 3 || name[0] == '.' | |
b59bf0b2 | 3941 | || !memEQs(name + package_len, len - package_len, ".pm")) |
147e3846 | 3942 | DIE(aTHX_ "Bareword in require maps to disallowed filename \"%" SVf "\"", sv); |
a52f2cce NC |
3943 | if (memchr(name, 0, package_len)) { |
3944 | /* diag_listed_as: Bareword in require contains "%s" */ | |
3945 | DIE(aTHX_ "Bareword in require contains \"\\0\""); | |
3946 | } | |
3947 | if (ninstr(name, name + package_len, slashdot, | |
3948 | slashdot + sizeof(slashdot))) { | |
3949 | /* diag_listed_as: Bareword in require contains "%s" */ | |
3950 | DIE(aTHX_ "Bareword in require contains \"/.\""); | |
3951 | } | |
3952 | #ifdef DOSISH | |
3953 | if (ninstr(name, name + package_len, backslashdot, | |
3954 | backslashdot + sizeof(backslashdot))) { | |
3955 | /* diag_listed_as: Bareword in require contains "%s" */ | |
3956 | DIE(aTHX_ "Bareword in require contains \"\\.\""); | |
3957 | } | |
3958 | #endif | |
3959 | } | |
3960 | } | |
4d8b06f1 | 3961 | } |
a0d0e21e | 3962 | |
3f6bd23a | 3963 | PERL_DTRACE_PROBE_FILE_LOADING(unixname); |
32aeab29 | 3964 | |
f0dea69c | 3965 | /* Try to locate and open a file, possibly using @INC */ |
a0d0e21e | 3966 | |
f0dea69c DM |
3967 | /* with "/foo/bar.pm", "./foo.pm" and "../foo/bar.pm", try to load |
3968 | * the file directly rather than via @INC ... */ | |
511712dc | 3969 | if (!path_searchable) { |
282b29ee | 3970 | /* At this point, name is SvPVX(sv) */ |
46fc3d4c | 3971 | tryname = name; |
282b29ee | 3972 | tryrsfp = doopen_pm(sv); |
bf4acbe4 | 3973 | } |
f0dea69c DM |
3974 | |
3975 | /* ... but if we fail, still search @INC for code references; | |
3976 | * these are applied even on on-searchable paths (except | |
3977 | * if we got EACESS). | |
3978 | * | |
3979 | * For searchable paths, just search @INC normally | |
3980 | */ | |
511712dc | 3981 | if (!tryrsfp && !(errno == EACCES && !path_searchable)) { |
44f8325f | 3982 | AV * const ar = GvAVn(PL_incgv); |
c70927a6 | 3983 | SSize_t i; |
748a9306 | 3984 | #ifdef VMS |
4492be7a | 3985 | if (vms_unixname) |
46fc3d4c | 3986 | #endif |
3987 | { | |
9ffd39ab | 3988 | SV *nsv = sv; |
d0328fd7 | 3989 | namesv = newSV_type(SVt_PV); |
46fc3d4c | 3990 | for (i = 0; i <= AvFILL(ar); i++) { |
df528165 | 3991 | SV * const dirsv = *av_fetch(ar, i, TRUE); |
bbed91b5 | 3992 | |
6567ce24 | 3993 | SvGETMAGIC(dirsv); |
bbed91b5 KF |
3994 | if (SvROK(dirsv)) { |
3995 | int count; | |
a3b58a99 | 3996 | SV **svp; |
bbed91b5 KF |
3997 | SV *loader = dirsv; |
3998 | ||
e14e2dc8 | 3999 | if (SvTYPE(SvRV(loader)) == SVt_PVAV |
6567ce24 | 4000 | && !SvOBJECT(SvRV(loader))) |
e14e2dc8 | 4001 | { |
502c6561 | 4002 | loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE); |
6567ce24 | 4003 | SvGETMAGIC(loader); |
bbed91b5 KF |
4004 | } |
4005 | ||
147e3846 | 4006 | Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%" UVxf "/%s", |
44f0be63 | 4007 | PTR2UV(SvRV(dirsv)), name); |
349d4f2f | 4008 | tryname = SvPVX_const(namesv); |
c445ea15 | 4009 | tryrsfp = NULL; |
bbed91b5 | 4010 | |
9ffd39ab FC |
4011 | if (SvPADTMP(nsv)) { |
4012 | nsv = sv_newmortal(); | |
4013 | SvSetSV_nosteal(nsv,sv); | |
4014 | } | |
901ee108 FC |
4015 | |
4016 | ENTER_with_name("call_INC"); | |
4017 | SAVETMPS; | |
bbed91b5 KF |
4018 | EXTEND(SP, 2); |
4019 | ||
4020 | PUSHMARK(SP); | |
4021 | PUSHs(dirsv); | |
9ffd39ab | 4022 | PUSHs(nsv); |
bbed91b5 | 4023 | PUTBACK; |
6567ce24 FC |
4024 | if (SvGMAGICAL(loader)) { |
4025 | SV *l = sv_newmortal(); | |
4026 | sv_setsv_nomg(l, loader); | |
4027 | loader = l; | |
4028 | } | |
e982885c NC |
4029 | if (sv_isobject(loader)) |
4030 | count = call_method("INC", G_ARRAY); | |
4031 | else | |
4032 | count = call_sv(loader, G_ARRAY); | |
bbed91b5 KF |
4033 | SPAGAIN; |
4034 | ||
4035 | if (count > 0) { | |
4036 | int i = 0; | |
4037 | SV *arg; | |
4038 | ||
4039 | SP -= count - 1; | |
4040 | arg = SP[i++]; | |
4041 | ||
34113e50 NC |
4042 | if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV) |
4043 | && !isGV_with_GP(SvRV(arg))) { | |
4044 | filter_cache = SvRV(arg); | |
34113e50 NC |
4045 | |
4046 | if (i < count) { | |
4047 | arg = SP[i++]; | |
4048 | } | |
4049 | } | |
4050 | ||
6e592b3a | 4051 | if (SvROK(arg) && isGV_with_GP(SvRV(arg))) { |
bbed91b5 KF |
4052 | arg = SvRV(arg); |
4053 | } | |
4054 | ||
6e592b3a | 4055 | if (isGV_with_GP(arg)) { |
159b6efe | 4056 | IO * const io = GvIO((const GV *)arg); |
bbed91b5 KF |
4057 | |
4058 | ++filter_has_file; | |
4059 | ||
4060 | if (io) { | |
4061 | tryrsfp = IoIFP(io); | |
0f7de14d NC |
4062 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { |
4063 | PerlIO_close(IoOFP(io)); | |
bbed91b5 | 4064 | } |
0f7de14d NC |
4065 | IoIFP(io) = NULL; |
4066 | IoOFP(io) = NULL; | |
bbed91b5 KF |
4067 | } |
4068 | ||
4069 | if (i < count) { | |
4070 | arg = SP[i++]; | |
4071 | } | |
4072 | } | |
4073 | ||
4074 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) { | |
4075 | filter_sub = arg; | |
74c765eb | 4076 | SvREFCNT_inc_simple_void_NN(filter_sub); |
bbed91b5 KF |
4077 | |
4078 | if (i < count) { | |
4079 | filter_state = SP[i]; | |
b37c2d43 | 4080 | SvREFCNT_inc_simple_void(filter_state); |
bbed91b5 | 4081 | } |
34113e50 | 4082 | } |
bbed91b5 | 4083 | |
34113e50 NC |
4084 | if (!tryrsfp && (filter_cache || filter_sub)) { |
4085 | tryrsfp = PerlIO_open(BIT_BUCKET, | |
4086 | PERL_SCRIPT_MODE); | |
bbed91b5 | 4087 | } |
1d06aecd | 4088 | SP--; |
bbed91b5 KF |
4089 | } |
4090 | ||
c39fcc09 FC |
4091 | /* FREETMPS may free our filter_cache */ |
4092 | SvREFCNT_inc_simple_void(filter_cache); | |
4093 | ||
bbed91b5 KF |
4094 | PUTBACK; |
4095 | FREETMPS; | |
d343c3ef | 4096 | LEAVE_with_name("call_INC"); |
bbed91b5 | 4097 | |
c39fcc09 FC |
4098 | /* Now re-mortalize it. */ |
4099 | sv_2mortal(filter_cache); | |
4100 | ||
c5f55552 NC |
4101 | /* Adjust file name if the hook has set an %INC entry. |
4102 | This needs to happen after the FREETMPS above. */ | |
4103 | svp = hv_fetch(GvHVn(PL_incgv), name, len, 0); | |
4104 | if (svp) | |
4105 | tryname = SvPV_nolen_const(*svp); | |
4106 | ||
bbed91b5 | 4107 | if (tryrsfp) { |
89ccab8c | 4108 | hook_sv = dirsv; |
bbed91b5 KF |
4109 | break; |
4110 | } | |
4111 | ||
4112 | filter_has_file = 0; | |
9b7d7782 | 4113 | filter_cache = NULL; |
bbed91b5 | 4114 | if (filter_state) { |
762333d9 | 4115 | SvREFCNT_dec_NN(filter_state); |
c445ea15 | 4116 | filter_state = NULL; |
bbed91b5 KF |
4117 | } |
4118 | if (filter_sub) { | |
762333d9 | 4119 | SvREFCNT_dec_NN(filter_sub); |
c445ea15 | 4120 | filter_sub = NULL; |
bbed91b5 KF |
4121 | } |
4122 | } | |
13e8e866 DM |
4123 | else if (path_searchable) { |
4124 | /* match against a plain @INC element (non-searchable | |
4125 | * paths are only matched against refs in @INC) */ | |
b640a14a NC |
4126 | const char *dir; |
4127 | STRLEN dirlen; | |
4128 | ||
4129 | if (SvOK(dirsv)) { | |
6567ce24 | 4130 | dir = SvPV_nomg_const(dirsv, dirlen); |
b640a14a NC |
4131 | } else { |
4132 | dir = ""; | |
4133 | dirlen = 0; | |
4134 | } | |
4135 | ||
33fe1955 | 4136 | if (!IS_SAFE_SYSCALL(dir, dirlen, "@INC entry", op_name)) |
ddc65b67 | 4137 | continue; |
e37778c2 | 4138 | #ifdef VMS |
8de90695 FC |
4139 | if ((unixdir = |
4140 | tounixpath(dir, SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))) | |
4141 | == NULL) | |
bbed91b5 KF |
4142 | continue; |
4143 | sv_setpv(namesv, unixdir); | |
4144 | sv_catpv(namesv, unixname); | |
4fda7c0c | 4145 | #elif defined(__SYMBIAN32__) |
27da23d5 JH |
4146 | if (PL_origfilename[0] && |
4147 | PL_origfilename[1] == ':' && | |
4148 | !(dir[0] && dir[1] == ':')) | |
4149 | Perl_sv_setpvf(aTHX_ namesv, | |
4150 | "%c:%s\\%s", | |
4151 | PL_origfilename[0], | |
4152 | dir, name); | |
4153 | else | |
4154 | Perl_sv_setpvf(aTHX_ namesv, | |
4155 | "%s\\%s", | |
4156 | dir, name); | |
4fda7c0c | 4157 | #else |
b640a14a NC |
4158 | /* The equivalent of |
4159 | Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name); | |
4160 | but without the need to parse the format string, or | |
4161 | call strlen on either pointer, and with the correct | |
4162 | allocation up front. */ | |
4163 | { | |
4164 | char *tmp = SvGROW(namesv, dirlen + len + 2); | |
4165 | ||
4166 | memcpy(tmp, dir, dirlen); | |
4167 | tmp +=dirlen; | |
6b0bdd7f MH |
4168 | |
4169 | /* Avoid '<dir>//<file>' */ | |
4170 | if (!dirlen || *(tmp-1) != '/') { | |
4171 | *tmp++ = '/'; | |
9fdd5a7a MH |
4172 | } else { |
4173 | /* So SvCUR_set reports the correct length below */ | |
4174 | dirlen--; | |
6b0bdd7f MH |
4175 | } |
4176 | ||
b640a14a NC |
4177 | /* name came from an SV, so it will have a '\0' at the |
4178 | end that we can copy as part of this memcpy(). */ | |
4179 | memcpy(tmp, name, len + 1); | |
4180 | ||
4181 | SvCUR_set(namesv, dirlen + len + 1); | |
282b29ee | 4182 | SvPOK_on(namesv); |
b640a14a | 4183 | } |
bf4acbe4 | 4184 | #endif |
33fe1955 | 4185 | TAINT_PROPER(op_name); |
349d4f2f | 4186 | tryname = SvPVX_const(namesv); |
282b29ee | 4187 | tryrsfp = doopen_pm(namesv); |
bbed91b5 | 4188 | if (tryrsfp) { |
e63be746 RGS |
4189 | if (tryname[0] == '.' && tryname[1] == '/') { |
4190 | ++tryname; | |
4910606a | 4191 | while (*++tryname == '/') {} |
e63be746 | 4192 | } |
bbed91b5 KF |
4193 | break; |
4194 | } | |
2433d39e BF |
4195 | else if (errno == EMFILE || errno == EACCES) { |
4196 | /* no point in trying other paths if out of handles; | |
4197 | * on the other hand, if we couldn't open one of the | |
4198 | * files, then going on with the search could lead to | |
4199 | * unexpected results; see perl #113422 | |
4200 | */ | |
4201 | break; | |
4202 | } | |
46fc3d4c | 4203 | } |
a0d0e21e LW |
4204 | } |
4205 | } | |
4206 | } | |
f0dea69c DM |
4207 | |
4208 | /* at this point we've ether opened a file (tryrsfp) or set errno */ | |
4209 | ||
83b195e4 | 4210 | saved_errno = errno; /* sv_2mortal can realloc things */ |
b2ef6d44 | 4211 | sv_2mortal(namesv); |
a0d0e21e | 4212 | if (!tryrsfp) { |
f0dea69c | 4213 | /* we failed; croak if require() or return undef if do() */ |
33fe1955 | 4214 | if (op_is_require) { |
83b195e4 | 4215 | if(saved_errno == EMFILE || saved_errno == EACCES) { |
c9d5e35e | 4216 | /* diag_listed_as: Can't locate %s */ |
e2ce0950 P |
4217 | DIE(aTHX_ "Can't locate %s: %s: %s", |
4218 | name, tryname, Strerror(saved_errno)); | |
e31de809 | 4219 | } else { |
4b62894a | 4220 | if (path_searchable) { /* did we lookup @INC? */ |
44f8325f | 4221 | AV * const ar = GvAVn(PL_incgv); |
c70927a6 | 4222 | SSize_t i; |
1e5f02b3 | 4223 | SV *const msg = newSVpvs_flags("", SVs_TEMP); |
c9d5e35e NC |
4224 | SV *const inc = newSVpvs_flags("", SVs_TEMP); |
4225 | for (i = 0; i <= AvFILL(ar); i++) { | |
4226 | sv_catpvs(inc, " "); | |
4227 | sv_catsv(inc, *av_fetch(ar, i, TRUE)); | |
4228 | } | |
b80f8424 KW |
4229 | if (memENDPs(name, len, ".pm")) { |
4230 | const char *e = name + len - (sizeof(".pm") - 1); | |
d31614f5 DM |
4231 | const char *c; |
4232 | bool utf8 = cBOOL(SvUTF8(sv)); | |
4233 | ||
4234 | /* if the filename, when converted from "Foo/Bar.pm" | |
4235 | * form back to Foo::Bar form, makes a valid | |
4236 | * package name (i.e. parseable by C<require | |
4237 | * Foo::Bar>), then emit a hint. | |
4238 | * | |
4239 | * this loop is modelled after the one in | |
4240 | S_parse_ident */ | |
4241 | c = name; | |
4242 | while (c < e) { | |
4243 | if (utf8 && isIDFIRST_utf8_safe(c, e)) { | |
4244 | c += UTF8SKIP(c); | |
4245 | while (c < e && isIDCONT_utf8_safe( | |
4246 | (const U8*) c, (const U8*) e)) | |
4247 | c += UTF8SKIP(c); | |
4248 | } | |
4249 | else if (isWORDCHAR_A(*c)) { | |
4250 | while (c < e && isWORDCHAR_A(*c)) | |
4251 | c++; | |
4252 | } | |
4253 | else if (*c == '/') | |
4254 | c++; | |
4255 | else | |
4256 | break; | |
4257 | } | |
4258 | ||
4259 | if (c == e && isIDFIRST_lazy_if_safe(name, e, utf8)) { | |
f8db7d5b | 4260 | sv_catpvs(msg, " (you may need to install the "); |
d31614f5 DM |
4261 | for (c = name; c < e; c++) { |
4262 | if (*c == '/') { | |
4263 | sv_catpvs(msg, "::"); | |
4264 | } | |
4265 | else { | |
4266 | sv_catpvn(msg, c, 1); | |
4267 | } | |
4268 | } | |
f8db7d5b | 4269 | sv_catpvs(msg, " module)"); |
d31614f5 | 4270 | } |
f7ee53b5 | 4271 | } |
8a0832a1 | 4272 | else if (memENDs(name, len, ".h")) { |
f8db7d5b | 4273 | sv_catpvs(msg, " (change .h to .ph maybe?) (did you run h2ph?)"); |
f7ee53b5 | 4274 | } |
8a0832a1 | 4275 | else if (memENDs(name, len, ".ph")) { |
f8db7d5b | 4276 | sv_catpvs(msg, " (did you run h2ph?)"); |
f7ee53b5 | 4277 | } |
c9d5e35e NC |
4278 | |
4279 | /* diag_listed_as: Can't locate %s */ | |
4280 | DIE(aTHX_ | |
f7ee53b5 PJ |
4281 | "Can't locate %s in @INC%" SVf " (@INC contains:%" SVf ")", |
4282 | name, msg, inc); | |
c9d5e35e | 4283 | } |
2683423c | 4284 | } |
c9d5e35e | 4285 | DIE(aTHX_ "Can't locate %s", name); |
a0d0e21e | 4286 | } |
2a0461a3 TC |
4287 | else { |
4288 | #ifdef DEFAULT_INC_EXCLUDES_DOT | |
4289 | Stat_t st; | |
4290 | PerlIO *io = NULL; | |
4291 | dSAVE_ERRNO; | |
f0dea69c DM |
4292 | /* the complication is to match the logic from doopen_pm() so |
4293 | * we don't treat do "sda1" as a previously successful "do". | |
2a0461a3 TC |
4294 | */ |
4295 | bool do_warn = namesv && ckWARN_d(WARN_DEPRECATED) | |
4296 | && PerlLIO_stat(name, &st) == 0 && !S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode) | |
4297 | && (io = PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &sv)) != NULL; | |
4298 | if (io) | |
4299 | PerlIO_close(io); | |
4300 | ||
4301 | RESTORE_ERRNO; | |
4302 | if (do_warn) { | |
1c99110e DM |
4303 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), |
4304 | "do \"%s\" failed, '.' is no longer in @INC; " | |
4305 | "did you mean do \"./%s\"?", | |
4306 | name, name); | |
2a0461a3 TC |
4307 | } |
4308 | #endif | |
4309 | CLEAR_ERRSV(); | |
4310 | RETPUSHUNDEF; | |
4311 | } | |
a0d0e21e | 4312 | } |
d8bfb8bd | 4313 | else |
93189314 | 4314 | SETERRNO(0, SS_NORMAL); |
a0d0e21e | 4315 | |
f0dea69c | 4316 | /* Update %INC. Assume success here to prevent recursive requirement. */ |
238d24b4 | 4317 | /* name is never assigned to again, so len is still strlen(name) */ |
d3a4e64e | 4318 | /* Check whether a hook in @INC has already filled %INC */ |
44f8325f | 4319 | if (!hook_sv) { |
4492be7a | 4320 | (void)hv_store(GvHVn(PL_incgv), |
b2ef6d44 | 4321 | unixname, unixlen, newSVpv(tryname,0),0); |
44f8325f | 4322 | } else { |
4492be7a | 4323 | SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0); |
44f8325f | 4324 | if (!svp) |
4492be7a JM |
4325 | (void)hv_store(GvHVn(PL_incgv), |
4326 | unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 ); | |
d3a4e64e | 4327 | } |
a0d0e21e | 4328 | |
f0dea69c DM |
4329 | /* Now parse the file */ |
4330 | ||
adcbf118 | 4331 | old_savestack_ix = PL_savestack_ix; |
b2ef6d44 FC |
4332 | SAVECOPFILE_FREE(&PL_compiling); |
4333 | CopFILE_set(&PL_compiling, tryname); | |
8eaa0acf | 4334 | lex_start(NULL, tryrsfp, 0); |
e50aee73 | 4335 | |
34113e50 | 4336 | if (filter_sub || filter_cache) { |
4464f08e NC |
4337 | /* We can use the SvPV of the filter PVIO itself as our cache, rather |
4338 | than hanging another SV from it. In turn, filter_add() optionally | |
4339 | takes the SV to use as the filter (or creates a new SV if passed | |
4340 | NULL), so simply pass in whatever value filter_cache has. */ | |
9b7d7782 FC |
4341 | SV * const fc = filter_cache ? newSV(0) : NULL; |
4342 | SV *datasv; | |
4343 | if (fc) sv_copypv(fc, filter_cache); | |
4344 | datasv = filter_add(S_run_user_filter, fc); | |
bbed91b5 | 4345 | IoLINES(datasv) = filter_has_file; |
159b6efe NC |
4346 | IoTOP_GV(datasv) = MUTABLE_GV(filter_state); |
4347 | IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub); | |
bbed91b5 KF |
4348 | } |
4349 | ||
4350 | /* switch to eval mode */ | |
d7e3f70f | 4351 | assert(!CATCH_GET); |
ed8ff0f3 | 4352 | cx = cx_pushblock(CXt_EVAL, gimme, SP, old_savestack_ix); |
13febba5 | 4353 | cx_pusheval(cx, PL_op->op_next, newSVpv(name, 0)); |
a0d0e21e | 4354 | |
57843af0 GS |
4355 | SAVECOPLINE(&PL_compiling); |
4356 | CopLINE_set(&PL_compiling, 0); | |
a0d0e21e LW |
4357 | |
4358 | PUTBACK; | |
6ec9efec | 4359 | |
9aba0c93 | 4360 | if (doeval_compile(gimme, NULL, PL_curcop->cop_seq, NULL)) |
d7e3f70f | 4361 | op = PL_eval_start; |
410be5db DM |
4362 | else |
4363 | op = PL_op->op_next; | |
bfed75c6 | 4364 | |
3f6bd23a | 4365 | PERL_DTRACE_PROBE_FILE_LOADED(unixname); |
32aeab29 | 4366 | |
6ec9efec | 4367 | return op; |
a0d0e21e LW |
4368 | } |
4369 | ||
5fb41388 DM |
4370 | |
4371 | /* also used for: pp_dofile() */ | |
4372 | ||
4373 | PP(pp_require) | |
4374 | { | |
d7e3f70f Z |
4375 | RUN_PP_CATCHABLY(Perl_pp_require); |
4376 | ||
4377 | { | |
4378 | dSP; | |
4379 | SV *sv = POPs; | |
4380 | SvGETMAGIC(sv); | |
4381 | PUTBACK; | |
4382 | return ((SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) | |
4383 | ? S_require_version(aTHX_ sv) | |
4384 | : S_require_file(aTHX_ sv); | |
4385 | } | |
5fb41388 DM |
4386 | } |
4387 | ||
4388 | ||
996c9baa VP |
4389 | /* This is a op added to hold the hints hash for |
4390 | pp_entereval. The hash can be modified by the code | |
4391 | being eval'ed, so we return a copy instead. */ | |
4392 | ||
4393 | PP(pp_hintseval) | |
4394 | { | |
996c9baa | 4395 | dSP; |
defdfed5 | 4396 | mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv)))); |
996c9baa VP |
4397 | RETURN; |
4398 | } | |
4399 | ||
4400 | ||
a0d0e21e LW |
4401 | PP(pp_entereval) |
4402 | { | |
20b7effb | 4403 | dSP; |
eb578fdb | 4404 | PERL_CONTEXT *cx; |
0d863452 | 4405 | SV *sv; |
d7e3f70f Z |
4406 | U8 gimme; |
4407 | U32 was; | |
83ee9e09 | 4408 | char tbuf[TYPE_DIGITS(long) + 12]; |
d7e3f70f Z |
4409 | bool saved_delete; |
4410 | char *tmpbuf; | |
a0d0e21e | 4411 | STRLEN len; |
a3985cdc | 4412 | CV* runcv; |
d7e3f70f Z |
4413 | U32 seq, lex_flags; |
4414 | HV *saved_hh; | |
4415 | bool bytes; | |
adcbf118 | 4416 | I32 old_savestack_ix; |
e389bba9 | 4417 | |
d7e3f70f Z |
4418 | RUN_PP_CATCHABLY(Perl_pp_entereval); |
4419 | ||
4420 | gimme = GIMME_V; | |
4421 | was = PL_breakable_sub_gen; | |
4422 | saved_delete = FALSE; | |
4423 | tmpbuf = tbuf; | |
4424 | lex_flags = 0; | |
4425 | saved_hh = NULL; | |
4426 | bytes = PL_op->op_private & OPpEVAL_BYTES; | |
4427 | ||
0d863452 | 4428 | if (PL_op->op_private & OPpEVAL_HAS_HH) { |
85fbaab2 | 4429 | saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs)); |
0d863452 | 4430 | } |
bc344123 FC |
4431 | else if (PL_hints & HINT_LOCALIZE_HH || ( |
4432 | PL_op->op_private & OPpEVAL_COPHH | |
4433 | && PL_curcop->cop_hints & HINT_LOCALIZE_HH | |
4434 | )) { | |
7d789282 FC |
4435 | saved_hh = cop_hints_2hv(PL_curcop, 0); |
4436 | hv_magic(saved_hh, NULL, PERL_MAGIC_hints); | |
4437 | } | |
0d863452 | 4438 | sv = POPs; |
895b760f DM |
4439 | if (!SvPOK(sv)) { |
4440 | /* make sure we've got a plain PV (no overload etc) before testing | |
4441 | * for taint. Making a copy here is probably overkill, but better | |
4442 | * safe than sorry */ | |
0479a84a NC |
4443 | STRLEN len; |
4444 | const char * const p = SvPV_const(sv, len); | |
4445 | ||
4446 | sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv)); | |
0abcdfa4 | 4447 | lex_flags |= LEX_START_COPIED; |
7d789282 | 4448 | |
60d63348 | 4449 | if (bytes && SvUTF8(sv)) |
7d789282 FC |
4450 | SvPVbyte_force(sv, len); |
4451 | } | |
60d63348 | 4452 | else if (bytes && SvUTF8(sv)) { |
e1fa07e3 | 4453 | /* Don't modify someone else's scalar */ |
7d789282 FC |
4454 | STRLEN len; |
4455 | sv = newSVsv(sv); | |
5cefc8c1 | 4456 | (void)sv_2mortal(sv); |
7d789282 | 4457 | SvPVbyte_force(sv,len); |
0abcdfa4 | 4458 | lex_flags |= LEX_START_COPIED; |
895b760f | 4459 | } |
a0d0e21e | 4460 | |
af2d3def | 4461 | TAINT_IF(SvTAINTED(sv)); |
748a9306 | 4462 | TAINT_PROPER("eval"); |
a0d0e21e | 4463 | |
adcbf118 DM |
4464 | old_savestack_ix = PL_savestack_ix; |
4465 | ||
0abcdfa4 | 4466 | lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE |
60d63348 FC |
4467 | ? LEX_IGNORE_UTF8_HINTS |
4468 | : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER | |
0abcdfa4 | 4469 | ) |
60d63348 | 4470 | ); |
ac27b0f5 | 4471 | |
a0d0e21e LW |
4472 | /* switch to eval mode */ |
4473 | ||
83ee9e09 | 4474 | if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) { |
8b38226b | 4475 | SV * const temp_sv = sv_newmortal(); |
147e3846 | 4476 | Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%" IVdf "]", |
83ee9e09 GS |
4477 | (unsigned long)++PL_evalseq, |
4478 | CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); | |
8b38226b AL |
4479 | tmpbuf = SvPVX(temp_sv); |
4480 | len = SvCUR(temp_sv); | |
83ee9e09 GS |
4481 | } |
4482 | else | |
d9fad198 | 4483 | len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq); |
f4dd75d9 | 4484 | SAVECOPFILE_FREE(&PL_compiling); |
57843af0 | 4485 | CopFILE_set(&PL_compiling, tmpbuf+2); |
f4dd75d9 | 4486 | SAVECOPLINE(&PL_compiling); |
57843af0 | 4487 | CopLINE_set(&PL_compiling, 1); |
d819b83a DM |
4488 | /* special case: an eval '' executed within the DB package gets lexically |
4489 | * placed in the first non-DB CV rather than the current CV - this | |
4490 | * allows the debugger to execute code, find lexicals etc, in the | |
4491 | * scope of the code being debugged. Passing &seq gets find_runcv | |
4492 | * to do the dirty work for us */ | |
4493 | runcv = find_runcv(&seq); | |
a0d0e21e | 4494 | |
d7e3f70f | 4495 | assert(!CATCH_GET); |
ed8ff0f3 | 4496 | cx = cx_pushblock((CXt_EVAL|CXp_REAL), gimme, SP, old_savestack_ix); |
13febba5 | 4497 | cx_pusheval(cx, PL_op->op_next, NULL); |
a0d0e21e LW |
4498 | |
4499 | /* prepare to compile string */ | |
4500 | ||
c7a622b3 | 4501 | if (PERLDB_LINE_OR_SAVESRC && PL_curstash != PL_debstash) |
bdc0bf6f | 4502 | save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr); |
78da7625 | 4503 | else { |
c8cb8d55 FC |
4504 | /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up |
4505 | deleting the eval's FILEGV from the stash before gv_check() runs | |
4506 | (i.e. before run-time proper). To work around the coredump that | |
4507 | ensues, we always turn GvMULTI_on for any globals that were | |
4508 | introduced within evals. See force_ident(). GSAR 96-10-12 */ | |
78da7625 FC |
4509 | char *const safestr = savepvn(tmpbuf, len); |
4510 | SAVEDELETE(PL_defstash, safestr, len); | |
4511 | saved_delete = TRUE; | |
4512 | } | |
4513 | ||
a0d0e21e | 4514 | PUTBACK; |
f9bddea7 | 4515 | |
9aba0c93 | 4516 | if (doeval_compile(gimme, runcv, seq, saved_hh)) { |
f9bddea7 | 4517 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ |
c7a622b3 | 4518 | ? PERLDB_LINE_OR_SAVESRC |
f9bddea7 NC |
4519 | : PERLDB_SAVESRC_NOSUBS) { |
4520 | /* Retain the filegv we created. */ | |
78da7625 | 4521 | } else if (!saved_delete) { |
f9bddea7 NC |
4522 | char *const safestr = savepvn(tmpbuf, len); |
4523 | SAVEDELETE(PL_defstash, safestr, len); | |
4524 | } | |
d7e3f70f | 4525 | return PL_eval_start; |
f9bddea7 | 4526 | } else { |
486ec47a | 4527 | /* We have already left the scope set up earlier thanks to the LEAVE |
9aba0c93 | 4528 | in doeval_compile(). */ |
eb044b10 | 4529 | if (was != PL_breakable_sub_gen /* Some subs defined here. */ |
c7a622b3 | 4530 | ? PERLDB_LINE_OR_SAVESRC |
eb044b10 | 4531 | : PERLDB_SAVESRC_INVALID) { |
f9bddea7 | 4532 | /* Retain the filegv we created. */ |
7857f360 | 4533 | } else if (!saved_delete) { |
f9bddea7 NC |
4534 | (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD); |
4535 | } | |
4536 | return PL_op->op_next; | |
4537 | } | |
a0d0e21e LW |
4538 | } |
4539 | ||
c349b9a0 DM |
4540 | |
4541 | /* also tail-called by pp_return */ | |
4542 | ||
a0d0e21e LW |
4543 | PP(pp_leaveeval) |
4544 | { | |
f5ddd604 | 4545 | SV **oldsp; |
1c23e2bd | 4546 | U8 gimme; |
eb578fdb | 4547 | PERL_CONTEXT *cx; |
a0d0e21e | 4548 | OP *retop; |
06a7bc17 | 4549 | int failed; |
676a678a | 4550 | CV *evalcv; |
06a7bc17 | 4551 | bool keep; |
a0d0e21e | 4552 | |
011c3814 | 4553 | PERL_ASYNC_CHECK(); |
61d3b95a | 4554 | |
4ebe6e95 | 4555 | cx = CX_CUR(); |
61d3b95a | 4556 | assert(CxTYPE(cx) == CXt_EVAL); |
2aabfe8a | 4557 | |
f5ddd604 | 4558 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a DM |
4559 | gimme = cx->blk_gimme; |
4560 | ||
2aabfe8a | 4561 | /* did require return a false value? */ |
06a7bc17 DM |
4562 | failed = CxOLD_OP_TYPE(cx) == OP_REQUIRE |
4563 | && !(gimme == G_SCALAR | |
f4c975aa | 4564 | ? SvTRUE_NN(*PL_stack_sp) |
06a7bc17 | 4565 | : PL_stack_sp > oldsp); |
2aabfe8a | 4566 | |
b66d79a6 | 4567 | if (gimme == G_VOID) { |
f5ddd604 | 4568 | PL_stack_sp = oldsp; |
b66d79a6 DM |
4569 | /* free now to avoid late-called destructors clobbering $@ */ |
4570 | FREETMPS; | |
4571 | } | |
2aabfe8a | 4572 | else |
f5ddd604 | 4573 | leave_adjust_stacks(oldsp, oldsp, gimme, 0); |
2aabfe8a | 4574 | |
13febba5 | 4575 | /* the cx_popeval does a leavescope, which frees the optree associated |
4df352a8 DM |
4576 | * with eval, which if it frees the nextstate associated with |
4577 | * PL_curcop, sets PL_curcop to NULL. Which can mess up freeing a | |
4578 | * regex when running under 'use re Debug' because it needs PL_curcop | |
4579 | * to get the current hints. So restore it early. | |
4580 | */ | |
4581 | PL_curcop = cx->blk_oldcop; | |
2aabfe8a | 4582 | |
06a7bc17 DM |
4583 | /* grab this value before cx_popeval restores the old PL_in_eval */ |
4584 | keep = cBOOL(PL_in_eval & EVAL_KEEPERR); | |
f39bc417 | 4585 | retop = cx->blk_eval.retop; |
676a678a | 4586 | evalcv = cx->blk_eval.cv; |
4fdae800 | 4587 | #ifdef DEBUGGING |
676a678a | 4588 | assert(CvDEPTH(evalcv) == 1); |
4fdae800 | 4589 | #endif |
676a678a | 4590 | CvDEPTH(evalcv) = 0; |
4fdae800 | 4591 | |
06a7bc17 DM |
4592 | /* pop the CXt_EVAL, and if a require failed, croak */ |
4593 | S_pop_eval_context_maybe_croak(aTHX_ cx, NULL, failed); | |
a0d0e21e | 4594 | |
d308a779 DM |
4595 | if (!keep) |
4596 | CLEAR_ERRSV(); | |
4597 | ||
2aabfe8a | 4598 | return retop; |
a0d0e21e LW |
4599 | } |
4600 | ||
edb2152a NC |
4601 | /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it |
4602 | close to the related Perl_create_eval_scope. */ | |
4603 | void | |
4604 | Perl_delete_eval_scope(pTHX) | |
a0d0e21e | 4605 | { |
eb578fdb | 4606 | PERL_CONTEXT *cx; |
edb2152a | 4607 | |
4ebe6e95 | 4608 | cx = CX_CUR(); |
2f450c1b | 4609 | CX_LEAVE_SCOPE(cx); |
13febba5 | 4610 | cx_popeval(cx); |
ed8ff0f3 | 4611 | cx_popblock(cx); |
5da525e9 | 4612 | CX_POP(cx); |
edb2152a | 4613 | } |
a0d0e21e | 4614 | |
edb2152a NC |
4615 | /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was |
4616 | also needed by Perl_fold_constants. */ | |
274ed8ae DM |
4617 | void |
4618 | Perl_create_eval_scope(pTHX_ OP *retop, U32 flags) | |
edb2152a NC |
4619 | { |
4620 | PERL_CONTEXT *cx; | |
1c23e2bd | 4621 | const U8 gimme = GIMME_V; |
edb2152a | 4622 | |
ed8ff0f3 | 4623 | cx = cx_pushblock((CXt_EVAL|CXp_TRYBLOCK), gimme, |
490576d1 | 4624 | PL_stack_sp, PL_savestack_ix); |
13febba5 | 4625 | cx_pusheval(cx, retop, NULL); |
a0d0e21e | 4626 | |
faef0170 | 4627 | PL_in_eval = EVAL_INEVAL; |
edb2152a NC |
4628 | if (flags & G_KEEPERR) |
4629 | PL_in_eval |= EVAL_KEEPERR; | |
ab69dbc2 RGS |
4630 | else |
4631 | CLEAR_ERRSV(); | |
edb2152a NC |
4632 | if (flags & G_FAKINGEVAL) { |
4633 | PL_eval_root = PL_op; /* Only needed so that goto works right. */ | |
4634 | } | |
edb2152a NC |
4635 | } |
4636 | ||
4637 | PP(pp_entertry) | |
4638 | { | |
d7e3f70f Z |
4639 | RUN_PP_CATCHABLY(Perl_pp_entertry); |
4640 | ||
4641 | assert(!CATCH_GET); | |
274ed8ae | 4642 | create_eval_scope(cLOGOP->op_other->op_next, 0); |
d7e3f70f | 4643 | return PL_op->op_next; |
a0d0e21e LW |
4644 | } |
4645 | ||
c349b9a0 DM |
4646 | |
4647 | /* also tail-called by pp_return */ | |
4648 | ||
a0d0e21e LW |
4649 | PP(pp_leavetry) |
4650 | { | |
f5ddd604 | 4651 | SV **oldsp; |
1c23e2bd | 4652 | U8 gimme; |
eb578fdb | 4653 | PERL_CONTEXT *cx; |
334ea179 | 4654 | OP *retop; |
a0d0e21e | 4655 | |
011c3814 | 4656 | PERL_ASYNC_CHECK(); |
61d3b95a | 4657 | |
4ebe6e95 | 4658 | cx = CX_CUR(); |
61d3b95a | 4659 | assert(CxTYPE(cx) == CXt_EVAL); |
f5ddd604 | 4660 | oldsp = PL_stack_base + cx->blk_oldsp; |
61d3b95a DM |
4661 | gimme = cx->blk_gimme; |
4662 | ||
b66d79a6 | 4663 | if (gimme == G_VOID) { |
f5ddd604 | 4664 | PL_stack_sp = oldsp; |
b66d79a6 DM |
4665 | /* free now to avoid late-called destructors clobbering $@ */ |
4666 | FREETMPS; | |
4667 | } | |
0663a8f8 | 4668 | else |
f5ddd604 | 4669 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); |
2f450c1b | 4670 | CX_LEAVE_SCOPE(cx); |
13febba5 | 4671 | cx_popeval(cx); |
ed8ff0f3 | 4672 | cx_popblock(cx); |
61d3b95a | 4673 | retop = cx->blk_eval.retop; |
5da525e9 | 4674 | CX_POP(cx); |
67f63db7 | 4675 | |
ab69dbc2 | 4676 | CLEAR_ERRSV(); |
0663a8f8 | 4677 | return retop; |
a0d0e21e LW |
4678 | } |
4679 | ||
0d863452 RH |
4680 | PP(pp_entergiven) |
4681 | { | |
20b7effb | 4682 | dSP; |
eb578fdb | 4683 | PERL_CONTEXT *cx; |
1c23e2bd | 4684 | const U8 gimme = GIMME_V; |
b95eccd3 DM |
4685 | SV *origsv = DEFSV; |
4686 | SV *newsv = POPs; | |
0d863452 | 4687 | |
5d051ee0 | 4688 | assert(!PL_op->op_targ); /* used to be set for lexical $_ */ |
b95eccd3 | 4689 | GvSV(PL_defgv) = SvREFCNT_inc(newsv); |
0d863452 | 4690 | |
7896dde7 Z |
4691 | cx = cx_pushblock(CXt_GIVEN, gimme, SP, PL_savestack_ix); |
4692 | cx_pushgiven(cx, origsv); | |
0d863452 RH |
4693 | |
4694 | RETURN; | |
4695 | } | |
4696 | ||
7896dde7 Z |
4697 | PP(pp_leavegiven) |
4698 | { | |
4699 | PERL_CONTEXT *cx; | |
4700 | U8 gimme; | |
4701 | SV **oldsp; | |
4702 | PERL_UNUSED_CONTEXT; | |
4703 | ||
4704 | cx = CX_CUR(); | |
4705 | assert(CxTYPE(cx) == CXt_GIVEN); | |
4706 | oldsp = PL_stack_base + cx->blk_oldsp; | |
4707 | gimme = cx->blk_gimme; | |
4708 | ||
4709 | if (gimme == G_VOID) | |
4710 | PL_stack_sp = oldsp; | |
4711 | else | |
4712 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); | |
4713 | ||
4714 | CX_LEAVE_SCOPE(cx); | |
4715 | cx_popgiven(cx); | |
4716 | cx_popblock(cx); | |
4717 | CX_POP(cx); | |
4718 | ||
4719 | return NORMAL; | |
4720 | } | |
4721 | ||
4722 | /* Helper routines used by pp_smartmatch */ | |
4723 | STATIC PMOP * | |
4724 | S_make_matcher(pTHX_ REGEXP *re) | |
4725 | { | |
4726 | PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED); | |
4727 | ||
4728 | PERL_ARGS_ASSERT_MAKE_MATCHER; | |
4729 | ||
4730 | PM_SETRE(matcher, ReREFCNT_inc(re)); | |
4731 | ||
4732 | SAVEFREEOP((OP *) matcher); | |
4733 | ENTER_with_name("matcher"); SAVETMPS; | |
4734 | SAVEOP(); | |
4735 | return matcher; | |
4736 | } | |
4737 | ||
4738 | STATIC bool | |
4739 | S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv) | |
4740 | { | |
4741 | dSP; | |
4742 | bool result; | |
4743 | ||
4744 | PERL_ARGS_ASSERT_MATCHER_MATCHES_SV; | |
4745 | ||
4746 | PL_op = (OP *) matcher; | |
4747 | XPUSHs(sv); | |
4748 | PUTBACK; | |
4749 | (void) Perl_pp_match(aTHX); | |
4750 | SPAGAIN; | |
4751 | result = SvTRUEx(POPs); | |
4752 | PUTBACK; | |
4753 | ||
4754 | return result; | |
4755 | } | |
4756 | ||
4757 | STATIC void | |
4758 | S_destroy_matcher(pTHX_ PMOP *matcher) | |
4759 | { | |
4760 | PERL_ARGS_ASSERT_DESTROY_MATCHER; | |
4761 | PERL_UNUSED_ARG(matcher); | |
4762 | ||
4763 | FREETMPS; | |
4764 | LEAVE_with_name("matcher"); | |
4765 | } | |
4766 | ||
4767 | /* Do a smart match */ | |
0d863452 RH |
4768 | PP(pp_smartmatch) |
4769 | { | |
7896dde7 Z |
4770 | DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n")); |
4771 | return do_smartmatch(NULL, NULL, 0); | |
4772 | } | |
4773 | ||
4774 | /* This version of do_smartmatch() implements the | |
4775 | * table of smart matches that is found in perlsyn. | |
4776 | */ | |
4777 | STATIC OP * | |
4778 | S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied) | |
4779 | { | |
0d863452 | 4780 | dSP; |
7896dde7 Z |
4781 | |
4782 | bool object_on_left = FALSE; | |
4783 | SV *e = TOPs; /* e is for 'expression' */ | |
4784 | SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */ | |
4785 | ||
4786 | /* Take care only to invoke mg_get() once for each argument. | |
4787 | * Currently we do this by copying the SV if it's magical. */ | |
4788 | if (d) { | |
4789 | if (!copied && SvGMAGICAL(d)) | |
4790 | d = sv_mortalcopy(d); | |
4791 | } | |
4792 | else | |
4793 | d = &PL_sv_undef; | |
6f1401dc | 4794 | |
7896dde7 Z |
4795 | assert(e); |
4796 | if (SvGMAGICAL(e)) | |
4797 | e = sv_mortalcopy(e); | |
4798 | ||
4799 | /* First of all, handle overload magic of the rightmost argument */ | |
4800 | if (SvAMAGIC(e)) { | |
4801 | SV * tmpsv; | |
4802 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); | |
4803 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); | |
4804 | ||
4805 | tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft); | |
4806 | if (tmpsv) { | |
4807 | SPAGAIN; | |
4808 | (void)POPs; | |
4809 | SETs(tmpsv); | |
4810 | RETURN; | |
4811 | } | |
4812 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n")); | |
4813 | } | |
4814 | ||
4815 | SP -= 2; /* Pop the values */ | |
e8fe1b7c | 4816 | PUTBACK; |
7896dde7 Z |
4817 | |
4818 | /* ~~ undef */ | |
4819 | if (!SvOK(e)) { | |
4820 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n")); | |
4821 | if (SvOK(d)) | |
4822 | RETPUSHNO; | |
4823 | else | |
4824 | RETPUSHYES; | |
4825 | } | |
4826 | ||
4827 | if (SvROK(e) && SvOBJECT(SvRV(e)) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) { | |
4828 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n")); | |
4829 | Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation"); | |
4830 | } | |
4831 | if (SvROK(d) && SvOBJECT(SvRV(d)) && (SvTYPE(SvRV(d)) != SVt_REGEXP)) | |
4832 | object_on_left = TRUE; | |
4833 | ||
4834 | /* ~~ sub */ | |
4835 | if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) { | |
4836 | I32 c; | |
4837 | if (object_on_left) { | |
4838 | goto sm_any_sub; /* Treat objects like scalars */ | |
4839 | } | |
4840 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
4841 | /* Test sub truth for each key */ | |
4842 | HE *he; | |
4843 | bool andedresults = TRUE; | |
4844 | HV *hv = (HV*) SvRV(d); | |
4845 | I32 numkeys = hv_iterinit(hv); | |
4846 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n")); | |
4847 | if (numkeys == 0) | |
4848 | RETPUSHYES; | |
4849 | while ( (he = hv_iternext(hv)) ) { | |
4850 | DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n")); | |
4851 | ENTER_with_name("smartmatch_hash_key_test"); | |
4852 | SAVETMPS; | |
4853 | PUSHMARK(SP); | |
4854 | PUSHs(hv_iterkeysv(he)); | |
4855 | PUTBACK; | |
4856 | c = call_sv(e, G_SCALAR); | |
4857 | SPAGAIN; | |
4858 | if (c == 0) | |
4859 | andedresults = FALSE; | |
4860 | else | |
4861 | andedresults = SvTRUEx(POPs) && andedresults; | |
4862 | FREETMPS; | |
4863 | LEAVE_with_name("smartmatch_hash_key_test"); | |
4864 | } | |
4865 | if (andedresults) | |
4866 | RETPUSHYES; | |
4867 | else | |
4868 | RETPUSHNO; | |
4869 | } | |
4870 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
4871 | /* Test sub truth for each element */ | |
4872 | SSize_t i; | |
4873 | bool andedresults = TRUE; | |
4874 | AV *av = (AV*) SvRV(d); | |
4875 | const I32 len = av_tindex(av); | |
4876 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n")); | |
4877 | if (len == -1) | |
4878 | RETPUSHYES; | |
4879 | for (i = 0; i <= len; ++i) { | |
4880 | SV * const * const svp = av_fetch(av, i, FALSE); | |
4881 | DEBUG_M(Perl_deb(aTHX_ " testing array element...\n")); | |
4882 | ENTER_with_name("smartmatch_array_elem_test"); | |
4883 | SAVETMPS; | |
4884 | PUSHMARK(SP); | |
4885 | if (svp) | |
4886 | PUSHs(*svp); | |
4887 | PUTBACK; | |
4888 | c = call_sv(e, G_SCALAR); | |
4889 | SPAGAIN; | |
4890 | if (c == 0) | |
4891 | andedresults = FALSE; | |
4892 | else | |
4893 | andedresults = SvTRUEx(POPs) && andedresults; | |
4894 | FREETMPS; | |
4895 | LEAVE_with_name("smartmatch_array_elem_test"); | |
4896 | } | |
4897 | if (andedresults) | |
4898 | RETPUSHYES; | |
4899 | else | |
4900 | RETPUSHNO; | |
4901 | } | |
4902 | else { | |
4903 | sm_any_sub: | |
4904 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n")); | |
4905 | ENTER_with_name("smartmatch_coderef"); | |
4906 | SAVETMPS; | |
4907 | PUSHMARK(SP); | |
4908 | PUSHs(d); | |
4909 | PUTBACK; | |
4910 | c = call_sv(e, G_SCALAR); | |
4911 | SPAGAIN; | |
4912 | if (c == 0) | |
4913 | PUSHs(&PL_sv_no); | |
4914 | else if (SvTEMP(TOPs)) | |
4915 | SvREFCNT_inc_void(TOPs); | |
4916 | FREETMPS; | |
4917 | LEAVE_with_name("smartmatch_coderef"); | |
4918 | RETURN; | |
4919 | } | |
4920 | } | |
4921 | /* ~~ %hash */ | |
4922 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { | |
4923 | if (object_on_left) { | |
4924 | goto sm_any_hash; /* Treat objects like scalars */ | |
4925 | } | |
4926 | else if (!SvOK(d)) { | |
4927 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n")); | |
4928 | RETPUSHNO; | |
4929 | } | |
4930 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
4931 | /* Check that the key-sets are identical */ | |
4932 | HE *he; | |
4933 | HV *other_hv = MUTABLE_HV(SvRV(d)); | |
4934 | bool tied; | |
4935 | bool other_tied; | |
4936 | U32 this_key_count = 0, | |
4937 | other_key_count = 0; | |
4938 | HV *hv = MUTABLE_HV(SvRV(e)); | |
4939 | ||
4940 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n")); | |
4941 | /* Tied hashes don't know how many keys they have. */ | |
4942 | tied = cBOOL(SvTIED_mg((SV*)hv, PERL_MAGIC_tied)); | |
4943 | other_tied = cBOOL(SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)); | |
4944 | if (!tied ) { | |
4945 | if(other_tied) { | |
4946 | /* swap HV sides */ | |
4947 | HV * const temp = other_hv; | |
4948 | other_hv = hv; | |
4949 | hv = temp; | |
4950 | tied = TRUE; | |
4951 | other_tied = FALSE; | |
4952 | } | |
4953 | else if(HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv)) | |
4954 | RETPUSHNO; | |
4955 | } | |
4956 | ||
4957 | /* The hashes have the same number of keys, so it suffices | |
4958 | to check that one is a subset of the other. */ | |
4959 | (void) hv_iterinit(hv); | |
4960 | while ( (he = hv_iternext(hv)) ) { | |
4961 | SV *key = hv_iterkeysv(he); | |
4962 | ||
4963 | DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n")); | |
4964 | ++ this_key_count; | |
4965 | ||
4966 | if(!hv_exists_ent(other_hv, key, 0)) { | |
4967 | (void) hv_iterinit(hv); /* reset iterator */ | |
4968 | RETPUSHNO; | |
4969 | } | |
4970 | } | |
4971 | ||
4972 | if (other_tied) { | |
4973 | (void) hv_iterinit(other_hv); | |
4974 | while ( hv_iternext(other_hv) ) | |
4975 | ++other_key_count; | |
4976 | } | |
4977 | else | |
4978 | other_key_count = HvUSEDKEYS(other_hv); | |
4979 | ||
4980 | if (this_key_count != other_key_count) | |
4981 | RETPUSHNO; | |
4982 | else | |
4983 | RETPUSHYES; | |
4984 | } | |
4985 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
4986 | AV * const other_av = MUTABLE_AV(SvRV(d)); | |
4987 | const SSize_t other_len = av_tindex(other_av) + 1; | |
4988 | SSize_t i; | |
4989 | HV *hv = MUTABLE_HV(SvRV(e)); | |
4990 | ||
4991 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n")); | |
4992 | for (i = 0; i < other_len; ++i) { | |
4993 | SV ** const svp = av_fetch(other_av, i, FALSE); | |
4994 | DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n")); | |
4995 | if (svp) { /* ??? When can this not happen? */ | |
4996 | if (hv_exists_ent(hv, *svp, 0)) | |
4997 | RETPUSHYES; | |
4998 | } | |
4999 | } | |
5000 | RETPUSHNO; | |
5001 | } | |
5002 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { | |
5003 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n")); | |
5004 | sm_regex_hash: | |
5005 | { | |
5006 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); | |
5007 | HE *he; | |
5008 | HV *hv = MUTABLE_HV(SvRV(e)); | |
5009 | ||
5010 | (void) hv_iterinit(hv); | |
5011 | while ( (he = hv_iternext(hv)) ) { | |
5012 | DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n")); | |
5013 | PUTBACK; | |
5014 | if (matcher_matches_sv(matcher, hv_iterkeysv(he))) { | |
5015 | SPAGAIN; | |
5016 | (void) hv_iterinit(hv); | |
5017 | destroy_matcher(matcher); | |
5018 | RETPUSHYES; | |
5019 | } | |
5020 | SPAGAIN; | |
5021 | } | |
5022 | destroy_matcher(matcher); | |
5023 | RETPUSHNO; | |
5024 | } | |
5025 | } | |
5026 | else { | |
5027 | sm_any_hash: | |
5028 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n")); | |
5029 | if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0)) | |
5030 | RETPUSHYES; | |
5031 | else | |
5032 | RETPUSHNO; | |
5033 | } | |
5034 | } | |
5035 | /* ~~ @array */ | |
5036 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) { | |
5037 | if (object_on_left) { | |
5038 | goto sm_any_array; /* Treat objects like scalars */ | |
5039 | } | |
5040 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
5041 | AV * const other_av = MUTABLE_AV(SvRV(e)); | |
5042 | const SSize_t other_len = av_tindex(other_av) + 1; | |
5043 | SSize_t i; | |
5044 | ||
5045 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n")); | |
5046 | for (i = 0; i < other_len; ++i) { | |
5047 | SV ** const svp = av_fetch(other_av, i, FALSE); | |
5048 | ||
5049 | DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n")); | |
5050 | if (svp) { /* ??? When can this not happen? */ | |
5051 | if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0)) | |
5052 | RETPUSHYES; | |
5053 | } | |
5054 | } | |
5055 | RETPUSHNO; | |
5056 | } | |
5057 | if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5058 | AV *other_av = MUTABLE_AV(SvRV(d)); | |
5059 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n")); | |
5060 | if (av_tindex(MUTABLE_AV(SvRV(e))) != av_tindex(other_av)) | |
5061 | RETPUSHNO; | |
5062 | else { | |
5063 | SSize_t i; | |
5064 | const SSize_t other_len = av_tindex(other_av); | |
5065 | ||
5066 | if (NULL == seen_this) { | |
5067 | seen_this = newHV(); | |
5068 | (void) sv_2mortal(MUTABLE_SV(seen_this)); | |
5069 | } | |
5070 | if (NULL == seen_other) { | |
5071 | seen_other = newHV(); | |
5072 | (void) sv_2mortal(MUTABLE_SV(seen_other)); | |
5073 | } | |
5074 | for(i = 0; i <= other_len; ++i) { | |
5075 | SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5076 | SV * const * const other_elem = av_fetch(other_av, i, FALSE); | |
5077 | ||
5078 | if (!this_elem || !other_elem) { | |
5079 | if ((this_elem && SvOK(*this_elem)) | |
5080 | || (other_elem && SvOK(*other_elem))) | |
5081 | RETPUSHNO; | |
5082 | } | |
5083 | else if (hv_exists_ent(seen_this, | |
5084 | sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) || | |
5085 | hv_exists_ent(seen_other, | |
5086 | sv_2mortal(newSViv(PTR2IV(*other_elem))), 0)) | |
5087 | { | |
5088 | if (*this_elem != *other_elem) | |
5089 | RETPUSHNO; | |
5090 | } | |
5091 | else { | |
5092 | (void)hv_store_ent(seen_this, | |
5093 | sv_2mortal(newSViv(PTR2IV(*this_elem))), | |
5094 | &PL_sv_undef, 0); | |
5095 | (void)hv_store_ent(seen_other, | |
5096 | sv_2mortal(newSViv(PTR2IV(*other_elem))), | |
5097 | &PL_sv_undef, 0); | |
5098 | PUSHs(*other_elem); | |
5099 | PUSHs(*this_elem); | |
5100 | ||
5101 | PUTBACK; | |
5102 | DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n")); | |
5103 | (void) do_smartmatch(seen_this, seen_other, 0); | |
5104 | SPAGAIN; | |
5105 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); | |
5106 | ||
5107 | if (!SvTRUEx(POPs)) | |
5108 | RETPUSHNO; | |
5109 | } | |
5110 | } | |
5111 | RETPUSHYES; | |
5112 | } | |
5113 | } | |
5114 | else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) { | |
5115 | DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n")); | |
5116 | sm_regex_array: | |
5117 | { | |
5118 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(d)); | |
5119 | const SSize_t this_len = av_tindex(MUTABLE_AV(SvRV(e))); | |
5120 | SSize_t i; | |
5121 | ||
5122 | for(i = 0; i <= this_len; ++i) { | |
5123 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5124 | DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n")); | |
5125 | PUTBACK; | |
5126 | if (svp && matcher_matches_sv(matcher, *svp)) { | |
5127 | SPAGAIN; | |
5128 | destroy_matcher(matcher); | |
5129 | RETPUSHYES; | |
5130 | } | |
5131 | SPAGAIN; | |
5132 | } | |
5133 | destroy_matcher(matcher); | |
5134 | RETPUSHNO; | |
5135 | } | |
5136 | } | |
5137 | else if (!SvOK(d)) { | |
5138 | /* undef ~~ array */ | |
5139 | const SSize_t this_len = av_tindex(MUTABLE_AV(SvRV(e))); | |
5140 | SSize_t i; | |
5141 | ||
5142 | DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n")); | |
5143 | for (i = 0; i <= this_len; ++i) { | |
5144 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5145 | DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n")); | |
5146 | if (!svp || !SvOK(*svp)) | |
5147 | RETPUSHYES; | |
5148 | } | |
5149 | RETPUSHNO; | |
5150 | } | |
5151 | else { | |
5152 | sm_any_array: | |
5153 | { | |
5154 | SSize_t i; | |
5155 | const SSize_t this_len = av_tindex(MUTABLE_AV(SvRV(e))); | |
5156 | ||
5157 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n")); | |
5158 | for (i = 0; i <= this_len; ++i) { | |
5159 | SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE); | |
5160 | if (!svp) | |
5161 | continue; | |
5162 | ||
5163 | PUSHs(d); | |
5164 | PUSHs(*svp); | |
5165 | PUTBACK; | |
5166 | /* infinite recursion isn't supposed to happen here */ | |
5167 | DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n")); | |
5168 | (void) do_smartmatch(NULL, NULL, 1); | |
5169 | SPAGAIN; | |
5170 | DEBUG_M(Perl_deb(aTHX_ " recursion finished\n")); | |
5171 | if (SvTRUEx(POPs)) | |
5172 | RETPUSHYES; | |
5173 | } | |
5174 | RETPUSHNO; | |
5175 | } | |
5176 | } | |
5177 | } | |
5178 | /* ~~ qr// */ | |
5179 | else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) { | |
5180 | if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) { | |
5181 | SV *t = d; d = e; e = t; | |
5182 | DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n")); | |
5183 | goto sm_regex_hash; | |
5184 | } | |
5185 | else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) { | |
5186 | SV *t = d; d = e; e = t; | |
5187 | DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n")); | |
5188 | goto sm_regex_array; | |
5189 | } | |
5190 | else { | |
5191 | PMOP * const matcher = make_matcher((REGEXP*) SvRV(e)); | |
5192 | bool result; | |
5193 | ||
5194 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n")); | |
5195 | PUTBACK; | |
5196 | result = matcher_matches_sv(matcher, d); | |
5197 | SPAGAIN; | |
5198 | PUSHs(result ? &PL_sv_yes : &PL_sv_no); | |
5199 | destroy_matcher(matcher); | |
5200 | RETURN; | |
5201 | } | |
5202 | } | |
5203 | /* ~~ scalar */ | |
5204 | /* See if there is overload magic on left */ | |
5205 | else if (object_on_left && SvAMAGIC(d)) { | |
5206 | SV *tmpsv; | |
5207 | DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n")); | |
5208 | DEBUG_M(Perl_deb(aTHX_ " attempting overload\n")); | |
5209 | PUSHs(d); PUSHs(e); | |
5210 | PUTBACK; | |
5211 | tmpsv = amagic_call(d, e, smart_amg, AMGf_noright); | |
5212 | if (tmpsv) { | |
5213 | SPAGAIN; | |
5214 | (void)POPs; | |
5215 | SETs(tmpsv); | |
5216 | RETURN; | |
5217 | } | |
5218 | SP -= 2; | |
5219 | DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n")); | |
5220 | goto sm_any_scalar; | |
5221 | } | |
5222 | else if (!SvOK(d)) { | |
5223 | /* undef ~~ scalar ; we already know that the scalar is SvOK */ | |
5224 | DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n")); | |
5225 | RETPUSHNO; | |
5226 | } | |
5227 | else | |
5228 | sm_any_scalar: | |
5229 | if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) { | |
5230 | DEBUG_M(if (SvNIOK(e)) | |
5231 | Perl_deb(aTHX_ " applying rule Any-Num\n"); | |
5232 | else | |
5233 | Perl_deb(aTHX_ " applying rule Num-numish\n"); | |
5234 | ); | |
5235 | /* numeric comparison */ | |
5236 | PUSHs(d); PUSHs(e); | |
5237 | PUTBACK; | |
5238 | if (CopHINTS_get(PL_curcop) & HINT_INTEGER) | |
5239 | (void) Perl_pp_i_eq(aTHX); | |
5240 | else | |
5241 | (void) Perl_pp_eq(aTHX); | |
0d863452 | 5242 | SPAGAIN; |
7896dde7 Z |
5243 | if (SvTRUEx(POPs)) |
5244 | RETPUSHYES; | |
5245 | else | |
5246 | RETPUSHNO; | |
0d863452 | 5247 | } |
7896dde7 Z |
5248 | |
5249 | /* As a last resort, use string comparison */ | |
5250 | DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n")); | |
5251 | PUSHs(d); PUSHs(e); | |
5252 | PUTBACK; | |
5253 | return Perl_pp_seq(aTHX); | |
0d863452 RH |
5254 | } |
5255 | ||
7896dde7 | 5256 | PP(pp_enterwhen) |
0d863452 | 5257 | { |
20b7effb | 5258 | dSP; |
eb578fdb | 5259 | PERL_CONTEXT *cx; |
1c23e2bd | 5260 | const U8 gimme = GIMME_V; |
0d863452 RH |
5261 | |
5262 | /* This is essentially an optimization: if the match | |
5263 | fails, we don't want to push a context and then | |
5264 | pop it again right away, so we skip straight | |
7896dde7 | 5265 | to the op that follows the leavewhen. |
25b991bf | 5266 | RETURNOP calls PUTBACK which restores the stack pointer after the POPs. |
0d863452 | 5267 | */ |
7896dde7 | 5268 | if (!(PL_op->op_flags & OPf_SPECIAL) && !SvTRUEx(POPs)) { |
b98da25d Z |
5269 | if (gimme == G_SCALAR) |
5270 | PUSHs(&PL_sv_undef); | |
25b991bf | 5271 | RETURNOP(cLOGOP->op_other->op_next); |
b98da25d | 5272 | } |
0d863452 | 5273 | |
7896dde7 Z |
5274 | cx = cx_pushblock(CXt_WHEN, gimme, SP, PL_savestack_ix); |
5275 | cx_pushwhen(cx); | |
0d863452 RH |
5276 | |
5277 | RETURN; | |
5278 | } | |
5279 | ||
7896dde7 | 5280 | PP(pp_leavewhen) |
0d863452 | 5281 | { |
c08f093b | 5282 | I32 cxix; |
eb578fdb | 5283 | PERL_CONTEXT *cx; |
1c23e2bd | 5284 | U8 gimme; |
f5ddd604 | 5285 | SV **oldsp; |
8aef2117 | 5286 | |
4ebe6e95 | 5287 | cx = CX_CUR(); |
7896dde7 | 5288 | assert(CxTYPE(cx) == CXt_WHEN); |
8aef2117 | 5289 | gimme = cx->blk_gimme; |
0d863452 | 5290 | |
7896dde7 | 5291 | cxix = dopoptogivenfor(cxstack_ix); |
c08f093b | 5292 | if (cxix < 0) |
7896dde7 Z |
5293 | /* diag_listed_as: Can't "when" outside a topicalizer */ |
5294 | DIE(aTHX_ "Can't \"%s\" outside a topicalizer", | |
5295 | PL_op->op_flags & OPf_SPECIAL ? "default" : "when"); | |
c08f093b | 5296 | |
f5ddd604 | 5297 | oldsp = PL_stack_base + cx->blk_oldsp; |
0663a8f8 | 5298 | if (gimme == G_VOID) |
f5ddd604 | 5299 | PL_stack_sp = oldsp; |
0663a8f8 | 5300 | else |
f5ddd604 | 5301 | leave_adjust_stacks(oldsp, oldsp, gimme, 1); |
75bc488d | 5302 | |
7896dde7 | 5303 | /* pop the WHEN, BLOCK and anything else before the GIVEN/FOR */ |
8aef2117 DM |
5304 | assert(cxix < cxstack_ix); |
5305 | dounwind(cxix); | |
c08f093b VP |
5306 | |
5307 | cx = &cxstack[cxix]; | |
5308 | ||
7896dde7 | 5309 | if (CxFOREACH(cx)) { |
590529d8 DM |
5310 | /* emulate pp_next. Note that any stack(s) cleanup will be |
5311 | * done by the pp_unstack which op_nextop should point to */ | |
7e637ba4 | 5312 | cx = CX_CUR(); |
ed8ff0f3 | 5313 | cx_topblock(cx); |
c08f093b | 5314 | PL_curcop = cx->blk_oldcop; |
c08f093b VP |
5315 | return cx->blk_loop.my_op->op_nextop; |
5316 | } | |
47c9d59f NC |
5317 | else { |
5318 | PERL_ASYNC_CHECK(); | |
7896dde7 Z |
5319 | assert(cx->blk_givwhen.leave_op->op_type == OP_LEAVEGIVEN); |
5320 | return cx->blk_givwhen.leave_op; | |
47c9d59f | 5321 | } |
0d863452 RH |
5322 | } |
5323 | ||
5324 | PP(pp_continue) | |
5325 | { | |
0d863452 | 5326 | I32 cxix; |
eb578fdb | 5327 | PERL_CONTEXT *cx; |
5da525e9 | 5328 | OP *nextop; |
0d863452 | 5329 | |
7896dde7 | 5330 | cxix = dopoptowhen(cxstack_ix); |
0d863452 | 5331 | if (cxix < 0) |
7896dde7 | 5332 | DIE(aTHX_ "Can't \"continue\" outside a when block"); |
c08f093b | 5333 | |
0d863452 RH |
5334 | if (cxix < cxstack_ix) |
5335 | dounwind(cxix); | |
5336 | ||
4ebe6e95 | 5337 | cx = CX_CUR(); |
7896dde7 | 5338 | assert(CxTYPE(cx) == CXt_WHEN); |
4df352a8 | 5339 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; |
2f450c1b | 5340 | CX_LEAVE_SCOPE(cx); |
7896dde7 | 5341 | cx_popwhen(cx); |
ed8ff0f3 | 5342 | cx_popblock(cx); |
7896dde7 | 5343 | nextop = cx->blk_givwhen.leave_op->op_next; |
5da525e9 | 5344 | CX_POP(cx); |
c08f093b | 5345 | |
5da525e9 | 5346 | return nextop; |
0d863452 RH |
5347 | } |
5348 | ||
7896dde7 Z |
5349 | PP(pp_break) |
5350 | { | |
5351 | I32 cxix; | |
5352 | PERL_CONTEXT *cx; | |
5353 | ||
5354 | cxix = dopoptogivenfor(cxstack_ix); | |
5355 | if (cxix < 0) | |
5356 | DIE(aTHX_ "Can't \"break\" outside a given block"); | |
5357 | ||
5358 | cx = &cxstack[cxix]; | |
5359 | if (CxFOREACH(cx)) | |
5360 | DIE(aTHX_ "Can't \"break\" in a loop topicalizer"); | |
5361 | ||
5362 | if (cxix < cxstack_ix) | |
5363 | dounwind(cxix); | |
5364 | ||
5365 | /* Restore the sp at the time we entered the given block */ | |
5366 | cx = CX_CUR(); | |
5367 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; | |
5368 | ||
5369 | return cx->blk_givwhen.leave_op; | |
5370 | } | |
5371 | ||
74e0ddf7 | 5372 | static MAGIC * |
cea2e8a9 | 5373 | S_doparseform(pTHX_ SV *sv) |
a0d0e21e LW |
5374 | { |
5375 | STRLEN len; | |
eb578fdb KW |
5376 | char *s = SvPV(sv, len); |
5377 | char *send; | |
5378 | char *base = NULL; /* start of current field */ | |
5379 | I32 skipspaces = 0; /* number of contiguous spaces seen */ | |
086b26f3 DM |
5380 | bool noblank = FALSE; /* ~ or ~~ seen on this line */ |
5381 | bool repeat = FALSE; /* ~~ seen on this line */ | |
5382 | bool postspace = FALSE; /* a text field may need right padding */ | |
dea28490 | 5383 | U32 *fops; |
eb578fdb | 5384 | U32 *fpc; |
086b26f3 | 5385 | U32 *linepc = NULL; /* position of last FF_LINEMARK */ |
eb578fdb | 5386 | I32 arg; |
086b26f3 DM |
5387 | bool ischop; /* it's a ^ rather than a @ */ |
5388 | bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */ | |
a1b95068 | 5389 | int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */ |
3808a683 DM |
5390 | MAGIC *mg = NULL; |
5391 | SV *sv_copy; | |
a0d0e21e | 5392 | |
7918f24d NC |
5393 | PERL_ARGS_ASSERT_DOPARSEFORM; |
5394 | ||
55497cff | 5395 | if (len == 0) |
cea2e8a9 | 5396 | Perl_croak(aTHX_ "Null picture in formline"); |
ac27b0f5 | 5397 | |
3808a683 DM |
5398 | if (SvTYPE(sv) >= SVt_PVMG) { |
5399 | /* This might, of course, still return NULL. */ | |
5400 | mg = mg_find(sv, PERL_MAGIC_fm); | |
5401 | } else { | |
5402 | sv_upgrade(sv, SVt_PVMG); | |
5403 | } | |
5404 | ||
5405 | if (mg) { | |
5406 | /* still the same as previously-compiled string? */ | |
5407 | SV *old = mg->mg_obj; | |
5408 | if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv)) | |
5409 | && len == SvCUR(old) | |
dd314e1c | 5410 | && strnEQ(SvPVX(old), s, len) |
b57b1734 DM |
5411 | ) { |
5412 | DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n")); | |
3808a683 | 5413 | return mg; |
b57b1734 | 5414 | } |
3808a683 | 5415 | |
b57b1734 | 5416 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n")); |
3808a683 DM |
5417 | Safefree(mg->mg_ptr); |
5418 | mg->mg_ptr = NULL; | |
5419 | SvREFCNT_dec(old); | |
5420 | mg->mg_obj = NULL; | |
5421 | } | |
b57b1734 DM |
5422 | else { |
5423 | DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n")); | |
3808a683 | 5424 | mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0); |
b57b1734 | 5425 | } |
3808a683 DM |
5426 | |
5427 | sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv)); | |
5428 | s = SvPV(sv_copy, len); /* work on the copy, not the original */ | |
5429 | send = s + len; | |
5430 | ||
5431 | ||
815f25c6 DM |
5432 | /* estimate the buffer size needed */ |
5433 | for (base = s; s <= send; s++) { | |
a1b95068 | 5434 | if (*s == '\n' || *s == '@' || *s == '^') |
815f25c6 DM |
5435 | maxops += 10; |
5436 | } | |
5437 | s = base; | |
c445ea15 | 5438 | base = NULL; |
815f25c6 | 5439 | |
a02a5408 | 5440 | Newx(fops, maxops, U32); |
a0d0e21e LW |
5441 | fpc = fops; |
5442 | ||
5443 | if (s < send) { | |
5444 | linepc = fpc; | |
5445 | *fpc++ = FF_LINEMARK; | |
5446 | noblank = repeat = FALSE; | |
5447 | base = s; | |
5448 | } | |
5449 | ||
5450 | while (s <= send) { | |
5451 | switch (*s++) { | |
5452 | default: | |
5453 | skipspaces = 0; | |
5454 | continue; | |
5455 | ||
5456 | case '~': | |
5457 | if (*s == '~') { | |
5458 | repeat = TRUE; | |
b57b1734 DM |
5459 | skipspaces++; |
5460 | s++; | |
a0d0e21e LW |
5461 | } |
5462 | noblank = TRUE; | |
924ba076 | 5463 | /* FALLTHROUGH */ |
a0d0e21e LW |
5464 | case ' ': case '\t': |
5465 | skipspaces++; | |
5466 | continue; | |
a1b95068 WL |
5467 | case 0: |
5468 | if (s < send) { | |
5469 | skipspaces = 0; | |
5470 | continue; | |
2165bd23 LM |
5471 | } |
5472 | /* FALLTHROUGH */ | |
a1b95068 | 5473 | case '\n': |
a0d0e21e LW |
5474 | arg = s - base; |
5475 | skipspaces++; | |
5476 | arg -= skipspaces; | |
5477 | if (arg) { | |
5f05dabc | 5478 | if (postspace) |
a0d0e21e | 5479 | *fpc++ = FF_SPACE; |
a0d0e21e | 5480 | *fpc++ = FF_LITERAL; |
76912796 | 5481 | *fpc++ = (U32)arg; |
a0d0e21e | 5482 | } |
5f05dabc | 5483 | postspace = FALSE; |
a0d0e21e LW |
5484 | if (s <= send) |
5485 | skipspaces--; | |
5486 | if (skipspaces) { | |
5487 | *fpc++ = FF_SKIP; | |
76912796 | 5488 | *fpc++ = (U32)skipspaces; |
a0d0e21e LW |
5489 | } |
5490 | skipspaces = 0; | |
5491 | if (s <= send) | |
5492 | *fpc++ = FF_NEWLINE; | |
5493 | if (noblank) { | |
5494 | *fpc++ = FF_BLANK; | |
5495 | if (repeat) | |
5496 | arg = fpc - linepc + 1; | |
5497 | else | |
5498 | arg = 0; | |
76912796 | 5499 | *fpc++ = (U32)arg; |
a0d0e21e LW |
5500 | } |
5501 | if (s < send) { | |
5502 | linepc = fpc; | |
5503 | *fpc++ = FF_LINEMARK; | |
5504 | noblank = repeat = FALSE; | |
5505 | base = s; | |
5506 | } | |
5507 | else | |
5508 | s++; | |
5509 | continue; | |
5510 | ||
5511 | case '@': | |
5512 | case '^': | |
5513 | ischop = s[-1] == '^'; | |
5514 | ||
5515 | if (postspace) { | |
5516 | *fpc++ = FF_SPACE; | |
5517 | postspace = FALSE; | |
5518 | } | |
5519 | arg = (s - base) - 1; | |
5520 | if (arg) { | |
5521 | *fpc++ = FF_LITERAL; | |
76912796 | 5522 | *fpc++ = (U32)arg; |
a0d0e21e LW |
5523 | } |
5524 | ||
5525 | base = s - 1; | |
5526 | *fpc++ = FF_FETCH; | |
086b26f3 | 5527 | if (*s == '*') { /* @* or ^* */ |
a0d0e21e | 5528 | s++; |
a1b95068 WL |
5529 | *fpc++ = 2; /* skip the @* or ^* */ |
5530 | if (ischop) { | |
5531 | *fpc++ = FF_LINESNGL; | |
5532 | *fpc++ = FF_CHOP; | |
5533 | } else | |
5534 | *fpc++ = FF_LINEGLOB; | |
a0d0e21e | 5535 | } |
086b26f3 | 5536 | else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */ |
a701009a | 5537 | arg = ischop ? FORM_NUM_BLANK : 0; |
a0d0e21e LW |
5538 | base = s - 1; |
5539 | while (*s == '#') | |
5540 | s++; | |
5541 | if (*s == '.') { | |
06b5626a | 5542 | const char * const f = ++s; |
a0d0e21e LW |
5543 | while (*s == '#') |
5544 | s++; | |
a701009a | 5545 | arg |= FORM_NUM_POINT + (s - f); |
a0d0e21e LW |
5546 | } |
5547 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5548 | *fpc++ = FF_DECIMAL; | |
76912796 | 5549 | *fpc++ = (U32)arg; |
a1b95068 | 5550 | unchopnum |= ! ischop; |
784707d5 JP |
5551 | } |
5552 | else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */ | |
a701009a | 5553 | arg = ischop ? FORM_NUM_BLANK : 0; |
784707d5 JP |
5554 | base = s - 1; |
5555 | s++; /* skip the '0' first */ | |
5556 | while (*s == '#') | |
5557 | s++; | |
5558 | if (*s == '.') { | |
06b5626a | 5559 | const char * const f = ++s; |
784707d5 JP |
5560 | while (*s == '#') |
5561 | s++; | |
a701009a | 5562 | arg |= FORM_NUM_POINT + (s - f); |
784707d5 JP |
5563 | } |
5564 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5565 | *fpc++ = FF_0DECIMAL; | |
76912796 | 5566 | *fpc++ = (U32)arg; |
a1b95068 | 5567 | unchopnum |= ! ischop; |
a0d0e21e | 5568 | } |
086b26f3 | 5569 | else { /* text field */ |
a0d0e21e LW |
5570 | I32 prespace = 0; |
5571 | bool ismore = FALSE; | |
5572 | ||
5573 | if (*s == '>') { | |
5574 | while (*++s == '>') ; | |
5575 | prespace = FF_SPACE; | |
5576 | } | |
5577 | else if (*s == '|') { | |
5578 | while (*++s == '|') ; | |
5579 | prespace = FF_HALFSPACE; | |
5580 | postspace = TRUE; | |
5581 | } | |
5582 | else { | |
5583 | if (*s == '<') | |
5584 | while (*++s == '<') ; | |
5585 | postspace = TRUE; | |
5586 | } | |
5587 | if (*s == '.' && s[1] == '.' && s[2] == '.') { | |
5588 | s += 3; | |
5589 | ismore = TRUE; | |
5590 | } | |
5591 | *fpc++ = s - base; /* fieldsize for FETCH */ | |
5592 | ||
5593 | *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL; | |
5594 | ||
5595 | if (prespace) | |
76912796 | 5596 | *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */ |
a0d0e21e LW |
5597 | *fpc++ = FF_ITEM; |
5598 | if (ismore) | |
5599 | *fpc++ = FF_MORE; | |
5600 | if (ischop) | |
5601 | *fpc++ = FF_CHOP; | |
5602 | } | |
5603 | base = s; | |
5604 | skipspaces = 0; | |
5605 | continue; | |
5606 | } | |
5607 | } | |
5608 | *fpc++ = FF_END; | |
5609 | ||
815f25c6 | 5610 | assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */ |
a0d0e21e | 5611 | arg = fpc - fops; |
74e0ddf7 | 5612 | |
3808a683 | 5613 | mg->mg_ptr = (char *) fops; |
74e0ddf7 | 5614 | mg->mg_len = arg * sizeof(U32); |
3808a683 DM |
5615 | mg->mg_obj = sv_copy; |
5616 | mg->mg_flags |= MGf_REFCOUNTED; | |
a1b95068 | 5617 | |
bfed75c6 | 5618 | if (unchopnum && repeat) |
75f63940 | 5619 | Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)"); |
74e0ddf7 NC |
5620 | |
5621 | return mg; | |
a1b95068 WL |
5622 | } |
5623 | ||
5624 | ||
5625 | STATIC bool | |
5626 | S_num_overflow(NV value, I32 fldsize, I32 frcsize) | |
5627 | { | |
5628 | /* Can value be printed in fldsize chars, using %*.*f ? */ | |
5629 | NV pwr = 1; | |
5630 | NV eps = 0.5; | |
5631 | bool res = FALSE; | |
5632 | int intsize = fldsize - (value < 0 ? 1 : 0); | |
5633 | ||
a701009a | 5634 | if (frcsize & FORM_NUM_POINT) |
a1b95068 | 5635 | intsize--; |
a701009a | 5636 | frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK); |
a1b95068 WL |
5637 | intsize -= frcsize; |
5638 | ||
5639 | while (intsize--) pwr *= 10.0; | |
5640 | while (frcsize--) eps /= 10.0; | |
5641 | ||
5642 | if( value >= 0 ){ | |
5643 | if (value + eps >= pwr) | |
5644 | res = TRUE; | |
5645 | } else { | |
5646 | if (value - eps <= -pwr) | |
5647 | res = TRUE; | |
5648 | } | |
5649 | return res; | |
a0d0e21e | 5650 | } |
4e35701f | 5651 | |
bbed91b5 | 5652 | static I32 |
0bd48802 | 5653 | S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen) |
bbed91b5 | 5654 | { |
0bd48802 | 5655 | SV * const datasv = FILTER_DATA(idx); |
504618e9 | 5656 | const int filter_has_file = IoLINES(datasv); |
ad64d0ec NC |
5657 | SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv)); |
5658 | SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv)); | |
941a98a0 | 5659 | int status = 0; |
ec0b63d7 | 5660 | SV *upstream; |
941a98a0 | 5661 | STRLEN got_len; |
162177c1 Z |
5662 | char *got_p = NULL; |
5663 | char *prune_from = NULL; | |
34113e50 | 5664 | bool read_from_cache = FALSE; |
bb7a0f54 | 5665 | STRLEN umaxlen; |
d60d2019 | 5666 | SV *err = NULL; |
bb7a0f54 | 5667 | |
7918f24d NC |
5668 | PERL_ARGS_ASSERT_RUN_USER_FILTER; |
5669 | ||
bb7a0f54 MHM |
5670 | assert(maxlen >= 0); |
5671 | umaxlen = maxlen; | |
5675696b | 5672 | |
bbed91b5 | 5673 | /* I was having segfault trouble under Linux 2.2.5 after a |
f6bab5f6 | 5674 | parse error occurred. (Had to hack around it with a test |
13765c85 | 5675 | for PL_parser->error_count == 0.) Solaris doesn't segfault -- |
bbed91b5 KF |
5676 | not sure where the trouble is yet. XXX */ |
5677 | ||
4464f08e NC |
5678 | { |
5679 | SV *const cache = datasv; | |
937b367d NC |
5680 | if (SvOK(cache)) { |
5681 | STRLEN cache_len; | |
5682 | const char *cache_p = SvPV(cache, cache_len); | |
941a98a0 NC |
5683 | STRLEN take = 0; |
5684 | ||
bb7a0f54 | 5685 | if (umaxlen) { |
941a98a0 NC |
5686 | /* Running in block mode and we have some cached data already. |
5687 | */ | |
bb7a0f54 | 5688 | if (cache_len >= umaxlen) { |
941a98a0 NC |
5689 | /* In fact, so much data we don't even need to call |
5690 | filter_read. */ | |
bb7a0f54 | 5691 | take = umaxlen; |
941a98a0 NC |
5692 | } |
5693 | } else { | |
10edeb5d JH |
5694 | const char *const first_nl = |
5695 | (const char *)memchr(cache_p, '\n', cache_len); | |
941a98a0 NC |
5696 | if (first_nl) { |
5697 | take = first_nl + 1 - cache_p; | |
5698 | } | |
5699 | } | |
5700 | if (take) { | |
5701 | sv_catpvn(buf_sv, cache_p, take); | |
5702 | sv_chop(cache, cache_p + take); | |
486ec47a | 5703 | /* Definitely not EOF */ |
937b367d NC |
5704 | return 1; |
5705 | } | |
941a98a0 | 5706 | |
937b367d | 5707 | sv_catsv(buf_sv, cache); |
bb7a0f54 MHM |
5708 | if (umaxlen) { |
5709 | umaxlen -= cache_len; | |
941a98a0 | 5710 | } |
937b367d | 5711 | SvOK_off(cache); |
34113e50 | 5712 | read_from_cache = TRUE; |
937b367d NC |
5713 | } |
5714 | } | |
ec0b63d7 | 5715 | |
34113e50 NC |
5716 | /* Filter API says that the filter appends to the contents of the buffer. |
5717 | Usually the buffer is "", so the details don't matter. But if it's not, | |
5718 | then clearly what it contains is already filtered by this filter, so we | |
5719 | don't want to pass it in a second time. | |
5720 | I'm going to use a mortal in case the upstream filter croaks. */ | |
ec0b63d7 NC |
5721 | upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv)) |
5722 | ? sv_newmortal() : buf_sv; | |
5723 | SvUPGRADE(upstream, SVt_PV); | |
937b367d | 5724 | |
bbed91b5 | 5725 | if (filter_has_file) { |
67e70b33 | 5726 | status = FILTER_READ(idx+1, upstream, 0); |
bbed91b5 KF |
5727 | } |
5728 | ||
34113e50 | 5729 | if (filter_sub && status >= 0) { |
39644a26 | 5730 | dSP; |
bbed91b5 KF |
5731 | int count; |
5732 | ||
d343c3ef | 5733 | ENTER_with_name("call_filter_sub"); |
55b5114f | 5734 | SAVE_DEFSV; |
bbed91b5 KF |
5735 | SAVETMPS; |
5736 | EXTEND(SP, 2); | |
5737 | ||
414bf5ae | 5738 | DEFSV_set(upstream); |
bbed91b5 | 5739 | PUSHMARK(SP); |
725c44f9 | 5740 | PUSHs(&PL_sv_zero); |
bbed91b5 KF |
5741 | if (filter_state) { |
5742 | PUSHs(filter_state); | |
5743 | } | |
5744 | PUTBACK; | |
d60d2019 | 5745 | count = call_sv(filter_sub, G_SCALAR|G_EVAL); |
bbed91b5 KF |
5746 | SPAGAIN; |
5747 | ||
5748 | if (count > 0) { | |
5749 | SV *out = POPs; | |
2e8409ad | 5750 | SvGETMAGIC(out); |
bbed91b5 | 5751 | if (SvOK(out)) { |
941a98a0 | 5752 | status = SvIV(out); |
bbed91b5 | 5753 | } |
eed484f9 DD |
5754 | else { |
5755 | SV * const errsv = ERRSV; | |
5756 | if (SvTRUE_NN(errsv)) | |
5757 | err = newSVsv(errsv); | |
d60d2019 | 5758 | } |
bbed91b5 KF |
5759 | } |
5760 | ||
5761 | PUTBACK; | |
5762 | FREETMPS; | |
d343c3ef | 5763 | LEAVE_with_name("call_filter_sub"); |
bbed91b5 KF |
5764 | } |
5765 | ||
536ac391 FC |
5766 | if (SvGMAGICAL(upstream)) { |
5767 | mg_get(upstream); | |
5768 | if (upstream == buf_sv) mg_free(buf_sv); | |
5769 | } | |
b68108d9 | 5770 | if (SvIsCOW(upstream)) sv_force_normal(upstream); |
d60d2019 | 5771 | if(!err && SvOK(upstream)) { |
536ac391 | 5772 | got_p = SvPV_nomg(upstream, got_len); |
bb7a0f54 MHM |
5773 | if (umaxlen) { |
5774 | if (got_len > umaxlen) { | |
5775 | prune_from = got_p + umaxlen; | |
937b367d | 5776 | } |
941a98a0 | 5777 | } else { |
162177c1 | 5778 | char *const first_nl = (char *)memchr(got_p, '\n', got_len); |
941a98a0 NC |
5779 | if (first_nl && first_nl + 1 < got_p + got_len) { |
5780 | /* There's a second line here... */ | |
5781 | prune_from = first_nl + 1; | |
937b367d | 5782 | } |
937b367d NC |
5783 | } |
5784 | } | |
d60d2019 | 5785 | if (!err && prune_from) { |
941a98a0 NC |
5786 | /* Oh. Too long. Stuff some in our cache. */ |
5787 | STRLEN cached_len = got_p + got_len - prune_from; | |
4464f08e | 5788 | SV *const cache = datasv; |
941a98a0 | 5789 | |
4464f08e | 5790 | if (SvOK(cache)) { |
941a98a0 NC |
5791 | /* Cache should be empty. */ |
5792 | assert(!SvCUR(cache)); | |
5793 | } | |
5794 | ||
5795 | sv_setpvn(cache, prune_from, cached_len); | |
5796 | /* If you ask for block mode, you may well split UTF-8 characters. | |
5797 | "If it breaks, you get to keep both parts" | |
5798 | (Your code is broken if you don't put them back together again | |
5799 | before something notices.) */ | |
5800 | if (SvUTF8(upstream)) { | |
5801 | SvUTF8_on(cache); | |
5802 | } | |
00752fe1 FC |
5803 | if (SvPOK(upstream)) SvCUR_set(upstream, got_len - cached_len); |
5804 | else | |
5805 | /* Cannot just use sv_setpvn, as that could free the buffer | |
5806 | before we have a chance to assign it. */ | |
5807 | sv_usepvn(upstream, savepvn(got_p, got_len - cached_len), | |
5808 | got_len - cached_len); | |
162177c1 | 5809 | *prune_from = 0; |
941a98a0 NC |
5810 | /* Can't yet be EOF */ |
5811 | if (status == 0) | |
5812 | status = 1; | |
5813 | } | |
937b367d | 5814 | |
34113e50 NC |
5815 | /* If they are at EOF but buf_sv has something in it, then they may never |
5816 | have touched the SV upstream, so it may be undefined. If we naively | |
5817 | concatenate it then we get a warning about use of uninitialised value. | |
5818 | */ | |
d60d2019 | 5819 | if (!err && upstream != buf_sv && |
dc423e96 | 5820 | SvOK(upstream)) { |
536ac391 | 5821 | sv_catsv_nomg(buf_sv, upstream); |
937b367d | 5822 | } |
ae2c96ed | 5823 | else if (SvOK(upstream)) (void)SvPV_force_nolen(buf_sv); |
937b367d | 5824 | |
941a98a0 | 5825 | if (status <= 0) { |
bbed91b5 | 5826 | IoLINES(datasv) = 0; |
bbed91b5 KF |
5827 | if (filter_state) { |
5828 | SvREFCNT_dec(filter_state); | |
a0714e2c | 5829 | IoTOP_GV(datasv) = NULL; |
bbed91b5 KF |
5830 | } |
5831 | if (filter_sub) { | |
5832 | SvREFCNT_dec(filter_sub); | |
a0714e2c | 5833 | IoBOTTOM_GV(datasv) = NULL; |
bbed91b5 | 5834 | } |
0bd48802 | 5835 | filter_del(S_run_user_filter); |
bbed91b5 | 5836 | } |
d60d2019 JL |
5837 | |
5838 | if (err) | |
5839 | croak_sv(err); | |
5840 | ||
34113e50 NC |
5841 | if (status == 0 && read_from_cache) { |
5842 | /* If we read some data from the cache (and by getting here it implies | |
5843 | that we emptied the cache) then we aren't yet at EOF, and mustn't | |
5844 | report that to our caller. */ | |
5845 | return 1; | |
5846 | } | |
941a98a0 | 5847 | return status; |
bbed91b5 | 5848 | } |
84d4ea48 | 5849 | |
241d1a3b | 5850 | /* |
14d04a33 | 5851 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 5852 | */ |