This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add explicit 1-arg and 3-arg sig handler functions
[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"Magic" is special data attached to SV structures in order to give them
21"magical" properties. When any Perl code tries to read from, or assign to,
22an SV marked as magical, it calls the 'get' or 'set' function associated
154e47c8 23with that SV's magic. A get is called prior to reading an SV, in order to
ddfa107c 24give it a chance to update its internal value (get on $. writes the line
b53c43bf 25number of the last read filehandle into the SV's IV slot), while
166f8a29 26set is called after an SV has been written to, in order to allow it to make
ddfa107c 27use of its changed value (set on $/ copies the SV's new value to the
166f8a29
DM
28PL_rs global variable).
29
30Magic is implemented as a linked list of MAGIC structures attached to the
154e47c8 31SV. Each MAGIC struct holds the type of the magic, a pointer to an array
166f8a29 32of functions that implement the get(), set(), length() etc functions,
154e47c8 33plus space for some flags and pointers. For example, a tied variable has
166f8a29
DM
34a MAGIC structure that contains a pointer to the object associated with the
35tie.
36
7fefc6c1
KW
37=cut
38
ccfc67b7
JH
39*/
40
79072805 41#include "EXTERN.h"
864dbfa3 42#define PERL_IN_MG_C
79072805
LW
43#include "perl.h"
44
5cd24f17 45#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
b7953727
JH
46# ifdef I_GRP
47# include <grp.h>
48# endif
188ea221
CS
49#endif
50
757f63d8
SP
51#if defined(HAS_SETGROUPS)
52# ifndef NGROUPS
53# define NGROUPS 32
54# endif
55#endif
56
17aa7f3d
JH
57#ifdef __hpux
58# include <sys/pstat.h>
59#endif
60
7636ea95
AB
61#ifdef HAS_PRCTL_SET_NAME
62# include <sys/prctl.h>
63#endif
64
dc37125b 65
5e7940ce 66#ifdef PERL_USE_3ARG_SIGHANDLER
e7124897 67Signal_t Perl_csighandler(int sig, Siginfo_t *, void *);
8aad04aa 68#else
e69880a5 69Signal_t Perl_csighandler(int sig);
8aad04aa 70#endif
e69880a5 71
9cffb111
OS
72#ifdef __Lynx__
73/* Missing protos on LynxOS */
74void setruid(uid_t id);
75void seteuid(uid_t id);
76void setrgid(uid_t id);
77void setegid(uid_t id);
78#endif
79
c07a80fd 80/*
4bac9ae4 81 * Pre-magic setup and post-magic takedown.
c07a80fd
PP
82 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
83 */
84
85struct magic_state {
86 SV* mgs_sv;
455ece5e 87 I32 mgs_ss_ix;
a623f893 88 U32 mgs_flags;
150b625d 89 bool mgs_bumped;
c07a80fd 90};
455ece5e 91/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
92
93STATIC void
ff44333e 94S_save_magic_flags(pTHX_ I32 mgs_ix, SV *sv, U32 flags)
c07a80fd 95{
455ece5e 96 MGS* mgs;
150b625d 97 bool bumped = FALSE;
7918f24d 98
ff44333e 99 PERL_ARGS_ASSERT_SAVE_MAGIC_FLAGS;
7918f24d 100
4bac9ae4
CS
101 assert(SvMAGICAL(sv));
102
150b625d
DM
103 /* we shouldn't really be called here with RC==0, but it can sometimes
104 * happen via mg_clear() (which also shouldn't be called when RC==0,
105 * but it can happen). Handle this case gracefully(ish) by not RC++
106 * and thus avoiding the resultant double free */
107 if (SvREFCNT(sv) > 0) {
108 /* guard against sv getting freed midway through the mg clearing,
109 * by holding a private reference for the duration. */
110 SvREFCNT_inc_simple_void_NN(sv);
111 bumped = TRUE;
112 }
8985fe98 113
8772537c 114 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
115
116 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd 117 mgs->mgs_sv = sv;
a623f893 118 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 119 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
150b625d 120 mgs->mgs_bumped = bumped;
c07a80fd 121
ff44333e 122 SvFLAGS(sv) &= ~flags;
3805b5fb 123 SvREADONLY_off(sv);
c07a80fd
PP
124}
125
ff44333e
FC
126#define save_magic(a,b) save_magic_flags(a,b,SVs_GMG|SVs_SMG|SVs_RMG)
127
954c1994
GS
128/*
129=for apidoc mg_magical
130
fbe13c60 131Turns on the magical status of an SV. See C<L</sv_magic>>.
954c1994
GS
132
133=cut
134*/
135
8990e307 136void
ddeaf645 137Perl_mg_magical(SV *sv)
8990e307 138{
e1ec3a88 139 const MAGIC* mg;
7918f24d 140 PERL_ARGS_ASSERT_MG_MAGICAL;
f9c6fee5
CS
141
142 SvMAGICAL_off(sv);
218787bd 143 if ((mg = SvMAGIC(sv))) {
218787bd
VP
144 do {
145 const MGVTBL* const vtbl = mg->mg_virtual;
146 if (vtbl) {
147 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
148 SvGMAGICAL_on(sv);
149 if (vtbl->svt_set)
150 SvSMAGICAL_on(sv);
151 if (vtbl->svt_clear)
152 SvRMAGICAL_on(sv);
153 }
154 } while ((mg = mg->mg_moremagic));
155 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
156 SvRMAGICAL_on(sv);
8990e307
LW
157 }
158}
159
954c1994
GS
160/*
161=for apidoc mg_get
162
4b1e7f06 163Do magic before a value is retrieved from the SV. The type of SV must
796b6530 164be >= C<SVt_PVMG>. See C<L</sv_magic>>.
954c1994
GS
165
166=cut
167*/
168
79072805 169int
864dbfa3 170Perl_mg_get(pTHX_ SV *sv)
79072805 171{
35a4481c 172 const I32 mgs_ix = SSNEW(sizeof(MGS));
a9844598 173 bool saved = FALSE;
f9c6fee5 174 bool have_new = 0;
b949b68f 175 bool taint_only = TRUE; /* the only get method seen is taint */
ff76feab 176 MAGIC *newmg, *head, *cur, *mg;
6683b158 177
7918f24d
NC
178 PERL_ARGS_ASSERT_MG_GET;
179
407287f9
FC
180 if (PL_localizing == 1 && sv == DEFSV) return 0;
181
ff76feab
AMS
182 /* We must call svt_get(sv, mg) for each valid entry in the linked
183 list of magic. svt_get() may delete the current entry, add new
184 magic to the head of the list, or upgrade the SV. AMS 20010810 */
185
186 newmg = cur = head = mg = SvMAGIC(sv);
187 while (mg) {
35a4481c 188 const MGVTBL * const vtbl = mg->mg_virtual;
f9c6fee5 189 MAGIC * const nextmg = mg->mg_moremagic; /* it may delete itself */
ff76feab 190
2b260de0 191 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
a9844598
CS
192
193 /* taint's mg get is so dumb it doesn't need flag saving */
b949b68f
DM
194 if (mg->mg_type != PERL_MAGIC_taint) {
195 taint_only = FALSE;
196 if (!saved) {
197 save_magic(mgs_ix, sv);
198 saved = TRUE;
199 }
200 }
a9844598 201
16c91539 202 vtbl->svt_get(aTHX_ sv, mg);
b77f7d40 203
58f82c5c
DM
204 /* guard against magic having been deleted - eg FETCH calling
205 * untie */
f9c6fee5 206 if (!SvMAGIC(sv)) {
a623f893
FC
207 /* recalculate flags */
208 (SSPTR(mgs_ix, MGS *))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
58f82c5c 209 break;
f9c6fee5 210 }
b77f7d40 211
f9c6fee5 212 /* recalculate flags if this entry was deleted. */
ff76feab 213 if (mg->mg_flags & MGf_GSKIP)
a623f893
FC
214 (SSPTR(mgs_ix, MGS *))->mgs_flags &=
215 ~(SVs_GMG|SVs_SMG|SVs_RMG);
a0d0e21e 216 }
7d1328bb 217 else if (vtbl == &PL_vtbl_utf8) {
b949b68f
DM
218 /* get-magic can reallocate the PV, unless there's only taint
219 * magic */
220 if (taint_only) {
221 MAGIC *mg2;
222 for (mg2 = nextmg; mg2; mg2 = mg2->mg_moremagic) {
223 if ( mg2->mg_type != PERL_MAGIC_taint
224 && !(mg2->mg_flags & MGf_GSKIP)
225 && mg2->mg_virtual
226 && mg2->mg_virtual->svt_get
227 ) {
228 taint_only = FALSE;
229 break;
230 }
231 }
232 }
233 if (!taint_only)
234 magic_setutf8(sv, mg);
7d1328bb 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;
a623f893
FC
254 /* recalculate flags */
255 (SSPTR(mgs_ix, MGS *))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
760ac839 256 }
79072805 257 }
463ee0b2 258
a9844598
CS
259 if (saved)
260 restore_magic(INT2PTR(void *, (IV)mgs_ix));
261
79072805
LW
262 return 0;
263}
264
954c1994
GS
265/*
266=for apidoc mg_set
267
fbe13c60 268Do magic after a value is assigned to the SV. See C<L</sv_magic>>.
954c1994
GS
269
270=cut
271*/
272
79072805 273int
864dbfa3 274Perl_mg_set(pTHX_ SV *sv)
79072805 275{
35a4481c 276 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 277 MAGIC* mg;
463ee0b2
LW
278 MAGIC* nextmg;
279
7918f24d
NC
280 PERL_ARGS_ASSERT_MG_SET;
281
b5ed8c44
FC
282 if (PL_localizing == 2 && sv == DEFSV) return 0;
283
ff44333e 284 save_magic_flags(mgs_ix, sv, SVs_GMG|SVs_SMG); /* leave SVs_RMG on */
463ee0b2
LW
285
286 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
e1ec3a88 287 const MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 288 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
289 if (mg->mg_flags & MGf_GSKIP) {
290 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
a623f893 291 (SSPTR(mgs_ix, MGS*))->mgs_flags &= ~(SVs_GMG|SVs_SMG|SVs_RMG);
a0d0e21e 292 }
e0a73de4 293 if (PL_localizing == 2
b5ed8c44 294 && PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type))
e7cbf6c6 295 continue;
2b260de0 296 if (vtbl && vtbl->svt_set)
16c91539 297 vtbl->svt_set(aTHX_ sv, mg);
79072805 298 }
463ee0b2 299
8772537c 300 restore_magic(INT2PTR(void*, (IV)mgs_ix));
79072805
LW
301 return 0;
302}
303
954c1994
GS
304/*
305=for apidoc mg_length
306
d4e99c76 307Reports on the SV's length in bytes, calling length magic if available,
796b6530 308but does not set the UTF8 flag on C<sv>. It will fall back to 'get'
bd8446cf 309magic if there is no 'length' magic, but with no indication as to
796b6530
KW
310whether it called 'get' magic. It assumes C<sv> is a C<PVMG> or
311higher. Use C<sv_len()> instead.
954c1994
GS
312
313=cut
314*/
315
79072805 316U32
864dbfa3 317Perl_mg_length(pTHX_ SV *sv)
79072805
LW
318{
319 MAGIC* mg;
463ee0b2 320 STRLEN len;
463ee0b2 321
7918f24d
NC
322 PERL_ARGS_ASSERT_MG_LENGTH;
323
79072805 324 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 325 const MGVTBL * const vtbl = mg->mg_virtual;
2b260de0 326 if (vtbl && vtbl->svt_len) {
35a4481c 327 const I32 mgs_ix = SSNEW(sizeof(MGS));
455ece5e 328 save_magic(mgs_ix, sv);
a0d0e21e 329 /* omit MGf_GSKIP -- not changed here */
16c91539 330 len = vtbl->svt_len(aTHX_ sv, mg);
8772537c 331 restore_magic(INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
332 return len;
333 }
334 }
335
bd8446cf 336 (void)SvPV_const(sv, len);
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");
81d52ecd 367
93965878 368 }
a25b5927 369 NOT_REACHED; /* NOTREACHED */
93965878
NIS
370}
371
954c1994
GS
372/*
373=for apidoc mg_clear
374
fbe13c60 375Clear something magical that the SV represents. See C<L</sv_magic>>.
954c1994
GS
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
ed624ca8 405static MAGIC*
ddeaf645 406S_mg_findext_flags(const SV *sv, int type, const MGVTBL *vtbl, U32 flags)
39de7f53 407{
39de7f53
FR
408 assert(flags <= 1);
409
410 if (sv) {
411 MAGIC *mg;
412
413 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
414 if (mg->mg_type == type && (!flags || mg->mg_virtual == vtbl)) {
415 return mg;
416 }
417 }
418 }
419
420 return NULL;
421}
422
954c1994
GS
423/*
424=for apidoc mg_find
425
796b6530 426Finds the magic pointer for C<type> matching the SV. See C<L</sv_magic>>.
954c1994
GS
427
428=cut
429*/
430
93a17b20 431MAGIC*
ddeaf645 432Perl_mg_find(const SV *sv, int type)
93a17b20 433{
ddeaf645 434 return S_mg_findext_flags(sv, type, NULL, 0);
39de7f53
FR
435}
436
437/*
438=for apidoc mg_findext
439
440Finds the magic pointer of C<type> with the given C<vtbl> for the C<SV>. See
fbe13c60 441C<L</sv_magicext>>.
39de7f53
FR
442
443=cut
444*/
445
446MAGIC*
ddeaf645 447Perl_mg_findext(const SV *sv, int type, const MGVTBL *vtbl)
39de7f53 448{
ddeaf645 449 return S_mg_findext_flags(sv, type, vtbl, 1);
93a17b20
LW
450}
451
96c2a8ff
FC
452MAGIC *
453Perl_mg_find_mglob(pTHX_ SV *sv)
454{
455 PERL_ARGS_ASSERT_MG_FIND_MGLOB;
456 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
457 /* This sv is only a delegate. //g magic must be attached to
458 its target. */
459 vivify_defelem(sv);
460 sv = LvTARG(sv);
461 }
462 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
ddeaf645 463 return S_mg_findext_flags(sv, PERL_MAGIC_regex_global, 0, 0);
96c2a8ff
FC
464 return NULL;
465}
466
954c1994
GS
467/*
468=for apidoc mg_copy
469
fbe13c60 470Copies the magic from one SV to another. See C<L</sv_magic>>.
954c1994
GS
471
472=cut
473*/
474
79072805 475int
864dbfa3 476Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 477{
463ee0b2 478 int count = 0;
79072805 479 MAGIC* mg;
7918f24d
NC
480
481 PERL_ARGS_ASSERT_MG_COPY;
482
463ee0b2 483 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 484 const MGVTBL* const vtbl = mg->mg_virtual;
68795e93 485 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
16c91539 486 count += vtbl->svt_copy(aTHX_ sv, mg, nsv, key, klen);
68795e93 487 }
823a54a3
AL
488 else {
489 const char type = mg->mg_type;
1e73acc8 490 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
823a54a3
AL
491 sv_magic(nsv,
492 (type == PERL_MAGIC_tied)
493 ? SvTIED_obj(sv, mg)
27deb0cf 494 : mg->mg_obj,
823a54a3
AL
495 toLOWER(type), key, klen);
496 count++;
497 }
79072805 498 }
79072805 499 }
463ee0b2 500 return count;
79072805
LW
501}
502
954c1994 503/*
0cbee0a4
DM
504=for apidoc mg_localize
505
9711599e 506Copy some of the magic from an existing SV to new localized version of that
dfcfced8
KW
507SV. Container magic (I<e.g.>, C<%ENV>, C<$1>, C<tie>)
508gets copied, value magic doesn't (I<e.g.>,
796b6530 509C<taint>, C<pos>).
9711599e 510
796b6530 511If C<setmagic> is false then no set magic will be called on the new (empty) SV.
61b16eb9 512This typically means that assignment will soon follow (e.g. S<C<'local $x = $y'>>),
9711599e 513and that will handle the magic.
0cbee0a4
DM
514
515=cut
516*/
517
518void
af7df257 519Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
0cbee0a4
DM
520{
521 MAGIC *mg;
7918f24d
NC
522
523 PERL_ARGS_ASSERT_MG_LOCALIZE;
524
658a9f31
JD
525 if (nsv == DEFSV)
526 return;
527
0cbee0a4 528 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
2b1b43ea 529 const MGVTBL* const vtbl = mg->mg_virtual;
e0a73de4 530 if (PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type))
0cbee0a4 531 continue;
0cbee0a4 532
a5063e7c 533 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
16c91539 534 (void)vtbl->svt_local(aTHX_ nsv, mg);
a5063e7c 535 else
0cbee0a4
DM
536 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
537 mg->mg_ptr, mg->mg_len);
a5063e7c 538
0cbee0a4 539 /* container types should remain read-only across localization */
9ff3e6d8 540 SvFLAGS(nsv) |= SvREADONLY(sv);
0cbee0a4
DM
541 }
542
543 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
544 SvFLAGS(nsv) |= SvMAGICAL(sv);
af7df257 545 if (setmagic) {
9711599e
CS
546 PL_localizing = 1;
547 SvSETMAGIC(nsv);
548 PL_localizing = 0;
549 }
0cbee0a4
DM
550 }
551}
552
d9088386
Z
553#define mg_free_struct(sv, mg) S_mg_free_struct(aTHX_ sv, mg)
554static void
555S_mg_free_struct(pTHX_ SV *sv, MAGIC *mg)
556{
557 const MGVTBL* const vtbl = mg->mg_virtual;
558 if (vtbl && vtbl->svt_free)
559 vtbl->svt_free(aTHX_ sv, mg);
44955e7d
DM
560
561 if (mg->mg_type == PERL_MAGIC_collxfrm && mg->mg_len >= 0)
562 /* collate magic uses string len not buffer len, so
563 * free even with mg_len == 0 */
564 Safefree(mg->mg_ptr);
565 else if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
d9088386
Z
566 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
567 Safefree(mg->mg_ptr);
568 else if (mg->mg_len == HEf_SVKEY)
569 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
570 }
44955e7d 571
d9088386
Z
572 if (mg->mg_flags & MGf_REFCOUNTED)
573 SvREFCNT_dec(mg->mg_obj);
574 Safefree(mg);
575}
576
0cbee0a4 577/*
954c1994
GS
578=for apidoc mg_free
579
fbe13c60 580Free any magic storage used by the SV. See C<L</sv_magic>>.
954c1994
GS
581
582=cut
583*/
584
79072805 585int
864dbfa3 586Perl_mg_free(pTHX_ SV *sv)
79072805
LW
587{
588 MAGIC* mg;
589 MAGIC* moremagic;
7918f24d
NC
590
591 PERL_ARGS_ASSERT_MG_FREE;
592
79072805 593 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
79072805 594 moremagic = mg->mg_moremagic;
d9088386 595 mg_free_struct(sv, mg);
c826f41b 596 SvMAGIC_set(sv, moremagic);
79072805 597 }
b162af07 598 SvMAGIC_set(sv, NULL);
68f8932e 599 SvMAGICAL_off(sv);
79072805
LW
600 return 0;
601}
602
d9088386 603/*
44170c9a 604=for apidoc mg_free_type
d9088386 605
2d7f6611 606Remove any magic of type C<how> from the SV C<sv>. See L</sv_magic>.
d9088386
Z
607
608=cut
609*/
610
611void
612Perl_mg_free_type(pTHX_ SV *sv, int how)
613{
614 MAGIC *mg, *prevmg, *moremg;
615 PERL_ARGS_ASSERT_MG_FREE_TYPE;
616 for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) {
d9088386
Z
617 moremg = mg->mg_moremagic;
618 if (mg->mg_type == how) {
19742f39 619 MAGIC *newhead;
d9088386
Z
620 /* temporarily move to the head of the magic chain, in case
621 custom free code relies on this historical aspect of mg_free */
622 if (prevmg) {
623 prevmg->mg_moremagic = moremg;
624 mg->mg_moremagic = SvMAGIC(sv);
625 SvMAGIC_set(sv, mg);
626 }
627 newhead = mg->mg_moremagic;
628 mg_free_struct(sv, mg);
629 SvMAGIC_set(sv, newhead);
630 mg = prevmg;
631 }
632 }
633 mg_magical(sv);
634}
635
e0d4aead
TC
636/*
637=for apidoc mg_freeext
638
639Remove any magic of type C<how> using virtual table C<vtbl> from the
640SV C<sv>. See L</sv_magic>.
641
642C<mg_freeext(sv, how, NULL)> is equivalent to C<mg_free_type(sv, how)>.
643
644=cut
645*/
646
647void
648Perl_mg_freeext(pTHX_ SV *sv, int how, const MGVTBL *vtbl)
649{
650 MAGIC *mg, *prevmg, *moremg;
651 PERL_ARGS_ASSERT_MG_FREEEXT;
652 for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) {
653 MAGIC *newhead;
654 moremg = mg->mg_moremagic;
655 if (mg->mg_type == how && (vtbl == NULL || mg->mg_virtual == vtbl)) {
656 /* temporarily move to the head of the magic chain, in case
657 custom free code relies on this historical aspect of mg_free */
658 if (prevmg) {
659 prevmg->mg_moremagic = moremg;
660 mg->mg_moremagic = SvMAGIC(sv);
661 SvMAGIC_set(sv, mg);
662 }
663 newhead = mg->mg_moremagic;
664 mg_free_struct(sv, mg);
665 SvMAGIC_set(sv, newhead);
666 mg = prevmg;
667 }
668 }
669 mg_magical(sv);
670}
671
79072805 672#include <signal.h>
79072805 673
942e002e 674U32
864dbfa3 675Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 676{
8772537c 677 PERL_UNUSED_ARG(sv);
6cef1e77 678
7918f24d
NC
679 PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
680
0bd48802 681 if (PL_curpm) {
27deb0cf 682 REGEXP * const rx = PM_GETRE(PL_curpm);
0bd48802 683 if (rx) {
fa531f32
DM
684 const SSize_t n = (SSize_t)mg->mg_obj;
685 if (n == '+') { /* @+ */
a678b0e4 686 /* return the number possible */
07bc277f 687 return RX_NPARENS(rx);
27deb0cf 688 } else { /* @- @^CAPTURE @{^CAPTURE} */
07bc277f 689 I32 paren = RX_LASTPAREN(rx);
a678b0e4
YO
690
691 /* return the last filled */
f46c4081 692 while ( paren >= 0
07bc277f
NC
693 && (RX_OFFS(rx)[paren].start == -1
694 || RX_OFFS(rx)[paren].end == -1) )
f46c4081 695 paren--;
fa531f32 696 if (n == '-') {
27deb0cf
YO
697 /* @- */
698 return (U32)paren;
699 } else {
700 /* @^CAPTURE @{^CAPTURE} */
701 return paren >= 0 ? (U32)(paren-1) : (U32)-1;
702 }
703 }
0bd48802 704 }
8f580fb8 705 }
ac27b0f5 706
942e002e 707 return (U32)-1;
6cef1e77
IZ
708}
709
6502e081
DM
710/* @-, @+ */
711
6cef1e77 712int
864dbfa3 713Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 714{
7918f24d
NC
715 PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
716
0bd48802 717 if (PL_curpm) {
27deb0cf 718 REGEXP * const rx = PM_GETRE(PL_curpm);
0bd48802 719 if (rx) {
fa531f32 720 const SSize_t n = (SSize_t)mg->mg_obj;
27deb0cf
YO
721 /* @{^CAPTURE} does not contain $&, so we need to increment by 1 */
722 const I32 paren = mg->mg_len
fa531f32 723 + (n == '\003' ? 1 : 0);
a10eae29
FC
724 SSize_t s;
725 SSize_t t;
0bd48802
AL
726 if (paren < 0)
727 return 0;
07bc277f
NC
728 if (paren <= (I32)RX_NPARENS(rx) &&
729 (s = RX_OFFS(rx)[paren].start) != -1 &&
730 (t = RX_OFFS(rx)[paren].end) != -1)
0bd48802 731 {
a10eae29 732 SSize_t i;
27deb0cf 733
fa531f32 734 if (n == '+') /* @+ */
0bd48802 735 i = t;
fa531f32 736 else if (n == '-') /* @- */
0bd48802 737 i = s;
27deb0cf
YO
738 else { /* @^CAPTURE @{^CAPTURE} */
739 CALLREG_NUMBUF_FETCH(rx,paren,sv);
740 return 0;
741 }
0bd48802 742
a10eae29 743 if (RX_MATCH_UTF8(rx)) {
07bc277f 744 const char * const b = RX_SUBBEG(rx);
0bd48802 745 if (b)
6502e081
DM
746 i = RX_SUBCOFFSET(rx) +
747 utf8_length((U8*)b,
748 (U8*)(b-RX_SUBOFFSET(rx)+i));
0bd48802 749 }
727405f8 750
a10eae29 751 sv_setuv(sv, i);
232af1f8 752 return 0;
1aa99e6b 753 }
0bd48802 754 }
6cef1e77 755 }
3773545d 756 sv_set_undef(sv);
6cef1e77
IZ
757 return 0;
758}
759
6502e081
DM
760/* @-, @+ */
761
e4b89193 762int
a29d06ed
MG
763Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
764{
7918f24d 765 PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
dc3bf405 766 PERL_UNUSED_CONTEXT;
d4c19fe8
AL
767 PERL_UNUSED_ARG(sv);
768 PERL_UNUSED_ARG(mg);
cb077ed2 769 Perl_croak_no_modify();
0dbb1585 770 NORETURN_FUNCTION_END;
a29d06ed
MG
771}
772
ad3296c6 773#define SvRTRIM(sv) STMT_START { \
eae92ea0
GA
774 if (SvPOK(sv)) { \
775 STRLEN len = SvCUR(sv); \
776 char * const p = SvPVX(sv); \
8e6b4db6
PC
777 while (len > 0 && isSPACE(p[len-1])) \
778 --len; \
779 SvCUR_set(sv, len); \
780 p[len] = '\0'; \
781 } \
ad3296c6
SH
782} STMT_END
783
8b850bd5
NC
784void
785Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
786{
7918f24d
NC
787 PERL_ARGS_ASSERT_EMULATE_COP_IO;
788
8b850bd5 789 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
e03e82a0 790 sv_set_undef(sv);
8b850bd5 791 else {
b2aed775 792 SvPVCLEAR(sv);
8b850bd5
NC
793 SvUTF8_off(sv);
794 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
20439bc7 795 SV *const value = cop_hints_fetch_pvs(c, "open<", 0);
8b850bd5
NC
796 assert(value);
797 sv_catsv(sv, value);
798 }
799 sv_catpvs(sv, "\0");
800 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
20439bc7 801 SV *const value = cop_hints_fetch_pvs(c, "open>", 0);
8b850bd5
NC
802 assert(value);
803 sv_catsv(sv, value);
804 }
805 }
806}
807
d9fe7d23
KW
808STATIC void
809S_fixup_errno_string(pTHX_ SV* sv)
810{
811 /* Do what is necessary to fixup the non-empty string in 'sv' for return to
812 * Perl space. */
813
814 PERL_ARGS_ASSERT_FIXUP_ERRNO_STRING;
815
816 assert(SvOK(sv));
b35e4f8b
KW
817
818 if(strEQ(SvPVX(sv), "")) {
819 sv_catpv(sv, UNKNOWN_ERRNO_MSG);
820 }
821 else {
d9fe7d23 822
d3c0b317
KW
823 /* In some locales the error string may come back as UTF-8, in which
824 * case we should turn on that flag. This didn't use to happen, and to
5320b60d
KW
825 * avoid as many possible backward compatibility issues as possible, we
826 * don't turn on the flag unless we have to. So the flag stays off for
9f10db87 827 * an entirely invariant string. We assume that if the string looks
fab19207
KW
828 * like UTF-8 in a single script, it really is UTF-8: "text in any
829 * other encoding that uses bytes with the high bit set is extremely
830 * unlikely to pass a UTF-8 validity test"
9f10db87
KW
831 * (http://en.wikipedia.org/wiki/Charset_detection). There is a
832 * potential that we will get it wrong however, especially on short
a8f4b0c6 833 * error message text, so do an additional check. */
027a0d26
KW
834 if ( ! IN_BYTES /* respect 'use bytes' */
835 && is_utf8_non_invariant_string((U8*) SvPVX_const(sv), SvCUR(sv))
a8f4b0c6
KW
836
837#ifdef USE_LOCALE_MESSAGES
838
61520eee 839 && _is_cur_LC_category_utf8(LC_MESSAGES)
a8f4b0c6 840
fab19207
KW
841#else /* If can't check directly, at least can see if script is consistent,
842 under UTF-8, which gives us an extra measure of confidence. */
843
844 && isSCRIPT_RUN((const U8 *) SvPVX_const(sv), (U8 *) SvEND(sv),
8c53dada 845 TRUE) /* Means assume UTF-8 */
a8f4b0c6
KW
846#endif
847
848 ) {
d3c0b317
KW
849 SvUTF8_on(sv);
850 }
b35e4f8b 851 }
d9fe7d23
KW
852}
853
658db622 854/*
44170c9a 855=for apidoc sv_string_from_errnum
658db622
Z
856
857Generates the message string describing an OS error and returns it as
858an SV. C<errnum> must be a value that C<errno> could take, identifying
859the type of error.
860
861If C<tgtsv> is non-null then the string will be written into that SV
862(overwriting existing content) and it will be returned. If C<tgtsv>
863is a null pointer then the string will be written into a new mortal SV
864which will be returned.
865
866The message will be taken from whatever locale would be used by C<$!>,
867and will be encoded in the SV in whatever manner would be used by C<$!>.
868The details of this process are subject to future change. Currently,
869the message is taken from the C locale by default (usually producing an
870English message), and from the currently selected locale when in the scope
871of the C<use locale> pragma. A heuristic attempt is made to decode the
872message from the locale's character encoding, but it will only be decoded
873as either UTF-8 or ISO-8859-1. It is always correctly decoded in a UTF-8
874locale, usually in an ISO-8859-1 locale, and never in any other locale.
875
876The SV is always returned containing an actual string, and with no other
877OK bits set. Unlike C<$!>, a message is even yielded for C<errnum> zero
878(meaning success), and if no useful message is available then a useless
879string (currently empty) is returned.
880
881=cut
882*/
883
884SV *
885Perl_sv_string_from_errnum(pTHX_ int errnum, SV *tgtsv)
886{
887 char const *errstr;
888 if(!tgtsv)
889 tgtsv = sv_newmortal();
890 errstr = my_strerror(errnum);
891 if(errstr) {
892 sv_setpv(tgtsv, errstr);
893 fixup_errno_string(tgtsv);
894 } else {
895 SvPVCLEAR(tgtsv);
896 }
897 return tgtsv;
898}
899
25bbd826
CB
900#ifdef VMS
901#include <descrip.h>
902#include <starlet.h>
903#endif
904
79072805 905int
864dbfa3 906Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805 907{
eb578fdb
KW
908 I32 paren;
909 const char *s = NULL;
910 REGEXP *rx;
823a54a3 911 const char * const remaining = mg->mg_ptr + 1;
e91d8259 912 char nextchar;
79072805 913
7918f24d
NC
914 PERL_ARGS_ASSERT_MAGIC_GET;
915
e91d8259 916 if (!mg->mg_ptr) {
e91d8259 917 paren = mg->mg_len;
e91d8259 918 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
a6d0b1b5 919 do_numbuf_fetch:
e91d8259 920 CALLREG_NUMBUF_FETCH(rx,paren,sv);
e91d8259 921 }
e03e82a0
DM
922 else
923 goto set_undef;
e91d8259
NC
924 return 0;
925 }
926
927 nextchar = *remaining;
79072805 928 switch (*mg->mg_ptr) {
748a9306 929 case '\001': /* ^A */
f2da823f 930 if (SvOK(PL_bodytarget)) sv_copypv(sv, PL_bodytarget);
e03e82a0
DM
931 else
932 sv_set_undef(sv);
125b9982
NT
933 if (SvTAINTED(PL_bodytarget))
934 SvTAINTED_on(sv);
748a9306 935 break;
e5218da5 936 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
823a54a3 937 if (nextchar == '\0') {
e5218da5
GA
938 sv_setiv(sv, (IV)PL_minus_c);
939 }
823a54a3 940 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
e5218da5
GA
941 sv_setiv(sv, (IV)STATUS_NATIVE);
942 }
49460fe6
NIS
943 break;
944
79072805 945 case '\004': /* ^D */
aea4f609 946 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 947 break;
28f23441 948 case '\005': /* ^E */
6c6eab55
KW
949 if (nextchar != '\0') {
950 if (strEQ(remaining, "NCODING"))
3773545d 951 sv_set_undef(sv);
4f4ff3a7 952 break;
6c6eab55 953 }
4f4ff3a7
KW
954
955#if defined(VMS) || defined(OS2) || defined(WIN32)
956# if defined(VMS)
2a99ff86
KW
957 {
958 char msg[255];
959 $DESCRIPTOR(msgdsc,msg);
960 sv_setnv(sv,(NV) vaxc$errno);
961 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
962 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
963 else
b2aed775 964 SvPVCLEAR(sv);
2a99ff86 965 }
4b645107 966#elif defined(OS2)
2a99ff86
KW
967 if (!(_emx_env & 0x200)) { /* Under DOS */
968 sv_setnv(sv, (NV)errno);
2c6ee1a7 969 sv_setpv(sv, errno ? my_strerror(errno) : "");
2a99ff86
KW
970 } else {
971 if (errno != errno_isOS2) {
972 const int tmp = _syserrno();
973 if (tmp) /* 2nd call to _syserrno() makes it 0 */
974 Perl_rc = tmp;
975 }
976 sv_setnv(sv, (NV)Perl_rc);
977 sv_setpv(sv, os2error(Perl_rc));
978 }
1534c6f5 979 if (SvOK(sv) && strNE(SvPVX(sv), "")) {
99461a73
KW
980 fixup_errno_string(sv);
981 }
4f4ff3a7 982# elif defined(WIN32)
2a99ff86
KW
983 {
984 const DWORD dwErr = GetLastError();
985 sv_setnv(sv, (NV)dwErr);
986 if (dwErr) {
987 PerlProc_GetOSError(sv, dwErr);
2efab60d 988 fixup_errno_string(sv);
2a99ff86
KW
989 }
990 else
b2aed775 991 SvPVCLEAR(sv);
2a99ff86
KW
992 SetLastError(dwErr);
993 }
4f4ff3a7
KW
994# else
995# error Missing code for platform
996# endif
2a99ff86
KW
997 SvRTRIM(sv);
998 SvNOK_on(sv); /* what a wonderful hack! */
999 break;
4f4ff3a7
KW
1000#endif /* End of platforms with special handling for $^E; others just fall
1001 through to $! */
2165bd23 1002 /* FALLTHROUGH */
46f3e16c
KW
1003
1004 case '!':
1005 {
5f1b979d 1006 dSAVE_ERRNO;
46f3e16c 1007#ifdef VMS
5f1b979d 1008 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
46f3e16c 1009#else
5f1b979d 1010 sv_setnv(sv, (NV)errno);
46f3e16c
KW
1011#endif
1012#ifdef OS2
5f1b979d
KW
1013 if (errno == errno_isOS2 || errno == errno_isOS2_set)
1014 sv_setpv(sv, os2error(Perl_rc));
1015 else
46f3e16c 1016#endif
5f1b979d 1017 if (! errno) {
b2aed775 1018 SvPVCLEAR(sv);
46f3e16c 1019 }
5f1b979d 1020 else {
658db622
Z
1021 sv_string_from_errnum(errno, sv);
1022 /* If no useful string is available, don't
1023 * claim to have a string part. The SvNOK_on()
5f1b979d 1024 * below will cause just the number part to be valid */
658db622
Z
1025 if (!SvCUR(sv))
1026 SvPOK_off(sv);
5f1b979d
KW
1027 }
1028 RESTORE_ERRNO;
46f3e16c
KW
1029 }
1030
1031 SvRTRIM(sv);
1032 SvNOK_on(sv); /* what a wonderful hack! */
1033 break;
1034
79072805 1035 case '\006': /* ^F */
9f601cf3
TC
1036 if (nextchar == '\0') {
1037 sv_setiv(sv, (IV)PL_maxsysfd);
1038 }
1039 else if (strEQ(remaining, "EATURE_BITS")) {
1040 sv_setuv(sv, PL_compiling.cop_features);
1041 }
79072805 1042 break;
9ebf26ad
FR
1043 case '\007': /* ^GLOBAL_PHASE */
1044 if (strEQ(remaining, "LOBAL_PHASE")) {
1045 sv_setpvn(sv, PL_phase_names[PL_phase],
1046 strlen(PL_phase_names[PL_phase]));
1047 }
1048 break;
a0d0e21e 1049 case '\010': /* ^H */
5d382c25 1050 sv_setuv(sv, PL_hints);
a0d0e21e 1051 break;
9d116dd7 1052 case '\011': /* ^I */ /* NOT \t in EBCDIC */
120f7abe 1053 sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
79072805 1054 break;
8561ea1d
FC
1055 case '\014': /* ^LAST_FH */
1056 if (strEQ(remaining, "AST_FH")) {
745e740c 1057 if (PL_last_in_gv && (SV*)PL_last_in_gv != &PL_sv_undef) {
8561ea1d
FC
1058 assert(isGV_with_GP(PL_last_in_gv));
1059 SV_CHECK_THINKFIRST_COW_DROP(sv);
1060 prepare_SV_for_RV(sv);
1061 SvOK_off(sv);
1062 SvRV_set(sv, SvREFCNT_inc_simple_NN(PL_last_in_gv));
1063 SvROK_on(sv);
1064 sv_rvweaken(sv);
1065 }
3773545d
DM
1066 else
1067 sv_set_undef(sv);
8561ea1d
FC
1068 }
1069 break;
ac27b0f5 1070 case '\017': /* ^O & ^OPEN */
823a54a3 1071 if (nextchar == '\0') {
ac27b0f5 1072 sv_setpv(sv, PL_osname);
3511154c
DM
1073 SvTAINTED_off(sv);
1074 }
823a54a3 1075 else if (strEQ(remaining, "PEN")) {
8b850bd5 1076 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
ac27b0f5 1077 }
28f23441 1078 break;
9ebf26ad 1079 case '\020':
a6d0b1b5 1080 sv_setiv(sv, (IV)PL_perldb);
79072805 1081 break;
fb73857a 1082 case '\023': /* ^S */
f512d242 1083 if (nextchar == '\0') {
bc177e6b 1084 if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
0c34ef67 1085 SvOK_off(sv);
3280af22 1086 else if (PL_in_eval)
6dc8a9e4 1087 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
1088 else
1089 sv_setiv(sv, 0);
d58bf5aa 1090 }
f512d242
KW
1091 else if (strEQ(remaining, "AFE_LOCALES")) {
1092
1093#if ! defined(USE_ITHREADS) || defined(USE_THREAD_SAFE_LOCALE)
1094
1095 sv_setuv(sv, (UV) 1);
1096
1097#else
1098 sv_setuv(sv, (UV) 0);
1099
1100#endif
1101
1102 }
fb73857a 1103 break;
79072805 1104 case '\024': /* ^T */
823a54a3 1105 if (nextchar == '\0') {
88e89b8a 1106#ifdef BIG_TIME
7c36658b 1107 sv_setnv(sv, PL_basetime);
88e89b8a 1108#else
7c36658b 1109 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 1110#endif
7c36658b 1111 }
823a54a3 1112 else if (strEQ(remaining, "AINT"))
284167a5
SM
1113 sv_setiv(sv, TAINTING_get
1114 ? (TAINT_WARN_get || PL_unsafe ? -1 : 1)
9aa05f58 1115 : 0);
7c36658b 1116 break;
e07ea26a 1117 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
823a54a3 1118 if (strEQ(remaining, "NICODE"))
a05d7ebb 1119 sv_setuv(sv, (UV) PL_unicode);
823a54a3 1120 else if (strEQ(remaining, "TF8LOCALE"))
7cebcbc0 1121 sv_setuv(sv, (UV) PL_utf8locale);
e07ea26a
NC
1122 else if (strEQ(remaining, "TF8CACHE"))
1123 sv_setiv(sv, (IV) PL_utf8cache);
fde18df1
JH
1124 break;
1125 case '\027': /* ^W & $^WARNING_BITS */
823a54a3 1126 if (nextchar == '\0')
8298454c 1127 sv_setiv(sv, (IV)cBOOL(PL_dowarn & G_WARN_ON));
823a54a3 1128 else if (strEQ(remaining, "ARNING_BITS")) {
013b78e8 1129 if (PL_compiling.cop_warnings == pWARN_NONE) {
4438c4b7 1130 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
013b78e8
RGS
1131 }
1132 else if (PL_compiling.cop_warnings == pWARN_STD) {
e03e82a0 1133 goto set_undef;
013b78e8 1134 }
d3a7d8c7 1135 else if (PL_compiling.cop_warnings == pWARN_ALL) {
006c1a1d 1136 sv_setpvn(sv, WARN_ALLstring, WARNsize);
ac27b0f5 1137 }
4438c4b7 1138 else {
72dc9ed5
NC
1139 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
1140 *PL_compiling.cop_warnings);
ac27b0f5 1141 }
4438c4b7 1142 }
8ca2a5d6
DD
1143#ifdef WIN32
1144 else if (strEQ(remaining, "IN32_SLOPPY_STAT")) {
1145 sv_setiv(sv, w32_sloppystat);
1146 }
1147#endif
79072805 1148 break;
79072805 1149 case '+':
aaa362c4 1150 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2c7b5d76
DM
1151 paren = RX_LASTPAREN(rx);
1152 if (paren)
1153 goto do_numbuf_fetch;
79072805 1154 }
e03e82a0 1155 goto set_undef;
a01268b5
JH
1156 case '\016': /* ^N */
1157 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2c7b5d76
DM
1158 paren = RX_LASTCLOSEPAREN(rx);
1159 if (paren)
1160 goto do_numbuf_fetch;
a01268b5 1161 }
e03e82a0 1162 goto set_undef;
79072805 1163 case '.':
3280af22 1164 if (GvIO(PL_last_in_gv)) {
357c8808 1165 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805 1166 }
79072805
LW
1167 break;
1168 case '?':
809a5acc 1169 {
809a5acc 1170 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 1171#ifdef COMPLEX_STATUS
41cb7b2b 1172 SvUPGRADE(sv, SVt_PVLV);
6b88bc9c
GS
1173 LvTARGOFF(sv) = PL_statusvalue;
1174 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 1175#endif
809a5acc 1176 }
79072805
LW
1177 break;
1178 case '^':
acbe1b9d 1179 if (GvIOp(PL_defoutgv))
099be4f1 1180 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
1181 if (s)
1182 sv_setpv(sv,s);
1183 else {
3280af22 1184 sv_setpv(sv,GvENAME(PL_defoutgv));
cb421d5e 1185 sv_catpvs(sv,"_TOP");
79072805
LW
1186 }
1187 break;
1188 case '~':
acbe1b9d 1189 if (GvIOp(PL_defoutgv))
0daa599b 1190 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 1191 if (!s)
3280af22 1192 s = GvENAME(PL_defoutgv);
79072805
LW
1193 sv_setpv(sv,s);
1194 break;
79072805 1195 case '=':
099be4f1 1196 if (GvIO(PL_defoutgv))
0daa599b 1197 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
1198 break;
1199 case '-':
099be4f1 1200 if (GvIO(PL_defoutgv))
0daa599b 1201 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
1202 break;
1203 case '%':
099be4f1 1204 if (GvIO(PL_defoutgv))
0daa599b 1205 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 1206 break;
79072805 1207 case ':':
79072805
LW
1208 case '/':
1209 break;
1210 case '[':
e1dccc0d 1211 sv_setiv(sv, 0);
79072805
LW
1212 break;
1213 case '|':
099be4f1 1214 if (GvIO(PL_defoutgv))
0daa599b 1215 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805 1216 break;
79072805 1217 case '\\':
b2ce0fda 1218 if (PL_ors_sv)
f28098ff 1219 sv_copypv(sv, PL_ors_sv);
4bac9ae4 1220 else
e03e82a0 1221 goto set_undef;
79072805 1222 break;
0e219455 1223 case '$': /* $$ */
9cdac2a2
FC
1224 {
1225 IV const pid = (IV)PerlProc_getpid();
19db9fb7 1226 if (isGV(mg->mg_obj) || SvIV(mg->mg_obj) != pid) {
9cdac2a2
FC
1227 /* never set manually, or at least not since last fork */
1228 sv_setiv(sv, pid);
19db9fb7
Z
1229 /* never unsafe, even if reading in a tainted expression */
1230 SvTAINTED_off(sv);
1231 }
9cdac2a2
FC
1232 /* else a value has been assigned manually, so do nothing */
1233 }
0e219455 1234 break;
79072805 1235 case '<':
dfff4baf 1236 sv_setuid(sv, PerlProc_getuid());
79072805
LW
1237 break;
1238 case '>':
dfff4baf 1239 sv_setuid(sv, PerlProc_geteuid());
79072805
LW
1240 break;
1241 case '(':
dfff4baf 1242 sv_setgid(sv, PerlProc_getgid());
79072805
LW
1243 goto add_groups;
1244 case ')':
dfff4baf 1245 sv_setgid(sv, PerlProc_getegid());
79072805 1246 add_groups:
79072805 1247#ifdef HAS_GETGROUPS
79072805 1248 {
57d7c65e 1249 Groups_t *gary = NULL;
375ed12a
JH
1250 I32 num_groups = getgroups(0, gary);
1251 if (num_groups > 0) {
19742f39 1252 I32 i;
375ed12a
JH
1253 Newx(gary, num_groups, Groups_t);
1254 num_groups = getgroups(num_groups, gary);
1255 for (i = 0; i < num_groups; i++)
147e3846 1256 Perl_sv_catpvf(aTHX_ sv, " %" IVdf, (IV)gary[i]);
375ed12a
JH
1257 Safefree(gary);
1258 }
79072805 1259 }
155aba94 1260 (void)SvIOK_on(sv); /* what a wonderful hack! */
cd70abae 1261#endif
79072805 1262 break;
79072805
LW
1263 case '0':
1264 break;
1265 }
a0d0e21e 1266 return 0;
e03e82a0
DM
1267
1268 set_undef:
1269 sv_set_undef(sv);
1270 return 0;
79072805
LW
1271}
1272
1273int
864dbfa3 1274Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1275{
8772537c 1276 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805 1277
7918f24d
NC
1278 PERL_ARGS_ASSERT_MAGIC_GETUVAR;
1279
79072805 1280 if (uf && uf->uf_val)
24f81a43 1281 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
1282 return 0;
1283}
1284
1285int
864dbfa3 1286Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 1287{
9ae3433d 1288 STRLEN len = 0, klen;
12033064 1289 const char * const key = MgPV_const(mg,klen);
888a67f6 1290 const char *s = "";
1e422769 1291
7918f24d
NC
1292 PERL_ARGS_ASSERT_MAGIC_SETENV;
1293
613c63b4 1294 SvGETMAGIC(sv);
12033064 1295 if (SvOK(sv)) {
613c63b4
CS
1296 /* defined environment variables are byte strings; unfortunately
1297 there is no SvPVbyte_force_nomg(), so we must do this piecewise */
1298 (void)SvPV_force_nomg_nolen(sv);
1299 sv_utf8_downgrade(sv, /* fail_ok */ TRUE);
1300 if (SvUTF8(sv)) {
1301 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "Wide character in %s", "setenv");
1302 SvUTF8_off(sv);
1303 }
1304 s = SvPVX(sv);
1305 len = SvCUR(sv);
12033064
CS
1306 }
1307 my_setenv(key, s); /* does the deed */
1308
a0d0e21e
LW
1309#ifdef DYNAMIC_ENV_FETCH
1310 /* We just undefd an environment var. Is a replacement */
1311 /* waiting in the wings? */
1312 if (!len) {
6bd66ee9 1313 SV ** const valp = hv_fetch(GvHVn(PL_envgv), key, klen, FALSE);
fabdb6c0 1314 if (valp)
4ab59fcc 1315 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
a0d0e21e
LW
1316 }
1317#endif
1e422769 1318
28542d8f 1319#if !defined(OS2) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
1320 /* And you'll never guess what the dog had */
1321 /* in its mouth... */
284167a5 1322 if (TAINTING_get) {
1e422769
PP
1323 MgTAINTEDDIR_off(mg);
1324#ifdef VMS
b2aed775 1325 if (s && memEQs(key, klen, "DCL$PATH")) {
b8ffc8df 1326 char pathbuf[256], eltbuf[256], *cp, *elt;
1e422769
PP
1327 int i = 0, j = 0;
1328
6fca0082 1329 my_strlcpy(eltbuf, s, sizeof(eltbuf));
b8ffc8df 1330 elt = eltbuf;
1e422769
PP
1331 do { /* DCL$PATH may be a search list */
1332 while (1) { /* as may dev portion of any element */
1333 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1334 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1335 cando_by_name(S_IWUSR,0,elt) ) {
1336 MgTAINTEDDIR_on(mg);
1337 return 0;
1338 }
1339 }
bd61b366 1340 if ((cp = strchr(elt, ':')) != NULL)
1e422769
PP
1341 *cp = '\0';
1342 if (my_trnlnm(elt, eltbuf, j++))
1343 elt = eltbuf;
1344 else
1345 break;
1346 }
1347 j = 0;
1348 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1349 }
1350#endif /* VMS */
b2aed775 1351 if (s && memEQs(key, klen, "PATH")) {
8772537c 1352 const char * const strend = s + len;
463ee0b2 1353
70b484a8
DM
1354 /* set MGf_TAINTEDDIR if any component of the new path is
1355 * relative or world-writeable */
463ee0b2 1356 while (s < strend) {
96827780 1357 char tmpbuf[256];
c623ac67 1358 Stat_t st;
5f74f29c 1359 I32 i;
83269ced
CB
1360#ifdef __VMS /* Hmm. How do we get $Config{path_sep} from C? */
1361 const char path_sep = PL_perllib_sep;
f5a63d97
AL
1362#else
1363 const char path_sep = ':';
1364#endif
ba0a4150 1365 s = delimcpy_no_escape(tmpbuf, tmpbuf + sizeof tmpbuf,
427eaa01 1366 s, strend, path_sep, &i);
463ee0b2 1367 s++;
bb7a0f54 1368 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
83269ced
CB
1369#ifdef __VMS
1370 /* no colon thus no device name -- assume relative path */
1371 || (PL_perllib_sep != ':' && !strchr(tmpbuf, ':'))
1372 /* Using Unix separator, e.g. under bash, so act line Unix */
1373 || (PL_perllib_sep == ':' && *tmpbuf != '/')
326b5008
CB
1374#else
1375 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1376#endif
c6ed36e1 1377 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1378 MgTAINTEDDIR_on(mg);
1e422769
PP
1379 return 0;
1380 }
463ee0b2 1381 }
79072805
LW
1382 }
1383 }
28542d8f 1384#endif /* neither OS2 nor WIN32 nor MSDOS */
1e422769 1385
79072805
LW
1386 return 0;
1387}
1388
1389int
864dbfa3 1390Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1391{
7918f24d 1392 PERL_ARGS_ASSERT_MAGIC_CLEARENV;
8772537c 1393 PERL_UNUSED_ARG(sv);
bd61b366 1394 my_setenv(MgPV_nolen_const(mg),NULL);
85e6fe83
LW
1395 return 0;
1396}
1397
88e89b8a 1398int
864dbfa3 1399Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 1400{
7918f24d 1401 PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV;
65e66c80 1402 PERL_UNUSED_ARG(mg);
b0269e46 1403#if defined(VMS)
cea2e8a9 1404 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1405#else
3280af22 1406 if (PL_localizing) {
fb73857a 1407 HE* entry;
b0269e46 1408 my_clearenv();
85fbaab2
NC
1409 hv_iterinit(MUTABLE_HV(sv));
1410 while ((entry = hv_iternext(MUTABLE_HV(sv)))) {
fb73857a
PP
1411 I32 keylen;
1412 my_setenv(hv_iterkey(entry, &keylen),
85fbaab2 1413 SvPV_nolen_const(hv_iterval(MUTABLE_HV(sv), entry)));
fb73857a
PP
1414 }
1415 }
1416#endif
1417 return 0;
1418}
1419
1420int
864dbfa3 1421Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1422{
7918f24d 1423 PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV;
8772537c
AL
1424 PERL_UNUSED_ARG(sv);
1425 PERL_UNUSED_ARG(mg);
b0269e46
AB
1426#if defined(VMS)
1427 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1428#else
1429 my_clearenv();
1430#endif
3e3baf6d 1431 return 0;
66b1d557
HM
1432}
1433
64ca3a65 1434#ifndef PERL_MICRO
2d4fcd5e
AJ
1435#ifdef HAS_SIGPROCMASK
1436static void
1437restore_sigmask(pTHX_ SV *save_sv)
1438{
0bd48802 1439 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
f5a63d97 1440 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
2d4fcd5e
AJ
1441}
1442#endif
66b1d557 1443int
864dbfa3 1444Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1445{
88e89b8a 1446 /* Are we fetching a signal entry? */
708854f2 1447 int i = (I16)mg->mg_private;
7918f24d
NC
1448
1449 PERL_ARGS_ASSERT_MAGIC_GETSIG;
1450
708854f2 1451 if (!i) {
84c7b88c
BF
1452 STRLEN siglen;
1453 const char * sig = MgPV_const(mg, siglen);
1454 mg->mg_private = i = whichsig_pvn(sig, siglen);
708854f2
NC
1455 }
1456
e02bfb16 1457 if (i > 0) {
22c35a8c
GS
1458 if(PL_psig_ptr[i])
1459 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1460 else {
46da273f 1461 Sighandler_t sigstate = rsignal_state(i);
23ada85b 1462#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
46da273f
AL
1463 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1464 sigstate = SIG_IGN;
2e34cc90
CL
1465#endif
1466#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
46da273f
AL
1467 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1468 sigstate = SIG_DFL;
85b332e2 1469#endif
88e89b8a 1470 /* cache state so we don't fetch it again */
8aad04aa 1471 if(sigstate == (Sighandler_t) SIG_IGN)
6502358f 1472 sv_setpvs(sv,"IGNORE");
88e89b8a 1473 else
e03e82a0 1474 sv_set_undef(sv);
46da273f 1475 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
88e89b8a
PP
1476 SvTEMP_off(sv);
1477 }
1478 }
1479 return 0;
1480}
1481int
864dbfa3 1482Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1483{
7918f24d 1484 PERL_ARGS_ASSERT_MAGIC_CLEARSIG;
179c85a2 1485
38a124f0 1486 magic_setsig(NULL, mg);
179c85a2 1487 return sv_unmagic(sv, mg->mg_type);
88e89b8a 1488}
3d37d572 1489
dc37125b 1490
5e7940ce 1491#ifdef PERL_USE_3ARG_SIGHANDLER
dc37125b
DM
1492Signal_t
1493Perl_csighandler(int sig, Siginfo_t *sip, void *uap)
1494{
1495 Perl_csighandler3(sig, sip, uap);
1496}
8aad04aa 1497#else
dc37125b 1498Signal_t
0a8e0eff 1499Perl_csighandler(int sig)
dc37125b
DM
1500{
1501 Perl_csighandler3(sig, NULL, NULL);
1502}
8aad04aa 1503#endif
dc37125b
DM
1504
1505Signal_t
1506Perl_csighandler1(int sig)
1507{
1508 Perl_csighandler3(sig, NULL, NULL);
1509}
1510
1511/* Handler intended to directly handle signal calls from the kernel.
1512 * (Depending on configuration, the kernel may actually call one of the
1513 * wrappers csighandler() or csighandler1() instead.)
1514 * It either queues up the signal or dispatches it immediately depending
1515 * on whether safe signals are enabled and whether the signal is capable
1516 * of being deferred (e.g. SEGV isn't).
1517 */
1518
1519Signal_t
1520Perl_csighandler3(int sig, Siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
0a8e0eff 1521{
1018e26f
NIS
1522#ifdef PERL_GET_SIG_CONTEXT
1523 dTHXa(PERL_GET_SIG_CONTEXT);
1524#else
85b332e2
CL
1525 dTHX;
1526#endif
5e7940ce
DM
1527
1528#ifdef PERL_USE_3ARG_SIGHANDLER
ffb6256b
JH
1529#if defined(__cplusplus) && defined(__GNUC__)
1530 /* g++ doesn't support PERL_UNUSED_DECL, so the sip and uap
1531 * parameters would be warned about. */
1532 PERL_UNUSED_ARG(sip);
1533 PERL_UNUSED_ARG(uap);
1534#endif
1271a1be 1535#endif
5e7940ce 1536
23ada85b 1537#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1538 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1539 if (PL_sig_ignoring[sig]) return;
85b332e2 1540#endif
2e34cc90 1541#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1542 if (PL_sig_defaulting[sig])
2e34cc90
CL
1543#ifdef KILL_BY_SIGPRC
1544 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1545#else
1546 exit(1);
1547#endif
1548#endif
406878dd 1549 if (
853d2c32
RGS
1550#ifdef SIGILL
1551 sig == SIGILL ||
1552#endif
1553#ifdef SIGBUS
1554 sig == SIGBUS ||
1555#endif
1556#ifdef SIGSEGV
1557 sig == SIGSEGV ||
1558#endif
1559 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
4ffa73a3 1560 /* Call the perl level handler now--
31c91b43 1561 * with risk we may be in malloc() or being destructed etc. */
8a5e470b
DM
1562 {
1563 if (PL_sighandlerp == Perl_sighandler)
1564 /* default handler, so can call perly_sighandler() directly
1565 * rather than via Perl_sighandler, passing the extra
1566 * 'safe = false' arg
1567 */
ddb513d5 1568 Perl_perly_sighandler(sig, NULL, NULL, 0 /* unsafe */);
8a5e470b 1569 else
5e7940ce 1570#ifdef PERL_USE_3ARG_SIGHANDLER
8a5e470b 1571 (*PL_sighandlerp)(sig, NULL, NULL);
6e324d09 1572#else
8a5e470b 1573 (*PL_sighandlerp)(sig);
92807b6d 1574#endif
8a5e470b 1575 }
406878dd 1576 else {
31c91b43 1577 if (!PL_psig_pend) return;
406878dd
GA
1578 /* Set a flag to say this signal is pending, that is awaiting delivery after
1579 * the current Perl opcode completes */
1580 PL_psig_pend[sig]++;
1581
1582#ifndef SIG_PENDING_DIE_COUNT
1583# define SIG_PENDING_DIE_COUNT 120
1584#endif
fe13d51d 1585 /* Add one to say _a_ signal is pending */
406878dd
GA
1586 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1587 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1588 (unsigned long)SIG_PENDING_DIE_COUNT);
1589 }
0a8e0eff
NIS
1590}
1591
2e34cc90
CL
1592#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1593void
1594Perl_csighandler_init(void)
1595{
1596 int sig;
27da23d5 1597 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1598
1599 for (sig = 1; sig < SIG_SIZE; sig++) {
1600#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1601 dTHX;
27da23d5 1602 PL_sig_defaulting[sig] = 1;
5c1546dc 1603 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1604#endif
1605#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1606 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1607#endif
1608 }
27da23d5 1609 PL_sig_handlers_initted = 1;
2e34cc90
CL
1610}
1611#endif
1612
7fe50b8b
LT
1613#if defined HAS_SIGPROCMASK
1614static void
1615unblock_sigmask(pTHX_ void* newset)
1616{
dc3bf405 1617 PERL_UNUSED_CONTEXT;
7fe50b8b
LT
1618 sigprocmask(SIG_UNBLOCK, (sigset_t*)newset, NULL);
1619}
1620#endif
1621
0a8e0eff
NIS
1622void
1623Perl_despatch_signals(pTHX)
1624{
1625 int sig;
1626 PL_sig_pending = 0;
1627 for (sig = 1; sig < SIG_SIZE; sig++) {
1628 if (PL_psig_pend[sig]) {
d0166017 1629 dSAVE_ERRNO;
7fe50b8b 1630#ifdef HAS_SIGPROCMASK
55534442
LT
1631 /* From sigaction(2) (FreeBSD man page):
1632 * | Signal routines normally execute with the signal that
1633 * | caused their invocation blocked, but other signals may
1634 * | yet occur.
1635 * Emulation of this behavior (from within Perl) is enabled
1636 * using sigprocmask
1637 */
1638 int was_blocked;
1639 sigset_t newset, oldset;
1640
1641 sigemptyset(&newset);
1642 sigaddset(&newset, sig);
1643 sigprocmask(SIG_BLOCK, &newset, &oldset);
1644 was_blocked = sigismember(&oldset, sig);
7fe50b8b
LT
1645 if (!was_blocked) {
1646 SV* save_sv = newSVpvn((char *)(&newset), sizeof(sigset_t));
1647 ENTER;
1648 SAVEFREESV(save_sv);
1649 SAVEDESTRUCTOR_X(unblock_sigmask, SvPV_nolen(save_sv));
1650 }
55534442 1651#endif
25da4428 1652 PL_psig_pend[sig] = 0;
8a5e470b
DM
1653 if (PL_sighandlerp == Perl_sighandler)
1654 /* default handler, so can call perly_sighandler() directly
1655 * rather than via Perl_sighandler, passing the extra
1656 * 'safe = true' arg
1657 */
1658 Perl_perly_sighandler(sig, NULL, NULL, 1 /* safe */);
1659 else
5e7940ce 1660#ifdef PERL_USE_3ARG_SIGHANDLER
8a5e470b 1661 (*PL_sighandlerp)(sig, NULL, NULL);
6e324d09 1662#else
8a5e470b 1663 (*PL_sighandlerp)(sig);
92807b6d 1664#endif
8a5e470b 1665
7fe50b8b 1666#ifdef HAS_SIGPROCMASK
55534442 1667 if (!was_blocked)
7fe50b8b 1668 LEAVE;
55534442 1669#endif
d0166017 1670 RESTORE_ERRNO;
0a8e0eff
NIS
1671 }
1672 }
1673}
1674
38a124f0 1675/* sv of NULL signifies that we're acting as magic_clearsig. */
85e6fe83 1676int
864dbfa3 1677Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1678{
27da23d5 1679 dVAR;
79072805 1680 I32 i;
cbbf8932 1681 SV** svp = NULL;
2d4fcd5e
AJ
1682 /* Need to be careful with SvREFCNT_dec(), because that can have side
1683 * effects (due to closures). We must make sure that the new disposition
1684 * is in place before it is called.
1685 */
cbbf8932 1686 SV* to_dec = NULL;
e72dc28c 1687 STRLEN len;
2d4fcd5e
AJ
1688#ifdef HAS_SIGPROCMASK
1689 sigset_t set, save;
1690 SV* save_sv;
1691#endif
eb578fdb 1692 const char *s = MgPV_const(mg,len);
7918f24d
NC
1693
1694 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1695
748a9306 1696 if (*s == '_') {
84c7b88c 1697 if (memEQs(s, len, "__DIE__"))
3280af22 1698 svp = &PL_diehook;
84c7b88c 1699 else if (memEQs(s, len, "__WARN__")
38a124f0
NC
1700 && (sv ? 1 : PL_warnhook != PERL_WARNHOOK_FATAL)) {
1701 /* Merge the existing behaviours, which are as follows:
1702 magic_setsig, we always set svp to &PL_warnhook
1703 (hence we always change the warnings handler)
1704 For magic_clearsig, we don't change the warnings handler if it's
1705 set to the &PL_warnhook. */
3280af22 1706 svp = &PL_warnhook;
84c7b88c
BF
1707 } else if (sv) {
1708 SV *tmp = sv_newmortal();
1709 Perl_croak(aTHX_ "No such hook: %s",
1710 pv_pretty(tmp, s, len, 0, NULL, NULL, 0));
1711 }
748a9306 1712 i = 0;
38a124f0 1713 if (svp && *svp) {
9289f461
RGS
1714 if (*svp != PERL_WARNHOOK_FATAL)
1715 to_dec = *svp;
cbbf8932 1716 *svp = NULL;
4633a7c4 1717 }
748a9306
LW
1718 }
1719 else {
708854f2
NC
1720 i = (I16)mg->mg_private;
1721 if (!i) {
84c7b88c 1722 i = whichsig_pvn(s, len); /* ...no, a brick */
58a26b12 1723 mg->mg_private = (U16)i;
708854f2 1724 }
86d86cad 1725 if (i <= 0) {
84c7b88c
BF
1726 if (sv) {
1727 SV *tmp = sv_newmortal();
1728 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s",
1729 pv_pretty(tmp, s, len, 0, NULL, NULL, 0));
1730 }
748a9306
LW
1731 return 0;
1732 }
2d4fcd5e
AJ
1733#ifdef HAS_SIGPROCMASK
1734 /* Avoid having the signal arrive at a bad time, if possible. */
1735 sigemptyset(&set);
1736 sigaddset(&set,i);
1737 sigprocmask(SIG_BLOCK, &set, &save);
1738 ENTER;
9ff8e806 1739 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
2d4fcd5e
AJ
1740 SAVEFREESV(save_sv);
1741 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1742#endif
1743 PERL_ASYNC_CHECK();
2e34cc90 1744#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1745 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1746#endif
23ada85b 1747#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1748 PL_sig_ignoring[i] = 0;
85b332e2 1749#endif
2e34cc90 1750#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1751 PL_sig_defaulting[i] = 0;
2e34cc90 1752#endif
2d4fcd5e 1753 to_dec = PL_psig_ptr[i];
38a124f0
NC
1754 if (sv) {
1755 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1756 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
79fd3822
NC
1757
1758 /* Signals don't change name during the program's execution, so once
1759 they're cached in the appropriate slot of PL_psig_name, they can
1760 stay there.
1761
1762 Ideally we'd find some way of making SVs at (C) compile time, or
1763 at least, doing most of the work. */
1764 if (!PL_psig_name[i]) {
1765 PL_psig_name[i] = newSVpvn(s, len);
1766 SvREADONLY_on(PL_psig_name[i]);
1767 }
38a124f0 1768 } else {
79fd3822 1769 SvREFCNT_dec(PL_psig_name[i]);
38a124f0
NC
1770 PL_psig_name[i] = NULL;
1771 PL_psig_ptr[i] = NULL;
1772 }
748a9306 1773 }
38a124f0 1774 if (sv && (isGV_with_GP(sv) || SvROK(sv))) {
2d4fcd5e 1775 if (i) {
5c1546dc 1776 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e 1777 }
748a9306 1778 else
b37c2d43 1779 *svp = SvREFCNT_inc_simple_NN(sv);
38a124f0 1780 } else {
9dfa190b
NC
1781 if (sv && SvOK(sv)) {
1782 s = SvPV_force(sv, len);
1783 } else {
1784 sv = NULL;
1785 }
84c7b88c 1786 if (sv && memEQs(s, len,"IGNORE")) {
9dfa190b 1787 if (i) {
23ada85b 1788#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
9dfa190b
NC
1789 PL_sig_ignoring[i] = 1;
1790 (void)rsignal(i, PL_csighandlerp);
85b332e2 1791#else
9dfa190b 1792 (void)rsignal(i, (Sighandler_t) SIG_IGN);
85b332e2 1793#endif
9dfa190b 1794 }
2d4fcd5e 1795 }
84c7b88c 1796 else if (!sv || memEQs(s, len,"DEFAULT") || !len) {
9dfa190b 1797 if (i) {
2e34cc90 1798#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
9dfa190b
NC
1799 PL_sig_defaulting[i] = 1;
1800 (void)rsignal(i, PL_csighandlerp);
2e34cc90 1801#else
9dfa190b 1802 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2e34cc90 1803#endif
9dfa190b
NC
1804 }
1805 }
1806 else {
1807 /*
1808 * We should warn if HINT_STRICT_REFS, but without
1809 * access to a known hint bit in a known OP, we can't
1810 * tell whether HINT_STRICT_REFS is in force or not.
1811 */
0687887e 1812 if (!memchr(s, ':', len) && !memchr(s, '\'', len))
9dfa190b
NC
1813 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1814 SV_GMAGIC);
1815 if (i)
1816 (void)rsignal(i, PL_csighandlerp);
1817 else
1818 *svp = SvREFCNT_inc_simple_NN(sv);
136e0459 1819 }
748a9306 1820 }
9dfa190b 1821
2d4fcd5e
AJ
1822#ifdef HAS_SIGPROCMASK
1823 if(i)
1824 LEAVE;
1825#endif
ef8d46e8 1826 SvREFCNT_dec(to_dec);
79072805
LW
1827 return 0;
1828}
64ca3a65 1829#endif /* !PERL_MICRO */
79072805
LW
1830
1831int
864dbfa3 1832Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1833{
7918f24d 1834 PERL_ARGS_ASSERT_MAGIC_SETISA;
8772537c 1835 PERL_UNUSED_ARG(sv);
e1a479c5 1836
89c14e2e 1837 /* Skip _isaelem because _isa will handle it shortly */
354b0578 1838 if (PL_delaymagic & DM_ARRAY_ISA && mg->mg_type == PERL_MAGIC_isaelem)
89c14e2e
BB
1839 return 0;
1840
0e446081 1841 return magic_clearisa(NULL, mg);
463ee0b2
LW
1842}
1843
0e446081 1844/* sv of NULL signifies that we're acting as magic_setisa. */
463ee0b2 1845int
52b45067
RD
1846Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1847{
52b45067 1848 HV* stash;
7918f24d
NC
1849 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1850
52b45067 1851 /* Bail out if destruction is going on */
627364f1 1852 if(PL_phase == PERL_PHASE_DESTRUCT) return 0;
52b45067 1853
0e446081
NC
1854 if (sv)
1855 av_clear(MUTABLE_AV(sv));
52b45067 1856
6624142a
FC
1857 if (SvTYPE(mg->mg_obj) != SVt_PVGV && SvSMAGICAL(mg->mg_obj))
1858 /* This occurs with setisa_elem magic, which calls this
1859 same function. */
1860 mg = mg_find(mg->mg_obj, PERL_MAGIC_isa);
1861
316ebaf2 1862 assert(mg);
6624142a
FC
1863 if (SvTYPE(mg->mg_obj) == SVt_PVAV) { /* multiple stashes */
1864 SV **svp = AvARRAY((AV *)mg->mg_obj);
1865 I32 items = AvFILLp((AV *)mg->mg_obj) + 1;
1866 while (items--) {
1867 stash = GvSTASH((GV *)*svp++);
1868 if (stash && HvENAME(stash)) mro_isa_changed_in(stash);
1869 }
1870
1871 return 0;
1872 }
1873
52b45067 1874 stash = GvSTASH(
6624142a 1875 (const GV *)mg->mg_obj
52b45067
RD
1876 );
1877
00169e2c
FC
1878 /* The stash may have been detached from the symbol table, so check its
1879 name before doing anything. */
1880 if (stash && HvENAME_get(stash))
5562fa71 1881 mro_isa_changed_in(stash);
52b45067
RD
1882
1883 return 0;
1884}
1885
1886int
864dbfa3 1887Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1888{
85fbaab2 1889 HV * const hv = MUTABLE_HV(LvTARG(sv));
6ff81951 1890 I32 i = 0;
7918f24d
NC
1891
1892 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
8772537c 1893 PERL_UNUSED_ARG(mg);
7719e241 1894
6ff81951 1895 if (hv) {
497b47a8 1896 (void) hv_iterinit(hv);
ad64d0ec 1897 if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
1b95d04f 1898 i = HvUSEDKEYS(hv);
497b47a8
JH
1899 else {
1900 while (hv_iternext(hv))
1901 i++;
1902 }
6ff81951
GS
1903 }
1904
1905 sv_setiv(sv, (IV)i);
1906 return 0;
1907}
1908
1909int
864dbfa3 1910Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1911{
7918f24d 1912 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
8772537c 1913 PERL_UNUSED_ARG(mg);
946ec16e 1914 if (LvTARG(sv)) {
85fbaab2 1915 hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv));
946ec16e
PP
1916 }
1917 return 0;
ac27b0f5 1918}
946ec16e 1919
efaf3674
DM
1920/*
1921=for apidoc magic_methcall
1922
1923Invoke a magic method (like FETCH).
1924
2baee27b
FC
1925C<sv> and C<mg> are the tied thingy and the tie magic.
1926
1927C<meth> is the name of the method to call.
1928
1929C<argc> is the number of args (in addition to $self) to pass to the method.
1930
1931The C<flags> can be:
1932
1933 G_DISCARD invoke method with G_DISCARD flag and don't
1934 return a value
1935 G_UNDEF_FILL fill the stack with argc pointers to
1936 PL_sv_undef
1937
1938The arguments themselves are any values following the C<flags> argument.
efaf3674 1939
796b6530 1940Returns the SV (if any) returned by the method, or C<NULL> on failure.
efaf3674
DM
1941
1942
1943=cut
1944*/
1945
1946SV*
36925d9e 1947Perl_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, SV *meth, U32 flags,
046b0c7d 1948 U32 argc, ...)
a0d0e21e
LW
1949{
1950 dSP;
efaf3674 1951 SV* ret = NULL;
463ee0b2 1952
7918f24d
NC
1953 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1954
efaf3674 1955 ENTER;
d1d7a15d
NC
1956
1957 if (flags & G_WRITING_TO_STDERR) {
1958 SAVETMPS;
1959
2782061f 1960 save_re_context();
d1d7a15d
NC
1961 SAVESPTR(PL_stderrgv);
1962 PL_stderrgv = NULL;
1963 }
1964
efaf3674 1965 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1966 PUSHMARK(SP);
efaf3674 1967
052a7c76
DM
1968 /* EXTEND() expects a signed argc; don't wrap when casting */
1969 assert(argc <= I32_MAX);
1970 EXTEND(SP, (I32)argc+1);
67549bd2
NC
1971 PUSHs(SvTIED_obj(sv, mg));
1972 if (flags & G_UNDEF_FILL) {
1973 while (argc--) {
efaf3674 1974 PUSHs(&PL_sv_undef);
93965878 1975 }
67549bd2 1976 } else if (argc > 0) {
046b0c7d
NC
1977 va_list args;
1978 va_start(args, argc);
1979
1980 do {
35bc1e35
JK
1981 SV *const this_sv = va_arg(args, SV *);
1982 PUSHs(this_sv);
046b0c7d
NC
1983 } while (--argc);
1984
1985 va_end(args);
88e89b8a 1986 }
463ee0b2 1987 PUTBACK;
efaf3674 1988 if (flags & G_DISCARD) {
36925d9e 1989 call_sv(meth, G_SCALAR|G_DISCARD|G_METHOD_NAMED);
efaf3674
DM
1990 }
1991 else {
36925d9e 1992 if (call_sv(meth, G_SCALAR|G_METHOD_NAMED))
efaf3674
DM
1993 ret = *PL_stack_sp--;
1994 }
1995 POPSTACK;
d1d7a15d
NC
1996 if (flags & G_WRITING_TO_STDERR)
1997 FREETMPS;
efaf3674
DM
1998 LEAVE;
1999 return ret;
2000}
2001
efaf3674 2002/* wrapper for magic_methcall that creates the first arg */
463ee0b2 2003
efaf3674 2004STATIC SV*
36925d9e 2005S_magic_methcall1(pTHX_ SV *sv, const MAGIC *mg, SV *meth, U32 flags,
efaf3674
DM
2006 int n, SV *val)
2007{
efaf3674
DM
2008 SV* arg1 = NULL;
2009
2010 PERL_ARGS_ASSERT_MAGIC_METHCALL1;
2011
2012 if (mg->mg_ptr) {
2013 if (mg->mg_len >= 0) {
db4b3a1d 2014 arg1 = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
efaf3674
DM
2015 }
2016 else if (mg->mg_len == HEf_SVKEY)
2017 arg1 = MUTABLE_SV(mg->mg_ptr);
2018 }
2019 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
db4b3a1d 2020 arg1 = newSViv((IV)(mg->mg_len));
efaf3674
DM
2021 sv_2mortal(arg1);
2022 }
2023 if (!arg1) {
046b0c7d 2024 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n - 1, val);
efaf3674 2025 }
046b0c7d 2026 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n, arg1, val);
946ec16e
PP
2027}
2028
76e3520e 2029STATIC int
36925d9e 2030S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, SV *meth)
a0d0e21e 2031{
efaf3674 2032 SV* ret;
463ee0b2 2033
7918f24d
NC
2034 PERL_ARGS_ASSERT_MAGIC_METHPACK;
2035
efaf3674
DM
2036 ret = magic_methcall1(sv, mg, meth, 0, 1, NULL);
2037 if (ret)
2038 sv_setsv(sv, ret);
a0d0e21e
LW
2039 return 0;
2040}
463ee0b2 2041
a0d0e21e 2042int
864dbfa3 2043Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2044{
7918f24d
NC
2045 PERL_ARGS_ASSERT_MAGIC_GETPACK;
2046
fd69380d 2047 if (mg->mg_type == PERL_MAGIC_tiedelem)
a0d0e21e 2048 mg->mg_flags |= MGf_GSKIP;
36925d9e 2049 magic_methpack(sv,mg,SV_CONST(FETCH));
463ee0b2
LW
2050 return 0;
2051}
2052
2053int
864dbfa3 2054Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 2055{
b112cff9
DM
2056 MAGIC *tmg;
2057 SV *val;
7918f24d
NC
2058
2059 PERL_ARGS_ASSERT_MAGIC_SETPACK;
2060
b112cff9
DM
2061 /* in the code C<$tied{foo} = $val>, the "thing" that gets passed to
2062 * STORE() is not $val, but rather a PVLV (the sv in this call), whose
2063 * public flags indicate its value based on copying from $val. Doing
2064 * mg_set() on the PVLV temporarily does SvMAGICAL_off(), then calls us.
2065 * So STORE()'s $_[2] arg is a temporarily disarmed PVLV. This goes
2066 * wrong if $val happened to be tainted, as sv hasn't got magic
2067 * enabled, even though taint magic is in the chain. In which case,
2068 * fake up a temporary tainted value (this is easier than temporarily
2069 * re-enabling magic on sv). */
2070
284167a5 2071 if (TAINTING_get && (tmg = mg_find(sv, PERL_MAGIC_taint))
b112cff9
DM
2072 && (tmg->mg_len & 1))
2073 {
2074 val = sv_mortalcopy(sv);
2075 SvTAINTED_on(val);
2076 }
2077 else
2078 val = sv;
2079
36925d9e 2080 magic_methcall1(sv, mg, SV_CONST(STORE), G_DISCARD, 2, val);
463ee0b2
LW
2081 return 0;
2082}
2083
2084int
864dbfa3 2085Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2086{
7918f24d
NC
2087 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
2088
4c13be3f 2089 if (mg->mg_type == PERL_MAGIC_tiedscalar) return 0;
36925d9e 2090 return magic_methpack(sv,mg,SV_CONST(DELETE));
a0d0e21e 2091}
463ee0b2 2092
93965878
NIS
2093
2094U32
864dbfa3 2095Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 2096{
22846ab4 2097 I32 retval = 0;
efaf3674 2098 SV* retsv;
93965878 2099
7918f24d
NC
2100 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
2101
36925d9e 2102 retsv = magic_methcall1(sv, mg, SV_CONST(FETCHSIZE), 0, 1, NULL);
efaf3674
DM
2103 if (retsv) {
2104 retval = SvIV(retsv)-1;
22846ab4
AB
2105 if (retval < -1)
2106 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
93965878 2107 }
22846ab4 2108 return (U32) retval;
93965878
NIS
2109}
2110
cea2e8a9
GS
2111int
2112Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2113{
7918f24d
NC
2114 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
2115
36925d9e 2116 Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(CLEAR), G_DISCARD, 0);
463ee0b2
LW
2117 return 0;
2118}
2119
2120int
864dbfa3 2121Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 2122{
efaf3674 2123 SV* ret;
463ee0b2 2124
7918f24d
NC
2125 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
2126
36925d9e
RZ
2127 ret = SvOK(key) ? Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(NEXTKEY), 0, 1, key)
2128 : Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(FIRSTKEY), 0, 0);
efaf3674
DM
2129 if (ret)
2130 sv_setsv(key,ret);
79072805
LW
2131 return 0;
2132}
2133
2134int
1146e912 2135Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
a0d0e21e 2136{
7918f24d
NC
2137 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
2138
36925d9e 2139 return magic_methpack(sv,mg,SV_CONST(EXISTS));
ac27b0f5 2140}
a0d0e21e 2141
a3bcc51e
TP
2142SV *
2143Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
2144{
5fcbf73d 2145 SV *retval;
ad64d0ec
NC
2146 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
2147 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
a3bcc51e 2148
7918f24d
NC
2149 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
2150
a3bcc51e
TP
2151 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
2152 SV *key;
bfcb3514 2153 if (HvEITER_get(hv))
a3bcc51e
TP
2154 /* we are in an iteration so the hash cannot be empty */
2155 return &PL_sv_yes;
2156 /* no xhv_eiter so now use FIRSTKEY */
2157 key = sv_newmortal();
ad64d0ec 2158 magic_nextpack(MUTABLE_SV(hv), mg, key);
bfcb3514 2159 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
2160 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
2161 }
2162
2163 /* there is a SCALAR method that we can call */
36925d9e 2164 retval = Perl_magic_methcall(aTHX_ MUTABLE_SV(hv), mg, SV_CONST(SCALAR), 0, 0);
efaf3674 2165 if (!retval)
5fcbf73d 2166 retval = &PL_sv_undef;
a3bcc51e
TP
2167 return retval;
2168}
2169
a0d0e21e 2170int
864dbfa3 2171Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 2172{
089b5c60 2173 SV **svp;
7918f24d
NC
2174
2175 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
2176
089b5c60
NC
2177 /* The magic ptr/len for the debugger's hash should always be an SV. */
2178 if (UNLIKELY(mg->mg_len != HEf_SVKEY)) {
147e3846 2179 Perl_croak(aTHX_ "panic: magic_setdbline len=%" IVdf ", ptr='%s'",
7b0eb0b8 2180 (IV)mg->mg_len, mg->mg_ptr);
089b5c60
NC
2181 }
2182
2183 /* Use sv_2iv instead of SvIV() as the former generates smaller code, and
2184 setting/clearing debugger breakpoints is not a hot path. */
43e4250a
FC
2185 svp = av_fetch(MUTABLE_AV(mg->mg_obj),
2186 sv_2iv(MUTABLE_SV((mg)->mg_ptr)), FALSE);
089b5c60 2187
8772537c 2188 if (svp && SvIOKp(*svp)) {
88df5f01
FC
2189 OP * const o = INT2PTR(OP*,SvIVX(*svp));
2190 if (o) {
2191#ifdef PERL_DEBUG_READONLY_OPS
2192 Slab_to_rw(OpSLAB(o));
2193#endif
2194 /* set or clear breakpoint in the relevant control op */
089b5c60 2195 if (SvTRUE(sv))
88df5f01 2196 o->op_flags |= OPf_SPECIAL;
8772537c 2197 else
88df5f01
FC
2198 o->op_flags &= ~OPf_SPECIAL;
2199#ifdef PERL_DEBUG_READONLY_OPS
2200 Slab_to_ro(OpSLAB(o));
2201#endif
8772537c 2202 }
5df8de69 2203 }
79072805
LW
2204 return 0;
2205}
2206
2207int
8772537c 2208Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
79072805 2209{
502c6561 2210 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
2211
2212 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
2213
83bf042f 2214 if (obj) {
e1dccc0d 2215 sv_setiv(sv, AvFILL(obj));
83bf042f 2216 } else {
3773545d 2217 sv_set_undef(sv);
83bf042f 2218 }
79072805
LW
2219 return 0;
2220}
2221
2222int
864dbfa3 2223Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 2224{
502c6561 2225 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
2226
2227 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
2228
83bf042f 2229 if (obj) {
e1dccc0d 2230 av_fill(obj, SvIV(sv));
83bf042f 2231 } else {
a2a5de95
NC
2232 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
2233 "Attempt to set length of freed array");
83bf042f
NC
2234 }
2235 return 0;
2236}
2237
2238int
83f29afa
VP
2239Perl_magic_cleararylen_p(pTHX_ SV *sv, MAGIC *mg)
2240{
83f29afa
VP
2241 PERL_ARGS_ASSERT_MAGIC_CLEARARYLEN_P;
2242 PERL_UNUSED_ARG(sv);
dc3bf405 2243 PERL_UNUSED_CONTEXT;
83f29afa
VP
2244
2245 /* Reset the iterator when the array is cleared */
4803e7f7
Z
2246 if (sizeof(IV) == sizeof(SSize_t)) {
2247 *((IV *) &(mg->mg_len)) = 0;
2248 } else {
2249 if (mg->mg_ptr)
2250 *((IV *) mg->mg_ptr) = 0;
2251 }
83f29afa
VP
2252
2253 return 0;
2254}
2255
2256int
83bf042f
NC
2257Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
2258{
7918f24d 2259 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
53c1dcc0 2260 PERL_UNUSED_ARG(sv);
7918f24d 2261
94f3782b
DM
2262 /* during global destruction, mg_obj may already have been freed */
2263 if (PL_in_clean_all)
1ea47f64 2264 return 0;
94f3782b 2265
83bf042f
NC
2266 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
2267
2268 if (mg) {
2269 /* arylen scalar holds a pointer back to the array, but doesn't own a
2270 reference. Hence the we (the array) are about to go away with it
2271 still pointing at us. Clear its pointer, else it would be pointing
2272 at free memory. See the comment in sv_magic about reference loops,
2273 and why it can't own a reference to us. */
2274 mg->mg_obj = 0;
2275 }
a0d0e21e
LW
2276 return 0;
2277}
2278
2279int
864dbfa3 2280Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2281{
16eb5365 2282 SV* const lsv = LvTARG(sv);
96c2a8ff 2283 MAGIC * const found = mg_find_mglob(lsv);
7918f24d
NC
2284
2285 PERL_ARGS_ASSERT_MAGIC_GETPOS;
16eb5365 2286 PERL_UNUSED_ARG(mg);
ac27b0f5 2287
6174b39a
FC
2288 if (found && found->mg_len != -1) {
2289 STRLEN i = found->mg_len;
25fdce4a 2290 if (found->mg_flags & MGf_BYTES && DO_UTF8(lsv))
6174b39a
FC
2291 i = sv_pos_b2u_flags(lsv, i, SV_GMAGIC|SV_CONST_RETURN);
2292 sv_setuv(sv, i);
a0d0e21e 2293 return 0;
a0d0e21e 2294 }
3773545d 2295 sv_set_undef(sv);
a0d0e21e
LW
2296 return 0;
2297}
2298
2299int
864dbfa3 2300Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2301{
16eb5365 2302 SV* const lsv = LvTARG(sv);
a0d0e21e
LW
2303 SSize_t pos;
2304 STRLEN len;
53d44271 2305 MAGIC* found;
aec43834 2306 const char *s;
a0d0e21e 2307
7918f24d 2308 PERL_ARGS_ASSERT_MAGIC_SETPOS;
16eb5365 2309 PERL_UNUSED_ARG(mg);
ac27b0f5 2310
96c2a8ff 2311 found = mg_find_mglob(lsv);
3881461a 2312 if (!found) {
a0d0e21e
LW
2313 if (!SvOK(sv))
2314 return 0;
96c2a8ff 2315 found = sv_magicext_mglob(lsv);
a0d0e21e
LW
2316 }
2317 else if (!SvOK(sv)) {
3881461a 2318 found->mg_len = -1;
a0d0e21e
LW
2319 return 0;
2320 }
aec43834 2321 s = SvPV_const(lsv, len);
a0d0e21e 2322
e1dccc0d 2323 pos = SvIV(sv);
a0ed51b3 2324
7e2040f0 2325 if (DO_UTF8(lsv)) {
19742f39 2326 const STRLEN ulen = sv_or_pv_len_utf8(lsv, s, len);
a0ed51b3
LW
2327 if (ulen)
2328 len = ulen;
a0ed51b3
LW
2329 }
2330
a0d0e21e
LW
2331 if (pos < 0) {
2332 pos += len;
2333 if (pos < 0)
2334 pos = 0;
2335 }
eb160463 2336 else if (pos > (SSize_t)len)
a0d0e21e 2337 pos = len;
a0ed51b3 2338
3881461a 2339 found->mg_len = pos;
25fdce4a 2340 found->mg_flags &= ~(MGf_MINMATCH|MGf_BYTES);
a0d0e21e 2341
79072805
LW
2342 return 0;
2343}
2344
2345int
864dbfa3 2346Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
2347{
2348 STRLEN len;
35a4481c 2349 SV * const lsv = LvTARG(sv);
b83604b4 2350 const char * const tmps = SvPV_const(lsv,len);
777f7c56
EB
2351 STRLEN offs = LvTARGOFF(sv);
2352 STRLEN rem = LvTARGLEN(sv);
b063b0a8
DIM
2353 const bool negoff = LvFLAGS(sv) & LVf_NEG_OFF;
2354 const bool negrem = LvFLAGS(sv) & LVf_NEG_LEN;
7918f24d
NC
2355
2356 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
8772537c 2357 PERL_UNUSED_ARG(mg);
6ff81951 2358
83f78d1a 2359 if (!translate_substr_offsets(
4ddea69a 2360 SvUTF8(lsv) ? sv_or_pv_len_utf8(lsv, tmps, len) : len,
83f78d1a
FC
2361 negoff ? -(IV)offs : (IV)offs, !negoff,
2362 negrem ? -(IV)rem : (IV)rem, !negrem, &offs, &rem
2363 )) {
2364 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
e03e82a0 2365 sv_set_undef(sv);
83f78d1a
FC
2366 return 0;
2367 }
2368
9aa983d2 2369 if (SvUTF8(lsv))
4ddea69a 2370 offs = sv_or_pv_pos_u2b(lsv, tmps, offs, &rem);
1c900557 2371 sv_setpvn(sv, tmps + offs, rem);
9aa983d2 2372 if (SvUTF8(lsv))
2ef4b674 2373 SvUTF8_on(sv);
6ff81951
GS
2374 return 0;
2375}
2376
2377int
864dbfa3 2378Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 2379{
83f78d1a 2380 STRLEN len, lsv_len, oldtarglen, newtarglen;
5fcbf73d 2381 const char * const tmps = SvPV_const(sv, len);
dd374669 2382 SV * const lsv = LvTARG(sv);
777f7c56
EB
2383 STRLEN lvoff = LvTARGOFF(sv);
2384 STRLEN lvlen = LvTARGLEN(sv);
b063b0a8
DIM
2385 const bool negoff = LvFLAGS(sv) & LVf_NEG_OFF;
2386 const bool neglen = LvFLAGS(sv) & LVf_NEG_LEN;
7918f24d
NC
2387
2388 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
8772537c 2389 PERL_UNUSED_ARG(mg);
075a4a2b 2390
a74fb2cd
FC
2391 SvGETMAGIC(lsv);
2392 if (SvROK(lsv))
2393 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
2394 "Attempt to use reference as lvalue in substr"
2395 );
fc061ed8
FC
2396 SvPV_force_nomg(lsv,lsv_len);
2397 if (SvUTF8(lsv)) lsv_len = sv_len_utf8_nomg(lsv);
83f78d1a
FC
2398 if (!translate_substr_offsets(
2399 lsv_len,
2400 negoff ? -(IV)lvoff : (IV)lvoff, !negoff,
2401 neglen ? -(IV)lvlen : (IV)lvlen, !neglen, &lvoff, &lvlen
2402 ))
2403 Perl_croak(aTHX_ "substr outside of string");
2404 oldtarglen = lvlen;
1aa99e6b 2405 if (DO_UTF8(sv)) {
73a087f0 2406 sv_utf8_upgrade_nomg(lsv);
d931b1be 2407 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
a74fb2cd 2408 sv_insert_flags(lsv, lvoff, lvlen, tmps, len, 0);
7a385470 2409 newtarglen = sv_or_pv_len_utf8(sv, tmps, len);
9aa983d2
JH
2410 SvUTF8_on(lsv);
2411 }
0d336106 2412 else if (SvUTF8(lsv)) {
5fcbf73d 2413 const char *utf8;
d931b1be 2414 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
83f78d1a 2415 newtarglen = len;
5fcbf73d 2416 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
a74fb2cd 2417 sv_insert_flags(lsv, lvoff, lvlen, utf8, len, 0);
5fcbf73d 2418 Safefree(utf8);
1aa99e6b 2419 }
b76f3ce2 2420 else {
a74fb2cd 2421 sv_insert_flags(lsv, lvoff, lvlen, tmps, len, 0);
83f78d1a 2422 newtarglen = len;
b76f3ce2 2423 }
83f78d1a
FC
2424 if (!neglen) LvTARGLEN(sv) = newtarglen;
2425 if (negoff) LvTARGOFF(sv) += newtarglen - oldtarglen;
b76f3ce2 2426
79072805
LW
2427 return 0;
2428}
2429
2430int
864dbfa3 2431Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2432{
7918f24d 2433 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
8772537c 2434 PERL_UNUSED_ARG(sv);
9a9b5ec9
DM
2435#ifdef NO_TAINT_SUPPORT
2436 PERL_UNUSED_ARG(mg);
2437#endif
7918f24d 2438
64ff300b 2439 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1) && IN_PERL_RUNTIME);
463ee0b2
LW
2440 return 0;
2441}
2442
2443int
864dbfa3 2444Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2445{
7918f24d 2446 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
8772537c 2447 PERL_UNUSED_ARG(sv);
7918f24d 2448
b01e650a 2449 /* update taint status */
284167a5 2450 if (TAINT_get)
b01e650a
DM
2451 mg->mg_len |= 1;
2452 else
2453 mg->mg_len &= ~1;
463ee0b2
LW
2454 return 0;
2455}
2456
2457int
864dbfa3 2458Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 2459{
35a4481c 2460 SV * const lsv = LvTARG(sv);
1b92e694 2461 char errflags = LvFLAGS(sv);
7918f24d
NC
2462
2463 PERL_ARGS_ASSERT_MAGIC_GETVEC;
8772537c 2464 PERL_UNUSED_ARG(mg);
6ff81951 2465
1b92e694 2466 /* non-zero errflags implies deferred out-of-range condition */
b063b0a8 2467 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
1b92e694 2468 sv_setuv(sv, errflags ? 0 : do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951 2469
6ff81951
GS
2470 return 0;
2471}
2472
2473int
864dbfa3 2474Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 2475{
7918f24d 2476 PERL_ARGS_ASSERT_MAGIC_SETVEC;
8772537c 2477 PERL_UNUSED_ARG(mg);
79072805
LW
2478 do_vecset(sv); /* XXX slurp this routine */
2479 return 0;
2480}
2481
13733cde
FC
2482SV *
2483Perl_defelem_target(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 2484{
a0714e2c 2485 SV *targ = NULL;
13733cde
FC
2486 PERL_ARGS_ASSERT_DEFELEM_TARGET;
2487 if (!mg) mg = mg_find(sv, PERL_MAGIC_defelem);
2488 assert(mg);
5f05dabc 2489 if (LvTARGLEN(sv)) {
68dc0745 2490 if (mg->mg_obj) {
8772537c 2491 SV * const ahv = LvTARG(sv);
85fbaab2 2492 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
6d822dc4
MS
2493 if (he)
2494 targ = HeVAL(he);
68dc0745 2495 }
bbfdc870 2496 else if (LvSTARGOFF(sv) >= 0) {
502c6561 2497 AV *const av = MUTABLE_AV(LvTARG(sv));
bbfdc870 2498 if (LvSTARGOFF(sv) <= AvFILL(av))
ca58dfd9
FC
2499 {
2500 if (SvRMAGICAL(av)) {
2501 SV * const * const svp = av_fetch(av, LvSTARGOFF(sv), 0);
2502 targ = svp ? *svp : NULL;
2503 }
2504 else
bbfdc870 2505 targ = AvARRAY(av)[LvSTARGOFF(sv)];
ca58dfd9 2506 }
68dc0745 2507 }
46da273f 2508 if (targ && (targ != &PL_sv_undef)) {
68dc0745
PP
2509 /* somebody else defined it for us */
2510 SvREFCNT_dec(LvTARG(sv));
b37c2d43 2511 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
68dc0745
PP
2512 LvTARGLEN(sv) = 0;
2513 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2514 mg->mg_obj = NULL;
68dc0745
PP
2515 mg->mg_flags &= ~MGf_REFCOUNTED;
2516 }
13733cde 2517 return targ;
5f05dabc 2518 }
71be2cbc 2519 else
13733cde
FC
2520 return LvTARG(sv);
2521}
2522
2523int
2524Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2525{
2526 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2527
2528 sv_setsv(sv, defelem_target(sv, mg));
71be2cbc
PP
2529 return 0;
2530}
2531
2532int
864dbfa3 2533Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 2534{
7918f24d 2535 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
8772537c 2536 PERL_UNUSED_ARG(mg);
71be2cbc 2537 if (LvTARGLEN(sv))
68dc0745
PP
2538 vivify_defelem(sv);
2539 if (LvTARG(sv)) {
5f05dabc 2540 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
2541 SvSETMAGIC(LvTARG(sv));
2542 }
5f05dabc
PP
2543 return 0;
2544}
2545
71be2cbc 2546void
864dbfa3 2547Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 2548{
74e13ce4 2549 MAGIC *mg;
a0714e2c 2550 SV *value = NULL;
71be2cbc 2551
7918f24d
NC
2552 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2553
14befaf4 2554 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 2555 return;
68dc0745 2556 if (mg->mg_obj) {
8772537c 2557 SV * const ahv = LvTARG(sv);
85fbaab2 2558 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
6d822dc4
MS
2559 if (he)
2560 value = HeVAL(he);
3280af22 2561 if (!value || value == &PL_sv_undef)
be2597df 2562 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
71be2cbc 2563 }
bbfdc870
FC
2564 else if (LvSTARGOFF(sv) < 0)
2565 Perl_croak(aTHX_ PL_no_aelem, LvSTARGOFF(sv));
68dc0745 2566 else {
502c6561 2567 AV *const av = MUTABLE_AV(LvTARG(sv));
bbfdc870 2568 if ((I32)LvTARGLEN(sv) < 0 && LvSTARGOFF(sv) > AvFILL(av))
a0714e2c 2569 LvTARG(sv) = NULL; /* array can't be extended */
68dc0745 2570 else {
bbfdc870 2571 SV* const * const svp = av_fetch(av, LvSTARGOFF(sv), TRUE);
031c6d82 2572 if (!svp || !(value = *svp))
bbfdc870 2573 Perl_croak(aTHX_ PL_no_aelem, LvSTARGOFF(sv));
68dc0745
PP
2574 }
2575 }
b37c2d43 2576 SvREFCNT_inc_simple_void(value);
68dc0745
PP
2577 SvREFCNT_dec(LvTARG(sv));
2578 LvTARG(sv) = value;
71be2cbc 2579 LvTARGLEN(sv) = 0;
68dc0745 2580 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2581 mg->mg_obj = NULL;
68dc0745 2582 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
2583}
2584
2585int
1f1dcfb5
FC
2586Perl_magic_setnonelem(pTHX_ SV *sv, MAGIC *mg)
2587{
2588 PERL_ARGS_ASSERT_MAGIC_SETNONELEM;
2589 PERL_UNUSED_ARG(mg);
2590 sv_unmagic(sv, PERL_MAGIC_nonelem);
2591 return 0;
2592}
2593
2594int
864dbfa3 2595Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 2596{
7918f24d 2597 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
5648c0ae
DM
2598 Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2599 return 0;
810b8aa5
GS
2600}
2601
2602int
864dbfa3 2603Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2604{
7918f24d 2605 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
96a5add6 2606 PERL_UNUSED_CONTEXT;
0177730e 2607 PERL_UNUSED_ARG(sv);
565764a8 2608 mg->mg_len = -1;
93a17b20
LW
2609 return 0;
2610}
2611
2612int
864dbfa3 2613Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2614{
35a4481c 2615 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805 2616
7918f24d
NC
2617 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2618
79072805 2619 if (uf && uf->uf_set)
24f81a43 2620 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2621 return 0;
2622}
2623
c277df42 2624int
faf82a0b
AE
2625Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2626{
488344d2 2627 const char type = mg->mg_type;
7918f24d
NC
2628
2629 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2630
a5c7cb08
DM
2631 assert( type == PERL_MAGIC_fm
2632 || type == PERL_MAGIC_qr
2633 || type == PERL_MAGIC_bm);
488344d2 2634 return sv_unmagic(sv, type);
faf82a0b
AE
2635}
2636
7a4c00b4 2637#ifdef USE_LOCALE_COLLATE
79072805 2638int
864dbfa3 2639Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69 2640{
7918f24d
NC
2641 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2642
bbce6d69 2643 /*
838b5b74 2644 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2645 * and vanished with a faint plop.
2646 */
96a5add6 2647 PERL_UNUSED_CONTEXT;
8772537c 2648 PERL_UNUSED_ARG(sv);
7a4c00b4
PP
2649 if (mg->mg_ptr) {
2650 Safefree(mg->mg_ptr);
2651 mg->mg_ptr = NULL;
565764a8 2652 mg->mg_len = -1;
7a4c00b4 2653 }
bbce6d69
PP
2654 return 0;
2655}
7a4c00b4 2656#endif /* USE_LOCALE_COLLATE */
bbce6d69 2657
7e8c5dac
HS
2658/* Just clear the UTF-8 cache data. */
2659int
2660Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2661{
7918f24d 2662 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
96a5add6 2663 PERL_UNUSED_CONTEXT;
8772537c 2664 PERL_UNUSED_ARG(sv);
7e8c5dac 2665 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
3881461a 2666 mg->mg_ptr = NULL;
7e8c5dac
HS
2667 mg->mg_len = -1; /* The mg_len holds the len cache. */
2668 return 0;
2669}
2670
bbce6d69 2671int
9cce4f9a
FC
2672Perl_magic_setlvref(pTHX_ SV *sv, MAGIC *mg)
2673{
9782ce69 2674 const char *bad = NULL;
9cce4f9a 2675 PERL_ARGS_ASSERT_MAGIC_SETLVREF;
26a50d99 2676 if (!SvROK(sv)) Perl_croak(aTHX_ "Assigned value is not a reference");
9782ce69
FC
2677 switch (mg->mg_private & OPpLVREF_TYPE) {
2678 case OPpLVREF_SV:
2679 if (SvTYPE(SvRV(sv)) > SVt_PVLV)
2680 bad = " SCALAR";
2681 break;
2682 case OPpLVREF_AV:
2683 if (SvTYPE(SvRV(sv)) != SVt_PVAV)
2684 bad = "n ARRAY";
2685 break;
2686 case OPpLVREF_HV:
2687 if (SvTYPE(SvRV(sv)) != SVt_PVHV)
2688 bad = " HASH";
2689 break;
2690 case OPpLVREF_CV:
2691 if (SvTYPE(SvRV(sv)) != SVt_PVCV)
2692 bad = " CODE";
2693 }
2694 if (bad)
26a50d99 2695 /* diag_listed_as: Assigned value is not %s reference */
9782ce69 2696 Perl_croak(aTHX_ "Assigned value is not a%s reference", bad);
c146a62a
FC
2697 switch (mg->mg_obj ? SvTYPE(mg->mg_obj) : 0) {
2698 case 0:
2699 {
2700 SV * const old = PAD_SV(mg->mg_len);
2701 PAD_SETSV(mg->mg_len, SvREFCNT_inc_NN(SvRV(sv)));
2702 SvREFCNT_dec(old);
2703 break;
2704 }
2705 case SVt_PVGV:
2706 gv_setref(mg->mg_obj, sv);
2707 SvSETMAGIC(mg->mg_obj);
6102323a
FC
2708 break;
2709 case SVt_PVAV:
2710 av_store((AV *)mg->mg_obj, SvIV((SV *)mg->mg_ptr),
2711 SvREFCNT_inc_simple_NN(SvRV(sv)));
5f94141d
FC
2712 break;
2713 case SVt_PVHV:
7fcb36d5
JH
2714 (void)hv_store_ent((HV *)mg->mg_obj, (SV *)mg->mg_ptr,
2715 SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
c146a62a 2716 }
d39c26a6
FC
2717 if (mg->mg_flags & MGf_PERSIST)
2718 NOOP; /* This sv is in use as an iterator var and will be reused,
2719 so we must leave the magic. */
2720 else
2721 /* This sv could be returned by the assignment op, so clear the
2722 magic, as lvrefs are an implementation detail that must not be
2723 leaked to the user. */
2724 sv_unmagic(sv, PERL_MAGIC_lvref);
9cce4f9a
FC
2725 return 0;
2726}
2727
f1540bed
JH
2728static void
2729S_set_dollarzero(pTHX_ SV *sv)
2730 PERL_TSA_REQUIRES(PL_dollarzero_mutex)
2731{
2732#ifdef USE_ITHREADS
2733 dVAR;
2734#endif
2735 const char *s;
2736 STRLEN len;
f1540bed
JH
2737#ifdef HAS_SETPROCTITLE
2738 /* The BSDs don't show the argv[] in ps(1) output, they
2739 * show a string from the process struct and provide
2740 * the setproctitle() routine to manipulate that. */
2741 if (PL_origalen != 1) {
2742 s = SvPV_const(sv, len);
38626956 2743# if __FreeBSD_version > 410001 || defined(__DragonFly__)
f1540bed
JH
2744 /* The leading "-" removes the "perl: " prefix,
2745 * but not the "(perl) suffix from the ps(1)
2746 * output, because that's what ps(1) shows if the
2747 * argv[] is modified. */
2748 setproctitle("-%s", s);
2749# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2750 /* This doesn't really work if you assume that
2751 * $0 = 'foobar'; will wipe out 'perl' from the $0
2752 * because in ps(1) output the result will be like
2753 * sprintf("perl: %s (perl)", s)
2754 * I guess this is a security feature:
2755 * one (a user process) cannot get rid of the original name.
2756 * --jhi */
2757 setproctitle("%s", s);
2758# endif
2759 }
2760#elif defined(__hpux) && defined(PSTAT_SETCMD)
2761 if (PL_origalen != 1) {
2762 union pstun un;
2763 s = SvPV_const(sv, len);
2764 un.pst_command = (char *)s;
2765 pstat(PSTAT_SETCMD, un, len, 0, 0);
2766 }
2767#else
2768 if (PL_origalen > 1) {
4f3e04fd 2769 I32 i;
f1540bed
JH
2770 /* PL_origalen is set in perl_parse(). */
2771 s = SvPV_force(sv,len);
2772 if (len >= (STRLEN)PL_origalen-1) {
2773 /* Longer than original, will be truncated. We assume that
2774 * PL_origalen bytes are available. */
2775 Copy(s, PL_origargv[0], PL_origalen-1, char);
2776 }
2777 else {
2778 /* Shorter than original, will be padded. */
2779#ifdef PERL_DARWIN
2780 /* Special case for Mac OS X: see [perl #38868] */
2781 const int pad = 0;
2782#else
2783 /* Is the space counterintuitive? Yes.
2784 * (You were expecting \0?)
2785 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2786 * --jhi */
2787 const int pad = ' ';
2788#endif
2789 Copy(s, PL_origargv[0], len, char);
2790 PL_origargv[0][len] = 0;
2791 memset(PL_origargv[0] + len + 1,
2792 pad, PL_origalen - len - 1);
2793 }
2794 PL_origargv[0][PL_origalen-1] = 0;
2795 for (i = 1; i < PL_origargc; i++)
2796 PL_origargv[i] = 0;
2797#ifdef HAS_PRCTL_SET_NAME
2798 /* Set the legacy process name in addition to the POSIX name on Linux */
2799 if (prctl(PR_SET_NAME, (unsigned long)s, 0, 0, 0) != 0) {
2800 /* diag_listed_as: SKIPME */
2801 Perl_croak(aTHX_ "Can't set $0 with prctl(): %s", Strerror(errno));
2802 }
2803#endif
2804 }
2805#endif
2806}
2807
9cce4f9a 2808int
864dbfa3 2809Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2810{
20b7effb 2811#ifdef USE_ITHREADS
97aff369 2812 dVAR;
20b7effb 2813#endif
eb578fdb
KW
2814 I32 paren;
2815 const REGEXP * rx;
79072805 2816 I32 i;
8990e307 2817 STRLEN len;
125b9982 2818 MAGIC *tmg;
2fdbfb4d 2819
7918f24d
NC
2820 PERL_ARGS_ASSERT_MAGIC_SET;
2821
e91d8259 2822 if (!mg->mg_ptr) {
960b831f 2823 paren = mg->mg_len;
1e05feb3 2824 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
e91d8259 2825 setparen_got_rx:
2fdbfb4d 2826 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
1e05feb3 2827 } else {
2fdbfb4d
AB
2828 /* Croak with a READONLY error when a numbered match var is
2829 * set without a previous pattern match. Unless it's C<local $1>
2830 */
e91d8259 2831 croakparen:
2fdbfb4d 2832 if (!PL_localizing) {
cb077ed2 2833 Perl_croak_no_modify();
2fdbfb4d
AB
2834 }
2835 }
e91d8259
NC
2836 return 0;
2837 }
2838
2839 switch (*mg->mg_ptr) {
748a9306 2840 case '\001': /* ^A */
f2da823f
FC
2841 if (SvOK(sv)) sv_copypv(PL_bodytarget, sv);
2842 else SvOK_off(PL_bodytarget);
64eff8b7
DM
2843 FmLINES(PL_bodytarget) = 0;
2844 if (SvPOK(PL_bodytarget)) {
2845 char *s = SvPVX(PL_bodytarget);
0687887e
KW
2846 char *e = SvEND(PL_bodytarget);
2847 while ( ((s = (char *) memchr(s, '\n', e - s))) ) {
64eff8b7
DM
2848 FmLINES(PL_bodytarget)++;
2849 s++;
2850 }
2851 }
125b9982 2852 /* mg_set() has temporarily made sv non-magical */
284167a5 2853 if (TAINTING_get) {
125b9982
NT
2854 if ((tmg = mg_find(sv,PERL_MAGIC_taint)) && tmg->mg_len & 1)
2855 SvTAINTED_on(PL_bodytarget);
2856 else
2857 SvTAINTED_off(PL_bodytarget);
2858 }
748a9306 2859 break;
49460fe6 2860 case '\003': /* ^C */
f2338a2e 2861 PL_minus_c = cBOOL(SvIV(sv));
49460fe6
NIS
2862 break;
2863
79072805 2864 case '\004': /* ^D */
b4ab917c 2865#ifdef DEBUGGING
a9e2d2bf
DM
2866 {
2867 const char *s = SvPV_nolen_const(sv);
2868 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2869 if (DEBUG_x_TEST || DEBUG_B_TEST)
2870 dump_all_perl(!DEBUG_B_TEST);
2871 }
b4ab917c 2872#else
38ab35f8 2873 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
b4ab917c 2874#endif
79072805 2875 break;
28f23441 2876 case '\005': /* ^E */
d0063567 2877 if (*(mg->mg_ptr+1) == '\0') {
e37778c2 2878#ifdef VMS
38ab35f8 2879 set_vaxc_errno(SvIV(sv));
2abc681d 2880#elif defined(WIN32)
d0063567 2881 SetLastError( SvIV(sv) );
2abc681d 2882#elif defined(OS2)
38ab35f8 2883 os2_setsyserrno(SvIV(sv));
2abc681d 2884#else
d0063567 2885 /* will anyone ever use this? */
38ab35f8 2886 SETERRNO(SvIV(sv), 4);
22fae026 2887#endif
d0063567 2888 }
fd503f5c
DIM
2889 else if (strEQ(mg->mg_ptr + 1, "NCODING") && SvOK(sv))
2890 Perl_croak(aTHX_ "${^ENCODING} is no longer supported");
d0063567 2891 break;
79072805 2892 case '\006': /* ^F */
9f601cf3
TC
2893 if (mg->mg_ptr[1] == '\0') {
2894 PL_maxsysfd = SvIV(sv);
2895 }
2896 else if (strEQ(mg->mg_ptr + 1, "EATURE_BITS")) {
2897 PL_compiling.cop_features = SvUV(sv);
2898 }
79072805 2899 break;
a0d0e21e 2900 case '\010': /* ^H */
efa571ab
KW
2901 {
2902 U32 save_hints = PL_hints;
5d382c25 2903 PL_hints = SvUV(sv);
efa571ab
KW
2904
2905 /* If wasn't UTF-8, and now is, notify the parser */
2906 if ((PL_hints & HINT_UTF8) && ! (save_hints & HINT_UTF8)) {
2907 notify_parser_that_changed_to_utf8();
2908 }
2909 }
a0d0e21e 2910 break;
9d116dd7 2911 case '\011': /* ^I */ /* NOT \t in EBCDIC */
43c5f42d 2912 Safefree(PL_inplace);
bd61b366 2913 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
da78da6e 2914 break;
d9bad346
FC
2915 case '\016': /* ^N */
2916 if (PL_curpm && (rx = PM_GETRE(PL_curpm))
2917 && (paren = RX_LASTCLOSEPAREN(rx))) goto setparen_got_rx;
2918 goto croakparen;
28f23441 2919 case '\017': /* ^O */
ac27b0f5 2920 if (*(mg->mg_ptr+1) == '\0') {
43c5f42d 2921 Safefree(PL_osname);
bd61b366 2922 PL_osname = NULL;
3511154c
DM
2923 if (SvOK(sv)) {
2924 TAINT_PROPER("assigning to $^O");
2e0de35c 2925 PL_osname = savesvpv(sv);
3511154c 2926 }
ac27b0f5
NIS
2927 }
2928 else if (strEQ(mg->mg_ptr, "\017PEN")) {
8b850bd5
NC
2929 STRLEN len;
2930 const char *const start = SvPV(sv, len);
b54fc2b6 2931 const char *out = (const char*)memchr(start, '\0', len);
8b850bd5 2932 SV *tmp;
8b850bd5
NC
2933
2934
2935 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
f747ebd6 2936 PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
8b850bd5
NC
2937
2938 /* Opening for input is more common than opening for output, so
2939 ensure that hints for input are sooner on linked list. */
59cd0e26 2940 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
f747ebd6
Z
2941 SvUTF8(sv))
2942 : newSVpvs_flags("", SvUTF8(sv));
2943 (void)hv_stores(GvHV(PL_hintgv), "open>", tmp);
2944 mg_set(tmp);
8b850bd5 2945
f747ebd6
Z
2946 tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len,
2947 SvUTF8(sv));
2948 (void)hv_stores(GvHV(PL_hintgv), "open<", tmp);
2949 mg_set(tmp);
ac27b0f5 2950 }
28f23441 2951 break;
79072805 2952 case '\020': /* ^P */
2fdbfb4d
AB
2953 PL_perldb = SvIV(sv);
2954 if (PL_perldb && !PL_DBsingle)
2955 init_debugger();
9b9e0be4 2956 break;
79072805 2957 case '\024': /* ^T */
88e89b8a 2958#ifdef BIG_TIME
6b88bc9c 2959 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2960#else
38ab35f8 2961 PL_basetime = (Time_t)SvIV(sv);
88e89b8a 2962#endif
79072805 2963 break;
e07ea26a
NC
2964 case '\025': /* ^UTF8CACHE */
2965 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2966 PL_utf8cache = (signed char) sv_2iv(sv);
2967 }
2968 break;
fde18df1 2969 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2970 if (*(mg->mg_ptr+1) == '\0') {
2971 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
38ab35f8 2972 i = SvIV(sv);
ac27b0f5 2973 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2974 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2975 }
599cee73 2976 }
0a378802 2977 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2978 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
7e4f0450 2979 if (!SvPOK(sv)) {
1113f30d
DM
2980 if (!specialWARN(PL_compiling.cop_warnings))
2981 PerlMemShared_free(PL_compiling.cop_warnings);
7e4f0450 2982 PL_compiling.cop_warnings = pWARN_STD;
d775746e
GS
2983 break;
2984 }
f4fc7782 2985 {
b5477537 2986 STRLEN len, i;
006c1a1d
Z
2987 int not_none = 0, not_all = 0;
2988 const U8 * const ptr = (const U8 *)SvPV_const(sv, len) ;
f4fc7782 2989 for (i = 0 ; i < len ; ++i) {
006c1a1d
Z
2990 not_none |= ptr[i];
2991 not_all |= ptr[i] ^ 0x55;
f4fc7782 2992 }
006c1a1d 2993 if (!not_none) {
4243c432
NC
2994 if (!specialWARN(PL_compiling.cop_warnings))
2995 PerlMemShared_free(PL_compiling.cop_warnings);
2996 PL_compiling.cop_warnings = pWARN_NONE;
006c1a1d
Z
2997 } else if (len >= WARNsize && !not_all) {
2998 if (!specialWARN(PL_compiling.cop_warnings))
3c3f8cd6
AB
2999 PerlMemShared_free(PL_compiling.cop_warnings);
3000 PL_compiling.cop_warnings = pWARN_ALL;
3001 PL_dowarn |= G_WARN_ONCE ;
3002 }
d3a7d8c7 3003 else {
72dc9ed5
NC
3004 STRLEN len;
3005 const char *const p = SvPV_const(sv, len);
3006
3007 PL_compiling.cop_warnings
8ee4cf24 3008 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
72dc9ed5
NC
3009 p, len);
3010
d3a7d8c7
GS
3011 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
3012 PL_dowarn |= G_WARN_ONCE ;
3013 }
f4fc7782 3014
d3a7d8c7 3015 }
4438c4b7 3016 }
971a9dd3 3017 }
8ca2a5d6
DD
3018#ifdef WIN32
3019 else if (strEQ(mg->mg_ptr+1, "IN32_SLOPPY_STAT")) {
3020 w32_sloppystat = (bool)sv_true(sv);
3021 }
3022#endif
79072805
LW
3023 break;
3024 case '.':
3280af22
NIS
3025 if (PL_localizing) {
3026 if (PL_localizing == 1)
7766f137 3027 SAVESPTR(PL_last_in_gv);
748a9306 3028 }
3280af22 3029 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 3030 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
3031 break;
3032 case '^':
acbe1b9d 3033 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
a9e2d2bf 3034 IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
acbe1b9d 3035 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
3036 break;
3037 case '~':
acbe1b9d 3038 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
a9e2d2bf 3039 IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
acbe1b9d 3040 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
3041 break;
3042 case '=':
acbe1b9d 3043 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
3044 break;
3045 case '-':
acbe1b9d
FC
3046 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
3047 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
099be4f1 3048 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
3049 break;
3050 case '%':
acbe1b9d 3051 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
3052 break;
3053 case '|':
4b65379b 3054 {
099be4f1 3055 IO * const io = GvIO(PL_defoutgv);
720f287d
AB
3056 if(!io)
3057 break;
38ab35f8 3058 if ((SvIV(sv)) == 0)
4b65379b
CS
3059 IoFLAGS(io) &= ~IOf_FLUSH;
3060 else {
3061 if (!(IoFLAGS(io) & IOf_FLUSH)) {
3062 PerlIO *ofp = IoOFP(io);
3063 if (ofp)
3064 (void)PerlIO_flush(ofp);
3065 IoFLAGS(io) |= IOf_FLUSH;
3066 }
3067 }
79072805
LW
3068 }
3069 break;
79072805 3070 case '/':
1ab48e3a 3071 {
1ab48e3a 3072 if (SvROK(sv)) {
8d37cdf7
DIM
3073 SV *referent = SvRV(sv);
3074 const char *reftype = sv_reftype(referent, 0);
d239bea0
A
3075 /* XXX: dodgy type check: This leaves me feeling dirty, but
3076 * the alternative is to copy pretty much the entire
3077 * sv_reftype() into this routine, or to do a full string
3078 * comparison on the return of sv_reftype() both of which
3079 * make me feel worse! NOTE, do not modify this comment
3080 * without reviewing the corresponding comment in
3081 * sv_reftype(). - Yves */
1ab48e3a 3082 if (reftype[0] == 'S' || reftype[0] == 'L') {
8d37cdf7 3083 IV val = SvIV(referent);
1ab48e3a 3084 if (val <= 0) {
520b6fb6
DIM
3085 sv_setsv(sv, PL_rs);
3086 Perl_croak(aTHX_ "Setting $/ to a reference to %s is forbidden",
3087 val < 0 ? "a negative integer" : "zero");
1ab48e3a
YO
3088 }
3089 } else {
ddce084a 3090 sv_setsv(sv, PL_rs);
8d37cdf7 3091 /* diag_listed_as: Setting $/ to %s reference is forbidden */
a48e4205
FC
3092 Perl_croak(aTHX_ "Setting $/ to a%s %s reference is forbidden",
3093 *reftype == 'A' ? "n" : "", reftype);
1ab48e3a
YO
3094 }
3095 }
b3a2acfa 3096 SvREFCNT_dec(PL_rs);
520b6fb6 3097 PL_rs = newSVsv(sv);
b3a2acfa 3098 }
79072805
LW
3099 break;
3100 case '\\':
ef8d46e8 3101 SvREFCNT_dec(PL_ors_sv);
6bc2995b 3102 if (SvOK(sv)) {
7889fe52 3103 PL_ors_sv = newSVsv(sv);
009c130f 3104 }
e3c19b7b 3105 else {
a0714e2c 3106 PL_ors_sv = NULL;
e3c19b7b 3107 }
79072805 3108 break;
7d69d4a6
FC
3109 case '[':
3110 if (SvIV(sv) != 0)
3111 Perl_croak(aTHX_ "Assigning non-zero to $[ is no longer possible");
3112 break;
79072805 3113 case '?':
ff0cee69 3114#ifdef COMPLEX_STATUS
6b88bc9c 3115 if (PL_localizing == 2) {
41cb7b2b 3116 SvUPGRADE(sv, SVt_PVLV);
6b88bc9c
GS
3117 PL_statusvalue = LvTARGOFF(sv);
3118 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
3119 }
3120 else
3121#endif
3122#ifdef VMSISH_STATUS
3123 if (VMSISH_STATUS)
fb38d079 3124 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
ff0cee69
PP
3125 else
3126#endif
38ab35f8 3127 STATUS_UNIX_EXIT_SET(SvIV(sv));
79072805
LW
3128 break;
3129 case '!':
93189314
JH
3130 {
3131#ifdef VMS
3132# define PERL_VMS_BANG vaxc$errno
3133#else
3134# define PERL_VMS_BANG 0
3135#endif
79c32fc2 3136#if defined(WIN32)
c9beaf97
SH
3137 SETERRNO(win32_get_errno(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0),
3138 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
3139#else
91487cfc 3140 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314 3141 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
c9beaf97 3142#endif
93189314 3143 }
79072805
LW
3144 break;
3145 case '<':
985213f2 3146 {
b469f1e0 3147 /* XXX $< currently silently ignores failures */
dfff4baf 3148 const Uid_t new_uid = SvUID(sv);
985213f2 3149 PL_delaymagic_uid = new_uid;
3280af22
NIS
3150 if (PL_delaymagic) {
3151 PL_delaymagic |= DM_RUID;
79072805
LW
3152 break; /* don't do magic till later */
3153 }
3154#ifdef HAS_SETRUID
b469f1e0 3155 PERL_UNUSED_RESULT(setruid(new_uid));
2abc681d 3156#elif defined(HAS_SETREUID)
b469f1e0 3157 PERL_UNUSED_RESULT(setreuid(new_uid, (Uid_t)-1));
2abc681d 3158#elif defined(HAS_SETRESUID)
b469f1e0 3159 PERL_UNUSED_RESULT(setresuid(new_uid, (Uid_t)-1, (Uid_t)-1));
79072805 3160#else
985213f2 3161 if (new_uid == PerlProc_geteuid()) { /* special case $< = $> */
2abc681d 3162# ifdef PERL_DARWIN
75870ed3 3163 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
985213f2 3164 if (new_uid != 0 && PerlProc_getuid() == 0)
b469f1e0 3165 PERL_UNUSED_RESULT(PerlProc_setuid(0));
2abc681d 3166# endif
b469f1e0 3167 PERL_UNUSED_RESULT(PerlProc_setuid(new_uid));
75870ed3 3168 } else {
cea2e8a9 3169 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 3170 }
79072805 3171#endif
79072805 3172 break;
985213f2 3173 }
79072805 3174 case '>':
985213f2 3175 {
b469f1e0 3176 /* XXX $> currently silently ignores failures */
dfff4baf 3177 const Uid_t new_euid = SvUID(sv);
985213f2 3178 PL_delaymagic_euid = new_euid;
3280af22
NIS
3179 if (PL_delaymagic) {
3180 PL_delaymagic |= DM_EUID;
79072805
LW
3181 break; /* don't do magic till later */
3182 }
3183#ifdef HAS_SETEUID
b469f1e0 3184 PERL_UNUSED_RESULT(seteuid(new_euid));
2abc681d 3185#elif defined(HAS_SETREUID)
b469f1e0 3186 PERL_UNUSED_RESULT(setreuid((Uid_t)-1, new_euid));
2abc681d 3187#elif defined(HAS_SETRESUID)
b469f1e0 3188 PERL_UNUSED_RESULT(setresuid((Uid_t)-1, new_euid, (Uid_t)-1));
79072805 3189#else
985213f2 3190 if (new_euid == PerlProc_getuid()) /* special case $> = $< */
b469f1e0 3191 PERL_UNUSED_RESULT(PerlProc_setuid(new_euid));
a0d0e21e 3192 else {
cea2e8a9 3193 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 3194 }
79072805 3195#endif
79072805 3196 break;
985213f2 3197 }
79072805 3198 case '(':
985213f2 3199 {
b469f1e0 3200 /* XXX $( currently silently ignores failures */
dfff4baf 3201 const Gid_t new_gid = SvGID(sv);
985213f2 3202 PL_delaymagic_gid = new_gid;
3280af22
NIS
3203 if (PL_delaymagic) {
3204 PL_delaymagic |= DM_RGID;
79072805
LW
3205 break; /* don't do magic till later */
3206 }
3207#ifdef HAS_SETRGID
b469f1e0 3208 PERL_UNUSED_RESULT(setrgid(new_gid));
2abc681d 3209#elif defined(HAS_SETREGID)
b469f1e0 3210 PERL_UNUSED_RESULT(setregid(new_gid, (Gid_t)-1));
2abc681d 3211#elif defined(HAS_SETRESGID)
b469f1e0 3212 PERL_UNUSED_RESULT(setresgid(new_gid, (Gid_t)-1, (Gid_t) -1));
79072805 3213#else
985213f2 3214 if (new_gid == PerlProc_getegid()) /* special case $( = $) */
b469f1e0 3215 PERL_UNUSED_RESULT(PerlProc_setgid(new_gid));
748a9306 3216 else {
cea2e8a9 3217 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 3218 }
79072805 3219#endif
79072805 3220 break;
985213f2 3221 }
79072805 3222 case ')':
985213f2 3223 {
0635704a
HS
3224/* (hv) best guess: maybe we'll need configure probes to do a better job,
3225 * but you can override it if you need to.
3226 */
3227#ifndef INVALID_GID
3228#define INVALID_GID ((Gid_t)-1)
3229#endif
b469f1e0 3230 /* XXX $) currently silently ignores failures */
dfff4baf 3231 Gid_t new_egid;
5cd24f17
PP
3232#ifdef HAS_SETGROUPS
3233 {
b83604b4 3234 const char *p = SvPV_const(sv, len);
757f63d8 3235 Groups_t *gary = NULL;
79e302e6
MM
3236 const char* p_end = p + len;
3237 const char* endptr = p_end;
22ff3130 3238 UV uv;
fb4089e0 3239#ifdef _SC_NGROUPS_MAX
3240 int maxgrp = sysconf(_SC_NGROUPS_MAX);
3241
3242 if (maxgrp < 0)
3243 maxgrp = NGROUPS;
3244#else
3245 int maxgrp = NGROUPS;
3246#endif
757f63d8
SP
3247
3248 while (isSPACE(*p))
3249 ++p;
22ff3130
HS
3250 if (grok_atoUV(p, &uv, &endptr))
3251 new_egid = (Gid_t)uv;
3252 else {
0635704a 3253 new_egid = INVALID_GID;
22ff3130
HS
3254 endptr = NULL;
3255 }
fb4089e0 3256 for (i = 0; i < maxgrp; ++i) {
fdadaf77
JH
3257 if (endptr == NULL)
3258 break;
3259 p = endptr;
79e302e6 3260 endptr = p_end;
757f63d8
SP
3261 while (isSPACE(*p))
3262 ++p;
3263 if (!*p)
3264 break;
fdadaf77 3265 if (!gary)
757f63d8
SP
3266 Newx(gary, i + 1, Groups_t);
3267 else
3268 Renew(gary, i + 1, Groups_t);
22ff3130
HS
3269 if (grok_atoUV(p, &uv, &endptr))
3270 gary[i] = (Groups_t)uv;
3271 else {
0635704a 3272 gary[i] = INVALID_GID;
22ff3130
HS
3273 endptr = NULL;
3274 }
757f63d8
SP
3275 }
3276 if (i)
b469f1e0 3277 PERL_UNUSED_RESULT(setgroups(i, gary));
f5a63d97 3278 Safefree(gary);
5cd24f17
PP
3279 }
3280#else /* HAS_SETGROUPS */
dfff4baf 3281 new_egid = SvGID(sv);
5cd24f17 3282#endif /* HAS_SETGROUPS */
985213f2 3283 PL_delaymagic_egid = new_egid;
3280af22
NIS
3284 if (PL_delaymagic) {
3285 PL_delaymagic |= DM_EGID;
79072805
LW
3286 break; /* don't do magic till later */
3287 }
3288#ifdef HAS_SETEGID
b469f1e0 3289 PERL_UNUSED_RESULT(setegid(new_egid));
2abc681d 3290#elif defined(HAS_SETREGID)
b469f1e0 3291 PERL_UNUSED_RESULT(setregid((Gid_t)-1, new_egid));
2abc681d 3292#elif defined(HAS_SETRESGID)
b469f1e0 3293 PERL_UNUSED_RESULT(setresgid((Gid_t)-1, new_egid, (Gid_t)-1));
79072805 3294#else
985213f2 3295 if (new_egid == PerlProc_getgid()) /* special case $) = $( */
b469f1e0 3296 PERL_UNUSED_RESULT(PerlProc_setgid(new_egid));
748a9306 3297 else {
cea2e8a9 3298 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 3299 }
79072805 3300#endif
79072805 3301 break;
985213f2 3302 }
79072805 3303 case ':':
2d8e6c8d 3304 PL_chopset = SvPV_force(sv,len);
79072805 3305 break;
9cdac2a2
FC
3306 case '$': /* $$ */
3307 /* Store the pid in mg->mg_obj so we can tell when a fork has
3308 occurred. mg->mg_obj points to *$ by default, so clear it. */
3309 if (isGV(mg->mg_obj)) {
3310 if (mg->mg_flags & MGf_REFCOUNTED) /* probably never true */
3311 SvREFCNT_dec(mg->mg_obj);
3312 mg->mg_flags |= MGf_REFCOUNTED;
3313 mg->mg_obj = newSViv((IV)PerlProc_getpid());
3314 }
3315 else sv_setiv(mg->mg_obj, (IV)PerlProc_getpid());
3316 break;
79072805 3317 case '0':
e2975953 3318 LOCK_DOLLARZERO_MUTEX;
f1540bed 3319 S_set_dollarzero(aTHX_ sv);
e2975953 3320 UNLOCK_DOLLARZERO_MUTEX;
79072805
LW
3321 break;
3322 }
3323 return 0;
3324}
3325
3326I32
84c7b88c
BF
3327Perl_whichsig_sv(pTHX_ SV *sigsv)
3328{
3329 const char *sigpv;
3330 STRLEN siglen;
3331 PERL_ARGS_ASSERT_WHICHSIG_SV;
84c7b88c
BF
3332 sigpv = SvPV_const(sigsv, siglen);
3333 return whichsig_pvn(sigpv, siglen);
3334}
3335
3336I32
3337Perl_whichsig_pv(pTHX_ const char *sig)
3338{
3339 PERL_ARGS_ASSERT_WHICHSIG_PV;
84c7b88c
BF
3340 return whichsig_pvn(sig, strlen(sig));
3341}
3342
3343I32
3344Perl_whichsig_pvn(pTHX_ const char *sig, STRLEN len)
79072805 3345{
eb578fdb 3346 char* const* sigv;
7918f24d 3347
84c7b88c 3348 PERL_ARGS_ASSERT_WHICHSIG_PVN;
96a5add6 3349 PERL_UNUSED_CONTEXT;
79072805 3350
aadb217d 3351 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
84c7b88c 3352 if (strlen(*sigv) == len && memEQ(sig,*sigv, len))
aadb217d 3353 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805 3354#ifdef SIGCLD
84c7b88c 3355 if (memEQs(sig, len, "CHLD"))
79072805
LW
3356 return SIGCLD;
3357#endif
3358#ifdef SIGCHLD
84c7b88c 3359 if (memEQs(sig, len, "CLD"))
79072805
LW
3360 return SIGCHLD;
3361#endif
7f1236c0 3362 return -1;
79072805
LW
3363}
3364
dc37125b
DM
3365
3366/* Perl_sighandler(), Perl_sighandler1(), Perl_sighandler3():
3367 * these three function are intended to be called by the OS as 'C' level
3368 * signal handler functions in the case where unsafe signals are being
3369 * used - i.e. they immediately invoke Perl_perly_sighandler() to call the
3370 * perl-level sighandler, rather than deferring.
3371 * In fact, the core itself will normally use Perl_csighandler as the
3372 * OS-level handler; that function will then decide whether to queue the
3373 * signal or call Perl_sighandler / Perl_perly_sighandler itself. So these
3374 * functions are more useful for e.g. POSIX.xs when it wants explicit
3375 * control of what's happening.
3376 */
3377
3378
5e7940ce 3379#ifdef PERL_USE_3ARG_SIGHANDLER
8a5e470b
DM
3380
3381Signal_t
e7124897 3382Perl_sighandler(int sig, Siginfo_t *sip, void *uap)
8a5e470b
DM
3383{
3384 Perl_perly_sighandler(sig, sip, uap, 0);
3385}
3386
1e82f5a6 3387#else
8a5e470b
DM
3388
3389Signal_t
1e82f5a6 3390Perl_sighandler(int sig)
8a5e470b
DM
3391{
3392 Perl_perly_sighandler(sig, NULL, NULL, 0);
3393}
3394
1e82f5a6 3395#endif
8a5e470b 3396
dc37125b
DM
3397Signal_t
3398Perl_sighandler1(int sig)
3399{
3400 Perl_perly_sighandler(sig, NULL, NULL, 0);
3401}
3402
3403Signal_t
3404Perl_sighandler3(int sig, Siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
3405{
3406 Perl_perly_sighandler(sig, sip, uap, 0);
3407}
3408
3409
8a5e470b
DM
3410/* Invoke the perl-level signal handler. This function is called either
3411 * directly from one of the C-level signals handlers (Perl_sighandler or
3412 * Perl_csighandler), or for safe signals, later from
3413 * Perl_despatch_signals() at a suitable safe point during execution.
3414 *
3415 * 'safe' is a boolean indicating the latter call path.
3416 */
3417
3418Signal_t
3419Perl_perly_sighandler(int sig, Siginfo_t *sip PERL_UNUSED_DECL,
3420 void *uap PERL_UNUSED_DECL, bool safe)
79072805 3421{
1018e26f
NIS
3422#ifdef PERL_GET_SIG_CONTEXT
3423 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 3424#else
cea2e8a9 3425 dTHX;
71d280e3 3426#endif
79072805 3427 dSP;
a0714e2c
SS
3428 GV *gv = NULL;
3429 SV *sv = NULL;
8772537c 3430 SV * const tSv = PL_Sv;
601f1833 3431 CV *cv = NULL;
533c011a 3432 OP *myop = PL_op;
84902520 3433 U32 flags = 0;
8772537c 3434 XPV * const tXpv = PL_Xpv;
0c4d3b5e 3435 I32 old_ss_ix = PL_savestack_ix;
100c03aa 3436 SV *errsv_save = NULL;
71d280e3 3437
84902520 3438
727405f8 3439 if (!PL_psig_ptr[sig]) {
99ef548b 3440 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
3441 PL_sig_name[sig]);
3442 exit(sig);
3443 }
ff0cee69 3444
a0d63a7b
DM
3445 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
3446 /* Max number of items pushed there is 3*n or 4. We cannot fix
3447 infinity, so we fix 4 (in fact 5): */
3448 if (PL_savestack_ix + 15 <= PL_savestack_max) {
3449 flags |= 1;
3450 PL_savestack_ix += 5; /* Protect save in progress. */
3451 SAVEDESTRUCTOR_X(S_unwind_handler_stack, NULL);
3452 }
84902520 3453 }
84902520 3454 /* sv_2cv is too complicated, try a simpler variant first: */
ea726b52 3455 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
8772537c
AL
3456 || SvTYPE(cv) != SVt_PVCV) {
3457 HV *st;
f2c0649b 3458 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
8772537c 3459 }
84902520 3460
a0d0e21e 3461 if (!cv || !CvROOT(cv)) {
07c325bb 3462 const HEK * const hek = gv
1d6bb9ae
FC
3463 ? GvENAME_HEK(gv)
3464 : cv && CvNAMED(cv)
3465 ? CvNAME_HEK(cv)
07c325bb
FC
3466 : cv && CvGV(cv) ? GvENAME_HEK(CvGV(cv)) : NULL;
3467 if (hek)
3468 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL),
147e3846 3469 "SIG%s handler \"%" HEKf "\" not defined.\n",
ac892e4a 3470 PL_sig_name[sig], HEKfARG(hek));
07c325bb
FC
3471 /* diag_listed_as: SIG%s handler "%s" not defined */
3472 else Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL),
3473 "SIG%s handler \"__ANON__\" not defined.\n",
3474 PL_sig_name[sig]);
00d579c5 3475 goto cleanup;
79072805
LW
3476 }
3477
0c4d3b5e
DM
3478 sv = PL_psig_