This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make die/warn and other diagnostics go to wherever STDERR happens
[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
51371543 51 SAVEDESTRUCTOR(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) {
76e3520e 70 if ((vtbl->svt_get != NULL) && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
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
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;
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
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 }
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
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;
76e3520e 150 if (vtbl && (vtbl->svt_len != NULL)) {
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;
565764a8 174 if (vtbl && (vtbl->svt_len != NULL)) {
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
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
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;
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
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
PP
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
PP
645 errno = saveerrno;
646 }
647#endif
946ec16e 648 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
649 break;
650 case '<':
3280af22 651 sv_setiv(sv, (IV)PL_uid);
79072805
LW
652 break;
653 case '>':
3280af22 654 sv_setiv(sv, (IV)PL_euid);
79072805
LW
655 break;
656 case '(':
3280af22 657 sv_setiv(sv, (IV)PL_gid);
cea2e8a9 658 Perl_sv_setpvf(aTHX_ sv, "%Vd", (IV)PL_gid);
79072805
LW
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
LW
670 }
671#endif
29355cf7 672 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
673 break;
674 case '*':
675 break;
676 case '0':
677 break;
a863c7d1
MB
678#ifdef USE_THREADS
679 case '@':
38a03e6e 680 sv_setsv(sv, thr->errsv);
a863c7d1
MB
681 break;
682#endif /* USE_THREADS */
79072805 683 }
a0d0e21e 684 return 0;
79072805
LW
685}
686
687int
864dbfa3 688Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
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
LW
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
LW
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
LW
716 }
717#endif
1e422769 718
39e571d4 719#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
720 /* And you'll never guess what the dog had */
721 /* in its mouth... */
3280af22 722 if (PL_tainting) {
1e422769
PP
723 MgTAINTEDDIR_off(mg);
724#ifdef VMS
5aabfad6 725 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
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
LW
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
MB
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
PP
763 return 0;
764 }
463ee0b2 765 }
79072805
LW
766 }
767 }
39e571d4 768#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 769
79072805
LW
770 return 0;
771}
772
773int
864dbfa3 774Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 775{
2d8e6c8d
GS
776 STRLEN n_a;
777 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83
LW
778 return 0;
779}
780
88e89b8a 781int
864dbfa3 782Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a
PP
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
PP
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
PP
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
TB
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
TB
820 }
821 else if ((len = strlen(cur)))
822 cur += len+1;
823 }
824 FreeEnvironmentStrings(envv);
f2517201 825# else
2c2d71f5
JH
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
HM
832 I32 i;
833
3280af22 834 if (environ == PL_origenviron)
f2517201 835 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
836 else
837 for (i = 0; environ[i]; i++)
f2517201 838 safesysfree(environ[i]);
2c2d71f5
JH
839# endif /* PERL_USE_SAFE_PUTENV */
840# endif /* CYGWIN */
f2517201 841
66b1d557
HM
842 environ[0] = Nullch;
843
f2517201
GS
844# endif /* WIN32 */
845#endif /* VMS */
3e3baf6d 846 return 0;
66b1d557
HM
847}
848
849int
864dbfa3 850Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
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
GS
857 if(PL_psig_ptr[i])
858 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 859 else {
ff68c719
PP
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
PP
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
PP
868 SvTEMP_off(sv);
869 }
870 }
871 return 0;
872}
873int
864dbfa3 874Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
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
GS
881 if(PL_psig_ptr[i]) {
882 SvREFCNT_dec(PL_psig_ptr[i]);
883 PL_psig_ptr[i]=0;
88e89b8a 884 }
22c35a8c
GS
885 if(PL_psig_name[i]) {
886 SvREFCNT_dec(PL_psig_name[i]);
887 PL_psig_name[i]=0;
88e89b8a
PP
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
LW
897 register char *s;
898 I32 i;
748a9306 899 SV** svp;
e72dc28c 900 STRLEN len;
a0d0e21e 901
e72dc28c 902 s = MgPV(mg,len);
748a9306
LW
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
cea2e8a9 909 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 910 i = 0;
4633a7c4
LW
911 if (*svp) {
912 SvREFCNT_dec(*svp);
913 *svp = 0;
914 }
748a9306
LW
915 }
916 else {
917 i = whichsig(s); /* ...no, a brick */
918 if (!i) {
599cee73 919 if (ckWARN(WARN_SIGNAL) || strEQ(s,"ALARM"))
cea2e8a9 920 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
748a9306
LW
921 return 0;
922 }
22c35a8c
GS
923 SvREFCNT_dec(PL_psig_name[i]);
924 SvREFCNT_dec(PL_psig_ptr[i]);
925 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 926 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 927 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 928 SvREADONLY_on(PL_psig_name[i]);
748a9306 929 }
a0d0e21e 930 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 931 if (i)
3280af22 932 (void)rsignal(i, PL_sighandlerp);
748a9306
LW
933 else
934 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
935 return 0;
936 }
e72dc28c 937 s = SvPV_force(sv,len);
748a9306
LW
938 if (strEQ(s,"IGNORE")) {
939 if (i)
ff68c719 940 (void)rsignal(i, SIG_IGN);
748a9306
LW
941 else
942 *svp = 0;
943 }
944 else if (strEQ(s,"DEFAULT") || !*s) {
945 if (i)
ff68c719 946 (void)rsignal(i, SIG_DFL);
748a9306
LW
947 else
948 *svp = 0;
949 }
79072805 950 else {
5aabfad6
PP
951 /*
952 * We should warn if HINT_STRICT_REFS, but without
953 * access to a known hint bit in a known OP, we can't
954 * tell whether HINT_STRICT_REFS is in force or not.
955 */
46fc3d4c 956 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 957 sv_insert(sv, 0, 0, "main::", 6);
748a9306 958 if (i)
3280af22 959 (void)rsignal(i, PL_sighandlerp);
748a9306
LW
960 else
961 *svp = SvREFCNT_inc(sv);
79072805
LW
962 }
963 return 0;
964}
965
966int
864dbfa3 967Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 968{
3280af22 969 PL_sub_generation++;
463ee0b2
LW
970 return 0;
971}
972
973int
864dbfa3 974Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 975{
a0d0e21e 976 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 977 PL_amagic_generation++;
463ee0b2 978
a0d0e21e
LW
979 return 0;
980}
463ee0b2 981
946ec16e 982int
864dbfa3 983Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
984{
985 HV *hv = (HV*)LvTARG(sv);
986 HE *entry;
987 I32 i = 0;
988
989 if (hv) {
990 (void) hv_iterinit(hv);
33c27489 991 if (! SvTIED_mg((SV*)hv, 'P'))
6ff81951
GS
992 i = HvKEYS(hv);
993 else {
994 /*SUPPRESS 560*/
995 while (entry = hv_iternext(hv)) {
996 i++;
997 }
998 }
999 }
1000
1001 sv_setiv(sv, (IV)i);
1002 return 0;
1003}
1004
1005int
864dbfa3 1006Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1007{
1008 if (LvTARG(sv)) {
1009 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1010 }
1011 return 0;
93965878 1012}
946ec16e 1013
e336de0d 1014/* caller is responsible for stack switching/cleanup */
565764a8 1015STATIC int
cea2e8a9 1016S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1017{
1018 dSP;
463ee0b2 1019
924508f0
GS
1020 PUSHMARK(SP);
1021 EXTEND(SP, n);
33c27489 1022 PUSHs(SvTIED_obj(sv, mg));
93965878
NIS
1023 if (n > 1) {
1024 if (mg->mg_ptr) {
565764a8 1025 if (mg->mg_len >= 0)
79cb57f6 1026 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1027 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1028 PUSHs((SV*)mg->mg_ptr);
1029 }
1030 else if (mg->mg_type == 'p') {
565764a8 1031 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1032 }
1033 }
1034 if (n > 2) {
1035 PUSHs(val);
88e89b8a 1036 }
463ee0b2
LW
1037 PUTBACK;
1038
864dbfa3 1039 return call_method(meth, flags);
946ec16e
PP
1040}
1041
76e3520e 1042STATIC int
cea2e8a9 1043S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1044{
1045 dSP;
463ee0b2 1046
a0d0e21e
LW
1047 ENTER;
1048 SAVETMPS;
e788e7d3 1049 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1050
33c27489 1051 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1052 sv_setsv(sv, *PL_stack_sp--);
93965878 1053 }
463ee0b2 1054
d3acc0f7 1055 POPSTACK;
a0d0e21e
LW
1056 FREETMPS;
1057 LEAVE;
1058 return 0;
1059}
463ee0b2 1060
a0d0e21e 1061int
864dbfa3 1062Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1063{
1064 magic_methpack(sv,mg,"FETCH");
1065 if (mg->mg_ptr)
1066 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1067 return 0;
1068}
1069
1070int
864dbfa3 1071Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1072{
1073 dSP;
a60c0954 1074 ENTER;
e788e7d3 1075 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1076 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1077 POPSTACK;
a60c0954 1078 LEAVE;
463ee0b2
LW
1079 return 0;
1080}
1081
1082int
864dbfa3 1083Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1084{
a0d0e21e
LW
1085 return magic_methpack(sv,mg,"DELETE");
1086}
463ee0b2 1087
93965878
NIS
1088
1089U32
864dbfa3 1090Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
93965878 1091{
e336de0d 1092 dSP;
93965878
NIS
1093 U32 retval = 0;
1094
1095 ENTER;
1096 SAVETMPS;
e788e7d3 1097 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1098 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1099 sv = *PL_stack_sp--;
a60c0954 1100 retval = (U32) SvIV(sv)-1;
93965878 1101 }
d3acc0f7 1102 POPSTACK;
93965878
NIS
1103 FREETMPS;
1104 LEAVE;
1105 return retval;
1106}
1107
cea2e8a9
GS
1108int
1109Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1110{
1111 dSP;
463ee0b2 1112
e336de0d 1113 ENTER;
e788e7d3 1114 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1115 PUSHMARK(SP);
33c27489 1116 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1117 PUTBACK;
864dbfa3 1118 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1119 POPSTACK;
a60c0954 1120 LEAVE;
463ee0b2
LW
1121 return 0;
1122}
1123
1124int
864dbfa3 1125Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1126{
463ee0b2 1127 dSP;
a0d0e21e 1128 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1129
1130 ENTER;
a0d0e21e 1131 SAVETMPS;
e788e7d3 1132 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1133 PUSHMARK(SP);
1134 EXTEND(SP, 2);
33c27489 1135 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1136 if (SvOK(key))
1137 PUSHs(key);
1138 PUTBACK;
1139
864dbfa3 1140 if (call_method(meth, G_SCALAR))
3280af22 1141 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1142
d3acc0f7 1143 POPSTACK;
a0d0e21e
LW
1144 FREETMPS;
1145 LEAVE;
79072805
LW
1146 return 0;
1147}
1148
1149int
864dbfa3 1150Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1151{
1152 return magic_methpack(sv,mg,"EXISTS");
1153}
1154
1155int
864dbfa3 1156Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805 1157{
11343788 1158 dTHR;
79072805
LW
1159 OP *o;
1160 I32 i;
1161 GV* gv;
1162 SV** svp;
2d8e6c8d 1163 STRLEN n_a;
79072805 1164
3280af22 1165 gv = PL_DBline;
79072805 1166 i = SvTRUE(sv);
188ea221 1167 svp = av_fetch(GvAV(gv),
2d8e6c8d 1168 atoi(MgPV(mg,n_a)), FALSE);
8990e307 1169 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 1170 o->op_private = i;
0453d815
PM
1171 else if (ckWARN_d(WARN_INTERNAL))
1172 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
79072805
LW
1173 return 0;
1174}
1175
1176int
864dbfa3 1177Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1178{
0f15f207 1179 dTHR;
3280af22 1180 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1181 return 0;
1182}
1183
1184int
864dbfa3 1185Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1186{
0f15f207 1187 dTHR;
3280af22 1188 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1189 return 0;
1190}
1191
1192int
864dbfa3 1193Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1194{
1195 SV* lsv = LvTARG(sv);
1196
1197 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1198 mg = mg_find(lsv, 'g');
565764a8 1199 if (mg && mg->mg_len >= 0) {
0f15f207 1200 dTHR;
a0ed51b3
LW
1201 I32 i = mg->mg_len;
1202 if (IN_UTF8)
1203 sv_pos_b2u(lsv, &i);
1204 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1205 return 0;
1206 }
1207 }
1208 (void)SvOK_off(sv);
1209 return 0;
1210}
1211
1212int
864dbfa3 1213Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1214{
1215 SV* lsv = LvTARG(sv);
1216 SSize_t pos;
1217 STRLEN len;
a0ed51b3 1218 STRLEN ulen;
c485e607 1219 dTHR;
a0d0e21e
LW
1220
1221 mg = 0;
1222
1223 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1224 mg = mg_find(lsv, 'g');
1225 if (!mg) {
1226 if (!SvOK(sv))
1227 return 0;
1228 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1229 mg = mg_find(lsv, 'g');
1230 }
1231 else if (!SvOK(sv)) {
565764a8 1232 mg->mg_len = -1;
a0d0e21e
LW
1233 return 0;
1234 }
1235 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1236
c485e607 1237 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3
LW
1238
1239 if (IN_UTF8) {
1240 ulen = sv_len_utf8(lsv);
1241 if (ulen)
1242 len = ulen;
1243 else
1244 ulen = 0;
1245 }
1246
a0d0e21e
LW
1247 if (pos < 0) {
1248 pos += len;
1249 if (pos < 0)
1250 pos = 0;
1251 }
1252 else if (pos > len)
1253 pos = len;
a0ed51b3
LW
1254
1255 if (ulen) {
1256 I32 p = pos;
1257 sv_pos_u2b(lsv, &p, 0);
1258 pos = p;
1259 }
1260
565764a8 1261 mg->mg_len = pos;
71be2cbc 1262 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1263
79072805
LW
1264 return 0;
1265}
1266
1267int
864dbfa3 1268Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1269{
8646b087
PP
1270 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1271 SvFAKE_off(sv);
946ec16e 1272 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1273 SvFAKE_on(sv);
1274 }
1275 else
946ec16e 1276 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1277 return 0;
1278}
1279
1280int
864dbfa3 1281Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1282{
1283 register char *s;
1284 GV* gv;
2d8e6c8d 1285 STRLEN n_a;
79072805
LW
1286
1287 if (!SvOK(sv))
1288 return 0;
2d8e6c8d 1289 s = SvPV(sv, n_a);
79072805
LW
1290 if (*s == '*' && s[1])
1291 s++;
85e6fe83 1292 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1293 if (sv == (SV*)gv)
1294 return 0;
1295 if (GvGP(sv))
88e89b8a 1296 gp_free((GV*)sv);
79072805 1297 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1298 return 0;
1299}
1300
1301int
864dbfa3 1302Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1303{
1304 STRLEN len;
1305 SV *lsv = LvTARG(sv);
1306 char *tmps = SvPV(lsv,len);
1307 I32 offs = LvTARGOFF(sv);
1308 I32 rem = LvTARGLEN(sv);
1309
1310 if (offs > len)
1311 offs = len;
1312 if (rem + offs > len)
1313 rem = len - offs;
1314 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1315 return 0;
1316}
1317
1318int
864dbfa3 1319Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1320{
8990e307
LW
1321 STRLEN len;
1322 char *tmps = SvPV(sv,len);
1323 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1324 return 0;
1325}
1326
1327int
864dbfa3 1328Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1329{
a863c7d1 1330 dTHR;
565764a8
DL
1331 TAINT_IF((mg->mg_len & 1) ||
1332 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1333 return 0;
1334}
1335
1336int
864dbfa3 1337Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1338{
11343788 1339 dTHR;
3280af22
NIS
1340 if (PL_localizing) {
1341 if (PL_localizing == 1)
565764a8 1342 mg->mg_len <<= 1;
748a9306 1343 else
565764a8 1344 mg->mg_len >>= 1;
a0d0e21e 1345 }
3280af22 1346 else if (PL_tainted)
565764a8 1347 mg->mg_len |= 1;
748a9306 1348 else
565764a8 1349 mg->mg_len &= ~1;
463ee0b2
LW
1350 return 0;
1351}
1352
1353int
864dbfa3 1354Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1355{
1356 SV *lsv = LvTARG(sv);
6ff81951
GS
1357
1358 if (!lsv) {
1359 SvOK_off(sv);
1360 return 0;
1361 }
6ff81951 1362
81e118e0 1363 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1364 return 0;
1365}
1366
1367int
864dbfa3 1368Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1369{
1370 do_vecset(sv); /* XXX slurp this routine */
1371 return 0;
1372}
1373
1374int
864dbfa3 1375Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1376{
71be2cbc 1377 SV *targ = Nullsv;
5f05dabc 1378 if (LvTARGLEN(sv)) {
68dc0745 1379 if (mg->mg_obj) {
74e13ce4
GS
1380 SV *ahv = LvTARG(sv);
1381 if (SvTYPE(ahv) == SVt_PVHV) {
1382 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1383 if (he)
1384 targ = HeVAL(he);
1385 }
1386 else {
1387 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1388 if (svp)
1389 targ = *svp;
1390 }
68dc0745
PP
1391 }
1392 else {
3c78fafa 1393 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1394 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1395 targ = AvARRAY(av)[LvTARGOFF(sv)];
1396 }
3280af22 1397 if (targ && targ != &PL_sv_undef) {
e858de61 1398 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1399 /* somebody else defined it for us */
1400 SvREFCNT_dec(LvTARG(sv));
1401 LvTARG(sv) = SvREFCNT_inc(targ);
1402 LvTARGLEN(sv) = 0;
1403 SvREFCNT_dec(mg->mg_obj);
1404 mg->mg_obj = Nullsv;
1405 mg->mg_flags &= ~MGf_REFCOUNTED;
1406 }
5f05dabc 1407 }
71be2cbc
PP
1408 else
1409 targ = LvTARG(sv);
3280af22 1410 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1411 return 0;
1412}
1413
1414int
864dbfa3 1415Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1416{
1417 if (LvTARGLEN(sv))
68dc0745
PP
1418 vivify_defelem(sv);
1419 if (LvTARG(sv)) {
5f05dabc 1420 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1421 SvSETMAGIC(LvTARG(sv));
1422 }
5f05dabc
PP
1423 return 0;
1424}
1425
71be2cbc 1426void
864dbfa3 1427Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1428{
e858de61 1429 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
74e13ce4
GS
1430 MAGIC *mg;
1431 SV *value = Nullsv;
71be2cbc 1432
68dc0745 1433 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1434 return;
68dc0745 1435 if (mg->mg_obj) {
74e13ce4 1436 SV *ahv = LvTARG(sv);
2d8e6c8d 1437 STRLEN n_a;
74e13ce4 1438 if (SvTYPE(ahv) == SVt_PVHV) {
af7f9c15 1439 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1440 if (he)
1441 value = HeVAL(he);
1442 }
1443 else {
af7f9c15 1444 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
74e13ce4
GS
1445 if (svp)
1446 value = *svp;
1447 }
3280af22 1448 if (!value || value == &PL_sv_undef)
cea2e8a9 1449 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1450 }
68dc0745
PP
1451 else {
1452 AV* av = (AV*)LvTARG(sv);
5aabfad6 1453 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1454 LvTARG(sv) = Nullsv; /* array can't be extended */
1455 else {
1456 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1457 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1458 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1459 }
1460 }
3e3baf6d 1461 (void)SvREFCNT_inc(value);
68dc0745
PP
1462 SvREFCNT_dec(LvTARG(sv));
1463 LvTARG(sv) = value;
71be2cbc 1464 LvTARGLEN(sv) = 0;
68dc0745
PP
1465 SvREFCNT_dec(mg->mg_obj);
1466 mg->mg_obj = Nullsv;
1467 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1468}
1469
1470int
864dbfa3 1471Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1472{
1473 AV *av = (AV*)mg->mg_obj;
1474 SV **svp = AvARRAY(av);
1475 I32 i = AvFILLp(av);
1476 while (i >= 0) {
1477 if (svp[i] && svp[i] != &PL_sv_undef) {
1478 if (!SvWEAKREF(svp[i]))
cea2e8a9 1479 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1480 /* XXX Should we check that it hasn't changed? */
1481 SvRV(svp[i]) = 0;
1482 SvOK_off(svp[i]);
1483 SvWEAKREF_off(svp[i]);
1484 svp[i] = &PL_sv_undef;
1485 }
1486 i--;
1487 }
1488 return 0;
1489}
1490
1491int
864dbfa3 1492Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1493{
565764a8 1494 mg->mg_len = -1;
c6496cc7 1495 SvSCREAM_off(sv);
93a17b20
LW
1496 return 0;
1497}
1498
1499int
864dbfa3 1500Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1501{
463ee0b2 1502 sv_unmagic(sv, 'B');
79072805
LW
1503 SvVALID_off(sv);
1504 return 0;
1505}
1506
1507int
864dbfa3 1508Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff
PP
1509{
1510 sv_unmagic(sv, 'f');
1511 SvCOMPILED_off(sv);
1512 return 0;
1513}
1514
1515int
864dbfa3 1516Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1517{
1518 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1519
1520 if (uf && uf->uf_set)
1521 (*uf->uf_set)(uf->uf_index, sv);
1522 return 0;
1523}
1524
c277df42 1525int
864dbfa3 1526Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
1527{
1528 regexp *re = (regexp *)mg->mg_obj;
1529 ReREFCNT_dec(re);
1530 return 0;
1531}
1532
7a4c00b4 1533#ifdef USE_LOCALE_COLLATE
79072805 1534int
864dbfa3 1535Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
1536{
1537 /*
838b5b74 1538 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
1539 * and vanished with a faint plop.
1540 */
7a4c00b4
PP
1541 if (mg->mg_ptr) {
1542 Safefree(mg->mg_ptr);
1543 mg->mg_ptr = NULL;
565764a8 1544 mg->mg_len = -1;
7a4c00b4 1545 }
bbce6d69
PP
1546 return 0;
1547}
7a4c00b4 1548#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1549
1550int
864dbfa3 1551Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805 1552{
11343788 1553 dTHR;
79072805
LW
1554 register char *s;
1555 I32 i;
8990e307 1556 STRLEN len;
79072805 1557 switch (*mg->mg_ptr) {
748a9306 1558 case '\001': /* ^A */
3280af22 1559 sv_setsv(PL_bodytarget, sv);
748a9306 1560 break;
49460fe6
NIS
1561 case '\003': /* ^C */
1562 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1563 break;
1564
79072805 1565 case '\004': /* ^D */
3280af22 1566 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1567 DEBUG_x(dump_all());
1568 break;
28f23441
PP
1569 case '\005': /* ^E */
1570#ifdef VMS
1571 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1572#else
048c1ddf 1573# ifdef WIN32
22fae026 1574 SetLastError( SvIV(sv) );
048c1ddf
IZ
1575# else
1576# ifndef OS2
f86702cc
PP
1577 /* will anyone ever use this? */
1578 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
048c1ddf
IZ
1579# endif
1580# endif
22fae026 1581#endif
28f23441 1582 break;
79072805 1583 case '\006': /* ^F */
3280af22 1584 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1585 break;
a0d0e21e 1586 case '\010': /* ^H */
3280af22 1587 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 1588 break;
9d116dd7 1589 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
1590 if (PL_inplace)
1591 Safefree(PL_inplace);
79072805 1592 if (SvOK(sv))
2d8e6c8d 1593 PL_inplace = savepv(SvPV(sv,len));
79072805 1594 else
3280af22 1595 PL_inplace = Nullch;
79072805 1596 break;
28f23441 1597 case '\017': /* ^O */
3280af22
NIS
1598 if (PL_osname)
1599 Safefree(PL_osname);
28f23441 1600 if (SvOK(sv))
2d8e6c8d 1601 PL_osname = savepv(SvPV(sv,len));
28f23441 1602 else
3280af22 1603 PL_osname = Nullch;
28f23441 1604 break;
79072805 1605 case '\020': /* ^P */
3280af22 1606 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
1607 if (PL_perldb && !PL_DBsingle)
1608 init_debugger();
79072805
LW
1609 break;
1610 case '\024': /* ^T */
88e89b8a 1611#ifdef BIG_TIME
6b88bc9c 1612 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 1613#else
3280af22 1614 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1615#endif
79072805 1616 break;
4438c4b7
JH
1617 case '\027': /* ^W & $^Warnings */
1618 if (*(mg->mg_ptr+1) == '\0') {
1619 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1620 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1621 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 1622 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 1623 }
599cee73 1624 }
4438c4b7
JH
1625 else if (strEQ(mg->mg_ptr, "\027arnings")) {
1626 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1627 if (memEQ(SvPVX(sv), WARN_ALLstring, WARNsize)) {
1628 PL_compiling.cop_warnings = WARN_ALL;
1629 PL_dowarn |= G_WARN_ONCE ;
1630 }
1631 else if (memEQ(SvPVX(sv), WARN_NONEstring, WARNsize))
1632 PL_compiling.cop_warnings = WARN_NONE;
1633 else {
1634 if (specialWARN(PL_compiling.cop_warnings))
1635 PL_compiling.cop_warnings = newSVsv(sv) ;
1636 else
1637 sv_setsv(PL_compiling.cop_warnings, sv);
1638 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1639 PL_dowarn |= G_WARN_ONCE ;
1640 }
1641 }
1642 }
79072805
LW
1643 break;
1644 case '.':
3280af22
NIS
1645 if (PL_localizing) {
1646 if (PL_localizing == 1)
1647 save_sptr((SV**)&PL_last_in_gv);
748a9306 1648 }
3280af22
NIS
1649 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1650 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
79072805
LW
1651 break;
1652 case '^':
3280af22 1653 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1654 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1655 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1656 break;
1657 case '~':
3280af22 1658 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 1659 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 1660 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1661 break;
1662 case '=':
3280af22 1663 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1664 break;
1665 case '-':
3280af22
NIS
1666 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1667 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1668 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
1669 break;
1670 case '%':
3280af22 1671 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1672 break;
1673 case '|':
4b65379b 1674 {
3280af22 1675 IO *io = GvIOp(PL_defoutgv);
4b65379b
CS
1676 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1677 IoFLAGS(io) &= ~IOf_FLUSH;
1678 else {
1679 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1680 PerlIO *ofp = IoOFP(io);
1681 if (ofp)
1682 (void)PerlIO_flush(ofp);
1683 IoFLAGS(io) |= IOf_FLUSH;
1684 }
1685 }
79072805
LW
1686 }
1687 break;
1688 case '*':
463ee0b2 1689 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
3280af22 1690 PL_multiline = (i != 0);
79072805
LW
1691 break;
1692 case '/':
3280af22
NIS
1693 SvREFCNT_dec(PL_nrs);
1694 PL_nrs = newSVsv(sv);
1695 SvREFCNT_dec(PL_rs);
1696 PL_rs = SvREFCNT_inc(PL_nrs);
79072805
LW
1697 break;
1698 case '\\':
3280af22
NIS
1699 if (PL_ors)
1700 Safefree(PL_ors);
e3c19b7b 1701 if (SvOK(sv) || SvGMAGICAL(sv))
3280af22 1702 PL_ors = savepv(SvPV(sv,PL_orslen));
e3c19b7b 1703 else {
3280af22
NIS
1704 PL_ors = Nullch;
1705 PL_orslen = 0;
e3c19b7b 1706 }
79072805
LW
1707 break;
1708 case ',':
3280af22
NIS
1709 if (PL_ofs)
1710 Safefree(PL_ofs);
1711 PL_ofs = savepv(SvPV(sv, PL_ofslen));
79072805
LW
1712 break;
1713 case '#':
3280af22
NIS
1714 if (PL_ofmt)
1715 Safefree(PL_ofmt);
2d8e6c8d 1716 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
1717 break;
1718 case '[':
3280af22 1719 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1720 break;
1721 case '?':
ff0cee69 1722#ifdef COMPLEX_STATUS
6b88bc9c
GS
1723 if (PL_localizing == 2) {
1724 PL_statusvalue = LvTARGOFF(sv);
1725 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
1726 }
1727 else
1728#endif
1729#ifdef VMSISH_STATUS
1730 if (VMSISH_STATUS)
1731 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1732 else
1733#endif
1734 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1735 break;
1736 case '!':
78987ded 1737 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1738 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1739 break;
1740 case '<':
3280af22
NIS
1741 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1742 if (PL_delaymagic) {
1743 PL_delaymagic |= DM_RUID;
79072805
LW
1744 break; /* don't do magic till later */
1745 }
1746#ifdef HAS_SETRUID
b28d0864 1747 (void)setruid((Uid_t)PL_uid);
79072805
LW
1748#else
1749#ifdef HAS_SETREUID
3280af22 1750 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 1751#else
85e6fe83 1752#ifdef HAS_SETRESUID
b28d0864 1753 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 1754#else
b28d0864
NIS
1755 if (PL_uid == PL_euid) /* special case $< = $> */
1756 (void)PerlProc_setuid(PL_uid);
a0d0e21e 1757 else {
d8eceb89 1758 PL_uid = PerlProc_getuid();
cea2e8a9 1759 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 1760 }
79072805
LW
1761#endif
1762#endif
85e6fe83 1763#endif
d8eceb89 1764 PL_uid = PerlProc_getuid();
3280af22 1765 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1766 break;
1767 case '>':
3280af22
NIS
1768 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1769 if (PL_delaymagic) {
1770 PL_delaymagic |= DM_EUID;
79072805
LW
1771 break; /* don't do magic till later */
1772 }
1773#ifdef HAS_SETEUID
3280af22 1774 (void)seteuid((Uid_t)PL_euid);
79072805
LW
1775#else
1776#ifdef HAS_SETREUID
b28d0864 1777 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
1778#else
1779#ifdef HAS_SETRESUID
6b88bc9c 1780 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 1781#else
b28d0864
NIS
1782 if (PL_euid == PL_uid) /* special case $> = $< */
1783 PerlProc_setuid(PL_euid);
a0d0e21e 1784 else {
e8ee3774 1785 PL_euid = PerlProc_geteuid();
cea2e8a9 1786 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 1787 }
79072805
LW
1788#endif
1789#endif
85e6fe83 1790#endif
d8eceb89 1791 PL_euid = PerlProc_geteuid();
3280af22 1792 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1793 break;
1794 case '(':
3280af22
NIS
1795 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1796 if (PL_delaymagic) {
1797 PL_delaymagic |= DM_RGID;
79072805
LW
1798 break; /* don't do magic till later */
1799 }
1800#ifdef HAS_SETRGID
b28d0864 1801 (void)setrgid((Gid_t)PL_gid);
79072805
LW
1802#else
1803#ifdef HAS_SETREGID
3280af22 1804 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
1805#else
1806#ifdef HAS_SETRESGID
b28d0864 1807 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 1808#else
b28d0864
NIS
1809 if (PL_gid == PL_egid) /* special case $( = $) */
1810 (void)PerlProc_setgid(PL_gid);
748a9306 1811 else {
d8eceb89 1812 PL_gid = PerlProc_getgid();
cea2e8a9 1813 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 1814 }
79072805
LW
1815#endif
1816#endif
85e6fe83 1817#endif
d8eceb89 1818 PL_gid = PerlProc_getgid();
3280af22 1819 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1820 break;
1821 case ')':
5cd24f17
PP
1822#ifdef HAS_SETGROUPS
1823 {
2d8e6c8d 1824 char *p = SvPV(sv, len);
5cd24f17
PP
1825 Groups_t gary[NGROUPS];
1826
5cd24f17
PP
1827 while (isSPACE(*p))
1828 ++p;
2d4389e4 1829 PL_egid = Atol(p);
5cd24f17
PP
1830 for (i = 0; i < NGROUPS; ++i) {
1831 while (*p && !isSPACE(*p))
1832 ++p;
1833 while (isSPACE(*p))
1834 ++p;
1835 if (!*p)
1836 break;
2d4389e4 1837 gary[i] = Atol(p);
5cd24f17 1838 }
8cc95fdb
PP
1839 if (i)
1840 (void)setgroups(i, gary);
5cd24f17
PP
1841 }
1842#else /* HAS_SETGROUPS */
b28d0864 1843 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1844#endif /* HAS_SETGROUPS */
3280af22
NIS
1845 if (PL_delaymagic) {
1846 PL_delaymagic |= DM_EGID;
79072805
LW
1847 break; /* don't do magic till later */
1848 }
1849#ifdef HAS_SETEGID
3280af22 1850 (void)setegid((Gid_t)PL_egid);
79072805
LW
1851#else
1852#ifdef HAS_SETREGID
b28d0864 1853 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
1854#else
1855#ifdef HAS_SETRESGID
b28d0864 1856 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 1857#else
b28d0864
NIS
1858 if (PL_egid == PL_gid) /* special case $) = $( */
1859 (void)PerlProc_setgid(PL_egid);
748a9306 1860 else {
d8eceb89 1861 PL_egid = PerlProc_getegid();
cea2e8a9 1862 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 1863 }
79072805
LW
1864#endif
1865#endif
85e6fe83 1866#endif
d8eceb89 1867 PL_egid = PerlProc_getegid();
3280af22 1868 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
1869 break;
1870 case ':':
2d8e6c8d 1871 PL_chopset = SvPV_force(sv,len);
79072805
LW
1872 break;
1873 case '0':
3280af22
NIS
1874 if (!PL_origalen) {
1875 s = PL_origargv[0];
79072805
LW
1876 s += strlen(s);
1877 /* See if all the arguments are contiguous in memory */
3280af22
NIS
1878 for (i = 1; i < PL_origargc; i++) {
1879 if (PL_origargv[i] == s + 1
fb73857a 1880#ifdef OS2
6b88bc9c 1881 || PL_origargv[i] == s + 2
fb73857a
PP
1882#endif
1883 )
379c4362
GS
1884 {
1885 ++s;
1886 s += strlen(s); /* this one is ok too */
1887 }
fb73857a
PP
1888 else
1889 break;
79072805 1890 }
bbce6d69 1891 /* can grab env area too? */
3280af22 1892 if (PL_origenviron && (PL_origenviron[0] == s + 1
fb73857a 1893#ifdef OS2
6b88bc9c 1894 || (PL_origenviron[0] == s + 9 && (s += 8))
fb73857a
PP
1895#endif
1896 )) {
66b1d557 1897 my_setenv("NoNe SuCh", Nullch);
79072805 1898 /* force copy of environment */
3280af22 1899 for (i = 0; PL_origenviron[i]; i++)
379c4362
GS
1900 if (PL_origenviron[i] == s + 1) {
1901 ++s;
1902 s += strlen(s);
1903 }
fb73857a
PP
1904 else
1905 break;
79072805 1906 }
3280af22 1907 PL_origalen = s - PL_origargv[0];
79072805 1908 }
a0d0e21e 1909 s = SvPV_force(sv,len);
8990e307 1910 i = len;
3280af22
NIS
1911 if (i >= PL_origalen) {
1912 i = PL_origalen;
fb73857a
PP
1913 /* don't allow system to limit $0 seen by script */
1914 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
3280af22
NIS
1915 Copy(s, PL_origargv[0], i, char);
1916 s = PL_origargv[0]+i;
fb73857a 1917 *s = '\0';
79072805
LW
1918 }
1919 else {
3280af22
NIS
1920 Copy(s, PL_origargv[0], i, char);
1921 s = PL_origargv[0]+i;
79072805 1922 *s++ = '\0';
3280af22 1923 while (++i < PL_origalen)
8990e307 1924 *s++ = ' ';
3280af22
NIS
1925 s = PL_origargv[0]+i;
1926 for (i = 1; i < PL_origargc; i++)
1927 PL_origargv[i] = Nullch;
79072805
LW
1928 }
1929 break;
a863c7d1
MB
1930#ifdef USE_THREADS
1931 case '@':
38a03e6e 1932 sv_setsv(thr->errsv, sv);
a863c7d1
MB
1933 break;
1934#endif /* USE_THREADS */
79072805
LW
1935 }
1936 return 0;
1937}
1938
f93b4edd
MB
1939#ifdef USE_THREADS
1940int
864dbfa3 1941Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
f93b4edd
MB
1942{
1943 dTHR;
bf49b057 1944 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%lx: magic_mutexfree 0x%lx\n",
bc1f4c86 1945 (unsigned long)thr, (unsigned long)sv);)
f93b4edd 1946 if (MgOWNER(mg))
cea2e8a9 1947 Perl_croak(aTHX_ "panic: magic_mutexfree");
f93b4edd
MB
1948 MUTEX_DESTROY(MgMUTEXP(mg));
1949 COND_DESTROY(MgCONDP(mg));
1950 return 0;
1951}
1952#endif /* USE_THREADS */
1953
79072805 1954I32
864dbfa3 1955Perl_whichsig(pTHX_ char *sig)
79072805
LW
1956{
1957 register char **sigv;
1958
22c35a8c 1959 for (sigv = PL_sig_name+1; *sigv; sigv++)
79072805 1960 if (strEQ(sig,*sigv))
22c35a8c 1961 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
1962#ifdef SIGCLD
1963 if (strEQ(sig,"CHLD"))
1964 return SIGCLD;
1965#endif
1966#ifdef SIGCHLD
1967 if (strEQ(sig,"CLD"))
1968 return SIGCHLD;
1969#endif
1970 return 0;
1971}
1972
84902520
TB
1973static SV* sig_sv;
1974
ecfc5424 1975Signal_t
cea2e8a9 1976Perl_sighandler(int sig)
79072805 1977{
cea2e8a9 1978 dTHX;
79072805 1979 dSP;
00d579c5 1980 GV *gv = Nullgv;
a0d0e21e 1981 HV *st;
3280af22 1982 SV *sv, *tSv = PL_Sv;
00d579c5 1983 CV *cv = Nullcv;
533c011a 1984 OP *myop = PL_op;
84902520 1985 U32 flags = 0;
3280af22
NIS
1986 I32 o_save_i = PL_savestack_ix, type;
1987 XPV *tXpv = PL_Xpv;
84902520 1988
3280af22 1989 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 1990 flags |= 1;
3280af22 1991 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 1992 flags |= 4;
3280af22 1993 if (PL_retstack_ix < PL_retstack_max - 2)
84902520 1994 flags |= 8;
3280af22 1995 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
1996 flags |= 16;
1997
22c35a8c 1998 if (!PL_psig_ptr[sig])
cea2e8a9 1999 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
22c35a8c 2000 PL_sig_name[sig]);
ff0cee69 2001
84902520
TB
2002 /* Max number of items pushed there is 3*n or 4. We cannot fix
2003 infinity, so we fix 4 (in fact 5): */
2004 if (flags & 1) {
3280af22
NIS
2005 PL_savestack_ix += 5; /* Protect save in progress. */
2006 o_save_i = PL_savestack_ix;
51371543 2007 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
84902520
TB
2008 }
2009 if (flags & 4)
3280af22 2010 PL_markstack_ptr++; /* Protect mark. */
84902520 2011 if (flags & 8) {
3280af22
NIS
2012 PL_retstack_ix++;
2013 PL_retstack[PL_retstack_ix] = NULL;
84902520
TB
2014 }
2015 if (flags & 16)
3280af22 2016 PL_scopestack_ix += 1;
84902520 2017 /* sv_2cv is too complicated, try a simpler variant first: */
22c35a8c 2018 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2019 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2020 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2021
a0d0e21e 2022 if (!cv || !CvROOT(cv)) {
599cee73 2023 if (ckWARN(WARN_SIGNAL))
cea2e8a9 2024 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2025 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2026 : ((cv && CvGV(cv))
2027 ? GvENAME(CvGV(cv))
2028 : "__ANON__")));
2029 goto cleanup;
79072805
LW
2030 }
2031
22c35a8c
GS
2032 if(PL_psig_name[sig]) {
2033 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520
TB
2034 flags |= 64;
2035 sig_sv = sv;
2036 } else {
ff0cee69 2037 sv = sv_newmortal();
22c35a8c 2038 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2039 }
e336de0d 2040
e788e7d3 2041 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2042 PUSHMARK(SP);
79072805 2043 PUSHs(sv);
79072805 2044 PUTBACK;
a0d0e21e 2045
864dbfa3 2046 call_sv((SV*)cv, G_DISCARD);
79072805 2047
d3acc0f7 2048 POPSTACK;
00d579c5 2049cleanup:
84902520 2050 if (flags & 1)
3280af22 2051 PL_savestack_ix -= 8; /* Unprotect save in progress. */
84902520 2052 if (flags & 4)
3280af22 2053 PL_markstack_ptr--;
84902520 2054 if (flags & 8)
3280af22 2055 PL_retstack_ix--;
84902520 2056 if (flags & 16)
3280af22 2057 PL_scopestack_ix -= 1;
84902520
TB
2058 if (flags & 64)
2059 SvREFCNT_dec(sv);
533c011a 2060 PL_op = myop; /* Apparently not needed... */
84902520 2061
3280af22
NIS
2062 PL_Sv = tSv; /* Restore global temporaries. */
2063 PL_Xpv = tXpv;
79072805
LW
2064 return;
2065}
4e35701f
NIS
2066
2067
51371543
GS
2068#ifdef PERL_OBJECT
2069#define NO_XSLOCKS
2070#include "XSUB.h"
2071#endif
2072
2073static void
2074restore_magic(pTHXo_ void *p)
2075{
2076 dTHR;
2077 MGS* mgs = SSPTR((I32)p, MGS*);
2078 SV* sv = mgs->mgs_sv;
2079
2080 if (!sv)
2081 return;
2082
2083 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2084 {
2085 if (mgs->mgs_flags)
2086 SvFLAGS(sv) |= mgs->mgs_flags;
2087 else
2088 mg_magical(sv);
2089 if (SvGMAGICAL(sv))
2090 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2091 }
2092
2093 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2094
2095 /* If we're still on top of the stack, pop us off. (That condition
2096 * will be satisfied if restore_magic was called explicitly, but *not*
2097 * if it's being called via leave_scope.)
2098 * The reason for doing this is that otherwise, things like sv_2cv()
2099 * may leave alloc gunk on the savestack, and some code
2100 * (e.g. sighandler) doesn't expect that...
2101 */
2102 if (PL_savestack_ix == mgs->mgs_ss_ix)
2103 {
2104 I32 popval = SSPOPINT;
2105 assert(popval == SAVEt_DESTRUCTOR);
2106 PL_savestack_ix -= 2;
2107 popval = SSPOPINT;
2108 assert(popval == SAVEt_ALLOC);
2109 popval = SSPOPINT;
2110 PL_savestack_ix -= popval;
2111 }
2112
2113}
2114
2115static void
2116unwind_handler_stack(pTHXo_ void *p)
2117{
2118 dTHR;
2119 U32 flags = *(U32*)p;
2120
2121 if (flags & 1)
2122 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2123 /* cxstack_ix-- Not needed, die already unwound it. */
2124 if (flags & 64)
2125 SvREFCNT_dec(sig_sv);
2126}