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