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