This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Same fix, for the test, now.
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
cbdf9ef8 4 * 2000, 2001, 2002, 2003, 2004, 2005 by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
11/*
12 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
79072805
LW
14 */
15
ccfc67b7
JH
16/*
17=head1 Magical Functions
166f8a29
DM
18
19"Magic" is special data attached to SV structures in order to give them
20"magical" properties. When any Perl code tries to read from, or assign to,
21an SV marked as magical, it calls the 'get' or 'set' function associated
22with that SV's magic. A get is called prior to reading an SV, in order to
ddfa107c 23give it a chance to update its internal value (get on $. writes the line
166f8a29
DM
24number of the last read filehandle into to the SV's IV slot), while
25set is called after an SV has been written to, in order to allow it to make
ddfa107c 26use of its changed value (set on $/ copies the SV's new value to the
166f8a29
DM
27PL_rs global variable).
28
29Magic is implemented as a linked list of MAGIC structures attached to the
30SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31of functions that implement the get(), set(), length() etc functions,
32plus space for some flags and pointers. For example, a tied variable has
33a MAGIC structure that contains a pointer to the object associated with the
34tie.
35
ccfc67b7
JH
36*/
37
79072805 38#include "EXTERN.h"
864dbfa3 39#define PERL_IN_MG_C
79072805
LW
40#include "perl.h"
41
5cd24f17 42#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221
CS
43# ifndef NGROUPS
44# define NGROUPS 32
45# endif
b7953727
JH
46# ifdef I_GRP
47# include <grp.h>
48# endif
188ea221
CS
49#endif
50
17aa7f3d
JH
51#ifdef __hpux
52# include <sys/pstat.h>
53#endif
54
e69880a5
CB
55Signal_t Perl_csighandler(int sig);
56
35a4481c
AL
57static void restore_magic(pTHX_ const void *p);
58static void unwind_handler_stack(pTHX_ const void *p);
51371543 59
9cffb111
OS
60#ifdef __Lynx__
61/* Missing protos on LynxOS */
62void setruid(uid_t id);
63void seteuid(uid_t id);
64void setrgid(uid_t id);
65void setegid(uid_t id);
66#endif
67
c07a80fd
PP
68/*
69 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
70 */
71
72struct magic_state {
73 SV* mgs_sv;
74 U32 mgs_flags;
455ece5e 75 I32 mgs_ss_ix;
c07a80fd 76};
455ece5e 77/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
78
79STATIC void
8fb26106 80S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
c07a80fd 81{
455ece5e 82 MGS* mgs;
c07a80fd 83 assert(SvMAGICAL(sv));
f8c7b90f 84#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
85 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
86 if (SvIsCOW(sv))
87 sv_force_normal(sv);
88#endif
c07a80fd 89
685f876f 90 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
91
92 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd
PP
93 mgs->mgs_sv = sv;
94 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 95 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd
PP
96
97 SvMAGICAL_off(sv);
98 SvREADONLY_off(sv);
06759ea0 99 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
100}
101
954c1994
GS
102/*
103=for apidoc mg_magical
104
105Turns on the magical status of an SV. See C<sv_magic>.
106
107=cut
108*/
109
8990e307 110void
864dbfa3 111Perl_mg_magical(pTHX_ SV *sv)
8990e307 112{
e1ec3a88 113 const MAGIC* mg;
8990e307 114 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 115 const MGVTBL* const vtbl = mg->mg_virtual;
8990e307 116 if (vtbl) {
2b260de0 117 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
118 SvGMAGICAL_on(sv);
119 if (vtbl->svt_set)
120 SvSMAGICAL_on(sv);
2b260de0 121 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307
LW
122 SvRMAGICAL_on(sv);
123 }
124 }
125}
126
954c1994
GS
127/*
128=for apidoc mg_get
129
130Do magic after a value is retrieved from the SV. See C<sv_magic>.
131
132=cut
133*/
134
79072805 135int
864dbfa3 136Perl_mg_get(pTHX_ SV *sv)
79072805 137{
35a4481c 138 const I32 mgs_ix = SSNEW(sizeof(MGS));
fe2774ed 139 const bool was_temp = (bool)SvTEMP(sv);
0723351e 140 int have_new = 0;
ff76feab 141 MAGIC *newmg, *head, *cur, *mg;
20135930 142 /* guard against sv having being freed midway by holding a private
6683b158
NC
143 reference. */
144
145 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
146 cause the SV's buffer to get stolen (and maybe other stuff).
147 So restore it.
148 */
149 sv_2mortal(SvREFCNT_inc(sv));
150 if (!was_temp) {
151 SvTEMP_off(sv);
152 }
153
455ece5e 154 save_magic(mgs_ix, sv);
463ee0b2 155
ff76feab
AMS
156 /* We must call svt_get(sv, mg) for each valid entry in the linked
157 list of magic. svt_get() may delete the current entry, add new
158 magic to the head of the list, or upgrade the SV. AMS 20010810 */
159
160 newmg = cur = head = mg = SvMAGIC(sv);
161 while (mg) {
35a4481c 162 const MGVTBL * const vtbl = mg->mg_virtual;
ff76feab 163
2b260de0 164 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 165 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
b77f7d40 166
58f82c5c
DM
167 /* guard against magic having been deleted - eg FETCH calling
168 * untie */
169 if (!SvMAGIC(sv))
170 break;
b77f7d40 171
ff76feab
AMS
172 /* Don't restore the flags for this entry if it was deleted. */
173 if (mg->mg_flags & MGf_GSKIP)
174 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
a0d0e21e 175 }
ff76feab
AMS
176
177 mg = mg->mg_moremagic;
178
0723351e 179 if (have_new) {
ff76feab
AMS
180 /* Have we finished with the new entries we saw? Start again
181 where we left off (unless there are more new entries). */
182 if (mg == head) {
0723351e 183 have_new = 0;
ff76feab
AMS
184 mg = cur;
185 head = newmg;
186 }
187 }
188
189 /* Were any new entries added? */
0723351e
NC
190 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
191 have_new = 1;
ff76feab
AMS
192 cur = mg;
193 mg = newmg;
760ac839 194 }
79072805 195 }
463ee0b2 196
acfe0abc 197 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
6683b158
NC
198
199 if (SvREFCNT(sv) == 1) {
200 /* We hold the last reference to this SV, which implies that the
201 SV was deleted as a side effect of the routines we called. */
0c34ef67 202 SvOK_off(sv);
6683b158 203 }
79072805
LW
204 return 0;
205}
206
954c1994
GS
207/*
208=for apidoc mg_set
209
210Do magic after a value is assigned to the SV. See C<sv_magic>.
211
212=cut
213*/
214
79072805 215int
864dbfa3 216Perl_mg_set(pTHX_ SV *sv)
79072805 217{
35a4481c 218 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 219 MAGIC* mg;
463ee0b2
LW
220 MAGIC* nextmg;
221
455ece5e 222 save_magic(mgs_ix, sv);
463ee0b2
LW
223
224 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
e1ec3a88 225 const MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 226 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
227 if (mg->mg_flags & MGf_GSKIP) {
228 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 229 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 230 }
2b260de0 231 if (vtbl && vtbl->svt_set)
fc0dc3b3 232 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 233 }
463ee0b2 234
acfe0abc 235 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
236 return 0;
237}
238
954c1994
GS
239/*
240=for apidoc mg_length
241
242Report on the SV's length. See C<sv_magic>.
243
244=cut
245*/
246
79072805 247U32
864dbfa3 248Perl_mg_length(pTHX_ SV *sv)
79072805
LW
249{
250 MAGIC* mg;
463ee0b2 251 STRLEN len;
463ee0b2 252
79072805 253 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 254 const MGVTBL * const vtbl = mg->mg_virtual;
2b260de0 255 if (vtbl && vtbl->svt_len) {
35a4481c 256 const I32 mgs_ix = SSNEW(sizeof(MGS));
455ece5e 257 save_magic(mgs_ix, sv);
a0d0e21e 258 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 259 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 260 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
261 return len;
262 }
263 }
264
35a4481c 265 if (DO_UTF8(sv)) {
10516c54 266 const U8 *s = (U8*)SvPV_const(sv, len);
5636d518
DB
267 len = Perl_utf8_length(aTHX_ s, s + len);
268 }
269 else
10516c54 270 (void)SvPV_const(sv, len);
463ee0b2 271 return len;
79072805
LW
272}
273
8fb26106 274I32
864dbfa3 275Perl_mg_size(pTHX_ SV *sv)
93965878
NIS
276{
277 MAGIC* mg;
ac27b0f5 278
93965878 279 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 280 const MGVTBL* const vtbl = mg->mg_virtual;
2b260de0 281 if (vtbl && vtbl->svt_len) {
35a4481c
AL
282 const I32 mgs_ix = SSNEW(sizeof(MGS));
283 I32 len;
455ece5e 284 save_magic(mgs_ix, sv);
93965878 285 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 286 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 287 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
93965878
NIS
288 return len;
289 }
290 }
291
292 switch(SvTYPE(sv)) {
293 case SVt_PVAV:
35a4481c 294 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
93965878
NIS
295 case SVt_PVHV:
296 /* FIXME */
297 default:
cea2e8a9 298 Perl_croak(aTHX_ "Size magic not implemented");
93965878
NIS
299 break;
300 }
301 return 0;
302}
303
954c1994
GS
304/*
305=for apidoc mg_clear
306
307Clear something magical that the SV represents. See C<sv_magic>.
308
309=cut
310*/
311
79072805 312int
864dbfa3 313Perl_mg_clear(pTHX_ SV *sv)
79072805 314{
35a4481c 315 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 316 MAGIC* mg;
463ee0b2 317
455ece5e 318 save_magic(mgs_ix, sv);
463ee0b2 319
79072805 320 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 321 const MGVTBL* const vtbl = mg->mg_virtual;
a0d0e21e 322 /* omit GSKIP -- never set here */
727405f8 323
2b260de0 324 if (vtbl && vtbl->svt_clear)
fc0dc3b3 325 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 326 }
463ee0b2 327
acfe0abc 328 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
329 return 0;
330}
331
954c1994
GS
332/*
333=for apidoc mg_find
334
335Finds the magic pointer for type matching the SV. See C<sv_magic>.
336
337=cut
338*/
339
93a17b20 340MAGIC*
35a4481c 341Perl_mg_find(pTHX_ const SV *sv, int type)
93a17b20 342{
35a4481c
AL
343 if (sv) {
344 MAGIC *mg;
345 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
346 if (mg->mg_type == type)
347 return mg;
348 }
93a17b20
LW
349 }
350 return 0;
351}
352
954c1994
GS
353/*
354=for apidoc mg_copy
355
356Copies the magic from one SV to another. See C<sv_magic>.
357
358=cut
359*/
360
79072805 361int
864dbfa3 362Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 363{
463ee0b2 364 int count = 0;
79072805 365 MAGIC* mg;
463ee0b2 366 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 367 const MGVTBL* const vtbl = mg->mg_virtual;
68795e93
NIS
368 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
369 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
370 }
371 else if (isUPPER(mg->mg_type)) {
33c27489 372 sv_magic(nsv,
14befaf4
DM
373 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
374 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
375 ? sv : mg->mg_obj,
33c27489 376 toLOWER(mg->mg_type), key, klen);
463ee0b2 377 count++;
79072805 378 }
79072805 379 }
463ee0b2 380 return count;
79072805
LW
381}
382
954c1994
GS
383/*
384=for apidoc mg_free
385
386Free any magic storage used by the SV. See C<sv_magic>.
387
388=cut
389*/
390
79072805 391int
864dbfa3 392Perl_mg_free(pTHX_ SV *sv)
79072805
LW
393{
394 MAGIC* mg;
395 MAGIC* moremagic;
396 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
35a4481c 397 const MGVTBL* const vtbl = mg->mg_virtual;
79072805 398 moremagic = mg->mg_moremagic;
2b260de0 399 if (vtbl && vtbl->svt_free)
fc0dc3b3 400 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 401 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
979acdb5 402 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
88e89b8a 403 Safefree(mg->mg_ptr);
565764a8 404 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 405 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 406 }
b881518d
JH
407 if (mg->mg_flags & MGf_REFCOUNTED)
408 SvREFCNT_dec(mg->mg_obj);
79072805
LW
409 Safefree(mg);
410 }
b162af07 411 SvMAGIC_set(sv, NULL);
79072805
LW
412 return 0;
413}
414
79072805 415#include <signal.h>
79072805 416
942e002e 417U32
864dbfa3 418Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 419{
35a4481c 420 register const REGEXP *rx;
dd374669 421 (void)sv;
6cef1e77 422
aaa362c4 423 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
8f580fb8
IZ
424 if (mg->mg_obj) /* @+ */
425 return rx->nparens;
426 else /* @- */
427 return rx->lastparen;
428 }
ac27b0f5 429
942e002e 430 return (U32)-1;
6cef1e77
IZ
431}
432
433int
864dbfa3 434Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 435{
6cef1e77 436 register REGEXP *rx;
6cef1e77 437
aaa362c4 438 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
35a4481c
AL
439 register const I32 paren = mg->mg_len;
440 register I32 s;
441 register I32 t;
6cef1e77
IZ
442 if (paren < 0)
443 return 0;
eb160463 444 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
445 (s = rx->startp[paren]) != -1 &&
446 (t = rx->endp[paren]) != -1)
6cef1e77 447 {
35a4481c 448 register I32 i;
6cef1e77 449 if (mg->mg_obj) /* @+ */
cf93c79d 450 i = t;
6cef1e77 451 else /* @- */
cf93c79d 452 i = s;
727405f8 453
a30b2f1f 454 if (i > 0 && RX_MATCH_UTF8(rx)) {
1aa99e6b 455 char *b = rx->subbeg;
0064a8a9
JH
456 if (b)
457 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 458 }
0064a8a9
JH
459
460 sv_setiv(sv, i);
6cef1e77
IZ
461 }
462 }
463 return 0;
464}
465
e4b89193 466int
a29d06ed
MG
467Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
468{
dd374669 469 (void)sv; (void)mg;
a29d06ed 470 Perl_croak(aTHX_ PL_no_modify);
0dbb1585 471 NORETURN_FUNCTION_END;
a29d06ed
MG
472}
473
93a17b20 474U32
864dbfa3 475Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20
LW
476{
477 register I32 paren;
93a17b20 478 register I32 i;
dd374669 479 register const REGEXP *rx;
a197cbdd 480 I32 s1, t1;
93a17b20
LW
481
482 switch (*mg->mg_ptr) {
483 case '1': case '2': case '3': case '4':
484 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 485 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 486
ffc61ed2 487 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 488 getparen:
eb160463 489 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
490 (s1 = rx->startp[paren]) != -1 &&
491 (t1 = rx->endp[paren]) != -1)
bbce6d69 492 {
cf93c79d 493 i = t1 - s1;
a197cbdd 494 getlen:
a30b2f1f 495 if (i > 0 && RX_MATCH_UTF8(rx)) {
a28509cc 496 const char * const s = rx->subbeg + s1;
768c67ee
JH
497 const U8 *ep;
498 STRLEN el;
ffc61ed2 499
6d5fa195 500 i = t1 - s1;
768c67ee
JH
501 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
502 i = el;
a197cbdd 503 }
ffc61ed2 504 if (i < 0)
0844c848 505 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 506 return i;
93a17b20 507 }
235bddc8
NIS
508 else {
509 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 510 report_uninit(sv);
235bddc8
NIS
511 }
512 }
513 else {
514 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 515 report_uninit(sv);
93a17b20 516 }
748a9306 517 return 0;
93a17b20 518 case '+':
aaa362c4 519 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 520 paren = rx->lastparen;
13f57bf8
CS
521 if (paren)
522 goto getparen;
93a17b20 523 }
748a9306 524 return 0;
a01268b5
JH
525 case '\016': /* ^N */
526 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
527 paren = rx->lastcloseparen;
528 if (paren)
529 goto getparen;
530 }
531 return 0;
93a17b20 532 case '`':
aaa362c4 533 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
534 if (rx->startp[0] != -1) {
535 i = rx->startp[0];
a197cbdd
GS
536 if (i > 0) {
537 s1 = 0;
538 t1 = i;
539 goto getlen;
540 }
93a17b20 541 }
93a17b20 542 }
748a9306 543 return 0;
93a17b20 544 case '\'':
aaa362c4 545 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
546 if (rx->endp[0] != -1) {
547 i = rx->sublen - rx->endp[0];
a197cbdd
GS
548 if (i > 0) {
549 s1 = rx->endp[0];
550 t1 = rx->sublen;
551 goto getlen;
552 }
93a17b20 553 }
93a17b20 554 }
748a9306 555 return 0;
93a17b20
LW
556 }
557 magic_get(sv,mg);
2d8e6c8d 558 if (!SvPOK(sv) && SvNIOK(sv)) {
8b6b16e7 559 sv_2pv(sv, 0);
2d8e6c8d 560 }
93a17b20
LW
561 if (SvPOK(sv))
562 return SvCUR(sv);
563 return 0;
564}
565
79072805 566int
864dbfa3 567Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805 568{
27da23d5 569 dVAR;
79072805 570 register I32 paren;
35272f84 571 register char *s = NULL;
79072805 572 register I32 i;
d9f97599 573 register REGEXP *rx;
79072805
LW
574
575 switch (*mg->mg_ptr) {
748a9306 576 case '\001': /* ^A */
3280af22 577 sv_setsv(sv, PL_bodytarget);
748a9306 578 break;
e5218da5
GA
579 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
580 if (*(mg->mg_ptr+1) == '\0') {
581 sv_setiv(sv, (IV)PL_minus_c);
582 }
583 else if (strEQ(mg->mg_ptr, "\003HILD_ERROR_NATIVE")) {
584 sv_setiv(sv, (IV)STATUS_NATIVE);
585 }
49460fe6
NIS
586 break;
587
79072805 588 case '\004': /* ^D */
aea4f609 589 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 590 break;
28f23441 591 case '\005': /* ^E */
0a378802 592 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 593#ifdef MACOS_TRADITIONAL
0a378802
JH
594 {
595 char msg[256];
727405f8 596
0a378802 597 sv_setnv(sv,(double)gMacPerl_OSErr);
727405f8 598 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
0a378802 599 }
727405f8 600#else
28f23441 601#ifdef VMS
0a378802
JH
602 {
603# include <descrip.h>
604# include <starlet.h>
605 char msg[255];
606 $DESCRIPTOR(msgdsc,msg);
607 sv_setnv(sv,(NV) vaxc$errno);
608 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
609 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
610 else
c69006e4 611 sv_setpvn(sv,"",0);
0a378802 612 }
28f23441 613#else
88e89b8a 614#ifdef OS2
0a378802
JH
615 if (!(_emx_env & 0x200)) { /* Under DOS */
616 sv_setnv(sv, (NV)errno);
617 sv_setpv(sv, errno ? Strerror(errno) : "");
618 } else {
619 if (errno != errno_isOS2) {
620 int tmp = _syserrno();
621 if (tmp) /* 2nd call to _syserrno() makes it 0 */
622 Perl_rc = tmp;
623 }
624 sv_setnv(sv, (NV)Perl_rc);
625 sv_setpv(sv, os2error(Perl_rc));
626 }
88e89b8a 627#else
22fae026 628#ifdef WIN32
0a378802
JH
629 {
630 DWORD dwErr = GetLastError();
631 sv_setnv(sv, (NV)dwErr);
632 if (dwErr)
633 {
634 PerlProc_GetOSError(sv, dwErr);
635 }
636 else
c69006e4 637 sv_setpvn(sv, "", 0);
0a378802
JH
638 SetLastError(dwErr);
639 }
22fae026 640#else
f6c8f21d
RGS
641 {
642 int saveerrno = errno;
643 sv_setnv(sv, (NV)errno);
644 sv_setpv(sv, errno ? Strerror(errno) : "");
645 errno = saveerrno;
646 }
28f23441 647#endif
88e89b8a 648#endif
22fae026 649#endif
cd39f2b6 650#endif
0a378802
JH
651 SvNOK_on(sv); /* what a wonderful hack! */
652 }
653 else if (strEQ(mg->mg_ptr+1, "NCODING"))
654 sv_setsv(sv, PL_encoding);
655 break;
79072805 656 case '\006': /* ^F */
3280af22 657 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 658 break;
a0d0e21e 659 case '\010': /* ^H */
3280af22 660 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 661 break;
9d116dd7 662 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
663 if (PL_inplace)
664 sv_setpv(sv, PL_inplace);
79072805 665 else
3280af22 666 sv_setsv(sv, &PL_sv_undef);
79072805 667 break;
ac27b0f5 668 case '\017': /* ^O & ^OPEN */
3511154c 669 if (*(mg->mg_ptr+1) == '\0') {
ac27b0f5 670 sv_setpv(sv, PL_osname);
3511154c
DM
671 SvTAINTED_off(sv);
672 }
ac27b0f5
NIS
673 else if (strEQ(mg->mg_ptr, "\017PEN")) {
674 if (!PL_compiling.cop_io)
675 sv_setsv(sv, &PL_sv_undef);
676 else {
677 sv_setsv(sv, PL_compiling.cop_io);
678 }
679 }
28f23441 680 break;
79072805 681 case '\020': /* ^P */
3280af22 682 sv_setiv(sv, (IV)PL_perldb);
79072805 683 break;
fb73857a 684 case '\023': /* ^S */
4ffa73a3 685 if (*(mg->mg_ptr+1) == '\0') {
3280af22 686 if (PL_lex_state != LEX_NOTPARSING)
0c34ef67 687 SvOK_off(sv);
3280af22 688 else if (PL_in_eval)
6dc8a9e4 689 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
690 else
691 sv_setiv(sv, 0);
d58bf5aa 692 }
fb73857a 693 break;
79072805 694 case '\024': /* ^T */
7c36658b 695 if (*(mg->mg_ptr+1) == '\0') {
88e89b8a 696#ifdef BIG_TIME
7c36658b 697 sv_setnv(sv, PL_basetime);
88e89b8a 698#else
7c36658b 699 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 700#endif
7c36658b
MS
701 }
702 else if (strEQ(mg->mg_ptr, "\024AINT"))
9aa05f58
RGS
703 sv_setiv(sv, PL_tainting
704 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
705 : 0);
7c36658b 706 break;
7cebcbc0 707 case '\025': /* $^UNICODE, $^UTF8LOCALE */
a05d7ebb
JH
708 if (strEQ(mg->mg_ptr, "\025NICODE"))
709 sv_setuv(sv, (UV) PL_unicode);
7cebcbc0
NC
710 else if (strEQ(mg->mg_ptr, "\025TF8LOCALE"))
711 sv_setuv(sv, (UV) PL_utf8locale);
fde18df1
JH
712 break;
713 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
714 if (*(mg->mg_ptr+1) == '\0')
715 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
0a378802 716 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
d3a7d8c7
GS
717 if (PL_compiling.cop_warnings == pWARN_NONE ||
718 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7
JH
719 {
720 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
721 }
d3a7d8c7 722 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca
RGS
723 /* Get the bit mask for $warnings::Bits{all}, because
724 * it could have been extended by warnings::register */
725 SV **bits_all;
726 HV *bits=get_hv("warnings::Bits", FALSE);
727 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
728 sv_setsv(sv, *bits_all);
729 }
730 else {
731 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
732 }
ac27b0f5 733 }
4438c4b7
JH
734 else {
735 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 736 }
d3a7d8c7 737 SvPOK_only(sv);
4438c4b7 738 }
79072805
LW
739 break;
740 case '1': case '2': case '3': case '4':
741 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 742 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
743 I32 s1, t1;
744
a863c7d1
MB
745 /*
746 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
747 * XXX Does the new way break anything?
748 */
ffc61ed2 749 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 750 getparen:
eb160463 751 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
752 (s1 = rx->startp[paren]) != -1 &&
753 (t1 = rx->endp[paren]) != -1)
bbce6d69 754 {
cf93c79d
IZ
755 i = t1 - s1;
756 s = rx->subbeg + s1;
01ec43d0 757 if (!rx->subbeg)
c2e66d9e
GS
758 break;
759
13f57bf8 760 getrx:
748a9306 761 if (i >= 0) {
cf93c79d 762 sv_setpvn(sv, s, i);
a30b2f1f 763 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
7e2040f0
GS
764 SvUTF8_on(sv);
765 else
766 SvUTF8_off(sv);
e9814ee1
HS
767 if (PL_tainting) {
768 if (RX_MATCH_TAINTED(rx)) {
769 MAGIC* mg = SvMAGIC(sv);
770 MAGIC* mgt;
771 PL_tainted = 1;
b162af07 772 SvMAGIC_set(sv, mg->mg_moremagic);
e9814ee1
HS
773 SvTAINT(sv);
774 if ((mgt = SvMAGIC(sv))) {
775 mg->mg_moremagic = mgt;
b162af07 776 SvMAGIC_set(sv, mg);
e9814ee1
HS
777 }
778 } else
779 SvTAINTED_off(sv);
780 }
748a9306
LW
781 break;
782 }
79072805 783 }
79072805 784 }
3280af22 785 sv_setsv(sv,&PL_sv_undef);
79072805
LW
786 break;
787 case '+':
aaa362c4 788 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 789 paren = rx->lastparen;
a0d0e21e
LW
790 if (paren)
791 goto getparen;
79072805 792 }
3280af22 793 sv_setsv(sv,&PL_sv_undef);
79072805 794 break;
a01268b5
JH
795 case '\016': /* ^N */
796 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
797 paren = rx->lastcloseparen;
798 if (paren)
799 goto getparen;
800 }
801 sv_setsv(sv,&PL_sv_undef);
802 break;
79072805 803 case '`':
aaa362c4 804 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
805 if ((s = rx->subbeg) && rx->startp[0] != -1) {
806 i = rx->startp[0];
13f57bf8 807 goto getrx;
79072805 808 }
79072805 809 }
3280af22 810 sv_setsv(sv,&PL_sv_undef);
79072805
LW
811 break;
812 case '\'':
aaa362c4 813 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
814 if (rx->subbeg && rx->endp[0] != -1) {
815 s = rx->subbeg + rx->endp[0];
816 i = rx->sublen - rx->endp[0];
13f57bf8 817 goto getrx;
79072805 818 }
79072805 819 }
3280af22 820 sv_setsv(sv,&PL_sv_undef);
79072805
LW
821 break;
822 case '.':
3280af22 823 if (GvIO(PL_last_in_gv)) {
357c8808 824 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805 825 }
79072805
LW
826 break;
827 case '?':
809a5acc 828 {
809a5acc 829 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 830#ifdef COMPLEX_STATUS
6b88bc9c
GS
831 LvTARGOFF(sv) = PL_statusvalue;
832 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 833#endif
809a5acc 834 }
79072805
LW
835 break;
836 case '^':
0daa599b
RGS
837 if (GvIOp(PL_defoutgv))
838 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
839 if (s)
840 sv_setpv(sv,s);
841 else {
3280af22 842 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
843 sv_catpv(sv,"_TOP");
844 }
845 break;
846 case '~':
0daa599b
RGS
847 if (GvIOp(PL_defoutgv))
848 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 849 if (!s)
3280af22 850 s = GvENAME(PL_defoutgv);
79072805
LW
851 sv_setpv(sv,s);
852 break;
79072805 853 case '=':
0daa599b
RGS
854 if (GvIOp(PL_defoutgv))
855 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
856 break;
857 case '-':
0daa599b
RGS
858 if (GvIOp(PL_defoutgv))
859 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
860 break;
861 case '%':
0daa599b
RGS
862 if (GvIOp(PL_defoutgv))
863 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 864 break;
79072805
LW
865 case ':':
866 break;
867 case '/':
868 break;
869 case '[':
3280af22 870 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
871 break;
872 case '|':
0daa599b
RGS
873 if (GvIOp(PL_defoutgv))
874 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
875 break;
876 case ',':
79072805
LW
877 break;
878 case '\\':
b2ce0fda 879 if (PL_ors_sv)
f28098ff 880 sv_copypv(sv, PL_ors_sv);
79072805 881 break;
79072805 882 case '!':
a5f75d66 883#ifdef VMS
65202027 884 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 885 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 886#else
88e89b8a
PP
887 {
888 int saveerrno = errno;
65202027 889 sv_setnv(sv, (NV)errno);
88e89b8a 890#ifdef OS2
ed344e4f
IZ
891 if (errno == errno_isOS2 || errno == errno_isOS2_set)
892 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 893 else
a5f75d66 894#endif
2304df62 895 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
896 errno = saveerrno;
897 }
898#endif
946ec16e 899 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
900 break;
901 case '<':
3280af22 902 sv_setiv(sv, (IV)PL_uid);
79072805
LW
903 break;
904 case '>':
3280af22 905 sv_setiv(sv, (IV)PL_euid);
79072805
LW
906 break;
907 case '(':
3280af22 908 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 909#ifdef HAS_GETGROUPS
a3b680e6 910 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_gid);
a52cb5f7 911#endif
79072805
LW
912 goto add_groups;
913 case ')':
3280af22 914 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 915#ifdef HAS_GETGROUPS
a3b680e6 916 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_egid);
a52cb5f7 917#endif
79072805 918 add_groups:
79072805 919#ifdef HAS_GETGROUPS
79072805 920 {
a0d0e21e 921 Groups_t gary[NGROUPS];
a3b680e6
AL
922 I32 j = getgroups(NGROUPS,gary);
923 while (--j >= 0)
924 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, (long unsigned int)gary[j]);
79072805
LW
925 }
926#endif
155aba94 927 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 928 break;
cd39f2b6 929#ifndef MACOS_TRADITIONAL
79072805
LW
930 case '0':
931 break;
cd39f2b6 932#endif
79072805 933 }
a0d0e21e 934 return 0;
79072805
LW
935}
936
937int
864dbfa3 938Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
939{
940 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
941
942 if (uf && uf->uf_val)
24f81a43 943 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
944 return 0;
945}
946
947int
864dbfa3 948Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 949{
27da23d5 950 dVAR;
b83604b4 951 const char *s;
e62f0680 952 const char *ptr;
5aabfad6 953 STRLEN len, klen;
1e422769 954
b83604b4 955 s = SvPV_const(sv,len);
e62f0680 956 ptr = MgPV_const(mg,klen);
88e89b8a 957 my_setenv(ptr, s);
1e422769 958
a0d0e21e
LW
959#ifdef DYNAMIC_ENV_FETCH
960 /* We just undefd an environment var. Is a replacement */
961 /* waiting in the wings? */
962 if (!len) {
5aabfad6 963 SV **valp;
6b88bc9c 964 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
b83604b4 965 s = SvPV_const(*valp, len);
a0d0e21e
LW
966 }
967#endif
1e422769 968
39e571d4 969#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
970 /* And you'll never guess what the dog had */
971 /* in its mouth... */
3280af22 972 if (PL_tainting) {
1e422769
PP
973 MgTAINTEDDIR_off(mg);
974#ifdef VMS
5aabfad6 975 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 976 char pathbuf[256], eltbuf[256], *cp, *elt = s;
c623ac67 977 Stat_t sbuf;
1e422769
PP
978 int i = 0, j = 0;
979
980 do { /* DCL$PATH may be a search list */
981 while (1) { /* as may dev portion of any element */
982 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
983 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
984 cando_by_name(S_IWUSR,0,elt) ) {
985 MgTAINTEDDIR_on(mg);
986 return 0;
987 }
988 }
989 if ((cp = strchr(elt, ':')) != Nullch)
990 *cp = '\0';
991 if (my_trnlnm(elt, eltbuf, j++))
992 elt = eltbuf;
993 else
994 break;
995 }
996 j = 0;
997 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
998 }
999#endif /* VMS */
5aabfad6 1000 if (s && klen == 4 && strEQ(ptr,"PATH")) {
b83604b4 1001 const char *strend = s + len;
463ee0b2
LW
1002
1003 while (s < strend) {
96827780 1004 char tmpbuf[256];
c623ac67 1005 Stat_t st;
5f74f29c 1006 I32 i;
96827780 1007 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 1008 s, strend, ':', &i);
463ee0b2 1009 s++;
96827780
MB
1010 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1011 || *tmpbuf != '/'
c6ed36e1 1012 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1013 MgTAINTEDDIR_on(mg);
1e422769
PP
1014 return 0;
1015 }
463ee0b2 1016 }
79072805
LW
1017 }
1018 }
39e571d4 1019#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1020
79072805
LW
1021 return 0;
1022}
1023
1024int
864dbfa3 1025Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1026{
dd374669 1027 (void)sv;
01b8bcb7 1028 my_setenv(MgPV_nolen_const(mg),Nullch);
85e6fe83
LW
1029 return 0;
1030}
1031
88e89b8a 1032int
864dbfa3 1033Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 1034{
27da23d5 1035#if defined(VMS) || defined(EPOC) || defined(SYMBIAN)
cea2e8a9 1036 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1037#else
3280af22 1038 if (PL_localizing) {
fb73857a
PP
1039 HE* entry;
1040 magic_clear_all_env(sv,mg);
1041 hv_iterinit((HV*)sv);
155aba94 1042 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1043 I32 keylen;
1044 my_setenv(hv_iterkey(entry, &keylen),
b83604b4 1045 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
fb73857a
PP
1046 }
1047 }
1048#endif
1049 return 0;
1050}
1051
1052int
864dbfa3 1053Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1054{
27da23d5 1055 dVAR;
2f42fcb0 1056#ifndef PERL_MICRO
27da23d5 1057#if defined(VMS) || defined(EPOC) || defined(SYMBIAN)
cea2e8a9 1058 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 1059#else
4efc5df6 1060# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137 1061 PerlEnv_clearenv();
4efc5df6
GS
1062# else
1063# ifdef USE_ENVIRON_ARRAY
1064# if defined(USE_ITHREADS)
1065 /* only the parent thread can clobber the process environment */
1066 if (PL_curinterp == aTHX)
1067# endif
1068 {
1069# ifndef PERL_USE_SAFE_PUTENV
50acdf95 1070 if (!PL_use_safe_putenv) {
66b1d557
HM
1071 I32 i;
1072
3280af22 1073 if (environ == PL_origenviron)
f2517201 1074 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
1075 else
1076 for (i = 0; environ[i]; i++)
f2517201 1077 safesysfree(environ[i]);
50acdf95 1078 }
4efc5df6 1079# endif /* PERL_USE_SAFE_PUTENV */
f2517201 1080
66b1d557 1081 environ[0] = Nullch;
4efc5df6
GS
1082 }
1083# endif /* USE_ENVIRON_ARRAY */
052c2dc6 1084# endif /* PERL_IMPLICIT_SYS || WIN32 */
2f42fcb0
JH
1085#endif /* VMS || EPOC */
1086#endif /* !PERL_MICRO */
dd374669
AL
1087 (void)sv;
1088 (void)mg;
3e3baf6d 1089 return 0;
66b1d557
HM
1090}
1091
64ca3a65 1092#ifndef PERL_MICRO
2d4fcd5e
AJ
1093#ifdef HAS_SIGPROCMASK
1094static void
1095restore_sigmask(pTHX_ SV *save_sv)
1096{
b83604b4 1097 const sigset_t *ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
2d4fcd5e
AJ
1098 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1099}
1100#endif
66b1d557 1101int
864dbfa3 1102Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1103{
1104 I32 i;
1105 /* Are we fetching a signal entry? */
01b8bcb7 1106 i = whichsig(MgPV_nolen_const(mg));
e02bfb16 1107 if (i > 0) {
22c35a8c
GS
1108 if(PL_psig_ptr[i])
1109 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1110 else {
85b332e2 1111 Sighandler_t sigstate;
2e34cc90 1112 sigstate = rsignal_state(i);
23ada85b 1113#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1114 if (PL_sig_handlers_initted && PL_sig_ignoring[i]) sigstate = SIG_IGN;
2e34cc90
CL
1115#endif
1116#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1117 if (PL_sig_handlers_initted && PL_sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1118#endif
88e89b8a 1119 /* cache state so we don't fetch it again */
ff68c719 1120 if(sigstate == SIG_IGN)
88e89b8a
PP
1121 sv_setpv(sv,"IGNORE");
1122 else
3280af22 1123 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1124 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1125 SvTEMP_off(sv);
1126 }
1127 }
1128 return 0;
1129}
1130int
864dbfa3 1131Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1132{
2d4fcd5e
AJ
1133 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1134 * refactoring might be in order.
1135 */
27da23d5 1136 dVAR;
01b8bcb7 1137 register const char *s = MgPV_nolen_const(mg);
dd374669 1138 (void)sv;
2d4fcd5e 1139 if (*s == '_') {
27da23d5 1140 SV** svp = 0;
2d4fcd5e
AJ
1141 if (strEQ(s,"__DIE__"))
1142 svp = &PL_diehook;
1143 else if (strEQ(s,"__WARN__"))
1144 svp = &PL_warnhook;
1145 else
1146 Perl_croak(aTHX_ "No such hook: %s", s);
27da23d5 1147 if (svp && *svp) {
dd374669 1148 SV *to_dec = *svp;
2d4fcd5e
AJ
1149 *svp = 0;
1150 SvREFCNT_dec(to_dec);
1151 }
1152 }
1153 else {
1154 I32 i;
1155 /* Are we clearing a signal entry? */
1156 i = whichsig(s);
e02bfb16 1157 if (i > 0) {
2d4fcd5e
AJ
1158#ifdef HAS_SIGPROCMASK
1159 sigset_t set, save;
1160 SV* save_sv;
1161 /* Avoid having the signal arrive at a bad time, if possible. */
1162 sigemptyset(&set);
1163 sigaddset(&set,i);
1164 sigprocmask(SIG_BLOCK, &set, &save);
1165 ENTER;
1166 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1167 SAVEFREESV(save_sv);
1168 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1169#endif
1170 PERL_ASYNC_CHECK();
1171#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1172 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2d4fcd5e
AJ
1173#endif
1174#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1175 PL_sig_defaulting[i] = 1;
5c1546dc 1176 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1177#else
1178 (void)rsignal(i, SIG_DFL);
1179#endif
1180 if(PL_psig_name[i]) {
1181 SvREFCNT_dec(PL_psig_name[i]);
1182 PL_psig_name[i]=0;
1183 }
1184 if(PL_psig_ptr[i]) {
dd374669 1185 SV *to_dec=PL_psig_ptr[i];
2d4fcd5e
AJ
1186 PL_psig_ptr[i]=0;
1187 LEAVE;
1188 SvREFCNT_dec(to_dec);
1189 }
1190 else
1191 LEAVE;
1192 }
88e89b8a
PP
1193 }
1194 return 0;
1195}
3d37d572 1196
dd374669
AL
1197static void
1198S_raise_signal(pTHX_ int sig)
0a8e0eff
NIS
1199{
1200 /* Set a flag to say this signal is pending */
1201 PL_psig_pend[sig]++;
1202 /* And one to say _a_ signal is pending */
1203 PL_sig_pending = 1;
1204}
1205
1206Signal_t
1207Perl_csighandler(int sig)
1208{
1018e26f
NIS
1209#ifdef PERL_GET_SIG_CONTEXT
1210 dTHXa(PERL_GET_SIG_CONTEXT);
1211#else
85b332e2
CL
1212 dTHX;
1213#endif
23ada85b 1214#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1215 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1216 if (PL_sig_ignoring[sig]) return;
85b332e2 1217#endif
2e34cc90 1218#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1219 if (PL_sig_defaulting[sig])
2e34cc90
CL
1220#ifdef KILL_BY_SIGPRC
1221 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1222#else
1223 exit(1);
1224#endif
1225#endif
4ffa73a3
JH
1226 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1227 /* Call the perl level handler now--
1228 * with risk we may be in malloc() etc. */
1229 (*PL_sighandlerp)(sig);
1230 else
dd374669 1231 S_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1232}
1233
2e34cc90
CL
1234#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1235void
1236Perl_csighandler_init(void)
1237{
1238 int sig;
27da23d5 1239 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1240
1241 for (sig = 1; sig < SIG_SIZE; sig++) {
1242#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1243 dTHX;
27da23d5 1244 PL_sig_defaulting[sig] = 1;
5c1546dc 1245 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1246#endif
1247#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1248 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1249#endif
1250 }
27da23d5 1251 PL_sig_handlers_initted = 1;
2e34cc90
CL
1252}
1253#endif
1254
0a8e0eff
NIS
1255void
1256Perl_despatch_signals(pTHX)
1257{
1258 int sig;
1259 PL_sig_pending = 0;
1260 for (sig = 1; sig < SIG_SIZE; sig++) {
1261 if (PL_psig_pend[sig]) {
25da4428
JH
1262 PERL_BLOCKSIG_ADD(set, sig);
1263 PL_psig_pend[sig] = 0;
1264 PERL_BLOCKSIG_BLOCK(set);
f5203343 1265 (*PL_sighandlerp)(sig);
25da4428 1266 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1267 }
1268 }
1269}
1270
85e6fe83 1271int
864dbfa3 1272Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1273{
27da23d5 1274 dVAR;
79072805 1275 I32 i;
b7953727 1276 SV** svp = 0;
2d4fcd5e
AJ
1277 /* Need to be careful with SvREFCNT_dec(), because that can have side
1278 * effects (due to closures). We must make sure that the new disposition
1279 * is in place before it is called.
1280 */
1281 SV* to_dec = 0;
e72dc28c 1282 STRLEN len;
2d4fcd5e
AJ
1283#ifdef HAS_SIGPROCMASK
1284 sigset_t set, save;
1285 SV* save_sv;
1286#endif
a0d0e21e 1287
d5263905 1288 register const char *s = MgPV_const(mg,len);
748a9306
LW
1289 if (*s == '_') {
1290 if (strEQ(s,"__DIE__"))
3280af22 1291 svp = &PL_diehook;
748a9306 1292 else if (strEQ(s,"__WARN__"))
3280af22 1293 svp = &PL_warnhook;
748a9306 1294 else
cea2e8a9 1295 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1296 i = 0;
4633a7c4 1297 if (*svp) {
2d4fcd5e 1298 to_dec = *svp;
4633a7c4
LW
1299 *svp = 0;
1300 }
748a9306
LW
1301 }
1302 else {
1303 i = whichsig(s); /* ...no, a brick */
86d86cad 1304 if (i <= 0) {
e476b1b5 1305 if (ckWARN(WARN_SIGNAL))
9014280d 1306 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1307 return 0;
1308 }
2d4fcd5e
AJ
1309#ifdef HAS_SIGPROCMASK
1310 /* Avoid having the signal arrive at a bad time, if possible. */
1311 sigemptyset(&set);
1312 sigaddset(&set,i);
1313 sigprocmask(SIG_BLOCK, &set, &save);
1314 ENTER;
1315 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1316 SAVEFREESV(save_sv);
1317 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1318#endif
1319 PERL_ASYNC_CHECK();
2e34cc90 1320#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1321 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1322#endif
23ada85b 1323#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1324 PL_sig_ignoring[i] = 0;
85b332e2 1325#endif
2e34cc90 1326#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1327 PL_sig_defaulting[i] = 0;
2e34cc90 1328#endif
22c35a8c 1329 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1330 to_dec = PL_psig_ptr[i];
22c35a8c 1331 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1332 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1333 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1334 SvREADONLY_on(PL_psig_name[i]);
748a9306 1335 }
a0d0e21e 1336 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1337 if (i) {
5c1546dc 1338 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1339#ifdef HAS_SIGPROCMASK
1340 LEAVE;
1341#endif
1342 }
748a9306
LW
1343 else
1344 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1345 if(to_dec)
1346 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1347 return 0;
1348 }
e72dc28c 1349 s = SvPV_force(sv,len);
748a9306 1350 if (strEQ(s,"IGNORE")) {
85b332e2 1351 if (i) {
23ada85b 1352#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1353 PL_sig_ignoring[i] = 1;
5c1546dc 1354 (void)rsignal(i, PL_csighandlerp);
85b332e2 1355#else
ff68c719 1356 (void)rsignal(i, SIG_IGN);
85b332e2 1357#endif
2d4fcd5e 1358 }
748a9306
LW
1359 }
1360 else if (strEQ(s,"DEFAULT") || !*s) {
1361 if (i)
2e34cc90
CL
1362#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1363 {
27da23d5 1364 PL_sig_defaulting[i] = 1;
5c1546dc 1365 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1366 }
1367#else
ff68c719 1368 (void)rsignal(i, SIG_DFL);
2e34cc90 1369#endif
748a9306 1370 }
79072805 1371 else {
5aabfad6
PP
1372 /*
1373 * We should warn if HINT_STRICT_REFS, but without
1374 * access to a known hint bit in a known OP, we can't
1375 * tell whether HINT_STRICT_REFS is in force or not.
1376 */
46fc3d4c 1377 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1378 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1379 if (i)
5c1546dc 1380 (void)rsignal(i, PL_csighandlerp);
748a9306
LW
1381 else
1382 *svp = SvREFCNT_inc(sv);
79072805 1383 }
2d4fcd5e
AJ
1384#ifdef HAS_SIGPROCMASK
1385 if(i)
1386 LEAVE;
1387#endif
1388 if(to_dec)
1389 SvREFCNT_dec(to_dec);
79072805
LW
1390 return 0;
1391}
64ca3a65 1392#endif /* !PERL_MICRO */
79072805
LW
1393
1394int
864dbfa3 1395Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1396{
dd374669
AL
1397 (void)sv;
1398 (void)mg;
3280af22 1399 PL_sub_generation++;
463ee0b2
LW
1400 return 0;
1401}
1402
1403int
864dbfa3 1404Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1405{
dd374669
AL
1406 (void)sv;
1407 (void)mg;
a0d0e21e 1408 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1409 PL_amagic_generation++;
463ee0b2 1410
a0d0e21e
LW
1411 return 0;
1412}
463ee0b2 1413
946ec16e 1414int
864dbfa3 1415Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1416{
dd374669 1417 HV * const hv = (HV*)LvTARG(sv);
6ff81951 1418 I32 i = 0;
dd374669 1419 (void)mg;
7719e241 1420
6ff81951 1421 if (hv) {
497b47a8
JH
1422 (void) hv_iterinit(hv);
1423 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1424 i = HvKEYS(hv);
1425 else {
1426 while (hv_iternext(hv))
1427 i++;
1428 }
6ff81951
GS
1429 }
1430
1431 sv_setiv(sv, (IV)i);
1432 return 0;
1433}
1434
1435int
864dbfa3 1436Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1437{
dd374669 1438 (void)mg;
946ec16e
PP
1439 if (LvTARG(sv)) {
1440 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1441 }
1442 return 0;
ac27b0f5 1443}
946ec16e 1444
e336de0d 1445/* caller is responsible for stack switching/cleanup */
565764a8 1446STATIC int
e1ec3a88 1447S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1448{
1449 dSP;
463ee0b2 1450
924508f0
GS
1451 PUSHMARK(SP);
1452 EXTEND(SP, n);
33c27489 1453 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1454 if (n > 1) {
93965878 1455 if (mg->mg_ptr) {
565764a8 1456 if (mg->mg_len >= 0)
79cb57f6 1457 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1458 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1459 PUSHs((SV*)mg->mg_ptr);
1460 }
14befaf4 1461 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1462 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1463 }
1464 }
1465 if (n > 2) {
1466 PUSHs(val);
88e89b8a 1467 }
463ee0b2
LW
1468 PUTBACK;
1469
864dbfa3 1470 return call_method(meth, flags);
946ec16e
PP
1471}
1472
76e3520e 1473STATIC int
e1ec3a88 1474S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
a0d0e21e 1475{
27da23d5 1476 dVAR; dSP;
463ee0b2 1477
a0d0e21e
LW
1478 ENTER;
1479 SAVETMPS;
e788e7d3 1480 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1481
33c27489 1482 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1483 sv_setsv(sv, *PL_stack_sp--);
93965878 1484 }
463ee0b2 1485
d3acc0f7 1486 POPSTACK;
a0d0e21e
LW
1487 FREETMPS;
1488 LEAVE;
1489 return 0;
1490}
463ee0b2 1491
a0d0e21e 1492int
864dbfa3 1493Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1494{
a0d0e21e
LW
1495 if (mg->mg_ptr)
1496 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1497 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1498 return 0;
1499}
1500
1501int
864dbfa3 1502Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1503{
27da23d5 1504 dVAR; dSP;
a60c0954 1505 ENTER;
e788e7d3 1506 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1507 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1508 POPSTACK;
a60c0954 1509 LEAVE;
463ee0b2
LW
1510 return 0;
1511}
1512
1513int
864dbfa3 1514Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1515{
a0d0e21e
LW
1516 return magic_methpack(sv,mg,"DELETE");
1517}
463ee0b2 1518
93965878
NIS
1519
1520U32
864dbfa3 1521Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1522{
27da23d5 1523 dVAR; dSP;
93965878
NIS
1524 U32 retval = 0;
1525
1526 ENTER;
1527 SAVETMPS;
e788e7d3 1528 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1529 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1530 sv = *PL_stack_sp--;
a60c0954 1531 retval = (U32) SvIV(sv)-1;
93965878 1532 }
d3acc0f7 1533 POPSTACK;
93965878
NIS
1534 FREETMPS;
1535 LEAVE;
1536 return retval;
1537}
1538
cea2e8a9
GS
1539int
1540Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1541{
27da23d5 1542 dVAR; dSP;
463ee0b2 1543
e336de0d 1544 ENTER;
e788e7d3 1545 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1546 PUSHMARK(SP);
33c27489 1547 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1548 PUTBACK;
864dbfa3 1549 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1550 POPSTACK;
a60c0954 1551 LEAVE;
a3bcc51e 1552
463ee0b2
LW
1553 return 0;
1554}
1555
1556int
864dbfa3 1557Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1558{
27da23d5 1559 dVAR; dSP;
35a4481c 1560 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1561
1562 ENTER;
a0d0e21e 1563 SAVETMPS;
e788e7d3 1564 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1565 PUSHMARK(SP);
1566 EXTEND(SP, 2);
33c27489 1567 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1568 if (SvOK(key))
1569 PUSHs(key);
1570 PUTBACK;
1571
864dbfa3 1572 if (call_method(meth, G_SCALAR))
3280af22 1573 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1574
d3acc0f7 1575 POPSTACK;
a0d0e21e
LW
1576 FREETMPS;
1577 LEAVE;
79072805
LW
1578 return 0;
1579}
1580
1581int
864dbfa3 1582Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1583{
1584 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1585}
a0d0e21e 1586
a3bcc51e
TP
1587SV *
1588Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1589{
27da23d5 1590 dVAR; dSP;
a3bcc51e
TP
1591 SV *retval = &PL_sv_undef;
1592 SV *tied = SvTIED_obj((SV*)hv, mg);
1593 HV *pkg = SvSTASH((SV*)SvRV(tied));
1594
1595 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1596 SV *key;
bfcb3514 1597 if (HvEITER_get(hv))
a3bcc51e
TP
1598 /* we are in an iteration so the hash cannot be empty */
1599 return &PL_sv_yes;
1600 /* no xhv_eiter so now use FIRSTKEY */
1601 key = sv_newmortal();
1602 magic_nextpack((SV*)hv, mg, key);
bfcb3514 1603 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1604 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1605 }
1606
1607 /* there is a SCALAR method that we can call */
1608 ENTER;
1609 PUSHSTACKi(PERLSI_MAGIC);
1610 PUSHMARK(SP);
1611 EXTEND(SP, 1);
1612 PUSHs(tied);
1613 PUTBACK;
1614
1615 if (call_method("SCALAR", G_SCALAR))
1616 retval = *PL_stack_sp--;
1617 POPSTACK;
1618 LEAVE;
1619 return retval;
1620}
1621
a0d0e21e 1622int
864dbfa3 1623Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1624{
1625 OP *o;
1626 I32 i;
1627 GV* gv;
1628 SV** svp;
1629
3280af22 1630 gv = PL_DBline;
79072805 1631 i = SvTRUE(sv);
188ea221 1632 svp = av_fetch(GvAV(gv),
01b8bcb7 1633 atoi(MgPV_nolen_const(mg)), FALSE);
5df8de69
DM
1634 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1635 /* set or clear breakpoint in the relevant control op */
1636 if (i)
1637 o->op_flags |= OPf_SPECIAL;
1638 else
1639 o->op_flags &= ~OPf_SPECIAL;
1640 }
79072805
LW
1641 return 0;
1642}
1643
1644int
864dbfa3 1645Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1646{
83bf042f
NC
1647 AV *obj = (AV*)mg->mg_obj;
1648 if (obj) {
1649 sv_setiv(sv, AvFILL(obj) + PL_curcop->cop_arybase);
1650 } else {
1651 SvOK_off(sv);
1652 }
79072805
LW
1653 return 0;
1654}
1655
1656int
864dbfa3 1657Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1658{
83bf042f
NC
1659 AV *obj = (AV*)mg->mg_obj;
1660 if (obj) {
1661 av_fill(obj, SvIV(sv) - PL_curcop->cop_arybase);
1662 } else {
1663 if (ckWARN(WARN_MISC))
1664 Perl_warner(aTHX_ packWARN(WARN_MISC),
1665 "Attempt to set length of freed array");
1666 }
1667 return 0;
1668}
1669
1670int
1671Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1672{
94f3782b
DM
1673 /* during global destruction, mg_obj may already have been freed */
1674 if (PL_in_clean_all)
1ea47f64 1675 return 0;
94f3782b 1676
83bf042f
NC
1677 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1678
1679 if (mg) {
1680 /* arylen scalar holds a pointer back to the array, but doesn't own a
1681 reference. Hence the we (the array) are about to go away with it
1682 still pointing at us. Clear its pointer, else it would be pointing
1683 at free memory. See the comment in sv_magic about reference loops,
1684 and why it can't own a reference to us. */
1685 mg->mg_obj = 0;
1686 }
a0d0e21e
LW
1687 return 0;
1688}
1689
1690int
864dbfa3 1691Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1692{
1693 SV* lsv = LvTARG(sv);
ac27b0f5 1694
a0d0e21e 1695 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1696 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1697 if (mg && mg->mg_len >= 0) {
a0ed51b3 1698 I32 i = mg->mg_len;
7e2040f0 1699 if (DO_UTF8(lsv))
a0ed51b3
LW
1700 sv_pos_b2u(lsv, &i);
1701 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1702 return 0;
1703 }
1704 }
0c34ef67 1705 SvOK_off(sv);
a0d0e21e
LW
1706 return 0;
1707}
1708
1709int
864dbfa3 1710Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1711{
1712 SV* lsv = LvTARG(sv);
1713 SSize_t pos;
1714 STRLEN len;
c00206c8 1715 STRLEN ulen = 0;
a0d0e21e
LW
1716
1717 mg = 0;
ac27b0f5 1718
a0d0e21e 1719 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1720 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1721 if (!mg) {
1722 if (!SvOK(sv))
1723 return 0;
14befaf4
DM
1724 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1725 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1726 }
1727 else if (!SvOK(sv)) {
565764a8 1728 mg->mg_len = -1;
a0d0e21e
LW
1729 return 0;
1730 }
1731 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1732
c485e607 1733 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1734
7e2040f0 1735 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1736 ulen = sv_len_utf8(lsv);
1737 if (ulen)
1738 len = ulen;
a0ed51b3
LW
1739 }
1740
a0d0e21e
LW
1741 if (pos < 0) {
1742 pos += len;
1743 if (pos < 0)
1744 pos = 0;
1745 }
eb160463 1746 else if (pos > (SSize_t)len)
a0d0e21e 1747 pos = len;
a0ed51b3
LW
1748
1749 if (ulen) {
1750 I32 p = pos;
1751 sv_pos_u2b(lsv, &p, 0);
1752 pos = p;
1753 }
727405f8 1754
565764a8 1755 mg->mg_len = pos;
71be2cbc 1756 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1757
79072805
LW
1758 return 0;
1759}
1760
1761int
864dbfa3 1762Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1763{
dd374669 1764 (void)mg;
8646b087
PP
1765 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1766 SvFAKE_off(sv);
946ec16e 1767 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1768 SvFAKE_on(sv);
1769 }
1770 else
946ec16e 1771 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1772 return 0;
1773}
1774
1775int
864dbfa3 1776Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1777{
79072805 1778 GV* gv;
dd374669 1779 (void)mg;
7a5fd60d 1780
79072805
LW
1781 if (!SvOK(sv))
1782 return 0;
7a5fd60d 1783 gv = gv_fetchsv(sv,TRUE, SVt_PVGV);
79072805
LW
1784 if (sv == (SV*)gv)
1785 return 0;
1786 if (GvGP(sv))
88e89b8a 1787 gp_free((GV*)sv);
79072805 1788 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1789 return 0;
1790}
1791
1792int
864dbfa3 1793Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1794{
1795 STRLEN len;
35a4481c 1796 SV * const lsv = LvTARG(sv);
b83604b4 1797 const char * const tmps = SvPV_const(lsv,len);
6ff81951
GS
1798 I32 offs = LvTARGOFF(sv);
1799 I32 rem = LvTARGLEN(sv);
dd374669 1800 (void)mg;
6ff81951 1801
9aa983d2
JH
1802 if (SvUTF8(lsv))
1803 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1804 if (offs > (I32)len)
6ff81951 1805 offs = len;
eb160463 1806 if (rem + offs > (I32)len)
6ff81951
GS
1807 rem = len - offs;
1808 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1809 if (SvUTF8(lsv))
2ef4b674 1810 SvUTF8_on(sv);
6ff81951
GS
1811 return 0;
1812}
1813
1814int
864dbfa3 1815Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1816{
9aa983d2 1817 STRLEN len;
b83604b4 1818 const char *tmps = SvPV_const(sv, len);
dd374669 1819 SV * const lsv = LvTARG(sv);
9aa983d2
JH
1820 I32 lvoff = LvTARGOFF(sv);
1821 I32 lvlen = LvTARGLEN(sv);
dd374669 1822 (void)mg;
075a4a2b 1823
1aa99e6b 1824 if (DO_UTF8(sv)) {
9aa983d2
JH
1825 sv_utf8_upgrade(lsv);
1826 sv_pos_u2b(lsv, &lvoff, &lvlen);
1827 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 1828 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
1829 SvUTF8_on(lsv);
1830 }
9bf12eaf 1831 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1832 sv_pos_u2b(lsv, &lvoff, &lvlen);
b76f3ce2 1833 LvTARGLEN(sv) = len;
e95af362 1834 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1835 sv_insert(lsv, lvoff, lvlen, tmps, len);
1836 Safefree(tmps);
1aa99e6b 1837 }
b76f3ce2
GB
1838 else {
1839 sv_insert(lsv, lvoff, lvlen, tmps, len);
1840 LvTARGLEN(sv) = len;
1841 }
1842
1aa99e6b 1843
79072805
LW
1844 return 0;
1845}
1846
1847int
864dbfa3 1848Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1849{
565764a8 1850 TAINT_IF((mg->mg_len & 1) ||
155aba94 1851 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1852 return 0;
1853}
1854
1855int
864dbfa3 1856Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1857{
dd374669 1858 (void)sv;
3280af22
NIS
1859 if (PL_localizing) {
1860 if (PL_localizing == 1)
565764a8 1861 mg->mg_len <<= 1;
748a9306 1862 else
565764a8 1863 mg->mg_len >>= 1;
a0d0e21e 1864 }
3280af22 1865 else if (PL_tainted)
565764a8 1866 mg->mg_len |= 1;
748a9306 1867 else
565764a8 1868 mg->mg_len &= ~1;
463ee0b2
LW
1869 return 0;
1870}
1871
1872int
864dbfa3 1873Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1874{
35a4481c 1875 SV * const lsv = LvTARG(sv);
dd374669 1876 (void)mg;
6ff81951
GS
1877
1878 if (!lsv) {
0c34ef67 1879 SvOK_off(sv);
6ff81951
GS
1880 return 0;
1881 }
6ff81951 1882
81e118e0 1883 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1884 return 0;
1885}
1886
1887int
864dbfa3 1888Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 1889{
dd374669 1890 (void)mg;
79072805
LW
1891 do_vecset(sv); /* XXX slurp this routine */
1892 return 0;
1893}
1894
1895int
864dbfa3 1896Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1897{
71be2cbc 1898 SV *targ = Nullsv;
5f05dabc 1899 if (LvTARGLEN(sv)) {
68dc0745 1900 if (mg->mg_obj) {
74e13ce4 1901 SV *ahv = LvTARG(sv);
6d822dc4
MS
1902 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1903 if (he)
1904 targ = HeVAL(he);
68dc0745
PP
1905 }
1906 else {
3c78fafa 1907 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1908 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1909 targ = AvARRAY(av)[LvTARGOFF(sv)];
1910 }
3280af22 1911 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1912 /* somebody else defined it for us */
1913 SvREFCNT_dec(LvTARG(sv));
1914 LvTARG(sv) = SvREFCNT_inc(targ);
1915 LvTARGLEN(sv) = 0;
1916 SvREFCNT_dec(mg->mg_obj);
1917 mg->mg_obj = Nullsv;
1918 mg->mg_flags &= ~MGf_REFCOUNTED;
1919 }
5f05dabc 1920 }
71be2cbc
PP
1921 else
1922 targ = LvTARG(sv);
3280af22 1923 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1924 return 0;
1925}
1926
1927int
864dbfa3 1928Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 1929{
dd374669 1930 (void)mg;
71be2cbc 1931 if (LvTARGLEN(sv))
68dc0745
PP
1932 vivify_defelem(sv);
1933 if (LvTARG(sv)) {
5f05dabc 1934 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1935 SvSETMAGIC(LvTARG(sv));
1936 }
5f05dabc
PP
1937 return 0;
1938}
1939
71be2cbc 1940void
864dbfa3 1941Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1942{
74e13ce4
GS
1943 MAGIC *mg;
1944 SV *value = Nullsv;
71be2cbc 1945
14befaf4 1946 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1947 return;
68dc0745 1948 if (mg->mg_obj) {
74e13ce4 1949 SV *ahv = LvTARG(sv);
6d822dc4
MS
1950 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1951 if (he)
1952 value = HeVAL(he);
3280af22 1953 if (!value || value == &PL_sv_undef)
ce5030a2 1954 Perl_croak(aTHX_ PL_no_helem_sv, mg->mg_obj);
71be2cbc 1955 }
68dc0745
PP
1956 else {
1957 AV* av = (AV*)LvTARG(sv);
5aabfad6 1958 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1959 LvTARG(sv) = Nullsv; /* array can't be extended */
1960 else {
1961 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1962 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1963 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1964 }
1965 }
3e3baf6d 1966 (void)SvREFCNT_inc(value);
68dc0745
PP
1967 SvREFCNT_dec(LvTARG(sv));
1968 LvTARG(sv) = value;
71be2cbc 1969 LvTARGLEN(sv) = 0;
68dc0745
PP
1970 SvREFCNT_dec(mg->mg_obj);
1971 mg->mg_obj = Nullsv;
1972 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1973}
1974
1975int
864dbfa3 1976Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1977{
1978 AV *av = (AV*)mg->mg_obj;
1979 SV **svp = AvARRAY(av);
1980 I32 i = AvFILLp(av);
dd374669
AL
1981 (void)sv;
1982
810b8aa5 1983 while (i >= 0) {
fdc9a813 1984 if (svp[i]) {
810b8aa5 1985 if (!SvWEAKREF(svp[i]))
cea2e8a9 1986 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5 1987 /* XXX Should we check that it hasn't changed? */
b162af07 1988 SvRV_set(svp[i], 0);
0c34ef67 1989 SvOK_off(svp[i]);
810b8aa5 1990 SvWEAKREF_off(svp[i]);
fdc9a813 1991 svp[i] = Nullsv;
810b8aa5
GS
1992 }
1993 i--;
1994 }
d99b02a1 1995 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5
GS
1996 return 0;
1997}
1998
1999int
864dbfa3 2000Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2001{
565764a8 2002 mg->mg_len = -1;
c6496cc7 2003 SvSCREAM_off(sv);
93a17b20
LW
2004 return 0;
2005}
2006
2007int
864dbfa3 2008Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 2009{
dd374669 2010 (void)mg;
14befaf4 2011 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
2012 SvVALID_off(sv);
2013 return 0;
2014}
2015
2016int
864dbfa3 2017Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 2018{
dd374669 2019 (void)mg;
14befaf4 2020 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
2021 SvCOMPILED_off(sv);
2022 return 0;
2023}
2024
2025int
864dbfa3 2026Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2027{
35a4481c 2028 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
2029
2030 if (uf && uf->uf_set)
24f81a43 2031 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2032 return 0;
2033}
2034
c277df42 2035int
faf82a0b
AE
2036Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2037{
dd374669 2038 (void)mg;
faf82a0b
AE
2039 sv_unmagic(sv, PERL_MAGIC_qr);
2040 return 0;
2041}
2042
2043int
864dbfa3 2044Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
2045{
2046 regexp *re = (regexp *)mg->mg_obj;
2047 ReREFCNT_dec(re);
dd374669 2048 (void)sv;
c277df42
IZ
2049 return 0;
2050}
2051
7a4c00b4 2052#ifdef USE_LOCALE_COLLATE
79072805 2053int
864dbfa3 2054Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
2055{
2056 /*
838b5b74 2057 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2058 * and vanished with a faint plop.
2059 */
dd374669 2060 (void)sv;
7a4c00b4
PP
2061 if (mg->mg_ptr) {
2062 Safefree(mg->mg_ptr);
2063 mg->mg_ptr = NULL;
565764a8 2064 mg->mg_len = -1;
7a4c00b4 2065 }
bbce6d69
PP
2066 return 0;
2067}
7a4c00b4 2068#endif /* USE_LOCALE_COLLATE */
bbce6d69 2069
7e8c5dac
HS
2070/* Just clear the UTF-8 cache data. */
2071int
2072Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2073{
dd374669 2074 (void)sv;
7e8c5dac
HS
2075 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2076 mg->mg_ptr = 0;
2077 mg->mg_len = -1; /* The mg_len holds the len cache. */
2078 return 0;
2079}
2080
bbce6d69 2081int
864dbfa3 2082Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2083{
e1ec3a88 2084 register const char *s;
79072805 2085 I32 i;
8990e307 2086 STRLEN len;
79072805 2087 switch (*mg->mg_ptr) {
748a9306 2088 case '\001': /* ^A */
3280af22 2089 sv_setsv(PL_bodytarget, sv);
748a9306 2090 break;
49460fe6 2091 case '\003': /* ^C */
eb160463 2092 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
2093 break;
2094
79072805 2095 case '\004': /* ^D */
b4ab917c 2096#ifdef DEBUGGING
b83604b4 2097 s = SvPV_nolen_const(sv);
ddcf8bc1 2098 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
79072805 2099 DEBUG_x(dump_all());
b4ab917c
DM
2100#else
2101 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2102#endif
79072805 2103 break;
28f23441 2104 case '\005': /* ^E */
d0063567 2105 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2106#ifdef MACOS_TRADITIONAL
d0063567 2107 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2108#else
cd39f2b6 2109# ifdef VMS
d0063567 2110 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2111# else
cd39f2b6 2112# ifdef WIN32
d0063567 2113 SetLastError( SvIV(sv) );
cd39f2b6 2114# else
9fed8b87 2115# ifdef OS2
d0063567 2116 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2117# else
d0063567
DK
2118 /* will anyone ever use this? */
2119 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2120# endif
048c1ddf
IZ
2121# endif
2122# endif
22fae026 2123#endif
d0063567
DK
2124 }
2125 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2126 if (PL_encoding)
2127 SvREFCNT_dec(PL_encoding);
2128 if (SvOK(sv) || SvGMAGICAL(sv)) {
2129 PL_encoding = newSVsv(sv);
2130 }
2131 else {
2132 PL_encoding = Nullsv;
2133 }
2134 }
2135 break;
79072805 2136 case '\006': /* ^F */
3280af22 2137 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2138 break;
a0d0e21e 2139 case '\010': /* ^H */
3280af22 2140 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2141 break;
9d116dd7 2142 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2143 if (PL_inplace)
2144 Safefree(PL_inplace);
79072805 2145 if (SvOK(sv))
2e0de35c 2146 PL_inplace = savesvpv(sv);
79072805 2147 else
3280af22 2148 PL_inplace = Nullch;
79072805 2149 break;
28f23441 2150 case '\017': /* ^O */
ac27b0f5 2151 if (*(mg->mg_ptr+1) == '\0') {
1bf29663 2152 if (PL_osname) {
ac27b0f5 2153 Safefree(PL_osname);
1bf29663
DM
2154 PL_osname = Nullch;
2155 }
3511154c
DM
2156 if (SvOK(sv)) {
2157 TAINT_PROPER("assigning to $^O");
2e0de35c 2158 PL_osname = savesvpv(sv);
3511154c 2159 }
ac27b0f5
NIS
2160 }
2161 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2162 if (!PL_compiling.cop_io)
2163 PL_compiling.cop_io = newSVsv(sv);
2164 else
2165 sv_setsv(PL_compiling.cop_io,sv);
2166 }
28f23441 2167 break;
79072805 2168 case '\020': /* ^P */
3280af22 2169 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
f2a7f298 2170 if (PL_perldb && !PL_DBsingle)
1ee4443e 2171 init_debugger();
79072805
LW
2172 break;
2173 case '\024': /* ^T */
88e89b8a 2174#ifdef BIG_TIME
6b88bc9c 2175 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2176#else
3280af22 2177 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2178#endif
79072805 2179 break;
fde18df1 2180 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2181 if (*(mg->mg_ptr+1) == '\0') {
2182 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2183 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2184 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2185 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2186 }
599cee73 2187 }
0a378802 2188 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2189 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2190 if (!SvPOK(sv) && PL_localizing) {
2191 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2192 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2193 break;
2194 }
f4fc7782 2195 {
b5477537 2196 STRLEN len, i;
d3a7d8c7 2197 int accumulate = 0 ;
f4fc7782 2198 int any_fatals = 0 ;
b83604b4 2199 const char * const ptr = SvPV_const(sv, len) ;
f4fc7782
JH
2200 for (i = 0 ; i < len ; ++i) {
2201 accumulate |= ptr[i] ;
2202 any_fatals |= (ptr[i] & 0xAA) ;
2203 }
d3a7d8c7
GS
2204 if (!accumulate)
2205 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2206 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2207 PL_compiling.cop_warnings = pWARN_ALL;
2208 PL_dowarn |= G_WARN_ONCE ;
727405f8 2209 }
d3a7d8c7
GS
2210 else {
2211 if (specialWARN(PL_compiling.cop_warnings))
2212 PL_compiling.cop_warnings = newSVsv(sv) ;
2213 else
2214 sv_setsv(PL_compiling.cop_warnings, sv);
2215 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2216 PL_dowarn |= G_WARN_ONCE ;
2217 }
f4fc7782 2218
d3a7d8c7 2219 }
4438c4b7 2220 }
971a9dd3 2221 }
79072805
LW
2222 break;
2223 case '.':
3280af22
NIS
2224 if (PL_localizing) {
2225 if (PL_localizing == 1)
7766f137 2226 SAVESPTR(PL_last_in_gv);
748a9306 2227 }
3280af22 2228 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2229 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2230 break;
2231 case '^':
3280af22 2232 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2233 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
7a5fd60d 2234 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
79072805
LW
2235 break;
2236 case '~':
3280af22 2237 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2238 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
7a5fd60d 2239 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
79072805
LW
2240 break;
2241 case '=':
632db599 2242 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2243 break;
2244 case '-':
632db599 2245 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2246 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2247 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2248 break;
2249 case '%':
632db599 2250 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2251 break;
2252 case '|':
4b65379b 2253 {
3280af22 2254 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2255 if(!io)
2256 break;
4b65379b
CS
2257 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2258 IoFLAGS(io) &= ~IOf_FLUSH;
2259 else {
2260 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2261 PerlIO *ofp = IoOFP(io);
2262 if (ofp)
2263 (void)PerlIO_flush(ofp);
2264 IoFLAGS(io) |= IOf_FLUSH;
2265 }
2266 }
79072805
LW
2267 }
2268 break;
79072805 2269 case '/':
3280af22 2270 SvREFCNT_dec(PL_rs);
8bfdd7d9 2271 PL_rs = newSVsv(sv);
79072805
LW
2272 break;
2273 case '\\':
7889fe52
NIS
2274 if (PL_ors_sv)
2275 SvREFCNT_dec(PL_ors_sv);
009c130f 2276 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2277 PL_ors_sv = newSVsv(sv);
009c130f 2278 }
e3c19b7b 2279 else {
7889fe52 2280 PL_ors_sv = Nullsv;
e3c19b7b 2281 }
79072805
LW
2282 break;
2283 case ',':
7889fe52
NIS
2284 if (PL_ofs_sv)
2285 SvREFCNT_dec(PL_ofs_sv);
2286 if (SvOK(sv) || SvGMAGICAL(sv)) {
2287 PL_ofs_sv = newSVsv(sv);
2288 }
2289 else {
2290 PL_ofs_sv = Nullsv;
2291 }
79072805 2292 break;
79072805 2293 case '[':
3280af22 2294 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2295 break;
2296 case '?':
ff0cee69 2297#ifdef COMPLEX_STATUS
6b88bc9c
GS
2298 if (PL_localizing == 2) {
2299 PL_statusvalue = LvTARGOFF(sv);
2300 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2301 }
2302 else
2303#endif
2304#ifdef VMSISH_STATUS
2305 if (VMSISH_STATUS)
2306 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2307 else
2308#endif
e5218da5 2309 STATUS_UNIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2310 break;
2311 case '!':
93189314
JH
2312 {
2313#ifdef VMS
2314# define PERL_VMS_BANG vaxc$errno
2315#else
2316# define PERL_VMS_BANG 0
2317#endif
91487cfc 2318 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2319 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2320 }
79072805
LW
2321 break;
2322 case '<':
3280af22
NIS
2323 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2324 if (PL_delaymagic) {
2325 PL_delaymagic |= DM_RUID;
79072805
LW
2326 break; /* don't do magic till later */
2327 }
2328#ifdef HAS_SETRUID
b28d0864 2329 (void)setruid((Uid_t)PL_uid);
79072805
LW
2330#else
2331#ifdef HAS_SETREUID
3280af22 2332 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2333#else
85e6fe83 2334#ifdef HAS_SETRESUID
b28d0864 2335 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2336#else
75870ed3 2337 if (PL_uid == PL_euid) { /* special case $< = $> */
2338#ifdef PERL_DARWIN
2339 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2340 if (PL_uid != 0 && PerlProc_getuid() == 0)
2341 (void)PerlProc_setuid(0);
2342#endif
b28d0864 2343 (void)PerlProc_setuid(PL_uid);
75870ed3 2344 } else {
d8eceb89 2345 PL_uid = PerlProc_getuid();
cea2e8a9 2346 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2347 }
79072805
LW
2348#endif
2349#endif
85e6fe83 2350#endif
d8eceb89 2351 PL_uid = PerlProc_getuid();
3280af22 2352 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2353 break;
2354 case '>':
3280af22
NIS
2355 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2356 if (PL_delaymagic) {
2357 PL_delaymagic |= DM_EUID;
79072805
LW
2358 break; /* don't do magic till later */
2359 }
2360#ifdef HAS_SETEUID
3280af22 2361 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2362#else
2363#ifdef HAS_SETREUID
b28d0864 2364 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2365#else
2366#ifdef HAS_SETRESUID
6b88bc9c 2367 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2368#else
b28d0864
NIS
2369 if (PL_euid == PL_uid) /* special case $> = $< */
2370 PerlProc_setuid(PL_euid);
a0d0e21e 2371 else {
e8ee3774 2372 PL_euid = PerlProc_geteuid();
cea2e8a9 2373 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2374 }
79072805
LW
2375#endif
2376#endif
85e6fe83 2377#endif
d8eceb89 2378 PL_euid = PerlProc_geteuid();
3280af22 2379 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2380 break;
2381 case '(':
3280af22
NIS
2382 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2383 if (PL_delaymagic) {
2384 PL_delaymagic |= DM_RGID;
79072805
LW
2385 break; /* don't do magic till later */
2386 }
2387#ifdef HAS_SETRGID
b28d0864 2388 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2389#else
2390#ifdef HAS_SETREGID
3280af22 2391 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2392#else
2393#ifdef HAS_SETRESGID
b28d0864 2394 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2395#else
b28d0864
NIS
2396 if (PL_gid == PL_egid) /* special case $( = $) */
2397 (void)PerlProc_setgid(PL_gid);
748a9306 2398 else {
d8eceb89 2399 PL_gid = PerlProc_getgid();
cea2e8a9 2400 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2401 }
79072805
LW
2402#endif
2403#endif
85e6fe83 2404#endif
d8eceb89 2405 PL_gid = PerlProc_getgid();
3280af22 2406 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2407 break;
2408 case ')':
5cd24f17
PP
2409#ifdef HAS_SETGROUPS
2410 {
b83604b4 2411 const char *p = SvPV_const(sv, len);
5cd24f17
PP
2412 Groups_t gary[NGROUPS];
2413
5cd24f17
PP
2414 while (isSPACE(*p))
2415 ++p;
2d4389e4 2416 PL_egid = Atol(p);
5cd24f17
PP
2417 for (i = 0; i < NGROUPS; ++i) {
2418 while (*p && !isSPACE(*p))
2419 ++p;
2420 while (isSPACE(*p))
2421 ++p;
2422 if (!*p)
2423 break;
2d4389e4 2424 gary[i] = Atol(p);
5cd24f17 2425 }
8cc95fdb
PP
2426 if (i)
2427 (void)setgroups(i, gary);
5cd24f17
PP
2428 }
2429#else /* HAS_SETGROUPS */
b28d0864 2430 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2431#endif /* HAS_SETGROUPS */
3280af22
NIS
2432 if (PL_delaymagic) {
2433 PL_delaymagic |= DM_EGID;
79072805
LW
2434 break; /* don't do magic till later */
2435 }
2436#ifdef HAS_SETEGID
3280af22 2437 (void)setegid((Gid_t)PL_egid);
79072805
LW
2438#else
2439#ifdef HAS_SETREGID
b28d0864 2440 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2441#else
2442#ifdef HAS_SETRESGID
b28d0864 2443 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2444#else
b28d0864
NIS
2445 if (PL_egid == PL_gid) /* special case $) = $( */
2446 (void)PerlProc_setgid(PL_egid);
748a9306 2447 else {
d8eceb89 2448 PL_egid = PerlProc_getegid();
cea2e8a9 2449 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2450 }
79072805
LW
2451#endif
2452#endif
85e6fe83 2453#endif
d8eceb89 2454 PL_egid = PerlProc_getegid();
3280af22 2455 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2456 break;
2457 case ':':
2d8e6c8d 2458 PL_chopset = SvPV_force(sv,len);
79072805 2459 break;
cd39f2b6 2460#ifndef MACOS_TRADITIONAL
79072805 2461 case '0':
e2975953 2462 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2463#ifdef HAS_SETPROCTITLE
2464 /* The BSDs don't show the argv[] in ps(1) output, they
2465 * show a string from the process struct and provide
2466 * the setproctitle() routine to manipulate that. */
2467 {
b83604b4 2468 s = SvPV_const(sv, len);
98b76f99 2469# if __FreeBSD_version > 410001
9aad2c0e 2470 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2471 * but not the "(perl) suffix from the ps(1)
2472 * output, because that's what ps(1) shows if the
2473 * argv[] is modified. */
6f2ad931 2474 setproctitle("-%s", s);
9aad2c0e 2475# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2476 /* This doesn't really work if you assume that
2477 * $0 = 'foobar'; will wipe out 'perl' from the $0
2478 * because in ps(1) output the result will be like
2479 * sprintf("perl: %s (perl)", s)
2480 * I guess this is a security feature:
2481 * one (a user process) cannot get rid of the original name.
2482 * --jhi */
2483 setproctitle("%s", s);
2484# endif
2485 }
2486#endif
17aa7f3d
JH
2487#if defined(__hpux) && defined(PSTAT_SETCMD)
2488 {
2489 union pstun un;
b83604b4 2490 s = SvPV_const(sv, len);
6867be6d 2491 un.pst_command = (char *)s;
17aa7f3d
JH
2492 pstat(PSTAT_SETCMD, un, len, 0, 0);
2493 }
2494#endif
3cb9023d 2495 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2496 s = SvPV_force(sv,len);
6f698202
AMS
2497 if (len >= (STRLEN)PL_origalen-1) {
2498 /* Longer than original, will be truncated. We assume that
2499 * PL_origalen bytes are available. */
2500 Copy(s, PL_origargv[0], PL_origalen-1, char);
79072805
LW
2501 }
2502 else {
54bfe034
JH
2503 /* Shorter than original, will be padded. */
2504 Copy(s, PL_origargv[0], len, char);
2505 PL_origargv[0][len] = 0;
2506 memset(PL_origargv[0] + len + 1,
2507 /* Is the space counterintuitive? Yes.
2508 * (You were expecting \0?)
3cb9023d 2509 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2510 * --jhi */
2511 (int)' ',
2512 PL_origalen - len - 1);
79072805 2513 }
ad7eccf4
JD
2514 PL_origargv[0][PL_origalen-1] = 0;
2515 for (i = 1; i < PL_origargc; i++)
2516 PL_origargv[i] = 0;
e2975953 2517 UNLOCK_DOLLARZERO_MUTEX;
79072805 2518 break;
cd39f2b6 2519#endif
79072805
LW
2520 }
2521 return 0;
2522}
2523
2524I32
35a4481c 2525Perl_whichsig(pTHX_ const char *sig)
79072805 2526{
aadb217d 2527 register char* const* sigv;
79072805 2528
aadb217d 2529 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
79072805 2530 if (strEQ(sig,*sigv))
aadb217d 2531 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805
LW
2532#ifdef SIGCLD
2533 if (strEQ(sig,"CHLD"))
2534 return SIGCLD;
2535#endif
2536#ifdef SIGCHLD
2537 if (strEQ(sig,"CLD"))
2538 return SIGCHLD;
2539#endif
7f1236c0 2540 return -1;
79072805
LW
2541}
2542
ecfc5424 2543Signal_t
cea2e8a9 2544Perl_sighandler(int sig)
79072805 2545{
1018e26f
NIS
2546#ifdef PERL_GET_SIG_CONTEXT
2547 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2548#else
cea2e8a9 2549 dTHX;
71d280e3 2550#endif
79072805 2551 dSP;
00d579c5 2552 GV *gv = Nullgv;
a0d0e21e 2553 HV *st;
b7953727 2554 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2555 CV *cv = Nullcv;
533c011a 2556 OP *myop = PL_op;
84902520 2557 U32 flags = 0;
3280af22 2558 XPV *tXpv = PL_Xpv;
71d280e3 2559
3280af22 2560 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2561 flags |= 1;
3280af22 2562 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2563 flags |= 4;
3280af22 2564 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2565 flags |= 16;
2566
727405f8 2567 if (!PL_psig_ptr[sig]) {
99ef548b 2568 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2569 PL_sig_name[sig]);
2570 exit(sig);
2571 }
ff0cee69 2572
84902520
TB
2573 /* Max number of items pushed there is 3*n or 4. We cannot fix
2574 infinity, so we fix 4 (in fact 5): */
2575 if (flags & 1) {
3280af22 2576 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2577 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2578 }
ac27b0f5 2579 if (flags & 4)
3280af22 2580 PL_markstack_ptr++; /* Protect mark. */
84902520 2581 if (flags & 16)
3280af22 2582 PL_scopestack_ix += 1;
84902520 2583 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2584 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2585 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2586 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2587
a0d0e21e 2588 if (!cv || !CvROOT(cv)) {
599cee73 2589 if (ckWARN(WARN_SIGNAL))
9014280d 2590 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2591 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2592 : ((cv && CvGV(cv))
2593 ? GvENAME(CvGV(cv))
2594 : "__ANON__")));
2595 goto cleanup;
79072805
LW
2596 }
2597
22c35a8c
GS
2598 if(PL_psig_name[sig]) {
2599 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2600 flags |= 64;
df3728a2 2601#if !defined(PERL_IMPLICIT_CONTEXT)
27da23d5 2602 PL_sig_sv = sv;
df3728a2 2603#endif
84902520 2604 } else {
ff0cee69 2605 sv = sv_newmortal();
22c35a8c 2606 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2607 }
e336de0d 2608
e788e7d3 2609 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2610 PUSHMARK(SP);
79072805 2611 PUSHs(sv);
79072805 2612 PUTBACK;
a0d0e21e 2613
1b266415 2614 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2615
d3acc0f7 2616 POPSTACK;
1b266415 2617 if (SvTRUE(ERRSV)) {
1d615522 2618#ifndef PERL_MICRO
983dbef6 2619#ifdef HAS_SIGPROCMASK
1b266415
NIS
2620 /* Handler "died", for example to get out of a restart-able read().
2621 * Before we re-do that on its behalf re-enable the signal which was
2622 * blocked by the system when we entered.
2623 */
2624 sigset_t set;
2625 sigemptyset(&set);
2626 sigaddset(&set,sig);
2627 sigprocmask(SIG_UNBLOCK, &set, NULL);
2628#else
2629 /* Not clear if this will work */
2630 (void)rsignal(sig, SIG_IGN);
5c1546dc 2631 (void)rsignal(sig, PL_csighandlerp);
1b266415 2632#endif
1d615522 2633#endif /* !PERL_MICRO */
5df617be 2634 DieNull;
1b266415 2635 }
00d579c5 2636cleanup:
84902520 2637 if (flags & 1)
3280af22 2638 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2639 if (flags & 4)
3280af22 2640 PL_markstack_ptr--;
84902520 2641 if (flags & 16)
3280af22 2642 PL_scopestack_ix -= 1;
84902520
TB
2643 if (flags & 64)
2644 SvREFCNT_dec(sv);
533c011a 2645 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2646
3280af22
NIS
2647 PL_Sv = tSv; /* Restore global temporaries. */
2648 PL_Xpv = tXpv;
53bb94e2 2649 return;
79072805 2650}
4e35701f
NIS
2651
2652
51371543 2653static void
35a4481c 2654restore_magic(pTHX_ const void *p)
51371543 2655{
48944bdf 2656 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2657 SV* sv = mgs->mgs_sv;
2658
2659 if (!sv)
2660 return;
2661
2662 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2663 {
f8c7b90f 2664#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
2665 /* While magic was saved (and off) sv_setsv may well have seen
2666 this SV as a prime candidate for COW. */
2667 if (SvIsCOW(sv))
2668 sv_force_normal(sv);
2669#endif
2670
51371543
GS
2671 if (mgs->mgs_flags)
2672 SvFLAGS(sv) |= mgs->mgs_flags;
2673 else
2674 mg_magical(sv);
2675 if (SvGMAGICAL(sv))
2676 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2677 }
2678
2679 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2680
2681 /* If we're still on top of the stack, pop us off. (That condition
2682 * will be satisfied if restore_magic was called explicitly, but *not*
2683 * if it's being called via leave_scope.)
2684 * The reason for doing this is that otherwise, things like sv_2cv()
2685 * may leave alloc gunk on the savestack, and some code
2686 * (e.g. sighandler) doesn't expect that...
2687 */
2688 if (PL_savestack_ix == mgs->mgs_ss_ix)
2689 {
2690 I32 popval = SSPOPINT;
c76ac1ee 2691 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2692 PL_savestack_ix -= 2;
2693 popval = SSPOPINT;
2694 assert(popval == SAVEt_ALLOC);
2695 popval = SSPOPINT;
2696 PL_savestack_ix -= popval;
2697 }
2698
2699}
2700
2701static void
35a4481c 2702unwind_handler_stack(pTHX_ const void *p)
51371543 2703{
27da23d5 2704 dVAR;
e1ec3a88 2705 const U32 flags = *(const U32*)p;
51371543
GS
2706
2707 if (flags & 1)
2708 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2709 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2710#if !defined(PERL_IMPLICIT_CONTEXT)
51371543 2711 if (flags & 64)
27da23d5 2712 SvREFCNT_dec(PL_sig_sv);
df3728a2 2713#endif
51371543 2714}
1018e26f 2715
66610fdd
RGS
2716/*
2717 * Local variables:
2718 * c-indentation-style: bsd
2719 * c-basic-offset: 4
2720 * indent-tabs-mode: t
2721 * End:
2722 *
37442d52
RGS
2723 * ex: set ts=8 sts=4 sw=4 noet:
2724 */