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