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