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