This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweaks to allow compilation without -DUSE_THREADS and fix
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, 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
a0d0e21e
LW
18/* Omit -- it causes too much grief on mixed systems.
19#ifdef I_UNISTD
20# include <unistd.h>
21#endif
22*/
23
c07a80fd
PP
24/*
25 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
26 */
27
28struct magic_state {
29 SV* mgs_sv;
30 U32 mgs_flags;
31};
32typedef struct magic_state MGS;
33
34static void restore_magic _((void *p));
35
36static MGS *
37save_magic(sv)
38SV* sv;
39{
40 MGS* mgs;
41
42 assert(SvMAGICAL(sv));
43
44 mgs = (MGS*)safemalloc(sizeof(MGS));
45 mgs->mgs_sv = sv;
46 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
47 SAVEDESTRUCTOR(restore_magic, mgs);
48
49 SvMAGICAL_off(sv);
50 SvREADONLY_off(sv);
51 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
52
53 return mgs;
54}
55
56static void
57restore_magic(p)
58void* p;
59{
60 MGS *mgs = (MGS*)p;
61 SV* sv = mgs->mgs_sv;
62
63 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
64 {
65 if (mgs->mgs_flags)
66 SvFLAGS(sv) |= mgs->mgs_flags;
67 else
68 mg_magical(sv);
69 if (SvGMAGICAL(sv))
70 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
71 }
72
73 Safefree(mgs);
74}
75
8e07c86e 76
8990e307
LW
77void
78mg_magical(sv)
79SV* sv;
80{
81 MAGIC* mg;
82 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
83 MGVTBL* vtbl = mg->mg_virtual;
84 if (vtbl) {
a0d0e21e 85 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
86 SvGMAGICAL_on(sv);
87 if (vtbl->svt_set)
88 SvSMAGICAL_on(sv);
89 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
90 SvRMAGICAL_on(sv);
91 }
92 }
93}
94
79072805
LW
95int
96mg_get(sv)
97SV* sv;
98{
c07a80fd 99 MGS* mgs;
79072805 100 MAGIC* mg;
c6496cc7 101 MAGIC** mgp;
463ee0b2 102
c07a80fd
PP
103 ENTER;
104 mgs = save_magic(sv);
463ee0b2 105
c6496cc7
PP
106 mgp = &SvMAGIC(sv);
107 while ((mg = *mgp) != 0) {
79072805 108 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e 109 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
79072805 110 (*vtbl->svt_get)(sv, mg);
c6496cc7
PP
111 /* Ignore this magic if it's been deleted */
112 if (*mgp == mg && (mg->mg_flags & MGf_GSKIP))
c07a80fd 113 mgs->mgs_flags = 0;
a0d0e21e 114 }
c6496cc7
PP
115 /* Advance to next magic (complicated by possible deletion) */
116 if (*mgp == mg)
117 mgp = &mg->mg_moremagic;
79072805 118 }
463ee0b2 119
c07a80fd 120 LEAVE;
79072805
LW
121 return 0;
122}
123
124int
125mg_set(sv)
126SV* sv;
127{
c07a80fd 128 MGS* mgs;
79072805 129 MAGIC* mg;
463ee0b2
LW
130 MAGIC* nextmg;
131
c07a80fd
PP
132 ENTER;
133 mgs = save_magic(sv);
463ee0b2
LW
134
135 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 136 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 137 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
138 if (mg->mg_flags & MGf_GSKIP) {
139 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
c07a80fd 140 mgs->mgs_flags = 0;
a0d0e21e 141 }
79072805
LW
142 if (vtbl && vtbl->svt_set)
143 (*vtbl->svt_set)(sv, mg);
144 }
463ee0b2 145
c07a80fd 146 LEAVE;
79072805
LW
147 return 0;
148}
149
150U32
151mg_len(sv)
152SV* sv;
153{
154 MAGIC* mg;
748a9306 155 char *junk;
463ee0b2 156 STRLEN len;
463ee0b2 157
79072805
LW
158 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
159 MGVTBL* vtbl = mg->mg_virtual;
85e6fe83 160 if (vtbl && vtbl->svt_len) {
c07a80fd
PP
161 ENTER;
162 save_magic(sv);
a0d0e21e 163 /* omit MGf_GSKIP -- not changed here */
85e6fe83 164 len = (*vtbl->svt_len)(sv, mg);
c07a80fd 165 LEAVE;
85e6fe83
LW
166 return len;
167 }
168 }
169
748a9306 170 junk = SvPV(sv, len);
463ee0b2 171 return len;
79072805
LW
172}
173
174int
175mg_clear(sv)
176SV* sv;
177{
178 MAGIC* mg;
463ee0b2 179
c07a80fd
PP
180 ENTER;
181 save_magic(sv);
463ee0b2 182
79072805
LW
183 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
184 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e
LW
185 /* omit GSKIP -- never set here */
186
79072805
LW
187 if (vtbl && vtbl->svt_clear)
188 (*vtbl->svt_clear)(sv, mg);
189 }
463ee0b2 190
c07a80fd 191 LEAVE;
79072805
LW
192 return 0;
193}
194
93a17b20
LW
195MAGIC*
196mg_find(sv, type)
197SV* sv;
a0d0e21e 198int type;
93a17b20
LW
199{
200 MAGIC* mg;
93a17b20
LW
201 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
202 if (mg->mg_type == type)
203 return mg;
204 }
205 return 0;
206}
207
79072805 208int
463ee0b2 209mg_copy(sv, nsv, key, klen)
79072805 210SV* sv;
463ee0b2
LW
211SV* nsv;
212char *key;
213STRLEN klen;
79072805 214{
463ee0b2 215 int count = 0;
79072805 216 MAGIC* mg;
463ee0b2
LW
217 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
218 if (isUPPER(mg->mg_type)) {
a0d0e21e 219 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
463ee0b2 220 count++;
79072805 221 }
79072805 222 }
463ee0b2 223 return count;
79072805
LW
224}
225
226int
463ee0b2 227mg_free(sv)
79072805
LW
228SV* sv;
229{
230 MAGIC* mg;
231 MAGIC* moremagic;
232 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
233 MGVTBL* vtbl = mg->mg_virtual;
234 moremagic = mg->mg_moremagic;
235 if (vtbl && vtbl->svt_free)
236 (*vtbl->svt_free)(sv, mg);
93a17b20 237 if (mg->mg_ptr && mg->mg_type != 'g')
79072805 238 Safefree(mg->mg_ptr);
85e6fe83 239 if (mg->mg_flags & MGf_REFCOUNTED)
8990e307 240 SvREFCNT_dec(mg->mg_obj);
79072805
LW
241 Safefree(mg);
242 }
243 SvMAGIC(sv) = 0;
244 return 0;
245}
246
247#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
248#include <signal.h>
249#endif
250
93a17b20
LW
251U32
252magic_len(sv, mg)
253SV *sv;
254MAGIC *mg;
255{
256 register I32 paren;
257 register char *s;
258 register I32 i;
748a9306 259 char *t;
93a17b20
LW
260
261 switch (*mg->mg_ptr) {
262 case '1': case '2': case '3': case '4':
263 case '5': case '6': case '7': case '8': case '9': case '&':
264 if (curpm) {
265 paren = atoi(mg->mg_ptr);
266 getparen:
267 if (curpm->op_pmregexp &&
268 paren <= curpm->op_pmregexp->nparens &&
748a9306
LW
269 (s = curpm->op_pmregexp->startp[paren]) &&
270 (t = curpm->op_pmregexp->endp[paren]) ) {
271 i = t - s;
93a17b20
LW
272 if (i >= 0)
273 return i;
93a17b20 274 }
93a17b20 275 }
748a9306 276 return 0;
93a17b20
LW
277 break;
278 case '+':
279 if (curpm) {
280 paren = curpm->op_pmregexp->lastparen;
a0d0e21e
LW
281 if (!paren)
282 return 0;
93a17b20
LW
283 goto getparen;
284 }
748a9306 285 return 0;
93a17b20
LW
286 break;
287 case '`':
288 if (curpm) {
289 if (curpm->op_pmregexp &&
290 (s = curpm->op_pmregexp->subbeg) ) {
291 i = curpm->op_pmregexp->startp[0] - s;
292 if (i >= 0)
293 return i;
93a17b20 294 }
93a17b20 295 }
748a9306 296 return 0;
93a17b20
LW
297 case '\'':
298 if (curpm) {
299 if (curpm->op_pmregexp &&
300 (s = curpm->op_pmregexp->endp[0]) ) {
301 return (STRLEN) (curpm->op_pmregexp->subend - s);
302 }
93a17b20 303 }
748a9306 304 return 0;
93a17b20
LW
305 case ',':
306 return (STRLEN)ofslen;
307 case '\\':
308 return (STRLEN)orslen;
309 }
310 magic_get(sv,mg);
311 if (!SvPOK(sv) && SvNIOK(sv))
463ee0b2 312 sv_2pv(sv, &na);
93a17b20
LW
313 if (SvPOK(sv))
314 return SvCUR(sv);
315 return 0;
316}
317
79072805
LW
318int
319magic_get(sv, mg)
320SV *sv;
321MAGIC *mg;
322{
323 register I32 paren;
324 register char *s;
325 register I32 i;
748a9306 326 char *t;
79072805
LW
327
328 switch (*mg->mg_ptr) {
748a9306
LW
329 case '\001': /* ^A */
330 sv_setsv(sv, bodytarget);
331 break;
79072805
LW
332 case '\004': /* ^D */
333 sv_setiv(sv,(I32)(debug & 32767));
334 break;
28f23441
PP
335 case '\005': /* ^E */
336#ifdef VMS
337 {
338# include <descrip.h>
339# include <starlet.h>
340 char msg[255];
341 $DESCRIPTOR(msgdsc,msg);
342 sv_setnv(sv,(double)vaxc$errno);
343 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
344 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
345 else
346 sv_setpv(sv,"");
347 }
348#else
349 sv_setnv(sv,(double)errno);
350 sv_setpv(sv, errno ? Strerror(errno) : "");
351#endif
352 SvNOK_on(sv); /* what a wonderful hack! */
353 break;
79072805
LW
354 case '\006': /* ^F */
355 sv_setiv(sv,(I32)maxsysfd);
356 break;
a0d0e21e
LW
357 case '\010': /* ^H */
358 sv_setiv(sv,(I32)hints);
359 break;
79072805
LW
360 case '\t': /* ^I */
361 if (inplace)
362 sv_setpv(sv, inplace);
363 else
364 sv_setsv(sv,&sv_undef);
365 break;
28f23441
PP
366 case '\017': /* ^O */
367 sv_setpv(sv,osname);
368 break;
79072805
LW
369 case '\020': /* ^P */
370 sv_setiv(sv,(I32)perldb);
371 break;
372 case '\024': /* ^T */
373 sv_setiv(sv,(I32)basetime);
374 break;
375 case '\027': /* ^W */
376 sv_setiv(sv,(I32)dowarn);
377 break;
378 case '1': case '2': case '3': case '4':
379 case '5': case '6': case '7': case '8': case '9': case '&':
380 if (curpm) {
381 paren = atoi(GvENAME(mg->mg_obj));
382 getparen:
383 if (curpm->op_pmregexp &&
384 paren <= curpm->op_pmregexp->nparens &&
a0d0e21e 385 (s = curpm->op_pmregexp->startp[paren]) &&
748a9306
LW
386 (t = curpm->op_pmregexp->endp[paren]) ) {
387 i = t - s;
388 if (i >= 0) {
389 MAGIC *tmg;
79072805 390 sv_setpvn(sv,s,i);
748a9306
LW
391 if (tainting && (tmg = mg_find(sv,'t')))
392 tmg->mg_len = 0; /* guarantee $1 untainted */
393 break;
394 }
79072805 395 }
79072805 396 }
748a9306 397 sv_setsv(sv,&sv_undef);
79072805
LW
398 break;
399 case '+':
400 if (curpm) {
401 paren = curpm->op_pmregexp->lastparen;
a0d0e21e
LW
402 if (paren)
403 goto getparen;
79072805 404 }
748a9306 405 sv_setsv(sv,&sv_undef);
79072805
LW
406 break;
407 case '`':
408 if (curpm) {
409 if (curpm->op_pmregexp &&
410 (s = curpm->op_pmregexp->subbeg) ) {
411 i = curpm->op_pmregexp->startp[0] - s;
748a9306 412 if (i >= 0) {
79072805 413 sv_setpvn(sv,s,i);
748a9306
LW
414 break;
415 }
79072805 416 }
79072805 417 }
748a9306 418 sv_setsv(sv,&sv_undef);
79072805
LW
419 break;
420 case '\'':
421 if (curpm) {
422 if (curpm->op_pmregexp &&
423 (s = curpm->op_pmregexp->endp[0]) ) {
424 sv_setpvn(sv,s, curpm->op_pmregexp->subend - s);
748a9306 425 break;
79072805 426 }
79072805 427 }
748a9306 428 sv_setsv(sv,&sv_undef);
79072805
LW
429 break;
430 case '.':
431#ifndef lint
a0d0e21e 432 if (GvIO(last_in_gv)) {
8990e307 433 sv_setiv(sv,(I32)IoLINES(GvIO(last_in_gv)));
79072805
LW
434 }
435#endif
436 break;
437 case '?':
438 sv_setiv(sv,(I32)statusvalue);
439 break;
440 case '^':
a0d0e21e 441 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
442 if (s)
443 sv_setpv(sv,s);
444 else {
445 sv_setpv(sv,GvENAME(defoutgv));
446 sv_catpv(sv,"_TOP");
447 }
448 break;
449 case '~':
a0d0e21e 450 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
451 if (!s)
452 s = GvENAME(defoutgv);
453 sv_setpv(sv,s);
454 break;
455#ifndef lint
456 case '=':
a0d0e21e 457 sv_setiv(sv,(I32)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
458 break;
459 case '-':
a0d0e21e 460 sv_setiv(sv,(I32)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
461 break;
462 case '%':
a0d0e21e 463 sv_setiv(sv,(I32)IoPAGE(GvIOp(defoutgv)));
79072805
LW
464 break;
465#endif
466 case ':':
467 break;
468 case '/':
469 break;
470 case '[':
a0d0e21e 471 sv_setiv(sv,(I32)curcop->cop_arybase);
79072805
LW
472 break;
473 case '|':
a0d0e21e 474 sv_setiv(sv, (IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
475 break;
476 case ',':
477 sv_setpvn(sv,ofs,ofslen);
478 break;
479 case '\\':
480 sv_setpvn(sv,ors,orslen);
481 break;
482 case '#':
483 sv_setpv(sv,ofmt);
484 break;
485 case '!':
a5f75d66
AD
486#ifdef VMS
487 sv_setnv(sv,(double)((errno == EVMSERR) ? vaxc$errno : errno));
488#else
79072805 489 sv_setnv(sv,(double)errno);
a5f75d66 490#endif
2304df62 491 sv_setpv(sv, errno ? Strerror(errno) : "");
79072805
LW
492 SvNOK_on(sv); /* what a wonderful hack! */
493 break;
494 case '<':
495 sv_setiv(sv,(I32)uid);
496 break;
497 case '>':
498 sv_setiv(sv,(I32)euid);
499 break;
500 case '(':
501 s = buf;
502 (void)sprintf(s,"%d",(int)gid);
503 goto add_groups;
504 case ')':
505 s = buf;
506 (void)sprintf(s,"%d",(int)egid);
507 add_groups:
508 while (*s) s++;
509#ifdef HAS_GETGROUPS
510#ifndef NGROUPS
511#define NGROUPS 32
512#endif
513 {
a0d0e21e 514 Groups_t gary[NGROUPS];
79072805
LW
515
516 i = getgroups(NGROUPS,gary);
517 while (--i >= 0) {
518 (void)sprintf(s," %ld", (long)gary[i]);
519 while (*s) s++;
520 }
521 }
522#endif
523 sv_setpv(sv,buf);
524 break;
525 case '*':
526 break;
527 case '0':
528 break;
529 }
a0d0e21e 530 return 0;
79072805
LW
531}
532
533int
534magic_getuvar(sv, mg)
535SV *sv;
536MAGIC *mg;
537{
538 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
539
540 if (uf && uf->uf_val)
541 (*uf->uf_val)(uf->uf_index, sv);
542 return 0;
543}
544
545int
546magic_setenv(sv,mg)
547SV* sv;
548MAGIC* mg;
549{
550 register char *s;
a0d0e21e
LW
551 STRLEN len;
552 I32 i;
553 s = SvPV(sv,len);
79072805 554 my_setenv(mg->mg_ptr,s);
a0d0e21e
LW
555#ifdef DYNAMIC_ENV_FETCH
556 /* We just undefd an environment var. Is a replacement */
557 /* waiting in the wings? */
558 if (!len) {
559 SV **envsvp;
560 if (envsvp = hv_fetch(GvHVn(envgv),mg->mg_ptr,mg->mg_len,FALSE))
561 s = SvPV(*envsvp,len);
562 }
563#endif
79072805
LW
564 /* And you'll never guess what the dog had */
565 /* in its mouth... */
463ee0b2
LW
566 if (tainting) {
567 if (s && strEQ(mg->mg_ptr,"PATH")) {
a0d0e21e 568 char *strend = s + len;
463ee0b2
LW
569
570 while (s < strend) {
571 s = cpytill(tokenbuf,s,strend,':',&i);
572 s++;
573 if (*tokenbuf != '/'
a0d0e21e 574 || (Stat(tokenbuf,&statbuf) && (statbuf.st_mode & 2)) )
8990e307 575 MgTAINTEDDIR_on(mg);
463ee0b2 576 }
79072805
LW
577 }
578 }
79072805
LW
579 return 0;
580}
581
582int
85e6fe83
LW
583magic_clearenv(sv,mg)
584SV* sv;
585MAGIC* mg;
586{
587 my_setenv(mg->mg_ptr,Nullch);
588 return 0;
589}
590
3d37d572
PP
591#ifdef HAS_SIGACTION
592/* set up reliable signal() clone */
593
594typedef void (*Sigfunc) _((int));
595
596static
597Sigfunc rsignal(signo,handler)
598int signo;
599Sigfunc handler;
600{
601 struct sigaction act,oact;
602
603 act.sa_handler = handler;
604 sigemptyset(&act.sa_mask);
605 act.sa_flags = 0;
606#ifdef SIGALRM
607 if (signo == SIGALRM) {
608#else
609 if (0) {
610#endif
611#ifdef SA_INTERRUPT
612 act.sa_flags |= SA_INTERRUPT; /* SunOS */
613#endif
614 } else {
615#ifdef SA_RESTART
616 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
617#endif
618 }
619 if (sigaction(signo, &act, &oact) < 0)
620 return(SIG_ERR);
621 else
622 return(oact.sa_handler);
623}
624
625#else
626
627/* ah well, so much for reliability */
628
629#define rsignal(x,y) signal(x,y)
630
631#endif
632
633
85e6fe83 634int
79072805
LW
635magic_setsig(sv,mg)
636SV* sv;
637MAGIC* mg;
638{
11343788 639 dTHR;
79072805
LW
640 register char *s;
641 I32 i;
748a9306 642 SV** svp;
a0d0e21e 643
748a9306
LW
644 s = mg->mg_ptr;
645 if (*s == '_') {
646 if (strEQ(s,"__DIE__"))
647 svp = &diehook;
648 else if (strEQ(s,"__WARN__"))
649 svp = &warnhook;
650 else if (strEQ(s,"__PARSE__"))
651 svp = &parsehook;
652 else
653 croak("No such hook: %s", s);
654 i = 0;
4633a7c4
LW
655 if (*svp) {
656 SvREFCNT_dec(*svp);
657 *svp = 0;
658 }
748a9306
LW
659 }
660 else {
661 i = whichsig(s); /* ...no, a brick */
662 if (!i) {
663 if (dowarn || strEQ(s,"ALARM"))
664 warn("No such signal: SIG%s", s);
665 return 0;
666 }
667 }
a0d0e21e 668 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 669 if (i)
3d37d572 670 (void)rsignal(i,sighandler);
748a9306
LW
671 else
672 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
673 return 0;
674 }
675 s = SvPV_force(sv,na);
748a9306
LW
676 if (strEQ(s,"IGNORE")) {
677 if (i)
3d37d572 678 (void)rsignal(i,SIG_IGN);
748a9306
LW
679 else
680 *svp = 0;
681 }
682 else if (strEQ(s,"DEFAULT") || !*s) {
683 if (i)
3d37d572 684 (void)rsignal(i,SIG_DFL);
748a9306
LW
685 else
686 *svp = 0;
687 }
79072805 688 else {
2304df62
AD
689 if (!strchr(s,':') && !strchr(s,'\'')) {
690 sprintf(tokenbuf, "main::%s",s);
79072805
LW
691 sv_setpv(sv,tokenbuf);
692 }
748a9306 693 if (i)
3d37d572 694 (void)rsignal(i,sighandler);
748a9306
LW
695 else
696 *svp = SvREFCNT_inc(sv);
79072805
LW
697 }
698 return 0;
699}
700
701int
463ee0b2 702magic_setisa(sv,mg)
79072805
LW
703SV* sv;
704MAGIC* mg;
705{
463ee0b2
LW
706 sub_generation++;
707 return 0;
708}
709
a0d0e21e
LW
710#ifdef OVERLOAD
711
463ee0b2 712int
a0d0e21e 713magic_setamagic(sv,mg)
463ee0b2
LW
714SV* sv;
715MAGIC* mg;
716{
a0d0e21e
LW
717 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
718 amagic_generation++;
463ee0b2 719
a0d0e21e
LW
720 return 0;
721}
722#endif /* OVERLOAD */
463ee0b2 723
a0d0e21e
LW
724static int
725magic_methpack(sv,mg,meth)
726SV* sv;
727MAGIC* mg;
728char *meth;
729{
11343788 730 dTHR;
a0d0e21e 731 dSP;
463ee0b2 732
a0d0e21e
LW
733 ENTER;
734 SAVETMPS;
735 PUSHMARK(sp);
736 EXTEND(sp, 2);
737 PUSHs(mg->mg_obj);
463ee0b2 738 if (mg->mg_ptr)
a0d0e21e
LW
739 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
740 else if (mg->mg_type == 'p')
741 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
742 PUTBACK;
743
a0d0e21e
LW
744 if (perl_call_method(meth, G_SCALAR))
745 sv_setsv(sv, *stack_sp--);
463ee0b2 746
a0d0e21e
LW
747 FREETMPS;
748 LEAVE;
749 return 0;
750}
463ee0b2 751
a0d0e21e
LW
752int
753magic_getpack(sv,mg)
754SV* sv;
755MAGIC* mg;
756{
757 magic_methpack(sv,mg,"FETCH");
758 if (mg->mg_ptr)
759 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
760 return 0;
761}
762
763int
764magic_setpack(sv,mg)
765SV* sv;
766MAGIC* mg;
767{
11343788 768 dTHR;
463ee0b2 769 dSP;
463ee0b2 770
a0d0e21e
LW
771 PUSHMARK(sp);
772 EXTEND(sp, 3);
773 PUSHs(mg->mg_obj);
463ee0b2 774 if (mg->mg_ptr)
a0d0e21e
LW
775 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
776 else if (mg->mg_type == 'p')
777 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
778 PUSHs(sv);
779 PUTBACK;
780
a0d0e21e 781 perl_call_method("STORE", G_SCALAR|G_DISCARD);
463ee0b2
LW
782
783 return 0;
784}
785
786int
787magic_clearpack(sv,mg)
788SV* sv;
789MAGIC* mg;
790{
a0d0e21e
LW
791 return magic_methpack(sv,mg,"DELETE");
792}
463ee0b2 793
a0d0e21e
LW
794int magic_wipepack(sv,mg)
795SV* sv;
796MAGIC* mg;
797{
11343788 798 dTHR;
a0d0e21e 799 dSP;
463ee0b2 800
a0d0e21e
LW
801 PUSHMARK(sp);
802 XPUSHs(mg->mg_obj);
463ee0b2 803 PUTBACK;
463ee0b2 804
a0d0e21e 805 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
463ee0b2
LW
806
807 return 0;
808}
809
810int
811magic_nextpack(sv,mg,key)
812SV* sv;
813MAGIC* mg;
814SV* key;
815{
11343788 816 dTHR;
463ee0b2 817 dSP;
a0d0e21e 818 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
819
820 ENTER;
a0d0e21e
LW
821 SAVETMPS;
822 PUSHMARK(sp);
823 EXTEND(sp, 2);
824 PUSHs(mg->mg_obj);
463ee0b2
LW
825 if (SvOK(key))
826 PUSHs(key);
827 PUTBACK;
828
a0d0e21e
LW
829 if (perl_call_method(meth, G_SCALAR))
830 sv_setsv(key, *stack_sp--);
463ee0b2 831
a0d0e21e
LW
832 FREETMPS;
833 LEAVE;
79072805
LW
834 return 0;
835}
836
837int
a0d0e21e
LW
838magic_existspack(sv,mg)
839SV* sv;
840MAGIC* mg;
841{
842 return magic_methpack(sv,mg,"EXISTS");
843}
844
845int
79072805
LW
846magic_setdbline(sv,mg)
847SV* sv;
848MAGIC* mg;
849{
11343788 850 dTHR;
79072805
LW
851 OP *o;
852 I32 i;
853 GV* gv;
854 SV** svp;
855
856 gv = DBline;
857 i = SvTRUE(sv);
858 svp = av_fetch(GvAV(gv),atoi(mg->mg_ptr), FALSE);
8990e307 859 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 860 o->op_private = i;
79072805
LW
861 else
862 warn("Can't break at that line\n");
863 return 0;
864}
865
866int
867magic_getarylen(sv,mg)
868SV* sv;
869MAGIC* mg;
870{
a0d0e21e 871 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
872 return 0;
873}
874
875int
876magic_setarylen(sv,mg)
877SV* sv;
878MAGIC* mg;
879{
a0d0e21e
LW
880 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
881 return 0;
882}
883
884int
885magic_getpos(sv,mg)
886SV* sv;
887MAGIC* mg;
888{
889 SV* lsv = LvTARG(sv);
890
891 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
892 mg = mg_find(lsv, 'g');
893 if (mg && mg->mg_len >= 0) {
894 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
895 return 0;
896 }
897 }
898 (void)SvOK_off(sv);
899 return 0;
900}
901
902int
903magic_setpos(sv,mg)
904SV* sv;
905MAGIC* mg;
906{
907 SV* lsv = LvTARG(sv);
908 SSize_t pos;
909 STRLEN len;
910
911 mg = 0;
912
913 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
914 mg = mg_find(lsv, 'g');
915 if (!mg) {
916 if (!SvOK(sv))
917 return 0;
918 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
919 mg = mg_find(lsv, 'g');
920 }
921 else if (!SvOK(sv)) {
922 mg->mg_len = -1;
923 return 0;
924 }
925 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
926
927 pos = SvIV(sv) - curcop->cop_arybase;
928 if (pos < 0) {
929 pos += len;
930 if (pos < 0)
931 pos = 0;
932 }
933 else if (pos > len)
934 pos = len;
935 mg->mg_len = pos;
936
79072805
LW
937 return 0;
938}
939
940int
941magic_getglob(sv,mg)
942SV* sv;
943MAGIC* mg;
944{
945 gv_efullname(sv,((GV*)sv));/* a gv value, be nice */
946 return 0;
947}
948
949int
950magic_setglob(sv,mg)
951SV* sv;
952MAGIC* mg;
953{
954 register char *s;
955 GV* gv;
956
957 if (!SvOK(sv))
958 return 0;
463ee0b2 959 s = SvPV(sv, na);
79072805
LW
960 if (*s == '*' && s[1])
961 s++;
85e6fe83 962 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
963 if (sv == (SV*)gv)
964 return 0;
965 if (GvGP(sv))
966 gp_free(sv);
967 GvGP(sv) = gp_ref(GvGP(gv));
968 if (!GvAV(gv))
969 gv_AVadd(gv);
970 if (!GvHV(gv))
971 gv_HVadd(gv);
a0d0e21e
LW
972 if (!GvIOp(gv))
973 GvIOp(gv) = newIO();
79072805
LW
974 return 0;
975}
976
977int
978magic_setsubstr(sv,mg)
979SV* sv;
980MAGIC* mg;
981{
8990e307
LW
982 STRLEN len;
983 char *tmps = SvPV(sv,len);
984 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
985 return 0;
986}
987
988int
463ee0b2
LW
989magic_gettaint(sv,mg)
990SV* sv;
991MAGIC* mg;
992{
748a9306
LW
993 if (mg->mg_len & 1)
994 tainted = TRUE;
995 else if (mg->mg_len & 2 && mg->mg_obj == sv) /* kludge */
996 tainted = TRUE;
463ee0b2
LW
997 return 0;
998}
999
1000int
1001magic_settaint(sv,mg)
1002SV* sv;
1003MAGIC* mg;
1004{
11343788 1005 dTHR;
748a9306
LW
1006 if (localizing) {
1007 if (localizing == 1)
1008 mg->mg_len <<= 1;
1009 else
1010 mg->mg_len >>= 1;
a0d0e21e 1011 }
748a9306
LW
1012 else if (tainted)
1013 mg->mg_len |= 1;
1014 else
1015 mg->mg_len &= ~1;
463ee0b2
LW
1016 return 0;
1017}
1018
1019int
79072805
LW
1020magic_setvec(sv,mg)
1021SV* sv;
1022MAGIC* mg;
1023{
1024 do_vecset(sv); /* XXX slurp this routine */
1025 return 0;
1026}
1027
1028int
93a17b20
LW
1029magic_setmglob(sv,mg)
1030SV* sv;
1031MAGIC* mg;
1032{
a0d0e21e 1033 mg->mg_len = -1;
c6496cc7 1034 SvSCREAM_off(sv);
93a17b20
LW
1035 return 0;
1036}
1037
1038int
79072805
LW
1039magic_setbm(sv,mg)
1040SV* sv;
1041MAGIC* mg;
1042{
463ee0b2 1043 sv_unmagic(sv, 'B');
79072805
LW
1044 SvVALID_off(sv);
1045 return 0;
1046}
1047
1048int
1049magic_setuvar(sv,mg)
1050SV* sv;
1051MAGIC* mg;
1052{
1053 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1054
1055 if (uf && uf->uf_set)
1056 (*uf->uf_set)(uf->uf_index, sv);
1057 return 0;
1058}
1059
1060int
1061magic_set(sv,mg)
1062SV* sv;
1063MAGIC* mg;
1064{
11343788 1065 dTHR;
79072805
LW
1066 register char *s;
1067 I32 i;
8990e307 1068 STRLEN len;
79072805 1069 switch (*mg->mg_ptr) {
748a9306
LW
1070 case '\001': /* ^A */
1071 sv_setsv(bodytarget, sv);
1072 break;
79072805 1073 case '\004': /* ^D */
8990e307 1074 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1075 DEBUG_x(dump_all());
1076 break;
28f23441
PP
1077 case '\005': /* ^E */
1078#ifdef VMS
1079 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1080#else
1081 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),4); /* will anyone ever use this? */
1082#endif
1083 break;
79072805 1084 case '\006': /* ^F */
463ee0b2 1085 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1086 break;
a0d0e21e
LW
1087 case '\010': /* ^H */
1088 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1089 break;
79072805
LW
1090 case '\t': /* ^I */
1091 if (inplace)
1092 Safefree(inplace);
1093 if (SvOK(sv))
a0d0e21e 1094 inplace = savepv(SvPV(sv,na));
79072805
LW
1095 else
1096 inplace = Nullch;
1097 break;
28f23441
PP
1098 case '\017': /* ^O */
1099 if (osname)
1100 Safefree(osname);
1101 if (SvOK(sv))
1102 osname = savepv(SvPV(sv,na));
1103 else
1104 osname = Nullch;
1105 break;
79072805 1106 case '\020': /* ^P */
463ee0b2 1107 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1108 if (i != perldb) {
1109 if (perldb)
1110 oldlastpm = curpm;
1111 else
1112 curpm = oldlastpm;
1113 }
1114 perldb = i;
1115 break;
1116 case '\024': /* ^T */
85e6fe83 1117 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1118 break;
1119 case '\027': /* ^W */
463ee0b2 1120 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1121 break;
1122 case '.':
748a9306
LW
1123 if (localizing) {
1124 if (localizing == 1)
1125 save_sptr((SV**)&last_in_gv);
1126 }
2304df62 1127 else if (SvOK(sv))
a0d0e21e 1128 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1129 break;
1130 case '^':
a0d0e21e
LW
1131 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1132 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1133 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1134 break;
1135 case '~':
a0d0e21e
LW
1136 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1137 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1138 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1139 break;
1140 case '=':
a0d0e21e 1141 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1142 break;
1143 case '-':
a0d0e21e
LW
1144 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1145 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1146 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1147 break;
1148 case '%':
a0d0e21e 1149 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1150 break;
1151 case '|':
a0d0e21e 1152 IoFLAGS(GvIOp(defoutgv)) &= ~IOf_FLUSH;
463ee0b2 1153 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) != 0) {
a0d0e21e 1154 IoFLAGS(GvIOp(defoutgv)) |= IOf_FLUSH;
79072805
LW
1155 }
1156 break;
1157 case '*':
463ee0b2 1158 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1159 multiline = (i != 0);
1160 break;
1161 case '/':
c07a80fd
PP
1162 SvREFCNT_dec(nrs);
1163 nrs = newSVsv(sv);
1164 SvREFCNT_dec(rs);
1165 rs = SvREFCNT_inc(nrs);
79072805
LW
1166 break;
1167 case '\\':
1168 if (ors)
1169 Safefree(ors);
a0d0e21e 1170 ors = savepv(SvPV(sv,orslen));
79072805
LW
1171 break;
1172 case ',':
1173 if (ofs)
1174 Safefree(ofs);
a0d0e21e 1175 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1176 break;
1177 case '#':
1178 if (ofmt)
1179 Safefree(ofmt);
a0d0e21e 1180 ofmt = savepv(SvPV(sv,na));
79072805
LW
1181 break;
1182 case '[':
a0d0e21e 1183 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1184 break;
1185 case '?':
748a9306 1186 statusvalue = FIXSTATUS(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1187 break;
1188 case '!':
28f23441 1189 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),SvIV(sv) == EVMSERR ? 4 : vaxc$errno); /* will anyone ever use this? */
79072805
LW
1190 break;
1191 case '<':
463ee0b2 1192 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1193 if (delaymagic) {
1194 delaymagic |= DM_RUID;
1195 break; /* don't do magic till later */
1196 }
1197#ifdef HAS_SETRUID
85e6fe83 1198 (void)setruid((Uid_t)uid);
79072805
LW
1199#else
1200#ifdef HAS_SETREUID
85e6fe83 1201 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1202#else
85e6fe83
LW
1203#ifdef HAS_SETRESUID
1204 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1205#else
1206 if (uid == euid) /* special case $< = $> */
1207 (void)setuid(uid);
a0d0e21e
LW
1208 else {
1209 uid = (I32)getuid();
463ee0b2 1210 croak("setruid() not implemented");
a0d0e21e 1211 }
79072805
LW
1212#endif
1213#endif
85e6fe83 1214#endif
748a9306 1215 uid = (I32)getuid();
4633a7c4 1216 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1217 break;
1218 case '>':
463ee0b2 1219 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1220 if (delaymagic) {
1221 delaymagic |= DM_EUID;
1222 break; /* don't do magic till later */
1223 }
1224#ifdef HAS_SETEUID
85e6fe83 1225 (void)seteuid((Uid_t)euid);
79072805
LW
1226#else
1227#ifdef HAS_SETREUID
85e6fe83
LW
1228 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1229#else
1230#ifdef HAS_SETRESUID
1231 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1232#else
1233 if (euid == uid) /* special case $> = $< */
1234 setuid(euid);
a0d0e21e
LW
1235 else {
1236 euid = (I32)geteuid();
463ee0b2 1237 croak("seteuid() not implemented");
a0d0e21e 1238 }
79072805
LW
1239#endif
1240#endif
85e6fe83 1241#endif
79072805 1242 euid = (I32)geteuid();
4633a7c4 1243 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1244 break;
1245 case '(':
463ee0b2 1246 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1247 if (delaymagic) {
1248 delaymagic |= DM_RGID;
1249 break; /* don't do magic till later */
1250 }
1251#ifdef HAS_SETRGID
85e6fe83 1252 (void)setrgid((Gid_t)gid);
79072805
LW
1253#else
1254#ifdef HAS_SETREGID
85e6fe83
LW
1255 (void)setregid((Gid_t)gid, (Gid_t)-1);
1256#else
1257#ifdef HAS_SETRESGID
1258 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1259#else
1260 if (gid == egid) /* special case $( = $) */
1261 (void)setgid(gid);
748a9306
LW
1262 else {
1263 gid = (I32)getgid();
463ee0b2 1264 croak("setrgid() not implemented");
748a9306 1265 }
79072805
LW
1266#endif
1267#endif
85e6fe83 1268#endif
79072805 1269 gid = (I32)getgid();
4633a7c4 1270 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1271 break;
1272 case ')':
463ee0b2 1273 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1274 if (delaymagic) {
1275 delaymagic |= DM_EGID;
1276 break; /* don't do magic till later */
1277 }
1278#ifdef HAS_SETEGID
85e6fe83 1279 (void)setegid((Gid_t)egid);
79072805
LW
1280#else
1281#ifdef HAS_SETREGID
85e6fe83
LW
1282 (void)setregid((Gid_t)-1, (Gid_t)egid);
1283#else
1284#ifdef HAS_SETRESGID
1285 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1286#else
1287 if (egid == gid) /* special case $) = $( */
1288 (void)setgid(egid);
748a9306
LW
1289 else {
1290 egid = (I32)getegid();
463ee0b2 1291 croak("setegid() not implemented");
748a9306 1292 }
79072805
LW
1293#endif
1294#endif
85e6fe83 1295#endif
79072805 1296 egid = (I32)getegid();
4633a7c4 1297 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1298 break;
1299 case ':':
a0d0e21e 1300 chopset = SvPV_force(sv,na);
79072805
LW
1301 break;
1302 case '0':
1303 if (!origalen) {
1304 s = origargv[0];
1305 s += strlen(s);
1306 /* See if all the arguments are contiguous in memory */
1307 for (i = 1; i < origargc; i++) {
1308 if (origargv[i] == s + 1)
1309 s += strlen(++s); /* this one is ok too */
1310 }
1311 if (origenviron[0] == s + 1) { /* can grab env area too? */
1312 my_setenv("NoNeSuCh", Nullch);
1313 /* force copy of environment */
1314 for (i = 0; origenviron[i]; i++)
1315 if (origenviron[i] == s + 1)
1316 s += strlen(++s);
1317 }
1318 origalen = s - origargv[0];
1319 }
a0d0e21e 1320 s = SvPV_force(sv,len);
8990e307 1321 i = len;
79072805
LW
1322 if (i >= origalen) {
1323 i = origalen;
1324 SvCUR_set(sv, i);
1325 *SvEND(sv) = '\0';
1326 Copy(s, origargv[0], i, char);
1327 }
1328 else {
1329 Copy(s, origargv[0], i, char);
1330 s = origargv[0]+i;
1331 *s++ = '\0';
1332 while (++i < origalen)
8990e307
LW
1333 *s++ = ' ';
1334 s = origargv[0]+i;
ed6116ce 1335 for (i = 1; i < origargc; i++)
8990e307 1336 origargv[i] = Nullch;
79072805
LW
1337 }
1338 break;
1339 }
1340 return 0;
1341}
1342
1343I32
1344whichsig(sig)
1345char *sig;
1346{
1347 register char **sigv;
1348
1349 for (sigv = sig_name+1; *sigv; sigv++)
1350 if (strEQ(sig,*sigv))
8e07c86e 1351 return sig_num[sigv - sig_name];
79072805
LW
1352#ifdef SIGCLD
1353 if (strEQ(sig,"CHLD"))
1354 return SIGCLD;
1355#endif
1356#ifdef SIGCHLD
1357 if (strEQ(sig,"CLD"))
1358 return SIGCHLD;
1359#endif
1360 return 0;
1361}
1362
ecfc5424 1363Signal_t
79072805 1364sighandler(sig)
a0d0e21e 1365int sig;
79072805 1366{
11343788 1367 dTHR;
79072805
LW
1368 dSP;
1369 GV *gv;
a0d0e21e 1370 HV *st;
79072805
LW
1371 SV *sv;
1372 CV *cv;
79072805 1373 AV *oldstack;
8e07c86e 1374 char *signame;
79072805
LW
1375
1376#ifdef OS2 /* or anybody else who requires SIG_ACK */
1377 signal(sig, SIG_ACK);
1378#endif
1379
4633a7c4 1380 signame = sig_name[sig];
8e07c86e 1381 cv = sv_2cv(*hv_fetch(GvHVn(siggv),signame,strlen(signame),
a0d0e21e
LW
1382 TRUE),
1383 &st, &gv, TRUE);
1384 if (!cv || !CvROOT(cv) &&
8e07c86e 1385 *signame == 'C' && instr(signame,"LD")) {
a0d0e21e 1386
8e07c86e 1387 if (signame[1] == 'H')
a0d0e21e
LW
1388 cv = sv_2cv(*hv_fetch(GvHVn(siggv),"CLD",3,TRUE),
1389 &st, &gv, TRUE);
79072805 1390 else
a0d0e21e
LW
1391 cv = sv_2cv(*hv_fetch(GvHVn(siggv),"CHLD",4,TRUE),
1392 &st, &gv, TRUE);
1393 /* gag */
79072805 1394 }
a0d0e21e 1395 if (!cv || !CvROOT(cv)) {
79072805
LW
1396 if (dowarn)
1397 warn("SIG%s handler \"%s\" not defined.\n",
8e07c86e 1398 signame, GvENAME(gv) );
79072805
LW
1399 return;
1400 }
1401
1402 oldstack = stack;
a0d0e21e
LW
1403 if (stack != signalstack)
1404 AvFILL(signalstack) = 0;
79072805
LW
1405 SWITCHSTACK(stack, signalstack);
1406
8990e307 1407 sv = sv_newmortal();
8e07c86e 1408 sv_setpv(sv,signame);
a0d0e21e 1409 PUSHMARK(sp);
79072805 1410 PUSHs(sv);
79072805 1411 PUTBACK;
a0d0e21e
LW
1412
1413 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1414
1415 SWITCHSTACK(signalstack, oldstack);
79072805
LW
1416
1417 return;
1418}