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