Test case for #10433/#10424.
[p5sagit/p5-mst-13.2.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
79072805 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 *
a0d0e21e 8 */
9
10/*
11 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
79072805 13 */
14
15#include "EXTERN.h"
864dbfa3 16#define PERL_IN_MG_C
79072805 17#include "perl.h"
18
5cd24f17 19#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221 20# ifndef NGROUPS
21# define NGROUPS 32
22# endif
b7953727 23# ifdef I_GRP
24# include <grp.h>
25# endif
188ea221 26#endif
27
51371543 28static void restore_magic(pTHXo_ void *p);
29static void unwind_handler_stack(pTHXo_ void *p);
30
c07a80fd 31/*
32 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
33 */
34
35struct magic_state {
36 SV* mgs_sv;
37 U32 mgs_flags;
455ece5e 38 I32 mgs_ss_ix;
c07a80fd 39};
455ece5e 40/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e 41
42STATIC void
425d70b4 43S_save_magic(pTHX_ IV mgs_ix, SV *sv)
c07a80fd 44{
455ece5e 45 MGS* mgs;
c07a80fd 46 assert(SvMAGICAL(sv));
47
a6f8e609 48 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*,mgs_ix));
455ece5e 49
50 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd 51 mgs->mgs_sv = sv;
52 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 53 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd 54
55 SvMAGICAL_off(sv);
56 SvREADONLY_off(sv);
06759ea0 57 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd 58}
59
954c1994 60/*
61=for apidoc mg_magical
62
63Turns on the magical status of an SV. See C<sv_magic>.
64
65=cut
66*/
67
8990e307 68void
864dbfa3 69Perl_mg_magical(pTHX_ SV *sv)
8990e307 70{
71 MAGIC* mg;
72 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
73 MGVTBL* vtbl = mg->mg_virtual;
74 if (vtbl) {
2b260de0 75 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307 76 SvGMAGICAL_on(sv);
77 if (vtbl->svt_set)
78 SvSMAGICAL_on(sv);
2b260de0 79 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307 80 SvRMAGICAL_on(sv);
81 }
82 }
83}
84
954c1994 85/*
86=for apidoc mg_get
87
88Do magic after a value is retrieved from the SV. See C<sv_magic>.
89
90=cut
91*/
92
79072805 93int
864dbfa3 94Perl_mg_get(pTHX_ SV *sv)
79072805 95{
425d70b4 96 IV mgs_ix;
79072805 97 MAGIC* mg;
c6496cc7 98 MAGIC** mgp;
760ac839 99 int mgp_valid = 0;
463ee0b2 100
455ece5e 101 mgs_ix = SSNEW(sizeof(MGS));
102 save_magic(mgs_ix, sv);
463ee0b2 103
c6496cc7 104 mgp = &SvMAGIC(sv);
105 while ((mg = *mgp) != 0) {
79072805 106 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 107 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 108 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
c6496cc7 109 /* Ignore this magic if it's been deleted */
48e43a1c 110 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
111 (mg->mg_flags & MGf_GSKIP))
455ece5e 112 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 113 }
c6496cc7 114 /* Advance to next magic (complicated by possible deletion) */
760ac839 115 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
c6496cc7 116 mgp = &mg->mg_moremagic;
760ac839 117 mgp_valid = 1;
118 }
119 else
120 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
79072805 121 }
463ee0b2 122
a6f8e609 123 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
79072805 124 return 0;
125}
126
954c1994 127/*
128=for apidoc mg_set
129
130Do magic after a value is assigned to the SV. See C<sv_magic>.
131
132=cut
133*/
134
79072805 135int
864dbfa3 136Perl_mg_set(pTHX_ SV *sv)
79072805 137{
425d70b4 138 IV mgs_ix;
79072805 139 MAGIC* mg;
463ee0b2 140 MAGIC* nextmg;
141
455ece5e 142 mgs_ix = SSNEW(sizeof(MGS));
143 save_magic(mgs_ix, sv);
463ee0b2 144
145 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 146 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 147 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e 148 if (mg->mg_flags & MGf_GSKIP) {
149 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 150 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 151 }
2b260de0 152 if (vtbl && vtbl->svt_set)
fc0dc3b3 153 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 154 }
463ee0b2 155
a6f8e609 156 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
79072805 157 return 0;
158}
159
954c1994 160/*
161=for apidoc mg_length
162
163Report on the SV's length. See C<sv_magic>.
164
165=cut
166*/
167
79072805 168U32
864dbfa3 169Perl_mg_length(pTHX_ SV *sv)
79072805 170{
171 MAGIC* mg;
748a9306 172 char *junk;
463ee0b2 173 STRLEN len;
463ee0b2 174
79072805 175 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
176 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 177 if (vtbl && vtbl->svt_len) {
425d70b4 178 IV mgs_ix;
48e43a1c 179
455ece5e 180 mgs_ix = SSNEW(sizeof(MGS));
181 save_magic(mgs_ix, sv);
a0d0e21e 182 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 183 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
a6f8e609 184 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
85e6fe83 185 return len;
186 }
187 }
188
748a9306 189 junk = SvPV(sv, len);
463ee0b2 190 return len;
79072805 191}
192
425d70b4 193IV
864dbfa3 194Perl_mg_size(pTHX_ SV *sv)
93965878 195{
196 MAGIC* mg;
197 I32 len;
ac27b0f5 198
93965878 199 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
200 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 201 if (vtbl && vtbl->svt_len) {
425d70b4 202 IV mgs_ix;
455ece5e 203
204 mgs_ix = SSNEW(sizeof(MGS));
205 save_magic(mgs_ix, sv);
93965878 206 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 207 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
a6f8e609 208 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
93965878 209 return len;
210 }
211 }
212
213 switch(SvTYPE(sv)) {
214 case SVt_PVAV:
215 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
216 return len;
217 case SVt_PVHV:
218 /* FIXME */
219 default:
cea2e8a9 220 Perl_croak(aTHX_ "Size magic not implemented");
93965878 221 break;
222 }
223 return 0;
224}
225
954c1994 226/*
227=for apidoc mg_clear
228
229Clear something magical that the SV represents. See C<sv_magic>.
230
231=cut
232*/
233
79072805 234int
864dbfa3 235Perl_mg_clear(pTHX_ SV *sv)
79072805 236{
425d70b4 237 IV mgs_ix;
79072805 238 MAGIC* mg;
463ee0b2 239
455ece5e 240 mgs_ix = SSNEW(sizeof(MGS));
241 save_magic(mgs_ix, sv);
463ee0b2 242
79072805 243 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
244 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e 245 /* omit GSKIP -- never set here */
246
2b260de0 247 if (vtbl && vtbl->svt_clear)
fc0dc3b3 248 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 249 }
463ee0b2 250
a6f8e609 251 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
79072805 252 return 0;
253}
254
954c1994 255/*
256=for apidoc mg_find
257
258Finds the magic pointer for type matching the SV. See C<sv_magic>.
259
260=cut
261*/
262
93a17b20 263MAGIC*
864dbfa3 264Perl_mg_find(pTHX_ SV *sv, int type)
93a17b20 265{
266 MAGIC* mg;
3f8f4626 267 if (!sv)
268 return 0;
93a17b20 269 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
270 if (mg->mg_type == type)
271 return mg;
272 }
273 return 0;
274}
275
954c1994 276/*
277=for apidoc mg_copy
278
279Copies the magic from one SV to another. See C<sv_magic>.
280
281=cut
282*/
283
79072805 284int
864dbfa3 285Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 286{
463ee0b2 287 int count = 0;
79072805 288 MAGIC* mg;
463ee0b2 289 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
290 if (isUPPER(mg->mg_type)) {
33c27489 291 sv_magic(nsv,
14befaf4 292 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
293 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
294 ? sv : mg->mg_obj,
33c27489 295 toLOWER(mg->mg_type), key, klen);
463ee0b2 296 count++;
79072805 297 }
79072805 298 }
463ee0b2 299 return count;
79072805 300}
301
954c1994 302/*
303=for apidoc mg_free
304
305Free any magic storage used by the SV. See C<sv_magic>.
306
307=cut
308*/
309
79072805 310int
864dbfa3 311Perl_mg_free(pTHX_ SV *sv)
79072805 312{
313 MAGIC* mg;
314 MAGIC* moremagic;
315 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
316 MGVTBL* vtbl = mg->mg_virtual;
317 moremagic = mg->mg_moremagic;
2b260de0 318 if (vtbl && vtbl->svt_free)
fc0dc3b3 319 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 320 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
565764a8 321 if (mg->mg_len >= 0)
88e89b8a 322 Safefree(mg->mg_ptr);
565764a8 323 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 324 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 325 }
85e6fe83 326 if (mg->mg_flags & MGf_REFCOUNTED)
8990e307 327 SvREFCNT_dec(mg->mg_obj);
79072805 328 Safefree(mg);
329 }
330 SvMAGIC(sv) = 0;
331 return 0;
332}
333
d460ef45 334
79072805 335#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
336#include <signal.h>
337#endif
338
942e002e 339U32
864dbfa3 340Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 341{
6cef1e77 342 register REGEXP *rx;
6cef1e77 343
8f580fb8 344 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
345 if (mg->mg_obj) /* @+ */
346 return rx->nparens;
347 else /* @- */
348 return rx->lastparen;
349 }
ac27b0f5 350
942e002e 351 return (U32)-1;
6cef1e77 352}
353
354int
864dbfa3 355Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 356{
6cef1e77 357 register I32 paren;
cf93c79d 358 register I32 s;
6cef1e77 359 register I32 i;
360 register REGEXP *rx;
cf93c79d 361 I32 t;
6cef1e77 362
363 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
364 paren = mg->mg_len;
365 if (paren < 0)
366 return 0;
367 if (paren <= rx->nparens &&
cf93c79d 368 (s = rx->startp[paren]) != -1 &&
369 (t = rx->endp[paren]) != -1)
6cef1e77 370 {
371 if (mg->mg_obj) /* @+ */
cf93c79d 372 i = t;
6cef1e77 373 else /* @- */
cf93c79d 374 i = s;
1aa99e6b 375
376 if (i > 0 && DO_UTF8(PL_reg_sv)) {
377 char *b = rx->subbeg;
0064a8a9 378 if (b)
379 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 380 }
0064a8a9 381
382 sv_setiv(sv, i);
6cef1e77 383 }
384 }
385 return 0;
386}
387
e4b89193 388int
a29d06ed 389Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
390{
a29d06ed 391 Perl_croak(aTHX_ PL_no_modify);
e4b89193 392 /* NOT REACHED */
393 return 0;
a29d06ed 394}
395
93a17b20 396U32
864dbfa3 397Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20 398{
399 register I32 paren;
93a17b20 400 register I32 i;
d9f97599 401 register REGEXP *rx;
a197cbdd 402 I32 s1, t1;
93a17b20 403
404 switch (*mg->mg_ptr) {
405 case '1': case '2': case '3': case '4':
406 case '5': case '6': case '7': case '8': case '9': case '&':
3280af22 407 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 408
ffc61ed2 409 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 410 getparen:
d9f97599 411 if (paren <= rx->nparens &&
cf93c79d 412 (s1 = rx->startp[paren]) != -1 &&
413 (t1 = rx->endp[paren]) != -1)
bbce6d69 414 {
cf93c79d 415 i = t1 - s1;
a197cbdd 416 getlen:
ffc61ed2 417 if (i > 0 && DO_UTF8(PL_reg_sv)) {
418 char *s = rx->subbeg + s1;
a197cbdd 419 char *send = rx->subbeg + t1;
ffc61ed2 420
60425c38 421 i = t1 - s1;
422 if (is_utf8_string((U8*)s, i))
423 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
a197cbdd 424 }
ffc61ed2 425 if (i < 0)
0844c848 426 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 427 return i;
93a17b20 428 }
93a17b20 429 }
748a9306 430 return 0;
93a17b20 431 case '+':
3280af22 432 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
d9f97599 433 paren = rx->lastparen;
13f57bf8 434 if (paren)
435 goto getparen;
93a17b20 436 }
748a9306 437 return 0;
93a17b20 438 case '`':
3280af22 439 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 440 if (rx->startp[0] != -1) {
441 i = rx->startp[0];
a197cbdd 442 if (i > 0) {
443 s1 = 0;
444 t1 = i;
445 goto getlen;
446 }
93a17b20 447 }
93a17b20 448 }
748a9306 449 return 0;
93a17b20 450 case '\'':
3280af22 451 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 452 if (rx->endp[0] != -1) {
453 i = rx->sublen - rx->endp[0];
a197cbdd 454 if (i > 0) {
455 s1 = rx->endp[0];
456 t1 = rx->sublen;
457 goto getlen;
458 }
93a17b20 459 }
93a17b20 460 }
748a9306 461 return 0;
93a17b20 462 }
463 magic_get(sv,mg);
2d8e6c8d 464 if (!SvPOK(sv) && SvNIOK(sv)) {
465 STRLEN n_a;
466 sv_2pv(sv, &n_a);
467 }
93a17b20 468 if (SvPOK(sv))
469 return SvCUR(sv);
470 return 0;
471}
472
79072805 473int
864dbfa3 474Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805 475{
476 register I32 paren;
477 register char *s;
478 register I32 i;
d9f97599 479 register REGEXP *rx;
79072805 480
481 switch (*mg->mg_ptr) {
748a9306 482 case '\001': /* ^A */
3280af22 483 sv_setsv(sv, PL_bodytarget);
748a9306 484 break;
49460fe6 485 case '\003': /* ^C */
486 sv_setiv(sv, (IV)PL_minus_c);
487 break;
488
79072805 489 case '\004': /* ^D */
aea4f609 490 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
d056ab3f 491#if defined(YYDEBUG) && defined(DEBUGGING)
aea4f609 492 PL_yydebug = DEBUG_p_TEST;
d056ab3f 493#endif
79072805 494 break;
28f23441 495 case '\005': /* ^E */
cd39f2b6 496#ifdef MACOS_TRADITIONAL
497 {
498 char msg[256];
ac27b0f5 499
bf4acbe4 500 sv_setnv(sv,(double)gMacPerl_OSErr);
501 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
cd39f2b6 502 }
503#else
28f23441 504#ifdef VMS
505 {
506# include <descrip.h>
507# include <starlet.h>
508 char msg[255];
509 $DESCRIPTOR(msgdsc,msg);
65202027 510 sv_setnv(sv,(NV) vaxc$errno);
28f23441 511 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
512 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
513 else
514 sv_setpv(sv,"");
515 }
516#else
88e89b8a 517#ifdef OS2
fb73857a 518 if (!(_emx_env & 0x200)) { /* Under DOS */
65202027 519 sv_setnv(sv, (NV)errno);
fb73857a 520 sv_setpv(sv, errno ? Strerror(errno) : "");
521 } else {
017f25f1 522 if (errno != errno_isOS2) {
523 int tmp = _syserrno();
524 if (tmp) /* 2nd call to _syserrno() makes it 0 */
525 Perl_rc = tmp;
526 }
65202027 527 sv_setnv(sv, (NV)Perl_rc);
fb73857a 528 sv_setpv(sv, os2error(Perl_rc));
529 }
88e89b8a 530#else
22fae026 531#ifdef WIN32
532 {
533 DWORD dwErr = GetLastError();
65202027 534 sv_setnv(sv, (NV)dwErr);
22fae026 535 if (dwErr)
76e3520e 536 {
0cb96387 537 PerlProc_GetOSError(sv, dwErr);
76e3520e 538 }
22fae026 539 else
540 sv_setpv(sv, "");
541 SetLastError(dwErr);
542 }
543#else
65202027 544 sv_setnv(sv, (NV)errno);
28f23441 545 sv_setpv(sv, errno ? Strerror(errno) : "");
546#endif
88e89b8a 547#endif
22fae026 548#endif
cd39f2b6 549#endif
946ec16e 550 SvNOK_on(sv); /* what a wonderful hack! */
28f23441 551 break;
79072805 552 case '\006': /* ^F */
3280af22 553 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 554 break;
a0d0e21e 555 case '\010': /* ^H */
3280af22 556 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 557 break;
9d116dd7 558 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22 559 if (PL_inplace)
560 sv_setpv(sv, PL_inplace);
79072805 561 else
3280af22 562 sv_setsv(sv, &PL_sv_undef);
79072805 563 break;
ac27b0f5 564 case '\017': /* ^O & ^OPEN */
565 if (*(mg->mg_ptr+1) == '\0')
566 sv_setpv(sv, PL_osname);
567 else if (strEQ(mg->mg_ptr, "\017PEN")) {
568 if (!PL_compiling.cop_io)
569 sv_setsv(sv, &PL_sv_undef);
570 else {
571 sv_setsv(sv, PL_compiling.cop_io);
572 }
573 }
28f23441 574 break;
79072805 575 case '\020': /* ^P */
3280af22 576 sv_setiv(sv, (IV)PL_perldb);
79072805 577 break;
fb73857a 578 case '\023': /* ^S */
d58bf5aa 579 {
3280af22 580 if (PL_lex_state != LEX_NOTPARSING)
155aba94 581 (void)SvOK_off(sv);
3280af22 582 else if (PL_in_eval)
6dc8a9e4 583 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
d58bf5aa 584 }
fb73857a 585 break;
79072805 586 case '\024': /* ^T */
88e89b8a 587#ifdef BIG_TIME
6b88bc9c 588 sv_setnv(sv, PL_basetime);
88e89b8a 589#else
3280af22 590 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 591#endif
79072805 592 break;
46487f74 593 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
4438c4b7 594 if (*(mg->mg_ptr+1) == '\0')
595 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
6a818117 596 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
d3a7d8c7 597 if (PL_compiling.cop_warnings == pWARN_NONE ||
598 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7 599 {
600 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
601 }
d3a7d8c7 602 else if (PL_compiling.cop_warnings == pWARN_ALL) {
4438c4b7 603 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
ac27b0f5 604 }
4438c4b7 605 else {
606 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 607 }
d3a7d8c7 608 SvPOK_only(sv);
4438c4b7 609 }
46487f74 610 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
611 sv_setiv(sv, (IV)PL_widesyscalls);
79072805 612 break;
613 case '1': case '2': case '3': case '4':
614 case '5': case '6': case '7': case '8': case '9': case '&':
3280af22 615 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 616 I32 s1, t1;
617
a863c7d1 618 /*
619 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
620 * XXX Does the new way break anything?
621 */
ffc61ed2 622 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 623 getparen:
d9f97599 624 if (paren <= rx->nparens &&
cf93c79d 625 (s1 = rx->startp[paren]) != -1 &&
626 (t1 = rx->endp[paren]) != -1)
bbce6d69 627 {
cf93c79d 628 i = t1 - s1;
629 s = rx->subbeg + s1;
01ec43d0 630 if (!rx->subbeg)
c2e66d9e 631 break;
632
13f57bf8 633 getrx:
748a9306 634 if (i >= 0) {
b7953727 635 bool was_tainted = FALSE;
3280af22 636 if (PL_tainting) {
637 was_tainted = PL_tainted;
638 PL_tainted = FALSE;
13f57bf8 639 }
cf93c79d 640 sv_setpvn(sv, s, i);
bdc14a33 641 if (PL_reg_sv && DO_UTF8(PL_reg_sv) && is_utf8_string((U8*)s, i))
7e2040f0 642 SvUTF8_on(sv);
643 else
644 SvUTF8_off(sv);
3280af22 645 if (PL_tainting)
646 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
748a9306 647 break;
648 }
79072805 649 }
79072805 650 }
3280af22 651 sv_setsv(sv,&PL_sv_undef);
79072805 652 break;
653 case '+':
3280af22 654 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
d9f97599 655 paren = rx->lastparen;
a0d0e21e 656 if (paren)
657 goto getparen;
79072805 658 }
3280af22 659 sv_setsv(sv,&PL_sv_undef);
79072805 660 break;
661 case '`':
3280af22 662 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 663 if ((s = rx->subbeg) && rx->startp[0] != -1) {
664 i = rx->startp[0];
13f57bf8 665 goto getrx;
79072805 666 }
79072805 667 }
3280af22 668 sv_setsv(sv,&PL_sv_undef);
79072805 669 break;
670 case '\'':
3280af22 671 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 672 if (rx->subbeg && rx->endp[0] != -1) {
673 s = rx->subbeg + rx->endp[0];
674 i = rx->sublen - rx->endp[0];
13f57bf8 675 goto getrx;
79072805 676 }
79072805 677 }
3280af22 678 sv_setsv(sv,&PL_sv_undef);
79072805 679 break;
680 case '.':
681#ifndef lint
3280af22 682 if (GvIO(PL_last_in_gv)) {
357c8808 683 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805 684 }
685#endif
686 break;
687 case '?':
809a5acc 688 {
809a5acc 689 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 690#ifdef COMPLEX_STATUS
6b88bc9c 691 LvTARGOFF(sv) = PL_statusvalue;
692 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 693#endif
809a5acc 694 }
79072805 695 break;
696 case '^':
3280af22 697 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805 698 if (s)
699 sv_setpv(sv,s);
700 else {
3280af22 701 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805 702 sv_catpv(sv,"_TOP");
703 }
704 break;
705 case '~':
3280af22 706 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 707 if (!s)
3280af22 708 s = GvENAME(PL_defoutgv);
79072805 709 sv_setpv(sv,s);
710 break;
711#ifndef lint
712 case '=':
3280af22 713 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805 714 break;
715 case '-':
3280af22 716 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805 717 break;
718 case '%':
3280af22 719 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 720 break;
721#endif
722 case ':':
723 break;
724 case '/':
725 break;
726 case '[':
3280af22 727 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805 728 break;
729 case '|':
3280af22 730 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805 731 break;
732 case ',':
79072805 733 break;
734 case '\\':
79072805 735 break;
736 case '#':
3280af22 737 sv_setpv(sv,PL_ofmt);
79072805 738 break;
739 case '!':
a5f75d66 740#ifdef VMS
65202027 741 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 742 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 743#else
88e89b8a 744 {
745 int saveerrno = errno;
65202027 746 sv_setnv(sv, (NV)errno);
88e89b8a 747#ifdef OS2
ed344e4f 748 if (errno == errno_isOS2 || errno == errno_isOS2_set)
749 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 750 else
a5f75d66 751#endif
2304df62 752 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a 753 errno = saveerrno;
754 }
755#endif
946ec16e 756 SvNOK_on(sv); /* what a wonderful hack! */
79072805 757 break;
758 case '<':
3280af22 759 sv_setiv(sv, (IV)PL_uid);
79072805 760 break;
761 case '>':
3280af22 762 sv_setiv(sv, (IV)PL_euid);
79072805 763 break;
764 case '(':
3280af22 765 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 766#ifdef HAS_GETGROUPS
785fb66b 767 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
a52cb5f7 768#endif
79072805 769 goto add_groups;
770 case ')':
3280af22 771 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 772#ifdef HAS_GETGROUPS
785fb66b 773 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
a52cb5f7 774#endif
79072805 775 add_groups:
79072805 776#ifdef HAS_GETGROUPS
79072805 777 {
a0d0e21e 778 Groups_t gary[NGROUPS];
79072805 779 i = getgroups(NGROUPS,gary);
46fc3d4c 780 while (--i >= 0)
785fb66b 781 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
79072805 782 }
783#endif
155aba94 784 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 785 break;
786 case '*':
787 break;
cd39f2b6 788#ifndef MACOS_TRADITIONAL
79072805 789 case '0':
790 break;
cd39f2b6 791#endif
a863c7d1 792#ifdef USE_THREADS
793 case '@':
38a03e6e 794 sv_setsv(sv, thr->errsv);
a863c7d1 795 break;
796#endif /* USE_THREADS */
79072805 797 }
a0d0e21e 798 return 0;
79072805 799}
800
801int
864dbfa3 802Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 803{
804 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
805
806 if (uf && uf->uf_val)
24f81a43 807 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805 808 return 0;
809}
810
811int
864dbfa3 812Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 813{
814 register char *s;
88e89b8a 815 char *ptr;
5aabfad6 816 STRLEN len, klen;
a0d0e21e 817 I32 i;
1e422769 818
a0d0e21e 819 s = SvPV(sv,len);
5aabfad6 820 ptr = MgPV(mg,klen);
88e89b8a 821 my_setenv(ptr, s);
1e422769 822
a0d0e21e 823#ifdef DYNAMIC_ENV_FETCH
824 /* We just undefd an environment var. Is a replacement */
825 /* waiting in the wings? */
826 if (!len) {
5aabfad6 827 SV **valp;
6b88bc9c 828 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 829 s = SvPV(*valp, len);
a0d0e21e 830 }
831#endif
1e422769 832
39e571d4 833#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805 834 /* And you'll never guess what the dog had */
835 /* in its mouth... */
3280af22 836 if (PL_tainting) {
1e422769 837 MgTAINTEDDIR_off(mg);
838#ifdef VMS
5aabfad6 839 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 840 char pathbuf[256], eltbuf[256], *cp, *elt = s;
841 struct stat sbuf;
842 int i = 0, j = 0;
843
844 do { /* DCL$PATH may be a search list */
845 while (1) { /* as may dev portion of any element */
846 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
847 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
848 cando_by_name(S_IWUSR,0,elt) ) {
849 MgTAINTEDDIR_on(mg);
850 return 0;
851 }
852 }
853 if ((cp = strchr(elt, ':')) != Nullch)
854 *cp = '\0';
855 if (my_trnlnm(elt, eltbuf, j++))
856 elt = eltbuf;
857 else
858 break;
859 }
860 j = 0;
861 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
862 }
863#endif /* VMS */
5aabfad6 864 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 865 char *strend = s + len;
463ee0b2 866
867 while (s < strend) {
96827780 868 char tmpbuf[256];
1e422769 869 struct stat st;
96827780 870 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 871 s, strend, ':', &i);
463ee0b2 872 s++;
96827780 873 if (i >= sizeof tmpbuf /* too long -- assume the worst */
874 || *tmpbuf != '/'
c6ed36e1 875 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 876 MgTAINTEDDIR_on(mg);
1e422769 877 return 0;
878 }
463ee0b2 879 }
79072805 880 }
881 }
39e571d4 882#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 883
79072805 884 return 0;
885}
886
887int
864dbfa3 888Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 889{
2d8e6c8d 890 STRLEN n_a;
891 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83 892 return 0;
893}
894
88e89b8a 895int
864dbfa3 896Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 897{
898#if defined(VMS)
cea2e8a9 899 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 900#else
3280af22 901 if (PL_localizing) {
fb73857a 902 HE* entry;
2d8e6c8d 903 STRLEN n_a;
fb73857a 904 magic_clear_all_env(sv,mg);
905 hv_iterinit((HV*)sv);
155aba94 906 while ((entry = hv_iternext((HV*)sv))) {
fb73857a 907 I32 keylen;
908 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 909 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a 910 }
911 }
912#endif
913 return 0;
914}
915
916int
864dbfa3 917Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 918{
ed79a026 919#if defined(VMS) || defined(EPOC)
cea2e8a9 920 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 921#else
7766f137 922# ifdef PERL_IMPLICIT_SYS
923 PerlEnv_clearenv();
924# else
925# ifdef WIN32
3e3baf6d 926 char *envv = GetEnvironmentStrings();
927 char *cur = envv;
928 STRLEN len;
929 while (*cur) {
930 char *end = strchr(cur,'=');
931 if (end && end != cur) {
932 *end = '\0';
933 my_setenv(cur,Nullch);
934 *end = '=';
ac5c734f 935 cur = end + strlen(end+1)+2;
3e3baf6d 936 }
937 else if ((len = strlen(cur)))
938 cur += len+1;
939 }
940 FreeEnvironmentStrings(envv);
7766f137 941# else
5acaa6ec 942#ifdef USE_ENVIRON_ARRAY
7766f137 943# ifndef PERL_USE_SAFE_PUTENV
66b1d557 944 I32 i;
945
3280af22 946 if (environ == PL_origenviron)
f2517201 947 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557 948 else
949 for (i = 0; environ[i]; i++)
f2517201 950 safesysfree(environ[i]);
7766f137 951# endif /* PERL_USE_SAFE_PUTENV */
f2517201 952
66b1d557 953 environ[0] = Nullch;
954
5acaa6ec 955#endif /* USE_ENVIRON_ARRAY */
7766f137 956# endif /* WIN32 */
957# endif /* PERL_IMPLICIT_SYS */
f2517201 958#endif /* VMS */
3e3baf6d 959 return 0;
66b1d557 960}
961
64ca3a65 962#ifndef PERL_MICRO
66b1d557 963int
864dbfa3 964Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 965{
966 I32 i;
2d8e6c8d 967 STRLEN n_a;
88e89b8a 968 /* Are we fetching a signal entry? */
2d8e6c8d 969 i = whichsig(MgPV(mg,n_a));
88e89b8a 970 if (i) {
22c35a8c 971 if(PL_psig_ptr[i])
972 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 973 else {
ff68c719 974 Sighandler_t sigstate = rsignal_state(i);
975
88e89b8a 976 /* cache state so we don't fetch it again */
ff68c719 977 if(sigstate == SIG_IGN)
88e89b8a 978 sv_setpv(sv,"IGNORE");
979 else
3280af22 980 sv_setsv(sv,&PL_sv_undef);
22c35a8c 981 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 982 SvTEMP_off(sv);
983 }
984 }
985 return 0;
986}
987int
864dbfa3 988Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 989{
990 I32 i;
2d8e6c8d 991 STRLEN n_a;
88e89b8a 992 /* Are we clearing a signal entry? */
2d8e6c8d 993 i = whichsig(MgPV(mg,n_a));
88e89b8a 994 if (i) {
22c35a8c 995 if(PL_psig_ptr[i]) {
996 SvREFCNT_dec(PL_psig_ptr[i]);
997 PL_psig_ptr[i]=0;
88e89b8a 998 }
22c35a8c 999 if(PL_psig_name[i]) {
1000 SvREFCNT_dec(PL_psig_name[i]);
1001 PL_psig_name[i]=0;
88e89b8a 1002 }
1003 }
1004 return 0;
1005}
3d37d572 1006
0a8e0eff 1007void
1008Perl_raise_signal(pTHX_ int sig)
1009{
1010 /* Set a flag to say this signal is pending */
1011 PL_psig_pend[sig]++;
1012 /* And one to say _a_ signal is pending */
1013 PL_sig_pending = 1;
1014}
1015
1016Signal_t
1017Perl_csighandler(int sig)
1018{
1019#ifdef PERL_OLD_SIGNALS
1020 /* Call the perl level handler now with risk we may be in malloc() etc. */
1021 (*PL_sighandlerp)(sig);
1022#else
1023 dTHX;
1024 Perl_raise_signal(aTHX_ sig);
1025#endif
1026}
1027
1028void
1029Perl_despatch_signals(pTHX)
1030{
1031 int sig;
1032 PL_sig_pending = 0;
1033 for (sig = 1; sig < SIG_SIZE; sig++) {
1034 if (PL_psig_pend[sig]) {
1035 PL_psig_pend[sig] = 0;
1036 (*PL_sighandlerp)(sig);
1037 }
1038 }
1039}
1040
85e6fe83 1041int
864dbfa3 1042Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1043{
1044 register char *s;
1045 I32 i;
b7953727 1046 SV** svp = 0;
e72dc28c 1047 STRLEN len;
a0d0e21e 1048
e72dc28c 1049 s = MgPV(mg,len);
748a9306 1050 if (*s == '_') {
1051 if (strEQ(s,"__DIE__"))
3280af22 1052 svp = &PL_diehook;
748a9306 1053 else if (strEQ(s,"__WARN__"))
3280af22 1054 svp = &PL_warnhook;
748a9306 1055 else
cea2e8a9 1056 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1057 i = 0;
4633a7c4 1058 if (*svp) {
1059 SvREFCNT_dec(*svp);
1060 *svp = 0;
1061 }
748a9306 1062 }
1063 else {
1064 i = whichsig(s); /* ...no, a brick */
1065 if (!i) {
e476b1b5 1066 if (ckWARN(WARN_SIGNAL))
cea2e8a9 1067 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
748a9306 1068 return 0;
1069 }
22c35a8c 1070 SvREFCNT_dec(PL_psig_name[i]);
1071 SvREFCNT_dec(PL_psig_ptr[i]);
1072 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1073 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1074 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1075 SvREADONLY_on(PL_psig_name[i]);
748a9306 1076 }
a0d0e21e 1077 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 1078 if (i)
0a8e0eff 1079 (void)rsignal(i, &Perl_csighandler);
748a9306 1080 else
1081 *svp = SvREFCNT_inc(sv);
a0d0e21e 1082 return 0;
1083 }
e72dc28c 1084 s = SvPV_force(sv,len);
748a9306 1085 if (strEQ(s,"IGNORE")) {
1086 if (i)
ff68c719 1087 (void)rsignal(i, SIG_IGN);
748a9306 1088 else
1089 *svp = 0;
1090 }
1091 else if (strEQ(s,"DEFAULT") || !*s) {
1092 if (i)
ff68c719 1093 (void)rsignal(i, SIG_DFL);
748a9306 1094 else
1095 *svp = 0;
1096 }
79072805 1097 else {
5aabfad6 1098 /*
1099 * We should warn if HINT_STRICT_REFS, but without
1100 * access to a known hint bit in a known OP, we can't
1101 * tell whether HINT_STRICT_REFS is in force or not.
1102 */
46fc3d4c 1103 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1104 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1105 if (i)
0a8e0eff 1106 (void)rsignal(i, &Perl_csighandler);
748a9306 1107 else
1108 *svp = SvREFCNT_inc(sv);
79072805 1109 }
1110 return 0;
1111}
64ca3a65 1112#endif /* !PERL_MICRO */
79072805 1113
1114int
864dbfa3 1115Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1116{
3280af22 1117 PL_sub_generation++;
463ee0b2 1118 return 0;
1119}
1120
1121int
864dbfa3 1122Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1123{
a0d0e21e 1124 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1125 PL_amagic_generation++;
463ee0b2 1126
a0d0e21e 1127 return 0;
1128}
463ee0b2 1129
946ec16e 1130int
864dbfa3 1131Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1132{
1133 HV *hv = (HV*)LvTARG(sv);
1134 HE *entry;
1135 I32 i = 0;
1136
1137 if (hv) {
1138 (void) hv_iterinit(hv);
14befaf4 1139 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
6ff81951 1140 i = HvKEYS(hv);
1141 else {
1142 /*SUPPRESS 560*/
155aba94 1143 while ((entry = hv_iternext(hv))) {
6ff81951 1144 i++;
1145 }
1146 }
1147 }
1148
1149 sv_setiv(sv, (IV)i);
1150 return 0;
1151}
1152
1153int
864dbfa3 1154Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1155{
1156 if (LvTARG(sv)) {
1157 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e 1158 }
1159 return 0;
ac27b0f5 1160}
946ec16e 1161
e336de0d 1162/* caller is responsible for stack switching/cleanup */
565764a8 1163STATIC int
cea2e8a9 1164S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e 1165{
1166 dSP;
463ee0b2 1167
924508f0 1168 PUSHMARK(SP);
1169 EXTEND(SP, n);
33c27489 1170 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1171 if (n > 1) {
93965878 1172 if (mg->mg_ptr) {
565764a8 1173 if (mg->mg_len >= 0)
79cb57f6 1174 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1175 else if (mg->mg_len == HEf_SVKEY)
93965878 1176 PUSHs((SV*)mg->mg_ptr);
1177 }
14befaf4 1178 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1179 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878 1180 }
1181 }
1182 if (n > 2) {
1183 PUSHs(val);
88e89b8a 1184 }
463ee0b2 1185 PUTBACK;
1186
864dbfa3 1187 return call_method(meth, flags);
946ec16e 1188}
1189
76e3520e 1190STATIC int
cea2e8a9 1191S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e 1192{
1193 dSP;
463ee0b2 1194
a0d0e21e 1195 ENTER;
1196 SAVETMPS;
e788e7d3 1197 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1198
33c27489 1199 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1200 sv_setsv(sv, *PL_stack_sp--);
93965878 1201 }
463ee0b2 1202
d3acc0f7 1203 POPSTACK;
a0d0e21e 1204 FREETMPS;
1205 LEAVE;
1206 return 0;
1207}
463ee0b2 1208
a0d0e21e 1209int
864dbfa3 1210Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1211{
1212 magic_methpack(sv,mg,"FETCH");
1213 if (mg->mg_ptr)
1214 mg->mg_flags |= MGf_GSKIP;
463ee0b2 1215 return 0;
1216}
1217
1218int
864dbfa3 1219Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1220{
1221 dSP;
a60c0954 1222 ENTER;
e788e7d3 1223 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1224 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1225 POPSTACK;
a60c0954 1226 LEAVE;
463ee0b2 1227 return 0;
1228}
1229
1230int
864dbfa3 1231Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1232{
a0d0e21e 1233 return magic_methpack(sv,mg,"DELETE");
1234}
463ee0b2 1235
93965878 1236
1237U32
864dbfa3 1238Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1239{
e336de0d 1240 dSP;
93965878 1241 U32 retval = 0;
1242
1243 ENTER;
1244 SAVETMPS;
e788e7d3 1245 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1246 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1247 sv = *PL_stack_sp--;
a60c0954 1248 retval = (U32) SvIV(sv)-1;
93965878 1249 }
d3acc0f7 1250 POPSTACK;
93965878 1251 FREETMPS;
1252 LEAVE;
1253 return retval;
1254}
1255
cea2e8a9 1256int
1257Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1258{
1259 dSP;
463ee0b2 1260
e336de0d 1261 ENTER;
e788e7d3 1262 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1263 PUSHMARK(SP);
33c27489 1264 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1265 PUTBACK;
864dbfa3 1266 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1267 POPSTACK;
a60c0954 1268 LEAVE;
463ee0b2 1269 return 0;
1270}
1271
1272int
864dbfa3 1273Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1274{
463ee0b2 1275 dSP;
dff6d3cd 1276 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2 1277
1278 ENTER;
a0d0e21e 1279 SAVETMPS;
e788e7d3 1280 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1281 PUSHMARK(SP);
1282 EXTEND(SP, 2);
33c27489 1283 PUSHs(SvTIED_obj(sv, mg));
463ee0b2 1284 if (SvOK(key))
1285 PUSHs(key);
1286 PUTBACK;
1287
864dbfa3 1288 if (call_method(meth, G_SCALAR))
3280af22 1289 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1290
d3acc0f7 1291 POPSTACK;
a0d0e21e 1292 FREETMPS;
1293 LEAVE;
79072805 1294 return 0;
1295}
1296
1297int
864dbfa3 1298Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1299{
1300 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1301}
a0d0e21e 1302
1303int
864dbfa3 1304Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1305{
1306 OP *o;
1307 I32 i;
1308 GV* gv;
1309 SV** svp;
2d8e6c8d 1310 STRLEN n_a;
79072805 1311
3280af22 1312 gv = PL_DBline;
79072805 1313 i = SvTRUE(sv);
188ea221 1314 svp = av_fetch(GvAV(gv),
2d8e6c8d 1315 atoi(MgPV(mg,n_a)), FALSE);
57b2e452 1316 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
93a17b20 1317 o->op_private = i;
79072805 1318 return 0;
1319}
1320
1321int
864dbfa3 1322Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1323{
3280af22 1324 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805 1325 return 0;
1326}
1327
1328int
864dbfa3 1329Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1330{
3280af22 1331 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e 1332 return 0;
1333}
1334
1335int
864dbfa3 1336Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1337{
1338 SV* lsv = LvTARG(sv);
ac27b0f5 1339
a0d0e21e 1340 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1341 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1342 if (mg && mg->mg_len >= 0) {
a0ed51b3 1343 I32 i = mg->mg_len;
7e2040f0 1344 if (DO_UTF8(lsv))
a0ed51b3 1345 sv_pos_b2u(lsv, &i);
1346 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e 1347 return 0;
1348 }
1349 }
1350 (void)SvOK_off(sv);
1351 return 0;
1352}
1353
1354int
864dbfa3 1355Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1356{
1357 SV* lsv = LvTARG(sv);
1358 SSize_t pos;
1359 STRLEN len;
c00206c8 1360 STRLEN ulen = 0;
a0d0e21e 1361
1362 mg = 0;
ac27b0f5 1363
a0d0e21e 1364 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1365 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e 1366 if (!mg) {
1367 if (!SvOK(sv))
1368 return 0;
14befaf4 1369 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1370 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e 1371 }
1372 else if (!SvOK(sv)) {
565764a8 1373 mg->mg_len = -1;
a0d0e21e 1374 return 0;
1375 }
1376 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1377
c485e607 1378 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1379
7e2040f0 1380 if (DO_UTF8(lsv)) {
a0ed51b3 1381 ulen = sv_len_utf8(lsv);
1382 if (ulen)
1383 len = ulen;
a0ed51b3 1384 }
1385
a0d0e21e 1386 if (pos < 0) {
1387 pos += len;
1388 if (pos < 0)
1389 pos = 0;
1390 }
1391 else if (pos > len)
1392 pos = len;
a0ed51b3 1393
1394 if (ulen) {
1395 I32 p = pos;
1396 sv_pos_u2b(lsv, &p, 0);
1397 pos = p;
1398 }
1399
565764a8 1400 mg->mg_len = pos;
71be2cbc 1401 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1402
79072805 1403 return 0;
1404}
1405
1406int
864dbfa3 1407Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1408{
8646b087 1409 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1410 SvFAKE_off(sv);
946ec16e 1411 gv_efullname3(sv,((GV*)sv), "*");
8646b087 1412 SvFAKE_on(sv);
1413 }
1414 else
946ec16e 1415 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805 1416 return 0;
1417}
1418
1419int
864dbfa3 1420Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1421{
1422 register char *s;
1423 GV* gv;
2d8e6c8d 1424 STRLEN n_a;
79072805 1425
1426 if (!SvOK(sv))
1427 return 0;
2d8e6c8d 1428 s = SvPV(sv, n_a);
79072805 1429 if (*s == '*' && s[1])
1430 s++;
85e6fe83 1431 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805 1432 if (sv == (SV*)gv)
1433 return 0;
1434 if (GvGP(sv))
88e89b8a 1435 gp_free((GV*)sv);
79072805 1436 GvGP(sv) = gp_ref(GvGP(gv));
79072805 1437 return 0;
1438}
1439
1440int
864dbfa3 1441Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1442{
1443 STRLEN len;
1444 SV *lsv = LvTARG(sv);
1445 char *tmps = SvPV(lsv,len);
1446 I32 offs = LvTARGOFF(sv);
1447 I32 rem = LvTARGLEN(sv);
1448
9aa983d2 1449 if (SvUTF8(lsv))
1450 sv_pos_u2b(lsv, &offs, &rem);
6ff81951 1451 if (offs > len)
1452 offs = len;
1453 if (rem + offs > len)
1454 rem = len - offs;
1455 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1456 if (SvUTF8(lsv))
2ef4b674 1457 SvUTF8_on(sv);
6ff81951 1458 return 0;
1459}
1460
1461int
864dbfa3 1462Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1463{
9aa983d2 1464 STRLEN len;
1465 char *tmps = SvPV(sv, len);
1466 SV *lsv = LvTARG(sv);
1467 I32 lvoff = LvTARGOFF(sv);
1468 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1469
1aa99e6b 1470 if (DO_UTF8(sv)) {
9aa983d2 1471 sv_utf8_upgrade(lsv);
1472 sv_pos_u2b(lsv, &lvoff, &lvlen);
1473 sv_insert(lsv, lvoff, lvlen, tmps, len);
1474 SvUTF8_on(lsv);
1475 }
1476 else if (SvUTF8(lsv)) {
1477 sv_pos_u2b(lsv, &lvoff, &lvlen);
e95af362 1478 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2 1479 sv_insert(lsv, lvoff, lvlen, tmps, len);
1480 Safefree(tmps);
1aa99e6b 1481 }
1482 else
9aa983d2 1483 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1484
79072805 1485 return 0;
1486}
1487
1488int
864dbfa3 1489Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1490{
565764a8 1491 TAINT_IF((mg->mg_len & 1) ||
155aba94 1492 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2 1493 return 0;
1494}
1495
1496int
864dbfa3 1497Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1498{
3280af22 1499 if (PL_localizing) {
1500 if (PL_localizing == 1)
565764a8 1501 mg->mg_len <<= 1;
748a9306 1502 else
565764a8 1503 mg->mg_len >>= 1;
a0d0e21e 1504 }
3280af22 1505 else if (PL_tainted)
565764a8 1506 mg->mg_len |= 1;
748a9306 1507 else
565764a8 1508 mg->mg_len &= ~1;
463ee0b2 1509 return 0;
1510}
1511
1512int
864dbfa3 1513Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1514{
1515 SV *lsv = LvTARG(sv);
6ff81951 1516
1517 if (!lsv) {
155aba94 1518 (void)SvOK_off(sv);
6ff81951 1519 return 0;
1520 }
6ff81951 1521
81e118e0 1522 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951 1523 return 0;
1524}
1525
1526int
864dbfa3 1527Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 1528{
1529 do_vecset(sv); /* XXX slurp this routine */
1530 return 0;
1531}
1532
1533int
864dbfa3 1534Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1535{
71be2cbc 1536 SV *targ = Nullsv;
5f05dabc 1537 if (LvTARGLEN(sv)) {
68dc0745 1538 if (mg->mg_obj) {
74e13ce4 1539 SV *ahv = LvTARG(sv);
1540 if (SvTYPE(ahv) == SVt_PVHV) {
1541 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1542 if (he)
1543 targ = HeVAL(he);
1544 }
1545 else {
1546 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1547 if (svp)
1548 targ = *svp;
1549 }
68dc0745 1550 }
1551 else {
3c78fafa 1552 AV* av = (AV*)LvTARG(sv);
68dc0745 1553 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1554 targ = AvARRAY(av)[LvTARGOFF(sv)];
1555 }
3280af22 1556 if (targ && targ != &PL_sv_undef) {
68dc0745 1557 /* somebody else defined it for us */
1558 SvREFCNT_dec(LvTARG(sv));
1559 LvTARG(sv) = SvREFCNT_inc(targ);
1560 LvTARGLEN(sv) = 0;
1561 SvREFCNT_dec(mg->mg_obj);
1562 mg->mg_obj = Nullsv;
1563 mg->mg_flags &= ~MGf_REFCOUNTED;
1564 }
5f05dabc 1565 }
71be2cbc 1566 else
1567 targ = LvTARG(sv);
3280af22 1568 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc 1569 return 0;
1570}
1571
1572int
864dbfa3 1573Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 1574{
1575 if (LvTARGLEN(sv))
68dc0745 1576 vivify_defelem(sv);
1577 if (LvTARG(sv)) {
5f05dabc 1578 sv_setsv(LvTARG(sv), sv);
68dc0745 1579 SvSETMAGIC(LvTARG(sv));
1580 }
5f05dabc 1581 return 0;
1582}
1583
71be2cbc 1584void
864dbfa3 1585Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1586{
74e13ce4 1587 MAGIC *mg;
1588 SV *value = Nullsv;
71be2cbc 1589
14befaf4 1590 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1591 return;
68dc0745 1592 if (mg->mg_obj) {
74e13ce4 1593 SV *ahv = LvTARG(sv);
2d8e6c8d 1594 STRLEN n_a;
74e13ce4 1595 if (SvTYPE(ahv) == SVt_PVHV) {
af7f9c15 1596 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4 1597 if (he)
1598 value = HeVAL(he);
1599 }
1600 else {
af7f9c15 1601 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4 1602 if (svp)
1603 value = *svp;
1604 }
3280af22 1605 if (!value || value == &PL_sv_undef)
cea2e8a9 1606 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1607 }
68dc0745 1608 else {
1609 AV* av = (AV*)LvTARG(sv);
5aabfad6 1610 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745 1611 LvTARG(sv) = Nullsv; /* array can't be extended */
1612 else {
1613 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1614 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1615 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745 1616 }
1617 }
3e3baf6d 1618 (void)SvREFCNT_inc(value);
68dc0745 1619 SvREFCNT_dec(LvTARG(sv));
1620 LvTARG(sv) = value;
71be2cbc 1621 LvTARGLEN(sv) = 0;
68dc0745 1622 SvREFCNT_dec(mg->mg_obj);
1623 mg->mg_obj = Nullsv;
1624 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc 1625}
1626
1627int
864dbfa3 1628Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 1629{
1630 AV *av = (AV*)mg->mg_obj;
1631 SV **svp = AvARRAY(av);
1632 I32 i = AvFILLp(av);
1633 while (i >= 0) {
1634 if (svp[i] && svp[i] != &PL_sv_undef) {
1635 if (!SvWEAKREF(svp[i]))
cea2e8a9 1636 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5 1637 /* XXX Should we check that it hasn't changed? */
1638 SvRV(svp[i]) = 0;
155aba94 1639 (void)SvOK_off(svp[i]);
810b8aa5 1640 SvWEAKREF_off(svp[i]);
1641 svp[i] = &PL_sv_undef;
1642 }
1643 i--;
1644 }
1645 return 0;
1646}
1647
1648int
864dbfa3 1649Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1650{
565764a8 1651 mg->mg_len = -1;
c6496cc7 1652 SvSCREAM_off(sv);
93a17b20 1653 return 0;
1654}
1655
1656int
864dbfa3 1657Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1658{
14befaf4 1659 sv_unmagic(sv, PERL_MAGIC_bm);
79072805 1660 SvVALID_off(sv);
1661 return 0;
1662}
1663
1664int
864dbfa3 1665Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1666{
14befaf4 1667 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff 1668 SvCOMPILED_off(sv);
1669 return 0;
1670}
1671
1672int
864dbfa3 1673Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1674{
1675 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1676
1677 if (uf && uf->uf_set)
24f81a43 1678 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805 1679 return 0;
1680}
1681
c277df42 1682int
864dbfa3 1683Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42 1684{
1685 regexp *re = (regexp *)mg->mg_obj;
1686 ReREFCNT_dec(re);
1687 return 0;
1688}
1689
7a4c00b4 1690#ifdef USE_LOCALE_COLLATE
79072805 1691int
864dbfa3 1692Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69 1693{
1694 /*
838b5b74 1695 * RenE<eacute> Descartes said "I think not."
bbce6d69 1696 * and vanished with a faint plop.
1697 */
7a4c00b4 1698 if (mg->mg_ptr) {
1699 Safefree(mg->mg_ptr);
1700 mg->mg_ptr = NULL;
565764a8 1701 mg->mg_len = -1;
7a4c00b4 1702 }
bbce6d69 1703 return 0;
1704}
7a4c00b4 1705#endif /* USE_LOCALE_COLLATE */
bbce6d69 1706
1707int
864dbfa3 1708Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 1709{
1710 register char *s;
1711 I32 i;
8990e307 1712 STRLEN len;
79072805 1713 switch (*mg->mg_ptr) {
748a9306 1714 case '\001': /* ^A */
3280af22 1715 sv_setsv(PL_bodytarget, sv);
748a9306 1716 break;
49460fe6 1717 case '\003': /* ^C */
1718 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1719 break;
1720
79072805 1721 case '\004': /* ^D */
aea4f609 1722 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
79072805 1723 DEBUG_x(dump_all());
1724 break;
28f23441 1725 case '\005': /* ^E */
cd39f2b6 1726#ifdef MACOS_TRADITIONAL
bf4acbe4 1727 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1728#else
cd39f2b6 1729# ifdef VMS
1730 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1731# else
cd39f2b6 1732# ifdef WIN32
1733 SetLastError( SvIV(sv) );
1734# else
1735# ifndef OS2
f86702cc 1736 /* will anyone ever use this? */
1737 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 1738# endif
048c1ddf 1739# endif
1740# endif
22fae026 1741#endif
28f23441 1742 break;
79072805 1743 case '\006': /* ^F */
3280af22 1744 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1745 break;
a0d0e21e 1746 case '\010': /* ^H */
3280af22 1747 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 1748 break;
9d116dd7 1749 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22 1750 if (PL_inplace)
1751 Safefree(PL_inplace);
79072805 1752 if (SvOK(sv))
2d8e6c8d 1753 PL_inplace = savepv(SvPV(sv,len));
79072805 1754 else
3280af22 1755 PL_inplace = Nullch;
79072805 1756 break;
28f23441 1757 case '\017': /* ^O */
ac27b0f5 1758 if (*(mg->mg_ptr+1) == '\0') {
1759 if (PL_osname)
1760 Safefree(PL_osname);
1761 if (SvOK(sv))
1762 PL_osname = savepv(SvPV(sv,len));
1763 else
1764 PL_osname = Nullch;
1765 }
1766 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1767 if (!PL_compiling.cop_io)
1768 PL_compiling.cop_io = newSVsv(sv);
1769 else
1770 sv_setsv(PL_compiling.cop_io,sv);
1771 }
28f23441 1772 break;
79072805 1773 case '\020': /* ^P */
3280af22 1774 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e 1775 if (PL_perldb && !PL_DBsingle)
1776 init_debugger();
79072805 1777 break;
1778 case '\024': /* ^T */
88e89b8a 1779#ifdef BIG_TIME
6b88bc9c 1780 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 1781#else
3280af22 1782 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1783#endif
79072805 1784 break;
46487f74 1785 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
4438c4b7 1786 if (*(mg->mg_ptr+1) == '\0') {
1787 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1788 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 1789 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 1790 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 1791 }
599cee73 1792 }
6a818117 1793 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
4438c4b7 1794 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e 1795 if (!SvPOK(sv) && PL_localizing) {
1796 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 1797 PL_compiling.cop_warnings = pWARN_NONE;
d775746e 1798 break;
1799 }
f4fc7782 1800 {
b5477537 1801 STRLEN len, i;
d3a7d8c7 1802 int accumulate = 0 ;
f4fc7782 1803 int any_fatals = 0 ;
d3a7d8c7 1804 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782 1805 for (i = 0 ; i < len ; ++i) {
1806 accumulate |= ptr[i] ;
1807 any_fatals |= (ptr[i] & 0xAA) ;
1808 }
d3a7d8c7 1809 if (!accumulate)
1810 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782 1811 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1812 PL_compiling.cop_warnings = pWARN_ALL;
1813 PL_dowarn |= G_WARN_ONCE ;
1814 }
d3a7d8c7 1815 else {
1816 if (specialWARN(PL_compiling.cop_warnings))
1817 PL_compiling.cop_warnings = newSVsv(sv) ;
1818 else
1819 sv_setsv(PL_compiling.cop_warnings, sv);
1820 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1821 PL_dowarn |= G_WARN_ONCE ;
1822 }
f4fc7782 1823
d3a7d8c7 1824 }
4438c4b7 1825 }
971a9dd3 1826 }
46487f74 1827 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1828 PL_widesyscalls = SvTRUE(sv);
79072805 1829 break;
1830 case '.':
3280af22 1831 if (PL_localizing) {
1832 if (PL_localizing == 1)
7766f137 1833 SAVESPTR(PL_last_in_gv);
748a9306 1834 }
3280af22 1835 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1836 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
79072805 1837 break;
1838 case '^':
3280af22 1839 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1840 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1841 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 1842 break;
1843 case '~':
3280af22 1844 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1845 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1846 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 1847 break;
1848 case '=':
3280af22 1849 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1850 break;
1851 case '-':
3280af22 1852 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1853 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1854 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805 1855 break;
1856 case '%':
3280af22 1857 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1858 break;
1859 case '|':
4b65379b 1860 {
3280af22 1861 IO *io = GvIOp(PL_defoutgv);
4b65379b 1862 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1863 IoFLAGS(io) &= ~IOf_FLUSH;
1864 else {
1865 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1866 PerlIO *ofp = IoOFP(io);
1867 if (ofp)
1868 (void)PerlIO_flush(ofp);
1869 IoFLAGS(io) |= IOf_FLUSH;
1870 }
1871 }
79072805 1872 }
1873 break;
1874 case '*':
463ee0b2 1875 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
3280af22 1876 PL_multiline = (i != 0);
79072805 1877 break;
1878 case '/':
3280af22 1879 SvREFCNT_dec(PL_nrs);
1880 PL_nrs = newSVsv(sv);
1881 SvREFCNT_dec(PL_rs);
1882 PL_rs = SvREFCNT_inc(PL_nrs);
79072805 1883 break;
1884 case '\\':
7889fe52 1885 if (PL_ors_sv)
1886 SvREFCNT_dec(PL_ors_sv);
009c130f 1887 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 1888 PL_ors_sv = newSVsv(sv);
009c130f 1889 }
e3c19b7b 1890 else {
7889fe52 1891 PL_ors_sv = Nullsv;
e3c19b7b 1892 }
79072805 1893 break;
1894 case ',':
7889fe52 1895 if (PL_ofs_sv)
1896 SvREFCNT_dec(PL_ofs_sv);
1897 if (SvOK(sv) || SvGMAGICAL(sv)) {
1898 PL_ofs_sv = newSVsv(sv);
1899 }
1900 else {
1901 PL_ofs_sv = Nullsv;
1902 }
79072805 1903 break;
1904 case '#':
3280af22 1905 if (PL_ofmt)
1906 Safefree(PL_ofmt);
2d8e6c8d 1907 PL_ofmt = savepv(SvPV(sv,len));
79072805 1908 break;
1909 case '[':
3280af22 1910 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1911 break;
1912 case '?':
ff0cee69 1913#ifdef COMPLEX_STATUS
6b88bc9c 1914 if (PL_localizing == 2) {
1915 PL_statusvalue = LvTARGOFF(sv);
1916 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69 1917 }
1918 else
1919#endif
1920#ifdef VMSISH_STATUS
1921 if (VMSISH_STATUS)
1922 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1923 else
1924#endif
1925 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1926 break;
1927 case '!':
78987ded 1928 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1929 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805 1930 break;
1931 case '<':
3280af22 1932 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1933 if (PL_delaymagic) {
1934 PL_delaymagic |= DM_RUID;
79072805 1935 break; /* don't do magic till later */
1936 }
1937#ifdef HAS_SETRUID
b28d0864 1938 (void)setruid((Uid_t)PL_uid);
79072805 1939#else
1940#ifdef HAS_SETREUID
3280af22 1941 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 1942#else
85e6fe83 1943#ifdef HAS_SETRESUID
b28d0864 1944 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 1945#else
b28d0864 1946 if (PL_uid == PL_euid) /* special case $< = $> */
1947 (void)PerlProc_setuid(PL_uid);
a0d0e21e 1948 else {
d8eceb89 1949 PL_uid = PerlProc_getuid();
cea2e8a9 1950 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 1951 }
79072805 1952#endif
1953#endif
85e6fe83 1954#endif
d8eceb89 1955 PL_uid = PerlProc_getuid();
3280af22 1956 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1957 break;
1958 case '>':
3280af22 1959 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1960 if (PL_delaymagic) {
1961 PL_delaymagic |= DM_EUID;
79072805 1962 break; /* don't do magic till later */
1963 }
1964#ifdef HAS_SETEUID
3280af22 1965 (void)seteuid((Uid_t)PL_euid);
79072805 1966#else
1967#ifdef HAS_SETREUID
b28d0864 1968 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83 1969#else
1970#ifdef HAS_SETRESUID
6b88bc9c 1971 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 1972#else
b28d0864 1973 if (PL_euid == PL_uid) /* special case $> = $< */
1974 PerlProc_setuid(PL_euid);
a0d0e21e 1975 else {
e8ee3774 1976 PL_euid = PerlProc_geteuid();
cea2e8a9 1977 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 1978 }
79072805 1979#endif
1980#endif
85e6fe83 1981#endif
d8eceb89 1982 PL_euid = PerlProc_geteuid();
3280af22 1983 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1984 break;
1985 case '(':
3280af22 1986 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1987 if (PL_delaymagic) {
1988 PL_delaymagic |= DM_RGID;
79072805 1989 break; /* don't do magic till later */
1990 }
1991#ifdef HAS_SETRGID
b28d0864 1992 (void)setrgid((Gid_t)PL_gid);
79072805 1993#else
1994#ifdef HAS_SETREGID
3280af22 1995 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83 1996#else
1997#ifdef HAS_SETRESGID
b28d0864 1998 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 1999#else
b28d0864 2000 if (PL_gid == PL_egid) /* special case $( = $) */
2001 (void)PerlProc_setgid(PL_gid);
748a9306 2002 else {
d8eceb89 2003 PL_gid = PerlProc_getgid();
cea2e8a9 2004 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2005 }
79072805 2006#endif
2007#endif
85e6fe83 2008#endif
d8eceb89 2009 PL_gid = PerlProc_getgid();
3280af22 2010 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2011 break;
2012 case ')':
5cd24f17 2013#ifdef HAS_SETGROUPS
2014 {
2d8e6c8d 2015 char *p = SvPV(sv, len);
5cd24f17 2016 Groups_t gary[NGROUPS];
2017
5cd24f17 2018 while (isSPACE(*p))
2019 ++p;
2d4389e4 2020 PL_egid = Atol(p);
5cd24f17 2021 for (i = 0; i < NGROUPS; ++i) {
2022 while (*p && !isSPACE(*p))
2023 ++p;
2024 while (isSPACE(*p))
2025 ++p;
2026 if (!*p)
2027 break;
2d4389e4 2028 gary[i] = Atol(p);
5cd24f17 2029 }
8cc95fdb 2030 if (i)
2031 (void)setgroups(i, gary);
5cd24f17 2032 }
2033#else /* HAS_SETGROUPS */
b28d0864 2034 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2035#endif /* HAS_SETGROUPS */
3280af22 2036 if (PL_delaymagic) {
2037 PL_delaymagic |= DM_EGID;
79072805 2038 break; /* don't do magic till later */
2039 }
2040#ifdef HAS_SETEGID
3280af22 2041 (void)setegid((Gid_t)PL_egid);
79072805 2042#else
2043#ifdef HAS_SETREGID
b28d0864 2044 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83 2045#else
2046#ifdef HAS_SETRESGID
b28d0864 2047 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2048#else
b28d0864 2049 if (PL_egid == PL_gid) /* special case $) = $( */
2050 (void)PerlProc_setgid(PL_egid);
748a9306 2051 else {
d8eceb89 2052 PL_egid = PerlProc_getegid();
cea2e8a9 2053 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2054 }
79072805 2055#endif
2056#endif
85e6fe83 2057#endif
d8eceb89 2058 PL_egid = PerlProc_getegid();
3280af22 2059 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2060 break;
2061 case ':':
2d8e6c8d 2062 PL_chopset = SvPV_force(sv,len);
79072805 2063 break;
cd39f2b6 2064#ifndef MACOS_TRADITIONAL
79072805 2065 case '0':
4bc88a62 2066#ifdef HAS_SETPROCTITLE
2067 /* The BSDs don't show the argv[] in ps(1) output, they
2068 * show a string from the process struct and provide
2069 * the setproctitle() routine to manipulate that. */
2070 {
2071 s = SvPV(sv, len);
9aad2c0e 2072# if __FreeBSD_version >= 410001
2073 /* The leading "-" removes the "perl: " prefix,
4bc88a62 2074 * but not the "(perl) suffix from the ps(1)
2075 * output, because that's what ps(1) shows if the
2076 * argv[] is modified. */
2077 setproctitle("-%s", s, len + 1);
9aad2c0e 2078# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62 2079 /* This doesn't really work if you assume that
2080 * $0 = 'foobar'; will wipe out 'perl' from the $0
2081 * because in ps(1) output the result will be like
2082 * sprintf("perl: %s (perl)", s)
2083 * I guess this is a security feature:
2084 * one (a user process) cannot get rid of the original name.
2085 * --jhi */
2086 setproctitle("%s", s);
2087# endif
2088 }
2089#endif
3280af22 2090 if (!PL_origalen) {
2091 s = PL_origargv[0];
79072805 2092 s += strlen(s);
2093 /* See if all the arguments are contiguous in memory */
3280af22 2094 for (i = 1; i < PL_origargc; i++) {
2095 if (PL_origargv[i] == s + 1
fb73857a 2096#ifdef OS2
6b88bc9c 2097 || PL_origargv[i] == s + 2
ac27b0f5 2098#endif
fb73857a 2099 )
379c4362 2100 {
2101 ++s;
2102 s += strlen(s); /* this one is ok too */
2103 }
fb73857a 2104 else
2105 break;
79072805 2106 }
bbce6d69 2107 /* can grab env area too? */
3280af22 2108 if (PL_origenviron && (PL_origenviron[0] == s + 1
fb73857a 2109#ifdef OS2
6b88bc9c 2110 || (PL_origenviron[0] == s + 9 && (s += 8))
ac27b0f5 2111#endif
fb73857a 2112 )) {
66b1d557 2113 my_setenv("NoNe SuCh", Nullch);
79072805 2114 /* force copy of environment */
3280af22 2115 for (i = 0; PL_origenviron[i]; i++)
379c4362 2116 if (PL_origenviron[i] == s + 1) {
2117 ++s;
2118 s += strlen(s);
2119 }
fb73857a 2120 else
2121 break;
79072805 2122 }
3280af22 2123 PL_origalen = s - PL_origargv[0];
79072805 2124 }
a0d0e21e 2125 s = SvPV_force(sv,len);
8990e307 2126 i = len;
3280af22 2127 if (i >= PL_origalen) {
2128 i = PL_origalen;
fb73857a 2129 /* don't allow system to limit $0 seen by script */
2130 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
3280af22 2131 Copy(s, PL_origargv[0], i, char);
2132 s = PL_origargv[0]+i;
fb73857a 2133 *s = '\0';
79072805 2134 }
2135 else {
3280af22 2136 Copy(s, PL_origargv[0], i, char);
2137 s = PL_origargv[0]+i;
79072805 2138 *s++ = '\0';
3280af22 2139 while (++i < PL_origalen)
8990e307 2140 *s++ = ' ';
3280af22 2141 s = PL_origargv[0]+i;
2142 for (i = 1; i < PL_origargc; i++)
2143 PL_origargv[i] = Nullch;
79072805 2144 }
2145 break;
cd39f2b6 2146#endif
a863c7d1 2147#ifdef USE_THREADS
2148 case '@':
38a03e6e 2149 sv_setsv(thr->errsv, sv);
a863c7d1 2150 break;
2151#endif /* USE_THREADS */
79072805 2152 }
2153 return 0;
2154}
2155
f93b4edd 2156#ifdef USE_THREADS
2157int
864dbfa3 2158Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
f93b4edd 2159{
b900a521 2160 DEBUG_S(PerlIO_printf(Perl_debug_log,
2161 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2162 PTR2UV(thr), PTR2UV(sv));)
f93b4edd 2163 if (MgOWNER(mg))
cea2e8a9 2164 Perl_croak(aTHX_ "panic: magic_mutexfree");
f93b4edd 2165 MUTEX_DESTROY(MgMUTEXP(mg));
2166 COND_DESTROY(MgCONDP(mg));
2167 return 0;
2168}
2169#endif /* USE_THREADS */
2170
79072805 2171I32
864dbfa3 2172Perl_whichsig(pTHX_ char *sig)
79072805 2173{
2174 register char **sigv;
2175
22c35a8c 2176 for (sigv = PL_sig_name+1; *sigv; sigv++)
79072805 2177 if (strEQ(sig,*sigv))
22c35a8c 2178 return PL_sig_num[sigv - PL_sig_name];
79072805 2179#ifdef SIGCLD
2180 if (strEQ(sig,"CHLD"))
2181 return SIGCLD;
2182#endif
2183#ifdef SIGCHLD
2184 if (strEQ(sig,"CLD"))
2185 return SIGCHLD;
2186#endif
2187 return 0;
2188}
2189
84902520 2190static SV* sig_sv;
2191
ecfc5424 2192Signal_t
cea2e8a9 2193Perl_sighandler(int sig)
79072805 2194{
71d280e3 2195#if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2196 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2197#else
cea2e8a9 2198 dTHX;
71d280e3 2199#endif
79072805 2200 dSP;
00d579c5 2201 GV *gv = Nullgv;
a0d0e21e 2202 HV *st;
b7953727 2203 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2204 CV *cv = Nullcv;
533c011a 2205 OP *myop = PL_op;
84902520 2206 U32 flags = 0;
155aba94 2207 I32 o_save_i = PL_savestack_ix;
3280af22 2208 XPV *tXpv = PL_Xpv;
71d280e3 2209
2210#if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2211 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2212#endif
ac27b0f5 2213
3280af22 2214 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2215 flags |= 1;
3280af22 2216 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2217 flags |= 4;
3280af22 2218 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2219 flags |= 8;
3280af22 2220 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520 2221 flags |= 16;
2222
22c35a8c 2223 if (!PL_psig_ptr[sig])
cea2e8a9 2224 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
22c35a8c 2225 PL_sig_name[sig]);
ff0cee69 2226
84902520 2227 /* Max number of items pushed there is 3*n or 4. We cannot fix
2228 infinity, so we fix 4 (in fact 5): */
2229 if (flags & 1) {
3280af22 2230 PL_savestack_ix += 5; /* Protect save in progress. */
2231 o_save_i = PL_savestack_ix;
c76ac1ee 2232 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2233 }
ac27b0f5 2234 if (flags & 4)
3280af22 2235 PL_markstack_ptr++; /* Protect mark. */
84902520 2236 if (flags & 8) {
3280af22 2237 PL_retstack_ix++;
2238 PL_retstack[PL_retstack_ix] = NULL;
84902520 2239 }
2240 if (flags & 16)
3280af22 2241 PL_scopestack_ix += 1;
84902520 2242 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2243 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2244 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2245 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2246
a0d0e21e 2247 if (!cv || !CvROOT(cv)) {
599cee73 2248 if (ckWARN(WARN_SIGNAL))
cea2e8a9 2249 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2250 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5 2251 : ((cv && CvGV(cv))
2252 ? GvENAME(CvGV(cv))
2253 : "__ANON__")));
2254 goto cleanup;
79072805 2255 }
2256
22c35a8c 2257 if(PL_psig_name[sig]) {
2258 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2259 flags |= 64;
2260 sig_sv = sv;
2261 } else {
ff0cee69 2262 sv = sv_newmortal();
22c35a8c 2263 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2264 }
e336de0d 2265
e788e7d3 2266 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2267 PUSHMARK(SP);
79072805 2268 PUSHs(sv);
79072805 2269 PUTBACK;
a0d0e21e 2270
1b266415 2271 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2272
d3acc0f7 2273 POPSTACK;
1b266415 2274 if (SvTRUE(ERRSV)) {
1d615522 2275#ifndef PERL_MICRO
983dbef6 2276#ifdef HAS_SIGPROCMASK
1b266415 2277 /* Handler "died", for example to get out of a restart-able read().
2278 * Before we re-do that on its behalf re-enable the signal which was
2279 * blocked by the system when we entered.
2280 */
2281 sigset_t set;
2282 sigemptyset(&set);
2283 sigaddset(&set,sig);
2284 sigprocmask(SIG_UNBLOCK, &set, NULL);
2285#else
2286 /* Not clear if this will work */
2287 (void)rsignal(sig, SIG_IGN);
0a8e0eff 2288 (void)rsignal(sig, &Perl_csighandler);
1b266415 2289#endif
1d615522 2290#endif /* !PERL_MICRO */
1b266415 2291 Perl_die(aTHX_ Nullch);
2292 }
00d579c5 2293cleanup:
84902520 2294 if (flags & 1)
3280af22 2295 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2296 if (flags & 4)
3280af22 2297 PL_markstack_ptr--;
ac27b0f5 2298 if (flags & 8)
3280af22 2299 PL_retstack_ix--;
84902520 2300 if (flags & 16)
3280af22 2301 PL_scopestack_ix -= 1;
84902520 2302 if (flags & 64)
2303 SvREFCNT_dec(sv);
533c011a 2304 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2305
3280af22 2306 PL_Sv = tSv; /* Restore global temporaries. */
2307 PL_Xpv = tXpv;
79072805 2308 return;
2309}
4e35701f 2310
2311
51371543 2312#ifdef PERL_OBJECT
51371543 2313#include "XSUB.h"
2314#endif
2315
2316static void
2317restore_magic(pTHXo_ void *p)
2318{
48944bdf 2319 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543 2320 SV* sv = mgs->mgs_sv;
2321
2322 if (!sv)
2323 return;
2324
2325 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2326 {
2327 if (mgs->mgs_flags)
2328 SvFLAGS(sv) |= mgs->mgs_flags;
2329 else
2330 mg_magical(sv);
2331 if (SvGMAGICAL(sv))
2332 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2333 }
2334
2335 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2336
2337 /* If we're still on top of the stack, pop us off. (That condition
2338 * will be satisfied if restore_magic was called explicitly, but *not*
2339 * if it's being called via leave_scope.)
2340 * The reason for doing this is that otherwise, things like sv_2cv()
2341 * may leave alloc gunk on the savestack, and some code
2342 * (e.g. sighandler) doesn't expect that...
2343 */
2344 if (PL_savestack_ix == mgs->mgs_ss_ix)
2345 {
2346 I32 popval = SSPOPINT;
c76ac1ee 2347 assert(popval == SAVEt_DESTRUCTOR_X);
51371543 2348 PL_savestack_ix -= 2;
2349 popval = SSPOPINT;
2350 assert(popval == SAVEt_ALLOC);
2351 popval = SSPOPINT;
2352 PL_savestack_ix -= popval;
2353 }
2354
2355}
2356
2357static void
2358unwind_handler_stack(pTHXo_ void *p)
2359{
51371543 2360 U32 flags = *(U32*)p;
2361
2362 if (flags & 1)
2363 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2364 /* cxstack_ix-- Not needed, die already unwound it. */
2365 if (flags & 64)
2366 SvREFCNT_dec(sig_sv);
2367}