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