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