This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
another test of the Emergency Broadcast System--back out
[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 939# else
d308986b 940# ifdef __CYGWIN__
2c2d71f5
JH
941 I32 i;
942 for (i = 0; environ[i]; i++)
d702ae42 943 safesysfree(environ[i]);
7766f137
GS
944# else
945# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
946 I32 i;
947
3280af22 948 if (environ == PL_origenviron)
f2517201 949 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
950 else
951 for (i = 0; environ[i]; i++)
f2517201 952 safesysfree(environ[i]);
7766f137 953# endif /* PERL_USE_SAFE_PUTENV */
d308986b 954# endif /* __CYGWIN__ */
f2517201 955
66b1d557
HM
956 environ[0] = Nullch;
957
7766f137
GS
958# endif /* WIN32 */
959# endif /* PERL_IMPLICIT_SYS */
f2517201 960#endif /* VMS */
3e3baf6d 961 return 0;
66b1d557
HM
962}
963
64ca3a65 964#ifndef PERL_MICRO
66b1d557 965int
864dbfa3 966Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
967{
968 I32 i;
2d8e6c8d 969 STRLEN n_a;
88e89b8a 970 /* Are we fetching a signal entry? */
2d8e6c8d 971 i = whichsig(MgPV(mg,n_a));
88e89b8a 972 if (i) {
22c35a8c
GS
973 if(PL_psig_ptr[i])
974 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 975 else {
ff68c719
PP
976 Sighandler_t sigstate = rsignal_state(i);
977
88e89b8a 978 /* cache state so we don't fetch it again */
ff68c719 979 if(sigstate == SIG_IGN)
88e89b8a
PP
980 sv_setpv(sv,"IGNORE");
981 else
3280af22 982 sv_setsv(sv,&PL_sv_undef);
22c35a8c 983 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
984 SvTEMP_off(sv);
985 }
986 }
987 return 0;
988}
989int
864dbfa3 990Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
991{
992 I32 i;
2d8e6c8d 993 STRLEN n_a;
88e89b8a 994 /* Are we clearing a signal entry? */
2d8e6c8d 995 i = whichsig(MgPV(mg,n_a));
88e89b8a 996 if (i) {
22c35a8c
GS
997 if(PL_psig_ptr[i]) {
998 SvREFCNT_dec(PL_psig_ptr[i]);
999 PL_psig_ptr[i]=0;
88e89b8a 1000 }
22c35a8c
GS
1001 if(PL_psig_name[i]) {
1002 SvREFCNT_dec(PL_psig_name[i]);
1003 PL_psig_name[i]=0;
88e89b8a
PP
1004 }
1005 }
1006 return 0;
1007}
3d37d572 1008
85e6fe83 1009int
864dbfa3 1010Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1011{
11343788 1012 dTHR;
79072805
LW
1013 register char *s;
1014 I32 i;
748a9306 1015 SV** svp;
e72dc28c 1016 STRLEN len;
a0d0e21e 1017
e72dc28c 1018 s = MgPV(mg,len);
748a9306
LW
1019 if (*s == '_') {
1020 if (strEQ(s,"__DIE__"))
3280af22 1021 svp = &PL_diehook;
748a9306 1022 else if (strEQ(s,"__WARN__"))
3280af22 1023 svp = &PL_warnhook;
748a9306 1024 else
cea2e8a9 1025 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1026 i = 0;
4633a7c4
LW
1027 if (*svp) {
1028 SvREFCNT_dec(*svp);
1029 *svp = 0;
1030 }
748a9306
LW
1031 }
1032 else {
1033 i = whichsig(s); /* ...no, a brick */
1034 if (!i) {
e476b1b5 1035 if (ckWARN(WARN_SIGNAL))
cea2e8a9 1036 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
748a9306
LW
1037 return 0;
1038 }
22c35a8c
GS
1039 SvREFCNT_dec(PL_psig_name[i]);
1040 SvREFCNT_dec(PL_psig_ptr[i]);
1041 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1042 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1043 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1044 SvREADONLY_on(PL_psig_name[i]);
748a9306 1045 }
a0d0e21e 1046 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 1047 if (i)
3280af22 1048 (void)rsignal(i, PL_sighandlerp);
748a9306
LW
1049 else
1050 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
1051 return 0;
1052 }
e72dc28c 1053 s = SvPV_force(sv,len);
748a9306
LW
1054 if (strEQ(s,"IGNORE")) {
1055 if (i)
ff68c719 1056 (void)rsignal(i, SIG_IGN);
748a9306
LW
1057 else
1058 *svp = 0;
1059 }
1060 else if (strEQ(s,"DEFAULT") || !*s) {
1061 if (i)
ff68c719 1062 (void)rsignal(i, SIG_DFL);
748a9306
LW
1063 else
1064 *svp = 0;
1065 }
79072805 1066 else {
5aabfad6
PP
1067 /*
1068 * We should warn if HINT_STRICT_REFS, but without
1069 * access to a known hint bit in a known OP, we can't
1070 * tell whether HINT_STRICT_REFS is in force or not.
1071 */
46fc3d4c 1072 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1073 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1074 if (i)
3280af22 1075 (void)rsignal(i, PL_sighandlerp);
748a9306
LW
1076 else
1077 *svp = SvREFCNT_inc(sv);
79072805
LW
1078 }
1079 return 0;
1080}
64ca3a65 1081#endif /* !PERL_MICRO */
79072805
LW
1082
1083int
864dbfa3 1084Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1085{
3280af22 1086 PL_sub_generation++;
463ee0b2
LW
1087 return 0;
1088}
1089
1090int
864dbfa3 1091Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1092{
a0d0e21e 1093 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1094 PL_amagic_generation++;
463ee0b2 1095
a0d0e21e
LW
1096 return 0;
1097}
463ee0b2 1098
946ec16e 1099int
864dbfa3 1100Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1101{
1102 HV *hv = (HV*)LvTARG(sv);
1103 HE *entry;
1104 I32 i = 0;
1105
1106 if (hv) {
1107 (void) hv_iterinit(hv);
33c27489 1108 if (! SvTIED_mg((SV*)hv, 'P'))
6ff81951
GS
1109 i = HvKEYS(hv);
1110 else {
1111 /*SUPPRESS 560*/
155aba94 1112 while ((entry = hv_iternext(hv))) {
6ff81951
GS
1113 i++;
1114 }
1115 }
1116 }
1117
1118 sv_setiv(sv, (IV)i);
1119 return 0;
1120}
1121
1122int
864dbfa3 1123Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1124{
1125 if (LvTARG(sv)) {
1126 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1127 }
1128 return 0;
93965878 1129}
946ec16e 1130
e336de0d 1131/* caller is responsible for stack switching/cleanup */
565764a8 1132STATIC int
cea2e8a9 1133S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1134{
1135 dSP;
463ee0b2 1136
924508f0
GS
1137 PUSHMARK(SP);
1138 EXTEND(SP, n);
33c27489 1139 PUSHs(SvTIED_obj(sv, mg));
93965878
NIS
1140 if (n > 1) {
1141 if (mg->mg_ptr) {
565764a8 1142 if (mg->mg_len >= 0)
79cb57f6 1143 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1144 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1145 PUSHs((SV*)mg->mg_ptr);
1146 }
1147 else if (mg->mg_type == 'p') {
565764a8 1148 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1149 }
1150 }
1151 if (n > 2) {
1152 PUSHs(val);
88e89b8a 1153 }
463ee0b2
LW
1154 PUTBACK;
1155
864dbfa3 1156 return call_method(meth, flags);
946ec16e
PP
1157}
1158
76e3520e 1159STATIC int
cea2e8a9 1160S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1161{
1162 dSP;
463ee0b2 1163
a0d0e21e
LW
1164 ENTER;
1165 SAVETMPS;
e788e7d3 1166 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1167
33c27489 1168 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1169 sv_setsv(sv, *PL_stack_sp--);
93965878 1170 }
463ee0b2 1171
d3acc0f7 1172 POPSTACK;
a0d0e21e
LW
1173 FREETMPS;
1174 LEAVE;
1175 return 0;
1176}
463ee0b2 1177
a0d0e21e 1178int
864dbfa3 1179Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1180{
1181 magic_methpack(sv,mg,"FETCH");
1182 if (mg->mg_ptr)
1183 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1184 return 0;
1185}
1186
1187int
864dbfa3 1188Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1189{
1190 dSP;
a60c0954 1191 ENTER;
e788e7d3 1192 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1193 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1194 POPSTACK;
a60c0954 1195 LEAVE;
463ee0b2
LW
1196 return 0;
1197}
1198
1199int
864dbfa3 1200Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1201{
a0d0e21e
LW
1202 return magic_methpack(sv,mg,"DELETE");
1203}
463ee0b2 1204
93965878
NIS
1205
1206U32
864dbfa3 1207Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
93965878 1208{
e336de0d 1209 dSP;
93965878
NIS
1210 U32 retval = 0;
1211
1212 ENTER;
1213 SAVETMPS;
e788e7d3 1214 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1215 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1216 sv = *PL_stack_sp--;
a60c0954 1217 retval = (U32) SvIV(sv)-1;
93965878 1218 }
d3acc0f7 1219 POPSTACK;
93965878
NIS
1220 FREETMPS;
1221 LEAVE;
1222 return retval;
1223}
1224
cea2e8a9
GS
1225int
1226Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1227{
1228 dSP;
463ee0b2 1229
e336de0d 1230 ENTER;
e788e7d3 1231 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1232 PUSHMARK(SP);
33c27489 1233 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1234 PUTBACK;
864dbfa3 1235 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1236 POPSTACK;
a60c0954 1237 LEAVE;
463ee0b2
LW
1238 return 0;
1239}
1240
1241int
864dbfa3 1242Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1243{
463ee0b2 1244 dSP;
dff6d3cd 1245 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1246
1247 ENTER;
a0d0e21e 1248 SAVETMPS;
e788e7d3 1249 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1250 PUSHMARK(SP);
1251 EXTEND(SP, 2);
33c27489 1252 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1253 if (SvOK(key))
1254 PUSHs(key);
1255 PUTBACK;
1256
864dbfa3 1257 if (call_method(meth, G_SCALAR))
3280af22 1258 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1259
d3acc0f7 1260 POPSTACK;
a0d0e21e
LW
1261 FREETMPS;
1262 LEAVE;
79072805
LW
1263 return 0;
1264}
1265
1266int
864dbfa3 1267Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1268{
1269 return magic_methpack(sv,mg,"EXISTS");
1270}
1271
1272int
864dbfa3 1273Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1274{
11343788 1275 dTHR;
79072805
LW
1276 OP *o;
1277 I32 i;
1278 GV* gv;
1279 SV** svp;
2d8e6c8d 1280 STRLEN n_a;
79072805 1281
3280af22 1282 gv = PL_DBline;
79072805 1283 i = SvTRUE(sv);
188ea221 1284 svp = av_fetch(GvAV(gv),
2d8e6c8d 1285 atoi(MgPV(mg,n_a)), FALSE);
57b2e452 1286 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
93a17b20 1287 o->op_private = i;
79072805
LW
1288 return 0;
1289}
1290
1291int
864dbfa3 1292Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1293{
0f15f207 1294 dTHR;
3280af22 1295 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1296 return 0;
1297}
1298
1299int
864dbfa3 1300Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1301{
0f15f207 1302 dTHR;
3280af22 1303 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1304 return 0;
1305}
1306
1307int
864dbfa3 1308Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1309{
1310 SV* lsv = LvTARG(sv);
1311
1312 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1313 mg = mg_find(lsv, 'g');
565764a8 1314 if (mg && mg->mg_len >= 0) {
0f15f207 1315 dTHR;
a0ed51b3 1316 I32 i = mg->mg_len;
7e2040f0 1317 if (DO_UTF8(lsv))
a0ed51b3
LW
1318 sv_pos_b2u(lsv, &i);
1319 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1320 return 0;
1321 }
1322 }
1323 (void)SvOK_off(sv);
1324 return 0;
1325}
1326
1327int
864dbfa3 1328Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1329{
1330 SV* lsv = LvTARG(sv);
1331 SSize_t pos;
1332 STRLEN len;
c00206c8 1333 STRLEN ulen = 0;
c485e607 1334 dTHR;
a0d0e21e
LW
1335
1336 mg = 0;
1337
1338 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1339 mg = mg_find(lsv, 'g');
1340 if (!mg) {
1341 if (!SvOK(sv))
1342 return 0;
1343 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1344 mg = mg_find(lsv, 'g');
1345 }
1346 else if (!SvOK(sv)) {
565764a8 1347 mg->mg_len = -1;
a0d0e21e
LW
1348 return 0;
1349 }
1350 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1351
c485e607 1352 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1353
7e2040f0 1354 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1355 ulen = sv_len_utf8(lsv);
1356 if (ulen)
1357 len = ulen;
a0ed51b3
LW
1358 }
1359
a0d0e21e
LW
1360 if (pos < 0) {
1361 pos += len;
1362 if (pos < 0)
1363 pos = 0;
1364 }
1365 else if (pos > len)
1366 pos = len;
a0ed51b3
LW
1367
1368 if (ulen) {
1369 I32 p = pos;
1370 sv_pos_u2b(lsv, &p, 0);
1371 pos = p;
1372 }
1373
565764a8 1374 mg->mg_len = pos;
71be2cbc 1375 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1376
79072805
LW
1377 return 0;
1378}
1379
1380int
864dbfa3 1381Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1382{
8646b087
PP
1383 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1384 SvFAKE_off(sv);
946ec16e 1385 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1386 SvFAKE_on(sv);
1387 }
1388 else
946ec16e 1389 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1390 return 0;
1391}
1392
1393int
864dbfa3 1394Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1395{
1396 register char *s;
1397 GV* gv;
2d8e6c8d 1398 STRLEN n_a;
79072805
LW
1399
1400 if (!SvOK(sv))
1401 return 0;
2d8e6c8d 1402 s = SvPV(sv, n_a);
79072805
LW
1403 if (*s == '*' && s[1])
1404 s++;
85e6fe83 1405 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1406 if (sv == (SV*)gv)
1407 return 0;
1408 if (GvGP(sv))
88e89b8a 1409 gp_free((GV*)sv);
79072805 1410 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1411 return 0;
1412}
1413
1414int
864dbfa3 1415Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1416{
1417 STRLEN len;
1418 SV *lsv = LvTARG(sv);
1419 char *tmps = SvPV(lsv,len);
1420 I32 offs = LvTARGOFF(sv);
1421 I32 rem = LvTARGLEN(sv);
1422
1423 if (offs > len)
1424 offs = len;
1425 if (rem + offs > len)
1426 rem = len - offs;
1427 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1428 return 0;
1429}
1430
1431int
864dbfa3 1432Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1433{
8990e307
LW
1434 STRLEN len;
1435 char *tmps = SvPV(sv,len);
1436 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1437 return 0;
1438}
1439
1440int
864dbfa3 1441Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1442{
a863c7d1 1443 dTHR;
565764a8 1444 TAINT_IF((mg->mg_len & 1) ||
155aba94 1445 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1446 return 0;
1447}
1448
1449int
864dbfa3 1450Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1451{
11343788 1452 dTHR;
3280af22
NIS
1453 if (PL_localizing) {
1454 if (PL_localizing == 1)
565764a8 1455 mg->mg_len <<= 1;
748a9306 1456 else
565764a8 1457 mg->mg_len >>= 1;
a0d0e21e 1458 }
3280af22 1459 else if (PL_tainted)
565764a8 1460 mg->mg_len |= 1;
748a9306 1461 else
565764a8 1462 mg->mg_len &= ~1;
463ee0b2
LW
1463 return 0;
1464}
1465
1466int
864dbfa3 1467Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1468{
1469 SV *lsv = LvTARG(sv);
6ff81951
GS
1470
1471 if (!lsv) {
155aba94 1472 (void)SvOK_off(sv);
6ff81951
GS
1473 return 0;
1474 }
6ff81951 1475
81e118e0 1476 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1477 return 0;
1478}
1479
1480int
864dbfa3 1481Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1482{
1483 do_vecset(sv); /* XXX slurp this routine */
1484 return 0;
1485}
1486
1487int
864dbfa3 1488Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1489{
71be2cbc 1490 SV *targ = Nullsv;
5f05dabc 1491 if (LvTARGLEN(sv)) {
68dc0745 1492 if (mg->mg_obj) {
74e13ce4
GS
1493 SV *ahv = LvTARG(sv);
1494 if (SvTYPE(ahv) == SVt_PVHV) {
1495 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1496 if (he)
1497 targ = HeVAL(he);
1498 }
1499 else {
1500 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1501 if (svp)
1502 targ = *svp;
1503 }
68dc0745
PP
1504 }
1505 else {
3c78fafa 1506 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1507 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1508 targ = AvARRAY(av)[LvTARGOFF(sv)];
1509 }
3280af22 1510 if (targ && targ != &PL_sv_undef) {
e858de61 1511 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1512 /* somebody else defined it for us */
1513 SvREFCNT_dec(LvTARG(sv));
1514 LvTARG(sv) = SvREFCNT_inc(targ);
1515 LvTARGLEN(sv) = 0;
1516 SvREFCNT_dec(mg->mg_obj);
1517 mg->mg_obj = Nullsv;
1518 mg->mg_flags &= ~MGf_REFCOUNTED;
1519 }
5f05dabc 1520 }
71be2cbc
PP
1521 else
1522 targ = LvTARG(sv);
3280af22 1523 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1524 return 0;
1525}
1526
1527int
864dbfa3 1528Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1529{
1530 if (LvTARGLEN(sv))
68dc0745
PP
1531 vivify_defelem(sv);
1532 if (LvTARG(sv)) {
5f05dabc 1533 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1534 SvSETMAGIC(LvTARG(sv));
1535 }
5f05dabc
PP
1536 return 0;
1537}
1538
71be2cbc 1539void
864dbfa3 1540Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1541{
e858de61 1542 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
74e13ce4
GS
1543 MAGIC *mg;
1544 SV *value = Nullsv;
71be2cbc 1545
68dc0745 1546 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1547 return;
68dc0745 1548 if (mg->mg_obj) {
74e13ce4 1549 SV *ahv = LvTARG(sv);
2d8e6c8d 1550 STRLEN n_a;
74e13ce4 1551 if (SvTYPE(ahv) == SVt_PVHV) {
af7f9c15 1552 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1553 if (he)
1554 value = HeVAL(he);
1555 }
1556 else {
af7f9c15 1557 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1558 if (svp)
1559 value = *svp;
1560 }
3280af22 1561 if (!value || value == &PL_sv_undef)
cea2e8a9 1562 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1563 }
68dc0745
PP
1564 else {
1565 AV* av = (AV*)LvTARG(sv);
5aabfad6 1566 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1567 LvTARG(sv) = Nullsv; /* array can't be extended */
1568 else {
1569 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1570 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1571 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1572 }
1573 }
3e3baf6d 1574 (void)SvREFCNT_inc(value);
68dc0745
PP
1575 SvREFCNT_dec(LvTARG(sv));
1576 LvTARG(sv) = value;
71be2cbc 1577 LvTARGLEN(sv) = 0;
68dc0745
PP
1578 SvREFCNT_dec(mg->mg_obj);
1579 mg->mg_obj = Nullsv;
1580 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1581}
1582
1583int
864dbfa3 1584Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1585{
1586 AV *av = (AV*)mg->mg_obj;
1587 SV **svp = AvARRAY(av);
1588 I32 i = AvFILLp(av);
1589 while (i >= 0) {
1590 if (svp[i] && svp[i] != &PL_sv_undef) {
1591 if (!SvWEAKREF(svp[i]))
cea2e8a9 1592 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1593 /* XXX Should we check that it hasn't changed? */
1594 SvRV(svp[i]) = 0;
155aba94 1595 (void)SvOK_off(svp[i]);
810b8aa5
GS
1596 SvWEAKREF_off(svp[i]);
1597 svp[i] = &PL_sv_undef;
1598 }
1599 i--;
1600 }
1601 return 0;
1602}
1603
1604int
864dbfa3 1605Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1606{
565764a8 1607 mg->mg_len = -1;
c6496cc7 1608 SvSCREAM_off(sv);
93a17b20
LW
1609 return 0;
1610}
1611
1612int
864dbfa3 1613Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1614{
463ee0b2 1615 sv_unmagic(sv, 'B');
79072805
LW
1616 SvVALID_off(sv);
1617 return 0;
1618}
1619
1620int
864dbfa3 1621Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff
PP
1622{
1623 sv_unmagic(sv, 'f');
1624 SvCOMPILED_off(sv);
1625 return 0;
1626}
1627
1628int
864dbfa3 1629Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1630{
1631 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1632
1633 if (uf && uf->uf_set)
1634 (*uf->uf_set)(uf->uf_index, sv);
1635 return 0;
1636}
1637
c277df42 1638int
864dbfa3 1639Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1640{
1641 regexp *re = (regexp *)mg->mg_obj;
1642 ReREFCNT_dec(re);
1643 return 0;
1644}
1645
7a4c00b4 1646#ifdef USE_LOCALE_COLLATE
79072805 1647int
864dbfa3 1648Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1649{
1650 /*
838b5b74 1651 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1652 * and vanished with a faint plop.
1653 */
7a4c00b4
PP
1654 if (mg->mg_ptr) {
1655 Safefree(mg->mg_ptr);
1656 mg->mg_ptr = NULL;
565764a8 1657 mg->mg_len = -1;
7a4c00b4 1658 }
bbce6d69
PP
1659 return 0;
1660}
7a4c00b4 1661#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1662
1663int
864dbfa3 1664Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 1665{
11343788 1666 dTHR;
79072805
LW
1667 register char *s;
1668 I32 i;
8990e307 1669 STRLEN len;
79072805 1670 switch (*mg->mg_ptr) {
748a9306 1671 case '\001': /* ^A */
3280af22 1672 sv_setsv(PL_bodytarget, sv);
748a9306 1673 break;
49460fe6
NIS
1674 case '\003': /* ^C */
1675 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1676 break;
1677
79072805 1678 case '\004': /* ^D */
3280af22 1679 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1680 DEBUG_x(dump_all());
1681 break;
28f23441 1682 case '\005': /* ^E */
cd39f2b6 1683#ifdef MACOS_TRADITIONAL
bf4acbe4 1684 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1685#else
cd39f2b6
JH
1686# ifdef VMS
1687 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1688# else
cd39f2b6
JH
1689# ifdef WIN32
1690 SetLastError( SvIV(sv) );
1691# else
1692# ifndef OS2
f86702cc
PP
1693 /* will anyone ever use this? */
1694 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 1695# endif
048c1ddf
IZ
1696# endif
1697# endif
22fae026 1698#endif
28f23441 1699 break;
79072805 1700 case '\006': /* ^F */
3280af22 1701 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1702 break;
a0d0e21e 1703 case '\010': /* ^H */
3280af22 1704 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 1705 break;
9d116dd7 1706 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
1707 if (PL_inplace)
1708 Safefree(PL_inplace);
79072805 1709 if (SvOK(sv))
2d8e6c8d 1710 PL_inplace = savepv(SvPV(sv,len));
79072805 1711 else
3280af22 1712 PL_inplace = Nullch;
79072805 1713 break;
28f23441 1714 case '\017': /* ^O */
3280af22
NIS
1715 if (PL_osname)
1716 Safefree(PL_osname);
28f23441 1717 if (SvOK(sv))
2d8e6c8d 1718 PL_osname = savepv(SvPV(sv,len));
28f23441 1719 else
3280af22 1720 PL_osname = Nullch;
28f23441 1721 break;
79072805 1722 case '\020': /* ^P */
3280af22 1723 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
1724 if (PL_perldb && !PL_DBsingle)
1725 init_debugger();
79072805
LW
1726 break;
1727 case '\024': /* ^T */
88e89b8a 1728#ifdef BIG_TIME
6b88bc9c 1729 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 1730#else
3280af22 1731 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1732#endif
79072805 1733 break;
46487f74 1734 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
4438c4b7
JH
1735 if (*(mg->mg_ptr+1) == '\0') {
1736 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1737 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1738 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 1739 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 1740 }
599cee73 1741 }
6a818117 1742 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
4438c4b7 1743 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
1744 if (!SvPOK(sv) && PL_localizing) {
1745 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 1746 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
1747 break;
1748 }
f4fc7782 1749 {
b5477537 1750 STRLEN len, i;
d3a7d8c7 1751 int accumulate = 0 ;
f4fc7782 1752 int any_fatals = 0 ;
d3a7d8c7 1753 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
1754 for (i = 0 ; i < len ; ++i) {
1755 accumulate |= ptr[i] ;
1756 any_fatals |= (ptr[i] & 0xAA) ;
1757 }
d3a7d8c7
GS
1758 if (!accumulate)
1759 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
1760 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1761 PL_compiling.cop_warnings = pWARN_ALL;
1762 PL_dowarn |= G_WARN_ONCE ;
1763 }
d3a7d8c7
GS
1764 else {
1765 if (specialWARN(PL_compiling.cop_warnings))
1766 PL_compiling.cop_warnings = newSVsv(sv) ;
1767 else
1768 sv_setsv(PL_compiling.cop_warnings, sv);
1769 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1770 PL_dowarn |= G_WARN_ONCE ;
1771 }
f4fc7782 1772
d3a7d8c7 1773 }
4438c4b7 1774 }
971a9dd3 1775 }
46487f74
GS
1776 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1777 PL_widesyscalls = SvTRUE(sv);
79072805
LW
1778 break;
1779 case '.':
3280af22
NIS
1780 if (PL_localizing) {
1781 if (PL_localizing == 1)
7766f137 1782 SAVESPTR(PL_last_in_gv);
748a9306 1783 }
3280af22
NIS
1784 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1785 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
79072805
LW
1786 break;
1787 case '^':
3280af22 1788 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1789 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1790 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1791 break;
1792 case '~':
3280af22 1793 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1794 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1795 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1796 break;
1797 case '=':
3280af22 1798 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1799 break;
1800 case '-':
3280af22
NIS
1801 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1802 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1803 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
1804 break;
1805 case '%':
3280af22 1806 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1807 break;
1808 case '|':
4b65379b 1809 {
3280af22 1810 IO *io = GvIOp(PL_defoutgv);
4b65379b
CS
1811 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1812 IoFLAGS(io) &= ~IOf_FLUSH;
1813 else {
1814 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1815 PerlIO *ofp = IoOFP(io);
1816 if (ofp)
1817 (void)PerlIO_flush(ofp);
1818 IoFLAGS(io) |= IOf_FLUSH;
1819 }
1820 }
79072805
LW
1821 }
1822 break;
1823 case '*':
463ee0b2 1824 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
3280af22 1825 PL_multiline = (i != 0);
79072805
LW
1826 break;
1827 case '/':
3280af22
NIS
1828 SvREFCNT_dec(PL_nrs);
1829 PL_nrs = newSVsv(sv);
1830 SvREFCNT_dec(PL_rs);
1831 PL_rs = SvREFCNT_inc(PL_nrs);
79072805
LW
1832 break;
1833 case '\\':
3280af22
NIS
1834 if (PL_ors)
1835 Safefree(PL_ors);
009c130f
GS
1836 if (SvOK(sv) || SvGMAGICAL(sv)) {
1837 s = SvPV(sv,PL_orslen);
1838 PL_ors = savepvn(s,PL_orslen);
1839 }
e3c19b7b 1840 else {
3280af22
NIS
1841 PL_ors = Nullch;
1842 PL_orslen = 0;
e3c19b7b 1843 }
79072805
LW
1844 break;
1845 case ',':
3280af22
NIS
1846 if (PL_ofs)
1847 Safefree(PL_ofs);
1848 PL_ofs = savepv(SvPV(sv, PL_ofslen));
79072805
LW
1849 break;
1850 case '#':
3280af22
NIS
1851 if (PL_ofmt)
1852 Safefree(PL_ofmt);
2d8e6c8d 1853 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
1854 break;
1855 case '[':
3280af22 1856 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1857 break;
1858 case '?':
ff0cee69 1859#ifdef COMPLEX_STATUS
6b88bc9c
GS
1860 if (PL_localizing == 2) {
1861 PL_statusvalue = LvTARGOFF(sv);
1862 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
1863 }
1864 else
1865#endif
1866#ifdef VMSISH_STATUS
1867 if (VMSISH_STATUS)
1868 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1869 else
1870#endif
1871 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1872 break;
1873 case '!':
78987ded 1874 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1875 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1876 break;
1877 case '<':
3280af22
NIS
1878 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1879 if (PL_delaymagic) {
1880 PL_delaymagic |= DM_RUID;
79072805
LW
1881 break; /* don't do magic till later */
1882 }
1883#ifdef HAS_SETRUID
b28d0864 1884 (void)setruid((Uid_t)PL_uid);
79072805
LW
1885#else
1886#ifdef HAS_SETREUID
3280af22 1887 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 1888#else
85e6fe83 1889#ifdef HAS_SETRESUID
b28d0864 1890 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 1891#else
b28d0864
NIS
1892 if (PL_uid == PL_euid) /* special case $< = $> */
1893 (void)PerlProc_setuid(PL_uid);
a0d0e21e 1894 else {
d8eceb89 1895 PL_uid = PerlProc_getuid();
cea2e8a9 1896 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 1897 }
79072805
LW
1898#endif
1899#endif
85e6fe83 1900#endif
d8eceb89 1901 PL_uid = PerlProc_getuid();
3280af22 1902 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1903 break;
1904 case '>':
3280af22
NIS
1905 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1906 if (PL_delaymagic) {
1907 PL_delaymagic |= DM_EUID;
79072805
LW
1908 break; /* don't do magic till later */
1909 }
1910#ifdef HAS_SETEUID
3280af22 1911 (void)seteuid((Uid_t)PL_euid);
79072805
LW
1912#else
1913#ifdef HAS_SETREUID
b28d0864 1914 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
1915#else
1916#ifdef HAS_SETRESUID
6b88bc9c 1917 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 1918#else
b28d0864
NIS
1919 if (PL_euid == PL_uid) /* special case $> = $< */
1920 PerlProc_setuid(PL_euid);
a0d0e21e 1921 else {
e8ee3774 1922 PL_euid = PerlProc_geteuid();
cea2e8a9 1923 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 1924 }
79072805
LW
1925#endif
1926#endif
85e6fe83 1927#endif
d8eceb89 1928 PL_euid = PerlProc_geteuid();
3280af22 1929 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1930 break;
1931 case '(':
3280af22
NIS
1932 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1933 if (PL_delaymagic) {
1934 PL_delaymagic |= DM_RGID;
79072805
LW
1935 break; /* don't do magic till later */
1936 }
1937#ifdef HAS_SETRGID
b28d0864 1938 (void)setrgid((Gid_t)PL_gid);
79072805
LW
1939#else
1940#ifdef HAS_SETREGID
3280af22 1941 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
1942#else
1943#ifdef HAS_SETRESGID
b28d0864 1944 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 1945#else
b28d0864
NIS
1946 if (PL_gid == PL_egid) /* special case $( = $) */
1947 (void)PerlProc_setgid(PL_gid);
748a9306 1948 else {
d8eceb89 1949 PL_gid = PerlProc_getgid();
cea2e8a9 1950 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 1951 }
79072805
LW
1952#endif
1953#endif
85e6fe83 1954#endif
d8eceb89 1955 PL_gid = PerlProc_getgid();
3280af22 1956 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1957 break;
1958 case ')':
5cd24f17
PP
1959#ifdef HAS_SETGROUPS
1960 {
2d8e6c8d 1961 char *p = SvPV(sv, len);
5cd24f17
PP
1962 Groups_t gary[NGROUPS];
1963
5cd24f17
PP
1964 while (isSPACE(*p))
1965 ++p;
2d4389e4 1966 PL_egid = Atol(p);
5cd24f17
PP
1967 for (i = 0; i < NGROUPS; ++i) {
1968 while (*p && !isSPACE(*p))
1969 ++p;
1970 while (isSPACE(*p))
1971 ++p;
1972 if (!*p)
1973 break;
2d4389e4 1974 gary[i] = Atol(p);
5cd24f17 1975 }
8cc95fdb
PP
1976 if (i)
1977 (void)setgroups(i, gary);
5cd24f17
PP
1978 }
1979#else /* HAS_SETGROUPS */
b28d0864 1980 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1981#endif /* HAS_SETGROUPS */
3280af22
NIS
1982 if (PL_delaymagic) {
1983 PL_delaymagic |= DM_EGID;
79072805
LW
1984 break; /* don't do magic till later */
1985 }
1986#ifdef HAS_SETEGID
3280af22 1987 (void)setegid((Gid_t)PL_egid);
79072805
LW
1988#else
1989#ifdef HAS_SETREGID
b28d0864 1990 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
1991#else
1992#ifdef HAS_SETRESGID
b28d0864 1993 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 1994#else
b28d0864
NIS
1995 if (PL_egid == PL_gid) /* special case $) = $( */
1996 (void)PerlProc_setgid(PL_egid);
748a9306 1997 else {
d8eceb89 1998 PL_egid = PerlProc_getegid();
cea2e8a9 1999 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2000 }
79072805
LW
2001#endif
2002#endif
85e6fe83 2003#endif
d8eceb89 2004 PL_egid = PerlProc_getegid();
3280af22 2005 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2006 break;
2007 case ':':
2d8e6c8d 2008 PL_chopset = SvPV_force(sv,len);
79072805 2009 break;
cd39f2b6 2010#ifndef MACOS_TRADITIONAL
79072805 2011 case '0':
4bc88a62
PS
2012#ifdef HAS_SETPROCTITLE
2013 /* The BSDs don't show the argv[] in ps(1) output, they
2014 * show a string from the process struct and provide
2015 * the setproctitle() routine to manipulate that. */
2016 {
2017 s = SvPV(sv, len);
9aad2c0e
JH
2018# if __FreeBSD_version >= 410001
2019 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2020 * but not the "(perl) suffix from the ps(1)
2021 * output, because that's what ps(1) shows if the
2022 * argv[] is modified. */
2023 setproctitle("-%s", s, len + 1);
9aad2c0e 2024# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2025 /* This doesn't really work if you assume that
2026 * $0 = 'foobar'; will wipe out 'perl' from the $0
2027 * because in ps(1) output the result will be like
2028 * sprintf("perl: %s (perl)", s)
2029 * I guess this is a security feature:
2030 * one (a user process) cannot get rid of the original name.
2031 * --jhi */
2032 setproctitle("%s", s);
2033# endif
2034 }
2035#endif
3280af22
NIS
2036 if (!PL_origalen) {
2037 s = PL_origargv[0];
79072805
LW
2038 s += strlen(s);
2039 /* See if all the arguments are contiguous in memory */
3280af22
NIS
2040 for (i = 1; i < PL_origargc; i++) {
2041 if (PL_origargv[i] == s + 1
fb73857a 2042#ifdef OS2
6b88bc9c 2043 || PL_origargv[i] == s + 2
fb73857a
PP
2044#endif
2045 )
379c4362
GS
2046 {
2047 ++s;
2048 s += strlen(s); /* this one is ok too */
2049 }
fb73857a
PP
2050 else
2051 break;
79072805 2052 }
bbce6d69 2053 /* can grab env area too? */
3280af22 2054 if (PL_origenviron && (PL_origenviron[0] == s + 1
fb73857a 2055#ifdef OS2
6b88bc9c 2056 || (PL_origenviron[0] == s + 9 && (s += 8))
fb73857a
PP
2057#endif
2058 )) {
66b1d557 2059 my_setenv("NoNe SuCh", Nullch);
79072805 2060 /* force copy of environment */
3280af22 2061 for (i = 0; PL_origenviron[i]; i++)
379c4362
GS
2062 if (PL_origenviron[i] == s + 1) {
2063 ++s;
2064 s += strlen(s);
2065 }
fb73857a
PP
2066 else
2067 break;
79072805 2068 }
3280af22 2069 PL_origalen = s - PL_origargv[0];
79072805 2070 }
a0d0e21e 2071 s = SvPV_force(sv,len);
8990e307 2072 i = len;
3280af22
NIS
2073 if (i >= PL_origalen) {
2074 i = PL_origalen;
fb73857a
PP
2075 /* don't allow system to limit $0 seen by script */
2076 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
3280af22
NIS
2077 Copy(s, PL_origargv[0], i, char);
2078 s = PL_origargv[0]+i;
fb73857a 2079 *s = '\0';
79072805
LW
2080 }
2081 else {
3280af22
NIS
2082 Copy(s, PL_origargv[0], i, char);
2083 s = PL_origargv[0]+i;
79072805 2084 *s++ = '\0';
3280af22 2085 while (++i < PL_origalen)
8990e307 2086 *s++ = ' ';
3280af22
NIS
2087 s = PL_origargv[0]+i;
2088 for (i = 1; i < PL_origargc; i++)
2089 PL_origargv[i] = Nullch;
79072805
LW
2090 }
2091 break;
cd39f2b6 2092#endif
a863c7d1
MB
2093#ifdef USE_THREADS
2094 case '@':
38a03e6e 2095 sv_setsv(thr->errsv, sv);
a863c7d1
MB
2096 break;
2097#endif /* USE_THREADS */
79072805
LW
2098 }
2099 return 0;
2100}
2101
f93b4edd
MB
2102#ifdef USE_THREADS
2103int
864dbfa3 2104Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
f93b4edd
MB
2105{
2106 dTHR;
b900a521
JH
2107 DEBUG_S(PerlIO_printf(Perl_debug_log,
2108 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2109 PTR2UV(thr), PTR2UV(sv));)
f93b4edd 2110 if (MgOWNER(mg))
cea2e8a9 2111 Perl_croak(aTHX_ "panic: magic_mutexfree");
f93b4edd
MB
2112 MUTEX_DESTROY(MgMUTEXP(mg));
2113 COND_DESTROY(MgCONDP(mg));
2114 return 0;
2115}
2116#endif /* USE_THREADS */
2117
79072805 2118I32
864dbfa3 2119Perl_whichsig(pTHX_ char *sig)
79072805
LW
2120{
2121 register char **sigv;
2122
22c35a8c 2123 for (sigv = PL_sig_name+1; *sigv; sigv++)
79072805 2124 if (strEQ(sig,*sigv))
22c35a8c 2125 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2126#ifdef SIGCLD
2127 if (strEQ(sig,"CHLD"))
2128 return SIGCLD;
2129#endif
2130#ifdef SIGCHLD
2131 if (strEQ(sig,"CLD"))
2132 return SIGCHLD;
2133#endif
2134 return 0;
2135}
2136
84902520
TB
2137static SV* sig_sv;
2138
ecfc5424 2139Signal_t
cea2e8a9 2140Perl_sighandler(int sig)
79072805 2141{
71d280e3
GS
2142#if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2143 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2144#else
cea2e8a9 2145 dTHX;
71d280e3 2146#endif
79072805 2147 dSP;
00d579c5 2148 GV *gv = Nullgv;
a0d0e21e 2149 HV *st;
3280af22 2150 SV *sv, *tSv = PL_Sv;
00d579c5 2151 CV *cv = Nullcv;
533c011a 2152 OP *myop = PL_op;
84902520 2153 U32 flags = 0;
155aba94 2154 I32 o_save_i = PL_savestack_ix;
3280af22 2155 XPV *tXpv = PL_Xpv;
71d280e3
GS
2156
2157#if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2158 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2159#endif
84902520 2160
3280af22 2161 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2162 flags |= 1;
3280af22 2163 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2164 flags |= 4;
3280af22 2165 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2166 flags |= 8;
3280af22 2167 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2168 flags |= 16;
2169
22c35a8c 2170 if (!PL_psig_ptr[sig])
cea2e8a9 2171 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
22c35a8c 2172 PL_sig_name[sig]);
ff0cee69 2173
84902520
TB
2174 /* Max number of items pushed there is 3*n or 4. We cannot fix
2175 infinity, so we fix 4 (in fact 5): */
2176 if (flags & 1) {
3280af22
NIS
2177 PL_savestack_ix += 5; /* Protect save in progress. */
2178 o_save_i = PL_savestack_ix;
c76ac1ee 2179 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520
TB
2180 }
2181 if (flags & 4)
3280af22 2182 PL_markstack_ptr++; /* Protect mark. */
84902520 2183 if (flags & 8) {
3280af22
NIS
2184 PL_retstack_ix++;
2185 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2186 }
2187 if (flags & 16)
3280af22 2188 PL_scopestack_ix += 1;
84902520 2189 /* sv_2cv is too complicated, try a simpler variant first: */
22c35a8c 2190 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2191 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2192 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2193
a0d0e21e 2194 if (!cv || !CvROOT(cv)) {
599cee73 2195 if (ckWARN(WARN_SIGNAL))
cea2e8a9 2196 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2197 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2198 : ((cv && CvGV(cv))
2199 ? GvENAME(CvGV(cv))
2200 : "__ANON__")));
2201 goto cleanup;
79072805
LW
2202 }
2203
22c35a8c
GS
2204 if(PL_psig_name[sig]) {
2205 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520
TB
2206 flags |= 64;
2207 sig_sv = sv;
2208 } else {
ff0cee69 2209 sv = sv_newmortal();
22c35a8c 2210 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2211 }
e336de0d 2212
e788e7d3 2213 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2214 PUSHMARK(SP);
79072805 2215 PUSHs(sv);
79072805 2216 PUTBACK;
a0d0e21e 2217
864dbfa3 2218 call_sv((SV*)cv, G_DISCARD);
79072805 2219
d3acc0f7 2220 POPSTACK;
00d579c5 2221cleanup:
84902520 2222 if (flags & 1)
3280af22 2223 PL_savestack_ix -= 8; /* Unprotect save in progress. */
84902520 2224 if (flags & 4)
3280af22 2225 PL_markstack_ptr--;
84902520 2226 if (flags & 8)
3280af22 2227 PL_retstack_ix--;
84902520 2228 if (flags & 16)
3280af22 2229 PL_scopestack_ix -= 1;
84902520
TB
2230 if (flags & 64)
2231 SvREFCNT_dec(sv);
533c011a 2232 PL_op = myop; /* Apparently not needed... */
84902520 2233
3280af22
NIS
2234 PL_Sv = tSv; /* Restore global temporaries. */
2235 PL_Xpv = tXpv;
79072805
LW
2236 return;
2237}
4e35701f
NIS
2238
2239
51371543 2240#ifdef PERL_OBJECT
51371543
GS
2241#include "XSUB.h"
2242#endif
2243
2244static void
2245restore_magic(pTHXo_ void *p)
2246{
2247 dTHR;
48944bdf 2248 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2249 SV* sv = mgs->mgs_sv;
2250
2251 if (!sv)
2252 return;
2253
2254 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2255 {
2256 if (mgs->mgs_flags)
2257 SvFLAGS(sv) |= mgs->mgs_flags;
2258 else
2259 mg_magical(sv);
2260 if (SvGMAGICAL(sv))
2261 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2262 }
2263
2264 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2265
2266 /* If we're still on top of the stack, pop us off. (That condition
2267 * will be satisfied if restore_magic was called explicitly, but *not*
2268 * if it's being called via leave_scope.)
2269 * The reason for doing this is that otherwise, things like sv_2cv()
2270 * may leave alloc gunk on the savestack, and some code
2271 * (e.g. sighandler) doesn't expect that...
2272 */
2273 if (PL_savestack_ix == mgs->mgs_ss_ix)
2274 {
2275 I32 popval = SSPOPINT;
c76ac1ee 2276 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2277 PL_savestack_ix -= 2;
2278 popval = SSPOPINT;
2279 assert(popval == SAVEt_ALLOC);
2280 popval = SSPOPINT;
2281 PL_savestack_ix -= popval;
2282 }
2283
2284}
2285
2286static void
2287unwind_handler_stack(pTHXo_ void *p)
2288{
2289 dTHR;
2290 U32 flags = *(U32*)p;
2291
2292 if (flags & 1)
2293 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2294 /* cxstack_ix-- Not needed, die already unwound it. */
2295 if (flags & 64)
2296 SvREFCNT_dec(sig_sv);
2297}