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