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