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