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