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