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