This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
various fixes for clean build and test on win32; configpm broken,
[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) {
599cee73
PM
1273 if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START))
1274 warner(WARN_CLOSED,
1275 "Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
54310121 1276 if (gimme == G_SCALAR) {
a0d0e21e
LW
1277 (void)SvOK_off(TARG);
1278 PUSHTARG;
1279 }
1280 RETURN;
1281 }
a2008d6d 1282 have_fp:
54310121 1283 if (gimme == G_SCALAR) {
a0d0e21e 1284 sv = TARG;
9607fc9c
PP
1285 if (SvROK(sv))
1286 sv_unref(sv);
a0d0e21e
LW
1287 (void)SvUPGRADE(sv, SVt_PV);
1288 tmplen = SvLEN(sv); /* remember if already alloced */
1289 if (!tmplen)
1290 Sv_Grow(sv, 80); /* try short-buffering it */
1291 if (type == OP_RCATLINE)
1292 offset = SvCUR(sv);
1293 else
1294 offset = 0;
1295 }
54310121
PP
1296 else {
1297 sv = sv_2mortal(NEWSV(57, 80));
1298 offset = 0;
1299 }
fbad3eb5
GS
1300
1301/* flip-flop EOF state for a snarfed empty file */
1302#define SNARF_EOF(gimme,rs,io,sv) \
1303 ((gimme != G_SCALAR || SvCUR(sv) \
1304 || (IoFLAGS(io) & IOf_NOLINE) || IoLINES(io) || !RsSNARF(rs)) \
1305 ? ((IoFLAGS(io) &= ~IOf_NOLINE), TRUE) \
1306 : ((IoFLAGS(io) |= IOf_NOLINE), FALSE))
1307
a0d0e21e 1308 for (;;) {
fbad3eb5
GS
1309 if (!sv_gets(sv, fp, offset)
1310 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1311 {
760ac839 1312 PerlIO_clearerr(fp);
a0d0e21e 1313 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1314 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1315 if (fp)
1316 continue;
3280af22 1317 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1318 IoFLAGS(io) |= IOf_START;
1319 }
1320 else if (type == OP_GLOB) {
4eb79ab5
GS
1321 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
1322 warner(WARN_CLOSED,
1323 "glob failed (child exited with status %d%s)",
1324 STATUS_CURRENT >> 8,
cf494569 1325 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1326 }
a0d0e21e 1327 }
54310121 1328 if (gimme == G_SCALAR) {
a0d0e21e
LW
1329 (void)SvOK_off(TARG);
1330 PUSHTARG;
1331 }
1332 RETURN;
1333 }
bbce6d69
PP
1334 /* This should not be marked tainted if the fp is marked clean */
1335 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1336 TAINT;
1337 SvTAINTED_on(sv);
1338 }
a0d0e21e 1339 IoLINES(io)++;
71be2cbc 1340 SvSETMAGIC(sv);
a0d0e21e 1341 XPUSHs(sv);
a0d0e21e
LW
1342 if (type == OP_GLOB) {
1343 char *tmps;
1344
3280af22 1345 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1346 tmps = SvEND(sv) - 1;
3280af22 1347 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1348 *tmps = '\0';
1349 SvCUR(sv)--;
1350 }
1351 }
a0d0e21e
LW
1352 for (tmps = SvPVX(sv); *tmps; tmps++)
1353 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1354 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1355 break;
3280af22 1356 if (*tmps && PerlLIO_stat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1357 (void)POPs; /* Unmatched wildcard? Chuck it... */
1358 continue;
1359 }
1360 }
54310121 1361 if (gimme == G_ARRAY) {
a0d0e21e
LW
1362 if (SvLEN(sv) - SvCUR(sv) > 20) {
1363 SvLEN_set(sv, SvCUR(sv)+1);
1364 Renew(SvPVX(sv), SvLEN(sv), char);
1365 }
1366 sv = sv_2mortal(NEWSV(58, 80));
1367 continue;
1368 }
54310121 1369 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1370 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1371 if (SvCUR(sv) < 60)
1372 SvLEN_set(sv, 80);
1373 else
1374 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1375 Renew(SvPVX(sv), SvLEN(sv), char);
1376 }
1377 RETURN;
1378 }
1379}
1380
1381PP(pp_enter)
1382{
4e35701f 1383 djSP;
c09156bb 1384 register PERL_CONTEXT *cx;
533c011a 1385 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1386
54310121
PP
1387 if (gimme == -1) {
1388 if (cxstack_ix >= 0)
1389 gimme = cxstack[cxstack_ix].blk_gimme;
1390 else
1391 gimme = G_SCALAR;
1392 }
a0d0e21e
LW
1393
1394 ENTER;
1395
1396 SAVETMPS;
924508f0 1397 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1398
1399 RETURN;
1400}
1401
1402PP(pp_helem)
1403{
4e35701f 1404 djSP;
760ac839 1405 HE* he;
ae77835f 1406 SV **svp;
a0d0e21e 1407 SV *keysv = POPs;
a0d0e21e 1408 HV *hv = (HV*)POPs;
533c011a
NIS
1409 U32 lval = PL_op->op_flags & OPf_MOD;
1410 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1411 SV *sv;
a0d0e21e 1412
ae77835f
MB
1413 if (SvTYPE(hv) == SVt_PVHV) {
1414 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1415 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1416 }
1417 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038
SM
1418 if (PL_op->op_private & OPpLVAL_INTRO)
1419 DIE("Can't localize pseudo-hash element");
97fcbf96 1420 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1421 }
c750a3ec 1422 else {
a0d0e21e 1423 RETPUSHUNDEF;
c750a3ec 1424 }
a0d0e21e 1425 if (lval) {
3280af22 1426 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1427 SV* lv;
1428 SV* key2;
2d8e6c8d
GS
1429 if (!defer) {
1430 STRLEN n_a;
1431 DIE(PL_no_helem, SvPV(keysv, n_a));
1432 }
68dc0745
PP
1433 lv = sv_newmortal();
1434 sv_upgrade(lv, SVt_PVLV);
1435 LvTYPE(lv) = 'y';
1436 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1437 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1438 LvTARG(lv) = SvREFCNT_inc(hv);
1439 LvTARGLEN(lv) = 1;
1440 PUSHs(lv);
1441 RETURN;
1442 }
533c011a 1443 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1444 if (HvNAME(hv) && isGV(*svp))
533c011a 1445 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
5f05dabc 1446 else
161b7d16 1447 save_helem(hv, keysv, svp);
5f05dabc 1448 }
533c011a
NIS
1449 else if (PL_op->op_private & OPpDEREF)
1450 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1451 }
3280af22 1452 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1453 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1454 * Pushing the magical RHS on to the stack is useless, since
1455 * that magic is soon destined to be misled by the local(),
1456 * and thus the later pp_sassign() will fail to mg_get() the
1457 * old value. This should also cure problems with delayed
1458 * mg_get()s. GSAR 98-07-03 */
1459 if (!lval && SvGMAGICAL(sv))
1460 sv = sv_mortalcopy(sv);
1461 PUSHs(sv);
a0d0e21e
LW
1462 RETURN;
1463}
1464
1465PP(pp_leave)
1466{
4e35701f 1467 djSP;
c09156bb 1468 register PERL_CONTEXT *cx;
a0d0e21e
LW
1469 register SV **mark;
1470 SV **newsp;
1471 PMOP *newpm;
1472 I32 gimme;
1473
533c011a 1474 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1475 cx = &cxstack[cxstack_ix];
3280af22 1476 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1477 }
1478
1479 POPBLOCK(cx,newpm);
1480
533c011a 1481 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1482 if (gimme == -1) {
1483 if (cxstack_ix >= 0)
1484 gimme = cxstack[cxstack_ix].blk_gimme;
1485 else
1486 gimme = G_SCALAR;
1487 }
a0d0e21e 1488
a1f49e72 1489 TAINT_NOT;
54310121
PP
1490 if (gimme == G_VOID)
1491 SP = newsp;
1492 else if (gimme == G_SCALAR) {
1493 MARK = newsp + 1;
1494 if (MARK <= SP)
1495 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1496 *MARK = TOPs;
1497 else
1498 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1499 else {
54310121 1500 MEXTEND(mark,0);
3280af22 1501 *MARK = &PL_sv_undef;
a0d0e21e 1502 }
54310121 1503 SP = MARK;
a0d0e21e 1504 }
54310121 1505 else if (gimme == G_ARRAY) {
a1f49e72
CS
1506 /* in case LEAVE wipes old return values */
1507 for (mark = newsp + 1; mark <= SP; mark++) {
1508 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1509 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1510 TAINT_NOT; /* Each item is independent */
1511 }
1512 }
a0d0e21e 1513 }
3280af22 1514 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1515
1516 LEAVE;
1517
1518 RETURN;
1519}
1520
1521PP(pp_iter)
1522{
4e35701f 1523 djSP;
c09156bb 1524 register PERL_CONTEXT *cx;
5f05dabc 1525 SV* sv;
4633a7c4 1526 AV* av;
a0d0e21e 1527
924508f0 1528 EXTEND(SP, 1);
a0d0e21e 1529 cx = &cxstack[cxstack_ix];
6b35e009 1530 if (CxTYPE(cx) != CXt_LOOP)
a0d0e21e
LW
1531 DIE("panic: pp_iter");
1532
4633a7c4 1533 av = cx->blk_loop.iterary;
89ea2908
GA
1534 if (SvTYPE(av) != SVt_PVAV) {
1535 /* iterate ($min .. $max) */
1536 if (cx->blk_loop.iterlval) {
1537 /* string increment */
1538 register SV* cur = cx->blk_loop.iterlval;
1539 STRLEN maxlen;
1540 char *max = SvPV((SV*)av, maxlen);
1541 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1542#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1543 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1544 && !SvMAGICAL(*cx->blk_loop.itervar))
1545 {
eaa5c2d6
GA
1546 /* safe to reuse old SV */
1547 sv_setsv(*cx->blk_loop.itervar, cur);
1548 }
1549 else
1550#endif
1551 {
1552 /* we need a fresh SV every time so that loop body sees a
1553 * completely new SV for closures/references to work as
1554 * they used to */
1555 SvREFCNT_dec(*cx->blk_loop.itervar);
1556 *cx->blk_loop.itervar = newSVsv(cur);
1557 }
89ea2908
GA
1558 if (strEQ(SvPVX(cur), max))
1559 sv_setiv(cur, 0); /* terminate next time */
1560 else
1561 sv_inc(cur);
1562 RETPUSHYES;
1563 }
1564 RETPUSHNO;
1565 }
1566 /* integer increment */
1567 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1568 RETPUSHNO;
7f61b687 1569
eaa5c2d6 1570#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1571 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1572 && !SvMAGICAL(*cx->blk_loop.itervar))
1573 {
eaa5c2d6
GA
1574 /* safe to reuse old SV */
1575 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1576 }
1577 else
1578#endif
1579 {
1580 /* we need a fresh SV every time so that loop body sees a
1581 * completely new SV for closures/references to work as they
1582 * used to */
1583 SvREFCNT_dec(*cx->blk_loop.itervar);
1584 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1585 }
89ea2908
GA
1586 RETPUSHYES;
1587 }
1588
1589 /* iterate array */
3280af22 1590 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1591 RETPUSHNO;
a0d0e21e 1592
7a4c00b4 1593 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1594
93965878
NIS
1595 if (sv = (SvMAGICAL(av))
1596 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1597 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1598 SvTEMP_off(sv);
a0d0e21e 1599 else
3280af22
NIS
1600 sv = &PL_sv_undef;
1601 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1602 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1603 if (lv && SvREFCNT(lv) > 1) {
1604 SvREFCNT_dec(lv);
1605 lv = Nullsv;
1606 }
5f05dabc
PP
1607 if (lv)
1608 SvREFCNT_dec(LvTARG(lv));
1609 else {
68dc0745 1610 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1611 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1612 LvTYPE(lv) = 'y';
68dc0745 1613 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1614 }
1615 LvTARG(lv) = SvREFCNT_inc(av);
1616 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1617 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1618 sv = (SV*)lv;
1619 }
a0d0e21e 1620
7a4c00b4 1621 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1622 RETPUSHYES;
1623}
1624
1625PP(pp_subst)
1626{
4e35701f 1627 djSP; dTARG;
a0d0e21e
LW
1628 register PMOP *pm = cPMOP;
1629 PMOP *rpm = pm;
1630 register SV *dstr;
1631 register char *s;
1632 char *strend;
1633 register char *m;
1634 char *c;
1635 register char *d;
1636 STRLEN clen;
1637 I32 iters = 0;
1638 I32 maxiters;
1639 register I32 i;
1640 bool once;
71be2cbc 1641 bool rxtainted;
a0d0e21e 1642 char *orig;
22e551b9 1643 I32 r_flags;
d9f97599 1644 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1645 STRLEN len;
1646 int force_on_match = 0;
3280af22 1647 I32 oldsave = PL_savestack_ix;
c277df42 1648 I32 update_minmatch = 1;
a0d0e21e 1649
5cd24f17
PP
1650 /* known replacement string? */
1651 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1652 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1653 TARG = POPs;
1654 else {
54b9620d 1655 TARG = DEFSV;
a0d0e21e 1656 EXTEND(SP,1);
8ec5e241 1657 }
68dc0745
PP
1658 if (SvREADONLY(TARG)
1659 || (SvTYPE(TARG) > SVt_PVLV
1660 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
22c35a8c 1661 croak(PL_no_modify);
8ec5e241
NIS
1662 PUTBACK;
1663
a0d0e21e 1664 s = SvPV(TARG, len);
68dc0745 1665 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1666 force_on_match = 1;
b3eb6a9b 1667 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1668 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1669 if (PL_tainted)
b3eb6a9b 1670 rxtainted |= 2;
9212bbba 1671 TAINT_NOT;
a0d0e21e
LW
1672
1673 force_it:
1674 if (!pm || !s)
1675 DIE("panic: do_subst");
1676
1677 strend = s + len;
2beec16e
IZ
1678 maxiters = 2*(strend - s) + 10; /* We can match twice at each
1679 position, once with zero-length,
1680 second time with non-zero. */
a0d0e21e 1681
3280af22
NIS
1682 if (!rx->prelen && PL_curpm) {
1683 pm = PL_curpm;
d9f97599 1684 rx = pm->op_pmregexp;
a0d0e21e 1685 }
22e551b9 1686 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1687 ? REXEC_COPY_STR : 0;
22e551b9
IZ
1688 if (SvSCREAM(TARG) && rx->check_substr
1689 && SvTYPE(rx->check_substr) == SVt_PVBM
1690 && SvVALID(rx->check_substr))
1691 r_flags |= REXEC_SCREAM;
a0d0e21e 1692 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1693 SAVEINT(PL_multiline);
1694 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1695 }
1696 orig = m = s;
d9f97599
GS
1697 if (rx->check_substr) {
1698 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
22e551b9 1699 if (r_flags & REXEC_SCREAM) {
c277df42 1700 I32 p = -1;
a0ed51b3 1701 char *b;
c277df42 1702
3280af22 1703 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1704 goto nope;
a0ed51b3 1705
dfe13c55 1706 b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1707 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
a0d0e21e
LW
1708 goto nope;
1709 }
a0ed51b3 1710 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
c277df42 1711 (unsigned char*)strend,
411d5715 1712 rx->check_substr, 0)))
a0d0e21e 1713 goto nope;
d9f97599
GS
1714 if (s && rx->check_offset_max < s - m) {
1715 ++BmUSEFUL(rx->check_substr);
dfe13c55 1716 s = (char*)HOP((U8*)s, -rx->check_offset_max);
a0d0e21e
LW
1717 }
1718 else
1719 s = m;
1720 }
c277df42
IZ
1721 /* Now checkstring is fixed, i.e. at fixed offset from the
1722 beginning of match, and the match is anchored at s. */
3280af22 1723 else if (!PL_multiline) { /* Anchored at beginning of string. */
c277df42 1724 I32 slen;
dfe13c55 1725 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
a0ed51b3 1726 if (*SvPVX(rx->check_substr) != *b
d9f97599 1727 || ((slen = SvCUR(rx->check_substr)) > 1
a0ed51b3 1728 && memNE(SvPVX(rx->check_substr), b, slen)))
bbce6d69 1729 goto nope;
a0d0e21e 1730 }
a0ed51b3 1731 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
d9f97599
GS
1732 && rx->check_substr == rx->float_substr) {
1733 SvREFCNT_dec(rx->check_substr);
1734 rx->check_substr = Nullsv; /* opt is being useless */
1735 rx->float_substr = Nullsv;
a0d0e21e
LW
1736 }
1737 }
71be2cbc
PP
1738
1739 /* only replace once? */
a0d0e21e 1740 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1741
1742 /* known replacement string? */
5cd24f17 1743 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1744
1745 /* can do inplace substitution? */
22e551b9 1746 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1747 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
22e551b9 1748 if (!CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
8ec5e241 1749 SPAGAIN;
3280af22 1750 PUSHs(&PL_sv_no);
71be2cbc
PP
1751 LEAVE_SCOPE(oldsave);
1752 RETURN;
1753 }
1754 if (force_on_match) {
1755 force_on_match = 0;
1756 s = SvPV_force(TARG, len);
1757 goto force_it;
1758 }
71be2cbc 1759 d = s;
3280af22 1760 PL_curpm = pm;
71be2cbc
PP
1761 SvSCREAM_off(TARG); /* disable possible screamer */
1762 if (once) {
48c036b1 1763 rxtainted |= RX_MATCH_TAINTED(rx);
d9f97599
GS
1764 if (rx->subbase) {
1765 m = orig + (rx->startp[0] - rx->subbase);
1766 d = orig + (rx->endp[0] - rx->subbase);
c277df42 1767 } else {
d9f97599
GS
1768 m = rx->startp[0];
1769 d = rx->endp[0];
c277df42 1770 }
71be2cbc
PP
1771 s = orig;
1772 if (m - s > strend - d) { /* faster to shorten from end */
1773 if (clen) {
1774 Copy(c, m, clen, char);
1775 m += clen;
a0d0e21e 1776 }
71be2cbc
PP
1777 i = strend - d;
1778 if (i > 0) {
1779 Move(d, m, i, char);
1780 m += i;
a0d0e21e 1781 }
71be2cbc
PP
1782 *m = '\0';
1783 SvCUR_set(TARG, m - s);
1784 }
1785 /*SUPPRESS 560*/
1786 else if (i = m - s) { /* faster from front */
1787 d -= clen;
1788 m = d;
1789 sv_chop(TARG, d-i);
1790 s += i;
1791 while (i--)
1792 *--d = *--s;
1793 if (clen)
1794 Copy(c, m, clen, char);
1795 }
1796 else if (clen) {
1797 d -= clen;
1798 sv_chop(TARG, d);
1799 Copy(c, d, clen, char);
1800 }
1801 else {
1802 sv_chop(TARG, d);
1803 }
48c036b1 1804 TAINT_IF(rxtainted & 1);
8ec5e241 1805 SPAGAIN;
3280af22 1806 PUSHs(&PL_sv_yes);
71be2cbc
PP
1807 }
1808 else {
71be2cbc
PP
1809 do {
1810 if (iters++ > maxiters)
1811 DIE("Substitution loop");
d9f97599
GS
1812 rxtainted |= RX_MATCH_TAINTED(rx);
1813 m = rx->startp[0];
71be2cbc
PP
1814 /*SUPPRESS 560*/
1815 if (i = m - s) {
1816 if (s != d)
1817 Move(s, d, i, char);
1818 d += i;
a0d0e21e 1819 }
71be2cbc
PP
1820 if (clen) {
1821 Copy(c, d, clen, char);
1822 d += clen;
1823 }
d9f97599 1824 s = rx->endp[0];
15e52e56 1825 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
c277df42 1826 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1827 if (s != d) {
1828 i = strend - s;
1829 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1830 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1831 }
48c036b1 1832 TAINT_IF(rxtainted & 1);
8ec5e241 1833 SPAGAIN;
71be2cbc 1834 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1835 }
71be2cbc 1836 (void)SvPOK_only(TARG);
48c036b1 1837 TAINT_IF(rxtainted);
8ec5e241
NIS
1838 if (SvSMAGICAL(TARG)) {
1839 PUTBACK;
1840 mg_set(TARG);
1841 SPAGAIN;
1842 }
9212bbba 1843 SvTAINT(TARG);
71be2cbc
PP
1844 LEAVE_SCOPE(oldsave);
1845 RETURN;
a0d0e21e 1846 }
71be2cbc 1847
22e551b9 1848 if (CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL, r_flags)) {
a0d0e21e
LW
1849 if (force_on_match) {
1850 force_on_match = 0;
1851 s = SvPV_force(TARG, len);
1852 goto force_it;
1853 }
48c036b1 1854 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1855 dstr = NEWSV(25, len);
a0d0e21e 1856 sv_setpvn(dstr, m, s-m);
3280af22 1857 PL_curpm = pm;
a0d0e21e 1858 if (!c) {
c09156bb 1859 register PERL_CONTEXT *cx;
8ec5e241 1860 SPAGAIN;
a0d0e21e
LW
1861 PUSHSUBST(cx);
1862 RETURNOP(cPMOP->op_pmreplroot);
1863 }
ad94a511 1864 r_flags |= REXEC_IGNOREPOS;
a0d0e21e
LW
1865 do {
1866 if (iters++ > maxiters)
1867 DIE("Substitution loop");
d9f97599
GS
1868 rxtainted |= RX_MATCH_TAINTED(rx);
1869 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
1870 m = s;
1871 s = orig;
d9f97599 1872 orig = rx->subbase;
a0d0e21e
LW
1873 s = orig + (m - s);
1874 strend = s + (strend - m);
1875 }
d9f97599 1876 m = rx->startp[0];
a0d0e21e 1877 sv_catpvn(dstr, s, m-s);
d9f97599 1878 s = rx->endp[0];
a0d0e21e
LW
1879 if (clen)
1880 sv_catpvn(dstr, c, clen);
1881 if (once)
1882 break;
ad94a511 1883 } while (CALLREGEXEC(rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1884 sv_catpvn(dstr, s, strend - s);
748a9306 1885
4633a7c4 1886 (void)SvOOK_off(TARG);
cb0b1708 1887 Safefree(SvPVX(TARG));
748a9306
LW
1888 SvPVX(TARG) = SvPVX(dstr);
1889 SvCUR_set(TARG, SvCUR(dstr));
1890 SvLEN_set(TARG, SvLEN(dstr));
1891 SvPVX(dstr) = 0;
1892 sv_free(dstr);
1893
48c036b1 1894 TAINT_IF(rxtainted & 1);
f878fbec 1895 SPAGAIN;
48c036b1
GS
1896 PUSHs(sv_2mortal(newSViv((I32)iters)));
1897
a0d0e21e 1898 (void)SvPOK_only(TARG);
48c036b1 1899 TAINT_IF(rxtainted);
a0d0e21e 1900 SvSETMAGIC(TARG);
9212bbba 1901 SvTAINT(TARG);
4633a7c4 1902 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1903 RETURN;
1904 }
5cd24f17 1905 goto ret_no;
a0d0e21e
LW
1906
1907nope:
d9f97599 1908 ++BmUSEFUL(rx->check_substr);
5cd24f17 1909
8ec5e241
NIS
1910ret_no:
1911 SPAGAIN;
3280af22 1912 PUSHs(&PL_sv_no);
4633a7c4 1913 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1914 RETURN;
1915}
1916
1917PP(pp_grepwhile)
1918{
4e35701f 1919 djSP;
a0d0e21e
LW
1920
1921 if (SvTRUEx(POPs))
3280af22
NIS
1922 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1923 ++*PL_markstack_ptr;
a0d0e21e
LW
1924 LEAVE; /* exit inner scope */
1925
1926 /* All done yet? */
3280af22 1927 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 1928 I32 items;
54310121 1929 I32 gimme = GIMME_V;
a0d0e21e
LW
1930
1931 LEAVE; /* exit outer scope */
1932 (void)POPMARK; /* pop src */
3280af22 1933 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1934 (void)POPMARK; /* pop dst */
3280af22 1935 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1936 if (gimme == G_SCALAR) {
a0d0e21e
LW
1937 dTARGET;
1938 XPUSHi(items);
a0d0e21e 1939 }
54310121
PP
1940 else if (gimme == G_ARRAY)
1941 SP += items;
a0d0e21e
LW
1942 RETURN;
1943 }
1944 else {
1945 SV *src;
1946
1947 ENTER; /* enter inner scope */
3280af22 1948 SAVESPTR(PL_curpm);
a0d0e21e 1949
3280af22 1950 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1951 SvTEMP_off(src);
54b9620d 1952 DEFSV = src;
a0d0e21e
LW
1953
1954 RETURNOP(cLOGOP->op_other);
1955 }
1956}
1957
1958PP(pp_leavesub)
1959{
4e35701f 1960 djSP;
a0d0e21e
LW
1961 SV **mark;
1962 SV **newsp;
1963 PMOP *newpm;
1964 I32 gimme;
c09156bb 1965 register PERL_CONTEXT *cx;
f86702cc 1966 struct block_sub cxsub;
a0d0e21e
LW
1967
1968 POPBLOCK(cx,newpm);
f86702cc
PP
1969 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1970
a1f49e72 1971 TAINT_NOT;
a0d0e21e
LW
1972 if (gimme == G_SCALAR) {
1973 MARK = newsp + 1;
a29cdaf0
IZ
1974 if (MARK <= SP) {
1975 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1976 if (SvTEMP(TOPs)) {
1977 *MARK = SvREFCNT_inc(TOPs);
1978 FREETMPS;
1979 sv_2mortal(*MARK);
1980 } else {
1981 FREETMPS;
1982 *MARK = sv_mortalcopy(TOPs);
1983 }
1984 } else
1985 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1986 } else {
f86702cc 1987 MEXTEND(MARK, 0);
3280af22 1988 *MARK = &PL_sv_undef;
a0d0e21e
LW
1989 }
1990 SP = MARK;
1991 }
54310121 1992 else if (gimme == G_ARRAY) {
f86702cc 1993 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1994 if (!SvTEMP(*MARK)) {
f86702cc 1995 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1996 TAINT_NOT; /* Each item is independent */
1997 }
f86702cc 1998 }
a0d0e21e 1999 }
f86702cc
PP
2000 PUTBACK;
2001
2002 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
3280af22 2003 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
2004
2005 LEAVE;
a0d0e21e
LW
2006 return pop_return();
2007}
2008
76e3520e 2009STATIC CV *
8ac85365 2010get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
2011{
2012 dTHR;
3280af22 2013 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2014
2015 if (!PERLDB_SUB_NN) {
2016 GV *gv = CvGV(cv);
2017
2018 save_item(dbsv);
2019 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2020 || strEQ(GvNAME(gv), "END")
2021 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2022 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2023 && (gv = (GV*)*svp) ))) {
2024 /* Use GV from the stack as a fallback. */
2025 /* GV is potentially non-unique, or contain different CV. */
2026 sv_setsv(dbsv, newRV((SV*)cv));
2027 }
2028 else {
2029 gv_efullname3(dbsv, gv, Nullch);
2030 }
3de9ffa1
MB
2031 }
2032 else {
491527d0
GS
2033 SvUPGRADE(dbsv, SVt_PVIV);
2034 SvIOK_on(dbsv);
2035 SAVEIV(SvIVX(dbsv));
2036 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 2037 }
491527d0 2038
3de9ffa1 2039 if (CvXSUB(cv))
3280af22
NIS
2040 PL_curcopdb = PL_curcop;
2041 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2042 return cv;
2043}
2044
a0d0e21e
LW
2045PP(pp_entersub)
2046{
4e35701f 2047 djSP; dPOPss;
a0d0e21e
LW
2048 GV *gv;
2049 HV *stash;
2050 register CV *cv;
c09156bb 2051 register PERL_CONTEXT *cx;
5d94fbed 2052 I32 gimme;
533c011a 2053 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2054
2055 if (!sv)
2056 DIE("Not a CODE reference");
2057 switch (SvTYPE(sv)) {
2058 default:
2059 if (!SvROK(sv)) {
748a9306 2060 char *sym;
2d8e6c8d 2061 STRLEN n_a;
748a9306 2062
3280af22 2063 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2064 if (hasargs)
3280af22 2065 SP = PL_stack_base + POPMARK;
a0d0e21e 2066 RETURN;
fb73857a 2067 }
15ff848f
CS
2068 if (SvGMAGICAL(sv)) {
2069 mg_get(sv);
2070 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2071 }
2072 else
2d8e6c8d 2073 sym = SvPV(sv, n_a);
15ff848f 2074 if (!sym)
22c35a8c 2075 DIE(PL_no_usym, "a subroutine");
533c011a 2076 if (PL_op->op_private & HINT_STRICT_REFS)
22c35a8c 2077 DIE(PL_no_symref, sym, "a subroutine");
748a9306 2078 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
2079 break;
2080 }
f5284f61
IZ
2081 {
2082 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2083 tryAMAGICunDEREF(to_cv);
2084 }
a0d0e21e
LW
2085 cv = (CV*)SvRV(sv);
2086 if (SvTYPE(cv) == SVt_PVCV)
2087 break;
2088 /* FALL THROUGH */
2089 case SVt_PVHV:
2090 case SVt_PVAV:
2091 DIE("Not a CODE reference");
2092 case SVt_PVCV:
2093 cv = (CV*)sv;
2094 break;
2095 case SVt_PVGV:
8ebc5c01 2096 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e 2097 cv = sv_2cv(sv, &stash, &gv, TRUE);
67caa1fe
GS
2098 if (cv)
2099 break;
2100 DIE("Not a CODE reference");
a0d0e21e
LW
2101 }
2102
2103 ENTER;
2104 SAVETMPS;
2105
2106 retry:
a0d0e21e 2107 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2108 GV* autogv;
22239a37 2109 SV* sub_name;
44a8e56a
PP
2110
2111 /* anonymous or undef'd function leaves us no recourse */
2112 if (CvANON(cv) || !(gv = CvGV(cv)))
2113 DIE("Undefined subroutine called");
67caa1fe 2114
44a8e56a
PP
2115 /* autoloaded stub? */
2116 if (cv != GvCV(gv)) {
2117 cv = GvCV(gv);
a0d0e21e 2118 }
44a8e56a 2119 /* should call AUTOLOAD now? */
67caa1fe 2120 else if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
54310121
PP
2121 FALSE)))
2122 {
44a8e56a 2123 cv = GvCV(autogv);
a0d0e21e 2124 }
44a8e56a 2125 /* sorry */
67caa1fe
GS
2126 else {
2127 sub_name = sv_newmortal();
2128 gv_efullname3(sub_name, gv, Nullch);
2129 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
2130 }
2131 if (!cv)
2132 DIE("Not a CODE reference");
2133 goto retry;
a0d0e21e
LW
2134 }
2135
54310121 2136 gimme = GIMME_V;
67caa1fe 2137 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
4f01c5a5 2138 cv = get_db_sub(&sv, cv);
67caa1fe
GS
2139 if (!cv)
2140 DIE("No DBsub routine");
2141 }
a0d0e21e 2142
11343788 2143#ifdef USE_THREADS
3de9ffa1
MB
2144 /*
2145 * First we need to check if the sub or method requires locking.
458fb581
MB
2146 * If so, we gain a lock on the CV, the first argument or the
2147 * stash (for static methods), as appropriate. This has to be
2148 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2149 * reschedule by returning a new op.
3de9ffa1 2150 */
11343788 2151 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2152 if (CvFLAGS(cv) & CVf_LOCKED) {
2153 MAGIC *mg;
2154 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2155 if (SP > PL_stack_base + TOPMARK)
2156 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2157 else {
13e08037
GS
2158 AV *av = (AV*)PL_curpad[0];
2159 if (hasargs || !av || AvFILLp(av) < 0
2160 || !(sv = AvARRAY(av)[0]))
2161 {
2162 MUTEX_UNLOCK(CvMUTEXP(cv));
2163 croak("no argument for locked method call");
2164 }
77a005ab
MB
2165 }
2166 if (SvROK(sv))
2167 sv = SvRV(sv);
458fb581
MB
2168 else {
2169 STRLEN len;
2170 char *stashname = SvPV(sv, len);
2171 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2172 }
77a005ab
MB
2173 }
2174 else {
2175 sv = (SV*)cv;
2176 }
2177 MUTEX_UNLOCK(CvMUTEXP(cv));
2178 mg = condpair_magic(sv);
2179 MUTEX_LOCK(MgMUTEXP(mg));
2180 if (MgOWNER(mg) == thr)
2181 MUTEX_UNLOCK(MgMUTEXP(mg));
2182 else {
2183 while (MgOWNER(mg))
2184 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2185 MgOWNER(mg) = thr;
8b73bbec 2186 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
1fd28e87 2187 thr, sv);)
77a005ab
MB
2188 MUTEX_UNLOCK(MgMUTEXP(mg));
2189 save_destructor(unlock_condpair, sv);
11343788 2190 }
77a005ab 2191 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2192 }
3de9ffa1
MB
2193 /*
2194 * Now we have permission to enter the sub, we must distinguish
2195 * four cases. (0) It's an XSUB (in which case we don't care
2196 * about ownership); (1) it's ours already (and we're recursing);
2197 * (2) it's free (but we may already be using a cached clone);
2198 * (3) another thread owns it. Case (1) is easy: we just use it.
2199 * Case (2) means we look for a clone--if we have one, use it
2200 * otherwise grab ownership of cv. Case (3) means we look for a
2201 * clone (for non-XSUBs) and have to create one if we don't
2202 * already have one.
2203 * Why look for a clone in case (2) when we could just grab
2204 * ownership of cv straight away? Well, we could be recursing,
2205 * i.e. we originally tried to enter cv while another thread
2206 * owned it (hence we used a clone) but it has been freed up
2207 * and we're now recursing into it. It may or may not be "better"
2208 * to use the clone but at least CvDEPTH can be trusted.
2209 */
2210 if (CvOWNER(cv) == thr || CvXSUB(cv))
2211 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2212 else {
3de9ffa1
MB
2213 /* Case (2) or (3) */
2214 SV **svp;
2215
11343788 2216 /*
3de9ffa1
MB
2217 * XXX Might it be better to release CvMUTEXP(cv) while we
2218 * do the hv_fetch? We might find someone has pinched it
2219 * when we look again, in which case we would be in case
2220 * (3) instead of (2) so we'd have to clone. Would the fact
2221 * that we released the mutex more quickly make up for this?
2222 */
b099ddc0 2223 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2224 {
3de9ffa1 2225 /* We already have a clone to use */
11343788 2226 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2227 cv = *(CV**)svp;
8b73bbec 2228 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2229 "entersub: %p already has clone %p:%s\n",
2230 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2231 CvOWNER(cv) = thr;
2232 SvREFCNT_inc(cv);
2233 if (CvDEPTH(cv) == 0)
2234 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2235 }
11343788 2236 else {
3de9ffa1
MB
2237 /* (2) => grab ownership of cv. (3) => make clone */
2238 if (!CvOWNER(cv)) {
2239 CvOWNER(cv) = thr;
2240 SvREFCNT_inc(cv);
11343788 2241 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2242 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2243 "entersub: %p grabbing %p:%s in stash %s\n",
2244 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2245 HvNAME(CvSTASH(cv)) : "(none)"));
2246 } else {
2247 /* Make a new clone. */
2248 CV *clonecv;
2249 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2250 MUTEX_UNLOCK(CvMUTEXP(cv));
8b73bbec 2251 DEBUG_S((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2252 "entersub: %p cloning %p:%s\n",
2253 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2254 /*
2255 * We're creating a new clone so there's no race
2256 * between the original MUTEX_UNLOCK and the
2257 * SvREFCNT_inc since no one will be trying to undef
2258 * it out from underneath us. At least, I don't think
2259 * there's a race...
2260 */
2261 clonecv = cv_clone(cv);
2262 SvREFCNT_dec(cv); /* finished with this */
199100c8 2263 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2264 CvOWNER(clonecv) = thr;
2265 cv = clonecv;
11343788 2266 SvREFCNT_inc(cv);
11343788 2267 }
8b73bbec 2268 DEBUG_S(if (CvDEPTH(cv) != 0)
3de9ffa1
MB
2269 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2270 CvDEPTH(cv)););
2271 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2272 }
3de9ffa1 2273 }
11343788
MB
2274#endif /* USE_THREADS */
2275
a0d0e21e 2276 if (CvXSUB(cv)) {
67caa1fe 2277#ifdef PERL_XSUB_OLDSTYLE
a0d0e21e 2278 if (CvOLDSTYLE(cv)) {
ecfc5424 2279 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2280 dMARK;
2281 register I32 items = SP - MARK;
67955e0c 2282 /* We dont worry to copy from @_. */
924508f0
GS
2283 while (SP > mark) {
2284 SP[1] = SP[0];
2285 SP--;
a0d0e21e 2286 }
3280af22 2287 PL_stack_sp = mark + 1;
ecfc5424
AD
2288 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2289 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2290 MARK - PL_stack_base + 1,
ecfc5424 2291 items);
3280af22 2292 PL_stack_sp = PL_stack_base + items;
a0d0e21e 2293 }
67caa1fe
GS
2294 else
2295#endif /* PERL_XSUB_OLDSTYLE */
2296 {
748a9306
LW
2297 I32 markix = TOPMARK;
2298
a0d0e21e 2299 PUTBACK;
67955e0c
PP
2300
2301 if (!hasargs) {
2302 /* Need to copy @_ to stack. Alternative may be to
2303 * switch stack to @_, and copy return values
2304 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2305 AV* av;
2306 I32 items;
2307#ifdef USE_THREADS
533c011a 2308 av = (AV*)PL_curpad[0];
6d4ff0d2 2309#else
3280af22 2310 av = GvAV(PL_defgv);
6d4ff0d2 2311#endif /* USE_THREADS */
93965878 2312 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2313
2314 if (items) {
2315 /* Mark is at the end of the stack. */
924508f0
GS
2316 EXTEND(SP, items);
2317 Copy(AvARRAY(av), SP + 1, items, SV*);
2318 SP += items;
67955e0c
PP
2319 PUTBACK ;
2320 }
2321 }
67caa1fe
GS
2322 /* We assume first XSUB in &DB::sub is the called one. */
2323 if (PL_curcopdb) {
3280af22
NIS
2324 SAVESPTR(PL_curcop);
2325 PL_curcop = PL_curcopdb;
2326 PL_curcopdb = NULL;
67955e0c
PP
2327 }
2328 /* Do we need to open block here? XXXX */
1d583055 2329 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
748a9306
LW
2330
2331 /* Enforce some sanity in scalar context. */
3280af22
NIS
2332 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2333 if (markix > PL_stack_sp - PL_stack_base)
2334 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2335 else
3280af22
NIS
2336 *(PL_stack_base + markix) = *PL_stack_sp;
2337 PL_stack_sp = PL_stack_base + markix;
748a9306 2338 }
a0d0e21e
LW
2339 }
2340 LEAVE;
2341 return NORMAL;
2342 }
2343 else {
2344 dMARK;
2345 register I32 items = SP - MARK;
a0d0e21e
LW
2346 AV* padlist = CvPADLIST(cv);
2347 SV** svp = AvARRAY(padlist);
533c011a 2348 push_return(PL_op->op_next);
a0d0e21e
LW
2349 PUSHBLOCK(cx, CXt_SUB, MARK);
2350 PUSHSUB(cx);
2351 CvDEPTH(cv)++;
6b35e009
GS
2352 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2353 * that eval'' ops within this sub know the correct lexical space.
2354 * Owing the speed considerations, we choose to search for the cv
2355 * in doeval() instead.
2356 */
a0d0e21e
LW
2357 if (CvDEPTH(cv) < 2)
2358 (void)SvREFCNT_inc(cv);
2359 else { /* save temporaries on recursion? */
93965878 2360 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2361 AV *av;
2362 AV *newpad = newAV();
4aa0a1f7 2363 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2364 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2365 svp = AvARRAY(svp[0]);
748a9306 2366 for ( ;ix > 0; ix--) {
3280af22 2367 if (svp[ix] != &PL_sv_undef) {
748a9306 2368 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2369 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2370 || *name == '&') /* anonymous code? */
2371 {
2372 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2373 }
2374 else { /* our own lexical */
2375 if (*name == '@')
2376 av_store(newpad, ix, sv = (SV*)newAV());
2377 else if (*name == '%')
2378 av_store(newpad, ix, sv = (SV*)newHV());
2379 else
2380 av_store(newpad, ix, sv = NEWSV(0,0));
2381 SvPADMY_on(sv);
2382 }
a0d0e21e
LW
2383 }
2384 else {
748a9306 2385 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2386 SvPADTMP_on(sv);
2387 }
2388 }
2389 av = newAV(); /* will be @_ */
2390 av_extend(av, 0);
2391 av_store(newpad, 0, (SV*)av);
2392 AvFLAGS(av) = AVf_REIFY;
2393 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2394 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2395 svp = AvARRAY(padlist);
2396 }
2397 }
6d4ff0d2
MB
2398#ifdef USE_THREADS
2399 if (!hasargs) {
533c011a 2400 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2401
93965878 2402 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2403 if (items) {
2404 /* Mark is at the end of the stack. */
924508f0
GS
2405 EXTEND(SP, items);
2406 Copy(AvARRAY(av), SP + 1, items, SV*);
2407 SP += items;
6d4ff0d2
MB
2408 PUTBACK ;
2409 }
2410 }
2411#endif /* USE_THREADS */
3280af22
NIS
2412 SAVESPTR(PL_curpad);
2413 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2414#ifndef USE_THREADS
2415 if (hasargs)
2416#endif /* USE_THREADS */
2417 {
2418 AV* av;
a0d0e21e
LW
2419 SV** ary;
2420
77a005ab 2421#if 0
8b73bbec 2422 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2423 "%p entersub preparing @_\n", thr));
77a005ab 2424#endif
3280af22 2425 av = (AV*)PL_curpad[0];
a0d0e21e
LW
2426 if (AvREAL(av)) {
2427 av_clear(av);
2428 AvREAL_off(av);
2429 }
6d4ff0d2 2430#ifndef USE_THREADS
3280af22
NIS
2431 cx->blk_sub.savearray = GvAV(PL_defgv);
2432 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2433#endif /* USE_THREADS */
2434 cx->blk_sub.argarray = av;
a0d0e21e
LW
2435 ++MARK;
2436
2437 if (items > AvMAX(av) + 1) {
2438 ary = AvALLOC(av);
2439 if (AvARRAY(av) != ary) {
2440 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2441 SvPVX(av) = (char*)ary;
2442 }
2443 if (items > AvMAX(av) + 1) {
2444 AvMAX(av) = items - 1;
2445 Renew(ary,items,SV*);
2446 AvALLOC(av) = ary;
2447 SvPVX(av) = (char*)ary;
2448 }
2449 }
2450 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2451 AvFILLp(av) = items - 1;
a0d0e21e
LW
2452
2453 while (items--) {
2454 if (*MARK)
2455 SvTEMP_off(*MARK);
2456 MARK++;
2457 }
2458 }
4a925ff6
GS
2459 /* warning must come *after* we fully set up the context
2460 * stuff so that __WARN__ handlers can safely dounwind()
2461 * if they want to
2462 */
2463 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2464 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2465 sub_crush_depth(cv);
77a005ab 2466#if 0
8b73bbec 2467 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0f15f207 2468 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2469#endif
a0d0e21e
LW
2470 RETURNOP(CvSTART(cv));
2471 }
2472}
2473
44a8e56a 2474void
8ac85365 2475sub_crush_depth(CV *cv)
44a8e56a
PP
2476{
2477 if (CvANON(cv))
599cee73 2478 warner(WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2479 else {
2480 SV* tmpstr = sv_newmortal();
2481 gv_efullname3(tmpstr, CvGV(cv), Nullch);
599cee73
PM
2482 warner(WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
2483 SvPVX(tmpstr));
44a8e56a
PP
2484 }
2485}
2486
a0d0e21e
LW
2487PP(pp_aelem)
2488{
4e35701f 2489 djSP;
a0d0e21e 2490 SV** svp;
748a9306 2491 I32 elem = POPi;
68dc0745 2492 AV* av = (AV*)POPs;
533c011a
NIS
2493 U32 lval = PL_op->op_flags & OPf_MOD;
2494 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2495 SV *sv;
a0d0e21e 2496
748a9306 2497 if (elem > 0)
3280af22 2498 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2499 if (SvTYPE(av) != SVt_PVAV)
2500 RETPUSHUNDEF;
68dc0745 2501 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2502 if (lval) {
3280af22 2503 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2504 SV* lv;
2505 if (!defer)
22c35a8c 2506 DIE(PL_no_aelem, elem);
68dc0745
PP
2507 lv = sv_newmortal();
2508 sv_upgrade(lv, SVt_PVLV);
2509 LvTYPE(lv) = 'y';
2510 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2511 LvTARG(lv) = SvREFCNT_inc(av);
2512 LvTARGOFF(lv) = elem;
2513 LvTARGLEN(lv) = 1;
2514 PUSHs(lv);
2515 RETURN;
2516 }
533c011a 2517 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2518 save_aelem(av, elem, svp);
533c011a
NIS
2519 else if (PL_op->op_private & OPpDEREF)
2520 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2521 }
3280af22 2522 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2523 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2524 sv = sv_mortalcopy(sv);
2525 PUSHs(sv);
a0d0e21e
LW
2526 RETURN;
2527}
2528
02a9e968 2529void
8ac85365 2530vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2531{
2532 if (SvGMAGICAL(sv))
2533 mg_get(sv);
2534 if (!SvOK(sv)) {
2535 if (SvREADONLY(sv))
22c35a8c 2536 croak(PL_no_modify);
5f05dabc
PP
2537 if (SvTYPE(sv) < SVt_RV)
2538 sv_upgrade(sv, SVt_RV);
2539 else if (SvTYPE(sv) >= SVt_PV) {
2540 (void)SvOOK_off(sv);
2541 Safefree(SvPVX(sv));
2542 SvLEN(sv) = SvCUR(sv) = 0;
2543 }
68dc0745 2544 switch (to_what) {
5f05dabc 2545 case OPpDEREF_SV:
8c52afec 2546 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2547 break;
2548 case OPpDEREF_AV:
2549 SvRV(sv) = (SV*)newAV();
2550 break;
2551 case OPpDEREF_HV:
2552 SvRV(sv) = (SV*)newHV();
2553 break;
2554 }
02a9e968
CS
2555 SvROK_on(sv);
2556 SvSETMAGIC(sv);
2557 }
2558}
2559
a0d0e21e
LW
2560PP(pp_method)
2561{
4e35701f 2562 djSP;
a0d0e21e
LW
2563 SV* sv;
2564 SV* ob;
2565 GV* gv;
56304f61
CS
2566 HV* stash;
2567 char* name;
ac91690f
CS
2568 char* packname;
2569 STRLEN packlen;
a0d0e21e 2570
fb73857a
PP
2571 if (SvROK(TOPs)) {
2572 sv = SvRV(TOPs);
2573 if (SvTYPE(sv) == SVt_PVCV) {
2574 SETs(sv);
2575 RETURN;
2576 }
2577 }
2578
2d8e6c8d 2579 name = SvPV(TOPs, packlen);
3280af22 2580 sv = *(PL_stack_base + TOPMARK + 1);
a0d0e21e 2581
16d20bd9
AD
2582 if (SvGMAGICAL(sv))
2583 mg_get(sv);
a0d0e21e 2584 if (SvROK(sv))
16d20bd9 2585 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2586 else {
2587 GV* iogv;
a0d0e21e 2588
56304f61 2589 packname = Nullch;
a0d0e21e 2590 if (!SvOK(sv) ||
56304f61 2591 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2592 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2593 !(ob=(SV*)GvIO(iogv)))
2594 {
834a4ddd
LW
2595 if (!packname ||
2596 ((*(U8*)packname >= 0xc0 && IN_UTF8)
b86a2fa7 2597 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2598 : !isIDFIRST(*packname)
2599 ))
2600 {
72b5445b
GS
2601 DIE("Can't call method \"%s\" %s", name,
2602 SvOK(sv)? "without a package or object reference"
2603 : "on an undefined value");
834a4ddd 2604 }
56304f61 2605 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2606 goto fetch;
a0d0e21e 2607 }
3280af22 2608 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2609 }
2610
ac91690f 2611 if (!ob || !SvOBJECT(ob))
a0d0e21e 2612 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2613
56304f61 2614 stash = SvSTASH(ob);
a0d0e21e 2615
ac91690f 2616 fetch:
ac91690f 2617 gv = gv_fetchmethod(stash, name);
56304f61
CS
2618 if (!gv) {
2619 char* leaf = name;
2620 char* sep = Nullch;
2621 char* p;
2622
2623 for (p = name; *p; p++) {
2624 if (*p == '\'')
2625 sep = p, leaf = p + 1;
2626 else if (*p == ':' && *(p + 1) == ':')
2627 sep = p, leaf = p + 2;
2628 }
2629 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3280af22 2630 packname = HvNAME(sep ? PL_curcop->cop_stash : stash);
56304f61
CS
2631 packlen = strlen(packname);
2632 }
2633 else {
2634 packname = name;
2635 packlen = sep - name;
2636 }
2637 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2638 leaf, (int)packlen, packname);
2639 }
8ebc5c01 2640 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2641 RETURN;
2642}
22239a37 2643