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