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