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