This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #116925] discuss modifying an SV's buffer
[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");
81d52ecd 351
93965878 352 }
a25b5927 353 NOT_REACHED; /* NOTREACHED */
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
ffb6256b
JH
1366#if defined(__cplusplus) && defined(__GNUC__)
1367 /* g++ doesn't support PERL_UNUSED_DECL, so the sip and uap
1368 * parameters would be warned about. */
1369 PERL_UNUSED_ARG(sip);
1370 PERL_UNUSED_ARG(uap);
1371#endif
23ada85b 1372#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1373 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1374 if (PL_sig_ignoring[sig]) return;
85b332e2 1375#endif
2e34cc90 1376#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1377 if (PL_sig_defaulting[sig])
2e34cc90
CL
1378#ifdef KILL_BY_SIGPRC
1379 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1380#else
1381 exit(1);
1382#endif
1383#endif
406878dd 1384 if (
853d2c32
RGS
1385#ifdef SIGILL
1386 sig == SIGILL ||
1387#endif
1388#ifdef SIGBUS
1389 sig == SIGBUS ||
1390#endif
1391#ifdef SIGSEGV
1392 sig == SIGSEGV ||
1393#endif
1394 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
4ffa73a3 1395 /* Call the perl level handler now--
31c91b43 1396 * with risk we may be in malloc() or being destructed etc. */
6e324d09 1397#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
80626cf1 1398 (*PL_sighandlerp)(sig, NULL, NULL);
6e324d09
CB
1399#else
1400 (*PL_sighandlerp)(sig);
92807b6d 1401#endif
406878dd 1402 else {
31c91b43 1403 if (!PL_psig_pend) return;
406878dd
GA
1404 /* Set a flag to say this signal is pending, that is awaiting delivery after
1405 * the current Perl opcode completes */
1406 PL_psig_pend[sig]++;
1407
1408#ifndef SIG_PENDING_DIE_COUNT
1409# define SIG_PENDING_DIE_COUNT 120
1410#endif
fe13d51d 1411 /* Add one to say _a_ signal is pending */
406878dd
GA
1412 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1413 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1414 (unsigned long)SIG_PENDING_DIE_COUNT);
1415 }
0a8e0eff
NIS
1416}
1417
2e34cc90
CL
1418#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1419void
1420Perl_csighandler_init(void)
1421{
1422 int sig;
27da23d5 1423 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1424
1425 for (sig = 1; sig < SIG_SIZE; sig++) {
1426#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1427 dTHX;
27da23d5 1428 PL_sig_defaulting[sig] = 1;
5c1546dc 1429 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1430#endif
1431#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1432 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1433#endif
1434 }
27da23d5 1435 PL_sig_handlers_initted = 1;
2e34cc90
CL
1436}
1437#endif
1438
7fe50b8b
LT
1439#if defined HAS_SIGPROCMASK
1440static void
1441unblock_sigmask(pTHX_ void* newset)
1442{
dc3bf405 1443 PERL_UNUSED_CONTEXT;
7fe50b8b
LT
1444 sigprocmask(SIG_UNBLOCK, (sigset_t*)newset, NULL);
1445}
1446#endif
1447
0a8e0eff
NIS
1448void
1449Perl_despatch_signals(pTHX)
1450{
97aff369 1451 dVAR;
0a8e0eff
NIS
1452 int sig;
1453 PL_sig_pending = 0;
1454 for (sig = 1; sig < SIG_SIZE; sig++) {
1455 if (PL_psig_pend[sig]) {
d0166017 1456 dSAVE_ERRNO;
7fe50b8b 1457#ifdef HAS_SIGPROCMASK
55534442
LT
1458 /* From sigaction(2) (FreeBSD man page):
1459 * | Signal routines normally execute with the signal that
1460 * | caused their invocation blocked, but other signals may
1461 * | yet occur.
1462 * Emulation of this behavior (from within Perl) is enabled
1463 * using sigprocmask
1464 */
1465 int was_blocked;
1466 sigset_t newset, oldset;
1467
1468 sigemptyset(&newset);
1469 sigaddset(&newset, sig);
1470 sigprocmask(SIG_BLOCK, &newset, &oldset);
1471 was_blocked = sigismember(&oldset, sig);
7fe50b8b
LT
1472 if (!was_blocked) {
1473 SV* save_sv = newSVpvn((char *)(&newset), sizeof(sigset_t));
1474 ENTER;
1475 SAVEFREESV(save_sv);
1476 SAVEDESTRUCTOR_X(unblock_sigmask, SvPV_nolen(save_sv));
1477 }
55534442 1478#endif
25da4428 1479 PL_psig_pend[sig] = 0;
6e324d09 1480#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
80626cf1 1481 (*PL_sighandlerp)(sig, NULL, NULL);
6e324d09
CB
1482#else
1483 (*PL_sighandlerp)(sig);
92807b6d 1484#endif
7fe50b8b 1485#ifdef HAS_SIGPROCMASK
55534442 1486 if (!was_blocked)
7fe50b8b 1487 LEAVE;
55534442 1488#endif
d0166017 1489 RESTORE_ERRNO;
0a8e0eff
NIS
1490 }
1491 }
1492}
1493
38a124f0 1494/* sv of NULL signifies that we're acting as magic_clearsig. */
85e6fe83 1495int
864dbfa3 1496Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1497{
27da23d5 1498 dVAR;
79072805 1499 I32 i;
cbbf8932 1500 SV** svp = NULL;
2d4fcd5e
AJ
1501 /* Need to be careful with SvREFCNT_dec(), because that can have side
1502 * effects (due to closures). We must make sure that the new disposition
1503 * is in place before it is called.
1504 */
cbbf8932 1505 SV* to_dec = NULL;
e72dc28c 1506 STRLEN len;
2d4fcd5e
AJ
1507#ifdef HAS_SIGPROCMASK
1508 sigset_t set, save;
1509 SV* save_sv;
1510#endif
eb578fdb 1511 const char *s = MgPV_const(mg,len);
7918f24d
NC
1512
1513 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1514
748a9306 1515 if (*s == '_') {
84c7b88c 1516 if (memEQs(s, len, "__DIE__"))
3280af22 1517 svp = &PL_diehook;
84c7b88c 1518 else if (memEQs(s, len, "__WARN__")
38a124f0
NC
1519 && (sv ? 1 : PL_warnhook != PERL_WARNHOOK_FATAL)) {
1520 /* Merge the existing behaviours, which are as follows:
1521 magic_setsig, we always set svp to &PL_warnhook
1522 (hence we always change the warnings handler)
1523 For magic_clearsig, we don't change the warnings handler if it's
1524 set to the &PL_warnhook. */
3280af22 1525 svp = &PL_warnhook;
84c7b88c
BF
1526 } else if (sv) {
1527 SV *tmp = sv_newmortal();
1528 Perl_croak(aTHX_ "No such hook: %s",
1529 pv_pretty(tmp, s, len, 0, NULL, NULL, 0));
1530 }
748a9306 1531 i = 0;
38a124f0 1532 if (svp && *svp) {
9289f461
RGS
1533 if (*svp != PERL_WARNHOOK_FATAL)
1534 to_dec = *svp;
cbbf8932 1535 *svp = NULL;
4633a7c4 1536 }
748a9306
LW
1537 }
1538 else {
708854f2
NC
1539 i = (I16)mg->mg_private;
1540 if (!i) {
84c7b88c 1541 i = whichsig_pvn(s, len); /* ...no, a brick */
58a26b12 1542 mg->mg_private = (U16)i;
708854f2 1543 }
86d86cad 1544 if (i <= 0) {
84c7b88c
BF
1545 if (sv) {
1546 SV *tmp = sv_newmortal();
1547 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s",
1548 pv_pretty(tmp, s, len, 0, NULL, NULL, 0));
1549 }
748a9306
LW
1550 return 0;
1551 }
2d4fcd5e
AJ
1552#ifdef HAS_SIGPROCMASK
1553 /* Avoid having the signal arrive at a bad time, if possible. */
1554 sigemptyset(&set);
1555 sigaddset(&set,i);
1556 sigprocmask(SIG_BLOCK, &set, &save);
1557 ENTER;
9ff8e806 1558 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
2d4fcd5e
AJ
1559 SAVEFREESV(save_sv);
1560 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1561#endif
1562 PERL_ASYNC_CHECK();
2e34cc90 1563#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1564 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1565#endif
23ada85b 1566#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1567 PL_sig_ignoring[i] = 0;
85b332e2 1568#endif
2e34cc90 1569#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1570 PL_sig_defaulting[i] = 0;
2e34cc90 1571#endif
2d4fcd5e 1572 to_dec = PL_psig_ptr[i];
38a124f0
NC
1573 if (sv) {
1574 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1575 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
79fd3822
NC
1576
1577 /* Signals don't change name during the program's execution, so once
1578 they're cached in the appropriate slot of PL_psig_name, they can
1579 stay there.
1580
1581 Ideally we'd find some way of making SVs at (C) compile time, or
1582 at least, doing most of the work. */
1583 if (!PL_psig_name[i]) {
1584 PL_psig_name[i] = newSVpvn(s, len);
1585 SvREADONLY_on(PL_psig_name[i]);
1586 }
38a124f0 1587 } else {
79fd3822 1588 SvREFCNT_dec(PL_psig_name[i]);
38a124f0
NC
1589 PL_psig_name[i] = NULL;
1590 PL_psig_ptr[i] = NULL;
1591 }
748a9306 1592 }
38a124f0 1593 if (sv && (isGV_with_GP(sv) || SvROK(sv))) {
2d4fcd5e 1594 if (i) {
5c1546dc 1595 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e 1596 }
748a9306 1597 else
b37c2d43 1598 *svp = SvREFCNT_inc_simple_NN(sv);
38a124f0 1599 } else {
9dfa190b
NC
1600 if (sv && SvOK(sv)) {
1601 s = SvPV_force(sv, len);
1602 } else {
1603 sv = NULL;
1604 }
84c7b88c 1605 if (sv && memEQs(s, len,"IGNORE")) {
9dfa190b 1606 if (i) {
23ada85b 1607#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
9dfa190b
NC
1608 PL_sig_ignoring[i] = 1;
1609 (void)rsignal(i, PL_csighandlerp);
85b332e2 1610#else
9dfa190b 1611 (void)rsignal(i, (Sighandler_t) SIG_IGN);
85b332e2 1612#endif
9dfa190b 1613 }
2d4fcd5e 1614 }
84c7b88c 1615 else if (!sv || memEQs(s, len,"DEFAULT") || !len) {
9dfa190b 1616 if (i) {
2e34cc90 1617#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
9dfa190b
NC
1618 PL_sig_defaulting[i] = 1;
1619 (void)rsignal(i, PL_csighandlerp);
2e34cc90 1620#else
9dfa190b 1621 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2e34cc90 1622#endif
9dfa190b
NC
1623 }
1624 }
1625 else {
1626 /*
1627 * We should warn if HINT_STRICT_REFS, but without
1628 * access to a known hint bit in a known OP, we can't
1629 * tell whether HINT_STRICT_REFS is in force or not.
1630 */
1631 if (!strchr(s,':') && !strchr(s,'\''))
1632 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1633 SV_GMAGIC);
1634 if (i)
1635 (void)rsignal(i, PL_csighandlerp);
1636 else
1637 *svp = SvREFCNT_inc_simple_NN(sv);
136e0459 1638 }
748a9306 1639 }
9dfa190b 1640
2d4fcd5e
AJ
1641#ifdef HAS_SIGPROCMASK
1642 if(i)
1643 LEAVE;
1644#endif
ef8d46e8 1645 SvREFCNT_dec(to_dec);
79072805
LW
1646 return 0;
1647}
64ca3a65 1648#endif /* !PERL_MICRO */
79072805
LW
1649
1650int
864dbfa3 1651Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1652{
97aff369 1653 dVAR;
7918f24d 1654 PERL_ARGS_ASSERT_MAGIC_SETISA;
8772537c 1655 PERL_UNUSED_ARG(sv);
e1a479c5 1656
89c14e2e 1657 /* Skip _isaelem because _isa will handle it shortly */
354b0578 1658 if (PL_delaymagic & DM_ARRAY_ISA && mg->mg_type == PERL_MAGIC_isaelem)
89c14e2e
BB
1659 return 0;
1660
0e446081 1661 return magic_clearisa(NULL, mg);
463ee0b2
LW
1662}
1663
0e446081 1664/* sv of NULL signifies that we're acting as magic_setisa. */
463ee0b2 1665int
52b45067
RD
1666Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1667{
1668 dVAR;
1669 HV* stash;
1670
7918f24d
NC
1671 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1672
52b45067 1673 /* Bail out if destruction is going on */
627364f1 1674 if(PL_phase == PERL_PHASE_DESTRUCT) return 0;
52b45067 1675
0e446081
NC
1676 if (sv)
1677 av_clear(MUTABLE_AV(sv));
52b45067 1678
6624142a
FC
1679 if (SvTYPE(mg->mg_obj) != SVt_PVGV && SvSMAGICAL(mg->mg_obj))
1680 /* This occurs with setisa_elem magic, which calls this
1681 same function. */
1682 mg = mg_find(mg->mg_obj, PERL_MAGIC_isa);
1683
316ebaf2 1684 assert(mg);
6624142a
FC
1685 if (SvTYPE(mg->mg_obj) == SVt_PVAV) { /* multiple stashes */
1686 SV **svp = AvARRAY((AV *)mg->mg_obj);
1687 I32 items = AvFILLp((AV *)mg->mg_obj) + 1;
1688 while (items--) {
1689 stash = GvSTASH((GV *)*svp++);
1690 if (stash && HvENAME(stash)) mro_isa_changed_in(stash);
1691 }
1692
1693 return 0;
1694 }
1695
52b45067 1696 stash = GvSTASH(
6624142a 1697 (const GV *)mg->mg_obj
52b45067
RD
1698 );
1699
00169e2c
FC
1700 /* The stash may have been detached from the symbol table, so check its
1701 name before doing anything. */
1702 if (stash && HvENAME_get(stash))
5562fa71 1703 mro_isa_changed_in(stash);
52b45067
RD
1704
1705 return 0;
1706}
1707
1708int
864dbfa3 1709Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1710{
85fbaab2 1711 HV * const hv = MUTABLE_HV(LvTARG(sv));
6ff81951 1712 I32 i = 0;
7918f24d
NC
1713
1714 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
8772537c 1715 PERL_UNUSED_ARG(mg);
7719e241 1716
6ff81951 1717 if (hv) {
497b47a8 1718 (void) hv_iterinit(hv);
ad64d0ec 1719 if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
1b95d04f 1720 i = HvUSEDKEYS(hv);
497b47a8
JH
1721 else {
1722 while (hv_iternext(hv))
1723 i++;
1724 }
6ff81951
GS
1725 }
1726
1727 sv_setiv(sv, (IV)i);
1728 return 0;
1729}
1730
1731int
864dbfa3 1732Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1733{
7918f24d 1734 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
8772537c 1735 PERL_UNUSED_ARG(mg);
946ec16e 1736 if (LvTARG(sv)) {
85fbaab2 1737 hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv));
946ec16e
PP
1738 }
1739 return 0;
ac27b0f5 1740}
946ec16e 1741
efaf3674
DM
1742/*
1743=for apidoc magic_methcall
1744
1745Invoke a magic method (like FETCH).
1746
2baee27b
FC
1747C<sv> and C<mg> are the tied thingy and the tie magic.
1748
1749C<meth> is the name of the method to call.
1750
1751C<argc> is the number of args (in addition to $self) to pass to the method.
1752
1753The C<flags> can be:
1754
1755 G_DISCARD invoke method with G_DISCARD flag and don't
1756 return a value
1757 G_UNDEF_FILL fill the stack with argc pointers to
1758 PL_sv_undef
1759
1760The arguments themselves are any values following the C<flags> argument.
efaf3674
DM
1761
1762Returns the SV (if any) returned by the method, or NULL on failure.
1763
1764
1765=cut
1766*/
1767
1768SV*
36925d9e 1769Perl_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, SV *meth, U32 flags,
046b0c7d 1770 U32 argc, ...)
a0d0e21e 1771{
97aff369 1772 dVAR;
a0d0e21e 1773 dSP;
efaf3674 1774 SV* ret = NULL;
463ee0b2 1775
7918f24d
NC
1776 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1777
efaf3674 1778 ENTER;
d1d7a15d
NC
1779
1780 if (flags & G_WRITING_TO_STDERR) {
1781 SAVETMPS;
1782
1783 save_re_context();
1784 SAVESPTR(PL_stderrgv);
1785 PL_stderrgv = NULL;
1786 }
1787
efaf3674 1788 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1789 PUSHMARK(SP);
efaf3674 1790
67549bd2
NC
1791 EXTEND(SP, argc+1);
1792 PUSHs(SvTIED_obj(sv, mg));
1793 if (flags & G_UNDEF_FILL) {
1794 while (argc--) {
efaf3674 1795 PUSHs(&PL_sv_undef);
93965878 1796 }
67549bd2 1797 } else if (argc > 0) {
046b0c7d
NC
1798 va_list args;
1799 va_start(args, argc);
1800
1801 do {
1802 SV *const sv = va_arg(args, SV *);
1803 PUSHs(sv);
1804 } while (--argc);
1805
1806 va_end(args);
88e89b8a 1807 }
463ee0b2 1808 PUTBACK;
efaf3674 1809 if (flags & G_DISCARD) {
36925d9e 1810 call_sv(meth, G_SCALAR|G_DISCARD|G_METHOD_NAMED);
efaf3674
DM
1811 }
1812 else {
36925d9e 1813 if (call_sv(meth, G_SCALAR|G_METHOD_NAMED))
efaf3674
DM
1814 ret = *PL_stack_sp--;
1815 }
1816 POPSTACK;
d1d7a15d
NC
1817 if (flags & G_WRITING_TO_STDERR)
1818 FREETMPS;
efaf3674
DM
1819 LEAVE;
1820 return ret;
1821}
1822
efaf3674 1823/* wrapper for magic_methcall that creates the first arg */
463ee0b2 1824
efaf3674 1825STATIC SV*
36925d9e 1826S_magic_methcall1(pTHX_ SV *sv, const MAGIC *mg, SV *meth, U32 flags,
efaf3674
DM
1827 int n, SV *val)
1828{
1829 dVAR;
1830 SV* arg1 = NULL;
1831
1832 PERL_ARGS_ASSERT_MAGIC_METHCALL1;
1833
1834 if (mg->mg_ptr) {
1835 if (mg->mg_len >= 0) {
db4b3a1d 1836 arg1 = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
efaf3674
DM
1837 }
1838 else if (mg->mg_len == HEf_SVKEY)
1839 arg1 = MUTABLE_SV(mg->mg_ptr);
1840 }
1841 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
db4b3a1d 1842 arg1 = newSViv((IV)(mg->mg_len));
efaf3674
DM
1843 sv_2mortal(arg1);
1844 }
1845 if (!arg1) {
046b0c7d 1846 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n - 1, val);
efaf3674 1847 }
046b0c7d 1848 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n, arg1, val);
946ec16e
PP
1849}
1850
76e3520e 1851STATIC int
36925d9e 1852S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, SV *meth)
a0d0e21e 1853{
efaf3674
DM
1854 dVAR;
1855 SV* ret;
463ee0b2 1856
7918f24d
NC
1857 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1858
efaf3674
DM
1859 ret = magic_methcall1(sv, mg, meth, 0, 1, NULL);
1860 if (ret)
1861 sv_setsv(sv, ret);
a0d0e21e
LW
1862 return 0;
1863}
463ee0b2 1864
a0d0e21e 1865int
864dbfa3 1866Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1867{
7918f24d
NC
1868 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1869
fd69380d 1870 if (mg->mg_type == PERL_MAGIC_tiedelem)
a0d0e21e 1871 mg->mg_flags |= MGf_GSKIP;
36925d9e 1872 magic_methpack(sv,mg,SV_CONST(FETCH));
463ee0b2
LW
1873 return 0;
1874}
1875
1876int
864dbfa3 1877Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1878{
efaf3674 1879 dVAR;
b112cff9
DM
1880 MAGIC *tmg;
1881 SV *val;
7918f24d
NC
1882
1883 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1884
b112cff9
DM
1885 /* in the code C<$tied{foo} = $val>, the "thing" that gets passed to
1886 * STORE() is not $val, but rather a PVLV (the sv in this call), whose
1887 * public flags indicate its value based on copying from $val. Doing
1888 * mg_set() on the PVLV temporarily does SvMAGICAL_off(), then calls us.
1889 * So STORE()'s $_[2] arg is a temporarily disarmed PVLV. This goes
1890 * wrong if $val happened to be tainted, as sv hasn't got magic
1891 * enabled, even though taint magic is in the chain. In which case,
1892 * fake up a temporary tainted value (this is easier than temporarily
1893 * re-enabling magic on sv). */
1894
284167a5 1895 if (TAINTING_get && (tmg = mg_find(sv, PERL_MAGIC_taint))
b112cff9
DM
1896 && (tmg->mg_len & 1))
1897 {
1898 val = sv_mortalcopy(sv);
1899 SvTAINTED_on(val);
1900 }
1901 else
1902 val = sv;
1903
36925d9e 1904 magic_methcall1(sv, mg, SV_CONST(STORE), G_DISCARD, 2, val);
463ee0b2
LW
1905 return 0;
1906}
1907
1908int
864dbfa3 1909Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1910{
7918f24d
NC
1911 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1912
4c13be3f 1913 if (mg->mg_type == PERL_MAGIC_tiedscalar) return 0;
36925d9e 1914 return magic_methpack(sv,mg,SV_CONST(DELETE));
a0d0e21e 1915}
463ee0b2 1916
93965878
NIS
1917
1918U32
864dbfa3 1919Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1920{
efaf3674 1921 dVAR;
22846ab4 1922 I32 retval = 0;
efaf3674 1923 SV* retsv;
93965878 1924
7918f24d
NC
1925 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1926
36925d9e 1927 retsv = magic_methcall1(sv, mg, SV_CONST(FETCHSIZE), 0, 1, NULL);
efaf3674
DM
1928 if (retsv) {
1929 retval = SvIV(retsv)-1;
22846ab4
AB
1930 if (retval < -1)
1931 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
93965878 1932 }
22846ab4 1933 return (U32) retval;
93965878
NIS
1934}
1935
cea2e8a9
GS
1936int
1937Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1938{
efaf3674 1939 dVAR;
463ee0b2 1940
7918f24d
NC
1941 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1942
36925d9e 1943 Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(CLEAR), G_DISCARD, 0);
463ee0b2
LW
1944 return 0;
1945}
1946
1947int
864dbfa3 1948Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1949{
efaf3674
DM
1950 dVAR;
1951 SV* ret;
463ee0b2 1952
7918f24d
NC
1953 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1954
36925d9e
RZ
1955 ret = SvOK(key) ? Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(NEXTKEY), 0, 1, key)
1956 : Perl_magic_methcall(aTHX_ sv, mg, SV_CONST(FIRSTKEY), 0, 0);
efaf3674
DM
1957 if (ret)
1958 sv_setsv(key,ret);
79072805
LW
1959 return 0;
1960}
1961
1962int
1146e912 1963Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
a0d0e21e 1964{
7918f24d
NC
1965 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1966
36925d9e 1967 return magic_methpack(sv,mg,SV_CONST(EXISTS));
ac27b0f5 1968}
a0d0e21e 1969
a3bcc51e
TP
1970SV *
1971Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1972{
efaf3674 1973 dVAR;
5fcbf73d 1974 SV *retval;
ad64d0ec
NC
1975 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
1976 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
a3bcc51e 1977
7918f24d
NC
1978 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1979
a3bcc51e
TP
1980 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1981 SV *key;
bfcb3514 1982 if (HvEITER_get(hv))
a3bcc51e
TP
1983 /* we are in an iteration so the hash cannot be empty */
1984 return &PL_sv_yes;
1985 /* no xhv_eiter so now use FIRSTKEY */
1986 key = sv_newmortal();
ad64d0ec 1987 magic_nextpack(MUTABLE_SV(hv), mg, key);
bfcb3514 1988 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1989 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1990 }
1991
1992 /* there is a SCALAR method that we can call */
36925d9e 1993 retval = Perl_magic_methcall(aTHX_ MUTABLE_SV(hv), mg, SV_CONST(SCALAR), 0, 0);
efaf3674 1994 if (!retval)
5fcbf73d 1995 retval = &PL_sv_undef;
a3bcc51e
TP
1996 return retval;
1997}
1998
a0d0e21e 1999int
864dbfa3 2000Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 2001{
97aff369 2002 dVAR;
089b5c60 2003 SV **svp;
7918f24d
NC
2004
2005 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
2006
089b5c60
NC
2007 /* The magic ptr/len for the debugger's hash should always be an SV. */
2008 if (UNLIKELY(mg->mg_len != HEf_SVKEY)) {
2009 Perl_croak(aTHX_ "panic: magic_setdbline len=%"IVdf", ptr='%s'",
7b0eb0b8 2010 (IV)mg->mg_len, mg->mg_ptr);
089b5c60
NC
2011 }
2012
2013 /* Use sv_2iv instead of SvIV() as the former generates smaller code, and
2014 setting/clearing debugger breakpoints is not a hot path. */
43e4250a
FC
2015 svp = av_fetch(MUTABLE_AV(mg->mg_obj),
2016 sv_2iv(MUTABLE_SV((mg)->mg_ptr)), FALSE);
089b5c60 2017
8772537c 2018 if (svp && SvIOKp(*svp)) {
88df5f01
FC
2019 OP * const o = INT2PTR(OP*,SvIVX(*svp));
2020 if (o) {
2021#ifdef PERL_DEBUG_READONLY_OPS
2022 Slab_to_rw(OpSLAB(o));
2023#endif
2024 /* set or clear breakpoint in the relevant control op */
089b5c60 2025 if (SvTRUE(sv))
88df5f01 2026 o->op_flags |= OPf_SPECIAL;
8772537c 2027 else
88df5f01
FC
2028 o->op_flags &= ~OPf_SPECIAL;
2029#ifdef PERL_DEBUG_READONLY_OPS
2030 Slab_to_ro(OpSLAB(o));
2031#endif
8772537c 2032 }
5df8de69 2033 }
79072805
LW
2034 return 0;
2035}
2036
2037int
8772537c 2038Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
79072805 2039{
97aff369 2040 dVAR;
502c6561 2041 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
2042
2043 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
2044
83bf042f 2045 if (obj) {
e1dccc0d 2046 sv_setiv(sv, AvFILL(obj));
83bf042f 2047 } else {
d4a823b3 2048 sv_setsv(sv, NULL);
83bf042f 2049 }
79072805
LW
2050 return 0;
2051}
2052
2053int
864dbfa3 2054Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 2055{
97aff369 2056 dVAR;
502c6561 2057 AV * const obj = MUTABLE_AV(mg->mg_obj);
7918f24d
NC
2058
2059 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
2060
83bf042f 2061 if (obj) {
e1dccc0d 2062 av_fill(obj, SvIV(sv));
83bf042f 2063 } else {
a2a5de95
NC
2064 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
2065 "Attempt to set length of freed array");
83bf042f
NC
2066 }
2067 return 0;
2068}
2069
2070int
83f29afa
VP
2071Perl_magic_cleararylen_p(pTHX_ SV *sv, MAGIC *mg)
2072{
2073 dVAR;
2074
2075 PERL_ARGS_ASSERT_MAGIC_CLEARARYLEN_P;
2076 PERL_UNUSED_ARG(sv);
dc3bf405 2077 PERL_UNUSED_CONTEXT;
83f29afa
VP
2078
2079 /* Reset the iterator when the array is cleared */
3565fbf3
VP
2080#if IVSIZE == I32SIZE
2081 *((IV *) &(mg->mg_len)) = 0;
2082#else
83f29afa
VP
2083 if (mg->mg_ptr)
2084 *((IV *) mg->mg_ptr) = 0;
3565fbf3 2085#endif
83f29afa
VP
2086
2087 return 0;
2088}
2089
2090int
83bf042f
NC
2091Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
2092{
97aff369 2093 dVAR;
7918f24d
NC
2094
2095 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
53c1dcc0 2096 PERL_UNUSED_ARG(sv);
7918f24d 2097
94f3782b
DM
2098 /* during global destruction, mg_obj may already have been freed */
2099 if (PL_in_clean_all)
1ea47f64 2100 return 0;
94f3782b 2101
83bf042f
NC
2102 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
2103
2104 if (mg) {
2105 /* arylen scalar holds a pointer back to the array, but doesn't own a
2106 reference. Hence the we (the array) are about to go away with it
2107 still pointing at us. Clear its pointer, else it would be pointing
2108 at free memory. See the comment in sv_magic about reference loops,
2109 and why it can't own a reference to us. */
2110 mg->mg_obj = 0;
2111 }
a0d0e21e
LW
2112 return 0;
2113}
2114
2115int
864dbfa3 2116Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2117{
97aff369 2118 dVAR;
16eb5365 2119 SV* const lsv = LvTARG(sv);
96c2a8ff 2120 MAGIC * const found = mg_find_mglob(lsv);
7918f24d
NC
2121
2122 PERL_ARGS_ASSERT_MAGIC_GETPOS;
16eb5365 2123 PERL_UNUSED_ARG(mg);
ac27b0f5 2124
6174b39a
FC
2125 if (found && found->mg_len != -1) {
2126 STRLEN i = found->mg_len;
25fdce4a 2127 if (found->mg_flags & MGf_BYTES && DO_UTF8(lsv))
6174b39a
FC
2128 i = sv_pos_b2u_flags(lsv, i, SV_GMAGIC|SV_CONST_RETURN);
2129 sv_setuv(sv, i);
a0d0e21e 2130 return 0;
a0d0e21e 2131 }
35995e5c 2132 sv_setsv(sv,NULL);
a0d0e21e
LW
2133 return 0;
2134}
2135
2136int
864dbfa3 2137Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 2138{
97aff369 2139 dVAR;
16eb5365 2140 SV* const lsv = LvTARG(sv);
a0d0e21e
LW
2141 SSize_t pos;
2142 STRLEN len;
c00206c8 2143 STRLEN ulen = 0;
53d44271 2144 MAGIC* found;
aec43834 2145 const char *s;
a0d0e21e 2146
7918f24d 2147 PERL_ARGS_ASSERT_MAGIC_SETPOS;
16eb5365 2148 PERL_UNUSED_ARG(mg);
ac27b0f5 2149
96c2a8ff 2150 found = mg_find_mglob(lsv);
3881461a 2151 if (!found) {
a0d0e21e
LW
2152 if (!SvOK(sv))
2153 return 0;
96c2a8ff 2154 found = sv_magicext_mglob(lsv);
a0d0e21e
LW
2155 }
2156 else if (!SvOK(sv)) {
3881461a 2157 found->mg_len = -1;
a0d0e21e
LW
2158 return 0;
2159 }
aec43834 2160 s = SvPV_const(lsv, len);
a0d0e21e 2161
e1dccc0d 2162 pos = SvIV(sv);
a0ed51b3 2163
7e2040f0 2164 if (DO_UTF8(lsv)) {
aec43834 2165 ulen = sv_or_pv_len_utf8(lsv, s, len);
a0ed51b3
LW
2166 if (ulen)
2167 len = ulen;
a0ed51b3
LW
2168 }
2169
a0d0e21e
LW
2170 if (pos < 0) {
2171 pos += len;
2172 if (pos < 0)
2173 pos = 0;
2174 }
eb160463 2175 else if (pos > (SSize_t)len)
a0d0e21e 2176 pos = len;
a0ed51b3 2177
3881461a 2178 found->mg_len = pos;
25fdce4a 2179 found->mg_flags &= ~(MGf_MINMATCH|MGf_BYTES);
a0d0e21e 2180
79072805
LW
2181 return 0;
2182}
2183
2184int
864dbfa3 2185Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
2186{
2187 STRLEN len;
35a4481c 2188 SV * const lsv = LvTARG(sv);
b83604b4 2189 const char * const tmps = SvPV_const(lsv,len);
777f7c56
EB
2190 STRLEN offs = LvTARGOFF(sv);
2191 STRLEN rem = LvTARGLEN(sv);
83f78d1a
FC
2192 const bool negoff = LvFLAGS(sv) & 1;
2193 const bool negrem = LvFLAGS(sv) & 2;
7918f24d
NC
2194
2195 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
8772537c 2196 PERL_UNUSED_ARG(mg);
6ff81951 2197
83f78d1a 2198 if (!translate_substr_offsets(
4ddea69a 2199 SvUTF8(lsv) ? sv_or_pv_len_utf8(lsv, tmps, len) : len,
83f78d1a
FC
2200 negoff ? -(IV)offs : (IV)offs, !negoff,
2201 negrem ? -(IV)rem : (IV)rem, !negrem, &offs, &rem
2202 )) {
2203 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
2204 sv_setsv_nomg(sv, &PL_sv_undef);
2205 return 0;
2206 }
2207
9aa983d2 2208 if (SvUTF8(lsv))
4ddea69a 2209 offs = sv_or_pv_pos_u2b(lsv, tmps, offs, &rem);
1c900557 2210 sv_setpvn(sv, tmps + offs, rem);
9aa983d2 2211 if (SvUTF8(lsv))
2ef4b674 2212 SvUTF8_on(sv);
6ff81951
GS
2213 return 0;
2214}
2215
2216int
864dbfa3 2217Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 2218{
97aff369 2219 dVAR;
83f78d1a 2220 STRLEN len, lsv_len, oldtarglen, newtarglen;
5fcbf73d 2221 const char * const tmps = SvPV_const(sv, len);
dd374669 2222 SV * const lsv = LvTARG(sv);
777f7c56
EB
2223 STRLEN lvoff = LvTARGOFF(sv);
2224 STRLEN lvlen = LvTARGLEN(sv);
83f78d1a
FC
2225 const bool negoff = LvFLAGS(sv) & 1;
2226 const bool neglen = LvFLAGS(sv) & 2;
7918f24d
NC
2227
2228 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
8772537c 2229 PERL_UNUSED_ARG(mg);
075a4a2b 2230
a74fb2cd
FC
2231 SvGETMAGIC(lsv);
2232 if (SvROK(lsv))
2233 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
2234 "Attempt to use reference as lvalue in substr"
2235 );
fc061ed8
FC
2236 SvPV_force_nomg(lsv,lsv_len);
2237 if (SvUTF8(lsv)) lsv_len = sv_len_utf8_nomg(lsv);
83f78d1a
FC
2238 if (!translate_substr_offsets(
2239 lsv_len,
2240 negoff ? -(IV)lvoff : (IV)lvoff, !negoff,
2241 neglen ? -(IV)lvlen : (IV)lvlen, !neglen, &lvoff, &lvlen
2242 ))
2243 Perl_croak(aTHX_ "substr outside of string");
2244 oldtarglen = lvlen;
1aa99e6b 2245 if (DO_UTF8(sv)) {
73a087f0 2246 sv_utf8_upgrade_nomg(lsv);
d931b1be 2247 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
a74fb2cd 2248 sv_insert_flags(lsv, lvoff, lvlen, tmps, len, 0);
7a385470 2249 newtarglen = sv_or_pv_len_utf8(sv, tmps, len);
9aa983d2
JH
2250 SvUTF8_on(lsv);
2251 }
0d336106 2252 else if (SvUTF8(lsv)) {
5fcbf73d 2253 const char *utf8;
d931b1be 2254 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
83f78d1a 2255 newtarglen = len;
5fcbf73d 2256 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
a74fb2cd 2257 sv_insert_flags(lsv, lvoff, lvlen, utf8, len, 0);
5fcbf73d 2258 Safefree(utf8);
1aa99e6b 2259 }
b76f3ce2 2260 else {
a74fb2cd 2261 sv_insert_flags(lsv, lvoff, lvlen, tmps, len, 0);
83f78d1a 2262 newtarglen = len;
b76f3ce2 2263 }
83f78d1a
FC
2264 if (!neglen) LvTARGLEN(sv) = newtarglen;
2265 if (negoff) LvTARGOFF(sv) += newtarglen - oldtarglen;
b76f3ce2 2266
79072805
LW
2267 return 0;
2268}
2269
2270int
864dbfa3 2271Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2272{
97aff369 2273 dVAR;
7918f24d
NC
2274
2275 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
8772537c 2276 PERL_UNUSED_ARG(sv);
9a9b5ec9
DM
2277#ifdef NO_TAINT_SUPPORT
2278 PERL_UNUSED_ARG(mg);
2279#endif
7918f24d 2280
27cc343c 2281 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
463ee0b2
LW
2282 return 0;
2283}
2284
2285int
864dbfa3 2286Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 2287{
97aff369 2288 dVAR;
7918f24d
NC
2289
2290 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
8772537c 2291 PERL_UNUSED_ARG(sv);
7918f24d 2292
b01e650a 2293 /* update taint status */
284167a5 2294 if (TAINT_get)
b01e650a
DM
2295 mg->mg_len |= 1;
2296 else
2297 mg->mg_len &= ~1;
463ee0b2
LW
2298 return 0;
2299}
2300
2301int
864dbfa3 2302Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 2303{
35a4481c 2304 SV * const lsv = LvTARG(sv);
7918f24d
NC
2305
2306 PERL_ARGS_ASSERT_MAGIC_GETVEC;
8772537c 2307 PERL_UNUSED_ARG(mg);
6ff81951 2308
2e9e14dd 2309 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951 2310
6ff81951
GS
2311 return 0;
2312}
2313
2314int
864dbfa3 2315Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 2316{
7918f24d 2317 PERL_ARGS_ASSERT_MAGIC_SETVEC;
8772537c 2318 PERL_UNUSED_ARG(mg);
79072805
LW
2319 do_vecset(sv); /* XXX slurp this routine */
2320 return 0;
2321}
2322
13733cde
FC
2323SV *
2324Perl_defelem_target(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 2325{
97aff369 2326 dVAR;
a0714e2c 2327 SV *targ = NULL;
13733cde
FC
2328 PERL_ARGS_ASSERT_DEFELEM_TARGET;
2329 if (!mg) mg = mg_find(sv, PERL_MAGIC_defelem);
2330 assert(mg);
5f05dabc 2331 if (LvTARGLEN(sv)) {
68dc0745 2332 if (mg->mg_obj) {
8772537c 2333 SV * const ahv = LvTARG(sv);
85fbaab2 2334 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
6d822dc4
MS
2335 if (he)
2336 targ = HeVAL(he);
68dc0745 2337 }
bbfdc870 2338 else if (LvSTARGOFF(sv) >= 0) {
502c6561 2339 AV *const av = MUTABLE_AV(LvTARG(sv));
bbfdc870 2340 if (LvSTARGOFF(sv) <= AvFILL(av))
ca58dfd9
FC
2341 {
2342 if (SvRMAGICAL(av)) {
2343 SV * const * const svp = av_fetch(av, LvSTARGOFF(sv), 0);
2344 targ = svp ? *svp : NULL;
2345 }
2346 else
bbfdc870 2347 targ = AvARRAY(av)[LvSTARGOFF(sv)];
ca58dfd9 2348 }
68dc0745 2349 }
46da273f 2350 if (targ && (targ != &PL_sv_undef)) {
68dc0745
PP
2351 /* somebody else defined it for us */
2352 SvREFCNT_dec(LvTARG(sv));
b37c2d43 2353 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
68dc0745
PP
2354 LvTARGLEN(sv) = 0;
2355 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2356 mg->mg_obj = NULL;
68dc0745
PP
2357 mg->mg_flags &= ~MGf_REFCOUNTED;
2358 }
13733cde 2359 return targ;
5f05dabc 2360 }
71be2cbc 2361 else
13733cde
FC
2362 return LvTARG(sv);
2363}
2364
2365int
2366Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2367{
2368 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2369
2370 sv_setsv(sv, defelem_target(sv, mg));
71be2cbc
PP
2371 return 0;
2372}
2373
2374int
864dbfa3 2375Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 2376{
7918f24d 2377 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
8772537c 2378 PERL_UNUSED_ARG(mg);
71be2cbc 2379 if (LvTARGLEN(sv))
68dc0745
PP
2380 vivify_defelem(sv);
2381 if (LvTARG(sv)) {
5f05dabc 2382 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
2383 SvSETMAGIC(LvTARG(sv));
2384 }
5f05dabc
PP
2385 return 0;
2386}
2387
71be2cbc 2388void
864dbfa3 2389Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 2390{
97aff369 2391 dVAR;
74e13ce4 2392 MAGIC *mg;
a0714e2c 2393 SV *value = NULL;
71be2cbc 2394
7918f24d
NC
2395 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2396
14befaf4 2397 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 2398 return;
68dc0745 2399 if (mg->mg_obj) {
8772537c 2400 SV * const ahv = LvTARG(sv);
85fbaab2 2401 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
6d822dc4
MS
2402 if (he)
2403 value = HeVAL(he);
3280af22 2404 if (!value || value == &PL_sv_undef)
be2597df 2405 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
71be2cbc 2406 }
bbfdc870
FC
2407 else if (LvSTARGOFF(sv) < 0)
2408 Perl_croak(aTHX_ PL_no_aelem, LvSTARGOFF(sv));
68dc0745 2409 else {
502c6561 2410 AV *const av = MUTABLE_AV(LvTARG(sv));
bbfdc870 2411 if ((I32)LvTARGLEN(sv) < 0 && LvSTARGOFF(sv) > AvFILL(av))
a0714e2c 2412 LvTARG(sv) = NULL; /* array can't be extended */
68dc0745 2413 else {
bbfdc870 2414 SV* const * const svp = av_fetch(av, LvSTARGOFF(sv), TRUE);
031c6d82 2415 if (!svp || !(value = *svp))
bbfdc870 2416 Perl_croak(aTHX_ PL_no_aelem, LvSTARGOFF(sv));
68dc0745
PP
2417 }
2418 }
b37c2d43 2419 SvREFCNT_inc_simple_void(value);
68dc0745
PP
2420 SvREFCNT_dec(LvTARG(sv));
2421 LvTARG(sv) = value;
71be2cbc 2422 LvTARGLEN(sv) = 0;
68dc0745 2423 SvREFCNT_dec(mg->mg_obj);
a0714e2c 2424 mg->mg_obj = NULL;
68dc0745 2425 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
2426}
2427
2428int
864dbfa3 2429Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 2430{
7918f24d 2431 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
5648c0ae
DM
2432 Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2433 return 0;
810b8aa5
GS
2434}
2435
2436int
864dbfa3 2437Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2438{
7918f24d 2439 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
96a5add6 2440 PERL_UNUSED_CONTEXT;
0177730e 2441 PERL_UNUSED_ARG(sv);
565764a8 2442 mg->mg_len = -1;
93a17b20
LW
2443 return 0;
2444}
2445
2446int
864dbfa3 2447Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2448{
35a4481c 2449 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805 2450
7918f24d
NC
2451 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2452
79072805 2453 if (uf && uf->uf_set)
24f81a43 2454 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2455 return 0;
2456}
2457
c277df42 2458int
faf82a0b
AE
2459Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2460{
488344d2 2461 const char type = mg->mg_type;
7918f24d
NC
2462
2463 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2464
488344d2
NC
2465 if (type == PERL_MAGIC_qr) {
2466 } else if (type == PERL_MAGIC_bm) {
2467 SvTAIL_off(sv);
2468 SvVALID_off(sv);
2469 } else {
2470 assert(type == PERL_MAGIC_fm);
488344d2
NC
2471 }
2472 return sv_unmagic(sv, type);
faf82a0b
AE
2473}
2474
7a4c00b4 2475#ifdef USE_LOCALE_COLLATE
79072805 2476int
864dbfa3 2477Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69 2478{
7918f24d
NC
2479 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2480
bbce6d69 2481 /*
838b5b74 2482 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2483 * and vanished with a faint plop.
2484 */
96a5add6 2485 PERL_UNUSED_CONTEXT;
8772537c 2486 PERL_UNUSED_ARG(sv);
7a4c00b4
PP
2487 if (mg->mg_ptr) {
2488 Safefree(mg->mg_ptr);
2489 mg->mg_ptr = NULL;
565764a8 2490 mg->mg_len = -1;
7a4c00b4 2491 }
bbce6d69
PP
2492 return 0;
2493}
7a4c00b4 2494#endif /* USE_LOCALE_COLLATE */
bbce6d69 2495
7e8c5dac
HS
2496/* Just clear the UTF-8 cache data. */
2497int
2498Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2499{
7918f24d 2500 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
96a5add6 2501 PERL_UNUSED_CONTEXT;
8772537c 2502 PERL_UNUSED_ARG(sv);
7e8c5dac 2503 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
3881461a 2504 mg->mg_ptr = NULL;
7e8c5dac
HS
2505 mg->mg_len = -1; /* The mg_len holds the len cache. */
2506 return 0;
2507}
2508
bbce6d69 2509int
864dbfa3 2510Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2511{
97aff369 2512 dVAR;
eb578fdb
KW
2513 const char *s;
2514 I32 paren;
2515 const REGEXP * rx;
79072805 2516 I32 i;
8990e307 2517 STRLEN len;
125b9982 2518 MAGIC *tmg;
2fdbfb4d 2519
7918f24d
NC
2520 PERL_ARGS_ASSERT_MAGIC_SET;
2521
e91d8259 2522 if (!mg->mg_ptr) {
960b831f 2523 paren = mg->mg_len;
1e05feb3 2524 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
e91d8259 2525 setparen_got_rx:
2fdbfb4d 2526 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
1e05feb3 2527 } else {
2fdbfb4d
AB
2528 /* Croak with a READONLY error when a numbered match var is
2529 * set without a previous pattern match. Unless it's C<local $1>
2530 */
e91d8259 2531 croakparen:
2fdbfb4d 2532 if (!PL_localizing) {
cb077ed2 2533 Perl_croak_no_modify();
2fdbfb4d
AB
2534 }
2535 }
e91d8259
NC
2536 return 0;
2537 }
2538
2539 switch (*mg->mg_ptr) {
748a9306 2540 case '\001': /* ^A */
f2da823f
FC
2541 if (SvOK(sv)) sv_copypv(PL_bodytarget, sv);
2542 else SvOK_off(PL_bodytarget);
64eff8b7
DM
2543 FmLINES(PL_bodytarget) = 0;
2544 if (SvPOK(PL_bodytarget)) {
2545 char *s = SvPVX(PL_bodytarget);
2546 while ( ((s = strchr(s, '\n'))) ) {
2547 FmLINES(PL_bodytarget)++;
2548 s++;
2549 }
2550 }
125b9982 2551 /* mg_set() has temporarily made sv non-magical */
284167a5 2552 if (TAINTING_get) {
125b9982
NT
2553 if ((tmg = mg_find(sv,PERL_MAGIC_taint)) && tmg->mg_len & 1)
2554 SvTAINTED_on(PL_bodytarget);
2555 else
2556 SvTAINTED_off(PL_bodytarget);
2557 }
748a9306 2558 break;
49460fe6 2559 case '\003': /* ^C */
f2338a2e 2560 PL_minus_c = cBOOL(SvIV(sv));
49460fe6
NIS
2561 break;
2562
79072805 2563 case '\004': /* ^D */
b4ab917c 2564#ifdef DEBUGGING
b83604b4 2565 s = SvPV_nolen_const(sv);
ddcf8bc1 2566 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
a58fb6f9
CS
2567 if (DEBUG_x_TEST || DEBUG_B_TEST)
2568 dump_all_perl(!DEBUG_B_TEST);
b4ab917c 2569#else
38ab35f8 2570 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
b4ab917c 2571#endif
79072805 2572 break;
28f23441 2573 case '\005': /* ^E */
d0063567 2574 if (*(mg->mg_ptr+1) == '\0') {
e37778c2 2575#ifdef VMS
38ab35f8 2576 set_vaxc_errno(SvIV(sv));
e37778c2
NC
2577#else
2578# ifdef WIN32
d0063567 2579 SetLastError( SvIV(sv) );
e37778c2
NC
2580# else
2581# ifdef OS2
38ab35f8 2582 os2_setsyserrno(SvIV(sv));
e37778c2 2583# else
d0063567 2584 /* will anyone ever use this? */
38ab35f8 2585 SETERRNO(SvIV(sv), 4);
048c1ddf
IZ
2586# endif
2587# endif
22fae026 2588#endif
d0063567
DK
2589 }
2590 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
ef8d46e8 2591 SvREFCNT_dec(PL_encoding);
d0063567
DK
2592 if (SvOK(sv) || SvGMAGICAL(sv)) {
2593 PL_encoding = newSVsv(sv);
2594 }
2595 else {
a0714e2c 2596 PL_encoding = NULL;
d0063567
DK
2597 }
2598 }
2599 break;
79072805 2600 case '\006': /* ^F */
38ab35f8 2601 PL_maxsysfd = SvIV(sv);
79072805 2602 break;
a0d0e21e 2603 case '\010': /* ^H */
38ab35f8 2604 PL_hints = SvIV(sv);
a0d0e21e 2605 break;
9d116dd7 2606 case '\011': /* ^I */ /* NOT \t in EBCDIC */
43c5f42d 2607 Safefree(PL_inplace);
bd61b366 2608 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
da78da6e 2609 break;
d9bad346
FC
2610 case '\016': /* ^N */
2611 if (PL_curpm && (rx = PM_GETRE(PL_curpm))
2612 && (paren = RX_LASTCLOSEPAREN(rx))) goto setparen_got_rx;
2613 goto croakparen;
28f23441 2614 case '\017': /* ^O */
ac27b0f5 2615 if (*(mg->mg_ptr+1) == '\0') {
43c5f42d 2616 Safefree(PL_osname);
bd61b366 2617 PL_osname = NULL;
3511154c
DM
2618 if (SvOK(sv)) {
2619 TAINT_PROPER("assigning to $^O");
2e0de35c 2620 PL_osname = savesvpv(sv);
3511154c 2621 }
ac27b0f5
NIS
2622 }
2623 else if (strEQ(mg->mg_ptr, "\017PEN")) {
8b850bd5
NC
2624 STRLEN len;
2625 const char *const start = SvPV(sv, len);
b54fc2b6 2626 const char *out = (const char*)memchr(start, '\0', len);
8b850bd5 2627 SV *tmp;
8b850bd5
NC
2628
2629
2630 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
f747ebd6 2631 PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
8b850bd5
NC
2632
2633 /* Opening for input is more common than opening for output, so
2634 ensure that hints for input are sooner on linked list. */
59cd0e26 2635 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
f747ebd6
Z
2636 SvUTF8(sv))
2637 : newSVpvs_flags("", SvUTF8(sv));
2638 (void)hv_stores(GvHV(PL_hintgv), "open>", tmp);
2639 mg_set(tmp);
8b850bd5 2640
f747ebd6
Z
2641 tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len,
2642 SvUTF8(sv));
2643 (void)hv_stores(GvHV(PL_hintgv), "open<", tmp);
2644 mg_set(tmp);
ac27b0f5 2645 }
28f23441 2646 break;
79072805 2647 case '\020': /* ^P */
2fdbfb4d
AB
2648 PL_perldb = SvIV(sv);
2649 if (PL_perldb && !PL_DBsingle)
2650 init_debugger();
9b9e0be4 2651 break;
79072805 2652 case '\024': /* ^T */
88e89b8a 2653#ifdef BIG_TIME
6b88bc9c 2654 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2655#else
38ab35f8 2656 PL_basetime = (Time_t)SvIV(sv);
88e89b8a 2657#endif
79072805 2658 break;
e07ea26a
NC
2659 case '\025': /* ^UTF8CACHE */
2660 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2661 PL_utf8cache = (signed char) sv_2iv(sv);
2662 }
2663 break;
fde18df1 2664 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2665 if (*(mg->mg_ptr+1) == '\0') {
2666 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
38ab35f8 2667 i = SvIV(sv);
ac27b0f5 2668 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2669 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2670 }
599cee73 2671 }
0a378802 2672 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2673 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
7e4f0450
FC
2674 if (!SvPOK(sv)) {
2675 PL_compiling.cop_warnings = pWARN_STD;
d775746e
GS
2676 break;
2677 }
f4fc7782 2678 {
b5477537 2679 STRLEN len, i;
d3a7d8c7 2680 int accumulate = 0 ;
f4fc7782 2681 int any_fatals = 0 ;
b83604b4 2682 const char * const ptr = SvPV_const(sv, len) ;
f4fc7782
JH
2683 for (i = 0 ; i < len ; ++i) {
2684 accumulate |= ptr[i] ;
2685 any_fatals |= (ptr[i] & 0xAA) ;
2686 }
4243c432
NC
2687 if (!accumulate) {
2688 if (!specialWARN(PL_compiling.cop_warnings))
2689 PerlMemShared_free(PL_compiling.cop_warnings);
2690 PL_compiling.cop_warnings = pWARN_NONE;
2691 }
72dc9ed5 2692 /* Yuck. I can't see how to abstract this: */
2f3f0b56
KW
2693 else if (isWARN_on(
2694 ((STRLEN *)SvPV_nolen_const(sv)) - 1,
2695 WARN_ALL)
2696 && !any_fatals)
2697 {
4243c432
NC
2698 if (!specialWARN(PL_compiling.cop_warnings))
2699 PerlMemShared_free(PL_compiling.cop_warnings);
f4fc7782
JH
2700 PL_compiling.cop_warnings = pWARN_ALL;
2701 PL_dowarn |= G_WARN_ONCE ;
727405f8 2702 }
d3a7d8c7 2703 else {
72dc9ed5
NC
2704 STRLEN len;
2705 const char *const p = SvPV_const(sv, len);
2706
2707 PL_compiling.cop_warnings
8ee4cf24 2708 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
72dc9ed5
NC
2709 p, len);
2710
d3a7d8c7
GS
2711 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2712 PL_dowarn |= G_WARN_ONCE ;
2713 }
f4fc7782 2714
d3a7d8c7 2715 }
4438c4b7 2716 }
971a9dd3 2717 }
79072805
LW
2718 break;
2719 case '.':
3280af22
NIS
2720 if (PL_localizing) {
2721 if (PL_localizing == 1)
7766f137 2722 SAVESPTR(PL_last_in_gv);
748a9306 2723 }
3280af22 2724 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2725 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2726 break;
2727 case '^':
acbe1b9d
FC
2728 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2729 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2730 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2731 break;
2732 case '~':
acbe1b9d
FC
2733 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2734 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2735 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2736 break;
2737 case '=':
acbe1b9d 2738 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2739 break;
2740 case '-':
acbe1b9d
FC
2741 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2742 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
099be4f1 2743 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2744 break;
2745 case '%':
acbe1b9d 2746 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
79072805
LW
2747 break;
2748 case '|':
4b65379b 2749 {
099be4f1 2750 IO * const io = GvIO(PL_defoutgv);
720f287d
AB
2751 if(!io)
2752 break;
38ab35f8 2753 if ((SvIV(sv)) == 0)
4b65379b
CS
2754 IoFLAGS(io) &= ~IOf_FLUSH;
2755 else {
2756 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2757 PerlIO *ofp = IoOFP(io);
2758 if (ofp)
2759 (void)PerlIO_flush(ofp);
2760 IoFLAGS(io) |= IOf_FLUSH;
2761 }
2762 }
79072805
LW
2763 }
2764 break;
79072805 2765 case '/':
1ab48e3a
YO
2766 {
2767 SV *tmpsv= sv;
2768 if (SvROK(sv)) {
2769 SV *referent= SvRV(sv);
2770 const char *reftype= sv_reftype(referent, 0);
2771 /* XXX: dodgy type check: This leaves me feeling dirty, but the alternative
2772 * is to copy pretty much the entire sv_reftype() into this routine, or to do
2773 * a full string comparison on the return of sv_reftype() both of which
2774 * make me feel worse! NOTE, do not modify this comment without reviewing the
2775 * corresponding comment in sv_reftype(). - Yves */
2776 if (reftype[0] == 'S' || reftype[0] == 'L') {
2777 IV val= SvIV(referent);
2778 if (val <= 0) {
2779 tmpsv= &PL_sv_undef;
2780 Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED),
2781 "Setting $/ to a reference to %s as a form of slurp is deprecated, treating as undef",
2782 SvIV(SvRV(sv)) < 0 ? "a negative integer" : "zero"
2783 );
2784 }
2785 } else {
ee0ba734 2786 /* diag_listed_as: Setting $/ to %s reference is forbidden */
a48e4205
FC
2787 Perl_croak(aTHX_ "Setting $/ to a%s %s reference is forbidden",
2788 *reftype == 'A' ? "n" : "", reftype);
1ab48e3a
YO
2789 }
2790 }
b3a2acfa 2791 SvREFCNT_dec(PL_rs);
1ab48e3a 2792 PL_rs = newSVsv(tmpsv);
b3a2acfa 2793 }
79072805
LW
2794 break;
2795 case '\\':
ef8d46e8 2796 SvREFCNT_dec(PL_ors_sv);
6bc2995b 2797 if (SvOK(sv)) {
7889fe52 2798 PL_ors_sv = newSVsv(sv);
009c130f 2799 }
e3c19b7b 2800 else {
a0714e2c 2801 PL_ors_sv = NULL;
e3c19b7b 2802 }
79072805 2803 break;
7d69d4a6
FC
2804 case '[':
2805 if (SvIV(sv) != 0)
2806 Perl_croak(aTHX_ "Assigning non-zero to $[ is no longer possible");
2807 break;
79072805 2808 case '?':
ff0cee69 2809#ifdef COMPLEX_STATUS
6b88bc9c 2810 if (PL_localizing == 2) {
41cb7b2b 2811 SvUPGRADE(sv, SVt_PVLV);
6b88bc9c
GS
2812 PL_statusvalue = LvTARGOFF(sv);
2813 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2814 }
2815 else
2816#endif
2817#ifdef VMSISH_STATUS
2818 if (VMSISH_STATUS)
fb38d079 2819 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
ff0cee69
PP
2820 else
2821#endif
38ab35f8 2822 STATUS_UNIX_EXIT_SET(SvIV(sv));
79072805
LW
2823 break;
2824 case '!':
93189314
JH
2825 {
2826#ifdef VMS
2827# define PERL_VMS_BANG vaxc$errno
2828#else
2829# define PERL_VMS_BANG 0
2830#endif
27329181 2831#if defined(WIN32) && ! defined(UNDER_CE)
c9beaf97
SH
2832 SETERRNO(win32_get_errno(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0),
2833 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2834#else
91487cfc 2835 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314 2836 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
c9beaf97 2837#endif
93189314 2838 }
79072805
LW
2839 break;
2840 case '<':
985213f2 2841 {
b469f1e0 2842 /* XXX $< currently silently ignores failures */
dfff4baf 2843 const Uid_t new_uid = SvUID(sv);
985213f2 2844 PL_delaymagic_uid = new_uid;
3280af22
NIS
2845 if (PL_delaymagic) {
2846 PL_delaymagic |= DM_RUID;
79072805
LW
2847 break; /* don't do magic till later */
2848 }
2849#ifdef HAS_SETRUID
b469f1e0 2850 PERL_UNUSED_RESULT(setruid(new_uid));
79072805
LW
2851#else
2852#ifdef HAS_SETREUID
b469f1e0 2853 PERL_UNUSED_RESULT(setreuid(new_uid, (Uid_t)-1));
748a9306 2854#else
85e6fe83 2855#ifdef HAS_SETRESUID
b469f1e0 2856 PERL_UNUSED_RESULT(setresuid(new_uid, (Uid_t)-1, (Uid_t)-1));
79072805 2857#else
985213f2 2858 if (new_uid == PerlProc_geteuid()) { /* special case $< = $> */
75870ed3 2859#ifdef PERL_DARWIN
2860 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
985213f2 2861 if (new_uid != 0 && PerlProc_getuid() == 0)
b469f1e0 2862 PERL_UNUSED_RESULT(PerlProc_setuid(0));
75870ed3 2863#endif
b469f1e0 2864 PERL_UNUSED_RESULT(PerlProc_setuid(new_uid));
75870ed3 2865 } else {
cea2e8a9 2866 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2867 }
79072805
LW
2868#endif
2869#endif
85e6fe83 2870#endif
79072805 2871 break;
985213f2 2872 }
79072805 2873 case '>':
985213f2 2874 {
b469f1e0 2875 /* XXX $> currently silently ignores failures */
dfff4baf 2876 const Uid_t new_euid = SvUID(sv);
985213f2 2877 PL_delaymagic_euid = new_euid;
3280af22
NIS
2878 if (PL_delaymagic) {
2879 PL_delaymagic |= DM_EUID;
79072805
LW
2880 break; /* don't do magic till later */
2881 }
2882#ifdef HAS_SETEUID
b469f1e0 2883 PERL_UNUSED_RESULT(seteuid(new_euid));
79072805
LW
2884#else
2885#ifdef HAS_SETREUID
b469f1e0 2886 PERL_UNUSED_RESULT(setreuid((Uid_t)-1, new_euid));
85e6fe83
LW
2887#else
2888#ifdef HAS_SETRESUID
b469f1e0 2889 PERL_UNUSED_RESULT(setresuid((Uid_t)-1, new_euid, (Uid_t)-1));
79072805 2890#else
985213f2 2891 if (new_euid == PerlProc_getuid()) /* special case $> = $< */
b469f1e0 2892 PERL_UNUSED_RESULT(PerlProc_setuid(new_euid));
a0d0e21e 2893 else {
cea2e8a9 2894 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2895 }
79072805
LW
2896#endif
2897#endif
85e6fe83 2898#endif
79072805 2899 break;
985213f2 2900 }
79072805 2901 case '(':
985213f2 2902 {
b469f1e0 2903 /* XXX $( currently silently ignores failures */
dfff4baf 2904 const Gid_t new_gid = SvGID(sv);
985213f2 2905 PL_delaymagic_gid = new_gid;
3280af22
NIS
2906 if (PL_delaymagic) {
2907 PL_delaymagic |= DM_RGID;
79072805
LW
2908 break; /* don't do magic till later */
2909 }
2910#ifdef HAS_SETRGID
b469f1e0 2911 PERL_UNUSED_RESULT(setrgid(new_gid));
79072805
LW
2912#else
2913#ifdef HAS_SETREGID
b469f1e0 2914 PERL_UNUSED_RESULT(setregid(new_gid, (Gid_t)-1));
85e6fe83
LW
2915#else
2916#ifdef HAS_SETRESGID
b469f1e0 2917 PERL_UNUSED_RESULT(setresgid(new_gid, (Gid_t)-1, (Gid_t) -1));
79072805 2918#else
985213f2 2919 if (new_gid == PerlProc_getegid()) /* special case $( = $) */
b469f1e0 2920 PERL_UNUSED_RESULT(PerlProc_setgid(new_gid));
748a9306 2921 else {
cea2e8a9 2922 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2923 }
79072805
LW
2924#endif
2925#endif
85e6fe83 2926#endif
79072805 2927 break;
985213f2 2928 }
79072805 2929 case ')':
985213f2 2930 {
b469f1e0 2931 /* XXX $) currently silently ignores failures */
dfff4baf 2932 Gid_t new_egid;
5cd24f17
PP
2933#ifdef HAS_SETGROUPS
2934 {
b83604b4 2935 const char *p = SvPV_const(sv, len);
757f63d8 2936 Groups_t *gary = NULL;
fb4089e0 2937#ifdef _SC_NGROUPS_MAX
2938 int maxgrp = sysconf(_SC_NGROUPS_MAX);
2939
2940 if (maxgrp < 0)
2941 maxgrp = NGROUPS;
2942#else
2943 int maxgrp = NGROUPS;
2944#endif
757f63d8
SP
2945
2946 while (isSPACE(*p))
2947 ++p;
dfff4baf 2948 new_egid = (Gid_t)Atol(p);
fb4089e0 2949 for (i = 0; i < maxgrp; ++i) {
757f63d8
SP
2950 while (*p && !isSPACE(*p))
2951 ++p;
2952 while (isSPACE(*p))
2953 ++p;
2954 if (!*p)
2955 break;
2956 if(!gary)
2957 Newx(gary, i + 1, Groups_t);
2958 else
2959 Renew(gary, i + 1, Groups_t);
dfff4baf 2960 gary[i] = (Groups_t)Atol(p);
757f63d8
SP
2961 }
2962 if (i)
b469f1e0 2963 PERL_UNUSED_RESULT(setgroups(i, gary));
f5a63d97 2964 Safefree(gary);
5cd24f17
PP
2965 }
2966#else /* HAS_SETGROUPS */
dfff4baf 2967 new_egid = SvGID(sv);
5cd24f17 2968#endif /* HAS_SETGROUPS */
985213f2 2969 PL_delaymagic_egid = new_egid;
3280af22
NIS
2970 if (PL_delaymagic) {
2971 PL_delaymagic |= DM_EGID;
79072805
LW
2972 break; /* don't do magic till later */
2973 }
2974#ifdef HAS_SETEGID
b469f1e0 2975 PERL_UNUSED_RESULT(setegid(new_egid));
79072805
LW
2976#else
2977#ifdef HAS_SETREGID
b469f1e0 2978 PERL_UNUSED_RESULT(setregid((Gid_t)-1, new_egid));
85e6fe83
LW
2979#else
2980#ifdef HAS_SETRESGID
b469f1e0 2981 PERL_UNUSED_RESULT(setresgid((Gid_t)-1, new_egid, (Gid_t)-1));
79072805 2982#else
985213f2 2983 if (new_egid == PerlProc_getgid()) /* special case $) = $( */
b469f1e0 2984 PERL_UNUSED_RESULT(PerlProc_setgid(new_egid));
748a9306 2985 else {
cea2e8a9 2986 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2987 }
79072805
LW
2988#endif
2989#endif
85e6fe83 2990#endif
79072805 2991 break;
985213f2 2992 }
79072805 2993 case ':':
2d8e6c8d 2994 PL_chopset = SvPV_force(sv,len);
79072805 2995 break;
9cdac2a2
FC
2996 case '$': /* $$ */
2997 /* Store the pid in mg->mg_obj so we can tell when a fork has
2998 occurred. mg->mg_obj points to *$ by default, so clear it. */
2999 if (isGV(mg->mg_obj)) {
3000 if (mg->mg_flags & MGf_REFCOUNTED) /* probably never true */
3001 SvREFCNT_dec(mg->mg_obj);
3002 mg->mg_flags |= MGf_REFCOUNTED;
3003 mg->mg_obj = newSViv((IV)PerlProc_getpid());
3004 }
3005 else sv_setiv(mg->mg_obj, (IV)PerlProc_getpid());
3006 break;
79072805 3007 case '0':
e2975953 3008 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
3009#ifdef HAS_SETPROCTITLE
3010 /* The BSDs don't show the argv[] in ps(1) output, they
3011 * show a string from the process struct and provide
3012 * the setproctitle() routine to manipulate that. */
a2722ac9 3013 if (PL_origalen != 1) {
b83604b4 3014 s = SvPV_const(sv, len);
98b76f99 3015# if __FreeBSD_version > 410001
9aad2c0e 3016 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
3017 * but not the "(perl) suffix from the ps(1)
3018 * output, because that's what ps(1) shows if the
3019 * argv[] is modified. */
6f2ad931 3020 setproctitle("-%s", s);
9aad2c0e 3021# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
3022 /* This doesn't really work if you assume that
3023 * $0 = 'foobar'; will wipe out 'perl' from the $0
3024 * because in ps(1) output the result will be like
3025 * sprintf("perl: %s (perl)", s)
3026 * I guess this is a security feature:
3027 * one (a user process) cannot get rid of the original name.
3028 * --jhi */
3029 setproctitle("%s", s);
3030# endif
3031 }
9d3968b2 3032#elif defined(__hpux) && defined(PSTAT_SETCMD)
a2722ac9 3033 if (PL_origalen != 1) {
17aa7f3d 3034 union pstun un;
b83604b4 3035 s = SvPV_const(sv, len);
6867be6d 3036 un.pst_command = (char *)s;
17aa7f3d
JH
3037 pstat(PSTAT_SETCMD, un, len, 0, 0);
3038 }
9d3968b2 3039#else
2d2af554
GA
3040 if (PL_origalen > 1) {
3041 /* PL_origalen is set in perl_parse(). */
3042 s = SvPV_force(sv,len);
3043 if (len >= (STRLEN)PL_origalen-1) {
3044 /* Longer than original, will be truncated. We assume that
3045 * PL_origalen bytes are available. */
3046 Copy(s, PL_origargv[0], PL_origalen-1, char);
3047 }
3048 else {
3049 /* Shorter than original, will be padded. */
235ac35d 3050#ifdef PERL_DARWIN
60777a0d
JH
3051 /* Special case for Mac OS X: see [perl #38868] */
3052 const int pad = 0;
235ac35d 3053#else
8a89a4f1
MB
3054 /* Is the space counterintuitive? Yes.
3055 * (You were expecting \0?)
3056 * Does it work? Seems to. (In Linux 2.4.20 at least.)
3057 * --jhi */
60777a0d 3058 const int pad = ' ';
235ac35d 3059#endif
60777a0d
JH
3060 Copy(s, PL_origargv[0], len, char);
3061 PL_origargv[0][len] = 0;
3062 memset(PL_origargv[0] + len + 1,
3063 pad, PL_origalen - len - 1);
2d2af554
GA
3064 }
3065 PL_origargv[0][PL_origalen-1] = 0;
3066 for (i = 1; i < PL_origargc; i++)
3067 PL_origargv[i] = 0;
7636ea95
AB
3068#ifdef HAS_PRCTL_SET_NAME
3069 /* Set the legacy process name in addition to the POSIX name on Linux */
3070 if (prctl(PR_SET_NAME, (unsigned long)s, 0, 0, 0) != 0) {
3071 /* diag_listed_as: SKIPME */
3072 Perl_croak(aTHX_ "Can't set $0 with prctl(): %s", Strerror(errno));
3073 }
3074#endif
79072805 3075 }
9d3968b2 3076#endif
e2975953 3077 UNLOCK_DOLLARZERO_MUTEX;
79072805
LW
3078 break;
3079 }
3080 return 0;
3081}
3082
3083I32
84c7b88c
BF
3084Perl_whichsig_sv(pTHX_ SV *sigsv)
3085{
3086 const char *sigpv;
3087 STRLEN siglen;
3088 PERL_ARGS_ASSERT_WHICHSIG_SV;
3089 PERL_UNUSED_CONTEXT;
3090 sigpv = SvPV_const(sigsv, siglen);
3091 return whichsig_pvn(sigpv, siglen);
3092}
3093
3094I32
3095Perl_whichsig_pv(pTHX_ const char *sig)
3096{
3097 PERL_ARGS_ASSERT_WHICHSIG_PV;
3098 PERL_UNUSED_CONTEXT;
3099 return whichsig_pvn(sig, strlen(sig));
3100}
3101
3102I32
3103Perl_whichsig_pvn(pTHX_ const char *sig, STRLEN len)
79072805 3104{
eb578fdb 3105 char* const* sigv;
7918f24d 3106
84c7b88c 3107 PERL_ARGS_ASSERT_WHICHSIG_PVN;
96a5add6 3108 PERL_UNUSED_CONTEXT;
79072805 3109
aadb217d 3110 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
84c7b88c 3111 if (strlen(*sigv) == len && memEQ(sig,*sigv, len))
aadb217d 3112 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805 3113#ifdef SIGCLD
84c7b88c 3114 if (memEQs(sig, len, "CHLD"))
79072805
LW
3115 return SIGCLD;
3116#endif
3117#ifdef SIGCHLD
84c7b88c 3118 if (memEQs(sig, len, "CLD"))
79072805
LW
3119 return SIGCHLD;
3120#endif
7f1236c0 3121 return -1;
79072805
LW
3122}
3123
ecfc5424 3124Signal_t
1e82f5a6 3125#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
b3dbdd48 3126Perl_sighandler(int sig, siginfo_t *sip, void *uap)
1e82f5a6
SH
3127#else
3128Perl_sighandler(int sig)
3129#endif
79072805 3130{
1018e26f
NIS
3131#ifdef PERL_GET_SIG_CONTEXT
3132 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 3133#else
cea2e8a9 3134 dTHX;
71d280e3 3135#endif
79072805 3136 dSP;
a0714e2c
SS
3137 GV *gv = NULL;
3138 SV *sv = NULL;
8772537c 3139 SV * const tSv = PL_Sv;
601f1833 3140 CV *cv = NULL;
533c011a 3141 OP *myop = PL_op;
84902520 3142 U32 flags = 0;
8772537c 3143 XPV * const tXpv = PL_Xpv;
0c4d3b5e 3144 I32 old_ss_ix = PL_savestack_ix;
100c03aa 3145 SV *errsv_save = NULL;
71d280e3 3146
84902520 3147
727405f8 3148 if (!PL_psig_ptr[sig]) {
99ef548b 3149 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
3150 PL_sig_name[sig]);
3151 exit(sig);
3152 }
ff0cee69 3153
a0d63a7b
DM
3154 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
3155 /* Max number of items pushed there is 3*n or 4. We cannot fix
3156 infinity, so we fix 4 (in fact 5): */
3157 if (PL_savestack_ix + 15 <= PL_savestack_max) {
3158 flags |= 1;
3159 PL_savestack_ix += 5; /* Protect save in progress. */
3160 SAVEDESTRUCTOR_X(S_unwind_handler_stack, NULL);
3161 }
84902520 3162 }
84902520 3163 /* sv_2cv is too complicated, try a simpler variant first: */
ea726b52 3164 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
8772537c
AL
3165 || SvTYPE(cv) != SVt_PVCV) {
3166 HV *st;
f2c0649b 3167 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
8772537c 3168 }
84902520 3169
a0d0e21e 3170 if (!cv || !CvROOT(cv)) {
a2a5de95
NC
3171 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
3172 PL_sig_name[sig], (gv ? GvENAME(gv)
3173 : ((cv && CvGV(cv))
3174 ? GvENAME(CvGV(cv))
3175 : "__ANON__")));
00d579c5 3176 goto cleanup;
79072805
LW
3177 }
3178
0c4d3b5e
DM
3179 sv = PL_psig_name[sig]
3180 ? SvREFCNT_inc_NN(PL_psig_name[sig])
3181 : newSVpv(PL_sig_name[sig],0);
72048cfe 3182 flags |= 8;
0c4d3b5e
DM
3183 SAVEFREESV(sv);
3184
a0d63a7b
DM
3185 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
3186 /* make sure our assumption about the size of the SAVEs are correct:
3187 * 3 for SAVEDESTRUCTOR_X, 2 for SAVEFREESV */
3188 assert(old_ss_ix + 2 + ((flags & 1) ? 3+5 : 0) == PL_savestack_ix);
3189 }
e336de0d 3190
e788e7d3 3191 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 3192 PUSHMARK(SP);
79072805 3193 PUSHs(sv);
8aad04aa
JH
3194#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
3195 {
3196 struct sigaction oact;
3197
3198 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
8aad04aa
JH
3199 if (sip) {
3200 HV *sih = newHV();
ad64d0ec 3201 SV *rv = newRV_noinc(MUTABLE_SV(sih));
8aad04aa
JH
3202 /* The siginfo fields signo, code, errno, pid, uid,
3203 * addr, status, and band are defined by POSIX/SUSv3. */
85771703
NC
3204 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
3205 (void)hv_stores(sih, "code", newSViv(sip->si_code));
79dec0f4 3206#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
3207 hv_stores(sih, "errno", newSViv(sip->si_errno));
3208 hv_stores(sih, "status", newSViv(sip->si_status));
3209 hv_stores(sih, "uid", newSViv(sip->si_uid));
3210 hv_stores(sih, "pid", newSViv(sip->si_pid));
3211 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
3212 hv_stores(sih, "band", newSViv(sip->si_band));
79dec0f4 3213#endif
8aad04aa 3214 EXTEND(SP, 2);
ad64d0ec 3215 PUSHs(rv);
22f1178f 3216 mPUSHp((char *)sip, sizeof(*sip));
8aad04aa 3217 }
b4552a27 3218
8aad04aa
JH
3219 }
3220 }
3221#endif
79072805 3222 PUTBACK;
a0d0e21e 3223
100c03aa
JL
3224 errsv_save = newSVsv(ERRSV);
3225
ad64d0ec 3226 call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL);
79072805 3227
d3acc0f7 3228 POPSTACK;
eed484f9
DD
3229 {
3230 SV * const errsv = ERRSV;
3231 if (SvTRUE_NN(errsv)) {
3232 SvREFCNT_dec(errsv_save);
c22d665b 3233#ifndef PERL_MICRO
1b266415
NIS
3234 /* Handler "died", for example to get out of a restart-able read().
3235 * Before we re-do that on its behalf re-enable the signal which was
3236 * blocked by the system when we entered.
3237 */
c22d665b 3238#ifdef HAS_SIGPROCMASK
d488af49 3239#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
eed484f9 3240 if (sip || uap)
c22d665b 3241#endif
eed484f9
DD
3242 {
3243 sigset_t set;
3244 sigemptyset(&set);
3245 sigaddset(&set,sig);
3246 sigprocmask(SIG_UNBLOCK, &set, NULL);
3247 }
c22d665b 3248#else
eed484f9
DD
3249 /* Not clear if this will work */
3250 (void)rsignal(sig, SIG_IGN);
3251 (void)rsignal(sig, PL_csighandlerp);
c22d665b
LT
3252#endif
3253#endif /* !PERL_MICRO */
eed484f9
DD
3254 die_sv(errsv);
3255 }
3256 else {
3257 sv_setsv(errsv, errsv_save);
3258 SvREFCNT_dec(errsv_save);
3259 }
100c03aa
JL
3260 }
3261
00d579c5 3262cleanup:
0c4d3b5e
DM
3263 /* pop any of SAVEFREESV, SAVEDESTRUCTOR_X and "save in progress" */
3264 PL_savestack_ix = old_ss_ix;
72048cfe 3265 if (flags & 8)
2357bae7 3266 SvREFCNT_dec_NN(sv);
533c011a 3267 PL_op = myop; /* Apparently not needed... */
ac27b0f5 3268
3280af22
NIS
3269 PL_Sv = tSv; /* Restore global temporaries. */
3270 PL_Xpv = tXpv;
53bb94e2 3271 return;
79072805 3272}
4e35701f
NIS
3273
3274
51371543 3275static void
8772537c 3276S_restore_magic(pTHX_ const void *p)
51371543 3277{
97aff369 3278 dVAR;
8772537c
AL
3279 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
3280 SV* const sv = mgs->mgs_sv;
150b625d 3281 bool bumped;
51371543
GS
3282
3283 if (!sv)
3284 return;
3285
4bac9ae4
CS
3286 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3287 SvTEMP_off(sv); /* if it's still magical, this value isn't temporary */
f8c7b90f 3288#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
3289 /* While magic was saved (and off) sv_setsv may well have seen
3290 this SV as a prime candidate for COW. */
3291 if (SvIsCOW(sv))
e424a81e 3292 sv_force_normal_flags(sv, 0);
f9701176 3293#endif
f9c6fee5
CS
3294 if (mgs->mgs_readonly)
3295 SvREADONLY_on(sv);
3296 if (mgs->mgs_magical)
3297 SvFLAGS(sv) |= mgs->mgs_magical;
51371543
GS
3298 else
3299 mg_magical(sv);
51371543
GS
3300 }
3301
150b625d 3302 bumped = mgs->mgs_bumped;
51371543
GS
3303 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3304
3305 /* If we're still on top of the stack, pop us off. (That condition
3306 * will be satisfied if restore_magic was called explicitly, but *not*
3307 * if it's being called via leave_scope.)
3308 * The reason for doing this is that otherwise, things like sv_2cv()
3309 * may leave alloc gunk on the savestack, and some code
3310 * (e.g. sighandler) doesn't expect that...
3311 */
3312 if (PL_savestack_ix == mgs->mgs_ss_ix)
3313 {
1be36ce0
NC
3314 UV popval = SSPOPUV;
3315 assert(popval == SAVEt_DESTRUCTOR_X);
51371543 3316 PL_savestack_ix -= 2;
1be36ce0
NC
3317 popval = SSPOPUV;
3318 assert((popval & SAVE_MASK) == SAVEt_ALLOC);
3319 PL_savestack_ix -= popval >> SAVE_TIGHT_SHIFT;
51371543 3320 }
150b625d
DM
3321 if (bumped) {
3322 if (SvREFCNT(sv) == 1) {
3323 /* We hold the last reference to this SV, which implies that the
3324 SV was deleted as a side effect of the routines we called.
3325 So artificially keep it alive a bit longer.
3326 We avoid turning on the TEMP flag, which can cause the SV's
3327 buffer to get stolen (and maybe other stuff). */
150b625d 3328 sv_2mortal(sv);
4bac9ae4 3329 SvTEMP_off(sv);
8985fe98 3330 }
150b625d 3331 else
2357bae7 3332 SvREFCNT_dec_NN(sv); /* undo the inc in S_save_magic() */
8985fe98 3333 }
51371543
GS
3334}
3335
0c4d3b5e
DM
3336/* clean up the mess created by Perl_sighandler().
3337 * Note that this is only called during an exit in a signal handler;
3338 * a die is trapped by the call_sv() and the SAVEDESTRUCTOR_X manually
9a7f166c 3339 * skipped over. */
0c4d3b5e 3340
51371543 3341static void
8772537c 3342S_unwind_handler_stack(pTHX_ const void *p)
51371543 3343{
27da23d5 3344 dVAR;
0c4d3b5e 3345 PERL_UNUSED_ARG(p);
7918f24d 3346
0c4d3b5e 3347 PL_savestack_ix -= 5; /* Unprotect save in progress. */
51371543 3348}
1018e26f 3349
66610fdd 3350/*
b3ca2e83
NC
3351=for apidoc magic_sethint
3352
3353Triggered by a store to %^H, records the key/value pair to
c28fe1ec
NC
3354C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3355anything that would need a deep copy. Maybe we should warn if we find a
3356reference.
b3ca2e83
NC
3357
3358=cut
3359*/
3360int
3361Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3362{
3363 dVAR;
ad64d0ec 3364 SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr)
59cd0e26 3365 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
b3ca2e83 3366
7918f24d
NC
3367 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3368
e6e3e454
NC
3369 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3370 an alternative leaf in there, with PL_compiling.cop_hints being used if
3371 it's NULL. If needed for threads, the alternative could lock a mutex,
3372 or take other more complex action. */
3373
5b9c0671
NC
3374 /* Something changed in %^H, so it will need to be restored on scope exit.
3375 Doing this here saves a lot of doing it manually in perl code (and
3376 forgetting to do it, and consequent subtle errors. */
3377 PL_hints |= HINT_LOCALIZE_HH;
20439bc7
Z
3378 CopHINTHASH_set(&PL_compiling,
3379 cophh_store_sv(CopHINTHASH_get(&PL_compiling), key, 0, sv, 0));
b3ca2e83
NC
3380 return 0;
3381}
3382
3383/*
f175cff5 3384=for apidoc magic_clearhint
b3ca2e83 3385
c28fe1ec
NC
3386Triggered by a delete from %^H, records the key to
3387C<PL_compiling.cop_hints_hash>.
b3ca2e83
NC
3388
3389=cut
3390*/
3391int
3392Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3393{
3394 dVAR;
7918f24d
NC
3395
3396 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
f5a63d97
AL
3397 PERL_UNUSED_ARG(sv);
3398
5b9c0671 3399 PL_hints |= HINT_LOCALIZE_HH;
20439bc7 3400 CopHINTHASH_set(&PL_compiling,
e3352591
FC
3401 mg->mg_len == HEf_SVKEY
3402 ? cophh_delete_sv(CopHINTHASH_get(&PL_compiling),
3403 MUTABLE_SV(mg->mg_ptr), 0, 0)
3404 : cophh_delete_pvn(CopHINTHASH_get(&PL_compiling),
3405 mg->mg_ptr, mg->mg_len, 0, 0));
b3ca2e83
NC
3406 return 0;
3407}
3408
3409/*
f747ebd6
Z
3410=for apidoc magic_clearhints
3411
3412Triggered by clearing %^H, resets C<PL_compiling.cop_hints_hash>.
3413
3414=cut
3415*/
3416int
3417Perl_magic_clearhints(pTHX_ SV *sv, MAGIC *mg)
3418{
3419 PERL_ARGS_ASSERT_MAGIC_CLEARHINTS;
3420 PERL_UNUSED_ARG(sv);
3421 PERL_UNUSED_ARG(mg);
20439bc7
Z
3422 cophh_free(CopHINTHASH_get(&PL_compiling));
3423 CopHINTHASH_set(&PL_compiling, cophh_new_empty());
f747ebd6
Z
3424 return 0;
3425}
3426
09fb282d
FC
3427int
3428Perl_magic_copycallchecker(pTHX_ SV *sv, MAGIC *mg, SV *nsv,
3429 const char *name, I32 namlen)
3430{
3431 MAGIC *nmg;
3432
3433 PERL_ARGS_ASSERT_MAGIC_COPYCALLCHECKER;
4f8dbb2d 3434 PERL_UNUSED_ARG(sv);
09fb282d
FC
3435 PERL_UNUSED_ARG(name);
3436 PERL_UNUSED_ARG(namlen);
3437
3438 sv_magic(nsv, &PL_sv_undef, mg->mg_type, NULL, 0);
3439 nmg = mg_find(nsv, mg->mg_type);
316ebaf2 3440 assert(nmg);
09fb282d
FC
3441 if (nmg->mg_flags & MGf_REFCOUNTED) SvREFCNT_dec(nmg->mg_obj);
3442 nmg->mg_ptr = mg->mg_ptr;
3443 nmg->mg_obj = SvREFCNT_inc_simple(mg->mg_obj);
3444 nmg->mg_flags |= MGf_REFCOUNTED;
3445 return 1;
3446}
3447
f747ebd6 3448/*
66610fdd
RGS
3449 * Local variables:
3450 * c-indentation-style: bsd
3451 * c-basic-offset: 4
14d04a33 3452 * indent-tabs-mode: nil
66610fdd
RGS
3453 * End:
3454 *
14d04a33 3455 * ex: set ts=8 sts=4 sw=4 et:
37442d52 3456 */