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