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