This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix copy & paste bugs in mX?PUSH macro tests.
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b5f8cc5c 4 * 2000, 2001, 2002, 2003, 2004, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
11/*
12 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
79072805
LW
14 */
15
ccfc67b7
JH
16/*
17=head1 Magical Functions
18*/
19
79072805 20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_MG_C
79072805
LW
22#include "perl.h"
23
5cd24f17 24#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221
CS
25# ifndef NGROUPS
26# define NGROUPS 32
27# endif
b7953727
JH
28# ifdef I_GRP
29# include <grp.h>
30# endif
188ea221
CS
31#endif
32
17aa7f3d
JH
33#ifdef __hpux
34# include <sys/pstat.h>
35#endif
36
e69880a5
CB
37Signal_t Perl_csighandler(int sig);
38
23ada85b 39/* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
85b332e2 40#if !defined(HAS_SIGACTION) && defined(VMS)
23ada85b 41# define FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2 42#endif
2e34cc90 43/* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
7719e241 44#if defined(KILL_BY_SIGPRC)
2e34cc90
CL
45# define FAKE_DEFAULT_SIGNAL_HANDLERS
46#endif
85b332e2 47
acfe0abc
GS
48static void restore_magic(pTHX_ void *p);
49static void unwind_handler_stack(pTHX_ void *p);
51371543 50
9cffb111
OS
51#ifdef __Lynx__
52/* Missing protos on LynxOS */
53void setruid(uid_t id);
54void seteuid(uid_t id);
55void setrgid(uid_t id);
56void setegid(uid_t id);
57#endif
58
c07a80fd
PP
59/*
60 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
61 */
62
63struct magic_state {
64 SV* mgs_sv;
65 U32 mgs_flags;
455ece5e 66 I32 mgs_ss_ix;
c07a80fd 67};
455ece5e 68/* MGS is typedef'ed to struct magic_state in perl.h */
76e3520e
GS
69
70STATIC void
8fb26106 71S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
c07a80fd 72{
455ece5e 73 MGS* mgs;
c07a80fd 74 assert(SvMAGICAL(sv));
765f542d
NC
75#ifdef PERL_COPY_ON_WRITE
76 /* Turning READONLY off for a copy-on-write scalar is a bad idea. */
77 if (SvIsCOW(sv))
78 sv_force_normal(sv);
79#endif
c07a80fd 80
685f876f 81 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
455ece5e
AD
82
83 mgs = SSPTR(mgs_ix, MGS*);
c07a80fd
PP
84 mgs->mgs_sv = sv;
85 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
455ece5e 86 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
c07a80fd
PP
87
88 SvMAGICAL_off(sv);
89 SvREADONLY_off(sv);
06759ea0 90 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
91}
92
954c1994
GS
93/*
94=for apidoc mg_magical
95
96Turns on the magical status of an SV. See C<sv_magic>.
97
98=cut
99*/
100
8990e307 101void
864dbfa3 102Perl_mg_magical(pTHX_ SV *sv)
8990e307
LW
103{
104 MAGIC* mg;
105 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
106 MGVTBL* vtbl = mg->mg_virtual;
107 if (vtbl) {
2b260de0 108 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
109 SvGMAGICAL_on(sv);
110 if (vtbl->svt_set)
111 SvSMAGICAL_on(sv);
2b260de0 112 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
8990e307
LW
113 SvRMAGICAL_on(sv);
114 }
115 }
116}
117
954c1994
GS
118/*
119=for apidoc mg_get
120
121Do magic after a value is retrieved from the SV. See C<sv_magic>.
122
123=cut
124*/
125
79072805 126int
864dbfa3 127Perl_mg_get(pTHX_ SV *sv)
79072805 128{
ff76feab
AMS
129 int new = 0;
130 MAGIC *newmg, *head, *cur, *mg;
131 I32 mgs_ix = SSNEW(sizeof(MGS));
6683b158 132 int was_temp = SvTEMP(sv);
20135930 133 /* guard against sv having being freed midway by holding a private
6683b158
NC
134 reference. */
135
136 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
137 cause the SV's buffer to get stolen (and maybe other stuff).
138 So restore it.
139 */
140 sv_2mortal(SvREFCNT_inc(sv));
141 if (!was_temp) {
142 SvTEMP_off(sv);
143 }
144
455ece5e 145 save_magic(mgs_ix, sv);
463ee0b2 146
ff76feab
AMS
147 /* We must call svt_get(sv, mg) for each valid entry in the linked
148 list of magic. svt_get() may delete the current entry, add new
149 magic to the head of the list, or upgrade the SV. AMS 20010810 */
150
151 newmg = cur = head = mg = SvMAGIC(sv);
152 while (mg) {
153 MGVTBL *vtbl = mg->mg_virtual;
154
2b260de0 155 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
316ad4fe 156 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
b77f7d40 157
58f82c5c
DM
158 /* guard against magic having been deleted - eg FETCH calling
159 * untie */
160 if (!SvMAGIC(sv))
161 break;
b77f7d40 162
ff76feab
AMS
163 /* Don't restore the flags for this entry if it was deleted. */
164 if (mg->mg_flags & MGf_GSKIP)
165 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
a0d0e21e 166 }
ff76feab
AMS
167
168 mg = mg->mg_moremagic;
169
170 if (new) {
171 /* Have we finished with the new entries we saw? Start again
172 where we left off (unless there are more new entries). */
173 if (mg == head) {
174 new = 0;
175 mg = cur;
176 head = newmg;
177 }
178 }
179
180 /* Were any new entries added? */
181 if (!new && (newmg = SvMAGIC(sv)) != head) {
182 new = 1;
183 cur = mg;
184 mg = newmg;
760ac839 185 }
79072805 186 }
463ee0b2 187
acfe0abc 188 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
6683b158
NC
189
190 if (SvREFCNT(sv) == 1) {
191 /* We hold the last reference to this SV, which implies that the
192 SV was deleted as a side effect of the routines we called. */
193 SvOK_off(sv);
194 }
79072805
LW
195 return 0;
196}
197
954c1994
GS
198/*
199=for apidoc mg_set
200
201Do magic after a value is assigned to the SV. See C<sv_magic>.
202
203=cut
204*/
205
79072805 206int
864dbfa3 207Perl_mg_set(pTHX_ SV *sv)
79072805 208{
8fb26106 209 I32 mgs_ix;
79072805 210 MAGIC* mg;
463ee0b2
LW
211 MAGIC* nextmg;
212
455ece5e
AD
213 mgs_ix = SSNEW(sizeof(MGS));
214 save_magic(mgs_ix, sv);
463ee0b2
LW
215
216 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 217 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 218 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
219 if (mg->mg_flags & MGf_GSKIP) {
220 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
455ece5e 221 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
a0d0e21e 222 }
2b260de0 223 if (vtbl && vtbl->svt_set)
fc0dc3b3 224 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
79072805 225 }
463ee0b2 226
acfe0abc 227 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
228 return 0;
229}
230
954c1994
GS
231/*
232=for apidoc mg_length
233
234Report on the SV's length. See C<sv_magic>.
235
236=cut
237*/
238
79072805 239U32
864dbfa3 240Perl_mg_length(pTHX_ SV *sv)
79072805
LW
241{
242 MAGIC* mg;
463ee0b2 243 STRLEN len;
463ee0b2 244
79072805
LW
245 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
246 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 247 if (vtbl && vtbl->svt_len) {
8fb26106 248 I32 mgs_ix;
48e43a1c 249
455ece5e
AD
250 mgs_ix = SSNEW(sizeof(MGS));
251 save_magic(mgs_ix, sv);
a0d0e21e 252 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 253 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 254 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
85e6fe83
LW
255 return len;
256 }
257 }
258
7719e241 259 if (DO_UTF8(sv))
5636d518
DB
260 {
261 U8 *s = (U8*)SvPV(sv, len);
262 len = Perl_utf8_length(aTHX_ s, s + len);
263 }
264 else
265 (void)SvPV(sv, len);
463ee0b2 266 return len;
79072805
LW
267}
268
8fb26106 269I32
864dbfa3 270Perl_mg_size(pTHX_ SV *sv)
93965878
NIS
271{
272 MAGIC* mg;
273 I32 len;
ac27b0f5 274
93965878
NIS
275 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
276 MGVTBL* vtbl = mg->mg_virtual;
2b260de0 277 if (vtbl && vtbl->svt_len) {
8fb26106 278 I32 mgs_ix;
455ece5e
AD
279
280 mgs_ix = SSNEW(sizeof(MGS));
281 save_magic(mgs_ix, sv);
93965878 282 /* omit MGf_GSKIP -- not changed here */
fc0dc3b3 283 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
acfe0abc 284 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
93965878
NIS
285 return len;
286 }
287 }
288
289 switch(SvTYPE(sv)) {
290 case SVt_PVAV:
291 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
292 return len;
293 case SVt_PVHV:
294 /* FIXME */
295 default:
cea2e8a9 296 Perl_croak(aTHX_ "Size magic not implemented");
93965878
NIS
297 break;
298 }
299 return 0;
300}
301
954c1994
GS
302/*
303=for apidoc mg_clear
304
305Clear something magical that the SV represents. See C<sv_magic>.
306
307=cut
308*/
309
79072805 310int
864dbfa3 311Perl_mg_clear(pTHX_ SV *sv)
79072805 312{
8fb26106 313 I32 mgs_ix;
79072805 314 MAGIC* mg;
463ee0b2 315
455ece5e
AD
316 mgs_ix = SSNEW(sizeof(MGS));
317 save_magic(mgs_ix, sv);
463ee0b2 318
79072805
LW
319 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
320 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e 321 /* omit GSKIP -- never set here */
727405f8 322
2b260de0 323 if (vtbl && vtbl->svt_clear)
fc0dc3b3 324 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
79072805 325 }
463ee0b2 326
acfe0abc 327 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
79072805
LW
328 return 0;
329}
330
954c1994
GS
331/*
332=for apidoc mg_find
333
334Finds the magic pointer for type matching the SV. See C<sv_magic>.
335
336=cut
337*/
338
93a17b20 339MAGIC*
864dbfa3 340Perl_mg_find(pTHX_ SV *sv, int type)
93a17b20
LW
341{
342 MAGIC* mg;
3f8f4626
DC
343 if (!sv)
344 return 0;
93a17b20
LW
345 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
346 if (mg->mg_type == type)
347 return mg;
348 }
349 return 0;
350}
351
954c1994
GS
352/*
353=for apidoc mg_copy
354
355Copies the magic from one SV to another. See C<sv_magic>.
356
357=cut
358*/
359
79072805 360int
864dbfa3 361Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
79072805 362{
463ee0b2 363 int count = 0;
79072805 364 MAGIC* mg;
463ee0b2 365 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
68795e93
NIS
366 MGVTBL* vtbl = mg->mg_virtual;
367 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
368 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
369 }
370 else if (isUPPER(mg->mg_type)) {
33c27489 371 sv_magic(nsv,
14befaf4
DM
372 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
373 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
374 ? sv : mg->mg_obj,
33c27489 375 toLOWER(mg->mg_type), key, klen);
463ee0b2 376 count++;
79072805 377 }
79072805 378 }
463ee0b2 379 return count;
79072805
LW
380}
381
954c1994
GS
382/*
383=for apidoc mg_free
384
385Free any magic storage used by the SV. See C<sv_magic>.
386
387=cut
388*/
389
79072805 390int
864dbfa3 391Perl_mg_free(pTHX_ SV *sv)
79072805
LW
392{
393 MAGIC* mg;
394 MAGIC* moremagic;
395 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
396 MGVTBL* vtbl = mg->mg_virtual;
397 moremagic = mg->mg_moremagic;
2b260de0 398 if (vtbl && vtbl->svt_free)
fc0dc3b3 399 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 400 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
979acdb5 401 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
88e89b8a 402 Safefree(mg->mg_ptr);
565764a8 403 else if (mg->mg_len == HEf_SVKEY)
88e89b8a 404 SvREFCNT_dec((SV*)mg->mg_ptr);
d460ef45 405 }
b881518d
JH
406 if (mg->mg_flags & MGf_REFCOUNTED)
407 SvREFCNT_dec(mg->mg_obj);
79072805
LW
408 Safefree(mg);
409 }
410 SvMAGIC(sv) = 0;
411 return 0;
412}
413
79072805 414#include <signal.h>
79072805 415
942e002e 416U32
864dbfa3 417Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 418{
6cef1e77 419 register REGEXP *rx;
6cef1e77 420
aaa362c4 421 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
8f580fb8
IZ
422 if (mg->mg_obj) /* @+ */
423 return rx->nparens;
424 else /* @- */
425 return rx->lastparen;
426 }
ac27b0f5 427
942e002e 428 return (U32)-1;
6cef1e77
IZ
429}
430
431int
864dbfa3 432Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
6cef1e77 433{
6cef1e77 434 register I32 paren;
cf93c79d 435 register I32 s;
6cef1e77
IZ
436 register I32 i;
437 register REGEXP *rx;
cf93c79d 438 I32 t;
6cef1e77 439
aaa362c4 440 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
6cef1e77
IZ
441 paren = mg->mg_len;
442 if (paren < 0)
443 return 0;
eb160463 444 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
445 (s = rx->startp[paren]) != -1 &&
446 (t = rx->endp[paren]) != -1)
6cef1e77
IZ
447 {
448 if (mg->mg_obj) /* @+ */
cf93c79d 449 i = t;
6cef1e77 450 else /* @- */
cf93c79d 451 i = s;
727405f8 452
a30b2f1f 453 if (i > 0 && RX_MATCH_UTF8(rx)) {
1aa99e6b 454 char *b = rx->subbeg;
0064a8a9
JH
455 if (b)
456 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
1aa99e6b 457 }
0064a8a9
JH
458
459 sv_setiv(sv, i);
6cef1e77
IZ
460 }
461 }
462 return 0;
463}
464
e4b89193 465int
a29d06ed
MG
466Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
467{
a29d06ed 468 Perl_croak(aTHX_ PL_no_modify);
e4b89193
GS
469 /* NOT REACHED */
470 return 0;
a29d06ed
MG
471}
472
93a17b20 473U32
864dbfa3 474Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
93a17b20
LW
475{
476 register I32 paren;
93a17b20 477 register I32 i;
d9f97599 478 register REGEXP *rx;
a197cbdd 479 I32 s1, t1;
93a17b20
LW
480
481 switch (*mg->mg_ptr) {
482 case '1': case '2': case '3': case '4':
483 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 484 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d 485
ffc61ed2 486 paren = atoi(mg->mg_ptr); /* $& is in [0] */
93a17b20 487 getparen:
eb160463 488 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
489 (s1 = rx->startp[paren]) != -1 &&
490 (t1 = rx->endp[paren]) != -1)
bbce6d69 491 {
cf93c79d 492 i = t1 - s1;
a197cbdd 493 getlen:
a30b2f1f 494 if (i > 0 && RX_MATCH_UTF8(rx)) {
ffc61ed2 495 char *s = rx->subbeg + s1;
a197cbdd 496 char *send = rx->subbeg + t1;
ffc61ed2 497
6d5fa195 498 i = t1 - s1;
60425c38
JH
499 if (is_utf8_string((U8*)s, i))
500 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
a197cbdd 501 }
ffc61ed2 502 if (i < 0)
0844c848 503 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
ffc61ed2 504 return i;
93a17b20 505 }
235bddc8
NIS
506 else {
507 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 508 report_uninit(sv);
235bddc8
NIS
509 }
510 }
511 else {
512 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 513 report_uninit(sv);
93a17b20 514 }
748a9306 515 return 0;
93a17b20 516 case '+':
aaa362c4 517 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 518 paren = rx->lastparen;
13f57bf8
CS
519 if (paren)
520 goto getparen;
93a17b20 521 }
748a9306 522 return 0;
a01268b5
JH
523 case '\016': /* ^N */
524 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
525 paren = rx->lastcloseparen;
526 if (paren)
527 goto getparen;
528 }
529 return 0;
93a17b20 530 case '`':
aaa362c4 531 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
532 if (rx->startp[0] != -1) {
533 i = rx->startp[0];
a197cbdd
GS
534 if (i > 0) {
535 s1 = 0;
536 t1 = i;
537 goto getlen;
538 }
93a17b20 539 }
93a17b20 540 }
748a9306 541 return 0;
93a17b20 542 case '\'':
aaa362c4 543 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
544 if (rx->endp[0] != -1) {
545 i = rx->sublen - rx->endp[0];
a197cbdd
GS
546 if (i > 0) {
547 s1 = rx->endp[0];
548 t1 = rx->sublen;
549 goto getlen;
550 }
93a17b20 551 }
93a17b20 552 }
748a9306 553 return 0;
93a17b20
LW
554 }
555 magic_get(sv,mg);
2d8e6c8d
GS
556 if (!SvPOK(sv) && SvNIOK(sv)) {
557 STRLEN n_a;
558 sv_2pv(sv, &n_a);
559 }
93a17b20
LW
560 if (SvPOK(sv))
561 return SvCUR(sv);
562 return 0;
563}
564
79072805 565int
864dbfa3 566Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
567{
568 register I32 paren;
35272f84 569 register char *s = NULL;
79072805 570 register I32 i;
d9f97599 571 register REGEXP *rx;
79072805
LW
572
573 switch (*mg->mg_ptr) {
748a9306 574 case '\001': /* ^A */
3280af22 575 sv_setsv(sv, PL_bodytarget);
748a9306 576 break;
49460fe6
NIS
577 case '\003': /* ^C */
578 sv_setiv(sv, (IV)PL_minus_c);
579 break;
580
79072805 581 case '\004': /* ^D */
aea4f609 582 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
79072805 583 break;
28f23441 584 case '\005': /* ^E */
0a378802 585 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 586#ifdef MACOS_TRADITIONAL
0a378802
JH
587 {
588 char msg[256];
727405f8 589
0a378802 590 sv_setnv(sv,(double)gMacPerl_OSErr);
727405f8 591 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
0a378802 592 }
727405f8 593#else
28f23441 594#ifdef VMS
0a378802
JH
595 {
596# include <descrip.h>
597# include <starlet.h>
598 char msg[255];
599 $DESCRIPTOR(msgdsc,msg);
600 sv_setnv(sv,(NV) vaxc$errno);
601 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
602 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
603 else
604 sv_setpv(sv,"");
605 }
28f23441 606#else
88e89b8a 607#ifdef OS2
0a378802
JH
608 if (!(_emx_env & 0x200)) { /* Under DOS */
609 sv_setnv(sv, (NV)errno);
610 sv_setpv(sv, errno ? Strerror(errno) : "");
611 } else {
612 if (errno != errno_isOS2) {
613 int tmp = _syserrno();
614 if (tmp) /* 2nd call to _syserrno() makes it 0 */
615 Perl_rc = tmp;
616 }
617 sv_setnv(sv, (NV)Perl_rc);
618 sv_setpv(sv, os2error(Perl_rc));
619 }
88e89b8a 620#else
22fae026 621#ifdef WIN32
0a378802
JH
622 {
623 DWORD dwErr = GetLastError();
624 sv_setnv(sv, (NV)dwErr);
625 if (dwErr)
626 {
627 PerlProc_GetOSError(sv, dwErr);
628 }
629 else
630 sv_setpv(sv, "");
631 SetLastError(dwErr);
632 }
22fae026 633#else
f6c8f21d
RGS
634 {
635 int saveerrno = errno;
636 sv_setnv(sv, (NV)errno);
637 sv_setpv(sv, errno ? Strerror(errno) : "");
638 errno = saveerrno;
639 }
28f23441 640#endif
88e89b8a 641#endif
22fae026 642#endif
cd39f2b6 643#endif
0a378802
JH
644 SvNOK_on(sv); /* what a wonderful hack! */
645 }
646 else if (strEQ(mg->mg_ptr+1, "NCODING"))
647 sv_setsv(sv, PL_encoding);
648 break;
79072805 649 case '\006': /* ^F */
3280af22 650 sv_setiv(sv, (IV)PL_maxsysfd);
79072805 651 break;
a0d0e21e 652 case '\010': /* ^H */
3280af22 653 sv_setiv(sv, (IV)PL_hints);
a0d0e21e 654 break;
9d116dd7 655 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
656 if (PL_inplace)
657 sv_setpv(sv, PL_inplace);
79072805 658 else
3280af22 659 sv_setsv(sv, &PL_sv_undef);
79072805 660 break;
ac27b0f5 661 case '\017': /* ^O & ^OPEN */
3511154c 662 if (*(mg->mg_ptr+1) == '\0') {
ac27b0f5 663 sv_setpv(sv, PL_osname);
3511154c
DM
664 SvTAINTED_off(sv);
665 }
ac27b0f5
NIS
666 else if (strEQ(mg->mg_ptr, "\017PEN")) {
667 if (!PL_compiling.cop_io)
668 sv_setsv(sv, &PL_sv_undef);
669 else {
670 sv_setsv(sv, PL_compiling.cop_io);
671 }
672 }
28f23441 673 break;
79072805 674 case '\020': /* ^P */
3280af22 675 sv_setiv(sv, (IV)PL_perldb);
79072805 676 break;
fb73857a 677 case '\023': /* ^S */
4ffa73a3 678 if (*(mg->mg_ptr+1) == '\0') {
3280af22 679 if (PL_lex_state != LEX_NOTPARSING)
155aba94 680 (void)SvOK_off(sv);
3280af22 681 else if (PL_in_eval)
6dc8a9e4 682 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
a4268c0a
AMS
683 else
684 sv_setiv(sv, 0);
d58bf5aa 685 }
fb73857a 686 break;
79072805 687 case '\024': /* ^T */
7c36658b 688 if (*(mg->mg_ptr+1) == '\0') {
88e89b8a 689#ifdef BIG_TIME
7c36658b 690 sv_setnv(sv, PL_basetime);
88e89b8a 691#else
7c36658b 692 sv_setiv(sv, (IV)PL_basetime);
88e89b8a 693#endif
7c36658b
MS
694 }
695 else if (strEQ(mg->mg_ptr, "\024AINT"))
9aa05f58
RGS
696 sv_setiv(sv, PL_tainting
697 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
698 : 0);
7c36658b 699 break;
a05d7ebb
JH
700 case '\025': /* $^UNICODE */
701 if (strEQ(mg->mg_ptr, "\025NICODE"))
702 sv_setuv(sv, (UV) PL_unicode);
fde18df1
JH
703 break;
704 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
705 if (*(mg->mg_ptr+1) == '\0')
706 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
0a378802 707 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
d3a7d8c7
GS
708 if (PL_compiling.cop_warnings == pWARN_NONE ||
709 PL_compiling.cop_warnings == pWARN_STD)
4438c4b7
JH
710 {
711 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
712 }
d3a7d8c7 713 else if (PL_compiling.cop_warnings == pWARN_ALL) {
75b6c4ca
RGS
714 /* Get the bit mask for $warnings::Bits{all}, because
715 * it could have been extended by warnings::register */
716 SV **bits_all;
717 HV *bits=get_hv("warnings::Bits", FALSE);
718 if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
719 sv_setsv(sv, *bits_all);
720 }
721 else {
722 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
723 }
ac27b0f5 724 }
4438c4b7
JH
725 else {
726 sv_setsv(sv, PL_compiling.cop_warnings);
ac27b0f5 727 }
d3a7d8c7 728 SvPOK_only(sv);
4438c4b7 729 }
79072805
LW
730 break;
731 case '1': case '2': case '3': case '4':
732 case '5': case '6': case '7': case '8': case '9': case '&':
aaa362c4 733 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
734 I32 s1, t1;
735
a863c7d1
MB
736 /*
737 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
738 * XXX Does the new way break anything?
739 */
ffc61ed2 740 paren = atoi(mg->mg_ptr); /* $& is in [0] */
79072805 741 getparen:
eb160463 742 if (paren <= (I32)rx->nparens &&
cf93c79d
IZ
743 (s1 = rx->startp[paren]) != -1 &&
744 (t1 = rx->endp[paren]) != -1)
bbce6d69 745 {
cf93c79d
IZ
746 i = t1 - s1;
747 s = rx->subbeg + s1;
01ec43d0 748 if (!rx->subbeg)
c2e66d9e
GS
749 break;
750
13f57bf8 751 getrx:
748a9306 752 if (i >= 0) {
cf93c79d 753 sv_setpvn(sv, s, i);
a30b2f1f 754 if (RX_MATCH_UTF8(rx) && is_utf8_string((U8*)s, i))
7e2040f0
GS
755 SvUTF8_on(sv);
756 else
757 SvUTF8_off(sv);
e9814ee1
HS
758 if (PL_tainting) {
759 if (RX_MATCH_TAINTED(rx)) {
760 MAGIC* mg = SvMAGIC(sv);
761 MAGIC* mgt;
762 PL_tainted = 1;
763 SvMAGIC(sv) = mg->mg_moremagic;
764 SvTAINT(sv);
765 if ((mgt = SvMAGIC(sv))) {
766 mg->mg_moremagic = mgt;
767 SvMAGIC(sv) = mg;
768 }
769 } else
770 SvTAINTED_off(sv);
771 }
748a9306
LW
772 break;
773 }
79072805 774 }
79072805 775 }
3280af22 776 sv_setsv(sv,&PL_sv_undef);
79072805
LW
777 break;
778 case '+':
aaa362c4 779 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
d9f97599 780 paren = rx->lastparen;
a0d0e21e
LW
781 if (paren)
782 goto getparen;
79072805 783 }
3280af22 784 sv_setsv(sv,&PL_sv_undef);
79072805 785 break;
a01268b5
JH
786 case '\016': /* ^N */
787 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
788 paren = rx->lastcloseparen;
789 if (paren)
790 goto getparen;
791 }
792 sv_setsv(sv,&PL_sv_undef);
793 break;
79072805 794 case '`':
aaa362c4 795 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
796 if ((s = rx->subbeg) && rx->startp[0] != -1) {
797 i = rx->startp[0];
13f57bf8 798 goto getrx;
79072805 799 }
79072805 800 }
3280af22 801 sv_setsv(sv,&PL_sv_undef);
79072805
LW
802 break;
803 case '\'':
aaa362c4 804 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
cf93c79d
IZ
805 if (rx->subbeg && rx->endp[0] != -1) {
806 s = rx->subbeg + rx->endp[0];
807 i = rx->sublen - rx->endp[0];
13f57bf8 808 goto getrx;
79072805 809 }
79072805 810 }
3280af22 811 sv_setsv(sv,&PL_sv_undef);
79072805
LW
812 break;
813 case '.':
814#ifndef lint
3280af22 815 if (GvIO(PL_last_in_gv)) {
357c8808 816 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
79072805
LW
817 }
818#endif
819 break;
820 case '?':
809a5acc 821 {
809a5acc 822 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 823#ifdef COMPLEX_STATUS
6b88bc9c
GS
824 LvTARGOFF(sv) = PL_statusvalue;
825 LvTARGLEN(sv) = PL_statusvalue_vms;
ff0cee69 826#endif
809a5acc 827 }
79072805
LW
828 break;
829 case '^':
0daa599b
RGS
830 if (GvIOp(PL_defoutgv))
831 s = IoTOP_NAME(GvIOp(PL_defoutgv));
79072805
LW
832 if (s)
833 sv_setpv(sv,s);
834 else {
3280af22 835 sv_setpv(sv,GvENAME(PL_defoutgv));
79072805
LW
836 sv_catpv(sv,"_TOP");
837 }
838 break;
839 case '~':
0daa599b
RGS
840 if (GvIOp(PL_defoutgv))
841 s = IoFMT_NAME(GvIOp(PL_defoutgv));
79072805 842 if (!s)
3280af22 843 s = GvENAME(PL_defoutgv);
79072805
LW
844 sv_setpv(sv,s);
845 break;
846#ifndef lint
847 case '=':
0daa599b
RGS
848 if (GvIOp(PL_defoutgv))
849 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
79072805
LW
850 break;
851 case '-':
0daa599b
RGS
852 if (GvIOp(PL_defoutgv))
853 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
79072805
LW
854 break;
855 case '%':
0daa599b
RGS
856 if (GvIOp(PL_defoutgv))
857 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
79072805
LW
858 break;
859#endif
860 case ':':
861 break;
862 case '/':
863 break;
864 case '[':
3280af22 865 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
79072805
LW
866 break;
867 case '|':
0daa599b
RGS
868 if (GvIOp(PL_defoutgv))
869 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
870 break;
871 case ',':
79072805
LW
872 break;
873 case '\\':
b2ce0fda 874 if (PL_ors_sv)
f28098ff 875 sv_copypv(sv, PL_ors_sv);
79072805
LW
876 break;
877 case '#':
3280af22 878 sv_setpv(sv,PL_ofmt);
79072805
LW
879 break;
880 case '!':
a5f75d66 881#ifdef VMS
65202027 882 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 883 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 884#else
88e89b8a
PP
885 {
886 int saveerrno = errno;
65202027 887 sv_setnv(sv, (NV)errno);
88e89b8a 888#ifdef OS2
ed344e4f
IZ
889 if (errno == errno_isOS2 || errno == errno_isOS2_set)
890 sv_setpv(sv, os2error(Perl_rc));
88e89b8a 891 else
a5f75d66 892#endif
2304df62 893 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
894 errno = saveerrno;
895 }
896#endif
946ec16e 897 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
898 break;
899 case '<':
3280af22 900 sv_setiv(sv, (IV)PL_uid);
79072805
LW
901 break;
902 case '>':
3280af22 903 sv_setiv(sv, (IV)PL_euid);
79072805
LW
904 break;
905 case '(':
3280af22 906 sv_setiv(sv, (IV)PL_gid);
a52cb5f7 907#ifdef HAS_GETGROUPS
785fb66b 908 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
a52cb5f7 909#endif
79072805
LW
910 goto add_groups;
911 case ')':
3280af22 912 sv_setiv(sv, (IV)PL_egid);
a52cb5f7 913#ifdef HAS_GETGROUPS
785fb66b 914 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
a52cb5f7 915#endif
79072805 916 add_groups:
79072805 917#ifdef HAS_GETGROUPS
79072805 918 {
a0d0e21e 919 Groups_t gary[NGROUPS];
79072805 920 i = getgroups(NGROUPS,gary);
46fc3d4c 921 while (--i >= 0)
785fb66b 922 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
79072805
LW
923 }
924#endif
155aba94 925 (void)SvIOK_on(sv); /* what a wonderful hack! */
79072805 926 break;
cd39f2b6 927#ifndef MACOS_TRADITIONAL
79072805
LW
928 case '0':
929 break;
cd39f2b6 930#endif
79072805 931 }
a0d0e21e 932 return 0;
79072805
LW
933}
934
935int
864dbfa3 936Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
937{
938 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
939
940 if (uf && uf->uf_val)
24f81a43 941 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
79072805
LW
942 return 0;
943}
944
945int
864dbfa3 946Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
947{
948 register char *s;
88e89b8a 949 char *ptr;
5aabfad6 950 STRLEN len, klen;
1e422769 951
a0d0e21e 952 s = SvPV(sv,len);
5aabfad6 953 ptr = MgPV(mg,klen);
88e89b8a 954 my_setenv(ptr, s);
1e422769 955
a0d0e21e
LW
956#ifdef DYNAMIC_ENV_FETCH
957 /* We just undefd an environment var. Is a replacement */
958 /* waiting in the wings? */
959 if (!len) {
5aabfad6 960 SV **valp;
6b88bc9c 961 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
5aabfad6 962 s = SvPV(*valp, len);
a0d0e21e
LW
963 }
964#endif
1e422769 965
39e571d4 966#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
967 /* And you'll never guess what the dog had */
968 /* in its mouth... */
3280af22 969 if (PL_tainting) {
1e422769
PP
970 MgTAINTEDDIR_off(mg);
971#ifdef VMS
5aabfad6 972 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769 973 char pathbuf[256], eltbuf[256], *cp, *elt = s;
c623ac67 974 Stat_t sbuf;
1e422769
PP
975 int i = 0, j = 0;
976
977 do { /* DCL$PATH may be a search list */
978 while (1) { /* as may dev portion of any element */
979 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
980 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
981 cando_by_name(S_IWUSR,0,elt) ) {
982 MgTAINTEDDIR_on(mg);
983 return 0;
984 }
985 }
986 if ((cp = strchr(elt, ':')) != Nullch)
987 *cp = '\0';
988 if (my_trnlnm(elt, eltbuf, j++))
989 elt = eltbuf;
990 else
991 break;
992 }
993 j = 0;
994 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
995 }
996#endif /* VMS */
5aabfad6 997 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 998 char *strend = s + len;
463ee0b2
LW
999
1000 while (s < strend) {
96827780 1001 char tmpbuf[256];
c623ac67 1002 Stat_t st;
5f74f29c 1003 I32 i;
96827780 1004 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 1005 s, strend, ':', &i);
463ee0b2 1006 s++;
96827780
MB
1007 if (i >= sizeof tmpbuf /* too long -- assume the worst */
1008 || *tmpbuf != '/'
c6ed36e1 1009 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 1010 MgTAINTEDDIR_on(mg);
1e422769
PP
1011 return 0;
1012 }
463ee0b2 1013 }
79072805
LW
1014 }
1015 }
39e571d4 1016#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 1017
79072805
LW
1018 return 0;
1019}
1020
1021int
864dbfa3 1022Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
85e6fe83 1023{
2d8e6c8d
GS
1024 STRLEN n_a;
1025 my_setenv(MgPV(mg,n_a),Nullch);
85e6fe83
LW
1026 return 0;
1027}
1028
88e89b8a 1029int
864dbfa3 1030Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
fb73857a
PP
1031{
1032#if defined(VMS)
cea2e8a9 1033 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
fb73857a 1034#else
3280af22 1035 if (PL_localizing) {
fb73857a 1036 HE* entry;
2d8e6c8d 1037 STRLEN n_a;
fb73857a
PP
1038 magic_clear_all_env(sv,mg);
1039 hv_iterinit((HV*)sv);
155aba94 1040 while ((entry = hv_iternext((HV*)sv))) {
fb73857a
PP
1041 I32 keylen;
1042 my_setenv(hv_iterkey(entry, &keylen),
2d8e6c8d 1043 SvPV(hv_iterval((HV*)sv, entry), n_a));
fb73857a
PP
1044 }
1045 }
1046#endif
1047 return 0;
1048}
1049
1050int
864dbfa3 1051Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
66b1d557 1052{
2f42fcb0 1053#ifndef PERL_MICRO
ed79a026 1054#if defined(VMS) || defined(EPOC)
cea2e8a9 1055 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
3e3baf6d 1056#else
4efc5df6 1057# if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
7766f137 1058 PerlEnv_clearenv();
4efc5df6
GS
1059# else
1060# ifdef USE_ENVIRON_ARRAY
1061# if defined(USE_ITHREADS)
1062 /* only the parent thread can clobber the process environment */
1063 if (PL_curinterp == aTHX)
1064# endif
1065 {
1066# ifndef PERL_USE_SAFE_PUTENV
66b1d557
HM
1067 I32 i;
1068
3280af22 1069 if (environ == PL_origenviron)
f2517201 1070 environ = (char**)safesysmalloc(sizeof(char*));
66b1d557
HM
1071 else
1072 for (i = 0; environ[i]; i++)
f2517201 1073 safesysfree(environ[i]);
4efc5df6 1074# endif /* PERL_USE_SAFE_PUTENV */
f2517201 1075
66b1d557 1076 environ[0] = Nullch;
4efc5df6
GS
1077 }
1078# endif /* USE_ENVIRON_ARRAY */
052c2dc6 1079# endif /* PERL_IMPLICIT_SYS || WIN32 */
2f42fcb0
JH
1080#endif /* VMS || EPOC */
1081#endif /* !PERL_MICRO */
3e3baf6d 1082 return 0;
66b1d557
HM
1083}
1084
2e34cc90
CL
1085#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1086static int sig_handlers_initted = 0;
1087#endif
7719e241 1088#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1089static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1090#endif
2e34cc90
CL
1091#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1092static int sig_defaulting[SIG_SIZE];
1093#endif
85b332e2 1094
64ca3a65 1095#ifndef PERL_MICRO
2d4fcd5e
AJ
1096#ifdef HAS_SIGPROCMASK
1097static void
1098restore_sigmask(pTHX_ SV *save_sv)
1099{
1100 sigset_t *ossetp = (sigset_t *) SvPV_nolen( save_sv );
1101 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1102}
1103#endif
66b1d557 1104int
864dbfa3 1105Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a
PP
1106{
1107 I32 i;
2d8e6c8d 1108 STRLEN n_a;
88e89b8a 1109 /* Are we fetching a signal entry? */
2d8e6c8d 1110 i = whichsig(MgPV(mg,n_a));
e02bfb16 1111 if (i > 0) {
22c35a8c
GS
1112 if(PL_psig_ptr[i])
1113 sv_setsv(sv,PL_psig_ptr[i]);
88e89b8a 1114 else {
85b332e2 1115 Sighandler_t sigstate;
2e34cc90 1116 sigstate = rsignal_state(i);
23ada85b 1117#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
2e34cc90
CL
1118 if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN;
1119#endif
1120#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1121 if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL;
85b332e2 1122#endif
88e89b8a 1123 /* cache state so we don't fetch it again */
ff68c719 1124 if(sigstate == SIG_IGN)
88e89b8a
PP
1125 sv_setpv(sv,"IGNORE");
1126 else
3280af22 1127 sv_setsv(sv,&PL_sv_undef);
22c35a8c 1128 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a
PP
1129 SvTEMP_off(sv);
1130 }
1131 }
1132 return 0;
1133}
1134int
864dbfa3 1135Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
88e89b8a 1136{
2d4fcd5e
AJ
1137 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1138 * refactoring might be in order.
1139 */
1140 register char *s;
2d8e6c8d 1141 STRLEN n_a;
2d4fcd5e
AJ
1142 SV* to_dec;
1143 s = MgPV(mg,n_a);
1144 if (*s == '_') {
1145 SV** svp;
1146 if (strEQ(s,"__DIE__"))
1147 svp = &PL_diehook;
1148 else if (strEQ(s,"__WARN__"))
1149 svp = &PL_warnhook;
1150 else
1151 Perl_croak(aTHX_ "No such hook: %s", s);
1152 if (*svp) {
1153 to_dec = *svp;
1154 *svp = 0;
1155 SvREFCNT_dec(to_dec);
1156 }
1157 }
1158 else {
1159 I32 i;
1160 /* Are we clearing a signal entry? */
1161 i = whichsig(s);
e02bfb16 1162 if (i > 0) {
2d4fcd5e
AJ
1163#ifdef HAS_SIGPROCMASK
1164 sigset_t set, save;
1165 SV* save_sv;
1166 /* Avoid having the signal arrive at a bad time, if possible. */
1167 sigemptyset(&set);
1168 sigaddset(&set,i);
1169 sigprocmask(SIG_BLOCK, &set, &save);
1170 ENTER;
1171 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1172 SAVEFREESV(save_sv);
1173 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1174#endif
1175 PERL_ASYNC_CHECK();
1176#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1177 if (!sig_handlers_initted) Perl_csighandler_init();
1178#endif
1179#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1180 sig_defaulting[i] = 1;
5c1546dc 1181 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1182#else
1183 (void)rsignal(i, SIG_DFL);
1184#endif
1185 if(PL_psig_name[i]) {
1186 SvREFCNT_dec(PL_psig_name[i]);
1187 PL_psig_name[i]=0;
1188 }
1189 if(PL_psig_ptr[i]) {
1190 to_dec=PL_psig_ptr[i];
1191 PL_psig_ptr[i]=0;
1192 LEAVE;
1193 SvREFCNT_dec(to_dec);
1194 }
1195 else
1196 LEAVE;
1197 }
88e89b8a
PP
1198 }
1199 return 0;
1200}
3d37d572 1201
0a8e0eff
NIS
1202void
1203Perl_raise_signal(pTHX_ int sig)
1204{
1205 /* Set a flag to say this signal is pending */
1206 PL_psig_pend[sig]++;
1207 /* And one to say _a_ signal is pending */
1208 PL_sig_pending = 1;
1209}
1210
1211Signal_t
1212Perl_csighandler(int sig)
1213{
1018e26f
NIS
1214#ifdef PERL_GET_SIG_CONTEXT
1215 dTHXa(PERL_GET_SIG_CONTEXT);
1216#else
85b332e2
CL
1217 dTHX;
1218#endif
23ada85b 1219#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
5c1546dc 1220 (void) rsignal(sig, PL_csighandlerp);
85b332e2
CL
1221 if (sig_ignoring[sig]) return;
1222#endif
2e34cc90 1223#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
7719e241 1224 if (sig_defaulting[sig])
2e34cc90
CL
1225#ifdef KILL_BY_SIGPRC
1226 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1227#else
1228 exit(1);
1229#endif
1230#endif
4ffa73a3
JH
1231 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1232 /* Call the perl level handler now--
1233 * with risk we may be in malloc() etc. */
1234 (*PL_sighandlerp)(sig);
1235 else
1236 Perl_raise_signal(aTHX_ sig);
0a8e0eff
NIS
1237}
1238
2e34cc90
CL
1239#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1240void
1241Perl_csighandler_init(void)
1242{
1243 int sig;
1244 if (sig_handlers_initted) return;
1245
1246 for (sig = 1; sig < SIG_SIZE; sig++) {
1247#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
218fdd94 1248 dTHX;
2e34cc90 1249 sig_defaulting[sig] = 1;
5c1546dc 1250 (void) rsignal(sig, PL_csighandlerp);
2e34cc90
CL
1251#endif
1252#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1253 sig_ignoring[sig] = 0;
1254#endif
1255 }
1256 sig_handlers_initted = 1;
1257}
1258#endif
1259
0a8e0eff
NIS
1260void
1261Perl_despatch_signals(pTHX)
1262{
1263 int sig;
1264 PL_sig_pending = 0;
1265 for (sig = 1; sig < SIG_SIZE; sig++) {
1266 if (PL_psig_pend[sig]) {
25da4428
JH
1267 PERL_BLOCKSIG_ADD(set, sig);
1268 PL_psig_pend[sig] = 0;
1269 PERL_BLOCKSIG_BLOCK(set);
f5203343 1270 (*PL_sighandlerp)(sig);
25da4428 1271 PERL_BLOCKSIG_UNBLOCK(set);
0a8e0eff
NIS
1272 }
1273 }
1274}
1275
85e6fe83 1276int
864dbfa3 1277Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1278{
1279 register char *s;
1280 I32 i;
b7953727 1281 SV** svp = 0;
2d4fcd5e
AJ
1282 /* Need to be careful with SvREFCNT_dec(), because that can have side
1283 * effects (due to closures). We must make sure that the new disposition
1284 * is in place before it is called.
1285 */
1286 SV* to_dec = 0;
e72dc28c 1287 STRLEN len;
2d4fcd5e
AJ
1288#ifdef HAS_SIGPROCMASK
1289 sigset_t set, save;
1290 SV* save_sv;
1291#endif
a0d0e21e 1292
e72dc28c 1293 s = MgPV(mg,len);
748a9306
LW
1294 if (*s == '_') {
1295 if (strEQ(s,"__DIE__"))
3280af22 1296 svp = &PL_diehook;
748a9306 1297 else if (strEQ(s,"__WARN__"))
3280af22 1298 svp = &PL_warnhook;
748a9306 1299 else
cea2e8a9 1300 Perl_croak(aTHX_ "No such hook: %s", s);
748a9306 1301 i = 0;
4633a7c4 1302 if (*svp) {
2d4fcd5e 1303 to_dec = *svp;
4633a7c4
LW
1304 *svp = 0;
1305 }
748a9306
LW
1306 }
1307 else {
1308 i = whichsig(s); /* ...no, a brick */
e02bfb16 1309 if (i < 0) {
e476b1b5 1310 if (ckWARN(WARN_SIGNAL))
9014280d 1311 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
748a9306
LW
1312 return 0;
1313 }
2d4fcd5e
AJ
1314#ifdef HAS_SIGPROCMASK
1315 /* Avoid having the signal arrive at a bad time, if possible. */
1316 sigemptyset(&set);
1317 sigaddset(&set,i);
1318 sigprocmask(SIG_BLOCK, &set, &save);
1319 ENTER;
1320 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1321 SAVEFREESV(save_sv);
1322 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1323#endif
1324 PERL_ASYNC_CHECK();
2e34cc90
CL
1325#if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1326 if (!sig_handlers_initted) Perl_csighandler_init();
1327#endif
23ada85b 1328#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2
CL
1329 sig_ignoring[i] = 0;
1330#endif
2e34cc90 1331#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
2d4fcd5e 1332 sig_defaulting[i] = 0;
2e34cc90 1333#endif
22c35a8c 1334 SvREFCNT_dec(PL_psig_name[i]);
2d4fcd5e 1335 to_dec = PL_psig_ptr[i];
22c35a8c 1336 PL_psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 1337 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
e72dc28c 1338 PL_psig_name[i] = newSVpvn(s, len);
22c35a8c 1339 SvREADONLY_on(PL_psig_name[i]);
748a9306 1340 }
a0d0e21e 1341 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
2d4fcd5e 1342 if (i) {
5c1546dc 1343 (void)rsignal(i, PL_csighandlerp);
2d4fcd5e
AJ
1344#ifdef HAS_SIGPROCMASK
1345 LEAVE;
1346#endif
1347 }
748a9306
LW
1348 else
1349 *svp = SvREFCNT_inc(sv);
2d4fcd5e
AJ
1350 if(to_dec)
1351 SvREFCNT_dec(to_dec);
a0d0e21e
LW
1352 return 0;
1353 }
e72dc28c 1354 s = SvPV_force(sv,len);
748a9306 1355 if (strEQ(s,"IGNORE")) {
85b332e2 1356 if (i) {
23ada85b 1357#ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
85b332e2 1358 sig_ignoring[i] = 1;
5c1546dc 1359 (void)rsignal(i, PL_csighandlerp);
85b332e2 1360#else
ff68c719 1361 (void)rsignal(i, SIG_IGN);
85b332e2 1362#endif
2d4fcd5e 1363 }
748a9306
LW
1364 }
1365 else if (strEQ(s,"DEFAULT") || !*s) {
1366 if (i)
2e34cc90
CL
1367#ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1368 {
1369 sig_defaulting[i] = 1;
5c1546dc 1370 (void)rsignal(i, PL_csighandlerp);
2e34cc90
CL
1371 }
1372#else
ff68c719 1373 (void)rsignal(i, SIG_DFL);
2e34cc90 1374#endif
748a9306 1375 }
79072805 1376 else {
5aabfad6
PP
1377 /*
1378 * We should warn if HINT_STRICT_REFS, but without
1379 * access to a known hint bit in a known OP, we can't
1380 * tell whether HINT_STRICT_REFS is in force or not.
1381 */
46fc3d4c 1382 if (!strchr(s,':') && !strchr(s,'\''))
e72dc28c 1383 sv_insert(sv, 0, 0, "main::", 6);
748a9306 1384 if (i)
5c1546dc 1385 (void)rsignal(i, PL_csighandlerp);
748a9306
LW
1386 else
1387 *svp = SvREFCNT_inc(sv);
79072805 1388 }
2d4fcd5e
AJ
1389#ifdef HAS_SIGPROCMASK
1390 if(i)
1391 LEAVE;
1392#endif
1393 if(to_dec)
1394 SvREFCNT_dec(to_dec);
79072805
LW
1395 return 0;
1396}
64ca3a65 1397#endif /* !PERL_MICRO */
79072805
LW
1398
1399int
864dbfa3 1400Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
79072805 1401{
3280af22 1402 PL_sub_generation++;
463ee0b2
LW
1403 return 0;
1404}
1405
1406int
864dbfa3 1407Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1408{
a0d0e21e 1409 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
3280af22 1410 PL_amagic_generation++;
463ee0b2 1411
a0d0e21e
LW
1412 return 0;
1413}
463ee0b2 1414
946ec16e 1415int
864dbfa3 1416Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1417{
1418 HV *hv = (HV*)LvTARG(sv);
6ff81951 1419 I32 i = 0;
7719e241 1420
6ff81951 1421 if (hv) {
497b47a8
JH
1422 (void) hv_iterinit(hv);
1423 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1424 i = HvKEYS(hv);
1425 else {
1426 while (hv_iternext(hv))
1427 i++;
1428 }
6ff81951
GS
1429 }
1430
1431 sv_setiv(sv, (IV)i);
1432 return 0;
1433}
1434
1435int
864dbfa3 1436Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
946ec16e
PP
1437{
1438 if (LvTARG(sv)) {
1439 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
1440 }
1441 return 0;
ac27b0f5 1442}
946ec16e 1443
e336de0d 1444/* caller is responsible for stack switching/cleanup */
565764a8 1445STATIC int
cea2e8a9 1446S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
1447{
1448 dSP;
463ee0b2 1449
924508f0
GS
1450 PUSHMARK(SP);
1451 EXTEND(SP, n);
33c27489 1452 PUSHs(SvTIED_obj(sv, mg));
ac27b0f5 1453 if (n > 1) {
93965878 1454 if (mg->mg_ptr) {
565764a8 1455 if (mg->mg_len >= 0)
79cb57f6 1456 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
565764a8 1457 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
1458 PUSHs((SV*)mg->mg_ptr);
1459 }
14befaf4 1460 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
565764a8 1461 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
1462 }
1463 }
1464 if (n > 2) {
1465 PUSHs(val);
88e89b8a 1466 }
463ee0b2
LW
1467 PUTBACK;
1468
864dbfa3 1469 return call_method(meth, flags);
946ec16e
PP
1470}
1471
76e3520e 1472STATIC int
cea2e8a9 1473S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
1474{
1475 dSP;
463ee0b2 1476
a0d0e21e
LW
1477 ENTER;
1478 SAVETMPS;
e788e7d3 1479 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 1480
33c27489 1481 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
3280af22 1482 sv_setsv(sv, *PL_stack_sp--);
93965878 1483 }
463ee0b2 1484
d3acc0f7 1485 POPSTACK;
a0d0e21e
LW
1486 FREETMPS;
1487 LEAVE;
1488 return 0;
1489}
463ee0b2 1490
a0d0e21e 1491int
864dbfa3 1492Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e 1493{
a0d0e21e
LW
1494 if (mg->mg_ptr)
1495 mg->mg_flags |= MGf_GSKIP;
58f82c5c 1496 magic_methpack(sv,mg,"FETCH");
463ee0b2
LW
1497 return 0;
1498}
1499
1500int
864dbfa3 1501Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
e336de0d
GS
1502{
1503 dSP;
a60c0954 1504 ENTER;
e788e7d3 1505 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1506 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1507 POPSTACK;
a60c0954 1508 LEAVE;
463ee0b2
LW
1509 return 0;
1510}
1511
1512int
864dbfa3 1513Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1514{
a0d0e21e
LW
1515 return magic_methpack(sv,mg,"DELETE");
1516}
463ee0b2 1517
93965878
NIS
1518
1519U32
864dbfa3 1520Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
ac27b0f5 1521{
e336de0d 1522 dSP;
93965878
NIS
1523 U32 retval = 0;
1524
1525 ENTER;
1526 SAVETMPS;
e788e7d3 1527 PUSHSTACKi(PERLSI_MAGIC);
33c27489 1528 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
3280af22 1529 sv = *PL_stack_sp--;
a60c0954 1530 retval = (U32) SvIV(sv)-1;
93965878 1531 }
d3acc0f7 1532 POPSTACK;
93965878
NIS
1533 FREETMPS;
1534 LEAVE;
1535 return retval;
1536}
1537
cea2e8a9
GS
1538int
1539Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1540{
1541 dSP;
463ee0b2 1542
e336de0d 1543 ENTER;
e788e7d3 1544 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1545 PUSHMARK(SP);
33c27489 1546 XPUSHs(SvTIED_obj(sv, mg));
463ee0b2 1547 PUTBACK;
864dbfa3 1548 call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1549 POPSTACK;
a60c0954 1550 LEAVE;
a3bcc51e 1551
463ee0b2
LW
1552 return 0;
1553}
1554
1555int
864dbfa3 1556Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
463ee0b2 1557{
463ee0b2 1558 dSP;
dff6d3cd 1559 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1560
1561 ENTER;
a0d0e21e 1562 SAVETMPS;
e788e7d3 1563 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1564 PUSHMARK(SP);
1565 EXTEND(SP, 2);
33c27489 1566 PUSHs(SvTIED_obj(sv, mg));
463ee0b2
LW
1567 if (SvOK(key))
1568 PUSHs(key);
1569 PUTBACK;
1570
864dbfa3 1571 if (call_method(meth, G_SCALAR))
3280af22 1572 sv_setsv(key, *PL_stack_sp--);
463ee0b2 1573
d3acc0f7 1574 POPSTACK;
a0d0e21e
LW
1575 FREETMPS;
1576 LEAVE;
79072805
LW
1577 return 0;
1578}
1579
1580int
864dbfa3 1581Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1582{
1583 return magic_methpack(sv,mg,"EXISTS");
ac27b0f5 1584}
a0d0e21e 1585
a3bcc51e
TP
1586SV *
1587Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1588{
1589 dSP;
1590 SV *retval = &PL_sv_undef;
1591 SV *tied = SvTIED_obj((SV*)hv, mg);
1592 HV *pkg = SvSTASH((SV*)SvRV(tied));
1593
1594 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1595 SV *key;
1596 if (HvEITER(hv))
1597 /* we are in an iteration so the hash cannot be empty */
1598 return &PL_sv_yes;
1599 /* no xhv_eiter so now use FIRSTKEY */
1600 key = sv_newmortal();
1601 magic_nextpack((SV*)hv, mg, key);
1602 HvEITER(hv) = NULL; /* need to reset iterator */
1603 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1604 }
1605
1606 /* there is a SCALAR method that we can call */
1607 ENTER;
1608 PUSHSTACKi(PERLSI_MAGIC);
1609 PUSHMARK(SP);
1610 EXTEND(SP, 1);
1611 PUSHs(tied);
1612 PUTBACK;
1613
1614 if (call_method("SCALAR", G_SCALAR))
1615 retval = *PL_stack_sp--;
1616 POPSTACK;
1617 LEAVE;
1618 return retval;
1619}
1620
a0d0e21e 1621int
864dbfa3 1622Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1623{
1624 OP *o;
1625 I32 i;
1626 GV* gv;
1627 SV** svp;
2d8e6c8d 1628 STRLEN n_a;
79072805 1629
3280af22 1630 gv = PL_DBline;
79072805 1631 i = SvTRUE(sv);
188ea221 1632 svp = av_fetch(GvAV(gv),
2d8e6c8d 1633 atoi(MgPV(mg,n_a)), FALSE);
5df8de69
DM
1634 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) {
1635 /* set or clear breakpoint in the relevant control op */
1636 if (i)
1637 o->op_flags |= OPf_SPECIAL;
1638 else
1639 o->op_flags &= ~OPf_SPECIAL;
1640 }
79072805
LW
1641 return 0;
1642}
1643
1644int
864dbfa3 1645Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1646{
3280af22 1647 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
79072805
LW
1648 return 0;
1649}
1650
1651int
864dbfa3 1652Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
79072805 1653{
3280af22 1654 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
a0d0e21e
LW
1655 return 0;
1656}
1657
1658int
864dbfa3 1659Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1660{
1661 SV* lsv = LvTARG(sv);
ac27b0f5 1662
a0d0e21e 1663 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
14befaf4 1664 mg = mg_find(lsv, PERL_MAGIC_regex_global);
565764a8 1665 if (mg && mg->mg_len >= 0) {
a0ed51b3 1666 I32 i = mg->mg_len;
7e2040f0 1667 if (DO_UTF8(lsv))
a0ed51b3
LW
1668 sv_pos_b2u(lsv, &i);
1669 sv_setiv(sv, i + PL_curcop->cop_arybase);
a0d0e21e
LW
1670 return 0;
1671 }
1672 }
1673 (void)SvOK_off(sv);
1674 return 0;
1675}
1676
1677int
864dbfa3 1678Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
a0d0e21e
LW
1679{
1680 SV* lsv = LvTARG(sv);
1681 SSize_t pos;
1682 STRLEN len;
c00206c8 1683 STRLEN ulen = 0;
a0d0e21e
LW
1684
1685 mg = 0;
ac27b0f5 1686
a0d0e21e 1687 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
14befaf4 1688 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1689 if (!mg) {
1690 if (!SvOK(sv))
1691 return 0;
14befaf4
DM
1692 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1693 mg = mg_find(lsv, PERL_MAGIC_regex_global);
a0d0e21e
LW
1694 }
1695 else if (!SvOK(sv)) {
565764a8 1696 mg->mg_len = -1;
a0d0e21e
LW
1697 return 0;
1698 }
1699 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1700
c485e607 1701 pos = SvIV(sv) - PL_curcop->cop_arybase;
a0ed51b3 1702
7e2040f0 1703 if (DO_UTF8(lsv)) {
a0ed51b3
LW
1704 ulen = sv_len_utf8(lsv);
1705 if (ulen)
1706 len = ulen;
a0ed51b3
LW
1707 }
1708
a0d0e21e
LW
1709 if (pos < 0) {
1710 pos += len;
1711 if (pos < 0)
1712 pos = 0;
1713 }
eb160463 1714 else if (pos > (SSize_t)len)
a0d0e21e 1715 pos = len;
a0ed51b3
LW
1716
1717 if (ulen) {
1718 I32 p = pos;
1719 sv_pos_u2b(lsv, &p, 0);
1720 pos = p;
1721 }
727405f8 1722
565764a8 1723 mg->mg_len = pos;
71be2cbc 1724 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1725
79072805
LW
1726 return 0;
1727}
1728
1729int
864dbfa3 1730Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
79072805 1731{
8646b087
PP
1732 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1733 SvFAKE_off(sv);
946ec16e 1734 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1735 SvFAKE_on(sv);
1736 }
1737 else
946ec16e 1738 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1739 return 0;
1740}
1741
1742int
864dbfa3 1743Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1744{
1745 register char *s;
1746 GV* gv;
2d8e6c8d 1747 STRLEN n_a;
79072805
LW
1748
1749 if (!SvOK(sv))
1750 return 0;
2d8e6c8d 1751 s = SvPV(sv, n_a);
79072805
LW
1752 if (*s == '*' && s[1])
1753 s++;
85e6fe83 1754 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1755 if (sv == (SV*)gv)
1756 return 0;
1757 if (GvGP(sv))
88e89b8a 1758 gp_free((GV*)sv);
79072805 1759 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1760 return 0;
1761}
1762
1763int
864dbfa3 1764Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1765{
1766 STRLEN len;
1767 SV *lsv = LvTARG(sv);
1768 char *tmps = SvPV(lsv,len);
1769 I32 offs = LvTARGOFF(sv);
1770 I32 rem = LvTARGLEN(sv);
1771
9aa983d2
JH
1772 if (SvUTF8(lsv))
1773 sv_pos_u2b(lsv, &offs, &rem);
eb160463 1774 if (offs > (I32)len)
6ff81951 1775 offs = len;
eb160463 1776 if (rem + offs > (I32)len)
6ff81951
GS
1777 rem = len - offs;
1778 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
9aa983d2 1779 if (SvUTF8(lsv))
2ef4b674 1780 SvUTF8_on(sv);
6ff81951
GS
1781 return 0;
1782}
1783
1784int
864dbfa3 1785Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
79072805 1786{
9aa983d2
JH
1787 STRLEN len;
1788 char *tmps = SvPV(sv, len);
1789 SV *lsv = LvTARG(sv);
1790 I32 lvoff = LvTARGOFF(sv);
1791 I32 lvlen = LvTARGLEN(sv);
075a4a2b 1792
1aa99e6b 1793 if (DO_UTF8(sv)) {
9aa983d2
JH
1794 sv_utf8_upgrade(lsv);
1795 sv_pos_u2b(lsv, &lvoff, &lvlen);
1796 sv_insert(lsv, lvoff, lvlen, tmps, len);
b76f3ce2 1797 LvTARGLEN(sv) = sv_len_utf8(sv);
9aa983d2
JH
1798 SvUTF8_on(lsv);
1799 }
9bf12eaf 1800 else if (lsv && SvUTF8(lsv)) {
9aa983d2 1801 sv_pos_u2b(lsv, &lvoff, &lvlen);
b76f3ce2 1802 LvTARGLEN(sv) = len;
e95af362 1803 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
9aa983d2
JH
1804 sv_insert(lsv, lvoff, lvlen, tmps, len);
1805 Safefree(tmps);
1aa99e6b 1806 }
b76f3ce2
GB
1807 else {
1808 sv_insert(lsv, lvoff, lvlen, tmps, len);
1809 LvTARGLEN(sv) = len;
1810 }
1811
1aa99e6b 1812
79072805
LW
1813 return 0;
1814}
1815
1816int
864dbfa3 1817Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1818{
565764a8 1819 TAINT_IF((mg->mg_len & 1) ||
155aba94 1820 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
463ee0b2
LW
1821 return 0;
1822}
1823
1824int
864dbfa3 1825Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
463ee0b2 1826{
3280af22
NIS
1827 if (PL_localizing) {
1828 if (PL_localizing == 1)
565764a8 1829 mg->mg_len <<= 1;
748a9306 1830 else
565764a8 1831 mg->mg_len >>= 1;
a0d0e21e 1832 }
3280af22 1833 else if (PL_tainted)
565764a8 1834 mg->mg_len |= 1;
748a9306 1835 else
565764a8 1836 mg->mg_len &= ~1;
463ee0b2
LW
1837 return 0;
1838}
1839
1840int
864dbfa3 1841Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
6ff81951
GS
1842{
1843 SV *lsv = LvTARG(sv);
6ff81951
GS
1844
1845 if (!lsv) {
155aba94 1846 (void)SvOK_off(sv);
6ff81951
GS
1847 return 0;
1848 }
6ff81951 1849
81e118e0 1850 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
6ff81951
GS
1851 return 0;
1852}
1853
1854int
864dbfa3 1855Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1856{
1857 do_vecset(sv); /* XXX slurp this routine */
1858 return 0;
1859}
1860
1861int
864dbfa3 1862Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
5f05dabc 1863{
71be2cbc 1864 SV *targ = Nullsv;
5f05dabc 1865 if (LvTARGLEN(sv)) {
68dc0745 1866 if (mg->mg_obj) {
74e13ce4 1867 SV *ahv = LvTARG(sv);
6d822dc4
MS
1868 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1869 if (he)
1870 targ = HeVAL(he);
68dc0745
PP
1871 }
1872 else {
3c78fafa 1873 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1874 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1875 targ = AvARRAY(av)[LvTARGOFF(sv)];
1876 }
3280af22 1877 if (targ && targ != &PL_sv_undef) {
68dc0745
PP
1878 /* somebody else defined it for us */
1879 SvREFCNT_dec(LvTARG(sv));
1880 LvTARG(sv) = SvREFCNT_inc(targ);
1881 LvTARGLEN(sv) = 0;
1882 SvREFCNT_dec(mg->mg_obj);
1883 mg->mg_obj = Nullsv;
1884 mg->mg_flags &= ~MGf_REFCOUNTED;
1885 }
5f05dabc 1886 }
71be2cbc
PP
1887 else
1888 targ = LvTARG(sv);
3280af22 1889 sv_setsv(sv, targ ? targ : &PL_sv_undef);
71be2cbc
PP
1890 return 0;
1891}
1892
1893int
864dbfa3 1894Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
71be2cbc
PP
1895{
1896 if (LvTARGLEN(sv))
68dc0745
PP
1897 vivify_defelem(sv);
1898 if (LvTARG(sv)) {
5f05dabc 1899 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1900 SvSETMAGIC(LvTARG(sv));
1901 }
5f05dabc
PP
1902 return 0;
1903}
1904
71be2cbc 1905void
864dbfa3 1906Perl_vivify_defelem(pTHX_ SV *sv)
71be2cbc 1907{
74e13ce4
GS
1908 MAGIC *mg;
1909 SV *value = Nullsv;
71be2cbc 1910
14befaf4 1911 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
71be2cbc 1912 return;
68dc0745 1913 if (mg->mg_obj) {
74e13ce4 1914 SV *ahv = LvTARG(sv);
2d8e6c8d 1915 STRLEN n_a;
6d822dc4
MS
1916 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1917 if (he)
1918 value = HeVAL(he);
3280af22 1919 if (!value || value == &PL_sv_undef)
cea2e8a9 1920 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
71be2cbc 1921 }
68dc0745
PP
1922 else {
1923 AV* av = (AV*)LvTARG(sv);
5aabfad6 1924 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1925 LvTARG(sv) = Nullsv; /* array can't be extended */
1926 else {
1927 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
3280af22 1928 if (!svp || (value = *svp) == &PL_sv_undef)
cea2e8a9 1929 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
68dc0745
PP
1930 }
1931 }
3e3baf6d 1932 (void)SvREFCNT_inc(value);
68dc0745
PP
1933 SvREFCNT_dec(LvTARG(sv));
1934 LvTARG(sv) = value;
71be2cbc 1935 LvTARGLEN(sv) = 0;
68dc0745
PP
1936 SvREFCNT_dec(mg->mg_obj);
1937 mg->mg_obj = Nullsv;
1938 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1939}
1940
1941int
864dbfa3 1942Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
810b8aa5
GS
1943{
1944 AV *av = (AV*)mg->mg_obj;
1945 SV **svp = AvARRAY(av);
1946 I32 i = AvFILLp(av);
1947 while (i >= 0) {
fdc9a813 1948 if (svp[i]) {
810b8aa5 1949 if (!SvWEAKREF(svp[i]))
cea2e8a9 1950 Perl_croak(aTHX_ "panic: magic_killbackrefs");
810b8aa5
GS
1951 /* XXX Should we check that it hasn't changed? */
1952 SvRV(svp[i]) = 0;
155aba94 1953 (void)SvOK_off(svp[i]);
810b8aa5 1954 SvWEAKREF_off(svp[i]);
fdc9a813 1955 svp[i] = Nullsv;
810b8aa5
GS
1956 }
1957 i--;
1958 }
d99b02a1 1959 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
810b8aa5
GS
1960 return 0;
1961}
1962
1963int
864dbfa3 1964Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
93a17b20 1965{
565764a8 1966 mg->mg_len = -1;
c6496cc7 1967 SvSCREAM_off(sv);
93a17b20
LW
1968 return 0;
1969}
1970
1971int
864dbfa3 1972Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
79072805 1973{
14befaf4 1974 sv_unmagic(sv, PERL_MAGIC_bm);
79072805
LW
1975 SvVALID_off(sv);
1976 return 0;
1977}
1978
1979int
864dbfa3 1980Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
55497cff 1981{
14befaf4 1982 sv_unmagic(sv, PERL_MAGIC_fm);
55497cff
PP
1983 SvCOMPILED_off(sv);
1984 return 0;
1985}
1986
1987int
864dbfa3 1988Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
1989{
1990 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1991
1992 if (uf && uf->uf_set)
24f81a43 1993 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
79072805
LW
1994 return 0;
1995}
1996
c277df42 1997int
faf82a0b
AE
1998Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
1999{
2000 sv_unmagic(sv, PERL_MAGIC_qr);
2001 return 0;
2002}
2003
2004int
864dbfa3 2005Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
c277df42
IZ
2006{
2007 regexp *re = (regexp *)mg->mg_obj;
2008 ReREFCNT_dec(re);
2009 return 0;
2010}
2011
7a4c00b4 2012#ifdef USE_LOCALE_COLLATE
79072805 2013int
864dbfa3 2014Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
bbce6d69
PP
2015{
2016 /*
838b5b74 2017 * RenE<eacute> Descartes said "I think not."
bbce6d69
PP
2018 * and vanished with a faint plop.
2019 */
7a4c00b4
PP
2020 if (mg->mg_ptr) {
2021 Safefree(mg->mg_ptr);
2022 mg->mg_ptr = NULL;
565764a8 2023 mg->mg_len = -1;
7a4c00b4 2024 }
bbce6d69
PP
2025 return 0;
2026}
7a4c00b4 2027#endif /* USE_LOCALE_COLLATE */
bbce6d69 2028
7e8c5dac
HS
2029/* Just clear the UTF-8 cache data. */
2030int
2031Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2032{
2033 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2034 mg->mg_ptr = 0;
2035 mg->mg_len = -1; /* The mg_len holds the len cache. */
2036 return 0;
2037}
2038
bbce6d69 2039int
864dbfa3 2040Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
79072805
LW
2041{
2042 register char *s;
2043 I32 i;
8990e307 2044 STRLEN len;
79072805 2045 switch (*mg->mg_ptr) {
748a9306 2046 case '\001': /* ^A */
3280af22 2047 sv_setsv(PL_bodytarget, sv);
748a9306 2048 break;
49460fe6 2049 case '\003': /* ^C */
eb160463 2050 PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
49460fe6
NIS
2051 break;
2052
79072805 2053 case '\004': /* ^D */
b4ab917c
DM
2054#ifdef DEBUGGING
2055 s = SvPV_nolen(sv);
2056 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
79072805 2057 DEBUG_x(dump_all());
b4ab917c
DM
2058#else
2059 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
2060#endif
79072805 2061 break;
28f23441 2062 case '\005': /* ^E */
d0063567 2063 if (*(mg->mg_ptr+1) == '\0') {
cd39f2b6 2064#ifdef MACOS_TRADITIONAL
d0063567 2065 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
28f23441 2066#else
cd39f2b6 2067# ifdef VMS
d0063567 2068 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
048c1ddf 2069# else
cd39f2b6 2070# ifdef WIN32
d0063567 2071 SetLastError( SvIV(sv) );
cd39f2b6 2072# else
9fed8b87 2073# ifdef OS2
d0063567 2074 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
9fed8b87 2075# else
d0063567
DK
2076 /* will anyone ever use this? */
2077 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
cd39f2b6 2078# endif
048c1ddf
IZ
2079# endif
2080# endif
22fae026 2081#endif
d0063567
DK
2082 }
2083 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2084 if (PL_encoding)
2085 SvREFCNT_dec(PL_encoding);
2086 if (SvOK(sv) || SvGMAGICAL(sv)) {
2087 PL_encoding = newSVsv(sv);
2088 }
2089 else {
2090 PL_encoding = Nullsv;
2091 }
2092 }
2093 break;
79072805 2094 case '\006': /* ^F */
3280af22 2095 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 2096 break;
a0d0e21e 2097 case '\010': /* ^H */
3280af22 2098 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
a0d0e21e 2099 break;
9d116dd7 2100 case '\011': /* ^I */ /* NOT \t in EBCDIC */
3280af22
NIS
2101 if (PL_inplace)
2102 Safefree(PL_inplace);
79072805 2103 if (SvOK(sv))
2d8e6c8d 2104 PL_inplace = savepv(SvPV(sv,len));
79072805 2105 else
3280af22 2106 PL_inplace = Nullch;
79072805 2107 break;
28f23441 2108 case '\017': /* ^O */
ac27b0f5 2109 if (*(mg->mg_ptr+1) == '\0') {
1bf29663 2110 if (PL_osname) {
ac27b0f5 2111 Safefree(PL_osname);
1bf29663
DM
2112 PL_osname = Nullch;
2113 }
3511154c
DM
2114 if (SvOK(sv)) {
2115 TAINT_PROPER("assigning to $^O");
ac27b0f5 2116 PL_osname = savepv(SvPV(sv,len));
3511154c 2117 }
ac27b0f5
NIS
2118 }
2119 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2120 if (!PL_compiling.cop_io)
2121 PL_compiling.cop_io = newSVsv(sv);
2122 else
2123 sv_setsv(PL_compiling.cop_io,sv);
2124 }
28f23441 2125 break;
79072805 2126 case '\020': /* ^P */
3280af22 2127 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
39bd0f18
PJ
2128 if ((PERLDB_SUB || PERLDB_LINE || PERLDB_SUBLINE || PERLDB_ASSERTION)
2129 && !PL_DBsingle)
1ee4443e 2130 init_debugger();
79072805
LW
2131 break;
2132 case '\024': /* ^T */
88e89b8a 2133#ifdef BIG_TIME
6b88bc9c 2134 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
88e89b8a 2135#else
3280af22 2136 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 2137#endif
79072805 2138 break;
fde18df1 2139 case '\027': /* ^W & $^WARNING_BITS */
4438c4b7
JH
2140 if (*(mg->mg_ptr+1) == '\0') {
2141 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2142 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
ac27b0f5 2143 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
0453d815 2144 | (i ? G_WARN_ON : G_WARN_OFF) ;
4438c4b7 2145 }
599cee73 2146 }
0a378802 2147 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
4438c4b7 2148 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
d775746e
GS
2149 if (!SvPOK(sv) && PL_localizing) {
2150 sv_setpvn(sv, WARN_NONEstring, WARNsize);
d3a7d8c7 2151 PL_compiling.cop_warnings = pWARN_NONE;
d775746e
GS
2152 break;
2153 }
f4fc7782 2154 {
b5477537 2155 STRLEN len, i;
d3a7d8c7 2156 int accumulate = 0 ;
f4fc7782 2157 int any_fatals = 0 ;
d3a7d8c7 2158 char * ptr = (char*)SvPV(sv, len) ;
f4fc7782
JH
2159 for (i = 0 ; i < len ; ++i) {
2160 accumulate |= ptr[i] ;
2161 any_fatals |= (ptr[i] & 0xAA) ;
2162 }
d3a7d8c7
GS
2163 if (!accumulate)
2164 PL_compiling.cop_warnings = pWARN_NONE;
f4fc7782
JH
2165 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
2166 PL_compiling.cop_warnings = pWARN_ALL;
2167 PL_dowarn |= G_WARN_ONCE ;
727405f8 2168 }
d3a7d8c7
GS
2169 else {
2170 if (specialWARN(PL_compiling.cop_warnings))
2171 PL_compiling.cop_warnings = newSVsv(sv) ;
2172 else
2173 sv_setsv(PL_compiling.cop_warnings, sv);
2174 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2175 PL_dowarn |= G_WARN_ONCE ;
2176 }
f4fc7782 2177
d3a7d8c7 2178 }
4438c4b7 2179 }
971a9dd3 2180 }
79072805
LW
2181 break;
2182 case '.':
3280af22
NIS
2183 if (PL_localizing) {
2184 if (PL_localizing == 1)
7766f137 2185 SAVESPTR(PL_last_in_gv);
748a9306 2186 }
3280af22 2187 else if (SvOK(sv) && GvIO(PL_last_in_gv))
632db599 2188 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
79072805
LW
2189 break;
2190 case '^':
3280af22 2191 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2192 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2193 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2194 break;
2195 case '~':
3280af22 2196 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2d8e6c8d 2197 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
3280af22 2198 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
2199 break;
2200 case '=':
632db599 2201 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2202 break;
2203 case '-':
632db599 2204 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
3280af22
NIS
2205 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2206 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
79072805
LW
2207 break;
2208 case '%':
632db599 2209 IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2210 break;
2211 case '|':
4b65379b 2212 {
3280af22 2213 IO *io = GvIOp(PL_defoutgv);
720f287d
AB
2214 if(!io)
2215 break;
4b65379b
CS
2216 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
2217 IoFLAGS(io) &= ~IOf_FLUSH;
2218 else {
2219 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2220 PerlIO *ofp = IoOFP(io);
2221 if (ofp)
2222 (void)PerlIO_flush(ofp);
2223 IoFLAGS(io) |= IOf_FLUSH;
2224 }
2225 }
79072805
LW
2226 }
2227 break;
79072805 2228 case '/':
3280af22 2229 SvREFCNT_dec(PL_rs);
8bfdd7d9 2230 PL_rs = newSVsv(sv);
79072805
LW
2231 break;
2232 case '\\':
7889fe52
NIS
2233 if (PL_ors_sv)
2234 SvREFCNT_dec(PL_ors_sv);
009c130f 2235 if (SvOK(sv) || SvGMAGICAL(sv)) {
7889fe52 2236 PL_ors_sv = newSVsv(sv);
009c130f 2237 }
e3c19b7b 2238 else {
7889fe52 2239 PL_ors_sv = Nullsv;
e3c19b7b 2240 }
79072805
LW
2241 break;
2242 case ',':
7889fe52
NIS
2243 if (PL_ofs_sv)
2244 SvREFCNT_dec(PL_ofs_sv);
2245 if (SvOK(sv) || SvGMAGICAL(sv)) {
2246 PL_ofs_sv = newSVsv(sv);
2247 }
2248 else {
2249 PL_ofs_sv = Nullsv;
2250 }
79072805
LW
2251 break;
2252 case '#':
3280af22
NIS
2253 if (PL_ofmt)
2254 Safefree(PL_ofmt);
2d8e6c8d 2255 PL_ofmt = savepv(SvPV(sv,len));
79072805
LW
2256 break;
2257 case '[':
3280af22 2258 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
2259 break;
2260 case '?':
ff0cee69 2261#ifdef COMPLEX_STATUS
6b88bc9c
GS
2262 if (PL_localizing == 2) {
2263 PL_statusvalue = LvTARGOFF(sv);
2264 PL_statusvalue_vms = LvTARGLEN(sv);
ff0cee69
PP
2265 }
2266 else
2267#endif
2268#ifdef VMSISH_STATUS
2269 if (VMSISH_STATUS)
2270 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
2271 else
2272#endif
2273 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
2274 break;
2275 case '!':
93189314
JH
2276 {
2277#ifdef VMS
2278# define PERL_VMS_BANG vaxc$errno
2279#else
2280# define PERL_VMS_BANG 0
2281#endif
91487cfc 2282 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
93189314
JH
2283 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2284 }
79072805
LW
2285 break;
2286 case '<':
3280af22
NIS
2287 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2288 if (PL_delaymagic) {
2289 PL_delaymagic |= DM_RUID;
79072805
LW
2290 break; /* don't do magic till later */
2291 }
2292#ifdef HAS_SETRUID
b28d0864 2293 (void)setruid((Uid_t)PL_uid);
79072805
LW
2294#else
2295#ifdef HAS_SETREUID
3280af22 2296 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
748a9306 2297#else
85e6fe83 2298#ifdef HAS_SETRESUID
b28d0864 2299 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
79072805 2300#else
75870ed3 2301 if (PL_uid == PL_euid) { /* special case $< = $> */
2302#ifdef PERL_DARWIN
2303 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2304 if (PL_uid != 0 && PerlProc_getuid() == 0)
2305 (void)PerlProc_setuid(0);
2306#endif
b28d0864 2307 (void)PerlProc_setuid(PL_uid);
75870ed3 2308 } else {
d8eceb89 2309 PL_uid = PerlProc_getuid();
cea2e8a9 2310 Perl_croak(aTHX_ "setruid() not implemented");
a0d0e21e 2311 }
79072805
LW
2312#endif
2313#endif
85e6fe83 2314#endif
d8eceb89 2315 PL_uid = PerlProc_getuid();
3280af22 2316 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2317 break;
2318 case '>':
3280af22
NIS
2319 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2320 if (PL_delaymagic) {
2321 PL_delaymagic |= DM_EUID;
79072805
LW
2322 break; /* don't do magic till later */
2323 }
2324#ifdef HAS_SETEUID
3280af22 2325 (void)seteuid((Uid_t)PL_euid);
79072805
LW
2326#else
2327#ifdef HAS_SETREUID
b28d0864 2328 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
85e6fe83
LW
2329#else
2330#ifdef HAS_SETRESUID
6b88bc9c 2331 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
79072805 2332#else
b28d0864
NIS
2333 if (PL_euid == PL_uid) /* special case $> = $< */
2334 PerlProc_setuid(PL_euid);
a0d0e21e 2335 else {
e8ee3774 2336 PL_euid = PerlProc_geteuid();
cea2e8a9 2337 Perl_croak(aTHX_ "seteuid() not implemented");
a0d0e21e 2338 }
79072805
LW
2339#endif
2340#endif
85e6fe83 2341#endif
d8eceb89 2342 PL_euid = PerlProc_geteuid();
3280af22 2343 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2344 break;
2345 case '(':
3280af22
NIS
2346 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2347 if (PL_delaymagic) {
2348 PL_delaymagic |= DM_RGID;
79072805
LW
2349 break; /* don't do magic till later */
2350 }
2351#ifdef HAS_SETRGID
b28d0864 2352 (void)setrgid((Gid_t)PL_gid);
79072805
LW
2353#else
2354#ifdef HAS_SETREGID
3280af22 2355 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
85e6fe83
LW
2356#else
2357#ifdef HAS_SETRESGID
b28d0864 2358 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
79072805 2359#else
b28d0864
NIS
2360 if (PL_gid == PL_egid) /* special case $( = $) */
2361 (void)PerlProc_setgid(PL_gid);
748a9306 2362 else {
d8eceb89 2363 PL_gid = PerlProc_getgid();
cea2e8a9 2364 Perl_croak(aTHX_ "setrgid() not implemented");
748a9306 2365 }
79072805
LW
2366#endif
2367#endif
85e6fe83 2368#endif
d8eceb89 2369 PL_gid = PerlProc_getgid();
3280af22 2370 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2371 break;
2372 case ')':
5cd24f17
PP
2373#ifdef HAS_SETGROUPS
2374 {
2d8e6c8d 2375 char *p = SvPV(sv, len);
5cd24f17
PP
2376 Groups_t gary[NGROUPS];
2377
5cd24f17
PP
2378 while (isSPACE(*p))
2379 ++p;
2d4389e4 2380 PL_egid = Atol(p);
5cd24f17
PP
2381 for (i = 0; i < NGROUPS; ++i) {
2382 while (*p && !isSPACE(*p))
2383 ++p;
2384 while (isSPACE(*p))
2385 ++p;
2386 if (!*p)
2387 break;
2d4389e4 2388 gary[i] = Atol(p);
5cd24f17 2389 }
8cc95fdb
PP
2390 if (i)
2391 (void)setgroups(i, gary);
5cd24f17
PP
2392 }
2393#else /* HAS_SETGROUPS */
b28d0864 2394 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 2395#endif /* HAS_SETGROUPS */
3280af22
NIS
2396 if (PL_delaymagic) {
2397 PL_delaymagic |= DM_EGID;
79072805
LW
2398 break; /* don't do magic till later */
2399 }
2400#ifdef HAS_SETEGID
3280af22 2401 (void)setegid((Gid_t)PL_egid);
79072805
LW
2402#else
2403#ifdef HAS_SETREGID
b28d0864 2404 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
85e6fe83
LW
2405#else
2406#ifdef HAS_SETRESGID
b28d0864 2407 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
79072805 2408#else
b28d0864
NIS
2409 if (PL_egid == PL_gid) /* special case $) = $( */
2410 (void)PerlProc_setgid(PL_egid);
748a9306 2411 else {
d8eceb89 2412 PL_egid = PerlProc_getegid();
cea2e8a9 2413 Perl_croak(aTHX_ "setegid() not implemented");
748a9306 2414 }
79072805
LW
2415#endif
2416#endif
85e6fe83 2417#endif
d8eceb89 2418 PL_egid = PerlProc_getegid();
3280af22 2419 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
79072805
LW
2420 break;
2421 case ':':
2d8e6c8d 2422 PL_chopset = SvPV_force(sv,len);
79072805 2423 break;
cd39f2b6 2424#ifndef MACOS_TRADITIONAL
79072805 2425 case '0':
e2975953 2426 LOCK_DOLLARZERO_MUTEX;
4bc88a62
PS
2427#ifdef HAS_SETPROCTITLE
2428 /* The BSDs don't show the argv[] in ps(1) output, they
2429 * show a string from the process struct and provide
2430 * the setproctitle() routine to manipulate that. */
2431 {
2432 s = SvPV(sv, len);
98b76f99 2433# if __FreeBSD_version > 410001
9aad2c0e 2434 /* The leading "-" removes the "perl: " prefix,
4bc88a62
PS
2435 * but not the "(perl) suffix from the ps(1)
2436 * output, because that's what ps(1) shows if the
2437 * argv[] is modified. */
6f2ad931 2438 setproctitle("-%s", s);
9aad2c0e 2439# else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
4bc88a62
PS
2440 /* This doesn't really work if you assume that
2441 * $0 = 'foobar'; will wipe out 'perl' from the $0
2442 * because in ps(1) output the result will be like
2443 * sprintf("perl: %s (perl)", s)
2444 * I guess this is a security feature:
2445 * one (a user process) cannot get rid of the original name.
2446 * --jhi */
2447 setproctitle("%s", s);
2448# endif
2449 }
2450#endif
17aa7f3d
JH
2451#if defined(__hpux) && defined(PSTAT_SETCMD)
2452 {
2453 union pstun un;
2454 s = SvPV(sv, len);
2455 un.pst_command = s;
2456 pstat(PSTAT_SETCMD, un, len, 0, 0);
2457 }
2458#endif
3cb9023d 2459 /* PL_origalen is set in perl_parse(). */
a0d0e21e 2460 s = SvPV_force(sv,len);
6f698202
AMS
2461 if (len >= (STRLEN)PL_origalen-1) {
2462 /* Longer than original, will be truncated. We assume that
2463 * PL_origalen bytes are available. */
2464 Copy(s, PL_origargv[0], PL_origalen-1, char);
79072805
LW
2465 }
2466 else {
54bfe034
JH
2467 /* Shorter than original, will be padded. */
2468 Copy(s, PL_origargv[0], len, char);
2469 PL_origargv[0][len] = 0;
2470 memset(PL_origargv[0] + len + 1,
2471 /* Is the space counterintuitive? Yes.
2472 * (You were expecting \0?)
3cb9023d 2473 * Does it work? Seems to. (In Linux 2.4.20 at least.)
54bfe034
JH
2474 * --jhi */
2475 (int)' ',
2476 PL_origalen - len - 1);
79072805 2477 }
ad7eccf4
JD
2478 PL_origargv[0][PL_origalen-1] = 0;
2479 for (i = 1; i < PL_origargc; i++)
2480 PL_origargv[i] = 0;
e2975953 2481 UNLOCK_DOLLARZERO_MUTEX;
79072805 2482 break;
cd39f2b6 2483#endif
79072805
LW
2484 }
2485 return 0;
2486}
2487
2488I32
864dbfa3 2489Perl_whichsig(pTHX_ char *sig)
79072805
LW
2490{
2491 register char **sigv;
2492
e02bfb16 2493 for (sigv = PL_sig_name; *sigv; sigv++)
79072805 2494 if (strEQ(sig,*sigv))
22c35a8c 2495 return PL_sig_num[sigv - PL_sig_name];
79072805
LW
2496#ifdef SIGCLD
2497 if (strEQ(sig,"CHLD"))
2498 return SIGCLD;
2499#endif
2500#ifdef SIGCHLD
2501 if (strEQ(sig,"CLD"))
2502 return SIGCHLD;
2503#endif
7f1236c0 2504 return -1;
79072805
LW
2505}
2506
df3728a2 2507#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2508static SV* sig_sv;
df3728a2 2509#endif
84902520 2510
ecfc5424 2511Signal_t
cea2e8a9 2512Perl_sighandler(int sig)
79072805 2513{
1018e26f
NIS
2514#ifdef PERL_GET_SIG_CONTEXT
2515 dTHXa(PERL_GET_SIG_CONTEXT);
71d280e3 2516#else
cea2e8a9 2517 dTHX;
71d280e3 2518#endif
79072805 2519 dSP;
00d579c5 2520 GV *gv = Nullgv;
a0d0e21e 2521 HV *st;
b7953727 2522 SV *sv = Nullsv, *tSv = PL_Sv;
00d579c5 2523 CV *cv = Nullcv;
533c011a 2524 OP *myop = PL_op;
84902520 2525 U32 flags = 0;
3280af22 2526 XPV *tXpv = PL_Xpv;
71d280e3 2527
3280af22 2528 if (PL_savestack_ix + 15 <= PL_savestack_max)
84902520 2529 flags |= 1;
3280af22 2530 if (PL_markstack_ptr < PL_markstack_max - 2)
84902520 2531 flags |= 4;
3280af22 2532 if (PL_scopestack_ix < PL_scopestack_max - 3)
84902520
TB
2533 flags |= 16;
2534
727405f8 2535 if (!PL_psig_ptr[sig]) {
99ef548b 2536 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
727405f8
NIS
2537 PL_sig_name[sig]);
2538 exit(sig);
2539 }
ff0cee69 2540
84902520
TB
2541 /* Max number of items pushed there is 3*n or 4. We cannot fix
2542 infinity, so we fix 4 (in fact 5): */
2543 if (flags & 1) {
3280af22 2544 PL_savestack_ix += 5; /* Protect save in progress. */
c76ac1ee 2545 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
84902520 2546 }
ac27b0f5 2547 if (flags & 4)
3280af22 2548 PL_markstack_ptr++; /* Protect mark. */
84902520 2549 if (flags & 16)
3280af22 2550 PL_scopestack_ix += 1;
84902520 2551 /* sv_2cv is too complicated, try a simpler variant first: */
ac27b0f5 2552 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
84902520 2553 || SvTYPE(cv) != SVt_PVCV)
22c35a8c 2554 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
84902520 2555
a0d0e21e 2556 if (!cv || !CvROOT(cv)) {
599cee73 2557 if (ckWARN(WARN_SIGNAL))
9014280d 2558 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
22c35a8c 2559 PL_sig_name[sig], (gv ? GvENAME(gv)
00d579c5
GS
2560 : ((cv && CvGV(cv))
2561 ? GvENAME(CvGV(cv))
2562 : "__ANON__")));
2563 goto cleanup;
79072805
LW
2564 }
2565
22c35a8c
GS
2566 if(PL_psig_name[sig]) {
2567 sv = SvREFCNT_inc(PL_psig_name[sig]);
84902520 2568 flags |= 64;
df3728a2 2569#if !defined(PERL_IMPLICIT_CONTEXT)
84902520 2570 sig_sv = sv;
df3728a2 2571#endif
84902520 2572 } else {
ff0cee69 2573 sv = sv_newmortal();
22c35a8c 2574 sv_setpv(sv,PL_sig_name[sig]);
88e89b8a 2575 }
e336de0d 2576
e788e7d3 2577 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 2578 PUSHMARK(SP);
79072805 2579 PUSHs(sv);
79072805 2580 PUTBACK;
a0d0e21e 2581
1b266415 2582 call_sv((SV*)cv, G_DISCARD|G_EVAL);
79072805 2583
d3acc0f7 2584 POPSTACK;
1b266415 2585 if (SvTRUE(ERRSV)) {
1d615522 2586#ifndef PERL_MICRO
983dbef6 2587#ifdef HAS_SIGPROCMASK
1b266415
NIS
2588 /* Handler "died", for example to get out of a restart-able read().
2589 * Before we re-do that on its behalf re-enable the signal which was
2590 * blocked by the system when we entered.
2591 */
2592 sigset_t set;
2593 sigemptyset(&set);
2594 sigaddset(&set,sig);
2595 sigprocmask(SIG_UNBLOCK, &set, NULL);
2596#else
2597 /* Not clear if this will work */
2598 (void)rsignal(sig, SIG_IGN);
5c1546dc 2599 (void)rsignal(sig, PL_csighandlerp);
1b266415 2600#endif
1d615522 2601#endif /* !PERL_MICRO */
b3fe4827 2602 Perl_die(aTHX_ Nullformat);
1b266415 2603 }
00d579c5 2604cleanup:
84902520 2605 if (flags & 1)
3280af22 2606 PL_savestack_ix -= 8; /* Unprotect save in progress. */
ac27b0f5 2607 if (flags & 4)
3280af22 2608 PL_markstack_ptr--;
84902520 2609 if (flags & 16)
3280af22 2610 PL_scopestack_ix -= 1;
84902520
TB
2611 if (flags & 64)
2612 SvREFCNT_dec(sv);
533c011a 2613 PL_op = myop; /* Apparently not needed... */
ac27b0f5 2614
3280af22
NIS
2615 PL_Sv = tSv; /* Restore global temporaries. */
2616 PL_Xpv = tXpv;
53bb94e2 2617 return;
79072805 2618}
4e35701f
NIS
2619
2620
51371543 2621static void
acfe0abc 2622restore_magic(pTHX_ void *p)
51371543 2623{
48944bdf 2624 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
51371543
GS
2625 SV* sv = mgs->mgs_sv;
2626
2627 if (!sv)
2628 return;
2629
2630 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2631 {
f9701176
NC
2632#ifdef PERL_COPY_ON_WRITE
2633 /* While magic was saved (and off) sv_setsv may well have seen
2634 this SV as a prime candidate for COW. */
2635 if (SvIsCOW(sv))
2636 sv_force_normal(sv);
2637#endif
2638
51371543
GS
2639 if (mgs->mgs_flags)
2640 SvFLAGS(sv) |= mgs->mgs_flags;
2641 else
2642 mg_magical(sv);
2643 if (SvGMAGICAL(sv))
2644 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2645 }
2646
2647 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2648
2649 /* If we're still on top of the stack, pop us off. (That condition
2650 * will be satisfied if restore_magic was called explicitly, but *not*
2651 * if it's being called via leave_scope.)
2652 * The reason for doing this is that otherwise, things like sv_2cv()
2653 * may leave alloc gunk on the savestack, and some code
2654 * (e.g. sighandler) doesn't expect that...
2655 */
2656 if (PL_savestack_ix == mgs->mgs_ss_ix)
2657 {
2658 I32 popval = SSPOPINT;
c76ac1ee 2659 assert(popval == SAVEt_DESTRUCTOR_X);
51371543
GS
2660 PL_savestack_ix -= 2;
2661 popval = SSPOPINT;
2662 assert(popval == SAVEt_ALLOC);
2663 popval = SSPOPINT;
2664 PL_savestack_ix -= popval;
2665 }
2666
2667}
2668
2669static void
acfe0abc 2670unwind_handler_stack(pTHX_ void *p)
51371543 2671{
51371543
GS
2672 U32 flags = *(U32*)p;
2673
2674 if (flags & 1)
2675 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2676 /* cxstack_ix-- Not needed, die already unwound it. */
df3728a2 2677#if !defined(PERL_IMPLICIT_CONTEXT)
51371543
GS
2678 if (flags & 64)
2679 SvREFCNT_dec(sig_sv);
df3728a2 2680#endif
51371543 2681}
1018e26f
NIS
2682
2683