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