This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] fix Env.pm to weed out illegal names
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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"
16#include "perl.h"
17
e6d9441c 18/* XXX If this causes problems, set i_unistd=undef in the hint file. */
a0d0e21e
LW
19#ifdef I_UNISTD
20# include <unistd.h>
21#endif
a0d0e21e 22
5cd24f17 23#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221
CS
24# ifndef NGROUPS
25# define NGROUPS 32
26# endif
27#endif
28
c07a80fd
PP
29/*
30 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
31 */
32
33struct magic_state {
34 SV* mgs_sv;
35 U32 mgs_flags;
36};
37typedef struct magic_state MGS;
38
39static void restore_magic _((void *p));
40
48e43a1c 41static void
8ac85365 42save_magic(MGS *mgs, SV *sv)
c07a80fd 43{
c07a80fd
PP
44 assert(SvMAGICAL(sv));
45
c07a80fd
PP
46 mgs->mgs_sv = sv;
47 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
48 SAVEDESTRUCTOR(restore_magic, mgs);
49
50 SvMAGICAL_off(sv);
51 SvREADONLY_off(sv);
52 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
53}
54
55static void
8ac85365 56restore_magic(void *p)
c07a80fd 57{
48e43a1c 58 MGS* mgs = (MGS*)p;
c07a80fd
PP
59 SV* sv = mgs->mgs_sv;
60
61 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
62 {
63 if (mgs->mgs_flags)
64 SvFLAGS(sv) |= mgs->mgs_flags;
65 else
66 mg_magical(sv);
67 if (SvGMAGICAL(sv))
68 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
69 }
c07a80fd
PP
70}
71
8990e307 72void
8ac85365 73mg_magical(SV *sv)
8990e307
LW
74{
75 MAGIC* mg;
76 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
77 MGVTBL* vtbl = mg->mg_virtual;
78 if (vtbl) {
a0d0e21e 79 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
80 SvGMAGICAL_on(sv);
81 if (vtbl->svt_set)
82 SvSMAGICAL_on(sv);
83 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
84 SvRMAGICAL_on(sv);
85 }
86 }
87}
88
79072805 89int
8ac85365 90mg_get(SV *sv)
79072805 91{
48e43a1c 92 MGS mgs;
79072805 93 MAGIC* mg;
c6496cc7 94 MAGIC** mgp;
760ac839 95 int mgp_valid = 0;
463ee0b2 96
c07a80fd 97 ENTER;
48e43a1c 98 save_magic(&mgs, sv);
463ee0b2 99
c6496cc7
PP
100 mgp = &SvMAGIC(sv);
101 while ((mg = *mgp) != 0) {
79072805 102 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e 103 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
79072805 104 (*vtbl->svt_get)(sv, mg);
c6496cc7 105 /* Ignore this magic if it's been deleted */
48e43a1c
CS
106 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
107 (mg->mg_flags & MGf_GSKIP))
108 mgs.mgs_flags = 0;
a0d0e21e 109 }
c6496cc7 110 /* Advance to next magic (complicated by possible deletion) */
760ac839 111 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
c6496cc7 112 mgp = &mg->mg_moremagic;
760ac839
LW
113 mgp_valid = 1;
114 }
115 else
116 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
79072805 117 }
463ee0b2 118
c07a80fd 119 LEAVE;
79072805
LW
120 return 0;
121}
122
123int
8ac85365 124mg_set(SV *sv)
79072805 125{
48e43a1c 126 MGS mgs;
79072805 127 MAGIC* mg;
463ee0b2
LW
128 MAGIC* nextmg;
129
c07a80fd 130 ENTER;
48e43a1c 131 save_magic(&mgs, sv);
463ee0b2
LW
132
133 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 134 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 135 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
136 if (mg->mg_flags & MGf_GSKIP) {
137 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
48e43a1c 138 mgs.mgs_flags = 0;
a0d0e21e 139 }
79072805
LW
140 if (vtbl && vtbl->svt_set)
141 (*vtbl->svt_set)(sv, mg);
142 }
463ee0b2 143
c07a80fd 144 LEAVE;
79072805
LW
145 return 0;
146}
147
148U32
8ac85365 149mg_len(SV *sv)
79072805
LW
150{
151 MAGIC* mg;
748a9306 152 char *junk;
463ee0b2 153 STRLEN len;
463ee0b2 154
79072805
LW
155 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
156 MGVTBL* vtbl = mg->mg_virtual;
85e6fe83 157 if (vtbl && vtbl->svt_len) {
48e43a1c
CS
158 MGS mgs;
159
c07a80fd 160 ENTER;
48e43a1c 161 save_magic(&mgs, sv);
a0d0e21e 162 /* omit MGf_GSKIP -- not changed here */
85e6fe83 163 len = (*vtbl->svt_len)(sv, mg);
c07a80fd 164 LEAVE;
85e6fe83
LW
165 return len;
166 }
167 }
168
748a9306 169 junk = SvPV(sv, len);
463ee0b2 170 return len;
79072805
LW
171}
172
93965878
NIS
173I32
174mg_size(SV *sv)
175{
176 MAGIC* mg;
177 I32 len;
178
179 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
180 MGVTBL* vtbl = mg->mg_virtual;
181 if (vtbl && vtbl->svt_len) {
182 MGS mgs;
183 ENTER;
184 /* omit MGf_GSKIP -- not changed here */
185 len = (*vtbl->svt_len)(sv, mg);
186 LEAVE;
187 return len;
188 }
189 }
190
191 switch(SvTYPE(sv)) {
192 case SVt_PVAV:
193 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
194 return len;
195 case SVt_PVHV:
196 /* FIXME */
197 default:
198 croak("Size magic not implemented");
199 break;
200 }
201 return 0;
202}
203
79072805 204int
8ac85365 205mg_clear(SV *sv)
79072805 206{
48e43a1c 207 MGS mgs;
79072805 208 MAGIC* mg;
463ee0b2 209
c07a80fd 210 ENTER;
48e43a1c 211 save_magic(&mgs, sv);
463ee0b2 212
79072805
LW
213 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
214 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e
LW
215 /* omit GSKIP -- never set here */
216
79072805
LW
217 if (vtbl && vtbl->svt_clear)
218 (*vtbl->svt_clear)(sv, mg);
219 }
463ee0b2 220
c07a80fd 221 LEAVE;
79072805
LW
222 return 0;
223}
224
93a17b20 225MAGIC*
8ac85365 226mg_find(SV *sv, int type)
93a17b20
LW
227{
228 MAGIC* mg;
93a17b20
LW
229 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
230 if (mg->mg_type == type)
231 return mg;
232 }
233 return 0;
234}
235
79072805 236int
8ac85365 237mg_copy(SV *sv, SV *nsv, char *key, I32 klen)
79072805 238{
463ee0b2 239 int count = 0;
79072805 240 MAGIC* mg;
463ee0b2
LW
241 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
242 if (isUPPER(mg->mg_type)) {
a0d0e21e 243 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
463ee0b2 244 count++;
79072805 245 }
79072805 246 }
463ee0b2 247 return count;
79072805
LW
248}
249
250int
8ac85365 251mg_free(SV *sv)
79072805
LW
252{
253 MAGIC* mg;
254 MAGIC* moremagic;
255 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
256 MGVTBL* vtbl = mg->mg_virtual;
257 moremagic = mg->mg_moremagic;
258 if (vtbl && vtbl->svt_free)
259 (*vtbl->svt_free)(sv, mg);
93a17b20 260 if (mg->mg_ptr && mg->mg_type != 'g')
88e89b8a
PP
261 if (mg->mg_len >= 0)
262 Safefree(mg->mg_ptr);
263 else if (mg->mg_len == HEf_SVKEY)
264 SvREFCNT_dec((SV*)mg->mg_ptr);
85e6fe83 265 if (mg->mg_flags & MGf_REFCOUNTED)
8990e307 266 SvREFCNT_dec(mg->mg_obj);
79072805
LW
267 Safefree(mg);
268 }
269 SvMAGIC(sv) = 0;
270 return 0;
271}
272
273#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
274#include <signal.h>
275#endif
276
93a17b20 277U32
8ac85365 278magic_len(SV *sv, MAGIC *mg)
93a17b20 279{
a863c7d1 280 dTHR;
93a17b20
LW
281 register I32 paren;
282 register char *s;
283 register I32 i;
bbce6d69 284 register REGEXP *rx;
748a9306 285 char *t;
93a17b20
LW
286
287 switch (*mg->mg_ptr) {
288 case '1': case '2': case '3': case '4':
289 case '5': case '6': case '7': case '8': case '9': case '&':
bbce6d69 290 if (curpm && (rx = curpm->op_pmregexp)) {
93a17b20
LW
291 paren = atoi(mg->mg_ptr);
292 getparen:
bbce6d69
PP
293 if (paren <= rx->nparens &&
294 (s = rx->startp[paren]) &&
295 (t = rx->endp[paren]))
296 {
748a9306 297 i = t - s;
71be2cbc 298 if (i >= 0)
93a17b20 299 return i;
93a17b20 300 }
93a17b20 301 }
748a9306 302 return 0;
93a17b20 303 case '+':
bbce6d69
PP
304 if (curpm && (rx = curpm->op_pmregexp)) {
305 paren = rx->lastparen;
13f57bf8
CS
306 if (paren)
307 goto getparen;
93a17b20 308 }
748a9306 309 return 0;
93a17b20 310 case '`':
bbce6d69 311 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 312 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 313 i = rx->startp[0] - s;
71be2cbc 314 if (i >= 0)
93a17b20 315 return i;
93a17b20 316 }
93a17b20 317 }
748a9306 318 return 0;
93a17b20 319 case '\'':
bbce6d69 320 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
321 if (rx->subend && (s = rx->endp[0])) {
322 i = rx->subend - s;
323 if (i >= 0)
5cd24f17 324 return i;
93a17b20 325 }
93a17b20 326 }
748a9306 327 return 0;
93a17b20
LW
328 case ',':
329 return (STRLEN)ofslen;
330 case '\\':
331 return (STRLEN)orslen;
332 }
333 magic_get(sv,mg);
334 if (!SvPOK(sv) && SvNIOK(sv))
463ee0b2 335 sv_2pv(sv, &na);
93a17b20
LW
336 if (SvPOK(sv))
337 return SvCUR(sv);
338 return 0;
339}
340
79072805 341int
8ac85365 342magic_get(SV *sv, MAGIC *mg)
79072805 343{
a863c7d1 344 dTHR;
79072805
LW
345 register I32 paren;
346 register char *s;
347 register I32 i;
bbce6d69 348 register REGEXP *rx;
748a9306 349 char *t;
79072805
LW
350
351 switch (*mg->mg_ptr) {
748a9306
LW
352 case '\001': /* ^A */
353 sv_setsv(sv, bodytarget);
354 break;
79072805 355 case '\004': /* ^D */
188ea221 356 sv_setiv(sv, (IV)(debug & 32767));
79072805 357 break;
28f23441
PP
358 case '\005': /* ^E */
359#ifdef VMS
360 {
361# include <descrip.h>
362# include <starlet.h>
363 char msg[255];
364 $DESCRIPTOR(msgdsc,msg);
946ec16e 365 sv_setnv(sv,(double) vaxc$errno);
28f23441
PP
366 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
367 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
368 else
369 sv_setpv(sv,"");
370 }
371#else
88e89b8a 372#ifdef OS2
fb73857a
PP
373 if (!(_emx_env & 0x200)) { /* Under DOS */
374 sv_setnv(sv, (double)errno);
375 sv_setpv(sv, errno ? Strerror(errno) : "");
376 } else {
377 if (errno != errno_isOS2)
378 Perl_rc = _syserrno();
379 sv_setnv(sv, (double)Perl_rc);
380 sv_setpv(sv, os2error(Perl_rc));
381 }
88e89b8a 382#else
22fae026
TM
383#ifdef WIN32
384 {
385 DWORD dwErr = GetLastError();
386 sv_setnv(sv, (double)dwErr);
387 if (dwErr)
388 win32_str_os_error(sv, dwErr);
389 else
390 sv_setpv(sv, "");
391 SetLastError(dwErr);
392 }
393#else
946ec16e 394 sv_setnv(sv, (double)errno);
28f23441
PP
395 sv_setpv(sv, errno ? Strerror(errno) : "");
396#endif
88e89b8a 397#endif
22fae026 398#endif
946ec16e 399 SvNOK_on(sv); /* what a wonderful hack! */
28f23441 400 break;
79072805 401 case '\006': /* ^F */
188ea221 402 sv_setiv(sv, (IV)maxsysfd);
79072805 403 break;
a0d0e21e 404 case '\010': /* ^H */
188ea221 405 sv_setiv(sv, (IV)hints);
a0d0e21e 406 break;
79072805
LW
407 case '\t': /* ^I */
408 if (inplace)
409 sv_setpv(sv, inplace);
410 else
188ea221 411 sv_setsv(sv, &sv_undef);
79072805 412 break;
28f23441 413 case '\017': /* ^O */
188ea221 414 sv_setpv(sv, osname);
28f23441 415 break;
79072805 416 case '\020': /* ^P */
188ea221 417 sv_setiv(sv, (IV)perldb);
79072805 418 break;
fb73857a 419 case '\023': /* ^S */
d58bf5aa
MB
420 {
421 dTHR;
422 if (lex_state != LEX_NOTPARSING)
423 SvOK_off(sv);
424 else if (in_eval)
425 sv_setiv(sv, 1);
426 else
427 sv_setiv(sv, 0);
428 }
fb73857a 429 break;
79072805 430 case '\024': /* ^T */
88e89b8a 431#ifdef BIG_TIME
188ea221 432 sv_setnv(sv, basetime);
88e89b8a 433#else
188ea221 434 sv_setiv(sv, (IV)basetime);
88e89b8a 435#endif
79072805
LW
436 break;
437 case '\027': /* ^W */
188ea221 438 sv_setiv(sv, (IV)dowarn);
79072805
LW
439 break;
440 case '1': case '2': case '3': case '4':
441 case '5': case '6': case '7': case '8': case '9': case '&':
bbce6d69 442 if (curpm && (rx = curpm->op_pmregexp)) {
a863c7d1
MB
443 /*
444 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
445 * XXX Does the new way break anything?
446 */
447 paren = atoi(mg->mg_ptr);
79072805 448 getparen:
bbce6d69
PP
449 if (paren <= rx->nparens &&
450 (s = rx->startp[paren]) &&
451 (t = rx->endp[paren]))
452 {
748a9306 453 i = t - s;
13f57bf8 454 getrx:
748a9306 455 if (i >= 0) {
13f57bf8
CS
456 bool was_tainted;
457 if (tainting) {
458 was_tainted = tainted;
459 tainted = FALSE;
460 }
79072805 461 sv_setpvn(sv,s,i);
13f57bf8 462 if (tainting)
c277df42 463 tainted = was_tainted || RX_MATCH_TAINTED(rx);
748a9306
LW
464 break;
465 }
79072805 466 }
79072805 467 }
748a9306 468 sv_setsv(sv,&sv_undef);
79072805
LW
469 break;
470 case '+':
bbce6d69
PP
471 if (curpm && (rx = curpm->op_pmregexp)) {
472 paren = rx->lastparen;
a0d0e21e
LW
473 if (paren)
474 goto getparen;
79072805 475 }
748a9306 476 sv_setsv(sv,&sv_undef);
79072805
LW
477 break;
478 case '`':
bbce6d69 479 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 480 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 481 i = rx->startp[0] - s;
13f57bf8 482 goto getrx;
79072805 483 }
79072805 484 }
748a9306 485 sv_setsv(sv,&sv_undef);
79072805
LW
486 break;
487 case '\'':
bbce6d69 488 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
489 if (rx->subend && (s = rx->endp[0])) {
490 i = rx->subend - s;
491 goto getrx;
79072805 492 }
79072805 493 }
748a9306 494 sv_setsv(sv,&sv_undef);
79072805
LW
495 break;
496 case '.':
497#ifndef lint
a0d0e21e 498 if (GvIO(last_in_gv)) {
188ea221 499 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
79072805
LW
500 }
501#endif
502 break;
503 case '?':
809a5acc
MB
504 {
505 dTHR;
506 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 507#ifdef COMPLEX_STATUS
809a5acc
MB
508 LvTARGOFF(sv) = statusvalue;
509 LvTARGLEN(sv) = statusvalue_vms;
ff0cee69 510#endif
809a5acc 511 }
79072805
LW
512 break;
513 case '^':
a0d0e21e 514 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
515 if (s)
516 sv_setpv(sv,s);
517 else {
518 sv_setpv(sv,GvENAME(defoutgv));
519 sv_catpv(sv,"_TOP");
520 }
521 break;
522 case '~':
a0d0e21e 523 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
524 if (!s)
525 s = GvENAME(defoutgv);
526 sv_setpv(sv,s);
527 break;
528#ifndef lint
529 case '=':
188ea221 530 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
531 break;
532 case '-':
188ea221 533 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
534 break;
535 case '%':
188ea221 536 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
79072805
LW
537 break;
538#endif
539 case ':':
540 break;
541 case '/':
542 break;
543 case '[':
0f15f207 544 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
79072805
LW
545 break;
546 case '|':
188ea221 547 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
548 break;
549 case ',':
550 sv_setpvn(sv,ofs,ofslen);
551 break;
552 case '\\':
553 sv_setpvn(sv,ors,orslen);
554 break;
555 case '#':
556 sv_setpv(sv,ofmt);
557 break;
558 case '!':
a5f75d66 559#ifdef VMS
946ec16e 560 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 561 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 562#else
88e89b8a
PP
563 {
564 int saveerrno = errno;
946ec16e 565 sv_setnv(sv, (double)errno);
88e89b8a
PP
566#ifdef OS2
567 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
568 else
a5f75d66 569#endif
2304df62 570 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
571 errno = saveerrno;
572 }
573#endif
946ec16e 574 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
575 break;
576 case '<':
188ea221 577 sv_setiv(sv, (IV)uid);
79072805
LW
578 break;
579 case '>':
188ea221 580 sv_setiv(sv, (IV)euid);
79072805
LW
581 break;
582 case '(':
188ea221 583 sv_setiv(sv, (IV)gid);
fc36a67e 584 sv_setpvf(sv, "%Vd", (IV)gid);
79072805
LW
585 goto add_groups;
586 case ')':
188ea221 587 sv_setiv(sv, (IV)egid);
fc36a67e 588 sv_setpvf(sv, "%Vd", (IV)egid);
79072805 589 add_groups:
79072805 590#ifdef HAS_GETGROUPS
79072805 591 {
a0d0e21e 592 Groups_t gary[NGROUPS];
79072805 593 i = getgroups(NGROUPS,gary);
46fc3d4c 594 while (--i >= 0)
fc36a67e 595 sv_catpvf(sv, " %Vd", (IV)gary[i]);
79072805
LW
596 }
597#endif
29355cf7 598 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
599 break;
600 case '*':
601 break;
602 case '0':
603 break;
a863c7d1
MB
604#ifdef USE_THREADS
605 case '@':
38a03e6e 606 sv_setsv(sv, thr->errsv);
a863c7d1
MB
607 break;
608#endif /* USE_THREADS */
79072805 609 }
a0d0e21e 610 return 0;
79072805
LW
611}
612
613int
8ac85365 614magic_getuvar(SV *sv, MAGIC *mg)
79072805
LW
615{
616 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
617
618 if (uf && uf->uf_val)
619 (*uf->uf_val)(uf->uf_index, sv);
620 return 0;
621}
622
623int
8ac85365 624magic_setenv(SV *sv, MAGIC *mg)
79072805
LW
625{
626 register char *s;
88e89b8a 627 char *ptr;
5aabfad6 628 STRLEN len, klen;
a0d0e21e 629 I32 i;
1e422769 630
a0d0e21e 631 s = SvPV(sv,len);
5aabfad6 632 ptr = MgPV(mg,klen);
88e89b8a 633 my_setenv(ptr, s);
1e422769 634
a0d0e21e
LW
635#ifdef DYNAMIC_ENV_FETCH
636 /* We just undefd an environment var. Is a replacement */
637 /* waiting in the wings? */
638 if (!len) {
5aabfad6
PP
639 SV **valp;
640 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
641 s = SvPV(*valp, len);
a0d0e21e
LW
642 }
643#endif
1e422769 644
39e571d4 645#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
646 /* And you'll never guess what the dog had */
647 /* in its mouth... */
463ee0b2 648 if (tainting) {
1e422769
PP
649 MgTAINTEDDIR_off(mg);
650#ifdef VMS
5aabfad6 651 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
652 char pathbuf[256], eltbuf[256], *cp, *elt = s;
653 struct stat sbuf;
654 int i = 0, j = 0;
655
656 do { /* DCL$PATH may be a search list */
657 while (1) { /* as may dev portion of any element */
658 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
659 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
660 cando_by_name(S_IWUSR,0,elt) ) {
661 MgTAINTEDDIR_on(mg);
662 return 0;
663 }
664 }
665 if ((cp = strchr(elt, ':')) != Nullch)
666 *cp = '\0';
667 if (my_trnlnm(elt, eltbuf, j++))
668 elt = eltbuf;
669 else
670 break;
671 }
672 j = 0;
673 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
674 }
675#endif /* VMS */
5aabfad6 676 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 677 char *strend = s + len;
463ee0b2
LW
678
679 while (s < strend) {
96827780 680 char tmpbuf[256];
1e422769 681 struct stat st;
96827780 682 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 683 s, strend, ':', &i);
463ee0b2 684 s++;
96827780
MB
685 if (i >= sizeof tmpbuf /* too long -- assume the worst */
686 || *tmpbuf != '/'
687 || (Stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 688 MgTAINTEDDIR_on(mg);
1e422769
PP
689 return 0;
690 }
463ee0b2 691 }
79072805
LW
692 }
693 }
39e571d4 694#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 695
79072805
LW
696 return 0;
697}
698
699int
8ac85365 700magic_clearenv(SV *sv, MAGIC *mg)
85e6fe83 701{
5aabfad6 702 my_setenv(MgPV(mg,na),Nullch);
85e6fe83
LW
703 return 0;
704}
705
88e89b8a 706int
8ac85365 707magic_set_all_env(SV *sv, MAGIC *mg)
fb73857a
PP
708{
709#if defined(VMS)
710 die("Can't make list assignment to %%ENV on this system");
711#else
d58bf5aa 712 dTHR;
fb73857a
PP
713 if (localizing) {
714 HE* entry;
715 magic_clear_all_env(sv,mg);
716 hv_iterinit((HV*)sv);
717 while (entry = hv_iternext((HV*)sv)) {
718 I32 keylen;
719 my_setenv(hv_iterkey(entry, &keylen),
720 SvPV(hv_iterval((HV*)sv, entry), na));
721 }
722 }
723#endif
724 return 0;
725}
726
727int
8ac85365 728magic_clear_all_env(SV *sv, MAGIC *mg)
66b1d557 729{
3e3baf6d
TB
730#if defined(VMS)
731 die("Can't make list assignment to %%ENV on this system");
732#else
733#ifdef WIN32
734 char *envv = GetEnvironmentStrings();
735 char *cur = envv;
736 STRLEN len;
737 while (*cur) {
738 char *end = strchr(cur,'=');
739 if (end && end != cur) {
740 *end = '\0';
741 my_setenv(cur,Nullch);
742 *end = '=';
743 cur += strlen(end+1)+1;
744 }
745 else if ((len = strlen(cur)))
746 cur += len+1;
747 }
748 FreeEnvironmentStrings(envv);
66b1d557
HM
749#else
750 I32 i;
751
752 if (environ == origenviron)
753 New(901, environ, 1, char*);
754 else
755 for (i = 0; environ[i]; i++)
756 Safefree(environ[i]);
757 environ[0] = Nullch;
758
66b1d557 759#endif
3e3baf6d
TB
760#endif
761 return 0;
66b1d557
HM
762}
763
764int
8ac85365 765magic_getsig(SV *sv, MAGIC *mg)
88e89b8a
PP
766{
767 I32 i;
768 /* Are we fetching a signal entry? */
5aabfad6 769 i = whichsig(MgPV(mg,na));
88e89b8a
PP
770 if (i) {
771 if(psig_ptr[i])
772 sv_setsv(sv,psig_ptr[i]);
773 else {
ff68c719
PP
774 Sighandler_t sigstate = rsignal_state(i);
775
88e89b8a 776 /* cache state so we don't fetch it again */
ff68c719 777 if(sigstate == SIG_IGN)
88e89b8a
PP
778 sv_setpv(sv,"IGNORE");
779 else
780 sv_setsv(sv,&sv_undef);
781 psig_ptr[i] = SvREFCNT_inc(sv);
782 SvTEMP_off(sv);
783 }
784 }
785 return 0;
786}
787int
8ac85365 788magic_clearsig(SV *sv, MAGIC *mg)
88e89b8a
PP
789{
790 I32 i;
791 /* Are we clearing a signal entry? */
5aabfad6 792 i = whichsig(MgPV(mg,na));
88e89b8a
PP
793 if (i) {
794 if(psig_ptr[i]) {
795 SvREFCNT_dec(psig_ptr[i]);
796 psig_ptr[i]=0;
797 }
798 if(psig_name[i]) {
799 SvREFCNT_dec(psig_name[i]);
800 psig_name[i]=0;
801 }
802 }
803 return 0;
804}
3d37d572 805
85e6fe83 806int
8ac85365 807magic_setsig(SV *sv, MAGIC *mg)
79072805 808{
11343788 809 dTHR;
79072805
LW
810 register char *s;
811 I32 i;
748a9306 812 SV** svp;
a0d0e21e 813
5aabfad6 814 s = MgPV(mg,na);
748a9306
LW
815 if (*s == '_') {
816 if (strEQ(s,"__DIE__"))
817 svp = &diehook;
818 else if (strEQ(s,"__WARN__"))
819 svp = &warnhook;
820 else if (strEQ(s,"__PARSE__"))
821 svp = &parsehook;
822 else
823 croak("No such hook: %s", s);
824 i = 0;
4633a7c4
LW
825 if (*svp) {
826 SvREFCNT_dec(*svp);
827 *svp = 0;
828 }
748a9306
LW
829 }
830 else {
831 i = whichsig(s); /* ...no, a brick */
832 if (!i) {
833 if (dowarn || strEQ(s,"ALARM"))
834 warn("No such signal: SIG%s", s);
835 return 0;
836 }
ff0cee69
PP
837 SvREFCNT_dec(psig_name[i]);
838 SvREFCNT_dec(psig_ptr[i]);
88e89b8a 839 psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 840 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
ff0cee69 841 psig_name[i] = newSVpv(s, strlen(s));
88e89b8a 842 SvREADONLY_on(psig_name[i]);
748a9306 843 }
a0d0e21e 844 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 845 if (i)
c23142e2 846 (void)rsignal(i, sighandlerp);
748a9306
LW
847 else
848 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
849 return 0;
850 }
851 s = SvPV_force(sv,na);
748a9306
LW
852 if (strEQ(s,"IGNORE")) {
853 if (i)
ff68c719 854 (void)rsignal(i, SIG_IGN);
748a9306
LW
855 else
856 *svp = 0;
857 }
858 else if (strEQ(s,"DEFAULT") || !*s) {
859 if (i)
ff68c719 860 (void)rsignal(i, SIG_DFL);
748a9306
LW
861 else
862 *svp = 0;
863 }
79072805 864 else {
5aabfad6
PP
865 /*
866 * We should warn if HINT_STRICT_REFS, but without
867 * access to a known hint bit in a known OP, we can't
868 * tell whether HINT_STRICT_REFS is in force or not.
869 */
46fc3d4c
PP
870 if (!strchr(s,':') && !strchr(s,'\''))
871 sv_setpv(sv, form("main::%s", s));
748a9306 872 if (i)
c23142e2 873 (void)rsignal(i, sighandlerp);
748a9306
LW
874 else
875 *svp = SvREFCNT_inc(sv);
79072805
LW
876 }
877 return 0;
878}
879
880int
8ac85365 881magic_setisa(SV *sv, MAGIC *mg)
79072805 882{
a0231f0e
MB
883 HV *stash;
884 SV **svp;
885 I32 fill;
886 HV *basefields = Nullhv;
887 GV **gvp;
888 GV *gv;
889 HE *he;
890 static char *FIELDS = "FIELDS";
891
463ee0b2 892 sub_generation++;
a0231f0e
MB
893
894 if (mg->mg_type == 'i')
895 return 0; /* Ignore lower-case version of the magic */
896
897 stash = GvSTASH(mg->mg_obj);
898 svp = AvARRAY((AV*)sv);
93965878
NIS
899
900 /* NOTE: No support for tied ISA */
901 for (fill = AvFILLp((AV*)sv); fill >= 0; fill--, svp++) {
a0231f0e
MB
902 HV *basestash = gv_stashsv(*svp, FALSE);
903
904 if (!basestash) {
905 if (dowarn)
906 warn("No such package \"%_\" in @ISA assignment", *svp);
907 continue;
908 }
909 gvp = (GV**)hv_fetch(basestash, FIELDS, 6, FALSE);
910 if (gvp && *gvp && GvHV(*gvp)) {
911 if (basefields)
912 croak("Can't multiply inherit %%FIELDS");
913 basefields = GvHV(*gvp);
914 }
915 }
916
917 if (!basefields)
918 return 0;
919
920 gv = (GV*)*hv_fetch(stash, FIELDS, 6, TRUE);
921 if (!isGV(gv))
922 gv_init(gv, stash, FIELDS, 6, TRUE);
923 if (!GvHV(gv))
924 GvHV(gv) = newHV();
925 if (HvKEYS(GvHV(gv)))
926 croak("Inherited %%FIELDS can't override existing %%FIELDS");
927
928 hv_iterinit(GvHV(gv));
929 while ((he = hv_iternext(basefields)))
930 hv_store(GvHV(gv), HeKEY(he), HeKLEN(he), HeVAL(he), HeHASH(he));
931
463ee0b2
LW
932 return 0;
933}
934
a0d0e21e
LW
935#ifdef OVERLOAD
936
463ee0b2 937int
8ac85365 938magic_setamagic(SV *sv, MAGIC *mg)
463ee0b2 939{
a0d0e21e
LW
940 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
941 amagic_generation++;
463ee0b2 942
a0d0e21e
LW
943 return 0;
944}
945#endif /* OVERLOAD */
463ee0b2 946
946ec16e 947int
8ac85365 948magic_setnkeys(SV *sv, MAGIC *mg)
946ec16e
PP
949{
950 if (LvTARG(sv)) {
951 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
952 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
953 }
954 return 0;
93965878 955}
946ec16e 956
a0d0e21e 957static int
93965878 958magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
959{
960 dSP;
463ee0b2 961
924508f0
GS
962 PUSHMARK(SP);
963 EXTEND(SP, n);
a0d0e21e 964 PUSHs(mg->mg_obj);
93965878
NIS
965 if (n > 1) {
966 if (mg->mg_ptr) {
967 if (mg->mg_len >= 0)
968 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
969 else if (mg->mg_len == HEf_SVKEY)
970 PUSHs((SV*)mg->mg_ptr);
971 }
972 else if (mg->mg_type == 'p') {
973 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
974 }
975 }
976 if (n > 2) {
977 PUSHs(val);
88e89b8a 978 }
463ee0b2
LW
979 PUTBACK;
980
93965878
NIS
981 return perl_call_method(meth, flags);
982}
983
984static int
985magic_methpack(SV *sv, MAGIC *mg, char *meth)
986{
987 dSP;
988
989 ENTER;
990 SAVETMPS;
991
992 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
a0d0e21e 993 sv_setsv(sv, *stack_sp--);
93965878 994 }
463ee0b2 995
a0d0e21e
LW
996 FREETMPS;
997 LEAVE;
998 return 0;
999}
463ee0b2 1000
a0d0e21e 1001int
8ac85365 1002magic_getpack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1003{
1004 magic_methpack(sv,mg,"FETCH");
1005 if (mg->mg_ptr)
1006 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1007 return 0;
1008}
1009
1010int
8ac85365 1011magic_setpack(SV *sv, MAGIC *mg)
a60c0954
NIS
1012{
1013 ENTER;
93965878 1014 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
a60c0954 1015 LEAVE;
463ee0b2
LW
1016 return 0;
1017}
1018
1019int
8ac85365 1020magic_clearpack(SV *sv, MAGIC *mg)
463ee0b2 1021{
a0d0e21e
LW
1022 return magic_methpack(sv,mg,"DELETE");
1023}
463ee0b2 1024
93965878
NIS
1025
1026U32
1027magic_sizepack(SV *sv, MAGIC *mg)
1028{
1029 dTHR;
1030 U32 retval = 0;
1031
1032 ENTER;
1033 SAVETMPS;
1034 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1035 sv = *stack_sp--;
a60c0954 1036 retval = (U32) SvIV(sv)-1;
93965878
NIS
1037 }
1038 FREETMPS;
1039 LEAVE;
1040 return retval;
1041}
1042
8ac85365 1043int magic_wipepack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1044{
1045 dSP;
463ee0b2 1046
924508f0 1047 PUSHMARK(SP);
a0d0e21e 1048 XPUSHs(mg->mg_obj);
463ee0b2 1049 PUTBACK;
a60c0954 1050 ENTER;
a0d0e21e 1051 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
a60c0954 1052 LEAVE;
463ee0b2
LW
1053 return 0;
1054}
1055
1056int
8ac85365 1057magic_nextpack(SV *sv, MAGIC *mg, SV *key)
463ee0b2 1058{
463ee0b2 1059 dSP;
a0d0e21e 1060 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1061
1062 ENTER;
a0d0e21e 1063 SAVETMPS;
924508f0
GS
1064 PUSHMARK(SP);
1065 EXTEND(SP, 2);
a0d0e21e 1066 PUSHs(mg->mg_obj);
463ee0b2
LW
1067 if (SvOK(key))
1068 PUSHs(key);
1069 PUTBACK;
1070
a0d0e21e
LW
1071 if (perl_call_method(meth, G_SCALAR))
1072 sv_setsv(key, *stack_sp--);
463ee0b2 1073
a0d0e21e
LW
1074 FREETMPS;
1075 LEAVE;
79072805
LW
1076 return 0;
1077}
1078
1079int
8ac85365 1080magic_existspack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1081{
1082 return magic_methpack(sv,mg,"EXISTS");
1083}
1084
1085int
8ac85365 1086magic_setdbline(SV *sv, MAGIC *mg)
79072805 1087{
11343788 1088 dTHR;
79072805
LW
1089 OP *o;
1090 I32 i;
1091 GV* gv;
1092 SV** svp;
1093
1094 gv = DBline;
1095 i = SvTRUE(sv);
188ea221 1096 svp = av_fetch(GvAV(gv),
5aabfad6 1097 atoi(MgPV(mg,na)), FALSE);
8990e307 1098 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 1099 o->op_private = i;
79072805
LW
1100 else
1101 warn("Can't break at that line\n");
1102 return 0;
1103}
1104
1105int
8ac85365 1106magic_getarylen(SV *sv, MAGIC *mg)
79072805 1107{
0f15f207 1108 dTHR;
a0d0e21e 1109 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
1110 return 0;
1111}
1112
1113int
8ac85365 1114magic_setarylen(SV *sv, MAGIC *mg)
79072805 1115{
0f15f207 1116 dTHR;
a0d0e21e
LW
1117 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1118 return 0;
1119}
1120
1121int
8ac85365 1122magic_getpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1123{
1124 SV* lsv = LvTARG(sv);
1125
1126 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1127 mg = mg_find(lsv, 'g');
1128 if (mg && mg->mg_len >= 0) {
0f15f207 1129 dTHR;
a0d0e21e
LW
1130 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1131 return 0;
1132 }
1133 }
1134 (void)SvOK_off(sv);
1135 return 0;
1136}
1137
1138int
8ac85365 1139magic_setpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1140{
1141 SV* lsv = LvTARG(sv);
1142 SSize_t pos;
1143 STRLEN len;
1144
1145 mg = 0;
1146
1147 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1148 mg = mg_find(lsv, 'g');
1149 if (!mg) {
1150 if (!SvOK(sv))
1151 return 0;
1152 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1153 mg = mg_find(lsv, 'g');
1154 }
1155 else if (!SvOK(sv)) {
1156 mg->mg_len = -1;
1157 return 0;
1158 }
1159 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1160
0f15f207 1161 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
a0d0e21e
LW
1162 if (pos < 0) {
1163 pos += len;
1164 if (pos < 0)
1165 pos = 0;
1166 }
1167 else if (pos > len)
1168 pos = len;
1169 mg->mg_len = pos;
71be2cbc 1170 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1171
79072805
LW
1172 return 0;
1173}
1174
1175int
8ac85365 1176magic_getglob(SV *sv, MAGIC *mg)
79072805 1177{
8646b087
PP
1178 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1179 SvFAKE_off(sv);
946ec16e 1180 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1181 SvFAKE_on(sv);
1182 }
1183 else
946ec16e 1184 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1185 return 0;
1186}
1187
1188int
8ac85365 1189magic_setglob(SV *sv, MAGIC *mg)
79072805
LW
1190{
1191 register char *s;
1192 GV* gv;
1193
1194 if (!SvOK(sv))
1195 return 0;
463ee0b2 1196 s = SvPV(sv, na);
79072805
LW
1197 if (*s == '*' && s[1])
1198 s++;
85e6fe83 1199 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1200 if (sv == (SV*)gv)
1201 return 0;
1202 if (GvGP(sv))
88e89b8a 1203 gp_free((GV*)sv);
79072805 1204 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1205 return 0;
1206}
1207
1208int
8ac85365 1209magic_setsubstr(SV *sv, MAGIC *mg)
79072805 1210{
8990e307
LW
1211 STRLEN len;
1212 char *tmps = SvPV(sv,len);
1213 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1214 return 0;
1215}
1216
1217int
8ac85365 1218magic_gettaint(SV *sv, MAGIC *mg)
463ee0b2 1219{
a863c7d1 1220 dTHR;
bbce6d69
PP
1221 TAINT_IF((mg->mg_len & 1) ||
1222 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1223 return 0;
1224}
1225
1226int
8ac85365 1227magic_settaint(SV *sv, MAGIC *mg)
463ee0b2 1228{
11343788 1229 dTHR;
748a9306
LW
1230 if (localizing) {
1231 if (localizing == 1)
1232 mg->mg_len <<= 1;
1233 else
1234 mg->mg_len >>= 1;
a0d0e21e 1235 }
748a9306
LW
1236 else if (tainted)
1237 mg->mg_len |= 1;
1238 else
1239 mg->mg_len &= ~1;
463ee0b2
LW
1240 return 0;
1241}
1242
1243int
8ac85365 1244magic_setvec(SV *sv, MAGIC *mg)
79072805
LW
1245{
1246 do_vecset(sv); /* XXX slurp this routine */
1247 return 0;
1248}
1249
1250int
8ac85365 1251magic_getdefelem(SV *sv, MAGIC *mg)
5f05dabc 1252{
71be2cbc 1253 SV *targ = Nullsv;
5f05dabc 1254 if (LvTARGLEN(sv)) {
68dc0745
PP
1255 if (mg->mg_obj) {
1256 HV* hv = (HV*)LvTARG(sv);
1257 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1258 if (he)
1259 targ = HeVAL(he);
1260 }
1261 else {
93965878 1262 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1263 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1264 targ = AvARRAY(av)[LvTARGOFF(sv)];
1265 }
1266 if (targ && targ != &sv_undef) {
e858de61 1267 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1268 /* somebody else defined it for us */
1269 SvREFCNT_dec(LvTARG(sv));
1270 LvTARG(sv) = SvREFCNT_inc(targ);
1271 LvTARGLEN(sv) = 0;
1272 SvREFCNT_dec(mg->mg_obj);
1273 mg->mg_obj = Nullsv;
1274 mg->mg_flags &= ~MGf_REFCOUNTED;
1275 }
5f05dabc 1276 }
71be2cbc
PP
1277 else
1278 targ = LvTARG(sv);
1279 sv_setsv(sv, targ ? targ : &sv_undef);
1280 return 0;
1281}
1282
1283int
8ac85365 1284magic_setdefelem(SV *sv, MAGIC *mg)
71be2cbc
PP
1285{
1286 if (LvTARGLEN(sv))
68dc0745
PP
1287 vivify_defelem(sv);
1288 if (LvTARG(sv)) {
5f05dabc 1289 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1290 SvSETMAGIC(LvTARG(sv));
1291 }
5f05dabc
PP
1292 return 0;
1293}
1294
1295int
8ac85365 1296magic_freedefelem(SV *sv, MAGIC *mg)
5f05dabc
PP
1297{
1298 SvREFCNT_dec(LvTARG(sv));
71be2cbc
PP
1299 return 0;
1300}
1301
1302void
8ac85365 1303vivify_defelem(SV *sv)
71be2cbc 1304{
e858de61 1305 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
68dc0745
PP
1306 MAGIC* mg;
1307 SV* value;
71be2cbc 1308
68dc0745 1309 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1310 return;
68dc0745
PP
1311 if (mg->mg_obj) {
1312 HV* hv = (HV*)LvTARG(sv);
1313 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1314 if (!he || (value = HeVAL(he)) == &sv_undef)
1315 croak(no_helem, SvPV(mg->mg_obj, na));
71be2cbc 1316 }
68dc0745
PP
1317 else {
1318 AV* av = (AV*)LvTARG(sv);
5aabfad6 1319 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1320 LvTARG(sv) = Nullsv; /* array can't be extended */
1321 else {
1322 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1323 if (!svp || (value = *svp) == &sv_undef)
1324 croak(no_aelem, (I32)LvTARGOFF(sv));
1325 }
1326 }
3e3baf6d 1327 (void)SvREFCNT_inc(value);
68dc0745
PP
1328 SvREFCNT_dec(LvTARG(sv));
1329 LvTARG(sv) = value;
71be2cbc 1330 LvTARGLEN(sv) = 0;
68dc0745
PP
1331 SvREFCNT_dec(mg->mg_obj);
1332 mg->mg_obj = Nullsv;
1333 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1334}
1335
1336int
8ac85365 1337magic_setmglob(SV *sv, MAGIC *mg)
93a17b20 1338{
a0d0e21e 1339 mg->mg_len = -1;
c6496cc7 1340 SvSCREAM_off(sv);
93a17b20
LW
1341 return 0;
1342}
1343
1344int
8ac85365 1345magic_setbm(SV *sv, MAGIC *mg)
79072805 1346{
463ee0b2 1347 sv_unmagic(sv, 'B');
79072805
LW
1348 SvVALID_off(sv);
1349 return 0;
1350}
1351
1352int
8ac85365 1353magic_setfm(SV *sv, MAGIC *mg)
55497cff
PP
1354{
1355 sv_unmagic(sv, 'f');
1356 SvCOMPILED_off(sv);
1357 return 0;
1358}
1359
1360int
8ac85365 1361magic_setuvar(SV *sv, MAGIC *mg)
79072805
LW
1362{
1363 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1364
1365 if (uf && uf->uf_set)
1366 (*uf->uf_set)(uf->uf_index, sv);
1367 return 0;
1368}
1369
c277df42
IZ
1370int
1371magic_freeregexp(SV *sv, MAGIC *mg)
1372{
1373 regexp *re = (regexp *)mg->mg_obj;
1374 ReREFCNT_dec(re);
1375 return 0;
1376}
1377
7a4c00b4 1378#ifdef USE_LOCALE_COLLATE
79072805 1379int
8ac85365 1380magic_setcollxfrm(SV *sv, MAGIC *mg)
bbce6d69
PP
1381{
1382 /*
1383 * René Descartes said "I think not."
1384 * and vanished with a faint plop.
1385 */
7a4c00b4
PP
1386 if (mg->mg_ptr) {
1387 Safefree(mg->mg_ptr);
1388 mg->mg_ptr = NULL;
1389 mg->mg_len = -1;
1390 }
bbce6d69
PP
1391 return 0;
1392}
7a4c00b4 1393#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1394
1395int
8ac85365 1396magic_set(SV *sv, MAGIC *mg)
79072805 1397{
11343788 1398 dTHR;
79072805
LW
1399 register char *s;
1400 I32 i;
8990e307 1401 STRLEN len;
79072805 1402 switch (*mg->mg_ptr) {
748a9306
LW
1403 case '\001': /* ^A */
1404 sv_setsv(bodytarget, sv);
1405 break;
79072805 1406 case '\004': /* ^D */
8990e307 1407 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1408 DEBUG_x(dump_all());
1409 break;
28f23441
PP
1410 case '\005': /* ^E */
1411#ifdef VMS
1412 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1413#else
22fae026
TM
1414#ifdef WIN32
1415 SetLastError( SvIV(sv) );
1416#else
f86702cc
PP
1417 /* will anyone ever use this? */
1418 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441 1419#endif
22fae026 1420#endif
28f23441 1421 break;
79072805 1422 case '\006': /* ^F */
463ee0b2 1423 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1424 break;
a0d0e21e
LW
1425 case '\010': /* ^H */
1426 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1427 break;
79072805
LW
1428 case '\t': /* ^I */
1429 if (inplace)
1430 Safefree(inplace);
1431 if (SvOK(sv))
a0d0e21e 1432 inplace = savepv(SvPV(sv,na));
79072805
LW
1433 else
1434 inplace = Nullch;
1435 break;
28f23441
PP
1436 case '\017': /* ^O */
1437 if (osname)
1438 Safefree(osname);
1439 if (SvOK(sv))
1440 osname = savepv(SvPV(sv,na));
1441 else
1442 osname = Nullch;
1443 break;
79072805 1444 case '\020': /* ^P */
84902520 1445 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1446 break;
1447 case '\024': /* ^T */
88e89b8a
PP
1448#ifdef BIG_TIME
1449 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1450#else
85e6fe83 1451 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1452#endif
79072805
LW
1453 break;
1454 case '\027': /* ^W */
463ee0b2 1455 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1456 break;
1457 case '.':
748a9306
LW
1458 if (localizing) {
1459 if (localizing == 1)
1460 save_sptr((SV**)&last_in_gv);
1461 }
88e89b8a 1462 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1463 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1464 break;
1465 case '^':
a0d0e21e
LW
1466 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1467 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1468 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1469 break;
1470 case '~':
a0d0e21e
LW
1471 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1472 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1473 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1474 break;
1475 case '=':
a0d0e21e 1476 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1477 break;
1478 case '-':
a0d0e21e
LW
1479 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1480 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1481 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1482 break;
1483 case '%':
a0d0e21e 1484 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1485 break;
1486 case '|':
4b65379b
CS
1487 {
1488 IO *io = GvIOp(defoutgv);
1489 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1490 IoFLAGS(io) &= ~IOf_FLUSH;
1491 else {
1492 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1493 PerlIO *ofp = IoOFP(io);
1494 if (ofp)
1495 (void)PerlIO_flush(ofp);
1496 IoFLAGS(io) |= IOf_FLUSH;
1497 }
1498 }
79072805
LW
1499 }
1500 break;
1501 case '*':
463ee0b2 1502 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1503 multiline = (i != 0);
1504 break;
1505 case '/':
c07a80fd
PP
1506 SvREFCNT_dec(nrs);
1507 nrs = newSVsv(sv);
1508 SvREFCNT_dec(rs);
1509 rs = SvREFCNT_inc(nrs);
79072805
LW
1510 break;
1511 case '\\':
1512 if (ors)
1513 Safefree(ors);
e3c19b7b
CS
1514 if (SvOK(sv) || SvGMAGICAL(sv))
1515 ors = savepv(SvPV(sv,orslen));
1516 else {
1517 ors = Nullch;
1518 orslen = 0;
1519 }
79072805
LW
1520 break;
1521 case ',':
1522 if (ofs)
1523 Safefree(ofs);
a0d0e21e 1524 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1525 break;
1526 case '#':
1527 if (ofmt)
1528 Safefree(ofmt);
a0d0e21e 1529 ofmt = savepv(SvPV(sv,na));
79072805
LW
1530 break;
1531 case '[':
a0d0e21e 1532 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1533 break;
1534 case '?':
ff0cee69
PP
1535#ifdef COMPLEX_STATUS
1536 if (localizing == 2) {
1537 statusvalue = LvTARGOFF(sv);
1538 statusvalue_vms = LvTARGLEN(sv);
1539 }
1540 else
1541#endif
1542#ifdef VMSISH_STATUS
1543 if (VMSISH_STATUS)
1544 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1545 else
1546#endif
1547 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1548 break;
1549 case '!':
78987ded 1550 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1551 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1552 break;
1553 case '<':
463ee0b2 1554 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1555 if (delaymagic) {
1556 delaymagic |= DM_RUID;
1557 break; /* don't do magic till later */
1558 }
1559#ifdef HAS_SETRUID
85e6fe83 1560 (void)setruid((Uid_t)uid);
79072805
LW
1561#else
1562#ifdef HAS_SETREUID
85e6fe83 1563 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1564#else
85e6fe83
LW
1565#ifdef HAS_SETRESUID
1566 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1567#else
1568 if (uid == euid) /* special case $< = $> */
1569 (void)setuid(uid);
a0d0e21e
LW
1570 else {
1571 uid = (I32)getuid();
463ee0b2 1572 croak("setruid() not implemented");
a0d0e21e 1573 }
79072805
LW
1574#endif
1575#endif
85e6fe83 1576#endif
748a9306 1577 uid = (I32)getuid();
4633a7c4 1578 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1579 break;
1580 case '>':
463ee0b2 1581 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1582 if (delaymagic) {
1583 delaymagic |= DM_EUID;
1584 break; /* don't do magic till later */
1585 }
1586#ifdef HAS_SETEUID
85e6fe83 1587 (void)seteuid((Uid_t)euid);
79072805
LW
1588#else
1589#ifdef HAS_SETREUID
85e6fe83
LW
1590 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1591#else
1592#ifdef HAS_SETRESUID
1593 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1594#else
1595 if (euid == uid) /* special case $> = $< */
1596 setuid(euid);
a0d0e21e
LW
1597 else {
1598 euid = (I32)geteuid();
463ee0b2 1599 croak("seteuid() not implemented");
a0d0e21e 1600 }
79072805
LW
1601#endif
1602#endif
85e6fe83 1603#endif
79072805 1604 euid = (I32)geteuid();
4633a7c4 1605 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1606 break;
1607 case '(':
463ee0b2 1608 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1609 if (delaymagic) {
1610 delaymagic |= DM_RGID;
1611 break; /* don't do magic till later */
1612 }
1613#ifdef HAS_SETRGID
85e6fe83 1614 (void)setrgid((Gid_t)gid);
79072805
LW
1615#else
1616#ifdef HAS_SETREGID
85e6fe83
LW
1617 (void)setregid((Gid_t)gid, (Gid_t)-1);
1618#else
1619#ifdef HAS_SETRESGID
1620 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1621#else
1622 if (gid == egid) /* special case $( = $) */
1623 (void)setgid(gid);
748a9306
LW
1624 else {
1625 gid = (I32)getgid();
463ee0b2 1626 croak("setrgid() not implemented");
748a9306 1627 }
79072805
LW
1628#endif
1629#endif
85e6fe83 1630#endif
79072805 1631 gid = (I32)getgid();
4633a7c4 1632 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1633 break;
1634 case ')':
5cd24f17
PP
1635#ifdef HAS_SETGROUPS
1636 {
1637 char *p = SvPV(sv, na);
1638 Groups_t gary[NGROUPS];
1639
1640 SET_NUMERIC_STANDARD();
1641 while (isSPACE(*p))
1642 ++p;
1643 egid = I_V(atof(p));
1644 for (i = 0; i < NGROUPS; ++i) {
1645 while (*p && !isSPACE(*p))
1646 ++p;
1647 while (isSPACE(*p))
1648 ++p;
1649 if (!*p)
1650 break;
1651 gary[i] = I_V(atof(p));
1652 }
8cc95fdb
PP
1653 if (i)
1654 (void)setgroups(i, gary);
5cd24f17
PP
1655 }
1656#else /* HAS_SETGROUPS */
463ee0b2 1657 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1658#endif /* HAS_SETGROUPS */
79072805
LW
1659 if (delaymagic) {
1660 delaymagic |= DM_EGID;
1661 break; /* don't do magic till later */
1662 }
1663#ifdef HAS_SETEGID
85e6fe83 1664 (void)setegid((Gid_t)egid);
79072805
LW
1665#else
1666#ifdef HAS_SETREGID
85e6fe83
LW
1667 (void)setregid((Gid_t)-1, (Gid_t)egid);
1668#else
1669#ifdef HAS_SETRESGID
1670 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1671#else
1672 if (egid == gid) /* special case $) = $( */
1673 (void)setgid(egid);
748a9306
LW
1674 else {
1675 egid = (I32)getegid();
463ee0b2 1676 croak("setegid() not implemented");
748a9306 1677 }
79072805
LW
1678#endif
1679#endif
85e6fe83 1680#endif
79072805 1681 egid = (I32)getegid();
4633a7c4 1682 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1683 break;
1684 case ':':
a0d0e21e 1685 chopset = SvPV_force(sv,na);
79072805
LW
1686 break;
1687 case '0':
1688 if (!origalen) {
1689 s = origargv[0];
1690 s += strlen(s);
1691 /* See if all the arguments are contiguous in memory */
1692 for (i = 1; i < origargc; i++) {
fb73857a
PP
1693 if (origargv[i] == s + 1
1694#ifdef OS2
1695 || origargv[i] == s + 2
1696#endif
1697 )
79072805 1698 s += strlen(++s); /* this one is ok too */
fb73857a
PP
1699 else
1700 break;
79072805 1701 }
bbce6d69 1702 /* can grab env area too? */
fb73857a
PP
1703 if (origenviron && (origenviron[0] == s + 1
1704#ifdef OS2
1705 || (origenviron[0] == s + 9 && (s += 8))
1706#endif
1707 )) {
66b1d557 1708 my_setenv("NoNe SuCh", Nullch);
79072805
LW
1709 /* force copy of environment */
1710 for (i = 0; origenviron[i]; i++)
1711 if (origenviron[i] == s + 1)
1712 s += strlen(++s);
fb73857a
PP
1713 else
1714 break;
79072805
LW
1715 }
1716 origalen = s - origargv[0];
1717 }
a0d0e21e 1718 s = SvPV_force(sv,len);
8990e307 1719 i = len;
79072805
LW
1720 if (i >= origalen) {
1721 i = origalen;
fb73857a
PP
1722 /* don't allow system to limit $0 seen by script */
1723 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
79072805 1724 Copy(s, origargv[0], i, char);
fb73857a
PP
1725 s = origargv[0]+i;
1726 *s = '\0';
79072805
LW
1727 }
1728 else {
1729 Copy(s, origargv[0], i, char);
1730 s = origargv[0]+i;
1731 *s++ = '\0';
1732 while (++i < origalen)
8990e307
LW
1733 *s++ = ' ';
1734 s = origargv[0]+i;
ed6116ce 1735 for (i = 1; i < origargc; i++)
8990e307 1736 origargv[i] = Nullch;
79072805
LW
1737 }
1738 break;
a863c7d1
MB
1739#ifdef USE_THREADS
1740 case '@':
38a03e6e 1741 sv_setsv(thr->errsv, sv);
a863c7d1
MB
1742 break;
1743#endif /* USE_THREADS */
79072805
LW
1744 }
1745 return 0;
1746}
1747
f93b4edd
MB
1748#ifdef USE_THREADS
1749int
8ac85365 1750magic_mutexfree(SV *sv, MAGIC *mg)
f93b4edd
MB
1751{
1752 dTHR;
bc1f4c86
MB
1753 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1754 (unsigned long)thr, (unsigned long)sv);)
f93b4edd
MB
1755 if (MgOWNER(mg))
1756 croak("panic: magic_mutexfree");
1757 MUTEX_DESTROY(MgMUTEXP(mg));
1758 COND_DESTROY(MgCONDP(mg));
e55aaa0e 1759 SvREFCNT_dec(sv);
f93b4edd
MB
1760 return 0;
1761}
1762#endif /* USE_THREADS */
1763
79072805 1764I32
8ac85365 1765whichsig(char *sig)
79072805
LW
1766{
1767 register char **sigv;
1768
1769 for (sigv = sig_name+1; *sigv; sigv++)
1770 if (strEQ(sig,*sigv))
8e07c86e 1771 return sig_num[sigv - sig_name];
79072805
LW
1772#ifdef SIGCLD
1773 if (strEQ(sig,"CHLD"))
1774 return SIGCLD;
1775#endif
1776#ifdef SIGCHLD
1777 if (strEQ(sig,"CLD"))
1778 return SIGCHLD;
1779#endif
1780 return 0;
1781}
1782
84902520
TB
1783static SV* sig_sv;
1784
1785static void
8ac85365 1786unwind_handler_stack(void *p)
84902520 1787{
ff26ac79 1788 dTHR;
84902520
TB
1789 U32 flags = *(U32*)p;
1790
1791 if (flags & 1)
1792 savestack_ix -= 5; /* Unprotect save in progress. */
1793 /* cxstack_ix-- Not needed, die already unwound it. */
1794 if (flags & 64)
1795 SvREFCNT_dec(sig_sv);
1796}
1797
ecfc5424 1798Signal_t
8ac85365 1799sighandler(int sig)
79072805
LW
1800{
1801 dSP;
00d579c5 1802 GV *gv = Nullgv;
a0d0e21e 1803 HV *st;
84902520 1804 SV *sv, *tSv = Sv;
00d579c5 1805 CV *cv = Nullcv;
79072805 1806 AV *oldstack;
84902520
TB
1807 OP *myop = op;
1808 U32 flags = 0;
1809 I32 o_save_i = savestack_ix, type;
c09156bb 1810 PERL_CONTEXT *cx;
84902520
TB
1811 XPV *tXpv = Xpv;
1812
1813 if (savestack_ix + 15 <= savestack_max)
1814 flags |= 1;
1815 if (cxstack_ix < cxstack_max - 2)
1816 flags |= 2;
1817 if (markstack_ptr < markstack_max - 2)
1818 flags |= 4;
1819 if (retstack_ix < retstack_max - 2)
1820 flags |= 8;
1821 if (scopestack_ix < scopestack_max - 3)
1822 flags |= 16;
1823
1824 if (flags & 2) { /* POPBLOCK may decrease cxstack too early. */
1825 cxstack_ix++; /* Protect from overwrite. */
1826 cx = &cxstack[cxstack_ix];
1827 type = cx->cx_type; /* Can be during partial write. */
1828 cx->cx_type = CXt_NULL; /* Make it safe for unwind. */
1829 }
ff0cee69
PP
1830 if (!psig_ptr[sig])
1831 die("Signal SIG%s received, but no signal handler set.\n",
1832 sig_name[sig]);
1833
84902520
TB
1834 /* Max number of items pushed there is 3*n or 4. We cannot fix
1835 infinity, so we fix 4 (in fact 5): */
1836 if (flags & 1) {
1837 savestack_ix += 5; /* Protect save in progress. */
1838 o_save_i = savestack_ix;
1839 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1840 }
1841 if (flags & 4)
1842 markstack_ptr++; /* Protect mark. */
1843 if (flags & 8) {
1844 retstack_ix++;
1845 retstack[retstack_ix] = NULL;
1846 }
1847 if (flags & 16)
1848 scopestack_ix += 1;
1849 /* sv_2cv is too complicated, try a simpler variant first: */
1850 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1851 || SvTYPE(cv) != SVt_PVCV)
1852 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1853
a0d0e21e 1854 if (!cv || !CvROOT(cv)) {
79072805
LW
1855 if (dowarn)
1856 warn("SIG%s handler \"%s\" not defined.\n",
00d579c5
GS
1857 sig_name[sig], (gv ? GvENAME(gv)
1858 : ((cv && CvGV(cv))
1859 ? GvENAME(CvGV(cv))
1860 : "__ANON__")));
1861 goto cleanup;
79072805
LW
1862 }
1863
88e89b8a
PP
1864 oldstack = curstack;
1865 if (curstack != signalstack)
93965878 1866 AvFILLp(signalstack) = 0;
88e89b8a 1867 SWITCHSTACK(curstack, signalstack);
79072805 1868
84902520 1869 if(psig_name[sig]) {
88e89b8a 1870 sv = SvREFCNT_inc(psig_name[sig]);
84902520
TB
1871 flags |= 64;
1872 sig_sv = sv;
1873 } else {
ff0cee69
PP
1874 sv = sv_newmortal();
1875 sv_setpv(sv,sig_name[sig]);
88e89b8a 1876 }
924508f0 1877 PUSHMARK(SP);
79072805 1878 PUSHs(sv);
79072805 1879 PUTBACK;
a0d0e21e
LW
1880
1881 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1882
1883 SWITCHSTACK(signalstack, oldstack);
00d579c5 1884cleanup:
84902520
TB
1885 if (flags & 1)
1886 savestack_ix -= 8; /* Unprotect save in progress. */
1887 if (flags & 2) {
1888 cxstack[cxstack_ix].cx_type = type;
1889 cxstack_ix -= 1;
1890 }
1891 if (flags & 4)
1892 markstack_ptr--;
1893 if (flags & 8)
1894 retstack_ix--;
1895 if (flags & 16)
1896 scopestack_ix -= 1;
1897 if (flags & 64)
1898 SvREFCNT_dec(sv);
1899 op = myop; /* Apparently not needed... */
1900
1901 Sv = tSv; /* Restore global temporaries. */
1902 Xpv = tXpv;
79072805
LW
1903 return;
1904}
4e35701f
NIS
1905
1906