This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Whoops. We weren't actually testing hv_store_ent
[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
0a378802
JH
626 sv_setnv(sv, (NV)errno);
627 sv_setpv(sv, errno ? Strerror(errno) : "");
28f23441 628#endif
88e89b8a 629#endif
22fae026 630#endif
cd39f2b6 631#endif
0a378802
JH
632 SvNOK_on(sv); /* what a wonderful hack! */
633 }
634 else if (strEQ(mg->mg_ptr+1, "NCODING"))
635 sv_setsv(sv, PL_encoding);
636 break;
79072805 637 case '\006': /* ^F */
3280af22 638 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 639 break;
a0d0e21e 640 case '\010': /* ^H */
3280af22 641 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 642 break;
9d116dd7 643 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
644 if (PL_inplace)
645 sv_setpv(sv, PL_inplace);
79072805 646 else
3280af22 647 sv_setsv(sv, &PL_sv_undef);
79072805 648 break;
ac27b0f5
NIS
649 case '\017': /* ^O & ^OPEN */
650 if (*(mg->mg_ptr+1) == '\0')
651 sv_setpv(sv, PL_osname);
652 else if (strEQ(mg->mg_ptr, "\017PEN")) {
653 if (!PL_compiling.cop_io)
654 sv_setsv(sv, &PL_sv_undef);
655 else {
656 sv_setsv(sv, PL_compiling.cop_io);
657 }
658 }
28f23441 659 break;
79072805 660 case '\020': /* ^P */
3280af22 661 sv_setiv(sv, (IV)PL_perldb);
79072805 662 break;
fb73857a 663 case '\023': /* ^S */
4ffa73a3 664 if (*(mg->mg_ptr+1) == '\0') {
3280af22 665 if (PL_lex_state != LEX_NOTPARSING)
155aba94 666 (void)SvOK_off(sv);
3280af22 667 else if (PL_in_eval)
6dc8a9e4 668 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
669 else
670 sv_setiv(sv, 0);
d58bf5aa 671 }
fb73857a 672 break;
79072805 673 case '\024': /* ^T */
7c36658b 674 if (*(mg->mg_ptr+1) == '\0') {
88e89b8a 675#ifdef BIG_TIME
7c36658b 676 sv_setnv(sv, PL_basetime);
88e89b8a 677#else
7c36658b 678 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 679#endif
7c36658b
MS
680 }
681 else if (strEQ(mg->mg_ptr, "\024AINT"))
9aa05f58
RGS
682 sv_setiv(sv, PL_tainting
683 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
684 : 0);
7c36658b 685 break;
a05d7ebb
JH
686 case '\025': /* $^UNICODE */
687 if (strEQ(mg->mg_ptr, "\025NICODE"))
688 sv_setuv(sv, (UV) PL_unicode);
fde18df1
JH
689 break;
690 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
691 if (*(mg->mg_ptr+1) == '\0')
692 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
0a378802 693 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
d3a7d8c7
GS
694 if (PL_compiling.cop_warnings == pWARN_NONE ||
695 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7
JH
696 {
697 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
698 }
d3a7d8c7 699 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca
RGS
700 /* Get the bit mask for $warnings::Bits{all}, because
701 * it could have been extended by warnings::register */
702 SV **bits_all;
703 HV *bits=get_hv("warnings::Bits", FALSE);
704 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
705 sv_setsv(sv, *bits_all);
706 }
707 else {
708 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
709 }
ac27b0f5 710 }
4438c4b7
JH
711 else {
712 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 713 }
d3a7d8c7 714 SvPOK_only(sv);
4438c4b7 715 }
79072805
LW
716 break;
717 case '1': case '2': case '3': case '4':
718 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 719 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
720 I32 s1, t1;
721
a863c7d1
MB
722 /*
723 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
724 * XXX Does the new way break anything?
725 */
ffc61ed2 726 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 727 getparen:
eb160463 728 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
729 (s1 = rx->startp[paren]) != -1 &&
730 (t1 = rx->endp[paren]) != -1)
bbce6d69 731 {
cf93c79d
IZ
732 i = t1 - s1;
733 s = rx->subbeg + s1;
01ec43d0 734 if (!rx->subbeg)
c2e66d9e
GS
735 break;
736
13f57bf8 737 getrx:
748a9306 738 if (i >= 0) {
cf93c79d 739 sv_setpvn(sv, s, i);
a30b2f1f 740 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
7e2040f0
GS
741 SvUTF8_on(sv);
742 else
743 SvUTF8_off(sv);
e9814ee1
HS
744 if (PL_tainting) {
745 if (RX_MATCH_TAINTED(rx)) {
746 MAGIC* mg = SvMAGIC(sv);
747 MAGIC* mgt;
748 PL_tainted = 1;
749 SvMAGIC(sv) = mg->mg_moremagic;
750 SvTAINT(sv);
751 if ((mgt = SvMAGIC(sv))) {
752 mg->mg_moremagic = mgt;
753 SvMAGIC(sv) = mg;
754 }
755 } else
756 SvTAINTED_off(sv);
757 }
748a9306
LW
758 break;
759 }
79072805 760 }
79072805 761 }
3280af22 762 sv_setsv(sv,&PL_sv_undef);
79072805
LW
763 break;
764 case '+':
aaa362c4 765 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 766 paren = rx->lastparen;
a0d0e21e
LW
767 if (paren)
768 goto getparen;
79072805 769 }
3280af22 770 sv_setsv(sv,&PL_sv_undef);
79072805 771 break;
a01268b5
JH
772 case '\016': /* ^N */
773 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
774 paren = rx->lastcloseparen;
775 if (paren)
776 goto getparen;
777 }
778 sv_setsv(sv,&PL_sv_undef);
779 break;
79072805 780 case '`':
aaa362c4 781 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
782 if ((s = rx->subbeg) && rx->startp[0] != -1) {
783 i = rx->startp[0];
13f57bf8 784 goto getrx;
79072805 785 }
79072805 786 }
3280af22 787 sv_setsv(sv,&PL_sv_undef);
79072805
LW
788 break;
789 case '\'':
aaa362c4 790 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
791 if (rx->subbeg && rx->endp[0] != -1) {
792 s = rx->subbeg + rx->endp[0];
793 i = rx->sublen - rx->endp[0];
13f57bf8 794 goto getrx;
79072805 795 }
79072805 796 }
3280af22 797 sv_setsv(sv,&PL_sv_undef);
79072805
LW
798 break;
799 case '.':
800#ifndef lint
3280af22 801 if (GvIO(PL_last_in_gv)) {
357c8808 802 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805
LW
803 }
804#endif
805 break;
806 case '?':
809a5acc 807 {
809a5acc 808 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 809#ifdef COMPLEX_STATUS
6b88bc9c
GS
810 LvTARGOFF(sv) = PL_statusvalue;
811 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 812#endif
809a5acc 813 }
79072805
LW
814 break;
815 case '^':
0daa599b
RGS
816 if (GvIOp(PL_defoutgv))
817 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
818 if (s)
819 sv_setpv(sv,s);
820 else {
3280af22 821 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
822 sv_catpv(sv,"_TOP");
823 }
824 break;
825 case '~':
0daa599b
RGS
826 if (GvIOp(PL_defoutgv))
827 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 828 if (!s)
3280af22 829 s = GvENAME(PL_defoutgv);
79072805
LW
830 sv_setpv(sv,s);
831 break;
832#ifndef lint
833 case '=':
0daa599b
RGS
834 if (GvIOp(PL_defoutgv))
835 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
836 break;
837 case '-':
0daa599b
RGS
838 if (GvIOp(PL_defoutgv))
839 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
840 break;
841 case '%':
0daa599b
RGS
842 if (GvIOp(PL_defoutgv))
843 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805
LW
844 break;
845#endif
846 case ':':
847 break;
848 case '/':
849 break;
850 case '[':
3280af22 851 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
852 break;
853 case '|':
0daa599b
RGS
854 if (GvIOp(PL_defoutgv))
855 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
856 break;
857 case ',':
79072805
LW
858 break;
859 case '\\':
b2ce0fda 860 if (PL_ors_sv)
f28098ff 861 sv_copypv(sv, PL_ors_sv);
79072805
LW
862 break;
863 case '#':
3280af22 864 sv_setpv(sv,PL_ofmt);
79072805
LW
865 break;
866 case '!':
a5f75d66 867#ifdef VMS
65202027 868 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 869 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 870#else
88e89b8a
PP
871 {
872 int saveerrno = errno;
65202027 873 sv_setnv(sv, (NV)errno);
88e89b8a 874#ifdef OS2
ed344e4f
IZ
875 if (errno == errno_isOS2 || errno == errno_isOS2_set)
876 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 877 else
a5f75d66 878#endif
2304df62 879 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
880 errno = saveerrno;
881 }
882#endif
946ec16e 883 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
884 break;
885 case '<':
3280af22 886 sv_setiv(sv, (IV)PL_uid);
79072805
LW
887 break;
888 case '>':
3280af22 889 sv_setiv(sv, (IV)PL_euid);
79072805
LW
890 break;
891 case '(':
3280af22 892 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 893#ifdef HAS_GETGROUPS
785fb66b 894 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
a52cb5f7 895#endif
79072805
LW
896 goto add_groups;
897 case ')':
3280af22 898 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 899#ifdef HAS_GETGROUPS
785fb66b 900 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
a52cb5f7 901#endif
79072805 902 add_groups:
79072805 903#ifdef HAS_GETGROUPS
79072805 904 {
a0d0e21e 905 Groups_t gary[NGROUPS];
79072805 906 i = getgroups(NGROUPS,gary);
46fc3d4c 907 while (--i >= 0)
785fb66b 908 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
79072805
LW
909 }
910#endif
155aba94 911 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 912 break;
cd39f2b6 913#ifndef MACOS_TRADITIONAL
79072805
LW
914 case '0':
915 break;
cd39f2b6 916#endif
79072805 917 }
a0d0e21e 918 return 0;
79072805
LW
919}
920
921int
864dbfa3 922Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
923{
924 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
925
926 if (uf && uf->uf_val)
24f81a43 927 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
928 return 0;
929}
930
931int
864dbfa3 932Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
933{
934 register char *s;
88e89b8a 935 char *ptr;
5aabfad6 936 STRLEN len, klen;
1e422769 937
a0d0e21e 938 s = SvPV(sv,len);
5aabfad6 939 ptr = MgPV(mg,klen);
88e89b8a 940 my_setenv(ptr, s);
1e422769 941
a0d0e21e
LW
942#ifdef DYNAMIC_ENV_FETCH
943 /* We just undefd an environment var. Is a replacement */
944 /* waiting in the wings? */
945 if (!len) {
5aabfad6 946 SV **valp;
6b88bc9c 947 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 948 s = SvPV(*valp, len);
a0d0e21e
LW
949 }
950#endif
1e422769 951
39e571d4 952#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
953 /* And you'll never guess what the dog had */
954 /* in its mouth... */
3280af22 955 if (PL_tainting) {
1e422769
PP
956 MgTAINTEDDIR_off(mg);
957#ifdef VMS
5aabfad6 958 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 959 char pathbuf[256], eltbuf[256], *cp, *elt = s;
c623ac67 960 Stat_t sbuf;
1e422769
PP
961 int i = 0, j = 0;
962
963 do { /* DCL$PATH may be a search list */
964 while (1) { /* as may dev portion of any element */
965 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
966 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
967 cando_by_name(S_IWUSR,0,elt) ) {
968 MgTAINTEDDIR_on(mg);
969 return 0;
970 }
971 }
972 if ((cp = strchr(elt, ':')) != Nullch)
973 *cp = '\0';
974 if (my_trnlnm(elt, eltbuf, j++))
975 elt = eltbuf;
976 else
977 break;
978 }
979 j = 0;
980 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
981 }
982#endif /* VMS */
5aabfad6 983 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 984 char *strend = s + len;
463ee0b2
LW
985
986 while (s < strend) {
96827780 987 char tmpbuf[256];
c623ac67 988 Stat_t st;
5f74f29c 989 I32 i;
96827780 990 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 991 s, strend, ':', &i);
463ee0b2 992 s++;
96827780
MB
993 if (i >= sizeof tmpbuf /* too long -- assume the worst */
994 || *tmpbuf != '/'
c6ed36e1 995 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 996 MgTAINTEDDIR_on(mg);
1e422769
PP
997 return 0;
998 }
463ee0b2 999 }
79072805
LW
1000 }
1001 }
39e571d4 1002#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1003
79072805
LW
1004 return 0;
1005}
1006
1007int
864dbfa3 1008Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1009{
2d8e6c8d
GS
1010 STRLEN n_a;
1011 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83
LW
1012 return 0;
1013}
1014
88e89b8a 1015int
864dbfa3 1016Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a
PP
1017{
1018#if defined(VMS)
cea2e8a9 1019 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1020#else
3280af22 1021 if (PL_localizing) {
fb73857a 1022 HE* entry;
2d8e6c8d 1023 STRLEN n_a;
fb73857a
PP
1024 magic_clear_all_env(sv,mg);
1025 hv_iterinit((HV*)sv);
155aba94 1026 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1027 I32 keylen;
1028 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 1029 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a
PP
1030 }
1031 }
1032#endif
1033 return 0;
1034}
1035
1036int
864dbfa3 1037Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1038{
2f42fcb0 1039#ifndef PERL_MICRO
ed79a026 1040#if defined(VMS) || defined(EPOC)
cea2e8a9 1041 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 1042#else
4efc5df6 1043# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137 1044 PerlEnv_clearenv();
4efc5df6
GS
1045# else
1046# ifdef USE_ENVIRON_ARRAY
1047# if defined(USE_ITHREADS)
1048 /* only the parent thread can clobber the process environment */
1049 if (PL_curinterp == aTHX)
1050# endif
1051 {
1052# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
1053 I32 i;
1054
3280af22 1055 if (environ == PL_origenviron)
f2517201 1056 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
1057 else
1058 for (i = 0; environ[i]; i++)
f2517201 1059 safesysfree(environ[i]);
4efc5df6 1060# endif /* PERL_USE_SAFE_PUTENV */
f2517201 1061
66b1d557 1062 environ[0] = Nullch;
4efc5df6
GS
1063 }
1064# endif /* USE_ENVIRON_ARRAY */
052c2dc6 1065# endif /* PERL_IMPLICIT_SYS || WIN32 */
2f42fcb0
JH
1066#endif /* VMS || EPOC */
1067#endif /* !PERL_MICRO */
3e3baf6d 1068 return 0;
66b1d557
HM
1069}
1070
2e34cc90
CL
1071#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1072static int sig_handlers_initted = 0;
1073#endif
7719e241 1074#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1075static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1076#endif
2e34cc90
CL
1077#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1078static int sig_defaulting[SIG_SIZE];
1079#endif
85b332e2 1080
64ca3a65 1081#ifndef PERL_MICRO
2d4fcd5e
AJ
1082#ifdef HAS_SIGPROCMASK
1083static void
1084restore_sigmask(pTHX_ SV *save_sv)
1085{
1086 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1087 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1088}
1089#endif
66b1d557 1090int
864dbfa3 1091Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1092{
1093 I32 i;
2d8e6c8d 1094 STRLEN n_a;
88e89b8a 1095 /* Are we fetching a signal entry? */
2d8e6c8d 1096 i = whichsig(MgPV(mg,n_a));
e02bfb16 1097 if (i > 0) {
22c35a8c
GS
1098 if(PL_psig_ptr[i])
1099 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1100 else {
85b332e2 1101 Sighandler_t sigstate;
2e34cc90 1102 sigstate = rsignal_state(i);
23ada85b 1103#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
2e34cc90
CL
1104 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1105#endif
1106#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1107 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1108#endif
88e89b8a 1109 /* cache state so we don't fetch it again */
ff68c719 1110 if(sigstate == SIG_IGN)
88e89b8a
PP
1111 sv_setpv(sv,"IGNORE");
1112 else
3280af22 1113 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1114 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1115 SvTEMP_off(sv);
1116 }
1117 }
1118 return 0;
1119}
1120int
864dbfa3 1121Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1122{
2d4fcd5e
AJ
1123 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1124 * refactoring might be in order.
1125 */
1126 register char *s;
2d8e6c8d 1127 STRLEN n_a;
2d4fcd5e
AJ
1128 SV* to_dec;
1129 s = MgPV(mg,n_a);
1130 if (*s == '_') {
1131 SV** svp;
1132 if (strEQ(s,"__DIE__"))
1133 svp = &PL_diehook;
1134 else if (strEQ(s,"__WARN__"))
1135 svp = &PL_warnhook;
1136 else
1137 Perl_croak(aTHX_ "No such hook: %s", s);
1138 if (*svp) {
1139 to_dec = *svp;
1140 *svp = 0;
1141 SvREFCNT_dec(to_dec);
1142 }
1143 }
1144 else {
1145 I32 i;
1146 /* Are we clearing a signal entry? */
1147 i = whichsig(s);
e02bfb16 1148 if (i > 0) {
2d4fcd5e
AJ
1149#ifdef HAS_SIGPROCMASK
1150 sigset_t set, save;
1151 SV* save_sv;
1152 /* Avoid having the signal arrive at a bad time, if possible. */
1153 sigemptyset(&set);
1154 sigaddset(&set,i);
1155 sigprocmask(SIG_BLOCK, &set, &save);
1156 ENTER;
1157 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1158 SAVEFREESV(save_sv);
1159 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1160#endif
1161 PERL_ASYNC_CHECK();
1162#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1163 if (!sig_handlers_initted) Perl_csighandler_init();
1164#endif
1165#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1166 sig_defaulting[i] = 1;
5c1546dc 1167 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1168#else
1169 (void)rsignal(i, SIG_DFL);
1170#endif
1171 if(PL_psig_name[i]) {
1172 SvREFCNT_dec(PL_psig_name[i]);
1173 PL_psig_name[i]=0;
1174 }
1175 if(PL_psig_ptr[i]) {
1176 to_dec=PL_psig_ptr[i];
1177 PL_psig_ptr[i]=0;
1178 LEAVE;
1179 SvREFCNT_dec(to_dec);
1180 }
1181 else
1182 LEAVE;
1183 }
88e89b8a
PP
1184 }
1185 return 0;
1186}
3d37d572 1187
0a8e0eff
NIS
1188void
1189Perl_raise_signal(pTHX_ int sig)
1190{
1191 /* Set a flag to say this signal is pending */
1192 PL_psig_pend[sig]++;
1193 /* And one to say _a_ signal is pending */
1194 PL_sig_pending = 1;
1195}
1196
1197Signal_t
1198Perl_csighandler(int sig)
1199{
1018e26f
NIS
1200#ifdef PERL_GET_SIG_CONTEXT
1201 dTHXa(PERL_GET_SIG_CONTEXT);
1202#else
85b332e2
CL
1203 dTHX;
1204#endif
23ada85b 1205#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1206 (void) rsignal(sig, PL_csighandlerp);
85b332e2
CL
1207 if (sig_ignoring[sig]) return;
1208#endif
2e34cc90 1209#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
7719e241 1210 if (sig_defaulting[sig])
2e34cc90
CL
1211#ifdef KILL_BY_SIGPRC
1212 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1213#else
1214 exit(1);
1215#endif
1216#endif
4ffa73a3
JH
1217 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1218 /* Call the perl level handler now--
1219 * with risk we may be in malloc() etc. */
1220 (*PL_sighandlerp)(sig);
1221 else
1222 Perl_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1223}
1224
2e34cc90
CL
1225#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1226void
1227Perl_csighandler_init(void)
1228{
1229 int sig;
1230 if (sig_handlers_initted) return;
1231
1232 for (sig = 1; sig < SIG_SIZE; sig++) {
1233#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1234 dTHX;
2e34cc90 1235 sig_defaulting[sig] = 1;
5c1546dc 1236 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1237#endif
1238#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1239 sig_ignoring[sig] = 0;
1240#endif
1241 }
1242 sig_handlers_initted = 1;
1243}
1244#endif
1245
0a8e0eff
NIS
1246void
1247Perl_despatch_signals(pTHX)
1248{
1249 int sig;
1250 PL_sig_pending = 0;
1251 for (sig = 1; sig < SIG_SIZE; sig++) {
1252 if (PL_psig_pend[sig]) {
25da4428
JH
1253 PERL_BLOCKSIG_ADD(set, sig);
1254 PL_psig_pend[sig] = 0;
1255 PERL_BLOCKSIG_BLOCK(set);
f5203343 1256 (*PL_sighandlerp)(sig);
25da4428 1257 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1258 }
1259 }
1260}
1261
85e6fe83 1262int
864dbfa3 1263Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1264{
1265 register char *s;
1266 I32 i;
b7953727 1267 SV** svp = 0;
2d4fcd5e
AJ
1268 /* Need to be careful with SvREFCNT_dec(), because that can have side
1269 * effects (due to closures). We must make sure that the new disposition
1270 * is in place before it is called.
1271 */
1272 SV* to_dec = 0;
e72dc28c 1273 STRLEN len;
2d4fcd5e
AJ
1274#ifdef HAS_SIGPROCMASK
1275 sigset_t set, save;
1276 SV* save_sv;
1277#endif
a0d0e21e 1278
e72dc28c 1279 s = MgPV(mg,len);
748a9306
LW
1280 if (*s == '_') {
1281 if (strEQ(s,"__DIE__"))
3280af22 1282 svp = &PL_diehook;
748a9306 1283 else if (strEQ(s,"__WARN__"))
3280af22 1284 svp = &PL_warnhook;
748a9306 1285 else
cea2e8a9 1286 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1287 i = 0;
4633a7c4 1288 if (*svp) {
2d4fcd5e 1289 to_dec = *svp;
4633a7c4
LW
1290 *svp = 0;
1291 }
748a9306
LW
1292 }
1293 else {
1294 i = whichsig(s); /* ...no, a brick */
e02bfb16 1295 if (i < 0) {
e476b1b5 1296 if (ckWARN(WARN_SIGNAL))
9014280d 1297 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1298 return 0;
1299 }
2d4fcd5e
AJ
1300#ifdef HAS_SIGPROCMASK
1301 /* Avoid having the signal arrive at a bad time, if possible. */
1302 sigemptyset(&set);
1303 sigaddset(&set,i);
1304 sigprocmask(SIG_BLOCK, &set, &save);
1305 ENTER;
1306 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1307 SAVEFREESV(save_sv);
1308 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1309#endif
1310 PERL_ASYNC_CHECK();
2e34cc90
CL
1311#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1312 if (!sig_handlers_initted) Perl_csighandler_init();
1313#endif
23ada85b 1314#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1315 sig_ignoring[i] = 0;
1316#endif
2e34cc90 1317#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
2d4fcd5e 1318 sig_defaulting[i] = 0;
2e34cc90 1319#endif
22c35a8c 1320 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1321 to_dec = PL_psig_ptr[i];
22c35a8c 1322 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1323 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1324 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1325 SvREADONLY_on(PL_psig_name[i]);
748a9306 1326 }
a0d0e21e 1327 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1328 if (i) {
5c1546dc 1329 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1330#ifdef HAS_SIGPROCMASK
1331 LEAVE;
1332#endif
1333 }
748a9306
LW
1334 else
1335 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1336 if(to_dec)
1337 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1338 return 0;
1339 }
e72dc28c 1340 s = SvPV_force(sv,len);
748a9306 1341 if (strEQ(s,"IGNORE")) {
85b332e2 1342 if (i) {
23ada85b 1343#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2 1344 sig_ignoring[i] = 1;
5c1546dc 1345 (void)rsignal(i, PL_csighandlerp);
85b332e2 1346#else
ff68c719 1347 (void)rsignal(i, SIG_IGN);
85b332e2 1348#endif
2d4fcd5e 1349 }
748a9306
LW
1350 }
1351 else if (strEQ(s,"DEFAULT") || !*s) {
1352 if (i)
2e34cc90
CL
1353#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1354 {
1355 sig_defaulting[i] = 1;
5c1546dc 1356 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1357 }
1358#else
ff68c719 1359 (void)rsignal(i, SIG_DFL);
2e34cc90 1360#endif
748a9306 1361 }
79072805 1362 else {
5aabfad6
PP
1363 /*
1364 * We should warn if HINT_STRICT_REFS, but without
1365 * access to a known hint bit in a known OP, we can't
1366 * tell whether HINT_STRICT_REFS is in force or not.
1367 */
46fc3d4c 1368 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1369 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1370 if (i)
5c1546dc 1371 (void)rsignal(i, PL_csighandlerp);
748a9306
LW
1372 else
1373 *svp = SvREFCNT_inc(sv);
79072805 1374 }
2d4fcd5e
AJ
1375#ifdef HAS_SIGPROCMASK
1376 if(i)
1377 LEAVE;
1378#endif
1379 if(to_dec)
1380 SvREFCNT_dec(to_dec);
79072805
LW
1381 return 0;
1382}
64ca3a65 1383#endif /* !PERL_MICRO */
79072805
LW
1384
1385int
864dbfa3 1386Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1387{
3280af22 1388 PL_sub_generation++;
463ee0b2
LW
1389 return 0;
1390}
1391
1392int
864dbfa3 1393Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1394{
a0d0e21e 1395 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1396 PL_amagic_generation++;
463ee0b2 1397
a0d0e21e
LW
1398 return 0;
1399}
463ee0b2 1400
946ec16e 1401int
864dbfa3 1402Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1403{
1404 HV *hv = (HV*)LvTARG(sv);
6ff81951 1405 I32 i = 0;
7719e241 1406
6ff81951 1407 if (hv) {
497b47a8
JH
1408 (void) hv_iterinit(hv);
1409 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1410 i = HvKEYS(hv);
1411 else {
1412 while (hv_iternext(hv))
1413 i++;
1414 }
6ff81951
GS
1415 }
1416
1417 sv_setiv(sv, (IV)i);
1418 return 0;
1419}
1420
1421int
864dbfa3 1422Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1423{
1424 if (LvTARG(sv)) {
1425 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1426 }
1427 return 0;
ac27b0f5 1428}
946ec16e 1429
e336de0d 1430/* caller is responsible for stack switching/cleanup */
565764a8 1431STATIC int
cea2e8a9 1432S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1433{
1434 dSP;
463ee0b2 1435
924508f0
GS
1436 PUSHMARK(SP);
1437 EXTEND(SP, n);
33c27489 1438 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1439 if (n > 1) {
93965878 1440 if (mg->mg_ptr) {
565764a8 1441 if (mg->mg_len >= 0)
79cb57f6 1442 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1443 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1444 PUSHs((SV*)mg->mg_ptr);
1445 }
14befaf4 1446 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1447 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1448 }
1449 }
1450 if (n > 2) {
1451 PUSHs(val);
88e89b8a 1452 }
463ee0b2
LW
1453 PUTBACK;
1454
864dbfa3 1455 return call_method(meth, flags);
946ec16e
PP
1456}
1457
76e3520e 1458STATIC int
cea2e8a9 1459S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1460{
1461 dSP;
463ee0b2 1462
a0d0e21e
LW
1463 ENTER;
1464 SAVETMPS;
e788e7d3 1465 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1466
33c27489 1467 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1468 sv_setsv(sv, *PL_stack_sp--);
93965878 1469 }
463ee0b2 1470
d3acc0f7 1471 POPSTACK;
a0d0e21e
LW
1472 FREETMPS;
1473 LEAVE;
1474 return 0;
1475}
463ee0b2 1476
a0d0e21e 1477int
864dbfa3 1478Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1479{
a0d0e21e
LW
1480 if (mg->mg_ptr)
1481 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1482 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1483 return 0;
1484}
1485
1486int
864dbfa3 1487Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1488{
1489 dSP;
a60c0954 1490 ENTER;
e788e7d3 1491 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1492 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1493 POPSTACK;
a60c0954 1494 LEAVE;
463ee0b2
LW
1495 return 0;
1496}
1497
1498int
864dbfa3 1499Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1500{
a0d0e21e
LW
1501 return magic_methpack(sv,mg,"DELETE");
1502}
463ee0b2 1503
93965878
NIS
1504
1505U32
864dbfa3 1506Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1507{
e336de0d 1508 dSP;
93965878
NIS
1509 U32 retval = 0;
1510
1511 ENTER;
1512 SAVETMPS;
e788e7d3 1513 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1514 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1515 sv = *PL_stack_sp--;
a60c0954 1516 retval = (U32) SvIV(sv)-1;
93965878 1517 }
d3acc0f7 1518 POPSTACK;
93965878
NIS
1519 FREETMPS;
1520 LEAVE;
1521 return retval;
1522}
1523
cea2e8a9
GS
1524int
1525Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1526{
1527 dSP;
463ee0b2 1528
e336de0d 1529 ENTER;
e788e7d3 1530 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1531 PUSHMARK(SP);
33c27489 1532 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1533 PUTBACK;
864dbfa3 1534 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1535 POPSTACK;
a60c0954 1536 LEAVE;
463ee0b2
LW
1537 return 0;
1538}
1539
1540int
864dbfa3 1541Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1542{
463ee0b2 1543 dSP;
dff6d3cd 1544 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1545
1546 ENTER;
a0d0e21e 1547 SAVETMPS;
e788e7d3 1548 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1549 PUSHMARK(SP);
1550 EXTEND(SP, 2);
33c27489 1551 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1552 if (SvOK(key))
1553 PUSHs(key);
1554 PUTBACK;
1555
864dbfa3 1556 if (call_method(meth, G_SCALAR))
3280af22 1557 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1558
d3acc0f7 1559 POPSTACK;
a0d0e21e
LW
1560 FREETMPS;
1561 LEAVE;
79072805
LW
1562 return 0;
1563}
1564
1565int
864dbfa3 1566Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1567{
1568 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1569}
a0d0e21e
LW
1570
1571int
864dbfa3 1572Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1573{
1574 OP *o;
1575 I32 i;
1576 GV* gv;
1577 SV** svp;
2d8e6c8d 1578 STRLEN n_a;
79072805 1579
3280af22 1580 gv = PL_DBline;
79072805 1581 i = SvTRUE(sv);
188ea221 1582 svp = av_fetch(GvAV(gv),
2d8e6c8d 1583 atoi(MgPV(mg,n_a)), FALSE);
5df8de69
DM
1584 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1585 /* set or clear breakpoint in the relevant control op */
1586 if (i)
1587 o->op_flags |= OPf_SPECIAL;
1588 else
1589 o->op_flags &= ~OPf_SPECIAL;
1590 }
79072805
LW
1591 return 0;
1592}
1593
1594int
864dbfa3 1595Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1596{
3280af22 1597 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1598 return 0;
1599}
1600
1601int
864dbfa3 1602Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1603{
3280af22 1604 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1605 return 0;
1606}
1607
1608int
864dbfa3 1609Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1610{
1611 SV* lsv = LvTARG(sv);
ac27b0f5 1612
a0d0e21e 1613 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1614 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1615 if (mg && mg->mg_len >= 0) {
a0ed51b3 1616 I32 i = mg->mg_len;
7e2040f0 1617 if (DO_UTF8(lsv))
a0ed51b3
LW
1618 sv_pos_b2u(lsv, &i);
1619 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1620 return 0;
1621 }
1622 }
1623 (void)SvOK_off(sv);
1624 return 0;
1625}
1626
1627int
864dbfa3 1628Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1629{
1630 SV* lsv = LvTARG(sv);
1631 SSize_t pos;
1632 STRLEN len;
c00206c8 1633 STRLEN ulen = 0;
a0d0e21e
LW
1634
1635 mg = 0;
ac27b0f5 1636
a0d0e21e 1637 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1638 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1639 if (!mg) {
1640 if (!SvOK(sv))
1641 return 0;
14befaf4
DM
1642 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1643 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1644 }
1645 else if (!SvOK(sv)) {
565764a8 1646 mg->mg_len = -1;
a0d0e21e
LW
1647 return 0;
1648 }
1649 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1650
c485e607 1651 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1652
7e2040f0 1653 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1654 ulen = sv_len_utf8(lsv);
1655 if (ulen)
1656 len = ulen;
a0ed51b3
LW
1657 }
1658
a0d0e21e
LW
1659 if (pos < 0) {
1660 pos += len;
1661 if (pos < 0)
1662 pos = 0;
1663 }
eb160463 1664 else if (pos > (SSize_t)len)
a0d0e21e 1665 pos = len;
a0ed51b3
LW
1666
1667 if (ulen) {
1668 I32 p = pos;
1669 sv_pos_u2b(lsv, &p, 0);
1670 pos = p;
1671 }
727405f8 1672
565764a8 1673 mg->mg_len = pos;
71be2cbc 1674 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1675
79072805
LW
1676 return 0;
1677}
1678
1679int
864dbfa3 1680Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1681{
8646b087
PP
1682 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1683 SvFAKE_off(sv);
946ec16e 1684 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1685 SvFAKE_on(sv);
1686 }
1687 else
946ec16e 1688 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1689 return 0;
1690}
1691
1692int
864dbfa3 1693Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1694{
1695 register char *s;
1696 GV* gv;
2d8e6c8d 1697 STRLEN n_a;
79072805
LW
1698
1699 if (!SvOK(sv))
1700 return 0;
2d8e6c8d 1701 s = SvPV(sv, n_a);
79072805
LW
1702 if (*s == '*' && s[1])
1703 s++;
85e6fe83 1704 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1705 if (sv == (SV*)gv)
1706 return 0;
1707 if (GvGP(sv))
88e89b8a 1708 gp_free((GV*)sv);
79072805 1709 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1710 return 0;
1711}
1712
1713int
864dbfa3 1714Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1715{
1716 STRLEN len;
1717 SV *lsv = LvTARG(sv);
1718 char *tmps = SvPV(lsv,len);
1719 I32 offs = LvTARGOFF(sv);
1720 I32 rem = LvTARGLEN(sv);
1721
9aa983d2
JH
1722 if (SvUTF8(lsv))
1723 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1724 if (offs > (I32)len)
6ff81951 1725 offs = len;
eb160463 1726 if (rem + offs > (I32)len)
6ff81951
GS
1727 rem = len - offs;
1728 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1729 if (SvUTF8(lsv))
2ef4b674 1730 SvUTF8_on(sv);
6ff81951
GS
1731 return 0;
1732}
1733
1734int
864dbfa3 1735Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1736{
9aa983d2
JH
1737 STRLEN len;
1738 char *tmps = SvPV(sv, len);
1739 SV *lsv = LvTARG(sv);
1740 I32 lvoff = LvTARGOFF(sv);
1741 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1742
1aa99e6b 1743 if (DO_UTF8(sv)) {
9aa983d2
JH
1744 sv_utf8_upgrade(lsv);
1745 sv_pos_u2b(lsv, &lvoff, &lvlen);
1746 sv_insert(lsv, lvoff, lvlen, tmps, len);
1747 SvUTF8_on(lsv);
1748 }
9bf12eaf 1749 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1750 sv_pos_u2b(lsv, &lvoff, &lvlen);
e95af362 1751 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1752 sv_insert(lsv, lvoff, lvlen, tmps, len);
1753 Safefree(tmps);
1aa99e6b
IH
1754 }
1755 else
9aa983d2 1756 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1757
79072805
LW
1758 return 0;
1759}
1760
1761int
864dbfa3 1762Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1763{
565764a8 1764 TAINT_IF((mg->mg_len & 1) ||
155aba94 1765 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1766 return 0;
1767}
1768
1769int
864dbfa3 1770Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1771{
3280af22
NIS
1772 if (PL_localizing) {
1773 if (PL_localizing == 1)
565764a8 1774 mg->mg_len <<= 1;
748a9306 1775 else
565764a8 1776 mg->mg_len >>= 1;
a0d0e21e 1777 }
3280af22 1778 else if (PL_tainted)
565764a8 1779 mg->mg_len |= 1;
748a9306 1780 else
565764a8 1781 mg->mg_len &= ~1;
463ee0b2
LW
1782 return 0;
1783}
1784
1785int
864dbfa3 1786Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1787{
1788 SV *lsv = LvTARG(sv);
6ff81951
GS
1789
1790 if (!lsv) {
155aba94 1791 (void)SvOK_off(sv);
6ff81951
GS
1792 return 0;
1793 }
6ff81951 1794
81e118e0 1795 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1796 return 0;
1797}
1798
1799int
864dbfa3 1800Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1801{
1802 do_vecset(sv); /* XXX slurp this routine */
1803 return 0;
1804}
1805
1806int
864dbfa3 1807Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1808{
71be2cbc 1809 SV *targ = Nullsv;
5f05dabc 1810 if (LvTARGLEN(sv)) {
68dc0745 1811 if (mg->mg_obj) {
74e13ce4 1812 SV *ahv = LvTARG(sv);
6d822dc4
MS
1813 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1814 if (he)
1815 targ = HeVAL(he);
68dc0745
PP
1816 }
1817 else {
3c78fafa 1818 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1819 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1820 targ = AvARRAY(av)[LvTARGOFF(sv)];
1821 }
3280af22 1822 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1823 /* somebody else defined it for us */
1824 SvREFCNT_dec(LvTARG(sv));
1825 LvTARG(sv) = SvREFCNT_inc(targ);
1826 LvTARGLEN(sv) = 0;
1827 SvREFCNT_dec(mg->mg_obj);
1828 mg->mg_obj = Nullsv;
1829 mg->mg_flags &= ~MGf_REFCOUNTED;
1830 }
5f05dabc 1831 }
71be2cbc
PP
1832 else
1833 targ = LvTARG(sv);
3280af22 1834 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1835 return 0;
1836}
1837
1838int
864dbfa3 1839Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1840{
1841 if (LvTARGLEN(sv))
68dc0745
PP
1842 vivify_defelem(sv);
1843 if (LvTARG(sv)) {
5f05dabc 1844 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1845 SvSETMAGIC(LvTARG(sv));
1846 }
5f05dabc
PP
1847 return 0;
1848}
1849
71be2cbc 1850void
864dbfa3 1851Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1852{
74e13ce4
GS
1853 MAGIC *mg;
1854 SV *value = Nullsv;
71be2cbc 1855
14befaf4 1856 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1857 return;
68dc0745 1858 if (mg->mg_obj) {
74e13ce4 1859 SV *ahv = LvTARG(sv);
2d8e6c8d 1860 STRLEN n_a;
6d822dc4
MS
1861 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1862 if (he)
1863 value = HeVAL(he);
3280af22 1864 if (!value || value == &PL_sv_undef)
cea2e8a9 1865 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1866 }
68dc0745
PP
1867 else {
1868 AV* av = (AV*)LvTARG(sv);
5aabfad6 1869 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1870 LvTARG(sv) = Nullsv; /* array can't be extended */
1871 else {
1872 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1873 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1874 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1875 }
1876 }
3e3baf6d 1877 (void)SvREFCNT_inc(value);
68dc0745
PP
1878 SvREFCNT_dec(LvTARG(sv));
1879 LvTARG(sv) = value;
71be2cbc 1880 LvTARGLEN(sv) = 0;
68dc0745
PP
1881 SvREFCNT_dec(mg->mg_obj);
1882 mg->mg_obj = Nullsv;
1883 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1884}
1885
1886int
864dbfa3 1887Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1888{
1889 AV *av = (AV*)mg->mg_obj;
1890 SV **svp = AvARRAY(av);
1891 I32 i = AvFILLp(av);
1892 while (i >= 0) {
1893 if (svp[i] && svp[i] != &PL_sv_undef) {
1894 if (!SvWEAKREF(svp[i]))
cea2e8a9 1895 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1896 /* XXX Should we check that it hasn't changed? */
1897 SvRV(svp[i]) = 0;
155aba94 1898 (void)SvOK_off(svp[i]);
810b8aa5
GS
1899 SvWEAKREF_off(svp[i]);
1900 svp[i] = &PL_sv_undef;
1901 }
1902 i--;
1903 }
d99b02a1 1904 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5
GS
1905 return 0;
1906}
1907
1908int
864dbfa3 1909Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1910{
565764a8 1911 mg->mg_len = -1;
c6496cc7 1912 SvSCREAM_off(sv);
93a17b20
LW
1913 return 0;
1914}
1915
1916int
864dbfa3 1917Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1918{
14befaf4 1919 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1920 SvVALID_off(sv);
1921 return 0;
1922}
1923
1924int
864dbfa3 1925Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1926{
14befaf4 1927 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1928 SvCOMPILED_off(sv);
1929 return 0;
1930}
1931
1932int
864dbfa3 1933Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1934{
1935 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1936
1937 if (uf && uf->uf_set)
24f81a43 1938 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
1939 return 0;
1940}
1941
c277df42 1942int
faf82a0b
AE
1943Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1944{
1945 sv_unmagic(sv, PERL_MAGIC_qr);
1946 return 0;
1947}
1948
1949int
864dbfa3 1950Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1951{
1952 regexp *re = (regexp *)mg->mg_obj;
1953 ReREFCNT_dec(re);
1954 return 0;
1955}
1956
7a4c00b4 1957#ifdef USE_LOCALE_COLLATE
79072805 1958int
864dbfa3 1959Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1960{
1961 /*
838b5b74 1962 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1963 * and vanished with a faint plop.
1964 */
7a4c00b4
PP
1965 if (mg->mg_ptr) {
1966 Safefree(mg->mg_ptr);
1967 mg->mg_ptr = NULL;
565764a8 1968 mg->mg_len = -1;
7a4c00b4 1969 }
bbce6d69
PP
1970 return 0;
1971}
7a4c00b4 1972#endif /* USE_LOCALE_COLLATE */
bbce6d69 1973
7e8c5dac
HS
1974/* Just clear the UTF-8 cache data. */
1975int
1976Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
1977{
1978 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
1979 mg->mg_ptr = 0;
1980 mg->mg_len = -1; /* The mg_len holds the len cache. */
1981 return 0;
1982}
1983
bbce6d69 1984int
864dbfa3 1985Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1986{
1987 register char *s;
1988 I32 i;
8990e307 1989 STRLEN len;
79072805 1990 switch (*mg->mg_ptr) {
748a9306 1991 case '\001': /* ^A */
3280af22 1992 sv_setsv(PL_bodytarget, sv);
748a9306 1993 break;
49460fe6 1994 case '\003': /* ^C */
eb160463 1995 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
1996 break;
1997
79072805 1998 case '\004': /* ^D */
b4ab917c
DM
1999#ifdef DEBUGGING
2000 s = SvPV_nolen(sv);
2001 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
79072805 2002 DEBUG_x(dump_all());
b4ab917c
DM
2003#else
2004 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2005#endif
79072805 2006 break;
28f23441 2007 case '\005': /* ^E */
d0063567 2008 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2009#ifdef MACOS_TRADITIONAL
d0063567 2010 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2011#else
cd39f2b6 2012# ifdef VMS
d0063567 2013 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2014# else
cd39f2b6 2015# ifdef WIN32
d0063567 2016 SetLastError( SvIV(sv) );
cd39f2b6 2017# else
9fed8b87 2018# ifdef OS2
d0063567 2019 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2020# else
d0063567
DK
2021 /* will anyone ever use this? */
2022 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2023# endif
048c1ddf
IZ
2024# endif
2025# endif
22fae026 2026#endif
d0063567
DK
2027 }
2028 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2029 if (PL_encoding)
2030 SvREFCNT_dec(PL_encoding);
2031 if (SvOK(sv) || SvGMAGICAL(sv)) {
2032 PL_encoding = newSVsv(sv);
2033 }
2034 else {
2035 PL_encoding = Nullsv;
2036 }
2037 }
2038 break;
79072805 2039 case '\006': /* ^F */
3280af22 2040 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2041 break;
a0d0e21e 2042 case '\010': /* ^H */
3280af22 2043 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2044 break;
9d116dd7 2045 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2046 if (PL_inplace)
2047 Safefree(PL_inplace);
79072805 2048 if (SvOK(sv))
2d8e6c8d 2049 PL_inplace = savepv(SvPV(sv,len));
79072805 2050 else
3280af22 2051 PL_inplace = Nullch;
79072805 2052 break;
28f23441 2053 case '\017': /* ^O */
ac27b0f5
NIS
2054 if (*(mg->mg_ptr+1) == '\0') {
2055 if (PL_osname)
2056 Safefree(PL_osname);
2057 if (SvOK(sv))
2058 PL_osname = savepv(SvPV(sv,len));
2059 else
2060 PL_osname = Nullch;
2061 }
2062 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2063 if (!PL_compiling.cop_io)
2064 PL_compiling.cop_io = newSVsv(sv);
2065 else
2066 sv_setsv(PL_compiling.cop_io,sv);
2067 }
28f23441 2068 break;
79072805 2069 case '\020': /* ^P */
3280af22 2070 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
2071 if (PL_perldb && !PL_DBsingle)
2072 init_debugger();
79072805
LW
2073 break;
2074 case '\024': /* ^T */
88e89b8a 2075#ifdef BIG_TIME
6b88bc9c 2076 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2077#else
3280af22 2078 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2079#endif
79072805 2080 break;
fde18df1 2081 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2082 if (*(mg->mg_ptr+1) == '\0') {
2083 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2084 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2085 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2086 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2087 }
599cee73 2088 }
0a378802 2089 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2090 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2091 if (!SvPOK(sv) && PL_localizing) {
2092 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2093 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2094 break;
2095 }
f4fc7782 2096 {
b5477537 2097 STRLEN len, i;
d3a7d8c7 2098 int accumulate = 0 ;
f4fc7782 2099 int any_fatals = 0 ;
d3a7d8c7 2100 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
2101 for (i = 0 ; i < len ; ++i) {
2102 accumulate |= ptr[i] ;
2103 any_fatals |= (ptr[i] & 0xAA) ;
2104 }
d3a7d8c7
GS
2105 if (!accumulate)
2106 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2107 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2108 PL_compiling.cop_warnings = pWARN_ALL;
2109 PL_dowarn |= G_WARN_ONCE ;
727405f8 2110 }
d3a7d8c7
GS
2111 else {
2112 if (specialWARN(PL_compiling.cop_warnings))
2113 PL_compiling.cop_warnings = newSVsv(sv) ;
2114 else
2115 sv_setsv(PL_compiling.cop_warnings, sv);
2116 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2117 PL_dowarn |= G_WARN_ONCE ;
2118 }
f4fc7782 2119
d3a7d8c7 2120 }
4438c4b7 2121 }
971a9dd3 2122 }
79072805
LW
2123 break;
2124 case '.':
3280af22
NIS
2125 if (PL_localizing) {
2126 if (PL_localizing == 1)
7766f137 2127 SAVESPTR(PL_last_in_gv);
748a9306 2128 }
3280af22 2129 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2130 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2131 break;
2132 case '^':
3280af22 2133 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2134 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2135 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2136 break;
2137 case '~':
3280af22 2138 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2139 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2140 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2141 break;
2142 case '=':
632db599 2143 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2144 break;
2145 case '-':
632db599 2146 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2147 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2148 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2149 break;
2150 case '%':
632db599 2151 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2152 break;
2153 case '|':
4b65379b 2154 {
3280af22 2155 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2156 if(!io)
2157 break;
4b65379b
CS
2158 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2159 IoFLAGS(io) &= ~IOf_FLUSH;
2160 else {
2161 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2162 PerlIO *ofp = IoOFP(io);
2163 if (ofp)
2164 (void)PerlIO_flush(ofp);
2165 IoFLAGS(io) |= IOf_FLUSH;
2166 }
2167 }
79072805
LW
2168 }
2169 break;
79072805 2170 case '/':
3280af22 2171 SvREFCNT_dec(PL_rs);
8bfdd7d9 2172 PL_rs = newSVsv(sv);
79072805
LW
2173 break;
2174 case '\\':
7889fe52
NIS
2175 if (PL_ors_sv)
2176 SvREFCNT_dec(PL_ors_sv);
009c130f 2177 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2178 PL_ors_sv = newSVsv(sv);
009c130f 2179 }
e3c19b7b 2180 else {
7889fe52 2181 PL_ors_sv = Nullsv;
e3c19b7b 2182 }
79072805
LW
2183 break;
2184 case ',':
7889fe52
NIS
2185 if (PL_ofs_sv)
2186 SvREFCNT_dec(PL_ofs_sv);
2187 if (SvOK(sv) || SvGMAGICAL(sv)) {
2188 PL_ofs_sv = newSVsv(sv);
2189 }
2190 else {
2191 PL_ofs_sv = Nullsv;
2192 }
79072805
LW
2193 break;
2194 case '#':
3280af22
NIS
2195 if (PL_ofmt)
2196 Safefree(PL_ofmt);
2d8e6c8d 2197 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
2198 break;
2199 case '[':
3280af22 2200 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2201 break;
2202 case '?':
ff0cee69 2203#ifdef COMPLEX_STATUS
6b88bc9c
GS
2204 if (PL_localizing == 2) {
2205 PL_statusvalue = LvTARGOFF(sv);
2206 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2207 }
2208 else
2209#endif
2210#ifdef VMSISH_STATUS
2211 if (VMSISH_STATUS)
2212 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2213 else
2214#endif
2215 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2216 break;
2217 case '!':
93189314
JH
2218 {
2219#ifdef VMS
2220# define PERL_VMS_BANG vaxc$errno
2221#else
2222# define PERL_VMS_BANG 0
2223#endif
91487cfc 2224 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2225 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2226 }
79072805
LW
2227 break;
2228 case '<':
3280af22
NIS
2229 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2230 if (PL_delaymagic) {
2231 PL_delaymagic |= DM_RUID;
79072805
LW
2232 break; /* don't do magic till later */
2233 }
2234#ifdef HAS_SETRUID
b28d0864 2235 (void)setruid((Uid_t)PL_uid);
79072805
LW
2236#else
2237#ifdef HAS_SETREUID
3280af22 2238 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2239#else
85e6fe83 2240#ifdef HAS_SETRESUID
b28d0864 2241 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2242#else
75870ed3 2243 if (PL_uid == PL_euid) { /* special case $< = $> */
2244#ifdef PERL_DARWIN
2245 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2246 if (PL_uid != 0 && PerlProc_getuid() == 0)
2247 (void)PerlProc_setuid(0);
2248#endif
b28d0864 2249 (void)PerlProc_setuid(PL_uid);
75870ed3 2250 } else {
d8eceb89 2251 PL_uid = PerlProc_getuid();
cea2e8a9 2252 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2253 }
79072805
LW
2254#endif
2255#endif
85e6fe83 2256#endif
d8eceb89 2257 PL_uid = PerlProc_getuid();
3280af22 2258 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2259 break;
2260 case '>':
3280af22
NIS
2261 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2262 if (PL_delaymagic) {
2263 PL_delaymagic |= DM_EUID;
79072805
LW
2264 break; /* don't do magic till later */
2265 }
2266#ifdef HAS_SETEUID
3280af22 2267 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2268#else
2269#ifdef HAS_SETREUID
b28d0864 2270 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2271#else
2272#ifdef HAS_SETRESUID
6b88bc9c 2273 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2274#else
b28d0864
NIS
2275 if (PL_euid == PL_uid) /* special case $> = $< */
2276 PerlProc_setuid(PL_euid);
a0d0e21e 2277 else {
e8ee3774 2278 PL_euid = PerlProc_geteuid();
cea2e8a9 2279 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2280 }
79072805
LW
2281#endif
2282#endif
85e6fe83 2283#endif
d8eceb89 2284 PL_euid = PerlProc_geteuid();
3280af22 2285 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2286 break;
2287 case '(':
3280af22
NIS
2288 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2289 if (PL_delaymagic) {
2290 PL_delaymagic |= DM_RGID;
79072805
LW
2291 break; /* don't do magic till later */
2292 }
2293#ifdef HAS_SETRGID
b28d0864 2294 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2295#else
2296#ifdef HAS_SETREGID
3280af22 2297 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2298#else
2299#ifdef HAS_SETRESGID
b28d0864 2300 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2301#else
b28d0864
NIS
2302 if (PL_gid == PL_egid) /* special case $( = $) */
2303 (void)PerlProc_setgid(PL_gid);
748a9306 2304 else {
d8eceb89 2305 PL_gid = PerlProc_getgid();
cea2e8a9 2306 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2307 }
79072805
LW
2308#endif
2309#endif
85e6fe83 2310#endif
d8eceb89 2311 PL_gid = PerlProc_getgid();
3280af22 2312 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2313 break;
2314 case ')':
5cd24f17
PP
2315#ifdef HAS_SETGROUPS
2316 {
2d8e6c8d 2317 char *p = SvPV(sv, len);
5cd24f17
PP
2318 Groups_t gary[NGROUPS];
2319
5cd24f17
PP
2320 while (isSPACE(*p))
2321 ++p;
2d4389e4 2322 PL_egid = Atol(p);
5cd24f17
PP
2323 for (i = 0; i < NGROUPS; ++i) {
2324 while (*p && !isSPACE(*p))
2325 ++p;
2326 while (isSPACE(*p))
2327 ++p;
2328 if (!*p)
2329 break;
2d4389e4 2330 gary[i] = Atol(p);
5cd24f17 2331 }
8cc95fdb
PP
2332 if (i)
2333 (void)setgroups(i, gary);
5cd24f17
PP
2334 }
2335#else /* HAS_SETGROUPS */
b28d0864 2336 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2337#endif /* HAS_SETGROUPS */
3280af22
NIS
2338 if (PL_delaymagic) {
2339 PL_delaymagic |= DM_EGID;
79072805
LW
2340 break; /* don't do magic till later */
2341 }
2342#ifdef HAS_SETEGID
3280af22 2343 (void)setegid((Gid_t)PL_egid);
79072805
LW
2344#else
2345#ifdef HAS_SETREGID
b28d0864 2346 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2347#else
2348#ifdef HAS_SETRESGID
b28d0864 2349 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2350#else
b28d0864
NIS
2351 if (PL_egid == PL_gid) /* special case $) = $( */
2352 (void)PerlProc_setgid(PL_egid);
748a9306 2353 else {
d8eceb89 2354 PL_egid = PerlProc_getegid();
cea2e8a9 2355 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2356 }
79072805
LW
2357#endif
2358#endif
85e6fe83 2359#endif
d8eceb89 2360 PL_egid = PerlProc_getegid();
3280af22 2361 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2362 break;
2363 case ':':
2d8e6c8d 2364 PL_chopset = SvPV_force(sv,len);
79072805 2365 break;
cd39f2b6 2366#ifndef MACOS_TRADITIONAL
79072805 2367 case '0':
e2975953 2368 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2369#ifdef HAS_SETPROCTITLE
2370 /* The BSDs don't show the argv[] in ps(1) output, they
2371 * show a string from the process struct and provide
2372 * the setproctitle() routine to manipulate that. */
2373 {
2374 s = SvPV(sv, len);
98b76f99 2375# if __FreeBSD_version > 410001
9aad2c0e 2376 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2377 * but not the "(perl) suffix from the ps(1)
2378 * output, because that's what ps(1) shows if the
2379 * argv[] is modified. */
6f2ad931 2380 setproctitle("-%s", s);
9aad2c0e 2381# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2382 /* This doesn't really work if you assume that
2383 * $0 = 'foobar'; will wipe out 'perl' from the $0
2384 * because in ps(1) output the result will be like
2385 * sprintf("perl: %s (perl)", s)
2386 * I guess this is a security feature:
2387 * one (a user process) cannot get rid of the original name.
2388 * --jhi */
2389 setproctitle("%s", s);
2390# endif
2391 }
2392#endif
17aa7f3d
JH
2393#if defined(__hpux) && defined(PSTAT_SETCMD)
2394 {
2395 union pstun un;
2396 s = SvPV(sv, len);
2397 un.pst_command = s;
2398 pstat(PSTAT_SETCMD, un, len, 0, 0);
2399 }
2400#endif
3cb9023d 2401 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2402 s = SvPV_force(sv,len);
6f698202
AMS
2403 if (len >= (STRLEN)PL_origalen-1) {
2404 /* Longer than original, will be truncated. We assume that
2405 * PL_origalen bytes are available. */
2406 Copy(s, PL_origargv[0], PL_origalen-1, char);
2407 PL_origargv[0][PL_origalen-1] = 0;
79072805
LW
2408 }
2409 else {
54bfe034
JH
2410 /* Shorter than original, will be padded. */
2411 Copy(s, PL_origargv[0], len, char);
2412 PL_origargv[0][len] = 0;
2413 memset(PL_origargv[0] + len + 1,
2414 /* Is the space counterintuitive? Yes.
2415 * (You were expecting \0?)
3cb9023d 2416 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2417 * --jhi */
2418 (int)' ',
2419 PL_origalen - len - 1);
3280af22 2420 for (i = 1; i < PL_origargc; i++)
54bfe034 2421 PL_origargv[i] = 0;
79072805 2422 }
e2975953 2423 UNLOCK_DOLLARZERO_MUTEX;
79072805 2424 break;
cd39f2b6 2425#endif
79072805
LW
2426 }
2427 return 0;
2428}
2429
2430I32
864dbfa3 2431Perl_whichsig(pTHX_ char *sig)
79072805
LW
2432{
2433 register char **sigv;
2434
e02bfb16 2435 for (sigv = PL_sig_name; *sigv; sigv++)
79072805 2436 if (strEQ(sig,*sigv))
22c35a8c 2437 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2438#ifdef SIGCLD
2439 if (strEQ(sig,"CHLD"))
2440 return SIGCLD;
2441#endif
2442#ifdef SIGCHLD
2443 if (strEQ(sig,"CLD"))
2444 return SIGCHLD;
2445#endif
7f1236c0 2446 return -1;
79072805
LW
2447}
2448
df3728a2 2449#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2450static SV* sig_sv;
df3728a2 2451#endif
84902520 2452
ecfc5424 2453Signal_t
cea2e8a9 2454Perl_sighandler(int sig)
79072805 2455{
1018e26f
NIS
2456#ifdef PERL_GET_SIG_CONTEXT
2457 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2458#else
cea2e8a9 2459 dTHX;
71d280e3 2460#endif
79072805 2461 dSP;
00d579c5 2462 GV *gv = Nullgv;
a0d0e21e 2463 HV *st;
b7953727 2464 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2465 CV *cv = Nullcv;
533c011a 2466 OP *myop = PL_op;
84902520 2467 U32 flags = 0;
3280af22 2468 XPV *tXpv = PL_Xpv;
71d280e3 2469
3280af22 2470 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2471 flags |= 1;
3280af22 2472 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2473 flags |= 4;
3280af22 2474 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2475 flags |= 8;
3280af22 2476 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2477 flags |= 16;
2478
727405f8 2479 if (!PL_psig_ptr[sig]) {
99ef548b 2480 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2481 PL_sig_name[sig]);
2482 exit(sig);
2483 }
ff0cee69 2484
84902520
TB
2485 /* Max number of items pushed there is 3*n or 4. We cannot fix
2486 infinity, so we fix 4 (in fact 5): */
2487 if (flags & 1) {
3280af22 2488 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2489 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2490 }
ac27b0f5 2491 if (flags & 4)
3280af22 2492 PL_markstack_ptr++; /* Protect mark. */
84902520 2493 if (flags & 8) {
3280af22
NIS
2494 PL_retstack_ix++;
2495 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2496 }
2497 if (flags & 16)
3280af22 2498 PL_scopestack_ix += 1;
84902520 2499 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2500 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2501 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2502 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2503
a0d0e21e 2504 if (!cv || !CvROOT(cv)) {
599cee73 2505 if (ckWARN(WARN_SIGNAL))
9014280d 2506 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2507 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2508 : ((cv && CvGV(cv))
2509 ? GvENAME(CvGV(cv))
2510 : "__ANON__")));
2511 goto cleanup;
79072805
LW
2512 }
2513
22c35a8c
GS
2514 if(PL_psig_name[sig]) {
2515 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2516 flags |= 64;
df3728a2 2517#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2518 sig_sv = sv;
df3728a2 2519#endif
84902520 2520 } else {
ff0cee69 2521 sv = sv_newmortal();
22c35a8c 2522 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2523 }
e336de0d 2524
e788e7d3 2525 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2526 PUSHMARK(SP);
79072805 2527 PUSHs(sv);
79072805 2528 PUTBACK;
a0d0e21e 2529
1b266415 2530 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2531
d3acc0f7 2532 POPSTACK;
1b266415 2533 if (SvTRUE(ERRSV)) {
1d615522 2534#ifndef PERL_MICRO
983dbef6 2535#ifdef HAS_SIGPROCMASK
1b266415
NIS
2536 /* Handler "died", for example to get out of a restart-able read().
2537 * Before we re-do that on its behalf re-enable the signal which was
2538 * blocked by the system when we entered.
2539 */
2540 sigset_t set;
2541 sigemptyset(&set);
2542 sigaddset(&set,sig);
2543 sigprocmask(SIG_UNBLOCK, &set, NULL);
2544#else
2545 /* Not clear if this will work */
2546 (void)rsignal(sig, SIG_IGN);
5c1546dc 2547 (void)rsignal(sig, PL_csighandlerp);
1b266415 2548#endif
1d615522 2549#endif /* !PERL_MICRO */
b3fe4827 2550 Perl_die(aTHX_ Nullformat);
1b266415 2551 }
00d579c5 2552cleanup:
84902520 2553 if (flags & 1)
3280af22 2554 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2555 if (flags & 4)
3280af22 2556 PL_markstack_ptr--;
ac27b0f5 2557 if (flags & 8)
3280af22 2558 PL_retstack_ix--;
84902520 2559 if (flags & 16)
3280af22 2560 PL_scopestack_ix -= 1;
84902520
TB
2561 if (flags & 64)
2562 SvREFCNT_dec(sv);
533c011a 2563 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2564
3280af22
NIS
2565 PL_Sv = tSv; /* Restore global temporaries. */
2566 PL_Xpv = tXpv;
53bb94e2 2567 return;
79072805 2568}
4e35701f
NIS
2569
2570
51371543 2571static void
acfe0abc 2572restore_magic(pTHX_ void *p)
51371543 2573{
48944bdf 2574 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2575 SV* sv = mgs->mgs_sv;
2576
2577 if (!sv)
2578 return;
2579
2580 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2581 {
2582 if (mgs->mgs_flags)
2583 SvFLAGS(sv) |= mgs->mgs_flags;
2584 else
2585 mg_magical(sv);
2586 if (SvGMAGICAL(sv))
2587 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2588 }
2589
2590 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2591
2592 /* If we're still on top of the stack, pop us off. (That condition
2593 * will be satisfied if restore_magic was called explicitly, but *not*
2594 * if it's being called via leave_scope.)
2595 * The reason for doing this is that otherwise, things like sv_2cv()
2596 * may leave alloc gunk on the savestack, and some code
2597 * (e.g. sighandler) doesn't expect that...
2598 */
2599 if (PL_savestack_ix == mgs->mgs_ss_ix)
2600 {
2601 I32 popval = SSPOPINT;
c76ac1ee 2602 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2603 PL_savestack_ix -= 2;
2604 popval = SSPOPINT;
2605 assert(popval == SAVEt_ALLOC);
2606 popval = SSPOPINT;
2607 PL_savestack_ix -= popval;
2608 }
2609
2610}
2611
2612static void
acfe0abc 2613unwind_handler_stack(pTHX_ void *p)
51371543 2614{
51371543
GS
2615 U32 flags = *(U32*)p;
2616
2617 if (flags & 1)
2618 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2619 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2620#if !defined(PERL_IMPLICIT_CONTEXT)
51371543
GS
2621 if (flags & 64)
2622 SvREFCNT_dec(sig_sv);
df3728a2 2623#endif
51371543 2624}
1018e26f
NIS
2625
2626