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