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