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