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