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