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