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