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