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