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