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