This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fixes for mortalization bug in xsubpp, other efficiency tweaks
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
19#include "perl.h"
20
8ac85365
NIS
21#ifdef I_UNISTD
22#include <unistd.h>
23#endif
24
a0d0e21e
LW
25/* Hot code. */
26
11343788
MB
27#ifdef USE_THREADS
28static void
8ac85365 29unset_cvowner(void *cvarg)
11343788
MB
30{
31 register CV* cv = (CV *) cvarg;
32#ifdef DEBUGGING
33 dTHR;
34#endif /* DEBUGGING */
35
1fd28e87
MB
36 DEBUG_L((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
37 thr, cv, SvPEEK((SV*)cv))));
11343788 38 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
39 DEBUG_L(if (CvDEPTH(cv) != 0)
40 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
41 CvDEPTH(cv)););
11343788
MB
42 assert(thr == CvOWNER(cv));
43 CvOWNER(cv) = 0;
11343788
MB
44 MUTEX_UNLOCK(CvMUTEXP(cv));
45 SvREFCNT_dec(cv);
46}
11343788
MB
47#endif /* USE_THREADS */
48
a0d0e21e
LW
49PP(pp_const)
50{
4e35701f 51 djSP;
a0d0e21e
LW
52 XPUSHs(cSVOP->op_sv);
53 RETURN;
54}
55
56PP(pp_nextstate)
57{
58 curcop = (COP*)op;
59 TAINT_NOT; /* Each statement is presumed innocent */
60 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
61 FREETMPS;
62 return NORMAL;
63}
64
65PP(pp_gvsv)
66{
4e35701f 67 djSP;
924508f0 68 EXTEND(SP,1);
a0d0e21e
LW
69 if (op->op_private & OPpLVAL_INTRO)
70 PUSHs(save_scalar(cGVOP->op_gv));
71 else
72 PUSHs(GvSV(cGVOP->op_gv));
73 RETURN;
74}
75
76PP(pp_null)
77{
78 return NORMAL;
79}
80
81PP(pp_pushmark)
82{
83 PUSHMARK(stack_sp);
84 return NORMAL;
85}
86
87PP(pp_stringify)
88{
4e35701f 89 djSP; dTARGET;
a0d0e21e
LW
90 STRLEN len;
91 char *s;
92 s = SvPV(TOPs,len);
93 sv_setpvn(TARG,s,len);
94 SETTARG;
95 RETURN;
96}
97
98PP(pp_gv)
99{
4e35701f 100 djSP;
a0d0e21e
LW
101 XPUSHs((SV*)cGVOP->op_gv);
102 RETURN;
103}
104
105PP(pp_and)
106{
4e35701f 107 djSP;
a0d0e21e
LW
108 if (!SvTRUE(TOPs))
109 RETURN;
110 else {
111 --SP;
112 RETURNOP(cLOGOP->op_other);
113 }
114}
115
116PP(pp_sassign)
117{
4e35701f 118 djSP; dPOPTOPssrl;
748a9306
LW
119 MAGIC *mg;
120
a0d0e21e
LW
121 if (op->op_private & OPpASSIGN_BACKWARDS) {
122 SV *temp;
123 temp = left; left = right; right = temp;
124 }
bbce6d69 125 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 126 TAINT_NOT;
54310121 127 SvSetMagicSV(right, left);
a0d0e21e
LW
128 SETs(right);
129 RETURN;
130}
131
132PP(pp_cond_expr)
133{
4e35701f 134 djSP;
a0d0e21e
LW
135 if (SvTRUEx(POPs))
136 RETURNOP(cCONDOP->op_true);
137 else
138 RETURNOP(cCONDOP->op_false);
139}
140
141PP(pp_unstack)
142{
143 I32 oldsave;
144 TAINT_NOT; /* Each statement is presumed innocent */
145 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
146 FREETMPS;
147 oldsave = scopestack[scopestack_ix - 1];
148 LEAVE_SCOPE(oldsave);
149 return NORMAL;
150}
151
a0d0e21e
LW
152PP(pp_concat)
153{
4e35701f 154 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
155 {
156 dPOPTOPssrl;
a0d0e21e
LW
157 STRLEN len;
158 char *s;
159 if (TARG != left) {
160 s = SvPV(left,len);
161 sv_setpvn(TARG,s,len);
162 }
c07a80fd
PP
163 else if (SvGMAGICAL(TARG))
164 mg_get(TARG);
7a4c00b4 165 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 166 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 167 s = SvPV_force(TARG, len);
4633a7c4 168 }
a0d0e21e 169 s = SvPV(right,len);
68dc0745
PP
170 if (SvOK(TARG))
171 sv_catpvn(TARG,s,len);
172 else
173 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
174 SETTARG;
175 RETURN;
748a9306 176 }
a0d0e21e
LW
177}
178
179PP(pp_padsv)
180{
4e35701f 181 djSP; dTARGET;
a0d0e21e 182 XPUSHs(TARG);
4633a7c4
LW
183 if (op->op_flags & OPf_MOD) {
184 if (op->op_private & OPpLVAL_INTRO)
185 SAVECLEARSV(curpad[op->op_targ]);
8ec5e241
NIS
186 else if (op->op_private & OPpDEREF) {
187 PUTBACK;
54310121 188 vivify_ref(curpad[op->op_targ], op->op_private & OPpDEREF);
8ec5e241
NIS
189 SPAGAIN;
190 }
4633a7c4 191 }
a0d0e21e
LW
192 RETURN;
193}
194
195PP(pp_readline)
196{
197 last_in_gv = (GV*)(*stack_sp--);
198 return do_readline();
199}
200
201PP(pp_eq)
202{
4e35701f 203 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
204 {
205 dPOPnv;
54310121 206 SETs(boolSV(TOPn == value));
a0d0e21e
LW
207 RETURN;
208 }
209}
210
211PP(pp_preinc)
212{
4e35701f 213 djSP;
68dc0745 214 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 215 croak(no_modify);
55497cff
PP
216 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
217 SvIVX(TOPs) != IV_MAX)
218 {
748a9306 219 ++SvIVX(TOPs);
55497cff 220 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
221 }
222 else
223 sv_inc(TOPs);
a0d0e21e
LW
224 SvSETMAGIC(TOPs);
225 return NORMAL;
226}
227
228PP(pp_or)
229{
4e35701f 230 djSP;
a0d0e21e
LW
231 if (SvTRUE(TOPs))
232 RETURN;
233 else {
234 --SP;
235 RETURNOP(cLOGOP->op_other);
236 }
237}
238
239PP(pp_add)
240{
4e35701f 241 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 242 {
7a4c00b4 243 dPOPTOPnnrl_ul;
a0d0e21e
LW
244 SETn( left + right );
245 RETURN;
246 }
247}
248
249PP(pp_aelemfast)
250{
4e35701f 251 djSP;
a0d0e21e
LW
252 AV *av = GvAV((GV*)cSVOP->op_sv);
253 SV** svp = av_fetch(av, op->op_private, op->op_flags & OPf_MOD);
6ff81951 254 EXTEND(SP, 1);
a0d0e21e
LW
255 PUSHs(svp ? *svp : &sv_undef);
256 RETURN;
257}
258
259PP(pp_join)
260{
4e35701f 261 djSP; dMARK; dTARGET;
a0d0e21e
LW
262 MARK++;
263 do_join(TARG, *MARK, MARK, SP);
264 SP = MARK;
265 SETs(TARG);
266 RETURN;
267}
268
269PP(pp_pushre)
270{
4e35701f 271 djSP;
44a8e56a
PP
272#ifdef DEBUGGING
273 /*
274 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
275 * will be enough to hold an OP*.
276 */
277 SV* sv = sv_newmortal();
278 sv_upgrade(sv, SVt_PVLV);
279 LvTYPE(sv) = '/';
280 Copy(&op, &LvTARGOFF(sv), 1, OP*);
281 XPUSHs(sv);
282#else
a0d0e21e 283 XPUSHs((SV*)op);
44a8e56a 284#endif
a0d0e21e
LW
285 RETURN;
286}
287
288/* Oversized hot code. */
289
290PP(pp_print)
291{
4e35701f 292 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
293 GV *gv;
294 IO *io;
760ac839 295 register PerlIO *fp;
236988e4 296 MAGIC *mg;
a0d0e21e
LW
297
298 if (op->op_flags & OPf_STACKED)
299 gv = (GV*)*++MARK;
300 else
301 gv = defoutgv;
3049007d 302 if (SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
68dc0745 303 if (MARK == ORIGMARK) {
a60c0954
NIS
304 /* If using default handle then we need to make space to
305 * pass object as 1st arg, so move other args up ...
306 */
4352c267 307 MEXTEND(SP, 1);
68dc0745
PP
308 ++MARK;
309 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
310 ++SP;
311 }
312 PUSHMARK(MARK - 1);
236988e4 313 *MARK = mg->mg_obj;
68dc0745 314 PUTBACK;
236988e4
PP
315 ENTER;
316 perl_call_method("PRINT", G_SCALAR);
317 LEAVE;
318 SPAGAIN;
68dc0745
PP
319 MARK = ORIGMARK + 1;
320 *MARK = *SP;
321 SP = MARK;
236988e4
PP
322 RETURN;
323 }
a0d0e21e 324 if (!(io = GvIO(gv))) {
748a9306
LW
325 if (dowarn) {
326 SV* sv = sv_newmortal();
4f732624 327 gv_fullname3(sv, gv, Nullch);
748a9306
LW
328 warn("Filehandle %s never opened", SvPV(sv,na));
329 }
330
331 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
332 goto just_say_no;
333 }
334 else if (!(fp = IoOFP(io))) {
335 if (dowarn) {
748a9306 336 SV* sv = sv_newmortal();
4f732624 337 gv_fullname3(sv, gv, Nullch);
a0d0e21e 338 if (IoIFP(io))
748a9306 339 warn("Filehandle %s opened only for input", SvPV(sv,na));
a0d0e21e 340 else
748a9306 341 warn("print on closed filehandle %s", SvPV(sv,na));
a0d0e21e 342 }
748a9306 343 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
344 goto just_say_no;
345 }
346 else {
347 MARK++;
348 if (ofslen) {
349 while (MARK <= SP) {
350 if (!do_print(*MARK, fp))
351 break;
352 MARK++;
353 if (MARK <= SP) {
760ac839 354 if (PerlIO_write(fp, ofs, ofslen) == 0 || PerlIO_error(fp)) {
a0d0e21e
LW
355 MARK--;
356 break;
357 }
358 }
359 }
360 }
361 else {
362 while (MARK <= SP) {
363 if (!do_print(*MARK, fp))
364 break;
365 MARK++;
366 }
367 }
368 if (MARK <= SP)
369 goto just_say_no;
370 else {
371 if (orslen)
760ac839 372 if (PerlIO_write(fp, ors, orslen) == 0 || PerlIO_error(fp))
a0d0e21e
LW
373 goto just_say_no;
374
375 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 376 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
377 goto just_say_no;
378 }
379 }
380 SP = ORIGMARK;
381 PUSHs(&sv_yes);
382 RETURN;
383
384 just_say_no:
385 SP = ORIGMARK;
386 PUSHs(&sv_undef);
387 RETURN;
388}
389
390PP(pp_rv2av)
391{
4e35701f 392 djSP; dPOPss;
a0d0e21e
LW
393 AV *av;
394
395 if (SvROK(sv)) {
396 wasref:
397 av = (AV*)SvRV(sv);
398 if (SvTYPE(av) != SVt_PVAV)
399 DIE("Not an ARRAY reference");
a0d0e21e
LW
400 if (op->op_flags & OPf_REF) {
401 PUSHs((SV*)av);
402 RETURN;
403 }
404 }
405 else {
406 if (SvTYPE(sv) == SVt_PVAV) {
407 av = (AV*)sv;
408 if (op->op_flags & OPf_REF) {
409 PUSHs((SV*)av);
410 RETURN;
411 }
412 }
413 else {
67955e0c
PP
414 GV *gv;
415
a0d0e21e 416 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
417 char *sym;
418
a0d0e21e
LW
419 if (SvGMAGICAL(sv)) {
420 mg_get(sv);
421 if (SvROK(sv))
422 goto wasref;
423 }
424 if (!SvOK(sv)) {
425 if (op->op_flags & OPf_REF ||
426 op->op_private & HINT_STRICT_REFS)
427 DIE(no_usym, "an ARRAY");
d83e6520
CS
428 if (dowarn)
429 warn(warn_uninit);
4633a7c4
LW
430 if (GIMME == G_ARRAY)
431 RETURN;
a0d0e21e
LW
432 RETPUSHUNDEF;
433 }
748a9306 434 sym = SvPV(sv,na);
a0d0e21e 435 if (op->op_private & HINT_STRICT_REFS)
748a9306 436 DIE(no_symref, sym, "an ARRAY");
67955e0c
PP
437 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
438 } else {
439 gv = (GV*)sv;
a0d0e21e 440 }
67955e0c 441 av = GvAVn(gv);
a0d0e21e 442 if (op->op_private & OPpLVAL_INTRO)
67955e0c 443 av = save_ary(gv);
a0d0e21e
LW
444 if (op->op_flags & OPf_REF) {
445 PUSHs((SV*)av);
446 RETURN;
447 }
448 }
449 }
450
451 if (GIMME == G_ARRAY) {
452 I32 maxarg = AvFILL(av) + 1;
93965878
NIS
453 EXTEND(SP, maxarg);
454 if (SvRMAGICAL(av)) {
455 U32 i;
456 for (i=0; i < maxarg; i++) {
457 SV **svp = av_fetch(av, i, FALSE);
458 SP[i+1] = (svp) ? *svp : &sv_undef;
459 }
460 }
461 else {
462 Copy(AvARRAY(av), SP+1, maxarg, SV*);
463 }
a0d0e21e
LW
464 SP += maxarg;
465 }
466 else {
467 dTARGET;
468 I32 maxarg = AvFILL(av) + 1;
469 PUSHi(maxarg);
470 }
471 RETURN;
472}
473
474PP(pp_rv2hv)
475{
4e35701f 476 djSP; dTOPss;
a0d0e21e
LW
477 HV *hv;
478
479 if (SvROK(sv)) {
480 wasref:
481 hv = (HV*)SvRV(sv);
c750a3ec 482 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
a0d0e21e 483 DIE("Not a HASH reference");
a0d0e21e
LW
484 if (op->op_flags & OPf_REF) {
485 SETs((SV*)hv);
486 RETURN;
487 }
488 }
489 else {
c750a3ec 490 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
a0d0e21e
LW
491 hv = (HV*)sv;
492 if (op->op_flags & OPf_REF) {
493 SETs((SV*)hv);
494 RETURN;
495 }
496 }
497 else {
67955e0c
PP
498 GV *gv;
499
a0d0e21e 500 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
501 char *sym;
502
a0d0e21e
LW
503 if (SvGMAGICAL(sv)) {
504 mg_get(sv);
505 if (SvROK(sv))
506 goto wasref;
507 }
508 if (!SvOK(sv)) {
509 if (op->op_flags & OPf_REF ||
510 op->op_private & HINT_STRICT_REFS)
511 DIE(no_usym, "a HASH");
d83e6520
CS
512 if (dowarn)
513 warn(warn_uninit);
4633a7c4
LW
514 if (GIMME == G_ARRAY) {
515 SP--;
516 RETURN;
517 }
a0d0e21e
LW
518 RETSETUNDEF;
519 }
748a9306 520 sym = SvPV(sv,na);
a0d0e21e 521 if (op->op_private & HINT_STRICT_REFS)
748a9306 522 DIE(no_symref, sym, "a HASH");
67955e0c
PP
523 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
524 } else {
525 gv = (GV*)sv;
a0d0e21e 526 }
67955e0c 527 hv = GvHVn(gv);
a0d0e21e 528 if (op->op_private & OPpLVAL_INTRO)
67955e0c 529 hv = save_hash(gv);
a0d0e21e
LW
530 if (op->op_flags & OPf_REF) {
531 SETs((SV*)hv);
532 RETURN;
533 }
534 }
535 }
536
537 if (GIMME == G_ARRAY) { /* array wanted */
538 *stack_sp = (SV*)hv;
539 return do_kv(ARGS);
540 }
541 else {
542 dTARGET;
c750a3ec 543 /* This bit is OK even when hv is really an AV */
46fc3d4c
PP
544 if (HvFILL(hv))
545 sv_setpvf(TARG, "%ld/%ld",
546 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
a0d0e21e
LW
547 else
548 sv_setiv(TARG, 0);
c750a3ec 549
a0d0e21e
LW
550 SETTARG;
551 RETURN;
552 }
553}
554
555PP(pp_aassign)
556{
4e35701f 557 djSP;
a0d0e21e
LW
558 SV **lastlelem = stack_sp;
559 SV **lastrelem = stack_base + POPMARK;
560 SV **firstrelem = stack_base + POPMARK + 1;
561 SV **firstlelem = lastrelem + 1;
562
563 register SV **relem;
564 register SV **lelem;
565
566 register SV *sv;
567 register AV *ary;
568
54310121 569 I32 gimme;
a0d0e21e
LW
570 HV *hash;
571 I32 i;
572 int magic;
573
574 delaymagic = DM_DELAY; /* catch simultaneous items */
575
576 /* If there's a common identifier on both sides we have to take
577 * special care that assigning the identifier on the left doesn't
578 * clobber a value on the right that's used later in the list.
579 */
580 if (op->op_private & OPpASSIGN_COMMON) {
581 for (relem = firstrelem; relem <= lastrelem; relem++) {
582 /*SUPPRESS 560*/
a1f49e72
CS
583 if (sv = *relem) {
584 TAINT_NOT; /* Each item is independent */
a0d0e21e 585 *relem = sv_mortalcopy(sv);
a1f49e72 586 }
a0d0e21e
LW
587 }
588 }
589
590 relem = firstrelem;
591 lelem = firstlelem;
592 ary = Null(AV*);
593 hash = Null(HV*);
594 while (lelem <= lastlelem) {
bbce6d69 595 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
596 sv = *lelem++;
597 switch (SvTYPE(sv)) {
598 case SVt_PVAV:
599 ary = (AV*)sv;
748a9306 600 magic = SvMAGICAL(ary) != 0;
a0d0e21e
LW
601
602 av_clear(ary);
7e42bd57 603 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
604 i = 0;
605 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 606 SV **didstore;
a0d0e21e
LW
607 sv = NEWSV(28,0);
608 assert(*relem);
609 sv_setsv(sv,*relem);
610 *(relem++) = sv;
5117ca91
GS
611 didstore = av_store(ary,i++,sv);
612 if (magic) {
fb73857a
PP
613 if (SvSMAGICAL(sv))
614 mg_set(sv);
5117ca91
GS
615 if (!didstore)
616 SvREFCNT_dec(sv);
617 }
bbce6d69 618 TAINT_NOT;
a0d0e21e
LW
619 }
620 break;
621 case SVt_PVHV: {
a0d0e21e
LW
622 SV *tmpstr;
623
624 hash = (HV*)sv;
748a9306 625 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
626 hv_clear(hash);
627
628 while (relem < lastrelem) { /* gobble up all the rest */
5117ca91 629 HE *didstore;
4633a7c4 630 if (*relem)
a0d0e21e 631 sv = *(relem++);
4633a7c4 632 else
a0d0e21e 633 sv = &sv_no, relem++;
a0d0e21e
LW
634 tmpstr = NEWSV(29,0);
635 if (*relem)
636 sv_setsv(tmpstr,*relem); /* value */
637 *(relem++) = tmpstr;
5117ca91
GS
638 didstore = hv_store_ent(hash,sv,tmpstr,0);
639 if (magic) {
fb73857a
PP
640 if (SvSMAGICAL(tmpstr))
641 mg_set(tmpstr);
5117ca91
GS
642 if (!didstore)
643 SvREFCNT_dec(tmpstr);
644 }
bbce6d69 645 TAINT_NOT;
8e07c86e 646 }
6a0deba8
GS
647 if (relem == lastrelem) {
648 if (*relem) {
649 HE *didstore;
650 if (dowarn) {
651 if (relem == firstrelem &&
652 SvROK(*relem) &&
653 ( SvTYPE(SvRV(*relem)) == SVt_PVAV ||
654 SvTYPE(SvRV(*relem)) == SVt_PVHV ) )
655 warn("Reference found where even-sized list expected");
656 else
657 warn("Odd number of elements in hash assignment");
658 }
659 tmpstr = NEWSV(29,0);
660 didstore = hv_store_ent(hash,*relem,tmpstr,0);
661 if (magic) {
662 if (SvSMAGICAL(tmpstr))
663 mg_set(tmpstr);
664 if (!didstore)
665 SvREFCNT_dec(tmpstr);
666 }
667 TAINT_NOT;
668 }
669 relem++;
1930e939 670 }
a0d0e21e
LW
671 }
672 break;
673 default:
674 if (SvTHINKFIRST(sv)) {
675 if (SvREADONLY(sv) && curcop != &compiling) {
d689ffdd 676 if (!SvIMMORTAL(sv))
a0d0e21e
LW
677 DIE(no_modify);
678 if (relem <= lastrelem)
679 relem++;
680 break;
681 }
682 if (SvROK(sv))
683 sv_unref(sv);
684 }
685 if (relem <= lastrelem) {
686 sv_setsv(sv, *relem);
687 *(relem++) = sv;
688 }
689 else
690 sv_setsv(sv, &sv_undef);
691 SvSETMAGIC(sv);
692 break;
693 }
694 }
695 if (delaymagic & ~DM_DELAY) {
696 if (delaymagic & DM_UID) {
697#ifdef HAS_SETRESUID
698 (void)setresuid(uid,euid,(Uid_t)-1);
56febc5e
AD
699#else
700# ifdef HAS_SETREUID
a0d0e21e 701 (void)setreuid(uid,euid);
56febc5e
AD
702# else
703# ifdef HAS_SETRUID
a0d0e21e
LW
704 if ((delaymagic & DM_UID) == DM_RUID) {
705 (void)setruid(uid);
748a9306 706 delaymagic &= ~DM_RUID;
a0d0e21e 707 }
56febc5e
AD
708# endif /* HAS_SETRUID */
709# ifdef HAS_SETEUID
a0d0e21e
LW
710 if ((delaymagic & DM_UID) == DM_EUID) {
711 (void)seteuid(uid);
748a9306 712 delaymagic &= ~DM_EUID;
a0d0e21e 713 }
56febc5e 714# endif /* HAS_SETEUID */
a0d0e21e
LW
715 if (delaymagic & DM_UID) {
716 if (uid != euid)
717 DIE("No setreuid available");
76e3520e 718 (void)PerlProc_setuid(uid);
a0d0e21e 719 }
56febc5e
AD
720# endif /* HAS_SETREUID */
721#endif /* HAS_SETRESUID */
76e3520e
GS
722 uid = (int)PerlProc_getuid();
723 euid = (int)PerlProc_geteuid();
a0d0e21e
LW
724 }
725 if (delaymagic & DM_GID) {
726#ifdef HAS_SETRESGID
727 (void)setresgid(gid,egid,(Gid_t)-1);
56febc5e
AD
728#else
729# ifdef HAS_SETREGID
a0d0e21e 730 (void)setregid(gid,egid);
56febc5e
AD
731# else
732# ifdef HAS_SETRGID
a0d0e21e
LW
733 if ((delaymagic & DM_GID) == DM_RGID) {
734 (void)setrgid(gid);
748a9306 735 delaymagic &= ~DM_RGID;
a0d0e21e 736 }
56febc5e
AD
737# endif /* HAS_SETRGID */
738# ifdef HAS_SETEGID
a0d0e21e
LW
739 if ((delaymagic & DM_GID) == DM_EGID) {
740 (void)setegid(gid);
748a9306 741 delaymagic &= ~DM_EGID;
a0d0e21e 742 }
56febc5e 743# endif /* HAS_SETEGID */
a0d0e21e
LW
744 if (delaymagic & DM_GID) {
745 if (gid != egid)
746 DIE("No setregid available");
76e3520e 747 (void)PerlProc_setgid(gid);
a0d0e21e 748 }
56febc5e
AD
749# endif /* HAS_SETREGID */
750#endif /* HAS_SETRESGID */
76e3520e
GS
751 gid = (int)PerlProc_getgid();
752 egid = (int)PerlProc_getegid();
a0d0e21e 753 }
4633a7c4 754 tainting |= (uid && (euid != uid || egid != gid));
a0d0e21e
LW
755 }
756 delaymagic = 0;
54310121
PP
757
758 gimme = GIMME_V;
759 if (gimme == G_VOID)
760 SP = firstrelem - 1;
761 else if (gimme == G_SCALAR) {
762 dTARGET;
763 SP = firstrelem;
764 SETi(lastrelem - firstrelem + 1);
765 }
766 else {
a0d0e21e
LW
767 if (ary || hash)
768 SP = lastrelem;
769 else
770 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 771 lelem = firstlelem + (relem - firstrelem);
5f05dabc 772 while (relem <= SP)
0c8c7a05 773 *relem++ = (lelem <= lastlelem) ? *lelem++ : &sv_undef;
a0d0e21e 774 }
54310121 775 RETURN;
a0d0e21e
LW
776}
777
778PP(pp_match)
779{
4e35701f 780 djSP; dTARG;
a0d0e21e
LW
781 register PMOP *pm = cPMOP;
782 register char *t;
783 register char *s;
784 char *strend;
785 I32 global;
786 I32 safebase;
787 char *truebase;
d9f97599 788 register REGEXP *rx = pm->op_pmregexp;
b3eb6a9b 789 bool rxtainted;
a0d0e21e
LW
790 I32 gimme = GIMME;
791 STRLEN len;
748a9306 792 I32 minmatch = 0;
4633a7c4 793 I32 oldsave = savestack_ix;
f86702cc 794 I32 update_minmatch = 1;
c277df42 795 SV *screamer;
a0d0e21e
LW
796
797 if (op->op_flags & OPf_STACKED)
798 TARG = POPs;
799 else {
54b9620d 800 TARG = DEFSV;
a0d0e21e
LW
801 EXTEND(SP,1);
802 }
c277df42 803 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
804 s = SvPV(TARG, len);
805 strend = s + len;
806 if (!s)
807 DIE("panic: do_match");
b3eb6a9b
GS
808 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
809 (tainted && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 810 TAINT_NOT;
a0d0e21e 811
48c036b1 812 if (pm->op_pmdynflags & PMdf_USED) {
c277df42 813 failure:
a0d0e21e
LW
814 if (gimme == G_ARRAY)
815 RETURN;
816 RETPUSHNO;
817 }
818
d9f97599 819 if (!rx->prelen && curpm) {
a0d0e21e 820 pm = curpm;
d9f97599 821 rx = pm->op_pmregexp;
a0d0e21e 822 }
d9f97599 823 if (rx->minlen > len) goto failure;
c277df42 824
d9f97599
GS
825 screamer = ( (SvSCREAM(TARG) && rx->check_substr
826 && SvTYPE(rx->check_substr) == SVt_PVBM
827 && SvVALID(rx->check_substr))
c277df42 828 ? TARG : Nullsv);
a0d0e21e
LW
829 truebase = t = s;
830 if (global = pm->op_pmflags & PMf_GLOBAL) {
d9f97599 831 rx->startp[0] = 0;
a0d0e21e
LW
832 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
833 MAGIC* mg = mg_find(TARG, 'g');
565764a8 834 if (mg && mg->mg_len >= 0) {
d9f97599 835 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306 836 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 837 update_minmatch = 0;
748a9306 838 }
a0d0e21e
LW
839 }
840 }
d9f97599 841 if (!rx->nparens && !global)
748a9306 842 gimme = G_SCALAR; /* accidental array context? */
d9f97599 843 safebase = (((gimme == G_ARRAY) || global || !rx->nparens)
137443ea 844 && !sawampersand);
c277df42 845 safebase = safebase ? 0 : REXEC_COPY_STR ;
a0d0e21e
LW
846 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
847 SAVEINT(multiline);
848 multiline = pm->op_pmflags & PMf_MULTILINE;
849 }
850
851play_it_again:
d9f97599
GS
852 if (global && rx->startp[0]) {
853 t = s = rx->endp[0];
854 if ((s + rx->minlen) > strend)
a0d0e21e 855 goto nope;
f86702cc 856 if (update_minmatch++)
d9f97599 857 minmatch = (s == rx->startp[0]);
a0d0e21e 858 }
d9f97599
GS
859 if (rx->check_substr) {
860 if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
c277df42
IZ
861 if ( screamer ) {
862 I32 p = -1;
863
d9f97599 864 if (screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 865 goto nope;
d9f97599
GS
866 else if (!(s = screaminstr(TARG, rx->check_substr,
867 rx->check_offset_min, 0, &p, 0)))
a0d0e21e 868 goto nope;
d9f97599
GS
869 else if ((rx->reganch & ROPT_CHECK_ALL)
870 && !sawampersand && !SvTAIL(rx->check_substr))
a0d0e21e
LW
871 goto yup;
872 }
d9f97599 873 else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
c277df42 874 (unsigned char*)strend,
411d5715 875 rx->check_substr, 0)))
a0d0e21e 876 goto nope;
d9f97599 877 else if ((rx->reganch & ROPT_CHECK_ALL) && !sawampersand)
a0d0e21e 878 goto yup;
d9f97599
GS
879 if (s && rx->check_offset_max < t - s) {
880 ++BmUSEFUL(rx->check_substr);
881 s -= rx->check_offset_max;
a0d0e21e
LW
882 }
883 else
884 s = t;
885 }
c277df42
IZ
886 /* Now checkstring is fixed, i.e. at fixed offset from the
887 beginning of match, and the match is anchored at s. */
888 else if (!multiline) { /* Anchored near beginning of string. */
889 I32 slen;
d9f97599
GS
890 if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
891 || ((slen = SvCUR(rx->check_substr)) > 1
892 && memNE(SvPVX(rx->check_substr),
893 s + rx->check_offset_min, slen)))
bbce6d69 894 goto nope;
a0d0e21e 895 }
d9f97599
GS
896 if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
897 && rx->check_substr == rx->float_substr) {
898 SvREFCNT_dec(rx->check_substr);
899 rx->check_substr = Nullsv; /* opt is being useless */
900 rx->float_substr = Nullsv;
a0d0e21e
LW
901 }
902 }
d9f97599 903 if (regexec_flags(rx, s, strend, truebase, minmatch,
c277df42 904 screamer, NULL, safebase))
bbce6d69 905 {
a0d0e21e
LW
906 curpm = pm;
907 if (pm->op_pmflags & PMf_ONCE)
48c036b1 908 pm->op_pmdynflags |= PMdf_USED;
a0d0e21e
LW
909 goto gotcha;
910 }
911 else
912 goto ret_no;
913 /*NOTREACHED*/
914
915 gotcha:
b3eb6a9b 916 RX_MATCH_TAINTED_SET(rx, rxtainted);
a0d0e21e
LW
917 if (gimme == G_ARRAY) {
918 I32 iters, i, len;
919
d9f97599 920 iters = rx->nparens;
a0d0e21e
LW
921 if (global && !iters)
922 i = 1;
923 else
924 i = 0;
c277df42 925 SPAGAIN; /* EVAL blocks could move the stack. */
a0d0e21e 926 EXTEND(SP, iters + i);
bbce6d69 927 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
928 for (i = !i; i <= iters; i++) {
929 PUSHs(sv_newmortal());
930 /*SUPPRESS 560*/
d9f97599
GS
931 if ((s = rx->startp[i]) && rx->endp[i] ) {
932 len = rx->endp[i] - s;
a0d0e21e
LW
933 sv_setpvn(*SP, s, len);
934 }
935 }
936 if (global) {
d9f97599
GS
937 truebase = rx->subbeg;
938 strend = rx->subend;
939 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
940 ++rx->endp[0];
c277df42 941 PUTBACK; /* EVAL blocks may use stack */
a0d0e21e
LW
942 goto play_it_again;
943 }
4633a7c4 944 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
945 RETURN;
946 }
947 else {
948 if (global) {
949 MAGIC* mg = 0;
950 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
951 mg = mg_find(TARG, 'g');
952 if (!mg) {
953 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
954 mg = mg_find(TARG, 'g');
955 }
d9f97599
GS
956 if (rx->startp[0]) {
957 mg->mg_len = rx->endp[0] - rx->subbeg;
958 if (rx->startp[0] == rx->endp[0])
748a9306
LW
959 mg->mg_flags |= MGf_MINMATCH;
960 else
961 mg->mg_flags &= ~MGf_MINMATCH;
962 }
a0d0e21e 963 }
4633a7c4 964 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
965 RETPUSHYES;
966 }
967
c277df42 968yup: /* Confirmed by check_substr */
b3eb6a9b 969 RX_MATCH_TAINTED_SET(rx, rxtainted);
d9f97599 970 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
971 curpm = pm;
972 if (pm->op_pmflags & PMf_ONCE)
48c036b1 973 pm->op_pmdynflags |= PMdf_USED;
d9f97599
GS
974 Safefree(rx->subbase);
975 rx->subbase = Nullch;
a0d0e21e 976 if (global) {
d9f97599
GS
977 rx->subbeg = truebase;
978 rx->subend = strend;
979 rx->startp[0] = s;
980 rx->endp[0] = s + SvCUR(rx->check_substr);
a0d0e21e
LW
981 goto gotcha;
982 }
983 if (sawampersand) {
984 char *tmps;
985
d9f97599
GS
986 tmps = rx->subbase = savepvn(t, strend-t);
987 rx->subbeg = tmps;
988 rx->subend = tmps + (strend-t);
989 tmps = rx->startp[0] = tmps + (s - t);
990 rx->endp[0] = tmps + SvCUR(rx->check_substr);
a0d0e21e 991 }
4633a7c4 992 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
993 RETPUSHYES;
994
995nope:
d9f97599
GS
996 if (rx->check_substr)
997 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
998
999ret_no:
c90c0ff4 1000 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
1001 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1002 MAGIC* mg = mg_find(TARG, 'g');
1003 if (mg)
565764a8 1004 mg->mg_len = -1;
a0d0e21e
LW
1005 }
1006 }
4633a7c4 1007 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1008 if (gimme == G_ARRAY)
1009 RETURN;
1010 RETPUSHNO;
1011}
1012
1013OP *
8ac85365 1014do_readline(void)
a0d0e21e
LW
1015{
1016 dSP; dTARGETSTACKED;
1017 register SV *sv;
1018 STRLEN tmplen = 0;
1019 STRLEN offset;
760ac839 1020 PerlIO *fp;
a0d0e21e
LW
1021 register IO *io = GvIO(last_in_gv);
1022 register I32 type = op->op_type;
54310121 1023 I32 gimme = GIMME_V;
e79b0511 1024 MAGIC *mg;
a0d0e21e 1025
3049007d 1026 if (SvRMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
e79b0511
PP
1027 PUSHMARK(SP);
1028 XPUSHs(mg->mg_obj);
1029 PUTBACK;
1030 ENTER;
54310121 1031 perl_call_method("READLINE", gimme);
e79b0511
PP
1032 LEAVE;
1033 SPAGAIN;
54310121
PP
1034 if (gimme == G_SCALAR)
1035 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1036 RETURN;
1037 }
a0d0e21e
LW
1038 fp = Nullfp;
1039 if (io) {
1040 fp = IoIFP(io);
1041 if (!fp) {
1042 if (IoFLAGS(io) & IOf_ARGV) {
1043 if (IoFLAGS(io) & IOf_START) {
1044 IoFLAGS(io) &= ~IOf_START;
1045 IoLINES(io) = 0;
1046 if (av_len(GvAVn(last_in_gv)) < 0) {
a2008d6d
GS
1047 do_open(last_in_gv,"-",1,FALSE,0,0,Nullfp);
1048 sv_setpvn(GvSV(last_in_gv), "-", 1);
1049 SvSETMAGIC(GvSV(last_in_gv));
1050 fp = IoIFP(io);
1051 goto have_fp;
a0d0e21e
LW
1052 }
1053 }
1054 fp = nextargv(last_in_gv);
1055 if (!fp) { /* Note: fp != IoIFP(io) */
1056 (void)do_close(last_in_gv, FALSE); /* now it does*/
1057 IoFLAGS(io) |= IOf_START;
1058 }
1059 }
1060 else if (type == OP_GLOB) {
1061 SV *tmpcmd = NEWSV(55, 0);
1062 SV *tmpglob = POPs;
1063 ENTER;
1064 SAVEFREESV(tmpcmd);
1065#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1066 /* since spawning off a process is a real performance hit */
1067 {
1068#include <descrip.h>
1069#include <lib$routines.h>
1070#include <nam.h>
1071#include <rmsdef.h>
1072 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1073 char vmsspec[NAM$C_MAXRSS+1];
1074 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1075 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1076 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1077 PerlIO *tmpfp;
a0d0e21e
LW
1078 STRLEN i;
1079 struct dsc$descriptor_s wilddsc
1080 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1081 struct dsc$descriptor_vs rsdsc
1082 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1083 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1084
1085 /* We could find out if there's an explicit dev/dir or version
1086 by peeking into lib$find_file's internal context at
1087 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1088 but that's unsupported, so I don't want to do it now and
1089 have it bite someone in the future. */
6ad3d225 1090 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1091 cp = SvPV(tmpglob,i);
1092 for (; i; i--) {
1093 if (cp[i] == ';') hasver = 1;
1094 if (cp[i] == '.') {
1095 if (sts) hasver = 1;
1096 else sts = 1;
1097 }
1098 if (cp[i] == '/') {
1099 hasdir = isunix = 1;
1100 break;
748a9306 1101 }
a0d0e21e
LW
1102 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1103 hasdir = 1;
1104 break;
1105 }
1106 }
760ac839 1107 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1108 Stat_t st;
c6ed36e1 1109 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1110 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1111 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1112 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1113 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1114 &dfltdsc,NULL,NULL,NULL))&1)) {
1115 end = rstr + (unsigned long int) *rslt;
1116 if (!hasver) while (*end != ';') end--;
1117 *(end++) = '\n'; *end = '\0';
1118 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1119 if (hasdir) {
f86702cc 1120 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1121 begin = rstr;
1122 }
1123 else {
1124 begin = end;
1125 while (*(--begin) != ']' && *begin != '>') ;
1126 ++begin;
1127 }
760ac839 1128 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1129 }
1130 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1131 if (ok && sts != RMS$_NMF &&
1132 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1133 if (!ok) {
c07a80fd
PP
1134 if (!(sts & 1)) {
1135 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1136 }
760ac839 1137 PerlIO_close(tmpfp);
a0d0e21e
LW
1138 fp = NULL;
1139 }
1140 else {
760ac839 1141 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1142 IoTYPE(io) = '<';
1143 IoIFP(io) = fp = tmpfp;
1e422769 1144 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1145 }
1146 }
1147 }
1148#else /* !VMS */
1149#ifdef DOSISH
67955e0c
PP
1150#ifdef OS2
1151 sv_setpv(tmpcmd, "for a in ");
1152 sv_catsv(tmpcmd, tmpglob);
1153 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1154#else
39e571d4
ML
1155#ifdef DJGPP
1156 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1157 sv_catsv(tmpcmd, tmpglob);
1158#else
a0d0e21e
LW
1159 sv_setpv(tmpcmd, "perlglob ");
1160 sv_catsv(tmpcmd, tmpglob);
1161 sv_catpv(tmpcmd, " |");
39e571d4 1162#endif /* !DJGPP */
67955e0c
PP
1163#endif /* !OS2 */
1164#else /* !DOSISH */
1165#if defined(CSH)
a0d0e21e
LW
1166 sv_setpvn(tmpcmd, cshname, cshlen);
1167 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1168 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1169 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1170#else
1171 sv_setpv(tmpcmd, "echo ");
1172 sv_catsv(tmpcmd, tmpglob);
1173#if 'z' - 'a' == 25
1174 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1175#else
1176 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1177#endif
1178#endif /* !CSH */
67955e0c 1179#endif /* !DOSISH */
c07a80fd
PP
1180 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1181 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1182 fp = IoIFP(io);
1183#endif /* !VMS */
1184 LEAVE;
1185 }
1186 }
1187 else if (type == OP_GLOB)
1188 SP--;
1189 }
1190 if (!fp) {
4633a7c4 1191 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e 1192 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
54310121 1193 if (gimme == G_SCALAR) {
a0d0e21e
LW
1194 (void)SvOK_off(TARG);
1195 PUSHTARG;
1196 }
1197 RETURN;
1198 }
a2008d6d 1199 have_fp:
54310121 1200 if (gimme == G_SCALAR) {
a0d0e21e 1201 sv = TARG;
9607fc9c
PP
1202 if (SvROK(sv))
1203 sv_unref(sv);
a0d0e21e
LW
1204 (void)SvUPGRADE(sv, SVt_PV);
1205 tmplen = SvLEN(sv); /* remember if already alloced */
1206 if (!tmplen)
1207 Sv_Grow(sv, 80); /* try short-buffering it */
1208 if (type == OP_RCATLINE)
1209 offset = SvCUR(sv);
1210 else
1211 offset = 0;
1212 }
54310121
PP
1213 else {
1214 sv = sv_2mortal(NEWSV(57, 80));
1215 offset = 0;
1216 }
a0d0e21e
LW
1217 for (;;) {
1218 if (!sv_gets(sv, fp, offset)) {
760ac839 1219 PerlIO_clearerr(fp);
a0d0e21e
LW
1220 if (IoFLAGS(io) & IOf_ARGV) {
1221 fp = nextargv(last_in_gv);
1222 if (fp)
1223 continue;
1224 (void)do_close(last_in_gv, FALSE);
1225 IoFLAGS(io) |= IOf_START;
1226 }
1227 else if (type == OP_GLOB) {
5cd24f17
PP
1228 if (do_close(last_in_gv, FALSE) & ~0xFF)
1229 warn("internal error: glob failed");
a0d0e21e 1230 }
54310121 1231 if (gimme == G_SCALAR) {
a0d0e21e
LW
1232 (void)SvOK_off(TARG);
1233 PUSHTARG;
1234 }
1235 RETURN;
1236 }
bbce6d69
PP
1237 /* This should not be marked tainted if the fp is marked clean */
1238 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1239 TAINT;
1240 SvTAINTED_on(sv);
1241 }
a0d0e21e 1242 IoLINES(io)++;
71be2cbc 1243 SvSETMAGIC(sv);
a0d0e21e 1244 XPUSHs(sv);
a0d0e21e
LW
1245 if (type == OP_GLOB) {
1246 char *tmps;
1247
c07a80fd
PP
1248 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1249 tmps = SvEND(sv) - 1;
1250 if (*tmps == *SvPVX(rs)) {
1251 *tmps = '\0';
1252 SvCUR(sv)--;
1253 }
1254 }
a0d0e21e
LW
1255 for (tmps = SvPVX(sv); *tmps; tmps++)
1256 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1257 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1258 break;
76e3520e 1259 if (*tmps && PerlLIO_stat(SvPVX(sv), &statbuf) < 0) {
a0d0e21e
LW
1260 (void)POPs; /* Unmatched wildcard? Chuck it... */
1261 continue;
1262 }
1263 }
54310121 1264 if (gimme == G_ARRAY) {
a0d0e21e
LW
1265 if (SvLEN(sv) - SvCUR(sv) > 20) {
1266 SvLEN_set(sv, SvCUR(sv)+1);
1267 Renew(SvPVX(sv), SvLEN(sv), char);
1268 }
1269 sv = sv_2mortal(NEWSV(58, 80));
1270 continue;
1271 }
54310121 1272 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1273 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1274 if (SvCUR(sv) < 60)
1275 SvLEN_set(sv, 80);
1276 else
1277 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1278 Renew(SvPVX(sv), SvLEN(sv), char);
1279 }
1280 RETURN;
1281 }
1282}
1283
1284PP(pp_enter)
1285{
4e35701f 1286 djSP;
c09156bb 1287 register PERL_CONTEXT *cx;
54310121 1288 I32 gimme = OP_GIMME(op, -1);
a0d0e21e 1289
54310121
PP
1290 if (gimme == -1) {
1291 if (cxstack_ix >= 0)
1292 gimme = cxstack[cxstack_ix].blk_gimme;
1293 else
1294 gimme = G_SCALAR;
1295 }
a0d0e21e
LW
1296
1297 ENTER;
1298
1299 SAVETMPS;
924508f0 1300 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1301
1302 RETURN;
1303}
1304
1305PP(pp_helem)
1306{
4e35701f 1307 djSP;
760ac839 1308 HE* he;
ae77835f 1309 SV **svp;
a0d0e21e 1310 SV *keysv = POPs;
a0d0e21e 1311 HV *hv = (HV*)POPs;
68dc0745
PP
1312 U32 lval = op->op_flags & OPf_MOD;
1313 U32 defer = op->op_private & OPpLVAL_DEFER;
a0d0e21e 1314
ae77835f
MB
1315 if (SvTYPE(hv) == SVt_PVHV) {
1316 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1317 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1318 }
1319 else if (SvTYPE(hv) == SVt_PVAV) {
97fcbf96 1320 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1321 }
c750a3ec 1322 else {
a0d0e21e 1323 RETPUSHUNDEF;
c750a3ec 1324 }
a0d0e21e 1325 if (lval) {
97fcbf96 1326 if (!svp || *svp == &sv_undef) {
68dc0745
PP
1327 SV* lv;
1328 SV* key2;
1329 if (!defer)
1330 DIE(no_helem, SvPV(keysv, na));
1331 lv = sv_newmortal();
1332 sv_upgrade(lv, SVt_PVLV);
1333 LvTYPE(lv) = 'y';
1334 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1335 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1336 LvTARG(lv) = SvREFCNT_inc(hv);
1337 LvTARGLEN(lv) = 1;
1338 PUSHs(lv);
1339 RETURN;
1340 }
5f05dabc 1341 if (op->op_private & OPpLVAL_INTRO) {
ae77835f
MB
1342 if (HvNAME(hv) && isGV(*svp))
1343 save_gp((GV*)*svp, !(op->op_flags & OPf_SPECIAL));
5f05dabc 1344 else
161b7d16 1345 save_helem(hv, keysv, svp);
5f05dabc
PP
1346 }
1347 else if (op->op_private & OPpDEREF)
ae77835f 1348 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
1349 }
1350 PUSHs(svp ? *svp : &sv_undef);
1351 RETURN;
1352}
1353
1354PP(pp_leave)
1355{
4e35701f 1356 djSP;
c09156bb 1357 register PERL_CONTEXT *cx;
a0d0e21e
LW
1358 register SV **mark;
1359 SV **newsp;
1360 PMOP *newpm;
1361 I32 gimme;
1362
1363 if (op->op_flags & OPf_SPECIAL) {
1364 cx = &cxstack[cxstack_ix];
1365 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1366 }
1367
1368 POPBLOCK(cx,newpm);
1369
54310121
PP
1370 gimme = OP_GIMME(op, -1);
1371 if (gimme == -1) {
1372 if (cxstack_ix >= 0)
1373 gimme = cxstack[cxstack_ix].blk_gimme;
1374 else
1375 gimme = G_SCALAR;
1376 }
a0d0e21e 1377
a1f49e72 1378 TAINT_NOT;
54310121
PP
1379 if (gimme == G_VOID)
1380 SP = newsp;
1381 else if (gimme == G_SCALAR) {
1382 MARK = newsp + 1;
1383 if (MARK <= SP)
1384 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1385 *MARK = TOPs;
1386 else
1387 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1388 else {
54310121
PP
1389 MEXTEND(mark,0);
1390 *MARK = &sv_undef;
a0d0e21e 1391 }
54310121 1392 SP = MARK;
a0d0e21e 1393 }
54310121 1394 else if (gimme == G_ARRAY) {
a1f49e72
CS
1395 /* in case LEAVE wipes old return values */
1396 for (mark = newsp + 1; mark <= SP; mark++) {
1397 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1398 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1399 TAINT_NOT; /* Each item is independent */
1400 }
1401 }
a0d0e21e
LW
1402 }
1403 curpm = newpm; /* Don't pop $1 et al till now */
1404
1405 LEAVE;
1406
1407 RETURN;
1408}
1409
1410PP(pp_iter)
1411{
4e35701f 1412 djSP;
c09156bb 1413 register PERL_CONTEXT *cx;
5f05dabc 1414 SV* sv;
4633a7c4 1415 AV* av;
a0d0e21e 1416
924508f0 1417 EXTEND(SP, 1);
a0d0e21e
LW
1418 cx = &cxstack[cxstack_ix];
1419 if (cx->cx_type != CXt_LOOP)
1420 DIE("panic: pp_iter");
1421
4633a7c4 1422 av = cx->blk_loop.iterary;
89ea2908
GA
1423 if (SvTYPE(av) != SVt_PVAV) {
1424 /* iterate ($min .. $max) */
1425 if (cx->blk_loop.iterlval) {
1426 /* string increment */
1427 register SV* cur = cx->blk_loop.iterlval;
1428 STRLEN maxlen;
1429 char *max = SvPV((SV*)av, maxlen);
1430 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1431 sv_setsv(*cx->blk_loop.itervar, cur);
1432 if (strEQ(SvPVX(cur), max))
1433 sv_setiv(cur, 0); /* terminate next time */
1434 else
1435 sv_inc(cur);
1436 RETPUSHYES;
1437 }
1438 RETPUSHNO;
1439 }
1440 /* integer increment */
1441 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1442 RETPUSHNO;
1443 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1444 RETPUSHYES;
1445 }
1446
1447 /* iterate array */
71be2cbc 1448 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1449 RETPUSHNO;
a0d0e21e 1450
7a4c00b4 1451 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1452
93965878
NIS
1453 if (sv = (SvMAGICAL(av))
1454 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1455 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1456 SvTEMP_off(sv);
a0d0e21e 1457 else
5f05dabc
PP
1458 sv = &sv_undef;
1459 if (av != curstack && SvIMMORTAL(sv)) {
1460 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1461 if (lv && SvREFCNT(lv) > 1) {
1462 SvREFCNT_dec(lv);
1463 lv = Nullsv;
1464 }
5f05dabc
PP
1465 if (lv)
1466 SvREFCNT_dec(LvTARG(lv));
1467 else {
68dc0745 1468 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1469 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1470 LvTYPE(lv) = 'y';
68dc0745 1471 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1472 }
1473 LvTARG(lv) = SvREFCNT_inc(av);
1474 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1475 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1476 sv = (SV*)lv;
1477 }
a0d0e21e 1478
7a4c00b4 1479 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1480 RETPUSHYES;
1481}
1482
1483PP(pp_subst)
1484{
4e35701f 1485 djSP; dTARG;
a0d0e21e
LW
1486 register PMOP *pm = cPMOP;
1487 PMOP *rpm = pm;
1488 register SV *dstr;
1489 register char *s;
1490 char *strend;
1491 register char *m;
1492 char *c;
1493 register char *d;
1494 STRLEN clen;
1495 I32 iters = 0;
1496 I32 maxiters;
1497 register I32 i;
1498 bool once;
71be2cbc 1499 bool rxtainted;
a0d0e21e
LW
1500 char *orig;
1501 I32 safebase;
d9f97599 1502 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1503 STRLEN len;
1504 int force_on_match = 0;
4633a7c4 1505 I32 oldsave = savestack_ix;
c277df42
IZ
1506 I32 update_minmatch = 1;
1507 SV *screamer;
a0d0e21e 1508
5cd24f17
PP
1509 /* known replacement string? */
1510 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1511 if (op->op_flags & OPf_STACKED)
1512 TARG = POPs;
1513 else {
54b9620d 1514 TARG = DEFSV;
a0d0e21e 1515 EXTEND(SP,1);
8ec5e241 1516 }
68dc0745
PP
1517 if (SvREADONLY(TARG)
1518 || (SvTYPE(TARG) > SVt_PVLV
1519 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1520 croak(no_modify);
8ec5e241
NIS
1521 PUTBACK;
1522
a0d0e21e 1523 s = SvPV(TARG, len);
68dc0745 1524 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1525 force_on_match = 1;
b3eb6a9b
GS
1526 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1527 (tainted && (pm->op_pmflags & PMf_RETAINT)));
1528 if (tainted)
1529 rxtainted |= 2;
9212bbba 1530 TAINT_NOT;
a0d0e21e
LW
1531
1532 force_it:
1533 if (!pm || !s)
1534 DIE("panic: do_subst");
1535
1536 strend = s + len;
1537 maxiters = (strend - s) + 10;
1538
d9f97599 1539 if (!rx->prelen && curpm) {
a0d0e21e 1540 pm = curpm;
d9f97599 1541 rx = pm->op_pmregexp;
a0d0e21e 1542 }
d9f97599
GS
1543 screamer = ( (SvSCREAM(TARG) && rx->check_substr
1544 && SvTYPE(rx->check_substr) == SVt_PVBM
1545 && SvVALID(rx->check_substr))
c277df42 1546 ? TARG : Nullsv);
d9f97599 1547 safebase = (!rx->nparens && !sawampersand) ? 0 : REXEC_COPY_STR;
a0d0e21e
LW
1548 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1549 SAVEINT(multiline);
1550 multiline = pm->op_pmflags & PMf_MULTILINE;
1551 }
1552 orig = m = s;
d9f97599
GS
1553 if (rx->check_substr) {
1554 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
c277df42
IZ
1555 if (screamer) {
1556 I32 p = -1;
1557
d9f97599 1558 if (screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1559 goto nope;
d9f97599 1560 else if (!(s = screaminstr(TARG, rx->check_substr, rx->check_offset_min, 0, &p, 0)))
a0d0e21e
LW
1561 goto nope;
1562 }
d9f97599 1563 else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
c277df42 1564 (unsigned char*)strend,
411d5715 1565 rx->check_substr, 0)))
a0d0e21e 1566 goto nope;
d9f97599
GS
1567 if (s && rx->check_offset_max < s - m) {
1568 ++BmUSEFUL(rx->check_substr);
1569 s -= rx->check_offset_max;
a0d0e21e
LW
1570 }
1571 else
1572 s = m;
1573 }
c277df42
IZ
1574 /* Now checkstring is fixed, i.e. at fixed offset from the
1575 beginning of match, and the match is anchored at s. */
1576 else if (!multiline) { /* Anchored at beginning of string. */
1577 I32 slen;
d9f97599
GS
1578 if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
1579 || ((slen = SvCUR(rx->check_substr)) > 1
1580 && memNE(SvPVX(rx->check_substr),
1581 s + rx->check_offset_min, slen)))
bbce6d69 1582 goto nope;
a0d0e21e 1583 }
d9f97599
GS
1584 if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
1585 && rx->check_substr == rx->float_substr) {
1586 SvREFCNT_dec(rx->check_substr);
1587 rx->check_substr = Nullsv; /* opt is being useless */
1588 rx->float_substr = Nullsv;
a0d0e21e
LW
1589 }
1590 }
71be2cbc
PP
1591
1592 /* only replace once? */
a0d0e21e 1593 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1594
1595 /* known replacement string? */
5cd24f17 1596 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1597
1598 /* can do inplace substitution? */
d9f97599
GS
1599 if (c && clen <= rx->minlen && (once || !(safebase & REXEC_COPY_STR))
1600 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
1601 if (!regexec_flags(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
8ec5e241 1602 SPAGAIN;
71be2cbc
PP
1603 PUSHs(&sv_no);
1604 LEAVE_SCOPE(oldsave);
1605 RETURN;
1606 }
1607 if (force_on_match) {
1608 force_on_match = 0;
1609 s = SvPV_force(TARG, len);
1610 goto force_it;
1611 }
71be2cbc
PP
1612 d = s;
1613 curpm = pm;
1614 SvSCREAM_off(TARG); /* disable possible screamer */
1615 if (once) {
48c036b1 1616 rxtainted |= RX_MATCH_TAINTED(rx);
d9f97599
GS
1617 if (rx->subbase) {
1618 m = orig + (rx->startp[0] - rx->subbase);
1619 d = orig + (rx->endp[0] - rx->subbase);
c277df42 1620 } else {
d9f97599
GS
1621 m = rx->startp[0];
1622 d = rx->endp[0];
c277df42 1623 }
71be2cbc
PP
1624 s = orig;
1625 if (m - s > strend - d) { /* faster to shorten from end */
1626 if (clen) {
1627 Copy(c, m, clen, char);
1628 m += clen;
a0d0e21e 1629 }
71be2cbc
PP
1630 i = strend - d;
1631 if (i > 0) {
1632 Move(d, m, i, char);
1633 m += i;
a0d0e21e 1634 }
71be2cbc
PP
1635 *m = '\0';
1636 SvCUR_set(TARG, m - s);
1637 }
1638 /*SUPPRESS 560*/
1639 else if (i = m - s) { /* faster from front */
1640 d -= clen;
1641 m = d;
1642 sv_chop(TARG, d-i);
1643 s += i;
1644 while (i--)
1645 *--d = *--s;
1646 if (clen)
1647 Copy(c, m, clen, char);
1648 }
1649 else if (clen) {
1650 d -= clen;
1651 sv_chop(TARG, d);
1652 Copy(c, d, clen, char);
1653 }
1654 else {
1655 sv_chop(TARG, d);
1656 }
48c036b1 1657 TAINT_IF(rxtainted & 1);
8ec5e241 1658 SPAGAIN;
71be2cbc
PP
1659 PUSHs(&sv_yes);
1660 }
1661 else {
71be2cbc
PP
1662 do {
1663 if (iters++ > maxiters)
1664 DIE("Substitution loop");
d9f97599
GS
1665 rxtainted |= RX_MATCH_TAINTED(rx);
1666 m = rx->startp[0];
71be2cbc
PP
1667 /*SUPPRESS 560*/
1668 if (i = m - s) {
1669 if (s != d)
1670 Move(s, d, i, char);
1671 d += i;
a0d0e21e 1672 }
71be2cbc
PP
1673 if (clen) {
1674 Copy(c, d, clen, char);
1675 d += clen;
1676 }
d9f97599
GS
1677 s = rx->endp[0];
1678 } while (regexec_flags(rx, s, strend, orig, s == m,
c277df42 1679 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1680 if (s != d) {
1681 i = strend - s;
1682 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1683 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1684 }
48c036b1 1685 TAINT_IF(rxtainted & 1);
8ec5e241 1686 SPAGAIN;
71be2cbc 1687 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1688 }
71be2cbc 1689 (void)SvPOK_only(TARG);
48c036b1 1690 TAINT_IF(rxtainted);
8ec5e241
NIS
1691 if (SvSMAGICAL(TARG)) {
1692 PUTBACK;
1693 mg_set(TARG);
1694 SPAGAIN;
1695 }
9212bbba 1696 SvTAINT(TARG);
71be2cbc
PP
1697 LEAVE_SCOPE(oldsave);
1698 RETURN;
a0d0e21e 1699 }
71be2cbc 1700
d9f97599 1701 if (regexec_flags(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
a0d0e21e
LW
1702 if (force_on_match) {
1703 force_on_match = 0;
1704 s = SvPV_force(TARG, len);
1705 goto force_it;
1706 }
48c036b1 1707 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1708 dstr = NEWSV(25, len);
a0d0e21e
LW
1709 sv_setpvn(dstr, m, s-m);
1710 curpm = pm;
1711 if (!c) {
c09156bb 1712 register PERL_CONTEXT *cx;
8ec5e241 1713 SPAGAIN;
a0d0e21e
LW
1714 PUSHSUBST(cx);
1715 RETURNOP(cPMOP->op_pmreplroot);
1716 }
1717 do {
1718 if (iters++ > maxiters)
1719 DIE("Substitution loop");
d9f97599
GS
1720 rxtainted |= RX_MATCH_TAINTED(rx);
1721 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
1722 m = s;
1723 s = orig;
d9f97599 1724 orig = rx->subbase;
a0d0e21e
LW
1725 s = orig + (m - s);
1726 strend = s + (strend - m);
1727 }
d9f97599 1728 m = rx->startp[0];
a0d0e21e 1729 sv_catpvn(dstr, s, m-s);
d9f97599 1730 s = rx->endp[0];
a0d0e21e
LW
1731 if (clen)
1732 sv_catpvn(dstr, c, clen);
1733 if (once)
1734 break;
d9f97599 1735 } while (regexec_flags(rx, s, strend, orig, s == m, Nullsv, NULL, safebase));
a0d0e21e 1736 sv_catpvn(dstr, s, strend - s);
748a9306 1737
4633a7c4 1738 (void)SvOOK_off(TARG);
cb0b1708 1739 Safefree(SvPVX(TARG));
748a9306
LW
1740 SvPVX(TARG) = SvPVX(dstr);
1741 SvCUR_set(TARG, SvCUR(dstr));
1742 SvLEN_set(TARG, SvLEN(dstr));
1743 SvPVX(dstr) = 0;
1744 sv_free(dstr);
1745
48c036b1 1746 TAINT_IF(rxtainted & 1);
f878fbec 1747 SPAGAIN;
48c036b1
GS
1748 PUSHs(sv_2mortal(newSViv((I32)iters)));
1749
a0d0e21e 1750 (void)SvPOK_only(TARG);
48c036b1 1751 TAINT_IF(rxtainted);
a0d0e21e 1752 SvSETMAGIC(TARG);
9212bbba 1753 SvTAINT(TARG);
4633a7c4 1754 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1755 RETURN;
1756 }
5cd24f17 1757 goto ret_no;
a0d0e21e
LW
1758
1759nope:
d9f97599 1760 ++BmUSEFUL(rx->check_substr);
5cd24f17 1761
8ec5e241
NIS
1762ret_no:
1763 SPAGAIN;
a0d0e21e 1764 PUSHs(&sv_no);
4633a7c4 1765 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1766 RETURN;
1767}
1768
1769PP(pp_grepwhile)
1770{
4e35701f 1771 djSP;
a0d0e21e
LW
1772
1773 if (SvTRUEx(POPs))
1774 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1775 ++*markstack_ptr;
1776 LEAVE; /* exit inner scope */
1777
1778 /* All done yet? */
924508f0 1779 if (stack_base + *markstack_ptr > SP) {
a0d0e21e 1780 I32 items;
54310121 1781 I32 gimme = GIMME_V;
a0d0e21e
LW
1782
1783 LEAVE; /* exit outer scope */
1784 (void)POPMARK; /* pop src */
1785 items = --*markstack_ptr - markstack_ptr[-1];
1786 (void)POPMARK; /* pop dst */
1787 SP = stack_base + POPMARK; /* pop original mark */
54310121 1788 if (gimme == G_SCALAR) {
a0d0e21e
LW
1789 dTARGET;
1790 XPUSHi(items);
a0d0e21e 1791 }
54310121
PP
1792 else if (gimme == G_ARRAY)
1793 SP += items;
a0d0e21e
LW
1794 RETURN;
1795 }
1796 else {
1797 SV *src;
1798
1799 ENTER; /* enter inner scope */
1800 SAVESPTR(curpm);
1801
1802 src = stack_base[*markstack_ptr];
1803 SvTEMP_off(src);
54b9620d 1804 DEFSV = src;
a0d0e21e
LW
1805
1806 RETURNOP(cLOGOP->op_other);
1807 }
1808}
1809
1810PP(pp_leavesub)
1811{
4e35701f 1812 djSP;
a0d0e21e
LW
1813 SV **mark;
1814 SV **newsp;
1815 PMOP *newpm;
1816 I32 gimme;
c09156bb 1817 register PERL_CONTEXT *cx;
f86702cc 1818 struct block_sub cxsub;
a0d0e21e
LW
1819
1820 POPBLOCK(cx,newpm);
f86702cc
PP
1821 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1822
a1f49e72 1823 TAINT_NOT;
a0d0e21e
LW
1824 if (gimme == G_SCALAR) {
1825 MARK = newsp + 1;
a29cdaf0
IZ
1826 if (MARK <= SP) {
1827 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1828 if (SvTEMP(TOPs)) {
1829 *MARK = SvREFCNT_inc(TOPs);
1830 FREETMPS;
1831 sv_2mortal(*MARK);
1832 } else {
1833 FREETMPS;
1834 *MARK = sv_mortalcopy(TOPs);
1835 }
1836 } else
1837 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1838 } else {
f86702cc 1839 MEXTEND(MARK, 0);
a0d0e21e
LW
1840 *MARK = &sv_undef;
1841 }
1842 SP = MARK;
1843 }
54310121 1844 else if (gimme == G_ARRAY) {
f86702cc 1845 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1846 if (!SvTEMP(*MARK)) {
f86702cc 1847 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1848 TAINT_NOT; /* Each item is independent */
1849 }
f86702cc 1850 }
a0d0e21e 1851 }
f86702cc
PP
1852 PUTBACK;
1853
1854 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1855 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1856
1857 LEAVE;
a0d0e21e
LW
1858 return pop_return();
1859}
1860
76e3520e 1861STATIC CV *
8ac85365 1862get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
1863{
1864 dTHR;
491527d0
GS
1865 SV *dbsv = GvSV(DBsub);
1866
1867 if (!PERLDB_SUB_NN) {
1868 GV *gv = CvGV(cv);
1869
1870 save_item(dbsv);
1871 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1872 || strEQ(GvNAME(gv), "END")
1873 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1874 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
1875 && (gv = (GV*)*svp) ))) {
1876 /* Use GV from the stack as a fallback. */
1877 /* GV is potentially non-unique, or contain different CV. */
1878 sv_setsv(dbsv, newRV((SV*)cv));
1879 }
1880 else {
1881 gv_efullname3(dbsv, gv, Nullch);
1882 }
3de9ffa1
MB
1883 }
1884 else {
491527d0
GS
1885 SvUPGRADE(dbsv, SVt_PVIV);
1886 SvIOK_on(dbsv);
1887 SAVEIV(SvIVX(dbsv));
1888 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 1889 }
491527d0 1890
3de9ffa1
MB
1891 if (CvXSUB(cv))
1892 curcopdb = curcop;
491527d0 1893 cv = GvCV(DBsub);
3de9ffa1
MB
1894 return cv;
1895}
1896
a0d0e21e
LW
1897PP(pp_entersub)
1898{
4e35701f 1899 djSP; dPOPss;
a0d0e21e
LW
1900 GV *gv;
1901 HV *stash;
1902 register CV *cv;
c09156bb 1903 register PERL_CONTEXT *cx;
5d94fbed 1904 I32 gimme;
07055b4c 1905 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1906
1907 if (!sv)
1908 DIE("Not a CODE reference");
1909 switch (SvTYPE(sv)) {
1910 default:
1911 if (!SvROK(sv)) {
748a9306
LW
1912 char *sym;
1913
fb73857a
PP
1914 if (sv == &sv_yes) { /* unfound import, ignore */
1915 if (hasargs)
1916 SP = stack_base + POPMARK;
a0d0e21e 1917 RETURN;
fb73857a 1918 }
15ff848f
CS
1919 if (SvGMAGICAL(sv)) {
1920 mg_get(sv);
1921 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1922 }
1923 else
1924 sym = SvPV(sv, na);
1925 if (!sym)
a0d0e21e
LW
1926 DIE(no_usym, "a subroutine");
1927 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1928 DIE(no_symref, sym, "a subroutine");
1929 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1930 break;
1931 }
1932 cv = (CV*)SvRV(sv);
1933 if (SvTYPE(cv) == SVt_PVCV)
1934 break;
1935 /* FALL THROUGH */
1936 case SVt_PVHV:
1937 case SVt_PVAV:
1938 DIE("Not a CODE reference");
1939 case SVt_PVCV:
1940 cv = (CV*)sv;
1941 break;
1942 case SVt_PVGV:
8ebc5c01 1943 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1944 cv = sv_2cv(sv, &stash, &gv, TRUE);
1945 break;
1946 }
1947
1948 ENTER;
1949 SAVETMPS;
1950
1951 retry:
1952 if (!cv)
1953 DIE("Not a CODE reference");
1954
1955 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 1956 GV* autogv;
22239a37 1957 SV* sub_name;
44a8e56a
PP
1958
1959 /* anonymous or undef'd function leaves us no recourse */
1960 if (CvANON(cv) || !(gv = CvGV(cv)))
1961 DIE("Undefined subroutine called");
1962 /* autoloaded stub? */
1963 if (cv != GvCV(gv)) {
1964 cv = GvCV(gv);
1965 goto retry;
a0d0e21e 1966 }
44a8e56a 1967 /* should call AUTOLOAD now? */
54310121
PP
1968 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
1969 FALSE)))
1970 {
44a8e56a
PP
1971 cv = GvCV(autogv);
1972 goto retry;
a0d0e21e 1973 }
44a8e56a 1974 /* sorry */
22239a37
NIS
1975 sub_name = sv_newmortal();
1976 gv_efullname3(sub_name, gv, Nullch);
1977 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
a0d0e21e
LW
1978 }
1979
54310121 1980 gimme = GIMME_V;
3de9ffa1 1981 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv))
4f01c5a5 1982 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
1983 if (!cv)
1984 DIE("No DBsub routine");
a0d0e21e 1985
11343788 1986#ifdef USE_THREADS
3de9ffa1
MB
1987 /*
1988 * First we need to check if the sub or method requires locking.
458fb581
MB
1989 * If so, we gain a lock on the CV, the first argument or the
1990 * stash (for static methods), as appropriate. This has to be
1991 * inline because for FAKE_THREADS, COND_WAIT inlines code to
1992 * reschedule by returning a new op.
3de9ffa1 1993 */
11343788 1994 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
1995 if (CvFLAGS(cv) & CVf_LOCKED) {
1996 MAGIC *mg;
1997 if (CvFLAGS(cv) & CVf_METHOD) {
1998 if (SP > stack_base + TOPMARK)
1999 sv = *(stack_base + TOPMARK + 1);
2000 else {
2001 MUTEX_UNLOCK(CvMUTEXP(cv));
2002 croak("no argument for locked method call");
2003 }
2004 if (SvROK(sv))
2005 sv = SvRV(sv);
458fb581
MB
2006 else {
2007 STRLEN len;
2008 char *stashname = SvPV(sv, len);
2009 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2010 }
77a005ab
MB
2011 }
2012 else {
2013 sv = (SV*)cv;
2014 }
2015 MUTEX_UNLOCK(CvMUTEXP(cv));
2016 mg = condpair_magic(sv);
2017 MUTEX_LOCK(MgMUTEXP(mg));
2018 if (MgOWNER(mg) == thr)
2019 MUTEX_UNLOCK(MgMUTEXP(mg));
2020 else {
2021 while (MgOWNER(mg))
2022 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2023 MgOWNER(mg) = thr;
1fd28e87
MB
2024 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
2025 thr, sv);)
77a005ab 2026 MUTEX_UNLOCK(MgMUTEXP(mg));
e55aaa0e 2027 SvREFCNT_inc(sv); /* Keep alive until magic_mutexfree */
77a005ab 2028 save_destructor(unlock_condpair, sv);
11343788 2029 }
77a005ab 2030 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2031 }
3de9ffa1
MB
2032 /*
2033 * Now we have permission to enter the sub, we must distinguish
2034 * four cases. (0) It's an XSUB (in which case we don't care
2035 * about ownership); (1) it's ours already (and we're recursing);
2036 * (2) it's free (but we may already be using a cached clone);
2037 * (3) another thread owns it. Case (1) is easy: we just use it.
2038 * Case (2) means we look for a clone--if we have one, use it
2039 * otherwise grab ownership of cv. Case (3) means we look for a
2040 * clone (for non-XSUBs) and have to create one if we don't
2041 * already have one.
2042 * Why look for a clone in case (2) when we could just grab
2043 * ownership of cv straight away? Well, we could be recursing,
2044 * i.e. we originally tried to enter cv while another thread
2045 * owned it (hence we used a clone) but it has been freed up
2046 * and we're now recursing into it. It may or may not be "better"
2047 * to use the clone but at least CvDEPTH can be trusted.
2048 */
2049 if (CvOWNER(cv) == thr || CvXSUB(cv))
2050 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2051 else {
3de9ffa1
MB
2052 /* Case (2) or (3) */
2053 SV **svp;
2054
11343788 2055 /*
3de9ffa1
MB
2056 * XXX Might it be better to release CvMUTEXP(cv) while we
2057 * do the hv_fetch? We might find someone has pinched it
2058 * when we look again, in which case we would be in case
2059 * (3) instead of (2) so we'd have to clone. Would the fact
2060 * that we released the mutex more quickly make up for this?
2061 */
6ee623d5
GS
2062 if (threadnum &&
2063 (svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2064 {
3de9ffa1 2065 /* We already have a clone to use */
11343788 2066 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1
MB
2067 cv = *(CV**)svp;
2068 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2069 "entersub: %p already has clone %p:%s\n",
2070 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2071 CvOWNER(cv) = thr;
2072 SvREFCNT_inc(cv);
2073 if (CvDEPTH(cv) == 0)
2074 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2075 }
11343788 2076 else {
3de9ffa1
MB
2077 /* (2) => grab ownership of cv. (3) => make clone */
2078 if (!CvOWNER(cv)) {
2079 CvOWNER(cv) = thr;
2080 SvREFCNT_inc(cv);
11343788 2081 MUTEX_UNLOCK(CvMUTEXP(cv));
d9f997d7 2082 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2083 "entersub: %p grabbing %p:%s in stash %s\n",
2084 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2085 HvNAME(CvSTASH(cv)) : "(none)"));
2086 } else {
2087 /* Make a new clone. */
2088 CV *clonecv;
2089 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2090 MUTEX_UNLOCK(CvMUTEXP(cv));
2091 DEBUG_L((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2092 "entersub: %p cloning %p:%s\n",
2093 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2094 /*
2095 * We're creating a new clone so there's no race
2096 * between the original MUTEX_UNLOCK and the
2097 * SvREFCNT_inc since no one will be trying to undef
2098 * it out from underneath us. At least, I don't think
2099 * there's a race...
2100 */
2101 clonecv = cv_clone(cv);
2102 SvREFCNT_dec(cv); /* finished with this */
199100c8 2103 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2104 CvOWNER(clonecv) = thr;
2105 cv = clonecv;
11343788 2106 SvREFCNT_inc(cv);
11343788 2107 }
3de9ffa1
MB
2108 DEBUG_L(if (CvDEPTH(cv) != 0)
2109 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2110 CvDEPTH(cv)););
2111 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2112 }
3de9ffa1 2113 }
11343788
MB
2114#endif /* USE_THREADS */
2115
a0d0e21e
LW
2116 if (CvXSUB(cv)) {
2117 if (CvOLDSTYLE(cv)) {
ecfc5424 2118 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2119 dMARK;
2120 register I32 items = SP - MARK;
67955e0c 2121 /* We dont worry to copy from @_. */
924508f0
GS
2122 while (SP > mark) {
2123 SP[1] = SP[0];
2124 SP--;
a0d0e21e
LW
2125 }
2126 stack_sp = mark + 1;
ecfc5424
AD
2127 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2128 items = (*fp3)(CvXSUBANY(cv).any_i32,
2129 MARK - stack_base + 1,
2130 items);
a0d0e21e
LW
2131 stack_sp = stack_base + items;
2132 }
2133 else {
748a9306
LW
2134 I32 markix = TOPMARK;
2135
a0d0e21e 2136 PUTBACK;
67955e0c
PP
2137
2138 if (!hasargs) {
2139 /* Need to copy @_ to stack. Alternative may be to
2140 * switch stack to @_, and copy return values
2141 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2142 AV* av;
2143 I32 items;
2144#ifdef USE_THREADS
2145 av = (AV*)curpad[0];
2146#else
2147 av = GvAV(defgv);
2148#endif /* USE_THREADS */
93965878 2149 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2150
2151 if (items) {
2152 /* Mark is at the end of the stack. */
924508f0
GS
2153 EXTEND(SP, items);
2154 Copy(AvARRAY(av), SP + 1, items, SV*);
2155 SP += items;
67955e0c
PP
2156 PUTBACK ;
2157 }
2158 }
2159 if (curcopdb) { /* We assume that the first
2160 XSUB in &DB::sub is the
2161 called one. */
2162 SAVESPTR(curcop);
2163 curcop = curcopdb;
2164 curcopdb = NULL;
2165 }
2166 /* Do we need to open block here? XXXX */
1d583055 2167 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
748a9306
LW
2168
2169 /* Enforce some sanity in scalar context. */
5d94fbed 2170 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
2171 if (markix > stack_sp - stack_base)
2172 *(stack_base + markix) = &sv_undef;
2173 else
2174 *(stack_base + markix) = *stack_sp;
2175 stack_sp = stack_base + markix;
2176 }
a0d0e21e
LW
2177 }
2178 LEAVE;
2179 return NORMAL;
2180 }
2181 else {
2182 dMARK;
2183 register I32 items = SP - MARK;
a0d0e21e
LW
2184 AV* padlist = CvPADLIST(cv);
2185 SV** svp = AvARRAY(padlist);
2186 push_return(op->op_next);
2187 PUSHBLOCK(cx, CXt_SUB, MARK);
2188 PUSHSUB(cx);
2189 CvDEPTH(cv)++;
2190 if (CvDEPTH(cv) < 2)
2191 (void)SvREFCNT_inc(cv);
2192 else { /* save temporaries on recursion? */
5f05dabc 2193 if (CvDEPTH(cv) == 100 && dowarn
84902520 2194 && !(PERLDB_SUB && cv == GvCV(DBsub)))
44a8e56a 2195 sub_crush_depth(cv);
93965878 2196 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2197 AV *av;
2198 AV *newpad = newAV();
4aa0a1f7 2199 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2200 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2201 svp = AvARRAY(svp[0]);
748a9306 2202 for ( ;ix > 0; ix--) {
a0d0e21e 2203 if (svp[ix] != &sv_undef) {
748a9306 2204 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2205 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2206 || *name == '&') /* anonymous code? */
2207 {
2208 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2209 }
2210 else { /* our own lexical */
2211 if (*name == '@')
2212 av_store(newpad, ix, sv = (SV*)newAV());
2213 else if (*name == '%')
2214 av_store(newpad, ix, sv = (SV*)newHV());
2215 else
2216 av_store(newpad, ix, sv = NEWSV(0,0));
2217 SvPADMY_on(sv);
2218 }
a0d0e21e
LW
2219 }
2220 else {
748a9306 2221 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2222 SvPADTMP_on(sv);
2223 }
2224 }
2225 av = newAV(); /* will be @_ */
2226 av_extend(av, 0);
2227 av_store(newpad, 0, (SV*)av);
2228 AvFLAGS(av) = AVf_REIFY;
2229 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2230 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2231 svp = AvARRAY(padlist);
2232 }
2233 }
6d4ff0d2
MB
2234#ifdef USE_THREADS
2235 if (!hasargs) {
a0d0e21e 2236 AV* av = (AV*)curpad[0];
6d4ff0d2 2237
93965878 2238 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2239 if (items) {
2240 /* Mark is at the end of the stack. */
924508f0
GS
2241 EXTEND(SP, items);
2242 Copy(AvARRAY(av), SP + 1, items, SV*);
2243 SP += items;
6d4ff0d2
MB
2244 PUTBACK ;
2245 }
2246 }
2247#endif /* USE_THREADS */
2248 SAVESPTR(curpad);
2249 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2250#ifndef USE_THREADS
2251 if (hasargs)
2252#endif /* USE_THREADS */
2253 {
2254 AV* av;
a0d0e21e
LW
2255 SV** ary;
2256
77a005ab 2257#if 0
0f15f207
MB
2258 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2259 "%p entersub preparing @_\n", thr));
77a005ab 2260#endif
6d4ff0d2 2261 av = (AV*)curpad[0];
a0d0e21e
LW
2262 if (AvREAL(av)) {
2263 av_clear(av);
2264 AvREAL_off(av);
2265 }
6d4ff0d2 2266#ifndef USE_THREADS
a0d0e21e 2267 cx->blk_sub.savearray = GvAV(defgv);
0c8c7a05 2268 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2269#endif /* USE_THREADS */
2270 cx->blk_sub.argarray = av;
a0d0e21e
LW
2271 ++MARK;
2272
2273 if (items > AvMAX(av) + 1) {
2274 ary = AvALLOC(av);
2275 if (AvARRAY(av) != ary) {
2276 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2277 SvPVX(av) = (char*)ary;
2278 }
2279 if (items > AvMAX(av) + 1) {
2280 AvMAX(av) = items - 1;
2281 Renew(ary,items,SV*);
2282 AvALLOC(av) = ary;
2283 SvPVX(av) = (char*)ary;
2284 }
2285 }
2286 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2287 AvFILLp(av) = items - 1;
a0d0e21e
LW
2288
2289 while (items--) {
2290 if (*MARK)
2291 SvTEMP_off(*MARK);
2292 MARK++;
2293 }
2294 }
77a005ab 2295#if 0
0f15f207
MB
2296 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2297 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2298#endif
a0d0e21e
LW
2299 RETURNOP(CvSTART(cv));
2300 }
2301}
2302
44a8e56a 2303void
8ac85365 2304sub_crush_depth(CV *cv)
44a8e56a
PP
2305{
2306 if (CvANON(cv))
2307 warn("Deep recursion on anonymous subroutine");
2308 else {
2309 SV* tmpstr = sv_newmortal();
2310 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2311 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
2312 }
2313}
2314
a0d0e21e
LW
2315PP(pp_aelem)
2316{
4e35701f 2317 djSP;
a0d0e21e 2318 SV** svp;
748a9306 2319 I32 elem = POPi;
68dc0745
PP
2320 AV* av = (AV*)POPs;
2321 U32 lval = op->op_flags & OPf_MOD;
2322 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
a0d0e21e 2323
748a9306
LW
2324 if (elem > 0)
2325 elem -= curcop->cop_arybase;
a0d0e21e
LW
2326 if (SvTYPE(av) != SVt_PVAV)
2327 RETPUSHUNDEF;
68dc0745 2328 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2329 if (lval) {
68dc0745
PP
2330 if (!svp || *svp == &sv_undef) {
2331 SV* lv;
2332 if (!defer)
2333 DIE(no_aelem, elem);
2334 lv = sv_newmortal();
2335 sv_upgrade(lv, SVt_PVLV);
2336 LvTYPE(lv) = 'y';
2337 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2338 LvTARG(lv) = SvREFCNT_inc(av);
2339 LvTARGOFF(lv) = elem;
2340 LvTARGLEN(lv) = 1;
2341 PUSHs(lv);
2342 RETURN;
2343 }
a0d0e21e 2344 if (op->op_private & OPpLVAL_INTRO)
161b7d16 2345 save_aelem(av, elem, svp);
5f05dabc 2346 else if (op->op_private & OPpDEREF)
68dc0745 2347 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
2348 }
2349 PUSHs(svp ? *svp : &sv_undef);
2350 RETURN;
2351}
2352
02a9e968 2353void
8ac85365 2354vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2355{
2356 if (SvGMAGICAL(sv))
2357 mg_get(sv);
2358 if (!SvOK(sv)) {
2359 if (SvREADONLY(sv))
2360 croak(no_modify);
5f05dabc
PP
2361 if (SvTYPE(sv) < SVt_RV)
2362 sv_upgrade(sv, SVt_RV);
2363 else if (SvTYPE(sv) >= SVt_PV) {
2364 (void)SvOOK_off(sv);
2365 Safefree(SvPVX(sv));
2366 SvLEN(sv) = SvCUR(sv) = 0;
2367 }
68dc0745 2368 switch (to_what) {
5f05dabc 2369 case OPpDEREF_SV:
8c52afec 2370 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2371 break;
2372 case OPpDEREF_AV:
2373 SvRV(sv) = (SV*)newAV();
2374 break;
2375 case OPpDEREF_HV:
2376 SvRV(sv) = (SV*)newHV();
2377 break;
2378 }
02a9e968
CS
2379 SvROK_on(sv);
2380 SvSETMAGIC(sv);
2381 }
2382}
2383
a0d0e21e
LW
2384PP(pp_method)
2385{
4e35701f 2386 djSP;
a0d0e21e
LW
2387 SV* sv;
2388 SV* ob;
2389 GV* gv;
56304f61
CS
2390 HV* stash;
2391 char* name;
ac91690f
CS
2392 char* packname;
2393 STRLEN packlen;
a0d0e21e 2394
fb73857a
PP
2395 if (SvROK(TOPs)) {
2396 sv = SvRV(TOPs);
2397 if (SvTYPE(sv) == SVt_PVCV) {
2398 SETs(sv);
2399 RETURN;
2400 }
2401 }
2402
56304f61 2403 name = SvPV(TOPs, na);
a0d0e21e
LW
2404 sv = *(stack_base + TOPMARK + 1);
2405
16d20bd9
AD
2406 if (SvGMAGICAL(sv))
2407 mg_get(sv);
a0d0e21e 2408 if (SvROK(sv))
16d20bd9 2409 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2410 else {
2411 GV* iogv;
a0d0e21e 2412
56304f61 2413 packname = Nullch;
a0d0e21e 2414 if (!SvOK(sv) ||
56304f61 2415 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2416 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2417 !(ob=(SV*)GvIO(iogv)))
2418 {
56304f61 2419 if (!packname || !isIDFIRST(*packname))
ac91690f 2420 DIE("Can't call method \"%s\" without a package or object reference", name);
56304f61 2421 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2422 goto fetch;
a0d0e21e 2423 }
ac91690f 2424 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2425 }
2426
ac91690f 2427 if (!ob || !SvOBJECT(ob))
a0d0e21e 2428 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2429
56304f61 2430 stash = SvSTASH(ob);
a0d0e21e 2431
ac91690f 2432 fetch:
ac91690f 2433 gv = gv_fetchmethod(stash, name);
56304f61
CS
2434 if (!gv) {
2435 char* leaf = name;
2436 char* sep = Nullch;
2437 char* p;
2438
2439 for (p = name; *p; p++) {
2440 if (*p == '\'')
2441 sep = p, leaf = p + 1;
2442 else if (*p == ':' && *(p + 1) == ':')
2443 sep = p, leaf = p + 2;
2444 }
2445 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2446 packname = HvNAME(sep ? curcop->cop_stash : stash);
2447 packlen = strlen(packname);
2448 }
2449 else {
2450 packname = name;
2451 packlen = sep - name;
2452 }
2453 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2454 leaf, (int)packlen, packname);
2455 }
8ebc5c01 2456 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2457 RETURN;
2458}
22239a37 2459