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