This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate post-beta tweaks to ansiperl
[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;
d9f97599 292 register REGEXP *rx;
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 '&':
d9f97599 298 if (curpm && (rx = curpm->op_pmregexp)) {
93a17b20
LW
299 paren = atoi(mg->mg_ptr);
300 getparen:
d9f97599
GS
301 if (paren <= rx->nparens &&
302 (s = rx->startp[paren]) &&
303 (t = rx->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 '+':
d9f97599
GS
312 if (curpm && (rx = curpm->op_pmregexp)) {
313 paren = rx->lastparen;
13f57bf8
CS
314 if (paren)
315 goto getparen;
93a17b20 316 }
748a9306 317 return 0;
93a17b20 318 case '`':
d9f97599
GS
319 if (curpm && (rx = curpm->op_pmregexp)) {
320 if ((s = rx->subbeg) && rx->startp[0]) {
321 i = rx->startp[0] - s;
71be2cbc 322 if (i >= 0)
93a17b20 323 return i;
93a17b20 324 }
93a17b20 325 }
748a9306 326 return 0;
93a17b20 327 case '\'':
d9f97599
GS
328 if (curpm && (rx = curpm->op_pmregexp)) {
329 if (rx->subend && (s = rx->endp[0])) {
330 i = rx->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;
d9f97599 356 register REGEXP *rx;
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 '&':
d9f97599 460 if (curpm && (rx = 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:
d9f97599
GS
467 if (paren <= rx->nparens &&
468 (s = rx->startp[paren]) &&
469 (t = rx->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)
b3eb6a9b 481 tainted = (was_tainted || RX_MATCH_TAINTED(rx));
748a9306
LW
482 break;
483 }
79072805 484 }
79072805 485 }
748a9306 486 sv_setsv(sv,&sv_undef);
79072805
LW
487 break;
488 case '+':
d9f97599
GS
489 if (curpm && (rx = curpm->op_pmregexp)) {
490 paren = rx->lastparen;
a0d0e21e
LW
491 if (paren)
492 goto getparen;
79072805 493 }
748a9306 494 sv_setsv(sv,&sv_undef);
79072805
LW
495 break;
496 case '`':
d9f97599
GS
497 if (curpm && (rx = curpm->op_pmregexp)) {
498 if ((s = rx->subbeg) && rx->startp[0]) {
499 i = rx->startp[0] - s;
13f57bf8 500 goto getrx;
79072805 501 }
79072805 502 }
748a9306 503 sv_setsv(sv,&sv_undef);
79072805
LW
504 break;
505 case '\'':
d9f97599
GS
506 if (curpm && (rx = curpm->op_pmregexp)) {
507 if (rx->subend && (s = rx->endp[0])) {
508 i = rx->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 != '/'
c6ed36e1 705 || (PerlLIO_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{
463ee0b2
LW
901 sub_generation++;
902 return 0;
903}
904
a0d0e21e
LW
905#ifdef OVERLOAD
906
463ee0b2 907int
8ac85365 908magic_setamagic(SV *sv, MAGIC *mg)
463ee0b2 909{
a0d0e21e
LW
910 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
911 amagic_generation++;
463ee0b2 912
a0d0e21e
LW
913 return 0;
914}
915#endif /* OVERLOAD */
463ee0b2 916
946ec16e 917int
6ff81951
GS
918magic_getnkeys(SV *sv, MAGIC *mg)
919{
920 HV *hv = (HV*)LvTARG(sv);
921 HE *entry;
922 I32 i = 0;
923
924 if (hv) {
925 (void) hv_iterinit(hv);
926 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
927 i = HvKEYS(hv);
928 else {
929 /*SUPPRESS 560*/
930 while (entry = hv_iternext(hv)) {
931 i++;
932 }
933 }
934 }
935
936 sv_setiv(sv, (IV)i);
937 return 0;
938}
939
940int
8ac85365 941magic_setnkeys(SV *sv, MAGIC *mg)
946ec16e
PP
942{
943 if (LvTARG(sv)) {
944 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
946ec16e
PP
945 }
946 return 0;
93965878 947}
946ec16e 948
e336de0d 949/* caller is responsible for stack switching/cleanup */
565764a8 950STATIC int
93965878 951magic_methcall(MAGIC *mg, char *meth, I32 flags, int n, SV *val)
a0d0e21e
LW
952{
953 dSP;
463ee0b2 954
924508f0
GS
955 PUSHMARK(SP);
956 EXTEND(SP, n);
a0d0e21e 957 PUSHs(mg->mg_obj);
93965878
NIS
958 if (n > 1) {
959 if (mg->mg_ptr) {
565764a8
DL
960 if (mg->mg_len >= 0)
961 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
962 else if (mg->mg_len == HEf_SVKEY)
93965878
NIS
963 PUSHs((SV*)mg->mg_ptr);
964 }
965 else if (mg->mg_type == 'p') {
565764a8 966 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
93965878
NIS
967 }
968 }
969 if (n > 2) {
970 PUSHs(val);
88e89b8a 971 }
463ee0b2
LW
972 PUTBACK;
973
93965878 974 return perl_call_method(meth, flags);
946ec16e
PP
975}
976
76e3520e 977STATIC int
8ac85365 978magic_methpack(SV *sv, MAGIC *mg, char *meth)
a0d0e21e
LW
979{
980 dSP;
463ee0b2 981
a0d0e21e
LW
982 ENTER;
983 SAVETMPS;
e788e7d3 984 PUSHSTACKi(PERLSI_MAGIC);
463ee0b2 985
93965878 986 if (magic_methcall(mg, meth, G_SCALAR, 2, NULL)) {
a0d0e21e 987 sv_setsv(sv, *stack_sp--);
93965878 988 }
463ee0b2 989
d3acc0f7 990 POPSTACK;
a0d0e21e
LW
991 FREETMPS;
992 LEAVE;
993 return 0;
994}
463ee0b2 995
a0d0e21e 996int
8ac85365 997magic_getpack(SV *sv, MAGIC *mg)
a0d0e21e
LW
998{
999 magic_methpack(sv,mg,"FETCH");
1000 if (mg->mg_ptr)
1001 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
1002 return 0;
1003}
1004
1005int
8ac85365 1006magic_setpack(SV *sv, MAGIC *mg)
e336de0d
GS
1007{
1008 dSP;
a60c0954 1009 ENTER;
e788e7d3 1010 PUSHSTACKi(PERLSI_MAGIC);
93965878 1011 magic_methcall(mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
d3acc0f7 1012 POPSTACK;
a60c0954 1013 LEAVE;
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{
e336de0d 1027 dSP;
93965878
NIS
1028 U32 retval = 0;
1029
1030 ENTER;
1031 SAVETMPS;
e788e7d3 1032 PUSHSTACKi(PERLSI_MAGIC);
93965878
NIS
1033 if (magic_methcall(mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1034 sv = *stack_sp--;
a60c0954 1035 retval = (U32) SvIV(sv)-1;
93965878 1036 }
d3acc0f7 1037 POPSTACK;
93965878
NIS
1038 FREETMPS;
1039 LEAVE;
1040 return retval;
1041}
1042
8ac85365 1043int magic_wipepack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1044{
1045 dSP;
463ee0b2 1046
e336de0d 1047 ENTER;
e788e7d3 1048 PUSHSTACKi(PERLSI_MAGIC);
924508f0 1049 PUSHMARK(SP);
a0d0e21e 1050 XPUSHs(mg->mg_obj);
463ee0b2 1051 PUTBACK;
a0d0e21e 1052 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
d3acc0f7 1053 POPSTACK;
a60c0954 1054 LEAVE;
463ee0b2
LW
1055 return 0;
1056}
1057
1058int
8ac85365 1059magic_nextpack(SV *sv, MAGIC *mg, SV *key)
463ee0b2 1060{
463ee0b2 1061 dSP;
a0d0e21e 1062 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
1063
1064 ENTER;
a0d0e21e 1065 SAVETMPS;
e788e7d3 1066 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
1067 PUSHMARK(SP);
1068 EXTEND(SP, 2);
a0d0e21e 1069 PUSHs(mg->mg_obj);
463ee0b2
LW
1070 if (SvOK(key))
1071 PUSHs(key);
1072 PUTBACK;
1073
a0d0e21e
LW
1074 if (perl_call_method(meth, G_SCALAR))
1075 sv_setsv(key, *stack_sp--);
463ee0b2 1076
d3acc0f7 1077 POPSTACK;
a0d0e21e
LW
1078 FREETMPS;
1079 LEAVE;
79072805
LW
1080 return 0;
1081}
1082
1083int
8ac85365 1084magic_existspack(SV *sv, MAGIC *mg)
a0d0e21e
LW
1085{
1086 return magic_methpack(sv,mg,"EXISTS");
1087}
1088
1089int
8ac85365 1090magic_setdbline(SV *sv, MAGIC *mg)
79072805 1091{
11343788 1092 dTHR;
79072805
LW
1093 OP *o;
1094 I32 i;
1095 GV* gv;
1096 SV** svp;
1097
1098 gv = DBline;
1099 i = SvTRUE(sv);
188ea221 1100 svp = av_fetch(GvAV(gv),
5aabfad6 1101 atoi(MgPV(mg,na)), FALSE);
8990e307 1102 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 1103 o->op_private = i;
79072805
LW
1104 else
1105 warn("Can't break at that line\n");
1106 return 0;
1107}
1108
1109int
8ac85365 1110magic_getarylen(SV *sv, MAGIC *mg)
79072805 1111{
0f15f207 1112 dTHR;
a0d0e21e 1113 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
1114 return 0;
1115}
1116
1117int
8ac85365 1118magic_setarylen(SV *sv, MAGIC *mg)
79072805 1119{
0f15f207 1120 dTHR;
a0d0e21e
LW
1121 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
1122 return 0;
1123}
1124
1125int
8ac85365 1126magic_getpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1127{
1128 SV* lsv = LvTARG(sv);
1129
1130 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1131 mg = mg_find(lsv, 'g');
565764a8 1132 if (mg && mg->mg_len >= 0) {
0f15f207 1133 dTHR;
565764a8 1134 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
a0d0e21e
LW
1135 return 0;
1136 }
1137 }
1138 (void)SvOK_off(sv);
1139 return 0;
1140}
1141
1142int
8ac85365 1143magic_setpos(SV *sv, MAGIC *mg)
a0d0e21e
LW
1144{
1145 SV* lsv = LvTARG(sv);
1146 SSize_t pos;
1147 STRLEN len;
1148
1149 mg = 0;
1150
1151 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1152 mg = mg_find(lsv, 'g');
1153 if (!mg) {
1154 if (!SvOK(sv))
1155 return 0;
1156 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1157 mg = mg_find(lsv, 'g');
1158 }
1159 else if (!SvOK(sv)) {
565764a8 1160 mg->mg_len = -1;
a0d0e21e
LW
1161 return 0;
1162 }
1163 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1164
0f15f207 1165 WITH_THR(pos = SvIV(sv) - curcop->cop_arybase);
a0d0e21e
LW
1166 if (pos < 0) {
1167 pos += len;
1168 if (pos < 0)
1169 pos = 0;
1170 }
1171 else if (pos > len)
1172 pos = len;
565764a8 1173 mg->mg_len = pos;
71be2cbc 1174 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1175
79072805
LW
1176 return 0;
1177}
1178
1179int
8ac85365 1180magic_getglob(SV *sv, MAGIC *mg)
79072805 1181{
8646b087
PP
1182 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1183 SvFAKE_off(sv);
946ec16e 1184 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1185 SvFAKE_on(sv);
1186 }
1187 else
946ec16e 1188 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1189 return 0;
1190}
1191
1192int
8ac85365 1193magic_setglob(SV *sv, MAGIC *mg)
79072805
LW
1194{
1195 register char *s;
1196 GV* gv;
1197
1198 if (!SvOK(sv))
1199 return 0;
463ee0b2 1200 s = SvPV(sv, na);
79072805
LW
1201 if (*s == '*' && s[1])
1202 s++;
85e6fe83 1203 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1204 if (sv == (SV*)gv)
1205 return 0;
1206 if (GvGP(sv))
88e89b8a 1207 gp_free((GV*)sv);
79072805 1208 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1209 return 0;
1210}
1211
1212int
6ff81951
GS
1213magic_getsubstr(SV *sv, MAGIC *mg)
1214{
1215 STRLEN len;
1216 SV *lsv = LvTARG(sv);
1217 char *tmps = SvPV(lsv,len);
1218 I32 offs = LvTARGOFF(sv);
1219 I32 rem = LvTARGLEN(sv);
1220
1221 if (offs > len)
1222 offs = len;
1223 if (rem + offs > len)
1224 rem = len - offs;
1225 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1226 return 0;
1227}
1228
1229int
8ac85365 1230magic_setsubstr(SV *sv, MAGIC *mg)
79072805 1231{
8990e307
LW
1232 STRLEN len;
1233 char *tmps = SvPV(sv,len);
1234 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1235 return 0;
1236}
1237
1238int
8ac85365 1239magic_gettaint(SV *sv, MAGIC *mg)
463ee0b2 1240{
a863c7d1 1241 dTHR;
565764a8
DL
1242 TAINT_IF((mg->mg_len & 1) ||
1243 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1244 return 0;
1245}
1246
1247int
8ac85365 1248magic_settaint(SV *sv, MAGIC *mg)
463ee0b2 1249{
11343788 1250 dTHR;
748a9306
LW
1251 if (localizing) {
1252 if (localizing == 1)
565764a8 1253 mg->mg_len <<= 1;
748a9306 1254 else
565764a8 1255 mg->mg_len >>= 1;
a0d0e21e 1256 }
748a9306 1257 else if (tainted)
565764a8 1258 mg->mg_len |= 1;
748a9306 1259 else
565764a8 1260 mg->mg_len &= ~1;
463ee0b2
LW
1261 return 0;
1262}
1263
1264int
6ff81951
GS
1265magic_getvec(SV *sv, MAGIC *mg)
1266{
1267 SV *lsv = LvTARG(sv);
1268 unsigned char *s;
1269 unsigned long retnum;
1270 STRLEN lsvlen;
1271 I32 len;
1272 I32 offset;
1273 I32 size;
1274
1275 if (!lsv) {
1276 SvOK_off(sv);
1277 return 0;
1278 }
1279 s = (unsigned char *) SvPV(lsv, lsvlen);
1280 offset = LvTARGOFF(sv);
1281 size = LvTARGLEN(sv);
1282 len = (offset + size + 7) / 8;
1283
1284 /* Copied from pp_vec() */
1285
1286 if (len > lsvlen) {
1287 if (size <= 8)
1288 retnum = 0;
1289 else {
1290 offset >>= 3;
1291 if (size == 16) {
1292 if (offset >= lsvlen)
1293 retnum = 0;
1294 else
1295 retnum = (unsigned long) s[offset] << 8;
1296 }
1297 else if (size == 32) {
1298 if (offset >= lsvlen)
1299 retnum = 0;
1300 else if (offset + 1 >= lsvlen)
1301 retnum = (unsigned long) s[offset] << 24;
1302 else if (offset + 2 >= lsvlen)
1303 retnum = ((unsigned long) s[offset] << 24) +
1304 ((unsigned long) s[offset + 1] << 16);
1305 else
1306 retnum = ((unsigned long) s[offset] << 24) +
1307 ((unsigned long) s[offset + 1] << 16) +
1308 (s[offset + 2] << 8);
1309 }
1310 }
1311 }
1312 else if (size < 8)
1313 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1314 else {
1315 offset >>= 3;
1316 if (size == 8)
1317 retnum = s[offset];
1318 else if (size == 16)
1319 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1320 else if (size == 32)
1321 retnum = ((unsigned long) s[offset] << 24) +
1322 ((unsigned long) s[offset + 1] << 16) +
1323 (s[offset + 2] << 8) + s[offset+3];
1324 }
1325
1326 sv_setuv(sv, (UV)retnum);
1327 return 0;
1328}
1329
1330int
8ac85365 1331magic_setvec(SV *sv, MAGIC *mg)
79072805
LW
1332{
1333 do_vecset(sv); /* XXX slurp this routine */
1334 return 0;
1335}
1336
1337int
8ac85365 1338magic_getdefelem(SV *sv, MAGIC *mg)
5f05dabc 1339{
71be2cbc 1340 SV *targ = Nullsv;
5f05dabc 1341 if (LvTARGLEN(sv)) {
68dc0745 1342 if (mg->mg_obj) {
74e13ce4
GS
1343 SV *ahv = LvTARG(sv);
1344 if (SvTYPE(ahv) == SVt_PVHV) {
1345 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1346 if (he)
1347 targ = HeVAL(he);
1348 }
1349 else {
1350 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1351 if (svp)
1352 targ = *svp;
1353 }
68dc0745
PP
1354 }
1355 else {
3c78fafa 1356 AV* av = (AV*)LvTARG(sv);
68dc0745
PP
1357 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1358 targ = AvARRAY(av)[LvTARGOFF(sv)];
1359 }
1360 if (targ && targ != &sv_undef) {
e858de61 1361 dTHR; /* just for SvREFCNT_dec */
68dc0745
PP
1362 /* somebody else defined it for us */
1363 SvREFCNT_dec(LvTARG(sv));
1364 LvTARG(sv) = SvREFCNT_inc(targ);
1365 LvTARGLEN(sv) = 0;
1366 SvREFCNT_dec(mg->mg_obj);
1367 mg->mg_obj = Nullsv;
1368 mg->mg_flags &= ~MGf_REFCOUNTED;
1369 }
5f05dabc 1370 }
71be2cbc
PP
1371 else
1372 targ = LvTARG(sv);
1373 sv_setsv(sv, targ ? targ : &sv_undef);
1374 return 0;
1375}
1376
1377int
8ac85365 1378magic_setdefelem(SV *sv, MAGIC *mg)
71be2cbc
PP
1379{
1380 if (LvTARGLEN(sv))
68dc0745
PP
1381 vivify_defelem(sv);
1382 if (LvTARG(sv)) {
5f05dabc 1383 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1384 SvSETMAGIC(LvTARG(sv));
1385 }
5f05dabc
PP
1386 return 0;
1387}
1388
71be2cbc 1389void
8ac85365 1390vivify_defelem(SV *sv)
71be2cbc 1391{
e858de61 1392 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
74e13ce4
GS
1393 MAGIC *mg;
1394 SV *value = Nullsv;
71be2cbc 1395
68dc0745 1396 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1397 return;
68dc0745 1398 if (mg->mg_obj) {
74e13ce4
GS
1399 SV *ahv = LvTARG(sv);
1400 if (SvTYPE(ahv) == SVt_PVHV) {
1401 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1402 if (he)
1403 value = HeVAL(he);
1404 }
1405 else {
1406 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1407 if (svp)
1408 value = *svp;
1409 }
1410 if (!value || value == &sv_undef)
68dc0745 1411 croak(no_helem, SvPV(mg->mg_obj, na));
71be2cbc 1412 }
68dc0745
PP
1413 else {
1414 AV* av = (AV*)LvTARG(sv);
5aabfad6 1415 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1416 LvTARG(sv) = Nullsv; /* array can't be extended */
1417 else {
1418 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1419 if (!svp || (value = *svp) == &sv_undef)
1420 croak(no_aelem, (I32)LvTARGOFF(sv));
1421 }
1422 }
3e3baf6d 1423 (void)SvREFCNT_inc(value);
68dc0745
PP
1424 SvREFCNT_dec(LvTARG(sv));
1425 LvTARG(sv) = value;
71be2cbc 1426 LvTARGLEN(sv) = 0;
68dc0745
PP
1427 SvREFCNT_dec(mg->mg_obj);
1428 mg->mg_obj = Nullsv;
1429 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1430}
1431
1432int
8ac85365 1433magic_setmglob(SV *sv, MAGIC *mg)
93a17b20 1434{
565764a8 1435 mg->mg_len = -1;
c6496cc7 1436 SvSCREAM_off(sv);
93a17b20
LW
1437 return 0;
1438}
1439
1440int
8ac85365 1441magic_setbm(SV *sv, MAGIC *mg)
79072805 1442{
463ee0b2 1443 sv_unmagic(sv, 'B');
79072805
LW
1444 SvVALID_off(sv);
1445 return 0;
1446}
1447
1448int
8ac85365 1449magic_setfm(SV *sv, MAGIC *mg)
55497cff
PP
1450{
1451 sv_unmagic(sv, 'f');
1452 SvCOMPILED_off(sv);
1453 return 0;
1454}
1455
1456int
8ac85365 1457magic_setuvar(SV *sv, MAGIC *mg)
79072805
LW
1458{
1459 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1460
1461 if (uf && uf->uf_set)
1462 (*uf->uf_set)(uf->uf_index, sv);
1463 return 0;
1464}
1465
c277df42
IZ
1466int
1467magic_freeregexp(SV *sv, MAGIC *mg)
1468{
1469 regexp *re = (regexp *)mg->mg_obj;
1470 ReREFCNT_dec(re);
1471 return 0;
1472}
1473
7a4c00b4 1474#ifdef USE_LOCALE_COLLATE
79072805 1475int
8ac85365 1476magic_setcollxfrm(SV *sv, MAGIC *mg)
bbce6d69
PP
1477{
1478 /*
1479 * René Descartes said "I think not."
1480 * and vanished with a faint plop.
1481 */
7a4c00b4
PP
1482 if (mg->mg_ptr) {
1483 Safefree(mg->mg_ptr);
1484 mg->mg_ptr = NULL;
565764a8 1485 mg->mg_len = -1;
7a4c00b4 1486 }
bbce6d69
PP
1487 return 0;
1488}
7a4c00b4 1489#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1490
1491int
8ac85365 1492magic_set(SV *sv, MAGIC *mg)
79072805 1493{
11343788 1494 dTHR;
79072805
LW
1495 register char *s;
1496 I32 i;
8990e307 1497 STRLEN len;
79072805 1498 switch (*mg->mg_ptr) {
748a9306
LW
1499 case '\001': /* ^A */
1500 sv_setsv(bodytarget, sv);
1501 break;
79072805 1502 case '\004': /* ^D */
8990e307 1503 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1504 DEBUG_x(dump_all());
1505 break;
28f23441
PP
1506 case '\005': /* ^E */
1507#ifdef VMS
1508 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1509#else
22fae026
TM
1510#ifdef WIN32
1511 SetLastError( SvIV(sv) );
1512#else
f86702cc
PP
1513 /* will anyone ever use this? */
1514 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441 1515#endif
22fae026 1516#endif
28f23441 1517 break;
79072805 1518 case '\006': /* ^F */
463ee0b2 1519 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1520 break;
a0d0e21e
LW
1521 case '\010': /* ^H */
1522 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1523 break;
79072805
LW
1524 case '\t': /* ^I */
1525 if (inplace)
1526 Safefree(inplace);
1527 if (SvOK(sv))
a0d0e21e 1528 inplace = savepv(SvPV(sv,na));
79072805
LW
1529 else
1530 inplace = Nullch;
1531 break;
28f23441
PP
1532 case '\017': /* ^O */
1533 if (osname)
1534 Safefree(osname);
1535 if (SvOK(sv))
1536 osname = savepv(SvPV(sv,na));
1537 else
1538 osname = Nullch;
1539 break;
79072805 1540 case '\020': /* ^P */
84902520 1541 perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1542 break;
1543 case '\024': /* ^T */
88e89b8a
PP
1544#ifdef BIG_TIME
1545 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1546#else
85e6fe83 1547 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1548#endif
79072805
LW
1549 break;
1550 case '\027': /* ^W */
463ee0b2 1551 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1552 break;
1553 case '.':
748a9306
LW
1554 if (localizing) {
1555 if (localizing == 1)
1556 save_sptr((SV**)&last_in_gv);
1557 }
88e89b8a 1558 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1559 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1560 break;
1561 case '^':
a0d0e21e
LW
1562 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1563 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1564 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1565 break;
1566 case '~':
a0d0e21e
LW
1567 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1568 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1569 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1570 break;
1571 case '=':
a0d0e21e 1572 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1573 break;
1574 case '-':
a0d0e21e
LW
1575 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1576 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1577 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1578 break;
1579 case '%':
a0d0e21e 1580 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1581 break;
1582 case '|':
4b65379b
CS
1583 {
1584 IO *io = GvIOp(defoutgv);
1585 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1586 IoFLAGS(io) &= ~IOf_FLUSH;
1587 else {
1588 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1589 PerlIO *ofp = IoOFP(io);
1590 if (ofp)
1591 (void)PerlIO_flush(ofp);
1592 IoFLAGS(io) |= IOf_FLUSH;
1593 }
1594 }
79072805
LW
1595 }
1596 break;
1597 case '*':
463ee0b2 1598 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1599 multiline = (i != 0);
1600 break;
1601 case '/':
c07a80fd
PP
1602 SvREFCNT_dec(nrs);
1603 nrs = newSVsv(sv);
1604 SvREFCNT_dec(rs);
1605 rs = SvREFCNT_inc(nrs);
79072805
LW
1606 break;
1607 case '\\':
1608 if (ors)
1609 Safefree(ors);
e3c19b7b
CS
1610 if (SvOK(sv) || SvGMAGICAL(sv))
1611 ors = savepv(SvPV(sv,orslen));
1612 else {
1613 ors = Nullch;
1614 orslen = 0;
1615 }
79072805
LW
1616 break;
1617 case ',':
1618 if (ofs)
1619 Safefree(ofs);
a0d0e21e 1620 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1621 break;
1622 case '#':
1623 if (ofmt)
1624 Safefree(ofmt);
a0d0e21e 1625 ofmt = savepv(SvPV(sv,na));
79072805
LW
1626 break;
1627 case '[':
a0d0e21e 1628 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1629 break;
1630 case '?':
ff0cee69
PP
1631#ifdef COMPLEX_STATUS
1632 if (localizing == 2) {
1633 statusvalue = LvTARGOFF(sv);
1634 statusvalue_vms = LvTARGLEN(sv);
1635 }
1636 else
1637#endif
1638#ifdef VMSISH_STATUS
1639 if (VMSISH_STATUS)
1640 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1641 else
1642#endif
1643 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1644 break;
1645 case '!':
78987ded 1646 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
f86702cc 1647 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1648 break;
1649 case '<':
463ee0b2 1650 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1651 if (delaymagic) {
1652 delaymagic |= DM_RUID;
1653 break; /* don't do magic till later */
1654 }
1655#ifdef HAS_SETRUID
85e6fe83 1656 (void)setruid((Uid_t)uid);
79072805
LW
1657#else
1658#ifdef HAS_SETREUID
85e6fe83 1659 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1660#else
85e6fe83
LW
1661#ifdef HAS_SETRESUID
1662 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1663#else
1664 if (uid == euid) /* special case $< = $> */
76e3520e 1665 (void)PerlProc_setuid(uid);
a0d0e21e 1666 else {
76e3520e 1667 uid = (I32)PerlProc_getuid();
463ee0b2 1668 croak("setruid() not implemented");
a0d0e21e 1669 }
79072805
LW
1670#endif
1671#endif
85e6fe83 1672#endif
76e3520e 1673 uid = (I32)PerlProc_getuid();
4633a7c4 1674 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1675 break;
1676 case '>':
463ee0b2 1677 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1678 if (delaymagic) {
1679 delaymagic |= DM_EUID;
1680 break; /* don't do magic till later */
1681 }
1682#ifdef HAS_SETEUID
85e6fe83 1683 (void)seteuid((Uid_t)euid);
79072805
LW
1684#else
1685#ifdef HAS_SETREUID
85e6fe83
LW
1686 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1687#else
1688#ifdef HAS_SETRESUID
1689 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1690#else
1691 if (euid == uid) /* special case $> = $< */
76e3520e 1692 PerlProc_setuid(euid);
a0d0e21e 1693 else {
76e3520e 1694 euid = (I32)PerlProc_geteuid();
463ee0b2 1695 croak("seteuid() not implemented");
a0d0e21e 1696 }
79072805
LW
1697#endif
1698#endif
85e6fe83 1699#endif
76e3520e 1700 euid = (I32)PerlProc_geteuid();
4633a7c4 1701 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1702 break;
1703 case '(':
463ee0b2 1704 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1705 if (delaymagic) {
1706 delaymagic |= DM_RGID;
1707 break; /* don't do magic till later */
1708 }
1709#ifdef HAS_SETRGID
85e6fe83 1710 (void)setrgid((Gid_t)gid);
79072805
LW
1711#else
1712#ifdef HAS_SETREGID
85e6fe83
LW
1713 (void)setregid((Gid_t)gid, (Gid_t)-1);
1714#else
1715#ifdef HAS_SETRESGID
1716 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1717#else
1718 if (gid == egid) /* special case $( = $) */
76e3520e 1719 (void)PerlProc_setgid(gid);
748a9306 1720 else {
76e3520e 1721 gid = (I32)PerlProc_getgid();
463ee0b2 1722 croak("setrgid() not implemented");
748a9306 1723 }
79072805
LW
1724#endif
1725#endif
85e6fe83 1726#endif
76e3520e 1727 gid = (I32)PerlProc_getgid();
4633a7c4 1728 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1729 break;
1730 case ')':
5cd24f17
PP
1731#ifdef HAS_SETGROUPS
1732 {
1733 char *p = SvPV(sv, na);
1734 Groups_t gary[NGROUPS];
1735
1736 SET_NUMERIC_STANDARD();
1737 while (isSPACE(*p))
1738 ++p;
1739 egid = I_V(atof(p));
1740 for (i = 0; i < NGROUPS; ++i) {
1741 while (*p && !isSPACE(*p))
1742 ++p;
1743 while (isSPACE(*p))
1744 ++p;
1745 if (!*p)
1746 break;
1747 gary[i] = I_V(atof(p));
1748 }
8cc95fdb
PP
1749 if (i)
1750 (void)setgroups(i, gary);
5cd24f17
PP
1751 }
1752#else /* HAS_SETGROUPS */
463ee0b2 1753 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1754#endif /* HAS_SETGROUPS */
79072805
LW
1755 if (delaymagic) {
1756 delaymagic |= DM_EGID;
1757 break; /* don't do magic till later */
1758 }
1759#ifdef HAS_SETEGID
85e6fe83 1760 (void)setegid((Gid_t)egid);
79072805
LW
1761#else
1762#ifdef HAS_SETREGID
85e6fe83
LW
1763 (void)setregid((Gid_t)-1, (Gid_t)egid);
1764#else
1765#ifdef HAS_SETRESGID
1766 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1767#else
1768 if (egid == gid) /* special case $) = $( */
76e3520e 1769 (void)PerlProc_setgid(egid);
748a9306 1770 else {
76e3520e 1771 egid = (I32)PerlProc_getegid();
463ee0b2 1772 croak("setegid() not implemented");
748a9306 1773 }
79072805
LW
1774#endif
1775#endif
85e6fe83 1776#endif
76e3520e 1777 egid = (I32)PerlProc_getegid();
4633a7c4 1778 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1779 break;
1780 case ':':
a0d0e21e 1781 chopset = SvPV_force(sv,na);
79072805
LW
1782 break;
1783 case '0':
1784 if (!origalen) {
1785 s = origargv[0];
1786 s += strlen(s);
1787 /* See if all the arguments are contiguous in memory */
1788 for (i = 1; i < origargc; i++) {
fb73857a
PP
1789 if (origargv[i] == s + 1
1790#ifdef OS2
1791 || origargv[i] == s + 2
1792#endif
1793 )
79072805 1794 s += strlen(++s); /* this one is ok too */
fb73857a
PP
1795 else
1796 break;
79072805 1797 }
bbce6d69 1798 /* can grab env area too? */
fb73857a
PP
1799 if (origenviron && (origenviron[0] == s + 1
1800#ifdef OS2
1801 || (origenviron[0] == s + 9 && (s += 8))
1802#endif
1803 )) {
66b1d557 1804 my_setenv("NoNe SuCh", Nullch);
79072805
LW
1805 /* force copy of environment */
1806 for (i = 0; origenviron[i]; i++)
1807 if (origenviron[i] == s + 1)
1808 s += strlen(++s);
fb73857a
PP
1809 else
1810 break;
79072805
LW
1811 }
1812 origalen = s - origargv[0];
1813 }
a0d0e21e 1814 s = SvPV_force(sv,len);
8990e307 1815 i = len;
79072805
LW
1816 if (i >= origalen) {
1817 i = origalen;
fb73857a
PP
1818 /* don't allow system to limit $0 seen by script */
1819 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
79072805 1820 Copy(s, origargv[0], i, char);
fb73857a
PP
1821 s = origargv[0]+i;
1822 *s = '\0';
79072805
LW
1823 }
1824 else {
1825 Copy(s, origargv[0], i, char);
1826 s = origargv[0]+i;
1827 *s++ = '\0';
1828 while (++i < origalen)
8990e307
LW
1829 *s++ = ' ';
1830 s = origargv[0]+i;
ed6116ce 1831 for (i = 1; i < origargc; i++)
8990e307 1832 origargv[i] = Nullch;
79072805
LW
1833 }
1834 break;
a863c7d1
MB
1835#ifdef USE_THREADS
1836 case '@':
38a03e6e 1837 sv_setsv(thr->errsv, sv);
a863c7d1
MB
1838 break;
1839#endif /* USE_THREADS */
79072805
LW
1840 }
1841 return 0;
1842}
1843
f93b4edd
MB
1844#ifdef USE_THREADS
1845int
8ac85365 1846magic_mutexfree(SV *sv, MAGIC *mg)
f93b4edd
MB
1847{
1848 dTHR;
bc1f4c86
MB
1849 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: magic_mutexfree 0x%lx\n",
1850 (unsigned long)thr, (unsigned long)sv);)
f93b4edd
MB
1851 if (MgOWNER(mg))
1852 croak("panic: magic_mutexfree");
1853 MUTEX_DESTROY(MgMUTEXP(mg));
1854 COND_DESTROY(MgCONDP(mg));
e55aaa0e 1855 SvREFCNT_dec(sv);
f93b4edd
MB
1856 return 0;
1857}
1858#endif /* USE_THREADS */
1859
79072805 1860I32
8ac85365 1861whichsig(char *sig)
79072805
LW
1862{
1863 register char **sigv;
1864
1865 for (sigv = sig_name+1; *sigv; sigv++)
1866 if (strEQ(sig,*sigv))
8e07c86e 1867 return sig_num[sigv - sig_name];
79072805
LW
1868#ifdef SIGCLD
1869 if (strEQ(sig,"CHLD"))
1870 return SIGCLD;
1871#endif
1872#ifdef SIGCHLD
1873 if (strEQ(sig,"CLD"))
1874 return SIGCHLD;
1875#endif
1876 return 0;
1877}
1878
84902520
TB
1879static SV* sig_sv;
1880
76e3520e 1881STATIC void
8ac85365 1882unwind_handler_stack(void *p)
84902520 1883{
ff26ac79 1884 dTHR;
84902520
TB
1885 U32 flags = *(U32*)p;
1886
1887 if (flags & 1)
1888 savestack_ix -= 5; /* Unprotect save in progress. */
1889 /* cxstack_ix-- Not needed, die already unwound it. */
1890 if (flags & 64)
1891 SvREFCNT_dec(sig_sv);
1892}
1893
ecfc5424 1894Signal_t
8ac85365 1895sighandler(int sig)
79072805
LW
1896{
1897 dSP;
00d579c5 1898 GV *gv = Nullgv;
a0d0e21e 1899 HV *st;
84902520 1900 SV *sv, *tSv = Sv;
00d579c5 1901 CV *cv = Nullcv;
84902520
TB
1902 OP *myop = op;
1903 U32 flags = 0;
1904 I32 o_save_i = savestack_ix, type;
84902520
TB
1905 XPV *tXpv = Xpv;
1906
1907 if (savestack_ix + 15 <= savestack_max)
1908 flags |= 1;
84902520
TB
1909 if (markstack_ptr < markstack_max - 2)
1910 flags |= 4;
1911 if (retstack_ix < retstack_max - 2)
1912 flags |= 8;
1913 if (scopestack_ix < scopestack_max - 3)
1914 flags |= 16;
1915
ff0cee69
PP
1916 if (!psig_ptr[sig])
1917 die("Signal SIG%s received, but no signal handler set.\n",
1918 sig_name[sig]);
1919
84902520
TB
1920 /* Max number of items pushed there is 3*n or 4. We cannot fix
1921 infinity, so we fix 4 (in fact 5): */
1922 if (flags & 1) {
1923 savestack_ix += 5; /* Protect save in progress. */
1924 o_save_i = savestack_ix;
565764a8 1925 SAVEDESTRUCTOR(unwind_handler_stack, (void*)&flags);
84902520
TB
1926 }
1927 if (flags & 4)
1928 markstack_ptr++; /* Protect mark. */
1929 if (flags & 8) {
1930 retstack_ix++;
1931 retstack[retstack_ix] = NULL;
1932 }
1933 if (flags & 16)
1934 scopestack_ix += 1;
1935 /* sv_2cv is too complicated, try a simpler variant first: */
1936 if (!SvROK(psig_ptr[sig]) || !(cv = (CV*)SvRV(psig_ptr[sig]))
1937 || SvTYPE(cv) != SVt_PVCV)
1938 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
1939
a0d0e21e 1940 if (!cv || !CvROOT(cv)) {
79072805
LW
1941 if (dowarn)
1942 warn("SIG%s handler \"%s\" not defined.\n",
00d579c5
GS
1943 sig_name[sig], (gv ? GvENAME(gv)
1944 : ((cv && CvGV(cv))
1945 ? GvENAME(CvGV(cv))
1946 : "__ANON__")));
1947 goto cleanup;
79072805
LW
1948 }
1949
84902520 1950 if(psig_name[sig]) {
88e89b8a 1951 sv = SvREFCNT_inc(psig_name[sig]);
84902520
TB
1952 flags |= 64;
1953 sig_sv = sv;
1954 } else {
ff0cee69
PP
1955 sv = sv_newmortal();
1956 sv_setpv(sv,sig_name[sig]);
88e89b8a 1957 }
e336de0d 1958
e788e7d3 1959 PUSHSTACKi(PERLSI_SIGNAL);
924508f0 1960 PUSHMARK(SP);
79072805 1961 PUSHs(sv);
79072805 1962 PUTBACK;
a0d0e21e
LW
1963
1964 perl_call_sv((SV*)cv, G_DISCARD);
79072805 1965
d3acc0f7 1966 POPSTACK;
00d579c5 1967cleanup:
84902520
TB
1968 if (flags & 1)
1969 savestack_ix -= 8; /* Unprotect save in progress. */
84902520
TB
1970 if (flags & 4)
1971 markstack_ptr--;
1972 if (flags & 8)
1973 retstack_ix--;
1974 if (flags & 16)
1975 scopestack_ix -= 1;
1976 if (flags & 64)
1977 SvREFCNT_dec(sv);
1978 op = myop; /* Apparently not needed... */
1979
1980 Sv = tSv; /* Restore global temporaries. */
1981 Xpv = tXpv;
79072805
LW
1982 return;
1983}
4e35701f
NIS
1984
1985