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