This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] add pTHX_ parameter to new_warnings_bitfield()
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
11/*
12 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
79072805
LW
14 */
15
ccfc67b7
JH
16/*
17=head1 Magical Functions
166f8a29
DM
18
19"Magic" is special data attached to SV structures in order to give them
20"magical" properties. When any Perl code tries to read from, or assign to,
21an SV marked as magical, it calls the 'get' or 'set' function associated
22with that SV's magic. A get is called prior to reading an SV, in order to
ddfa107c 23give it a chance to update its internal value (get on $. writes the line
166f8a29
DM
24number of the last read filehandle into to the SV's IV slot), while
25set is called after an SV has been written to, in order to allow it to make
ddfa107c 26use of its changed value (set on $/ copies the SV's new value to the
166f8a29
DM
27PL_rs global variable).
28
29Magic is implemented as a linked list of MAGIC structures attached to the
30SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31of functions that implement the get(), set(), length() etc functions,
32plus space for some flags and pointers. For example, a tied variable has
33a MAGIC structure that contains a pointer to the object associated with the
34tie.
35
ccfc67b7
JH
36*/
37
79072805 38#include "EXTERN.h"
864dbfa3 39#define PERL_IN_MG_C
79072805
LW
40#include "perl.h"
41
5cd24f17 42#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
b7953727
JH
43# ifdef I_GRP
44# include <grp.h>
45# endif
188ea221
CS
46#endif
47
757f63d8
SP
48#if defined(HAS_SETGROUPS)
49# ifndef NGROUPS
50# define NGROUPS 32
51# endif
52#endif
53
17aa7f3d
JH
54#ifdef __hpux
55# include <sys/pstat.h>
56#endif
57
8aad04aa
JH
58#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
59Signal_t Perl_csighandler(int sig, ...);
60#else
e69880a5 61Signal_t Perl_csighandler(int sig);
8aad04aa 62#endif
e69880a5 63
9cffb111
OS
64#ifdef __Lynx__
65/* Missing protos on LynxOS */
66void setruid(uid_t id);
67void seteuid(uid_t id);
68void setrgid(uid_t id);
69void setegid(uid_t id);
70#endif
71
c07a80fd
PP
72/*
73 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
74 */
75
76struct magic_state {
77 SV* mgs_sv;
78 U32 mgs_flags;
455ece5e 79 I32 mgs_ss_ix;
c07a80fd 80};
455ece5e 81/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
82
83STATIC void
8fb26106 84S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
c07a80fd 85{
97aff369 86 dVAR;
455ece5e 87 MGS* mgs;
c07a80fd 88 assert(SvMAGICAL(sv));
d8b2590f
NC
89 /* Turning READONLY off for a copy-on-write scalar (including shared
90 hash keys) is a bad idea. */
765f542d 91 if (SvIsCOW(sv))
9a265e59 92 sv_force_normal_flags(sv, 0);
c07a80fd 93
8772537c 94 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
95
96 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd
PP
97 mgs->mgs_sv = sv;
98 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 99 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd
PP
100
101 SvMAGICAL_off(sv);
102 SvREADONLY_off(sv);
c268c2a6 103 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
104}
105
954c1994
GS
106/*
107=for apidoc mg_magical
108
109Turns on the magical status of an SV. See C<sv_magic>.
110
111=cut
112*/
113
8990e307 114void
864dbfa3 115Perl_mg_magical(pTHX_ SV *sv)
8990e307 116{
e1ec3a88 117 const MAGIC* mg;
96a5add6 118 PERL_UNUSED_CONTEXT;
8990e307 119 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 120 const MGVTBL* const vtbl = mg->mg_virtual;
8990e307 121 if (vtbl) {
2b260de0 122 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
123 SvGMAGICAL_on(sv);
124 if (vtbl->svt_set)
125 SvSMAGICAL_on(sv);
2b260de0 126 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307
LW
127 SvRMAGICAL_on(sv);
128 }
129 }
130}
131
954c1994
GS
132/*
133=for apidoc mg_get
134
135Do magic after a value is retrieved from the SV. See C<sv_magic>.
136
137=cut
138*/
139
79072805 140int
864dbfa3 141Perl_mg_get(pTHX_ SV *sv)
79072805 142{
97aff369 143 dVAR;
35a4481c 144 const I32 mgs_ix = SSNEW(sizeof(MGS));
fe2774ed 145 const bool was_temp = (bool)SvTEMP(sv);
0723351e 146 int have_new = 0;
ff76feab 147 MAGIC *newmg, *head, *cur, *mg;
20135930 148 /* guard against sv having being freed midway by holding a private
6683b158
NC
149 reference. */
150
151 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
152 cause the SV's buffer to get stolen (and maybe other stuff).
153 So restore it.
154 */
b37c2d43 155 sv_2mortal(SvREFCNT_inc_simple(sv));
6683b158
NC
156 if (!was_temp) {
157 SvTEMP_off(sv);
158 }
159
455ece5e 160 save_magic(mgs_ix, sv);
463ee0b2 161
ff76feab
AMS
162 /* We must call svt_get(sv, mg) for each valid entry in the linked
163 list of magic. svt_get() may delete the current entry, add new
164 magic to the head of the list, or upgrade the SV. AMS 20010810 */
165
166 newmg = cur = head = mg = SvMAGIC(sv);
167 while (mg) {
35a4481c 168 const MGVTBL * const vtbl = mg->mg_virtual;
ff76feab 169
2b260de0 170 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 171 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
b77f7d40 172
58f82c5c
DM
173 /* guard against magic having been deleted - eg FETCH calling
174 * untie */
175 if (!SvMAGIC(sv))
176 break;
b77f7d40 177
ff76feab
AMS
178 /* Don't restore the flags for this entry if it was deleted. */
179 if (mg->mg_flags & MGf_GSKIP)
180 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
a0d0e21e 181 }
ff76feab
AMS
182
183 mg = mg->mg_moremagic;
184
0723351e 185 if (have_new) {
ff76feab
AMS
186 /* Have we finished with the new entries we saw? Start again
187 where we left off (unless there are more new entries). */
188 if (mg == head) {
0723351e 189 have_new = 0;
ff76feab
AMS
190 mg = cur;
191 head = newmg;
192 }
193 }
194
195 /* Were any new entries added? */
0723351e
NC
196 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
197 have_new = 1;
ff76feab
AMS
198 cur = mg;
199 mg = newmg;
760ac839 200 }
79072805 201 }
463ee0b2 202
8772537c 203 restore_magic(INT2PTR(void *, (IV)mgs_ix));
6683b158
NC
204
205 if (SvREFCNT(sv) == 1) {
206 /* We hold the last reference to this SV, which implies that the
207 SV was deleted as a side effect of the routines we called. */
0c34ef67 208 SvOK_off(sv);
6683b158 209 }
79072805
LW
210 return 0;
211}
212
954c1994
GS
213/*
214=for apidoc mg_set
215
216Do magic after a value is assigned to the SV. See C<sv_magic>.
217
218=cut
219*/
220
79072805 221int
864dbfa3 222Perl_mg_set(pTHX_ SV *sv)
79072805 223{
97aff369 224 dVAR;
35a4481c 225 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 226 MAGIC* mg;
463ee0b2
LW
227 MAGIC* nextmg;
228
455ece5e 229 save_magic(mgs_ix, sv);
463ee0b2
LW
230
231 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
e1ec3a88 232 const MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 233 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
234 if (mg->mg_flags & MGf_GSKIP) {
235 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 236 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 237 }
2b260de0 238 if (vtbl && vtbl->svt_set)
fc0dc3b3 239 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 240 }
463ee0b2 241
8772537c 242 restore_magic(INT2PTR(void*, (IV)mgs_ix));
79072805
LW
243 return 0;
244}
245
954c1994
GS
246/*
247=for apidoc mg_length
248
249Report on the SV's length. See C<sv_magic>.
250
251=cut
252*/
253
79072805 254U32
864dbfa3 255Perl_mg_length(pTHX_ SV *sv)
79072805 256{
97aff369 257 dVAR;
79072805 258 MAGIC* mg;
463ee0b2 259 STRLEN len;
463ee0b2 260
79072805 261 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 262 const MGVTBL * const vtbl = mg->mg_virtual;
2b260de0 263 if (vtbl && vtbl->svt_len) {
35a4481c 264 const I32 mgs_ix = SSNEW(sizeof(MGS));
455ece5e 265 save_magic(mgs_ix, sv);
a0d0e21e 266 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 267 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
8772537c 268 restore_magic(INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
269 return len;
270 }
271 }
272
35a4481c 273 if (DO_UTF8(sv)) {
10516c54 274 const U8 *s = (U8*)SvPV_const(sv, len);
5636d518
DB
275 len = Perl_utf8_length(aTHX_ s, s + len);
276 }
277 else
10516c54 278 (void)SvPV_const(sv, len);
463ee0b2 279 return len;
79072805
LW
280}
281
8fb26106 282I32
864dbfa3 283Perl_mg_size(pTHX_ SV *sv)
93965878
NIS
284{
285 MAGIC* mg;
ac27b0f5 286
93965878 287 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 288 const MGVTBL* const vtbl = mg->mg_virtual;
2b260de0 289 if (vtbl && vtbl->svt_len) {
35a4481c
AL
290 const I32 mgs_ix = SSNEW(sizeof(MGS));
291 I32 len;
455ece5e 292 save_magic(mgs_ix, sv);
93965878 293 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 294 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
8772537c 295 restore_magic(INT2PTR(void*, (IV)mgs_ix));
93965878
NIS
296 return len;
297 }
298 }
299
300 switch(SvTYPE(sv)) {
301 case SVt_PVAV:
35a4481c 302 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
93965878
NIS
303 case SVt_PVHV:
304 /* FIXME */
305 default:
cea2e8a9 306 Perl_croak(aTHX_ "Size magic not implemented");
93965878
NIS
307 break;
308 }
309 return 0;
310}
311
954c1994
GS
312/*
313=for apidoc mg_clear
314
315Clear something magical that the SV represents. See C<sv_magic>.
316
317=cut
318*/
319
79072805 320int
864dbfa3 321Perl_mg_clear(pTHX_ SV *sv)
79072805 322{
35a4481c 323 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 324 MAGIC* mg;
463ee0b2 325
455ece5e 326 save_magic(mgs_ix, sv);
463ee0b2 327
79072805 328 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 329 const MGVTBL* const vtbl = mg->mg_virtual;
a0d0e21e 330 /* omit GSKIP -- never set here */
727405f8 331
2b260de0 332 if (vtbl && vtbl->svt_clear)
fc0dc3b3 333 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 334 }
463ee0b2 335
8772537c 336 restore_magic(INT2PTR(void*, (IV)mgs_ix));
79072805
LW
337 return 0;
338}
339
954c1994
GS
340/*
341=for apidoc mg_find
342
343Finds the magic pointer for type matching the SV. See C<sv_magic>.
344
345=cut
346*/
347
93a17b20 348MAGIC*
35a4481c 349Perl_mg_find(pTHX_ const SV *sv, int type)
93a17b20 350{
96a5add6 351 PERL_UNUSED_CONTEXT;
35a4481c
AL
352 if (sv) {
353 MAGIC *mg;
354 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
355 if (mg->mg_type == type)
356 return mg;
357 }
93a17b20 358 }
5f66b61c 359 return NULL;
93a17b20
LW
360}
361
954c1994
GS
362/*
363=for apidoc mg_copy
364
365Copies the magic from one SV to another. See C<sv_magic>.
366
367=cut
368*/
369
79072805 370int
864dbfa3 371Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 372{
463ee0b2 373 int count = 0;
79072805 374 MAGIC* mg;
463ee0b2 375 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 376 const MGVTBL* const vtbl = mg->mg_virtual;
68795e93
NIS
377 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
378 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
379 }
823a54a3
AL
380 else {
381 const char type = mg->mg_type;
382 if (isUPPER(type)) {
383 sv_magic(nsv,
384 (type == PERL_MAGIC_tied)
385 ? SvTIED_obj(sv, mg)
386 : (type == PERL_MAGIC_regdata && mg->mg_obj)
387 ? sv
388 : mg->mg_obj,
389 toLOWER(type), key, klen);
390 count++;
391 }
79072805 392 }
79072805 393 }
463ee0b2 394 return count;
79072805
LW
395}
396
954c1994 397/*
0cbee0a4
DM
398=for apidoc mg_localize
399
400Copy some of the magic from an existing SV to new localized version of
401that SV. Container magic (eg %ENV, $1, tie) gets copied, value magic
402doesn't (eg taint, pos).
403
404=cut
405*/
406
407void
408Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
409{
97aff369 410 dVAR;
0cbee0a4
DM
411 MAGIC *mg;
412 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
92e67595 413 MGVTBL* const vtbl = mg->mg_virtual;
0cbee0a4
DM
414 switch (mg->mg_type) {
415 /* value magic types: don't copy */
416 case PERL_MAGIC_bm:
417 case PERL_MAGIC_fm:
418 case PERL_MAGIC_regex_global:
419 case PERL_MAGIC_nkeys:
420#ifdef USE_LOCALE_COLLATE
421 case PERL_MAGIC_collxfrm:
422#endif
423 case PERL_MAGIC_qr:
424 case PERL_MAGIC_taint:
425 case PERL_MAGIC_vec:
426 case PERL_MAGIC_vstring:
427 case PERL_MAGIC_utf8:
428 case PERL_MAGIC_substr:
429 case PERL_MAGIC_defelem:
430 case PERL_MAGIC_arylen:
431 case PERL_MAGIC_pos:
432 case PERL_MAGIC_backref:
433 case PERL_MAGIC_arylen_p:
434 case PERL_MAGIC_rhash:
435 case PERL_MAGIC_symtab:
436 continue;
437 }
438
a5063e7c
DM
439 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
440 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
441 else
0cbee0a4
DM
442 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
443 mg->mg_ptr, mg->mg_len);
a5063e7c 444
0cbee0a4
DM
445 /* container types should remain read-only across localization */
446 SvFLAGS(nsv) |= SvREADONLY(sv);
447 }
448
449 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
450 SvFLAGS(nsv) |= SvMAGICAL(sv);
451 PL_localizing = 1;
452 SvSETMAGIC(nsv);
453 PL_localizing = 0;
454 }
455}
456
457/*
954c1994
GS
458=for apidoc mg_free
459
460Free any magic storage used by the SV. See C<sv_magic>.
461
462=cut
463*/
464
79072805 465int
864dbfa3 466Perl_mg_free(pTHX_ SV *sv)
79072805
LW
467{
468 MAGIC* mg;
469 MAGIC* moremagic;
470 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
35a4481c 471 const MGVTBL* const vtbl = mg->mg_virtual;
79072805 472 moremagic = mg->mg_moremagic;
2b260de0 473 if (vtbl && vtbl->svt_free)
fc0dc3b3 474 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 475 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
979acdb5 476 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
88e89b8a 477 Safefree(mg->mg_ptr);
565764a8 478 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 479 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 480 }
b881518d
JH
481 if (mg->mg_flags & MGf_REFCOUNTED)
482 SvREFCNT_dec(mg->mg_obj);
79072805
LW
483 Safefree(mg);
484 }
b162af07 485 SvMAGIC_set(sv, NULL);
79072805
LW
486 return 0;
487}
488
79072805 489#include <signal.h>
79072805 490
942e002e 491U32
864dbfa3 492Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 493{
97aff369 494 dVAR;
8772537c 495 PERL_UNUSED_ARG(sv);
6cef1e77 496
0bd48802
AL
497 if (PL_curpm) {
498 register const REGEXP * const rx = PM_GETRE(PL_curpm);
499 if (rx) {
500 return mg->mg_obj
501 ? rx->nparens /* @+ */
502 : rx->lastparen; /* @- */
503 }
8f580fb8 504 }
ac27b0f5 505
942e002e 506 return (U32)-1;
6cef1e77
IZ
507}
508
509int
864dbfa3 510Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 511{
97aff369 512 dVAR;
0bd48802
AL
513 if (PL_curpm) {
514 register const REGEXP * const rx = PM_GETRE(PL_curpm);
515 if (rx) {
516 register const I32 paren = mg->mg_len;
517 register I32 s;
518 register I32 t;
519 if (paren < 0)
520 return 0;
521 if (paren <= (I32)rx->nparens &&
522 (s = rx->startp[paren]) != -1 &&
523 (t = rx->endp[paren]) != -1)
524 {
525 register I32 i;
526 if (mg->mg_obj) /* @+ */
527 i = t;
528 else /* @- */
529 i = s;
530
531 if (i > 0 && RX_MATCH_UTF8(rx)) {
532 const char * const b = rx->subbeg;
533 if (b)
534 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
535 }
727405f8 536
0bd48802 537 sv_setiv(sv, i);
1aa99e6b 538 }
0bd48802 539 }
6cef1e77
IZ
540 }
541 return 0;
542}
543
e4b89193 544int
a29d06ed
MG
545Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
546{
d4c19fe8
AL
547 PERL_UNUSED_ARG(sv);
548 PERL_UNUSED_ARG(mg);
a29d06ed 549 Perl_croak(aTHX_ PL_no_modify);
0dbb1585 550 NORETURN_FUNCTION_END;
a29d06ed
MG
551}
552
93a17b20 553U32
864dbfa3 554Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20 555{
97aff369 556 dVAR;
93a17b20 557 register I32 paren;
93a17b20 558 register I32 i;
dd374669 559 register const REGEXP *rx;
a197cbdd 560 I32 s1, t1;
93a17b20
LW
561
562 switch (*mg->mg_ptr) {
563 case '1': case '2': case '3': case '4':
564 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 565 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 566
ffc61ed2 567 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 568 getparen:
eb160463 569 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
570 (s1 = rx->startp[paren]) != -1 &&
571 (t1 = rx->endp[paren]) != -1)
bbce6d69 572 {
cf93c79d 573 i = t1 - s1;
a197cbdd 574 getlen:
a30b2f1f 575 if (i > 0 && RX_MATCH_UTF8(rx)) {
a28509cc 576 const char * const s = rx->subbeg + s1;
768c67ee
JH
577 const U8 *ep;
578 STRLEN el;
ffc61ed2 579
6d5fa195 580 i = t1 - s1;
768c67ee
JH
581 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
582 i = el;
a197cbdd 583 }
ffc61ed2 584 if (i < 0)
0844c848 585 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 586 return i;
93a17b20 587 }
235bddc8
NIS
588 else {
589 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 590 report_uninit(sv);
235bddc8
NIS
591 }
592 }
593 else {
594 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 595 report_uninit(sv);
93a17b20 596 }
748a9306 597 return 0;
93a17b20 598 case '+':
aaa362c4 599 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 600 paren = rx->lastparen;
13f57bf8
CS
601 if (paren)
602 goto getparen;
93a17b20 603 }
748a9306 604 return 0;
a01268b5
JH
605 case '\016': /* ^N */
606 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
607 paren = rx->lastcloseparen;
608 if (paren)
609 goto getparen;
610 }
611 return 0;
93a17b20 612 case '`':
aaa362c4 613 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
614 if (rx->startp[0] != -1) {
615 i = rx->startp[0];
a197cbdd
GS
616 if (i > 0) {
617 s1 = 0;
618 t1 = i;
619 goto getlen;
620 }
93a17b20 621 }
93a17b20 622 }
748a9306 623 return 0;
93a17b20 624 case '\'':
aaa362c4 625 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
626 if (rx->endp[0] != -1) {
627 i = rx->sublen - rx->endp[0];
a197cbdd
GS
628 if (i > 0) {
629 s1 = rx->endp[0];
630 t1 = rx->sublen;
631 goto getlen;
632 }
93a17b20 633 }
93a17b20 634 }
748a9306 635 return 0;
93a17b20
LW
636 }
637 magic_get(sv,mg);
2d8e6c8d 638 if (!SvPOK(sv) && SvNIOK(sv)) {
8b6b16e7 639 sv_2pv(sv, 0);
2d8e6c8d 640 }
93a17b20
LW
641 if (SvPOK(sv))
642 return SvCUR(sv);
643 return 0;
644}
645
ad3296c6 646#define SvRTRIM(sv) STMT_START { \
eae92ea0
GA
647 if (SvPOK(sv)) { \
648 STRLEN len = SvCUR(sv); \
649 char * const p = SvPVX(sv); \
8e6b4db6
PC
650 while (len > 0 && isSPACE(p[len-1])) \
651 --len; \
652 SvCUR_set(sv, len); \
653 p[len] = '\0'; \
654 } \
ad3296c6
SH
655} STMT_END
656
79072805 657int
864dbfa3 658Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805 659{
27da23d5 660 dVAR;
79072805 661 register I32 paren;
35272f84 662 register char *s = NULL;
79072805 663 register I32 i;
d9f97599 664 register REGEXP *rx;
823a54a3
AL
665 const char * const remaining = mg->mg_ptr + 1;
666 const char nextchar = *remaining;
79072805
LW
667
668 switch (*mg->mg_ptr) {
748a9306 669 case '\001': /* ^A */
3280af22 670 sv_setsv(sv, PL_bodytarget);
748a9306 671 break;
e5218da5 672 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
823a54a3 673 if (nextchar == '\0') {
e5218da5
GA
674 sv_setiv(sv, (IV)PL_minus_c);
675 }
823a54a3 676 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
e5218da5
GA
677 sv_setiv(sv, (IV)STATUS_NATIVE);
678 }
49460fe6
NIS
679 break;
680
79072805 681 case '\004': /* ^D */
aea4f609 682 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 683 break;
28f23441 684 case '\005': /* ^E */
823a54a3 685 if (nextchar == '\0') {
4b645107 686#if defined(MACOS_TRADITIONAL)
0a378802
JH
687 {
688 char msg[256];
727405f8 689
0a378802 690 sv_setnv(sv,(double)gMacPerl_OSErr);
727405f8 691 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
0a378802 692 }
4b645107 693#elif defined(VMS)
0a378802
JH
694 {
695# include <descrip.h>
696# include <starlet.h>
697 char msg[255];
698 $DESCRIPTOR(msgdsc,msg);
699 sv_setnv(sv,(NV) vaxc$errno);
700 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
701 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
702 else
c69006e4 703 sv_setpvn(sv,"",0);
0a378802 704 }
4b645107 705#elif defined(OS2)
0a378802
JH
706 if (!(_emx_env & 0x200)) { /* Under DOS */
707 sv_setnv(sv, (NV)errno);
708 sv_setpv(sv, errno ? Strerror(errno) : "");
709 } else {
710 if (errno != errno_isOS2) {
823a54a3 711 const int tmp = _syserrno();
0a378802
JH
712 if (tmp) /* 2nd call to _syserrno() makes it 0 */
713 Perl_rc = tmp;
714 }
715 sv_setnv(sv, (NV)Perl_rc);
716 sv_setpv(sv, os2error(Perl_rc));
717 }
4b645107 718#elif defined(WIN32)
0a378802 719 {
d4c19fe8 720 const DWORD dwErr = GetLastError();
0a378802 721 sv_setnv(sv, (NV)dwErr);
823a54a3 722 if (dwErr) {
0a378802
JH
723 PerlProc_GetOSError(sv, dwErr);
724 }
725 else
c69006e4 726 sv_setpvn(sv, "", 0);
0a378802
JH
727 SetLastError(dwErr);
728 }
22fae026 729#else
f6c8f21d 730 {
8772537c 731 const int saveerrno = errno;
f6c8f21d
RGS
732 sv_setnv(sv, (NV)errno);
733 sv_setpv(sv, errno ? Strerror(errno) : "");
734 errno = saveerrno;
735 }
28f23441 736#endif
ad3296c6 737 SvRTRIM(sv);
0a378802
JH
738 SvNOK_on(sv); /* what a wonderful hack! */
739 }
823a54a3 740 else if (strEQ(remaining, "NCODING"))
0a378802
JH
741 sv_setsv(sv, PL_encoding);
742 break;
79072805 743 case '\006': /* ^F */
3280af22 744 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 745 break;
a0d0e21e 746 case '\010': /* ^H */
3280af22 747 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 748 break;
9d116dd7 749 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
750 if (PL_inplace)
751 sv_setpv(sv, PL_inplace);
79072805 752 else
3280af22 753 sv_setsv(sv, &PL_sv_undef);
79072805 754 break;
ac27b0f5 755 case '\017': /* ^O & ^OPEN */
823a54a3 756 if (nextchar == '\0') {
ac27b0f5 757 sv_setpv(sv, PL_osname);
3511154c
DM
758 SvTAINTED_off(sv);
759 }
823a54a3 760 else if (strEQ(remaining, "PEN")) {
ac27b0f5
NIS
761 if (!PL_compiling.cop_io)
762 sv_setsv(sv, &PL_sv_undef);
763 else {
764 sv_setsv(sv, PL_compiling.cop_io);
765 }
766 }
28f23441 767 break;
79072805 768 case '\020': /* ^P */
3280af22 769 sv_setiv(sv, (IV)PL_perldb);
79072805 770 break;
fb73857a 771 case '\023': /* ^S */
823a54a3 772 if (nextchar == '\0') {
3280af22 773 if (PL_lex_state != LEX_NOTPARSING)
0c34ef67 774 SvOK_off(sv);
3280af22 775 else if (PL_in_eval)
6dc8a9e4 776 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
777 else
778 sv_setiv(sv, 0);
d58bf5aa 779 }
fb73857a 780 break;
79072805 781 case '\024': /* ^T */
823a54a3 782 if (nextchar == '\0') {
88e89b8a 783#ifdef BIG_TIME
7c36658b 784 sv_setnv(sv, PL_basetime);
88e89b8a 785#else
7c36658b 786 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 787#endif
7c36658b 788 }
823a54a3 789 else if (strEQ(remaining, "AINT"))
9aa05f58
RGS
790 sv_setiv(sv, PL_tainting
791 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
792 : 0);
7c36658b 793 break;
e07ea26a 794 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
823a54a3 795 if (strEQ(remaining, "NICODE"))
a05d7ebb 796 sv_setuv(sv, (UV) PL_unicode);
823a54a3 797 else if (strEQ(remaining, "TF8LOCALE"))
7cebcbc0 798 sv_setuv(sv, (UV) PL_utf8locale);
e07ea26a
NC
799 else if (strEQ(remaining, "TF8CACHE"))
800 sv_setiv(sv, (IV) PL_utf8cache);
fde18df1
JH
801 break;
802 case '\027': /* ^W & $^WARNING_BITS */
823a54a3 803 if (nextchar == '\0')
4438c4b7 804 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
823a54a3 805 else if (strEQ(remaining, "ARNING_BITS")) {
013b78e8 806 if (PL_compiling.cop_warnings == pWARN_NONE) {
4438c4b7 807 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
013b78e8
RGS
808 }
809 else if (PL_compiling.cop_warnings == pWARN_STD) {
810 sv_setpvn(
811 sv,
812 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
813 WARNsize
814 );
815 }
d3a7d8c7 816 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca
RGS
817 /* Get the bit mask for $warnings::Bits{all}, because
818 * it could have been extended by warnings::register */
819 SV **bits_all;
823a54a3 820 HV * const bits=get_hv("warnings::Bits", FALSE);
017a3ce5 821 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
822 sv_setsv(sv, *bits_all);
823 }
824 else {
825 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
826 }
ac27b0f5 827 }
4438c4b7 828 else {
72dc9ed5
NC
829 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
830 *PL_compiling.cop_warnings);
ac27b0f5 831 }
d3a7d8c7 832 SvPOK_only(sv);
4438c4b7 833 }
79072805
LW
834 break;
835 case '1': case '2': case '3': case '4':
836 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 837 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
838 I32 s1, t1;
839
a863c7d1
MB
840 /*
841 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
842 * XXX Does the new way break anything?
843 */
ffc61ed2 844 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 845 getparen:
eb160463 846 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
847 (s1 = rx->startp[paren]) != -1 &&
848 (t1 = rx->endp[paren]) != -1)
bbce6d69 849 {
cf93c79d
IZ
850 i = t1 - s1;
851 s = rx->subbeg + s1;
c2b4a044 852 assert(rx->subbeg);
c2e66d9e 853
13f57bf8 854 getrx:
748a9306 855 if (i >= 0) {
fabdb6c0 856 const int oldtainted = PL_tainted;
f6ba9920 857 TAINT_NOT;
cf93c79d 858 sv_setpvn(sv, s, i);
f6ba9920 859 PL_tainted = oldtainted;
a30b2f1f 860 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
7e2040f0
GS
861 SvUTF8_on(sv);
862 else
863 SvUTF8_off(sv);
e9814ee1
HS
864 if (PL_tainting) {
865 if (RX_MATCH_TAINTED(rx)) {
823a54a3 866 MAGIC* const mg = SvMAGIC(sv);
e9814ee1
HS
867 MAGIC* mgt;
868 PL_tainted = 1;
b162af07 869 SvMAGIC_set(sv, mg->mg_moremagic);
e9814ee1
HS
870 SvTAINT(sv);
871 if ((mgt = SvMAGIC(sv))) {
872 mg->mg_moremagic = mgt;
b162af07 873 SvMAGIC_set(sv, mg);
e9814ee1
HS
874 }
875 } else
876 SvTAINTED_off(sv);
877 }
748a9306
LW
878 break;
879 }
79072805 880 }
79072805 881 }
3280af22 882 sv_setsv(sv,&PL_sv_undef);
79072805
LW
883 break;
884 case '+':
aaa362c4 885 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 886 paren = rx->lastparen;
a0d0e21e
LW
887 if (paren)
888 goto getparen;
79072805 889 }
3280af22 890 sv_setsv(sv,&PL_sv_undef);
79072805 891 break;
a01268b5
JH
892 case '\016': /* ^N */
893 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
894 paren = rx->lastcloseparen;
895 if (paren)
896 goto getparen;
897 }
898 sv_setsv(sv,&PL_sv_undef);
899 break;
79072805 900 case '`':
aaa362c4 901 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
902 if ((s = rx->subbeg) && rx->startp[0] != -1) {
903 i = rx->startp[0];
13f57bf8 904 goto getrx;
79072805 905 }
79072805 906 }
3280af22 907 sv_setsv(sv,&PL_sv_undef);
79072805
LW
908 break;
909 case '\'':
aaa362c4 910 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
911 if (rx->subbeg && rx->endp[0] != -1) {
912 s = rx->subbeg + rx->endp[0];
913 i = rx->sublen - rx->endp[0];
13f57bf8 914 goto getrx;
79072805 915 }
79072805 916 }
3280af22 917 sv_setsv(sv,&PL_sv_undef);
79072805
LW
918 break;
919 case '.':
3280af22 920 if (GvIO(PL_last_in_gv)) {
357c8808 921 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805 922 }
79072805
LW
923 break;
924 case '?':
809a5acc 925 {
809a5acc 926 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 927#ifdef COMPLEX_STATUS
6b88bc9c
GS
928 LvTARGOFF(sv) = PL_statusvalue;
929 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 930#endif
809a5acc 931 }
79072805
LW
932 break;
933 case '^':
0daa599b
RGS
934 if (GvIOp(PL_defoutgv))
935 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
936 if (s)
937 sv_setpv(sv,s);
938 else {
3280af22 939 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
940 sv_catpv(sv,"_TOP");
941 }
942 break;
943 case '~':
0daa599b
RGS
944 if (GvIOp(PL_defoutgv))
945 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 946 if (!s)
3280af22 947 s = GvENAME(PL_defoutgv);
79072805
LW
948 sv_setpv(sv,s);
949 break;
79072805 950 case '=':
0daa599b
RGS
951 if (GvIOp(PL_defoutgv))
952 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
953 break;
954 case '-':
0daa599b
RGS
955 if (GvIOp(PL_defoutgv))
956 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
957 break;
958 case '%':
0daa599b
RGS
959 if (GvIOp(PL_defoutgv))
960 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 961 break;
79072805
LW
962 case ':':
963 break;
964 case '/':
965 break;
966 case '[':
fc15ae8f 967 WITH_THR(sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop)));
79072805
LW
968 break;
969 case '|':
0daa599b
RGS
970 if (GvIOp(PL_defoutgv))
971 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
972 break;
973 case ',':
79072805
LW
974 break;
975 case '\\':
b2ce0fda 976 if (PL_ors_sv)
f28098ff 977 sv_copypv(sv, PL_ors_sv);
79072805 978 break;
79072805 979 case '!':
a5f75d66 980#ifdef VMS
65202027 981 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 982 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 983#else
88e89b8a 984 {
8772537c 985 const int saveerrno = errno;
65202027 986 sv_setnv(sv, (NV)errno);
88e89b8a 987#ifdef OS2
ed344e4f
IZ
988 if (errno == errno_isOS2 || errno == errno_isOS2_set)
989 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 990 else
a5f75d66 991#endif
2304df62 992 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
993 errno = saveerrno;
994 }
995#endif
ad3296c6 996 SvRTRIM(sv);
946ec16e 997 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
998 break;
999 case '<':
3280af22 1000 sv_setiv(sv, (IV)PL_uid);
79072805
LW
1001 break;
1002 case '>':
3280af22 1003 sv_setiv(sv, (IV)PL_euid);
79072805
LW
1004 break;
1005 case '(':
3280af22 1006 sv_setiv(sv, (IV)PL_gid);
79072805
LW
1007 goto add_groups;
1008 case ')':
3280af22 1009 sv_setiv(sv, (IV)PL_egid);
79072805 1010 add_groups:
79072805 1011#ifdef HAS_GETGROUPS
79072805 1012 {
57d7c65e 1013 Groups_t *gary = NULL;
fb45abb2 1014 I32 i, num_groups = getgroups(0, gary);
57d7c65e
JC
1015 Newx(gary, num_groups, Groups_t);
1016 num_groups = getgroups(num_groups, gary);
fb45abb2
GA
1017 for (i = 0; i < num_groups; i++)
1018 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
57d7c65e 1019 Safefree(gary);
79072805 1020 }
155aba94 1021 (void)SvIOK_on(sv); /* what a wonderful hack! */
cd70abae 1022#endif
79072805 1023 break;
cd39f2b6 1024#ifndef MACOS_TRADITIONAL
79072805
LW
1025 case '0':
1026 break;
cd39f2b6 1027#endif
79072805 1028 }
a0d0e21e 1029 return 0;
79072805
LW
1030}
1031
1032int
864dbfa3 1033Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1034{
8772537c 1035 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
1036
1037 if (uf && uf->uf_val)
24f81a43 1038 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
1039 return 0;
1040}
1041
1042int
864dbfa3 1043Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 1044{
27da23d5 1045 dVAR;
9ae3433d
RGS
1046 STRLEN len = 0, klen;
1047 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
fabdb6c0 1048 const char * const ptr = MgPV_const(mg,klen);
88e89b8a 1049 my_setenv(ptr, s);
1e422769 1050
a0d0e21e
LW
1051#ifdef DYNAMIC_ENV_FETCH
1052 /* We just undefd an environment var. Is a replacement */
1053 /* waiting in the wings? */
1054 if (!len) {
fabdb6c0
AL
1055 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1056 if (valp)
4ab59fcc 1057 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
a0d0e21e
LW
1058 }
1059#endif
1e422769 1060
39e571d4 1061#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
1062 /* And you'll never guess what the dog had */
1063 /* in its mouth... */
3280af22 1064 if (PL_tainting) {
1e422769
PP
1065 MgTAINTEDDIR_off(mg);
1066#ifdef VMS
5aabfad6 1067 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
b8ffc8df 1068 char pathbuf[256], eltbuf[256], *cp, *elt;
c623ac67 1069 Stat_t sbuf;
1e422769
PP
1070 int i = 0, j = 0;
1071
b8ffc8df
RGS
1072 strncpy(eltbuf, s, 255);
1073 eltbuf[255] = 0;
1074 elt = eltbuf;
1e422769
PP
1075 do { /* DCL$PATH may be a search list */
1076 while (1) { /* as may dev portion of any element */
1077 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1078 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1079 cando_by_name(S_IWUSR,0,elt) ) {
1080 MgTAINTEDDIR_on(mg);
1081 return 0;
1082 }
1083 }
bd61b366 1084 if ((cp = strchr(elt, ':')) != NULL)
1e422769
PP
1085 *cp = '\0';
1086 if (my_trnlnm(elt, eltbuf, j++))
1087 elt = eltbuf;
1088 else
1089 break;
1090 }
1091 j = 0;
1092 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1093 }
1094#endif /* VMS */
5aabfad6 1095 if (s && klen == 4 && strEQ(ptr,"PATH")) {
8772537c 1096 const char * const strend = s + len;
463ee0b2
LW
1097
1098 while (s < strend) {
96827780 1099 char tmpbuf[256];
c623ac67 1100 Stat_t st;
5f74f29c 1101 I32 i;
96827780 1102 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 1103 s, strend, ':', &i);
463ee0b2 1104 s++;
96827780
MB
1105 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1106 || *tmpbuf != '/'
c6ed36e1 1107 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1108 MgTAINTEDDIR_on(mg);
1e422769
PP
1109 return 0;
1110 }
463ee0b2 1111 }
79072805
LW
1112 }
1113 }
39e571d4 1114#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1115
79072805
LW
1116 return 0;
1117}
1118
1119int
864dbfa3 1120Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1121{
8772537c 1122 PERL_UNUSED_ARG(sv);
bd61b366 1123 my_setenv(MgPV_nolen_const(mg),NULL);
85e6fe83
LW
1124 return 0;
1125}
1126
88e89b8a 1127int
864dbfa3 1128Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 1129{
97aff369 1130 dVAR;
65e66c80 1131 PERL_UNUSED_ARG(mg);
b0269e46 1132#if defined(VMS)
cea2e8a9 1133 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1134#else
3280af22 1135 if (PL_localizing) {
fb73857a 1136 HE* entry;
b0269e46 1137 my_clearenv();
fb73857a 1138 hv_iterinit((HV*)sv);
155aba94 1139 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1140 I32 keylen;
1141 my_setenv(hv_iterkey(entry, &keylen),
b83604b4 1142 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
fb73857a
PP
1143 }
1144 }
1145#endif
1146 return 0;
1147}
1148
1149int
864dbfa3 1150Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1151{
27da23d5 1152 dVAR;
8772537c
AL
1153 PERL_UNUSED_ARG(sv);
1154 PERL_UNUSED_ARG(mg);
b0269e46
AB
1155#if defined(VMS)
1156 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1157#else
1158 my_clearenv();
1159#endif
3e3baf6d 1160 return 0;
66b1d557
HM
1161}
1162
64ca3a65 1163#ifndef PERL_MICRO
2d4fcd5e
AJ
1164#ifdef HAS_SIGPROCMASK
1165static void
1166restore_sigmask(pTHX_ SV *save_sv)
1167{
0bd48802 1168 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
2d4fcd5e
AJ
1169 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1170}
1171#endif
66b1d557 1172int
864dbfa3 1173Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1174{
97aff369 1175 dVAR;
88e89b8a 1176 /* Are we fetching a signal entry? */
8772537c 1177 const I32 i = whichsig(MgPV_nolen_const(mg));
e02bfb16 1178 if (i > 0) {
22c35a8c
GS
1179 if(PL_psig_ptr[i])
1180 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1181 else {
85b332e2 1182 Sighandler_t sigstate;
2e34cc90 1183 sigstate = rsignal_state(i);
23ada85b 1184#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1185 if (PL_sig_handlers_initted && PL_sig_ignoring[i]) sigstate = SIG_IGN;
2e34cc90
CL
1186#endif
1187#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1188 if (PL_sig_handlers_initted && PL_sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1189#endif
88e89b8a 1190 /* cache state so we don't fetch it again */
8aad04aa 1191 if(sigstate == (Sighandler_t) SIG_IGN)
88e89b8a
PP
1192 sv_setpv(sv,"IGNORE");
1193 else
3280af22 1194 sv_setsv(sv,&PL_sv_undef);
b37c2d43 1195 PL_psig_ptr[i] = SvREFCNT_inc_simple(sv);
88e89b8a
PP
1196 SvTEMP_off(sv);
1197 }
1198 }
1199 return 0;
1200}
1201int
864dbfa3 1202Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1203{
2d4fcd5e
AJ
1204 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1205 * refactoring might be in order.
1206 */
27da23d5 1207 dVAR;
8772537c
AL
1208 register const char * const s = MgPV_nolen_const(mg);
1209 PERL_UNUSED_ARG(sv);
2d4fcd5e 1210 if (*s == '_') {
cbbf8932 1211 SV** svp = NULL;
2d4fcd5e
AJ
1212 if (strEQ(s,"__DIE__"))
1213 svp = &PL_diehook;
1214 else if (strEQ(s,"__WARN__"))
1215 svp = &PL_warnhook;
1216 else
1217 Perl_croak(aTHX_ "No such hook: %s", s);
27da23d5 1218 if (svp && *svp) {
8772537c 1219 SV * const to_dec = *svp;
cbbf8932 1220 *svp = NULL;
2d4fcd5e
AJ
1221 SvREFCNT_dec(to_dec);
1222 }
1223 }
1224 else {
2d4fcd5e 1225 /* Are we clearing a signal entry? */
8772537c 1226 const I32 i = whichsig(s);
e02bfb16 1227 if (i > 0) {
2d4fcd5e
AJ
1228#ifdef HAS_SIGPROCMASK
1229 sigset_t set, save;
1230 SV* save_sv;
1231 /* Avoid having the signal arrive at a bad time, if possible. */
1232 sigemptyset(&set);
1233 sigaddset(&set,i);
1234 sigprocmask(SIG_BLOCK, &set, &save);
1235 ENTER;
1236 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1237 SAVEFREESV(save_sv);
1238 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1239#endif
1240 PERL_ASYNC_CHECK();
1241#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1242 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2d4fcd5e
AJ
1243#endif
1244#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1245 PL_sig_defaulting[i] = 1;
5c1546dc 1246 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e 1247#else
8aad04aa 1248 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2d4fcd5e
AJ
1249#endif
1250 if(PL_psig_name[i]) {
1251 SvREFCNT_dec(PL_psig_name[i]);
1252 PL_psig_name[i]=0;
1253 }
1254 if(PL_psig_ptr[i]) {
6136c704 1255 SV * const to_dec=PL_psig_ptr[i];
2d4fcd5e
AJ
1256 PL_psig_ptr[i]=0;
1257 LEAVE;
1258 SvREFCNT_dec(to_dec);
1259 }
1260 else
1261 LEAVE;
1262 }
88e89b8a
PP
1263 }
1264 return 0;
1265}
3d37d572 1266
dd374669
AL
1267static void
1268S_raise_signal(pTHX_ int sig)
0a8e0eff 1269{
97aff369 1270 dVAR;
0a8e0eff
NIS
1271 /* Set a flag to say this signal is pending */
1272 PL_psig_pend[sig]++;
1273 /* And one to say _a_ signal is pending */
1274 PL_sig_pending = 1;
1275}
1276
1277Signal_t
8aad04aa
JH
1278#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1279Perl_csighandler(int sig, ...)
1280#else
0a8e0eff 1281Perl_csighandler(int sig)
8aad04aa 1282#endif
0a8e0eff 1283{
1018e26f
NIS
1284#ifdef PERL_GET_SIG_CONTEXT
1285 dTHXa(PERL_GET_SIG_CONTEXT);
1286#else
85b332e2
CL
1287 dTHX;
1288#endif
23ada85b 1289#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1290 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1291 if (PL_sig_ignoring[sig]) return;
85b332e2 1292#endif
2e34cc90 1293#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1294 if (PL_sig_defaulting[sig])
2e34cc90
CL
1295#ifdef KILL_BY_SIGPRC
1296 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1297#else
1298 exit(1);
1299#endif
1300#endif
4ffa73a3
JH
1301 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1302 /* Call the perl level handler now--
1303 * with risk we may be in malloc() etc. */
1304 (*PL_sighandlerp)(sig);
1305 else
dd374669 1306 S_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1307}
1308
2e34cc90
CL
1309#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1310void
1311Perl_csighandler_init(void)
1312{
1313 int sig;
27da23d5 1314 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1315
1316 for (sig = 1; sig < SIG_SIZE; sig++) {
1317#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1318 dTHX;
27da23d5 1319 PL_sig_defaulting[sig] = 1;
5c1546dc 1320 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1321#endif
1322#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1323 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1324#endif
1325 }
27da23d5 1326 PL_sig_handlers_initted = 1;
2e34cc90
CL
1327}
1328#endif
1329
0a8e0eff
NIS
1330void
1331Perl_despatch_signals(pTHX)
1332{
97aff369 1333 dVAR;
0a8e0eff
NIS
1334 int sig;
1335 PL_sig_pending = 0;
1336 for (sig = 1; sig < SIG_SIZE; sig++) {
1337 if (PL_psig_pend[sig]) {
25da4428
JH
1338 PERL_BLOCKSIG_ADD(set, sig);
1339 PL_psig_pend[sig] = 0;
1340 PERL_BLOCKSIG_BLOCK(set);
f5203343 1341 (*PL_sighandlerp)(sig);
25da4428 1342 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1343 }
1344 }
1345}
1346
85e6fe83 1347int
864dbfa3 1348Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1349{
27da23d5 1350 dVAR;
79072805 1351 I32 i;
cbbf8932 1352 SV** svp = NULL;
2d4fcd5e
AJ
1353 /* Need to be careful with SvREFCNT_dec(), because that can have side
1354 * effects (due to closures). We must make sure that the new disposition
1355 * is in place before it is called.
1356 */
cbbf8932 1357 SV* to_dec = NULL;
e72dc28c 1358 STRLEN len;
2d4fcd5e
AJ
1359#ifdef HAS_SIGPROCMASK
1360 sigset_t set, save;
1361 SV* save_sv;
1362#endif
a0d0e21e 1363
d5263905 1364 register const char *s = MgPV_const(mg,len);
748a9306
LW
1365 if (*s == '_') {
1366 if (strEQ(s,"__DIE__"))
3280af22 1367 svp = &PL_diehook;
748a9306 1368 else if (strEQ(s,"__WARN__"))
3280af22 1369 svp = &PL_warnhook;
748a9306 1370 else
cea2e8a9 1371 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1372 i = 0;
4633a7c4 1373 if (*svp) {
2d4fcd5e 1374 to_dec = *svp;
cbbf8932 1375 *svp = NULL;
4633a7c4 1376 }
748a9306
LW
1377 }
1378 else {
1379 i = whichsig(s); /* ...no, a brick */
86d86cad 1380 if (i <= 0) {
e476b1b5 1381 if (ckWARN(WARN_SIGNAL))
9014280d 1382 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1383 return 0;
1384 }
2d4fcd5e
AJ
1385#ifdef HAS_SIGPROCMASK
1386 /* Avoid having the signal arrive at a bad time, if possible. */
1387 sigemptyset(&set);
1388 sigaddset(&set,i);
1389 sigprocmask(SIG_BLOCK, &set, &save);
1390 ENTER;
1391 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1392 SAVEFREESV(save_sv);
1393 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1394#endif
1395 PERL_ASYNC_CHECK();
2e34cc90 1396#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1397 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1398#endif
23ada85b 1399#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1400 PL_sig_ignoring[i] = 0;
85b332e2 1401#endif
2e34cc90 1402#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1403 PL_sig_defaulting[i] = 0;
2e34cc90 1404#endif
22c35a8c 1405 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1406 to_dec = PL_psig_ptr[i];
b37c2d43 1407 PL_psig_ptr[i] = SvREFCNT_inc_simple(sv);
88e89b8a 1408 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1409 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1410 SvREADONLY_on(PL_psig_name[i]);
748a9306 1411 }
a0d0e21e 1412 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1413 if (i) {
5c1546dc 1414 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1415#ifdef HAS_SIGPROCMASK
1416 LEAVE;
1417#endif
1418 }
748a9306 1419 else
b37c2d43 1420 *svp = SvREFCNT_inc_simple_NN(sv);
2d4fcd5e
AJ
1421 if(to_dec)
1422 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1423 return 0;
1424 }
e72dc28c 1425 s = SvPV_force(sv,len);
748a9306 1426 if (strEQ(s,"IGNORE")) {
85b332e2 1427 if (i) {
23ada85b 1428#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1429 PL_sig_ignoring[i] = 1;
5c1546dc 1430 (void)rsignal(i, PL_csighandlerp);
85b332e2 1431#else
8aad04aa 1432 (void)rsignal(i, (Sighandler_t) SIG_IGN);
85b332e2 1433#endif
2d4fcd5e 1434 }
748a9306
LW
1435 }
1436 else if (strEQ(s,"DEFAULT") || !*s) {
1437 if (i)
2e34cc90
CL
1438#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1439 {
27da23d5 1440 PL_sig_defaulting[i] = 1;
5c1546dc 1441 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1442 }
1443#else
8aad04aa 1444 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2e34cc90 1445#endif
748a9306 1446 }
79072805 1447 else {
5aabfad6
PP
1448 /*
1449 * We should warn if HINT_STRICT_REFS, but without
1450 * access to a known hint bit in a known OP, we can't
1451 * tell whether HINT_STRICT_REFS is in force or not.
1452 */
46fc3d4c 1453 if (!strchr(s,':') && !strchr(s,'\''))
89529cee 1454 Perl_sv_insert(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"));
748a9306 1455 if (i)
5c1546dc 1456 (void)rsignal(i, PL_csighandlerp);
748a9306 1457 else
b37c2d43 1458 *svp = SvREFCNT_inc_simple(sv);
79072805 1459 }
2d4fcd5e
AJ
1460#ifdef HAS_SIGPROCMASK
1461 if(i)
1462 LEAVE;
1463#endif
1464 if(to_dec)
1465 SvREFCNT_dec(to_dec);
79072805
LW
1466 return 0;
1467}
64ca3a65 1468#endif /* !PERL_MICRO */
79072805
LW
1469
1470int
864dbfa3 1471Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1472{
97aff369 1473 dVAR;
8772537c
AL
1474 PERL_UNUSED_ARG(sv);
1475 PERL_UNUSED_ARG(mg);
3280af22 1476 PL_sub_generation++;
463ee0b2
LW
1477 return 0;
1478}
1479
1480int
864dbfa3 1481Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1482{
97aff369 1483 dVAR;
8772537c
AL
1484 PERL_UNUSED_ARG(sv);
1485 PERL_UNUSED_ARG(mg);
a0d0e21e 1486 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1487 PL_amagic_generation++;
463ee0b2 1488
a0d0e21e
LW
1489 return 0;
1490}
463ee0b2 1491
946ec16e 1492int
864dbfa3 1493Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1494{
dd374669 1495 HV * const hv = (HV*)LvTARG(sv);
6ff81951 1496 I32 i = 0;
8772537c 1497 PERL_UNUSED_ARG(mg);
7719e241 1498
6ff81951 1499 if (hv) {
497b47a8
JH
1500 (void) hv_iterinit(hv);
1501 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1502 i = HvKEYS(hv);
1503 else {
1504 while (hv_iternext(hv))
1505 i++;
1506 }
6ff81951
GS
1507 }
1508
1509 sv_setiv(sv, (IV)i);
1510 return 0;
1511}
1512
1513int
864dbfa3 1514Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1515{
8772537c 1516 PERL_UNUSED_ARG(mg);
946ec16e
PP
1517 if (LvTARG(sv)) {
1518 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1519 }
1520 return 0;
ac27b0f5 1521}
946ec16e 1522
e336de0d 1523/* caller is responsible for stack switching/cleanup */
565764a8 1524STATIC int
e1ec3a88 1525S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
a0d0e21e 1526{
97aff369 1527 dVAR;
a0d0e21e 1528 dSP;
463ee0b2 1529
924508f0
GS
1530 PUSHMARK(SP);
1531 EXTEND(SP, n);
33c27489 1532 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1533 if (n > 1) {
93965878 1534 if (mg->mg_ptr) {
565764a8 1535 if (mg->mg_len >= 0)
79cb57f6 1536 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1537 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1538 PUSHs((SV*)mg->mg_ptr);
1539 }
14befaf4 1540 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1541 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1542 }
1543 }
1544 if (n > 2) {
1545 PUSHs(val);
88e89b8a 1546 }
463ee0b2
LW
1547 PUTBACK;
1548
864dbfa3 1549 return call_method(meth, flags);
946ec16e
PP
1550}
1551
76e3520e 1552STATIC int
e1ec3a88 1553S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
a0d0e21e 1554{
27da23d5 1555 dVAR; dSP;
463ee0b2 1556
a0d0e21e
LW
1557 ENTER;
1558 SAVETMPS;
e788e7d3 1559 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1560
33c27489 1561 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1562 sv_setsv(sv, *PL_stack_sp--);
93965878 1563 }
463ee0b2 1564
d3acc0f7 1565 POPSTACK;
a0d0e21e
LW
1566 FREETMPS;
1567 LEAVE;
1568 return 0;
1569}
463ee0b2 1570
a0d0e21e 1571int
864dbfa3 1572Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1573{
a0d0e21e
LW
1574 if (mg->mg_ptr)
1575 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1576 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1577 return 0;
1578}
1579
1580int
864dbfa3 1581Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1582{
27da23d5 1583 dVAR; dSP;
a60c0954 1584 ENTER;
e788e7d3 1585 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1586 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1587 POPSTACK;
a60c0954 1588 LEAVE;
463ee0b2
LW
1589 return 0;
1590}
1591
1592int
864dbfa3 1593Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1594{
a0d0e21e
LW
1595 return magic_methpack(sv,mg,"DELETE");
1596}
463ee0b2 1597
93965878
NIS
1598
1599U32
864dbfa3 1600Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1601{
27da23d5 1602 dVAR; dSP;
93965878
NIS
1603 U32 retval = 0;
1604
1605 ENTER;
1606 SAVETMPS;
e788e7d3 1607 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1608 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1609 sv = *PL_stack_sp--;
a60c0954 1610 retval = (U32) SvIV(sv)-1;
93965878 1611 }
d3acc0f7 1612 POPSTACK;
93965878
NIS
1613 FREETMPS;
1614 LEAVE;
1615 return retval;
1616}
1617
cea2e8a9
GS
1618int
1619Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1620{
27da23d5 1621 dVAR; dSP;
463ee0b2 1622
e336de0d 1623 ENTER;
e788e7d3 1624 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1625 PUSHMARK(SP);
33c27489 1626 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1627 PUTBACK;
864dbfa3 1628 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1629 POPSTACK;
a60c0954 1630 LEAVE;
a3bcc51e 1631
463ee0b2
LW
1632 return 0;
1633}
1634
1635int
864dbfa3 1636Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1637{
27da23d5 1638 dVAR; dSP;
35a4481c 1639 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1640
1641 ENTER;
a0d0e21e 1642 SAVETMPS;
e788e7d3 1643 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1644 PUSHMARK(SP);
1645 EXTEND(SP, 2);
33c27489 1646 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1647 if (SvOK(key))
1648 PUSHs(key);
1649 PUTBACK;
1650
864dbfa3 1651 if (call_method(meth, G_SCALAR))
3280af22 1652 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1653
d3acc0f7 1654 POPSTACK;
a0d0e21e
LW
1655 FREETMPS;
1656 LEAVE;
79072805
LW
1657 return 0;
1658}
1659
1660int
864dbfa3 1661Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1662{
1663 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1664}
a0d0e21e 1665
a3bcc51e
TP
1666SV *
1667Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1668{
27da23d5 1669 dVAR; dSP;
a3bcc51e 1670 SV *retval = &PL_sv_undef;
8772537c
AL
1671 SV * const tied = SvTIED_obj((SV*)hv, mg);
1672 HV * const pkg = SvSTASH((SV*)SvRV(tied));
a3bcc51e
TP
1673
1674 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1675 SV *key;
bfcb3514 1676 if (HvEITER_get(hv))
a3bcc51e
TP
1677 /* we are in an iteration so the hash cannot be empty */
1678 return &PL_sv_yes;
1679 /* no xhv_eiter so now use FIRSTKEY */
1680 key = sv_newmortal();
1681 magic_nextpack((SV*)hv, mg, key);
bfcb3514 1682 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1683 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1684 }
1685
1686 /* there is a SCALAR method that we can call */
1687 ENTER;
1688 PUSHSTACKi(PERLSI_MAGIC);
1689 PUSHMARK(SP);
1690 EXTEND(SP, 1);
1691 PUSHs(tied);
1692 PUTBACK;
1693
1694 if (call_method("SCALAR", G_SCALAR))
1695 retval = *PL_stack_sp--;
1696 POPSTACK;
1697 LEAVE;
1698 return retval;
1699}
1700
a0d0e21e 1701int
864dbfa3 1702Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1703{
97aff369 1704 dVAR;
8772537c
AL
1705 GV * const gv = PL_DBline;
1706 const I32 i = SvTRUE(sv);
1707 SV ** const svp = av_fetch(GvAV(gv),
01b8bcb7 1708 atoi(MgPV_nolen_const(mg)), FALSE);
8772537c
AL
1709 if (svp && SvIOKp(*svp)) {
1710 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1711 if (o) {
1712 /* set or clear breakpoint in the relevant control op */
1713 if (i)
1714 o->op_flags |= OPf_SPECIAL;
1715 else
1716 o->op_flags &= ~OPf_SPECIAL;
1717 }
5df8de69 1718 }
79072805
LW
1719 return 0;
1720}
1721
1722int
8772537c 1723Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
79072805 1724{
97aff369 1725 dVAR;
8772537c 1726 const AV * const obj = (AV*)mg->mg_obj;
83bf042f 1727 if (obj) {
fc15ae8f 1728 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
83bf042f
NC
1729 } else {
1730 SvOK_off(sv);
1731 }
79072805
LW
1732 return 0;
1733}
1734
1735int
864dbfa3 1736Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1737{
97aff369 1738 dVAR;
8772537c 1739 AV * const obj = (AV*)mg->mg_obj;
83bf042f 1740 if (obj) {
fc15ae8f 1741 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
83bf042f
NC
1742 } else {
1743 if (ckWARN(WARN_MISC))
1744 Perl_warner(aTHX_ packWARN(WARN_MISC),
1745 "Attempt to set length of freed array");
1746 }
1747 return 0;
1748}
1749
1750int
1751Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1752{
97aff369 1753 dVAR;
53c1dcc0 1754 PERL_UNUSED_ARG(sv);
94f3782b
DM
1755 /* during global destruction, mg_obj may already have been freed */
1756 if (PL_in_clean_all)
1ea47f64 1757 return 0;
94f3782b 1758
83bf042f
NC
1759 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1760
1761 if (mg) {
1762 /* arylen scalar holds a pointer back to the array, but doesn't own a
1763 reference. Hence the we (the array) are about to go away with it
1764 still pointing at us. Clear its pointer, else it would be pointing
1765 at free memory. See the comment in sv_magic about reference loops,
1766 and why it can't own a reference to us. */
1767 mg->mg_obj = 0;
1768 }
a0d0e21e
LW
1769 return 0;
1770}
1771
1772int
864dbfa3 1773Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1774{
97aff369 1775 dVAR;
8772537c 1776 SV* const lsv = LvTARG(sv);
3881461a 1777 PERL_UNUSED_ARG(mg);
ac27b0f5 1778
a0d0e21e 1779 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
3881461a
AL
1780 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1781 if (found && found->mg_len >= 0) {
1782 I32 i = found->mg_len;
7e2040f0 1783 if (DO_UTF8(lsv))
a0ed51b3 1784 sv_pos_b2u(lsv, &i);
fc15ae8f 1785 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
a0d0e21e
LW
1786 return 0;
1787 }
1788 }
0c34ef67 1789 SvOK_off(sv);
a0d0e21e
LW
1790 return 0;
1791}
1792
1793int
864dbfa3 1794Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1795{
97aff369 1796 dVAR;
8772537c 1797 SV* const lsv = LvTARG(sv);
a0d0e21e
LW
1798 SSize_t pos;
1799 STRLEN len;
c00206c8 1800 STRLEN ulen = 0;
3881461a 1801 MAGIC *found;
a0d0e21e 1802
3881461a 1803 PERL_UNUSED_ARG(mg);
ac27b0f5 1804
a0d0e21e 1805 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
3881461a
AL
1806 found = mg_find(lsv, PERL_MAGIC_regex_global);
1807 else
1808 found = NULL;
1809 if (!found) {
a0d0e21e
LW
1810 if (!SvOK(sv))
1811 return 0;
d83f0a82
NC
1812#ifdef PERL_OLD_COPY_ON_WRITE
1813 if (SvIsCOW(lsv))
1814 sv_force_normal_flags(lsv, 0);
1815#endif
3881461a 1816 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
d83f0a82 1817 NULL, 0);
a0d0e21e
LW
1818 }
1819 else if (!SvOK(sv)) {
3881461a 1820 found->mg_len = -1;
a0d0e21e
LW
1821 return 0;
1822 }
1823 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1824
fc15ae8f 1825 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
a0ed51b3 1826
7e2040f0 1827 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1828 ulen = sv_len_utf8(lsv);
1829 if (ulen)
1830 len = ulen;
a0ed51b3
LW
1831 }
1832
a0d0e21e
LW
1833 if (pos < 0) {
1834 pos += len;
1835 if (pos < 0)
1836 pos = 0;
1837 }
eb160463 1838 else if (pos > (SSize_t)len)
a0d0e21e 1839 pos = len;
a0ed51b3
LW
1840
1841 if (ulen) {
1842 I32 p = pos;
1843 sv_pos_u2b(lsv, &p, 0);
1844 pos = p;
1845 }
727405f8 1846
3881461a
AL
1847 found->mg_len = pos;
1848 found->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1849
79072805
LW
1850 return 0;
1851}
1852
1853int
864dbfa3 1854Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1855{
79072805 1856 GV* gv;
8772537c
AL
1857 PERL_UNUSED_ARG(mg);
1858
79072805
LW
1859 if (!SvOK(sv))
1860 return 0;
180488f8
NC
1861 if (SvFLAGS(sv) & SVp_SCREAM
1862 && (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVGV)) {
1863 /* We're actually already a typeglob, so don't need the stuff below.
1864 */
1865 return 0;
1866 }
f776e3cd 1867 gv = gv_fetchsv(sv, GV_ADD, SVt_PVGV);
79072805
LW
1868 if (sv == (SV*)gv)
1869 return 0;
1870 if (GvGP(sv))
88e89b8a 1871 gp_free((GV*)sv);
79072805 1872 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1873 return 0;
1874}
1875
1876int
864dbfa3 1877Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1878{
1879 STRLEN len;
35a4481c 1880 SV * const lsv = LvTARG(sv);
b83604b4 1881 const char * const tmps = SvPV_const(lsv,len);
6ff81951
GS
1882 I32 offs = LvTARGOFF(sv);
1883 I32 rem = LvTARGLEN(sv);
8772537c 1884 PERL_UNUSED_ARG(mg);
6ff81951 1885
9aa983d2
JH
1886 if (SvUTF8(lsv))
1887 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1888 if (offs > (I32)len)
6ff81951 1889 offs = len;
eb160463 1890 if (rem + offs > (I32)len)
6ff81951
GS
1891 rem = len - offs;
1892 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1893 if (SvUTF8(lsv))
2ef4b674 1894 SvUTF8_on(sv);
6ff81951
GS
1895 return 0;
1896}
1897
1898int
864dbfa3 1899Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1900{
97aff369 1901 dVAR;
9aa983d2 1902 STRLEN len;
b83604b4 1903 const char *tmps = SvPV_const(sv, len);
dd374669 1904 SV * const lsv = LvTARG(sv);
9aa983d2
JH
1905 I32 lvoff = LvTARGOFF(sv);
1906 I32 lvlen = LvTARGLEN(sv);
8772537c 1907 PERL_UNUSED_ARG(mg);
075a4a2b 1908
1aa99e6b 1909 if (DO_UTF8(sv)) {
9aa983d2
JH
1910 sv_utf8_upgrade(lsv);
1911 sv_pos_u2b(lsv, &lvoff, &lvlen);
1912 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 1913 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
1914 SvUTF8_on(lsv);
1915 }
9bf12eaf 1916 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1917 sv_pos_u2b(lsv, &lvoff, &lvlen);
b76f3ce2 1918 LvTARGLEN(sv) = len;
e95af362 1919 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1920 sv_insert(lsv, lvoff, lvlen, tmps, len);
1921 Safefree(tmps);
1aa99e6b 1922 }
b76f3ce2
GB
1923 else {
1924 sv_insert(lsv, lvoff, lvlen, tmps, len);
1925 LvTARGLEN(sv) = len;
1926 }
1927
1aa99e6b 1928
79072805
LW
1929 return 0;
1930}
1931
1932int
864dbfa3 1933Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1934{
97aff369 1935 dVAR;
8772537c 1936 PERL_UNUSED_ARG(sv);
27cc343c 1937 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
463ee0b2
LW
1938 return 0;
1939}
1940
1941int
864dbfa3 1942Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1943{
97aff369 1944 dVAR;
8772537c 1945 PERL_UNUSED_ARG(sv);
0a9c116b
DM
1946 /* update taint status unless we're restoring at scope exit */
1947 if (PL_localizing != 2) {
1948 if (PL_tainted)
1949 mg->mg_len |= 1;
1950 else
1951 mg->mg_len &= ~1;
1952 }
463ee0b2
LW
1953 return 0;
1954}
1955
1956int
864dbfa3 1957Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1958{
35a4481c 1959 SV * const lsv = LvTARG(sv);
8772537c 1960 PERL_UNUSED_ARG(mg);
6ff81951 1961
6136c704
AL
1962 if (lsv)
1963 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1964 else
0c34ef67 1965 SvOK_off(sv);
6ff81951 1966
6ff81951
GS
1967 return 0;
1968}
1969
1970int
864dbfa3 1971Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 1972{
8772537c 1973 PERL_UNUSED_ARG(mg);
79072805
LW
1974 do_vecset(sv); /* XXX slurp this routine */
1975 return 0;
1976}
1977
1978int
864dbfa3 1979Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1980{
97aff369 1981 dVAR;
a0714e2c 1982 SV *targ = NULL;
5f05dabc 1983 if (LvTARGLEN(sv)) {
68dc0745 1984 if (mg->mg_obj) {
8772537c
AL
1985 SV * const ahv = LvTARG(sv);
1986 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
6d822dc4
MS
1987 if (he)
1988 targ = HeVAL(he);
68dc0745
PP
1989 }
1990 else {
8772537c 1991 AV* const av = (AV*)LvTARG(sv);
68dc0745
PP
1992 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1993 targ = AvARRAY(av)[LvTARGOFF(sv)];
1994 }
3280af22 1995 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1996 /* somebody else defined it for us */
1997 SvREFCNT_dec(LvTARG(sv));
b37c2d43 1998 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
68dc0745
PP
1999 LvTARGLEN(sv) = 0;
2000 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2001 mg->mg_obj = NULL;
68dc0745
PP
2002 mg->mg_flags &= ~MGf_REFCOUNTED;
2003 }
5f05dabc 2004 }
71be2cbc
PP
2005 else
2006 targ = LvTARG(sv);
3280af22 2007 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
2008 return 0;
2009}
2010
2011int
864dbfa3 2012Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 2013{
8772537c 2014 PERL_UNUSED_ARG(mg);
71be2cbc 2015 if (LvTARGLEN(sv))
68dc0745
PP
2016 vivify_defelem(sv);
2017 if (LvTARG(sv)) {
5f05dabc 2018 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
2019 SvSETMAGIC(LvTARG(sv));
2020 }
5f05dabc
PP
2021 return 0;
2022}
2023
71be2cbc 2024void
864dbfa3 2025Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 2026{
97aff369 2027 dVAR;
74e13ce4 2028 MAGIC *mg;
a0714e2c 2029 SV *value = NULL;
71be2cbc 2030
14befaf4 2031 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 2032 return;
68dc0745 2033 if (mg->mg_obj) {
8772537c
AL
2034 SV * const ahv = LvTARG(sv);
2035 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
6d822dc4
MS
2036 if (he)
2037 value = HeVAL(he);
3280af22 2038 if (!value || value == &PL_sv_undef)
ce5030a2 2039 Perl_croak(aTHX_ PL_no_helem_sv, mg->mg_obj);
71be2cbc 2040 }
68dc0745 2041 else {
8772537c 2042 AV* const av = (AV*)LvTARG(sv);
5aabfad6 2043 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
a0714e2c 2044 LvTARG(sv) = NULL; /* array can't be extended */
68dc0745 2045 else {
d4c19fe8 2046 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 2047 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 2048 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
2049 }
2050 }
b37c2d43 2051 SvREFCNT_inc_simple_void(value);
68dc0745
PP
2052 SvREFCNT_dec(LvTARG(sv));
2053 LvTARG(sv) = value;
71be2cbc 2054 LvTARGLEN(sv) = 0;
68dc0745 2055 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2056 mg->mg_obj = NULL;
68dc0745 2057 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
2058}
2059
2060int
864dbfa3 2061Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 2062{
86f55936 2063 return Perl_sv_kill_backrefs(aTHX_ sv, (AV*)mg->mg_obj);
810b8aa5
GS
2064}
2065
2066int
864dbfa3 2067Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2068{
96a5add6 2069 PERL_UNUSED_CONTEXT;
565764a8 2070 mg->mg_len = -1;
c6496cc7 2071 SvSCREAM_off(sv);
93a17b20
LW
2072 return 0;
2073}
2074
2075int
864dbfa3 2076Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 2077{
8772537c 2078 PERL_UNUSED_ARG(mg);
14befaf4 2079 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
2080 SvVALID_off(sv);
2081 return 0;
2082}
2083
2084int
864dbfa3 2085Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 2086{
8772537c 2087 PERL_UNUSED_ARG(mg);
14befaf4 2088 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
2089 SvCOMPILED_off(sv);
2090 return 0;
2091}
2092
2093int
864dbfa3 2094Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2095{
35a4481c 2096 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
2097
2098 if (uf && uf->uf_set)
24f81a43 2099 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2100 return 0;
2101}
2102
c277df42 2103int
faf82a0b
AE
2104Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2105{
8772537c 2106 PERL_UNUSED_ARG(mg);
faf82a0b
AE
2107 sv_unmagic(sv, PERL_MAGIC_qr);
2108 return 0;
2109}
2110
2111int
864dbfa3 2112Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42 2113{
97aff369 2114 dVAR;
8772537c
AL
2115 regexp * const re = (regexp *)mg->mg_obj;
2116 PERL_UNUSED_ARG(sv);
2117
c277df42
IZ
2118 ReREFCNT_dec(re);
2119 return 0;
2120}
2121
7a4c00b4 2122#ifdef USE_LOCALE_COLLATE
79072805 2123int
864dbfa3 2124Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
2125{
2126 /*
838b5b74 2127 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2128 * and vanished with a faint plop.
2129 */
96a5add6 2130 PERL_UNUSED_CONTEXT;
8772537c 2131 PERL_UNUSED_ARG(sv);
7a4c00b4
PP
2132 if (mg->mg_ptr) {
2133 Safefree(mg->mg_ptr);
2134 mg->mg_ptr = NULL;
565764a8 2135 mg->mg_len = -1;
7a4c00b4 2136 }
bbce6d69
PP
2137 return 0;
2138}
7a4c00b4 2139#endif /* USE_LOCALE_COLLATE */
bbce6d69 2140
7e8c5dac
HS
2141/* Just clear the UTF-8 cache data. */
2142int
2143Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2144{
96a5add6 2145 PERL_UNUSED_CONTEXT;
8772537c 2146 PERL_UNUSED_ARG(sv);
7e8c5dac 2147 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
3881461a 2148 mg->mg_ptr = NULL;
7e8c5dac
HS
2149 mg->mg_len = -1; /* The mg_len holds the len cache. */
2150 return 0;
2151}
2152
bbce6d69 2153int
864dbfa3 2154Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2155{
97aff369 2156 dVAR;
e1ec3a88 2157 register const char *s;
79072805 2158 I32 i;
8990e307 2159 STRLEN len;
79072805 2160 switch (*mg->mg_ptr) {
748a9306 2161 case '\001': /* ^A */
3280af22 2162 sv_setsv(PL_bodytarget, sv);
748a9306 2163 break;
49460fe6 2164 case '\003': /* ^C */
38ab35f8 2165 PL_minus_c = (bool)SvIV(sv);
49460fe6
NIS
2166 break;
2167
79072805 2168 case '\004': /* ^D */
b4ab917c 2169#ifdef DEBUGGING
b83604b4 2170 s = SvPV_nolen_const(sv);
ddcf8bc1 2171 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
79072805 2172 DEBUG_x(dump_all());
b4ab917c 2173#else
38ab35f8 2174 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
b4ab917c 2175#endif
79072805 2176 break;
28f23441 2177 case '\005': /* ^E */
d0063567 2178 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2179#ifdef MACOS_TRADITIONAL
38ab35f8 2180 gMacPerl_OSErr = SvIV(sv);
28f23441 2181#else
cd39f2b6 2182# ifdef VMS
38ab35f8 2183 set_vaxc_errno(SvIV(sv));
048c1ddf 2184# else
cd39f2b6 2185# ifdef WIN32
d0063567 2186 SetLastError( SvIV(sv) );
cd39f2b6 2187# else
9fed8b87 2188# ifdef OS2
38ab35f8 2189 os2_setsyserrno(SvIV(sv));
9fed8b87 2190# else
d0063567 2191 /* will anyone ever use this? */
38ab35f8 2192 SETERRNO(SvIV(sv), 4);
cd39f2b6 2193# endif
048c1ddf
IZ
2194# endif
2195# endif
22fae026 2196#endif
d0063567
DK
2197 }
2198 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2199 if (PL_encoding)
2200 SvREFCNT_dec(PL_encoding);
2201 if (SvOK(sv) || SvGMAGICAL(sv)) {
2202 PL_encoding = newSVsv(sv);
2203 }
2204 else {
a0714e2c 2205 PL_encoding = NULL;
d0063567
DK
2206 }
2207 }
2208 break;
79072805 2209 case '\006': /* ^F */
38ab35f8 2210 PL_maxsysfd = SvIV(sv);
79072805 2211 break;
a0d0e21e 2212 case '\010': /* ^H */
38ab35f8 2213 PL_hints = SvIV(sv);
a0d0e21e 2214 break;
9d116dd7 2215 case '\011': /* ^I */ /* NOT \t in EBCDIC */
43c5f42d 2216 Safefree(PL_inplace);
bd61b366 2217 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
da78da6e 2218 break;
28f23441 2219 case '\017': /* ^O */
ac27b0f5 2220 if (*(mg->mg_ptr+1) == '\0') {
43c5f42d 2221 Safefree(PL_osname);
bd61b366 2222 PL_osname = NULL;
3511154c
DM
2223 if (SvOK(sv)) {
2224 TAINT_PROPER("assigning to $^O");
2e0de35c 2225 PL_osname = savesvpv(sv);
3511154c 2226 }
ac27b0f5
NIS
2227 }
2228 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2229 if (!PL_compiling.cop_io)
2230 PL_compiling.cop_io = newSVsv(sv);
2231 else
2232 sv_setsv(PL_compiling.cop_io,sv);
2233 }
28f23441 2234 break;
79072805 2235 case '\020': /* ^P */
38ab35f8 2236 PL_perldb = SvIV(sv);
f2a7f298 2237 if (PL_perldb && !PL_DBsingle)
1ee4443e 2238 init_debugger();
79072805
LW
2239 break;
2240 case '\024': /* ^T */
88e89b8a 2241#ifdef BIG_TIME
6b88bc9c 2242 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2243#else
38ab35f8 2244 PL_basetime = (Time_t)SvIV(sv);
88e89b8a 2245#endif
79072805 2246 break;
e07ea26a
NC
2247 case '\025': /* ^UTF8CACHE */
2248 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2249 PL_utf8cache = (signed char) sv_2iv(sv);
2250 }
2251 break;
fde18df1 2252 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2253 if (*(mg->mg_ptr+1) == '\0') {
2254 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
38ab35f8 2255 i = SvIV(sv);
ac27b0f5 2256 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2257 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2258 }
599cee73 2259 }
0a378802 2260 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2261 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2262 if (!SvPOK(sv) && PL_localizing) {
2263 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2264 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2265 break;
2266 }
f4fc7782 2267 {
b5477537 2268 STRLEN len, i;
d3a7d8c7 2269 int accumulate = 0 ;
f4fc7782 2270 int any_fatals = 0 ;
b83604b4 2271 const char * const ptr = SvPV_const(sv, len) ;
f4fc7782
JH
2272 for (i = 0 ; i < len ; ++i) {
2273 accumulate |= ptr[i] ;
2274 any_fatals |= (ptr[i] & 0xAA) ;
2275 }
d3a7d8c7
GS
2276 if (!accumulate)
2277 PL_compiling.cop_warnings = pWARN_NONE;
72dc9ed5
NC
2278 /* Yuck. I can't see how to abstract this: */
2279 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2280 WARN_ALL) && !any_fatals) {
f4fc7782
JH
2281 PL_compiling.cop_warnings = pWARN_ALL;
2282 PL_dowarn |= G_WARN_ONCE ;
727405f8 2283 }
d3a7d8c7 2284 else {
72dc9ed5
NC
2285 STRLEN len;
2286 const char *const p = SvPV_const(sv, len);
2287
2288 PL_compiling.cop_warnings
8ee4cf24 2289 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
72dc9ed5
NC
2290 p, len);
2291
d3a7d8c7
GS
2292 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2293 PL_dowarn |= G_WARN_ONCE ;
2294 }
f4fc7782 2295
d3a7d8c7 2296 }
4438c4b7 2297 }
971a9dd3 2298 }
79072805
LW
2299 break;
2300 case '.':
3280af22
NIS
2301 if (PL_localizing) {
2302 if (PL_localizing == 1)
7766f137 2303 SAVESPTR(PL_last_in_gv);
748a9306 2304 }
3280af22 2305 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2306 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2307 break;
2308 case '^':
3280af22 2309 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2310 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
f776e3cd 2311 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2312 break;
2313 case '~':
3280af22 2314 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2315 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
f776e3cd 2316 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2317 break;
2318 case '=':
38ab35f8 2319 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2320 break;
2321 case '-':
38ab35f8 2322 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
3280af22
NIS
2323 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2324 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2325 break;
2326 case '%':
38ab35f8 2327 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2328 break;
2329 case '|':
4b65379b 2330 {
8772537c 2331 IO * const io = GvIOp(PL_defoutgv);
720f287d
AB
2332 if(!io)
2333 break;
38ab35f8 2334 if ((SvIV(sv)) == 0)
4b65379b
CS
2335 IoFLAGS(io) &= ~IOf_FLUSH;
2336 else {
2337 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2338 PerlIO *ofp = IoOFP(io);
2339 if (ofp)
2340 (void)PerlIO_flush(ofp);
2341 IoFLAGS(io) |= IOf_FLUSH;
2342 }
2343 }
79072805
LW
2344 }
2345 break;
79072805 2346 case '/':
3280af22 2347 SvREFCNT_dec(PL_rs);
8bfdd7d9 2348 PL_rs = newSVsv(sv);
79072805
LW
2349 break;
2350 case '\\':
7889fe52
NIS
2351 if (PL_ors_sv)
2352 SvREFCNT_dec(PL_ors_sv);
009c130f 2353 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2354 PL_ors_sv = newSVsv(sv);
009c130f 2355 }
e3c19b7b 2356 else {
a0714e2c 2357 PL_ors_sv = NULL;
e3c19b7b 2358 }
79072805
LW
2359 break;
2360 case ',':
7889fe52
NIS
2361 if (PL_ofs_sv)
2362 SvREFCNT_dec(PL_ofs_sv);
2363 if (SvOK(sv) || SvGMAGICAL(sv)) {
2364 PL_ofs_sv = newSVsv(sv);
2365 }
2366 else {
a0714e2c 2367 PL_ofs_sv = NULL;
7889fe52 2368 }
79072805 2369 break;
79072805 2370 case '[':
38ab35f8 2371 CopARYBASE_set(&PL_compiling, SvIV(sv));
79072805
LW
2372 break;
2373 case '?':
ff0cee69 2374#ifdef COMPLEX_STATUS
6b88bc9c
GS
2375 if (PL_localizing == 2) {
2376 PL_statusvalue = LvTARGOFF(sv);
2377 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2378 }
2379 else
2380#endif
2381#ifdef VMSISH_STATUS
2382 if (VMSISH_STATUS)
fb38d079 2383 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
ff0cee69
PP
2384 else
2385#endif
38ab35f8 2386 STATUS_UNIX_EXIT_SET(SvIV(sv));
79072805
LW
2387 break;
2388 case '!':
93189314
JH
2389 {
2390#ifdef VMS
2391# define PERL_VMS_BANG vaxc$errno
2392#else
2393# define PERL_VMS_BANG 0
2394#endif
91487cfc 2395 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2396 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2397 }
79072805
LW
2398 break;
2399 case '<':
38ab35f8 2400 PL_uid = SvIV(sv);
3280af22
NIS
2401 if (PL_delaymagic) {
2402 PL_delaymagic |= DM_RUID;
79072805
LW
2403 break; /* don't do magic till later */
2404 }
2405#ifdef HAS_SETRUID
b28d0864 2406 (void)setruid((Uid_t)PL_uid);
79072805
LW
2407#else
2408#ifdef HAS_SETREUID
3280af22 2409 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2410#else
85e6fe83 2411#ifdef HAS_SETRESUID
b28d0864 2412 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2413#else
75870ed3 2414 if (PL_uid == PL_euid) { /* special case $< = $> */
2415#ifdef PERL_DARWIN
2416 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2417 if (PL_uid != 0 && PerlProc_getuid() == 0)
2418 (void)PerlProc_setuid(0);
2419#endif
b28d0864 2420 (void)PerlProc_setuid(PL_uid);
75870ed3 2421 } else {
d8eceb89 2422 PL_uid = PerlProc_getuid();
cea2e8a9 2423 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2424 }
79072805
LW
2425#endif
2426#endif
85e6fe83 2427#endif
d8eceb89 2428 PL_uid = PerlProc_getuid();
3280af22 2429 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2430 break;
2431 case '>':
38ab35f8 2432 PL_euid = SvIV(sv);
3280af22
NIS
2433 if (PL_delaymagic) {
2434 PL_delaymagic |= DM_EUID;
79072805
LW
2435 break; /* don't do magic till later */
2436 }
2437#ifdef HAS_SETEUID
3280af22 2438 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2439#else
2440#ifdef HAS_SETREUID
b28d0864 2441 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2442#else
2443#ifdef HAS_SETRESUID
6b88bc9c 2444 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2445#else
b28d0864
NIS
2446 if (PL_euid == PL_uid) /* special case $> = $< */
2447 PerlProc_setuid(PL_euid);
a0d0e21e 2448 else {
e8ee3774 2449 PL_euid = PerlProc_geteuid();
cea2e8a9 2450 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2451 }
79072805
LW
2452#endif
2453#endif
85e6fe83 2454#endif
d8eceb89 2455 PL_euid = PerlProc_geteuid();
3280af22 2456 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2457 break;
2458 case '(':
38ab35f8 2459 PL_gid = SvIV(sv);
3280af22
NIS
2460 if (PL_delaymagic) {
2461 PL_delaymagic |= DM_RGID;
79072805
LW
2462 break; /* don't do magic till later */
2463 }
2464#ifdef HAS_SETRGID
b28d0864 2465 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2466#else
2467#ifdef HAS_SETREGID
3280af22 2468 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2469#else
2470#ifdef HAS_SETRESGID
b28d0864 2471 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2472#else
b28d0864
NIS
2473 if (PL_gid == PL_egid) /* special case $( = $) */
2474 (void)PerlProc_setgid(PL_gid);
748a9306 2475 else {
d8eceb89 2476 PL_gid = PerlProc_getgid();
cea2e8a9 2477 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2478 }
79072805
LW
2479#endif
2480#endif
85e6fe83 2481#endif
d8eceb89 2482 PL_gid = PerlProc_getgid();
3280af22 2483 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2484 break;
2485 case ')':
5cd24f17
PP
2486#ifdef HAS_SETGROUPS
2487 {
b83604b4 2488 const char *p = SvPV_const(sv, len);
757f63d8
SP
2489 Groups_t *gary = NULL;
2490
2491 while (isSPACE(*p))
2492 ++p;
2493 PL_egid = Atol(p);
2494 for (i = 0; i < NGROUPS; ++i) {
2495 while (*p && !isSPACE(*p))
2496 ++p;
2497 while (isSPACE(*p))
2498 ++p;
2499 if (!*p)
2500 break;
2501 if(!gary)
2502 Newx(gary, i + 1, Groups_t);
2503 else
2504 Renew(gary, i + 1, Groups_t);
2505 gary[i] = Atol(p);
2506 }
2507 if (i)
2508 (void)setgroups(i, gary);
2509 if (gary)
2510 Safefree(gary);
5cd24f17
PP
2511 }
2512#else /* HAS_SETGROUPS */
38ab35f8 2513 PL_egid = SvIV(sv);
5cd24f17 2514#endif /* HAS_SETGROUPS */
3280af22
NIS
2515 if (PL_delaymagic) {
2516 PL_delaymagic |= DM_EGID;
79072805
LW
2517 break; /* don't do magic till later */
2518 }
2519#ifdef HAS_SETEGID
3280af22 2520 (void)setegid((Gid_t)PL_egid);
79072805
LW
2521#else
2522#ifdef HAS_SETREGID
b28d0864 2523 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2524#else
2525#ifdef HAS_SETRESGID
b28d0864 2526 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2527#else
b28d0864
NIS
2528 if (PL_egid == PL_gid) /* special case $) = $( */
2529 (void)PerlProc_setgid(PL_egid);
748a9306 2530 else {
d8eceb89 2531 PL_egid = PerlProc_getegid();
cea2e8a9 2532 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2533 }
79072805
LW
2534#endif
2535#endif
85e6fe83 2536#endif
d8eceb89 2537 PL_egid = PerlProc_getegid();
3280af22 2538 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2539 break;
2540 case ':':
2d8e6c8d 2541 PL_chopset = SvPV_force(sv,len);
79072805 2542 break;
cd39f2b6 2543#ifndef MACOS_TRADITIONAL
79072805 2544 case '0':
e2975953 2545 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2546#ifdef HAS_SETPROCTITLE
2547 /* The BSDs don't show the argv[] in ps(1) output, they
2548 * show a string from the process struct and provide
2549 * the setproctitle() routine to manipulate that. */
a2722ac9 2550 if (PL_origalen != 1) {
b83604b4 2551 s = SvPV_const(sv, len);
98b76f99 2552# if __FreeBSD_version > 410001
9aad2c0e 2553 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2554 * but not the "(perl) suffix from the ps(1)
2555 * output, because that's what ps(1) shows if the
2556 * argv[] is modified. */
6f2ad931 2557 setproctitle("-%s", s);
9aad2c0e 2558# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2559 /* This doesn't really work if you assume that
2560 * $0 = 'foobar'; will wipe out 'perl' from the $0
2561 * because in ps(1) output the result will be like
2562 * sprintf("perl: %s (perl)", s)
2563 * I guess this is a security feature:
2564 * one (a user process) cannot get rid of the original name.
2565 * --jhi */
2566 setproctitle("%s", s);
2567# endif
2568 }
2569#endif
17aa7f3d 2570#if defined(__hpux) && defined(PSTAT_SETCMD)
a2722ac9 2571 if (PL_origalen != 1) {
17aa7f3d 2572 union pstun un;
b83604b4 2573 s = SvPV_const(sv, len);
6867be6d 2574 un.pst_command = (char *)s;
17aa7f3d
JH
2575 pstat(PSTAT_SETCMD, un, len, 0, 0);
2576 }
2577#endif
2d2af554
GA
2578 if (PL_origalen > 1) {
2579 /* PL_origalen is set in perl_parse(). */
2580 s = SvPV_force(sv,len);
2581 if (len >= (STRLEN)PL_origalen-1) {
2582 /* Longer than original, will be truncated. We assume that
2583 * PL_origalen bytes are available. */
2584 Copy(s, PL_origargv[0], PL_origalen-1, char);
2585 }
2586 else {
2587 /* Shorter than original, will be padded. */
2588 Copy(s, PL_origargv[0], len, char);
2589 PL_origargv[0][len] = 0;
2590 memset(PL_origargv[0] + len + 1,
2591 /* Is the space counterintuitive? Yes.
2592 * (You were expecting \0?)
2593 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2594 * --jhi */
2595 (int)' ',
2596 PL_origalen - len - 1);
2597 }
2598 PL_origargv[0][PL_origalen-1] = 0;
2599 for (i = 1; i < PL_origargc; i++)
2600 PL_origargv[i] = 0;
79072805 2601 }
e2975953 2602 UNLOCK_DOLLARZERO_MUTEX;
79072805 2603 break;
cd39f2b6 2604#endif
79072805
LW
2605 }
2606 return 0;
2607}
2608
2609I32
35a4481c 2610Perl_whichsig(pTHX_ const char *sig)
79072805 2611{
aadb217d 2612 register char* const* sigv;
96a5add6 2613 PERL_UNUSED_CONTEXT;
79072805 2614
aadb217d 2615 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
79072805 2616 if (strEQ(sig,*sigv))
aadb217d 2617 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805
LW
2618#ifdef SIGCLD
2619 if (strEQ(sig,"CHLD"))
2620 return SIGCLD;
2621#endif
2622#ifdef SIGCHLD
2623 if (strEQ(sig,"CLD"))
2624 return SIGCHLD;
2625#endif
7f1236c0 2626 return -1;
79072805
LW
2627}
2628
ecfc5424 2629Signal_t
1e82f5a6 2630#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
8aad04aa 2631Perl_sighandler(int sig, ...)
1e82f5a6
SH
2632#else
2633Perl_sighandler(int sig)
2634#endif
79072805 2635{
1018e26f
NIS
2636#ifdef PERL_GET_SIG_CONTEXT
2637 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2638#else
cea2e8a9 2639 dTHX;
71d280e3 2640#endif
79072805 2641 dSP;
a0714e2c
SS
2642 GV *gv = NULL;
2643 SV *sv = NULL;
8772537c 2644 SV * const tSv = PL_Sv;
601f1833 2645 CV *cv = NULL;
533c011a 2646 OP *myop = PL_op;
84902520 2647 U32 flags = 0;
8772537c 2648 XPV * const tXpv = PL_Xpv;
71d280e3 2649
3280af22 2650 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2651 flags |= 1;
3280af22 2652 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2653 flags |= 4;
3280af22 2654 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2655 flags |= 16;
2656
727405f8 2657 if (!PL_psig_ptr[sig]) {
99ef548b 2658 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2659 PL_sig_name[sig]);
2660 exit(sig);
2661 }
ff0cee69 2662
84902520
TB
2663 /* Max number of items pushed there is 3*n or 4. We cannot fix
2664 infinity, so we fix 4 (in fact 5): */
2665 if (flags & 1) {
3280af22 2666 PL_savestack_ix += 5; /* Protect save in progress. */
8772537c 2667 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
84902520 2668 }
ac27b0f5 2669 if (flags & 4)
3280af22 2670 PL_markstack_ptr++; /* Protect mark. */
84902520 2671 if (flags & 16)
3280af22 2672 PL_scopestack_ix += 1;
84902520 2673 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2674 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
8772537c
AL
2675 || SvTYPE(cv) != SVt_PVCV) {
2676 HV *st;
f2c0649b 2677 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
8772537c 2678 }
84902520 2679
a0d0e21e 2680 if (!cv || !CvROOT(cv)) {
599cee73 2681 if (ckWARN(WARN_SIGNAL))
9014280d 2682 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2683 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2684 : ((cv && CvGV(cv))
2685 ? GvENAME(CvGV(cv))
2686 : "__ANON__")));
2687 goto cleanup;
79072805
LW
2688 }
2689
22c35a8c 2690 if(PL_psig_name[sig]) {
b37c2d43 2691 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
84902520 2692 flags |= 64;
df3728a2 2693#if !defined(PERL_IMPLICIT_CONTEXT)
27da23d5 2694 PL_sig_sv = sv;
df3728a2 2695#endif
84902520 2696 } else {
ff0cee69 2697 sv = sv_newmortal();
22c35a8c 2698 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2699 }
e336de0d 2700
e788e7d3 2701 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2702 PUSHMARK(SP);
79072805 2703 PUSHs(sv);
8aad04aa
JH
2704#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2705 {
2706 struct sigaction oact;
2707
2708 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2709 siginfo_t *sip;
2710 va_list args;
2711
2712 va_start(args, sig);
2713 sip = (siginfo_t*)va_arg(args, siginfo_t*);
2714 if (sip) {
2715 HV *sih = newHV();
2716 SV *rv = newRV_noinc((SV*)sih);
2717 /* The siginfo fields signo, code, errno, pid, uid,
2718 * addr, status, and band are defined by POSIX/SUSv3. */
2719 hv_store(sih, "signo", 5, newSViv(sip->si_signo), 0);
2720 hv_store(sih, "code", 4, newSViv(sip->si_code), 0);
79dec0f4 2721#if 0 /* XXX TODO: Configure scan for the existence of these, but even that does not help if the SA_SIGINFO is not implemented according to the spec. */
ea1bde16 2722 hv_store(sih, "errno", 5, newSViv(sip->si_errno), 0);
79dec0f4 2723 hv_store(sih, "status", 6, newSViv(sip->si_status), 0);
8aad04aa
JH
2724 hv_store(sih, "uid", 3, newSViv(sip->si_uid), 0);
2725 hv_store(sih, "pid", 3, newSViv(sip->si_pid), 0);
2726 hv_store(sih, "addr", 4, newSVuv(PTR2UV(sip->si_addr)), 0);
8aad04aa 2727 hv_store(sih, "band", 4, newSViv(sip->si_band), 0);
79dec0f4 2728#endif
8aad04aa
JH
2729 EXTEND(SP, 2);
2730 PUSHs((SV*)rv);
2731 PUSHs(newSVpv((void*)sip, sizeof(*sip)));
2732 }
b4552a27 2733
31427afe 2734 va_end(args);
8aad04aa
JH
2735 }
2736 }
2737#endif
79072805 2738 PUTBACK;
a0d0e21e 2739
1b266415 2740 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2741
d3acc0f7 2742 POPSTACK;
1b266415 2743 if (SvTRUE(ERRSV)) {
1d615522 2744#ifndef PERL_MICRO
983dbef6 2745#ifdef HAS_SIGPROCMASK
1b266415
NIS
2746 /* Handler "died", for example to get out of a restart-able read().
2747 * Before we re-do that on its behalf re-enable the signal which was
2748 * blocked by the system when we entered.
2749 */
2750 sigset_t set;
2751 sigemptyset(&set);
2752 sigaddset(&set,sig);
2753 sigprocmask(SIG_UNBLOCK, &set, NULL);
2754#else
2755 /* Not clear if this will work */
2756 (void)rsignal(sig, SIG_IGN);
5c1546dc 2757 (void)rsignal(sig, PL_csighandlerp);
1b266415 2758#endif
1d615522 2759#endif /* !PERL_MICRO */
bd61b366 2760 Perl_die(aTHX_ NULL);
1b266415 2761 }
00d579c5 2762cleanup:
84902520 2763 if (flags & 1)
3280af22 2764 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2765 if (flags & 4)
3280af22 2766 PL_markstack_ptr--;
84902520 2767 if (flags & 16)
3280af22 2768 PL_scopestack_ix -= 1;
84902520
TB
2769 if (flags & 64)
2770 SvREFCNT_dec(sv);
533c011a 2771 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2772
3280af22
NIS
2773 PL_Sv = tSv; /* Restore global temporaries. */
2774 PL_Xpv = tXpv;
53bb94e2 2775 return;
79072805 2776}
4e35701f
NIS
2777
2778
51371543 2779static void
8772537c 2780S_restore_magic(pTHX_ const void *p)
51371543 2781{
97aff369 2782 dVAR;
8772537c
AL
2783 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
2784 SV* const sv = mgs->mgs_sv;
51371543
GS
2785
2786 if (!sv)
2787 return;
2788
2789 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2790 {
f8c7b90f 2791#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
2792 /* While magic was saved (and off) sv_setsv may well have seen
2793 this SV as a prime candidate for COW. */
2794 if (SvIsCOW(sv))
e424a81e 2795 sv_force_normal_flags(sv, 0);
f9701176
NC
2796#endif
2797
51371543
GS
2798 if (mgs->mgs_flags)
2799 SvFLAGS(sv) |= mgs->mgs_flags;
2800 else
2801 mg_magical(sv);
2b77b520
YST
2802 if (SvGMAGICAL(sv)) {
2803 /* downgrade public flags to private,
2804 and discard any other private flags */
2805
2806 U32 public = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
2807 if (public) {
2808 SvFLAGS(sv) &= ~( public | SVp_IOK|SVp_NOK|SVp_POK );
2809 SvFLAGS(sv) |= ( public << PRIVSHIFT );
2810 }
2811 }
51371543
GS
2812 }
2813
2814 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2815
2816 /* If we're still on top of the stack, pop us off. (That condition
2817 * will be satisfied if restore_magic was called explicitly, but *not*
2818 * if it's being called via leave_scope.)
2819 * The reason for doing this is that otherwise, things like sv_2cv()
2820 * may leave alloc gunk on the savestack, and some code
2821 * (e.g. sighandler) doesn't expect that...
2822 */
2823 if (PL_savestack_ix == mgs->mgs_ss_ix)
2824 {
2825 I32 popval = SSPOPINT;
c76ac1ee 2826 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2827 PL_savestack_ix -= 2;
2828 popval = SSPOPINT;
2829 assert(popval == SAVEt_ALLOC);
2830 popval = SSPOPINT;
2831 PL_savestack_ix -= popval;
2832 }
2833
2834}
2835
2836static void
8772537c 2837S_unwind_handler_stack(pTHX_ const void *p)
51371543 2838{
27da23d5 2839 dVAR;
e1ec3a88 2840 const U32 flags = *(const U32*)p;
51371543
GS
2841
2842 if (flags & 1)
2843 PL_savestack_ix -= 5; /* Unprotect save in progress. */
df3728a2 2844#if !defined(PERL_IMPLICIT_CONTEXT)
51371543 2845 if (flags & 64)
27da23d5 2846 SvREFCNT_dec(PL_sig_sv);
df3728a2 2847#endif
51371543 2848}
1018e26f 2849
66610fdd 2850/*
b3ca2e83
NC
2851=for apidoc magic_sethint
2852
2853Triggered by a store to %^H, records the key/value pair to
2854C<PL_compiling.cop_hints>. It is assumed that hints aren't storing anything
2855that would need a deep copy. Maybe we should warn if we find a reference.
2856
2857=cut
2858*/
2859int
2860Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
2861{
2862 dVAR;
2863 assert(mg->mg_len == HEf_SVKEY);
2864
e6e3e454
NC
2865 /* mg->mg_obj isn't being used. If needed, it would be possible to store
2866 an alternative leaf in there, with PL_compiling.cop_hints being used if
2867 it's NULL. If needed for threads, the alternative could lock a mutex,
2868 or take other more complex action. */
2869
5b9c0671
NC
2870 /* Something changed in %^H, so it will need to be restored on scope exit.
2871 Doing this here saves a lot of doing it manually in perl code (and
2872 forgetting to do it, and consequent subtle errors. */
2873 PL_hints |= HINT_LOCALIZE_HH;
b3ca2e83
NC
2874 PL_compiling.cop_hints
2875 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints,
2876 (SV *)mg->mg_ptr, newSVsv(sv));
2877 return 0;
2878}
2879
2880/*
2881=for apidoc magic_sethint
2882
2883Triggered by a delete from %^H, records the key to C<PL_compiling.cop_hints>.
2884
2885=cut
2886*/
2887int
2888Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
2889{
2890 dVAR;
2891 assert(mg->mg_len == HEf_SVKEY);
2892
5b9c0671 2893 PL_hints |= HINT_LOCALIZE_HH;
b3ca2e83
NC
2894 PL_compiling.cop_hints
2895 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints,
2896 (SV *)mg->mg_ptr, &PL_sv_placeholder);
2897 return 0;
2898}
2899
2900/*
66610fdd
RGS
2901 * Local variables:
2902 * c-indentation-style: bsd
2903 * c-basic-offset: 4
2904 * indent-tabs-mode: t
2905 * End:
2906 *
37442d52
RGS
2907 * ex: set ts=8 sts=4 sw=4 noet:
2908 */