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