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