This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] merge change#905 from maintbranch, minor fixes to get
[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)
48c036b1
GS
463 tainted = (was_tainted || RX_MATCH_TAINTED(rx) ||
464 (curpm->op_pmflags & PMf_TAINTMEM));
748a9306
LW
465 break;
466 }
79072805 467 }
79072805 468 }
748a9306 469 sv_setsv(sv,&sv_undef);
79072805
LW
470 break;
471 case '+':
bbce6d69
PP
472 if (curpm && (rx = curpm->op_pmregexp)) {
473 paren = rx->lastparen;
a0d0e21e
LW
474 if (paren)
475 goto getparen;
79072805 476 }
748a9306 477 sv_setsv(sv,&sv_undef);
79072805
LW
478 break;
479 case '`':
bbce6d69 480 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 481 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 482 i = rx->startp[0] - s;
13f57bf8 483 goto getrx;
79072805 484 }
79072805 485 }
748a9306 486 sv_setsv(sv,&sv_undef);
79072805
LW
487 break;
488 case '\'':
bbce6d69 489 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
490 if (rx->subend && (s = rx->endp[0])) {
491 i = rx->subend - s;
492 goto getrx;
79072805 493 }
79072805 494 }
748a9306 495 sv_setsv(sv,&sv_undef);
79072805
LW
496 break;
497 case '.':
498#ifndef lint
a0d0e21e 499 if (GvIO(last_in_gv)) {
188ea221 500 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
79072805
LW
501 }
502#endif
503 break;
504 case '?':
809a5acc
MB
505 {
506 dTHR;
507 sv_setiv(sv, (IV)STATUS_CURRENT);
ff0cee69 508#ifdef COMPLEX_STATUS
809a5acc
MB
509 LvTARGOFF(sv) = statusvalue;
510 LvTARGLEN(sv) = statusvalue_vms;
ff0cee69 511#endif
809a5acc 512 }
79072805
LW
513 break;
514 case '^':
a0d0e21e 515 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
516 if (s)
517 sv_setpv(sv,s);
518 else {
519 sv_setpv(sv,GvENAME(defoutgv));
520 sv_catpv(sv,"_TOP");
521 }
522 break;
523 case '~':
a0d0e21e 524 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
525 if (!s)
526 s = GvENAME(defoutgv);
527 sv_setpv(sv,s);
528 break;
529#ifndef lint
530 case '=':
188ea221 531 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
532 break;
533 case '-':
188ea221 534 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
535 break;
536 case '%':
188ea221 537 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
79072805
LW
538 break;
539#endif
540 case ':':
541 break;
542 case '/':
543 break;
544 case '[':
0f15f207 545 WITH_THR(sv_setiv(sv, (IV)curcop->cop_arybase));
79072805
LW
546 break;
547 case '|':
188ea221 548 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
549 break;
550 case ',':
551 sv_setpvn(sv,ofs,ofslen);
552 break;
553 case '\\':
554 sv_setpvn(sv,ors,orslen);
555 break;
556 case '#':
557 sv_setpv(sv,ofmt);
558 break;
559 case '!':
a5f75d66 560#ifdef VMS
946ec16e 561 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 562 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 563#else
88e89b8a
PP
564 {
565 int saveerrno = errno;
946ec16e 566 sv_setnv(sv, (double)errno);
88e89b8a
PP
567#ifdef OS2
568 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
569 else
a5f75d66 570#endif
2304df62 571 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
572 errno = saveerrno;
573 }
574#endif
946ec16e 575 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
576 break;
577 case '<':
188ea221 578 sv_setiv(sv, (IV)uid);
79072805
LW
579 break;
580 case '>':
188ea221 581 sv_setiv(sv, (IV)euid);
79072805
LW
582 break;
583 case '(':
188ea221 584 sv_setiv(sv, (IV)gid);
fc36a67e 585 sv_setpvf(sv, "%Vd", (IV)gid);
79072805
LW
586 goto add_groups;
587 case ')':
188ea221 588 sv_setiv(sv, (IV)egid);
fc36a67e 589 sv_setpvf(sv, "%Vd", (IV)egid);
79072805 590 add_groups:
79072805 591#ifdef HAS_GETGROUPS
79072805 592 {
a0d0e21e 593 Groups_t gary[NGROUPS];
79072805 594 i = getgroups(NGROUPS,gary);
46fc3d4c 595 while (--i >= 0)
fc36a67e 596 sv_catpvf(sv, " %Vd", (IV)gary[i]);
79072805
LW
597 }
598#endif
29355cf7 599 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
600 break;
601 case '*':
602 break;
603 case '0':
604 break;
a863c7d1
MB
605#ifdef USE_THREADS
606 case '@':
38a03e6e 607 sv_setsv(sv, thr->errsv);
a863c7d1
MB
608 break;
609#endif /* USE_THREADS */
79072805 610 }
a0d0e21e 611 return 0;
79072805
LW
612}
613
614int
8ac85365 615magic_getuvar(SV *sv, MAGIC *mg)
79072805
LW
616{
617 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
618
619 if (uf && uf->uf_val)
620 (*uf->uf_val)(uf->uf_index, sv);
621 return 0;
622}
623
624int
8ac85365 625magic_setenv(SV *sv, MAGIC *mg)
79072805
LW
626{
627 register char *s;
88e89b8a 628 char *ptr;
5aabfad6 629 STRLEN len, klen;
a0d0e21e 630 I32 i;
1e422769 631
a0d0e21e 632 s = SvPV(sv,len);
5aabfad6 633 ptr = MgPV(mg,klen);
88e89b8a 634 my_setenv(ptr, s);
1e422769 635
a0d0e21e
LW
636#ifdef DYNAMIC_ENV_FETCH
637 /* We just undefd an environment var. Is a replacement */
638 /* waiting in the wings? */
639 if (!len) {
5aabfad6
PP
640 SV **valp;
641 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
642 s = SvPV(*valp, len);
a0d0e21e
LW
643 }
644#endif
1e422769 645
39e571d4 646#if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
79072805
LW
647 /* And you'll never guess what the dog had */
648 /* in its mouth... */
463ee0b2 649 if (tainting) {
1e422769
PP
650 MgTAINTEDDIR_off(mg);
651#ifdef VMS
5aabfad6 652 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
653 char pathbuf[256], eltbuf[256], *cp, *elt = s;
654 struct stat sbuf;
655 int i = 0, j = 0;
656
657 do { /* DCL$PATH may be a search list */
658 while (1) { /* as may dev portion of any element */
659 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
660 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
661 cando_by_name(S_IWUSR,0,elt) ) {
662 MgTAINTEDDIR_on(mg);
663 return 0;
664 }
665 }
666 if ((cp = strchr(elt, ':')) != Nullch)
667 *cp = '\0';
668 if (my_trnlnm(elt, eltbuf, j++))
669 elt = eltbuf;
670 else
671 break;
672 }
673 j = 0;
674 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
675 }
676#endif /* VMS */
5aabfad6 677 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 678 char *strend = s + len;
463ee0b2
LW
679
680 while (s < strend) {
96827780 681 char tmpbuf[256];
1e422769 682 struct stat st;
96827780 683 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
fc36a67e 684 s, strend, ':', &i);
463ee0b2 685 s++;
96827780
MB
686 if (i >= sizeof tmpbuf /* too long -- assume the worst */
687 || *tmpbuf != '/'
c6ed36e1 688 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 689 MgTAINTEDDIR_on(mg);
1e422769
PP
690 return 0;
691 }
463ee0b2 692 }
79072805
LW
693 }
694 }
39e571d4 695#endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1e422769 696
79072805
LW
697 return 0;
698}
699
700int
8ac85365 701magic_clearenv(SV *sv, MAGIC *mg)
85e6fe83 702{
5aabfad6 703 my_setenv(MgPV(mg,na),Nullch);
85e6fe83
LW
704 return 0;
705}
706
88e89b8a 707int
8ac85365 708magic_set_all_env(SV *sv, MAGIC *mg)
fb73857a
PP
709{
710#if defined(VMS)
711 die("Can't make list assignment to %%ENV on this system");
712#else
d58bf5aa 713 dTHR;
fb73857a
PP
714 if (localizing) {
715 HE* entry;
716 magic_clear_all_env(sv,mg);
717 hv_iterinit((HV*)sv);
718 while (entry = hv_iternext((HV*)sv)) {
719 I32 keylen;
720 my_setenv(hv_iterkey(entry, &keylen),
721 SvPV(hv_iterval((HV*)sv, entry), na));
722 }
723 }
724#endif
725 return 0;
726}
727
728int
8ac85365 729magic_clear_all_env(SV *sv, MAGIC *mg)
66b1d557 730{
3e3baf6d
TB
731#if defined(VMS)
732 die("Can't make list assignment to %%ENV on this system");
733#else
734#ifdef WIN32
735 char *envv = GetEnvironmentStrings();
736 char *cur = envv;
737 STRLEN len;
738 while (*cur) {
739 char *end = strchr(cur,'=');
740 if (end && end != cur) {
741 *end = '\0';
742 my_setenv(cur,Nullch);
743 *end = '=';
744 cur += strlen(end+1)+1;
745 }
746 else if ((len = strlen(cur)))
747 cur += len+1;
748 }
749 FreeEnvironmentStrings(envv);
66b1d557
HM
750#else
751 I32 i;
752
753 if (environ == origenviron)
754 New(901, environ, 1, char*);
755 else
756 for (i = 0; environ[i]; i++)
757 Safefree(environ[i]);
758 environ[0] = Nullch;
759
66b1d557 760#endif
3e3baf6d
TB
761#endif
762 return 0;
66b1d557
HM
763}
764
765int
8ac85365 766magic_getsig(SV *sv, MAGIC *mg)
88e89b8a
PP
767{
768 I32 i;
769 /* Are we fetching a signal entry? */
5aabfad6 770 i = whichsig(MgPV(mg,na));
88e89b8a
PP
771 if (i) {
772 if(psig_ptr[i])
773 sv_setsv(sv,psig_ptr[i]);
774 else {
ff68c719
PP
775 Sighandler_t sigstate = rsignal_state(i);
776
88e89b8a 777 /* cache state so we don't fetch it again */
ff68c719 778 if(sigstate == SIG_IGN)
88e89b8a
PP
779 sv_setpv(sv,"IGNORE");
780 else
781 sv_setsv(sv,&sv_undef);
782 psig_ptr[i] = SvREFCNT_inc(sv);
783 SvTEMP_off(sv);
784 }
785 }
786 return 0;
787}
788int
8ac85365 789magic_clearsig(SV *sv, MAGIC *mg)
88e89b8a
PP
790{
791 I32 i;
792 /* Are we clearing a signal entry? */
5aabfad6 793 i = whichsig(MgPV(mg,na));
88e89b8a
PP
794 if (i) {
795 if(psig_ptr[i]) {
796 SvREFCNT_dec(psig_ptr[i]);
797 psig_ptr[i]=0;
798 }
799 if(psig_name[i]) {
800 SvREFCNT_dec(psig_name[i]);
801 psig_name[i]=0;
802 }
803 }
804 return 0;
805}
3d37d572 806
85e6fe83 807int
8ac85365 808magic_setsig(SV *sv, MAGIC *mg)
79072805 809{
11343788 810 dTHR;
79072805
LW
811 register char *s;
812 I32 i;
748a9306 813 SV** svp;
a0d0e21e 814
5aabfad6 815 s = MgPV(mg,na);
748a9306
LW
816 if (*s == '_') {
817 if (strEQ(s,"__DIE__"))
818 svp = &diehook;
819 else if (strEQ(s,"__WARN__"))
820 svp = &warnhook;
821 else if (strEQ(s,"__PARSE__"))
822 svp = &parsehook;
823 else
824 croak("No such hook: %s", s);
825 i = 0;
4633a7c4
LW
826 if (*svp) {
827 SvREFCNT_dec(*svp);
828 *svp = 0;
829 }
748a9306
LW
830 }
831 else {
832 i = whichsig(s); /* ...no, a brick */
833 if (!i) {
834 if (dowarn || strEQ(s,"ALARM"))
835 warn("No such signal: SIG%s", s);
836 return 0;
837 }
ff0cee69
PP
838 SvREFCNT_dec(psig_name[i]);
839 SvREFCNT_dec(psig_ptr[i]);
88e89b8a 840 psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 841 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
ff0cee69 842 psig_name[i] = newSVpv(s, strlen(s));
88e89b8a 843 SvREADONLY_on(psig_name[i]);
748a9306 844 }
a0d0e21e 845 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 846 if (i)
c23142e2 847 (void)rsignal(i, sighandlerp);
748a9306
LW
848 else
849 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
850 return 0;
851 }
852 s = SvPV_force(sv,na);
748a9306
LW
853 if (strEQ(s,"IGNORE")) {
854 if (i)
ff68c719 855 (void)rsignal(i, SIG_IGN);
748a9306
LW
856 else
857 *svp = 0;
858 }
859 else if (strEQ(s,"DEFAULT") || !*s) {
860 if (i)
ff68c719 861 (void)rsignal(i, SIG_DFL);
748a9306
LW
862 else
863 *svp = 0;
864 }
79072805 865 else {
5aabfad6
PP
866 /*
867 * We should warn if HINT_STRICT_REFS, but without
868 * access to a known hint bit in a known OP, we can't
869 * tell whether HINT_STRICT_REFS is in force or not.
870 */
46fc3d4c
PP
871 if (!strchr(s,':') && !strchr(s,'\''))
872 sv_setpv(sv, form("main::%s", s));
748a9306 873 if (i)
c23142e2 874 (void)rsignal(i, sighandlerp);
748a9306
LW
875 else
876 *svp = SvREFCNT_inc(sv);
79072805
LW
877 }
878 return 0;
879}
880
881int
8ac85365 882magic_setisa(SV *sv, MAGIC *mg)
79072805 883{
a0231f0e
MB
884 HV *stash;
885 SV **svp;
886 I32 fill;
887 HV *basefields = Nullhv;
888 GV **gvp;
889 GV *gv;
890 HE *he;
891 static char *FIELDS = "FIELDS";
892
463ee0b2 893 sub_generation++;
a0231f0e
MB
894
895 if (mg->mg_type == 'i')
896 return 0; /* Ignore lower-case version of the magic */
897
898 stash = GvSTASH(mg->mg_obj);
899 svp = AvARRAY((AV*)sv);
93965878
NIS
900
901 /* NOTE: No support for tied ISA */
902 for (fill = AvFILLp((AV*)sv); fill >= 0; fill--, svp++) {
a0231f0e
MB
903 HV *basestash = gv_stashsv(*svp, FALSE);
904
905 if (!basestash) {
906 if (dowarn)
907 warn("No such package \"%_\" in @ISA assignment", *svp);
908 continue;
909 }
910 gvp = (GV**)hv_fetch(basestash, FIELDS, 6, FALSE);
911 if (gvp && *gvp && GvHV(*gvp)) {
912 if (basefields)
913 croak("Can't multiply inherit %%FIELDS");
914 basefields = GvHV(*gvp);
915 }
916 }
917
918 if (!basefields)
919 return 0;
920
921 gv = (GV*)*hv_fetch(stash, FIELDS, 6, TRUE);
922 if (!isGV(gv))
923 gv_init(gv, stash, FIELDS, 6, TRUE);
924 if (!GvHV(gv))
925 GvHV(gv) = newHV();
926 if (HvKEYS(GvHV(gv)))
927 croak("Inherited %%FIELDS can't override existing %%FIELDS");
928
929 hv_iterinit(GvHV(gv));
930 while ((he = hv_iternext(basefields)))
931 hv_store(GvHV(gv), HeKEY(he), HeKLEN(he), HeVAL(he), HeHASH(he));
932
463ee0b2
LW
933 return 0;
934}
935
a0d0e21e
LW
936#ifdef OVERLOAD
937
463ee0b2 938int
8ac85365 939magic_setamagic(SV *sv, MAGIC *mg)
463ee0b2 940{
a0d0e21e
LW
941 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
942 amagic_generation++;
463ee0b2 943
a0d0e21e
LW
944 return 0;
945}
946#endif /* OVERLOAD */
463ee0b2 947
946ec16e 948int
8ac85365 949magic_setnkeys(SV *sv, MAGIC *mg)
946ec16e
PP
950{
951 if (LvTARG(sv)) {
952 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
953 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
954 }
955 return 0;
93965878 956}
946ec16e 957
e336de0d 958/* caller is responsible for stack switching/cleanup */
a0d0e21e 959static int
93965878 960magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
961{
962 dSP;
463ee0b2 963
924508f0
GS
964 PUSHMARK(SP);
965 EXTEND(SP, n);
a0d0e21e 966 PUSHs(mg->mg_obj);
93965878
NIS
967 if (n > 1) {
968 if (mg->mg_ptr) {
969 if (mg->mg_len >= 0)
970 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
971 else if (mg->mg_len == HEf_SVKEY)
972 PUSHs((SV*)mg->mg_ptr);
973 }
974 else if (mg->mg_type == 'p') {
975 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
976 }
977 }
978 if (n > 2) {
979 PUSHs(val);
88e89b8a 980 }
463ee0b2
LW
981 PUTBACK;
982
93965878
NIS
983 return perl_call_method(meth, flags);
984}
985
986static int
987magic_methpack(SV *sv, MAGIC *mg, char *meth)
988{
989 dSP;
990
991 ENTER;
992 SAVETMPS;
e336de0d 993 PUSHSTACK(SI_MAGIC);
93965878
NIS
994
995 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
a0d0e21e 996 sv_setsv(sv, *stack_sp--);
93965878 997 }
463ee0b2 998
e336de0d 999 POPSTACK();
a0d0e21e
LW
1000 FREETMPS;
1001 LEAVE;
1002 return 0;
1003}
463ee0b2 1004
a0d0e21e 1005int
8ac85365 1006magic_getpack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1007{
1008 magic_methpack(sv,mg,"FETCH");
1009 if (mg->mg_ptr)
1010 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1011 return 0;
1012}
1013
1014int
8ac85365 1015magic_setpack(SV *sv, MAGIC *mg)
e336de0d
GS
1016{
1017 dSP;
a60c0954 1018 ENTER;
e336de0d 1019 PUSHSTACK(SI_MAGIC);
93965878 1020 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
e336de0d 1021 POPSTACK();
a60c0954 1022 LEAVE;
463ee0b2
LW
1023 return 0;
1024}
1025
1026int
8ac85365 1027magic_clearpack(SV *sv, MAGIC *mg)
463ee0b2 1028{
a0d0e21e
LW
1029 return magic_methpack(sv,mg,"DELETE");
1030}
463ee0b2 1031
93965878
NIS
1032
1033U32
1034magic_sizepack(SV *sv, MAGIC *mg)
1035{
e336de0d 1036 dSP;
93965878
NIS
1037 U32 retval = 0;
1038
1039 ENTER;
1040 SAVETMPS;
e336de0d 1041 PUSHSTACK(SI_MAGIC);
93965878
NIS
1042 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1043 sv = *stack_sp--;
a60c0954 1044 retval = (U32) SvIV(sv)-1;
93965878 1045 }
e336de0d 1046 POPSTACK();
93965878
NIS
1047 FREETMPS;
1048 LEAVE;
1049 return retval;
1050}
1051
8ac85365 1052int magic_wipepack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1053{
1054 dSP;
463ee0b2 1055
e336de0d
GS
1056 ENTER;
1057 PUSHSTACK(SI_MAGIC);
924508f0 1058 PUSHMARK(SP);
a0d0e21e 1059 XPUSHs(mg->mg_obj);
463ee0b2 1060 PUTBACK;
a0d0e21e 1061 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
e336de0d 1062 POPSTACK();
a60c0954 1063 LEAVE;
463ee0b2
LW
1064 return 0;
1065}
1066
1067int
8ac85365 1068magic_nextpack(SV *sv, MAGIC *mg, SV *key)
463ee0b2 1069{
463ee0b2 1070 dSP;
a0d0e21e 1071 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1072
1073 ENTER;
a0d0e21e 1074 SAVETMPS;
e336de0d 1075 PUSHSTACK(SI_MAGIC);
924508f0
GS
1076 PUSHMARK(SP);
1077 EXTEND(SP, 2);
a0d0e21e 1078 PUSHs(mg->mg_obj);
463ee0b2
LW
1079 if (SvOK(key))
1080 PUSHs(key);
1081 PUTBACK;
1082
a0d0e21e
LW
1083 if (perl_call_method(meth, G_SCALAR))
1084 sv_setsv(key, *stack_sp--);
463ee0b2 1085
e336de0d 1086 POPSTACK();
a0d0e21e
LW
1087 FREETMPS;
1088 LEAVE;
79072805
LW
1089 return 0;
1090}
1091
1092int
8ac85365 1093magic_existspack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1094{
1095 return magic_methpack(sv,mg,"EXISTS");
1096}
1097
1098int
8ac85365 1099magic_setdbline(SV *sv, MAGIC *mg)
79072805 1100{
11343788 1101 dTHR;
79072805
LW
1102 OP *o;
1103 I32 i;
1104 GV* gv;
1105 SV** svp;
1106
1107 gv = DBline;
1108 i = SvTRUE(sv);
188ea221 1109 svp = av_fetch(GvAV(gv),
5aabfad6 1110 atoi(MgPV(mg,na)), FALSE);
8990e307 1111 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 1112 o->op_private = i;
79072805
LW
1113 else
1114 warn("Can't break at that line\n");
1115 return 0;
1116}
1117
1118int
8ac85365 1119magic_getarylen(SV *sv, MAGIC *mg)
79072805 1120{
0f15f207 1121 dTHR;
a0d0e21e 1122 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
1123 return 0;
1124}
1125
1126int
8ac85365 1127magic_setarylen(SV *sv, MAGIC *mg)
79072805 1128{
0f15f207 1129 dTHR;
a0d0e21e
LW
1130 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1131 return 0;
1132}
1133
1134int
8ac85365 1135magic_getpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1136{
1137 SV* lsv = LvTARG(sv);
1138
1139 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1140 mg = mg_find(lsv, 'g');
1141 if (mg && mg->mg_len >= 0) {
0f15f207 1142 dTHR;
a0d0e21e
LW
1143 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1144 return 0;
1145 }
1146 }
1147 (void)SvOK_off(sv);
1148 return 0;
1149}
1150
1151int
8ac85365 1152magic_setpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1153{
1154 SV* lsv = LvTARG(sv);
1155 SSize_t pos;
1156 STRLEN len;
1157
1158 mg = 0;
1159
1160 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1161 mg = mg_find(lsv, 'g');
1162 if (!mg) {
1163 if (!SvOK(sv))
1164 return 0;
1165 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1166 mg = mg_find(lsv, 'g');
1167 }
1168 else if (!SvOK(sv)) {
1169 mg->mg_len = -1;
1170 return 0;
1171 }
1172 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1173
0f15f207 1174 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
a0d0e21e
LW
1175 if (pos < 0) {
1176 pos += len;
1177 if (pos < 0)
1178 pos = 0;
1179 }
1180 else if (pos > len)
1181 pos = len;
1182 mg->mg_len = pos;
71be2cbc 1183 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1184
79072805
LW
1185 return 0;
1186}
1187
1188int
8ac85365 1189magic_getglob(SV *sv, MAGIC *mg)
79072805 1190{
8646b087
PP
1191 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1192 SvFAKE_off(sv);
946ec16e 1193 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1194 SvFAKE_on(sv);
1195 }
1196 else
946ec16e 1197 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1198 return 0;
1199}
1200
1201int
8ac85365 1202magic_setglob(SV *sv, MAGIC *mg)
79072805
LW
1203{
1204 register char *s;
1205 GV* gv;
1206
1207 if (!SvOK(sv))
1208 return 0;
463ee0b2 1209 s = SvPV(sv, na);
79072805
LW
1210 if (*s == '*' && s[1])
1211 s++;
85e6fe83 1212 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1213 if (sv == (SV*)gv)
1214 return 0;
1215 if (GvGP(sv))
88e89b8a 1216 gp_free((GV*)sv);
79072805 1217 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1218 return 0;
1219}
1220
1221int
8ac85365 1222magic_setsubstr(SV *sv, MAGIC *mg)
79072805 1223{
8990e307
LW
1224 STRLEN len;
1225 char *tmps = SvPV(sv,len);
1226 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1227 return 0;
1228}
1229
1230int
8ac85365 1231magic_gettaint(SV *sv, MAGIC *mg)
463ee0b2 1232{
a863c7d1 1233 dTHR;
bbce6d69
PP
1234 TAINT_IF((mg->mg_len & 1) ||
1235 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1236 return 0;
1237}
1238
1239int
8ac85365 1240magic_settaint(SV *sv, MAGIC *mg)
463ee0b2 1241{
11343788 1242 dTHR;
748a9306
LW
1243 if (localizing) {
1244 if (localizing == 1)
1245 mg->mg_len <<= 1;
1246 else
1247 mg->mg_len >>= 1;
a0d0e21e 1248 }
748a9306
LW
1249 else if (tainted)
1250 mg->mg_len |= 1;
1251 else
1252 mg->mg_len &= ~1;
463ee0b2
LW
1253 return 0;
1254}
1255
1256int
8ac85365 1257magic_setvec(SV *sv, MAGIC *mg)
79072805
LW
1258{
1259 do_vecset(sv); /* XXX slurp this routine */
1260 return 0;
1261}
1262
1263int
8ac85365 1264magic_getdefelem(SV *sv, MAGIC *mg)
5f05dabc 1265{
71be2cbc 1266 SV *targ = Nullsv;
5f05dabc 1267 if (LvTARGLEN(sv)) {
68dc0745
PP
1268 if (mg->mg_obj) {
1269 HV* hv = (HV*)LvTARG(sv);
1270 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1271 if (he)
1272 targ = HeVAL(he);
1273 }
1274 else {
93965878 1275 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1276 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1277 targ = AvARRAY(av)[LvTARGOFF(sv)];
1278 }
1279 if (targ && targ != &sv_undef) {
e858de61 1280 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1281 /* somebody else defined it for us */
1282 SvREFCNT_dec(LvTARG(sv));
1283 LvTARG(sv) = SvREFCNT_inc(targ);
1284 LvTARGLEN(sv) = 0;
1285 SvREFCNT_dec(mg->mg_obj);
1286 mg->mg_obj = Nullsv;
1287 mg->mg_flags &= ~MGf_REFCOUNTED;
1288 }
5f05dabc 1289 }
71be2cbc
PP
1290 else
1291 targ = LvTARG(sv);
1292 sv_setsv(sv, targ ? targ : &sv_undef);
1293 return 0;
1294}
1295
1296int
8ac85365 1297magic_setdefelem(SV *sv, MAGIC *mg)
71be2cbc
PP
1298{
1299 if (LvTARGLEN(sv))
68dc0745
PP
1300 vivify_defelem(sv);
1301 if (LvTARG(sv)) {
5f05dabc 1302 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1303 SvSETMAGIC(LvTARG(sv));
1304 }
5f05dabc
PP
1305 return 0;
1306}
1307
1308int
8ac85365 1309magic_freedefelem(SV *sv, MAGIC *mg)
5f05dabc
PP
1310{
1311 SvREFCNT_dec(LvTARG(sv));
71be2cbc
PP
1312 return 0;
1313}
1314
1315void
8ac85365 1316vivify_defelem(SV *sv)
71be2cbc 1317{
e858de61 1318 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
68dc0745
PP
1319 MAGIC* mg;
1320 SV* value;
71be2cbc 1321
68dc0745 1322 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1323 return;
68dc0745
PP
1324 if (mg->mg_obj) {
1325 HV* hv = (HV*)LvTARG(sv);
1326 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1327 if (!he || (value = HeVAL(he)) == &sv_undef)
1328 croak(no_helem, SvPV(mg->mg_obj, na));
71be2cbc 1329 }
68dc0745
PP
1330 else {
1331 AV* av = (AV*)LvTARG(sv);
5aabfad6 1332 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1333 LvTARG(sv) = Nullsv; /* array can't be extended */
1334 else {
1335 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1336 if (!svp || (value = *svp) == &sv_undef)
1337 croak(no_aelem, (I32)LvTARGOFF(sv));
1338 }
1339 }
3e3baf6d 1340 (void)SvREFCNT_inc(value);
68dc0745
PP
1341 SvREFCNT_dec(LvTARG(sv));
1342 LvTARG(sv) = value;
71be2cbc 1343 LvTARGLEN(sv) = 0;
68dc0745
PP
1344 SvREFCNT_dec(mg->mg_obj);
1345 mg->mg_obj = Nullsv;
1346 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1347}
1348
1349int
8ac85365 1350magic_setmglob(SV *sv, MAGIC *mg)
93a17b20 1351{
a0d0e21e 1352 mg->mg_len = -1;
c6496cc7 1353 SvSCREAM_off(sv);
93a17b20
LW
1354 return 0;
1355}
1356
1357int
8ac85365 1358magic_setbm(SV *sv, MAGIC *mg)
79072805 1359{
463ee0b2 1360 sv_unmagic(sv, 'B');
79072805
LW
1361 SvVALID_off(sv);
1362 return 0;
1363}
1364
1365int
8ac85365 1366magic_setfm(SV *sv, MAGIC *mg)
55497cff
PP
1367{
1368 sv_unmagic(sv, 'f');
1369 SvCOMPILED_off(sv);
1370 return 0;
1371}
1372
1373int
8ac85365 1374magic_setuvar(SV *sv, MAGIC *mg)
79072805
LW
1375{
1376 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1377
1378 if (uf && uf->uf_set)
1379 (*uf->uf_set)(uf->uf_index, sv);
1380 return 0;
1381}
1382
c277df42
IZ
1383int
1384magic_freeregexp(SV *sv, MAGIC *mg)
1385{
1386 regexp *re = (regexp *)mg->mg_obj;
1387 ReREFCNT_dec(re);
1388 return 0;
1389}
1390
7a4c00b4 1391#ifdef USE_LOCALE_COLLATE
79072805 1392int
8ac85365 1393magic_setcollxfrm(SV *sv, MAGIC *mg)
bbce6d69
PP
1394{
1395 /*
1396 * René Descartes said "I think not."
1397 * and vanished with a faint plop.
1398 */
7a4c00b4
PP
1399 if (mg->mg_ptr) {
1400 Safefree(mg->mg_ptr);
1401 mg->mg_ptr = NULL;
1402 mg->mg_len = -1;
1403 }
bbce6d69
PP
1404 return 0;
1405}
7a4c00b4 1406#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1407
1408int
8ac85365 1409magic_set(SV *sv, MAGIC *mg)
79072805 1410{
11343788 1411 dTHR;
79072805
LW
1412 register char *s;
1413 I32 i;
8990e307 1414 STRLEN len;
79072805 1415 switch (*mg->mg_ptr) {
748a9306
LW
1416 case '\001': /* ^A */
1417 sv_setsv(bodytarget, sv);
1418 break;
79072805 1419 case '\004': /* ^D */
8990e307 1420 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1421 DEBUG_x(dump_all());
1422 break;
28f23441
PP
1423 case '\005': /* ^E */
1424#ifdef VMS
1425 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1426#else
22fae026
TM
1427#ifdef WIN32
1428 SetLastError( SvIV(sv) );
1429#else
f86702cc
PP
1430 /* will anyone ever use this? */
1431 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441 1432#endif
22fae026 1433#endif
28f23441 1434 break;
79072805 1435 case '\006': /* ^F */
463ee0b2 1436 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1437 break;
a0d0e21e
LW
1438 case '\010': /* ^H */
1439 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1440 break;
79072805
LW
1441 case '\t': /* ^I */
1442 if (inplace)
1443 Safefree(inplace);
1444 if (SvOK(sv))
a0d0e21e 1445 inplace = savepv(SvPV(sv,na));
79072805
LW
1446 else
1447 inplace = Nullch;
1448 break;
28f23441
PP
1449 case '\017': /* ^O */
1450 if (osname)
1451 Safefree(osname);
1452 if (SvOK(sv))
1453 osname = savepv(SvPV(sv,na));
1454 else
1455 osname = Nullch;
1456 break;
79072805 1457 case '\020': /* ^P */
84902520 1458 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1459 break;
1460 case '\024': /* ^T */
88e89b8a
PP
1461#ifdef BIG_TIME
1462 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1463#else
85e6fe83 1464 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1465#endif
79072805
LW
1466 break;
1467 case '\027': /* ^W */
463ee0b2 1468 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1469 break;
1470 case '.':
748a9306
LW
1471 if (localizing) {
1472 if (localizing == 1)
1473 save_sptr((SV**)&last_in_gv);
1474 }
88e89b8a 1475 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1476 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1477 break;
1478 case '^':
a0d0e21e
LW
1479 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1480 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1481 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1482 break;
1483 case '~':
a0d0e21e
LW
1484 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1485 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1486 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1487 break;
1488 case '=':
a0d0e21e 1489 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1490 break;
1491 case '-':
a0d0e21e
LW
1492 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1493 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1494 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1495 break;
1496 case '%':
a0d0e21e 1497 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1498 break;
1499 case '|':
4b65379b
CS
1500 {
1501 IO *io = GvIOp(defoutgv);
1502 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1503 IoFLAGS(io) &= ~IOf_FLUSH;
1504 else {
1505 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1506 PerlIO *ofp = IoOFP(io);
1507 if (ofp)
1508 (void)PerlIO_flush(ofp);
1509 IoFLAGS(io) |= IOf_FLUSH;
1510 }
1511 }
79072805
LW
1512 }
1513 break;
1514 case '*':
463ee0b2 1515 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1516 multiline = (i != 0);
1517 break;
1518 case '/':
c07a80fd
PP
1519 SvREFCNT_dec(nrs);
1520 nrs = newSVsv(sv);
1521 SvREFCNT_dec(rs);
1522 rs = SvREFCNT_inc(nrs);
79072805
LW
1523 break;
1524 case '\\':
1525 if (ors)
1526 Safefree(ors);
e3c19b7b
CS
1527 if (SvOK(sv) || SvGMAGICAL(sv))
1528 ors = savepv(SvPV(sv,orslen));
1529 else {
1530 ors = Nullch;
1531 orslen = 0;
1532 }
79072805
LW
1533 break;
1534 case ',':
1535 if (ofs)
1536 Safefree(ofs);
a0d0e21e 1537 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1538 break;
1539 case '#':
1540 if (ofmt)
1541 Safefree(ofmt);
a0d0e21e 1542 ofmt = savepv(SvPV(sv,na));
79072805
LW
1543 break;
1544 case '[':
a0d0e21e 1545 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1546 break;
1547 case '?':
ff0cee69
PP
1548#ifdef COMPLEX_STATUS
1549 if (localizing == 2) {
1550 statusvalue = LvTARGOFF(sv);
1551 statusvalue_vms = LvTARGLEN(sv);
1552 }
1553 else
1554#endif
1555#ifdef VMSISH_STATUS
1556 if (VMSISH_STATUS)
1557 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1558 else
1559#endif
1560 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1561 break;
1562 case '!':
78987ded 1563 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1564 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1565 break;
1566 case '<':
463ee0b2 1567 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1568 if (delaymagic) {
1569 delaymagic |= DM_RUID;
1570 break; /* don't do magic till later */
1571 }
1572#ifdef HAS_SETRUID
85e6fe83 1573 (void)setruid((Uid_t)uid);
79072805
LW
1574#else
1575#ifdef HAS_SETREUID
85e6fe83 1576 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1577#else
85e6fe83
LW
1578#ifdef HAS_SETRESUID
1579 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1580#else
1581 if (uid == euid) /* special case $< = $> */
1582 (void)setuid(uid);
a0d0e21e
LW
1583 else {
1584 uid = (I32)getuid();
463ee0b2 1585 croak("setruid() not implemented");
a0d0e21e 1586 }
79072805
LW
1587#endif
1588#endif
85e6fe83 1589#endif
748a9306 1590 uid = (I32)getuid();
4633a7c4 1591 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1592 break;
1593 case '>':
463ee0b2 1594 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1595 if (delaymagic) {
1596 delaymagic |= DM_EUID;
1597 break; /* don't do magic till later */
1598 }
1599#ifdef HAS_SETEUID
85e6fe83 1600 (void)seteuid((Uid_t)euid);
79072805
LW
1601#else
1602#ifdef HAS_SETREUID
85e6fe83
LW
1603 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1604#else
1605#ifdef HAS_SETRESUID
1606 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1607#else
1608 if (euid == uid) /* special case $> = $< */
1609 setuid(euid);
a0d0e21e
LW
1610 else {
1611 euid = (I32)geteuid();
463ee0b2 1612 croak("seteuid() not implemented");
a0d0e21e 1613 }
79072805
LW
1614#endif
1615#endif
85e6fe83 1616#endif
79072805 1617 euid = (I32)geteuid();
4633a7c4 1618 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1619 break;
1620 case '(':
463ee0b2 1621 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1622 if (delaymagic) {
1623 delaymagic |= DM_RGID;
1624 break; /* don't do magic till later */
1625 }
1626#ifdef HAS_SETRGID
85e6fe83 1627 (void)setrgid((Gid_t)gid);
79072805
LW
1628#else
1629#ifdef HAS_SETREGID
85e6fe83
LW
1630 (void)setregid((Gid_t)gid, (Gid_t)-1);
1631#else
1632#ifdef HAS_SETRESGID
1633 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1634#else
1635 if (gid == egid) /* special case $( = $) */
1636 (void)setgid(gid);
748a9306
LW
1637 else {
1638 gid = (I32)getgid();
463ee0b2 1639 croak("setrgid() not implemented");
748a9306 1640 }
79072805
LW
1641#endif
1642#endif
85e6fe83 1643#endif
79072805 1644 gid = (I32)getgid();
4633a7c4 1645 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1646 break;
1647 case ')':
5cd24f17
PP
1648#ifdef HAS_SETGROUPS
1649 {
1650 char *p = SvPV(sv, na);
1651 Groups_t gary[NGROUPS];
1652
1653 SET_NUMERIC_STANDARD();
1654 while (isSPACE(*p))
1655 ++p;
1656 egid = I_V(atof(p));
1657 for (i = 0; i < NGROUPS; ++i) {
1658 while (*p && !isSPACE(*p))
1659 ++p;
1660 while (isSPACE(*p))
1661 ++p;
1662 if (!*p)
1663 break;
1664 gary[i] = I_V(atof(p));
1665 }
8cc95fdb
PP
1666 if (i)
1667 (void)setgroups(i, gary);
5cd24f17
PP
1668 }
1669#else /* HAS_SETGROUPS */
463ee0b2 1670 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1671#endif /* HAS_SETGROUPS */
79072805
LW
1672 if (delaymagic) {
1673 delaymagic |= DM_EGID;
1674 break; /* don't do magic till later */
1675 }
1676#ifdef HAS_SETEGID
85e6fe83 1677 (void)setegid((Gid_t)egid);
79072805
LW
1678#else
1679#ifdef HAS_SETREGID
85e6fe83
LW
1680 (void)setregid((Gid_t)-1, (Gid_t)egid);
1681#else
1682#ifdef HAS_SETRESGID
1683 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1684#else
1685 if (egid == gid) /* special case $) = $( */
1686 (void)setgid(egid);
748a9306
LW
1687 else {
1688 egid = (I32)getegid();
463ee0b2 1689 croak("setegid() not implemented");
748a9306 1690 }
79072805
LW
1691#endif
1692#endif
85e6fe83 1693#endif
79072805 1694 egid = (I32)getegid();
4633a7c4 1695 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1696 break;
1697 case ':':
a0d0e21e 1698 chopset = SvPV_force(sv,na);
79072805
LW
1699 break;
1700 case '0':
1701 if (!origalen) {
1702 s = origargv[0];
1703 s += strlen(s);
1704 /* See if all the arguments are contiguous in memory */
1705 for (i = 1; i < origargc; i++) {
fb73857a
PP
1706 if (origargv[i] == s + 1
1707#ifdef OS2
1708 || origargv[i] == s + 2
1709#endif
1710 )
79072805 1711 s += strlen(++s); /* this one is ok too */
fb73857a
PP
1712 else
1713 break;
79072805 1714 }
bbce6d69 1715 /* can grab env area too? */
fb73857a
PP
1716 if (origenviron && (origenviron[0] == s + 1
1717#ifdef OS2
1718 || (origenviron[0] == s + 9 && (s += 8))
1719#endif
1720 )) {
66b1d557 1721 my_setenv("NoNe SuCh", Nullch);
79072805
LW
1722 /* force copy of environment */
1723 for (i = 0; origenviron[i]; i++)
1724 if (origenviron[i] == s + 1)
1725 s += strlen(++s);
fb73857a
PP
1726 else
1727 break;
79072805
LW
1728 }
1729 origalen = s - origargv[0];
1730 }
a0d0e21e 1731 s = SvPV_force(sv,len);
8990e307 1732 i = len;
79072805
LW
1733 if (i >= origalen) {
1734 i = origalen;
fb73857a
PP
1735 /* don't allow system to limit $0 seen by script */
1736 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
79072805 1737 Copy(s, origargv[0], i, char);
fb73857a
PP
1738 s = origargv[0]+i;
1739 *s = '\0';
79072805
LW
1740 }
1741 else {
1742 Copy(s, origargv[0], i, char);
1743 s = origargv[0]+i;
1744 *s++ = '\0';
1745 while (++i < origalen)
8990e307
LW
1746 *s++ = ' ';
1747 s = origargv[0]+i;
ed6116ce 1748 for (i = 1; i < origargc; i++)
8990e307 1749 origargv[i] = Nullch;
79072805
LW
1750 }
1751 break;
a863c7d1
MB
1752#ifdef USE_THREADS
1753 case '@':
38a03e6e 1754 sv_setsv(thr->errsv, sv);
a863c7d1
MB
1755 break;
1756#endif /* USE_THREADS */
79072805
LW
1757 }
1758 return 0;
1759}
1760
f93b4edd
MB
1761#ifdef USE_THREADS
1762int
8ac85365 1763magic_mutexfree(SV *sv, MAGIC *mg)
f93b4edd
MB
1764{
1765 dTHR;
bc1f4c86
MB
1766 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1767 (unsigned long)thr, (unsigned long)sv);)
f93b4edd
MB
1768 if (MgOWNER(mg))
1769 croak("panic: magic_mutexfree");
1770 MUTEX_DESTROY(MgMUTEXP(mg));
1771 COND_DESTROY(MgCONDP(mg));
e55aaa0e 1772 SvREFCNT_dec(sv);
f93b4edd
MB
1773 return 0;
1774}
1775#endif /* USE_THREADS */
1776
79072805 1777I32
8ac85365 1778whichsig(char *sig)
79072805
LW
1779{
1780 register char **sigv;
1781
1782 for (sigv = sig_name+1; *sigv; sigv++)
1783 if (strEQ(sig,*sigv))
8e07c86e 1784 return sig_num[sigv - sig_name];
79072805
LW
1785#ifdef SIGCLD
1786 if (strEQ(sig,"CHLD"))
1787 return SIGCLD;
1788#endif
1789#ifdef SIGCHLD
1790 if (strEQ(sig,"CLD"))
1791 return SIGCHLD;
1792#endif
1793 return 0;
1794}
1795
84902520
TB
1796static SV* sig_sv;
1797
1798static void
8ac85365 1799unwind_handler_stack(void *p)
84902520 1800{
ff26ac79 1801 dTHR;
84902520
TB
1802 U32 flags = *(U32*)p;
1803
1804 if (flags & 1)
1805 savestack_ix -= 5; /* Unprotect save in progress. */
1806 /* cxstack_ix-- Not needed, die already unwound it. */
1807 if (flags & 64)
1808 SvREFCNT_dec(sig_sv);
1809}
1810
ecfc5424 1811Signal_t
8ac85365 1812sighandler(int sig)
79072805
LW
1813{
1814 dSP;
00d579c5 1815 GV *gv = Nullgv;
a0d0e21e 1816 HV *st;
84902520 1817 SV *sv, *tSv = Sv;
00d579c5 1818 CV *cv = Nullcv;
84902520
TB
1819 OP *myop = op;
1820 U32 flags = 0;
1821 I32 o_save_i = savestack_ix, type;
84902520
TB
1822 XPV *tXpv = Xpv;
1823
1824 if (savestack_ix + 15 <= savestack_max)
1825 flags |= 1;
84902520
TB
1826 if (markstack_ptr < markstack_max - 2)
1827 flags |= 4;
1828 if (retstack_ix < retstack_max - 2)
1829 flags |= 8;
1830 if (scopestack_ix < scopestack_max - 3)
1831 flags |= 16;
1832
ff0cee69
PP
1833 if (!psig_ptr[sig])
1834 die("Signal SIG%s received, but no signal handler set.\n",
1835 sig_name[sig]);
1836
84902520
TB
1837 /* Max number of items pushed there is 3*n or 4. We cannot fix
1838 infinity, so we fix 4 (in fact 5): */
1839 if (flags & 1) {
1840 savestack_ix += 5; /* Protect save in progress. */
1841 o_save_i = savestack_ix;
1842 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1843 }
1844 if (flags & 4)
1845 markstack_ptr++; /* Protect mark. */
1846 if (flags & 8) {
1847 retstack_ix++;
1848 retstack[retstack_ix] = NULL;
1849 }
1850 if (flags & 16)
1851 scopestack_ix += 1;
1852 /* sv_2cv is too complicated, try a simpler variant first: */
1853 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1854 || SvTYPE(cv) != SVt_PVCV)
1855 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1856
a0d0e21e 1857 if (!cv || !CvROOT(cv)) {
79072805
LW
1858 if (dowarn)
1859 warn("SIG%s handler \"%s\" not defined.\n",
00d579c5
GS
1860 sig_name[sig], (gv ? GvENAME(gv)
1861 : ((cv && CvGV(cv))
1862 ? GvENAME(CvGV(cv))
1863 : "__ANON__")));
1864 goto cleanup;
79072805
LW
1865 }
1866
84902520 1867 if(psig_name[sig]) {
88e89b8a 1868 sv = SvREFCNT_inc(psig_name[sig]);
84902520
TB
1869 flags |= 64;
1870 sig_sv = sv;
1871 } else {
ff0cee69
PP
1872 sv = sv_newmortal();
1873 sv_setpv(sv,sig_name[sig]);
88e89b8a 1874 }
e336de0d
GS
1875
1876 PUSHSTACK(SI_SIGNAL);
924508f0 1877 PUSHMARK(SP);
79072805 1878 PUSHs(sv);
79072805 1879 PUTBACK;
a0d0e21e
LW
1880
1881 perl_call_sv((SV*)cv, G_DISCARD);
79072805 1882
e336de0d 1883 POPSTACK();
00d579c5 1884cleanup:
84902520
TB
1885 if (flags & 1)
1886 savestack_ix -= 8; /* Unprotect save in progress. */
84902520
TB
1887 if (flags & 4)
1888 markstack_ptr--;
1889 if (flags & 8)
1890 retstack_ix--;
1891 if (flags & 16)
1892 scopestack_ix -= 1;
1893 if (flags & 64)
1894 SvREFCNT_dec(sv);
1895 op = myop; /* Apparently not needed... */
1896
1897 Sv = tSv; /* Restore global temporaries. */
1898 Xpv = tXpv;
79072805
LW
1899 return;
1900}
4e35701f
NIS
1901
1902