This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump Socket's version to a real floating point value
[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) {
979acdb5 387 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
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
79072805 400#include <signal.h>
79072805 401
942e002e 402U32
864dbfa3 403Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 404{
6cef1e77 405 register REGEXP *rx;
6cef1e77 406
aaa362c4 407 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
8f580fb8
IZ
408 if (mg->mg_obj) /* @+ */
409 return rx->nparens;
410 else /* @- */
411 return rx->lastparen;
412 }
ac27b0f5 413
942e002e 414 return (U32)-1;
6cef1e77
IZ
415}
416
417int
864dbfa3 418Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 419{
6cef1e77 420 register I32 paren;
cf93c79d 421 register I32 s;
6cef1e77
IZ
422 register I32 i;
423 register REGEXP *rx;
cf93c79d 424 I32 t;
6cef1e77 425
aaa362c4 426 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
6cef1e77
IZ
427 paren = mg->mg_len;
428 if (paren < 0)
429 return 0;
eb160463 430 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
431 (s = rx->startp[paren]) != -1 &&
432 (t = rx->endp[paren]) != -1)
6cef1e77
IZ
433 {
434 if (mg->mg_obj) /* @+ */
cf93c79d 435 i = t;
6cef1e77 436 else /* @- */
cf93c79d 437 i = s;
727405f8 438
a30b2f1f 439 if (i > 0 && RX_MATCH_UTF8(rx)) {
1aa99e6b 440 char *b = rx->subbeg;
0064a8a9
JH
441 if (b)
442 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 443 }
0064a8a9
JH
444
445 sv_setiv(sv, i);
6cef1e77
IZ
446 }
447 }
448 return 0;
449}
450
e4b89193 451int
a29d06ed
MG
452Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
453{
a29d06ed 454 Perl_croak(aTHX_ PL_no_modify);
e4b89193
GS
455 /* NOT REACHED */
456 return 0;
a29d06ed
MG
457}
458
93a17b20 459U32
864dbfa3 460Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20
LW
461{
462 register I32 paren;
93a17b20 463 register I32 i;
d9f97599 464 register REGEXP *rx;
a197cbdd 465 I32 s1, t1;
93a17b20
LW
466
467 switch (*mg->mg_ptr) {
468 case '1': case '2': case '3': case '4':
469 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 470 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 471
ffc61ed2 472 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 473 getparen:
eb160463 474 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
475 (s1 = rx->startp[paren]) != -1 &&
476 (t1 = rx->endp[paren]) != -1)
bbce6d69 477 {
cf93c79d 478 i = t1 - s1;
a197cbdd 479 getlen:
a30b2f1f 480 if (i > 0 && RX_MATCH_UTF8(rx)) {
ffc61ed2 481 char *s = rx->subbeg + s1;
a197cbdd 482 char *send = rx->subbeg + t1;
ffc61ed2 483
6d5fa195 484 i = t1 - s1;
60425c38
JH
485 if (is_utf8_string((U8*)s, i))
486 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
a197cbdd 487 }
ffc61ed2 488 if (i < 0)
0844c848 489 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 490 return i;
93a17b20 491 }
235bddc8
NIS
492 else {
493 if (ckWARN(WARN_UNINITIALIZED))
494 report_uninit();
495 }
496 }
497 else {
498 if (ckWARN(WARN_UNINITIALIZED))
499 report_uninit();
93a17b20 500 }
748a9306 501 return 0;
93a17b20 502 case '+':
aaa362c4 503 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 504 paren = rx->lastparen;
13f57bf8
CS
505 if (paren)
506 goto getparen;
93a17b20 507 }
748a9306 508 return 0;
a01268b5
JH
509 case '\016': /* ^N */
510 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
511 paren = rx->lastcloseparen;
512 if (paren)
513 goto getparen;
514 }
515 return 0;
93a17b20 516 case '`':
aaa362c4 517 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
518 if (rx->startp[0] != -1) {
519 i = rx->startp[0];
a197cbdd
GS
520 if (i > 0) {
521 s1 = 0;
522 t1 = i;
523 goto getlen;
524 }
93a17b20 525 }
93a17b20 526 }
748a9306 527 return 0;
93a17b20 528 case '\'':
aaa362c4 529 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
530 if (rx->endp[0] != -1) {
531 i = rx->sublen - rx->endp[0];
a197cbdd
GS
532 if (i > 0) {
533 s1 = rx->endp[0];
534 t1 = rx->sublen;
535 goto getlen;
536 }
93a17b20 537 }
93a17b20 538 }
748a9306 539 return 0;
93a17b20
LW
540 }
541 magic_get(sv,mg);
2d8e6c8d
GS
542 if (!SvPOK(sv) && SvNIOK(sv)) {
543 STRLEN n_a;
544 sv_2pv(sv, &n_a);
545 }
93a17b20
LW
546 if (SvPOK(sv))
547 return SvCUR(sv);
548 return 0;
549}
550
79072805 551int
864dbfa3 552Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
553{
554 register I32 paren;
35272f84 555 register char *s = NULL;
79072805 556 register I32 i;
d9f97599 557 register REGEXP *rx;
79072805
LW
558
559 switch (*mg->mg_ptr) {
748a9306 560 case '\001': /* ^A */
3280af22 561 sv_setsv(sv, PL_bodytarget);
748a9306 562 break;
49460fe6
NIS
563 case '\003': /* ^C */
564 sv_setiv(sv, (IV)PL_minus_c);
565 break;
566
79072805 567 case '\004': /* ^D */
aea4f609 568 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 569 break;
28f23441 570 case '\005': /* ^E */
0a378802 571 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 572#ifdef MACOS_TRADITIONAL
0a378802
JH
573 {
574 char msg[256];
727405f8 575
0a378802 576 sv_setnv(sv,(double)gMacPerl_OSErr);
727405f8 577 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
0a378802 578 }
727405f8 579#else
28f23441 580#ifdef VMS
0a378802
JH
581 {
582# include <descrip.h>
583# include <starlet.h>
584 char msg[255];
585 $DESCRIPTOR(msgdsc,msg);
586 sv_setnv(sv,(NV) vaxc$errno);
587 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
588 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
589 else
590 sv_setpv(sv,"");
591 }
28f23441 592#else
88e89b8a 593#ifdef OS2
0a378802
JH
594 if (!(_emx_env & 0x200)) { /* Under DOS */
595 sv_setnv(sv, (NV)errno);
596 sv_setpv(sv, errno ? Strerror(errno) : "");
597 } else {
598 if (errno != errno_isOS2) {
599 int tmp = _syserrno();
600 if (tmp) /* 2nd call to _syserrno() makes it 0 */
601 Perl_rc = tmp;
602 }
603 sv_setnv(sv, (NV)Perl_rc);
604 sv_setpv(sv, os2error(Perl_rc));
605 }
88e89b8a 606#else
22fae026 607#ifdef WIN32
0a378802
JH
608 {
609 DWORD dwErr = GetLastError();
610 sv_setnv(sv, (NV)dwErr);
611 if (dwErr)
612 {
613 PerlProc_GetOSError(sv, dwErr);
614 }
615 else
616 sv_setpv(sv, "");
617 SetLastError(dwErr);
618 }
22fae026 619#else
f6c8f21d
RGS
620 {
621 int saveerrno = errno;
622 sv_setnv(sv, (NV)errno);
623 sv_setpv(sv, errno ? Strerror(errno) : "");
624 errno = saveerrno;
625 }
28f23441 626#endif
88e89b8a 627#endif
22fae026 628#endif
cd39f2b6 629#endif
0a378802
JH
630 SvNOK_on(sv); /* what a wonderful hack! */
631 }
632 else if (strEQ(mg->mg_ptr+1, "NCODING"))
633 sv_setsv(sv, PL_encoding);
634 break;
79072805 635 case '\006': /* ^F */
3280af22 636 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 637 break;
a0d0e21e 638 case '\010': /* ^H */
3280af22 639 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 640 break;
9d116dd7 641 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
642 if (PL_inplace)
643 sv_setpv(sv, PL_inplace);
79072805 644 else
3280af22 645 sv_setsv(sv, &PL_sv_undef);
79072805 646 break;
ac27b0f5 647 case '\017': /* ^O & ^OPEN */
3511154c 648 if (*(mg->mg_ptr+1) == '\0') {
ac27b0f5 649 sv_setpv(sv, PL_osname);
3511154c
DM
650 SvTAINTED_off(sv);
651 }
ac27b0f5
NIS
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;
a3bcc51e 1537
463ee0b2
LW
1538 return 0;
1539}
1540
1541int
864dbfa3 1542Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1543{
463ee0b2 1544 dSP;
dff6d3cd 1545 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1546
1547 ENTER;
a0d0e21e 1548 SAVETMPS;
e788e7d3 1549 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1550 PUSHMARK(SP);
1551 EXTEND(SP, 2);
33c27489 1552 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1553 if (SvOK(key))
1554 PUSHs(key);
1555 PUTBACK;
1556
864dbfa3 1557 if (call_method(meth, G_SCALAR))
3280af22 1558 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1559
d3acc0f7 1560 POPSTACK;
a0d0e21e
LW
1561 FREETMPS;
1562 LEAVE;
79072805
LW
1563 return 0;
1564}
1565
1566int
864dbfa3 1567Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1568{
1569 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1570}
a0d0e21e 1571
a3bcc51e
TP
1572SV *
1573Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1574{
1575 dSP;
1576 SV *retval = &PL_sv_undef;
1577 SV *tied = SvTIED_obj((SV*)hv, mg);
1578 HV *pkg = SvSTASH((SV*)SvRV(tied));
1579
1580 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1581 SV *key;
1582 if (HvEITER(hv))
1583 /* we are in an iteration so the hash cannot be empty */
1584 return &PL_sv_yes;
1585 /* no xhv_eiter so now use FIRSTKEY */
1586 key = sv_newmortal();
1587 magic_nextpack((SV*)hv, mg, key);
1588 HvEITER(hv) = NULL; /* need to reset iterator */
1589 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1590 }
1591
1592 /* there is a SCALAR method that we can call */
1593 ENTER;
1594 PUSHSTACKi(PERLSI_MAGIC);
1595 PUSHMARK(SP);
1596 EXTEND(SP, 1);
1597 PUSHs(tied);
1598 PUTBACK;
1599
1600 if (call_method("SCALAR", G_SCALAR))
1601 retval = *PL_stack_sp--;
1602 POPSTACK;
1603 LEAVE;
1604 return retval;
1605}
1606
a0d0e21e 1607int
864dbfa3 1608Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1609{
1610 OP *o;
1611 I32 i;
1612 GV* gv;
1613 SV** svp;
2d8e6c8d 1614 STRLEN n_a;
79072805 1615
3280af22 1616 gv = PL_DBline;
79072805 1617 i = SvTRUE(sv);
188ea221 1618 svp = av_fetch(GvAV(gv),
2d8e6c8d 1619 atoi(MgPV(mg,n_a)), FALSE);
5df8de69
DM
1620 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1621 /* set or clear breakpoint in the relevant control op */
1622 if (i)
1623 o->op_flags |= OPf_SPECIAL;
1624 else
1625 o->op_flags &= ~OPf_SPECIAL;
1626 }
79072805
LW
1627 return 0;
1628}
1629
1630int
864dbfa3 1631Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1632{
3280af22 1633 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1634 return 0;
1635}
1636
1637int
864dbfa3 1638Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1639{
3280af22 1640 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1641 return 0;
1642}
1643
1644int
864dbfa3 1645Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1646{
1647 SV* lsv = LvTARG(sv);
ac27b0f5 1648
a0d0e21e 1649 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1650 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1651 if (mg && mg->mg_len >= 0) {
a0ed51b3 1652 I32 i = mg->mg_len;
7e2040f0 1653 if (DO_UTF8(lsv))
a0ed51b3
LW
1654 sv_pos_b2u(lsv, &i);
1655 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1656 return 0;
1657 }
1658 }
1659 (void)SvOK_off(sv);
1660 return 0;
1661}
1662
1663int
864dbfa3 1664Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1665{
1666 SV* lsv = LvTARG(sv);
1667 SSize_t pos;
1668 STRLEN len;
c00206c8 1669 STRLEN ulen = 0;
a0d0e21e
LW
1670
1671 mg = 0;
ac27b0f5 1672
a0d0e21e 1673 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1674 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1675 if (!mg) {
1676 if (!SvOK(sv))
1677 return 0;
14befaf4
DM
1678 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1679 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1680 }
1681 else if (!SvOK(sv)) {
565764a8 1682 mg->mg_len = -1;
a0d0e21e
LW
1683 return 0;
1684 }
1685 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1686
c485e607 1687 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1688
7e2040f0 1689 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1690 ulen = sv_len_utf8(lsv);
1691 if (ulen)
1692 len = ulen;
a0ed51b3
LW
1693 }
1694
a0d0e21e
LW
1695 if (pos < 0) {
1696 pos += len;
1697 if (pos < 0)
1698 pos = 0;
1699 }
eb160463 1700 else if (pos > (SSize_t)len)
a0d0e21e 1701 pos = len;
a0ed51b3
LW
1702
1703 if (ulen) {
1704 I32 p = pos;
1705 sv_pos_u2b(lsv, &p, 0);
1706 pos = p;
1707 }
727405f8 1708
565764a8 1709 mg->mg_len = pos;
71be2cbc 1710 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1711
79072805
LW
1712 return 0;
1713}
1714
1715int
864dbfa3 1716Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1717{
8646b087
PP
1718 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1719 SvFAKE_off(sv);
946ec16e 1720 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1721 SvFAKE_on(sv);
1722 }
1723 else
946ec16e 1724 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1725 return 0;
1726}
1727
1728int
864dbfa3 1729Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1730{
1731 register char *s;
1732 GV* gv;
2d8e6c8d 1733 STRLEN n_a;
79072805
LW
1734
1735 if (!SvOK(sv))
1736 return 0;
2d8e6c8d 1737 s = SvPV(sv, n_a);
79072805
LW
1738 if (*s == '*' && s[1])
1739 s++;
85e6fe83 1740 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1741 if (sv == (SV*)gv)
1742 return 0;
1743 if (GvGP(sv))
88e89b8a 1744 gp_free((GV*)sv);
79072805 1745 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1746 return 0;
1747}
1748
1749int
864dbfa3 1750Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1751{
1752 STRLEN len;
1753 SV *lsv = LvTARG(sv);
1754 char *tmps = SvPV(lsv,len);
1755 I32 offs = LvTARGOFF(sv);
1756 I32 rem = LvTARGLEN(sv);
1757
9aa983d2
JH
1758 if (SvUTF8(lsv))
1759 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1760 if (offs > (I32)len)
6ff81951 1761 offs = len;
eb160463 1762 if (rem + offs > (I32)len)
6ff81951
GS
1763 rem = len - offs;
1764 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1765 if (SvUTF8(lsv))
2ef4b674 1766 SvUTF8_on(sv);
6ff81951
GS
1767 return 0;
1768}
1769
1770int
864dbfa3 1771Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1772{
9aa983d2
JH
1773 STRLEN len;
1774 char *tmps = SvPV(sv, len);
1775 SV *lsv = LvTARG(sv);
1776 I32 lvoff = LvTARGOFF(sv);
1777 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1778
1aa99e6b 1779 if (DO_UTF8(sv)) {
9aa983d2
JH
1780 sv_utf8_upgrade(lsv);
1781 sv_pos_u2b(lsv, &lvoff, &lvlen);
1782 sv_insert(lsv, lvoff, lvlen, tmps, len);
1783 SvUTF8_on(lsv);
1784 }
9bf12eaf 1785 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1786 sv_pos_u2b(lsv, &lvoff, &lvlen);
e95af362 1787 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1788 sv_insert(lsv, lvoff, lvlen, tmps, len);
1789 Safefree(tmps);
1aa99e6b
IH
1790 }
1791 else
9aa983d2 1792 sv_insert(lsv, lvoff, lvlen, tmps, len);
1aa99e6b 1793
79072805
LW
1794 return 0;
1795}
1796
1797int
864dbfa3 1798Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1799{
565764a8 1800 TAINT_IF((mg->mg_len & 1) ||
155aba94 1801 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1802 return 0;
1803}
1804
1805int
864dbfa3 1806Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1807{
3280af22
NIS
1808 if (PL_localizing) {
1809 if (PL_localizing == 1)
565764a8 1810 mg->mg_len <<= 1;
748a9306 1811 else
565764a8 1812 mg->mg_len >>= 1;
a0d0e21e 1813 }
3280af22 1814 else if (PL_tainted)
565764a8 1815 mg->mg_len |= 1;
748a9306 1816 else
565764a8 1817 mg->mg_len &= ~1;
463ee0b2
LW
1818 return 0;
1819}
1820
1821int
864dbfa3 1822Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1823{
1824 SV *lsv = LvTARG(sv);
6ff81951
GS
1825
1826 if (!lsv) {
155aba94 1827 (void)SvOK_off(sv);
6ff81951
GS
1828 return 0;
1829 }
6ff81951 1830
81e118e0 1831 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1832 return 0;
1833}
1834
1835int
864dbfa3 1836Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1837{
1838 do_vecset(sv); /* XXX slurp this routine */
1839 return 0;
1840}
1841
1842int
864dbfa3 1843Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1844{
71be2cbc 1845 SV *targ = Nullsv;
5f05dabc 1846 if (LvTARGLEN(sv)) {
68dc0745 1847 if (mg->mg_obj) {
74e13ce4 1848 SV *ahv = LvTARG(sv);
6d822dc4
MS
1849 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1850 if (he)
1851 targ = HeVAL(he);
68dc0745
PP
1852 }
1853 else {
3c78fafa 1854 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1855 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1856 targ = AvARRAY(av)[LvTARGOFF(sv)];
1857 }
3280af22 1858 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1859 /* somebody else defined it for us */
1860 SvREFCNT_dec(LvTARG(sv));
1861 LvTARG(sv) = SvREFCNT_inc(targ);
1862 LvTARGLEN(sv) = 0;
1863 SvREFCNT_dec(mg->mg_obj);
1864 mg->mg_obj = Nullsv;
1865 mg->mg_flags &= ~MGf_REFCOUNTED;
1866 }
5f05dabc 1867 }
71be2cbc
PP
1868 else
1869 targ = LvTARG(sv);
3280af22 1870 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1871 return 0;
1872}
1873
1874int
864dbfa3 1875Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1876{
1877 if (LvTARGLEN(sv))
68dc0745
PP
1878 vivify_defelem(sv);
1879 if (LvTARG(sv)) {
5f05dabc 1880 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1881 SvSETMAGIC(LvTARG(sv));
1882 }
5f05dabc
PP
1883 return 0;
1884}
1885
71be2cbc 1886void
864dbfa3 1887Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1888{
74e13ce4
GS
1889 MAGIC *mg;
1890 SV *value = Nullsv;
71be2cbc 1891
14befaf4 1892 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1893 return;
68dc0745 1894 if (mg->mg_obj) {
74e13ce4 1895 SV *ahv = LvTARG(sv);
2d8e6c8d 1896 STRLEN n_a;
6d822dc4
MS
1897 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1898 if (he)
1899 value = HeVAL(he);
3280af22 1900 if (!value || value == &PL_sv_undef)
cea2e8a9 1901 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1902 }
68dc0745
PP
1903 else {
1904 AV* av = (AV*)LvTARG(sv);
5aabfad6 1905 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1906 LvTARG(sv) = Nullsv; /* array can't be extended */
1907 else {
1908 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1909 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1910 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1911 }
1912 }
3e3baf6d 1913 (void)SvREFCNT_inc(value);
68dc0745
PP
1914 SvREFCNT_dec(LvTARG(sv));
1915 LvTARG(sv) = value;
71be2cbc 1916 LvTARGLEN(sv) = 0;
68dc0745
PP
1917 SvREFCNT_dec(mg->mg_obj);
1918 mg->mg_obj = Nullsv;
1919 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1920}
1921
1922int
864dbfa3 1923Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1924{
1925 AV *av = (AV*)mg->mg_obj;
1926 SV **svp = AvARRAY(av);
1927 I32 i = AvFILLp(av);
1928 while (i >= 0) {
fdc9a813 1929 if (svp[i]) {
810b8aa5 1930 if (!SvWEAKREF(svp[i]))
cea2e8a9 1931 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1932 /* XXX Should we check that it hasn't changed? */
1933 SvRV(svp[i]) = 0;
155aba94 1934 (void)SvOK_off(svp[i]);
810b8aa5 1935 SvWEAKREF_off(svp[i]);
fdc9a813 1936 svp[i] = Nullsv;
810b8aa5
GS
1937 }
1938 i--;
1939 }
d99b02a1 1940 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5
GS
1941 return 0;
1942}
1943
1944int
864dbfa3 1945Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1946{
565764a8 1947 mg->mg_len = -1;
c6496cc7 1948 SvSCREAM_off(sv);
93a17b20
LW
1949 return 0;
1950}
1951
1952int
864dbfa3 1953Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1954{
14befaf4 1955 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1956 SvVALID_off(sv);
1957 return 0;
1958}
1959
1960int
864dbfa3 1961Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1962{
14befaf4 1963 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1964 SvCOMPILED_off(sv);
1965 return 0;
1966}
1967
1968int
864dbfa3 1969Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1970{
1971 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1972
1973 if (uf && uf->uf_set)
24f81a43 1974 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
1975 return 0;
1976}
1977
c277df42 1978int
faf82a0b
AE
1979Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1980{
1981 sv_unmagic(sv, PERL_MAGIC_qr);
1982 return 0;
1983}
1984
1985int
864dbfa3 1986Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1987{
1988 regexp *re = (regexp *)mg->mg_obj;
1989 ReREFCNT_dec(re);
1990 return 0;
1991}
1992
7a4c00b4 1993#ifdef USE_LOCALE_COLLATE
79072805 1994int
864dbfa3 1995Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1996{
1997 /*
838b5b74 1998 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1999 * and vanished with a faint plop.
2000 */
7a4c00b4
PP
2001 if (mg->mg_ptr) {
2002 Safefree(mg->mg_ptr);
2003 mg->mg_ptr = NULL;
565764a8 2004 mg->mg_len = -1;
7a4c00b4 2005 }
bbce6d69
PP
2006 return 0;
2007}
7a4c00b4 2008#endif /* USE_LOCALE_COLLATE */
bbce6d69 2009
7e8c5dac
HS
2010/* Just clear the UTF-8 cache data. */
2011int
2012Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2013{
2014 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2015 mg->mg_ptr = 0;
2016 mg->mg_len = -1; /* The mg_len holds the len cache. */
2017 return 0;
2018}
2019
bbce6d69 2020int
864dbfa3 2021Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
2022{
2023 register char *s;
2024 I32 i;
8990e307 2025 STRLEN len;
79072805 2026 switch (*mg->mg_ptr) {
748a9306 2027 case '\001': /* ^A */
3280af22 2028 sv_setsv(PL_bodytarget, sv);
748a9306 2029 break;
49460fe6 2030 case '\003': /* ^C */
eb160463 2031 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
2032 break;
2033
79072805 2034 case '\004': /* ^D */
b4ab917c
DM
2035#ifdef DEBUGGING
2036 s = SvPV_nolen(sv);
2037 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
79072805 2038 DEBUG_x(dump_all());
b4ab917c
DM
2039#else
2040 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2041#endif
79072805 2042 break;
28f23441 2043 case '\005': /* ^E */
d0063567 2044 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2045#ifdef MACOS_TRADITIONAL
d0063567 2046 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2047#else
cd39f2b6 2048# ifdef VMS
d0063567 2049 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2050# else
cd39f2b6 2051# ifdef WIN32
d0063567 2052 SetLastError( SvIV(sv) );
cd39f2b6 2053# else
9fed8b87 2054# ifdef OS2
d0063567 2055 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2056# else
d0063567
DK
2057 /* will anyone ever use this? */
2058 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2059# endif
048c1ddf
IZ
2060# endif
2061# endif
22fae026 2062#endif
d0063567
DK
2063 }
2064 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2065 if (PL_encoding)
2066 SvREFCNT_dec(PL_encoding);
2067 if (SvOK(sv) || SvGMAGICAL(sv)) {
2068 PL_encoding = newSVsv(sv);
2069 }
2070 else {
2071 PL_encoding = Nullsv;
2072 }
2073 }
2074 break;
79072805 2075 case '\006': /* ^F */
3280af22 2076 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2077 break;
a0d0e21e 2078 case '\010': /* ^H */
3280af22 2079 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2080 break;
9d116dd7 2081 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2082 if (PL_inplace)
2083 Safefree(PL_inplace);
79072805 2084 if (SvOK(sv))
2d8e6c8d 2085 PL_inplace = savepv(SvPV(sv,len));
79072805 2086 else
3280af22 2087 PL_inplace = Nullch;
79072805 2088 break;
28f23441 2089 case '\017': /* ^O */
ac27b0f5 2090 if (*(mg->mg_ptr+1) == '\0') {
1bf29663 2091 if (PL_osname) {
ac27b0f5 2092 Safefree(PL_osname);
1bf29663
DM
2093 PL_osname = Nullch;
2094 }
3511154c
DM
2095 if (SvOK(sv)) {
2096 TAINT_PROPER("assigning to $^O");
ac27b0f5 2097 PL_osname = savepv(SvPV(sv,len));
3511154c 2098 }
ac27b0f5
NIS
2099 }
2100 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2101 if (!PL_compiling.cop_io)
2102 PL_compiling.cop_io = newSVsv(sv);
2103 else
2104 sv_setsv(PL_compiling.cop_io,sv);
2105 }
28f23441 2106 break;
79072805 2107 case '\020': /* ^P */
3280af22 2108 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
39bd0f18
PJ
2109 if ((PERLDB_SUB || PERLDB_LINE || PERLDB_SUBLINE || PERLDB_ASSERTION)
2110 && !PL_DBsingle)
1ee4443e 2111 init_debugger();
79072805
LW
2112 break;
2113 case '\024': /* ^T */
88e89b8a 2114#ifdef BIG_TIME
6b88bc9c 2115 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2116#else
3280af22 2117 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2118#endif
79072805 2119 break;
fde18df1 2120 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2121 if (*(mg->mg_ptr+1) == '\0') {
2122 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2123 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2124 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2125 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2126 }
599cee73 2127 }
0a378802 2128 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2129 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2130 if (!SvPOK(sv) && PL_localizing) {
2131 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2132 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2133 break;
2134 }
f4fc7782 2135 {
b5477537 2136 STRLEN len, i;
d3a7d8c7 2137 int accumulate = 0 ;
f4fc7782 2138 int any_fatals = 0 ;
d3a7d8c7 2139 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
2140 for (i = 0 ; i < len ; ++i) {
2141 accumulate |= ptr[i] ;
2142 any_fatals |= (ptr[i] & 0xAA) ;
2143 }
d3a7d8c7
GS
2144 if (!accumulate)
2145 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2146 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2147 PL_compiling.cop_warnings = pWARN_ALL;
2148 PL_dowarn |= G_WARN_ONCE ;
727405f8 2149 }
d3a7d8c7
GS
2150 else {
2151 if (specialWARN(PL_compiling.cop_warnings))
2152 PL_compiling.cop_warnings = newSVsv(sv) ;
2153 else
2154 sv_setsv(PL_compiling.cop_warnings, sv);
2155 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2156 PL_dowarn |= G_WARN_ONCE ;
2157 }
f4fc7782 2158
d3a7d8c7 2159 }
4438c4b7 2160 }
971a9dd3 2161 }
79072805
LW
2162 break;
2163 case '.':
3280af22
NIS
2164 if (PL_localizing) {
2165 if (PL_localizing == 1)
7766f137 2166 SAVESPTR(PL_last_in_gv);
748a9306 2167 }
3280af22 2168 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2169 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2170 break;
2171 case '^':
3280af22 2172 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2173 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2174 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2175 break;
2176 case '~':
3280af22 2177 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2178 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2179 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2180 break;
2181 case '=':
632db599 2182 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2183 break;
2184 case '-':
632db599 2185 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2186 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2187 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2188 break;
2189 case '%':
632db599 2190 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2191 break;
2192 case '|':
4b65379b 2193 {
3280af22 2194 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2195 if(!io)
2196 break;
4b65379b
CS
2197 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2198 IoFLAGS(io) &= ~IOf_FLUSH;
2199 else {
2200 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2201 PerlIO *ofp = IoOFP(io);
2202 if (ofp)
2203 (void)PerlIO_flush(ofp);
2204 IoFLAGS(io) |= IOf_FLUSH;
2205 }
2206 }
79072805
LW
2207 }
2208 break;
79072805 2209 case '/':
3280af22 2210 SvREFCNT_dec(PL_rs);
8bfdd7d9 2211 PL_rs = newSVsv(sv);
79072805
LW
2212 break;
2213 case '\\':
7889fe52
NIS
2214 if (PL_ors_sv)
2215 SvREFCNT_dec(PL_ors_sv);
009c130f 2216 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2217 PL_ors_sv = newSVsv(sv);
009c130f 2218 }
e3c19b7b 2219 else {
7889fe52 2220 PL_ors_sv = Nullsv;
e3c19b7b 2221 }
79072805
LW
2222 break;
2223 case ',':
7889fe52
NIS
2224 if (PL_ofs_sv)
2225 SvREFCNT_dec(PL_ofs_sv);
2226 if (SvOK(sv) || SvGMAGICAL(sv)) {
2227 PL_ofs_sv = newSVsv(sv);
2228 }
2229 else {
2230 PL_ofs_sv = Nullsv;
2231 }
79072805
LW
2232 break;
2233 case '#':
3280af22
NIS
2234 if (PL_ofmt)
2235 Safefree(PL_ofmt);
2d8e6c8d 2236 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
2237 break;
2238 case '[':
3280af22 2239 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2240 break;
2241 case '?':
ff0cee69 2242#ifdef COMPLEX_STATUS
6b88bc9c
GS
2243 if (PL_localizing == 2) {
2244 PL_statusvalue = LvTARGOFF(sv);
2245 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2246 }
2247 else
2248#endif
2249#ifdef VMSISH_STATUS
2250 if (VMSISH_STATUS)
2251 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2252 else
2253#endif
2254 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2255 break;
2256 case '!':
93189314
JH
2257 {
2258#ifdef VMS
2259# define PERL_VMS_BANG vaxc$errno
2260#else
2261# define PERL_VMS_BANG 0
2262#endif
91487cfc 2263 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2264 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2265 }
79072805
LW
2266 break;
2267 case '<':
3280af22
NIS
2268 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2269 if (PL_delaymagic) {
2270 PL_delaymagic |= DM_RUID;
79072805
LW
2271 break; /* don't do magic till later */
2272 }
2273#ifdef HAS_SETRUID
b28d0864 2274 (void)setruid((Uid_t)PL_uid);
79072805
LW
2275#else
2276#ifdef HAS_SETREUID
3280af22 2277 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2278#else
85e6fe83 2279#ifdef HAS_SETRESUID
b28d0864 2280 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2281#else
75870ed3 2282 if (PL_uid == PL_euid) { /* special case $< = $> */
2283#ifdef PERL_DARWIN
2284 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2285 if (PL_uid != 0 && PerlProc_getuid() == 0)
2286 (void)PerlProc_setuid(0);
2287#endif
b28d0864 2288 (void)PerlProc_setuid(PL_uid);
75870ed3 2289 } else {
d8eceb89 2290 PL_uid = PerlProc_getuid();
cea2e8a9 2291 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2292 }
79072805
LW
2293#endif
2294#endif
85e6fe83 2295#endif
d8eceb89 2296 PL_uid = PerlProc_getuid();
3280af22 2297 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2298 break;
2299 case '>':
3280af22
NIS
2300 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2301 if (PL_delaymagic) {
2302 PL_delaymagic |= DM_EUID;
79072805
LW
2303 break; /* don't do magic till later */
2304 }
2305#ifdef HAS_SETEUID
3280af22 2306 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2307#else
2308#ifdef HAS_SETREUID
b28d0864 2309 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2310#else
2311#ifdef HAS_SETRESUID
6b88bc9c 2312 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2313#else
b28d0864
NIS
2314 if (PL_euid == PL_uid) /* special case $> = $< */
2315 PerlProc_setuid(PL_euid);
a0d0e21e 2316 else {
e8ee3774 2317 PL_euid = PerlProc_geteuid();
cea2e8a9 2318 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2319 }
79072805
LW
2320#endif
2321#endif
85e6fe83 2322#endif
d8eceb89 2323 PL_euid = PerlProc_geteuid();
3280af22 2324 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2325 break;
2326 case '(':
3280af22
NIS
2327 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2328 if (PL_delaymagic) {
2329 PL_delaymagic |= DM_RGID;
79072805
LW
2330 break; /* don't do magic till later */
2331 }
2332#ifdef HAS_SETRGID
b28d0864 2333 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2334#else
2335#ifdef HAS_SETREGID
3280af22 2336 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2337#else
2338#ifdef HAS_SETRESGID
b28d0864 2339 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2340#else
b28d0864
NIS
2341 if (PL_gid == PL_egid) /* special case $( = $) */
2342 (void)PerlProc_setgid(PL_gid);
748a9306 2343 else {
d8eceb89 2344 PL_gid = PerlProc_getgid();
cea2e8a9 2345 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2346 }
79072805
LW
2347#endif
2348#endif
85e6fe83 2349#endif
d8eceb89 2350 PL_gid = PerlProc_getgid();
3280af22 2351 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2352 break;
2353 case ')':
5cd24f17
PP
2354#ifdef HAS_SETGROUPS
2355 {
2d8e6c8d 2356 char *p = SvPV(sv, len);
5cd24f17
PP
2357 Groups_t gary[NGROUPS];
2358
5cd24f17
PP
2359 while (isSPACE(*p))
2360 ++p;
2d4389e4 2361 PL_egid = Atol(p);
5cd24f17
PP
2362 for (i = 0; i < NGROUPS; ++i) {
2363 while (*p && !isSPACE(*p))
2364 ++p;
2365 while (isSPACE(*p))
2366 ++p;
2367 if (!*p)
2368 break;
2d4389e4 2369 gary[i] = Atol(p);
5cd24f17 2370 }
8cc95fdb
PP
2371 if (i)
2372 (void)setgroups(i, gary);
5cd24f17
PP
2373 }
2374#else /* HAS_SETGROUPS */
b28d0864 2375 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2376#endif /* HAS_SETGROUPS */
3280af22
NIS
2377 if (PL_delaymagic) {
2378 PL_delaymagic |= DM_EGID;
79072805
LW
2379 break; /* don't do magic till later */
2380 }
2381#ifdef HAS_SETEGID
3280af22 2382 (void)setegid((Gid_t)PL_egid);
79072805
LW
2383#else
2384#ifdef HAS_SETREGID
b28d0864 2385 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2386#else
2387#ifdef HAS_SETRESGID
b28d0864 2388 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2389#else
b28d0864
NIS
2390 if (PL_egid == PL_gid) /* special case $) = $( */
2391 (void)PerlProc_setgid(PL_egid);
748a9306 2392 else {
d8eceb89 2393 PL_egid = PerlProc_getegid();
cea2e8a9 2394 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2395 }
79072805
LW
2396#endif
2397#endif
85e6fe83 2398#endif
d8eceb89 2399 PL_egid = PerlProc_getegid();
3280af22 2400 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2401 break;
2402 case ':':
2d8e6c8d 2403 PL_chopset = SvPV_force(sv,len);
79072805 2404 break;
cd39f2b6 2405#ifndef MACOS_TRADITIONAL
79072805 2406 case '0':
e2975953 2407 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2408#ifdef HAS_SETPROCTITLE
2409 /* The BSDs don't show the argv[] in ps(1) output, they
2410 * show a string from the process struct and provide
2411 * the setproctitle() routine to manipulate that. */
2412 {
2413 s = SvPV(sv, len);
98b76f99 2414# if __FreeBSD_version > 410001
9aad2c0e 2415 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2416 * but not the "(perl) suffix from the ps(1)
2417 * output, because that's what ps(1) shows if the
2418 * argv[] is modified. */
6f2ad931 2419 setproctitle("-%s", s);
9aad2c0e 2420# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2421 /* This doesn't really work if you assume that
2422 * $0 = 'foobar'; will wipe out 'perl' from the $0
2423 * because in ps(1) output the result will be like
2424 * sprintf("perl: %s (perl)", s)
2425 * I guess this is a security feature:
2426 * one (a user process) cannot get rid of the original name.
2427 * --jhi */
2428 setproctitle("%s", s);
2429# endif
2430 }
2431#endif
17aa7f3d
JH
2432#if defined(__hpux) && defined(PSTAT_SETCMD)
2433 {
2434 union pstun un;
2435 s = SvPV(sv, len);
2436 un.pst_command = s;
2437 pstat(PSTAT_SETCMD, un, len, 0, 0);
2438 }
2439#endif
3cb9023d 2440 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2441 s = SvPV_force(sv,len);
6f698202
AMS
2442 if (len >= (STRLEN)PL_origalen-1) {
2443 /* Longer than original, will be truncated. We assume that
2444 * PL_origalen bytes are available. */
2445 Copy(s, PL_origargv[0], PL_origalen-1, char);
79072805
LW
2446 }
2447 else {
54bfe034
JH
2448 /* Shorter than original, will be padded. */
2449 Copy(s, PL_origargv[0], len, char);
2450 PL_origargv[0][len] = 0;
2451 memset(PL_origargv[0] + len + 1,
2452 /* Is the space counterintuitive? Yes.
2453 * (You were expecting \0?)
3cb9023d 2454 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2455 * --jhi */
2456 (int)' ',
2457 PL_origalen - len - 1);
79072805 2458 }
ad7eccf4
JD
2459 PL_origargv[0][PL_origalen-1] = 0;
2460 for (i = 1; i < PL_origargc; i++)
2461 PL_origargv[i] = 0;
e2975953 2462 UNLOCK_DOLLARZERO_MUTEX;
79072805 2463 break;
cd39f2b6 2464#endif
79072805
LW
2465 }
2466 return 0;
2467}
2468
2469I32
864dbfa3 2470Perl_whichsig(pTHX_ char *sig)
79072805
LW
2471{
2472 register char **sigv;
2473
e02bfb16 2474 for (sigv = PL_sig_name; *sigv; sigv++)
79072805 2475 if (strEQ(sig,*sigv))
22c35a8c 2476 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2477#ifdef SIGCLD
2478 if (strEQ(sig,"CHLD"))
2479 return SIGCLD;
2480#endif
2481#ifdef SIGCHLD
2482 if (strEQ(sig,"CLD"))
2483 return SIGCHLD;
2484#endif
7f1236c0 2485 return -1;
79072805
LW
2486}
2487
df3728a2 2488#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2489static SV* sig_sv;
df3728a2 2490#endif
84902520 2491
ecfc5424 2492Signal_t
cea2e8a9 2493Perl_sighandler(int sig)
79072805 2494{
1018e26f
NIS
2495#ifdef PERL_GET_SIG_CONTEXT
2496 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2497#else
cea2e8a9 2498 dTHX;
71d280e3 2499#endif
79072805 2500 dSP;
00d579c5 2501 GV *gv = Nullgv;
a0d0e21e 2502 HV *st;
b7953727 2503 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2504 CV *cv = Nullcv;
533c011a 2505 OP *myop = PL_op;
84902520 2506 U32 flags = 0;
3280af22 2507 XPV *tXpv = PL_Xpv;
71d280e3 2508
3280af22 2509 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2510 flags |= 1;
3280af22 2511 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2512 flags |= 4;
3280af22 2513 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2514 flags |= 8;
3280af22 2515 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2516 flags |= 16;
2517
727405f8 2518 if (!PL_psig_ptr[sig]) {
99ef548b 2519 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2520 PL_sig_name[sig]);
2521 exit(sig);
2522 }
ff0cee69 2523
84902520
TB
2524 /* Max number of items pushed there is 3*n or 4. We cannot fix
2525 infinity, so we fix 4 (in fact 5): */
2526 if (flags & 1) {
3280af22 2527 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2528 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2529 }
ac27b0f5 2530 if (flags & 4)
3280af22 2531 PL_markstack_ptr++; /* Protect mark. */
84902520 2532 if (flags & 8) {
3280af22
NIS
2533 PL_retstack_ix++;
2534 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2535 }
2536 if (flags & 16)
3280af22 2537 PL_scopestack_ix += 1;
84902520 2538 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2539 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2540 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2541 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2542
a0d0e21e 2543 if (!cv || !CvROOT(cv)) {
599cee73 2544 if (ckWARN(WARN_SIGNAL))
9014280d 2545 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2546 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2547 : ((cv && CvGV(cv))
2548 ? GvENAME(CvGV(cv))
2549 : "__ANON__")));
2550 goto cleanup;
79072805
LW
2551 }
2552
22c35a8c
GS
2553 if(PL_psig_name[sig]) {
2554 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2555 flags |= 64;
df3728a2 2556#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2557 sig_sv = sv;
df3728a2 2558#endif
84902520 2559 } else {
ff0cee69 2560 sv = sv_newmortal();
22c35a8c 2561 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2562 }
e336de0d 2563
e788e7d3 2564 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2565 PUSHMARK(SP);
79072805 2566 PUSHs(sv);
79072805 2567 PUTBACK;
a0d0e21e 2568
1b266415 2569 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2570
d3acc0f7 2571 POPSTACK;
1b266415 2572 if (SvTRUE(ERRSV)) {
1d615522 2573#ifndef PERL_MICRO
983dbef6 2574#ifdef HAS_SIGPROCMASK
1b266415
NIS
2575 /* Handler "died", for example to get out of a restart-able read().
2576 * Before we re-do that on its behalf re-enable the signal which was
2577 * blocked by the system when we entered.
2578 */
2579 sigset_t set;
2580 sigemptyset(&set);
2581 sigaddset(&set,sig);
2582 sigprocmask(SIG_UNBLOCK, &set, NULL);
2583#else
2584 /* Not clear if this will work */
2585 (void)rsignal(sig, SIG_IGN);
5c1546dc 2586 (void)rsignal(sig, PL_csighandlerp);
1b266415 2587#endif
1d615522 2588#endif /* !PERL_MICRO */
b3fe4827 2589 Perl_die(aTHX_ Nullformat);
1b266415 2590 }
00d579c5 2591cleanup:
84902520 2592 if (flags & 1)
3280af22 2593 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2594 if (flags & 4)
3280af22 2595 PL_markstack_ptr--;
ac27b0f5 2596 if (flags & 8)
3280af22 2597 PL_retstack_ix--;
84902520 2598 if (flags & 16)
3280af22 2599 PL_scopestack_ix -= 1;
84902520
TB
2600 if (flags & 64)
2601 SvREFCNT_dec(sv);
533c011a 2602 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2603
3280af22
NIS
2604 PL_Sv = tSv; /* Restore global temporaries. */
2605 PL_Xpv = tXpv;
53bb94e2 2606 return;
79072805 2607}
4e35701f
NIS
2608
2609
51371543 2610static void
acfe0abc 2611restore_magic(pTHX_ void *p)
51371543 2612{
48944bdf 2613 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2614 SV* sv = mgs->mgs_sv;
2615
2616 if (!sv)
2617 return;
2618
2619 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2620 {
f9701176
NC
2621#ifdef PERL_COPY_ON_WRITE
2622 /* While magic was saved (and off) sv_setsv may well have seen
2623 this SV as a prime candidate for COW. */
2624 if (SvIsCOW(sv))
2625 sv_force_normal(sv);
2626#endif
2627
51371543
GS
2628 if (mgs->mgs_flags)
2629 SvFLAGS(sv) |= mgs->mgs_flags;
2630 else
2631 mg_magical(sv);
2632 if (SvGMAGICAL(sv))
2633 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2634 }
2635
2636 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2637
2638 /* If we're still on top of the stack, pop us off. (That condition
2639 * will be satisfied if restore_magic was called explicitly, but *not*
2640 * if it's being called via leave_scope.)
2641 * The reason for doing this is that otherwise, things like sv_2cv()
2642 * may leave alloc gunk on the savestack, and some code
2643 * (e.g. sighandler) doesn't expect that...
2644 */
2645 if (PL_savestack_ix == mgs->mgs_ss_ix)
2646 {
2647 I32 popval = SSPOPINT;
c76ac1ee 2648 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2649 PL_savestack_ix -= 2;
2650 popval = SSPOPINT;
2651 assert(popval == SAVEt_ALLOC);
2652 popval = SSPOPINT;
2653 PL_savestack_ix -= popval;
2654 }
2655
2656}
2657
2658static void
acfe0abc 2659unwind_handler_stack(pTHX_ void *p)
51371543 2660{
51371543
GS
2661 U32 flags = *(U32*)p;
2662
2663 if (flags & 1)
2664 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2665 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2666#if !defined(PERL_IMPLICIT_CONTEXT)
51371543
GS
2667 if (flags & 64)
2668 SvREFCNT_dec(sig_sv);
df3728a2 2669#endif
51371543 2670}
1018e26f
NIS
2671
2672