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