This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The 'contiguous' test for argv[], envp[] was bogus
[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{
ed79a026 1021#if defined(VMS) || defined(EPOC)
cea2e8a9 1022 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 1023#else
4efc5df6 1024# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137 1025 PerlEnv_clearenv();
4efc5df6
GS
1026# else
1027# ifdef USE_ENVIRON_ARRAY
1028# if defined(USE_ITHREADS)
1029 /* only the parent thread can clobber the process environment */
1030 if (PL_curinterp == aTHX)
1031# endif
1032 {
1033# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
1034 I32 i;
1035
3280af22 1036 if (environ == PL_origenviron)
f2517201 1037 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
1038 else
1039 for (i = 0; environ[i]; i++)
f2517201 1040 safesysfree(environ[i]);
4efc5df6 1041# endif /* PERL_USE_SAFE_PUTENV */
f2517201 1042
66b1d557 1043 environ[0] = Nullch;
4efc5df6
GS
1044 }
1045# endif /* USE_ENVIRON_ARRAY */
052c2dc6
JH
1046# endif /* PERL_IMPLICIT_SYS || WIN32 */
1047#endif /* VMS || EPC */
3e3baf6d 1048 return 0;
66b1d557
HM
1049}
1050
2e34cc90
CL
1051#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1052static int sig_handlers_initted = 0;
1053#endif
7719e241 1054#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1055static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1056#endif
2e34cc90
CL
1057#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1058static int sig_defaulting[SIG_SIZE];
1059#endif
85b332e2 1060
64ca3a65 1061#ifndef PERL_MICRO
2d4fcd5e
AJ
1062#ifdef HAS_SIGPROCMASK
1063static void
1064restore_sigmask(pTHX_ SV *save_sv)
1065{
1066 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1067 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1068}
1069#endif
66b1d557 1070int
864dbfa3 1071Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1072{
1073 I32 i;
2d8e6c8d 1074 STRLEN n_a;
88e89b8a 1075 /* Are we fetching a signal entry? */
2d8e6c8d 1076 i = whichsig(MgPV(mg,n_a));
e02bfb16 1077 if (i > 0) {
22c35a8c
GS
1078 if(PL_psig_ptr[i])
1079 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1080 else {
85b332e2 1081 Sighandler_t sigstate;
2e34cc90 1082 sigstate = rsignal_state(i);
23ada85b 1083#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
2e34cc90
CL
1084 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1085#endif
1086#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1087 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1088#endif
88e89b8a 1089 /* cache state so we don't fetch it again */
ff68c719 1090 if(sigstate == SIG_IGN)
88e89b8a
PP
1091 sv_setpv(sv,"IGNORE");
1092 else
3280af22 1093 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1094 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1095 SvTEMP_off(sv);
1096 }
1097 }
1098 return 0;
1099}
1100int
864dbfa3 1101Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1102{
2d4fcd5e
AJ
1103 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1104 * refactoring might be in order.
1105 */
1106 register char *s;
2d8e6c8d 1107 STRLEN n_a;
2d4fcd5e
AJ
1108 SV* to_dec;
1109 s = MgPV(mg,n_a);
1110 if (*s == '_') {
1111 SV** svp;
1112 if (strEQ(s,"__DIE__"))
1113 svp = &PL_diehook;
1114 else if (strEQ(s,"__WARN__"))
1115 svp = &PL_warnhook;
1116 else
1117 Perl_croak(aTHX_ "No such hook: %s", s);
1118 if (*svp) {
1119 to_dec = *svp;
1120 *svp = 0;
1121 SvREFCNT_dec(to_dec);
1122 }
1123 }
1124 else {
1125 I32 i;
1126 /* Are we clearing a signal entry? */
1127 i = whichsig(s);
e02bfb16 1128 if (i > 0) {
2d4fcd5e
AJ
1129#ifdef HAS_SIGPROCMASK
1130 sigset_t set, save;
1131 SV* save_sv;
1132 /* Avoid having the signal arrive at a bad time, if possible. */
1133 sigemptyset(&set);
1134 sigaddset(&set,i);
1135 sigprocmask(SIG_BLOCK, &set, &save);
1136 ENTER;
1137 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1138 SAVEFREESV(save_sv);
1139 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1140#endif
1141 PERL_ASYNC_CHECK();
1142#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1143 if (!sig_handlers_initted) Perl_csighandler_init();
1144#endif
1145#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1146 sig_defaulting[i] = 1;
1147 (void)rsignal(i, &Perl_csighandler);
1148#else
1149 (void)rsignal(i, SIG_DFL);
1150#endif
1151 if(PL_psig_name[i]) {
1152 SvREFCNT_dec(PL_psig_name[i]);
1153 PL_psig_name[i]=0;
1154 }
1155 if(PL_psig_ptr[i]) {
1156 to_dec=PL_psig_ptr[i];
1157 PL_psig_ptr[i]=0;
1158 LEAVE;
1159 SvREFCNT_dec(to_dec);
1160 }
1161 else
1162 LEAVE;
1163 }
88e89b8a
PP
1164 }
1165 return 0;
1166}
3d37d572 1167
0a8e0eff
NIS
1168void
1169Perl_raise_signal(pTHX_ int sig)
1170{
1171 /* Set a flag to say this signal is pending */
1172 PL_psig_pend[sig]++;
1173 /* And one to say _a_ signal is pending */
1174 PL_sig_pending = 1;
1175}
1176
1177Signal_t
1178Perl_csighandler(int sig)
1179{
1018e26f
NIS
1180#ifdef PERL_GET_SIG_CONTEXT
1181 dTHXa(PERL_GET_SIG_CONTEXT);
1182#else
85b332e2
CL
1183 dTHX;
1184#endif
23ada85b 1185#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1186 (void) rsignal(sig, &Perl_csighandler);
1187 if (sig_ignoring[sig]) return;
1188#endif
2e34cc90 1189#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
7719e241 1190 if (sig_defaulting[sig])
2e34cc90
CL
1191#ifdef KILL_BY_SIGPRC
1192 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1193#else
1194 exit(1);
1195#endif
1196#endif
4ffa73a3
JH
1197 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1198 /* Call the perl level handler now--
1199 * with risk we may be in malloc() etc. */
1200 (*PL_sighandlerp)(sig);
1201 else
1202 Perl_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1203}
1204
2e34cc90
CL
1205#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1206void
1207Perl_csighandler_init(void)
1208{
1209 int sig;
1210 if (sig_handlers_initted) return;
1211
1212 for (sig = 1; sig < SIG_SIZE; sig++) {
1213#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1214 dTHX;
2e34cc90
CL
1215 sig_defaulting[sig] = 1;
1216 (void) rsignal(sig, &Perl_csighandler);
1217#endif
1218#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1219 sig_ignoring[sig] = 0;
1220#endif
1221 }
1222 sig_handlers_initted = 1;
1223}
1224#endif
1225
0a8e0eff
NIS
1226void
1227Perl_despatch_signals(pTHX)
1228{
1229 int sig;
1230 PL_sig_pending = 0;
1231 for (sig = 1; sig < SIG_SIZE; sig++) {
1232 if (PL_psig_pend[sig]) {
25da4428
JH
1233 PERL_BLOCKSIG_ADD(set, sig);
1234 PL_psig_pend[sig] = 0;
1235 PERL_BLOCKSIG_BLOCK(set);
f5203343 1236 (*PL_sighandlerp)(sig);
25da4428 1237 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1238 }
1239 }
1240}
1241
85e6fe83 1242int
864dbfa3 1243Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1244{
1245 register char *s;
1246 I32 i;
b7953727 1247 SV** svp = 0;
2d4fcd5e
AJ
1248 /* Need to be careful with SvREFCNT_dec(), because that can have side
1249 * effects (due to closures). We must make sure that the new disposition
1250 * is in place before it is called.
1251 */
1252 SV* to_dec = 0;
e72dc28c 1253 STRLEN len;
2d4fcd5e
AJ
1254#ifdef HAS_SIGPROCMASK
1255 sigset_t set, save;
1256 SV* save_sv;
1257#endif
a0d0e21e 1258
e72dc28c 1259 s = MgPV(mg,len);
748a9306
LW
1260 if (*s == '_') {
1261 if (strEQ(s,"__DIE__"))
3280af22 1262 svp = &PL_diehook;
748a9306 1263 else if (strEQ(s,"__WARN__"))
3280af22 1264 svp = &PL_warnhook;
748a9306 1265 else
cea2e8a9 1266 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1267 i = 0;
4633a7c4 1268 if (*svp) {
2d4fcd5e 1269 to_dec = *svp;
4633a7c4
LW
1270 *svp = 0;
1271 }
748a9306
LW
1272 }
1273 else {
1274 i = whichsig(s); /* ...no, a brick */
e02bfb16 1275 if (i < 0) {
e476b1b5 1276 if (ckWARN(WARN_SIGNAL))
9014280d 1277 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1278 return 0;
1279 }
2d4fcd5e
AJ
1280#ifdef HAS_SIGPROCMASK
1281 /* Avoid having the signal arrive at a bad time, if possible. */
1282 sigemptyset(&set);
1283 sigaddset(&set,i);
1284 sigprocmask(SIG_BLOCK, &set, &save);
1285 ENTER;
1286 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1287 SAVEFREESV(save_sv);
1288 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1289#endif
1290 PERL_ASYNC_CHECK();
2e34cc90
CL
1291#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1292 if (!sig_handlers_initted) Perl_csighandler_init();
1293#endif
23ada85b 1294#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1295 sig_ignoring[i] = 0;
1296#endif
2e34cc90 1297#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
2d4fcd5e 1298 sig_defaulting[i] = 0;
2e34cc90 1299#endif
22c35a8c 1300 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1301 to_dec = PL_psig_ptr[i];
22c35a8c 1302 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1303 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1304 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1305 SvREADONLY_on(PL_psig_name[i]);
748a9306 1306 }
a0d0e21e 1307 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1308 if (i) {
0a8e0eff 1309 (void)rsignal(i, &Perl_csighandler);
2d4fcd5e
AJ
1310#ifdef HAS_SIGPROCMASK
1311 LEAVE;
1312#endif
1313 }
748a9306
LW
1314 else
1315 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1316 if(to_dec)
1317 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1318 return 0;
1319 }
e72dc28c 1320 s = SvPV_force(sv,len);
748a9306 1321 if (strEQ(s,"IGNORE")) {
85b332e2 1322 if (i) {
23ada85b 1323#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1324 sig_ignoring[i] = 1;
1325 (void)rsignal(i, &Perl_csighandler);
1326#else
ff68c719 1327 (void)rsignal(i, SIG_IGN);
85b332e2 1328#endif
2d4fcd5e 1329 }
748a9306
LW
1330 }
1331 else if (strEQ(s,"DEFAULT") || !*s) {
1332 if (i)
2e34cc90
CL
1333#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1334 {
1335 sig_defaulting[i] = 1;
1336 (void)rsignal(i, &Perl_csighandler);
1337 }
1338#else
ff68c719 1339 (void)rsignal(i, SIG_DFL);
2e34cc90 1340#endif
748a9306 1341 }
79072805 1342 else {
5aabfad6
PP
1343 /*
1344 * We should warn if HINT_STRICT_REFS, but without
1345 * access to a known hint bit in a known OP, we can't
1346 * tell whether HINT_STRICT_REFS is in force or not.
1347 */
46fc3d4c 1348 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1349 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1350 if (i)
0a8e0eff 1351 (void)rsignal(i, &Perl_csighandler);
748a9306
LW
1352 else
1353 *svp = SvREFCNT_inc(sv);
79072805 1354 }
2d4fcd5e
AJ
1355#ifdef HAS_SIGPROCMASK
1356 if(i)
1357 LEAVE;
1358#endif
1359 if(to_dec)
1360 SvREFCNT_dec(to_dec);
79072805
LW
1361 return 0;
1362}
64ca3a65 1363#endif /* !PERL_MICRO */
79072805
LW
1364
1365int
864dbfa3 1366Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1367{
3280af22 1368 PL_sub_generation++;
463ee0b2
LW
1369 return 0;
1370}
1371
1372int
864dbfa3 1373Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1374{
a0d0e21e 1375 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1376 PL_amagic_generation++;
463ee0b2 1377
a0d0e21e
LW
1378 return 0;
1379}
463ee0b2 1380
946ec16e 1381int
864dbfa3 1382Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1383{
1384 HV *hv = (HV*)LvTARG(sv);
6ff81951 1385 I32 i = 0;
7719e241 1386
6ff81951 1387 if (hv) {
497b47a8
JH
1388 (void) hv_iterinit(hv);
1389 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1390 i = HvKEYS(hv);
1391 else {
1392 while (hv_iternext(hv))
1393 i++;
1394 }
6ff81951
GS
1395 }
1396
1397 sv_setiv(sv, (IV)i);
1398 return 0;
1399}
1400
1401int
864dbfa3 1402Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1403{
1404 if (LvTARG(sv)) {
1405 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1406 }
1407 return 0;
ac27b0f5 1408}
946ec16e 1409
e336de0d 1410/* caller is responsible for stack switching/cleanup */
565764a8 1411STATIC int
cea2e8a9 1412S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1413{
1414 dSP;
463ee0b2 1415
924508f0
GS
1416 PUSHMARK(SP);
1417 EXTEND(SP, n);
33c27489 1418 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1419 if (n > 1) {
93965878 1420 if (mg->mg_ptr) {
565764a8 1421 if (mg->mg_len >= 0)
79cb57f6 1422 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1423 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1424 PUSHs((SV*)mg->mg_ptr);
1425 }
14befaf4 1426 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1427 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1428 }
1429 }
1430 if (n > 2) {
1431 PUSHs(val);
88e89b8a 1432 }
463ee0b2
LW
1433 PUTBACK;
1434
864dbfa3 1435 return call_method(meth, flags);
946ec16e
PP
1436}
1437
76e3520e 1438STATIC int
cea2e8a9 1439S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1440{
1441 dSP;
463ee0b2 1442
a0d0e21e
LW
1443 ENTER;
1444 SAVETMPS;
e788e7d3 1445 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1446
33c27489 1447 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1448 sv_setsv(sv, *PL_stack_sp--);
93965878 1449 }
463ee0b2 1450
d3acc0f7 1451 POPSTACK;
a0d0e21e
LW
1452 FREETMPS;
1453 LEAVE;
1454 return 0;
1455}
463ee0b2 1456
a0d0e21e 1457int
864dbfa3 1458Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1459{
1460 magic_methpack(sv,mg,"FETCH");
1461 if (mg->mg_ptr)
1462 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1463 return 0;
1464}
1465
1466int
864dbfa3 1467Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1468{
1469 dSP;
a60c0954 1470 ENTER;
e788e7d3 1471 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1472 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1473 POPSTACK;
a60c0954 1474 LEAVE;
463ee0b2
LW
1475 return 0;
1476}
1477
1478int
864dbfa3 1479Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1480{
a0d0e21e
LW
1481 return magic_methpack(sv,mg,"DELETE");
1482}
463ee0b2 1483
93965878
NIS
1484
1485U32
864dbfa3 1486Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1487{
e336de0d 1488 dSP;
93965878
NIS
1489 U32 retval = 0;
1490
1491 ENTER;
1492 SAVETMPS;
e788e7d3 1493 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1494 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1495 sv = *PL_stack_sp--;
a60c0954 1496 retval = (U32) SvIV(sv)-1;
93965878 1497 }
d3acc0f7 1498 POPSTACK;
93965878
NIS
1499 FREETMPS;
1500 LEAVE;
1501 return retval;
1502}
1503
cea2e8a9
GS
1504int
1505Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1506{
1507 dSP;
463ee0b2 1508
e336de0d 1509 ENTER;
e788e7d3 1510 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1511 PUSHMARK(SP);
33c27489 1512 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1513 PUTBACK;
864dbfa3 1514 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1515 POPSTACK;
a60c0954 1516 LEAVE;
463ee0b2
LW
1517 return 0;
1518}
1519
1520int
864dbfa3 1521Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1522{
463ee0b2 1523 dSP;
dff6d3cd 1524 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1525
1526 ENTER;
a0d0e21e 1527 SAVETMPS;
e788e7d3 1528 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1529 PUSHMARK(SP);
1530 EXTEND(SP, 2);
33c27489 1531 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1532 if (SvOK(key))
1533 PUSHs(key);
1534 PUTBACK;
1535
864dbfa3 1536 if (call_method(meth, G_SCALAR))
3280af22 1537 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1538
d3acc0f7 1539 POPSTACK;
a0d0e21e
LW
1540 FREETMPS;
1541 LEAVE;
79072805
LW
1542 return 0;
1543}
1544
1545int
864dbfa3 1546Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1547{
1548 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1549}
a0d0e21e
LW
1550
1551int
864dbfa3 1552Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1553{
1554 OP *o;
1555 I32 i;
1556 GV* gv;
1557 SV** svp;
2d8e6c8d 1558 STRLEN n_a;
79072805 1559
3280af22 1560 gv = PL_DBline;
79072805 1561 i = SvTRUE(sv);
188ea221 1562 svp = av_fetch(GvAV(gv),
2d8e6c8d 1563 atoi(MgPV(mg,n_a)), FALSE);
5df8de69
DM
1564 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1565 /* set or clear breakpoint in the relevant control op */
1566 if (i)
1567 o->op_flags |= OPf_SPECIAL;
1568 else
1569 o->op_flags &= ~OPf_SPECIAL;
1570 }
79072805
LW
1571 return 0;
1572}
1573
1574int
864dbfa3 1575Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1576{
3280af22 1577 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1578 return 0;
1579}
1580
1581int
864dbfa3 1582Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1583{
3280af22 1584 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1585 return 0;
1586}
1587
1588int
864dbfa3 1589Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1590{
1591 SV* lsv = LvTARG(sv);
ac27b0f5 1592
a0d0e21e 1593 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1594 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1595 if (mg && mg->mg_len >= 0) {
a0ed51b3 1596 I32 i = mg->mg_len;
7e2040f0 1597 if (DO_UTF8(lsv))
a0ed51b3
LW
1598 sv_pos_b2u(lsv, &i);
1599 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1600 return 0;
1601 }
1602 }
1603 (void)SvOK_off(sv);
1604 return 0;
1605}
1606
1607int
864dbfa3 1608Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1609{
1610 SV* lsv = LvTARG(sv);
1611 SSize_t pos;
1612 STRLEN len;
c00206c8 1613 STRLEN ulen = 0;
a0d0e21e
LW
1614
1615 mg = 0;
ac27b0f5 1616
a0d0e21e 1617 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1618 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1619 if (!mg) {
1620 if (!SvOK(sv))
1621 return 0;
14befaf4
DM
1622 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1623 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1624 }
1625 else if (!SvOK(sv)) {
565764a8 1626 mg->mg_len = -1;
a0d0e21e
LW
1627 return 0;
1628 }
1629 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1630
c485e607 1631 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1632
7e2040f0 1633 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1634 ulen = sv_len_utf8(lsv);
1635 if (ulen)
1636 len = ulen;
a0ed51b3
LW
1637 }
1638
a0d0e21e
LW
1639 if (pos < 0) {
1640 pos += len;
1641 if (pos < 0)
1642 pos = 0;
1643 }
eb160463 1644 else if (pos > (SSize_t)len)
a0d0e21e 1645 pos = len;
a0ed51b3
LW
1646
1647 if (ulen) {
1648 I32 p = pos;
1649 sv_pos_u2b(lsv, &p, 0);
1650 pos = p;
1651 }
727405f8 1652
565764a8 1653 mg->mg_len = pos;
71be2cbc 1654 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1655
79072805
LW
1656 return 0;
1657}
1658
1659int
864dbfa3 1660Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1661{
8646b087
PP
1662 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1663 SvFAKE_off(sv);
946ec16e 1664 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1665 SvFAKE_on(sv);
1666 }
1667 else
946ec16e 1668 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1669 return 0;
1670}
1671
1672int
864dbfa3 1673Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1674{
1675 register char *s;
1676 GV* gv;
2d8e6c8d 1677 STRLEN n_a;
79072805
LW
1678
1679 if (!SvOK(sv))
1680 return 0;
2d8e6c8d 1681 s = SvPV(sv, n_a);
79072805
LW
1682 if (*s == '*' && s[1])
1683 s++;
85e6fe83 1684 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1685 if (sv == (SV*)gv)
1686 return 0;
1687 if (GvGP(sv))
88e89b8a 1688 gp_free((GV*)sv);
79072805 1689 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1690 return 0;
1691}
1692
1693int
864dbfa3 1694Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1695{
1696 STRLEN len;
1697 SV *lsv = LvTARG(sv);
1698 char *tmps = SvPV(lsv,len);
1699 I32 offs = LvTARGOFF(sv);
1700 I32 rem = LvTARGLEN(sv);
1701
9aa983d2
JH
1702 if (SvUTF8(lsv))
1703 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1704 if (offs > (I32)len)
6ff81951 1705 offs = len;
eb160463 1706 if (rem + offs > (I32)len)
6ff81951
GS
1707 rem = len - offs;
1708 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1709 if (SvUTF8(lsv))
2ef4b674 1710 SvUTF8_on(sv);
6ff81951
GS
1711 return 0;
1712}
1713
1714int
864dbfa3 1715Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1716{
9aa983d2
JH
1717 STRLEN len;
1718 char *tmps = SvPV(sv, len);
1719 SV *lsv = LvTARG(sv);
1720 I32 lvoff = LvTARGOFF(sv);
1721 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1722
1aa99e6b 1723 if (DO_UTF8(sv)) {
9aa983d2
JH
1724 sv_utf8_upgrade(lsv);
1725 sv_pos_u2b(lsv, &lvoff, &lvlen);
1726 sv_insert(lsv, lvoff, lvlen, tmps, len);
1727 SvUTF8_on(lsv);
1728 }
9bf12eaf 1729 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1730 sv_pos_u2b(lsv, &lvoff, &lvlen);
e95af362 1731 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1732 sv_insert(lsv, lvoff, lvlen, tmps, len);
1733 Safefree(tmps);
1aa99e6b
IH
1734 }
1735 else
9aa983d2 1736 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1737
79072805
LW
1738 return 0;
1739}
1740
1741int
864dbfa3 1742Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1743{
565764a8 1744 TAINT_IF((mg->mg_len & 1) ||
155aba94 1745 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1746 return 0;
1747}
1748
1749int
864dbfa3 1750Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1751{
3280af22
NIS
1752 if (PL_localizing) {
1753 if (PL_localizing == 1)
565764a8 1754 mg->mg_len <<= 1;
748a9306 1755 else
565764a8 1756 mg->mg_len >>= 1;
a0d0e21e 1757 }
3280af22 1758 else if (PL_tainted)
565764a8 1759 mg->mg_len |= 1;
748a9306 1760 else
565764a8 1761 mg->mg_len &= ~1;
463ee0b2
LW
1762 return 0;
1763}
1764
1765int
864dbfa3 1766Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1767{
1768 SV *lsv = LvTARG(sv);
6ff81951
GS
1769
1770 if (!lsv) {
155aba94 1771 (void)SvOK_off(sv);
6ff81951
GS
1772 return 0;
1773 }
6ff81951 1774
81e118e0 1775 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1776 return 0;
1777}
1778
1779int
864dbfa3 1780Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1781{
1782 do_vecset(sv); /* XXX slurp this routine */
1783 return 0;
1784}
1785
1786int
864dbfa3 1787Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1788{
71be2cbc 1789 SV *targ = Nullsv;
5f05dabc 1790 if (LvTARGLEN(sv)) {
68dc0745 1791 if (mg->mg_obj) {
74e13ce4 1792 SV *ahv = LvTARG(sv);
6d822dc4
MS
1793 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1794 if (he)
1795 targ = HeVAL(he);
68dc0745
PP
1796 }
1797 else {
3c78fafa 1798 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1799 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1800 targ = AvARRAY(av)[LvTARGOFF(sv)];
1801 }
3280af22 1802 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1803 /* somebody else defined it for us */
1804 SvREFCNT_dec(LvTARG(sv));
1805 LvTARG(sv) = SvREFCNT_inc(targ);
1806 LvTARGLEN(sv) = 0;
1807 SvREFCNT_dec(mg->mg_obj);
1808 mg->mg_obj = Nullsv;
1809 mg->mg_flags &= ~MGf_REFCOUNTED;
1810 }
5f05dabc 1811 }
71be2cbc
PP
1812 else
1813 targ = LvTARG(sv);
3280af22 1814 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1815 return 0;
1816}
1817
1818int
864dbfa3 1819Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1820{
1821 if (LvTARGLEN(sv))
68dc0745
PP
1822 vivify_defelem(sv);
1823 if (LvTARG(sv)) {
5f05dabc 1824 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1825 SvSETMAGIC(LvTARG(sv));
1826 }
5f05dabc
PP
1827 return 0;
1828}
1829
71be2cbc 1830void
864dbfa3 1831Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1832{
74e13ce4
GS
1833 MAGIC *mg;
1834 SV *value = Nullsv;
71be2cbc 1835
14befaf4 1836 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1837 return;
68dc0745 1838 if (mg->mg_obj) {
74e13ce4 1839 SV *ahv = LvTARG(sv);
2d8e6c8d 1840 STRLEN n_a;
6d822dc4
MS
1841 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1842 if (he)
1843 value = HeVAL(he);
3280af22 1844 if (!value || value == &PL_sv_undef)
cea2e8a9 1845 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1846 }
68dc0745
PP
1847 else {
1848 AV* av = (AV*)LvTARG(sv);
5aabfad6 1849 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1850 LvTARG(sv) = Nullsv; /* array can't be extended */
1851 else {
1852 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1853 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1854 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1855 }
1856 }
3e3baf6d 1857 (void)SvREFCNT_inc(value);
68dc0745
PP
1858 SvREFCNT_dec(LvTARG(sv));
1859 LvTARG(sv) = value;
71be2cbc 1860 LvTARGLEN(sv) = 0;
68dc0745
PP
1861 SvREFCNT_dec(mg->mg_obj);
1862 mg->mg_obj = Nullsv;
1863 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1864}
1865
1866int
864dbfa3 1867Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1868{
1869 AV *av = (AV*)mg->mg_obj;
1870 SV **svp = AvARRAY(av);
1871 I32 i = AvFILLp(av);
1872 while (i >= 0) {
1873 if (svp[i] && svp[i] != &PL_sv_undef) {
1874 if (!SvWEAKREF(svp[i]))
cea2e8a9 1875 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1876 /* XXX Should we check that it hasn't changed? */
1877 SvRV(svp[i]) = 0;
155aba94 1878 (void)SvOK_off(svp[i]);
810b8aa5
GS
1879 SvWEAKREF_off(svp[i]);
1880 svp[i] = &PL_sv_undef;
1881 }
1882 i--;
1883 }
1884 return 0;
1885}
1886
1887int
864dbfa3 1888Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1889{
565764a8 1890 mg->mg_len = -1;
c6496cc7 1891 SvSCREAM_off(sv);
93a17b20
LW
1892 return 0;
1893}
1894
1895int
864dbfa3 1896Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1897{
14befaf4 1898 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1899 SvVALID_off(sv);
1900 return 0;
1901}
1902
1903int
864dbfa3 1904Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1905{
14befaf4 1906 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1907 SvCOMPILED_off(sv);
1908 return 0;
1909}
1910
1911int
864dbfa3 1912Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1913{
1914 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1915
1916 if (uf && uf->uf_set)
24f81a43 1917 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
1918 return 0;
1919}
1920
c277df42 1921int
faf82a0b
AE
1922Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1923{
1924 sv_unmagic(sv, PERL_MAGIC_qr);
1925 return 0;
1926}
1927
1928int
864dbfa3 1929Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1930{
1931 regexp *re = (regexp *)mg->mg_obj;
1932 ReREFCNT_dec(re);
1933 return 0;
1934}
1935
7a4c00b4 1936#ifdef USE_LOCALE_COLLATE
79072805 1937int
864dbfa3 1938Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1939{
1940 /*
838b5b74 1941 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1942 * and vanished with a faint plop.
1943 */
7a4c00b4
PP
1944 if (mg->mg_ptr) {
1945 Safefree(mg->mg_ptr);
1946 mg->mg_ptr = NULL;
565764a8 1947 mg->mg_len = -1;
7a4c00b4 1948 }
bbce6d69
PP
1949 return 0;
1950}
7a4c00b4 1951#endif /* USE_LOCALE_COLLATE */
bbce6d69 1952
7e8c5dac
HS
1953/* Just clear the UTF-8 cache data. */
1954int
1955Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1956{
1957 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1958 mg->mg_ptr = 0;
1959 mg->mg_len = -1; /* The mg_len holds the len cache. */
1960 return 0;
1961}
1962
bbce6d69 1963int
864dbfa3 1964Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1965{
1966 register char *s;
1967 I32 i;
8990e307 1968 STRLEN len;
79072805 1969 switch (*mg->mg_ptr) {
748a9306 1970 case '\001': /* ^A */
3280af22 1971 sv_setsv(PL_bodytarget, sv);
748a9306 1972 break;
49460fe6 1973 case '\003': /* ^C */
eb160463 1974 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
1975 break;
1976
79072805 1977 case '\004': /* ^D */
b4ab917c
DM
1978#ifdef DEBUGGING
1979 s = SvPV_nolen(sv);
1980 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
79072805 1981 DEBUG_x(dump_all());
b4ab917c
DM
1982#else
1983 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1984#endif
79072805 1985 break;
28f23441 1986 case '\005': /* ^E */
d0063567 1987 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 1988#ifdef MACOS_TRADITIONAL
d0063567 1989 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1990#else
cd39f2b6 1991# ifdef VMS
d0063567 1992 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1993# else
cd39f2b6 1994# ifdef WIN32
d0063567 1995 SetLastError( SvIV(sv) );
cd39f2b6 1996# else
9fed8b87 1997# ifdef OS2
d0063567 1998 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 1999# else
d0063567
DK
2000 /* will anyone ever use this? */
2001 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2002# endif
048c1ddf
IZ
2003# endif
2004# endif
22fae026 2005#endif
d0063567
DK
2006 }
2007 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2008 if (PL_encoding)
2009 SvREFCNT_dec(PL_encoding);
2010 if (SvOK(sv) || SvGMAGICAL(sv)) {
2011 PL_encoding = newSVsv(sv);
2012 }
2013 else {
2014 PL_encoding = Nullsv;
2015 }
2016 }
2017 break;
79072805 2018 case '\006': /* ^F */
3280af22 2019 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2020 break;
a0d0e21e 2021 case '\010': /* ^H */
3280af22 2022 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2023 break;
9d116dd7 2024 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2025 if (PL_inplace)
2026 Safefree(PL_inplace);
79072805 2027 if (SvOK(sv))
2d8e6c8d 2028 PL_inplace = savepv(SvPV(sv,len));
79072805 2029 else
3280af22 2030 PL_inplace = Nullch;
79072805 2031 break;
28f23441 2032 case '\017': /* ^O */
ac27b0f5
NIS
2033 if (*(mg->mg_ptr+1) == '\0') {
2034 if (PL_osname)
2035 Safefree(PL_osname);
2036 if (SvOK(sv))
2037 PL_osname = savepv(SvPV(sv,len));
2038 else
2039 PL_osname = Nullch;
2040 }
2041 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2042 if (!PL_compiling.cop_io)
2043 PL_compiling.cop_io = newSVsv(sv);
2044 else
2045 sv_setsv(PL_compiling.cop_io,sv);
2046 }
28f23441 2047 break;
79072805 2048 case '\020': /* ^P */
3280af22 2049 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
2050 if (PL_perldb && !PL_DBsingle)
2051 init_debugger();
79072805
LW
2052 break;
2053 case '\024': /* ^T */
88e89b8a 2054#ifdef BIG_TIME
6b88bc9c 2055 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2056#else
3280af22 2057 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2058#endif
79072805 2059 break;
fde18df1 2060 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2061 if (*(mg->mg_ptr+1) == '\0') {
2062 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2063 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2064 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2065 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2066 }
599cee73 2067 }
0a378802 2068 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2069 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2070 if (!SvPOK(sv) && PL_localizing) {
2071 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2072 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2073 break;
2074 }
f4fc7782 2075 {
b5477537 2076 STRLEN len, i;
d3a7d8c7 2077 int accumulate = 0 ;
f4fc7782 2078 int any_fatals = 0 ;
d3a7d8c7 2079 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
2080 for (i = 0 ; i < len ; ++i) {
2081 accumulate |= ptr[i] ;
2082 any_fatals |= (ptr[i] & 0xAA) ;
2083 }
d3a7d8c7
GS
2084 if (!accumulate)
2085 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2086 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2087 PL_compiling.cop_warnings = pWARN_ALL;
2088 PL_dowarn |= G_WARN_ONCE ;
727405f8 2089 }
d3a7d8c7
GS
2090 else {
2091 if (specialWARN(PL_compiling.cop_warnings))
2092 PL_compiling.cop_warnings = newSVsv(sv) ;
2093 else
2094 sv_setsv(PL_compiling.cop_warnings, sv);
2095 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2096 PL_dowarn |= G_WARN_ONCE ;
2097 }
f4fc7782 2098
d3a7d8c7 2099 }
4438c4b7 2100 }
971a9dd3 2101 }
79072805
LW
2102 break;
2103 case '.':
3280af22
NIS
2104 if (PL_localizing) {
2105 if (PL_localizing == 1)
7766f137 2106 SAVESPTR(PL_last_in_gv);
748a9306 2107 }
3280af22 2108 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2109 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2110 break;
2111 case '^':
3280af22 2112 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2113 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2114 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2115 break;
2116 case '~':
3280af22 2117 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2118 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2119 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2120 break;
2121 case '=':
632db599 2122 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2123 break;
2124 case '-':
632db599 2125 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2126 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2127 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2128 break;
2129 case '%':
632db599 2130 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2131 break;
2132 case '|':
4b65379b 2133 {
3280af22 2134 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2135 if(!io)
2136 break;
4b65379b
CS
2137 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2138 IoFLAGS(io) &= ~IOf_FLUSH;
2139 else {
2140 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2141 PerlIO *ofp = IoOFP(io);
2142 if (ofp)
2143 (void)PerlIO_flush(ofp);
2144 IoFLAGS(io) |= IOf_FLUSH;
2145 }
2146 }
79072805
LW
2147 }
2148 break;
79072805 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
3cb9023d 2375 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2376 s = SvPV_force(sv,len);
54bfe034
JH
2377 if (len >= (I32)PL_origalen) {
2378 /* Longer than original, will be truncated. */
2379 Copy(s, PL_origargv[0], PL_origalen, char);
2380 PL_origargv[0][PL_origalen - 1] = 0;
79072805
LW
2381 }
2382 else {
54bfe034
JH
2383 /* Shorter than original, will be padded. */
2384 Copy(s, PL_origargv[0], len, char);
2385 PL_origargv[0][len] = 0;
2386 memset(PL_origargv[0] + len + 1,
2387 /* Is the space counterintuitive? Yes.
2388 * (You were expecting \0?)
3cb9023d 2389 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2390 * --jhi */
2391 (int)' ',
2392 PL_origalen - len - 1);
3280af22 2393 for (i = 1; i < PL_origargc; i++)
54bfe034 2394 PL_origargv[i] = 0;
79072805 2395 }
e2975953 2396 UNLOCK_DOLLARZERO_MUTEX;
79072805 2397 break;
cd39f2b6 2398#endif
79072805
LW
2399 }
2400 return 0;
2401}
2402
2403I32
864dbfa3 2404Perl_whichsig(pTHX_ char *sig)
79072805
LW
2405{
2406 register char **sigv;
2407
e02bfb16 2408 for (sigv = PL_sig_name; *sigv; sigv++)
79072805 2409 if (strEQ(sig,*sigv))
22c35a8c 2410 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2411#ifdef SIGCLD
2412 if (strEQ(sig,"CHLD"))
2413 return SIGCLD;
2414#endif
2415#ifdef SIGCHLD
2416 if (strEQ(sig,"CLD"))
2417 return SIGCHLD;
2418#endif
7f1236c0 2419 return -1;
79072805
LW
2420}
2421
df3728a2 2422#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2423static SV* sig_sv;
df3728a2 2424#endif
84902520 2425
ecfc5424 2426Signal_t
cea2e8a9 2427Perl_sighandler(int sig)
79072805 2428{
1018e26f
NIS
2429#ifdef PERL_GET_SIG_CONTEXT
2430 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2431#else
cea2e8a9 2432 dTHX;
71d280e3 2433#endif
79072805 2434 dSP;
00d579c5 2435 GV *gv = Nullgv;
a0d0e21e 2436 HV *st;
b7953727 2437 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2438 CV *cv = Nullcv;
533c011a 2439 OP *myop = PL_op;
84902520 2440 U32 flags = 0;
3280af22 2441 XPV *tXpv = PL_Xpv;
71d280e3 2442
3280af22 2443 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2444 flags |= 1;
3280af22 2445 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2446 flags |= 4;
3280af22 2447 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2448 flags |= 8;
3280af22 2449 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2450 flags |= 16;
2451
727405f8 2452 if (!PL_psig_ptr[sig]) {
99ef548b 2453 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2454 PL_sig_name[sig]);
2455 exit(sig);
2456 }
ff0cee69 2457
84902520
TB
2458 /* Max number of items pushed there is 3*n or 4. We cannot fix
2459 infinity, so we fix 4 (in fact 5): */
2460 if (flags & 1) {
3280af22 2461 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2462 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2463 }
ac27b0f5 2464 if (flags & 4)
3280af22 2465 PL_markstack_ptr++; /* Protect mark. */
84902520 2466 if (flags & 8) {
3280af22
NIS
2467 PL_retstack_ix++;
2468 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2469 }
2470 if (flags & 16)
3280af22 2471 PL_scopestack_ix += 1;
84902520 2472 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2473 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2474 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2475 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2476
a0d0e21e 2477 if (!cv || !CvROOT(cv)) {
599cee73 2478 if (ckWARN(WARN_SIGNAL))
9014280d 2479 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2480 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2481 : ((cv && CvGV(cv))
2482 ? GvENAME(CvGV(cv))
2483 : "__ANON__")));
2484 goto cleanup;
79072805
LW
2485 }
2486
22c35a8c
GS
2487 if(PL_psig_name[sig]) {
2488 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2489 flags |= 64;
df3728a2 2490#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2491 sig_sv = sv;
df3728a2 2492#endif
84902520 2493 } else {
ff0cee69 2494 sv = sv_newmortal();
22c35a8c 2495 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2496 }
e336de0d 2497
e788e7d3 2498 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2499 PUSHMARK(SP);
79072805 2500 PUSHs(sv);
79072805 2501 PUTBACK;
a0d0e21e 2502
1b266415 2503 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2504
d3acc0f7 2505 POPSTACK;
1b266415 2506 if (SvTRUE(ERRSV)) {
1d615522 2507#ifndef PERL_MICRO
983dbef6 2508#ifdef HAS_SIGPROCMASK
1b266415
NIS
2509 /* Handler "died", for example to get out of a restart-able read().
2510 * Before we re-do that on its behalf re-enable the signal which was
2511 * blocked by the system when we entered.
2512 */
2513 sigset_t set;
2514 sigemptyset(&set);
2515 sigaddset(&set,sig);
2516 sigprocmask(SIG_UNBLOCK, &set, NULL);
2517#else
2518 /* Not clear if this will work */
2519 (void)rsignal(sig, SIG_IGN);
0a8e0eff 2520 (void)rsignal(sig, &Perl_csighandler);
1b266415 2521#endif
1d615522 2522#endif /* !PERL_MICRO */
b3fe4827 2523 Perl_die(aTHX_ Nullformat);
1b266415 2524 }
00d579c5 2525cleanup:
84902520 2526 if (flags & 1)
3280af22 2527 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2528 if (flags & 4)
3280af22 2529 PL_markstack_ptr--;
ac27b0f5 2530 if (flags & 8)
3280af22 2531 PL_retstack_ix--;
84902520 2532 if (flags & 16)
3280af22 2533 PL_scopestack_ix -= 1;
84902520
TB
2534 if (flags & 64)
2535 SvREFCNT_dec(sv);
533c011a 2536 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2537
3280af22
NIS
2538 PL_Sv = tSv; /* Restore global temporaries. */
2539 PL_Xpv = tXpv;
79072805
LW
2540 return;
2541}
4e35701f
NIS
2542
2543
51371543 2544static void
acfe0abc 2545restore_magic(pTHX_ void *p)
51371543 2546{
48944bdf 2547 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2548 SV* sv = mgs->mgs_sv;
2549
2550 if (!sv)
2551 return;
2552
2553 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2554 {
2555 if (mgs->mgs_flags)
2556 SvFLAGS(sv) |= mgs->mgs_flags;
2557 else
2558 mg_magical(sv);
2559 if (SvGMAGICAL(sv))
2560 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2561 }
2562
2563 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2564
2565 /* If we're still on top of the stack, pop us off. (That condition
2566 * will be satisfied if restore_magic was called explicitly, but *not*
2567 * if it's being called via leave_scope.)
2568 * The reason for doing this is that otherwise, things like sv_2cv()
2569 * may leave alloc gunk on the savestack, and some code
2570 * (e.g. sighandler) doesn't expect that...
2571 */
2572 if (PL_savestack_ix == mgs->mgs_ss_ix)
2573 {
2574 I32 popval = SSPOPINT;
c76ac1ee 2575 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2576 PL_savestack_ix -= 2;
2577 popval = SSPOPINT;
2578 assert(popval == SAVEt_ALLOC);
2579 popval = SSPOPINT;
2580 PL_savestack_ix -= popval;
2581 }
2582
2583}
2584
2585static void
acfe0abc 2586unwind_handler_stack(pTHX_ void *p)
51371543 2587{
51371543
GS
2588 U32 flags = *(U32*)p;
2589
2590 if (flags & 1)
2591 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2592 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2593#if !defined(PERL_IMPLICIT_CONTEXT)
51371543
GS
2594 if (flags & 64)
2595 SvREFCNT_dec(sig_sv);
df3728a2 2596#endif
51371543 2597}
1018e26f
NIS
2598
2599