This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't ignore errors from an eval.
[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)) {
ffc61ed2 496 char *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
LW
881 break;
882 case '#':
3280af22 883 sv_setpv(sv,PL_ofmt);
79072805
LW
884 break;
885 case '!':
a5f75d66 886#ifdef VMS
65202027 887 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 888 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 889#else
88e89b8a
PP
890 {
891 int saveerrno = errno;
65202027 892 sv_setnv(sv, (NV)errno);
88e89b8a 893#ifdef OS2
ed344e4f
IZ
894 if (errno == errno_isOS2 || errno == errno_isOS2_set)
895 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 896 else
a5f75d66 897#endif
2304df62 898 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
899 errno = saveerrno;
900 }
901#endif
946ec16e 902 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
903 break;
904 case '<':
3280af22 905 sv_setiv(sv, (IV)PL_uid);
79072805
LW
906 break;
907 case '>':
3280af22 908 sv_setiv(sv, (IV)PL_euid);
79072805
LW
909 break;
910 case '(':
3280af22 911 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 912#ifdef HAS_GETGROUPS
a3b680e6 913 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_gid);
a52cb5f7 914#endif
79072805
LW
915 goto add_groups;
916 case ')':
3280af22 917 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 918#ifdef HAS_GETGROUPS
a3b680e6 919 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_egid);
a52cb5f7 920#endif
79072805 921 add_groups:
79072805 922#ifdef HAS_GETGROUPS
79072805 923 {
a0d0e21e 924 Groups_t gary[NGROUPS];
a3b680e6
AL
925 I32 j = getgroups(NGROUPS,gary);
926 while (--j >= 0)
927 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, (long unsigned int)gary[j]);
79072805
LW
928 }
929#endif
155aba94 930 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 931 break;
cd39f2b6 932#ifndef MACOS_TRADITIONAL
79072805
LW
933 case '0':
934 break;
cd39f2b6 935#endif
79072805 936 }
a0d0e21e 937 return 0;
79072805
LW
938}
939
940int
864dbfa3 941Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
942{
943 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
944
945 if (uf && uf->uf_val)
24f81a43 946 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
947 return 0;
948}
949
950int
864dbfa3 951Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 952{
27da23d5 953 dVAR;
79072805 954 register char *s;
e62f0680 955 const char *ptr;
5aabfad6 956 STRLEN len, klen;
1e422769 957
a0d0e21e 958 s = SvPV(sv,len);
e62f0680 959 ptr = MgPV_const(mg,klen);
88e89b8a 960 my_setenv(ptr, s);
1e422769 961
a0d0e21e
LW
962#ifdef DYNAMIC_ENV_FETCH
963 /* We just undefd an environment var. Is a replacement */
964 /* waiting in the wings? */
965 if (!len) {
5aabfad6 966 SV **valp;
6b88bc9c 967 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 968 s = SvPV(*valp, len);
a0d0e21e
LW
969 }
970#endif
1e422769 971
39e571d4 972#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
973 /* And you'll never guess what the dog had */
974 /* in its mouth... */
3280af22 975 if (PL_tainting) {
1e422769
PP
976 MgTAINTEDDIR_off(mg);
977#ifdef VMS
5aabfad6 978 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 979 char pathbuf[256], eltbuf[256], *cp, *elt = s;
c623ac67 980 Stat_t sbuf;
1e422769
PP
981 int i = 0, j = 0;
982
983 do { /* DCL$PATH may be a search list */
984 while (1) { /* as may dev portion of any element */
985 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
986 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
987 cando_by_name(S_IWUSR,0,elt) ) {
988 MgTAINTEDDIR_on(mg);
989 return 0;
990 }
991 }
992 if ((cp = strchr(elt, ':')) != Nullch)
993 *cp = '\0';
994 if (my_trnlnm(elt, eltbuf, j++))
995 elt = eltbuf;
996 else
997 break;
998 }
999 j = 0;
1000 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1001 }
1002#endif /* VMS */
5aabfad6 1003 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 1004 char *strend = s + len;
463ee0b2
LW
1005
1006 while (s < strend) {
96827780 1007 char tmpbuf[256];
c623ac67 1008 Stat_t st;
5f74f29c 1009 I32 i;
96827780 1010 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 1011 s, strend, ':', &i);
463ee0b2 1012 s++;
96827780
MB
1013 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1014 || *tmpbuf != '/'
c6ed36e1 1015 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1016 MgTAINTEDDIR_on(mg);
1e422769
PP
1017 return 0;
1018 }
463ee0b2 1019 }
79072805
LW
1020 }
1021 }
39e571d4 1022#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1023
79072805
LW
1024 return 0;
1025}
1026
1027int
864dbfa3 1028Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1029{
dd374669 1030 (void)sv;
01b8bcb7 1031 my_setenv(MgPV_nolen_const(mg),Nullch);
85e6fe83
LW
1032 return 0;
1033}
1034
88e89b8a 1035int
864dbfa3 1036Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 1037{
27da23d5 1038#if defined(VMS) || defined(EPOC) || defined(SYMBIAN)
cea2e8a9 1039 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1040#else
3280af22 1041 if (PL_localizing) {
fb73857a 1042 HE* entry;
2d8e6c8d 1043 STRLEN n_a;
fb73857a
PP
1044 magic_clear_all_env(sv,mg);
1045 hv_iterinit((HV*)sv);
155aba94 1046 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1047 I32 keylen;
1048 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 1049 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a
PP
1050 }
1051 }
1052#endif
1053 return 0;
1054}
1055
1056int
864dbfa3 1057Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1058{
27da23d5 1059 dVAR;
2f42fcb0 1060#ifndef PERL_MICRO
27da23d5 1061#if defined(VMS) || defined(EPOC) || defined(SYMBIAN)
cea2e8a9 1062 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 1063#else
4efc5df6 1064# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137 1065 PerlEnv_clearenv();
4efc5df6
GS
1066# else
1067# ifdef USE_ENVIRON_ARRAY
1068# if defined(USE_ITHREADS)
1069 /* only the parent thread can clobber the process environment */
1070 if (PL_curinterp == aTHX)
1071# endif
1072 {
1073# ifndef PERL_USE_SAFE_PUTENV
50acdf95 1074 if (!PL_use_safe_putenv) {
66b1d557
HM
1075 I32 i;
1076
3280af22 1077 if (environ == PL_origenviron)
f2517201 1078 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
1079 else
1080 for (i = 0; environ[i]; i++)
f2517201 1081 safesysfree(environ[i]);
50acdf95 1082 }
4efc5df6 1083# endif /* PERL_USE_SAFE_PUTENV */
f2517201 1084
66b1d557 1085 environ[0] = Nullch;
4efc5df6
GS
1086 }
1087# endif /* USE_ENVIRON_ARRAY */
052c2dc6 1088# endif /* PERL_IMPLICIT_SYS || WIN32 */
2f42fcb0
JH
1089#endif /* VMS || EPOC */
1090#endif /* !PERL_MICRO */
dd374669
AL
1091 (void)sv;
1092 (void)mg;
3e3baf6d 1093 return 0;
66b1d557
HM
1094}
1095
64ca3a65 1096#ifndef PERL_MICRO
2d4fcd5e
AJ
1097#ifdef HAS_SIGPROCMASK
1098static void
1099restore_sigmask(pTHX_ SV *save_sv)
1100{
1101 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1102 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1103}
1104#endif
66b1d557 1105int
864dbfa3 1106Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1107{
1108 I32 i;
1109 /* Are we fetching a signal entry? */
01b8bcb7 1110 i = whichsig(MgPV_nolen_const(mg));
e02bfb16 1111 if (i > 0) {
22c35a8c
GS
1112 if(PL_psig_ptr[i])
1113 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1114 else {
85b332e2 1115 Sighandler_t sigstate;
2e34cc90 1116 sigstate = rsignal_state(i);
23ada85b 1117#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1118 if (PL_sig_handlers_initted && PL_sig_ignoring[i]) sigstate = SIG_IGN;
2e34cc90
CL
1119#endif
1120#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1121 if (PL_sig_handlers_initted && PL_sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1122#endif
88e89b8a 1123 /* cache state so we don't fetch it again */
ff68c719 1124 if(sigstate == SIG_IGN)
88e89b8a
PP
1125 sv_setpv(sv,"IGNORE");
1126 else
3280af22 1127 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1128 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1129 SvTEMP_off(sv);
1130 }
1131 }
1132 return 0;
1133}
1134int
864dbfa3 1135Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1136{
2d4fcd5e
AJ
1137 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1138 * refactoring might be in order.
1139 */
27da23d5 1140 dVAR;
01b8bcb7 1141 register const char *s = MgPV_nolen_const(mg);
dd374669 1142 (void)sv;
2d4fcd5e 1143 if (*s == '_') {
27da23d5 1144 SV** svp = 0;
2d4fcd5e
AJ
1145 if (strEQ(s,"__DIE__"))
1146 svp = &PL_diehook;
1147 else if (strEQ(s,"__WARN__"))
1148 svp = &PL_warnhook;
1149 else
1150 Perl_croak(aTHX_ "No such hook: %s", s);
27da23d5 1151 if (svp && *svp) {
dd374669 1152 SV *to_dec = *svp;
2d4fcd5e
AJ
1153 *svp = 0;
1154 SvREFCNT_dec(to_dec);
1155 }
1156 }
1157 else {
1158 I32 i;
1159 /* Are we clearing a signal entry? */
1160 i = whichsig(s);
e02bfb16 1161 if (i > 0) {
2d4fcd5e
AJ
1162#ifdef HAS_SIGPROCMASK
1163 sigset_t set, save;
1164 SV* save_sv;
1165 /* Avoid having the signal arrive at a bad time, if possible. */
1166 sigemptyset(&set);
1167 sigaddset(&set,i);
1168 sigprocmask(SIG_BLOCK, &set, &save);
1169 ENTER;
1170 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1171 SAVEFREESV(save_sv);
1172 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1173#endif
1174 PERL_ASYNC_CHECK();
1175#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1176 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2d4fcd5e
AJ
1177#endif
1178#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1179 PL_sig_defaulting[i] = 1;
5c1546dc 1180 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1181#else
1182 (void)rsignal(i, SIG_DFL);
1183#endif
1184 if(PL_psig_name[i]) {
1185 SvREFCNT_dec(PL_psig_name[i]);
1186 PL_psig_name[i]=0;
1187 }
1188 if(PL_psig_ptr[i]) {
dd374669 1189 SV *to_dec=PL_psig_ptr[i];
2d4fcd5e
AJ
1190 PL_psig_ptr[i]=0;
1191 LEAVE;
1192 SvREFCNT_dec(to_dec);
1193 }
1194 else
1195 LEAVE;
1196 }
88e89b8a
PP
1197 }
1198 return 0;
1199}
3d37d572 1200
dd374669
AL
1201static void
1202S_raise_signal(pTHX_ int sig)
0a8e0eff
NIS
1203{
1204 /* Set a flag to say this signal is pending */
1205 PL_psig_pend[sig]++;
1206 /* And one to say _a_ signal is pending */
1207 PL_sig_pending = 1;
1208}
1209
1210Signal_t
1211Perl_csighandler(int sig)
1212{
1018e26f
NIS
1213#ifdef PERL_GET_SIG_CONTEXT
1214 dTHXa(PERL_GET_SIG_CONTEXT);
1215#else
85b332e2
CL
1216 dTHX;
1217#endif
23ada85b 1218#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1219 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1220 if (PL_sig_ignoring[sig]) return;
85b332e2 1221#endif
2e34cc90 1222#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1223 if (PL_sig_defaulting[sig])
2e34cc90
CL
1224#ifdef KILL_BY_SIGPRC
1225 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1226#else
1227 exit(1);
1228#endif
1229#endif
4ffa73a3
JH
1230 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1231 /* Call the perl level handler now--
1232 * with risk we may be in malloc() etc. */
1233 (*PL_sighandlerp)(sig);
1234 else
dd374669 1235 S_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1236}
1237
2e34cc90
CL
1238#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1239void
1240Perl_csighandler_init(void)
1241{
1242 int sig;
27da23d5 1243 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1244
1245 for (sig = 1; sig < SIG_SIZE; sig++) {
1246#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1247 dTHX;
27da23d5 1248 PL_sig_defaulting[sig] = 1;
5c1546dc 1249 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1250#endif
1251#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1252 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1253#endif
1254 }
27da23d5 1255 PL_sig_handlers_initted = 1;
2e34cc90
CL
1256}
1257#endif
1258
0a8e0eff
NIS
1259void
1260Perl_despatch_signals(pTHX)
1261{
1262 int sig;
1263 PL_sig_pending = 0;
1264 for (sig = 1; sig < SIG_SIZE; sig++) {
1265 if (PL_psig_pend[sig]) {
25da4428
JH
1266 PERL_BLOCKSIG_ADD(set, sig);
1267 PL_psig_pend[sig] = 0;
1268 PERL_BLOCKSIG_BLOCK(set);
f5203343 1269 (*PL_sighandlerp)(sig);
25da4428 1270 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1271 }
1272 }
1273}
1274
85e6fe83 1275int
864dbfa3 1276Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1277{
27da23d5 1278 dVAR;
79072805 1279 I32 i;
b7953727 1280 SV** svp = 0;
2d4fcd5e
AJ
1281 /* Need to be careful with SvREFCNT_dec(), because that can have side
1282 * effects (due to closures). We must make sure that the new disposition
1283 * is in place before it is called.
1284 */
1285 SV* to_dec = 0;
e72dc28c 1286 STRLEN len;
2d4fcd5e
AJ
1287#ifdef HAS_SIGPROCMASK
1288 sigset_t set, save;
1289 SV* save_sv;
1290#endif
a0d0e21e 1291
d5263905 1292 register const char *s = MgPV_const(mg,len);
748a9306
LW
1293 if (*s == '_') {
1294 if (strEQ(s,"__DIE__"))
3280af22 1295 svp = &PL_diehook;
748a9306 1296 else if (strEQ(s,"__WARN__"))
3280af22 1297 svp = &PL_warnhook;
748a9306 1298 else
cea2e8a9 1299 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1300 i = 0;
4633a7c4 1301 if (*svp) {
2d4fcd5e 1302 to_dec = *svp;
4633a7c4
LW
1303 *svp = 0;
1304 }
748a9306
LW
1305 }
1306 else {
1307 i = whichsig(s); /* ...no, a brick */
86d86cad 1308 if (i <= 0) {
e476b1b5 1309 if (ckWARN(WARN_SIGNAL))
9014280d 1310 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1311 return 0;
1312 }
2d4fcd5e
AJ
1313#ifdef HAS_SIGPROCMASK
1314 /* Avoid having the signal arrive at a bad time, if possible. */
1315 sigemptyset(&set);
1316 sigaddset(&set,i);
1317 sigprocmask(SIG_BLOCK, &set, &save);
1318 ENTER;
1319 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1320 SAVEFREESV(save_sv);
1321 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1322#endif
1323 PERL_ASYNC_CHECK();
2e34cc90 1324#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1325 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1326#endif
23ada85b 1327#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1328 PL_sig_ignoring[i] = 0;
85b332e2 1329#endif
2e34cc90 1330#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1331 PL_sig_defaulting[i] = 0;
2e34cc90 1332#endif
22c35a8c 1333 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1334 to_dec = PL_psig_ptr[i];
22c35a8c 1335 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1336 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1337 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1338 SvREADONLY_on(PL_psig_name[i]);
748a9306 1339 }
a0d0e21e 1340 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1341 if (i) {
5c1546dc 1342 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1343#ifdef HAS_SIGPROCMASK
1344 LEAVE;
1345#endif
1346 }
748a9306
LW
1347 else
1348 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1349 if(to_dec)
1350 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1351 return 0;
1352 }
e72dc28c 1353 s = SvPV_force(sv,len);
748a9306 1354 if (strEQ(s,"IGNORE")) {
85b332e2 1355 if (i) {
23ada85b 1356#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1357 PL_sig_ignoring[i] = 1;
5c1546dc 1358 (void)rsignal(i, PL_csighandlerp);
85b332e2 1359#else
ff68c719 1360 (void)rsignal(i, SIG_IGN);
85b332e2 1361#endif
2d4fcd5e 1362 }
748a9306
LW
1363 }
1364 else if (strEQ(s,"DEFAULT") || !*s) {
1365 if (i)
2e34cc90
CL
1366#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1367 {
27da23d5 1368 PL_sig_defaulting[i] = 1;
5c1546dc 1369 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1370 }
1371#else
ff68c719 1372 (void)rsignal(i, SIG_DFL);
2e34cc90 1373#endif
748a9306 1374 }
79072805 1375 else {
5aabfad6
PP
1376 /*
1377 * We should warn if HINT_STRICT_REFS, but without
1378 * access to a known hint bit in a known OP, we can't
1379 * tell whether HINT_STRICT_REFS is in force or not.
1380 */
46fc3d4c 1381 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1382 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1383 if (i)
5c1546dc 1384 (void)rsignal(i, PL_csighandlerp);
748a9306
LW
1385 else
1386 *svp = SvREFCNT_inc(sv);
79072805 1387 }
2d4fcd5e
AJ
1388#ifdef HAS_SIGPROCMASK
1389 if(i)
1390 LEAVE;
1391#endif
1392 if(to_dec)
1393 SvREFCNT_dec(to_dec);
79072805
LW
1394 return 0;
1395}
64ca3a65 1396#endif /* !PERL_MICRO */
79072805
LW
1397
1398int
864dbfa3 1399Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1400{
dd374669
AL
1401 (void)sv;
1402 (void)mg;
3280af22 1403 PL_sub_generation++;
463ee0b2
LW
1404 return 0;
1405}
1406
1407int
864dbfa3 1408Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1409{
dd374669
AL
1410 (void)sv;
1411 (void)mg;
a0d0e21e 1412 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1413 PL_amagic_generation++;
463ee0b2 1414
a0d0e21e
LW
1415 return 0;
1416}
463ee0b2 1417
946ec16e 1418int
864dbfa3 1419Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1420{
dd374669 1421 HV * const hv = (HV*)LvTARG(sv);
6ff81951 1422 I32 i = 0;
dd374669 1423 (void)mg;
7719e241 1424
6ff81951 1425 if (hv) {
497b47a8
JH
1426 (void) hv_iterinit(hv);
1427 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1428 i = HvKEYS(hv);
1429 else {
1430 while (hv_iternext(hv))
1431 i++;
1432 }
6ff81951
GS
1433 }
1434
1435 sv_setiv(sv, (IV)i);
1436 return 0;
1437}
1438
1439int
864dbfa3 1440Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1441{
dd374669 1442 (void)mg;
946ec16e
PP
1443 if (LvTARG(sv)) {
1444 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1445 }
1446 return 0;
ac27b0f5 1447}
946ec16e 1448
e336de0d 1449/* caller is responsible for stack switching/cleanup */
565764a8 1450STATIC int
e1ec3a88 1451S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1452{
1453 dSP;
463ee0b2 1454
924508f0
GS
1455 PUSHMARK(SP);
1456 EXTEND(SP, n);
33c27489 1457 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1458 if (n > 1) {
93965878 1459 if (mg->mg_ptr) {
565764a8 1460 if (mg->mg_len >= 0)
79cb57f6 1461 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1462 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1463 PUSHs((SV*)mg->mg_ptr);
1464 }
14befaf4 1465 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1466 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1467 }
1468 }
1469 if (n > 2) {
1470 PUSHs(val);
88e89b8a 1471 }
463ee0b2
LW
1472 PUTBACK;
1473
864dbfa3 1474 return call_method(meth, flags);
946ec16e
PP
1475}
1476
76e3520e 1477STATIC int
e1ec3a88 1478S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
a0d0e21e 1479{
27da23d5 1480 dVAR; dSP;
463ee0b2 1481
a0d0e21e
LW
1482 ENTER;
1483 SAVETMPS;
e788e7d3 1484 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1485
33c27489 1486 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1487 sv_setsv(sv, *PL_stack_sp--);
93965878 1488 }
463ee0b2 1489
d3acc0f7 1490 POPSTACK;
a0d0e21e
LW
1491 FREETMPS;
1492 LEAVE;
1493 return 0;
1494}
463ee0b2 1495
a0d0e21e 1496int
864dbfa3 1497Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1498{
a0d0e21e
LW
1499 if (mg->mg_ptr)
1500 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1501 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1502 return 0;
1503}
1504
1505int
864dbfa3 1506Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1507{
27da23d5 1508 dVAR; dSP;
a60c0954 1509 ENTER;
e788e7d3 1510 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1511 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1512 POPSTACK;
a60c0954 1513 LEAVE;
463ee0b2
LW
1514 return 0;
1515}
1516
1517int
864dbfa3 1518Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1519{
a0d0e21e
LW
1520 return magic_methpack(sv,mg,"DELETE");
1521}
463ee0b2 1522
93965878
NIS
1523
1524U32
864dbfa3 1525Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1526{
27da23d5 1527 dVAR; dSP;
93965878
NIS
1528 U32 retval = 0;
1529
1530 ENTER;
1531 SAVETMPS;
e788e7d3 1532 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1533 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1534 sv = *PL_stack_sp--;
a60c0954 1535 retval = (U32) SvIV(sv)-1;
93965878 1536 }
d3acc0f7 1537 POPSTACK;
93965878
NIS
1538 FREETMPS;
1539 LEAVE;
1540 return retval;
1541}
1542
cea2e8a9
GS
1543int
1544Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1545{
27da23d5 1546 dVAR; dSP;
463ee0b2 1547
e336de0d 1548 ENTER;
e788e7d3 1549 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1550 PUSHMARK(SP);
33c27489 1551 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1552 PUTBACK;
864dbfa3 1553 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1554 POPSTACK;
a60c0954 1555 LEAVE;
a3bcc51e 1556
463ee0b2
LW
1557 return 0;
1558}
1559
1560int
864dbfa3 1561Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1562{
27da23d5 1563 dVAR; dSP;
35a4481c 1564 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1565
1566 ENTER;
a0d0e21e 1567 SAVETMPS;
e788e7d3 1568 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1569 PUSHMARK(SP);
1570 EXTEND(SP, 2);
33c27489 1571 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1572 if (SvOK(key))
1573 PUSHs(key);
1574 PUTBACK;
1575
864dbfa3 1576 if (call_method(meth, G_SCALAR))
3280af22 1577 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1578
d3acc0f7 1579 POPSTACK;
a0d0e21e
LW
1580 FREETMPS;
1581 LEAVE;
79072805
LW
1582 return 0;
1583}
1584
1585int
864dbfa3 1586Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1587{
1588 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1589}
a0d0e21e 1590
a3bcc51e
TP
1591SV *
1592Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1593{
27da23d5 1594 dVAR; dSP;
a3bcc51e
TP
1595 SV *retval = &PL_sv_undef;
1596 SV *tied = SvTIED_obj((SV*)hv, mg);
1597 HV *pkg = SvSTASH((SV*)SvRV(tied));
1598
1599 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1600 SV *key;
bfcb3514 1601 if (HvEITER_get(hv))
a3bcc51e
TP
1602 /* we are in an iteration so the hash cannot be empty */
1603 return &PL_sv_yes;
1604 /* no xhv_eiter so now use FIRSTKEY */
1605 key = sv_newmortal();
1606 magic_nextpack((SV*)hv, mg, key);
bfcb3514 1607 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1608 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1609 }
1610
1611 /* there is a SCALAR method that we can call */
1612 ENTER;
1613 PUSHSTACKi(PERLSI_MAGIC);
1614 PUSHMARK(SP);
1615 EXTEND(SP, 1);
1616 PUSHs(tied);
1617 PUTBACK;
1618
1619 if (call_method("SCALAR", G_SCALAR))
1620 retval = *PL_stack_sp--;
1621 POPSTACK;
1622 LEAVE;
1623 return retval;
1624}
1625
a0d0e21e 1626int
864dbfa3 1627Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1628{
1629 OP *o;
1630 I32 i;
1631 GV* gv;
1632 SV** svp;
1633
3280af22 1634 gv = PL_DBline;
79072805 1635 i = SvTRUE(sv);
188ea221 1636 svp = av_fetch(GvAV(gv),
01b8bcb7 1637 atoi(MgPV_nolen_const(mg)), FALSE);
5df8de69
DM
1638 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1639 /* set or clear breakpoint in the relevant control op */
1640 if (i)
1641 o->op_flags |= OPf_SPECIAL;
1642 else
1643 o->op_flags &= ~OPf_SPECIAL;
1644 }
79072805
LW
1645 return 0;
1646}
1647
1648int
864dbfa3 1649Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1650{
3280af22 1651 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1652 return 0;
1653}
1654
1655int
864dbfa3 1656Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1657{
3280af22 1658 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1659 return 0;
1660}
1661
1662int
864dbfa3 1663Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1664{
1665 SV* lsv = LvTARG(sv);
ac27b0f5 1666
a0d0e21e 1667 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1668 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1669 if (mg && mg->mg_len >= 0) {
a0ed51b3 1670 I32 i = mg->mg_len;
7e2040f0 1671 if (DO_UTF8(lsv))
a0ed51b3
LW
1672 sv_pos_b2u(lsv, &i);
1673 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1674 return 0;
1675 }
1676 }
0c34ef67 1677 SvOK_off(sv);
a0d0e21e
LW
1678 return 0;
1679}
1680
1681int
864dbfa3 1682Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1683{
1684 SV* lsv = LvTARG(sv);
1685 SSize_t pos;
1686 STRLEN len;
c00206c8 1687 STRLEN ulen = 0;
a0d0e21e
LW
1688
1689 mg = 0;
ac27b0f5 1690
a0d0e21e 1691 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1692 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1693 if (!mg) {
1694 if (!SvOK(sv))
1695 return 0;
14befaf4
DM
1696 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1697 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1698 }
1699 else if (!SvOK(sv)) {
565764a8 1700 mg->mg_len = -1;
a0d0e21e
LW
1701 return 0;
1702 }
1703 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1704
c485e607 1705 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1706
7e2040f0 1707 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1708 ulen = sv_len_utf8(lsv);
1709 if (ulen)
1710 len = ulen;
a0ed51b3
LW
1711 }
1712
a0d0e21e
LW
1713 if (pos < 0) {
1714 pos += len;
1715 if (pos < 0)
1716 pos = 0;
1717 }
eb160463 1718 else if (pos > (SSize_t)len)
a0d0e21e 1719 pos = len;
a0ed51b3
LW
1720
1721 if (ulen) {
1722 I32 p = pos;
1723 sv_pos_u2b(lsv, &p, 0);
1724 pos = p;
1725 }
727405f8 1726
565764a8 1727 mg->mg_len = pos;
71be2cbc 1728 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1729
79072805
LW
1730 return 0;
1731}
1732
1733int
864dbfa3 1734Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1735{
dd374669 1736 (void)mg;
8646b087
PP
1737 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1738 SvFAKE_off(sv);
946ec16e 1739 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1740 SvFAKE_on(sv);
1741 }
1742 else
946ec16e 1743 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1744 return 0;
1745}
1746
1747int
864dbfa3 1748Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1749{
79072805 1750 GV* gv;
dd374669 1751 (void)mg;
7a5fd60d 1752
79072805
LW
1753 if (!SvOK(sv))
1754 return 0;
7a5fd60d 1755 gv = gv_fetchsv(sv,TRUE, SVt_PVGV);
79072805
LW
1756 if (sv == (SV*)gv)
1757 return 0;
1758 if (GvGP(sv))
88e89b8a 1759 gp_free((GV*)sv);
79072805 1760 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1761 return 0;
1762}
1763
1764int
864dbfa3 1765Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1766{
1767 STRLEN len;
35a4481c
AL
1768 SV * const lsv = LvTARG(sv);
1769 const char * const tmps = SvPV(lsv,len);
6ff81951
GS
1770 I32 offs = LvTARGOFF(sv);
1771 I32 rem = LvTARGLEN(sv);
dd374669 1772 (void)mg;
6ff81951 1773
9aa983d2
JH
1774 if (SvUTF8(lsv))
1775 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1776 if (offs > (I32)len)
6ff81951 1777 offs = len;
eb160463 1778 if (rem + offs > (I32)len)
6ff81951
GS
1779 rem = len - offs;
1780 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1781 if (SvUTF8(lsv))
2ef4b674 1782 SvUTF8_on(sv);
6ff81951
GS
1783 return 0;
1784}
1785
1786int
864dbfa3 1787Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1788{
9aa983d2
JH
1789 STRLEN len;
1790 char *tmps = SvPV(sv, len);
dd374669 1791 SV * const lsv = LvTARG(sv);
9aa983d2
JH
1792 I32 lvoff = LvTARGOFF(sv);
1793 I32 lvlen = LvTARGLEN(sv);
dd374669 1794 (void)mg;
075a4a2b 1795
1aa99e6b 1796 if (DO_UTF8(sv)) {
9aa983d2
JH
1797 sv_utf8_upgrade(lsv);
1798 sv_pos_u2b(lsv, &lvoff, &lvlen);
1799 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 1800 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
1801 SvUTF8_on(lsv);
1802 }
9bf12eaf 1803 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1804 sv_pos_u2b(lsv, &lvoff, &lvlen);
b76f3ce2 1805 LvTARGLEN(sv) = len;
e95af362 1806 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1807 sv_insert(lsv, lvoff, lvlen, tmps, len);
1808 Safefree(tmps);
1aa99e6b 1809 }
b76f3ce2
GB
1810 else {
1811 sv_insert(lsv, lvoff, lvlen, tmps, len);
1812 LvTARGLEN(sv) = len;
1813 }
1814
1aa99e6b 1815
79072805
LW
1816 return 0;
1817}
1818
1819int
864dbfa3 1820Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1821{
565764a8 1822 TAINT_IF((mg->mg_len & 1) ||
155aba94 1823 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1824 return 0;
1825}
1826
1827int
864dbfa3 1828Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1829{
dd374669 1830 (void)sv;
3280af22
NIS
1831 if (PL_localizing) {
1832 if (PL_localizing == 1)
565764a8 1833 mg->mg_len <<= 1;
748a9306 1834 else
565764a8 1835 mg->mg_len >>= 1;
a0d0e21e 1836 }
3280af22 1837 else if (PL_tainted)
565764a8 1838 mg->mg_len |= 1;
748a9306 1839 else
565764a8 1840 mg->mg_len &= ~1;
463ee0b2
LW
1841 return 0;
1842}
1843
1844int
864dbfa3 1845Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1846{
35a4481c 1847 SV * const lsv = LvTARG(sv);
dd374669 1848 (void)mg;
6ff81951
GS
1849
1850 if (!lsv) {
0c34ef67 1851 SvOK_off(sv);
6ff81951
GS
1852 return 0;
1853 }
6ff81951 1854
81e118e0 1855 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1856 return 0;
1857}
1858
1859int
864dbfa3 1860Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 1861{
dd374669 1862 (void)mg;
79072805
LW
1863 do_vecset(sv); /* XXX slurp this routine */
1864 return 0;
1865}
1866
1867int
864dbfa3 1868Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1869{
71be2cbc 1870 SV *targ = Nullsv;
5f05dabc 1871 if (LvTARGLEN(sv)) {
68dc0745 1872 if (mg->mg_obj) {
74e13ce4 1873 SV *ahv = LvTARG(sv);
6d822dc4
MS
1874 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1875 if (he)
1876 targ = HeVAL(he);
68dc0745
PP
1877 }
1878 else {
3c78fafa 1879 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1880 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1881 targ = AvARRAY(av)[LvTARGOFF(sv)];
1882 }
3280af22 1883 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1884 /* somebody else defined it for us */
1885 SvREFCNT_dec(LvTARG(sv));
1886 LvTARG(sv) = SvREFCNT_inc(targ);
1887 LvTARGLEN(sv) = 0;
1888 SvREFCNT_dec(mg->mg_obj);
1889 mg->mg_obj = Nullsv;
1890 mg->mg_flags &= ~MGf_REFCOUNTED;
1891 }
5f05dabc 1892 }
71be2cbc
PP
1893 else
1894 targ = LvTARG(sv);
3280af22 1895 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1896 return 0;
1897}
1898
1899int
864dbfa3 1900Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 1901{
dd374669 1902 (void)mg;
71be2cbc 1903 if (LvTARGLEN(sv))
68dc0745
PP
1904 vivify_defelem(sv);
1905 if (LvTARG(sv)) {
5f05dabc 1906 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1907 SvSETMAGIC(LvTARG(sv));
1908 }
5f05dabc
PP
1909 return 0;
1910}
1911
71be2cbc 1912void
864dbfa3 1913Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1914{
74e13ce4
GS
1915 MAGIC *mg;
1916 SV *value = Nullsv;
71be2cbc 1917
14befaf4 1918 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1919 return;
68dc0745 1920 if (mg->mg_obj) {
74e13ce4 1921 SV *ahv = LvTARG(sv);
6d822dc4
MS
1922 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1923 if (he)
1924 value = HeVAL(he);
3280af22 1925 if (!value || value == &PL_sv_undef)
ce5030a2 1926 Perl_croak(aTHX_ PL_no_helem_sv, mg->mg_obj);
71be2cbc 1927 }
68dc0745
PP
1928 else {
1929 AV* av = (AV*)LvTARG(sv);
5aabfad6 1930 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1931 LvTARG(sv) = Nullsv; /* array can't be extended */
1932 else {
1933 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1934 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1935 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1936 }
1937 }
3e3baf6d 1938 (void)SvREFCNT_inc(value);
68dc0745
PP
1939 SvREFCNT_dec(LvTARG(sv));
1940 LvTARG(sv) = value;
71be2cbc 1941 LvTARGLEN(sv) = 0;
68dc0745
PP
1942 SvREFCNT_dec(mg->mg_obj);
1943 mg->mg_obj = Nullsv;
1944 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1945}
1946
1947int
864dbfa3 1948Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1949{
1950 AV *av = (AV*)mg->mg_obj;
1951 SV **svp = AvARRAY(av);
1952 I32 i = AvFILLp(av);
dd374669
AL
1953 (void)sv;
1954
810b8aa5 1955 while (i >= 0) {
fdc9a813 1956 if (svp[i]) {
810b8aa5 1957 if (!SvWEAKREF(svp[i]))
cea2e8a9 1958 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5 1959 /* XXX Should we check that it hasn't changed? */
b162af07 1960 SvRV_set(svp[i], 0);
0c34ef67 1961 SvOK_off(svp[i]);
810b8aa5 1962 SvWEAKREF_off(svp[i]);
fdc9a813 1963 svp[i] = Nullsv;
810b8aa5
GS
1964 }
1965 i--;
1966 }
d99b02a1 1967 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5
GS
1968 return 0;
1969}
1970
1971int
864dbfa3 1972Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1973{
565764a8 1974 mg->mg_len = -1;
c6496cc7 1975 SvSCREAM_off(sv);
93a17b20
LW
1976 return 0;
1977}
1978
1979int
864dbfa3 1980Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1981{
dd374669 1982 (void)mg;
14befaf4 1983 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1984 SvVALID_off(sv);
1985 return 0;
1986}
1987
1988int
864dbfa3 1989Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1990{
dd374669 1991 (void)mg;
14befaf4 1992 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1993 SvCOMPILED_off(sv);
1994 return 0;
1995}
1996
1997int
864dbfa3 1998Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1999{
35a4481c 2000 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
2001
2002 if (uf && uf->uf_set)
24f81a43 2003 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2004 return 0;
2005}
2006
c277df42 2007int
faf82a0b
AE
2008Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2009{
dd374669 2010 (void)mg;
faf82a0b
AE
2011 sv_unmagic(sv, PERL_MAGIC_qr);
2012 return 0;
2013}
2014
2015int
864dbfa3 2016Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
2017{
2018 regexp *re = (regexp *)mg->mg_obj;
2019 ReREFCNT_dec(re);
dd374669 2020 (void)sv;
c277df42
IZ
2021 return 0;
2022}
2023
7a4c00b4 2024#ifdef USE_LOCALE_COLLATE
79072805 2025int
864dbfa3 2026Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
2027{
2028 /*
838b5b74 2029 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2030 * and vanished with a faint plop.
2031 */
dd374669 2032 (void)sv;
7a4c00b4
PP
2033 if (mg->mg_ptr) {
2034 Safefree(mg->mg_ptr);
2035 mg->mg_ptr = NULL;
565764a8 2036 mg->mg_len = -1;
7a4c00b4 2037 }
bbce6d69
PP
2038 return 0;
2039}
7a4c00b4 2040#endif /* USE_LOCALE_COLLATE */
bbce6d69 2041
7e8c5dac
HS
2042/* Just clear the UTF-8 cache data. */
2043int
2044Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2045{
dd374669 2046 (void)sv;
7e8c5dac
HS
2047 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2048 mg->mg_ptr = 0;
2049 mg->mg_len = -1; /* The mg_len holds the len cache. */
2050 return 0;
2051}
2052
bbce6d69 2053int
864dbfa3 2054Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2055{
e1ec3a88 2056 register const char *s;
79072805 2057 I32 i;
8990e307 2058 STRLEN len;
79072805 2059 switch (*mg->mg_ptr) {
748a9306 2060 case '\001': /* ^A */
3280af22 2061 sv_setsv(PL_bodytarget, sv);
748a9306 2062 break;
49460fe6 2063 case '\003': /* ^C */
eb160463 2064 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
2065 break;
2066
79072805 2067 case '\004': /* ^D */
b4ab917c
DM
2068#ifdef DEBUGGING
2069 s = SvPV_nolen(sv);
ddcf8bc1 2070 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
79072805 2071 DEBUG_x(dump_all());
b4ab917c
DM
2072#else
2073 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2074#endif
79072805 2075 break;
28f23441 2076 case '\005': /* ^E */
d0063567 2077 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2078#ifdef MACOS_TRADITIONAL
d0063567 2079 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2080#else
cd39f2b6 2081# ifdef VMS
d0063567 2082 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2083# else
cd39f2b6 2084# ifdef WIN32
d0063567 2085 SetLastError( SvIV(sv) );
cd39f2b6 2086# else
9fed8b87 2087# ifdef OS2
d0063567 2088 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2089# else
d0063567
DK
2090 /* will anyone ever use this? */
2091 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2092# endif
048c1ddf
IZ
2093# endif
2094# endif
22fae026 2095#endif
d0063567
DK
2096 }
2097 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2098 if (PL_encoding)
2099 SvREFCNT_dec(PL_encoding);
2100 if (SvOK(sv) || SvGMAGICAL(sv)) {
2101 PL_encoding = newSVsv(sv);
2102 }
2103 else {
2104 PL_encoding = Nullsv;
2105 }
2106 }
2107 break;
79072805 2108 case '\006': /* ^F */
3280af22 2109 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2110 break;
a0d0e21e 2111 case '\010': /* ^H */
3280af22 2112 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2113 break;
9d116dd7 2114 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2115 if (PL_inplace)
2116 Safefree(PL_inplace);
79072805 2117 if (SvOK(sv))
2e0de35c 2118 PL_inplace = savesvpv(sv);
79072805 2119 else
3280af22 2120 PL_inplace = Nullch;
79072805 2121 break;
28f23441 2122 case '\017': /* ^O */
ac27b0f5 2123 if (*(mg->mg_ptr+1) == '\0') {
1bf29663 2124 if (PL_osname) {
ac27b0f5 2125 Safefree(PL_osname);
1bf29663
DM
2126 PL_osname = Nullch;
2127 }
3511154c
DM
2128 if (SvOK(sv)) {
2129 TAINT_PROPER("assigning to $^O");
2e0de35c 2130 PL_osname = savesvpv(sv);
3511154c 2131 }
ac27b0f5
NIS
2132 }
2133 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2134 if (!PL_compiling.cop_io)
2135 PL_compiling.cop_io = newSVsv(sv);
2136 else
2137 sv_setsv(PL_compiling.cop_io,sv);
2138 }
28f23441 2139 break;
79072805 2140 case '\020': /* ^P */
3280af22 2141 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
f2a7f298 2142 if (PL_perldb && !PL_DBsingle)
1ee4443e 2143 init_debugger();
79072805
LW
2144 break;
2145 case '\024': /* ^T */
88e89b8a 2146#ifdef BIG_TIME
6b88bc9c 2147 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2148#else
3280af22 2149 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2150#endif
79072805 2151 break;
fde18df1 2152 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2153 if (*(mg->mg_ptr+1) == '\0') {
2154 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2155 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2156 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2157 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2158 }
599cee73 2159 }
0a378802 2160 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2161 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2162 if (!SvPOK(sv) && PL_localizing) {
2163 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2164 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2165 break;
2166 }
f4fc7782 2167 {
b5477537 2168 STRLEN len, i;
d3a7d8c7 2169 int accumulate = 0 ;
f4fc7782 2170 int any_fatals = 0 ;
35a4481c 2171 const char * const ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
2172 for (i = 0 ; i < len ; ++i) {
2173 accumulate |= ptr[i] ;
2174 any_fatals |= (ptr[i] & 0xAA) ;
2175 }
d3a7d8c7
GS
2176 if (!accumulate)
2177 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2178 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2179 PL_compiling.cop_warnings = pWARN_ALL;
2180 PL_dowarn |= G_WARN_ONCE ;
727405f8 2181 }
d3a7d8c7
GS
2182 else {
2183 if (specialWARN(PL_compiling.cop_warnings))
2184 PL_compiling.cop_warnings = newSVsv(sv) ;
2185 else
2186 sv_setsv(PL_compiling.cop_warnings, sv);
2187 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2188 PL_dowarn |= G_WARN_ONCE ;
2189 }
f4fc7782 2190
d3a7d8c7 2191 }
4438c4b7 2192 }
971a9dd3 2193 }
79072805
LW
2194 break;
2195 case '.':
3280af22
NIS
2196 if (PL_localizing) {
2197 if (PL_localizing == 1)
7766f137 2198 SAVESPTR(PL_last_in_gv);
748a9306 2199 }
3280af22 2200 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2201 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2202 break;
2203 case '^':
3280af22 2204 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2205 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
7a5fd60d 2206 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
79072805
LW
2207 break;
2208 case '~':
3280af22 2209 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2210 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
7a5fd60d 2211 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv,TRUE, SVt_PVIO);
79072805
LW
2212 break;
2213 case '=':
632db599 2214 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2215 break;
2216 case '-':
632db599 2217 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2218 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2219 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2220 break;
2221 case '%':
632db599 2222 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2223 break;
2224 case '|':
4b65379b 2225 {
3280af22 2226 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2227 if(!io)
2228 break;
4b65379b
CS
2229 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2230 IoFLAGS(io) &= ~IOf_FLUSH;
2231 else {
2232 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2233 PerlIO *ofp = IoOFP(io);
2234 if (ofp)
2235 (void)PerlIO_flush(ofp);
2236 IoFLAGS(io) |= IOf_FLUSH;
2237 }
2238 }
79072805
LW
2239 }
2240 break;
79072805 2241 case '/':
3280af22 2242 SvREFCNT_dec(PL_rs);
8bfdd7d9 2243 PL_rs = newSVsv(sv);
79072805
LW
2244 break;
2245 case '\\':
7889fe52
NIS
2246 if (PL_ors_sv)
2247 SvREFCNT_dec(PL_ors_sv);
009c130f 2248 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2249 PL_ors_sv = newSVsv(sv);
009c130f 2250 }
e3c19b7b 2251 else {
7889fe52 2252 PL_ors_sv = Nullsv;
e3c19b7b 2253 }
79072805
LW
2254 break;
2255 case ',':
7889fe52
NIS
2256 if (PL_ofs_sv)
2257 SvREFCNT_dec(PL_ofs_sv);
2258 if (SvOK(sv) || SvGMAGICAL(sv)) {
2259 PL_ofs_sv = newSVsv(sv);
2260 }
2261 else {
2262 PL_ofs_sv = Nullsv;
2263 }
79072805
LW
2264 break;
2265 case '#':
3280af22
NIS
2266 if (PL_ofmt)
2267 Safefree(PL_ofmt);
2e0de35c 2268 PL_ofmt = savesvpv(sv);
79072805
LW
2269 break;
2270 case '[':
3280af22 2271 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2272 break;
2273 case '?':
ff0cee69 2274#ifdef COMPLEX_STATUS
6b88bc9c
GS
2275 if (PL_localizing == 2) {
2276 PL_statusvalue = LvTARGOFF(sv);
2277 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2278 }
2279 else
2280#endif
2281#ifdef VMSISH_STATUS
2282 if (VMSISH_STATUS)
2283 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2284 else
2285#endif
e5218da5 2286 STATUS_UNIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2287 break;
2288 case '!':
93189314
JH
2289 {
2290#ifdef VMS
2291# define PERL_VMS_BANG vaxc$errno
2292#else
2293# define PERL_VMS_BANG 0
2294#endif
91487cfc 2295 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2296 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2297 }
79072805
LW
2298 break;
2299 case '<':
3280af22
NIS
2300 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2301 if (PL_delaymagic) {
2302 PL_delaymagic |= DM_RUID;
79072805
LW
2303 break; /* don't do magic till later */
2304 }
2305#ifdef HAS_SETRUID
b28d0864 2306 (void)setruid((Uid_t)PL_uid);
79072805
LW
2307#else
2308#ifdef HAS_SETREUID
3280af22 2309 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2310#else
85e6fe83 2311#ifdef HAS_SETRESUID
b28d0864 2312 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2313#else
75870ed3 2314 if (PL_uid == PL_euid) { /* special case $< = $> */
2315#ifdef PERL_DARWIN
2316 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2317 if (PL_uid != 0 && PerlProc_getuid() == 0)
2318 (void)PerlProc_setuid(0);
2319#endif
b28d0864 2320 (void)PerlProc_setuid(PL_uid);
75870ed3 2321 } else {
d8eceb89 2322 PL_uid = PerlProc_getuid();
cea2e8a9 2323 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2324 }
79072805
LW
2325#endif
2326#endif
85e6fe83 2327#endif
d8eceb89 2328 PL_uid = PerlProc_getuid();
3280af22 2329 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2330 break;
2331 case '>':
3280af22
NIS
2332 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2333 if (PL_delaymagic) {
2334 PL_delaymagic |= DM_EUID;
79072805
LW
2335 break; /* don't do magic till later */
2336 }
2337#ifdef HAS_SETEUID
3280af22 2338 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2339#else
2340#ifdef HAS_SETREUID
b28d0864 2341 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2342#else
2343#ifdef HAS_SETRESUID
6b88bc9c 2344 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2345#else
b28d0864
NIS
2346 if (PL_euid == PL_uid) /* special case $> = $< */
2347 PerlProc_setuid(PL_euid);
a0d0e21e 2348 else {
e8ee3774 2349 PL_euid = PerlProc_geteuid();
cea2e8a9 2350 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2351 }
79072805
LW
2352#endif
2353#endif
85e6fe83 2354#endif
d8eceb89 2355 PL_euid = PerlProc_geteuid();
3280af22 2356 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2357 break;
2358 case '(':
3280af22
NIS
2359 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2360 if (PL_delaymagic) {
2361 PL_delaymagic |= DM_RGID;
79072805
LW
2362 break; /* don't do magic till later */
2363 }
2364#ifdef HAS_SETRGID
b28d0864 2365 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2366#else
2367#ifdef HAS_SETREGID
3280af22 2368 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2369#else
2370#ifdef HAS_SETRESGID
b28d0864 2371 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2372#else
b28d0864
NIS
2373 if (PL_gid == PL_egid) /* special case $( = $) */
2374 (void)PerlProc_setgid(PL_gid);
748a9306 2375 else {
d8eceb89 2376 PL_gid = PerlProc_getgid();
cea2e8a9 2377 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2378 }
79072805
LW
2379#endif
2380#endif
85e6fe83 2381#endif
d8eceb89 2382 PL_gid = PerlProc_getgid();
3280af22 2383 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2384 break;
2385 case ')':
5cd24f17
PP
2386#ifdef HAS_SETGROUPS
2387 {
35a4481c 2388 const char *p = SvPV(sv, len);
5cd24f17
PP
2389 Groups_t gary[NGROUPS];
2390
5cd24f17
PP
2391 while (isSPACE(*p))
2392 ++p;
2d4389e4 2393 PL_egid = Atol(p);
5cd24f17
PP
2394 for (i = 0; i < NGROUPS; ++i) {
2395 while (*p && !isSPACE(*p))
2396 ++p;
2397 while (isSPACE(*p))
2398 ++p;
2399 if (!*p)
2400 break;
2d4389e4 2401 gary[i] = Atol(p);
5cd24f17 2402 }
8cc95fdb
PP
2403 if (i)
2404 (void)setgroups(i, gary);
5cd24f17
PP
2405 }
2406#else /* HAS_SETGROUPS */
b28d0864 2407 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2408#endif /* HAS_SETGROUPS */
3280af22
NIS
2409 if (PL_delaymagic) {
2410 PL_delaymagic |= DM_EGID;
79072805
LW
2411 break; /* don't do magic till later */
2412 }
2413#ifdef HAS_SETEGID
3280af22 2414 (void)setegid((Gid_t)PL_egid);
79072805
LW
2415#else
2416#ifdef HAS_SETREGID
b28d0864 2417 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2418#else
2419#ifdef HAS_SETRESGID
b28d0864 2420 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2421#else
b28d0864
NIS
2422 if (PL_egid == PL_gid) /* special case $) = $( */
2423 (void)PerlProc_setgid(PL_egid);
748a9306 2424 else {
d8eceb89 2425 PL_egid = PerlProc_getegid();
cea2e8a9 2426 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2427 }
79072805
LW
2428#endif
2429#endif
85e6fe83 2430#endif
d8eceb89 2431 PL_egid = PerlProc_getegid();
3280af22 2432 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2433 break;
2434 case ':':
2d8e6c8d 2435 PL_chopset = SvPV_force(sv,len);
79072805 2436 break;
cd39f2b6 2437#ifndef MACOS_TRADITIONAL
79072805 2438 case '0':
e2975953 2439 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2440#ifdef HAS_SETPROCTITLE
2441 /* The BSDs don't show the argv[] in ps(1) output, they
2442 * show a string from the process struct and provide
2443 * the setproctitle() routine to manipulate that. */
2444 {
2445 s = SvPV(sv, len);
98b76f99 2446# if __FreeBSD_version > 410001
9aad2c0e 2447 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2448 * but not the "(perl) suffix from the ps(1)
2449 * output, because that's what ps(1) shows if the
2450 * argv[] is modified. */
6f2ad931 2451 setproctitle("-%s", s);
9aad2c0e 2452# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2453 /* This doesn't really work if you assume that
2454 * $0 = 'foobar'; will wipe out 'perl' from the $0
2455 * because in ps(1) output the result will be like
2456 * sprintf("perl: %s (perl)", s)
2457 * I guess this is a security feature:
2458 * one (a user process) cannot get rid of the original name.
2459 * --jhi */
2460 setproctitle("%s", s);
2461# endif
2462 }
2463#endif
17aa7f3d
JH
2464#if defined(__hpux) && defined(PSTAT_SETCMD)
2465 {
2466 union pstun un;
2467 s = SvPV(sv, len);
6867be6d 2468 un.pst_command = (char *)s;
17aa7f3d
JH
2469 pstat(PSTAT_SETCMD, un, len, 0, 0);
2470 }
2471#endif
3cb9023d 2472 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2473 s = SvPV_force(sv,len);
6f698202
AMS
2474 if (len >= (STRLEN)PL_origalen-1) {
2475 /* Longer than original, will be truncated. We assume that
2476 * PL_origalen bytes are available. */
2477 Copy(s, PL_origargv[0], PL_origalen-1, char);
79072805
LW
2478 }
2479 else {
54bfe034
JH
2480 /* Shorter than original, will be padded. */
2481 Copy(s, PL_origargv[0], len, char);
2482 PL_origargv[0][len] = 0;
2483 memset(PL_origargv[0] + len + 1,
2484 /* Is the space counterintuitive? Yes.
2485 * (You were expecting \0?)
3cb9023d 2486 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2487 * --jhi */
2488 (int)' ',
2489 PL_origalen - len - 1);
79072805 2490 }
ad7eccf4
JD
2491 PL_origargv[0][PL_origalen-1] = 0;
2492 for (i = 1; i < PL_origargc; i++)
2493 PL_origargv[i] = 0;
e2975953 2494 UNLOCK_DOLLARZERO_MUTEX;
79072805 2495 break;
cd39f2b6 2496#endif
79072805
LW
2497 }
2498 return 0;
2499}
2500
2501I32
35a4481c 2502Perl_whichsig(pTHX_ const char *sig)
79072805 2503{
aadb217d 2504 register char* const* sigv;
79072805 2505
aadb217d 2506 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
79072805 2507 if (strEQ(sig,*sigv))
aadb217d 2508 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805
LW
2509#ifdef SIGCLD
2510 if (strEQ(sig,"CHLD"))
2511 return SIGCLD;
2512#endif
2513#ifdef SIGCHLD
2514 if (strEQ(sig,"CLD"))
2515 return SIGCHLD;
2516#endif
7f1236c0 2517 return -1;
79072805
LW
2518}
2519
ecfc5424 2520Signal_t
cea2e8a9 2521Perl_sighandler(int sig)
79072805 2522{
1018e26f
NIS
2523#ifdef PERL_GET_SIG_CONTEXT
2524 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2525#else
cea2e8a9 2526 dTHX;
71d280e3 2527#endif
79072805 2528 dSP;
00d579c5 2529 GV *gv = Nullgv;
a0d0e21e 2530 HV *st;
b7953727 2531 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2532 CV *cv = Nullcv;
533c011a 2533 OP *myop = PL_op;
84902520 2534 U32 flags = 0;
3280af22 2535 XPV *tXpv = PL_Xpv;
71d280e3 2536
3280af22 2537 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2538 flags |= 1;
3280af22 2539 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2540 flags |= 4;
3280af22 2541 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2542 flags |= 16;
2543
727405f8 2544 if (!PL_psig_ptr[sig]) {
99ef548b 2545 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2546 PL_sig_name[sig]);
2547 exit(sig);
2548 }
ff0cee69 2549
84902520
TB
2550 /* Max number of items pushed there is 3*n or 4. We cannot fix
2551 infinity, so we fix 4 (in fact 5): */
2552 if (flags & 1) {
3280af22 2553 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2554 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2555 }
ac27b0f5 2556 if (flags & 4)
3280af22 2557 PL_markstack_ptr++; /* Protect mark. */
84902520 2558 if (flags & 16)
3280af22 2559 PL_scopestack_ix += 1;
84902520 2560 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2561 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2562 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2563 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2564
a0d0e21e 2565 if (!cv || !CvROOT(cv)) {
599cee73 2566 if (ckWARN(WARN_SIGNAL))
9014280d 2567 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2568 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2569 : ((cv && CvGV(cv))
2570 ? GvENAME(CvGV(cv))
2571 : "__ANON__")));
2572 goto cleanup;
79072805
LW
2573 }
2574
22c35a8c
GS
2575 if(PL_psig_name[sig]) {
2576 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2577 flags |= 64;
df3728a2 2578#if !defined(PERL_IMPLICIT_CONTEXT)
27da23d5 2579 PL_sig_sv = sv;
df3728a2 2580#endif
84902520 2581 } else {
ff0cee69 2582 sv = sv_newmortal();
22c35a8c 2583 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2584 }
e336de0d 2585
e788e7d3 2586 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2587 PUSHMARK(SP);
79072805 2588 PUSHs(sv);
79072805 2589 PUTBACK;
a0d0e21e 2590
1b266415 2591 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2592
d3acc0f7 2593 POPSTACK;
1b266415 2594 if (SvTRUE(ERRSV)) {
1d615522 2595#ifndef PERL_MICRO
983dbef6 2596#ifdef HAS_SIGPROCMASK
1b266415
NIS
2597 /* Handler "died", for example to get out of a restart-able read().
2598 * Before we re-do that on its behalf re-enable the signal which was
2599 * blocked by the system when we entered.
2600 */
2601 sigset_t set;
2602 sigemptyset(&set);
2603 sigaddset(&set,sig);
2604 sigprocmask(SIG_UNBLOCK, &set, NULL);
2605#else
2606 /* Not clear if this will work */
2607 (void)rsignal(sig, SIG_IGN);
5c1546dc 2608 (void)rsignal(sig, PL_csighandlerp);
1b266415 2609#endif
1d615522 2610#endif /* !PERL_MICRO */
5df617be 2611 DieNull;
1b266415 2612 }
00d579c5 2613cleanup:
84902520 2614 if (flags & 1)
3280af22 2615 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2616 if (flags & 4)
3280af22 2617 PL_markstack_ptr--;
84902520 2618 if (flags & 16)
3280af22 2619 PL_scopestack_ix -= 1;
84902520
TB
2620 if (flags & 64)
2621 SvREFCNT_dec(sv);
533c011a 2622 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2623
3280af22
NIS
2624 PL_Sv = tSv; /* Restore global temporaries. */
2625 PL_Xpv = tXpv;
53bb94e2 2626 return;
79072805 2627}
4e35701f
NIS
2628
2629
51371543 2630static void
35a4481c 2631restore_magic(pTHX_ const void *p)
51371543 2632{
48944bdf 2633 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2634 SV* sv = mgs->mgs_sv;
2635
2636 if (!sv)
2637 return;
2638
2639 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2640 {
f8c7b90f 2641#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
2642 /* While magic was saved (and off) sv_setsv may well have seen
2643 this SV as a prime candidate for COW. */
2644 if (SvIsCOW(sv))
2645 sv_force_normal(sv);
2646#endif
2647
51371543
GS
2648 if (mgs->mgs_flags)
2649 SvFLAGS(sv) |= mgs->mgs_flags;
2650 else
2651 mg_magical(sv);
2652 if (SvGMAGICAL(sv))
2653 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2654 }
2655
2656 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2657
2658 /* If we're still on top of the stack, pop us off. (That condition
2659 * will be satisfied if restore_magic was called explicitly, but *not*
2660 * if it's being called via leave_scope.)
2661 * The reason for doing this is that otherwise, things like sv_2cv()
2662 * may leave alloc gunk on the savestack, and some code
2663 * (e.g. sighandler) doesn't expect that...
2664 */
2665 if (PL_savestack_ix == mgs->mgs_ss_ix)
2666 {
2667 I32 popval = SSPOPINT;
c76ac1ee 2668 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2669 PL_savestack_ix -= 2;
2670 popval = SSPOPINT;
2671 assert(popval == SAVEt_ALLOC);
2672 popval = SSPOPINT;
2673 PL_savestack_ix -= popval;
2674 }
2675
2676}
2677
2678static void
35a4481c 2679unwind_handler_stack(pTHX_ const void *p)
51371543 2680{
27da23d5 2681 dVAR;
e1ec3a88 2682 const U32 flags = *(const U32*)p;
51371543
GS
2683
2684 if (flags & 1)
2685 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2686 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2687#if !defined(PERL_IMPLICIT_CONTEXT)
51371543 2688 if (flags & 64)
27da23d5 2689 SvREFCNT_dec(PL_sig_sv);
df3728a2 2690#endif
51371543 2691}
1018e26f 2692
66610fdd
RGS
2693/*
2694 * Local variables:
2695 * c-indentation-style: bsd
2696 * c-basic-offset: 4
2697 * indent-tabs-mode: t
2698 * End:
2699 *
37442d52
RGS
2700 * ex: set ts=8 sts=4 sw=4 noet:
2701 */