This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix formatting in perldiag
[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 388 break;
f86702cc
PP
389 case '\023': /* ^S */
390 if (STATUS_NATIVE == -1)
391 sv_setiv(sv, (IV)-1);
392 else
393 sv_setuv(sv, (UV)STATUS_NATIVE);
394 break;
79072805 395 case '\024': /* ^T */
88e89b8a 396#ifdef BIG_TIME
188ea221 397 sv_setnv(sv, basetime);
88e89b8a 398#else
188ea221 399 sv_setiv(sv, (IV)basetime);
88e89b8a 400#endif
79072805
LW
401 break;
402 case '\027': /* ^W */
188ea221 403 sv_setiv(sv, (IV)dowarn);
79072805
LW
404 break;
405 case '1': case '2': case '3': case '4':
406 case '5': case '6': case '7': case '8': case '9': case '&':
bbce6d69 407 if (curpm && (rx = curpm->op_pmregexp)) {
88e89b8a 408 paren = atoi(GvENAME((GV*)mg->mg_obj));
79072805 409 getparen:
bbce6d69
PP
410 if (paren <= rx->nparens &&
411 (s = rx->startp[paren]) &&
412 (t = rx->endp[paren]))
413 {
748a9306 414 i = t - s;
13f57bf8 415 getrx:
748a9306 416 if (i >= 0) {
13f57bf8
CS
417 bool was_tainted;
418 if (tainting) {
419 was_tainted = tainted;
420 tainted = FALSE;
421 }
79072805 422 sv_setpvn(sv,s,i);
13f57bf8
CS
423 if (tainting)
424 tainted = was_tainted || rx->exec_tainted;
748a9306
LW
425 break;
426 }
79072805 427 }
79072805 428 }
748a9306 429 sv_setsv(sv,&sv_undef);
79072805
LW
430 break;
431 case '+':
bbce6d69
PP
432 if (curpm && (rx = curpm->op_pmregexp)) {
433 paren = rx->lastparen;
a0d0e21e
LW
434 if (paren)
435 goto getparen;
79072805 436 }
748a9306 437 sv_setsv(sv,&sv_undef);
79072805
LW
438 break;
439 case '`':
bbce6d69 440 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 441 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 442 i = rx->startp[0] - s;
13f57bf8 443 goto getrx;
79072805 444 }
79072805 445 }
748a9306 446 sv_setsv(sv,&sv_undef);
79072805
LW
447 break;
448 case '\'':
bbce6d69 449 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
450 if (rx->subend && (s = rx->endp[0])) {
451 i = rx->subend - s;
452 goto getrx;
79072805 453 }
79072805 454 }
748a9306 455 sv_setsv(sv,&sv_undef);
79072805
LW
456 break;
457 case '.':
458#ifndef lint
a0d0e21e 459 if (GvIO(last_in_gv)) {
188ea221 460 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
79072805
LW
461 }
462#endif
463 break;
464 case '?':
91e9c03f 465 sv_setiv(sv, (IV)STATUS_POSIX);
79072805
LW
466 break;
467 case '^':
a0d0e21e 468 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
469 if (s)
470 sv_setpv(sv,s);
471 else {
472 sv_setpv(sv,GvENAME(defoutgv));
473 sv_catpv(sv,"_TOP");
474 }
475 break;
476 case '~':
a0d0e21e 477 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
478 if (!s)
479 s = GvENAME(defoutgv);
480 sv_setpv(sv,s);
481 break;
482#ifndef lint
483 case '=':
188ea221 484 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
485 break;
486 case '-':
188ea221 487 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
488 break;
489 case '%':
188ea221 490 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
79072805
LW
491 break;
492#endif
493 case ':':
494 break;
495 case '/':
496 break;
497 case '[':
188ea221 498 sv_setiv(sv, (IV)curcop->cop_arybase);
79072805
LW
499 break;
500 case '|':
188ea221 501 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
502 break;
503 case ',':
504 sv_setpvn(sv,ofs,ofslen);
505 break;
506 case '\\':
507 sv_setpvn(sv,ors,orslen);
508 break;
509 case '#':
510 sv_setpv(sv,ofmt);
511 break;
512 case '!':
a5f75d66 513#ifdef VMS
946ec16e 514 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 515 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 516#else
88e89b8a
PP
517 {
518 int saveerrno = errno;
946ec16e 519 sv_setnv(sv, (double)errno);
88e89b8a
PP
520#ifdef OS2
521 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
522 else
a5f75d66 523#endif
2304df62 524 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
525 errno = saveerrno;
526 }
527#endif
946ec16e 528 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
529 break;
530 case '<':
188ea221 531 sv_setiv(sv, (IV)uid);
79072805
LW
532 break;
533 case '>':
188ea221 534 sv_setiv(sv, (IV)euid);
79072805
LW
535 break;
536 case '(':
188ea221 537 sv_setiv(sv, (IV)gid);
79072805
LW
538 s = buf;
539 (void)sprintf(s,"%d",(int)gid);
540 goto add_groups;
541 case ')':
188ea221 542 sv_setiv(sv, (IV)egid);
79072805
LW
543 s = buf;
544 (void)sprintf(s,"%d",(int)egid);
545 add_groups:
546 while (*s) s++;
547#ifdef HAS_GETGROUPS
79072805 548 {
a0d0e21e 549 Groups_t gary[NGROUPS];
79072805
LW
550
551 i = getgroups(NGROUPS,gary);
552 while (--i >= 0) {
188ea221 553 (void)sprintf(s," %d", (int)gary[i]);
79072805
LW
554 while (*s) s++;
555 }
556 }
557#endif
558 sv_setpv(sv,buf);
29355cf7 559 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
560 break;
561 case '*':
562 break;
563 case '0':
564 break;
565 }
a0d0e21e 566 return 0;
79072805
LW
567}
568
569int
570magic_getuvar(sv, mg)
571SV *sv;
572MAGIC *mg;
573{
574 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
575
576 if (uf && uf->uf_val)
577 (*uf->uf_val)(uf->uf_index, sv);
578 return 0;
579}
580
581int
582magic_setenv(sv,mg)
583SV* sv;
584MAGIC* mg;
585{
586 register char *s;
88e89b8a 587 char *ptr;
a0d0e21e
LW
588 STRLEN len;
589 I32 i;
590 s = SvPV(sv,len);
188ea221 591 ptr = MgPV(mg);
88e89b8a 592 my_setenv(ptr, s);
a0d0e21e
LW
593#ifdef DYNAMIC_ENV_FETCH
594 /* We just undefd an environment var. Is a replacement */
595 /* waiting in the wings? */
596 if (!len) {
88e89b8a 597 HE *envhe;
188ea221
CS
598 SV *keysv;
599 if (mg->mg_len == HEf_SVKEY) keysv = (SV *)mg->mg_ptr;
600 else keysv = newSVpv(mg->mg_ptr,mg->mg_len);
601 if (envhe = hv_fetch_ent(GvHVn(envgv),keysv,FALSE,0))
88e89b8a 602 s = SvPV(HeVAL(envhe),len);
188ea221 603 if (mg->mg_len != HEf_SVKEY) SvREFCNT_dec(keysv);
a0d0e21e
LW
604 }
605#endif
79072805
LW
606 /* And you'll never guess what the dog had */
607 /* in its mouth... */
463ee0b2 608 if (tainting) {
88e89b8a 609 if (s && strEQ(ptr,"PATH")) {
a0d0e21e 610 char *strend = s + len;
463ee0b2
LW
611
612 while (s < strend) {
613 s = cpytill(tokenbuf,s,strend,':',&i);
614 s++;
615 if (*tokenbuf != '/'
a0d0e21e 616 || (Stat(tokenbuf,&statbuf) && (statbuf.st_mode & 2)) )
8990e307 617 MgTAINTEDDIR_on(mg);
463ee0b2 618 }
79072805
LW
619 }
620 }
79072805
LW
621 return 0;
622}
623
624int
85e6fe83
LW
625magic_clearenv(sv,mg)
626SV* sv;
627MAGIC* mg;
628{
188ea221 629 my_setenv(MgPV(mg),Nullch);
85e6fe83
LW
630 return 0;
631}
632
88e89b8a
PP
633int
634magic_getsig(sv,mg)
635SV* sv;
636MAGIC* mg;
637{
638 I32 i;
639 /* Are we fetching a signal entry? */
188ea221 640 i = whichsig(MgPV(mg));
88e89b8a
PP
641 if (i) {
642 if(psig_ptr[i])
643 sv_setsv(sv,psig_ptr[i]);
644 else {
ff68c719
PP
645 Sighandler_t sigstate = rsignal_state(i);
646
88e89b8a 647 /* cache state so we don't fetch it again */
ff68c719 648 if(sigstate == SIG_IGN)
88e89b8a
PP
649 sv_setpv(sv,"IGNORE");
650 else
651 sv_setsv(sv,&sv_undef);
652 psig_ptr[i] = SvREFCNT_inc(sv);
653 SvTEMP_off(sv);
654 }
655 }
656 return 0;
657}
658int
659magic_clearsig(sv,mg)
660SV* sv;
661MAGIC* mg;
662{
663 I32 i;
664 /* Are we clearing a signal entry? */
188ea221 665 i = whichsig(MgPV(mg));
88e89b8a
PP
666 if (i) {
667 if(psig_ptr[i]) {
668 SvREFCNT_dec(psig_ptr[i]);
669 psig_ptr[i]=0;
670 }
671 if(psig_name[i]) {
672 SvREFCNT_dec(psig_name[i]);
673 psig_name[i]=0;
674 }
675 }
676 return 0;
677}
3d37d572 678
85e6fe83 679int
79072805
LW
680magic_setsig(sv,mg)
681SV* sv;
682MAGIC* mg;
683{
684 register char *s;
685 I32 i;
748a9306 686 SV** svp;
a0d0e21e 687
188ea221 688 s = MgPV(mg);
748a9306
LW
689 if (*s == '_') {
690 if (strEQ(s,"__DIE__"))
691 svp = &diehook;
692 else if (strEQ(s,"__WARN__"))
693 svp = &warnhook;
694 else if (strEQ(s,"__PARSE__"))
695 svp = &parsehook;
696 else
697 croak("No such hook: %s", s);
698 i = 0;
4633a7c4
LW
699 if (*svp) {
700 SvREFCNT_dec(*svp);
701 *svp = 0;
702 }
748a9306
LW
703 }
704 else {
705 i = whichsig(s); /* ...no, a brick */
706 if (!i) {
707 if (dowarn || strEQ(s,"ALARM"))
708 warn("No such signal: SIG%s", s);
709 return 0;
710 }
88e89b8a
PP
711 if(psig_ptr[i])
712 SvREFCNT_dec(psig_ptr[i]);
713 psig_ptr[i] = SvREFCNT_inc(sv);
714 if(psig_name[i])
715 SvREFCNT_dec(psig_name[i]);
8646b087 716 psig_name[i] = newSVpv(s,strlen(s));
88e89b8a
PP
717 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
718 SvREADONLY_on(psig_name[i]);
748a9306 719 }
a0d0e21e 720 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 721 if (i)
ff68c719 722 (void)rsignal(i, sighandler);
748a9306
LW
723 else
724 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
725 return 0;
726 }
727 s = SvPV_force(sv,na);
748a9306
LW
728 if (strEQ(s,"IGNORE")) {
729 if (i)
ff68c719 730 (void)rsignal(i, SIG_IGN);
748a9306
LW
731 else
732 *svp = 0;
733 }
734 else if (strEQ(s,"DEFAULT") || !*s) {
735 if (i)
ff68c719 736 (void)rsignal(i, SIG_DFL);
748a9306
LW
737 else
738 *svp = 0;
739 }
79072805 740 else {
760ac839
LW
741 if(hints & HINT_STRICT_REFS)
742 die(no_symref,s,"a subroutine");
2304df62
AD
743 if (!strchr(s,':') && !strchr(s,'\'')) {
744 sprintf(tokenbuf, "main::%s",s);
79072805
LW
745 sv_setpv(sv,tokenbuf);
746 }
748a9306 747 if (i)
ff68c719 748 (void)rsignal(i, sighandler);
748a9306
LW
749 else
750 *svp = SvREFCNT_inc(sv);
79072805
LW
751 }
752 return 0;
753}
754
755int
463ee0b2 756magic_setisa(sv,mg)
79072805
LW
757SV* sv;
758MAGIC* mg;
759{
463ee0b2
LW
760 sub_generation++;
761 return 0;
762}
763
a0d0e21e
LW
764#ifdef OVERLOAD
765
463ee0b2 766int
a0d0e21e 767magic_setamagic(sv,mg)
463ee0b2
LW
768SV* sv;
769MAGIC* mg;
770{
a0d0e21e
LW
771 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
772 amagic_generation++;
463ee0b2 773
a0d0e21e
LW
774 return 0;
775}
776#endif /* OVERLOAD */
463ee0b2 777
946ec16e
PP
778int
779magic_setnkeys(sv,mg)
780SV* sv;
781MAGIC* mg;
782{
783 if (LvTARG(sv)) {
784 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
785 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
786 }
787 return 0;
788}
789
a0d0e21e
LW
790static int
791magic_methpack(sv,mg,meth)
792SV* sv;
793MAGIC* mg;
794char *meth;
795{
796 dSP;
463ee0b2 797
a0d0e21e
LW
798 ENTER;
799 SAVETMPS;
800 PUSHMARK(sp);
801 EXTEND(sp, 2);
802 PUSHs(mg->mg_obj);
88e89b8a
PP
803 if (mg->mg_ptr) {
804 if (mg->mg_len >= 0)
805 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
806 else if (mg->mg_len == HEf_SVKEY)
807 PUSHs((SV*)mg->mg_ptr);
808 }
a0d0e21e
LW
809 else if (mg->mg_type == 'p')
810 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
811 PUTBACK;
812
a0d0e21e
LW
813 if (perl_call_method(meth, G_SCALAR))
814 sv_setsv(sv, *stack_sp--);
463ee0b2 815
a0d0e21e
LW
816 FREETMPS;
817 LEAVE;
818 return 0;
819}
463ee0b2 820
a0d0e21e
LW
821int
822magic_getpack(sv,mg)
823SV* sv;
824MAGIC* mg;
825{
826 magic_methpack(sv,mg,"FETCH");
827 if (mg->mg_ptr)
828 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
829 return 0;
830}
831
832int
833magic_setpack(sv,mg)
834SV* sv;
835MAGIC* mg;
836{
463ee0b2 837 dSP;
463ee0b2 838
a0d0e21e
LW
839 PUSHMARK(sp);
840 EXTEND(sp, 3);
841 PUSHs(mg->mg_obj);
88e89b8a
PP
842 if (mg->mg_ptr) {
843 if (mg->mg_len >= 0)
844 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
845 else if (mg->mg_len == HEf_SVKEY)
846 PUSHs((SV*)mg->mg_ptr);
847 }
a0d0e21e
LW
848 else if (mg->mg_type == 'p')
849 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
850 PUSHs(sv);
851 PUTBACK;
852
a0d0e21e 853 perl_call_method("STORE", G_SCALAR|G_DISCARD);
463ee0b2
LW
854
855 return 0;
856}
857
858int
859magic_clearpack(sv,mg)
860SV* sv;
861MAGIC* mg;
862{
a0d0e21e
LW
863 return magic_methpack(sv,mg,"DELETE");
864}
463ee0b2 865
a0d0e21e
LW
866int magic_wipepack(sv,mg)
867SV* sv;
868MAGIC* mg;
869{
870 dSP;
463ee0b2 871
a0d0e21e
LW
872 PUSHMARK(sp);
873 XPUSHs(mg->mg_obj);
463ee0b2 874 PUTBACK;
463ee0b2 875
a0d0e21e 876 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
463ee0b2
LW
877
878 return 0;
879}
880
881int
882magic_nextpack(sv,mg,key)
883SV* sv;
884MAGIC* mg;
885SV* key;
886{
463ee0b2 887 dSP;
a0d0e21e 888 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
889
890 ENTER;
a0d0e21e
LW
891 SAVETMPS;
892 PUSHMARK(sp);
893 EXTEND(sp, 2);
894 PUSHs(mg->mg_obj);
463ee0b2
LW
895 if (SvOK(key))
896 PUSHs(key);
897 PUTBACK;
898
a0d0e21e
LW
899 if (perl_call_method(meth, G_SCALAR))
900 sv_setsv(key, *stack_sp--);
463ee0b2 901
a0d0e21e
LW
902 FREETMPS;
903 LEAVE;
79072805
LW
904 return 0;
905}
906
907int
a0d0e21e
LW
908magic_existspack(sv,mg)
909SV* sv;
910MAGIC* mg;
911{
912 return magic_methpack(sv,mg,"EXISTS");
913}
914
915int
79072805
LW
916magic_setdbline(sv,mg)
917SV* sv;
918MAGIC* mg;
919{
920 OP *o;
921 I32 i;
922 GV* gv;
923 SV** svp;
924
925 gv = DBline;
926 i = SvTRUE(sv);
188ea221
CS
927 svp = av_fetch(GvAV(gv),
928 atoi(MgPV(mg)), FALSE);
8990e307 929 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 930 o->op_private = i;
79072805
LW
931 else
932 warn("Can't break at that line\n");
933 return 0;
934}
935
936int
937magic_getarylen(sv,mg)
938SV* sv;
939MAGIC* mg;
940{
a0d0e21e 941 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
942 return 0;
943}
944
945int
946magic_setarylen(sv,mg)
947SV* sv;
948MAGIC* mg;
949{
a0d0e21e
LW
950 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
951 return 0;
952}
953
954int
955magic_getpos(sv,mg)
956SV* sv;
957MAGIC* mg;
958{
959 SV* lsv = LvTARG(sv);
960
961 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
962 mg = mg_find(lsv, 'g');
963 if (mg && mg->mg_len >= 0) {
964 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
965 return 0;
966 }
967 }
968 (void)SvOK_off(sv);
969 return 0;
970}
971
972int
973magic_setpos(sv,mg)
974SV* sv;
975MAGIC* mg;
976{
977 SV* lsv = LvTARG(sv);
978 SSize_t pos;
979 STRLEN len;
980
981 mg = 0;
982
983 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
984 mg = mg_find(lsv, 'g');
985 if (!mg) {
986 if (!SvOK(sv))
987 return 0;
988 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
989 mg = mg_find(lsv, 'g');
990 }
991 else if (!SvOK(sv)) {
992 mg->mg_len = -1;
993 return 0;
994 }
995 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
996
997 pos = SvIV(sv) - curcop->cop_arybase;
998 if (pos < 0) {
999 pos += len;
1000 if (pos < 0)
1001 pos = 0;
1002 }
1003 else if (pos > len)
1004 pos = len;
1005 mg->mg_len = pos;
71be2cbc 1006 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1007
79072805
LW
1008 return 0;
1009}
1010
1011int
1012magic_getglob(sv,mg)
1013SV* sv;
1014MAGIC* mg;
1015{
8646b087
PP
1016 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1017 SvFAKE_off(sv);
946ec16e 1018 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1019 SvFAKE_on(sv);
1020 }
1021 else
946ec16e 1022 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1023 return 0;
1024}
1025
1026int
1027magic_setglob(sv,mg)
1028SV* sv;
1029MAGIC* mg;
1030{
1031 register char *s;
1032 GV* gv;
1033
1034 if (!SvOK(sv))
1035 return 0;
463ee0b2 1036 s = SvPV(sv, na);
79072805
LW
1037 if (*s == '*' && s[1])
1038 s++;
85e6fe83 1039 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1040 if (sv == (SV*)gv)
1041 return 0;
1042 if (GvGP(sv))
88e89b8a 1043 gp_free((GV*)sv);
79072805 1044 GvGP(sv) = gp_ref(GvGP(gv));
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
f86702cc
PP
1236 /* will anyone ever use this? */
1237 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441
PP
1238#endif
1239 break;
79072805 1240 case '\006': /* ^F */
463ee0b2 1241 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1242 break;
a0d0e21e
LW
1243 case '\010': /* ^H */
1244 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1245 break;
79072805
LW
1246 case '\t': /* ^I */
1247 if (inplace)
1248 Safefree(inplace);
1249 if (SvOK(sv))
a0d0e21e 1250 inplace = savepv(SvPV(sv,na));
79072805
LW
1251 else
1252 inplace = Nullch;
1253 break;
28f23441
PP
1254 case '\017': /* ^O */
1255 if (osname)
1256 Safefree(osname);
1257 if (SvOK(sv))
1258 osname = savepv(SvPV(sv,na));
1259 else
1260 osname = Nullch;
1261 break;
79072805 1262 case '\020': /* ^P */
463ee0b2 1263 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1264 if (i != perldb) {
1265 if (perldb)
1266 oldlastpm = curpm;
1267 else
1268 curpm = oldlastpm;
1269 }
1270 perldb = i;
1271 break;
f86702cc
PP
1272 case '\023': /* ^S */
1273 STATUS_NATIVE_SET(SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv));
1274 break;
79072805 1275 case '\024': /* ^T */
88e89b8a
PP
1276#ifdef BIG_TIME
1277 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1278#else
85e6fe83 1279 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1280#endif
79072805
LW
1281 break;
1282 case '\027': /* ^W */
463ee0b2 1283 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1284 break;
1285 case '.':
748a9306
LW
1286 if (localizing) {
1287 if (localizing == 1)
1288 save_sptr((SV**)&last_in_gv);
1289 }
88e89b8a 1290 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1291 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1292 break;
1293 case '^':
a0d0e21e
LW
1294 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1295 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1296 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1297 break;
1298 case '~':
a0d0e21e
LW
1299 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1300 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1301 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1302 break;
1303 case '=':
a0d0e21e 1304 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1305 break;
1306 case '-':
a0d0e21e
LW
1307 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1308 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1309 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1310 break;
1311 case '%':
a0d0e21e 1312 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1313 break;
1314 case '|':
a0d0e21e 1315 IoFLAGS(GvIOp(defoutgv)) &= ~IOf_FLUSH;
463ee0b2 1316 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) != 0) {
a0d0e21e 1317 IoFLAGS(GvIOp(defoutgv)) |= IOf_FLUSH;
79072805
LW
1318 }
1319 break;
1320 case '*':
463ee0b2 1321 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1322 multiline = (i != 0);
1323 break;
1324 case '/':
c07a80fd
PP
1325 SvREFCNT_dec(nrs);
1326 nrs = newSVsv(sv);
1327 SvREFCNT_dec(rs);
1328 rs = SvREFCNT_inc(nrs);
79072805
LW
1329 break;
1330 case '\\':
1331 if (ors)
1332 Safefree(ors);
e3c19b7b
CS
1333 if (SvOK(sv) || SvGMAGICAL(sv))
1334 ors = savepv(SvPV(sv,orslen));
1335 else {
1336 ors = Nullch;
1337 orslen = 0;
1338 }
79072805
LW
1339 break;
1340 case ',':
1341 if (ofs)
1342 Safefree(ofs);
a0d0e21e 1343 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1344 break;
1345 case '#':
1346 if (ofmt)
1347 Safefree(ofmt);
a0d0e21e 1348 ofmt = savepv(SvPV(sv,na));
79072805
LW
1349 break;
1350 case '[':
a0d0e21e 1351 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1352 break;
1353 case '?':
91e9c03f 1354 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1355 break;
1356 case '!':
f86702cc
PP
1357 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1358 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1359 break;
1360 case '<':
463ee0b2 1361 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1362 if (delaymagic) {
1363 delaymagic |= DM_RUID;
1364 break; /* don't do magic till later */
1365 }
1366#ifdef HAS_SETRUID
85e6fe83 1367 (void)setruid((Uid_t)uid);
79072805
LW
1368#else
1369#ifdef HAS_SETREUID
85e6fe83 1370 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1371#else
85e6fe83
LW
1372#ifdef HAS_SETRESUID
1373 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1374#else
1375 if (uid == euid) /* special case $< = $> */
1376 (void)setuid(uid);
a0d0e21e
LW
1377 else {
1378 uid = (I32)getuid();
463ee0b2 1379 croak("setruid() not implemented");
a0d0e21e 1380 }
79072805
LW
1381#endif
1382#endif
85e6fe83 1383#endif
748a9306 1384 uid = (I32)getuid();
4633a7c4 1385 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1386 break;
1387 case '>':
463ee0b2 1388 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1389 if (delaymagic) {
1390 delaymagic |= DM_EUID;
1391 break; /* don't do magic till later */
1392 }
1393#ifdef HAS_SETEUID
85e6fe83 1394 (void)seteuid((Uid_t)euid);
79072805
LW
1395#else
1396#ifdef HAS_SETREUID
85e6fe83
LW
1397 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1398#else
1399#ifdef HAS_SETRESUID
1400 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1401#else
1402 if (euid == uid) /* special case $> = $< */
1403 setuid(euid);
a0d0e21e
LW
1404 else {
1405 euid = (I32)geteuid();
463ee0b2 1406 croak("seteuid() not implemented");
a0d0e21e 1407 }
79072805
LW
1408#endif
1409#endif
85e6fe83 1410#endif
79072805 1411 euid = (I32)geteuid();
4633a7c4 1412 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1413 break;
1414 case '(':
463ee0b2 1415 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1416 if (delaymagic) {
1417 delaymagic |= DM_RGID;
1418 break; /* don't do magic till later */
1419 }
1420#ifdef HAS_SETRGID
85e6fe83 1421 (void)setrgid((Gid_t)gid);
79072805
LW
1422#else
1423#ifdef HAS_SETREGID
85e6fe83
LW
1424 (void)setregid((Gid_t)gid, (Gid_t)-1);
1425#else
1426#ifdef HAS_SETRESGID
1427 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1428#else
1429 if (gid == egid) /* special case $( = $) */
1430 (void)setgid(gid);
748a9306
LW
1431 else {
1432 gid = (I32)getgid();
463ee0b2 1433 croak("setrgid() not implemented");
748a9306 1434 }
79072805
LW
1435#endif
1436#endif
85e6fe83 1437#endif
79072805 1438 gid = (I32)getgid();
4633a7c4 1439 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1440 break;
1441 case ')':
463ee0b2 1442 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1443 if (delaymagic) {
1444 delaymagic |= DM_EGID;
1445 break; /* don't do magic till later */
1446 }
1447#ifdef HAS_SETEGID
85e6fe83 1448 (void)setegid((Gid_t)egid);
79072805
LW
1449#else
1450#ifdef HAS_SETREGID
85e6fe83
LW
1451 (void)setregid((Gid_t)-1, (Gid_t)egid);
1452#else
1453#ifdef HAS_SETRESGID
1454 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1455#else
1456 if (egid == gid) /* special case $) = $( */
1457 (void)setgid(egid);
748a9306
LW
1458 else {
1459 egid = (I32)getegid();
463ee0b2 1460 croak("setegid() not implemented");
748a9306 1461 }
79072805
LW
1462#endif
1463#endif
85e6fe83 1464#endif
79072805 1465 egid = (I32)getegid();
4633a7c4 1466 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1467 break;
1468 case ':':
a0d0e21e 1469 chopset = SvPV_force(sv,na);
79072805
LW
1470 break;
1471 case '0':
1472 if (!origalen) {
1473 s = origargv[0];
1474 s += strlen(s);
1475 /* See if all the arguments are contiguous in memory */
1476 for (i = 1; i < origargc; i++) {
1477 if (origargv[i] == s + 1)
1478 s += strlen(++s); /* this one is ok too */
1479 }
bbce6d69
PP
1480 /* can grab env area too? */
1481 if (origenviron && origenviron[0] == s + 1) {
79072805
LW
1482 my_setenv("NoNeSuCh", Nullch);
1483 /* force copy of environment */
1484 for (i = 0; origenviron[i]; i++)
1485 if (origenviron[i] == s + 1)
1486 s += strlen(++s);
1487 }
1488 origalen = s - origargv[0];
1489 }
a0d0e21e 1490 s = SvPV_force(sv,len);
8990e307 1491 i = len;
79072805
LW
1492 if (i >= origalen) {
1493 i = origalen;
1494 SvCUR_set(sv, i);
1495 *SvEND(sv) = '\0';
1496 Copy(s, origargv[0], i, char);
1497 }
1498 else {
1499 Copy(s, origargv[0], i, char);
1500 s = origargv[0]+i;
1501 *s++ = '\0';
1502 while (++i < origalen)
8990e307
LW
1503 *s++ = ' ';
1504 s = origargv[0]+i;
ed6116ce 1505 for (i = 1; i < origargc; i++)
8990e307 1506 origargv[i] = Nullch;
79072805
LW
1507 }
1508 break;
1509 }
1510 return 0;
1511}
1512
1513I32
1514whichsig(sig)
1515char *sig;
1516{
1517 register char **sigv;
1518
1519 for (sigv = sig_name+1; *sigv; sigv++)
1520 if (strEQ(sig,*sigv))
8e07c86e 1521 return sig_num[sigv - sig_name];
79072805
LW
1522#ifdef SIGCLD
1523 if (strEQ(sig,"CHLD"))
1524 return SIGCLD;
1525#endif
1526#ifdef SIGCHLD
1527 if (strEQ(sig,"CLD"))
1528 return SIGCHLD;
1529#endif
1530 return 0;
1531}
1532
ecfc5424 1533Signal_t
79072805 1534sighandler(sig)
a0d0e21e 1535int sig;
79072805
LW
1536{
1537 dSP;
1538 GV *gv;
a0d0e21e 1539 HV *st;
79072805
LW
1540 SV *sv;
1541 CV *cv;
79072805 1542 AV *oldstack;
760ac839
LW
1543
1544 if(!psig_ptr[sig])
1545 die("Signal SIG%s received, but no signal handler set.\n",
1546 sig_name[sig]);
79072805 1547
88e89b8a 1548 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
a0d0e21e 1549 if (!cv || !CvROOT(cv)) {
79072805
LW
1550 if (dowarn)
1551 warn("SIG%s handler \"%s\" not defined.\n",
88e89b8a 1552 sig_name[sig], GvENAME(gv) );
79072805
LW
1553 return;
1554 }
1555
88e89b8a
PP
1556 oldstack = curstack;
1557 if (curstack != signalstack)
a0d0e21e 1558 AvFILL(signalstack) = 0;
88e89b8a 1559 SWITCHSTACK(curstack, signalstack);
79072805 1560
88e89b8a
PP
1561 if(psig_name[sig])
1562 sv = SvREFCNT_inc(psig_name[sig]);
1563 else {
1564 sv = sv_newmortal();
1565 sv_setpv(sv,sig_name[sig]);
1566 }
a0d0e21e 1567 PUSHMARK(sp);
79072805 1568 PUSHs(sv);
79072805 1569 PUTBACK;
a0d0e21e
LW
1570
1571 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1572
1573 SWITCHSTACK(signalstack, oldstack);
79072805
LW
1574
1575 return;
1576}