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