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