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