This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Typo in thread.h: ADD_THREAD_INTERN should be HAVE_THREAD_INTERN
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
19#include "perl.h"
20
8ac85365
NIS
21#ifdef I_UNISTD
22#include <unistd.h>
23#endif
24
a0d0e21e
LW
25/* Hot code. */
26
11343788
MB
27#ifdef USE_THREADS
28static void
8ac85365 29unset_cvowner(void *cvarg)
11343788
MB
30{
31 register CV* cv = (CV *) cvarg;
32#ifdef DEBUGGING
33 dTHR;
34#endif /* DEBUGGING */
35
1fd28e87
MB
36 DEBUG_L((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
37 thr, cv, SvPEEK((SV*)cv))));
11343788 38 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
39 DEBUG_L(if (CvDEPTH(cv) != 0)
40 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
41 CvDEPTH(cv)););
11343788
MB
42 assert(thr == CvOWNER(cv));
43 CvOWNER(cv) = 0;
11343788
MB
44 MUTEX_UNLOCK(CvMUTEXP(cv));
45 SvREFCNT_dec(cv);
46}
11343788
MB
47#endif /* USE_THREADS */
48
a0d0e21e
LW
49PP(pp_const)
50{
4e35701f 51 djSP;
a0d0e21e
LW
52 XPUSHs(cSVOP->op_sv);
53 RETURN;
54}
55
56PP(pp_nextstate)
57{
58 curcop = (COP*)op;
59 TAINT_NOT; /* Each statement is presumed innocent */
60 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
61 FREETMPS;
62 return NORMAL;
63}
64
65PP(pp_gvsv)
66{
4e35701f 67 djSP;
a0d0e21e
LW
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{
4e35701f 89 djSP; dTARGET;
a0d0e21e
LW
90 STRLEN len;
91 char *s;
92 s = SvPV(TOPs,len);
93 sv_setpvn(TARG,s,len);
94 SETTARG;
95 RETURN;
96}
97
98PP(pp_gv)
99{
4e35701f 100 djSP;
a0d0e21e
LW
101 XPUSHs((SV*)cGVOP->op_gv);
102 RETURN;
103}
104
105PP(pp_and)
106{
4e35701f 107 djSP;
a0d0e21e
LW
108 if (!SvTRUE(TOPs))
109 RETURN;
110 else {
111 --SP;
112 RETURNOP(cLOGOP->op_other);
113 }
114}
115
116PP(pp_sassign)
117{
4e35701f 118 djSP; dPOPTOPssrl;
748a9306
LW
119 MAGIC *mg;
120
a0d0e21e
LW
121 if (op->op_private & OPpASSIGN_BACKWARDS) {
122 SV *temp;
123 temp = left; left = right; right = temp;
124 }
bbce6d69 125 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 126 TAINT_NOT;
54310121 127 SvSetMagicSV(right, left);
a0d0e21e
LW
128 SETs(right);
129 RETURN;
130}
131
132PP(pp_cond_expr)
133{
4e35701f 134 djSP;
a0d0e21e
LW
135 if (SvTRUEx(POPs))
136 RETURNOP(cCONDOP->op_true);
137 else
138 RETURNOP(cCONDOP->op_false);
139}
140
141PP(pp_unstack)
142{
143 I32 oldsave;
144 TAINT_NOT; /* Each statement is presumed innocent */
145 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
146 FREETMPS;
147 oldsave = scopestack[scopestack_ix - 1];
148 LEAVE_SCOPE(oldsave);
149 return NORMAL;
150}
151
a0d0e21e
LW
152PP(pp_concat)
153{
4e35701f 154 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
155 {
156 dPOPTOPssrl;
a0d0e21e
LW
157 STRLEN len;
158 char *s;
159 if (TARG != left) {
160 s = SvPV(left,len);
161 sv_setpvn(TARG,s,len);
162 }
c07a80fd
PP
163 else if (SvGMAGICAL(TARG))
164 mg_get(TARG);
7a4c00b4 165 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 166 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 167 s = SvPV_force(TARG, len);
4633a7c4 168 }
a0d0e21e 169 s = SvPV(right,len);
68dc0745
PP
170 if (SvOK(TARG))
171 sv_catpvn(TARG,s,len);
172 else
173 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
174 SETTARG;
175 RETURN;
748a9306 176 }
a0d0e21e
LW
177}
178
179PP(pp_padsv)
180{
4e35701f 181 djSP; dTARGET;
a0d0e21e 182 XPUSHs(TARG);
4633a7c4
LW
183 if (op->op_flags & OPf_MOD) {
184 if (op->op_private & OPpLVAL_INTRO)
185 SAVECLEARSV(curpad[op->op_targ]);
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{
4e35701f 200 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
201 {
202 dPOPnv;
54310121 203 SETs(boolSV(TOPn == value));
a0d0e21e
LW
204 RETURN;
205 }
206}
207
208PP(pp_preinc)
209{
4e35701f 210 djSP;
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{
4e35701f 227 djSP;
a0d0e21e
LW
228 if (SvTRUE(TOPs))
229 RETURN;
230 else {
231 --SP;
232 RETURNOP(cLOGOP->op_other);
233 }
234}
235
236PP(pp_add)
237{
4e35701f 238 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 239 {
7a4c00b4 240 dPOPTOPnnrl_ul;
a0d0e21e
LW
241 SETn( left + right );
242 RETURN;
243 }
244}
245
246PP(pp_aelemfast)
247{
4e35701f 248 djSP;
a0d0e21e
LW
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{
4e35701f 257 djSP; dMARK; dTARGET;
a0d0e21e
LW
258 MARK++;
259 do_join(TARG, *MARK, MARK, SP);
260 SP = MARK;
261 SETs(TARG);
262 RETURN;
263}
264
265PP(pp_pushre)
266{
4e35701f 267 djSP;
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{
4e35701f 288 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
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{
4e35701f 385 djSP; 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{
4e35701f 460 djSP; 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{
4e35701f 541 djSP;
a0d0e21e
LW
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{
4e35701f 743 djSP; dTARG;
a0d0e21e
LW
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
LW
954{
955 dSP; dTARGETSTACKED;
956 register SV *sv;
957 STRLEN tmplen = 0;
958 STRLEN offset;
760ac839 959 PerlIO *fp;
a0d0e21e
LW
960 register IO *io = GvIO(last_in_gv);
961 register I32 type = op->op_type;
54310121 962 I32 gimme = GIMME_V;
e79b0511 963 MAGIC *mg;
a0d0e21e 964
e79b0511
PP
965 if (SvMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
966 PUSHMARK(SP);
967 XPUSHs(mg->mg_obj);
968 PUTBACK;
969 ENTER;
54310121 970 perl_call_method("READLINE", gimme);
e79b0511
PP
971 LEAVE;
972 SPAGAIN;
54310121
PP
973 if (gimme == G_SCALAR)
974 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
975 RETURN;
976 }
a0d0e21e
LW
977 fp = Nullfp;
978 if (io) {
979 fp = IoIFP(io);
980 if (!fp) {
981 if (IoFLAGS(io) & IOf_ARGV) {
982 if (IoFLAGS(io) & IOf_START) {
983 IoFLAGS(io) &= ~IOf_START;
984 IoLINES(io) = 0;
985 if (av_len(GvAVn(last_in_gv)) < 0) {
986 SV *tmpstr = newSVpv("-", 1); /* assume stdin */
987 av_push(GvAVn(last_in_gv), tmpstr);
988 }
989 }
990 fp = nextargv(last_in_gv);
991 if (!fp) { /* Note: fp != IoIFP(io) */
992 (void)do_close(last_in_gv, FALSE); /* now it does*/
993 IoFLAGS(io) |= IOf_START;
994 }
995 }
996 else if (type == OP_GLOB) {
997 SV *tmpcmd = NEWSV(55, 0);
998 SV *tmpglob = POPs;
999 ENTER;
1000 SAVEFREESV(tmpcmd);
1001#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1002 /* since spawning off a process is a real performance hit */
1003 {
1004#include <descrip.h>
1005#include <lib$routines.h>
1006#include <nam.h>
1007#include <rmsdef.h>
1008 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1009 char vmsspec[NAM$C_MAXRSS+1];
1010 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1011 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1012 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1013 PerlIO *tmpfp;
a0d0e21e
LW
1014 STRLEN i;
1015 struct dsc$descriptor_s wilddsc
1016 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1017 struct dsc$descriptor_vs rsdsc
1018 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1019 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1020
1021 /* We could find out if there's an explicit dev/dir or version
1022 by peeking into lib$find_file's internal context at
1023 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1024 but that's unsupported, so I don't want to do it now and
1025 have it bite someone in the future. */
1026 strcat(tmpfnam,tmpnam(NULL));
1027 cp = SvPV(tmpglob,i);
1028 for (; i; i--) {
1029 if (cp[i] == ';') hasver = 1;
1030 if (cp[i] == '.') {
1031 if (sts) hasver = 1;
1032 else sts = 1;
1033 }
1034 if (cp[i] == '/') {
1035 hasdir = isunix = 1;
1036 break;
748a9306 1037 }
a0d0e21e
LW
1038 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1039 hasdir = 1;
1040 break;
1041 }
1042 }
760ac839 1043 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
a0d0e21e
LW
1044 ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
1045 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1046 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1047 &dfltdsc,NULL,NULL,NULL))&1)) {
1048 end = rstr + (unsigned long int) *rslt;
1049 if (!hasver) while (*end != ';') end--;
1050 *(end++) = '\n'; *end = '\0';
1051 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1052 if (hasdir) {
f86702cc 1053 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1054 begin = rstr;
1055 }
1056 else {
1057 begin = end;
1058 while (*(--begin) != ']' && *begin != '>') ;
1059 ++begin;
1060 }
760ac839 1061 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1062 }
1063 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1064 if (ok && sts != RMS$_NMF &&
1065 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1066 if (!ok) {
c07a80fd
PP
1067 if (!(sts & 1)) {
1068 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1069 }
760ac839 1070 PerlIO_close(tmpfp);
a0d0e21e
LW
1071 fp = NULL;
1072 }
1073 else {
760ac839 1074 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1075 IoTYPE(io) = '<';
1076 IoIFP(io) = fp = tmpfp;
1e422769 1077 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1078 }
1079 }
1080 }
1081#else /* !VMS */
1082#ifdef DOSISH
67955e0c
PP
1083#ifdef OS2
1084 sv_setpv(tmpcmd, "for a in ");
1085 sv_catsv(tmpcmd, tmpglob);
1086 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1087#else
a0d0e21e
LW
1088 sv_setpv(tmpcmd, "perlglob ");
1089 sv_catsv(tmpcmd, tmpglob);
1090 sv_catpv(tmpcmd, " |");
67955e0c
PP
1091#endif /* !OS2 */
1092#else /* !DOSISH */
1093#if defined(CSH)
a0d0e21e
LW
1094 sv_setpvn(tmpcmd, cshname, cshlen);
1095 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1096 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1097 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1098#else
1099 sv_setpv(tmpcmd, "echo ");
1100 sv_catsv(tmpcmd, tmpglob);
1101#if 'z' - 'a' == 25
1102 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1103#else
1104 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1105#endif
1106#endif /* !CSH */
67955e0c 1107#endif /* !DOSISH */
c07a80fd
PP
1108 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1109 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1110 fp = IoIFP(io);
1111#endif /* !VMS */
1112 LEAVE;
1113 }
1114 }
1115 else if (type == OP_GLOB)
1116 SP--;
1117 }
1118 if (!fp) {
4633a7c4 1119 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e 1120 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
54310121 1121 if (gimme == G_SCALAR) {
a0d0e21e
LW
1122 (void)SvOK_off(TARG);
1123 PUSHTARG;
1124 }
1125 RETURN;
1126 }
54310121 1127 if (gimme == G_SCALAR) {
a0d0e21e 1128 sv = TARG;
9607fc9c
PP
1129 if (SvROK(sv))
1130 sv_unref(sv);
a0d0e21e
LW
1131 (void)SvUPGRADE(sv, SVt_PV);
1132 tmplen = SvLEN(sv); /* remember if already alloced */
1133 if (!tmplen)
1134 Sv_Grow(sv, 80); /* try short-buffering it */
1135 if (type == OP_RCATLINE)
1136 offset = SvCUR(sv);
1137 else
1138 offset = 0;
1139 }
54310121
PP
1140 else {
1141 sv = sv_2mortal(NEWSV(57, 80));
1142 offset = 0;
1143 }
a0d0e21e
LW
1144 for (;;) {
1145 if (!sv_gets(sv, fp, offset)) {
760ac839 1146 PerlIO_clearerr(fp);
a0d0e21e
LW
1147 if (IoFLAGS(io) & IOf_ARGV) {
1148 fp = nextargv(last_in_gv);
1149 if (fp)
1150 continue;
1151 (void)do_close(last_in_gv, FALSE);
1152 IoFLAGS(io) |= IOf_START;
1153 }
1154 else if (type == OP_GLOB) {
5cd24f17
PP
1155 if (do_close(last_in_gv, FALSE) & ~0xFF)
1156 warn("internal error: glob failed");
a0d0e21e 1157 }
54310121 1158 if (gimme == G_SCALAR) {
a0d0e21e
LW
1159 (void)SvOK_off(TARG);
1160 PUSHTARG;
1161 }
1162 RETURN;
1163 }
bbce6d69
PP
1164 /* This should not be marked tainted if the fp is marked clean */
1165 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1166 TAINT;
1167 SvTAINTED_on(sv);
1168 }
a0d0e21e 1169 IoLINES(io)++;
71be2cbc 1170 SvSETMAGIC(sv);
a0d0e21e 1171 XPUSHs(sv);
a0d0e21e
LW
1172 if (type == OP_GLOB) {
1173 char *tmps;
1174
c07a80fd
PP
1175 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1176 tmps = SvEND(sv) - 1;
1177 if (*tmps == *SvPVX(rs)) {
1178 *tmps = '\0';
1179 SvCUR(sv)--;
1180 }
1181 }
a0d0e21e
LW
1182 for (tmps = SvPVX(sv); *tmps; tmps++)
1183 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1184 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1185 break;
1186 if (*tmps && Stat(SvPVX(sv), &statbuf) < 0) {
1187 (void)POPs; /* Unmatched wildcard? Chuck it... */
1188 continue;
1189 }
1190 }
54310121 1191 if (gimme == G_ARRAY) {
a0d0e21e
LW
1192 if (SvLEN(sv) - SvCUR(sv) > 20) {
1193 SvLEN_set(sv, SvCUR(sv)+1);
1194 Renew(SvPVX(sv), SvLEN(sv), char);
1195 }
1196 sv = sv_2mortal(NEWSV(58, 80));
1197 continue;
1198 }
54310121 1199 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1200 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1201 if (SvCUR(sv) < 60)
1202 SvLEN_set(sv, 80);
1203 else
1204 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1205 Renew(SvPVX(sv), SvLEN(sv), char);
1206 }
1207 RETURN;
1208 }
1209}
1210
1211PP(pp_enter)
1212{
4e35701f 1213 djSP;
a0d0e21e 1214 register CONTEXT *cx;
54310121 1215 I32 gimme = OP_GIMME(op, -1);
a0d0e21e 1216
54310121
PP
1217 if (gimme == -1) {
1218 if (cxstack_ix >= 0)
1219 gimme = cxstack[cxstack_ix].blk_gimme;
1220 else
1221 gimme = G_SCALAR;
1222 }
a0d0e21e
LW
1223
1224 ENTER;
1225
1226 SAVETMPS;
1227 PUSHBLOCK(cx, CXt_BLOCK, sp);
1228
1229 RETURN;
1230}
1231
1232PP(pp_helem)
1233{
4e35701f 1234 djSP;
760ac839 1235 HE* he;
ae77835f 1236 SV **svp;
a0d0e21e 1237 SV *keysv = POPs;
a0d0e21e 1238 HV *hv = (HV*)POPs;
68dc0745
PP
1239 U32 lval = op->op_flags & OPf_MOD;
1240 U32 defer = op->op_private & OPpLVAL_DEFER;
a0d0e21e 1241
ae77835f
MB
1242 if (SvTYPE(hv) == SVt_PVHV) {
1243 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1244 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1245 }
1246 else if (SvTYPE(hv) == SVt_PVAV) {
97fcbf96 1247 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1248 }
c750a3ec 1249 else {
a0d0e21e 1250 RETPUSHUNDEF;
c750a3ec 1251 }
a0d0e21e 1252 if (lval) {
97fcbf96 1253 if (!svp || *svp == &sv_undef) {
68dc0745
PP
1254 SV* lv;
1255 SV* key2;
1256 if (!defer)
1257 DIE(no_helem, SvPV(keysv, na));
1258 lv = sv_newmortal();
1259 sv_upgrade(lv, SVt_PVLV);
1260 LvTYPE(lv) = 'y';
1261 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1262 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1263 LvTARG(lv) = SvREFCNT_inc(hv);
1264 LvTARGLEN(lv) = 1;
1265 PUSHs(lv);
1266 RETURN;
1267 }
5f05dabc 1268 if (op->op_private & OPpLVAL_INTRO) {
ae77835f
MB
1269 if (HvNAME(hv) && isGV(*svp))
1270 save_gp((GV*)*svp, !(op->op_flags & OPf_SPECIAL));
5f05dabc 1271 else
ae77835f 1272 save_svref(svp);
5f05dabc
PP
1273 }
1274 else if (op->op_private & OPpDEREF)
ae77835f 1275 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
1276 }
1277 PUSHs(svp ? *svp : &sv_undef);
1278 RETURN;
1279}
1280
1281PP(pp_leave)
1282{
4e35701f 1283 djSP;
a0d0e21e
LW
1284 register CONTEXT *cx;
1285 register SV **mark;
1286 SV **newsp;
1287 PMOP *newpm;
1288 I32 gimme;
1289
1290 if (op->op_flags & OPf_SPECIAL) {
1291 cx = &cxstack[cxstack_ix];
1292 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1293 }
1294
1295 POPBLOCK(cx,newpm);
1296
54310121
PP
1297 gimme = OP_GIMME(op, -1);
1298 if (gimme == -1) {
1299 if (cxstack_ix >= 0)
1300 gimme = cxstack[cxstack_ix].blk_gimme;
1301 else
1302 gimme = G_SCALAR;
1303 }
a0d0e21e 1304
a1f49e72 1305 TAINT_NOT;
54310121
PP
1306 if (gimme == G_VOID)
1307 SP = newsp;
1308 else if (gimme == G_SCALAR) {
1309 MARK = newsp + 1;
1310 if (MARK <= SP)
1311 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1312 *MARK = TOPs;
1313 else
1314 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1315 else {
54310121
PP
1316 MEXTEND(mark,0);
1317 *MARK = &sv_undef;
a0d0e21e 1318 }
54310121 1319 SP = MARK;
a0d0e21e 1320 }
54310121 1321 else if (gimme == G_ARRAY) {
a1f49e72
CS
1322 /* in case LEAVE wipes old return values */
1323 for (mark = newsp + 1; mark <= SP; mark++) {
1324 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1325 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1326 TAINT_NOT; /* Each item is independent */
1327 }
1328 }
a0d0e21e
LW
1329 }
1330 curpm = newpm; /* Don't pop $1 et al till now */
1331
1332 LEAVE;
1333
1334 RETURN;
1335}
1336
1337PP(pp_iter)
1338{
4e35701f 1339 djSP;
a0d0e21e 1340 register CONTEXT *cx;
5f05dabc 1341 SV* sv;
4633a7c4 1342 AV* av;
a0d0e21e
LW
1343
1344 EXTEND(sp, 1);
1345 cx = &cxstack[cxstack_ix];
1346 if (cx->cx_type != CXt_LOOP)
1347 DIE("panic: pp_iter");
1348
4633a7c4 1349 av = cx->blk_loop.iterary;
71be2cbc 1350 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1351 RETPUSHNO;
a0d0e21e 1352
7a4c00b4 1353 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1354
5f05dabc 1355 if (sv = AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1356 SvTEMP_off(sv);
a0d0e21e 1357 else
5f05dabc
PP
1358 sv = &sv_undef;
1359 if (av != curstack && SvIMMORTAL(sv)) {
1360 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1361 if (lv && SvREFCNT(lv) > 1) {
1362 SvREFCNT_dec(lv);
1363 lv = Nullsv;
1364 }
5f05dabc
PP
1365 if (lv)
1366 SvREFCNT_dec(LvTARG(lv));
1367 else {
68dc0745 1368 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1369 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1370 LvTYPE(lv) = 'y';
68dc0745 1371 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1372 }
1373 LvTARG(lv) = SvREFCNT_inc(av);
1374 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1375 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1376 sv = (SV*)lv;
1377 }
a0d0e21e 1378
7a4c00b4 1379 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1380 RETPUSHYES;
1381}
1382
1383PP(pp_subst)
1384{
4e35701f 1385 djSP; dTARG;
a0d0e21e
LW
1386 register PMOP *pm = cPMOP;
1387 PMOP *rpm = pm;
1388 register SV *dstr;
1389 register char *s;
1390 char *strend;
1391 register char *m;
1392 char *c;
1393 register char *d;
1394 STRLEN clen;
1395 I32 iters = 0;
1396 I32 maxiters;
1397 register I32 i;
1398 bool once;
71be2cbc 1399 bool rxtainted;
a0d0e21e
LW
1400 char *orig;
1401 I32 safebase;
1402 register REGEXP *rx = pm->op_pmregexp;
1403 STRLEN len;
1404 int force_on_match = 0;
4633a7c4 1405 I32 oldsave = savestack_ix;
a0d0e21e 1406
5cd24f17
PP
1407 /* known replacement string? */
1408 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1409 if (op->op_flags & OPf_STACKED)
1410 TARG = POPs;
1411 else {
1412 TARG = GvSV(defgv);
1413 EXTEND(SP,1);
1414 }
68dc0745
PP
1415 if (SvREADONLY(TARG)
1416 || (SvTYPE(TARG) > SVt_PVLV
1417 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1418 croak(no_modify);
a0d0e21e 1419 s = SvPV(TARG, len);
68dc0745 1420 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1421 force_on_match = 1;
9212bbba 1422 TAINT_NOT;
a0d0e21e
LW
1423
1424 force_it:
1425 if (!pm || !s)
1426 DIE("panic: do_subst");
1427
1428 strend = s + len;
1429 maxiters = (strend - s) + 10;
1430
1431 if (!rx->prelen && curpm) {
1432 pm = curpm;
1433 rx = pm->op_pmregexp;
1434 }
5f05dabc 1435 safebase = (!rx->nparens && !sawampersand);
a0d0e21e
LW
1436 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1437 SAVEINT(multiline);
1438 multiline = pm->op_pmflags & PMf_MULTILINE;
1439 }
1440 orig = m = s;
1441 if (pm->op_pmshort) {
1442 if (pm->op_pmflags & PMf_SCANFIRST) {
1443 if (SvSCREAM(TARG)) {
1444 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
1445 goto nope;
1446 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
1447 goto nope;
1448 }
1449 else if (!(s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
1450 pm->op_pmshort)))
1451 goto nope;
1452 if (s && rx->regback >= 0) {
1453 ++BmUSEFUL(pm->op_pmshort);
1454 s -= rx->regback;
1455 if (s < m)
1456 s = m;
1457 }
1458 else
1459 s = m;
1460 }
1461 else if (!multiline) {
bbce6d69
PP
1462 if (*SvPVX(pm->op_pmshort) != *s
1463 || (pm->op_pmslen > 1
36477c24 1464 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 1465 goto nope;
a0d0e21e
LW
1466 }
1467 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
1468 SvREFCNT_dec(pm->op_pmshort);
1469 pm->op_pmshort = Nullsv; /* opt is being useless */
1470 }
1471 }
71be2cbc
PP
1472
1473 /* only replace once? */
a0d0e21e 1474 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1475
1476 /* known replacement string? */
5cd24f17 1477 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1478
1479 /* can do inplace substitution? */
137443ea 1480 if (c && clen <= rx->minlen && safebase) {
71be2cbc
PP
1481 if (! pregexec(rx, s, strend, orig, 0,
1482 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1483 PUSHs(&sv_no);
1484 LEAVE_SCOPE(oldsave);
1485 RETURN;
1486 }
1487 if (force_on_match) {
1488 force_on_match = 0;
1489 s = SvPV_force(TARG, len);
1490 goto force_it;
1491 }
71be2cbc
PP
1492 d = s;
1493 curpm = pm;
1494 SvSCREAM_off(TARG); /* disable possible screamer */
1495 if (once) {
1496 rxtainted = rx->exec_tainted;
1497 m = rx->startp[0];
1498 d = rx->endp[0];
1499 s = orig;
1500 if (m - s > strend - d) { /* faster to shorten from end */
1501 if (clen) {
1502 Copy(c, m, clen, char);
1503 m += clen;
a0d0e21e 1504 }
71be2cbc
PP
1505 i = strend - d;
1506 if (i > 0) {
1507 Move(d, m, i, char);
1508 m += i;
a0d0e21e 1509 }
71be2cbc
PP
1510 *m = '\0';
1511 SvCUR_set(TARG, m - s);
1512 }
1513 /*SUPPRESS 560*/
1514 else if (i = m - s) { /* faster from front */
1515 d -= clen;
1516 m = d;
1517 sv_chop(TARG, d-i);
1518 s += i;
1519 while (i--)
1520 *--d = *--s;
1521 if (clen)
1522 Copy(c, m, clen, char);
1523 }
1524 else if (clen) {
1525 d -= clen;
1526 sv_chop(TARG, d);
1527 Copy(c, d, clen, char);
1528 }
1529 else {
1530 sv_chop(TARG, d);
1531 }
9212bbba 1532 TAINT_IF(rxtainted);
71be2cbc
PP
1533 PUSHs(&sv_yes);
1534 }
1535 else {
1536 rxtainted = 0;
1537 do {
1538 if (iters++ > maxiters)
1539 DIE("Substitution loop");
1540 rxtainted |= rx->exec_tainted;
1541 m = rx->startp[0];
1542 /*SUPPRESS 560*/
1543 if (i = m - s) {
1544 if (s != d)
1545 Move(s, d, i, char);
1546 d += i;
a0d0e21e 1547 }
71be2cbc
PP
1548 if (clen) {
1549 Copy(c, d, clen, char);
1550 d += clen;
1551 }
1552 s = rx->endp[0];
1553 } while (pregexec(rx, s, strend, orig, s == m,
1554 Nullsv, TRUE)); /* don't match same null twice */
1555 if (s != d) {
1556 i = strend - s;
1557 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1558 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1559 }
9212bbba 1560 TAINT_IF(rxtainted);
71be2cbc 1561 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1562 }
71be2cbc
PP
1563 (void)SvPOK_only(TARG);
1564 SvSETMAGIC(TARG);
9212bbba 1565 SvTAINT(TARG);
71be2cbc
PP
1566 LEAVE_SCOPE(oldsave);
1567 RETURN;
a0d0e21e 1568 }
71be2cbc 1569
e50aee73 1570 if (pregexec(rx, s, strend, orig, 0,
5f05dabc 1571 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
a0d0e21e
LW
1572 if (force_on_match) {
1573 force_on_match = 0;
1574 s = SvPV_force(TARG, len);
1575 goto force_it;
1576 }
71be2cbc 1577 rxtainted = rx->exec_tainted;
a0d0e21e
LW
1578 dstr = NEWSV(25, sv_len(TARG));
1579 sv_setpvn(dstr, m, s-m);
1580 curpm = pm;
1581 if (!c) {
1582 register CONTEXT *cx;
1583 PUSHSUBST(cx);
1584 RETURNOP(cPMOP->op_pmreplroot);
1585 }
1586 do {
1587 if (iters++ > maxiters)
1588 DIE("Substitution loop");
71be2cbc 1589 rxtainted |= rx->exec_tainted;
a0d0e21e
LW
1590 if (rx->subbase && rx->subbase != orig) {
1591 m = s;
1592 s = orig;
1593 orig = rx->subbase;
1594 s = orig + (m - s);
1595 strend = s + (strend - m);
1596 }
1597 m = rx->startp[0];
1598 sv_catpvn(dstr, s, m-s);
1599 s = rx->endp[0];
1600 if (clen)
1601 sv_catpvn(dstr, c, clen);
1602 if (once)
1603 break;
5f05dabc 1604 } while (pregexec(rx, s, strend, orig, s == m, Nullsv, safebase));
a0d0e21e 1605 sv_catpvn(dstr, s, strend - s);
748a9306 1606
9212bbba
CS
1607 TAINT_IF(rxtainted);
1608
4633a7c4 1609 (void)SvOOK_off(TARG);
cb0b1708 1610 Safefree(SvPVX(TARG));
748a9306
LW
1611 SvPVX(TARG) = SvPVX(dstr);
1612 SvCUR_set(TARG, SvCUR(dstr));
1613 SvLEN_set(TARG, SvLEN(dstr));
1614 SvPVX(dstr) = 0;
1615 sv_free(dstr);
1616
a0d0e21e
LW
1617 (void)SvPOK_only(TARG);
1618 SvSETMAGIC(TARG);
9212bbba 1619 SvTAINT(TARG);
a0d0e21e 1620 PUSHs(sv_2mortal(newSViv((I32)iters)));
4633a7c4 1621 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1622 RETURN;
1623 }
5cd24f17 1624 goto ret_no;
a0d0e21e
LW
1625
1626nope:
1627 ++BmUSEFUL(pm->op_pmshort);
5cd24f17
PP
1628
1629ret_no:
a0d0e21e 1630 PUSHs(&sv_no);
4633a7c4 1631 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1632 RETURN;
1633}
1634
1635PP(pp_grepwhile)
1636{
4e35701f 1637 djSP;
a0d0e21e
LW
1638
1639 if (SvTRUEx(POPs))
1640 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1641 ++*markstack_ptr;
1642 LEAVE; /* exit inner scope */
1643
1644 /* All done yet? */
1645 if (stack_base + *markstack_ptr > sp) {
1646 I32 items;
54310121 1647 I32 gimme = GIMME_V;
a0d0e21e
LW
1648
1649 LEAVE; /* exit outer scope */
1650 (void)POPMARK; /* pop src */
1651 items = --*markstack_ptr - markstack_ptr[-1];
1652 (void)POPMARK; /* pop dst */
1653 SP = stack_base + POPMARK; /* pop original mark */
54310121 1654 if (gimme == G_SCALAR) {
a0d0e21e
LW
1655 dTARGET;
1656 XPUSHi(items);
a0d0e21e 1657 }
54310121
PP
1658 else if (gimme == G_ARRAY)
1659 SP += items;
a0d0e21e
LW
1660 RETURN;
1661 }
1662 else {
1663 SV *src;
1664
1665 ENTER; /* enter inner scope */
1666 SAVESPTR(curpm);
1667
1668 src = stack_base[*markstack_ptr];
1669 SvTEMP_off(src);
1670 GvSV(defgv) = src;
1671
1672 RETURNOP(cLOGOP->op_other);
1673 }
1674}
1675
1676PP(pp_leavesub)
1677{
4e35701f 1678 djSP;
a0d0e21e
LW
1679 SV **mark;
1680 SV **newsp;
1681 PMOP *newpm;
1682 I32 gimme;
1683 register CONTEXT *cx;
f86702cc 1684 struct block_sub cxsub;
a0d0e21e
LW
1685
1686 POPBLOCK(cx,newpm);
f86702cc
PP
1687 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1688
a1f49e72 1689 TAINT_NOT;
a0d0e21e
LW
1690 if (gimme == G_SCALAR) {
1691 MARK = newsp + 1;
1692 if (MARK <= SP)
f86702cc 1693 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
a0d0e21e 1694 else {
f86702cc 1695 MEXTEND(MARK, 0);
a0d0e21e
LW
1696 *MARK = &sv_undef;
1697 }
1698 SP = MARK;
1699 }
54310121 1700 else if (gimme == G_ARRAY) {
f86702cc 1701 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1702 if (!SvTEMP(*MARK)) {
f86702cc 1703 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1704 TAINT_NOT; /* Each item is independent */
1705 }
f86702cc 1706 }
a0d0e21e 1707 }
f86702cc
PP
1708 PUTBACK;
1709
1710 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1711 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1712
1713 LEAVE;
a0d0e21e
LW
1714 return pop_return();
1715}
1716
3de9ffa1 1717static CV *
8ac85365 1718get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
1719{
1720 dTHR;
4f01c5a5 1721 SV *oldsv = *svp;
3de9ffa1 1722 GV *gv;
3de9ffa1 1723
4f01c5a5
MB
1724 *svp = GvSV(DBsub);
1725 save_item(*svp);
3de9ffa1
MB
1726 gv = CvGV(cv);
1727 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1728 || strEQ(GvNAME(gv), "END")
1729 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1730 !( (SvTYPE(oldsv) == SVt_PVGV) && (GvCV((GV*)oldsv) == cv)
1731 && (gv = (GV*)oldsv) ))) {
1732 /* Use GV from the stack as a fallback. */
1733 /* GV is potentially non-unique, or contain different CV. */
4f01c5a5 1734 sv_setsv(*svp, newRV((SV*)cv));
3de9ffa1
MB
1735 }
1736 else {
4f01c5a5 1737 gv_efullname3(*svp, gv, Nullch);
3de9ffa1
MB
1738 }
1739 cv = GvCV(DBsub);
1740 if (CvXSUB(cv))
1741 curcopdb = curcop;
1742 return cv;
1743}
1744
a0d0e21e
LW
1745PP(pp_entersub)
1746{
4e35701f 1747 djSP; dPOPss;
a0d0e21e
LW
1748 GV *gv;
1749 HV *stash;
1750 register CV *cv;
1751 register CONTEXT *cx;
5d94fbed 1752 I32 gimme;
07055b4c 1753 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1754
1755 if (!sv)
1756 DIE("Not a CODE reference");
1757 switch (SvTYPE(sv)) {
1758 default:
1759 if (!SvROK(sv)) {
748a9306
LW
1760 char *sym;
1761
fb73857a
PP
1762 if (sv == &sv_yes) { /* unfound import, ignore */
1763 if (hasargs)
1764 SP = stack_base + POPMARK;
a0d0e21e 1765 RETURN;
fb73857a 1766 }
15ff848f
CS
1767 if (SvGMAGICAL(sv)) {
1768 mg_get(sv);
1769 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1770 }
1771 else
1772 sym = SvPV(sv, na);
1773 if (!sym)
a0d0e21e
LW
1774 DIE(no_usym, "a subroutine");
1775 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1776 DIE(no_symref, sym, "a subroutine");
1777 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1778 break;
1779 }
1780 cv = (CV*)SvRV(sv);
1781 if (SvTYPE(cv) == SVt_PVCV)
1782 break;
1783 /* FALL THROUGH */
1784 case SVt_PVHV:
1785 case SVt_PVAV:
1786 DIE("Not a CODE reference");
1787 case SVt_PVCV:
1788 cv = (CV*)sv;
1789 break;
1790 case SVt_PVGV:
8ebc5c01 1791 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1792 cv = sv_2cv(sv, &stash, &gv, TRUE);
1793 break;
1794 }
1795
1796 ENTER;
1797 SAVETMPS;
1798
1799 retry:
1800 if (!cv)
1801 DIE("Not a CODE reference");
1802
1803 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a
PP
1804 GV* autogv;
1805 SV* subname;
1806
1807 /* anonymous or undef'd function leaves us no recourse */
1808 if (CvANON(cv) || !(gv = CvGV(cv)))
1809 DIE("Undefined subroutine called");
1810 /* autoloaded stub? */
1811 if (cv != GvCV(gv)) {
1812 cv = GvCV(gv);
1813 goto retry;
a0d0e21e 1814 }
44a8e56a 1815 /* should call AUTOLOAD now? */
54310121
PP
1816 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
1817 FALSE)))
1818 {
44a8e56a
PP
1819 cv = GvCV(autogv);
1820 goto retry;
a0d0e21e 1821 }
44a8e56a
PP
1822 /* sorry */
1823 subname = sv_newmortal();
1824 gv_efullname3(subname, gv, Nullch);
1825 DIE("Undefined subroutine &%s called", SvPVX(subname));
a0d0e21e
LW
1826 }
1827
54310121 1828 gimme = GIMME_V;
3de9ffa1 1829 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv))
4f01c5a5 1830 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
1831 if (!cv)
1832 DIE("No DBsub routine");
a0d0e21e 1833
11343788 1834#ifdef USE_THREADS
3de9ffa1
MB
1835 /*
1836 * First we need to check if the sub or method requires locking.
1837 * If so, we gain a lock on the CV or the first argument, as
1838 * appropriate. This has to be inline because for FAKE_THREADS,
1839 * COND_WAIT inlines code to reschedule by returning a new op.
1840 */
11343788 1841 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
1842 if (CvFLAGS(cv) & CVf_LOCKED) {
1843 MAGIC *mg;
1844 if (CvFLAGS(cv) & CVf_METHOD) {
1845 if (SP > stack_base + TOPMARK)
1846 sv = *(stack_base + TOPMARK + 1);
1847 else {
1848 MUTEX_UNLOCK(CvMUTEXP(cv));
1849 croak("no argument for locked method call");
1850 }
1851 if (SvROK(sv))
1852 sv = SvRV(sv);
1853 }
1854 else {
1855 sv = (SV*)cv;
1856 }
1857 MUTEX_UNLOCK(CvMUTEXP(cv));
1858 mg = condpair_magic(sv);
1859 MUTEX_LOCK(MgMUTEXP(mg));
1860 if (MgOWNER(mg) == thr)
1861 MUTEX_UNLOCK(MgMUTEXP(mg));
1862 else {
1863 while (MgOWNER(mg))
1864 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
1865 MgOWNER(mg) = thr;
1fd28e87
MB
1866 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
1867 thr, sv);)
77a005ab 1868 MUTEX_UNLOCK(MgMUTEXP(mg));
e55aaa0e 1869 SvREFCNT_inc(sv); /* Keep alive until magic_mutexfree */
77a005ab 1870 save_destructor(unlock_condpair, sv);
11343788 1871 }
77a005ab 1872 MUTEX_LOCK(CvMUTEXP(cv));
11343788 1873 }
3de9ffa1
MB
1874 /*
1875 * Now we have permission to enter the sub, we must distinguish
1876 * four cases. (0) It's an XSUB (in which case we don't care
1877 * about ownership); (1) it's ours already (and we're recursing);
1878 * (2) it's free (but we may already be using a cached clone);
1879 * (3) another thread owns it. Case (1) is easy: we just use it.
1880 * Case (2) means we look for a clone--if we have one, use it
1881 * otherwise grab ownership of cv. Case (3) means we look for a
1882 * clone (for non-XSUBs) and have to create one if we don't
1883 * already have one.
1884 * Why look for a clone in case (2) when we could just grab
1885 * ownership of cv straight away? Well, we could be recursing,
1886 * i.e. we originally tried to enter cv while another thread
1887 * owned it (hence we used a clone) but it has been freed up
1888 * and we're now recursing into it. It may or may not be "better"
1889 * to use the clone but at least CvDEPTH can be trusted.
1890 */
1891 if (CvOWNER(cv) == thr || CvXSUB(cv))
1892 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 1893 else {
3de9ffa1
MB
1894 /* Case (2) or (3) */
1895 SV **svp;
1896
11343788 1897 /*
3de9ffa1
MB
1898 * XXX Might it be better to release CvMUTEXP(cv) while we
1899 * do the hv_fetch? We might find someone has pinched it
1900 * when we look again, in which case we would be in case
1901 * (3) instead of (2) so we'd have to clone. Would the fact
1902 * that we released the mutex more quickly make up for this?
1903 */
199100c8 1904 svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE);
3de9ffa1
MB
1905 if (svp) {
1906 /* We already have a clone to use */
11343788 1907 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1
MB
1908 cv = *(CV**)svp;
1909 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
1910 "entersub: %p already has clone %p:%s\n",
1911 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
1912 CvOWNER(cv) = thr;
1913 SvREFCNT_inc(cv);
1914 if (CvDEPTH(cv) == 0)
1915 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
1916 }
11343788 1917 else {
3de9ffa1
MB
1918 /* (2) => grab ownership of cv. (3) => make clone */
1919 if (!CvOWNER(cv)) {
1920 CvOWNER(cv) = thr;
1921 SvREFCNT_inc(cv);
11343788 1922 MUTEX_UNLOCK(CvMUTEXP(cv));
d9f997d7 1923 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
1924 "entersub: %p grabbing %p:%s in stash %s\n",
1925 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
1926 HvNAME(CvSTASH(cv)) : "(none)"));
1927 } else {
1928 /* Make a new clone. */
1929 CV *clonecv;
1930 SvREFCNT_inc(cv); /* don't let it vanish from under us */
1931 MUTEX_UNLOCK(CvMUTEXP(cv));
1932 DEBUG_L((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
1933 "entersub: %p cloning %p:%s\n",
1934 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
1935 /*
1936 * We're creating a new clone so there's no race
1937 * between the original MUTEX_UNLOCK and the
1938 * SvREFCNT_inc since no one will be trying to undef
1939 * it out from underneath us. At least, I don't think
1940 * there's a race...
1941 */
1942 clonecv = cv_clone(cv);
1943 SvREFCNT_dec(cv); /* finished with this */
199100c8 1944 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
1945 CvOWNER(clonecv) = thr;
1946 cv = clonecv;
11343788 1947 SvREFCNT_inc(cv);
11343788 1948 }
3de9ffa1
MB
1949 DEBUG_L(if (CvDEPTH(cv) != 0)
1950 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
1951 CvDEPTH(cv)););
1952 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 1953 }
3de9ffa1 1954 }
11343788
MB
1955#endif /* USE_THREADS */
1956
1957 gimme = GIMME;
1958
a0d0e21e
LW
1959 if (CvXSUB(cv)) {
1960 if (CvOLDSTYLE(cv)) {
ecfc5424 1961 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1962 dMARK;
1963 register I32 items = SP - MARK;
67955e0c 1964 /* We dont worry to copy from @_. */
a0d0e21e
LW
1965 while (sp > mark) {
1966 sp[1] = sp[0];
1967 sp--;
1968 }
1969 stack_sp = mark + 1;
ecfc5424
AD
1970 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1971 items = (*fp3)(CvXSUBANY(cv).any_i32,
1972 MARK - stack_base + 1,
1973 items);
a0d0e21e
LW
1974 stack_sp = stack_base + items;
1975 }
1976 else {
748a9306
LW
1977 I32 markix = TOPMARK;
1978
a0d0e21e 1979 PUTBACK;
67955e0c
PP
1980
1981 if (!hasargs) {
1982 /* Need to copy @_ to stack. Alternative may be to
1983 * switch stack to @_, and copy return values
1984 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
1985 AV* av;
1986 I32 items;
1987#ifdef USE_THREADS
1988 av = (AV*)curpad[0];
1989#else
1990 av = GvAV(defgv);
1991#endif /* USE_THREADS */
1992 items = AvFILL(av) + 1;
67955e0c
PP
1993
1994 if (items) {
1995 /* Mark is at the end of the stack. */
1996 EXTEND(sp, items);
1997 Copy(AvARRAY(av), sp + 1, items, SV*);
1998 sp += items;
1999 PUTBACK ;
2000 }
2001 }
2002 if (curcopdb) { /* We assume that the first
2003 XSUB in &DB::sub is the
2004 called one. */
2005 SAVESPTR(curcop);
2006 curcop = curcopdb;
2007 curcopdb = NULL;
2008 }
2009 /* Do we need to open block here? XXXX */
a0d0e21e 2010 (void)(*CvXSUB(cv))(cv);
748a9306
LW
2011
2012 /* Enforce some sanity in scalar context. */
5d94fbed 2013 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
2014 if (markix > stack_sp - stack_base)
2015 *(stack_base + markix) = &sv_undef;
2016 else
2017 *(stack_base + markix) = *stack_sp;
2018 stack_sp = stack_base + markix;
2019 }
a0d0e21e
LW
2020 }
2021 LEAVE;
2022 return NORMAL;
2023 }
2024 else {
2025 dMARK;
2026 register I32 items = SP - MARK;
a0d0e21e
LW
2027 AV* padlist = CvPADLIST(cv);
2028 SV** svp = AvARRAY(padlist);
2029 push_return(op->op_next);
2030 PUSHBLOCK(cx, CXt_SUB, MARK);
2031 PUSHSUB(cx);
2032 CvDEPTH(cv)++;
2033 if (CvDEPTH(cv) < 2)
2034 (void)SvREFCNT_inc(cv);
2035 else { /* save temporaries on recursion? */
5f05dabc 2036 if (CvDEPTH(cv) == 100 && dowarn
84902520 2037 && !(PERLDB_SUB && cv == GvCV(DBsub)))
44a8e56a 2038 sub_crush_depth(cv);
a0d0e21e
LW
2039 if (CvDEPTH(cv) > AvFILL(padlist)) {
2040 AV *av;
2041 AV *newpad = newAV();
4aa0a1f7 2042 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
2043 I32 ix = AvFILL((AV*)svp[1]);
2044 svp = AvARRAY(svp[0]);
748a9306 2045 for ( ;ix > 0; ix--) {
a0d0e21e 2046 if (svp[ix] != &sv_undef) {
748a9306 2047 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2048 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2049 || *name == '&') /* anonymous code? */
2050 {
2051 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2052 }
2053 else { /* our own lexical */
2054 if (*name == '@')
2055 av_store(newpad, ix, sv = (SV*)newAV());
2056 else if (*name == '%')
2057 av_store(newpad, ix, sv = (SV*)newHV());
2058 else
2059 av_store(newpad, ix, sv = NEWSV(0,0));
2060 SvPADMY_on(sv);
2061 }
a0d0e21e
LW
2062 }
2063 else {
748a9306 2064 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2065 SvPADTMP_on(sv);
2066 }
2067 }
2068 av = newAV(); /* will be @_ */
2069 av_extend(av, 0);
2070 av_store(newpad, 0, (SV*)av);
2071 AvFLAGS(av) = AVf_REIFY;
2072 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
2073 AvFILL(padlist) = CvDEPTH(cv);
2074 svp = AvARRAY(padlist);
2075 }
2076 }
6d4ff0d2
MB
2077#ifdef USE_THREADS
2078 if (!hasargs) {
a0d0e21e 2079 AV* av = (AV*)curpad[0];
6d4ff0d2
MB
2080
2081 items = AvFILL(av) + 1;
2082 if (items) {
2083 /* Mark is at the end of the stack. */
2084 EXTEND(sp, items);
2085 Copy(AvARRAY(av), sp + 1, items, SV*);
2086 sp += items;
2087 PUTBACK ;
2088 }
2089 }
2090#endif /* USE_THREADS */
2091 SAVESPTR(curpad);
2092 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2093#ifndef USE_THREADS
2094 if (hasargs)
2095#endif /* USE_THREADS */
2096 {
2097 AV* av;
a0d0e21e
LW
2098 SV** ary;
2099
77a005ab 2100#if 0
0f15f207
MB
2101 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2102 "%p entersub preparing @_\n", thr));
77a005ab 2103#endif
6d4ff0d2 2104 av = (AV*)curpad[0];
a0d0e21e
LW
2105 if (AvREAL(av)) {
2106 av_clear(av);
2107 AvREAL_off(av);
2108 }
6d4ff0d2 2109#ifndef USE_THREADS
a0d0e21e 2110 cx->blk_sub.savearray = GvAV(defgv);
0c8c7a05 2111 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2112#endif /* USE_THREADS */
2113 cx->blk_sub.argarray = av;
a0d0e21e
LW
2114 ++MARK;
2115
2116 if (items > AvMAX(av) + 1) {
2117 ary = AvALLOC(av);
2118 if (AvARRAY(av) != ary) {
2119 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2120 SvPVX(av) = (char*)ary;
2121 }
2122 if (items > AvMAX(av) + 1) {
2123 AvMAX(av) = items - 1;
2124 Renew(ary,items,SV*);
2125 AvALLOC(av) = ary;
2126 SvPVX(av) = (char*)ary;
2127 }
2128 }
2129 Copy(MARK,AvARRAY(av),items,SV*);
2130 AvFILL(av) = items - 1;
2131
2132 while (items--) {
2133 if (*MARK)
2134 SvTEMP_off(*MARK);
2135 MARK++;
2136 }
2137 }
77a005ab 2138#if 0
0f15f207
MB
2139 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2140 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2141#endif
a0d0e21e
LW
2142 RETURNOP(CvSTART(cv));
2143 }
2144}
2145
44a8e56a 2146void
8ac85365 2147sub_crush_depth(CV *cv)
44a8e56a
PP
2148{
2149 if (CvANON(cv))
2150 warn("Deep recursion on anonymous subroutine");
2151 else {
2152 SV* tmpstr = sv_newmortal();
2153 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2154 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
2155 }
2156}
2157
a0d0e21e
LW
2158PP(pp_aelem)
2159{
4e35701f 2160 djSP;
a0d0e21e 2161 SV** svp;
748a9306 2162 I32 elem = POPi;
68dc0745
PP
2163 AV* av = (AV*)POPs;
2164 U32 lval = op->op_flags & OPf_MOD;
2165 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
a0d0e21e 2166
748a9306
LW
2167 if (elem > 0)
2168 elem -= curcop->cop_arybase;
a0d0e21e
LW
2169 if (SvTYPE(av) != SVt_PVAV)
2170 RETPUSHUNDEF;
68dc0745 2171 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2172 if (lval) {
68dc0745
PP
2173 if (!svp || *svp == &sv_undef) {
2174 SV* lv;
2175 if (!defer)
2176 DIE(no_aelem, elem);
2177 lv = sv_newmortal();
2178 sv_upgrade(lv, SVt_PVLV);
2179 LvTYPE(lv) = 'y';
2180 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2181 LvTARG(lv) = SvREFCNT_inc(av);
2182 LvTARGOFF(lv) = elem;
2183 LvTARGLEN(lv) = 1;
2184 PUSHs(lv);
2185 RETURN;
2186 }
a0d0e21e
LW
2187 if (op->op_private & OPpLVAL_INTRO)
2188 save_svref(svp);
5f05dabc 2189 else if (op->op_private & OPpDEREF)
68dc0745 2190 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
2191 }
2192 PUSHs(svp ? *svp : &sv_undef);
2193 RETURN;
2194}
2195
02a9e968 2196void
8ac85365 2197vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2198{
2199 if (SvGMAGICAL(sv))
2200 mg_get(sv);
2201 if (!SvOK(sv)) {
2202 if (SvREADONLY(sv))
2203 croak(no_modify);
5f05dabc
PP
2204 if (SvTYPE(sv) < SVt_RV)
2205 sv_upgrade(sv, SVt_RV);
2206 else if (SvTYPE(sv) >= SVt_PV) {
2207 (void)SvOOK_off(sv);
2208 Safefree(SvPVX(sv));
2209 SvLEN(sv) = SvCUR(sv) = 0;
2210 }
68dc0745 2211 switch (to_what) {
5f05dabc
PP
2212 case OPpDEREF_SV:
2213 SvRV(sv) = newSV(0);
2214 break;
2215 case OPpDEREF_AV:
2216 SvRV(sv) = (SV*)newAV();
2217 break;
2218 case OPpDEREF_HV:
2219 SvRV(sv) = (SV*)newHV();
2220 break;
2221 }
02a9e968
CS
2222 SvROK_on(sv);
2223 SvSETMAGIC(sv);
2224 }
2225}
2226
a0d0e21e
LW
2227PP(pp_method)
2228{
4e35701f 2229 djSP;
a0d0e21e
LW
2230 SV* sv;
2231 SV* ob;
2232 GV* gv;
56304f61
CS
2233 HV* stash;
2234 char* name;
ac91690f
CS
2235 char* packname;
2236 STRLEN packlen;
a0d0e21e 2237
fb73857a
PP
2238 if (SvROK(TOPs)) {
2239 sv = SvRV(TOPs);
2240 if (SvTYPE(sv) == SVt_PVCV) {
2241 SETs(sv);
2242 RETURN;
2243 }
2244 }
2245
56304f61 2246 name = SvPV(TOPs, na);
a0d0e21e
LW
2247 sv = *(stack_base + TOPMARK + 1);
2248
16d20bd9
AD
2249 if (SvGMAGICAL(sv))
2250 mg_get(sv);
a0d0e21e 2251 if (SvROK(sv))
16d20bd9 2252 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2253 else {
2254 GV* iogv;
a0d0e21e 2255
56304f61 2256 packname = Nullch;
a0d0e21e 2257 if (!SvOK(sv) ||
56304f61 2258 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2259 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2260 !(ob=(SV*)GvIO(iogv)))
2261 {
56304f61 2262 if (!packname || !isIDFIRST(*packname))
ac91690f 2263 DIE("Can't call method \"%s\" without a package or object reference", name);
56304f61 2264 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2265 goto fetch;
a0d0e21e 2266 }
ac91690f 2267 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2268 }
2269
ac91690f 2270 if (!ob || !SvOBJECT(ob))
a0d0e21e 2271 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2272
56304f61 2273 stash = SvSTASH(ob);
a0d0e21e 2274
ac91690f 2275 fetch:
ac91690f 2276 gv = gv_fetchmethod(stash, name);
56304f61
CS
2277 if (!gv) {
2278 char* leaf = name;
2279 char* sep = Nullch;
2280 char* p;
2281
2282 for (p = name; *p; p++) {
2283 if (*p == '\'')
2284 sep = p, leaf = p + 1;
2285 else if (*p == ':' && *(p + 1) == ':')
2286 sep = p, leaf = p + 2;
2287 }
2288 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2289 packname = HvNAME(sep ? curcop->cop_stash : stash);
2290 packlen = strlen(packname);
2291 }
2292 else {
2293 packname = name;
2294 packlen = sep - name;
2295 }
2296 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2297 leaf, (int)packlen, packname);
2298 }
8ebc5c01 2299 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2300 RETURN;
2301}