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