Introduce i_ieefp because FreeBSD on ix86 needs that
[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;
342 register char *s;
343 register I32 i;
344 register REGEXP *rx;
345 char *t;
346
8f580fb8 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 355}
356
357int
864dbfa3 358Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 359{
360 dTHR;
361 register I32 paren;
cf93c79d 362 register I32 s;
6cef1e77 363 register I32 i;
364 register REGEXP *rx;
cf93c79d 365 I32 t;
6cef1e77 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 372 (s = rx->startp[paren]) != -1 &&
373 (t = rx->endp[paren]) != -1)
6cef1e77 374 {
375 if (mg->mg_obj) /* @+ */
cf93c79d 376 i = t;
6cef1e77 377 else /* @- */
cf93c79d 378 i = s;
6cef1e77 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 389 register I32 paren;
390 register char *s;
391 register I32 i;
d9f97599 392 register REGEXP *rx;
748a9306 393 char *t;
93a17b20 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 399 I32 s1, t1;
400
93a17b20 401 paren = atoi(mg->mg_ptr);
402 getparen:
d9f97599 403 if (paren <= rx->nparens &&
cf93c79d 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 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 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 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 442 }
443 magic_get(sv,mg);
2d8e6c8d 444 if (!SvPOK(sv) && SvNIOK(sv)) {
445 STRLEN n_a;
446 sv_2pv(sv, &n_a);
447 }
93a17b20 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 457 register I32 paren;
458 register char *s;
459 register I32 i;
d9f97599 460 register REGEXP *rx;
748a9306 461 char *t;
79072805 462
463 switch (*mg->mg_ptr) {
748a9306 464 case '\001': /* ^A */
3280af22 465 sv_setsv(sv, PL_bodytarget);
748a9306 466 break;
49460fe6 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 473#if defined(YYDEBUG) && defined(DEBUGGING)
474 PL_yydebug = (PL_debug & 1);
475#endif
79072805 476 break;
28f23441 477 case '\005': /* ^E */
cd39f2b6 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 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 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 502 sv_setpv(sv, errno ? Strerror(errno) : "");
503 } else {
017f25f1 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 510 sv_setpv(sv, os2error(Perl_rc));
511 }
88e89b8a 512#else
22fae026 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 521 else
522 sv_setpv(sv, "");
523 SetLastError(dwErr);
524 }
525#else
65202027 526 sv_setnv(sv, (NV)errno);
28f23441 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 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 553 {
554 dTHR;
3280af22 555 if (PL_lex_state != LEX_NOTPARSING)
d58bf5aa 556 SvOK_off(sv);
3280af22 557 else if (PL_in_eval)
d58bf5aa 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 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 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 586 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
587 sv_setiv(sv, (IV)PL_widesyscalls);
79072805 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 592 I32 s1, t1;
593
a863c7d1 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 601 (s1 = rx->startp[paren]) != -1 &&
602 (t1 = rx->endp[paren]) != -1)
bbce6d69 603 {
cf93c79d 604 i = t1 - s1;
605 s = rx->subbeg + s1;
13f57bf8 606 getrx:
748a9306 607 if (i >= 0) {
13f57bf8 608 bool was_tainted;
3280af22 609 if (PL_tainting) {
610 was_tainted = PL_tainted;
611 PL_tainted = FALSE;
13f57bf8 612 }
cf93c79d 613 sv_setpvn(sv, s, i);
7e2040f0 614 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
615 SvUTF8_on(sv);
616 else
617 SvUTF8_off(sv);
3280af22 618 if (PL_tainting)
619 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
748a9306 620 break;
621 }
79072805 622 }
79072805 623 }
3280af22 624 sv_setsv(sv,&PL_sv_undef);
79072805 625 break;
626 case '+':
3280af22 627 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
d9f97599 628 paren = rx->lastparen;
a0d0e21e 629 if (paren)
630 goto getparen;
79072805 631 }
3280af22 632 sv_setsv(sv,&PL_sv_undef);
79072805 633 break;
634 case '`':
3280af22 635 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 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 642 break;
643 case '\'':
3280af22 644 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d 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 652 break;
653 case '.':
654#ifndef lint
3280af22 655 if (GvIO(PL_last_in_gv)) {
656 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
79072805 657 }
658#endif
659 break;
660 case '?':
809a5acc 661 {
809a5acc 662 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 663#ifdef COMPLEX_STATUS
6b88bc9c 664 LvTARGOFF(sv) = PL_statusvalue;
665 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 666#endif
809a5acc 667 }
79072805 668 break;
669 case '^':
3280af22 670 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805 671 if (s)
672 sv_setpv(sv,s);
673 else {
3280af22 674 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805 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 682 sv_setpv(sv,s);
683 break;
684#ifndef lint
685 case '=':
3280af22 686 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805 687 break;
688 case '-':
3280af22 689 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805 690 break;
691 case '%':
3280af22 692 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805 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 701 break;
702 case '|':
3280af22 703 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805 704 break;
705 case ',':
3280af22 706 sv_setpvn(sv,PL_ofs,PL_ofslen);
79072805 707 break;
708 case '\\':
3280af22 709 sv_setpvn(sv,PL_ors,PL_orslen);
79072805 710 break;
711 case '#':
3280af22 712 sv_setpv(sv,PL_ofmt);
79072805 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 719 {
720 int saveerrno = errno;
65202027 721 sv_setnv(sv, (NV)errno);
88e89b8a 722#ifdef OS2
ed344e4f 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 728 errno = saveerrno;
729 }
730#endif
946ec16e 731 SvNOK_on(sv); /* what a wonderful hack! */
79072805 732 break;
733 case '<':
3280af22 734 sv_setiv(sv, (IV)PL_uid);
79072805 735 break;
736 case '>':
3280af22 737 sv_setiv(sv, (IV)PL_euid);
79072805 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 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 757 }
758#endif
29355cf7 759 SvIOK_on(sv); /* what a wonderful hack! */
79072805 760 break;
761 case '*':
762 break;
cd39f2b6 763#ifndef MACOS_TRADITIONAL
79072805 764 case '0':
765 break;
cd39f2b6 766#endif
a863c7d1 767#ifdef USE_THREADS
768 case '@':
38a03e6e 769 sv_setsv(sv, thr->errsv);
a863c7d1 770 break;
771#endif /* USE_THREADS */
79072805 772 }
a0d0e21e 773 return 0;
79072805 774}
775
776int
864dbfa3 777Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805 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 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 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 805 }
806#endif
1e422769 807
39e571d4 808#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805 809 /* And you'll never guess what the dog had */
810 /* in its mouth... */
3280af22 811 if (PL_tainting) {
1e422769 812 MgTAINTEDDIR_off(mg);
813#ifdef VMS
5aabfad6 814 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 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 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 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 852 return 0;
853 }
463ee0b2 854 }
79072805 855 }
856 }
39e571d4 857#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 858
79072805 859 return 0;
860}
861
862int
864dbfa3 863Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 864{
2d8e6c8d 865 STRLEN n_a;
866 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83 867 return 0;
868}
869
88e89b8a 870int
864dbfa3 871Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a 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 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 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 898# ifdef PERL_IMPLICIT_SYS
899 PerlEnv_clearenv();
900# else
901# ifdef WIN32
3e3baf6d 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 912 }
913 else if ((len = strlen(cur)))
914 cur += len+1;
915 }
916 FreeEnvironmentStrings(envv);
7766f137 917# else
d308986b 918# ifdef __CYGWIN__
2c2d71f5 919 I32 i;
920 for (i = 0; environ[i]; i++)
d702ae42 921 safesysfree(environ[i]);
7766f137 922# else
923# ifndef PERL_USE_SAFE_PUTENV
66b1d557 924 I32 i;
925
3280af22 926 if (environ == PL_origenviron)
f2517201 927 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557 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 934 environ[0] = Nullch;
935
7766f137 936# endif /* WIN32 */
937# endif /* PERL_IMPLICIT_SYS */
f2517201 938#endif /* VMS */
3e3baf6d 939 return 0;
66b1d557 940}
941
942int
864dbfa3 943Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 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 950 if(PL_psig_ptr[i])
951 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 952 else {
ff68c719 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 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 961 SvTEMP_off(sv);
962 }
963 }
964 return 0;
965}
966int
864dbfa3 967Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 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 974 if(PL_psig_ptr[i]) {
975 SvREFCNT_dec(PL_psig_ptr[i]);
976 PL_psig_ptr[i]=0;
88e89b8a 977 }
22c35a8c 978 if(PL_psig_name[i]) {
979 SvREFCNT_dec(PL_psig_name[i]);
980 PL_psig_name[i]=0;
88e89b8a 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 990 register char *s;
991 I32 i;
748a9306 992 SV** svp;
e72dc28c 993 STRLEN len;
a0d0e21e 994
e72dc28c 995 s = MgPV(mg,len);
748a9306 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 1004 if (*svp) {
1005 SvREFCNT_dec(*svp);
1006 *svp = 0;
1007 }
748a9306 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 1014 return 0;
1015 }
22c35a8c 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 1026 else
1027 *svp = SvREFCNT_inc(sv);
a0d0e21e 1028 return 0;
1029 }
e72dc28c 1030 s = SvPV_force(sv,len);
748a9306 1031 if (strEQ(s,"IGNORE")) {
1032 if (i)
ff68c719 1033 (void)rsignal(i, SIG_IGN);
748a9306 1034 else
1035 *svp = 0;
1036 }
1037 else if (strEQ(s,"DEFAULT") || !*s) {
1038 if (i)
ff68c719 1039 (void)rsignal(i, SIG_DFL);
748a9306 1040 else
1041 *svp = 0;
1042 }
79072805 1043 else {
5aabfad6 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 1053 else
1054 *svp = SvREFCNT_inc(sv);
79072805 1055 }
1056 return 0;
1057}
1058
1059int
864dbfa3 1060Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1061{
3280af22 1062 PL_sub_generation++;
463ee0b2 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 1072 return 0;
1073}
463ee0b2 1074
946ec16e 1075int
864dbfa3 1076Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951 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 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 1100{
1101 if (LvTARG(sv)) {
1102 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e 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 1110{
1111 dSP;
463ee0b2 1112
924508f0 1113 PUSHMARK(SP);
1114 EXTEND(SP, n);
33c27489 1115 PUSHs(SvTIED_obj(sv, mg));
93965878 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 1121 PUSHs((SV*)mg->mg_ptr);
1122 }
1123 else if (mg->mg_type == 'p') {
565764a8 1124 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878 1125 }
1126 }
1127 if (n > 2) {
1128 PUSHs(val);
88e89b8a 1129 }
463ee0b2 1130 PUTBACK;
1131
864dbfa3 1132 return call_method(meth, flags);
946ec16e 1133}
1134
76e3520e 1135STATIC int
cea2e8a9 1136S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e 1137{
1138 dSP;
463ee0b2 1139
a0d0e21e 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 1149 FREETMPS;
1150 LEAVE;
1151 return 0;
1152}
463ee0b2 1153
a0d0e21e 1154int
864dbfa3 1155Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1156{
1157 magic_methpack(sv,mg,"FETCH");
1158 if (mg->mg_ptr)
1159 mg->mg_flags |= MGf_GSKIP;
463ee0b2 1160 return 0;
1161}
1162
1163int
864dbfa3 1164Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d 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 1172 return 0;
1173}
1174
1175int
864dbfa3 1176Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1177{
a0d0e21e 1178 return magic_methpack(sv,mg,"DELETE");
1179}
463ee0b2 1180
93965878 1181
1182U32
864dbfa3 1183Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
93965878 1184{
e336de0d 1185 dSP;
93965878 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 1196 FREETMPS;
1197 LEAVE;
1198 return retval;
1199}
1200
cea2e8a9 1201int
1202Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 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 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 1222
1223 ENTER;
a0d0e21e 1224 SAVETMPS;
e788e7d3 1225 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1226 PUSHMARK(SP);
1227 EXTEND(SP, 2);
33c27489 1228 PUSHs(SvTIED_obj(sv, mg));
463ee0b2 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 1237 FREETMPS;
1238 LEAVE;
79072805 1239 return 0;
1240}
1241
1242int
864dbfa3 1243Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 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 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 1264 else if (ckWARN_d(WARN_INTERNAL))
1265 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
79072805 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 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 1282 return 0;
1283}
1284
1285int
864dbfa3 1286Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 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 1296 sv_pos_b2u(lsv, &i);
1297 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e 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 1307{
1308 SV* lsv = LvTARG(sv);
1309 SSize_t pos;
1310 STRLEN len;
c00206c8 1311 STRLEN ulen = 0;
c485e607 1312 dTHR;
a0d0e21e 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 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 1333 ulen = sv_len_utf8(lsv);
1334 if (ulen)
1335 len = ulen;
a0ed51b3 1336 }
1337
a0d0e21e 1338 if (pos < 0) {
1339 pos += len;
1340 if (pos < 0)
1341 pos = 0;
1342 }
1343 else if (pos > len)
1344 pos = len;
a0ed51b3 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 1355 return 0;
1356}
1357
1358int
864dbfa3 1359Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1360{
8646b087 1361 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1362 SvFAKE_off(sv);
946ec16e 1363 gv_efullname3(sv,((GV*)sv), "*");
8646b087 1364 SvFAKE_on(sv);
1365 }
1366 else
946ec16e 1367 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805 1368 return 0;
1369}
1370
1371int
864dbfa3 1372Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1373{
1374 register char *s;
1375 GV* gv;
2d8e6c8d 1376 STRLEN n_a;
79072805 1377
1378 if (!SvOK(sv))
1379 return 0;
2d8e6c8d 1380 s = SvPV(sv, n_a);
79072805 1381 if (*s == '*' && s[1])
1382 s++;
85e6fe83 1383 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805 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 1389 return 0;
1390}
1391
1392int
864dbfa3 1393Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951 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 1412 STRLEN len;
1413 char *tmps = SvPV(sv,len);
1414 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805 1415 return 0;
1416}
1417
1418int
864dbfa3 1419Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1420{
a863c7d1 1421 dTHR;
565764a8 1422 TAINT_IF((mg->mg_len & 1) ||
1423 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2 1424 return 0;
1425}
1426
1427int
864dbfa3 1428Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1429{
11343788 1430 dTHR;
3280af22 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 1441 return 0;
1442}
1443
1444int
864dbfa3 1445Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951 1446{
1447 SV *lsv = LvTARG(sv);
6ff81951 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 1455 return 0;
1456}
1457
1458int
864dbfa3 1459Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805 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 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 1482 }
1483 else {
3c78fafa 1484 AV* av = (AV*)LvTARG(sv);
68dc0745 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 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 1499 else
1500 targ = LvTARG(sv);
3280af22 1501 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc 1502 return 0;
1503}
1504
1505int
864dbfa3 1506Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc 1507{
1508 if (LvTARGLEN(sv))
68dc0745 1509 vivify_defelem(sv);
1510 if (LvTARG(sv)) {
5f05dabc 1511 sv_setsv(LvTARG(sv), sv);
68dc0745 1512 SvSETMAGIC(LvTARG(sv));
1513 }
5f05dabc 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 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 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 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 1542 else {
1543 AV* av = (AV*)LvTARG(sv);
5aabfad6 1544 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745 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 1550 }
1551 }
3e3baf6d 1552 (void)SvREFCNT_inc(value);
68dc0745 1553 SvREFCNT_dec(LvTARG(sv));
1554 LvTARG(sv) = value;
71be2cbc 1555 LvTARGLEN(sv) = 0;
68dc0745 1556 SvREFCNT_dec(mg->mg_obj);
1557 mg->mg_obj = Nullsv;
1558 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc 1559}
1560
1561int
864dbfa3 1562Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5 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 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 1587 return 0;
1588}
1589
1590int
864dbfa3 1591Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1592{
463ee0b2 1593 sv_unmagic(sv, 'B');
79072805 1594 SvVALID_off(sv);
1595 return 0;
1596}
1597
1598int
864dbfa3 1599Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 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 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 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 1627{
1628 /*
838b5b74 1629 * RenE<eacute> Descartes said "I think not."
bbce6d69 1630 * and vanished with a faint plop.
1631 */
7a4c00b4 1632 if (mg->mg_ptr) {
1633 Safefree(mg->mg_ptr);
1634 mg->mg_ptr = NULL;
565764a8 1635 mg->mg_len = -1;
7a4c00b4 1636 }
bbce6d69 1637 return 0;
1638}
7a4c00b4 1639#endif /* USE_LOCALE_COLLATE */
bbce6d69 1640
1641int
864dbfa3 1642Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 1643{
11343788 1644 dTHR;
79072805 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 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 1658 DEBUG_x(dump_all());
1659 break;
28f23441 1660 case '\005': /* ^E */
cd39f2b6 1661#ifdef MACOS_TRADITIONAL
1662 gLastMacOSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 1663#else
cd39f2b6 1664# ifdef VMS
1665 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 1666# else
cd39f2b6 1667# ifdef WIN32
1668 SetLastError( SvIV(sv) );
1669# else
1670# ifndef OS2
f86702cc 1671 /* will anyone ever use this? */
1672 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 1673# endif
048c1ddf 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 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 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 1702 if (PL_perldb && !PL_DBsingle)
1703 init_debugger();
79072805 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 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 1721 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e 1722 if (!SvPOK(sv) && PL_localizing) {
1723 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1724 PL_compiling.cop_warnings = WARN_NONE;
1725 break;
1726 }
4438c4b7 1727 if (memEQ(SvPVX(sv), WARN_ALLstring, WARNsize)) {
1728 PL_compiling.cop_warnings = WARN_ALL;
1729 PL_dowarn |= G_WARN_ONCE ;
1730 }
1731 else if (memEQ(SvPVX(sv), WARN_NONEstring, WARNsize))
1732 PL_compiling.cop_warnings = WARN_NONE;
1733 else {
1734 if (specialWARN(PL_compiling.cop_warnings))
1735 PL_compiling.cop_warnings = newSVsv(sv) ;
1736 else
1737 sv_setsv(PL_compiling.cop_warnings, sv);
1738 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1739 PL_dowarn |= G_WARN_ONCE ;
1740 }
1741 }
971a9dd3 1742 }
46487f74 1743 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1744 PL_widesyscalls = SvTRUE(sv);
79072805 1745 break;
1746 case '.':
3280af22 1747 if (PL_localizing) {
1748 if (PL_localizing == 1)
7766f137 1749 SAVESPTR(PL_last_in_gv);
748a9306 1750 }
3280af22 1751 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1752 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
79072805 1753 break;
1754 case '^':
3280af22 1755 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1756 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1757 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 1758 break;
1759 case '~':
3280af22 1760 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1761 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1762 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805 1763 break;
1764 case '=':
3280af22 1765 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1766 break;
1767 case '-':
3280af22 1768 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1769 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1770 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805 1771 break;
1772 case '%':
3280af22 1773 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1774 break;
1775 case '|':
4b65379b 1776 {
3280af22 1777 IO *io = GvIOp(PL_defoutgv);
4b65379b 1778 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1779 IoFLAGS(io) &= ~IOf_FLUSH;
1780 else {
1781 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1782 PerlIO *ofp = IoOFP(io);
1783 if (ofp)
1784 (void)PerlIO_flush(ofp);
1785 IoFLAGS(io) |= IOf_FLUSH;
1786 }
1787 }
79072805 1788 }
1789 break;
1790 case '*':
463ee0b2 1791 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
3280af22 1792 PL_multiline = (i != 0);
79072805 1793 break;
1794 case '/':
3280af22 1795 SvREFCNT_dec(PL_nrs);
1796 PL_nrs = newSVsv(sv);
1797 SvREFCNT_dec(PL_rs);
1798 PL_rs = SvREFCNT_inc(PL_nrs);
79072805 1799 break;
1800 case '\\':
3280af22 1801 if (PL_ors)
1802 Safefree(PL_ors);
009c130f 1803 if (SvOK(sv) || SvGMAGICAL(sv)) {
1804 s = SvPV(sv,PL_orslen);
1805 PL_ors = savepvn(s,PL_orslen);
1806 }
e3c19b7b 1807 else {
3280af22 1808 PL_ors = Nullch;
1809 PL_orslen = 0;
e3c19b7b 1810 }
79072805 1811 break;
1812 case ',':
3280af22 1813 if (PL_ofs)
1814 Safefree(PL_ofs);
1815 PL_ofs = savepv(SvPV(sv, PL_ofslen));
79072805 1816 break;
1817 case '#':
3280af22 1818 if (PL_ofmt)
1819 Safefree(PL_ofmt);
2d8e6c8d 1820 PL_ofmt = savepv(SvPV(sv,len));
79072805 1821 break;
1822 case '[':
3280af22 1823 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1824 break;
1825 case '?':
ff0cee69 1826#ifdef COMPLEX_STATUS
6b88bc9c 1827 if (PL_localizing == 2) {
1828 PL_statusvalue = LvTARGOFF(sv);
1829 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69 1830 }
1831 else
1832#endif
1833#ifdef VMSISH_STATUS
1834 if (VMSISH_STATUS)
1835 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1836 else
1837#endif
1838 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805 1839 break;
1840 case '!':
78987ded 1841 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1842 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805 1843 break;
1844 case '<':
3280af22 1845 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1846 if (PL_delaymagic) {
1847 PL_delaymagic |= DM_RUID;
79072805 1848 break; /* don't do magic till later */
1849 }
1850#ifdef HAS_SETRUID
b28d0864 1851 (void)setruid((Uid_t)PL_uid);
79072805 1852#else
1853#ifdef HAS_SETREUID
3280af22 1854 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 1855#else
85e6fe83 1856#ifdef HAS_SETRESUID
b28d0864 1857 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 1858#else
b28d0864 1859 if (PL_uid == PL_euid) /* special case $< = $> */
1860 (void)PerlProc_setuid(PL_uid);
a0d0e21e 1861 else {
d8eceb89 1862 PL_uid = PerlProc_getuid();
cea2e8a9 1863 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 1864 }
79072805 1865#endif
1866#endif
85e6fe83 1867#endif
d8eceb89 1868 PL_uid = PerlProc_getuid();
3280af22 1869 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1870 break;
1871 case '>':
3280af22 1872 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1873 if (PL_delaymagic) {
1874 PL_delaymagic |= DM_EUID;
79072805 1875 break; /* don't do magic till later */
1876 }
1877#ifdef HAS_SETEUID
3280af22 1878 (void)seteuid((Uid_t)PL_euid);
79072805 1879#else
1880#ifdef HAS_SETREUID
b28d0864 1881 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83 1882#else
1883#ifdef HAS_SETRESUID
6b88bc9c 1884 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 1885#else
b28d0864 1886 if (PL_euid == PL_uid) /* special case $> = $< */
1887 PerlProc_setuid(PL_euid);
a0d0e21e 1888 else {
e8ee3774 1889 PL_euid = PerlProc_geteuid();
cea2e8a9 1890 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 1891 }
79072805 1892#endif
1893#endif
85e6fe83 1894#endif
d8eceb89 1895 PL_euid = PerlProc_geteuid();
3280af22 1896 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1897 break;
1898 case '(':
3280af22 1899 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1900 if (PL_delaymagic) {
1901 PL_delaymagic |= DM_RGID;
79072805 1902 break; /* don't do magic till later */
1903 }
1904#ifdef HAS_SETRGID
b28d0864 1905 (void)setrgid((Gid_t)PL_gid);
79072805 1906#else
1907#ifdef HAS_SETREGID
3280af22 1908 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83 1909#else
1910#ifdef HAS_SETRESGID
b28d0864 1911 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 1912#else
b28d0864 1913 if (PL_gid == PL_egid) /* special case $( = $) */
1914 (void)PerlProc_setgid(PL_gid);
748a9306 1915 else {
d8eceb89 1916 PL_gid = PerlProc_getgid();
cea2e8a9 1917 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 1918 }
79072805 1919#endif
1920#endif
85e6fe83 1921#endif
d8eceb89 1922 PL_gid = PerlProc_getgid();
3280af22 1923 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1924 break;
1925 case ')':
5cd24f17 1926#ifdef HAS_SETGROUPS
1927 {
2d8e6c8d 1928 char *p = SvPV(sv, len);
5cd24f17 1929 Groups_t gary[NGROUPS];
1930
5cd24f17 1931 while (isSPACE(*p))
1932 ++p;
2d4389e4 1933 PL_egid = Atol(p);
5cd24f17 1934 for (i = 0; i < NGROUPS; ++i) {
1935 while (*p && !isSPACE(*p))
1936 ++p;
1937 while (isSPACE(*p))
1938 ++p;
1939 if (!*p)
1940 break;
2d4389e4 1941 gary[i] = Atol(p);
5cd24f17 1942 }
8cc95fdb 1943 if (i)
1944 (void)setgroups(i, gary);
5cd24f17 1945 }
1946#else /* HAS_SETGROUPS */
b28d0864 1947 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1948#endif /* HAS_SETGROUPS */
3280af22 1949 if (PL_delaymagic) {
1950 PL_delaymagic |= DM_EGID;
79072805 1951 break; /* don't do magic till later */
1952 }
1953#ifdef HAS_SETEGID
3280af22 1954 (void)setegid((Gid_t)PL_egid);
79072805 1955#else
1956#ifdef HAS_SETREGID
b28d0864 1957 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83 1958#else
1959#ifdef HAS_SETRESGID
b28d0864 1960 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 1961#else
b28d0864 1962 if (PL_egid == PL_gid) /* special case $) = $( */
1963 (void)PerlProc_setgid(PL_egid);
748a9306 1964 else {
d8eceb89 1965 PL_egid = PerlProc_getegid();
cea2e8a9 1966 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 1967 }
79072805 1968#endif
1969#endif
85e6fe83 1970#endif
d8eceb89 1971 PL_egid = PerlProc_getegid();
3280af22 1972 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805 1973 break;
1974 case ':':
2d8e6c8d 1975 PL_chopset = SvPV_force(sv,len);
79072805 1976 break;
cd39f2b6 1977#ifndef MACOS_TRADITIONAL
79072805 1978 case '0':
3280af22 1979 if (!PL_origalen) {
1980 s = PL_origargv[0];
79072805 1981 s += strlen(s);
1982 /* See if all the arguments are contiguous in memory */
3280af22 1983 for (i = 1; i < PL_origargc; i++) {
1984 if (PL_origargv[i] == s + 1
fb73857a 1985#ifdef OS2
6b88bc9c 1986 || PL_origargv[i] == s + 2
fb73857a 1987#endif
1988 )
379c4362 1989 {
1990 ++s;
1991 s += strlen(s); /* this one is ok too */
1992 }
fb73857a 1993 else
1994 break;
79072805 1995 }
bbce6d69 1996 /* can grab env area too? */
3280af22 1997 if (PL_origenviron && (PL_origenviron[0] == s + 1
fb73857a 1998#ifdef OS2
6b88bc9c 1999 || (PL_origenviron[0] == s + 9 && (s += 8))
fb73857a 2000#endif
2001 )) {
66b1d557 2002 my_setenv("NoNe SuCh", Nullch);
79072805 2003 /* force copy of environment */
3280af22 2004 for (i = 0; PL_origenviron[i]; i++)
379c4362 2005 if (PL_origenviron[i] == s + 1) {
2006 ++s;
2007 s += strlen(s);
2008 }
fb73857a 2009 else
2010 break;
79072805 2011 }
3280af22 2012 PL_origalen = s - PL_origargv[0];
79072805 2013 }
a0d0e21e 2014 s = SvPV_force(sv,len);
8990e307 2015 i = len;
3280af22 2016 if (i >= PL_origalen) {
2017 i = PL_origalen;
fb73857a 2018 /* don't allow system to limit $0 seen by script */
2019 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
3280af22 2020 Copy(s, PL_origargv[0], i, char);
2021 s = PL_origargv[0]+i;
fb73857a 2022 *s = '\0';
79072805 2023 }
2024 else {
3280af22 2025 Copy(s, PL_origargv[0], i, char);
2026 s = PL_origargv[0]+i;
79072805 2027 *s++ = '\0';
3280af22 2028 while (++i < PL_origalen)
8990e307 2029 *s++ = ' ';
3280af22 2030 s = PL_origargv[0]+i;
2031 for (i = 1; i < PL_origargc; i++)
2032 PL_origargv[i] = Nullch;
79072805 2033 }
2034 break;
cd39f2b6 2035#endif
a863c7d1 2036#ifdef USE_THREADS
2037 case '@':
38a03e6e 2038 sv_setsv(thr->errsv, sv);
a863c7d1 2039 break;
2040#endif /* USE_THREADS */
79072805 2041 }
2042 return 0;
2043}
2044
f93b4edd 2045#ifdef USE_THREADS
2046int
864dbfa3 2047Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
f93b4edd 2048{
2049 dTHR;
b900a521 2050 DEBUG_S(PerlIO_printf(Perl_debug_log,
2051 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2052 PTR2UV(thr), PTR2UV(sv));)
f93b4edd 2053 if (MgOWNER(mg))
cea2e8a9 2054 Perl_croak(aTHX_ "panic: magic_mutexfree");
f93b4edd 2055 MUTEX_DESTROY(MgMUTEXP(mg));
2056 COND_DESTROY(MgCONDP(mg));
2057 return 0;
2058}
2059#endif /* USE_THREADS */
2060
79072805 2061I32
864dbfa3 2062Perl_whichsig(pTHX_ char *sig)
79072805 2063{
2064 register char **sigv;
2065
22c35a8c 2066 for (sigv = PL_sig_name+1; *sigv; sigv++)
79072805 2067 if (strEQ(sig,*sigv))
22c35a8c 2068 return PL_sig_num[sigv - PL_sig_name];
79072805 2069#ifdef SIGCLD
2070 if (strEQ(sig,"CHLD"))
2071 return SIGCLD;
2072#endif
2073#ifdef SIGCHLD
2074 if (strEQ(sig,"CLD"))
2075 return SIGCHLD;
2076#endif
2077 return 0;
2078}
2079
84902520 2080static SV* sig_sv;
2081
ecfc5424 2082Signal_t
cea2e8a9 2083Perl_sighandler(int sig)
79072805 2084{
cea2e8a9 2085 dTHX;
79072805 2086 dSP;
00d579c5 2087 GV *gv = Nullgv;
a0d0e21e 2088 HV *st;
3280af22 2089 SV *sv, *tSv = PL_Sv;
00d579c5 2090 CV *cv = Nullcv;
533c011a 2091 OP *myop = PL_op;
84902520 2092 U32 flags = 0;
3280af22 2093 I32 o_save_i = PL_savestack_ix, type;
2094 XPV *tXpv = PL_Xpv;
84902520 2095
3280af22 2096 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2097 flags |= 1;
3280af22 2098 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2099 flags |= 4;
3280af22 2100 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 2101 flags |= 8;
3280af22 2102 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520 2103 flags |= 16;
2104
22c35a8c 2105 if (!PL_psig_ptr[sig])
cea2e8a9 2106 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
22c35a8c 2107 PL_sig_name[sig]);
ff0cee69 2108
84902520 2109 /* Max number of items pushed there is 3*n or 4. We cannot fix
2110 infinity, so we fix 4 (in fact 5): */
2111 if (flags & 1) {
3280af22 2112 PL_savestack_ix += 5; /* Protect save in progress. */
2113 o_save_i = PL_savestack_ix;
c76ac1ee 2114 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2115 }
2116 if (flags & 4)
3280af22 2117 PL_markstack_ptr++; /* Protect mark. */
84902520 2118 if (flags & 8) {
3280af22 2119 PL_retstack_ix++;
2120 PL_retstack[PL_retstack_ix] = NULL;
84902520 2121 }
2122 if (flags & 16)
3280af22 2123 PL_scopestack_ix += 1;
84902520 2124 /* sv_2cv is too complicated, try a simpler variant first: */
22c35a8c 2125 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2126 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2127 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2128
a0d0e21e 2129 if (!cv || !CvROOT(cv)) {
599cee73 2130 if (ckWARN(WARN_SIGNAL))
cea2e8a9 2131 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2132 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5 2133 : ((cv && CvGV(cv))
2134 ? GvENAME(CvGV(cv))
2135 : "__ANON__")));
2136 goto cleanup;
79072805 2137 }
2138
22c35a8c 2139 if(PL_psig_name[sig]) {
2140 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2141 flags |= 64;
2142 sig_sv = sv;
2143 } else {
ff0cee69 2144 sv = sv_newmortal();
22c35a8c 2145 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2146 }
e336de0d 2147
e788e7d3 2148 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2149 PUSHMARK(SP);
79072805 2150 PUSHs(sv);
79072805 2151 PUTBACK;
a0d0e21e 2152
864dbfa3 2153 call_sv((SV*)cv, G_DISCARD);
79072805 2154
d3acc0f7 2155 POPSTACK;
00d579c5 2156cleanup:
84902520 2157 if (flags & 1)
3280af22 2158 PL_savestack_ix -= 8; /* Unprotect save in progress. */
84902520 2159 if (flags & 4)
3280af22 2160 PL_markstack_ptr--;
84902520 2161 if (flags & 8)
3280af22 2162 PL_retstack_ix--;
84902520 2163 if (flags & 16)
3280af22 2164 PL_scopestack_ix -= 1;
84902520 2165 if (flags & 64)
2166 SvREFCNT_dec(sv);
533c011a 2167 PL_op = myop; /* Apparently not needed... */
84902520 2168
3280af22 2169 PL_Sv = tSv; /* Restore global temporaries. */
2170 PL_Xpv = tXpv;
79072805 2171 return;
2172}
4e35701f 2173
2174
51371543 2175#ifdef PERL_OBJECT
51371543 2176#include "XSUB.h"
2177#endif
2178
2179static void
2180restore_magic(pTHXo_ void *p)
2181{
2182 dTHR;
48944bdf 2183 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543 2184 SV* sv = mgs->mgs_sv;
2185
2186 if (!sv)
2187 return;
2188
2189 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2190 {
2191 if (mgs->mgs_flags)
2192 SvFLAGS(sv) |= mgs->mgs_flags;
2193 else
2194 mg_magical(sv);
2195 if (SvGMAGICAL(sv))
2196 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2197 }
2198
2199 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2200
2201 /* If we're still on top of the stack, pop us off. (That condition
2202 * will be satisfied if restore_magic was called explicitly, but *not*
2203 * if it's being called via leave_scope.)
2204 * The reason for doing this is that otherwise, things like sv_2cv()
2205 * may leave alloc gunk on the savestack, and some code
2206 * (e.g. sighandler) doesn't expect that...
2207 */
2208 if (PL_savestack_ix == mgs->mgs_ss_ix)
2209 {
2210 I32 popval = SSPOPINT;
c76ac1ee 2211 assert(popval == SAVEt_DESTRUCTOR_X);
51371543 2212 PL_savestack_ix -= 2;
2213 popval = SSPOPINT;
2214 assert(popval == SAVEt_ALLOC);
2215 popval = SSPOPINT;
2216 PL_savestack_ix -= popval;
2217 }
2218
2219}
2220
2221static void
2222unwind_handler_stack(pTHXo_ void *p)
2223{
2224 dTHR;
2225 U32 flags = *(U32*)p;
2226
2227 if (flags & 1)
2228 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2229 /* cxstack_ix-- Not needed, die already unwound it. */
2230 if (flags & 64)
2231 SvREFCNT_dec(sig_sv);
2232}