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