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