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