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