This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Create the equivalence tables based on
[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)) {
fc0dc3b3 96 CALL_FTPR(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;
599cee73 403 case '\002': /* ^B */
0453d815
PM
404 if (PL_curcop->cop_warnings == WARN_NONE ||
405 PL_curcop->cop_warnings == WARN_STD)
406 {
599cee73 407 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
0453d815
PM
408 }
409 else if (PL_curcop->cop_warnings == WARN_ALL) {
599cee73 410 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
0453d815
PM
411 }
412 else {
e24b16f9 413 sv_setsv(sv, PL_curcop->cop_warnings);
0453d815 414 }
599cee73 415 break;
49460fe6
NIS
416 case '\003': /* ^C */
417 sv_setiv(sv, (IV)PL_minus_c);
418 break;
419
79072805 420 case '\004': /* ^D */
3280af22 421 sv_setiv(sv, (IV)(PL_debug & 32767));
79072805 422 break;
28f23441
PP
423 case '\005': /* ^E */
424#ifdef VMS
425 {
426# include <descrip.h>
427# include <starlet.h>
428 char msg[255];
429 $DESCRIPTOR(msgdsc,msg);
65202027 430 sv_setnv(sv,(NV) vaxc$errno);
28f23441
PP
431 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
432 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
433 else
434 sv_setpv(sv,"");
435 }
436#else
88e89b8a 437#ifdef OS2
fb73857a 438 if (!(_emx_env & 0x200)) { /* Under DOS */
65202027 439 sv_setnv(sv, (NV)errno);
fb73857a
PP
440 sv_setpv(sv, errno ? Strerror(errno) : "");
441 } else {
017f25f1
IZ
442 if (errno != errno_isOS2) {
443 int tmp = _syserrno();
444 if (tmp) /* 2nd call to _syserrno() makes it 0 */
445 Perl_rc = tmp;
446 }
65202027 447 sv_setnv(sv, (NV)Perl_rc);
fb73857a
PP
448 sv_setpv(sv, os2error(Perl_rc));
449 }
88e89b8a 450#else
22fae026
TM
451#ifdef WIN32
452 {
453 DWORD dwErr = GetLastError();
65202027 454 sv_setnv(sv, (NV)dwErr);
22fae026 455 if (dwErr)
76e3520e 456 {
0cb96387 457 PerlProc_GetOSError(sv, dwErr);
76e3520e 458 }
22fae026
TM
459 else
460 sv_setpv(sv, "");
461 SetLastError(dwErr);
462 }
463#else
65202027 464 sv_setnv(sv, (NV)errno);
28f23441
PP
465 sv_setpv(sv, errno ? Strerror(errno) : "");
466#endif
88e89b8a 467#endif
22fae026 468#endif
946ec16e 469 SvNOK_on(sv); /* what a wonderful hack! */
28f23441 470 break;
79072805 471 case '\006': /* ^F */
3280af22 472 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 473 break;
a0d0e21e 474 case '\010': /* ^H */
3280af22 475 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 476 break;
9d116dd7 477 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
478 if (PL_inplace)
479 sv_setpv(sv, PL_inplace);
79072805 480 else
3280af22 481 sv_setsv(sv, &PL_sv_undef);
79072805 482 break;
28f23441 483 case '\017': /* ^O */
3280af22 484 sv_setpv(sv, PL_osname);
28f23441 485 break;
79072805 486 case '\020': /* ^P */
3280af22 487 sv_setiv(sv, (IV)PL_perldb);
79072805 488 break;
fb73857a 489 case '\023': /* ^S */
d58bf5aa
MB
490 {
491 dTHR;
3280af22 492 if (PL_lex_state != LEX_NOTPARSING)
d58bf5aa 493 SvOK_off(sv);
3280af22 494 else if (PL_in_eval)
d58bf5aa
MB
495 sv_setiv(sv, 1);
496 else
497 sv_setiv(sv, 0);
498 }
fb73857a 499 break;
79072805 500 case '\024': /* ^T */
88e89b8a 501#ifdef BIG_TIME
6b88bc9c 502 sv_setnv(sv, PL_basetime);
88e89b8a 503#else
3280af22 504 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 505#endif
79072805
LW
506 break;
507 case '\027': /* ^W */
0453d815 508 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
79072805
LW
509 break;
510 case '1': case '2': case '3': case '4':
511 case '5': case '6': case '7': case '8': case '9': case '&':
3280af22 512 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d
IZ
513 I32 s1, t1;
514
a863c7d1
MB
515 /*
516 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
517 * XXX Does the new way break anything?
518 */
519 paren = atoi(mg->mg_ptr);
79072805 520 getparen:
d9f97599 521 if (paren <= rx->nparens &&
cf93c79d
IZ
522 (s1 = rx->startp[paren]) != -1 &&
523 (t1 = rx->endp[paren]) != -1)
bbce6d69 524 {
cf93c79d
IZ
525 i = t1 - s1;
526 s = rx->subbeg + s1;
13f57bf8 527 getrx:
748a9306 528 if (i >= 0) {
13f57bf8 529 bool was_tainted;
3280af22
NIS
530 if (PL_tainting) {
531 was_tainted = PL_tainted;
532 PL_tainted = FALSE;
13f57bf8 533 }
cf93c79d 534 sv_setpvn(sv, s, i);
3280af22
NIS
535 if (PL_tainting)
536 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
748a9306
LW
537 break;
538 }
79072805 539 }
79072805 540 }
3280af22 541 sv_setsv(sv,&PL_sv_undef);
79072805
LW
542 break;
543 case '+':
3280af22 544 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
d9f97599 545 paren = rx->lastparen;
a0d0e21e
LW
546 if (paren)
547 goto getparen;
79072805 548 }
3280af22 549 sv_setsv(sv,&PL_sv_undef);
79072805
LW
550 break;
551 case '`':
3280af22 552 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d
IZ
553 if ((s = rx->subbeg) && rx->startp[0] != -1) {
554 i = rx->startp[0];
13f57bf8 555 goto getrx;
79072805 556 }
79072805 557 }
3280af22 558 sv_setsv(sv,&PL_sv_undef);
79072805
LW
559 break;
560 case '\'':
3280af22 561 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
cf93c79d
IZ
562 if (rx->subbeg && rx->endp[0] != -1) {
563 s = rx->subbeg + rx->endp[0];
564 i = rx->sublen - rx->endp[0];
13f57bf8 565 goto getrx;
79072805 566 }
79072805 567 }
3280af22 568 sv_setsv(sv,&PL_sv_undef);
79072805
LW
569 break;
570 case '.':
571#ifndef lint
3280af22
NIS
572 if (GvIO(PL_last_in_gv)) {
573 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
79072805
LW
574 }
575#endif
576 break;
577 case '?':
809a5acc 578 {
809a5acc 579 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 580#ifdef COMPLEX_STATUS
6b88bc9c
GS
581 LvTARGOFF(sv) = PL_statusvalue;
582 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 583#endif
809a5acc 584 }
79072805
LW
585 break;
586 case '^':
3280af22 587 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
588 if (s)
589 sv_setpv(sv,s);
590 else {
3280af22 591 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
592 sv_catpv(sv,"_TOP");
593 }
594 break;
595 case '~':
3280af22 596 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 597 if (!s)
3280af22 598 s = GvENAME(PL_defoutgv);
79072805
LW
599 sv_setpv(sv,s);
600 break;
601#ifndef lint
602 case '=':
3280af22 603 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
604 break;
605 case '-':
3280af22 606 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
607 break;
608 case '%':
3280af22 609 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805
LW
610 break;
611#endif
612 case ':':
613 break;
614 case '/':
615 break;
616 case '[':
3280af22 617 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
618 break;
619 case '|':
3280af22 620 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
621 break;
622 case ',':
3280af22 623 sv_setpvn(sv,PL_ofs,PL_ofslen);
79072805
LW
624 break;
625 case '\\':
3280af22 626 sv_setpvn(sv,PL_ors,PL_orslen);
79072805
LW
627 break;
628 case '#':
3280af22 629 sv_setpv(sv,PL_ofmt);
79072805
LW
630 break;
631 case '!':
a5f75d66 632#ifdef VMS
65202027 633 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 634 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 635#else
88e89b8a
PP
636 {
637 int saveerrno = errno;
65202027 638 sv_setnv(sv, (NV)errno);
88e89b8a
PP
639#ifdef OS2
640 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
641 else
a5f75d66 642#endif
2304df62 643 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
644 errno = saveerrno;
645 }
646#endif
946ec16e 647 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
648 break;
649 case '<':
3280af22 650 sv_setiv(sv, (IV)PL_uid);
79072805
LW
651 break;
652 case '>':
3280af22 653 sv_setiv(sv, (IV)PL_euid);
79072805
LW
654 break;
655 case '(':
3280af22 656 sv_setiv(sv, (IV)PL_gid);
cea2e8a9 657 Perl_sv_setpvf(aTHX_ sv, "%Vd", (IV)PL_gid);
79072805
LW
658 goto add_groups;
659 case ')':
3280af22 660 sv_setiv(sv, (IV)PL_egid);
cea2e8a9 661 Perl_sv_setpvf(aTHX_ sv, "%Vd", (IV)PL_egid);
79072805 662 add_groups:
79072805 663#ifdef HAS_GETGROUPS
79072805 664 {
a0d0e21e 665 Groups_t gary[NGROUPS];
79072805 666 i = getgroups(NGROUPS,gary);
46fc3d4c 667 while (--i >= 0)
cea2e8a9 668 Perl_sv_catpvf(aTHX_ sv, " %Vd", (IV)gary[i]);
79072805
LW
669 }
670#endif
29355cf7 671 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
672 break;
673 case '*':
674 break;
675 case '0':
676 break;
a863c7d1
MB
677#ifdef USE_THREADS
678 case '@':
38a03e6e 679 sv_setsv(sv, thr->errsv);
a863c7d1
MB
680 break;
681#endif /* USE_THREADS */
79072805 682 }
a0d0e21e 683 return 0;
79072805
LW
684}
685
686int
864dbfa3 687Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
688{
689 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
690
691 if (uf && uf->uf_val)
692 (*uf->uf_val)(uf->uf_index, sv);
693 return 0;
694}
695
696int
864dbfa3 697Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
698{
699 register char *s;
88e89b8a 700 char *ptr;
5aabfad6 701 STRLEN len, klen;
a0d0e21e 702 I32 i;
1e422769 703
a0d0e21e 704 s = SvPV(sv,len);
5aabfad6 705 ptr = MgPV(mg,klen);
88e89b8a 706 my_setenv(ptr, s);
1e422769 707
a0d0e21e
LW
708#ifdef DYNAMIC_ENV_FETCH
709 /* We just undefd an environment var. Is a replacement */
710 /* waiting in the wings? */
711 if (!len) {
5aabfad6 712 SV **valp;
6b88bc9c 713 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 714 s = SvPV(*valp, len);
a0d0e21e
LW
715 }
716#endif
1e422769 717
39e571d4 718#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
719 /* And you'll never guess what the dog had */
720 /* in its mouth... */
3280af22 721 if (PL_tainting) {
1e422769
PP
722 MgTAINTEDDIR_off(mg);
723#ifdef VMS
5aabfad6 724 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
725 char pathbuf[256], eltbuf[256], *cp, *elt = s;
726 struct stat sbuf;
727 int i = 0, j = 0;
728
729 do { /* DCL$PATH may be a search list */
730 while (1) { /* as may dev portion of any element */
731 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
732 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
733 cando_by_name(S_IWUSR,0,elt) ) {
734 MgTAINTEDDIR_on(mg);
735 return 0;
736 }
737 }
738 if ((cp = strchr(elt, ':')) != Nullch)
739 *cp = '\0';
740 if (my_trnlnm(elt, eltbuf, j++))
741 elt = eltbuf;
742 else
743 break;
744 }
745 j = 0;
746 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
747 }
748#endif /* VMS */
5aabfad6 749 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 750 char *strend = s + len;
463ee0b2
LW
751
752 while (s < strend) {
96827780 753 char tmpbuf[256];
1e422769 754 struct stat st;
96827780 755 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 756 s, strend, ':', &i);
463ee0b2 757 s++;
96827780
MB
758 if (i >= sizeof tmpbuf /* too long -- assume the worst */
759 || *tmpbuf != '/'
c6ed36e1 760 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 761 MgTAINTEDDIR_on(mg);
1e422769
PP
762 return 0;
763 }
463ee0b2 764 }
79072805
LW
765 }
766 }
39e571d4 767#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 768
79072805
LW
769 return 0;
770}
771
772int
864dbfa3 773Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 774{
2d8e6c8d
GS
775 STRLEN n_a;
776 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83
LW
777 return 0;
778}
779
88e89b8a 780int
864dbfa3 781Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a
PP
782{
783#if defined(VMS)
cea2e8a9 784 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 785#else
d58bf5aa 786 dTHR;
3280af22 787 if (PL_localizing) {
fb73857a 788 HE* entry;
2d8e6c8d 789 STRLEN n_a;
fb73857a
PP
790 magic_clear_all_env(sv,mg);
791 hv_iterinit((HV*)sv);
792 while (entry = hv_iternext((HV*)sv)) {
793 I32 keylen;
794 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 795 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a
PP
796 }
797 }
798#endif
799 return 0;
800}
801
802int
864dbfa3 803Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 804{
3e3baf6d 805#if defined(VMS)
cea2e8a9 806 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 807#else
f2517201 808# ifdef WIN32
3e3baf6d
TB
809 char *envv = GetEnvironmentStrings();
810 char *cur = envv;
811 STRLEN len;
812 while (*cur) {
813 char *end = strchr(cur,'=');
814 if (end && end != cur) {
815 *end = '\0';
816 my_setenv(cur,Nullch);
817 *end = '=';
ac5c734f 818 cur = end + strlen(end+1)+2;
3e3baf6d
TB
819 }
820 else if ((len = strlen(cur)))
821 cur += len+1;
822 }
823 FreeEnvironmentStrings(envv);
f2517201 824# else
2c2d71f5
JH
825# ifdef CYGWIN
826 I32 i;
827 for (i = 0; environ[i]; i++)
828 Safefree(environ[i]);
829# else
830# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
831 I32 i;
832
3280af22 833 if (environ == PL_origenviron)
f2517201 834 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
835 else
836 for (i = 0; environ[i]; i++)
f2517201 837 safesysfree(environ[i]);
2c2d71f5
JH
838# endif /* PERL_USE_SAFE_PUTENV */
839# endif /* CYGWIN */
f2517201 840
66b1d557
HM
841 environ[0] = Nullch;
842
f2517201
GS
843# endif /* WIN32 */
844#endif /* VMS */
3e3baf6d 845 return 0;
66b1d557
HM
846}
847
848int
864dbfa3 849Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
850{
851 I32 i;
2d8e6c8d 852 STRLEN n_a;
88e89b8a 853 /* Are we fetching a signal entry? */
2d8e6c8d 854 i = whichsig(MgPV(mg,n_a));
88e89b8a 855 if (i) {
22c35a8c
GS
856 if(PL_psig_ptr[i])
857 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 858 else {
ff68c719
PP
859 Sighandler_t sigstate = rsignal_state(i);
860
88e89b8a 861 /* cache state so we don't fetch it again */
ff68c719 862 if(sigstate == SIG_IGN)
88e89b8a
PP
863 sv_setpv(sv,"IGNORE");
864 else
3280af22 865 sv_setsv(sv,&PL_sv_undef);
22c35a8c 866 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
867 SvTEMP_off(sv);
868 }
869 }
870 return 0;
871}
872int
864dbfa3 873Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
874{
875 I32 i;
2d8e6c8d 876 STRLEN n_a;
88e89b8a 877 /* Are we clearing a signal entry? */
2d8e6c8d 878 i = whichsig(MgPV(mg,n_a));
88e89b8a 879 if (i) {
22c35a8c
GS
880 if(PL_psig_ptr[i]) {
881 SvREFCNT_dec(PL_psig_ptr[i]);
882 PL_psig_ptr[i]=0;
88e89b8a 883 }
22c35a8c
GS
884 if(PL_psig_name[i]) {
885 SvREFCNT_dec(PL_psig_name[i]);
886 PL_psig_name[i]=0;
88e89b8a
PP
887 }
888 }
889 return 0;
890}
3d37d572 891
85e6fe83 892int
864dbfa3 893Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805 894{
11343788 895 dTHR;
79072805
LW
896 register char *s;
897 I32 i;
748a9306 898 SV** svp;
e72dc28c 899 STRLEN len;
a0d0e21e 900
e72dc28c 901 s = MgPV(mg,len);
748a9306
LW
902 if (*s == '_') {
903 if (strEQ(s,"__DIE__"))
3280af22 904 svp = &PL_diehook;
748a9306 905 else if (strEQ(s,"__WARN__"))
3280af22 906 svp = &PL_warnhook;
748a9306 907 else if (strEQ(s,"__PARSE__"))
3280af22 908 svp = &PL_parsehook;
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;
a0d0e21e 1129 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;
599cee73
PM
1562 case '\002': /* ^B */
1563 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
0453d815 1564 if (memEQ(SvPVX(sv), WARN_ALLstring, WARNsize)) {
e24b16f9 1565 PL_compiling.cop_warnings = WARN_ALL;
0453d815
PM
1566 PL_dowarn |= G_WARN_ONCE ;
1567 }
599cee73 1568 else if (memEQ(SvPVX(sv), WARN_NONEstring, WARNsize))
e24b16f9 1569 PL_compiling.cop_warnings = WARN_NONE;
599cee73 1570 else {
0453d815 1571 if (specialWARN(PL_compiling.cop_warnings))
e24b16f9 1572 PL_compiling.cop_warnings = newSVsv(sv) ;
0453d815
PM
1573 else
1574 sv_setsv(PL_compiling.cop_warnings, sv);
1575 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1576 PL_dowarn |= G_WARN_ONCE ;
599cee73
PM
1577 }
1578 }
1579 break;
49460fe6
NIS
1580
1581 case '\003': /* ^C */
1582 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1583 break;
1584
79072805 1585 case '\004': /* ^D */
3280af22 1586 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1587 DEBUG_x(dump_all());
1588 break;
28f23441
PP
1589 case '\005': /* ^E */
1590#ifdef VMS
1591 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1592#else
048c1ddf 1593# ifdef WIN32
22fae026 1594 SetLastError( SvIV(sv) );
048c1ddf
IZ
1595# else
1596# ifndef OS2
f86702cc
PP
1597 /* will anyone ever use this? */
1598 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
048c1ddf
IZ
1599# endif
1600# endif
22fae026 1601#endif
28f23441 1602 break;
79072805 1603 case '\006': /* ^F */
3280af22 1604 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1605 break;
a0d0e21e 1606 case '\010': /* ^H */
3280af22 1607 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 1608 break;
9d116dd7 1609 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
1610 if (PL_inplace)
1611 Safefree(PL_inplace);
79072805 1612 if (SvOK(sv))
2d8e6c8d 1613 PL_inplace = savepv(SvPV(sv,len));
79072805 1614 else
3280af22 1615 PL_inplace = Nullch;
79072805 1616 break;
28f23441 1617 case '\017': /* ^O */
3280af22
NIS
1618 if (PL_osname)
1619 Safefree(PL_osname);
28f23441 1620 if (SvOK(sv))
2d8e6c8d 1621 PL_osname = savepv(SvPV(sv,len));
28f23441 1622 else
3280af22 1623 PL_osname = Nullch;
28f23441 1624 break;
79072805 1625 case '\020': /* ^P */
3280af22 1626 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1ee4443e
IZ
1627 if (PL_perldb && !PL_DBsingle)
1628 init_debugger();
79072805
LW
1629 break;
1630 case '\024': /* ^T */
88e89b8a 1631#ifdef BIG_TIME
6b88bc9c 1632 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 1633#else
3280af22 1634 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1635#endif
79072805
LW
1636 break;
1637 case '\027': /* ^W */
599cee73
PM
1638 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1639 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
0453d815
PM
1640 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1641 | (i ? G_WARN_ON : G_WARN_OFF) ;
599cee73 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;
8b73bbec 1944 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "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}