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