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