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