This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
tiearray tweaks
[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
a0d0e21e 962 PUSHMARK(sp);
93965878 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)
463ee0b2 1012{
93965878 1013 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
463ee0b2
LW
1014 return 0;
1015}
1016
1017int
8ac85365 1018magic_clearpack(SV *sv, MAGIC *mg)
463ee0b2 1019{
a0d0e21e
LW
1020 return magic_methpack(sv,mg,"DELETE");
1021}
463ee0b2 1022
93965878
NIS
1023
1024U32
1025magic_sizepack(SV *sv, MAGIC *mg)
1026{
1027 dTHR;
1028 U32 retval = 0;
1029
1030 ENTER;
1031 SAVETMPS;
1032 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1033 sv = *stack_sp--;
1034 retval = (U32) SvIV(sv);
1035 }
1036 FREETMPS;
1037 LEAVE;
1038 return retval;
1039}
1040
8ac85365 1041int magic_wipepack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1042{
1043 dSP;
463ee0b2 1044
a0d0e21e
LW
1045 PUSHMARK(sp);
1046 XPUSHs(mg->mg_obj);
463ee0b2 1047 PUTBACK;
463ee0b2 1048
a0d0e21e 1049 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
463ee0b2
LW
1050
1051 return 0;
1052}
1053
1054int
8ac85365 1055magic_nextpack(SV *sv, MAGIC *mg, SV *key)
463ee0b2 1056{
463ee0b2 1057 dSP;
a0d0e21e 1058 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1059
1060 ENTER;
a0d0e21e
LW
1061 SAVETMPS;
1062 PUSHMARK(sp);
1063 EXTEND(sp, 2);
1064 PUSHs(mg->mg_obj);
463ee0b2
LW
1065 if (SvOK(key))
1066 PUSHs(key);
1067 PUTBACK;
1068
a0d0e21e
LW
1069 if (perl_call_method(meth, G_SCALAR))
1070 sv_setsv(key, *stack_sp--);
463ee0b2 1071
a0d0e21e
LW
1072 FREETMPS;
1073 LEAVE;
79072805
LW
1074 return 0;
1075}
1076
1077int
8ac85365 1078magic_existspack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1079{
1080 return magic_methpack(sv,mg,"EXISTS");
1081}
1082
1083int
8ac85365 1084magic_setdbline(SV *sv, MAGIC *mg)
79072805 1085{
11343788 1086 dTHR;
79072805
LW
1087 OP *o;
1088 I32 i;
1089 GV* gv;
1090 SV** svp;
1091
1092 gv = DBline;
1093 i = SvTRUE(sv);
188ea221 1094 svp = av_fetch(GvAV(gv),
5aabfad6 1095 atoi(MgPV(mg,na)), FALSE);
8990e307 1096 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 1097 o->op_private = i;
79072805
LW
1098 else
1099 warn("Can't break at that line\n");
1100 return 0;
1101}
1102
1103int
8ac85365 1104magic_getarylen(SV *sv, MAGIC *mg)
79072805 1105{
0f15f207 1106 dTHR;
a0d0e21e 1107 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
1108 return 0;
1109}
1110
1111int
8ac85365 1112magic_setarylen(SV *sv, MAGIC *mg)
79072805 1113{
0f15f207 1114 dTHR;
a0d0e21e
LW
1115 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1116 return 0;
1117}
1118
1119int
8ac85365 1120magic_getpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1121{
1122 SV* lsv = LvTARG(sv);
1123
1124 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1125 mg = mg_find(lsv, 'g');
1126 if (mg && mg->mg_len >= 0) {
0f15f207 1127 dTHR;
a0d0e21e
LW
1128 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
1129 return 0;
1130 }
1131 }
1132 (void)SvOK_off(sv);
1133 return 0;
1134}
1135
1136int
8ac85365 1137magic_setpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1138{
1139 SV* lsv = LvTARG(sv);
1140 SSize_t pos;
1141 STRLEN len;
1142
1143 mg = 0;
1144
1145 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1146 mg = mg_find(lsv, 'g');
1147 if (!mg) {
1148 if (!SvOK(sv))
1149 return 0;
1150 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1151 mg = mg_find(lsv, 'g');
1152 }
1153 else if (!SvOK(sv)) {
1154 mg->mg_len = -1;
1155 return 0;
1156 }
1157 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1158
0f15f207 1159 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
a0d0e21e
LW
1160 if (pos < 0) {
1161 pos += len;
1162 if (pos < 0)
1163 pos = 0;
1164 }
1165 else if (pos > len)
1166 pos = len;
1167 mg->mg_len = pos;
71be2cbc 1168 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1169
79072805
LW
1170 return 0;
1171}
1172
1173int
8ac85365 1174magic_getglob(SV *sv, MAGIC *mg)
79072805 1175{
8646b087
PP
1176 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1177 SvFAKE_off(sv);
946ec16e 1178 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1179 SvFAKE_on(sv);
1180 }
1181 else
946ec16e 1182 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1183 return 0;
1184}
1185
1186int
8ac85365 1187magic_setglob(SV *sv, MAGIC *mg)
79072805
LW
1188{
1189 register char *s;
1190 GV* gv;
1191
1192 if (!SvOK(sv))
1193 return 0;
463ee0b2 1194 s = SvPV(sv, na);
79072805
LW
1195 if (*s == '*' && s[1])
1196 s++;
85e6fe83 1197 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1198 if (sv == (SV*)gv)
1199 return 0;
1200 if (GvGP(sv))
88e89b8a 1201 gp_free((GV*)sv);
79072805 1202 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1203 return 0;
1204}
1205
1206int
8ac85365 1207magic_setsubstr(SV *sv, MAGIC *mg)
79072805 1208{
8990e307
LW
1209 STRLEN len;
1210 char *tmps = SvPV(sv,len);
1211 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1212 return 0;
1213}
1214
1215int
8ac85365 1216magic_gettaint(SV *sv, MAGIC *mg)
463ee0b2 1217{
a863c7d1 1218 dTHR;
bbce6d69
PP
1219 TAINT_IF((mg->mg_len & 1) ||
1220 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1221 return 0;
1222}
1223
1224int
8ac85365 1225magic_settaint(SV *sv, MAGIC *mg)
463ee0b2 1226{
11343788 1227 dTHR;
748a9306
LW
1228 if (localizing) {
1229 if (localizing == 1)
1230 mg->mg_len <<= 1;
1231 else
1232 mg->mg_len >>= 1;
a0d0e21e 1233 }
748a9306
LW
1234 else if (tainted)
1235 mg->mg_len |= 1;
1236 else
1237 mg->mg_len &= ~1;
463ee0b2
LW
1238 return 0;
1239}
1240
1241int
8ac85365 1242magic_setvec(SV *sv, MAGIC *mg)
79072805
LW
1243{
1244 do_vecset(sv); /* XXX slurp this routine */
1245 return 0;
1246}
1247
1248int
8ac85365 1249magic_getdefelem(SV *sv, MAGIC *mg)
5f05dabc 1250{
71be2cbc 1251 SV *targ = Nullsv;
5f05dabc 1252 if (LvTARGLEN(sv)) {
68dc0745
PP
1253 if (mg->mg_obj) {
1254 HV* hv = (HV*)LvTARG(sv);
1255 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1256 if (he)
1257 targ = HeVAL(he);
1258 }
1259 else {
93965878 1260 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1261 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1262 targ = AvARRAY(av)[LvTARGOFF(sv)];
1263 }
1264 if (targ && targ != &sv_undef) {
e858de61 1265 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1266 /* somebody else defined it for us */
1267 SvREFCNT_dec(LvTARG(sv));
1268 LvTARG(sv) = SvREFCNT_inc(targ);
1269 LvTARGLEN(sv) = 0;
1270 SvREFCNT_dec(mg->mg_obj);
1271 mg->mg_obj = Nullsv;
1272 mg->mg_flags &= ~MGf_REFCOUNTED;
1273 }
5f05dabc 1274 }
71be2cbc
PP
1275 else
1276 targ = LvTARG(sv);
1277 sv_setsv(sv, targ ? targ : &sv_undef);
1278 return 0;
1279}
1280
1281int
8ac85365 1282magic_setdefelem(SV *sv, MAGIC *mg)
71be2cbc
PP
1283{
1284 if (LvTARGLEN(sv))
68dc0745
PP
1285 vivify_defelem(sv);
1286 if (LvTARG(sv)) {
5f05dabc 1287 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1288 SvSETMAGIC(LvTARG(sv));
1289 }
5f05dabc
PP
1290 return 0;
1291}
1292
1293int
8ac85365 1294magic_freedefelem(SV *sv, MAGIC *mg)
5f05dabc
PP
1295{
1296 SvREFCNT_dec(LvTARG(sv));
71be2cbc
PP
1297 return 0;
1298}
1299
1300void
8ac85365 1301vivify_defelem(SV *sv)
71be2cbc 1302{
e858de61 1303 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
68dc0745
PP
1304 MAGIC* mg;
1305 SV* value;
71be2cbc 1306
68dc0745 1307 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1308 return;
68dc0745
PP
1309 if (mg->mg_obj) {
1310 HV* hv = (HV*)LvTARG(sv);
1311 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1312 if (!he || (value = HeVAL(he)) == &sv_undef)
1313 croak(no_helem, SvPV(mg->mg_obj, na));
71be2cbc 1314 }
68dc0745
PP
1315 else {
1316 AV* av = (AV*)LvTARG(sv);
5aabfad6 1317 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1318 LvTARG(sv) = Nullsv; /* array can't be extended */
1319 else {
1320 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1321 if (!svp || (value = *svp) == &sv_undef)
1322 croak(no_aelem, (I32)LvTARGOFF(sv));
1323 }
1324 }
3e3baf6d 1325 (void)SvREFCNT_inc(value);
68dc0745
PP
1326 SvREFCNT_dec(LvTARG(sv));
1327 LvTARG(sv) = value;
71be2cbc 1328 LvTARGLEN(sv) = 0;
68dc0745
PP
1329 SvREFCNT_dec(mg->mg_obj);
1330 mg->mg_obj = Nullsv;
1331 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1332}
1333
1334int
8ac85365 1335magic_setmglob(SV *sv, MAGIC *mg)
93a17b20 1336{
a0d0e21e 1337 mg->mg_len = -1;
c6496cc7 1338 SvSCREAM_off(sv);
93a17b20
LW
1339 return 0;
1340}
1341
1342int
8ac85365 1343magic_setbm(SV *sv, MAGIC *mg)
79072805 1344{
463ee0b2 1345 sv_unmagic(sv, 'B');
79072805
LW
1346 SvVALID_off(sv);
1347 return 0;
1348}
1349
1350int
8ac85365 1351magic_setfm(SV *sv, MAGIC *mg)
55497cff
PP
1352{
1353 sv_unmagic(sv, 'f');
1354 SvCOMPILED_off(sv);
1355 return 0;
1356}
1357
1358int
8ac85365 1359magic_setuvar(SV *sv, MAGIC *mg)
79072805
LW
1360{
1361 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1362
1363 if (uf && uf->uf_set)
1364 (*uf->uf_set)(uf->uf_index, sv);
1365 return 0;
1366}
1367
c277df42
IZ
1368int
1369magic_freeregexp(SV *sv, MAGIC *mg)
1370{
1371 regexp *re = (regexp *)mg->mg_obj;
1372 ReREFCNT_dec(re);
1373 return 0;
1374}
1375
7a4c00b4 1376#ifdef USE_LOCALE_COLLATE
79072805 1377int
8ac85365 1378magic_setcollxfrm(SV *sv, MAGIC *mg)
bbce6d69
PP
1379{
1380 /*
1381 * René Descartes said "I think not."
1382 * and vanished with a faint plop.
1383 */
7a4c00b4
PP
1384 if (mg->mg_ptr) {
1385 Safefree(mg->mg_ptr);
1386 mg->mg_ptr = NULL;
1387 mg->mg_len = -1;
1388 }
bbce6d69
PP
1389 return 0;
1390}
7a4c00b4 1391#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1392
1393int
8ac85365 1394magic_set(SV *sv, MAGIC *mg)
79072805 1395{
11343788 1396 dTHR;
79072805
LW
1397 register char *s;
1398 I32 i;
8990e307 1399 STRLEN len;
79072805 1400 switch (*mg->mg_ptr) {
748a9306
LW
1401 case '\001': /* ^A */
1402 sv_setsv(bodytarget, sv);
1403 break;
79072805 1404 case '\004': /* ^D */
8990e307 1405 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1406 DEBUG_x(dump_all());
1407 break;
28f23441
PP
1408 case '\005': /* ^E */
1409#ifdef VMS
1410 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1411#else
22fae026
TM
1412#ifdef WIN32
1413 SetLastError( SvIV(sv) );
1414#else
f86702cc
PP
1415 /* will anyone ever use this? */
1416 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441 1417#endif
22fae026 1418#endif
28f23441 1419 break;
79072805 1420 case '\006': /* ^F */
463ee0b2 1421 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1422 break;
a0d0e21e
LW
1423 case '\010': /* ^H */
1424 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1425 break;
79072805
LW
1426 case '\t': /* ^I */
1427 if (inplace)
1428 Safefree(inplace);
1429 if (SvOK(sv))
a0d0e21e 1430 inplace = savepv(SvPV(sv,na));
79072805
LW
1431 else
1432 inplace = Nullch;
1433 break;
28f23441
PP
1434 case '\017': /* ^O */
1435 if (osname)
1436 Safefree(osname);
1437 if (SvOK(sv))
1438 osname = savepv(SvPV(sv,na));
1439 else
1440 osname = Nullch;
1441 break;
79072805 1442 case '\020': /* ^P */
84902520 1443 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1444 break;
1445 case '\024': /* ^T */
88e89b8a
PP
1446#ifdef BIG_TIME
1447 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1448#else
85e6fe83 1449 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1450#endif
79072805
LW
1451 break;
1452 case '\027': /* ^W */
463ee0b2 1453 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1454 break;
1455 case '.':
748a9306
LW
1456 if (localizing) {
1457 if (localizing == 1)
1458 save_sptr((SV**)&last_in_gv);
1459 }
88e89b8a 1460 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1461 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1462 break;
1463 case '^':
a0d0e21e
LW
1464 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1465 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1466 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1467 break;
1468 case '~':
a0d0e21e
LW
1469 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1470 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1471 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1472 break;
1473 case '=':
a0d0e21e 1474 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1475 break;
1476 case '-':
a0d0e21e
LW
1477 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1478 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1479 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1480 break;
1481 case '%':
a0d0e21e 1482 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1483 break;
1484 case '|':
4b65379b
CS
1485 {
1486 IO *io = GvIOp(defoutgv);
1487 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1488 IoFLAGS(io) &= ~IOf_FLUSH;
1489 else {
1490 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1491 PerlIO *ofp = IoOFP(io);
1492 if (ofp)
1493 (void)PerlIO_flush(ofp);
1494 IoFLAGS(io) |= IOf_FLUSH;
1495 }
1496 }
79072805
LW
1497 }
1498 break;
1499 case '*':
463ee0b2 1500 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1501 multiline = (i != 0);
1502 break;
1503 case '/':
c07a80fd
PP
1504 SvREFCNT_dec(nrs);
1505 nrs = newSVsv(sv);
1506 SvREFCNT_dec(rs);
1507 rs = SvREFCNT_inc(nrs);
79072805
LW
1508 break;
1509 case '\\':
1510 if (ors)
1511 Safefree(ors);
e3c19b7b
CS
1512 if (SvOK(sv) || SvGMAGICAL(sv))
1513 ors = savepv(SvPV(sv,orslen));
1514 else {
1515 ors = Nullch;
1516 orslen = 0;
1517 }
79072805
LW
1518 break;
1519 case ',':
1520 if (ofs)
1521 Safefree(ofs);
a0d0e21e 1522 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1523 break;
1524 case '#':
1525 if (ofmt)
1526 Safefree(ofmt);
a0d0e21e 1527 ofmt = savepv(SvPV(sv,na));
79072805
LW
1528 break;
1529 case '[':
a0d0e21e 1530 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1531 break;
1532 case '?':
ff0cee69
PP
1533#ifdef COMPLEX_STATUS
1534 if (localizing == 2) {
1535 statusvalue = LvTARGOFF(sv);
1536 statusvalue_vms = LvTARGLEN(sv);
1537 }
1538 else
1539#endif
1540#ifdef VMSISH_STATUS
1541 if (VMSISH_STATUS)
1542 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1543 else
1544#endif
1545 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1546 break;
1547 case '!':
f86702cc
PP
1548 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1549 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1550 break;
1551 case '<':
463ee0b2 1552 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1553 if (delaymagic) {
1554 delaymagic |= DM_RUID;
1555 break; /* don't do magic till later */
1556 }
1557#ifdef HAS_SETRUID
85e6fe83 1558 (void)setruid((Uid_t)uid);
79072805
LW
1559#else
1560#ifdef HAS_SETREUID
85e6fe83 1561 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1562#else
85e6fe83
LW
1563#ifdef HAS_SETRESUID
1564 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1565#else
1566 if (uid == euid) /* special case $< = $> */
1567 (void)setuid(uid);
a0d0e21e
LW
1568 else {
1569 uid = (I32)getuid();
463ee0b2 1570 croak("setruid() not implemented");
a0d0e21e 1571 }
79072805
LW
1572#endif
1573#endif
85e6fe83 1574#endif
748a9306 1575 uid = (I32)getuid();
4633a7c4 1576 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1577 break;
1578 case '>':
463ee0b2 1579 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1580 if (delaymagic) {
1581 delaymagic |= DM_EUID;
1582 break; /* don't do magic till later */
1583 }
1584#ifdef HAS_SETEUID
85e6fe83 1585 (void)seteuid((Uid_t)euid);
79072805
LW
1586#else
1587#ifdef HAS_SETREUID
85e6fe83
LW
1588 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1589#else
1590#ifdef HAS_SETRESUID
1591 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1592#else
1593 if (euid == uid) /* special case $> = $< */
1594 setuid(euid);
a0d0e21e
LW
1595 else {
1596 euid = (I32)geteuid();
463ee0b2 1597 croak("seteuid() not implemented");
a0d0e21e 1598 }
79072805
LW
1599#endif
1600#endif
85e6fe83 1601#endif
79072805 1602 euid = (I32)geteuid();
4633a7c4 1603 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1604 break;
1605 case '(':
463ee0b2 1606 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1607 if (delaymagic) {
1608 delaymagic |= DM_RGID;
1609 break; /* don't do magic till later */
1610 }
1611#ifdef HAS_SETRGID
85e6fe83 1612 (void)setrgid((Gid_t)gid);
79072805
LW
1613#else
1614#ifdef HAS_SETREGID
85e6fe83
LW
1615 (void)setregid((Gid_t)gid, (Gid_t)-1);
1616#else
1617#ifdef HAS_SETRESGID
1618 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1619#else
1620 if (gid == egid) /* special case $( = $) */
1621 (void)setgid(gid);
748a9306
LW
1622 else {
1623 gid = (I32)getgid();
463ee0b2 1624 croak("setrgid() not implemented");
748a9306 1625 }
79072805
LW
1626#endif
1627#endif
85e6fe83 1628#endif
79072805 1629 gid = (I32)getgid();
4633a7c4 1630 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1631 break;
1632 case ')':
5cd24f17
PP
1633#ifdef HAS_SETGROUPS
1634 {
1635 char *p = SvPV(sv, na);
1636 Groups_t gary[NGROUPS];
1637
1638 SET_NUMERIC_STANDARD();
1639 while (isSPACE(*p))
1640 ++p;
1641 egid = I_V(atof(p));
1642 for (i = 0; i < NGROUPS; ++i) {
1643 while (*p && !isSPACE(*p))
1644 ++p;
1645 while (isSPACE(*p))
1646 ++p;
1647 if (!*p)
1648 break;
1649 gary[i] = I_V(atof(p));
1650 }
8cc95fdb
PP
1651 if (i)
1652 (void)setgroups(i, gary);
5cd24f17
PP
1653 }
1654#else /* HAS_SETGROUPS */
463ee0b2 1655 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1656#endif /* HAS_SETGROUPS */
79072805
LW
1657 if (delaymagic) {
1658 delaymagic |= DM_EGID;
1659 break; /* don't do magic till later */
1660 }
1661#ifdef HAS_SETEGID
85e6fe83 1662 (void)setegid((Gid_t)egid);
79072805
LW
1663#else
1664#ifdef HAS_SETREGID
85e6fe83
LW
1665 (void)setregid((Gid_t)-1, (Gid_t)egid);
1666#else
1667#ifdef HAS_SETRESGID
1668 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1669#else
1670 if (egid == gid) /* special case $) = $( */
1671 (void)setgid(egid);
748a9306
LW
1672 else {
1673 egid = (I32)getegid();
463ee0b2 1674 croak("setegid() not implemented");
748a9306 1675 }
79072805
LW
1676#endif
1677#endif
85e6fe83 1678#endif
79072805 1679 egid = (I32)getegid();
4633a7c4 1680 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1681 break;
1682 case ':':
a0d0e21e 1683 chopset = SvPV_force(sv,na);
79072805
LW
1684 break;
1685 case '0':
1686 if (!origalen) {
1687 s = origargv[0];
1688 s += strlen(s);
1689 /* See if all the arguments are contiguous in memory */
1690 for (i = 1; i < origargc; i++) {
fb73857a
PP
1691 if (origargv[i] == s + 1
1692#ifdef OS2
1693 || origargv[i] == s + 2
1694#endif
1695 )
79072805 1696 s += strlen(++s); /* this one is ok too */
fb73857a
PP
1697 else
1698 break;
79072805 1699 }
bbce6d69 1700 /* can grab env area too? */
fb73857a
PP
1701 if (origenviron && (origenviron[0] == s + 1
1702#ifdef OS2
1703 || (origenviron[0] == s + 9 && (s += 8))
1704#endif
1705 )) {
66b1d557 1706 my_setenv("NoNe SuCh", Nullch);
79072805
LW
1707 /* force copy of environment */
1708 for (i = 0; origenviron[i]; i++)
1709 if (origenviron[i] == s + 1)
1710 s += strlen(++s);
fb73857a
PP
1711 else
1712 break;
79072805
LW
1713 }
1714 origalen = s - origargv[0];
1715 }
a0d0e21e 1716 s = SvPV_force(sv,len);
8990e307 1717 i = len;
79072805
LW
1718 if (i >= origalen) {
1719 i = origalen;
fb73857a
PP
1720 /* don't allow system to limit $0 seen by script */
1721 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
79072805 1722 Copy(s, origargv[0], i, char);
fb73857a
PP
1723 s = origargv[0]+i;
1724 *s = '\0';
79072805
LW
1725 }
1726 else {
1727 Copy(s, origargv[0], i, char);
1728 s = origargv[0]+i;
1729 *s++ = '\0';
1730 while (++i < origalen)
8990e307
LW
1731 *s++ = ' ';
1732 s = origargv[0]+i;
ed6116ce 1733 for (i = 1; i < origargc; i++)
8990e307 1734 origargv[i] = Nullch;
79072805
LW
1735 }
1736 break;
a863c7d1
MB
1737#ifdef USE_THREADS
1738 case '@':
38a03e6e 1739 sv_setsv(thr->errsv, sv);
a863c7d1
MB
1740 break;
1741#endif /* USE_THREADS */
79072805
LW
1742 }
1743 return 0;
1744}
1745
f93b4edd
MB
1746#ifdef USE_THREADS
1747int
8ac85365 1748magic_mutexfree(SV *sv, MAGIC *mg)
f93b4edd
MB
1749{
1750 dTHR;
bc1f4c86
MB
1751 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1752 (unsigned long)thr, (unsigned long)sv);)
f93b4edd
MB
1753 if (MgOWNER(mg))
1754 croak("panic: magic_mutexfree");
1755 MUTEX_DESTROY(MgMUTEXP(mg));
1756 COND_DESTROY(MgCONDP(mg));
e55aaa0e 1757 SvREFCNT_dec(sv);
f93b4edd
MB
1758 return 0;
1759}
1760#endif /* USE_THREADS */
1761
79072805 1762I32
8ac85365 1763whichsig(char *sig)
79072805
LW
1764{
1765 register char **sigv;
1766
1767 for (sigv = sig_name+1; *sigv; sigv++)
1768 if (strEQ(sig,*sigv))
8e07c86e 1769 return sig_num[sigv - sig_name];
79072805
LW
1770#ifdef SIGCLD
1771 if (strEQ(sig,"CHLD"))
1772 return SIGCLD;
1773#endif
1774#ifdef SIGCHLD
1775 if (strEQ(sig,"CLD"))
1776 return SIGCHLD;
1777#endif
1778 return 0;
1779}
1780
84902520
TB
1781static SV* sig_sv;
1782
1783static void
8ac85365 1784unwind_handler_stack(void *p)
84902520 1785{
ff26ac79 1786 dTHR;
84902520
TB
1787 U32 flags = *(U32*)p;
1788
1789 if (flags & 1)
1790 savestack_ix -= 5; /* Unprotect save in progress. */
1791 /* cxstack_ix-- Not needed, die already unwound it. */
1792 if (flags & 64)
1793 SvREFCNT_dec(sig_sv);
1794}
1795
ecfc5424 1796Signal_t
8ac85365 1797sighandler(int sig)
79072805
LW
1798{
1799 dSP;
00d579c5 1800 GV *gv = Nullgv;
a0d0e21e 1801 HV *st;
84902520 1802 SV *sv, *tSv = Sv;
00d579c5 1803 CV *cv = Nullcv;
79072805 1804 AV *oldstack;
84902520
TB
1805 OP *myop = op;
1806 U32 flags = 0;
1807 I32 o_save_i = savestack_ix, type;
c09156bb 1808 PERL_CONTEXT *cx;
84902520
TB
1809 XPV *tXpv = Xpv;
1810
1811 if (savestack_ix + 15 <= savestack_max)
1812 flags |= 1;
1813 if (cxstack_ix < cxstack_max - 2)
1814 flags |= 2;
1815 if (markstack_ptr < markstack_max - 2)
1816 flags |= 4;
1817 if (retstack_ix < retstack_max - 2)
1818 flags |= 8;
1819 if (scopestack_ix < scopestack_max - 3)
1820 flags |= 16;
1821
1822 if (flags & 2) { /* POPBLOCK may decrease cxstack too early. */
1823 cxstack_ix++; /* Protect from overwrite. */
1824 cx = &cxstack[cxstack_ix];
1825 type = cx->cx_type; /* Can be during partial write. */
1826 cx->cx_type = CXt_NULL; /* Make it safe for unwind. */
1827 }
ff0cee69
PP
1828 if (!psig_ptr[sig])
1829 die("Signal SIG%s received, but no signal handler set.\n",
1830 sig_name[sig]);
1831
84902520
TB
1832 /* Max number of items pushed there is 3*n or 4. We cannot fix
1833 infinity, so we fix 4 (in fact 5): */
1834 if (flags & 1) {
1835 savestack_ix += 5; /* Protect save in progress. */
1836 o_save_i = savestack_ix;
1837 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
1838 }
1839 if (flags & 4)
1840 markstack_ptr++; /* Protect mark. */
1841 if (flags & 8) {
1842 retstack_ix++;
1843 retstack[retstack_ix] = NULL;
1844 }
1845 if (flags & 16)
1846 scopestack_ix += 1;
1847 /* sv_2cv is too complicated, try a simpler variant first: */
1848 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1849 || SvTYPE(cv) != SVt_PVCV)
1850 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1851
a0d0e21e 1852 if (!cv || !CvROOT(cv)) {
79072805
LW
1853 if (dowarn)
1854 warn("SIG%s handler \"%s\" not defined.\n",
00d579c5
GS
1855 sig_name[sig], (gv ? GvENAME(gv)
1856 : ((cv && CvGV(cv))
1857 ? GvENAME(CvGV(cv))
1858 : "__ANON__")));
1859 goto cleanup;
79072805
LW
1860 }
1861
88e89b8a
PP
1862 oldstack = curstack;
1863 if (curstack != signalstack)
93965878 1864 AvFILLp(signalstack) = 0;
88e89b8a 1865 SWITCHSTACK(curstack, signalstack);
79072805 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 }
a0d0e21e 1875 PUSHMARK(sp);
79072805 1876 PUSHs(sv);
79072805 1877 PUTBACK;
a0d0e21e
LW
1878
1879 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1880
1881 SWITCHSTACK(signalstack, oldstack);
00d579c5 1882cleanup:
84902520
TB
1883 if (flags & 1)
1884 savestack_ix -= 8; /* Unprotect save in progress. */
1885 if (flags & 2) {
1886 cxstack[cxstack_ix].cx_type = type;
1887 cxstack_ix -= 1;
1888 }
1889 if (flags & 4)
1890 markstack_ptr--;
1891 if (flags & 8)
1892 retstack_ix--;
1893 if (flags & 16)
1894 scopestack_ix -= 1;
1895 if (flags & 64)
1896 SvREFCNT_dec(sv);
1897 op = myop; /* Apparently not needed... */
1898
1899 Sv = tSv; /* Restore global temporaries. */
1900 Xpv = tXpv;
79072805
LW
1901 return;
1902}
4e35701f
NIS
1903
1904