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