Upgrade to podlators 1.27.
[p5sagit/p5-mst-13.2.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
79072805 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 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 14 */
15
ccfc67b7 16/*
17=head1 Magical Functions
18*/
19
79072805 20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_MG_C
79072805 22#include "perl.h"
23
5cd24f17 24#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221 25# ifndef NGROUPS
26# define NGROUPS 32
27# endif
b7953727 28# ifdef I_GRP
29# include <grp.h>
30# endif
188ea221 31#endif
32
17aa7f3d 33#ifdef __hpux
34# include <sys/pstat.h>
35#endif
36
e69880a5 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 45# define FAKE_DEFAULT_SIGNAL_HANDLERS
46#endif
85b332e2 47
acfe0abc 48static void restore_magic(pTHX_ void *p);
49static void unwind_handler_stack(pTHX_ void *p);
51371543 50
c07a80fd 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 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 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 74
75 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd 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 79
80 SvMAGICAL_off(sv);
81 SvREADONLY_off(sv);
06759ea0 82 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd 83}
84
954c1994 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 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 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 105 SvRMAGICAL_on(sv);
106 }
107 }
108}
109
954c1994 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 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 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 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 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 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 169 return 0;
170}
171
954c1994 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 185 MAGIC* nextmg;
186
455ece5e 187 mgs_ix = SSNEW(sizeof(MGS));
188 save_magic(mgs_ix, sv);
463ee0b2 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 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 202 return 0;
203}
204
954c1994 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 215{
216 MAGIC* mg;
463ee0b2 217 STRLEN len;
463ee0b2 218
79072805 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 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 229 return len;
230 }
231 }
232
7719e241 233 if (DO_UTF8(sv))
5636d518 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 241}
242
8fb26106 243I32
864dbfa3 244Perl_mg_size(pTHX_ SV *sv)
93965878 245{
246 MAGIC* mg;
247 I32 len;
ac27b0f5 248
93965878 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 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 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 271 break;
272 }
273 return 0;
274}
275
954c1994 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 290 mgs_ix = SSNEW(sizeof(MGS));
291 save_magic(mgs_ix, sv);
463ee0b2 292
79072805 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 302 return 0;
303}
304
954c1994 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 315{
316 MAGIC* mg;
3f8f4626 317 if (!sv)
318 return 0;
93a17b20 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 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 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 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 354}
355
954c1994 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 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 380 if (mg->mg_flags & MGf_REFCOUNTED)
381 SvREFCNT_dec(mg->mg_obj);
79072805 382 Safefree(mg);
383 }
384 SvMAGIC(sv) = 0;
385 return 0;
386}
387
d460ef45 388
79072805 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 399 if (mg->mg_obj) /* @+ */
400 return rx->nparens;
401 else /* @- */
402 return rx->lastparen;
403 }
ac27b0f5 404
942e002e 405 return (U32)-1;
6cef1e77 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 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 418 paren = mg->mg_len;
419 if (paren < 0)
420 return 0;
eb160463 421 if (paren <= (I32)rx->nparens &&
cf93c79d 422 (s = rx->startp[paren]) != -1 &&
423 (t = rx->endp[paren]) != -1)
6cef1e77 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 432 if (b)
433 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 434 }
0064a8a9 435
436 sv_setiv(sv, i);
6cef1e77 437 }
438 }
439 return 0;
440}
441
e4b89193 442int
a29d06ed 443Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
444{
a29d06ed 445 Perl_croak(aTHX_ PL_no_modify);
e4b89193 446 /* NOT REACHED */
447 return 0;
a29d06ed 448}
449
93a17b20 450U32
864dbfa3 451Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20 452{
453 register I32 paren;
93a17b20 454 register I32 i;
d9f97599 455 register REGEXP *rx;
a197cbdd 456 I32 s1, t1;
93a17b20 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 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 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 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 496 if (paren)
497 goto getparen;
93a17b20 498 }
748a9306 499 return 0;
a01268b5 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 509 if (rx->startp[0] != -1) {
510 i = rx->startp[0];
a197cbdd 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 521 if (rx->endp[0] != -1) {
522 i = rx->sublen - rx->endp[0];
a197cbdd 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 531 }
532 magic_get(sv,mg);
2d8e6c8d 533 if (!SvPOK(sv) && SvNIOK(sv)) {
534 STRLEN n_a;
535 sv_2pv(sv, &n_a);
536 }
93a17b20 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 544{
545 register I32 paren;
546 register char *s;
547 register I32 i;
d9f97599 548 register REGEXP *rx;
79072805 549
550 switch (*mg->mg_ptr) {
748a9306 551 case '\001': /* ^A */
3280af22 552 sv_setsv(sv, PL_bodytarget);
748a9306 553 break;
49460fe6 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 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 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 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 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 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 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 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 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 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 668 }
669 else if (strEQ(mg->mg_ptr, "\024AINT"))
9aa05f58 670 sv_setiv(sv, PL_tainting
671 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
672 : 0);
7c36658b 673 break;
a05d7ebb 674 case '\025': /* $^UNICODE */
675 if (strEQ(mg->mg_ptr, "\025NICODE"))
676 sv_setuv(sv, (UV) PL_unicode);
fde18df1 677 break;
678 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7 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 682 if (PL_compiling.cop_warnings == pWARN_NONE ||
683 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7 684 {
685 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
686 }
d3a7d8c7 687 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca 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 699 else {
700 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 701 }
d3a7d8c7 702 SvPOK_only(sv);
4438c4b7 703 }
79072805 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 708 I32 s1, t1;
709
a863c7d1 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 717 (s1 = rx->startp[paren]) != -1 &&
718 (t1 = rx->endp[paren]) != -1)
bbce6d69 719 {
cf93c79d 720 i = t1 - s1;
721 s = rx->subbeg + s1;
01ec43d0 722 if (!rx->subbeg)
c2e66d9e 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 729 SvUTF8_on(sv);
730 else
731 SvUTF8_off(sv);
e9814ee1 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 746 break;
747 }
79072805 748 }
79072805 749 }
3280af22 750 sv_setsv(sv,&PL_sv_undef);
79072805 751 break;
752 case '+':
aaa362c4 753 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 754 paren = rx->lastparen;
a0d0e21e 755 if (paren)
756 goto getparen;
79072805 757 }
3280af22 758 sv_setsv(sv,&PL_sv_undef);
79072805 759 break;
a01268b5 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 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 776 break;
777 case '\'':
aaa362c4 778 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 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 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 791 }
792#endif
793 break;
794 case '?':
809a5acc 795 {
809a5acc 796 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 797#ifdef COMPLEX_STATUS
6b88bc9c 798 LvTARGOFF(sv) = PL_statusvalue;
799 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 800#endif
809a5acc 801 }
79072805 802 break;
803 case '^':
3280af22 804 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805 805 if (s)
806 sv_setpv(sv,s);
807 else {
3280af22 808 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805 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 816 sv_setpv(sv,s);
817 break;
818#ifndef lint
819 case '=':
3280af22 820 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805 821 break;
822 case '-':
3280af22 823 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805 824 break;
825 case '%':
3280af22 826 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 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 835 break;
836 case '|':
3280af22 837 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805 838 break;
839 case ',':
79072805 840 break;
841 case '\\':
b2ce0fda 842 if (PL_ors_sv)
f28098ff 843 sv_copypv(sv, PL_ors_sv);
79072805 844 break;
845 case '#':
3280af22 846 sv_setpv(sv,PL_ofmt);
79072805 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 853 {
854 int saveerrno = errno;
65202027 855 sv_setnv(sv, (NV)errno);
88e89b8a 856#ifdef OS2
ed344e4f 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 862 errno = saveerrno;
863 }
864#endif
946ec16e 865 SvNOK_on(sv); /* what a wonderful hack! */
79072805 866 break;
867 case '<':
3280af22 868 sv_setiv(sv, (IV)PL_uid);
79072805 869 break;
870 case '>':
3280af22 871 sv_setiv(sv, (IV)PL_euid);
79072805 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 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 891 }
892#endif
155aba94 893 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 894 break;
cd39f2b6 895#ifndef MACOS_TRADITIONAL
79072805 896 case '0':
897 break;
cd39f2b6 898#endif
79072805 899 }
a0d0e21e 900 return 0;
79072805 901}
902
903int
864dbfa3 904Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 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 910 return 0;
911}
912
913int
864dbfa3 914Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805 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 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 931 }
932#endif
1e422769 933
39e571d4 934#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805 935 /* And you'll never guess what the dog had */
936 /* in its mouth... */
3280af22 937 if (PL_tainting) {
1e422769 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 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 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 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 979 return 0;
980 }
463ee0b2 981 }
79072805 982 }
983 }
39e571d4 984#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 985
79072805 986 return 0;
987}
988
989int
864dbfa3 990Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 991{
2d8e6c8d 992 STRLEN n_a;
993 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83 994 return 0;
995}
996
88e89b8a 997int
864dbfa3 998Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 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 1006 magic_clear_all_env(sv,mg);
1007 hv_iterinit((HV*)sv);
155aba94 1008 while ((entry = hv_iternext((HV*)sv))) {
fb73857a 1009 I32 keylen;
1010 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 1011 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a 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 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 1034 I32 i;
1035
3280af22 1036 if (environ == PL_origenviron)
f2517201 1037 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557 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 1044 }
1045# endif /* USE_ENVIRON_ARRAY */
052c2dc6 1046# endif /* PERL_IMPLICIT_SYS || WIN32 */
1047#endif /* VMS || EPC */
3e3baf6d 1048 return 0;
66b1d557 1049}
1050
2e34cc90 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 1055static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1056#endif
2e34cc90 1057#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1058static int sig_defaulting[SIG_SIZE];
1059#endif
85b332e2 1060
64ca3a65 1061#ifndef PERL_MICRO
2d4fcd5e 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 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 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 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 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 1095 SvTEMP_off(sv);
1096 }
1097 }
1098 return 0;
1099}
1100int
864dbfa3 1101Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1102{
2d4fcd5e 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 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 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 1164 }
1165 return 0;
1166}
3d37d572 1167
0a8e0eff 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 1180#ifdef PERL_GET_SIG_CONTEXT
1181 dTHXa(PERL_GET_SIG_CONTEXT);
1182#else
85b332e2 1183 dTHX;
1184#endif
23ada85b 1185#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2 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 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 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 1203}
1204
2e34cc90 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 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 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 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 1238 }
1239 }
1240}
1241
85e6fe83 1242int
864dbfa3 1243Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 1244{
1245 register char *s;
1246 I32 i;
b7953727 1247 SV** svp = 0;
2d4fcd5e 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 1254#ifdef HAS_SIGPROCMASK
1255 sigset_t set, save;
1256 SV* save_sv;
1257#endif
a0d0e21e 1258
e72dc28c 1259 s = MgPV(mg,len);
748a9306 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 1270 *svp = 0;
1271 }
748a9306 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 1278 return 0;
1279 }
2d4fcd5e 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 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 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 1310#ifdef HAS_SIGPROCMASK
1311 LEAVE;
1312#endif
1313 }
748a9306 1314 else
1315 *svp = SvREFCNT_inc(sv);
2d4fcd5e 1316 if(to_dec)
1317 SvREFCNT_dec(to_dec);
a0d0e21e 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 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 1330 }
1331 else if (strEQ(s,"DEFAULT") || !*s) {
1332 if (i)
2e34cc90 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 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 1352 else
1353 *svp = SvREFCNT_inc(sv);
79072805 1354 }
2d4fcd5e 1355#ifdef HAS_SIGPROCMASK
1356 if(i)
1357 LEAVE;
1358#endif
1359 if(to_dec)
1360 SvREFCNT_dec(to_dec);
79072805 1361 return 0;
1362}
64ca3a65 1363#endif /* !PERL_MICRO */
79072805 1364
1365int
864dbfa3 1366Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1367{
3280af22 1368 PL_sub_generation++;
463ee0b2 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 1378 return 0;
1379}
463ee0b2 1380
946ec16e 1381int
864dbfa3 1382Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1383{
1384 HV *hv = (HV*)LvTARG(sv);
6ff81951 1385 I32 i = 0;
7719e241 1386
6ff81951 1387 if (hv) {
497b47a8 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 1395 }
1396
1397 sv_setiv(sv, (IV)i);
1398 return 0;
1399}
1400
1401int
864dbfa3 1402Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e 1403{
1404 if (LvTARG(sv)) {
1405 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e 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 1413{
1414 dSP;
463ee0b2 1415
924508f0 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 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 1428 }
1429 }
1430 if (n > 2) {
1431 PUSHs(val);
88e89b8a 1432 }
463ee0b2 1433 PUTBACK;
1434
864dbfa3 1435 return call_method(meth, flags);
946ec16e 1436}
1437
76e3520e 1438STATIC int
cea2e8a9 1439S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e 1440{
1441 dSP;
463ee0b2 1442
a0d0e21e 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 1452 FREETMPS;
1453 LEAVE;
1454 return 0;
1455}
463ee0b2 1456
a0d0e21e 1457int
864dbfa3 1458Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1459{
1460 magic_methpack(sv,mg,"FETCH");
1461 if (mg->mg_ptr)
1462 mg->mg_flags |= MGf_GSKIP;
463ee0b2 1463 return 0;
1464}
1465
1466int
864dbfa3 1467Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 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 1475 return 0;
1476}
1477
1478int
864dbfa3 1479Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1480{
a0d0e21e 1481 return magic_methpack(sv,mg,"DELETE");
1482}
463ee0b2 1483
93965878 1484
1485U32
864dbfa3 1486Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1487{
e336de0d 1488 dSP;
93965878 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 1499 FREETMPS;
1500 LEAVE;
1501 return retval;
1502}
1503
cea2e8a9 1504int
1505Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 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 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 1525
1526 ENTER;
a0d0e21e 1527 SAVETMPS;
e788e7d3 1528 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1529 PUSHMARK(SP);
1530 EXTEND(SP, 2);
33c27489 1531 PUSHs(SvTIED_obj(sv, mg));
463ee0b2 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 1540 FREETMPS;
1541 LEAVE;
79072805 1542 return 0;
1543}
1544
1545int
864dbfa3 1546Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1547{
1548 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1549}
a0d0e21e 1550
1551int
864dbfa3 1552Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 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 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 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 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 1585 return 0;
1586}
1587
1588int
864dbfa3 1589Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 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 1598 sv_pos_b2u(lsv, &i);
1599 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e 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 1609{
1610 SV* lsv = LvTARG(sv);
1611 SSize_t pos;
1612 STRLEN len;
c00206c8 1613 STRLEN ulen = 0;
a0d0e21e 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 1619 if (!mg) {
1620 if (!SvOK(sv))
1621 return 0;
14befaf4 1622 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1623 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e 1624 }
1625 else if (!SvOK(sv)) {
565764a8 1626 mg->mg_len = -1;
a0d0e21e 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 1634 ulen = sv_len_utf8(lsv);
1635 if (ulen)
1636 len = ulen;
a0ed51b3 1637 }
1638
a0d0e21e 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 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 1656 return 0;
1657}
1658
1659int
864dbfa3 1660Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1661{
8646b087 1662 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1663 SvFAKE_off(sv);
946ec16e 1664 gv_efullname3(sv,((GV*)sv), "*");
8646b087 1665 SvFAKE_on(sv);
1666 }
1667 else
946ec16e 1668 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805 1669 return 0;
1670}
1671
1672int
864dbfa3 1673Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1674{
1675 register char *s;
1676 GV* gv;
2d8e6c8d 1677 STRLEN n_a;
79072805 1678
1679 if (!SvOK(sv))
1680 return 0;
2d8e6c8d 1681 s = SvPV(sv, n_a);
79072805 1682 if (*s == '*' && s[1])
1683 s++;
85e6fe83 1684 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805 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 1690 return 0;
1691}
1692
1693int
864dbfa3 1694Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951 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 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 1707 rem = len - offs;
1708 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1709 if (SvUTF8(lsv))
2ef4b674 1710 SvUTF8_on(sv);
6ff81951 1711 return 0;
1712}
1713
1714int
864dbfa3 1715Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1716{
9aa983d2 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 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 1732 sv_insert(lsv, lvoff, lvlen, tmps, len);
1733 Safefree(tmps);
1aa99e6b 1734 }
1735 else
9aa983d2 1736 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1737
79072805 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 1746 return 0;
1747}
1748
1749int
864dbfa3 1750Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1751{
3280af22 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 1762 return 0;
1763}
1764
1765int
864dbfa3 1766Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1767{
1768 SV *lsv = LvTARG(sv);
6ff81951 1769
1770 if (!lsv) {
155aba94 1771 (void)SvOK_off(sv);
6ff81951 1772 return 0;
1773 }
6ff81951 1774
81e118e0 1775 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951 1776 return 0;
1777}
1778
1779int
864dbfa3 1780Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 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 1793 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1794 if (he)
1795 targ = HeVAL(he);
68dc0745 1796 }
1797 else {
3c78fafa 1798 AV* av = (AV*)LvTARG(sv);
68dc0745 1799 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1800 targ = AvARRAY(av)[LvTARGOFF(sv)];
1801 }
3280af22 1802 if (targ && targ != &PL_sv_undef) {
68dc0745 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 1812 else
1813 targ = LvTARG(sv);
3280af22 1814 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc 1815 return 0;
1816}
1817
1818int
864dbfa3 1819Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 1820{
1821 if (LvTARGLEN(sv))
68dc0745 1822 vivify_defelem(sv);
1823 if (LvTARG(sv)) {
5f05dabc 1824 sv_setsv(LvTARG(sv), sv);
68dc0745 1825 SvSETMAGIC(LvTARG(sv));
1826 }
5f05dabc 1827 return 0;
1828}
1829
71be2cbc 1830void
864dbfa3 1831Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1832{
74e13ce4 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 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 1847 else {
1848 AV* av = (AV*)LvTARG(sv);
5aabfad6 1849 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745 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 1855 }
1856 }
3e3baf6d 1857 (void)SvREFCNT_inc(value);
68dc0745 1858 SvREFCNT_dec(LvTARG(sv));
1859 LvTARG(sv) = value;
71be2cbc 1860 LvTARGLEN(sv) = 0;
68dc0745 1861 SvREFCNT_dec(mg->mg_obj);
1862 mg->mg_obj = Nullsv;
1863 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc 1864}
1865
1866int
864dbfa3 1867Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 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 1876 /* XXX Should we check that it hasn't changed? */
1877 SvRV(svp[i]) = 0;
155aba94 1878 (void)SvOK_off(svp[i]);
810b8aa5 1879 SvWEAKREF_off(svp[i]);
1880 svp[i] = &PL_sv_undef;
1881 }
1882 i--;
1883 }
d99b02a1 1884 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5 1885 return 0;
1886}
1887
1888int
864dbfa3 1889Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1890{
565764a8 1891 mg->mg_len = -1;
c6496cc7 1892 SvSCREAM_off(sv);
93a17b20 1893 return 0;
1894}
1895
1896int
864dbfa3 1897Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1898{
14befaf4 1899 sv_unmagic(sv, PERL_MAGIC_bm);
79072805 1900 SvVALID_off(sv);
1901 return 0;
1902}
1903
1904int
864dbfa3 1905Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1906{
14befaf4 1907 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff 1908 SvCOMPILED_off(sv);
1909 return 0;
1910}
1911
1912int
864dbfa3 1913Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 1914{
1915 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1916
1917 if (uf && uf->uf_set)
24f81a43 1918 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805 1919 return 0;
1920}
1921
c277df42 1922int
faf82a0b 1923Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1924{
1925 sv_unmagic(sv, PERL_MAGIC_qr);
1926 return 0;
1927}
1928
1929int
864dbfa3 1930Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42 1931{
1932 regexp *re = (regexp *)mg->mg_obj;
1933 ReREFCNT_dec(re);
1934 return 0;
1935}
1936
7a4c00b4 1937#ifdef USE_LOCALE_COLLATE
79072805 1938int
864dbfa3 1939Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69 1940{
1941 /*
838b5b74 1942 * RenE<eacute> Descartes said "I think not."
bbce6d69 1943 * and vanished with a faint plop.
1944 */
7a4c00b4 1945 if (mg->mg_ptr) {
1946 Safefree(mg->mg_ptr);
1947 mg->mg_ptr = NULL;
565764a8 1948 mg->mg_len = -1;
7a4c00b4 1949 }
bbce6d69 1950 return 0;
1951}
7a4c00b4 1952#endif /* USE_LOCALE_COLLATE */
bbce6d69 1953
7e8c5dac 1954/* Just clear the UTF-8 cache data. */
1955int
1956Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1957{
1958 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1959 mg->mg_ptr = 0;
1960 mg->mg_len = -1; /* The mg_len holds the len cache. */
1961 return 0;
1962}
1963
bbce6d69 1964int
864dbfa3 1965Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 1966{
1967 register char *s;
1968 I32 i;
8990e307 1969 STRLEN len;
79072805 1970 switch (*mg->mg_ptr) {
748a9306 1971 case '\001': /* ^A */
3280af22 1972 sv_setsv(PL_bodytarget, sv);
748a9306 1973 break;
49460fe6 1974 case '\003': /* ^C */
eb160463 1975 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6 1976 break;
1977
79072805 1978 case '\004': /* ^D */
b4ab917c 1979#ifdef DEBUGGING
1980 s = SvPV_nolen(sv);
1981 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
79072805 1982 DEBUG_x(dump_all());
b4ab917c 1983#else
1984 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1985#endif
79072805 1986 break;
28f23441 1987 case '\005': /* ^E */
d0063567 1988 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 1989#ifdef MACOS_TRADITIONAL
d0063567 1990 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1991#else
cd39f2b6 1992# ifdef VMS
d0063567 1993 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1994# else
cd39f2b6 1995# ifdef WIN32
d0063567 1996 SetLastError( SvIV(sv) );
cd39f2b6 1997# else
9fed8b87 1998# ifdef OS2
d0063567 1999 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2000# else
d0063567 2001 /* will anyone ever use this? */
2002 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2003# endif
048c1ddf 2004# endif
2005# endif
22fae026 2006#endif
d0063567 2007 }
2008 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2009 if (PL_encoding)
2010 SvREFCNT_dec(PL_encoding);
2011 if (SvOK(sv) || SvGMAGICAL(sv)) {
2012 PL_encoding = newSVsv(sv);
2013 }
2014 else {
2015 PL_encoding = Nullsv;
2016 }
2017 }
2018 break;
79072805 2019 case '\006': /* ^F */
3280af22 2020 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2021 break;
a0d0e21e 2022 case '\010': /* ^H */
3280af22 2023 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2024 break;
9d116dd7 2025 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22 2026 if (PL_inplace)
2027 Safefree(PL_inplace);
79072805 2028 if (SvOK(sv))
2d8e6c8d 2029 PL_inplace = savepv(SvPV(sv,len));
79072805 2030 else
3280af22 2031 PL_inplace = Nullch;
79072805 2032 break;
28f23441 2033 case '\017': /* ^O */
ac27b0f5 2034 if (*(mg->mg_ptr+1) == '\0') {
2035 if (PL_osname)
2036 Safefree(PL_osname);
2037 if (SvOK(sv))
2038 PL_osname = savepv(SvPV(sv,len));
2039 else
2040 PL_osname = Nullch;
2041 }
2042 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2043 if (!PL_compiling.cop_io)
2044 PL_compiling.cop_io = newSVsv(sv);
2045 else
2046 sv_setsv(PL_compiling.cop_io,sv);
2047 }
28f23441 2048 break;
79072805 2049 case '\020': /* ^P */
3280af22 2050 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e 2051 if (PL_perldb && !PL_DBsingle)
2052 init_debugger();
79072805 2053 break;
2054 case '\024': /* ^T */
88e89b8a 2055#ifdef BIG_TIME
6b88bc9c 2056 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2057#else
3280af22 2058 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2059#endif
79072805 2060 break;
fde18df1 2061 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7 2062 if (*(mg->mg_ptr+1) == '\0') {
2063 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2064 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2065 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2066 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2067 }
599cee73 2068 }
0a378802 2069 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2070 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e 2071 if (!SvPOK(sv) && PL_localizing) {
2072 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2073 PL_compiling.cop_warnings = pWARN_NONE;
d775746e 2074 break;
2075 }
f4fc7782 2076 {
b5477537 2077 STRLEN len, i;
d3a7d8c7 2078 int accumulate = 0 ;
f4fc7782 2079 int any_fatals = 0 ;
d3a7d8c7 2080 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782 2081 for (i = 0 ; i < len ; ++i) {
2082 accumulate |= ptr[i] ;
2083 any_fatals |= (ptr[i] & 0xAA) ;
2084 }
d3a7d8c7 2085 if (!accumulate)
2086 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782 2087 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2088 PL_compiling.cop_warnings = pWARN_ALL;
2089 PL_dowarn |= G_WARN_ONCE ;
727405f8 2090 }
d3a7d8c7 2091 else {
2092 if (specialWARN(PL_compiling.cop_warnings))
2093 PL_compiling.cop_warnings = newSVsv(sv) ;
2094 else
2095 sv_setsv(PL_compiling.cop_warnings, sv);
2096 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2097 PL_dowarn |= G_WARN_ONCE ;
2098 }
f4fc7782 2099
d3a7d8c7 2100 }
4438c4b7 2101 }
971a9dd3 2102 }
79072805 2103 break;
2104 case '.':
3280af22 2105 if (PL_localizing) {
2106 if (PL_localizing == 1)
7766f137 2107 SAVESPTR(PL_last_in_gv);
748a9306 2108 }
3280af22 2109 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2110 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805 2111 break;
2112 case '^':
3280af22 2113 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2114 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2115 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 2116 break;
2117 case '~':
3280af22 2118 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2119 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2120 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 2121 break;
2122 case '=':
632db599 2123 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 2124 break;
2125 case '-':
632db599 2126 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22 2127 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2128 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805 2129 break;
2130 case '%':
632db599 2131 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 2132 break;
2133 case '|':
4b65379b 2134 {
3280af22 2135 IO *io = GvIOp(PL_defoutgv);
720f287d 2136 if(!io)
2137 break;
4b65379b 2138 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2139 IoFLAGS(io) &= ~IOf_FLUSH;
2140 else {
2141 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2142 PerlIO *ofp = IoOFP(io);
2143 if (ofp)
2144 (void)PerlIO_flush(ofp);
2145 IoFLAGS(io) |= IOf_FLUSH;
2146 }
2147 }
79072805 2148 }
2149 break;
79072805 2150 case '/':
3280af22 2151 SvREFCNT_dec(PL_rs);
8bfdd7d9 2152 PL_rs = newSVsv(sv);
79072805 2153 break;
2154 case '\\':
7889fe52 2155 if (PL_ors_sv)
2156 SvREFCNT_dec(PL_ors_sv);
009c130f 2157 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2158 PL_ors_sv = newSVsv(sv);
009c130f 2159 }
e3c19b7b 2160 else {
7889fe52 2161 PL_ors_sv = Nullsv;
e3c19b7b 2162 }
79072805 2163 break;
2164 case ',':
7889fe52 2165 if (PL_ofs_sv)
2166 SvREFCNT_dec(PL_ofs_sv);
2167 if (SvOK(sv) || SvGMAGICAL(sv)) {
2168 PL_ofs_sv = newSVsv(sv);
2169 }
2170 else {
2171 PL_ofs_sv = Nullsv;
2172 }
79072805 2173 break;
2174 case '#':
3280af22 2175 if (PL_ofmt)
2176 Safefree(PL_ofmt);
2d8e6c8d 2177 PL_ofmt = savepv(SvPV(sv,len));
79072805 2178 break;
2179 case '[':
3280af22 2180 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2181 break;
2182 case '?':
ff0cee69 2183#ifdef COMPLEX_STATUS
6b88bc9c 2184 if (PL_localizing == 2) {
2185 PL_statusvalue = LvTARGOFF(sv);
2186 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69 2187 }
2188 else
2189#endif
2190#ifdef VMSISH_STATUS
2191 if (VMSISH_STATUS)
2192 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2193 else
2194#endif
2195 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 2196 break;
2197 case '!':
93189314 2198 {
2199#ifdef VMS
2200# define PERL_VMS_BANG vaxc$errno
2201#else
2202# define PERL_VMS_BANG 0
2203#endif
91487cfc 2204 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314 2205 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2206 }
79072805 2207 break;
2208 case '<':
3280af22 2209 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2210 if (PL_delaymagic) {
2211 PL_delaymagic |= DM_RUID;
79072805 2212 break; /* don't do magic till later */
2213 }
2214#ifdef HAS_SETRUID
b28d0864 2215 (void)setruid((Uid_t)PL_uid);
79072805 2216#else
2217#ifdef HAS_SETREUID
3280af22 2218 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2219#else
85e6fe83 2220#ifdef HAS_SETRESUID
b28d0864 2221 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2222#else
b28d0864 2223 if (PL_uid == PL_euid) /* special case $< = $> */
2224 (void)PerlProc_setuid(PL_uid);
a0d0e21e 2225 else {
d8eceb89 2226 PL_uid = PerlProc_getuid();
cea2e8a9 2227 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2228 }
79072805 2229#endif
2230#endif
85e6fe83 2231#endif
d8eceb89 2232 PL_uid = PerlProc_getuid();
3280af22 2233 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2234 break;
2235 case '>':
3280af22 2236 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2237 if (PL_delaymagic) {
2238 PL_delaymagic |= DM_EUID;
79072805 2239 break; /* don't do magic till later */
2240 }
2241#ifdef HAS_SETEUID
3280af22 2242 (void)seteuid((Uid_t)PL_euid);
79072805 2243#else
2244#ifdef HAS_SETREUID
b28d0864 2245 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83 2246#else
2247#ifdef HAS_SETRESUID
6b88bc9c 2248 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2249#else
b28d0864 2250 if (PL_euid == PL_uid) /* special case $> = $< */
2251 PerlProc_setuid(PL_euid);
a0d0e21e 2252 else {
e8ee3774 2253 PL_euid = PerlProc_geteuid();
cea2e8a9 2254 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2255 }
79072805 2256#endif
2257#endif
85e6fe83 2258#endif
d8eceb89 2259 PL_euid = PerlProc_geteuid();
3280af22 2260 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2261 break;
2262 case '(':
3280af22 2263 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2264 if (PL_delaymagic) {
2265 PL_delaymagic |= DM_RGID;
79072805 2266 break; /* don't do magic till later */
2267 }
2268#ifdef HAS_SETRGID
b28d0864 2269 (void)setrgid((Gid_t)PL_gid);
79072805 2270#else
2271#ifdef HAS_SETREGID
3280af22 2272 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83 2273#else
2274#ifdef HAS_SETRESGID
b28d0864 2275 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2276#else
b28d0864 2277 if (PL_gid == PL_egid) /* special case $( = $) */
2278 (void)PerlProc_setgid(PL_gid);
748a9306 2279 else {
d8eceb89 2280 PL_gid = PerlProc_getgid();
cea2e8a9 2281 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2282 }
79072805 2283#endif
2284#endif
85e6fe83 2285#endif
d8eceb89 2286 PL_gid = PerlProc_getgid();
3280af22 2287 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2288 break;
2289 case ')':
5cd24f17 2290#ifdef HAS_SETGROUPS
2291 {
2d8e6c8d 2292 char *p = SvPV(sv, len);
5cd24f17 2293 Groups_t gary[NGROUPS];
2294
5cd24f17 2295 while (isSPACE(*p))
2296 ++p;
2d4389e4 2297 PL_egid = Atol(p);
5cd24f17 2298 for (i = 0; i < NGROUPS; ++i) {
2299 while (*p && !isSPACE(*p))
2300 ++p;
2301 while (isSPACE(*p))
2302 ++p;
2303 if (!*p)
2304 break;
2d4389e4 2305 gary[i] = Atol(p);
5cd24f17 2306 }
8cc95fdb 2307 if (i)
2308 (void)setgroups(i, gary);
5cd24f17 2309 }
2310#else /* HAS_SETGROUPS */
b28d0864 2311 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2312#endif /* HAS_SETGROUPS */
3280af22 2313 if (PL_delaymagic) {
2314 PL_delaymagic |= DM_EGID;
79072805 2315 break; /* don't do magic till later */
2316 }
2317#ifdef HAS_SETEGID
3280af22 2318 (void)setegid((Gid_t)PL_egid);
79072805 2319#else
2320#ifdef HAS_SETREGID
b28d0864 2321 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83 2322#else
2323#ifdef HAS_SETRESGID
b28d0864 2324 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2325#else
b28d0864 2326 if (PL_egid == PL_gid) /* special case $) = $( */
2327 (void)PerlProc_setgid(PL_egid);
748a9306 2328 else {
d8eceb89 2329 PL_egid = PerlProc_getegid();
cea2e8a9 2330 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2331 }
79072805 2332#endif
2333#endif
85e6fe83 2334#endif
d8eceb89 2335 PL_egid = PerlProc_getegid();
3280af22 2336 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 2337 break;
2338 case ':':
2d8e6c8d 2339 PL_chopset = SvPV_force(sv,len);
79072805 2340 break;
cd39f2b6 2341#ifndef MACOS_TRADITIONAL
79072805 2342 case '0':
e2975953 2343 LOCK_DOLLARZERO_MUTEX;
4bc88a62 2344#ifdef HAS_SETPROCTITLE
2345 /* The BSDs don't show the argv[] in ps(1) output, they
2346 * show a string from the process struct and provide
2347 * the setproctitle() routine to manipulate that. */
2348 {
2349 s = SvPV(sv, len);
98b76f99 2350# if __FreeBSD_version > 410001
9aad2c0e 2351 /* The leading "-" removes the "perl: " prefix,
4bc88a62 2352 * but not the "(perl) suffix from the ps(1)
2353 * output, because that's what ps(1) shows if the
2354 * argv[] is modified. */
6f2ad931 2355 setproctitle("-%s", s);
9aad2c0e 2356# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62 2357 /* This doesn't really work if you assume that
2358 * $0 = 'foobar'; will wipe out 'perl' from the $0
2359 * because in ps(1) output the result will be like
2360 * sprintf("perl: %s (perl)", s)
2361 * I guess this is a security feature:
2362 * one (a user process) cannot get rid of the original name.
2363 * --jhi */
2364 setproctitle("%s", s);
2365# endif
2366 }
2367#endif
17aa7f3d 2368#if defined(__hpux) && defined(PSTAT_SETCMD)
2369 {
2370 union pstun un;
2371 s = SvPV(sv, len);
2372 un.pst_command = s;
2373 pstat(PSTAT_SETCMD, un, len, 0, 0);
2374 }
2375#endif
3cb9023d 2376 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2377 s = SvPV_force(sv,len);
8bafa735 2378 if (len >= (STRLEN)PL_origalen) {
54bfe034 2379 /* Longer than original, will be truncated. */
2380 Copy(s, PL_origargv[0], PL_origalen, char);
2381 PL_origargv[0][PL_origalen - 1] = 0;
79072805 2382 }
2383 else {
54bfe034 2384 /* Shorter than original, will be padded. */
2385 Copy(s, PL_origargv[0], len, char);
2386 PL_origargv[0][len] = 0;
2387 memset(PL_origargv[0] + len + 1,
2388 /* Is the space counterintuitive? Yes.
2389 * (You were expecting \0?)
3cb9023d 2390 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034 2391 * --jhi */
2392 (int)' ',
2393 PL_origalen - len - 1);
3280af22 2394 for (i = 1; i < PL_origargc; i++)
54bfe034 2395 PL_origargv[i] = 0;
79072805 2396 }
e2975953 2397 UNLOCK_DOLLARZERO_MUTEX;
79072805 2398 break;
cd39f2b6 2399#endif
79072805 2400 }
2401 return 0;
2402}
2403
2404I32
864dbfa3 2405Perl_whichsig(pTHX_ char *sig)
79072805 2406{
2407 register char **sigv;
2408
e02bfb16 2409 for (sigv = PL_sig_name; *sigv; sigv++)
79072805 2410 if (strEQ(sig,*sigv))
22c35a8c 2411 return PL_sig_num[sigv - PL_sig_name];
79072805 2412#ifdef SIGCLD
2413 if (strEQ(sig,"CHLD"))
2414 return SIGCLD;
2415#endif
2416#ifdef SIGCHLD
2417 if (strEQ(sig,"CLD"))
2418 return SIGCHLD;
2419#endif
7f1236c0 2420 return -1;
79072805 2421}
2422
df3728a2 2423#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2424static SV* sig_sv;
df3728a2 2425#endif
84902520 2426
ecfc5424 2427Signal_t
cea2e8a9 2428Perl_sighandler(int sig)
79072805 2429{
1018e26f 2430#ifdef PERL_GET_SIG_CONTEXT
2431 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2432#else
cea2e8a9 2433 dTHX;
71d280e3 2434#endif
79072805 2435 dSP;
00d579c5 2436 GV *gv = Nullgv;
a0d0e21e 2437 HV *st;
b7953727 2438 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2439 CV *cv = Nullcv;
533c011a 2440 OP *myop = PL_op;
84902520 2441 U32 flags = 0;
3280af22 2442 XPV *tXpv = PL_Xpv;
71d280e3 2443
3280af22 2444 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2445 flags |= 1;
3280af22 2446 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2447 flags |= 4;
3280af22 2448 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2449 flags |= 8;
3280af22 2450 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520 2451 flags |= 16;
2452
727405f8 2453 if (!PL_psig_ptr[sig]) {
99ef548b 2454 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8 2455 PL_sig_name[sig]);
2456 exit(sig);
2457 }
ff0cee69 2458
84902520 2459 /* Max number of items pushed there is 3*n or 4. We cannot fix
2460 infinity, so we fix 4 (in fact 5): */
2461 if (flags & 1) {
3280af22 2462 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2463 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2464 }
ac27b0f5 2465 if (flags & 4)
3280af22 2466 PL_markstack_ptr++; /* Protect mark. */
84902520 2467 if (flags & 8) {
3280af22 2468 PL_retstack_ix++;
2469 PL_retstack[PL_retstack_ix] = NULL;
84902520 2470 }
2471 if (flags & 16)
3280af22 2472 PL_scopestack_ix += 1;
84902520 2473 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2474 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2475 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2476 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2477
a0d0e21e 2478 if (!cv || !CvROOT(cv)) {
599cee73 2479 if (ckWARN(WARN_SIGNAL))
9014280d 2480 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2481 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5 2482 : ((cv && CvGV(cv))
2483 ? GvENAME(CvGV(cv))
2484 : "__ANON__")));
2485 goto cleanup;
79072805 2486 }
2487
22c35a8c 2488 if(PL_psig_name[sig]) {
2489 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2490 flags |= 64;
df3728a2 2491#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2492 sig_sv = sv;
df3728a2 2493#endif
84902520 2494 } else {
ff0cee69 2495 sv = sv_newmortal();
22c35a8c 2496 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2497 }
e336de0d 2498
e788e7d3 2499 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2500 PUSHMARK(SP);
79072805 2501 PUSHs(sv);
79072805 2502 PUTBACK;
a0d0e21e 2503
1b266415 2504 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2505
d3acc0f7 2506 POPSTACK;
1b266415 2507 if (SvTRUE(ERRSV)) {
1d615522 2508#ifndef PERL_MICRO
983dbef6 2509#ifdef HAS_SIGPROCMASK
1b266415 2510 /* Handler "died", for example to get out of a restart-able read().
2511 * Before we re-do that on its behalf re-enable the signal which was
2512 * blocked by the system when we entered.
2513 */
2514 sigset_t set;
2515 sigemptyset(&set);
2516 sigaddset(&set,sig);
2517 sigprocmask(SIG_UNBLOCK, &set, NULL);
2518#else
2519 /* Not clear if this will work */
2520 (void)rsignal(sig, SIG_IGN);
0a8e0eff 2521 (void)rsignal(sig, &Perl_csighandler);
1b266415 2522#endif
1d615522 2523#endif /* !PERL_MICRO */
b3fe4827 2524 Perl_die(aTHX_ Nullformat);
1b266415 2525 }
00d579c5 2526cleanup:
84902520 2527 if (flags & 1)
3280af22 2528 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2529 if (flags & 4)
3280af22 2530 PL_markstack_ptr--;
ac27b0f5 2531 if (flags & 8)
3280af22 2532 PL_retstack_ix--;
84902520 2533 if (flags & 16)
3280af22 2534 PL_scopestack_ix -= 1;
84902520 2535 if (flags & 64)
2536 SvREFCNT_dec(sv);
533c011a 2537 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2538
3280af22 2539 PL_Sv = tSv; /* Restore global temporaries. */
2540 PL_Xpv = tXpv;
79072805 2541 return;
2542}
4e35701f 2543
2544
51371543 2545static void
acfe0abc 2546restore_magic(pTHX_ void *p)
51371543 2547{
48944bdf 2548 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543 2549 SV* sv = mgs->mgs_sv;
2550
2551 if (!sv)
2552 return;
2553
2554 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2555 {
2556 if (mgs->mgs_flags)
2557 SvFLAGS(sv) |= mgs->mgs_flags;
2558 else
2559 mg_magical(sv);
2560 if (SvGMAGICAL(sv))
2561 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2562 }
2563
2564 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2565
2566 /* If we're still on top of the stack, pop us off. (That condition
2567 * will be satisfied if restore_magic was called explicitly, but *not*
2568 * if it's being called via leave_scope.)
2569 * The reason for doing this is that otherwise, things like sv_2cv()
2570 * may leave alloc gunk on the savestack, and some code
2571 * (e.g. sighandler) doesn't expect that...
2572 */
2573 if (PL_savestack_ix == mgs->mgs_ss_ix)
2574 {
2575 I32 popval = SSPOPINT;
c76ac1ee 2576 assert(popval == SAVEt_DESTRUCTOR_X);
51371543 2577 PL_savestack_ix -= 2;
2578 popval = SSPOPINT;
2579 assert(popval == SAVEt_ALLOC);
2580 popval = SSPOPINT;
2581 PL_savestack_ix -= popval;
2582 }
2583
2584}
2585
2586static void
acfe0abc 2587unwind_handler_stack(pTHX_ void *p)
51371543 2588{
51371543 2589 U32 flags = *(U32*)p;
2590
2591 if (flags & 1)
2592 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2593 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2594#if !defined(PERL_IMPLICIT_CONTEXT)
51371543 2595 if (flags & 64)
2596 SvREFCNT_dec(sig_sv);
df3728a2 2597#endif
51371543 2598}
1018e26f 2599
2600