This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
offsetof() is in <stddef.h>
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
79072805
LW
13 */
14
ccfc67b7
JH
15/*
16=head1 Magical Functions
17*/
18
79072805 19#include "EXTERN.h"
864dbfa3 20#define PERL_IN_MG_C
79072805
LW
21#include "perl.h"
22
5cd24f17 23#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221
CS
24# ifndef NGROUPS
25# define NGROUPS 32
26# endif
b7953727
JH
27# ifdef I_GRP
28# include <grp.h>
29# endif
188ea221
CS
30#endif
31
23ada85b 32/* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
85b332e2 33#if !defined(HAS_SIGACTION) && defined(VMS)
23ada85b 34# define FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2 35#endif
2e34cc90
CL
36/* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
37#if defined(KILL_BY_SIGPRC)
38# define FAKE_DEFAULT_SIGNAL_HANDLERS
39#endif
85b332e2 40
acfe0abc
GS
41static void restore_magic(pTHX_ void *p);
42static void unwind_handler_stack(pTHX_ void *p);
51371543 43
c07a80fd
PP
44/*
45 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
46 */
47
48struct magic_state {
49 SV* mgs_sv;
50 U32 mgs_flags;
455ece5e 51 I32 mgs_ss_ix;
c07a80fd 52};
455ece5e 53/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
54
55STATIC void
8fb26106 56S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
c07a80fd 57{
455ece5e 58 MGS* mgs;
c07a80fd
PP
59 assert(SvMAGICAL(sv));
60
685f876f 61 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
62
63 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd
PP
64 mgs->mgs_sv = sv;
65 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 66 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd
PP
67
68 SvMAGICAL_off(sv);
69 SvREADONLY_off(sv);
06759ea0 70 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
71}
72
954c1994
GS
73/*
74=for apidoc mg_magical
75
76Turns on the magical status of an SV. See C<sv_magic>.
77
78=cut
79*/
80
8990e307 81void
864dbfa3 82Perl_mg_magical(pTHX_ SV *sv)
8990e307
LW
83{
84 MAGIC* mg;
85 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
86 MGVTBL* vtbl = mg->mg_virtual;
87 if (vtbl) {
2b260de0 88 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
89 SvGMAGICAL_on(sv);
90 if (vtbl->svt_set)
91 SvSMAGICAL_on(sv);
2b260de0 92 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307
LW
93 SvRMAGICAL_on(sv);
94 }
95 }
96}
97
954c1994
GS
98/*
99=for apidoc mg_get
100
101Do magic after a value is retrieved from the SV. See C<sv_magic>.
102
103=cut
104*/
105
79072805 106int
864dbfa3 107Perl_mg_get(pTHX_ SV *sv)
79072805 108{
ff76feab
AMS
109 int new = 0;
110 MAGIC *newmg, *head, *cur, *mg;
111 I32 mgs_ix = SSNEW(sizeof(MGS));
463ee0b2 112
455ece5e 113 save_magic(mgs_ix, sv);
463ee0b2 114
ff76feab
AMS
115 /* We must call svt_get(sv, mg) for each valid entry in the linked
116 list of magic. svt_get() may delete the current entry, add new
117 magic to the head of the list, or upgrade the SV. AMS 20010810 */
118
119 newmg = cur = head = mg = SvMAGIC(sv);
120 while (mg) {
121 MGVTBL *vtbl = mg->mg_virtual;
122
2b260de0 123 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 124 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
ff76feab
AMS
125 /* Don't restore the flags for this entry if it was deleted. */
126 if (mg->mg_flags & MGf_GSKIP)
127 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
a0d0e21e 128 }
ff76feab
AMS
129
130 mg = mg->mg_moremagic;
131
132 if (new) {
133 /* Have we finished with the new entries we saw? Start again
134 where we left off (unless there are more new entries). */
135 if (mg == head) {
136 new = 0;
137 mg = cur;
138 head = newmg;
139 }
140 }
141
142 /* Were any new entries added? */
143 if (!new && (newmg = SvMAGIC(sv)) != head) {
144 new = 1;
145 cur = mg;
146 mg = newmg;
760ac839 147 }
79072805 148 }
463ee0b2 149
acfe0abc 150 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
79072805
LW
151 return 0;
152}
153
954c1994
GS
154/*
155=for apidoc mg_set
156
157Do magic after a value is assigned to the SV. See C<sv_magic>.
158
159=cut
160*/
161
79072805 162int
864dbfa3 163Perl_mg_set(pTHX_ SV *sv)
79072805 164{
8fb26106 165 I32 mgs_ix;
79072805 166 MAGIC* mg;
463ee0b2
LW
167 MAGIC* nextmg;
168
455ece5e
AD
169 mgs_ix = SSNEW(sizeof(MGS));
170 save_magic(mgs_ix, sv);
463ee0b2
LW
171
172 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 173 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 174 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
175 if (mg->mg_flags & MGf_GSKIP) {
176 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 177 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 178 }
2b260de0 179 if (vtbl && vtbl->svt_set)
fc0dc3b3 180 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 181 }
463ee0b2 182
acfe0abc 183 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
184 return 0;
185}
186
954c1994
GS
187/*
188=for apidoc mg_length
189
190Report on the SV's length. See C<sv_magic>.
191
192=cut
193*/
194
79072805 195U32
864dbfa3 196Perl_mg_length(pTHX_ SV *sv)
79072805
LW
197{
198 MAGIC* mg;
463ee0b2 199 STRLEN len;
463ee0b2 200
79072805
LW
201 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
202 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 203 if (vtbl && vtbl->svt_len) {
8fb26106 204 I32 mgs_ix;
48e43a1c 205
455ece5e
AD
206 mgs_ix = SSNEW(sizeof(MGS));
207 save_magic(mgs_ix, sv);
a0d0e21e 208 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 209 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 210 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
211 return len;
212 }
213 }
214
5636d518
DB
215 if (DO_UTF8(sv))
216 {
217 U8 *s = (U8*)SvPV(sv, len);
218 len = Perl_utf8_length(aTHX_ s, s + len);
219 }
220 else
221 (void)SvPV(sv, len);
463ee0b2 222 return len;
79072805
LW
223}
224
8fb26106 225I32
864dbfa3 226Perl_mg_size(pTHX_ SV *sv)
93965878
NIS
227{
228 MAGIC* mg;
229 I32 len;
ac27b0f5 230
93965878
NIS
231 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
232 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 233 if (vtbl && vtbl->svt_len) {
8fb26106 234 I32 mgs_ix;
455ece5e
AD
235
236 mgs_ix = SSNEW(sizeof(MGS));
237 save_magic(mgs_ix, sv);
93965878 238 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 239 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 240 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
93965878
NIS
241 return len;
242 }
243 }
244
245 switch(SvTYPE(sv)) {
246 case SVt_PVAV:
247 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
248 return len;
249 case SVt_PVHV:
250 /* FIXME */
251 default:
cea2e8a9 252 Perl_croak(aTHX_ "Size magic not implemented");
93965878
NIS
253 break;
254 }
255 return 0;
256}
257
954c1994
GS
258/*
259=for apidoc mg_clear
260
261Clear something magical that the SV represents. See C<sv_magic>.
262
263=cut
264*/
265
79072805 266int
864dbfa3 267Perl_mg_clear(pTHX_ SV *sv)
79072805 268{
8fb26106 269 I32 mgs_ix;
79072805 270 MAGIC* mg;
463ee0b2 271
455ece5e
AD
272 mgs_ix = SSNEW(sizeof(MGS));
273 save_magic(mgs_ix, sv);
463ee0b2 274
79072805
LW
275 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
276 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e
LW
277 /* omit GSKIP -- never set here */
278
2b260de0 279 if (vtbl && vtbl->svt_clear)
fc0dc3b3 280 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 281 }
463ee0b2 282
acfe0abc 283 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
284 return 0;
285}
286
954c1994
GS
287/*
288=for apidoc mg_find
289
290Finds the magic pointer for type matching the SV. See C<sv_magic>.
291
292=cut
293*/
294
93a17b20 295MAGIC*
864dbfa3 296Perl_mg_find(pTHX_ SV *sv, int type)
93a17b20
LW
297{
298 MAGIC* mg;
3f8f4626
DC
299 if (!sv)
300 return 0;
93a17b20
LW
301 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
302 if (mg->mg_type == type)
303 return mg;
304 }
305 return 0;
306}
307
954c1994
GS
308/*
309=for apidoc mg_copy
310
311Copies the magic from one SV to another. See C<sv_magic>.
312
313=cut
314*/
315
79072805 316int
864dbfa3 317Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 318{
463ee0b2 319 int count = 0;
79072805 320 MAGIC* mg;
463ee0b2
LW
321 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
322 if (isUPPER(mg->mg_type)) {
33c27489 323 sv_magic(nsv,
14befaf4
DM
324 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
325 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
326 ? sv : mg->mg_obj,
33c27489 327 toLOWER(mg->mg_type), key, klen);
463ee0b2 328 count++;
79072805 329 }
79072805 330 }
463ee0b2 331 return count;
79072805
LW
332}
333
954c1994
GS
334/*
335=for apidoc mg_free
336
337Free any magic storage used by the SV. See C<sv_magic>.
338
339=cut
340*/
341
79072805 342int
864dbfa3 343Perl_mg_free(pTHX_ SV *sv)
79072805
LW
344{
345 MAGIC* mg;
346 MAGIC* moremagic;
347 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
348 MGVTBL* vtbl = mg->mg_virtual;
349 moremagic = mg->mg_moremagic;
2b260de0 350 if (vtbl && vtbl->svt_free)
fc0dc3b3 351 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 352 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
565764a8 353 if (mg->mg_len >= 0)
88e89b8a 354 Safefree(mg->mg_ptr);
565764a8 355 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 356 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 357 }
85e6fe83 358 if (mg->mg_flags & MGf_REFCOUNTED)
8990e307 359 SvREFCNT_dec(mg->mg_obj);
79072805
LW
360 Safefree(mg);
361 }
362 SvMAGIC(sv) = 0;
363 return 0;
364}
365
d460ef45 366
79072805
LW
367#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
368#include <signal.h>
369#endif
370
942e002e 371U32
864dbfa3 372Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 373{
6cef1e77 374 register REGEXP *rx;
6cef1e77 375
aaa362c4 376 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
8f580fb8
IZ
377 if (mg->mg_obj) /* @+ */
378 return rx->nparens;
379 else /* @- */
380 return rx->lastparen;
381 }
ac27b0f5 382
942e002e 383 return (U32)-1;
6cef1e77
IZ
384}
385
386int
864dbfa3 387Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 388{
6cef1e77 389 register I32 paren;
cf93c79d 390 register I32 s;
6cef1e77
IZ
391 register I32 i;
392 register REGEXP *rx;
cf93c79d 393 I32 t;
6cef1e77 394
aaa362c4 395 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
6cef1e77
IZ
396 paren = mg->mg_len;
397 if (paren < 0)
398 return 0;
399 if (paren <= rx->nparens &&
cf93c79d
IZ
400 (s = rx->startp[paren]) != -1 &&
401 (t = rx->endp[paren]) != -1)
6cef1e77
IZ
402 {
403 if (mg->mg_obj) /* @+ */
cf93c79d 404 i = t;
6cef1e77 405 else /* @- */
cf93c79d 406 i = s;
1aa99e6b 407
53c4c00c 408 if (i > 0 && PL_reg_match_utf8) {
1aa99e6b 409 char *b = rx->subbeg;
0064a8a9
JH
410 if (b)
411 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 412 }
0064a8a9
JH
413
414 sv_setiv(sv, i);
6cef1e77
IZ
415 }
416 }
417 return 0;
418}
419
e4b89193 420int
a29d06ed
MG
421Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
422{
a29d06ed 423 Perl_croak(aTHX_ PL_no_modify);
e4b89193
GS
424 /* NOT REACHED */
425 return 0;
a29d06ed
MG
426}
427
93a17b20 428U32
864dbfa3 429Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20
LW
430{
431 register I32 paren;
93a17b20 432 register I32 i;
d9f97599 433 register REGEXP *rx;
a197cbdd 434 I32 s1, t1;
93a17b20
LW
435
436 switch (*mg->mg_ptr) {
437 case '1': case '2': case '3': case '4':
438 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 439 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 440
ffc61ed2 441 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 442 getparen:
d9f97599 443 if (paren <= rx->nparens &&
cf93c79d
IZ
444 (s1 = rx->startp[paren]) != -1 &&
445 (t1 = rx->endp[paren]) != -1)
bbce6d69 446 {
cf93c79d 447 i = t1 - s1;
a197cbdd 448 getlen:
53c4c00c 449 if (i > 0 && PL_reg_match_utf8) {
ffc61ed2 450 char *s = rx->subbeg + s1;
a197cbdd 451 char *send = rx->subbeg + t1;
ffc61ed2 452
60425c38
JH
453 i = t1 - s1;
454 if (is_utf8_string((U8*)s, i))
455 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
a197cbdd 456 }
ffc61ed2 457 if (i < 0)
0844c848 458 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 459 return i;
93a17b20 460 }
93a17b20 461 }
748a9306 462 return 0;
93a17b20 463 case '+':
aaa362c4 464 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 465 paren = rx->lastparen;
13f57bf8
CS
466 if (paren)
467 goto getparen;
93a17b20 468 }
748a9306 469 return 0;
a01268b5
JH
470 case '\016': /* ^N */
471 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
472 paren = rx->lastcloseparen;
473 if (paren)
474 goto getparen;
475 }
476 return 0;
93a17b20 477 case '`':
aaa362c4 478 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
479 if (rx->startp[0] != -1) {
480 i = rx->startp[0];
a197cbdd
GS
481 if (i > 0) {
482 s1 = 0;
483 t1 = i;
484 goto getlen;
485 }
93a17b20 486 }
93a17b20 487 }
748a9306 488 return 0;
93a17b20 489 case '\'':
aaa362c4 490 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
491 if (rx->endp[0] != -1) {
492 i = rx->sublen - rx->endp[0];
a197cbdd
GS
493 if (i > 0) {
494 s1 = rx->endp[0];
495 t1 = rx->sublen;
496 goto getlen;
497 }
93a17b20 498 }
93a17b20 499 }
748a9306 500 return 0;
93a17b20
LW
501 }
502 magic_get(sv,mg);
2d8e6c8d
GS
503 if (!SvPOK(sv) && SvNIOK(sv)) {
504 STRLEN n_a;
505 sv_2pv(sv, &n_a);
506 }
93a17b20
LW
507 if (SvPOK(sv))
508 return SvCUR(sv);
509 return 0;
510}
511
79072805 512int
864dbfa3 513Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
514{
515 register I32 paren;
516 register char *s;
517 register I32 i;
d9f97599 518 register REGEXP *rx;
79072805
LW
519
520 switch (*mg->mg_ptr) {
748a9306 521 case '\001': /* ^A */
3280af22 522 sv_setsv(sv, PL_bodytarget);
748a9306 523 break;
49460fe6
NIS
524 case '\003': /* ^C */
525 sv_setiv(sv, (IV)PL_minus_c);
526 break;
527
79072805 528 case '\004': /* ^D */
aea4f609 529 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
d056ab3f 530#if defined(YYDEBUG) && defined(DEBUGGING)
aea4f609 531 PL_yydebug = DEBUG_p_TEST;
d056ab3f 532#endif
79072805 533 break;
28f23441 534 case '\005': /* ^E */
0a378802 535 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 536#ifdef MACOS_TRADITIONAL
0a378802
JH
537 {
538 char msg[256];
ac27b0f5 539
0a378802
JH
540 sv_setnv(sv,(double)gMacPerl_OSErr);
541 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
542 }
cd39f2b6 543#else
28f23441 544#ifdef VMS
0a378802
JH
545 {
546# include <descrip.h>
547# include <starlet.h>
548 char msg[255];
549 $DESCRIPTOR(msgdsc,msg);
550 sv_setnv(sv,(NV) vaxc$errno);
551 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
552 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
553 else
554 sv_setpv(sv,"");
555 }
28f23441 556#else
88e89b8a 557#ifdef OS2
0a378802
JH
558 if (!(_emx_env & 0x200)) { /* Under DOS */
559 sv_setnv(sv, (NV)errno);
560 sv_setpv(sv, errno ? Strerror(errno) : "");
561 } else {
562 if (errno != errno_isOS2) {
563 int tmp = _syserrno();
564 if (tmp) /* 2nd call to _syserrno() makes it 0 */
565 Perl_rc = tmp;
566 }
567 sv_setnv(sv, (NV)Perl_rc);
568 sv_setpv(sv, os2error(Perl_rc));
569 }
88e89b8a 570#else
22fae026 571#ifdef WIN32
0a378802
JH
572 {
573 DWORD dwErr = GetLastError();
574 sv_setnv(sv, (NV)dwErr);
575 if (dwErr)
576 {
577 PerlProc_GetOSError(sv, dwErr);
578 }
579 else
580 sv_setpv(sv, "");
581 SetLastError(dwErr);
582 }
22fae026 583#else
0a378802
JH
584 sv_setnv(sv, (NV)errno);
585 sv_setpv(sv, errno ? Strerror(errno) : "");
28f23441 586#endif
88e89b8a 587#endif
22fae026 588#endif
cd39f2b6 589#endif
0a378802
JH
590 SvNOK_on(sv); /* what a wonderful hack! */
591 }
592 else if (strEQ(mg->mg_ptr+1, "NCODING"))
593 sv_setsv(sv, PL_encoding);
594 break;
79072805 595 case '\006': /* ^F */
3280af22 596 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 597 break;
a0d0e21e 598 case '\010': /* ^H */
3280af22 599 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 600 break;
9d116dd7 601 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
602 if (PL_inplace)
603 sv_setpv(sv, PL_inplace);
79072805 604 else
3280af22 605 sv_setsv(sv, &PL_sv_undef);
79072805 606 break;
ac27b0f5
NIS
607 case '\017': /* ^O & ^OPEN */
608 if (*(mg->mg_ptr+1) == '\0')
609 sv_setpv(sv, PL_osname);
610 else if (strEQ(mg->mg_ptr, "\017PEN")) {
611 if (!PL_compiling.cop_io)
612 sv_setsv(sv, &PL_sv_undef);
613 else {
614 sv_setsv(sv, PL_compiling.cop_io);
615 }
616 }
28f23441 617 break;
79072805 618 case '\020': /* ^P */
3280af22 619 sv_setiv(sv, (IV)PL_perldb);
79072805 620 break;
fb73857a 621 case '\023': /* ^S */
d58bf5aa 622 {
3280af22 623 if (PL_lex_state != LEX_NOTPARSING)
155aba94 624 (void)SvOK_off(sv);
3280af22 625 else if (PL_in_eval)
6dc8a9e4 626 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
627 else
628 sv_setiv(sv, 0);
d58bf5aa 629 }
fb73857a 630 break;
79072805 631 case '\024': /* ^T */
7c36658b 632 if (*(mg->mg_ptr+1) == '\0') {
88e89b8a 633#ifdef BIG_TIME
7c36658b 634 sv_setnv(sv, PL_basetime);
88e89b8a 635#else
7c36658b 636 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 637#endif
7c36658b
MS
638 }
639 else if (strEQ(mg->mg_ptr, "\024AINT"))
640 sv_setiv(sv, PL_tainting);
641 break;
46487f74 642 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
4438c4b7
JH
643 if (*(mg->mg_ptr+1) == '\0')
644 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
0a378802 645 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
d3a7d8c7
GS
646 if (PL_compiling.cop_warnings == pWARN_NONE ||
647 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7
JH
648 {
649 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
650 }
d3a7d8c7 651 else if (PL_compiling.cop_warnings == pWARN_ALL) {
4438c4b7 652 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
ac27b0f5 653 }
4438c4b7
JH
654 else {
655 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 656 }
d3a7d8c7 657 SvPOK_only(sv);
4438c4b7 658 }
0a378802 659 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
46487f74 660 sv_setiv(sv, (IV)PL_widesyscalls);
79072805
LW
661 break;
662 case '1': case '2': case '3': case '4':
663 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 664 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
665 I32 s1, t1;
666
a863c7d1
MB
667 /*
668 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
669 * XXX Does the new way break anything?
670 */
ffc61ed2 671 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 672 getparen:
d9f97599 673 if (paren <= rx->nparens &&
cf93c79d
IZ
674 (s1 = rx->startp[paren]) != -1 &&
675 (t1 = rx->endp[paren]) != -1)
bbce6d69 676 {
cf93c79d
IZ
677 i = t1 - s1;
678 s = rx->subbeg + s1;
01ec43d0 679 if (!rx->subbeg)
c2e66d9e
GS
680 break;
681
13f57bf8 682 getrx:
748a9306 683 if (i >= 0) {
b7953727 684 bool was_tainted = FALSE;
3280af22
NIS
685 if (PL_tainting) {
686 was_tainted = PL_tainted;
687 PL_tainted = FALSE;
13f57bf8 688 }
cf93c79d 689 sv_setpvn(sv, s, i);
53c4c00c 690 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
7e2040f0
GS
691 SvUTF8_on(sv);
692 else
693 SvUTF8_off(sv);
3280af22
NIS
694 if (PL_tainting)
695 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
748a9306
LW
696 break;
697 }
79072805 698 }
79072805 699 }
3280af22 700 sv_setsv(sv,&PL_sv_undef);
79072805
LW
701 break;
702 case '+':
aaa362c4 703 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 704 paren = rx->lastparen;
a0d0e21e
LW
705 if (paren)
706 goto getparen;
79072805 707 }
3280af22 708 sv_setsv(sv,&PL_sv_undef);
79072805 709 break;
a01268b5
JH
710 case '\016': /* ^N */
711 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
712 paren = rx->lastcloseparen;
713 if (paren)
714 goto getparen;
715 }
716 sv_setsv(sv,&PL_sv_undef);
717 break;
79072805 718 case '`':
aaa362c4 719 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
720 if ((s = rx->subbeg) && rx->startp[0] != -1) {
721 i = rx->startp[0];
13f57bf8 722 goto getrx;
79072805 723 }
79072805 724 }
3280af22 725 sv_setsv(sv,&PL_sv_undef);
79072805
LW
726 break;
727 case '\'':
aaa362c4 728 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
729 if (rx->subbeg && rx->endp[0] != -1) {
730 s = rx->subbeg + rx->endp[0];
731 i = rx->sublen - rx->endp[0];
13f57bf8 732 goto getrx;
79072805 733 }
79072805 734 }
3280af22 735 sv_setsv(sv,&PL_sv_undef);
79072805
LW
736 break;
737 case '.':
738#ifndef lint
3280af22 739 if (GvIO(PL_last_in_gv)) {
357c8808 740 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805
LW
741 }
742#endif
743 break;
744 case '?':
809a5acc 745 {
809a5acc 746 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 747#ifdef COMPLEX_STATUS
6b88bc9c
GS
748 LvTARGOFF(sv) = PL_statusvalue;
749 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 750#endif
809a5acc 751 }
79072805
LW
752 break;
753 case '^':
3280af22 754 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
755 if (s)
756 sv_setpv(sv,s);
757 else {
3280af22 758 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
759 sv_catpv(sv,"_TOP");
760 }
761 break;
762 case '~':
3280af22 763 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 764 if (!s)
3280af22 765 s = GvENAME(PL_defoutgv);
79072805
LW
766 sv_setpv(sv,s);
767 break;
768#ifndef lint
769 case '=':
3280af22 770 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
771 break;
772 case '-':
3280af22 773 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
774 break;
775 case '%':
3280af22 776 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805
LW
777 break;
778#endif
779 case ':':
780 break;
781 case '/':
782 break;
783 case '[':
3280af22 784 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
785 break;
786 case '|':
3280af22 787 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
788 break;
789 case ',':
79072805
LW
790 break;
791 case '\\':
b2ce0fda
JH
792 if (PL_ors_sv)
793 sv_setpv(sv,SvPVX(PL_ors_sv));
79072805
LW
794 break;
795 case '#':
3280af22 796 sv_setpv(sv,PL_ofmt);
79072805
LW
797 break;
798 case '!':
a5f75d66 799#ifdef VMS
65202027 800 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 801 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 802#else
88e89b8a
PP
803 {
804 int saveerrno = errno;
65202027 805 sv_setnv(sv, (NV)errno);
88e89b8a 806#ifdef OS2
ed344e4f
IZ
807 if (errno == errno_isOS2 || errno == errno_isOS2_set)
808 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 809 else
a5f75d66 810#endif
2304df62 811 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
812 errno = saveerrno;
813 }
814#endif
946ec16e 815 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
816 break;
817 case '<':
3280af22 818 sv_setiv(sv, (IV)PL_uid);
79072805
LW
819 break;
820 case '>':
3280af22 821 sv_setiv(sv, (IV)PL_euid);
79072805
LW
822 break;
823 case '(':
3280af22 824 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 825#ifdef HAS_GETGROUPS
785fb66b 826 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
a52cb5f7 827#endif
79072805
LW
828 goto add_groups;
829 case ')':
3280af22 830 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 831#ifdef HAS_GETGROUPS
785fb66b 832 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
a52cb5f7 833#endif
79072805 834 add_groups:
79072805 835#ifdef HAS_GETGROUPS
79072805 836 {
a0d0e21e 837 Groups_t gary[NGROUPS];
79072805 838 i = getgroups(NGROUPS,gary);
46fc3d4c 839 while (--i >= 0)
785fb66b 840 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
79072805
LW
841 }
842#endif
155aba94 843 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
844 break;
845 case '*':
846 break;
cd39f2b6 847#ifndef MACOS_TRADITIONAL
79072805
LW
848 case '0':
849 break;
cd39f2b6 850#endif
4d1ff10f 851#ifdef USE_5005THREADS
a863c7d1 852 case '@':
38a03e6e 853 sv_setsv(sv, thr->errsv);
a863c7d1 854 break;
4d1ff10f 855#endif /* USE_5005THREADS */
79072805 856 }
a0d0e21e 857 return 0;
79072805
LW
858}
859
860int
864dbfa3 861Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
862{
863 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
864
865 if (uf && uf->uf_val)
24f81a43 866 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
867 return 0;
868}
869
870int
864dbfa3 871Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
872{
873 register char *s;
88e89b8a 874 char *ptr;
5aabfad6 875 STRLEN len, klen;
1e422769 876
a0d0e21e 877 s = SvPV(sv,len);
5aabfad6 878 ptr = MgPV(mg,klen);
88e89b8a 879 my_setenv(ptr, s);
1e422769 880
a0d0e21e
LW
881#ifdef DYNAMIC_ENV_FETCH
882 /* We just undefd an environment var. Is a replacement */
883 /* waiting in the wings? */
884 if (!len) {
5aabfad6 885 SV **valp;
6b88bc9c 886 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 887 s = SvPV(*valp, len);
a0d0e21e
LW
888 }
889#endif
1e422769 890
39e571d4 891#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
892 /* And you'll never guess what the dog had */
893 /* in its mouth... */
3280af22 894 if (PL_tainting) {
1e422769
PP
895 MgTAINTEDDIR_off(mg);
896#ifdef VMS
5aabfad6 897 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
898 char pathbuf[256], eltbuf[256], *cp, *elt = s;
899 struct stat sbuf;
900 int i = 0, j = 0;
901
902 do { /* DCL$PATH may be a search list */
903 while (1) { /* as may dev portion of any element */
904 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
905 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
906 cando_by_name(S_IWUSR,0,elt) ) {
907 MgTAINTEDDIR_on(mg);
908 return 0;
909 }
910 }
911 if ((cp = strchr(elt, ':')) != Nullch)
912 *cp = '\0';
913 if (my_trnlnm(elt, eltbuf, j++))
914 elt = eltbuf;
915 else
916 break;
917 }
918 j = 0;
919 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
920 }
921#endif /* VMS */
5aabfad6 922 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 923 char *strend = s + len;
463ee0b2
LW
924
925 while (s < strend) {
96827780 926 char tmpbuf[256];
1e422769 927 struct stat st;
5f74f29c 928 I32 i;
96827780 929 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 930 s, strend, ':', &i);
463ee0b2 931 s++;
96827780
MB
932 if (i >= sizeof tmpbuf /* too long -- assume the worst */
933 || *tmpbuf != '/'
c6ed36e1 934 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 935 MgTAINTEDDIR_on(mg);
1e422769
PP
936 return 0;
937 }
463ee0b2 938 }
79072805
LW
939 }
940 }
39e571d4 941#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 942
79072805
LW
943 return 0;
944}
945
946int
864dbfa3 947Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 948{
2d8e6c8d
GS
949 STRLEN n_a;
950 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83
LW
951 return 0;
952}
953
88e89b8a 954int
864dbfa3 955Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a
PP
956{
957#if defined(VMS)
cea2e8a9 958 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 959#else
3280af22 960 if (PL_localizing) {
fb73857a 961 HE* entry;
2d8e6c8d 962 STRLEN n_a;
fb73857a
PP
963 magic_clear_all_env(sv,mg);
964 hv_iterinit((HV*)sv);
155aba94 965 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
966 I32 keylen;
967 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 968 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a
PP
969 }
970 }
971#endif
972 return 0;
973}
974
975int
864dbfa3 976Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 977{
ed79a026 978#if defined(VMS) || defined(EPOC)
cea2e8a9 979 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 980#else
df3728a2 981# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137
GS
982 PerlEnv_clearenv();
983# else
052c2dc6 984# ifdef USE_ENVIRON_ARRAY
7766f137 985# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
986 I32 i;
987
3280af22 988 if (environ == PL_origenviron)
f2517201 989 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
990 else
991 for (i = 0; environ[i]; i++)
f2517201 992 safesysfree(environ[i]);
7766f137 993# endif /* PERL_USE_SAFE_PUTENV */
f2517201 994
66b1d557
HM
995 environ[0] = Nullch;
996
052c2dc6
JH
997# endif /* USE_ENVIRON_ARRAY */
998# endif /* PERL_IMPLICIT_SYS || WIN32 */
999#endif /* VMS || EPC */
3e3baf6d 1000 return 0;
66b1d557
HM
1001}
1002
2e34cc90
CL
1003#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1004static int sig_handlers_initted = 0;
1005#endif
23ada85b 1006#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1007static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1008#endif
2e34cc90
CL
1009#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1010static int sig_defaulting[SIG_SIZE];
1011#endif
85b332e2 1012
64ca3a65 1013#ifndef PERL_MICRO
66b1d557 1014int
864dbfa3 1015Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1016{
1017 I32 i;
2d8e6c8d 1018 STRLEN n_a;
88e89b8a 1019 /* Are we fetching a signal entry? */
2d8e6c8d 1020 i = whichsig(MgPV(mg,n_a));
88e89b8a 1021 if (i) {
22c35a8c
GS
1022 if(PL_psig_ptr[i])
1023 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1024 else {
85b332e2 1025 Sighandler_t sigstate;
2e34cc90 1026 sigstate = rsignal_state(i);
23ada85b 1027#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
2e34cc90
CL
1028 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1029#endif
1030#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1031 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1032#endif
88e89b8a 1033 /* cache state so we don't fetch it again */
ff68c719 1034 if(sigstate == SIG_IGN)
88e89b8a
PP
1035 sv_setpv(sv,"IGNORE");
1036 else
3280af22 1037 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1038 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1039 SvTEMP_off(sv);
1040 }
1041 }
1042 return 0;
1043}
1044int
864dbfa3 1045Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1046{
1047 I32 i;
2d8e6c8d 1048 STRLEN n_a;
88e89b8a 1049 /* Are we clearing a signal entry? */
2d8e6c8d 1050 i = whichsig(MgPV(mg,n_a));
88e89b8a 1051 if (i) {
22c35a8c
GS
1052 if(PL_psig_ptr[i]) {
1053 SvREFCNT_dec(PL_psig_ptr[i]);
1054 PL_psig_ptr[i]=0;
88e89b8a 1055 }
22c35a8c
GS
1056 if(PL_psig_name[i]) {
1057 SvREFCNT_dec(PL_psig_name[i]);
1058 PL_psig_name[i]=0;
88e89b8a
PP
1059 }
1060 }
1061 return 0;
1062}
3d37d572 1063
0a8e0eff
NIS
1064void
1065Perl_raise_signal(pTHX_ int sig)
1066{
1067 /* Set a flag to say this signal is pending */
1068 PL_psig_pend[sig]++;
1069 /* And one to say _a_ signal is pending */
1070 PL_sig_pending = 1;
1071}
1072
1073Signal_t
1074Perl_csighandler(int sig)
1075{
1018e26f
NIS
1076#ifdef PERL_GET_SIG_CONTEXT
1077 dTHXa(PERL_GET_SIG_CONTEXT);
1078#else
85b332e2
CL
1079 dTHX;
1080#endif
23ada85b 1081#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1082 (void) rsignal(sig, &Perl_csighandler);
1083 if (sig_ignoring[sig]) return;
1084#endif
2e34cc90
CL
1085#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1086 if (sig_defaulting[sig])
1087#ifdef KILL_BY_SIGPRC
1088 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1089#else
1090 exit(1);
1091#endif
1092#endif
1093
0a8e0eff
NIS
1094#ifdef PERL_OLD_SIGNALS
1095 /* Call the perl level handler now with risk we may be in malloc() etc. */
1096 (*PL_sighandlerp)(sig);
1097#else
0a8e0eff
NIS
1098 Perl_raise_signal(aTHX_ sig);
1099#endif
1100}
1101
2e34cc90
CL
1102#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1103void
1104Perl_csighandler_init(void)
1105{
1106 int sig;
1107 if (sig_handlers_initted) return;
1108
1109 for (sig = 1; sig < SIG_SIZE; sig++) {
1110#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1111 dTHX;
2e34cc90
CL
1112 sig_defaulting[sig] = 1;
1113 (void) rsignal(sig, &Perl_csighandler);
1114#endif
1115#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1116 sig_ignoring[sig] = 0;
1117#endif
1118 }
1119 sig_handlers_initted = 1;
1120}
1121#endif
1122
0a8e0eff
NIS
1123void
1124Perl_despatch_signals(pTHX)
1125{
1126 int sig;
1127 PL_sig_pending = 0;
1128 for (sig = 1; sig < SIG_SIZE; sig++) {
1129 if (PL_psig_pend[sig]) {
1130 PL_psig_pend[sig] = 0;
1131 (*PL_sighandlerp)(sig);
1132 }
1133 }
1134}
1135
85e6fe83 1136int
864dbfa3 1137Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1138{
1139 register char *s;
1140 I32 i;
b7953727 1141 SV** svp = 0;
e72dc28c 1142 STRLEN len;
a0d0e21e 1143
e72dc28c 1144 s = MgPV(mg,len);
748a9306
LW
1145 if (*s == '_') {
1146 if (strEQ(s,"__DIE__"))
3280af22 1147 svp = &PL_diehook;
748a9306 1148 else if (strEQ(s,"__WARN__"))
3280af22 1149 svp = &PL_warnhook;
748a9306 1150 else
cea2e8a9 1151 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1152 i = 0;
4633a7c4
LW
1153 if (*svp) {
1154 SvREFCNT_dec(*svp);
1155 *svp = 0;
1156 }
748a9306
LW
1157 }
1158 else {
1159 i = whichsig(s); /* ...no, a brick */
1160 if (!i) {
e476b1b5 1161 if (ckWARN(WARN_SIGNAL))
cea2e8a9 1162 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
748a9306
LW
1163 return 0;
1164 }
2e34cc90
CL
1165#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1166 if (!sig_handlers_initted) Perl_csighandler_init();
1167#endif
23ada85b 1168#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1169 sig_ignoring[i] = 0;
1170#endif
2e34cc90
CL
1171#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1172 sig_defaulting[i] = 0;
1173#endif
22c35a8c
GS
1174 SvREFCNT_dec(PL_psig_name[i]);
1175 SvREFCNT_dec(PL_psig_ptr[i]);
1176 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1177 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1178 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1179 SvREADONLY_on(PL_psig_name[i]);
748a9306 1180 }
a0d0e21e 1181 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 1182 if (i)
0a8e0eff 1183 (void)rsignal(i, &Perl_csighandler);
748a9306
LW
1184 else
1185 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
1186 return 0;
1187 }
e72dc28c 1188 s = SvPV_force(sv,len);
748a9306 1189 if (strEQ(s,"IGNORE")) {
85b332e2 1190 if (i) {
23ada85b 1191#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1192 sig_ignoring[i] = 1;
1193 (void)rsignal(i, &Perl_csighandler);
1194#else
ff68c719 1195 (void)rsignal(i, SIG_IGN);
85b332e2
CL
1196#endif
1197 } else
748a9306
LW
1198 *svp = 0;
1199 }
1200 else if (strEQ(s,"DEFAULT") || !*s) {
1201 if (i)
2e34cc90
CL
1202#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1203 {
1204 sig_defaulting[i] = 1;
1205 (void)rsignal(i, &Perl_csighandler);
1206 }
1207#else
ff68c719 1208 (void)rsignal(i, SIG_DFL);
2e34cc90 1209#endif
748a9306
LW
1210 else
1211 *svp = 0;
1212 }
79072805 1213 else {
5aabfad6
PP
1214 /*
1215 * We should warn if HINT_STRICT_REFS, but without
1216 * access to a known hint bit in a known OP, we can't
1217 * tell whether HINT_STRICT_REFS is in force or not.
1218 */
46fc3d4c 1219 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1220 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1221 if (i)
0a8e0eff 1222 (void)rsignal(i, &Perl_csighandler);
748a9306
LW
1223 else
1224 *svp = SvREFCNT_inc(sv);
79072805
LW
1225 }
1226 return 0;
1227}
64ca3a65 1228#endif /* !PERL_MICRO */
79072805
LW
1229
1230int
864dbfa3 1231Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1232{
3280af22 1233 PL_sub_generation++;
463ee0b2
LW
1234 return 0;
1235}
1236
1237int
864dbfa3 1238Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1239{
a0d0e21e 1240 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1241 PL_amagic_generation++;
463ee0b2 1242
a0d0e21e
LW
1243 return 0;
1244}
463ee0b2 1245
946ec16e 1246int
864dbfa3 1247Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1248{
1249 HV *hv = (HV*)LvTARG(sv);
6ff81951 1250 I32 i = 0;
497b47a8 1251
6ff81951 1252 if (hv) {
497b47a8
JH
1253 (void) hv_iterinit(hv);
1254 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1255 i = HvKEYS(hv);
1256 else {
1257 while (hv_iternext(hv))
1258 i++;
1259 }
6ff81951
GS
1260 }
1261
1262 sv_setiv(sv, (IV)i);
1263 return 0;
1264}
1265
1266int
864dbfa3 1267Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1268{
1269 if (LvTARG(sv)) {
1270 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1271 }
1272 return 0;
ac27b0f5 1273}
946ec16e 1274
e336de0d 1275/* caller is responsible for stack switching/cleanup */
565764a8 1276STATIC int
cea2e8a9 1277S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1278{
1279 dSP;
463ee0b2 1280
924508f0
GS
1281 PUSHMARK(SP);
1282 EXTEND(SP, n);
33c27489 1283 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1284 if (n > 1) {
93965878 1285 if (mg->mg_ptr) {
565764a8 1286 if (mg->mg_len >= 0)
79cb57f6 1287 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1288 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1289 PUSHs((SV*)mg->mg_ptr);
1290 }
14befaf4 1291 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1292 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1293 }
1294 }
1295 if (n > 2) {
1296 PUSHs(val);
88e89b8a 1297 }
463ee0b2
LW
1298 PUTBACK;
1299
864dbfa3 1300 return call_method(meth, flags);
946ec16e
PP
1301}
1302
76e3520e 1303STATIC int
cea2e8a9 1304S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1305{
1306 dSP;
463ee0b2 1307
a0d0e21e
LW
1308 ENTER;
1309 SAVETMPS;
e788e7d3 1310 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1311
33c27489 1312 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1313 sv_setsv(sv, *PL_stack_sp--);
93965878 1314 }
463ee0b2 1315
d3acc0f7 1316 POPSTACK;
a0d0e21e
LW
1317 FREETMPS;
1318 LEAVE;
1319 return 0;
1320}
463ee0b2 1321
a0d0e21e 1322int
864dbfa3 1323Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1324{
1325 magic_methpack(sv,mg,"FETCH");
1326 if (mg->mg_ptr)
1327 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1328 return 0;
1329}
1330
1331int
864dbfa3 1332Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1333{
1334 dSP;
a60c0954 1335 ENTER;
e788e7d3 1336 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1337 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1338 POPSTACK;
a60c0954 1339 LEAVE;
463ee0b2
LW
1340 return 0;
1341}
1342
1343int
864dbfa3 1344Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1345{
a0d0e21e
LW
1346 return magic_methpack(sv,mg,"DELETE");
1347}
463ee0b2 1348
93965878
NIS
1349
1350U32
864dbfa3 1351Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1352{
e336de0d 1353 dSP;
93965878
NIS
1354 U32 retval = 0;
1355
1356 ENTER;
1357 SAVETMPS;
e788e7d3 1358 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1359 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1360 sv = *PL_stack_sp--;
a60c0954 1361 retval = (U32) SvIV(sv)-1;
93965878 1362 }
d3acc0f7 1363 POPSTACK;
93965878
NIS
1364 FREETMPS;
1365 LEAVE;
1366 return retval;
1367}
1368
cea2e8a9
GS
1369int
1370Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1371{
1372 dSP;
463ee0b2 1373
e336de0d 1374 ENTER;
e788e7d3 1375 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1376 PUSHMARK(SP);
33c27489 1377 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1378 PUTBACK;
864dbfa3 1379 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1380 POPSTACK;
a60c0954 1381 LEAVE;
463ee0b2
LW
1382 return 0;
1383}
1384
1385int
864dbfa3 1386Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1387{
463ee0b2 1388 dSP;
dff6d3cd 1389 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1390
1391 ENTER;
a0d0e21e 1392 SAVETMPS;
e788e7d3 1393 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1394 PUSHMARK(SP);
1395 EXTEND(SP, 2);
33c27489 1396 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1397 if (SvOK(key))
1398 PUSHs(key);
1399 PUTBACK;
1400
864dbfa3 1401 if (call_method(meth, G_SCALAR))
3280af22 1402 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1403
d3acc0f7 1404 POPSTACK;
a0d0e21e
LW
1405 FREETMPS;
1406 LEAVE;
79072805
LW
1407 return 0;
1408}
1409
1410int
864dbfa3 1411Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1412{
1413 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1414}
a0d0e21e
LW
1415
1416int
864dbfa3 1417Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1418{
1419 OP *o;
1420 I32 i;
1421 GV* gv;
1422 SV** svp;
2d8e6c8d 1423 STRLEN n_a;
79072805 1424
3280af22 1425 gv = PL_DBline;
79072805 1426 i = SvTRUE(sv);
188ea221 1427 svp = av_fetch(GvAV(gv),
2d8e6c8d 1428 atoi(MgPV(mg,n_a)), FALSE);
57b2e452 1429 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
93a17b20 1430 o->op_private = i;
79072805
LW
1431 return 0;
1432}
1433
1434int
864dbfa3 1435Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1436{
3280af22 1437 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1438 return 0;
1439}
1440
1441int
864dbfa3 1442Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1443{
3280af22 1444 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1445 return 0;
1446}
1447
1448int
864dbfa3 1449Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1450{
1451 SV* lsv = LvTARG(sv);
ac27b0f5 1452
a0d0e21e 1453 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1454 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1455 if (mg && mg->mg_len >= 0) {
a0ed51b3 1456 I32 i = mg->mg_len;
7e2040f0 1457 if (DO_UTF8(lsv))
a0ed51b3
LW
1458 sv_pos_b2u(lsv, &i);
1459 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1460 return 0;
1461 }
1462 }
1463 (void)SvOK_off(sv);
1464 return 0;
1465}
1466
1467int
864dbfa3 1468Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1469{
1470 SV* lsv = LvTARG(sv);
1471 SSize_t pos;
1472 STRLEN len;
c00206c8 1473 STRLEN ulen = 0;
a0d0e21e
LW
1474
1475 mg = 0;
ac27b0f5 1476
a0d0e21e 1477 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1478 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1479 if (!mg) {
1480 if (!SvOK(sv))
1481 return 0;
14befaf4
DM
1482 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1483 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1484 }
1485 else if (!SvOK(sv)) {
565764a8 1486 mg->mg_len = -1;
a0d0e21e
LW
1487 return 0;
1488 }
1489 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1490
c485e607 1491 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1492
7e2040f0 1493 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1494 ulen = sv_len_utf8(lsv);
1495 if (ulen)
1496 len = ulen;
a0ed51b3
LW
1497 }
1498
a0d0e21e
LW
1499 if (pos < 0) {
1500 pos += len;
1501 if (pos < 0)
1502 pos = 0;
1503 }
1504 else if (pos > len)
1505 pos = len;
a0ed51b3
LW
1506
1507 if (ulen) {
1508 I32 p = pos;
1509 sv_pos_u2b(lsv, &p, 0);
1510 pos = p;
1511 }
1512
565764a8 1513 mg->mg_len = pos;
71be2cbc 1514 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1515
79072805
LW
1516 return 0;
1517}
1518
1519int
864dbfa3 1520Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1521{
8646b087
PP
1522 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1523 SvFAKE_off(sv);
946ec16e 1524 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1525 SvFAKE_on(sv);
1526 }
1527 else
946ec16e 1528 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1529 return 0;
1530}
1531
1532int
864dbfa3 1533Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1534{
1535 register char *s;
1536 GV* gv;
2d8e6c8d 1537 STRLEN n_a;
79072805
LW
1538
1539 if (!SvOK(sv))
1540 return 0;
2d8e6c8d 1541 s = SvPV(sv, n_a);
79072805
LW
1542 if (*s == '*' && s[1])
1543 s++;
85e6fe83 1544 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1545 if (sv == (SV*)gv)
1546 return 0;
1547 if (GvGP(sv))
88e89b8a 1548 gp_free((GV*)sv);
79072805 1549 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1550 return 0;
1551}
1552
1553int
864dbfa3 1554Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1555{
1556 STRLEN len;
1557 SV *lsv = LvTARG(sv);
1558 char *tmps = SvPV(lsv,len);
1559 I32 offs = LvTARGOFF(sv);
1560 I32 rem = LvTARGLEN(sv);
1561
9aa983d2
JH
1562 if (SvUTF8(lsv))
1563 sv_pos_u2b(lsv, &offs, &rem);
6ff81951
GS
1564 if (offs > len)
1565 offs = len;
1566 if (rem + offs > len)
1567 rem = len - offs;
1568 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1569 if (SvUTF8(lsv))
2ef4b674 1570 SvUTF8_on(sv);
6ff81951
GS
1571 return 0;
1572}
1573
1574int
864dbfa3 1575Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1576{
9aa983d2
JH
1577 STRLEN len;
1578 char *tmps = SvPV(sv, len);
1579 SV *lsv = LvTARG(sv);
1580 I32 lvoff = LvTARGOFF(sv);
1581 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1582
1aa99e6b 1583 if (DO_UTF8(sv)) {
9aa983d2
JH
1584 sv_utf8_upgrade(lsv);
1585 sv_pos_u2b(lsv, &lvoff, &lvlen);
1586 sv_insert(lsv, lvoff, lvlen, tmps, len);
1587 SvUTF8_on(lsv);
1588 }
9bf12eaf 1589 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1590 sv_pos_u2b(lsv, &lvoff, &lvlen);
e95af362 1591 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1592 sv_insert(lsv, lvoff, lvlen, tmps, len);
1593 Safefree(tmps);
1aa99e6b
IH
1594 }
1595 else
9aa983d2 1596 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1597
79072805
LW
1598 return 0;
1599}
1600
1601int
864dbfa3 1602Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1603{
565764a8 1604 TAINT_IF((mg->mg_len & 1) ||
155aba94 1605 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1606 return 0;
1607}
1608
1609int
864dbfa3 1610Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1611{
3280af22
NIS
1612 if (PL_localizing) {
1613 if (PL_localizing == 1)
565764a8 1614 mg->mg_len <<= 1;
748a9306 1615 else
565764a8 1616 mg->mg_len >>= 1;
a0d0e21e 1617 }
3280af22 1618 else if (PL_tainted)
565764a8 1619 mg->mg_len |= 1;
748a9306 1620 else
565764a8 1621 mg->mg_len &= ~1;
463ee0b2
LW
1622 return 0;
1623}
1624
1625int
864dbfa3 1626Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1627{
1628 SV *lsv = LvTARG(sv);
6ff81951
GS
1629
1630 if (!lsv) {
155aba94 1631 (void)SvOK_off(sv);
6ff81951
GS
1632 return 0;
1633 }
6ff81951 1634
81e118e0 1635 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1636 return 0;
1637}
1638
1639int
864dbfa3 1640Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1641{
1642 do_vecset(sv); /* XXX slurp this routine */
1643 return 0;
1644}
1645
1646int
864dbfa3 1647Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1648{
71be2cbc 1649 SV *targ = Nullsv;
5f05dabc 1650 if (LvTARGLEN(sv)) {
68dc0745 1651 if (mg->mg_obj) {
74e13ce4
GS
1652 SV *ahv = LvTARG(sv);
1653 if (SvTYPE(ahv) == SVt_PVHV) {
1654 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1655 if (he)
1656 targ = HeVAL(he);
1657 }
1658 else {
1659 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1660 if (svp)
1661 targ = *svp;
1662 }
68dc0745
PP
1663 }
1664 else {
3c78fafa 1665 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1666 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1667 targ = AvARRAY(av)[LvTARGOFF(sv)];
1668 }
3280af22 1669 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1670 /* somebody else defined it for us */
1671 SvREFCNT_dec(LvTARG(sv));
1672 LvTARG(sv) = SvREFCNT_inc(targ);
1673 LvTARGLEN(sv) = 0;
1674 SvREFCNT_dec(mg->mg_obj);
1675 mg->mg_obj = Nullsv;
1676 mg->mg_flags &= ~MGf_REFCOUNTED;
1677 }
5f05dabc 1678 }
71be2cbc
PP
1679 else
1680 targ = LvTARG(sv);
3280af22 1681 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1682 return 0;
1683}
1684
1685int
864dbfa3 1686Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1687{
1688 if (LvTARGLEN(sv))
68dc0745
PP
1689 vivify_defelem(sv);
1690 if (LvTARG(sv)) {
5f05dabc 1691 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1692 SvSETMAGIC(LvTARG(sv));
1693 }
5f05dabc
PP
1694 return 0;
1695}
1696
71be2cbc 1697void
864dbfa3 1698Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1699{
74e13ce4
GS
1700 MAGIC *mg;
1701 SV *value = Nullsv;
71be2cbc 1702
14befaf4 1703 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1704 return;
68dc0745 1705 if (mg->mg_obj) {
74e13ce4 1706 SV *ahv = LvTARG(sv);
2d8e6c8d 1707 STRLEN n_a;
74e13ce4 1708 if (SvTYPE(ahv) == SVt_PVHV) {
af7f9c15 1709 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1710 if (he)
1711 value = HeVAL(he);
1712 }
1713 else {
af7f9c15 1714 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1715 if (svp)
1716 value = *svp;
1717 }
3280af22 1718 if (!value || value == &PL_sv_undef)
cea2e8a9 1719 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1720 }
68dc0745
PP
1721 else {
1722 AV* av = (AV*)LvTARG(sv);
5aabfad6 1723 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1724 LvTARG(sv) = Nullsv; /* array can't be extended */
1725 else {
1726 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1727 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1728 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1729 }
1730 }
3e3baf6d 1731 (void)SvREFCNT_inc(value);
68dc0745
PP
1732 SvREFCNT_dec(LvTARG(sv));
1733 LvTARG(sv) = value;
71be2cbc 1734 LvTARGLEN(sv) = 0;
68dc0745
PP
1735 SvREFCNT_dec(mg->mg_obj);
1736 mg->mg_obj = Nullsv;
1737 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1738}
1739
1740int
864dbfa3 1741Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1742{
1743 AV *av = (AV*)mg->mg_obj;
1744 SV **svp = AvARRAY(av);
1745 I32 i = AvFILLp(av);
1746 while (i >= 0) {
1747 if (svp[i] && svp[i] != &PL_sv_undef) {
1748 if (!SvWEAKREF(svp[i]))
cea2e8a9 1749 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1750 /* XXX Should we check that it hasn't changed? */
1751 SvRV(svp[i]) = 0;
155aba94 1752 (void)SvOK_off(svp[i]);
810b8aa5
GS
1753 SvWEAKREF_off(svp[i]);
1754 svp[i] = &PL_sv_undef;
1755 }
1756 i--;
1757 }
1758 return 0;
1759}
1760
1761int
864dbfa3 1762Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1763{
565764a8 1764 mg->mg_len = -1;
c6496cc7 1765 SvSCREAM_off(sv);
93a17b20
LW
1766 return 0;
1767}
1768
1769int
864dbfa3 1770Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1771{
14befaf4 1772 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1773 SvVALID_off(sv);
1774 return 0;
1775}
1776
1777int
864dbfa3 1778Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1779{
14befaf4 1780 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1781 SvCOMPILED_off(sv);
1782 return 0;
1783}
1784
1785int
864dbfa3 1786Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1787{
1788 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1789
1790 if (uf && uf->uf_set)
24f81a43 1791 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
1792 return 0;
1793}
1794
c277df42 1795int
864dbfa3 1796Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1797{
1798 regexp *re = (regexp *)mg->mg_obj;
1799 ReREFCNT_dec(re);
1800 return 0;
1801}
1802
7a4c00b4 1803#ifdef USE_LOCALE_COLLATE
79072805 1804int
864dbfa3 1805Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1806{
1807 /*
838b5b74 1808 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1809 * and vanished with a faint plop.
1810 */
7a4c00b4
PP
1811 if (mg->mg_ptr) {
1812 Safefree(mg->mg_ptr);
1813 mg->mg_ptr = NULL;
565764a8 1814 mg->mg_len = -1;
7a4c00b4 1815 }
bbce6d69
PP
1816 return 0;
1817}
7a4c00b4 1818#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1819
1820int
864dbfa3 1821Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1822{
1823 register char *s;
1824 I32 i;
8990e307 1825 STRLEN len;
79072805 1826 switch (*mg->mg_ptr) {
748a9306 1827 case '\001': /* ^A */
3280af22 1828 sv_setsv(PL_bodytarget, sv);
748a9306 1829 break;
49460fe6
NIS
1830 case '\003': /* ^C */
1831 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1832 break;
1833
79072805 1834 case '\004': /* ^D */
aea4f609 1835 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
79072805
LW
1836 DEBUG_x(dump_all());
1837 break;
28f23441 1838 case '\005': /* ^E */
0a378802 1839 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 1840#ifdef MACOS_TRADITIONAL
0a378802 1841 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1842#else
cd39f2b6 1843# ifdef VMS
0a378802 1844 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1845# else
cd39f2b6 1846# ifdef WIN32
0a378802 1847 SetLastError( SvIV(sv) );
cd39f2b6 1848# else
9fed8b87 1849# ifdef OS2
0a378802 1850 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 1851# else
0a378802
JH
1852 /* will anyone ever use this? */
1853 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 1854# endif
048c1ddf
IZ
1855# endif
1856# endif
22fae026 1857#endif
0a378802
JH
1858 }
1859 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1860 if (PL_encoding)
1861 sv_setsv(PL_encoding, sv);
1862 else
1863 PL_encoding = newSVsv(sv);
1864 }
79072805 1865 case '\006': /* ^F */
3280af22 1866 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1867 break;
a0d0e21e 1868 case '\010': /* ^H */
3280af22 1869 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 1870 break;
9d116dd7 1871 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
1872 if (PL_inplace)
1873 Safefree(PL_inplace);
79072805 1874 if (SvOK(sv))
2d8e6c8d 1875 PL_inplace = savepv(SvPV(sv,len));
79072805 1876 else
3280af22 1877 PL_inplace = Nullch;
79072805 1878 break;
28f23441 1879 case '\017': /* ^O */
ac27b0f5
NIS
1880 if (*(mg->mg_ptr+1) == '\0') {
1881 if (PL_osname)
1882 Safefree(PL_osname);
1883 if (SvOK(sv))
1884 PL_osname = savepv(SvPV(sv,len));
1885 else
1886 PL_osname = Nullch;
1887 }
1888 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1889 if (!PL_compiling.cop_io)
1890 PL_compiling.cop_io = newSVsv(sv);
1891 else
1892 sv_setsv(PL_compiling.cop_io,sv);
1893 }
28f23441 1894 break;
79072805 1895 case '\020': /* ^P */
3280af22 1896 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
1897 if (PL_perldb && !PL_DBsingle)
1898 init_debugger();
79072805
LW
1899 break;
1900 case '\024': /* ^T */
88e89b8a 1901#ifdef BIG_TIME
6b88bc9c 1902 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 1903#else
3280af22 1904 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1905#endif
79072805 1906 break;
46487f74 1907 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
4438c4b7
JH
1908 if (*(mg->mg_ptr+1) == '\0') {
1909 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1910 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 1911 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 1912 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 1913 }
599cee73 1914 }
0a378802 1915 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 1916 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
1917 if (!SvPOK(sv) && PL_localizing) {
1918 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 1919 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
1920 break;
1921 }
f4fc7782 1922 {
b5477537 1923 STRLEN len, i;
d3a7d8c7 1924 int accumulate = 0 ;
f4fc7782 1925 int any_fatals = 0 ;
d3a7d8c7 1926 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
1927 for (i = 0 ; i < len ; ++i) {
1928 accumulate |= ptr[i] ;
1929 any_fatals |= (ptr[i] & 0xAA) ;
1930 }
d3a7d8c7
GS
1931 if (!accumulate)
1932 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
1933 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1934 PL_compiling.cop_warnings = pWARN_ALL;
1935 PL_dowarn |= G_WARN_ONCE ;
1936 }
d3a7d8c7
GS
1937 else {
1938 if (specialWARN(PL_compiling.cop_warnings))
1939 PL_compiling.cop_warnings = newSVsv(sv) ;
1940 else
1941 sv_setsv(PL_compiling.cop_warnings, sv);
1942 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1943 PL_dowarn |= G_WARN_ONCE ;
1944 }
f4fc7782 1945
d3a7d8c7 1946 }
4438c4b7 1947 }
971a9dd3 1948 }
0a378802 1949 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
46487f74 1950 PL_widesyscalls = SvTRUE(sv);
79072805
LW
1951 break;
1952 case '.':
3280af22
NIS
1953 if (PL_localizing) {
1954 if (PL_localizing == 1)
7766f137 1955 SAVESPTR(PL_last_in_gv);
748a9306 1956 }
3280af22 1957 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 1958 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
1959 break;
1960 case '^':
3280af22 1961 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1962 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1963 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1964 break;
1965 case '~':
3280af22 1966 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1967 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1968 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1969 break;
1970 case '=':
632db599 1971 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1972 break;
1973 case '-':
632db599 1974 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
1975 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1976 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
1977 break;
1978 case '%':
632db599 1979 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1980 break;
1981 case '|':
4b65379b 1982 {
3280af22 1983 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
1984 if(!io)
1985 break;
4b65379b
CS
1986 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1987 IoFLAGS(io) &= ~IOf_FLUSH;
1988 else {
1989 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1990 PerlIO *ofp = IoOFP(io);
1991 if (ofp)
1992 (void)PerlIO_flush(ofp);
1993 IoFLAGS(io) |= IOf_FLUSH;
1994 }
1995 }
79072805
LW
1996 }
1997 break;
1998 case '*':
463ee0b2 1999 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
3280af22 2000 PL_multiline = (i != 0);
79072805
LW
2001 break;
2002 case '/':
3280af22 2003 SvREFCNT_dec(PL_rs);
8bfdd7d9 2004 PL_rs = newSVsv(sv);
79072805
LW
2005 break;
2006 case '\\':
7889fe52
NIS
2007 if (PL_ors_sv)
2008 SvREFCNT_dec(PL_ors_sv);
009c130f 2009 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2010 PL_ors_sv = newSVsv(sv);
009c130f 2011 }
e3c19b7b 2012 else {
7889fe52 2013 PL_ors_sv = Nullsv;
e3c19b7b 2014 }
79072805
LW
2015 break;
2016 case ',':
7889fe52
NIS
2017 if (PL_ofs_sv)
2018 SvREFCNT_dec(PL_ofs_sv);
2019 if (SvOK(sv) || SvGMAGICAL(sv)) {
2020 PL_ofs_sv = newSVsv(sv);
2021 }
2022 else {
2023 PL_ofs_sv = Nullsv;
2024 }
79072805
LW
2025 break;
2026 case '#':
3280af22
NIS
2027 if (PL_ofmt)
2028 Safefree(PL_ofmt);
2d8e6c8d 2029 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
2030 break;
2031 case '[':
3280af22 2032 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2033 break;
2034 case '?':
ff0cee69 2035#ifdef COMPLEX_STATUS
6b88bc9c
GS
2036 if (PL_localizing == 2) {
2037 PL_statusvalue = LvTARGOFF(sv);
2038 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2039 }
2040 else
2041#endif
2042#ifdef VMSISH_STATUS
2043 if (VMSISH_STATUS)
2044 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2045 else
2046#endif
2047 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2048 break;
2049 case '!':
91487cfc
JH
2050 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2051 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
2052 break;
2053 case '<':
3280af22
NIS
2054 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2055 if (PL_delaymagic) {
2056 PL_delaymagic |= DM_RUID;
79072805
LW
2057 break; /* don't do magic till later */
2058 }
2059#ifdef HAS_SETRUID
b28d0864 2060 (void)setruid((Uid_t)PL_uid);
79072805
LW
2061#else
2062#ifdef HAS_SETREUID
3280af22 2063 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2064#else
85e6fe83 2065#ifdef HAS_SETRESUID
b28d0864 2066 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2067#else
b28d0864
NIS
2068 if (PL_uid == PL_euid) /* special case $< = $> */
2069 (void)PerlProc_setuid(PL_uid);
a0d0e21e 2070 else {
d8eceb89 2071 PL_uid = PerlProc_getuid();
cea2e8a9 2072 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2073 }
79072805
LW
2074#endif
2075#endif
85e6fe83 2076#endif
d8eceb89 2077 PL_uid = PerlProc_getuid();
3280af22 2078 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2079 break;
2080 case '>':
3280af22
NIS
2081 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2082 if (PL_delaymagic) {
2083 PL_delaymagic |= DM_EUID;
79072805
LW
2084 break; /* don't do magic till later */
2085 }
2086#ifdef HAS_SETEUID
3280af22 2087 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2088#else
2089#ifdef HAS_SETREUID
b28d0864 2090 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2091#else
2092#ifdef HAS_SETRESUID
6b88bc9c 2093 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2094#else
b28d0864
NIS
2095 if (PL_euid == PL_uid) /* special case $> = $< */
2096 PerlProc_setuid(PL_euid);
a0d0e21e 2097 else {
e8ee3774 2098 PL_euid = PerlProc_geteuid();
cea2e8a9 2099 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2100 }
79072805
LW
2101#endif
2102#endif
85e6fe83 2103#endif
d8eceb89 2104 PL_euid = PerlProc_geteuid();
3280af22 2105 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2106 break;
2107 case '(':
3280af22
NIS
2108 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2109 if (PL_delaymagic) {
2110 PL_delaymagic |= DM_RGID;
79072805
LW
2111 break; /* don't do magic till later */
2112 }
2113#ifdef HAS_SETRGID
b28d0864 2114 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2115#else
2116#ifdef HAS_SETREGID
3280af22 2117 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2118#else
2119#ifdef HAS_SETRESGID
b28d0864 2120 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2121#else
b28d0864
NIS
2122 if (PL_gid == PL_egid) /* special case $( = $) */
2123 (void)PerlProc_setgid(PL_gid);
748a9306 2124 else {
d8eceb89 2125 PL_gid = PerlProc_getgid();
cea2e8a9 2126 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2127 }
79072805
LW
2128#endif
2129#endif
85e6fe83 2130#endif
d8eceb89 2131 PL_gid = PerlProc_getgid();
3280af22 2132 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2133 break;
2134 case ')':
5cd24f17
PP
2135#ifdef HAS_SETGROUPS
2136 {
2d8e6c8d 2137 char *p = SvPV(sv, len);
5cd24f17
PP
2138 Groups_t gary[NGROUPS];
2139
5cd24f17
PP
2140 while (isSPACE(*p))
2141 ++p;
2d4389e4 2142 PL_egid = Atol(p);
5cd24f17
PP
2143 for (i = 0; i < NGROUPS; ++i) {
2144 while (*p && !isSPACE(*p))
2145 ++p;
2146 while (isSPACE(*p))
2147 ++p;
2148 if (!*p)
2149 break;
2d4389e4 2150 gary[i] = Atol(p);
5cd24f17 2151 }
8cc95fdb
PP
2152 if (i)
2153 (void)setgroups(i, gary);
5cd24f17
PP
2154 }
2155#else /* HAS_SETGROUPS */
b28d0864 2156 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2157#endif /* HAS_SETGROUPS */
3280af22
NIS
2158 if (PL_delaymagic) {
2159 PL_delaymagic |= DM_EGID;
79072805
LW
2160 break; /* don't do magic till later */
2161 }
2162#ifdef HAS_SETEGID
3280af22 2163 (void)setegid((Gid_t)PL_egid);
79072805
LW
2164#else
2165#ifdef HAS_SETREGID
b28d0864 2166 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2167#else
2168#ifdef HAS_SETRESGID
b28d0864 2169 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2170#else
b28d0864
NIS
2171 if (PL_egid == PL_gid) /* special case $) = $( */
2172 (void)PerlProc_setgid(PL_egid);
748a9306 2173 else {
d8eceb89 2174 PL_egid = PerlProc_getegid();
cea2e8a9 2175 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2176 }
79072805
LW
2177#endif
2178#endif
85e6fe83 2179#endif
d8eceb89 2180 PL_egid = PerlProc_getegid();
3280af22 2181 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2182 break;
2183 case ':':
2d8e6c8d 2184 PL_chopset = SvPV_force(sv,len);
79072805 2185 break;
cd39f2b6 2186#ifndef MACOS_TRADITIONAL
79072805 2187 case '0':
4bc88a62
PS
2188#ifdef HAS_SETPROCTITLE
2189 /* The BSDs don't show the argv[] in ps(1) output, they
2190 * show a string from the process struct and provide
2191 * the setproctitle() routine to manipulate that. */
2192 {
2193 s = SvPV(sv, len);
98b76f99 2194# if __FreeBSD_version > 410001
9aad2c0e 2195 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2196 * but not the "(perl) suffix from the ps(1)
2197 * output, because that's what ps(1) shows if the
2198 * argv[] is modified. */
6f2ad931 2199 setproctitle("-%s", s);
9aad2c0e 2200# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2201 /* This doesn't really work if you assume that
2202 * $0 = 'foobar'; will wipe out 'perl' from the $0
2203 * because in ps(1) output the result will be like
2204 * sprintf("perl: %s (perl)", s)
2205 * I guess this is a security feature:
2206 * one (a user process) cannot get rid of the original name.
2207 * --jhi */
2208 setproctitle("%s", s);
2209# endif
2210 }
2211#endif
3280af22
NIS
2212 if (!PL_origalen) {
2213 s = PL_origargv[0];
79072805
LW
2214 s += strlen(s);
2215 /* See if all the arguments are contiguous in memory */
3280af22
NIS
2216 for (i = 1; i < PL_origargc; i++) {
2217 if (PL_origargv[i] == s + 1
fb73857a 2218#ifdef OS2
6b88bc9c 2219 || PL_origargv[i] == s + 2
ac27b0f5 2220#endif
fb73857a 2221 )
379c4362
GS
2222 {
2223 ++s;
2224 s += strlen(s); /* this one is ok too */
2225 }
fb73857a
PP
2226 else
2227 break;
79072805 2228 }
bbce6d69 2229 /* can grab env area too? */
764df951 2230 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
66b1d557 2231 my_setenv("NoNe SuCh", Nullch);
79072805 2232 /* force copy of environment */
3280af22 2233 for (i = 0; PL_origenviron[i]; i++)
379c4362
GS
2234 if (PL_origenviron[i] == s + 1) {
2235 ++s;
2236 s += strlen(s);
2237 }
fb73857a
PP
2238 else
2239 break;
79072805 2240 }
3280af22 2241 PL_origalen = s - PL_origargv[0];
79072805 2242 }
a0d0e21e 2243 s = SvPV_force(sv,len);
8990e307 2244 i = len;
3280af22
NIS
2245 if (i >= PL_origalen) {
2246 i = PL_origalen;
fb73857a
PP
2247 /* don't allow system to limit $0 seen by script */
2248 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
3280af22
NIS
2249 Copy(s, PL_origargv[0], i, char);
2250 s = PL_origargv[0]+i;
fb73857a 2251 *s = '\0';
79072805
LW
2252 }
2253 else {
3280af22
NIS
2254 Copy(s, PL_origargv[0], i, char);
2255 s = PL_origargv[0]+i;
79072805 2256 *s++ = '\0';
3280af22 2257 while (++i < PL_origalen)
8990e307 2258 *s++ = ' ';
3280af22
NIS
2259 s = PL_origargv[0]+i;
2260 for (i = 1; i < PL_origargc; i++)
2261 PL_origargv[i] = Nullch;
79072805
LW
2262 }
2263 break;
cd39f2b6 2264#endif
4d1ff10f 2265#ifdef USE_5005THREADS
a863c7d1 2266 case '@':
38a03e6e 2267 sv_setsv(thr->errsv, sv);
a863c7d1 2268 break;
4d1ff10f 2269#endif /* USE_5005THREADS */
79072805
LW
2270 }
2271 return 0;
2272}
2273
4d1ff10f 2274#ifdef USE_5005THREADS
f93b4edd 2275int
864dbfa3 2276Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
f93b4edd 2277{
b900a521
JH
2278 DEBUG_S(PerlIO_printf(Perl_debug_log,
2279 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
a674cc95 2280 PTR2UV(thr), PTR2UV(sv)));
f93b4edd 2281 if (MgOWNER(mg))
cea2e8a9 2282 Perl_croak(aTHX_ "panic: magic_mutexfree");
f93b4edd
MB
2283 MUTEX_DESTROY(MgMUTEXP(mg));
2284 COND_DESTROY(MgCONDP(mg));
2285 return 0;
2286}
4d1ff10f 2287#endif /* USE_5005THREADS */
f93b4edd 2288
79072805 2289I32
864dbfa3 2290Perl_whichsig(pTHX_ char *sig)
79072805
LW
2291{
2292 register char **sigv;
2293
22c35a8c 2294 for (sigv = PL_sig_name+1; *sigv; sigv++)
79072805 2295 if (strEQ(sig,*sigv))
22c35a8c 2296 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2297#ifdef SIGCLD
2298 if (strEQ(sig,"CHLD"))
2299 return SIGCLD;
2300#endif
2301#ifdef SIGCHLD
2302 if (strEQ(sig,"CLD"))
2303 return SIGCHLD;
2304#endif
2305 return 0;
2306}
2307
df3728a2 2308#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2309static SV* sig_sv;
df3728a2 2310#endif
84902520 2311
ecfc5424 2312Signal_t
cea2e8a9 2313Perl_sighandler(int sig)
79072805 2314{
1018e26f
NIS
2315#ifdef PERL_GET_SIG_CONTEXT
2316 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2317#else
cea2e8a9 2318 dTHX;
71d280e3 2319#endif
79072805 2320 dSP;
00d579c5 2321 GV *gv = Nullgv;
a0d0e21e 2322 HV *st;
b7953727 2323 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2324 CV *cv = Nullcv;
533c011a 2325 OP *myop = PL_op;
84902520 2326 U32 flags = 0;
3280af22 2327 XPV *tXpv = PL_Xpv;
71d280e3 2328
3280af22 2329 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2330 flags |= 1;
3280af22 2331 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2332 flags |= 4;
3280af22 2333 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2334 flags |= 8;
3280af22 2335 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2336 flags |= 16;
2337
22c35a8c 2338 if (!PL_psig_ptr[sig])
cea2e8a9 2339 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
22c35a8c 2340 PL_sig_name[sig]);
ff0cee69 2341
84902520
TB
2342 /* Max number of items pushed there is 3*n or 4. We cannot fix
2343 infinity, so we fix 4 (in fact 5): */
2344 if (flags & 1) {
3280af22 2345 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2346 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2347 }
ac27b0f5 2348 if (flags & 4)
3280af22 2349 PL_markstack_ptr++; /* Protect mark. */
84902520 2350 if (flags & 8) {
3280af22
NIS
2351 PL_retstack_ix++;
2352 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2353 }
2354 if (flags & 16)
3280af22 2355 PL_scopestack_ix += 1;
84902520 2356 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2357 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2358 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2359 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2360
a0d0e21e 2361 if (!cv || !CvROOT(cv)) {
599cee73 2362 if (ckWARN(WARN_SIGNAL))
cea2e8a9 2363 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2364 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2365 : ((cv && CvGV(cv))
2366 ? GvENAME(CvGV(cv))
2367 : "__ANON__")));
2368 goto cleanup;
79072805
LW
2369 }
2370
22c35a8c
GS
2371 if(PL_psig_name[sig]) {
2372 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2373 flags |= 64;
df3728a2 2374#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2375 sig_sv = sv;
df3728a2 2376#endif
84902520 2377 } else {
ff0cee69 2378 sv = sv_newmortal();
22c35a8c 2379 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2380 }
e336de0d 2381
e788e7d3 2382 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2383 PUSHMARK(SP);
79072805 2384 PUSHs(sv);
79072805 2385 PUTBACK;
a0d0e21e 2386
1b266415 2387 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2388
d3acc0f7 2389 POPSTACK;
1b266415 2390 if (SvTRUE(ERRSV)) {
1d615522 2391#ifndef PERL_MICRO
983dbef6 2392#ifdef HAS_SIGPROCMASK
1b266415
NIS
2393 /* Handler "died", for example to get out of a restart-able read().
2394 * Before we re-do that on its behalf re-enable the signal which was
2395 * blocked by the system when we entered.
2396 */
2397 sigset_t set;
2398 sigemptyset(&set);
2399 sigaddset(&set,sig);
2400 sigprocmask(SIG_UNBLOCK, &set, NULL);
2401#else
2402 /* Not clear if this will work */
2403 (void)rsignal(sig, SIG_IGN);
0a8e0eff 2404 (void)rsignal(sig, &Perl_csighandler);
1b266415 2405#endif
1d615522 2406#endif /* !PERL_MICRO */
b3fe4827 2407 Perl_die(aTHX_ Nullformat);
1b266415 2408 }
00d579c5 2409cleanup:
84902520 2410 if (flags & 1)
3280af22 2411 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2412 if (flags & 4)
3280af22 2413 PL_markstack_ptr--;
ac27b0f5 2414 if (flags & 8)
3280af22 2415 PL_retstack_ix--;
84902520 2416 if (flags & 16)
3280af22 2417 PL_scopestack_ix -= 1;
84902520
TB
2418 if (flags & 64)
2419 SvREFCNT_dec(sv);
533c011a 2420 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2421
3280af22
NIS
2422 PL_Sv = tSv; /* Restore global temporaries. */
2423 PL_Xpv = tXpv;
79072805
LW
2424 return;
2425}
4e35701f
NIS
2426
2427
51371543 2428static void
acfe0abc 2429restore_magic(pTHX_ void *p)
51371543 2430{
48944bdf 2431 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2432 SV* sv = mgs->mgs_sv;
2433
2434 if (!sv)
2435 return;
2436
2437 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2438 {
2439 if (mgs->mgs_flags)
2440 SvFLAGS(sv) |= mgs->mgs_flags;
2441 else
2442 mg_magical(sv);
2443 if (SvGMAGICAL(sv))
2444 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2445 }
2446
2447 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2448
2449 /* If we're still on top of the stack, pop us off. (That condition
2450 * will be satisfied if restore_magic was called explicitly, but *not*
2451 * if it's being called via leave_scope.)
2452 * The reason for doing this is that otherwise, things like sv_2cv()
2453 * may leave alloc gunk on the savestack, and some code
2454 * (e.g. sighandler) doesn't expect that...
2455 */
2456 if (PL_savestack_ix == mgs->mgs_ss_ix)
2457 {
2458 I32 popval = SSPOPINT;
c76ac1ee 2459 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2460 PL_savestack_ix -= 2;
2461 popval = SSPOPINT;
2462 assert(popval == SAVEt_ALLOC);
2463 popval = SSPOPINT;
2464 PL_savestack_ix -= popval;
2465 }
2466
2467}
2468
2469static void
acfe0abc 2470unwind_handler_stack(pTHX_ void *p)
51371543 2471{
51371543
GS
2472 U32 flags = *(U32*)p;
2473
2474 if (flags & 1)
2475 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2476 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2477#if !defined(PERL_IMPLICIT_CONTEXT)
51371543
GS
2478 if (flags & 64)
2479 SvREFCNT_dec(sig_sv);
df3728a2 2480#endif
51371543 2481}
1018e26f
NIS
2482
2483
2484