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