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