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