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