This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a few nits in perl593delta
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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/*
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."
79072805
LW
14 */
15
ccfc67b7
JH
16/*
17=head1 Magical Functions
166f8a29
DM
18
19"Magic" is special data attached to SV structures in order to give them
20"magical" properties. When any Perl code tries to read from, or assign to,
21an SV marked as magical, it calls the 'get' or 'set' function associated
22with that SV's magic. A get is called prior to reading an SV, in order to
ddfa107c 23give it a chance to update its internal value (get on $. writes the line
166f8a29
DM
24number of the last read filehandle into to the SV's IV slot), while
25set is called after an SV has been written to, in order to allow it to make
ddfa107c 26use of its changed value (set on $/ copies the SV's new value to the
166f8a29
DM
27PL_rs global variable).
28
29Magic is implemented as a linked list of MAGIC structures attached to the
30SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31of functions that implement the get(), set(), length() etc functions,
32plus space for some flags and pointers. For example, a tied variable has
33a MAGIC structure that contains a pointer to the object associated with the
34tie.
35
ccfc67b7
JH
36*/
37
79072805 38#include "EXTERN.h"
864dbfa3 39#define PERL_IN_MG_C
79072805
LW
40#include "perl.h"
41
5cd24f17 42#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
b7953727
JH
43# ifdef I_GRP
44# include <grp.h>
45# endif
188ea221
CS
46#endif
47
757f63d8
SP
48#if defined(HAS_SETGROUPS)
49# ifndef NGROUPS
50# define NGROUPS 32
51# endif
52#endif
53
17aa7f3d
JH
54#ifdef __hpux
55# include <sys/pstat.h>
56#endif
57
8aad04aa
JH
58#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
59Signal_t Perl_csighandler(int sig, ...);
60#else
e69880a5 61Signal_t Perl_csighandler(int sig);
8aad04aa 62#endif
e69880a5 63
9cffb111
OS
64#ifdef __Lynx__
65/* Missing protos on LynxOS */
66void setruid(uid_t id);
67void seteuid(uid_t id);
68void setrgid(uid_t id);
69void setegid(uid_t id);
70#endif
71
c07a80fd
PP
72/*
73 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
74 */
75
76struct magic_state {
77 SV* mgs_sv;
78 U32 mgs_flags;
455ece5e 79 I32 mgs_ss_ix;
c07a80fd 80};
455ece5e 81/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
82
83STATIC void
8fb26106 84S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
c07a80fd 85{
97aff369 86 dVAR;
455ece5e 87 MGS* mgs;
c07a80fd 88 assert(SvMAGICAL(sv));
d8b2590f
NC
89 /* Turning READONLY off for a copy-on-write scalar (including shared
90 hash keys) is a bad idea. */
765f542d 91 if (SvIsCOW(sv))
9a265e59 92 sv_force_normal_flags(sv, 0);
c07a80fd 93
8772537c 94 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
95
96 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd
PP
97 mgs->mgs_sv = sv;
98 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 99 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd
PP
100
101 SvMAGICAL_off(sv);
102 SvREADONLY_off(sv);
06759ea0 103 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
104}
105
954c1994
GS
106/*
107=for apidoc mg_magical
108
109Turns on the magical status of an SV. See C<sv_magic>.
110
111=cut
112*/
113
8990e307 114void
864dbfa3 115Perl_mg_magical(pTHX_ SV *sv)
8990e307 116{
e1ec3a88 117 const MAGIC* mg;
8990e307 118 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 119 const MGVTBL* const vtbl = mg->mg_virtual;
8990e307 120 if (vtbl) {
2b260de0 121 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
122 SvGMAGICAL_on(sv);
123 if (vtbl->svt_set)
124 SvSMAGICAL_on(sv);
2b260de0 125 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307
LW
126 SvRMAGICAL_on(sv);
127 }
128 }
129}
130
954c1994
GS
131/*
132=for apidoc mg_get
133
134Do magic after a value is retrieved from the SV. See C<sv_magic>.
135
136=cut
137*/
138
79072805 139int
864dbfa3 140Perl_mg_get(pTHX_ SV *sv)
79072805 141{
97aff369 142 dVAR;
35a4481c 143 const I32 mgs_ix = SSNEW(sizeof(MGS));
fe2774ed 144 const bool was_temp = (bool)SvTEMP(sv);
0723351e 145 int have_new = 0;
ff76feab 146 MAGIC *newmg, *head, *cur, *mg;
20135930 147 /* guard against sv having being freed midway by holding a private
6683b158
NC
148 reference. */
149
150 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
151 cause the SV's buffer to get stolen (and maybe other stuff).
152 So restore it.
153 */
154 sv_2mortal(SvREFCNT_inc(sv));
155 if (!was_temp) {
156 SvTEMP_off(sv);
157 }
158
455ece5e 159 save_magic(mgs_ix, sv);
463ee0b2 160
ff76feab
AMS
161 /* We must call svt_get(sv, mg) for each valid entry in the linked
162 list of magic. svt_get() may delete the current entry, add new
163 magic to the head of the list, or upgrade the SV. AMS 20010810 */
164
165 newmg = cur = head = mg = SvMAGIC(sv);
166 while (mg) {
35a4481c 167 const MGVTBL * const vtbl = mg->mg_virtual;
ff76feab 168
2b260de0 169 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 170 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
b77f7d40 171
58f82c5c
DM
172 /* guard against magic having been deleted - eg FETCH calling
173 * untie */
174 if (!SvMAGIC(sv))
175 break;
b77f7d40 176
ff76feab
AMS
177 /* Don't restore the flags for this entry if it was deleted. */
178 if (mg->mg_flags & MGf_GSKIP)
179 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
a0d0e21e 180 }
ff76feab
AMS
181
182 mg = mg->mg_moremagic;
183
0723351e 184 if (have_new) {
ff76feab
AMS
185 /* Have we finished with the new entries we saw? Start again
186 where we left off (unless there are more new entries). */
187 if (mg == head) {
0723351e 188 have_new = 0;
ff76feab
AMS
189 mg = cur;
190 head = newmg;
191 }
192 }
193
194 /* Were any new entries added? */
0723351e
NC
195 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
196 have_new = 1;
ff76feab
AMS
197 cur = mg;
198 mg = newmg;
760ac839 199 }
79072805 200 }
463ee0b2 201
8772537c 202 restore_magic(INT2PTR(void *, (IV)mgs_ix));
6683b158
NC
203
204 if (SvREFCNT(sv) == 1) {
205 /* We hold the last reference to this SV, which implies that the
206 SV was deleted as a side effect of the routines we called. */
0c34ef67 207 SvOK_off(sv);
6683b158 208 }
79072805
LW
209 return 0;
210}
211
954c1994
GS
212/*
213=for apidoc mg_set
214
215Do magic after a value is assigned to the SV. See C<sv_magic>.
216
217=cut
218*/
219
79072805 220int
864dbfa3 221Perl_mg_set(pTHX_ SV *sv)
79072805 222{
97aff369 223 dVAR;
35a4481c 224 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 225 MAGIC* mg;
463ee0b2
LW
226 MAGIC* nextmg;
227
455ece5e 228 save_magic(mgs_ix, sv);
463ee0b2
LW
229
230 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
e1ec3a88 231 const MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 232 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
233 if (mg->mg_flags & MGf_GSKIP) {
234 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 235 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 236 }
2b260de0 237 if (vtbl && vtbl->svt_set)
fc0dc3b3 238 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 239 }
463ee0b2 240
8772537c 241 restore_magic(INT2PTR(void*, (IV)mgs_ix));
79072805
LW
242 return 0;
243}
244
954c1994
GS
245/*
246=for apidoc mg_length
247
248Report on the SV's length. See C<sv_magic>.
249
250=cut
251*/
252
79072805 253U32
864dbfa3 254Perl_mg_length(pTHX_ SV *sv)
79072805 255{
97aff369 256 dVAR;
79072805 257 MAGIC* mg;
463ee0b2 258 STRLEN len;
463ee0b2 259
79072805 260 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 261 const MGVTBL * const vtbl = mg->mg_virtual;
2b260de0 262 if (vtbl && vtbl->svt_len) {
35a4481c 263 const I32 mgs_ix = SSNEW(sizeof(MGS));
455ece5e 264 save_magic(mgs_ix, sv);
a0d0e21e 265 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 266 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
8772537c 267 restore_magic(INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
268 return len;
269 }
270 }
271
35a4481c 272 if (DO_UTF8(sv)) {
10516c54 273 const U8 *s = (U8*)SvPV_const(sv, len);
5636d518
DB
274 len = Perl_utf8_length(aTHX_ s, s + len);
275 }
276 else
10516c54 277 (void)SvPV_const(sv, len);
463ee0b2 278 return len;
79072805
LW
279}
280
8fb26106 281I32
864dbfa3 282Perl_mg_size(pTHX_ SV *sv)
93965878
NIS
283{
284 MAGIC* mg;
ac27b0f5 285
93965878 286 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 287 const MGVTBL* const vtbl = mg->mg_virtual;
2b260de0 288 if (vtbl && vtbl->svt_len) {
35a4481c
AL
289 const I32 mgs_ix = SSNEW(sizeof(MGS));
290 I32 len;
455ece5e 291 save_magic(mgs_ix, sv);
93965878 292 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 293 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
8772537c 294 restore_magic(INT2PTR(void*, (IV)mgs_ix));
93965878
NIS
295 return len;
296 }
297 }
298
299 switch(SvTYPE(sv)) {
300 case SVt_PVAV:
35a4481c 301 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
93965878
NIS
302 case SVt_PVHV:
303 /* FIXME */
304 default:
cea2e8a9 305 Perl_croak(aTHX_ "Size magic not implemented");
93965878
NIS
306 break;
307 }
308 return 0;
309}
310
954c1994
GS
311/*
312=for apidoc mg_clear
313
314Clear something magical that the SV represents. See C<sv_magic>.
315
316=cut
317*/
318
79072805 319int
864dbfa3 320Perl_mg_clear(pTHX_ SV *sv)
79072805 321{
35a4481c 322 const I32 mgs_ix = SSNEW(sizeof(MGS));
79072805 323 MAGIC* mg;
463ee0b2 324
455ece5e 325 save_magic(mgs_ix, sv);
463ee0b2 326
79072805 327 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 328 const MGVTBL* const vtbl = mg->mg_virtual;
a0d0e21e 329 /* omit GSKIP -- never set here */
727405f8 330
2b260de0 331 if (vtbl && vtbl->svt_clear)
fc0dc3b3 332 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 333 }
463ee0b2 334
8772537c 335 restore_magic(INT2PTR(void*, (IV)mgs_ix));
79072805
LW
336 return 0;
337}
338
954c1994
GS
339/*
340=for apidoc mg_find
341
342Finds the magic pointer for type matching the SV. See C<sv_magic>.
343
344=cut
345*/
346
93a17b20 347MAGIC*
35a4481c 348Perl_mg_find(pTHX_ const SV *sv, int type)
93a17b20 349{
35a4481c
AL
350 if (sv) {
351 MAGIC *mg;
352 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
353 if (mg->mg_type == type)
354 return mg;
355 }
93a17b20
LW
356 }
357 return 0;
358}
359
954c1994
GS
360/*
361=for apidoc mg_copy
362
363Copies the magic from one SV to another. See C<sv_magic>.
364
365=cut
366*/
367
79072805 368int
864dbfa3 369Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 370{
463ee0b2 371 int count = 0;
79072805 372 MAGIC* mg;
463ee0b2 373 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
35a4481c 374 const MGVTBL* const vtbl = mg->mg_virtual;
68795e93
NIS
375 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
376 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
377 }
823a54a3
AL
378 else {
379 const char type = mg->mg_type;
380 if (isUPPER(type)) {
381 sv_magic(nsv,
382 (type == PERL_MAGIC_tied)
383 ? SvTIED_obj(sv, mg)
384 : (type == PERL_MAGIC_regdata && mg->mg_obj)
385 ? sv
386 : mg->mg_obj,
387 toLOWER(type), key, klen);
388 count++;
389 }
79072805 390 }
79072805 391 }
463ee0b2 392 return count;
79072805
LW
393}
394
954c1994 395/*
0cbee0a4
DM
396=for apidoc mg_localize
397
398Copy some of the magic from an existing SV to new localized version of
399that SV. Container magic (eg %ENV, $1, tie) gets copied, value magic
400doesn't (eg taint, pos).
401
402=cut
403*/
404
405void
406Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
407{
97aff369 408 dVAR;
0cbee0a4
DM
409 MAGIC *mg;
410 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
92e67595 411 MGVTBL* const vtbl = mg->mg_virtual;
0cbee0a4
DM
412 switch (mg->mg_type) {
413 /* value magic types: don't copy */
414 case PERL_MAGIC_bm:
415 case PERL_MAGIC_fm:
416 case PERL_MAGIC_regex_global:
417 case PERL_MAGIC_nkeys:
418#ifdef USE_LOCALE_COLLATE
419 case PERL_MAGIC_collxfrm:
420#endif
421 case PERL_MAGIC_qr:
422 case PERL_MAGIC_taint:
423 case PERL_MAGIC_vec:
424 case PERL_MAGIC_vstring:
425 case PERL_MAGIC_utf8:
426 case PERL_MAGIC_substr:
427 case PERL_MAGIC_defelem:
428 case PERL_MAGIC_arylen:
429 case PERL_MAGIC_pos:
430 case PERL_MAGIC_backref:
431 case PERL_MAGIC_arylen_p:
432 case PERL_MAGIC_rhash:
433 case PERL_MAGIC_symtab:
434 continue;
435 }
436
a5063e7c
DM
437 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
438 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
439 else
0cbee0a4
DM
440 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
441 mg->mg_ptr, mg->mg_len);
a5063e7c 442
0cbee0a4
DM
443 /* container types should remain read-only across localization */
444 SvFLAGS(nsv) |= SvREADONLY(sv);
445 }
446
447 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
448 SvFLAGS(nsv) |= SvMAGICAL(sv);
449 PL_localizing = 1;
450 SvSETMAGIC(nsv);
451 PL_localizing = 0;
452 }
453}
454
455/*
954c1994
GS
456=for apidoc mg_free
457
458Free any magic storage used by the SV. See C<sv_magic>.
459
460=cut
461*/
462
79072805 463int
864dbfa3 464Perl_mg_free(pTHX_ SV *sv)
79072805
LW
465{
466 MAGIC* mg;
467 MAGIC* moremagic;
468 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
35a4481c 469 const MGVTBL* const vtbl = mg->mg_virtual;
79072805 470 moremagic = mg->mg_moremagic;
2b260de0 471 if (vtbl && vtbl->svt_free)
fc0dc3b3 472 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 473 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
979acdb5 474 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
88e89b8a 475 Safefree(mg->mg_ptr);
565764a8 476 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 477 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 478 }
b881518d
JH
479 if (mg->mg_flags & MGf_REFCOUNTED)
480 SvREFCNT_dec(mg->mg_obj);
79072805
LW
481 Safefree(mg);
482 }
b162af07 483 SvMAGIC_set(sv, NULL);
79072805
LW
484 return 0;
485}
486
79072805 487#include <signal.h>
79072805 488
942e002e 489U32
864dbfa3 490Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 491{
97aff369 492 dVAR;
8772537c 493 PERL_UNUSED_ARG(sv);
6cef1e77 494
0bd48802
AL
495 if (PL_curpm) {
496 register const REGEXP * const rx = PM_GETRE(PL_curpm);
497 if (rx) {
498 return mg->mg_obj
499 ? rx->nparens /* @+ */
500 : rx->lastparen; /* @- */
501 }
8f580fb8 502 }
ac27b0f5 503
942e002e 504 return (U32)-1;
6cef1e77
IZ
505}
506
507int
864dbfa3 508Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 509{
97aff369 510 dVAR;
0bd48802
AL
511 if (PL_curpm) {
512 register const REGEXP * const rx = PM_GETRE(PL_curpm);
513 if (rx) {
514 register const I32 paren = mg->mg_len;
515 register I32 s;
516 register I32 t;
517 if (paren < 0)
518 return 0;
519 if (paren <= (I32)rx->nparens &&
520 (s = rx->startp[paren]) != -1 &&
521 (t = rx->endp[paren]) != -1)
522 {
523 register I32 i;
524 if (mg->mg_obj) /* @+ */
525 i = t;
526 else /* @- */
527 i = s;
528
529 if (i > 0 && RX_MATCH_UTF8(rx)) {
530 const char * const b = rx->subbeg;
531 if (b)
532 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
533 }
727405f8 534
0bd48802 535 sv_setiv(sv, i);
1aa99e6b 536 }
0bd48802 537 }
6cef1e77
IZ
538 }
539 return 0;
540}
541
e4b89193 542int
a29d06ed
MG
543Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
544{
8772537c 545 PERL_UNUSED_ARG(sv); PERL_UNUSED_ARG(mg);
a29d06ed 546 Perl_croak(aTHX_ PL_no_modify);
0dbb1585 547 NORETURN_FUNCTION_END;
a29d06ed
MG
548}
549
93a17b20 550U32
864dbfa3 551Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20 552{
97aff369 553 dVAR;
93a17b20 554 register I32 paren;
93a17b20 555 register I32 i;
dd374669 556 register const REGEXP *rx;
a197cbdd 557 I32 s1, t1;
93a17b20
LW
558
559 switch (*mg->mg_ptr) {
560 case '1': case '2': case '3': case '4':
561 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 562 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 563
ffc61ed2 564 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 565 getparen:
eb160463 566 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
567 (s1 = rx->startp[paren]) != -1 &&
568 (t1 = rx->endp[paren]) != -1)
bbce6d69 569 {
cf93c79d 570 i = t1 - s1;
a197cbdd 571 getlen:
a30b2f1f 572 if (i > 0 && RX_MATCH_UTF8(rx)) {
a28509cc 573 const char * const s = rx->subbeg + s1;
768c67ee
JH
574 const U8 *ep;
575 STRLEN el;
ffc61ed2 576
6d5fa195 577 i = t1 - s1;
768c67ee
JH
578 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
579 i = el;
a197cbdd 580 }
ffc61ed2 581 if (i < 0)
0844c848 582 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 583 return i;
93a17b20 584 }
235bddc8
NIS
585 else {
586 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 587 report_uninit(sv);
235bddc8
NIS
588 }
589 }
590 else {
591 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 592 report_uninit(sv);
93a17b20 593 }
748a9306 594 return 0;
93a17b20 595 case '+':
aaa362c4 596 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 597 paren = rx->lastparen;
13f57bf8
CS
598 if (paren)
599 goto getparen;
93a17b20 600 }
748a9306 601 return 0;
a01268b5
JH
602 case '\016': /* ^N */
603 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
604 paren = rx->lastcloseparen;
605 if (paren)
606 goto getparen;
607 }
608 return 0;
93a17b20 609 case '`':
aaa362c4 610 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
611 if (rx->startp[0] != -1) {
612 i = rx->startp[0];
a197cbdd
GS
613 if (i > 0) {
614 s1 = 0;
615 t1 = i;
616 goto getlen;
617 }
93a17b20 618 }
93a17b20 619 }
748a9306 620 return 0;
93a17b20 621 case '\'':
aaa362c4 622 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
623 if (rx->endp[0] != -1) {
624 i = rx->sublen - rx->endp[0];
a197cbdd
GS
625 if (i > 0) {
626 s1 = rx->endp[0];
627 t1 = rx->sublen;
628 goto getlen;
629 }
93a17b20 630 }
93a17b20 631 }
748a9306 632 return 0;
93a17b20
LW
633 }
634 magic_get(sv,mg);
2d8e6c8d 635 if (!SvPOK(sv) && SvNIOK(sv)) {
8b6b16e7 636 sv_2pv(sv, 0);
2d8e6c8d 637 }
93a17b20
LW
638 if (SvPOK(sv))
639 return SvCUR(sv);
640 return 0;
641}
642
ad3296c6
SH
643#define SvRTRIM(sv) STMT_START { \
644 STRLEN len = SvCUR(sv); \
6136c704
AL
645 char * const p = SvPVX(sv); \
646 while (len > 0 && isSPACE(p[len-1])) \
ad3296c6
SH
647 --len; \
648 SvCUR_set(sv, len); \
bb4058bb 649 p[len] = '\0'; \
ad3296c6
SH
650} STMT_END
651
79072805 652int
864dbfa3 653Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805 654{
27da23d5 655 dVAR;
79072805 656 register I32 paren;
35272f84 657 register char *s = NULL;
79072805 658 register I32 i;
d9f97599 659 register REGEXP *rx;
823a54a3
AL
660 const char * const remaining = mg->mg_ptr + 1;
661 const char nextchar = *remaining;
79072805
LW
662
663 switch (*mg->mg_ptr) {
748a9306 664 case '\001': /* ^A */
3280af22 665 sv_setsv(sv, PL_bodytarget);
748a9306 666 break;
e5218da5 667 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
823a54a3 668 if (nextchar == '\0') {
e5218da5
GA
669 sv_setiv(sv, (IV)PL_minus_c);
670 }
823a54a3 671 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
e5218da5
GA
672 sv_setiv(sv, (IV)STATUS_NATIVE);
673 }
49460fe6
NIS
674 break;
675
79072805 676 case '\004': /* ^D */
aea4f609 677 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 678 break;
28f23441 679 case '\005': /* ^E */
823a54a3 680 if (nextchar == '\0') {
cd39f2b6 681#ifdef MACOS_TRADITIONAL
0a378802
JH
682 {
683 char msg[256];
727405f8 684
0a378802 685 sv_setnv(sv,(double)gMacPerl_OSErr);
727405f8 686 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
0a378802 687 }
727405f8 688#else
28f23441 689#ifdef VMS
0a378802
JH
690 {
691# include <descrip.h>
692# include <starlet.h>
693 char msg[255];
694 $DESCRIPTOR(msgdsc,msg);
695 sv_setnv(sv,(NV) vaxc$errno);
696 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
697 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
698 else
c69006e4 699 sv_setpvn(sv,"",0);
0a378802 700 }
28f23441 701#else
88e89b8a 702#ifdef OS2
0a378802
JH
703 if (!(_emx_env & 0x200)) { /* Under DOS */
704 sv_setnv(sv, (NV)errno);
705 sv_setpv(sv, errno ? Strerror(errno) : "");
706 } else {
707 if (errno != errno_isOS2) {
823a54a3 708 const int tmp = _syserrno();
0a378802
JH
709 if (tmp) /* 2nd call to _syserrno() makes it 0 */
710 Perl_rc = tmp;
711 }
712 sv_setnv(sv, (NV)Perl_rc);
713 sv_setpv(sv, os2error(Perl_rc));
714 }
88e89b8a 715#else
22fae026 716#ifdef WIN32
0a378802
JH
717 {
718 DWORD dwErr = GetLastError();
719 sv_setnv(sv, (NV)dwErr);
823a54a3 720 if (dwErr) {
0a378802
JH
721 PerlProc_GetOSError(sv, dwErr);
722 }
723 else
c69006e4 724 sv_setpvn(sv, "", 0);
0a378802
JH
725 SetLastError(dwErr);
726 }
22fae026 727#else
f6c8f21d 728 {
8772537c 729 const int saveerrno = errno;
f6c8f21d
RGS
730 sv_setnv(sv, (NV)errno);
731 sv_setpv(sv, errno ? Strerror(errno) : "");
732 errno = saveerrno;
733 }
28f23441 734#endif
88e89b8a 735#endif
22fae026 736#endif
cd39f2b6 737#endif
ad3296c6 738 SvRTRIM(sv);
0a378802
JH
739 SvNOK_on(sv); /* what a wonderful hack! */
740 }
823a54a3 741 else if (strEQ(remaining, "NCODING"))
0a378802
JH
742 sv_setsv(sv, PL_encoding);
743 break;
79072805 744 case '\006': /* ^F */
3280af22 745 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 746 break;
a0d0e21e 747 case '\010': /* ^H */
3280af22 748 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 749 break;
9d116dd7 750 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
751 if (PL_inplace)
752 sv_setpv(sv, PL_inplace);
79072805 753 else
3280af22 754 sv_setsv(sv, &PL_sv_undef);
79072805 755 break;
ac27b0f5 756 case '\017': /* ^O & ^OPEN */
823a54a3 757 if (nextchar == '\0') {
ac27b0f5 758 sv_setpv(sv, PL_osname);
3511154c
DM
759 SvTAINTED_off(sv);
760 }
823a54a3 761 else if (strEQ(remaining, "PEN")) {
ac27b0f5
NIS
762 if (!PL_compiling.cop_io)
763 sv_setsv(sv, &PL_sv_undef);
764 else {
765 sv_setsv(sv, PL_compiling.cop_io);
766 }
767 }
28f23441 768 break;
79072805 769 case '\020': /* ^P */
3280af22 770 sv_setiv(sv, (IV)PL_perldb);
79072805 771 break;
fb73857a 772 case '\023': /* ^S */
823a54a3 773 if (nextchar == '\0') {
3280af22 774 if (PL_lex_state != LEX_NOTPARSING)
0c34ef67 775 SvOK_off(sv);
3280af22 776 else if (PL_in_eval)
6dc8a9e4 777 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
778 else
779 sv_setiv(sv, 0);
d58bf5aa 780 }
fb73857a 781 break;
79072805 782 case '\024': /* ^T */
823a54a3 783 if (nextchar == '\0') {
88e89b8a 784#ifdef BIG_TIME
7c36658b 785 sv_setnv(sv, PL_basetime);
88e89b8a 786#else
7c36658b 787 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 788#endif
7c36658b 789 }
823a54a3 790 else if (strEQ(remaining, "AINT"))
9aa05f58
RGS
791 sv_setiv(sv, PL_tainting
792 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
793 : 0);
7c36658b 794 break;
7cebcbc0 795 case '\025': /* $^UNICODE, $^UTF8LOCALE */
823a54a3 796 if (strEQ(remaining, "NICODE"))
a05d7ebb 797 sv_setuv(sv, (UV) PL_unicode);
823a54a3 798 else if (strEQ(remaining, "TF8LOCALE"))
7cebcbc0 799 sv_setuv(sv, (UV) PL_utf8locale);
fde18df1
JH
800 break;
801 case '\027': /* ^W & $^WARNING_BITS */
823a54a3 802 if (nextchar == '\0')
4438c4b7 803 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
823a54a3 804 else if (strEQ(remaining, "ARNING_BITS")) {
013b78e8 805 if (PL_compiling.cop_warnings == pWARN_NONE) {
4438c4b7 806 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
013b78e8
RGS
807 }
808 else if (PL_compiling.cop_warnings == pWARN_STD) {
809 sv_setpvn(
810 sv,
811 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
812 WARNsize
813 );
814 }
d3a7d8c7 815 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca
RGS
816 /* Get the bit mask for $warnings::Bits{all}, because
817 * it could have been extended by warnings::register */
818 SV **bits_all;
823a54a3 819 HV * const bits=get_hv("warnings::Bits", FALSE);
017a3ce5 820 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
821 sv_setsv(sv, *bits_all);
822 }
823 else {
824 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
825 }
ac27b0f5 826 }
4438c4b7
JH
827 else {
828 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 829 }
d3a7d8c7 830 SvPOK_only(sv);
4438c4b7 831 }
79072805
LW
832 break;
833 case '1': case '2': case '3': case '4':
834 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 835 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
836 I32 s1, t1;
837
a863c7d1
MB
838 /*
839 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
840 * XXX Does the new way break anything?
841 */
ffc61ed2 842 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 843 getparen:
eb160463 844 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
845 (s1 = rx->startp[paren]) != -1 &&
846 (t1 = rx->endp[paren]) != -1)
bbce6d69 847 {
cf93c79d
IZ
848 i = t1 - s1;
849 s = rx->subbeg + s1;
01ec43d0 850 if (!rx->subbeg)
c2e66d9e
GS
851 break;
852
13f57bf8 853 getrx:
748a9306 854 if (i >= 0) {
fabdb6c0 855 const int oldtainted = PL_tainted;
f6ba9920 856 TAINT_NOT;
cf93c79d 857 sv_setpvn(sv, s, i);
f6ba9920 858 PL_tainted = oldtainted;
a30b2f1f 859 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
7e2040f0
GS
860 SvUTF8_on(sv);
861 else
862 SvUTF8_off(sv);
e9814ee1
HS
863 if (PL_tainting) {
864 if (RX_MATCH_TAINTED(rx)) {
823a54a3 865 MAGIC* const mg = SvMAGIC(sv);
e9814ee1
HS
866 MAGIC* mgt;
867 PL_tainted = 1;
b162af07 868 SvMAGIC_set(sv, mg->mg_moremagic);
e9814ee1
HS
869 SvTAINT(sv);
870 if ((mgt = SvMAGIC(sv))) {
871 mg->mg_moremagic = mgt;
b162af07 872 SvMAGIC_set(sv, mg);
e9814ee1
HS
873 }
874 } else
875 SvTAINTED_off(sv);
876 }
748a9306
LW
877 break;
878 }
79072805 879 }
79072805 880 }
3280af22 881 sv_setsv(sv,&PL_sv_undef);
79072805
LW
882 break;
883 case '+':
aaa362c4 884 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 885 paren = rx->lastparen;
a0d0e21e
LW
886 if (paren)
887 goto getparen;
79072805 888 }
3280af22 889 sv_setsv(sv,&PL_sv_undef);
79072805 890 break;
a01268b5
JH
891 case '\016': /* ^N */
892 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
893 paren = rx->lastcloseparen;
894 if (paren)
895 goto getparen;
896 }
897 sv_setsv(sv,&PL_sv_undef);
898 break;
79072805 899 case '`':
aaa362c4 900 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
901 if ((s = rx->subbeg) && rx->startp[0] != -1) {
902 i = rx->startp[0];
13f57bf8 903 goto getrx;
79072805 904 }
79072805 905 }
3280af22 906 sv_setsv(sv,&PL_sv_undef);
79072805
LW
907 break;
908 case '\'':
aaa362c4 909 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
910 if (rx->subbeg && rx->endp[0] != -1) {
911 s = rx->subbeg + rx->endp[0];
912 i = rx->sublen - rx->endp[0];
13f57bf8 913 goto getrx;
79072805 914 }
79072805 915 }
3280af22 916 sv_setsv(sv,&PL_sv_undef);
79072805
LW
917 break;
918 case '.':
3280af22 919 if (GvIO(PL_last_in_gv)) {
357c8808 920 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805 921 }
79072805
LW
922 break;
923 case '?':
809a5acc 924 {
809a5acc 925 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 926#ifdef COMPLEX_STATUS
6b88bc9c
GS
927 LvTARGOFF(sv) = PL_statusvalue;
928 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 929#endif
809a5acc 930 }
79072805
LW
931 break;
932 case '^':
0daa599b
RGS
933 if (GvIOp(PL_defoutgv))
934 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
935 if (s)
936 sv_setpv(sv,s);
937 else {
3280af22 938 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
939 sv_catpv(sv,"_TOP");
940 }
941 break;
942 case '~':
0daa599b
RGS
943 if (GvIOp(PL_defoutgv))
944 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 945 if (!s)
3280af22 946 s = GvENAME(PL_defoutgv);
79072805
LW
947 sv_setpv(sv,s);
948 break;
79072805 949 case '=':
0daa599b
RGS
950 if (GvIOp(PL_defoutgv))
951 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
952 break;
953 case '-':
0daa599b
RGS
954 if (GvIOp(PL_defoutgv))
955 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
956 break;
957 case '%':
0daa599b
RGS
958 if (GvIOp(PL_defoutgv))
959 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 960 break;
79072805
LW
961 case ':':
962 break;
963 case '/':
964 break;
965 case '[':
3280af22 966 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
967 break;
968 case '|':
0daa599b
RGS
969 if (GvIOp(PL_defoutgv))
970 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
971 break;
972 case ',':
79072805
LW
973 break;
974 case '\\':
b2ce0fda 975 if (PL_ors_sv)
f28098ff 976 sv_copypv(sv, PL_ors_sv);
79072805 977 break;
79072805 978 case '!':
a5f75d66 979#ifdef VMS
65202027 980 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 981 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 982#else
88e89b8a 983 {
8772537c 984 const int saveerrno = errno;
65202027 985 sv_setnv(sv, (NV)errno);
88e89b8a 986#ifdef OS2
ed344e4f
IZ
987 if (errno == errno_isOS2 || errno == errno_isOS2_set)
988 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 989 else
a5f75d66 990#endif
2304df62 991 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
992 errno = saveerrno;
993 }
994#endif
ad3296c6 995 SvRTRIM(sv);
946ec16e 996 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
997 break;
998 case '<':
3280af22 999 sv_setiv(sv, (IV)PL_uid);
79072805
LW
1000 break;
1001 case '>':
3280af22 1002 sv_setiv(sv, (IV)PL_euid);
79072805
LW
1003 break;
1004 case '(':
3280af22 1005 sv_setiv(sv, (IV)PL_gid);
79072805
LW
1006 goto add_groups;
1007 case ')':
3280af22 1008 sv_setiv(sv, (IV)PL_egid);
79072805 1009 add_groups:
79072805 1010#ifdef HAS_GETGROUPS
79072805 1011 {
57d7c65e 1012 Groups_t *gary = NULL;
fb45abb2 1013 I32 i, num_groups = getgroups(0, gary);
57d7c65e
JC
1014 Newx(gary, num_groups, Groups_t);
1015 num_groups = getgroups(num_groups, gary);
fb45abb2
GA
1016 for (i = 0; i < num_groups; i++)
1017 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
57d7c65e 1018 Safefree(gary);
79072805 1019 }
155aba94 1020 (void)SvIOK_on(sv); /* what a wonderful hack! */
cd70abae 1021#endif
79072805 1022 break;
cd39f2b6 1023#ifndef MACOS_TRADITIONAL
79072805
LW
1024 case '0':
1025 break;
cd39f2b6 1026#endif
79072805 1027 }
a0d0e21e 1028 return 0;
79072805
LW
1029}
1030
1031int
864dbfa3 1032Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1033{
8772537c 1034 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
1035
1036 if (uf && uf->uf_val)
24f81a43 1037 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
1038 return 0;
1039}
1040
1041int
864dbfa3 1042Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 1043{
27da23d5 1044 dVAR;
5aabfad6 1045 STRLEN len, klen;
fabdb6c0
AL
1046 const char *s = SvPV_const(sv,len);
1047 const char * const ptr = MgPV_const(mg,klen);
88e89b8a 1048 my_setenv(ptr, s);
1e422769 1049
a0d0e21e
LW
1050#ifdef DYNAMIC_ENV_FETCH
1051 /* We just undefd an environment var. Is a replacement */
1052 /* waiting in the wings? */
1053 if (!len) {
fabdb6c0
AL
1054 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1055 if (valp)
b83604b4 1056 s = SvPV_const(*valp, len);
a0d0e21e
LW
1057 }
1058#endif
1e422769 1059
39e571d4 1060#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
1061 /* And you'll never guess what the dog had */
1062 /* in its mouth... */
3280af22 1063 if (PL_tainting) {
1e422769
PP
1064 MgTAINTEDDIR_off(mg);
1065#ifdef VMS
5aabfad6 1066 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
b8ffc8df 1067 char pathbuf[256], eltbuf[256], *cp, *elt;
c623ac67 1068 Stat_t sbuf;
1e422769
PP
1069 int i = 0, j = 0;
1070
b8ffc8df
RGS
1071 strncpy(eltbuf, s, 255);
1072 eltbuf[255] = 0;
1073 elt = eltbuf;
1e422769
PP
1074 do { /* DCL$PATH may be a search list */
1075 while (1) { /* as may dev portion of any element */
1076 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1077 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1078 cando_by_name(S_IWUSR,0,elt) ) {
1079 MgTAINTEDDIR_on(mg);
1080 return 0;
1081 }
1082 }
1083 if ((cp = strchr(elt, ':')) != Nullch)
1084 *cp = '\0';
1085 if (my_trnlnm(elt, eltbuf, j++))
1086 elt = eltbuf;
1087 else
1088 break;
1089 }
1090 j = 0;
1091 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1092 }
1093#endif /* VMS */
5aabfad6 1094 if (s && klen == 4 && strEQ(ptr,"PATH")) {
8772537c 1095 const char * const strend = s + len;
463ee0b2
LW
1096
1097 while (s < strend) {
96827780 1098 char tmpbuf[256];
c623ac67 1099 Stat_t st;
5f74f29c 1100 I32 i;
96827780 1101 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 1102 s, strend, ':', &i);
463ee0b2 1103 s++;
96827780
MB
1104 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1105 || *tmpbuf != '/'
c6ed36e1 1106 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1107 MgTAINTEDDIR_on(mg);
1e422769
PP
1108 return 0;
1109 }
463ee0b2 1110 }
79072805
LW
1111 }
1112 }
39e571d4 1113#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1114
79072805
LW
1115 return 0;
1116}
1117
1118int
864dbfa3 1119Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1120{
8772537c 1121 PERL_UNUSED_ARG(sv);
01b8bcb7 1122 my_setenv(MgPV_nolen_const(mg),Nullch);
85e6fe83
LW
1123 return 0;
1124}
1125
88e89b8a 1126int
864dbfa3 1127Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 1128{
97aff369 1129 dVAR;
65e66c80 1130 PERL_UNUSED_ARG(mg);
b0269e46 1131#if defined(VMS)
cea2e8a9 1132 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1133#else
3280af22 1134 if (PL_localizing) {
fb73857a 1135 HE* entry;
b0269e46 1136 my_clearenv();
fb73857a 1137 hv_iterinit((HV*)sv);
155aba94 1138 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1139 I32 keylen;
1140 my_setenv(hv_iterkey(entry, &keylen),
b83604b4 1141 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
fb73857a
PP
1142 }
1143 }
1144#endif
1145 return 0;
1146}
1147
1148int
864dbfa3 1149Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1150{
27da23d5 1151 dVAR;
8772537c
AL
1152 PERL_UNUSED_ARG(sv);
1153 PERL_UNUSED_ARG(mg);
b0269e46
AB
1154#if defined(VMS)
1155 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1156#else
1157 my_clearenv();
1158#endif
3e3baf6d 1159 return 0;
66b1d557
HM
1160}
1161
64ca3a65 1162#ifndef PERL_MICRO
2d4fcd5e
AJ
1163#ifdef HAS_SIGPROCMASK
1164static void
1165restore_sigmask(pTHX_ SV *save_sv)
1166{
0bd48802 1167 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
2d4fcd5e
AJ
1168 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1169}
1170#endif
66b1d557 1171int
864dbfa3 1172Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1173{
97aff369 1174 dVAR;
88e89b8a 1175 /* Are we fetching a signal entry? */
8772537c 1176 const I32 i = whichsig(MgPV_nolen_const(mg));
e02bfb16 1177 if (i > 0) {
22c35a8c
GS
1178 if(PL_psig_ptr[i])
1179 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1180 else {
85b332e2 1181 Sighandler_t sigstate;
2e34cc90 1182 sigstate = rsignal_state(i);
23ada85b 1183#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1184 if (PL_sig_handlers_initted && PL_sig_ignoring[i]) sigstate = SIG_IGN;
2e34cc90
CL
1185#endif
1186#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1187 if (PL_sig_handlers_initted && PL_sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1188#endif
88e89b8a 1189 /* cache state so we don't fetch it again */
8aad04aa 1190 if(sigstate == (Sighandler_t) SIG_IGN)
88e89b8a
PP
1191 sv_setpv(sv,"IGNORE");
1192 else
3280af22 1193 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1194 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1195 SvTEMP_off(sv);
1196 }
1197 }
1198 return 0;
1199}
1200int
864dbfa3 1201Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1202{
2d4fcd5e
AJ
1203 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1204 * refactoring might be in order.
1205 */
27da23d5 1206 dVAR;
8772537c
AL
1207 register const char * const s = MgPV_nolen_const(mg);
1208 PERL_UNUSED_ARG(sv);
2d4fcd5e 1209 if (*s == '_') {
cbbf8932 1210 SV** svp = NULL;
2d4fcd5e
AJ
1211 if (strEQ(s,"__DIE__"))
1212 svp = &PL_diehook;
1213 else if (strEQ(s,"__WARN__"))
1214 svp = &PL_warnhook;
1215 else
1216 Perl_croak(aTHX_ "No such hook: %s", s);
27da23d5 1217 if (svp && *svp) {
8772537c 1218 SV * const to_dec = *svp;
cbbf8932 1219 *svp = NULL;
2d4fcd5e
AJ
1220 SvREFCNT_dec(to_dec);
1221 }
1222 }
1223 else {
2d4fcd5e 1224 /* Are we clearing a signal entry? */
8772537c 1225 const I32 i = whichsig(s);
e02bfb16 1226 if (i > 0) {
2d4fcd5e
AJ
1227#ifdef HAS_SIGPROCMASK
1228 sigset_t set, save;
1229 SV* save_sv;
1230 /* Avoid having the signal arrive at a bad time, if possible. */
1231 sigemptyset(&set);
1232 sigaddset(&set,i);
1233 sigprocmask(SIG_BLOCK, &set, &save);
1234 ENTER;
1235 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1236 SAVEFREESV(save_sv);
1237 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1238#endif
1239 PERL_ASYNC_CHECK();
1240#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1241 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2d4fcd5e
AJ
1242#endif
1243#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1244 PL_sig_defaulting[i] = 1;
5c1546dc 1245 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e 1246#else
8aad04aa 1247 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2d4fcd5e
AJ
1248#endif
1249 if(PL_psig_name[i]) {
1250 SvREFCNT_dec(PL_psig_name[i]);
1251 PL_psig_name[i]=0;
1252 }
1253 if(PL_psig_ptr[i]) {
6136c704 1254 SV * const to_dec=PL_psig_ptr[i];
2d4fcd5e
AJ
1255 PL_psig_ptr[i]=0;
1256 LEAVE;
1257 SvREFCNT_dec(to_dec);
1258 }
1259 else
1260 LEAVE;
1261 }
88e89b8a
PP
1262 }
1263 return 0;
1264}
3d37d572 1265
dd374669
AL
1266static void
1267S_raise_signal(pTHX_ int sig)
0a8e0eff 1268{
97aff369 1269 dVAR;
0a8e0eff
NIS
1270 /* Set a flag to say this signal is pending */
1271 PL_psig_pend[sig]++;
1272 /* And one to say _a_ signal is pending */
1273 PL_sig_pending = 1;
1274}
1275
1276Signal_t
8aad04aa
JH
1277#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1278Perl_csighandler(int sig, ...)
1279#else
0a8e0eff 1280Perl_csighandler(int sig)
8aad04aa 1281#endif
0a8e0eff 1282{
1018e26f
NIS
1283#ifdef PERL_GET_SIG_CONTEXT
1284 dTHXa(PERL_GET_SIG_CONTEXT);
1285#else
85b332e2
CL
1286 dTHX;
1287#endif
23ada85b 1288#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1289 (void) rsignal(sig, PL_csighandlerp);
27da23d5 1290 if (PL_sig_ignoring[sig]) return;
85b332e2 1291#endif
2e34cc90 1292#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1293 if (PL_sig_defaulting[sig])
2e34cc90
CL
1294#ifdef KILL_BY_SIGPRC
1295 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1296#else
1297 exit(1);
1298#endif
1299#endif
4ffa73a3
JH
1300 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1301 /* Call the perl level handler now--
1302 * with risk we may be in malloc() etc. */
1303 (*PL_sighandlerp)(sig);
1304 else
dd374669 1305 S_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1306}
1307
2e34cc90
CL
1308#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1309void
1310Perl_csighandler_init(void)
1311{
1312 int sig;
27da23d5 1313 if (PL_sig_handlers_initted) return;
2e34cc90
CL
1314
1315 for (sig = 1; sig < SIG_SIZE; sig++) {
1316#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1317 dTHX;
27da23d5 1318 PL_sig_defaulting[sig] = 1;
5c1546dc 1319 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1320#endif
1321#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1322 PL_sig_ignoring[sig] = 0;
2e34cc90
CL
1323#endif
1324 }
27da23d5 1325 PL_sig_handlers_initted = 1;
2e34cc90
CL
1326}
1327#endif
1328
0a8e0eff
NIS
1329void
1330Perl_despatch_signals(pTHX)
1331{
97aff369 1332 dVAR;
0a8e0eff
NIS
1333 int sig;
1334 PL_sig_pending = 0;
1335 for (sig = 1; sig < SIG_SIZE; sig++) {
1336 if (PL_psig_pend[sig]) {
25da4428
JH
1337 PERL_BLOCKSIG_ADD(set, sig);
1338 PL_psig_pend[sig] = 0;
1339 PERL_BLOCKSIG_BLOCK(set);
f5203343 1340 (*PL_sighandlerp)(sig);
25da4428 1341 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1342 }
1343 }
1344}
1345
85e6fe83 1346int
864dbfa3 1347Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1348{
27da23d5 1349 dVAR;
79072805 1350 I32 i;
cbbf8932 1351 SV** svp = NULL;
2d4fcd5e
AJ
1352 /* Need to be careful with SvREFCNT_dec(), because that can have side
1353 * effects (due to closures). We must make sure that the new disposition
1354 * is in place before it is called.
1355 */
cbbf8932 1356 SV* to_dec = NULL;
e72dc28c 1357 STRLEN len;
2d4fcd5e
AJ
1358#ifdef HAS_SIGPROCMASK
1359 sigset_t set, save;
1360 SV* save_sv;
1361#endif
a0d0e21e 1362
d5263905 1363 register const char *s = MgPV_const(mg,len);
748a9306
LW
1364 if (*s == '_') {
1365 if (strEQ(s,"__DIE__"))
3280af22 1366 svp = &PL_diehook;
748a9306 1367 else if (strEQ(s,"__WARN__"))
3280af22 1368 svp = &PL_warnhook;
748a9306 1369 else
cea2e8a9 1370 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1371 i = 0;
4633a7c4 1372 if (*svp) {
2d4fcd5e 1373 to_dec = *svp;
cbbf8932 1374 *svp = NULL;
4633a7c4 1375 }
748a9306
LW
1376 }
1377 else {
1378 i = whichsig(s); /* ...no, a brick */
86d86cad 1379 if (i <= 0) {
e476b1b5 1380 if (ckWARN(WARN_SIGNAL))
9014280d 1381 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1382 return 0;
1383 }
2d4fcd5e
AJ
1384#ifdef HAS_SIGPROCMASK
1385 /* Avoid having the signal arrive at a bad time, if possible. */
1386 sigemptyset(&set);
1387 sigaddset(&set,i);
1388 sigprocmask(SIG_BLOCK, &set, &save);
1389 ENTER;
1390 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1391 SAVEFREESV(save_sv);
1392 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1393#endif
1394 PERL_ASYNC_CHECK();
2e34cc90 1395#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
27da23d5 1396 if (!PL_sig_handlers_initted) Perl_csighandler_init();
2e34cc90 1397#endif
23ada85b 1398#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1399 PL_sig_ignoring[i] = 0;
85b332e2 1400#endif
2e34cc90 1401#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
27da23d5 1402 PL_sig_defaulting[i] = 0;
2e34cc90 1403#endif
22c35a8c 1404 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1405 to_dec = PL_psig_ptr[i];
22c35a8c 1406 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1407 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1408 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1409 SvREADONLY_on(PL_psig_name[i]);
748a9306 1410 }
a0d0e21e 1411 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1412 if (i) {
5c1546dc 1413 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1414#ifdef HAS_SIGPROCMASK
1415 LEAVE;
1416#endif
1417 }
748a9306
LW
1418 else
1419 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1420 if(to_dec)
1421 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1422 return 0;
1423 }
e72dc28c 1424 s = SvPV_force(sv,len);
748a9306 1425 if (strEQ(s,"IGNORE")) {
85b332e2 1426 if (i) {
23ada85b 1427#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
27da23d5 1428 PL_sig_ignoring[i] = 1;
5c1546dc 1429 (void)rsignal(i, PL_csighandlerp);
85b332e2 1430#else
8aad04aa 1431 (void)rsignal(i, (Sighandler_t) SIG_IGN);
85b332e2 1432#endif
2d4fcd5e 1433 }
748a9306
LW
1434 }
1435 else if (strEQ(s,"DEFAULT") || !*s) {
1436 if (i)
2e34cc90
CL
1437#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1438 {
27da23d5 1439 PL_sig_defaulting[i] = 1;
5c1546dc 1440 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1441 }
1442#else
8aad04aa 1443 (void)rsignal(i, (Sighandler_t) SIG_DFL);
2e34cc90 1444#endif
748a9306 1445 }
79072805 1446 else {
5aabfad6
PP
1447 /*
1448 * We should warn if HINT_STRICT_REFS, but without
1449 * access to a known hint bit in a known OP, we can't
1450 * tell whether HINT_STRICT_REFS is in force or not.
1451 */
46fc3d4c 1452 if (!strchr(s,':') && !strchr(s,'\''))
89529cee 1453 Perl_sv_insert(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"));
748a9306 1454 if (i)
5c1546dc 1455 (void)rsignal(i, PL_csighandlerp);
748a9306
LW
1456 else
1457 *svp = SvREFCNT_inc(sv);
79072805 1458 }
2d4fcd5e
AJ
1459#ifdef HAS_SIGPROCMASK
1460 if(i)
1461 LEAVE;
1462#endif
1463 if(to_dec)
1464 SvREFCNT_dec(to_dec);
79072805
LW
1465 return 0;
1466}
64ca3a65 1467#endif /* !PERL_MICRO */
79072805
LW
1468
1469int
864dbfa3 1470Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1471{
97aff369 1472 dVAR;
8772537c
AL
1473 PERL_UNUSED_ARG(sv);
1474 PERL_UNUSED_ARG(mg);
3280af22 1475 PL_sub_generation++;
463ee0b2
LW
1476 return 0;
1477}
1478
1479int
864dbfa3 1480Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1481{
97aff369 1482 dVAR;
8772537c
AL
1483 PERL_UNUSED_ARG(sv);
1484 PERL_UNUSED_ARG(mg);
a0d0e21e 1485 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1486 PL_amagic_generation++;
463ee0b2 1487
a0d0e21e
LW
1488 return 0;
1489}
463ee0b2 1490
946ec16e 1491int
864dbfa3 1492Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1493{
dd374669 1494 HV * const hv = (HV*)LvTARG(sv);
6ff81951 1495 I32 i = 0;
8772537c 1496 PERL_UNUSED_ARG(mg);
7719e241 1497
6ff81951 1498 if (hv) {
497b47a8
JH
1499 (void) hv_iterinit(hv);
1500 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1501 i = HvKEYS(hv);
1502 else {
1503 while (hv_iternext(hv))
1504 i++;
1505 }
6ff81951
GS
1506 }
1507
1508 sv_setiv(sv, (IV)i);
1509 return 0;
1510}
1511
1512int
864dbfa3 1513Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1514{
8772537c 1515 PERL_UNUSED_ARG(mg);
946ec16e
PP
1516 if (LvTARG(sv)) {
1517 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1518 }
1519 return 0;
ac27b0f5 1520}
946ec16e 1521
e336de0d 1522/* caller is responsible for stack switching/cleanup */
565764a8 1523STATIC int
e1ec3a88 1524S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
a0d0e21e 1525{
97aff369 1526 dVAR;
a0d0e21e 1527 dSP;
463ee0b2 1528
924508f0
GS
1529 PUSHMARK(SP);
1530 EXTEND(SP, n);
33c27489 1531 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1532 if (n > 1) {
93965878 1533 if (mg->mg_ptr) {
565764a8 1534 if (mg->mg_len >= 0)
79cb57f6 1535 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1536 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1537 PUSHs((SV*)mg->mg_ptr);
1538 }
14befaf4 1539 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1540 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1541 }
1542 }
1543 if (n > 2) {
1544 PUSHs(val);
88e89b8a 1545 }
463ee0b2
LW
1546 PUTBACK;
1547
864dbfa3 1548 return call_method(meth, flags);
946ec16e
PP
1549}
1550
76e3520e 1551STATIC int
e1ec3a88 1552S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
a0d0e21e 1553{
27da23d5 1554 dVAR; dSP;
463ee0b2 1555
a0d0e21e
LW
1556 ENTER;
1557 SAVETMPS;
e788e7d3 1558 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1559
33c27489 1560 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1561 sv_setsv(sv, *PL_stack_sp--);
93965878 1562 }
463ee0b2 1563
d3acc0f7 1564 POPSTACK;
a0d0e21e
LW
1565 FREETMPS;
1566 LEAVE;
1567 return 0;
1568}
463ee0b2 1569
a0d0e21e 1570int
864dbfa3 1571Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1572{
a0d0e21e
LW
1573 if (mg->mg_ptr)
1574 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1575 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1576 return 0;
1577}
1578
1579int
864dbfa3 1580Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 1581{
27da23d5 1582 dVAR; dSP;
a60c0954 1583 ENTER;
e788e7d3 1584 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1585 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1586 POPSTACK;
a60c0954 1587 LEAVE;
463ee0b2
LW
1588 return 0;
1589}
1590
1591int
864dbfa3 1592Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1593{
a0d0e21e
LW
1594 return magic_methpack(sv,mg,"DELETE");
1595}
463ee0b2 1596
93965878
NIS
1597
1598U32
864dbfa3 1599Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1600{
27da23d5 1601 dVAR; dSP;
93965878
NIS
1602 U32 retval = 0;
1603
1604 ENTER;
1605 SAVETMPS;
e788e7d3 1606 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1607 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1608 sv = *PL_stack_sp--;
a60c0954 1609 retval = (U32) SvIV(sv)-1;
93965878 1610 }
d3acc0f7 1611 POPSTACK;
93965878
NIS
1612 FREETMPS;
1613 LEAVE;
1614 return retval;
1615}
1616
cea2e8a9
GS
1617int
1618Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1619{
27da23d5 1620 dVAR; dSP;
463ee0b2 1621
e336de0d 1622 ENTER;
e788e7d3 1623 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1624 PUSHMARK(SP);
33c27489 1625 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1626 PUTBACK;
864dbfa3 1627 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1628 POPSTACK;
a60c0954 1629 LEAVE;
a3bcc51e 1630
463ee0b2
LW
1631 return 0;
1632}
1633
1634int
864dbfa3 1635Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1636{
27da23d5 1637 dVAR; dSP;
35a4481c 1638 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1639
1640 ENTER;
a0d0e21e 1641 SAVETMPS;
e788e7d3 1642 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1643 PUSHMARK(SP);
1644 EXTEND(SP, 2);
33c27489 1645 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1646 if (SvOK(key))
1647 PUSHs(key);
1648 PUTBACK;
1649
864dbfa3 1650 if (call_method(meth, G_SCALAR))
3280af22 1651 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1652
d3acc0f7 1653 POPSTACK;
a0d0e21e
LW
1654 FREETMPS;
1655 LEAVE;
79072805
LW
1656 return 0;
1657}
1658
1659int
864dbfa3 1660Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1661{
1662 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1663}
a0d0e21e 1664
a3bcc51e
TP
1665SV *
1666Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1667{
27da23d5 1668 dVAR; dSP;
a3bcc51e 1669 SV *retval = &PL_sv_undef;
8772537c
AL
1670 SV * const tied = SvTIED_obj((SV*)hv, mg);
1671 HV * const pkg = SvSTASH((SV*)SvRV(tied));
a3bcc51e
TP
1672
1673 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1674 SV *key;
bfcb3514 1675 if (HvEITER_get(hv))
a3bcc51e
TP
1676 /* we are in an iteration so the hash cannot be empty */
1677 return &PL_sv_yes;
1678 /* no xhv_eiter so now use FIRSTKEY */
1679 key = sv_newmortal();
1680 magic_nextpack((SV*)hv, mg, key);
bfcb3514 1681 HvEITER_set(hv, NULL); /* need to reset iterator */
a3bcc51e
TP
1682 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1683 }
1684
1685 /* there is a SCALAR method that we can call */
1686 ENTER;
1687 PUSHSTACKi(PERLSI_MAGIC);
1688 PUSHMARK(SP);
1689 EXTEND(SP, 1);
1690 PUSHs(tied);
1691 PUTBACK;
1692
1693 if (call_method("SCALAR", G_SCALAR))
1694 retval = *PL_stack_sp--;
1695 POPSTACK;
1696 LEAVE;
1697 return retval;
1698}
1699
a0d0e21e 1700int
864dbfa3 1701Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1702{
97aff369 1703 dVAR;
8772537c
AL
1704 GV * const gv = PL_DBline;
1705 const I32 i = SvTRUE(sv);
1706 SV ** const svp = av_fetch(GvAV(gv),
01b8bcb7 1707 atoi(MgPV_nolen_const(mg)), FALSE);
8772537c
AL
1708 if (svp && SvIOKp(*svp)) {
1709 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1710 if (o) {
1711 /* set or clear breakpoint in the relevant control op */
1712 if (i)
1713 o->op_flags |= OPf_SPECIAL;
1714 else
1715 o->op_flags &= ~OPf_SPECIAL;
1716 }
5df8de69 1717 }
79072805
LW
1718 return 0;
1719}
1720
1721int
8772537c 1722Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
79072805 1723{
97aff369 1724 dVAR;
8772537c 1725 const AV * const obj = (AV*)mg->mg_obj;
83bf042f
NC
1726 if (obj) {
1727 sv_setiv(sv, AvFILL(obj) + PL_curcop->cop_arybase);
1728 } else {
1729 SvOK_off(sv);
1730 }
79072805
LW
1731 return 0;
1732}
1733
1734int
864dbfa3 1735Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1736{
97aff369 1737 dVAR;
8772537c 1738 AV * const obj = (AV*)mg->mg_obj;
83bf042f
NC
1739 if (obj) {
1740 av_fill(obj, SvIV(sv) - PL_curcop->cop_arybase);
1741 } else {
1742 if (ckWARN(WARN_MISC))
1743 Perl_warner(aTHX_ packWARN(WARN_MISC),
1744 "Attempt to set length of freed array");
1745 }
1746 return 0;
1747}
1748
1749int
1750Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1751{
97aff369 1752 dVAR;
53c1dcc0 1753 PERL_UNUSED_ARG(sv);
94f3782b
DM
1754 /* during global destruction, mg_obj may already have been freed */
1755 if (PL_in_clean_all)
1ea47f64 1756 return 0;
94f3782b 1757
83bf042f
NC
1758 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1759
1760 if (mg) {
1761 /* arylen scalar holds a pointer back to the array, but doesn't own a
1762 reference. Hence the we (the array) are about to go away with it
1763 still pointing at us. Clear its pointer, else it would be pointing
1764 at free memory. See the comment in sv_magic about reference loops,
1765 and why it can't own a reference to us. */
1766 mg->mg_obj = 0;
1767 }
a0d0e21e
LW
1768 return 0;
1769}
1770
1771int
864dbfa3 1772Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1773{
97aff369 1774 dVAR;
8772537c 1775 SV* const lsv = LvTARG(sv);
ac27b0f5 1776
a0d0e21e 1777 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1778 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1779 if (mg && mg->mg_len >= 0) {
a0ed51b3 1780 I32 i = mg->mg_len;
7e2040f0 1781 if (DO_UTF8(lsv))
a0ed51b3
LW
1782 sv_pos_b2u(lsv, &i);
1783 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1784 return 0;
1785 }
1786 }
0c34ef67 1787 SvOK_off(sv);
a0d0e21e
LW
1788 return 0;
1789}
1790
1791int
864dbfa3 1792Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1793{
97aff369 1794 dVAR;
8772537c 1795 SV* const lsv = LvTARG(sv);
a0d0e21e
LW
1796 SSize_t pos;
1797 STRLEN len;
c00206c8 1798 STRLEN ulen = 0;
a0d0e21e
LW
1799
1800 mg = 0;
ac27b0f5 1801
a0d0e21e 1802 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1803 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1804 if (!mg) {
1805 if (!SvOK(sv))
1806 return 0;
fabdb6c0 1807 sv_magic(lsv, NULL, PERL_MAGIC_regex_global, NULL, 0);
14befaf4 1808 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1809 }
1810 else if (!SvOK(sv)) {
565764a8 1811 mg->mg_len = -1;
a0d0e21e
LW
1812 return 0;
1813 }
1814 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1815
c485e607 1816 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1817
7e2040f0 1818 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1819 ulen = sv_len_utf8(lsv);
1820 if (ulen)
1821 len = ulen;
a0ed51b3
LW
1822 }
1823
a0d0e21e
LW
1824 if (pos < 0) {
1825 pos += len;
1826 if (pos < 0)
1827 pos = 0;
1828 }
eb160463 1829 else if (pos > (SSize_t)len)
a0d0e21e 1830 pos = len;
a0ed51b3
LW
1831
1832 if (ulen) {
1833 I32 p = pos;
1834 sv_pos_u2b(lsv, &p, 0);
1835 pos = p;
1836 }
727405f8 1837
565764a8 1838 mg->mg_len = pos;
71be2cbc 1839 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1840
79072805
LW
1841 return 0;
1842}
1843
1844int
864dbfa3 1845Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1846{
8772537c 1847 PERL_UNUSED_ARG(mg);
8646b087
PP
1848 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1849 SvFAKE_off(sv);
946ec16e 1850 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1851 SvFAKE_on(sv);
1852 }
1853 else
946ec16e 1854 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1855 return 0;
1856}
1857
1858int
864dbfa3 1859Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1860{
79072805 1861 GV* gv;
8772537c
AL
1862 PERL_UNUSED_ARG(mg);
1863
79072805
LW
1864 if (!SvOK(sv))
1865 return 0;
f776e3cd 1866 gv = gv_fetchsv(sv, GV_ADD, SVt_PVGV);
79072805
LW
1867 if (sv == (SV*)gv)
1868 return 0;
1869 if (GvGP(sv))
88e89b8a 1870 gp_free((GV*)sv);
79072805 1871 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1872 return 0;
1873}
1874
1875int
864dbfa3 1876Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1877{
1878 STRLEN len;
35a4481c 1879 SV * const lsv = LvTARG(sv);
b83604b4 1880 const char * const tmps = SvPV_const(lsv,len);
6ff81951
GS
1881 I32 offs = LvTARGOFF(sv);
1882 I32 rem = LvTARGLEN(sv);
8772537c 1883 PERL_UNUSED_ARG(mg);
6ff81951 1884
9aa983d2
JH
1885 if (SvUTF8(lsv))
1886 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1887 if (offs > (I32)len)
6ff81951 1888 offs = len;
eb160463 1889 if (rem + offs > (I32)len)
6ff81951
GS
1890 rem = len - offs;
1891 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1892 if (SvUTF8(lsv))
2ef4b674 1893 SvUTF8_on(sv);
6ff81951
GS
1894 return 0;
1895}
1896
1897int
864dbfa3 1898Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1899{
97aff369 1900 dVAR;
9aa983d2 1901 STRLEN len;
b83604b4 1902 const char *tmps = SvPV_const(sv, len);
dd374669 1903 SV * const lsv = LvTARG(sv);
9aa983d2
JH
1904 I32 lvoff = LvTARGOFF(sv);
1905 I32 lvlen = LvTARGLEN(sv);
8772537c 1906 PERL_UNUSED_ARG(mg);
075a4a2b 1907
1aa99e6b 1908 if (DO_UTF8(sv)) {
9aa983d2
JH
1909 sv_utf8_upgrade(lsv);
1910 sv_pos_u2b(lsv, &lvoff, &lvlen);
1911 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 1912 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
1913 SvUTF8_on(lsv);
1914 }
9bf12eaf 1915 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1916 sv_pos_u2b(lsv, &lvoff, &lvlen);
b76f3ce2 1917 LvTARGLEN(sv) = len;
e95af362 1918 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1919 sv_insert(lsv, lvoff, lvlen, tmps, len);
1920 Safefree(tmps);
1aa99e6b 1921 }
b76f3ce2
GB
1922 else {
1923 sv_insert(lsv, lvoff, lvlen, tmps, len);
1924 LvTARGLEN(sv) = len;
1925 }
1926
1aa99e6b 1927
79072805
LW
1928 return 0;
1929}
1930
1931int
864dbfa3 1932Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1933{
97aff369 1934 dVAR;
8772537c 1935 PERL_UNUSED_ARG(sv);
27cc343c 1936 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
463ee0b2
LW
1937 return 0;
1938}
1939
1940int
864dbfa3 1941Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1942{
97aff369 1943 dVAR;
8772537c 1944 PERL_UNUSED_ARG(sv);
0a9c116b
DM
1945 /* update taint status unless we're restoring at scope exit */
1946 if (PL_localizing != 2) {
1947 if (PL_tainted)
1948 mg->mg_len |= 1;
1949 else
1950 mg->mg_len &= ~1;
1951 }
463ee0b2
LW
1952 return 0;
1953}
1954
1955int
864dbfa3 1956Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1957{
35a4481c 1958 SV * const lsv = LvTARG(sv);
8772537c 1959 PERL_UNUSED_ARG(mg);
6ff81951 1960
6136c704
AL
1961 if (lsv)
1962 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1963 else
0c34ef67 1964 SvOK_off(sv);
6ff81951 1965
6ff81951
GS
1966 return 0;
1967}
1968
1969int
864dbfa3 1970Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 1971{
8772537c 1972 PERL_UNUSED_ARG(mg);
79072805
LW
1973 do_vecset(sv); /* XXX slurp this routine */
1974 return 0;
1975}
1976
1977int
864dbfa3 1978Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1979{
97aff369 1980 dVAR;
71be2cbc 1981 SV *targ = Nullsv;
5f05dabc 1982 if (LvTARGLEN(sv)) {
68dc0745 1983 if (mg->mg_obj) {
8772537c
AL
1984 SV * const ahv = LvTARG(sv);
1985 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
6d822dc4
MS
1986 if (he)
1987 targ = HeVAL(he);
68dc0745
PP
1988 }
1989 else {
8772537c 1990 AV* const av = (AV*)LvTARG(sv);
68dc0745
PP
1991 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1992 targ = AvARRAY(av)[LvTARGOFF(sv)];
1993 }
3280af22 1994 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1995 /* somebody else defined it for us */
1996 SvREFCNT_dec(LvTARG(sv));
1997 LvTARG(sv) = SvREFCNT_inc(targ);
1998 LvTARGLEN(sv) = 0;
1999 SvREFCNT_dec(mg->mg_obj);
2000 mg->mg_obj = Nullsv;
2001 mg->mg_flags &= ~MGf_REFCOUNTED;
2002 }
5f05dabc 2003 }
71be2cbc
PP
2004 else
2005 targ = LvTARG(sv);
3280af22 2006 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
2007 return 0;
2008}
2009
2010int
864dbfa3 2011Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 2012{
8772537c 2013 PERL_UNUSED_ARG(mg);
71be2cbc 2014 if (LvTARGLEN(sv))
68dc0745
PP
2015 vivify_defelem(sv);
2016 if (LvTARG(sv)) {
5f05dabc 2017 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
2018 SvSETMAGIC(LvTARG(sv));
2019 }
5f05dabc
PP
2020 return 0;
2021}
2022
71be2cbc 2023void
864dbfa3 2024Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 2025{
97aff369 2026 dVAR;
74e13ce4
GS
2027 MAGIC *mg;
2028 SV *value = Nullsv;
71be2cbc 2029
14befaf4 2030 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 2031 return;
68dc0745 2032 if (mg->mg_obj) {
8772537c
AL
2033 SV * const ahv = LvTARG(sv);
2034 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
6d822dc4
MS
2035 if (he)
2036 value = HeVAL(he);
3280af22 2037 if (!value || value == &PL_sv_undef)
ce5030a2 2038 Perl_croak(aTHX_ PL_no_helem_sv, mg->mg_obj);
71be2cbc 2039 }
68dc0745 2040 else {
8772537c 2041 AV* const av = (AV*)LvTARG(sv);
5aabfad6 2042 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
2043 LvTARG(sv) = Nullsv; /* array can't be extended */
2044 else {
aec46f14 2045 SV** const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 2046 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 2047 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
2048 }
2049 }
3e3baf6d 2050 (void)SvREFCNT_inc(value);
68dc0745
PP
2051 SvREFCNT_dec(LvTARG(sv));
2052 LvTARG(sv) = value;
71be2cbc 2053 LvTARGLEN(sv) = 0;
68dc0745
PP
2054 SvREFCNT_dec(mg->mg_obj);
2055 mg->mg_obj = Nullsv;
2056 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
2057}
2058
2059int
864dbfa3 2060Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 2061{
86f55936 2062 return Perl_sv_kill_backrefs(aTHX_ sv, (AV*)mg->mg_obj);
810b8aa5
GS
2063}
2064
2065int
864dbfa3 2066Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 2067{
565764a8 2068 mg->mg_len = -1;
c6496cc7 2069 SvSCREAM_off(sv);
93a17b20
LW
2070 return 0;
2071}
2072
2073int
864dbfa3 2074Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 2075{
8772537c 2076 PERL_UNUSED_ARG(mg);
14befaf4 2077 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
2078 SvVALID_off(sv);
2079 return 0;
2080}
2081
2082int
864dbfa3 2083Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 2084{
8772537c 2085 PERL_UNUSED_ARG(mg);
14befaf4 2086 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
2087 SvCOMPILED_off(sv);
2088 return 0;
2089}
2090
2091int
864dbfa3 2092Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 2093{
35a4481c 2094 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
79072805
LW
2095
2096 if (uf && uf->uf_set)
24f81a43 2097 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
2098 return 0;
2099}
2100
c277df42 2101int
faf82a0b
AE
2102Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2103{
8772537c 2104 PERL_UNUSED_ARG(mg);
faf82a0b
AE
2105 sv_unmagic(sv, PERL_MAGIC_qr);
2106 return 0;
2107}
2108
2109int
864dbfa3 2110Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42 2111{
97aff369 2112 dVAR;
8772537c
AL
2113 regexp * const re = (regexp *)mg->mg_obj;
2114 PERL_UNUSED_ARG(sv);
2115
c277df42
IZ
2116 ReREFCNT_dec(re);
2117 return 0;
2118}
2119
7a4c00b4 2120#ifdef USE_LOCALE_COLLATE
79072805 2121int
864dbfa3 2122Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
2123{
2124 /*
838b5b74 2125 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2126 * and vanished with a faint plop.
2127 */
8772537c 2128 PERL_UNUSED_ARG(sv);
7a4c00b4
PP
2129 if (mg->mg_ptr) {
2130 Safefree(mg->mg_ptr);
2131 mg->mg_ptr = NULL;
565764a8 2132 mg->mg_len = -1;
7a4c00b4 2133 }
bbce6d69
PP
2134 return 0;
2135}
7a4c00b4 2136#endif /* USE_LOCALE_COLLATE */
bbce6d69 2137
7e8c5dac
HS
2138/* Just clear the UTF-8 cache data. */
2139int
2140Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2141{
8772537c 2142 PERL_UNUSED_ARG(sv);
7e8c5dac
HS
2143 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2144 mg->mg_ptr = 0;
2145 mg->mg_len = -1; /* The mg_len holds the len cache. */
2146 return 0;
2147}
2148
bbce6d69 2149int
864dbfa3 2150Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 2151{
97aff369 2152 dVAR;
e1ec3a88 2153 register const char *s;
79072805 2154 I32 i;
8990e307 2155 STRLEN len;
79072805 2156 switch (*mg->mg_ptr) {
748a9306 2157 case '\001': /* ^A */
3280af22 2158 sv_setsv(PL_bodytarget, sv);
748a9306 2159 break;
49460fe6 2160 case '\003': /* ^C */
eb160463 2161 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
2162 break;
2163
79072805 2164 case '\004': /* ^D */
b4ab917c 2165#ifdef DEBUGGING
b83604b4 2166 s = SvPV_nolen_const(sv);
ddcf8bc1 2167 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
79072805 2168 DEBUG_x(dump_all());
b4ab917c
DM
2169#else
2170 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2171#endif
79072805 2172 break;
28f23441 2173 case '\005': /* ^E */
d0063567 2174 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2175#ifdef MACOS_TRADITIONAL
d0063567 2176 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2177#else
cd39f2b6 2178# ifdef VMS
d0063567 2179 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2180# else
cd39f2b6 2181# ifdef WIN32
d0063567 2182 SetLastError( SvIV(sv) );
cd39f2b6 2183# else
9fed8b87 2184# ifdef OS2
d0063567 2185 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2186# else
d0063567
DK
2187 /* will anyone ever use this? */
2188 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2189# endif
048c1ddf
IZ
2190# endif
2191# endif
22fae026 2192#endif
d0063567
DK
2193 }
2194 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2195 if (PL_encoding)
2196 SvREFCNT_dec(PL_encoding);
2197 if (SvOK(sv) || SvGMAGICAL(sv)) {
2198 PL_encoding = newSVsv(sv);
2199 }
2200 else {
2201 PL_encoding = Nullsv;
2202 }
2203 }
2204 break;
79072805 2205 case '\006': /* ^F */
3280af22 2206 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2207 break;
a0d0e21e 2208 case '\010': /* ^H */
3280af22 2209 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2210 break;
9d116dd7 2211 case '\011': /* ^I */ /* NOT \t in EBCDIC */
43c5f42d
NC
2212 Safefree(PL_inplace);
2213 PL_inplace = SvOK(sv) ? savesvpv(sv) : Nullch;
da78da6e 2214 break;
28f23441 2215 case '\017': /* ^O */
ac27b0f5 2216 if (*(mg->mg_ptr+1) == '\0') {
43c5f42d
NC
2217 Safefree(PL_osname);
2218 PL_osname = Nullch;
3511154c
DM
2219 if (SvOK(sv)) {
2220 TAINT_PROPER("assigning to $^O");
2e0de35c 2221 PL_osname = savesvpv(sv);
3511154c 2222 }
ac27b0f5
NIS
2223 }
2224 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2225 if (!PL_compiling.cop_io)
2226 PL_compiling.cop_io = newSVsv(sv);
2227 else
2228 sv_setsv(PL_compiling.cop_io,sv);
2229 }
28f23441 2230 break;
79072805 2231 case '\020': /* ^P */
3280af22 2232 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
f2a7f298 2233 if (PL_perldb && !PL_DBsingle)
1ee4443e 2234 init_debugger();
79072805
LW
2235 break;
2236 case '\024': /* ^T */
88e89b8a 2237#ifdef BIG_TIME
6b88bc9c 2238 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2239#else
3280af22 2240 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2241#endif
79072805 2242 break;
fde18df1 2243 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2244 if (*(mg->mg_ptr+1) == '\0') {
2245 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2246 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2247 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2248 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2249 }
599cee73 2250 }
0a378802 2251 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2252 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2253 if (!SvPOK(sv) && PL_localizing) {
2254 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2255 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2256 break;
2257 }
f4fc7782 2258 {
b5477537 2259 STRLEN len, i;
d3a7d8c7 2260 int accumulate = 0 ;
f4fc7782 2261 int any_fatals = 0 ;
b83604b4 2262 const char * const ptr = SvPV_const(sv, len) ;
f4fc7782
JH
2263 for (i = 0 ; i < len ; ++i) {
2264 accumulate |= ptr[i] ;
2265 any_fatals |= (ptr[i] & 0xAA) ;
2266 }
d3a7d8c7
GS
2267 if (!accumulate)
2268 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2269 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2270 PL_compiling.cop_warnings = pWARN_ALL;
2271 PL_dowarn |= G_WARN_ONCE ;
727405f8 2272 }
d3a7d8c7
GS
2273 else {
2274 if (specialWARN(PL_compiling.cop_warnings))
2275 PL_compiling.cop_warnings = newSVsv(sv) ;
2276 else
2277 sv_setsv(PL_compiling.cop_warnings, sv);
2278 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2279 PL_dowarn |= G_WARN_ONCE ;
2280 }
f4fc7782 2281
d3a7d8c7 2282 }
4438c4b7 2283 }
971a9dd3 2284 }
79072805
LW
2285 break;
2286 case '.':
3280af22
NIS
2287 if (PL_localizing) {
2288 if (PL_localizing == 1)
7766f137 2289 SAVESPTR(PL_last_in_gv);
748a9306 2290 }
3280af22 2291 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2292 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2293 break;
2294 case '^':
3280af22 2295 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2296 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
f776e3cd 2297 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2298 break;
2299 case '~':
3280af22 2300 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
e1ec3a88 2301 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
f776e3cd 2302 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
79072805
LW
2303 break;
2304 case '=':
632db599 2305 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2306 break;
2307 case '-':
632db599 2308 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2309 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2310 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2311 break;
2312 case '%':
632db599 2313 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2314 break;
2315 case '|':
4b65379b 2316 {
8772537c 2317 IO * const io = GvIOp(PL_defoutgv);
720f287d
AB
2318 if(!io)
2319 break;
4b65379b
CS
2320 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2321 IoFLAGS(io) &= ~IOf_FLUSH;
2322 else {
2323 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2324 PerlIO *ofp = IoOFP(io);
2325 if (ofp)
2326 (void)PerlIO_flush(ofp);
2327 IoFLAGS(io) |= IOf_FLUSH;
2328 }
2329 }
79072805
LW
2330 }
2331 break;
79072805 2332 case '/':
3280af22 2333 SvREFCNT_dec(PL_rs);
8bfdd7d9 2334 PL_rs = newSVsv(sv);
79072805
LW
2335 break;
2336 case '\\':
7889fe52
NIS
2337 if (PL_ors_sv)
2338 SvREFCNT_dec(PL_ors_sv);
009c130f 2339 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2340 PL_ors_sv = newSVsv(sv);
009c130f 2341 }
e3c19b7b 2342 else {
7889fe52 2343 PL_ors_sv = Nullsv;
e3c19b7b 2344 }
79072805
LW
2345 break;
2346 case ',':
7889fe52
NIS
2347 if (PL_ofs_sv)
2348 SvREFCNT_dec(PL_ofs_sv);
2349 if (SvOK(sv) || SvGMAGICAL(sv)) {
2350 PL_ofs_sv = newSVsv(sv);
2351 }
2352 else {
2353 PL_ofs_sv = Nullsv;
2354 }
79072805 2355 break;
79072805 2356 case '[':
3280af22 2357 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2358 break;
2359 case '?':
ff0cee69 2360#ifdef COMPLEX_STATUS
6b88bc9c
GS
2361 if (PL_localizing == 2) {
2362 PL_statusvalue = LvTARGOFF(sv);
2363 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2364 }
2365 else
2366#endif
2367#ifdef VMSISH_STATUS
2368 if (VMSISH_STATUS)
fb38d079 2369 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
ff0cee69
PP
2370 else
2371#endif
fb38d079 2372 STATUS_UNIX_EXIT_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2373 break;
2374 case '!':
93189314
JH
2375 {
2376#ifdef VMS
2377# define PERL_VMS_BANG vaxc$errno
2378#else
2379# define PERL_VMS_BANG 0
2380#endif
91487cfc 2381 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2382 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2383 }
79072805
LW
2384 break;
2385 case '<':
3280af22
NIS
2386 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2387 if (PL_delaymagic) {
2388 PL_delaymagic |= DM_RUID;
79072805
LW
2389 break; /* don't do magic till later */
2390 }
2391#ifdef HAS_SETRUID
b28d0864 2392 (void)setruid((Uid_t)PL_uid);
79072805
LW
2393#else
2394#ifdef HAS_SETREUID
3280af22 2395 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2396#else
85e6fe83 2397#ifdef HAS_SETRESUID
b28d0864 2398 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2399#else
75870ed3 2400 if (PL_uid == PL_euid) { /* special case $< = $> */
2401#ifdef PERL_DARWIN
2402 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2403 if (PL_uid != 0 && PerlProc_getuid() == 0)
2404 (void)PerlProc_setuid(0);
2405#endif
b28d0864 2406 (void)PerlProc_setuid(PL_uid);
75870ed3 2407 } else {
d8eceb89 2408 PL_uid = PerlProc_getuid();
cea2e8a9 2409 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2410 }
79072805
LW
2411#endif
2412#endif
85e6fe83 2413#endif
d8eceb89 2414 PL_uid = PerlProc_getuid();
3280af22 2415 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2416 break;
2417 case '>':
3280af22
NIS
2418 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2419 if (PL_delaymagic) {
2420 PL_delaymagic |= DM_EUID;
79072805
LW
2421 break; /* don't do magic till later */
2422 }
2423#ifdef HAS_SETEUID
3280af22 2424 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2425#else
2426#ifdef HAS_SETREUID
b28d0864 2427 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2428#else
2429#ifdef HAS_SETRESUID
6b88bc9c 2430 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2431#else
b28d0864
NIS
2432 if (PL_euid == PL_uid) /* special case $> = $< */
2433 PerlProc_setuid(PL_euid);
a0d0e21e 2434 else {
e8ee3774 2435 PL_euid = PerlProc_geteuid();
cea2e8a9 2436 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2437 }
79072805
LW
2438#endif
2439#endif
85e6fe83 2440#endif
d8eceb89 2441 PL_euid = PerlProc_geteuid();
3280af22 2442 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2443 break;
2444 case '(':
3280af22
NIS
2445 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2446 if (PL_delaymagic) {
2447 PL_delaymagic |= DM_RGID;
79072805
LW
2448 break; /* don't do magic till later */
2449 }
2450#ifdef HAS_SETRGID
b28d0864 2451 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2452#else
2453#ifdef HAS_SETREGID
3280af22 2454 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2455#else
2456#ifdef HAS_SETRESGID
b28d0864 2457 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2458#else
b28d0864
NIS
2459 if (PL_gid == PL_egid) /* special case $( = $) */
2460 (void)PerlProc_setgid(PL_gid);
748a9306 2461 else {
d8eceb89 2462 PL_gid = PerlProc_getgid();
cea2e8a9 2463 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2464 }
79072805
LW
2465#endif
2466#endif
85e6fe83 2467#endif
d8eceb89 2468 PL_gid = PerlProc_getgid();
3280af22 2469 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2470 break;
2471 case ')':
5cd24f17
PP
2472#ifdef HAS_SETGROUPS
2473 {
b83604b4 2474 const char *p = SvPV_const(sv, len);
757f63d8
SP
2475 Groups_t *gary = NULL;
2476
2477 while (isSPACE(*p))
2478 ++p;
2479 PL_egid = Atol(p);
2480 for (i = 0; i < NGROUPS; ++i) {
2481 while (*p && !isSPACE(*p))
2482 ++p;
2483 while (isSPACE(*p))
2484 ++p;
2485 if (!*p)
2486 break;
2487 if(!gary)
2488 Newx(gary, i + 1, Groups_t);
2489 else
2490 Renew(gary, i + 1, Groups_t);
2491 gary[i] = Atol(p);
2492 }
2493 if (i)
2494 (void)setgroups(i, gary);
2495 if (gary)
2496 Safefree(gary);
5cd24f17
PP
2497 }
2498#else /* HAS_SETGROUPS */
b28d0864 2499 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2500#endif /* HAS_SETGROUPS */
3280af22
NIS
2501 if (PL_delaymagic) {
2502 PL_delaymagic |= DM_EGID;
79072805
LW
2503 break; /* don't do magic till later */
2504 }
2505#ifdef HAS_SETEGID
3280af22 2506 (void)setegid((Gid_t)PL_egid);
79072805
LW
2507#else
2508#ifdef HAS_SETREGID
b28d0864 2509 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2510#else
2511#ifdef HAS_SETRESGID
b28d0864 2512 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2513#else
b28d0864
NIS
2514 if (PL_egid == PL_gid) /* special case $) = $( */
2515 (void)PerlProc_setgid(PL_egid);
748a9306 2516 else {
d8eceb89 2517 PL_egid = PerlProc_getegid();
cea2e8a9 2518 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2519 }
79072805
LW
2520#endif
2521#endif
85e6fe83 2522#endif
d8eceb89 2523 PL_egid = PerlProc_getegid();
3280af22 2524 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2525 break;
2526 case ':':
2d8e6c8d 2527 PL_chopset = SvPV_force(sv,len);
79072805 2528 break;
cd39f2b6 2529#ifndef MACOS_TRADITIONAL
79072805 2530 case '0':
e2975953 2531 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2532#ifdef HAS_SETPROCTITLE
2533 /* The BSDs don't show the argv[] in ps(1) output, they
2534 * show a string from the process struct and provide
2535 * the setproctitle() routine to manipulate that. */
a2722ac9 2536 if (PL_origalen != 1) {
b83604b4 2537 s = SvPV_const(sv, len);
98b76f99 2538# if __FreeBSD_version > 410001
9aad2c0e 2539 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2540 * but not the "(perl) suffix from the ps(1)
2541 * output, because that's what ps(1) shows if the
2542 * argv[] is modified. */
6f2ad931 2543 setproctitle("-%s", s);
9aad2c0e 2544# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2545 /* This doesn't really work if you assume that
2546 * $0 = 'foobar'; will wipe out 'perl' from the $0
2547 * because in ps(1) output the result will be like
2548 * sprintf("perl: %s (perl)", s)
2549 * I guess this is a security feature:
2550 * one (a user process) cannot get rid of the original name.
2551 * --jhi */
2552 setproctitle("%s", s);
2553# endif
2554 }
2555#endif
17aa7f3d 2556#if defined(__hpux) && defined(PSTAT_SETCMD)
a2722ac9 2557 if (PL_origalen != 1) {
17aa7f3d 2558 union pstun un;
b83604b4 2559 s = SvPV_const(sv, len);
6867be6d 2560 un.pst_command = (char *)s;
17aa7f3d
JH
2561 pstat(PSTAT_SETCMD, un, len, 0, 0);
2562 }
2563#endif
2d2af554
GA
2564 if (PL_origalen > 1) {
2565 /* PL_origalen is set in perl_parse(). */
2566 s = SvPV_force(sv,len);
2567 if (len >= (STRLEN)PL_origalen-1) {
2568 /* Longer than original, will be truncated. We assume that
2569 * PL_origalen bytes are available. */
2570 Copy(s, PL_origargv[0], PL_origalen-1, char);
2571 }
2572 else {
2573 /* Shorter than original, will be padded. */
2574 Copy(s, PL_origargv[0], len, char);
2575 PL_origargv[0][len] = 0;
2576 memset(PL_origargv[0] + len + 1,
2577 /* Is the space counterintuitive? Yes.
2578 * (You were expecting \0?)
2579 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2580 * --jhi */
2581 (int)' ',
2582 PL_origalen - len - 1);
2583 }
2584 PL_origargv[0][PL_origalen-1] = 0;
2585 for (i = 1; i < PL_origargc; i++)
2586 PL_origargv[i] = 0;
79072805 2587 }
e2975953 2588 UNLOCK_DOLLARZERO_MUTEX;
79072805 2589 break;
cd39f2b6 2590#endif
79072805
LW
2591 }
2592 return 0;
2593}
2594
2595I32
35a4481c 2596Perl_whichsig(pTHX_ const char *sig)
79072805 2597{
aadb217d 2598 register char* const* sigv;
79072805 2599
aadb217d 2600 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
79072805 2601 if (strEQ(sig,*sigv))
aadb217d 2602 return PL_sig_num[sigv - (char* const*)PL_sig_name];
79072805
LW
2603#ifdef SIGCLD
2604 if (strEQ(sig,"CHLD"))
2605 return SIGCLD;
2606#endif
2607#ifdef SIGCHLD
2608 if (strEQ(sig,"CLD"))
2609 return SIGCHLD;
2610#endif
7f1236c0 2611 return -1;
79072805
LW
2612}
2613
ecfc5424 2614Signal_t
1e82f5a6 2615#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
8aad04aa 2616Perl_sighandler(int sig, ...)
1e82f5a6
SH
2617#else
2618Perl_sighandler(int sig)
2619#endif
79072805 2620{
1018e26f
NIS
2621#ifdef PERL_GET_SIG_CONTEXT
2622 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2623#else
cea2e8a9 2624 dTHX;
71d280e3 2625#endif
79072805 2626 dSP;
00d579c5 2627 GV *gv = Nullgv;
8772537c
AL
2628 SV *sv = Nullsv;
2629 SV * const tSv = PL_Sv;
00d579c5 2630 CV *cv = Nullcv;
533c011a 2631 OP *myop = PL_op;
84902520 2632 U32 flags = 0;
8772537c 2633 XPV * const tXpv = PL_Xpv;
71d280e3 2634
3280af22 2635 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2636 flags |= 1;
3280af22 2637 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2638 flags |= 4;
3280af22 2639 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2640 flags |= 16;
2641
727405f8 2642 if (!PL_psig_ptr[sig]) {
99ef548b 2643 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2644 PL_sig_name[sig]);
2645 exit(sig);
2646 }
ff0cee69 2647
84902520
TB
2648 /* Max number of items pushed there is 3*n or 4. We cannot fix
2649 infinity, so we fix 4 (in fact 5): */
2650 if (flags & 1) {
3280af22 2651 PL_savestack_ix += 5; /* Protect save in progress. */
8772537c 2652 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
84902520 2653 }
ac27b0f5 2654 if (flags & 4)
3280af22 2655 PL_markstack_ptr++; /* Protect mark. */
84902520 2656 if (flags & 16)
3280af22 2657 PL_scopestack_ix += 1;
84902520 2658 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2659 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
8772537c
AL
2660 || SvTYPE(cv) != SVt_PVCV) {
2661 HV *st;
f2c0649b 2662 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
8772537c 2663 }
84902520 2664
a0d0e21e 2665 if (!cv || !CvROOT(cv)) {
599cee73 2666 if (ckWARN(WARN_SIGNAL))
9014280d 2667 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2668 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2669 : ((cv && CvGV(cv))
2670 ? GvENAME(CvGV(cv))
2671 : "__ANON__")));
2672 goto cleanup;
79072805
LW
2673 }
2674
22c35a8c
GS
2675 if(PL_psig_name[sig]) {
2676 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2677 flags |= 64;
df3728a2 2678#if !defined(PERL_IMPLICIT_CONTEXT)
27da23d5 2679 PL_sig_sv = sv;
df3728a2 2680#endif
84902520 2681 } else {
ff0cee69 2682 sv = sv_newmortal();
22c35a8c 2683 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2684 }
e336de0d 2685
e788e7d3 2686 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2687 PUSHMARK(SP);
79072805 2688 PUSHs(sv);
8aad04aa
JH
2689#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2690 {
2691 struct sigaction oact;
2692
2693 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2694 siginfo_t *sip;
2695 va_list args;
2696
2697 va_start(args, sig);
2698 sip = (siginfo_t*)va_arg(args, siginfo_t*);
2699 if (sip) {
2700 HV *sih = newHV();
2701 SV *rv = newRV_noinc((SV*)sih);
2702 /* The siginfo fields signo, code, errno, pid, uid,
2703 * addr, status, and band are defined by POSIX/SUSv3. */
2704 hv_store(sih, "signo", 5, newSViv(sip->si_signo), 0);
2705 hv_store(sih, "code", 4, newSViv(sip->si_code), 0);
79dec0f4 2706#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. */
ea1bde16 2707 hv_store(sih, "errno", 5, newSViv(sip->si_errno), 0);
79dec0f4 2708 hv_store(sih, "status", 6, newSViv(sip->si_status), 0);
8aad04aa
JH
2709 hv_store(sih, "uid", 3, newSViv(sip->si_uid), 0);
2710 hv_store(sih, "pid", 3, newSViv(sip->si_pid), 0);
2711 hv_store(sih, "addr", 4, newSVuv(PTR2UV(sip->si_addr)), 0);
8aad04aa 2712 hv_store(sih, "band", 4, newSViv(sip->si_band), 0);
79dec0f4 2713#endif
8aad04aa
JH
2714 EXTEND(SP, 2);
2715 PUSHs((SV*)rv);
2716 PUSHs(newSVpv((void*)sip, sizeof(*sip)));
2717 }
b4552a27 2718
31427afe 2719 va_end(args);
8aad04aa
JH
2720 }
2721 }
2722#endif
79072805 2723 PUTBACK;
a0d0e21e 2724
1b266415 2725 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2726
d3acc0f7 2727 POPSTACK;
1b266415 2728 if (SvTRUE(ERRSV)) {
1d615522 2729#ifndef PERL_MICRO
983dbef6 2730#ifdef HAS_SIGPROCMASK
1b266415
NIS
2731 /* Handler "died", for example to get out of a restart-able read().
2732 * Before we re-do that on its behalf re-enable the signal which was
2733 * blocked by the system when we entered.
2734 */
2735 sigset_t set;
2736 sigemptyset(&set);
2737 sigaddset(&set,sig);
2738 sigprocmask(SIG_UNBLOCK, &set, NULL);
2739#else
2740 /* Not clear if this will work */
2741 (void)rsignal(sig, SIG_IGN);
5c1546dc 2742 (void)rsignal(sig, PL_csighandlerp);
1b266415 2743#endif
1d615522 2744#endif /* !PERL_MICRO */
c3bdd826 2745 Perl_die(aTHX_ Nullch);
1b266415 2746 }
00d579c5 2747cleanup:
84902520 2748 if (flags & 1)
3280af22 2749 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2750 if (flags & 4)
3280af22 2751 PL_markstack_ptr--;
84902520 2752 if (flags & 16)
3280af22 2753 PL_scopestack_ix -= 1;
84902520
TB
2754 if (flags & 64)
2755 SvREFCNT_dec(sv);
533c011a 2756 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2757
3280af22
NIS
2758 PL_Sv = tSv; /* Restore global temporaries. */
2759 PL_Xpv = tXpv;
53bb94e2 2760 return;
79072805 2761}
4e35701f
NIS
2762
2763
51371543 2764static void
8772537c 2765S_restore_magic(pTHX_ const void *p)
51371543 2766{
97aff369 2767 dVAR;
8772537c
AL
2768 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
2769 SV* const sv = mgs->mgs_sv;
51371543
GS
2770
2771 if (!sv)
2772 return;
2773
2774 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2775 {
f8c7b90f 2776#ifdef PERL_OLD_COPY_ON_WRITE
f9701176
NC
2777 /* While magic was saved (and off) sv_setsv may well have seen
2778 this SV as a prime candidate for COW. */
2779 if (SvIsCOW(sv))
e424a81e 2780 sv_force_normal_flags(sv, 0);
f9701176
NC
2781#endif
2782
51371543
GS
2783 if (mgs->mgs_flags)
2784 SvFLAGS(sv) |= mgs->mgs_flags;
2785 else
2786 mg_magical(sv);
2b77b520
YST
2787 if (SvGMAGICAL(sv)) {
2788 /* downgrade public flags to private,
2789 and discard any other private flags */
2790
2791 U32 public = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
2792 if (public) {
2793 SvFLAGS(sv) &= ~( public | SVp_IOK|SVp_NOK|SVp_POK );
2794 SvFLAGS(sv) |= ( public << PRIVSHIFT );
2795 }
2796 }
51371543
GS
2797 }
2798
2799 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2800
2801 /* If we're still on top of the stack, pop us off. (That condition
2802 * will be satisfied if restore_magic was called explicitly, but *not*
2803 * if it's being called via leave_scope.)
2804 * The reason for doing this is that otherwise, things like sv_2cv()
2805 * may leave alloc gunk on the savestack, and some code
2806 * (e.g. sighandler) doesn't expect that...
2807 */
2808 if (PL_savestack_ix == mgs->mgs_ss_ix)
2809 {
2810 I32 popval = SSPOPINT;
c76ac1ee 2811 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2812 PL_savestack_ix -= 2;
2813 popval = SSPOPINT;
2814 assert(popval == SAVEt_ALLOC);
2815 popval = SSPOPINT;
2816 PL_savestack_ix -= popval;
2817 }
2818
2819}
2820
2821static void
8772537c 2822S_unwind_handler_stack(pTHX_ const void *p)
51371543 2823{
27da23d5 2824 dVAR;
e1ec3a88 2825 const U32 flags = *(const U32*)p;
51371543
GS
2826
2827 if (flags & 1)
2828 PL_savestack_ix -= 5; /* Unprotect save in progress. */
df3728a2 2829#if !defined(PERL_IMPLICIT_CONTEXT)
51371543 2830 if (flags & 64)
27da23d5 2831 SvREFCNT_dec(PL_sig_sv);
df3728a2 2832#endif
51371543 2833}
1018e26f 2834
66610fdd
RGS
2835/*
2836 * Local variables:
2837 * c-indentation-style: bsd
2838 * c-basic-offset: 4
2839 * indent-tabs-mode: t
2840 * End:
2841 *
37442d52
RGS
2842 * ex: set ts=8 sts=4 sw=4 noet:
2843 */