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