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