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