This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regexec.c: Fix so will compile on Windows
[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 1742 ENTER;
d1d7a15d
NC
1743
1744 if (flags & G_WRITING_TO_STDERR) {
1745 SAVETMPS;
1746
1747 save_re_context();
1748 SAVESPTR(PL_stderrgv);
1749 PL_stderrgv = NULL;
1750 }
1751
efaf3674 1752 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1753 PUSHMARK(SP);
efaf3674 1754
67549bd2
NC
1755 EXTEND(SP, argc+1);
1756 PUSHs(SvTIED_obj(sv, mg));
1757 if (flags & G_UNDEF_FILL) {
1758 while (argc--) {
efaf3674 1759 PUSHs(&PL_sv_undef);
93965878 1760 }
67549bd2 1761 } else if (argc > 0) {
046b0c7d
NC
1762 va_list args;
1763 va_start(args, argc);
1764
1765 do {
1766 SV *const sv = va_arg(args, SV *);
1767 PUSHs(sv);
1768 } while (--argc);
1769
1770 va_end(args);
88e89b8a 1771 }
463ee0b2 1772 PUTBACK;
efaf3674
DM
1773 if (flags & G_DISCARD) {
1774 call_method(meth, G_SCALAR|G_DISCARD);
1775 }
1776 else {
1777 if (call_method(meth, G_SCALAR))
1778 ret = *PL_stack_sp--;
1779 }
1780 POPSTACK;
d1d7a15d
NC
1781 if (flags & G_WRITING_TO_STDERR)
1782 FREETMPS;
efaf3674
DM
1783 LEAVE;
1784 return ret;
1785}
1786
1787
1788/* wrapper for magic_methcall that creates the first arg */
463ee0b2 1789
efaf3674 1790STATIC SV*
c7a0c747 1791S_magic_methcall1(pTHX_ SV *sv, const MAGIC *mg, const char *meth, U32 flags,
efaf3674
DM
1792 int n, SV *val)
1793{
1794 dVAR;
1795 SV* arg1 = NULL;
1796
1797 PERL_ARGS_ASSERT_MAGIC_METHCALL1;
1798
1799 if (mg->mg_ptr) {
1800 if (mg->mg_len >= 0) {
db4b3a1d 1801 arg1 = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
efaf3674
DM
1802 }
1803 else if (mg->mg_len == HEf_SVKEY)
1804 arg1 = MUTABLE_SV(mg->mg_ptr);
1805 }
1806 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
db4b3a1d 1807 arg1 = newSViv((IV)(mg->mg_len));
efaf3674
DM
1808 sv_2mortal(arg1);
1809 }
1810 if (!arg1) {
046b0c7d 1811 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n - 1, val);
efaf3674 1812 }
046b0c7d 1813 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n, arg1, val);
946ec16e
PP
1814}
1815
76e3520e 1816STATIC int
e1ec3a88 1817S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
a0d0e21e 1818{
efaf3674
DM
1819 dVAR;
1820 SV* ret;
463ee0b2 1821
7918f24d
NC
1822 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1823
efaf3674
DM
1824 ret = magic_methcall1(sv, mg, meth, 0, 1, NULL);
1825 if (ret)
1826 sv_setsv(sv, ret);
a0d0e21e
LW
1827 return 0;
1828}
463ee0b2 1829
a0d0e21e 1830int
864dbfa3 1831Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1832{
7918f24d
NC
1833 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1834
fd69380d 1835 if (mg->mg_type == PERL_MAGIC_tiedelem)
a0d0e21e 1836 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1837 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1838 return 0;
1839}
1840
1841int
864dbfa3 1842Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1843{
efaf3674 1844 dVAR;
b112cff9
DM
1845 MAGIC *tmg;
1846 SV *val;
7918f24d
NC
1847
1848 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1849
b112cff9
DM
1850 /* in the code C<$tied{foo} = $val>, the "thing" that gets passed to
1851 * STORE() is not $val, but rather a PVLV (the sv in this call), whose
1852 * public flags indicate its value based on copying from $val. Doing
1853 * mg_set() on the PVLV temporarily does SvMAGICAL_off(), then calls us.
1854 * So STORE()'s $_[2] arg is a temporarily disarmed PVLV. This goes
1855 * wrong if $val happened to be tainted, as sv hasn't got magic
1856 * enabled, even though taint magic is in the chain. In which case,
1857 * fake up a temporary tainted value (this is easier than temporarily
1858 * re-enabling magic on sv). */
1859
1860 if (PL_tainting && (tmg = mg_find(sv, PERL_MAGIC_taint))
1861 && (tmg->mg_len & 1))
1862 {
1863 val = sv_mortalcopy(sv);
1864 SvTAINTED_on(val);
1865 }
1866 else
1867 val = sv;
1868
efaf3674 1869 magic_methcall1(sv, mg, "STORE", G_DISCARD, 2, val);
463ee0b2
LW
1870 return 0;
1871}
1872
1873int
864dbfa3 1874Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1875{
7918f24d
NC
1876 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1877
a0d0e21e
LW
1878 return magic_methpack(sv,mg,"DELETE");
1879}
463ee0b2 1880
93965878
NIS
1881
1882U32
864dbfa3 1883Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1884{
efaf3674 1885 dVAR;
22846ab4 1886 I32 retval = 0;
efaf3674 1887 SV* retsv;
93965878 1888
7918f24d
NC
1889 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1890
efaf3674
DM
1891 retsv = magic_methcall1(sv, mg, "FETCHSIZE", 0, 1, NULL);
1892 if (retsv) {
1893 retval = SvIV(retsv)-1;
22846ab4
AB
1894 if (retval < -1)
1895 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
93965878 1896 }
22846ab4 1897 return (U32) retval;
93965878
NIS
1898}
1899
cea2e8a9
GS
1900int
1901Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1902{
efaf3674 1903 dVAR;
463ee0b2 1904
7918f24d
NC
1905 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1906
046b0c7d 1907 Perl_magic_methcall(aTHX_ sv, mg, "CLEAR", G_DISCARD, 0);
463ee0b2
LW
1908 return 0;
1909}
1910
1911int
864dbfa3 1912Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1913{
efaf3674
DM
1914 dVAR;
1915 SV* ret;
463ee0b2 1916
7918f24d
NC
1917 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1918
046b0c7d
NC
1919 ret = SvOK(key) ? Perl_magic_methcall(aTHX_ sv, mg, "NEXTKEY", 0, 1, key)
1920 : Perl_magic_methcall(aTHX_ sv, mg, "FIRSTKEY", 0, 0);
efaf3674
DM
1921 if (ret)
1922 sv_setsv(key,ret);
79072805
LW
1923 return 0;
1924}
1925
1926int
1146e912 1927Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
a0d0e21e 1928{
7918f24d
NC
1929 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1930
a0d0e21e 1931 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1932}
a0d0e21e 1933
a3bcc51e
TP
1934SV *
1935Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1936{
efaf3674 1937 dVAR;
5fcbf73d 1938 SV *retval;
ad64d0ec
NC
1939 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
1940 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
a3bcc51e 1941
7918f24d
NC
1942 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1943
a3bcc51e
TP
1944 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1945 SV *key;
bfcb3514 1946 if (HvEITER_get(hv))
a3bcc51e
TP
1947 /* we are in an iteration so the hash cannot be empty */
1948 return &PL_sv_yes;
1949 /* no xhv_eiter so now use FIRSTKEY */
1950 key = sv_newmortal();
ad64d0ec 1951 magic_nextpack(MUTABLE_SV(hv), mg, key);
bfcb3514 1952 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1953 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1954 }
1955
1956 /* there is a SCALAR method that we can call */
046b0c7d 1957 retval = Perl_magic_methcall(aTHX_ MUTABLE_SV(hv), mg, "SCALAR", 0, 0);
efaf3674 1958 if (!retval)
5fcbf73d 1959 retval = &PL_sv_undef;
a3bcc51e
TP
1960 return retval;
1961}
1962
a0d0e21e 1963int
864dbfa3 1964Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1965{
97aff369 1966 dVAR;
8772537c
AL
1967 GV * const gv = PL_DBline;
1968 const I32 i = SvTRUE(sv);
1969 SV ** const svp = av_fetch(GvAV(gv),
01b8bcb7 1970 atoi(MgPV_nolen_const(mg)), FALSE);
7918f24d
NC
1971
1972 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
1973
8772537c
AL
1974 if (svp && SvIOKp(*svp)) {
1975 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1976 if (o) {
1977 /* set or clear breakpoint in the relevant control op */
1978 if (i)
1979 o->op_flags |= OPf_SPECIAL;
1980 else
1981 o->op_flags &= ~OPf_SPECIAL;
1982 }
5df8de69 1983 }
79072805
LW
1984 return 0;
1985}
1986
1987int
8772537c 1988Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
79072805 1989{
97aff369 1990 dVAR;
502c6561 1991 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
1992
1993 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
1994
83bf042f 1995 if (obj) {
fc15ae8f 1996 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
83bf042f
NC
1997 } else {
1998 SvOK_off(sv);
1999 }
79072805
LW
2000 return 0;
2001}
2002
2003int
864dbfa3 2004Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 2005{
97aff369 2006 dVAR;
502c6561 2007 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
2008
2009 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
2010
83bf042f 2011 if (obj) {
fc15ae8f 2012 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
83bf042f 2013 } else {
a2a5de95
NC
2014 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
2015 "Attempt to set length of freed array");
83bf042f
NC
2016 }
2017 return 0;
2018}
2019
2020int
2021Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
2022{
97aff369 2023 dVAR;
7918f24d
NC
2024
2025 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
53c1dcc0 2026 PERL_UNUSED_ARG(sv);
7918f24d 2027
94f3782b
DM
2028 /* during global destruction, mg_obj may already have been freed */
2029 if (PL_in_clean_all)
1ea47f64 2030 return 0;
94f3782b 2031
83bf042f
NC
2032 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
2033
2034 if (mg) {
2035 /* arylen scalar holds a pointer back to the array, but doesn't own a
2036 reference. Hence the we (the array) are about to go away with it
2037 still pointing at us. Clear its pointer, else it would be pointing
2038 at free memory. See the comment in sv_magic about reference loops,
2039 and why it can't own a reference to us. */
2040 mg->mg_obj = 0;
2041 }
a0d0e21e
LW
2042 return 0;
2043}
2044
2045int
864dbfa3 2046Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2047{
97aff369 2048 dVAR;
8772537c 2049 SV* const lsv = LvTARG(sv);
7918f24d
NC
2050
2051 PERL_ARGS_ASSERT_MAGIC_GETPOS;
3881461a 2052 PERL_UNUSED_ARG(mg);
ac27b0f5 2053
a0d0e21e 2054 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
3881461a
AL
2055 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
2056 if (found && found->mg_len >= 0) {
2057 I32 i = found->mg_len;
7e2040f0 2058 if (DO_UTF8(lsv))
a0ed51b3 2059 sv_pos_b2u(lsv, &i);
fc15ae8f 2060 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
a0d0e21e
LW
2061 return 0;
2062 }
2063 }
0c34ef67 2064 SvOK_off(sv);
a0d0e21e
LW
2065 return 0;
2066}
2067
2068int
864dbfa3 2069Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2070{
97aff369 2071 dVAR;
8772537c 2072 SV* const lsv = LvTARG(sv);
a0d0e21e
LW
2073 SSize_t pos;
2074 STRLEN len;
c00206c8 2075 STRLEN ulen = 0;
53d44271 2076 MAGIC* found;
a0d0e21e 2077
7918f24d 2078 PERL_ARGS_ASSERT_MAGIC_SETPOS;
3881461a 2079 PERL_UNUSED_ARG(mg);
ac27b0f5 2080
a0d0e21e 2081 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
3881461a
AL
2082 found = mg_find(lsv, PERL_MAGIC_regex_global);
2083 else
2084 found = NULL;
2085 if (!found) {
a0d0e21e
LW
2086 if (!SvOK(sv))
2087 return 0;
d83f0a82
NC
2088#ifdef PERL_OLD_COPY_ON_WRITE
2089 if (SvIsCOW(lsv))
2090 sv_force_normal_flags(lsv, 0);
2091#endif
3881461a 2092 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
53d44271 2093 NULL, 0);
a0d0e21e
LW
2094 }
2095 else if (!SvOK(sv)) {
3881461a 2096 found->mg_len = -1;
a0d0e21e
LW
2097 return 0;
2098 }
2099 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
2100
fc15ae8f 2101 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
a0ed51b3 2102
7e2040f0 2103 if (DO_UTF8(lsv)) {
a0ed51b3
LW
2104 ulen = sv_len_utf8(lsv);
2105 if (ulen)
2106 len = ulen;
a0ed51b3
LW
2107 }
2108
a0d0e21e
LW
2109 if (pos < 0) {
2110 pos += len;
2111 if (pos < 0)
2112 pos = 0;
2113 }
eb160463 2114 else if (pos > (SSize_t)len)
a0d0e21e 2115 pos = len;
a0ed51b3
LW
2116
2117 if (ulen) {
2118 I32 p = pos;
2119 sv_pos_u2b(lsv, &p, 0);
2120 pos = p;
2121 }
727405f8 2122
3881461a
AL
2123 found->mg_len = pos;
2124 found->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 2125
79072805
LW
2126 return 0;
2127}
2128
2129int
864dbfa3 2130Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
2131{
2132 STRLEN len;
35a4481c 2133 SV * const lsv = LvTARG(sv);
b83604b4 2134 const char * const tmps = SvPV_const(lsv,len);
777f7c56
EB
2135 STRLEN offs = LvTARGOFF(sv);
2136 STRLEN rem = LvTARGLEN(sv);
7918f24d
NC
2137
2138 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
8772537c 2139 PERL_UNUSED_ARG(mg);
6ff81951 2140
9aa983d2 2141 if (SvUTF8(lsv))
d931b1be 2142 offs = sv_pos_u2b_flags(lsv, offs, &rem, SV_CONST_RETURN);
777f7c56 2143 if (offs > len)
6ff81951 2144 offs = len;
777f7c56 2145 if (rem > len - offs)
6ff81951 2146 rem = len - offs;
1c900557 2147 sv_setpvn(sv, tmps + offs, rem);
9aa983d2 2148 if (SvUTF8(lsv))
2ef4b674 2149 SvUTF8_on(sv);
6ff81951
GS
2150 return 0;
2151}
2152
2153int
864dbfa3 2154Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 2155{
97aff369 2156 dVAR;
9aa983d2 2157 STRLEN len;
5fcbf73d 2158 const char * const tmps = SvPV_const(sv, len);
dd374669 2159 SV * const lsv = LvTARG(sv);
777f7c56
EB
2160 STRLEN lvoff = LvTARGOFF(sv);
2161 STRLEN lvlen = LvTARGLEN(sv);
7918f24d
NC
2162
2163 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
8772537c 2164 PERL_UNUSED_ARG(mg);
075a4a2b 2165
1aa99e6b 2166 if (DO_UTF8(sv)) {
9aa983d2 2167 sv_utf8_upgrade(lsv);
d931b1be 2168 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
9aa983d2 2169 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 2170 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
2171 SvUTF8_on(lsv);
2172 }
9bf12eaf 2173 else if (lsv && SvUTF8(lsv)) {
5fcbf73d 2174 const char *utf8;
d931b1be 2175 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
b76f3ce2 2176 LvTARGLEN(sv) = len;
5fcbf73d
AL
2177 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
2178 sv_insert(lsv, lvoff, lvlen, utf8, len);
2179 Safefree(utf8);
1aa99e6b 2180 }
b76f3ce2
GB
2181 else {
2182 sv_insert(lsv, lvoff, lvlen, tmps, len);
2183 LvTARGLEN(sv) = len;
2184 }
2185
79072805
LW
2186 return 0;
2187}
2188
2189int
864dbfa3 2190Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2191{
97aff369 2192 dVAR;
7918f24d
NC
2193
2194 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
8772537c 2195 PERL_UNUSED_ARG(sv);
7918f24d 2196
27cc343c 2197 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
463ee0b2
LW
2198 return 0;
2199}
2200
2201int
864dbfa3 2202Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2203{
97aff369 2204 dVAR;
7918f24d
NC
2205
2206 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
8772537c 2207 PERL_UNUSED_ARG(sv);
7918f24d 2208
b01e650a
DM
2209 /* update taint status */
2210 if (PL_tainted)
2211 mg->mg_len |= 1;
2212 else
2213 mg->mg_len &= ~1;
463ee0b2
LW
2214 return 0;
2215}
2216
2217int
864dbfa3 2218Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 2219{
35a4481c 2220 SV * const lsv = LvTARG(sv);
7918f24d
NC
2221
2222 PERL_ARGS_ASSERT_MAGIC_GETVEC;
8772537c 2223 PERL_UNUSED_ARG(mg);
6ff81951 2224
6136c704
AL
2225 if (lsv)
2226 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2227 else
0c34ef67 2228 SvOK_off(sv);
6ff81951 2229
6ff81951
GS
2230 return 0;
2231}
2232
2233int
864dbfa3 2234Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 2235{
7918f24d 2236 PERL_ARGS_ASSERT_MAGIC_SETVEC;
8772537c 2237 PERL_UNUSED_ARG(mg);
79072805
LW
2238 do_vecset(sv); /* XXX slurp this routine */
2239 return 0;
2240}
2241
2242int
864dbfa3 2243Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 2244{
97aff369 2245 dVAR;
a0714e2c 2246 SV *targ = NULL;
7918f24d
NC
2247
2248 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2249
5f05dabc 2250 if (LvTARGLEN(sv)) {
68dc0745 2251 if (mg->mg_obj) {
8772537c 2252 SV * const ahv = LvTARG(sv);
85fbaab2 2253 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
6d822dc4
MS
2254 if (he)
2255 targ = HeVAL(he);
68dc0745
PP
2256 }
2257 else {
502c6561 2258 AV *const av = MUTABLE_AV(LvTARG(sv));
68dc0745
PP
2259 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2260 targ = AvARRAY(av)[LvTARGOFF(sv)];
2261 }
46da273f 2262 if (targ && (targ != &PL_sv_undef)) {
68dc0745
PP
2263 /* somebody else defined it for us */
2264 SvREFCNT_dec(LvTARG(sv));
b37c2d43 2265 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
68dc0745
PP
2266 LvTARGLEN(sv) = 0;
2267 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2268 mg->mg_obj = NULL;
68dc0745
PP
2269 mg->mg_flags &= ~MGf_REFCOUNTED;
2270 }
5f05dabc 2271 }
71be2cbc
PP
2272 else
2273 targ = LvTARG(sv);
3280af22 2274 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
2275 return 0;
2276}
2277
2278int
864dbfa3 2279Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 2280{
7918f24d 2281 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
8772537c 2282 PERL_UNUSED_ARG(mg);
71be2cbc 2283 if (LvTARGLEN(sv))
68dc0745
PP
2284 vivify_defelem(sv);
2285 if (LvTARG(sv)) {
5f05dabc 2286 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
2287 SvSETMAGIC(LvTARG(sv));
2288 }
5f05dabc
PP
2289 return 0;
2290}
2291
71be2cbc 2292void
864dbfa3 2293Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 2294{
97aff369 2295 dVAR;
74e13ce4 2296 MAGIC *mg;
a0714e2c 2297 SV *value = NULL;
71be2cbc 2298
7918f24d
NC
2299 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2300
14befaf4 2301 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 2302 return;
68dc0745 2303 if (mg->mg_obj) {
8772537c 2304 SV * const ahv = LvTARG(sv);
85fbaab2 2305 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
6d822dc4
MS
2306 if (he)
2307 value = HeVAL(he);
3280af22 2308 if (!value || value == &PL_sv_undef)
be2597df 2309 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
71be2cbc 2310 }
68dc0745 2311 else {
502c6561 2312 AV *const av = MUTABLE_AV(LvTARG(sv));
5aabfad6 2313 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
a0714e2c 2314 LvTARG(sv) = NULL; /* array can't be extended */
68dc0745 2315 else {
d4c19fe8 2316 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 2317 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 2318 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
2319 }
2320 }
b37c2d43 2321 SvREFCNT_inc_simple_void(value);
68dc0745
PP
2322 SvREFCNT_dec(LvTARG(sv));
2323 LvTARG(sv) = value;
71be2cbc 2324 LvTARGLEN(sv) = 0;
68dc0745 2325 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2326 mg->mg_obj = NULL;
68dc0745 2327 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
2328}
2329
2330int
864dbfa3 2331Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 2332{
7918f24d 2333 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
5648c0ae
DM
2334 Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2335 return 0;
810b8aa5
GS
2336}
2337
2338int
864dbfa3 2339Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2340{
7918f24d 2341 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
96a5add6 2342 PERL_UNUSED_CONTEXT;
565764a8 2343 mg->mg_len = -1;
1f730e6c
FC
2344 if (!isGV_with_GP(sv))
2345 SvSCREAM_off(sv);
93a17b20
LW
2346 return 0;
2347}
2348
2349int
864dbfa3 2350Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2351{
35a4481c 2352 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805 2353
7918f24d
NC
2354 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2355
79072805 2356 if (uf && uf->uf_set)
24f81a43 2357 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2358 return 0;
2359}
2360
c277df42 2361int
faf82a0b
AE
2362Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2363{
488344d2 2364 const char type = mg->mg_type;
7918f24d
NC
2365
2366 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2367
488344d2
NC
2368 if (type == PERL_MAGIC_qr) {
2369 } else if (type == PERL_MAGIC_bm) {
2370 SvTAIL_off(sv);
2371 SvVALID_off(sv);
2372 } else {
2373 assert(type == PERL_MAGIC_fm);
2374 SvCOMPILED_off(sv);
2375 }
2376 return sv_unmagic(sv, type);
faf82a0b
AE
2377}
2378
7a4c00b4 2379#ifdef USE_LOCALE_COLLATE
79072805 2380int
864dbfa3 2381Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69 2382{
7918f24d
NC
2383 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2384
bbce6d69 2385 /*
838b5b74 2386 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2387 * and vanished with a faint plop.
2388 */
96a5add6 2389 PERL_UNUSED_CONTEXT;
8772537c 2390 PERL_UNUSED_ARG(sv);
7a4c00b4
PP
2391 if (mg->mg_ptr) {
2392 Safefree(mg->mg_ptr);
2393 mg->mg_ptr = NULL;
565764a8 2394 mg->mg_len = -1;
7a4c00b4 2395 }
bbce6d69
PP
2396 return 0;
2397}
7a4c00b4 2398#endif /* USE_LOCALE_COLLATE */
bbce6d69 2399
7e8c5dac
HS
2400/* Just clear the UTF-8 cache data. */
2401int
2402Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2403{
7918f24d 2404 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
96a5add6 2405 PERL_UNUSED_CONTEXT;
8772537c 2406 PERL_UNUSED_ARG(sv);
7e8c5dac 2407 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
3881461a 2408 mg->mg_ptr = NULL;
7e8c5dac
HS
2409 mg->mg_len = -1; /* The mg_len holds the len cache. */
2410 return 0;
2411}
2412
bbce6d69 2413int
864dbfa3 2414Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2415{
97aff369 2416 dVAR;
e1ec3a88 2417 register const char *s;
2fdbfb4d
AB
2418 register I32 paren;
2419 register const REGEXP * rx;
2420 const char * const remaining = mg->mg_ptr + 1;
79072805 2421 I32 i;
8990e307 2422 STRLEN len;
125b9982 2423 MAGIC *tmg;
2fdbfb4d 2424
7918f24d
NC
2425 PERL_ARGS_ASSERT_MAGIC_SET;
2426
79072805 2427 switch (*mg->mg_ptr) {
2fdbfb4d
AB
2428 case '\015': /* $^MATCH */
2429 if (strEQ(remaining, "ATCH"))
2430 goto do_match;
2431 case '`': /* ${^PREMATCH} caught below */
2432 do_prematch:
f1b875a0 2433 paren = RX_BUFF_IDX_PREMATCH;
2fdbfb4d
AB
2434 goto setparen;
2435 case '\'': /* ${^POSTMATCH} caught below */
2436 do_postmatch:
f1b875a0 2437 paren = RX_BUFF_IDX_POSTMATCH;
2fdbfb4d
AB
2438 goto setparen;
2439 case '&':
2440 do_match:
f1b875a0 2441 paren = RX_BUFF_IDX_FULLMATCH;
2fdbfb4d
AB
2442 goto setparen;
2443 case '1': case '2': case '3': case '4':
2444 case '5': case '6': case '7': case '8': case '9':
104a8018 2445 paren = atoi(mg->mg_ptr);
2fdbfb4d 2446 setparen:
1e05feb3 2447 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2fdbfb4d 2448 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
1e05feb3 2449 } else {
2fdbfb4d
AB
2450 /* Croak with a READONLY error when a numbered match var is
2451 * set without a previous pattern match. Unless it's C<local $1>
2452 */
2453 if (!PL_localizing) {
6ad8f254 2454 Perl_croak_no_modify(aTHX);
2fdbfb4d
AB
2455 }
2456 }
9b9e0be4 2457 break;
748a9306 2458 case '\001': /* ^A */
3280af22 2459 sv_setsv(PL_bodytarget, sv);
125b9982
NT
2460 /* mg_set() has temporarily made sv non-magical */
2461 if (PL_tainting) {
2462 if ((tmg = mg_find(sv,PERL_MAGIC_taint)) && tmg->mg_len & 1)
2463 SvTAINTED_on(PL_bodytarget);
2464 else
2465 SvTAINTED_off(PL_bodytarget);
2466 }
748a9306 2467 break;
49460fe6 2468 case '\003': /* ^C */
f2338a2e 2469 PL_minus_c = cBOOL(SvIV(sv));
49460fe6
NIS
2470 break;
2471
79072805 2472 case '\004': /* ^D */
b4ab917c 2473#ifdef DEBUGGING
b83604b4 2474 s = SvPV_nolen_const(sv);
ddcf8bc1 2475 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
a58fb6f9
CS
2476 if (DEBUG_x_TEST || DEBUG_B_TEST)
2477 dump_all_perl(!DEBUG_B_TEST);
b4ab917c 2478#else
38ab35f8 2479 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
b4ab917c 2480#endif
79072805 2481 break;
28f23441 2482 case '\005': /* ^E */
d0063567 2483 if (*(mg->mg_ptr+1) == '\0') {
e37778c2 2484#ifdef VMS
38ab35f8 2485 set_vaxc_errno(SvIV(sv));
e37778c2
NC
2486#else
2487# ifdef WIN32
d0063567 2488 SetLastError( SvIV(sv) );
e37778c2
NC
2489# else
2490# ifdef OS2
38ab35f8 2491 os2_setsyserrno(SvIV(sv));
e37778c2 2492# else
d0063567 2493 /* will anyone ever use this? */
38ab35f8 2494 SETERRNO(SvIV(sv), 4);
048c1ddf
IZ
2495# endif
2496# endif
22fae026 2497#endif
d0063567
DK
2498 }
2499 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
ef8d46e8 2500 SvREFCNT_dec(PL_encoding);
d0063567
DK
2501 if (SvOK(sv) || SvGMAGICAL(sv)) {
2502 PL_encoding = newSVsv(sv);
2503 }
2504 else {
a0714e2c 2505 PL_encoding = NULL;
d0063567
DK
2506 }
2507 }
2508 break;
79072805 2509 case '\006': /* ^F */
38ab35f8 2510 PL_maxsysfd = SvIV(sv);
79072805 2511 break;
a0d0e21e 2512 case '\010': /* ^H */
38ab35f8 2513 PL_hints = SvIV(sv);
a0d0e21e 2514 break;
9d116dd7 2515 case '\011': /* ^I */ /* NOT \t in EBCDIC */
43c5f42d 2516 Safefree(PL_inplace);
bd61b366 2517 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
da78da6e 2518 break;
28f23441 2519 case '\017': /* ^O */
ac27b0f5 2520 if (*(mg->mg_ptr+1) == '\0') {
43c5f42d 2521 Safefree(PL_osname);
bd61b366 2522 PL_osname = NULL;
3511154c
DM
2523 if (SvOK(sv)) {
2524 TAINT_PROPER("assigning to $^O");
2e0de35c 2525 PL_osname = savesvpv(sv);
3511154c 2526 }
ac27b0f5
NIS
2527 }
2528 else if (strEQ(mg->mg_ptr, "\017PEN")) {
8b850bd5
NC
2529 STRLEN len;
2530 const char *const start = SvPV(sv, len);
b54fc2b6 2531 const char *out = (const char*)memchr(start, '\0', len);
8b850bd5 2532 SV *tmp;
8b850bd5
NC
2533
2534
2535 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
f747ebd6 2536 PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
8b850bd5
NC
2537
2538 /* Opening for input is more common than opening for output, so
2539 ensure that hints for input are sooner on linked list. */
59cd0e26 2540 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
f747ebd6
Z
2541 SvUTF8(sv))
2542 : newSVpvs_flags("", SvUTF8(sv));
2543 (void)hv_stores(GvHV(PL_hintgv), "open>", tmp);
2544 mg_set(tmp);
8b850bd5 2545
f747ebd6
Z
2546 tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len,
2547 SvUTF8(sv));
2548 (void)hv_stores(GvHV(PL_hintgv), "open<", tmp);
2549 mg_set(tmp);
ac27b0f5 2550 }
28f23441 2551 break;
79072805 2552 case '\020': /* ^P */
2fdbfb4d
AB
2553 if (*remaining == '\0') { /* ^P */
2554 PL_perldb = SvIV(sv);
2555 if (PL_perldb && !PL_DBsingle)
2556 init_debugger();
2557 break;
2558 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
2559 goto do_prematch;
2560 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
2561 goto do_postmatch;
2562 }
9b9e0be4 2563 break;
79072805 2564 case '\024': /* ^T */
88e89b8a 2565#ifdef BIG_TIME
6b88bc9c 2566 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2567#else
38ab35f8 2568 PL_basetime = (Time_t)SvIV(sv);
88e89b8a 2569#endif
79072805 2570 break;
e07ea26a
NC
2571 case '\025': /* ^UTF8CACHE */
2572 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2573 PL_utf8cache = (signed char) sv_2iv(sv);
2574 }
2575 break;
fde18df1 2576 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2577 if (*(mg->mg_ptr+1) == '\0') {
2578 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
38ab35f8 2579 i = SvIV(sv);
ac27b0f5 2580 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2581 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2582 }
599cee73 2583 }
0a378802 2584 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2585 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2586 if (!SvPOK(sv) && PL_localizing) {
2587 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2588 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2589 break;
2590 }
f4fc7782 2591 {
b5477537 2592 STRLEN len, i;
d3a7d8c7 2593 int accumulate = 0 ;
f4fc7782 2594 int any_fatals = 0 ;
b83604b4 2595 const char * const ptr = SvPV_const(sv, len) ;
f4fc7782
JH
2596 for (i = 0 ; i < len ; ++i) {
2597 accumulate |= ptr[i] ;
2598 any_fatals |= (ptr[i] & 0xAA) ;
2599 }
4243c432
NC
2600 if (!accumulate) {
2601 if (!specialWARN(PL_compiling.cop_warnings))
2602 PerlMemShared_free(PL_compiling.cop_warnings);
2603 PL_compiling.cop_warnings = pWARN_NONE;
2604 }
72dc9ed5
NC
2605 /* Yuck. I can't see how to abstract this: */
2606 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2607 WARN_ALL) && !any_fatals) {
4243c432
NC
2608 if (!specialWARN(PL_compiling.cop_warnings))
2609 PerlMemShared_free(PL_compiling.cop_warnings);
f4fc7782
JH
2610 PL_compiling.cop_warnings = pWARN_ALL;
2611 PL_dowarn |= G_WARN_ONCE ;
727405f8 2612 }
d3a7d8c7 2613 else {
72dc9ed5
NC
2614 STRLEN len;
2615 const char *const p = SvPV_const(sv, len);
2616
2617 PL_compiling.cop_warnings
8ee4cf24 2618 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
72dc9ed5
NC
2619 p, len);
2620
d3a7d8c7
GS
2621 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2622 PL_dowarn |= G_WARN_ONCE ;
2623 }
f4fc7782 2624
d3a7d8c7 2625 }
4438c4b7 2626 }
971a9dd3 2627 }
79072805
LW
2628 break;
2629 case '.':
3280af22
NIS
2630 if (PL_localizing) {
2631 if (PL_localizing == 1)
7766f137 2632 SAVESPTR(PL_last_in_gv);
748a9306 2633 }
3280af22 2634 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2635 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2636 break;
2637 case '^':
099be4f1
DM
2638 if (isGV_with_GP(PL_defoutgv)) {
2639 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2640 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2641 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2642 }
79072805
LW
2643 break;
2644 case '~':
099be4f1
DM
2645 if (isGV_with_GP(PL_defoutgv)) {
2646 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2647 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2648 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2649 }
79072805
LW
2650 break;
2651 case '=':
099be4f1
DM
2652 if (isGV_with_GP(PL_defoutgv))
2653 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2654 break;
2655 case '-':
099be4f1
DM
2656 if (isGV_with_GP(PL_defoutgv)) {
2657 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2658 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2659 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2660 }
79072805
LW
2661 break;
2662 case '%':
099be4f1
DM
2663 if (isGV_with_GP(PL_defoutgv))
2664 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2665 break;
2666 case '|':
4b65379b 2667 {
099be4f1 2668 IO * const io = GvIO(PL_defoutgv);
720f287d
AB
2669 if(!io)
2670 break;
38ab35f8 2671 if ((SvIV(sv)) == 0)
4b65379b
CS
2672 IoFLAGS(io) &= ~IOf_FLUSH;
2673 else {
2674 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2675 PerlIO *ofp = IoOFP(io);
2676 if (ofp)
2677 (void)PerlIO_flush(ofp);
2678 IoFLAGS(io) |= IOf_FLUSH;
2679 }
2680 }
79072805
LW
2681 }
2682 break;
79072805 2683 case '/':
3280af22 2684 SvREFCNT_dec(PL_rs);
8bfdd7d9 2685 PL_rs = newSVsv(sv);
79072805
LW
2686 break;
2687 case '\\':
ef8d46e8 2688 SvREFCNT_dec(PL_ors_sv);
009c130f 2689 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2690 PL_ors_sv = newSVsv(sv);
009c130f 2691 }
e3c19b7b 2692 else {
a0714e2c 2693 PL_ors_sv = NULL;
e3c19b7b 2694 }
79072805 2695 break;
79072805 2696 case '[':
38ab35f8 2697 CopARYBASE_set(&PL_compiling, SvIV(sv));
79072805
LW
2698 break;
2699 case '?':
ff0cee69 2700#ifdef COMPLEX_STATUS
6b88bc9c 2701 if (PL_localizing == 2) {
41cb7b2b 2702 SvUPGRADE(sv, SVt_PVLV);
6b88bc9c
GS
2703 PL_statusvalue = LvTARGOFF(sv);
2704 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2705 }
2706 else
2707#endif
2708#ifdef VMSISH_STATUS
2709 if (VMSISH_STATUS)
fb38d079 2710 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
ff0cee69
PP
2711 else
2712#endif
38ab35f8 2713 STATUS_UNIX_EXIT_SET(SvIV(sv));
79072805
LW
2714 break;
2715 case '!':
93189314
JH
2716 {
2717#ifdef VMS
2718# define PERL_VMS_BANG vaxc$errno
2719#else
2720# define PERL_VMS_BANG 0
2721#endif
91487cfc 2722 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2723 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2724 }
79072805
LW
2725 break;
2726 case '<':
38ab35f8 2727 PL_uid = SvIV(sv);
3280af22
NIS
2728 if (PL_delaymagic) {
2729 PL_delaymagic |= DM_RUID;
79072805
LW
2730 break; /* don't do magic till later */
2731 }
2732#ifdef HAS_SETRUID
b28d0864 2733 (void)setruid((Uid_t)PL_uid);
79072805
LW
2734#else
2735#ifdef HAS_SETREUID
3280af22 2736 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2737#else
85e6fe83 2738#ifdef HAS_SETRESUID
b28d0864 2739 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2740#else
75870ed3 2741 if (PL_uid == PL_euid) { /* special case $< = $> */
2742#ifdef PERL_DARWIN
2743 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2744 if (PL_uid != 0 && PerlProc_getuid() == 0)
2745 (void)PerlProc_setuid(0);
2746#endif
b28d0864 2747 (void)PerlProc_setuid(PL_uid);
75870ed3 2748 } else {
d8eceb89 2749 PL_uid = PerlProc_getuid();
cea2e8a9 2750 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2751 }
79072805
LW
2752#endif
2753#endif
85e6fe83 2754#endif
d8eceb89 2755 PL_uid = PerlProc_getuid();
79072805
LW
2756 break;
2757 case '>':
38ab35f8 2758 PL_euid = SvIV(sv);
3280af22
NIS
2759 if (PL_delaymagic) {
2760 PL_delaymagic |= DM_EUID;
79072805
LW
2761 break; /* don't do magic till later */
2762 }
2763#ifdef HAS_SETEUID
3280af22 2764 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2765#else
2766#ifdef HAS_SETREUID
b28d0864 2767 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2768#else
2769#ifdef HAS_SETRESUID
6b88bc9c 2770 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2771#else
b28d0864
NIS
2772 if (PL_euid == PL_uid) /* special case $> = $< */
2773 PerlProc_setuid(PL_euid);
a0d0e21e 2774 else {
e8ee3774 2775 PL_euid = PerlProc_geteuid();
cea2e8a9 2776 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2777 }
79072805
LW
2778#endif
2779#endif
85e6fe83 2780#endif
d8eceb89 2781 PL_euid = PerlProc_geteuid();
79072805
LW
2782 break;
2783 case '(':
38ab35f8 2784 PL_gid = SvIV(sv);
3280af22
NIS
2785 if (PL_delaymagic) {
2786 PL_delaymagic |= DM_RGID;
79072805
LW
2787 break; /* don't do magic till later */
2788 }
2789#ifdef HAS_SETRGID
b28d0864 2790 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2791#else
2792#ifdef HAS_SETREGID
3280af22 2793 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2794#else
2795#ifdef HAS_SETRESGID
b28d0864 2796 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2797#else
b28d0864
NIS
2798 if (PL_gid == PL_egid) /* special case $( = $) */
2799 (void)PerlProc_setgid(PL_gid);
748a9306 2800 else {
d8eceb89 2801 PL_gid = PerlProc_getgid();
cea2e8a9 2802 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2803 }
79072805
LW
2804#endif
2805#endif
85e6fe83 2806#endif
d8eceb89 2807 PL_gid = PerlProc_getgid();
79072805
LW
2808 break;
2809 case ')':
5cd24f17
PP
2810#ifdef HAS_SETGROUPS
2811 {
b83604b4 2812 const char *p = SvPV_const(sv, len);
757f63d8 2813 Groups_t *gary = NULL;
fb4089e0 2814#ifdef _SC_NGROUPS_MAX
2815 int maxgrp = sysconf(_SC_NGROUPS_MAX);
2816
2817 if (maxgrp < 0)
2818 maxgrp = NGROUPS;
2819#else
2820 int maxgrp = NGROUPS;
2821#endif
757f63d8
SP
2822
2823 while (isSPACE(*p))
2824 ++p;
2825 PL_egid = Atol(p);
fb4089e0 2826 for (i = 0; i < maxgrp; ++i) {
757f63d8
SP
2827 while (*p && !isSPACE(*p))
2828 ++p;
2829 while (isSPACE(*p))
2830 ++p;
2831 if (!*p)
2832 break;
2833 if(!gary)
2834 Newx(gary, i + 1, Groups_t);
2835 else
2836 Renew(gary, i + 1, Groups_t);
2837 gary[i] = Atol(p);
2838 }
2839 if (i)
2840 (void)setgroups(i, gary);
f5a63d97 2841 Safefree(gary);
5cd24f17
PP
2842 }
2843#else /* HAS_SETGROUPS */
38ab35f8 2844 PL_egid = SvIV(sv);
5cd24f17 2845#endif /* HAS_SETGROUPS */
3280af22
NIS
2846 if (PL_delaymagic) {
2847 PL_delaymagic |= DM_EGID;
79072805
LW
2848 break; /* don't do magic till later */
2849 }
2850#ifdef HAS_SETEGID
3280af22 2851 (void)setegid((Gid_t)PL_egid);
79072805
LW
2852#else
2853#ifdef HAS_SETREGID
b28d0864 2854 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2855#else
2856#ifdef HAS_SETRESGID
b28d0864 2857 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2858#else
b28d0864
NIS
2859 if (PL_egid == PL_gid) /* special case $) = $( */
2860 (void)PerlProc_setgid(PL_egid);
748a9306 2861 else {
d8eceb89 2862 PL_egid = PerlProc_getegid();
cea2e8a9 2863 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2864 }
79072805
LW
2865#endif
2866#endif
85e6fe83 2867#endif
d8eceb89 2868 PL_egid = PerlProc_getegid();
79072805
LW
2869 break;
2870 case ':':
2d8e6c8d 2871 PL_chopset = SvPV_force(sv,len);
79072805
LW
2872 break;
2873 case '0':
e2975953 2874 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2875#ifdef HAS_SETPROCTITLE
2876 /* The BSDs don't show the argv[] in ps(1) output, they
2877 * show a string from the process struct and provide
2878 * the setproctitle() routine to manipulate that. */
a2722ac9 2879 if (PL_origalen != 1) {
b83604b4 2880 s = SvPV_const(sv, len);
98b76f99 2881# if __FreeBSD_version > 410001
9aad2c0e 2882 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2883 * but not the "(perl) suffix from the ps(1)
2884 * output, because that's what ps(1) shows if the
2885 * argv[] is modified. */
6f2ad931 2886 setproctitle("-%s", s);
9aad2c0e 2887# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2888 /* This doesn't really work if you assume that
2889 * $0 = 'foobar'; will wipe out 'perl' from the $0
2890 * because in ps(1) output the result will be like
2891 * sprintf("perl: %s (perl)", s)
2892 * I guess this is a security feature:
2893 * one (a user process) cannot get rid of the original name.
2894 * --jhi */
2895 setproctitle("%s", s);
2896# endif
2897 }
9d3968b2 2898#elif defined(__hpux) && defined(PSTAT_SETCMD)
a2722ac9 2899 if (PL_origalen != 1) {
17aa7f3d 2900 union pstun un;
b83604b4 2901 s = SvPV_const(sv, len);
6867be6d 2902 un.pst_command = (char *)s;
17aa7f3d
JH
2903 pstat(PSTAT_SETCMD, un, len, 0, 0);
2904 }
9d3968b2 2905#else
2d2af554
GA
2906 if (PL_origalen > 1) {
2907 /* PL_origalen is set in perl_parse(). */
2908 s = SvPV_force(sv,len);
2909 if (len >= (STRLEN)PL_origalen-1) {
2910 /* Longer than original, will be truncated. We assume that
2911 * PL_origalen bytes are available. */
2912 Copy(s, PL_origargv[0], PL_origalen-1, char);
2913 }
2914 else {
2915 /* Shorter than original, will be padded. */
235ac35d 2916#ifdef PERL_DARWIN
60777a0d
JH
2917 /* Special case for Mac OS X: see [perl #38868] */
2918 const int pad = 0;
235ac35d 2919#else
8a89a4f1
MB
2920 /* Is the space counterintuitive? Yes.
2921 * (You were expecting \0?)
2922 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2923 * --jhi */
60777a0d 2924 const int pad = ' ';
235ac35d 2925#endif
60777a0d
JH
2926 Copy(s, PL_origargv[0], len, char);
2927 PL_origargv[0][len] = 0;
2928 memset(PL_origargv[0] + len + 1,
2929 pad, PL_origalen - len - 1);
2d2af554
GA
2930 }
2931 PL_origargv[0][PL_origalen-1] = 0;
2932 for (i = 1; i < PL_origargc; i++)
2933 PL_origargv[i] = 0;
7636ea95
AB
2934#ifdef HAS_PRCTL_SET_NAME
2935 /* Set the legacy process name in addition to the POSIX name on Linux */
2936 if (prctl(PR_SET_NAME, (unsigned long)s, 0, 0, 0) != 0) {
2937 /* diag_listed_as: SKIPME */
2938 Perl_croak(aTHX_ "Can't set $0 with prctl(): %s", Strerror(errno));
2939 }
2940#endif
79072805 2941 }
9d3968b2 2942#endif
e2975953 2943 UNLOCK_DOLLARZERO_MUTEX;
79072805
LW
2944 break;
2945 }
2946 return 0;
2947}
2948
2949I32
35a4481c 2950Perl_whichsig(pTHX_ const char *sig)
79072805 2951{
aadb217d 2952 register char* const* sigv;
7918f24d
NC
2953
2954 PERL_ARGS_ASSERT_WHICHSIG;
96a5add6 2955 PERL_UNUSED_CONTEXT;
79072805 2956
aadb217d 2957 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
79072805 2958 if (strEQ(sig,*sigv))
aadb217d 2959 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805
LW
2960#ifdef SIGCLD
2961 if (strEQ(sig,"CHLD"))
2962 return SIGCLD;
2963#endif
2964#ifdef SIGCHLD
2965 if (strEQ(sig,"CLD"))
2966 return SIGCHLD;
2967#endif
7f1236c0 2968 return -1;
79072805
LW
2969}
2970
ecfc5424 2971Signal_t
1e82f5a6 2972#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
b6455c53 2973Perl_sighandler(int sig, siginfo_t *sip, void *uap PERL_UNUSED_DECL)
1e82f5a6
SH
2974#else
2975Perl_sighandler(int sig)
2976#endif
79072805 2977{
1018e26f
NIS
2978#ifdef PERL_GET_SIG_CONTEXT
2979 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2980#else
cea2e8a9 2981 dTHX;
71d280e3 2982#endif
79072805 2983 dSP;
a0714e2c
SS
2984 GV *gv = NULL;
2985 SV *sv = NULL;
8772537c 2986 SV * const tSv = PL_Sv;
601f1833 2987 CV *cv = NULL;
533c011a 2988 OP *myop = PL_op;
84902520 2989 U32 flags = 0;
8772537c 2990 XPV * const tXpv = PL_Xpv;
0c4d3b5e 2991 I32 old_ss_ix = PL_savestack_ix;
71d280e3 2992
84902520 2993
727405f8 2994 if (!PL_psig_ptr[sig]) {
99ef548b 2995 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2996 PL_sig_name[sig]);
2997 exit(sig);
2998 }
ff0cee69 2999
84902520
TB
3000 /* Max number of items pushed there is 3*n or 4. We cannot fix
3001 infinity, so we fix 4 (in fact 5): */
72048cfe
DM
3002 if (PL_savestack_ix + 15 <= PL_savestack_max) {
3003 flags |= 1;
3280af22 3004 PL_savestack_ix += 5; /* Protect save in progress. */
0c4d3b5e 3005 SAVEDESTRUCTOR_X(S_unwind_handler_stack, NULL);
84902520 3006 }
72048cfe
DM
3007 if (PL_markstack_ptr < PL_markstack_max - 2) {
3008 flags |= 2;
3280af22 3009 PL_markstack_ptr++; /* Protect mark. */
72048cfe
DM
3010 }
3011 if (PL_scopestack_ix < PL_scopestack_max - 3) {
3012 flags |= 4;
3013 PL_scopestack_ix++;
3014 }
84902520 3015 /* sv_2cv is too complicated, try a simpler variant first: */
ea726b52 3016 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
8772537c
AL
3017 || SvTYPE(cv) != SVt_PVCV) {
3018 HV *st;
f2c0649b 3019 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
8772537c 3020 }
84902520 3021
a0d0e21e 3022 if (!cv || !CvROOT(cv)) {
a2a5de95
NC
3023 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
3024 PL_sig_name[sig], (gv ? GvENAME(gv)
3025 : ((cv && CvGV(cv))
3026 ? GvENAME(CvGV(cv))
3027 : "__ANON__")));
00d579c5 3028 goto cleanup;
79072805
LW
3029 }
3030
0c4d3b5e
DM
3031 sv = PL_psig_name[sig]
3032 ? SvREFCNT_inc_NN(PL_psig_name[sig])
3033 : newSVpv(PL_sig_name[sig],0);
72048cfe 3034 flags |= 8;
0c4d3b5e
DM
3035 SAVEFREESV(sv);
3036
3037 /* make sure our assumption about the size of the SAVEs are correct:
3038 * 3 for SAVEDESTRUCTOR_X, 2 for SAVEFREESV */
3039 assert(old_ss_ix + 2 + ((flags & 1) ? 3+5 : 0) == PL_savestack_ix);
e336de0d 3040
e788e7d3 3041 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 3042 PUSHMARK(SP);
79072805 3043 PUSHs(sv);
8aad04aa
JH
3044#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
3045 {
3046 struct sigaction oact;
3047
3048 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
8aad04aa
JH
3049 if (sip) {
3050 HV *sih = newHV();
ad64d0ec 3051 SV *rv = newRV_noinc(MUTABLE_SV(sih));
8aad04aa
JH
3052 /* The siginfo fields signo, code, errno, pid, uid,
3053 * addr, status, and band are defined by POSIX/SUSv3. */
85771703
NC
3054 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
3055 (void)hv_stores(sih, "code", newSViv(sip->si_code));
79dec0f4 3056#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
3057 hv_stores(sih, "errno", newSViv(sip->si_errno));
3058 hv_stores(sih, "status", newSViv(sip->si_status));
3059 hv_stores(sih, "uid", newSViv(sip->si_uid));
3060 hv_stores(sih, "pid", newSViv(sip->si_pid));
3061 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
3062 hv_stores(sih, "band", newSViv(sip->si_band));
79dec0f4 3063#endif
8aad04aa 3064 EXTEND(SP, 2);
ad64d0ec 3065 PUSHs(rv);
22f1178f 3066 mPUSHp((char *)sip, sizeof(*sip));
8aad04aa 3067 }
b4552a27 3068
8aad04aa
JH
3069 }
3070 }
3071#endif
79072805 3072 PUTBACK;
a0d0e21e 3073
ad64d0ec 3074 call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL);
79072805 3075
d3acc0f7 3076 POPSTACK;
1b266415 3077 if (SvTRUE(ERRSV)) {
1d615522 3078#ifndef PERL_MICRO
983dbef6 3079#ifdef HAS_SIGPROCMASK
1b266415
NIS
3080 /* Handler "died", for example to get out of a restart-able read().
3081 * Before we re-do that on its behalf re-enable the signal which was
3082 * blocked by the system when we entered.
3083 */
3084 sigset_t set;
3085 sigemptyset(&set);
3086 sigaddset(&set,sig);
3087 sigprocmask(SIG_UNBLOCK, &set, NULL);
3088#else
3089 /* Not clear if this will work */
3090 (void)rsignal(sig, SIG_IGN);
5c1546dc 3091 (void)rsignal(sig, PL_csighandlerp);
1b266415 3092#endif
1d615522 3093#endif /* !PERL_MICRO */
c5df3096 3094 die_sv(ERRSV);
1b266415 3095 }
00d579c5 3096cleanup:
0c4d3b5e
DM
3097 /* pop any of SAVEFREESV, SAVEDESTRUCTOR_X and "save in progress" */
3098 PL_savestack_ix = old_ss_ix;
72048cfe 3099 if (flags & 2)
3280af22 3100 PL_markstack_ptr--;
72048cfe 3101 if (flags & 4)
3280af22 3102 PL_scopestack_ix -= 1;
72048cfe 3103 if (flags & 8)
84902520 3104 SvREFCNT_dec(sv);
533c011a 3105 PL_op = myop; /* Apparently not needed... */
ac27b0f5 3106
3280af22
NIS
3107 PL_Sv = tSv; /* Restore global temporaries. */
3108 PL_Xpv = tXpv;
53bb94e2 3109 return;
79072805 3110}
4e35701f
NIS
3111
3112
51371543 3113static void
8772537c 3114S_restore_magic(pTHX_ const void *p)
51371543 3115{
97aff369 3116 dVAR;
8772537c
AL
3117 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
3118 SV* const sv = mgs->mgs_sv;
51371543
GS
3119
3120 if (!sv)
3121 return;
3122
3123 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3124 {
f8c7b90f 3125#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
3126 /* While magic was saved (and off) sv_setsv may well have seen
3127 this SV as a prime candidate for COW. */
3128 if (SvIsCOW(sv))
e424a81e 3129 sv_force_normal_flags(sv, 0);
f9701176
NC
3130#endif
3131
f9c6fee5
CS
3132 if (mgs->mgs_readonly)
3133 SvREADONLY_on(sv);
3134 if (mgs->mgs_magical)
3135 SvFLAGS(sv) |= mgs->mgs_magical;
51371543
GS
3136 else
3137 mg_magical(sv);
2b77b520
YST
3138 if (SvGMAGICAL(sv)) {
3139 /* downgrade public flags to private,
3140 and discard any other private flags */
3141
10edeb5d
JH
3142 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
3143 if (pubflags) {
3144 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
3145 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
2b77b520
YST
3146 }
3147 }
51371543
GS
3148 }
3149
3150 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3151
3152 /* If we're still on top of the stack, pop us off. (That condition
3153 * will be satisfied if restore_magic was called explicitly, but *not*
3154 * if it's being called via leave_scope.)
3155 * The reason for doing this is that otherwise, things like sv_2cv()
3156 * may leave alloc gunk on the savestack, and some code
3157 * (e.g. sighandler) doesn't expect that...
3158 */
3159 if (PL_savestack_ix == mgs->mgs_ss_ix)
3160 {
1be36ce0
NC
3161 UV popval = SSPOPUV;
3162 assert(popval == SAVEt_DESTRUCTOR_X);
51371543 3163 PL_savestack_ix -= 2;
1be36ce0
NC
3164 popval = SSPOPUV;
3165 assert((popval & SAVE_MASK) == SAVEt_ALLOC);
3166 PL_savestack_ix -= popval >> SAVE_TIGHT_SHIFT;
51371543 3167 }
8985fe98
DM
3168 if (SvREFCNT(sv) == 1) {
3169 /* We hold the last reference to this SV, which implies that the
3170 SV was deleted as a side effect of the routines we called.
3171 So artificially keep it alive a bit longer.
3172 We avoid turning on the TEMP flag, which can cause the SV's
3173 buffer to get stolen (and maybe other stuff). */
3174 int was_temp = SvTEMP(sv);
3175 sv_2mortal(sv);
3176 if (!was_temp) {
3177 SvTEMP_off(sv);
3178 }
3179 SvOK_off(sv);
3180 }
3181 else
3182 SvREFCNT_dec(sv); /* undo the inc in S_save_magic() */
51371543
GS
3183}
3184
0c4d3b5e
DM
3185/* clean up the mess created by Perl_sighandler().
3186 * Note that this is only called during an exit in a signal handler;
3187 * a die is trapped by the call_sv() and the SAVEDESTRUCTOR_X manually
3188 * skipped over. This is why we don't need to fix up the markstack and
3189 * scopestack - they're going to be set to 0 anyway */
3190
51371543 3191static void
8772537c 3192S_unwind_handler_stack(pTHX_ const void *p)
51371543 3193{
27da23d5 3194 dVAR;
0c4d3b5e 3195 PERL_UNUSED_ARG(p);
7918f24d 3196
0c4d3b5e 3197 PL_savestack_ix -= 5; /* Unprotect save in progress. */
51371543 3198}
1018e26f 3199
66610fdd 3200/*
b3ca2e83
NC
3201=for apidoc magic_sethint
3202
3203Triggered by a store to %^H, records the key/value pair to
c28fe1ec
NC
3204C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3205anything that would need a deep copy. Maybe we should warn if we find a
3206reference.
b3ca2e83
NC
3207
3208=cut
3209*/
3210int
3211Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3212{
3213 dVAR;
ad64d0ec 3214 SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr)
59cd0e26 3215 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
b3ca2e83 3216
7918f24d
NC
3217 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3218
e6e3e454
NC
3219 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3220 an alternative leaf in there, with PL_compiling.cop_hints being used if
3221 it's NULL. If needed for threads, the alternative could lock a mutex,
3222 or take other more complex action. */
3223
5b9c0671
NC
3224 /* Something changed in %^H, so it will need to be restored on scope exit.
3225 Doing this here saves a lot of doing it manually in perl code (and
3226 forgetting to do it, and consequent subtle errors. */
3227 PL_hints |= HINT_LOCALIZE_HH;
20439bc7
Z
3228 CopHINTHASH_set(&PL_compiling,
3229 cophh_store_sv(CopHINTHASH_get(&PL_compiling), key, 0, sv, 0));
b3ca2e83
NC
3230 return 0;
3231}
3232
3233/*
f175cff5 3234=for apidoc magic_clearhint
b3ca2e83 3235
c28fe1ec
NC
3236Triggered by a delete from %^H, records the key to
3237C<PL_compiling.cop_hints_hash>.
b3ca2e83
NC
3238
3239=cut
3240*/
3241int
3242Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3243{
3244 dVAR;
7918f24d
NC
3245
3246 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
f5a63d97
AL
3247 PERL_UNUSED_ARG(sv);
3248
b3ca2e83
NC
3249 assert(mg->mg_len == HEf_SVKEY);
3250
b3f24c00
MHM
3251 PERL_UNUSED_ARG(sv);
3252
5b9c0671 3253 PL_hints |= HINT_LOCALIZE_HH;
20439bc7
Z
3254 CopHINTHASH_set(&PL_compiling,
3255 cophh_delete_sv(CopHINTHASH_get(&PL_compiling),
3256 MUTABLE_SV(mg->mg_ptr), 0, 0));
b3ca2e83
NC
3257 return 0;
3258}
3259
3260/*
f747ebd6
Z
3261=for apidoc magic_clearhints
3262
3263Triggered by clearing %^H, resets C<PL_compiling.cop_hints_hash>.
3264
3265=cut
3266*/
3267int
3268Perl_magic_clearhints(pTHX_ SV *sv, MAGIC *mg)
3269{
3270 PERL_ARGS_ASSERT_MAGIC_CLEARHINTS;
3271 PERL_UNUSED_ARG(sv);
3272 PERL_UNUSED_ARG(mg);
20439bc7
Z
3273 cophh_free(CopHINTHASH_get(&PL_compiling));
3274 CopHINTHASH_set(&PL_compiling, cophh_new_empty());
f747ebd6
Z
3275 return 0;
3276}
3277
3278/*
66610fdd
RGS
3279 * Local variables:
3280 * c-indentation-style: bsd
3281 * c-basic-offset: 4
3282 * indent-tabs-mode: t
3283 * End:
3284 *
37442d52
RGS
3285 * ex: set ts=8 sts=4 sw=4 noet:
3286 */