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