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