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