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