This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Updates to perlfunc.pod
[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 '?':
ff0cee69
PP
459 sv_setiv(sv, (IV)STATUS_CURRENT);
460#ifdef COMPLEX_STATUS
461 LvTARGOFF(sv) = statusvalue;
462 LvTARGLEN(sv) = statusvalue_vms;
463#endif
79072805
LW
464 break;
465 case '^':
a0d0e21e 466 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
467 if (s)
468 sv_setpv(sv,s);
469 else {
470 sv_setpv(sv,GvENAME(defoutgv));
471 sv_catpv(sv,"_TOP");
472 }
473 break;
474 case '~':
a0d0e21e 475 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
476 if (!s)
477 s = GvENAME(defoutgv);
478 sv_setpv(sv,s);
479 break;
480#ifndef lint
481 case '=':
188ea221 482 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
483 break;
484 case '-':
188ea221 485 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
486 break;
487 case '%':
188ea221 488 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
79072805
LW
489 break;
490#endif
491 case ':':
492 break;
493 case '/':
494 break;
495 case '[':
188ea221 496 sv_setiv(sv, (IV)curcop->cop_arybase);
79072805
LW
497 break;
498 case '|':
188ea221 499 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
500 break;
501 case ',':
502 sv_setpvn(sv,ofs,ofslen);
503 break;
504 case '\\':
505 sv_setpvn(sv,ors,orslen);
506 break;
507 case '#':
508 sv_setpv(sv,ofmt);
509 break;
510 case '!':
a5f75d66 511#ifdef VMS
946ec16e 512 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 513 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 514#else
88e89b8a
PP
515 {
516 int saveerrno = errno;
946ec16e 517 sv_setnv(sv, (double)errno);
88e89b8a
PP
518#ifdef OS2
519 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
520 else
a5f75d66 521#endif
2304df62 522 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
523 errno = saveerrno;
524 }
525#endif
946ec16e 526 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
527 break;
528 case '<':
188ea221 529 sv_setiv(sv, (IV)uid);
79072805
LW
530 break;
531 case '>':
188ea221 532 sv_setiv(sv, (IV)euid);
79072805
LW
533 break;
534 case '(':
188ea221 535 sv_setiv(sv, (IV)gid);
79072805
LW
536 s = buf;
537 (void)sprintf(s,"%d",(int)gid);
538 goto add_groups;
539 case ')':
188ea221 540 sv_setiv(sv, (IV)egid);
79072805
LW
541 s = buf;
542 (void)sprintf(s,"%d",(int)egid);
543 add_groups:
544 while (*s) s++;
545#ifdef HAS_GETGROUPS
79072805 546 {
a0d0e21e 547 Groups_t gary[NGROUPS];
79072805
LW
548
549 i = getgroups(NGROUPS,gary);
550 while (--i >= 0) {
188ea221 551 (void)sprintf(s," %d", (int)gary[i]);
79072805
LW
552 while (*s) s++;
553 }
554 }
555#endif
556 sv_setpv(sv,buf);
29355cf7 557 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
558 break;
559 case '*':
560 break;
561 case '0':
562 break;
563 }
a0d0e21e 564 return 0;
79072805
LW
565}
566
567int
568magic_getuvar(sv, mg)
569SV *sv;
570MAGIC *mg;
571{
572 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
573
574 if (uf && uf->uf_val)
575 (*uf->uf_val)(uf->uf_index, sv);
576 return 0;
577}
578
579int
580magic_setenv(sv,mg)
581SV* sv;
582MAGIC* mg;
583{
584 register char *s;
88e89b8a 585 char *ptr;
a0d0e21e
LW
586 STRLEN len;
587 I32 i;
588 s = SvPV(sv,len);
188ea221 589 ptr = MgPV(mg);
88e89b8a 590 my_setenv(ptr, s);
a0d0e21e
LW
591#ifdef DYNAMIC_ENV_FETCH
592 /* We just undefd an environment var. Is a replacement */
593 /* waiting in the wings? */
594 if (!len) {
88e89b8a 595 HE *envhe;
188ea221
CS
596 SV *keysv;
597 if (mg->mg_len == HEf_SVKEY) keysv = (SV *)mg->mg_ptr;
598 else keysv = newSVpv(mg->mg_ptr,mg->mg_len);
599 if (envhe = hv_fetch_ent(GvHVn(envgv),keysv,FALSE,0))
88e89b8a 600 s = SvPV(HeVAL(envhe),len);
188ea221 601 if (mg->mg_len != HEf_SVKEY) SvREFCNT_dec(keysv);
a0d0e21e
LW
602 }
603#endif
79072805
LW
604 /* And you'll never guess what the dog had */
605 /* in its mouth... */
463ee0b2 606 if (tainting) {
88e89b8a 607 if (s && strEQ(ptr,"PATH")) {
a0d0e21e 608 char *strend = s + len;
463ee0b2
LW
609
610 while (s < strend) {
611 s = cpytill(tokenbuf,s,strend,':',&i);
612 s++;
613 if (*tokenbuf != '/'
a0d0e21e 614 || (Stat(tokenbuf,&statbuf) && (statbuf.st_mode & 2)) )
8990e307 615 MgTAINTEDDIR_on(mg);
463ee0b2 616 }
79072805
LW
617 }
618 }
79072805
LW
619 return 0;
620}
621
622int
85e6fe83
LW
623magic_clearenv(sv,mg)
624SV* sv;
625MAGIC* mg;
626{
188ea221 627 my_setenv(MgPV(mg),Nullch);
85e6fe83
LW
628 return 0;
629}
630
88e89b8a
PP
631int
632magic_getsig(sv,mg)
633SV* sv;
634MAGIC* mg;
635{
636 I32 i;
637 /* Are we fetching a signal entry? */
188ea221 638 i = whichsig(MgPV(mg));
88e89b8a
PP
639 if (i) {
640 if(psig_ptr[i])
641 sv_setsv(sv,psig_ptr[i]);
642 else {
ff68c719
PP
643 Sighandler_t sigstate = rsignal_state(i);
644
88e89b8a 645 /* cache state so we don't fetch it again */
ff68c719 646 if(sigstate == SIG_IGN)
88e89b8a
PP
647 sv_setpv(sv,"IGNORE");
648 else
649 sv_setsv(sv,&sv_undef);
650 psig_ptr[i] = SvREFCNT_inc(sv);
651 SvTEMP_off(sv);
652 }
653 }
654 return 0;
655}
656int
657magic_clearsig(sv,mg)
658SV* sv;
659MAGIC* mg;
660{
661 I32 i;
662 /* Are we clearing a signal entry? */
188ea221 663 i = whichsig(MgPV(mg));
88e89b8a
PP
664 if (i) {
665 if(psig_ptr[i]) {
666 SvREFCNT_dec(psig_ptr[i]);
667 psig_ptr[i]=0;
668 }
669 if(psig_name[i]) {
670 SvREFCNT_dec(psig_name[i]);
671 psig_name[i]=0;
672 }
673 }
674 return 0;
675}
3d37d572 676
85e6fe83 677int
79072805
LW
678magic_setsig(sv,mg)
679SV* sv;
680MAGIC* mg;
681{
682 register char *s;
683 I32 i;
748a9306 684 SV** svp;
a0d0e21e 685
188ea221 686 s = MgPV(mg);
748a9306
LW
687 if (*s == '_') {
688 if (strEQ(s,"__DIE__"))
689 svp = &diehook;
690 else if (strEQ(s,"__WARN__"))
691 svp = &warnhook;
692 else if (strEQ(s,"__PARSE__"))
693 svp = &parsehook;
694 else
695 croak("No such hook: %s", s);
696 i = 0;
4633a7c4
LW
697 if (*svp) {
698 SvREFCNT_dec(*svp);
699 *svp = 0;
700 }
748a9306
LW
701 }
702 else {
703 i = whichsig(s); /* ...no, a brick */
704 if (!i) {
705 if (dowarn || strEQ(s,"ALARM"))
706 warn("No such signal: SIG%s", s);
707 return 0;
708 }
ff0cee69
PP
709 SvREFCNT_dec(psig_name[i]);
710 SvREFCNT_dec(psig_ptr[i]);
88e89b8a 711 psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 712 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
ff0cee69 713 psig_name[i] = newSVpv(s, strlen(s));
88e89b8a 714 SvREADONLY_on(psig_name[i]);
748a9306 715 }
a0d0e21e 716 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 717 if (i)
ff68c719 718 (void)rsignal(i, sighandler);
748a9306
LW
719 else
720 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
721 return 0;
722 }
723 s = SvPV_force(sv,na);
748a9306
LW
724 if (strEQ(s,"IGNORE")) {
725 if (i)
ff68c719 726 (void)rsignal(i, SIG_IGN);
748a9306
LW
727 else
728 *svp = 0;
729 }
730 else if (strEQ(s,"DEFAULT") || !*s) {
731 if (i)
ff68c719 732 (void)rsignal(i, SIG_DFL);
748a9306
LW
733 else
734 *svp = 0;
735 }
79072805 736 else {
760ac839
LW
737 if(hints & HINT_STRICT_REFS)
738 die(no_symref,s,"a subroutine");
2304df62
AD
739 if (!strchr(s,':') && !strchr(s,'\'')) {
740 sprintf(tokenbuf, "main::%s",s);
79072805
LW
741 sv_setpv(sv,tokenbuf);
742 }
748a9306 743 if (i)
ff68c719 744 (void)rsignal(i, sighandler);
748a9306
LW
745 else
746 *svp = SvREFCNT_inc(sv);
79072805
LW
747 }
748 return 0;
749}
750
751int
463ee0b2 752magic_setisa(sv,mg)
79072805
LW
753SV* sv;
754MAGIC* mg;
755{
463ee0b2
LW
756 sub_generation++;
757 return 0;
758}
759
a0d0e21e
LW
760#ifdef OVERLOAD
761
463ee0b2 762int
a0d0e21e 763magic_setamagic(sv,mg)
463ee0b2
LW
764SV* sv;
765MAGIC* mg;
766{
a0d0e21e
LW
767 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
768 amagic_generation++;
463ee0b2 769
a0d0e21e
LW
770 return 0;
771}
772#endif /* OVERLOAD */
463ee0b2 773
946ec16e
PP
774int
775magic_setnkeys(sv,mg)
776SV* sv;
777MAGIC* mg;
778{
779 if (LvTARG(sv)) {
780 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
781 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
782 }
783 return 0;
784}
785
a0d0e21e
LW
786static int
787magic_methpack(sv,mg,meth)
788SV* sv;
789MAGIC* mg;
790char *meth;
791{
792 dSP;
463ee0b2 793
a0d0e21e
LW
794 ENTER;
795 SAVETMPS;
796 PUSHMARK(sp);
797 EXTEND(sp, 2);
798 PUSHs(mg->mg_obj);
88e89b8a
PP
799 if (mg->mg_ptr) {
800 if (mg->mg_len >= 0)
801 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
802 else if (mg->mg_len == HEf_SVKEY)
803 PUSHs((SV*)mg->mg_ptr);
804 }
a0d0e21e
LW
805 else if (mg->mg_type == 'p')
806 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
807 PUTBACK;
808
a0d0e21e
LW
809 if (perl_call_method(meth, G_SCALAR))
810 sv_setsv(sv, *stack_sp--);
463ee0b2 811
a0d0e21e
LW
812 FREETMPS;
813 LEAVE;
814 return 0;
815}
463ee0b2 816
a0d0e21e
LW
817int
818magic_getpack(sv,mg)
819SV* sv;
820MAGIC* mg;
821{
822 magic_methpack(sv,mg,"FETCH");
823 if (mg->mg_ptr)
824 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
825 return 0;
826}
827
828int
829magic_setpack(sv,mg)
830SV* sv;
831MAGIC* mg;
832{
463ee0b2 833 dSP;
463ee0b2 834
a0d0e21e
LW
835 PUSHMARK(sp);
836 EXTEND(sp, 3);
837 PUSHs(mg->mg_obj);
88e89b8a
PP
838 if (mg->mg_ptr) {
839 if (mg->mg_len >= 0)
840 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
841 else if (mg->mg_len == HEf_SVKEY)
842 PUSHs((SV*)mg->mg_ptr);
843 }
a0d0e21e
LW
844 else if (mg->mg_type == 'p')
845 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
846 PUSHs(sv);
847 PUTBACK;
848
a0d0e21e 849 perl_call_method("STORE", G_SCALAR|G_DISCARD);
463ee0b2
LW
850
851 return 0;
852}
853
854int
855magic_clearpack(sv,mg)
856SV* sv;
857MAGIC* mg;
858{
a0d0e21e
LW
859 return magic_methpack(sv,mg,"DELETE");
860}
463ee0b2 861
a0d0e21e
LW
862int magic_wipepack(sv,mg)
863SV* sv;
864MAGIC* mg;
865{
866 dSP;
463ee0b2 867
a0d0e21e
LW
868 PUSHMARK(sp);
869 XPUSHs(mg->mg_obj);
463ee0b2 870 PUTBACK;
463ee0b2 871
a0d0e21e 872 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
463ee0b2
LW
873
874 return 0;
875}
876
877int
878magic_nextpack(sv,mg,key)
879SV* sv;
880MAGIC* mg;
881SV* key;
882{
463ee0b2 883 dSP;
a0d0e21e 884 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
885
886 ENTER;
a0d0e21e
LW
887 SAVETMPS;
888 PUSHMARK(sp);
889 EXTEND(sp, 2);
890 PUSHs(mg->mg_obj);
463ee0b2
LW
891 if (SvOK(key))
892 PUSHs(key);
893 PUTBACK;
894
a0d0e21e
LW
895 if (perl_call_method(meth, G_SCALAR))
896 sv_setsv(key, *stack_sp--);
463ee0b2 897
a0d0e21e
LW
898 FREETMPS;
899 LEAVE;
79072805
LW
900 return 0;
901}
902
903int
a0d0e21e
LW
904magic_existspack(sv,mg)
905SV* sv;
906MAGIC* mg;
907{
908 return magic_methpack(sv,mg,"EXISTS");
909}
910
911int
79072805
LW
912magic_setdbline(sv,mg)
913SV* sv;
914MAGIC* mg;
915{
916 OP *o;
917 I32 i;
918 GV* gv;
919 SV** svp;
920
921 gv = DBline;
922 i = SvTRUE(sv);
188ea221
CS
923 svp = av_fetch(GvAV(gv),
924 atoi(MgPV(mg)), FALSE);
8990e307 925 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 926 o->op_private = i;
79072805
LW
927 else
928 warn("Can't break at that line\n");
929 return 0;
930}
931
932int
933magic_getarylen(sv,mg)
934SV* sv;
935MAGIC* mg;
936{
a0d0e21e 937 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
938 return 0;
939}
940
941int
942magic_setarylen(sv,mg)
943SV* sv;
944MAGIC* mg;
945{
a0d0e21e
LW
946 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
947 return 0;
948}
949
950int
951magic_getpos(sv,mg)
952SV* sv;
953MAGIC* mg;
954{
955 SV* lsv = LvTARG(sv);
956
957 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
958 mg = mg_find(lsv, 'g');
959 if (mg && mg->mg_len >= 0) {
960 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
961 return 0;
962 }
963 }
964 (void)SvOK_off(sv);
965 return 0;
966}
967
968int
969magic_setpos(sv,mg)
970SV* sv;
971MAGIC* mg;
972{
973 SV* lsv = LvTARG(sv);
974 SSize_t pos;
975 STRLEN len;
976
977 mg = 0;
978
979 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
980 mg = mg_find(lsv, 'g');
981 if (!mg) {
982 if (!SvOK(sv))
983 return 0;
984 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
985 mg = mg_find(lsv, 'g');
986 }
987 else if (!SvOK(sv)) {
988 mg->mg_len = -1;
989 return 0;
990 }
991 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
992
993 pos = SvIV(sv) - curcop->cop_arybase;
994 if (pos < 0) {
995 pos += len;
996 if (pos < 0)
997 pos = 0;
998 }
999 else if (pos > len)
1000 pos = len;
1001 mg->mg_len = pos;
71be2cbc 1002 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1003
79072805
LW
1004 return 0;
1005}
1006
1007int
1008magic_getglob(sv,mg)
1009SV* sv;
1010MAGIC* mg;
1011{
8646b087
PP
1012 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1013 SvFAKE_off(sv);
946ec16e 1014 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1015 SvFAKE_on(sv);
1016 }
1017 else
946ec16e 1018 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1019 return 0;
1020}
1021
1022int
1023magic_setglob(sv,mg)
1024SV* sv;
1025MAGIC* mg;
1026{
1027 register char *s;
1028 GV* gv;
1029
1030 if (!SvOK(sv))
1031 return 0;
463ee0b2 1032 s = SvPV(sv, na);
79072805
LW
1033 if (*s == '*' && s[1])
1034 s++;
85e6fe83 1035 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1036 if (sv == (SV*)gv)
1037 return 0;
1038 if (GvGP(sv))
88e89b8a 1039 gp_free((GV*)sv);
79072805 1040 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1041 return 0;
1042}
1043
1044int
1045magic_setsubstr(sv,mg)
1046SV* sv;
1047MAGIC* mg;
1048{
8990e307
LW
1049 STRLEN len;
1050 char *tmps = SvPV(sv,len);
1051 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1052 return 0;
1053}
1054
1055int
463ee0b2
LW
1056magic_gettaint(sv,mg)
1057SV* sv;
1058MAGIC* mg;
1059{
bbce6d69
PP
1060 TAINT_IF((mg->mg_len & 1) ||
1061 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1062 return 0;
1063}
1064
1065int
1066magic_settaint(sv,mg)
1067SV* sv;
1068MAGIC* mg;
1069{
748a9306
LW
1070 if (localizing) {
1071 if (localizing == 1)
1072 mg->mg_len <<= 1;
1073 else
1074 mg->mg_len >>= 1;
a0d0e21e 1075 }
748a9306
LW
1076 else if (tainted)
1077 mg->mg_len |= 1;
1078 else
1079 mg->mg_len &= ~1;
463ee0b2
LW
1080 return 0;
1081}
1082
1083int
79072805
LW
1084magic_setvec(sv,mg)
1085SV* sv;
1086MAGIC* mg;
1087{
1088 do_vecset(sv); /* XXX slurp this routine */
1089 return 0;
1090}
1091
1092int
71be2cbc 1093magic_getitervar(sv,mg)
5f05dabc
PP
1094SV* sv;
1095MAGIC* mg;
1096{
71be2cbc 1097 SV *targ = Nullsv;
5f05dabc
PP
1098 if (LvTARGLEN(sv)) {
1099 AV* av = (AV*)LvTARG(sv);
71be2cbc
PP
1100 if (LvTARGOFF(sv) <= AvFILL(av))
1101 targ = AvARRAY(av)[LvTARGOFF(sv)];
5f05dabc 1102 }
71be2cbc
PP
1103 else
1104 targ = LvTARG(sv);
1105 sv_setsv(sv, targ ? targ : &sv_undef);
1106 return 0;
1107}
1108
1109int
1110magic_setitervar(sv,mg)
1111SV* sv;
1112MAGIC* mg;
1113{
1114 if (LvTARGLEN(sv))
1115 vivify_itervar(sv);
5f05dabc
PP
1116 if (LvTARG(sv))
1117 sv_setsv(LvTARG(sv), sv);
1118 return 0;
1119}
1120
1121int
71be2cbc 1122magic_freeitervar(sv,mg)
5f05dabc
PP
1123SV* sv;
1124MAGIC* mg;
1125{
1126 SvREFCNT_dec(LvTARG(sv));
71be2cbc
PP
1127 return 0;
1128}
1129
1130void
1131vivify_itervar(sv)
1132SV* sv;
1133{
1134 AV* av;
1135
1136 if (!LvTARGLEN(sv))
1137 return;
1138 av = (AV*)LvTARG(sv);
1139 if (LvTARGOFF(sv) <= AvFILL(av)) {
1140 SV** svp = AvARRAY(av) + LvTARGOFF(sv);
1141 LvTARG(sv) = newSVsv(*svp);
1142 SvREFCNT_dec(*svp);
1143 *svp = SvREFCNT_inc(LvTARG(sv));
1144 }
1145 else
1146 LvTARG(sv) = Nullsv;
1147 SvREFCNT_dec(av);
1148 LvTARGLEN(sv) = 0;
5f05dabc
PP
1149}
1150
1151int
93a17b20
LW
1152magic_setmglob(sv,mg)
1153SV* sv;
1154MAGIC* mg;
1155{
a0d0e21e 1156 mg->mg_len = -1;
c6496cc7 1157 SvSCREAM_off(sv);
93a17b20
LW
1158 return 0;
1159}
1160
1161int
79072805
LW
1162magic_setbm(sv,mg)
1163SV* sv;
1164MAGIC* mg;
1165{
463ee0b2 1166 sv_unmagic(sv, 'B');
79072805
LW
1167 SvVALID_off(sv);
1168 return 0;
1169}
1170
1171int
55497cff
PP
1172magic_setfm(sv,mg)
1173SV* sv;
1174MAGIC* mg;
1175{
1176 sv_unmagic(sv, 'f');
1177 SvCOMPILED_off(sv);
1178 return 0;
1179}
1180
1181int
79072805
LW
1182magic_setuvar(sv,mg)
1183SV* sv;
1184MAGIC* mg;
1185{
1186 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1187
1188 if (uf && uf->uf_set)
1189 (*uf->uf_set)(uf->uf_index, sv);
1190 return 0;
1191}
1192
7a4c00b4 1193#ifdef USE_LOCALE_COLLATE
79072805 1194int
bbce6d69
PP
1195magic_setcollxfrm(sv,mg)
1196SV* sv;
1197MAGIC* mg;
1198{
1199 /*
1200 * René Descartes said "I think not."
1201 * and vanished with a faint plop.
1202 */
7a4c00b4
PP
1203 if (mg->mg_ptr) {
1204 Safefree(mg->mg_ptr);
1205 mg->mg_ptr = NULL;
1206 mg->mg_len = -1;
1207 }
bbce6d69
PP
1208 return 0;
1209}
7a4c00b4 1210#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1211
1212int
79072805
LW
1213magic_set(sv,mg)
1214SV* sv;
1215MAGIC* mg;
1216{
1217 register char *s;
1218 I32 i;
8990e307 1219 STRLEN len;
79072805 1220 switch (*mg->mg_ptr) {
748a9306
LW
1221 case '\001': /* ^A */
1222 sv_setsv(bodytarget, sv);
1223 break;
79072805 1224 case '\004': /* ^D */
8990e307 1225 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1226 DEBUG_x(dump_all());
1227 break;
28f23441
PP
1228 case '\005': /* ^E */
1229#ifdef VMS
1230 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1231#else
f86702cc
PP
1232 /* will anyone ever use this? */
1233 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441
PP
1234#endif
1235 break;
79072805 1236 case '\006': /* ^F */
463ee0b2 1237 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1238 break;
a0d0e21e
LW
1239 case '\010': /* ^H */
1240 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1241 break;
79072805
LW
1242 case '\t': /* ^I */
1243 if (inplace)
1244 Safefree(inplace);
1245 if (SvOK(sv))
a0d0e21e 1246 inplace = savepv(SvPV(sv,na));
79072805
LW
1247 else
1248 inplace = Nullch;
1249 break;
28f23441
PP
1250 case '\017': /* ^O */
1251 if (osname)
1252 Safefree(osname);
1253 if (SvOK(sv))
1254 osname = savepv(SvPV(sv,na));
1255 else
1256 osname = Nullch;
1257 break;
79072805 1258 case '\020': /* ^P */
463ee0b2 1259 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1260 if (i != perldb) {
1261 if (perldb)
1262 oldlastpm = curpm;
1263 else
1264 curpm = oldlastpm;
1265 }
1266 perldb = i;
1267 break;
1268 case '\024': /* ^T */
88e89b8a
PP
1269#ifdef BIG_TIME
1270 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1271#else
85e6fe83 1272 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1273#endif
79072805
LW
1274 break;
1275 case '\027': /* ^W */
463ee0b2 1276 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1277 break;
1278 case '.':
748a9306
LW
1279 if (localizing) {
1280 if (localizing == 1)
1281 save_sptr((SV**)&last_in_gv);
1282 }
88e89b8a 1283 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1284 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1285 break;
1286 case '^':
a0d0e21e
LW
1287 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1288 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1289 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1290 break;
1291 case '~':
a0d0e21e
LW
1292 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1293 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1294 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1295 break;
1296 case '=':
a0d0e21e 1297 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1298 break;
1299 case '-':
a0d0e21e
LW
1300 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1301 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1302 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1303 break;
1304 case '%':
a0d0e21e 1305 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1306 break;
1307 case '|':
a0d0e21e 1308 IoFLAGS(GvIOp(defoutgv)) &= ~IOf_FLUSH;
463ee0b2 1309 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) != 0) {
a0d0e21e 1310 IoFLAGS(GvIOp(defoutgv)) |= IOf_FLUSH;
79072805
LW
1311 }
1312 break;
1313 case '*':
463ee0b2 1314 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1315 multiline = (i != 0);
1316 break;
1317 case '/':
c07a80fd
PP
1318 SvREFCNT_dec(nrs);
1319 nrs = newSVsv(sv);
1320 SvREFCNT_dec(rs);
1321 rs = SvREFCNT_inc(nrs);
79072805
LW
1322 break;
1323 case '\\':
1324 if (ors)
1325 Safefree(ors);
e3c19b7b
CS
1326 if (SvOK(sv) || SvGMAGICAL(sv))
1327 ors = savepv(SvPV(sv,orslen));
1328 else {
1329 ors = Nullch;
1330 orslen = 0;
1331 }
79072805
LW
1332 break;
1333 case ',':
1334 if (ofs)
1335 Safefree(ofs);
a0d0e21e 1336 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1337 break;
1338 case '#':
1339 if (ofmt)
1340 Safefree(ofmt);
a0d0e21e 1341 ofmt = savepv(SvPV(sv,na));
79072805
LW
1342 break;
1343 case '[':
a0d0e21e 1344 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1345 break;
1346 case '?':
ff0cee69
PP
1347#ifdef COMPLEX_STATUS
1348 if (localizing == 2) {
1349 statusvalue = LvTARGOFF(sv);
1350 statusvalue_vms = LvTARGLEN(sv);
1351 }
1352 else
1353#endif
1354#ifdef VMSISH_STATUS
1355 if (VMSISH_STATUS)
1356 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1357 else
1358#endif
1359 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1360 break;
1361 case '!':
f86702cc
PP
1362 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1363 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1364 break;
1365 case '<':
463ee0b2 1366 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1367 if (delaymagic) {
1368 delaymagic |= DM_RUID;
1369 break; /* don't do magic till later */
1370 }
1371#ifdef HAS_SETRUID
85e6fe83 1372 (void)setruid((Uid_t)uid);
79072805
LW
1373#else
1374#ifdef HAS_SETREUID
85e6fe83 1375 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1376#else
85e6fe83
LW
1377#ifdef HAS_SETRESUID
1378 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1379#else
1380 if (uid == euid) /* special case $< = $> */
1381 (void)setuid(uid);
a0d0e21e
LW
1382 else {
1383 uid = (I32)getuid();
463ee0b2 1384 croak("setruid() not implemented");
a0d0e21e 1385 }
79072805
LW
1386#endif
1387#endif
85e6fe83 1388#endif
748a9306 1389 uid = (I32)getuid();
4633a7c4 1390 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1391 break;
1392 case '>':
463ee0b2 1393 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1394 if (delaymagic) {
1395 delaymagic |= DM_EUID;
1396 break; /* don't do magic till later */
1397 }
1398#ifdef HAS_SETEUID
85e6fe83 1399 (void)seteuid((Uid_t)euid);
79072805
LW
1400#else
1401#ifdef HAS_SETREUID
85e6fe83
LW
1402 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1403#else
1404#ifdef HAS_SETRESUID
1405 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1406#else
1407 if (euid == uid) /* special case $> = $< */
1408 setuid(euid);
a0d0e21e
LW
1409 else {
1410 euid = (I32)geteuid();
463ee0b2 1411 croak("seteuid() not implemented");
a0d0e21e 1412 }
79072805
LW
1413#endif
1414#endif
85e6fe83 1415#endif
79072805 1416 euid = (I32)geteuid();
4633a7c4 1417 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1418 break;
1419 case '(':
463ee0b2 1420 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1421 if (delaymagic) {
1422 delaymagic |= DM_RGID;
1423 break; /* don't do magic till later */
1424 }
1425#ifdef HAS_SETRGID
85e6fe83 1426 (void)setrgid((Gid_t)gid);
79072805
LW
1427#else
1428#ifdef HAS_SETREGID
85e6fe83
LW
1429 (void)setregid((Gid_t)gid, (Gid_t)-1);
1430#else
1431#ifdef HAS_SETRESGID
1432 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1433#else
1434 if (gid == egid) /* special case $( = $) */
1435 (void)setgid(gid);
748a9306
LW
1436 else {
1437 gid = (I32)getgid();
463ee0b2 1438 croak("setrgid() not implemented");
748a9306 1439 }
79072805
LW
1440#endif
1441#endif
85e6fe83 1442#endif
79072805 1443 gid = (I32)getgid();
4633a7c4 1444 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1445 break;
1446 case ')':
463ee0b2 1447 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1448 if (delaymagic) {
1449 delaymagic |= DM_EGID;
1450 break; /* don't do magic till later */
1451 }
1452#ifdef HAS_SETEGID
85e6fe83 1453 (void)setegid((Gid_t)egid);
79072805
LW
1454#else
1455#ifdef HAS_SETREGID
85e6fe83
LW
1456 (void)setregid((Gid_t)-1, (Gid_t)egid);
1457#else
1458#ifdef HAS_SETRESGID
1459 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1460#else
1461 if (egid == gid) /* special case $) = $( */
1462 (void)setgid(egid);
748a9306
LW
1463 else {
1464 egid = (I32)getegid();
463ee0b2 1465 croak("setegid() not implemented");
748a9306 1466 }
79072805
LW
1467#endif
1468#endif
85e6fe83 1469#endif
79072805 1470 egid = (I32)getegid();
4633a7c4 1471 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1472 break;
1473 case ':':
a0d0e21e 1474 chopset = SvPV_force(sv,na);
79072805
LW
1475 break;
1476 case '0':
1477 if (!origalen) {
1478 s = origargv[0];
1479 s += strlen(s);
1480 /* See if all the arguments are contiguous in memory */
1481 for (i = 1; i < origargc; i++) {
1482 if (origargv[i] == s + 1)
1483 s += strlen(++s); /* this one is ok too */
1484 }
bbce6d69
PP
1485 /* can grab env area too? */
1486 if (origenviron && origenviron[0] == s + 1) {
79072805
LW
1487 my_setenv("NoNeSuCh", Nullch);
1488 /* force copy of environment */
1489 for (i = 0; origenviron[i]; i++)
1490 if (origenviron[i] == s + 1)
1491 s += strlen(++s);
1492 }
1493 origalen = s - origargv[0];
1494 }
a0d0e21e 1495 s = SvPV_force(sv,len);
8990e307 1496 i = len;
79072805
LW
1497 if (i >= origalen) {
1498 i = origalen;
1499 SvCUR_set(sv, i);
1500 *SvEND(sv) = '\0';
1501 Copy(s, origargv[0], i, char);
1502 }
1503 else {
1504 Copy(s, origargv[0], i, char);
1505 s = origargv[0]+i;
1506 *s++ = '\0';
1507 while (++i < origalen)
8990e307
LW
1508 *s++ = ' ';
1509 s = origargv[0]+i;
ed6116ce 1510 for (i = 1; i < origargc; i++)
8990e307 1511 origargv[i] = Nullch;
79072805
LW
1512 }
1513 break;
1514 }
1515 return 0;
1516}
1517
1518I32
1519whichsig(sig)
1520char *sig;
1521{
1522 register char **sigv;
1523
1524 for (sigv = sig_name+1; *sigv; sigv++)
1525 if (strEQ(sig,*sigv))
8e07c86e 1526 return sig_num[sigv - sig_name];
79072805
LW
1527#ifdef SIGCLD
1528 if (strEQ(sig,"CHLD"))
1529 return SIGCLD;
1530#endif
1531#ifdef SIGCHLD
1532 if (strEQ(sig,"CLD"))
1533 return SIGCHLD;
1534#endif
1535 return 0;
1536}
1537
ecfc5424 1538Signal_t
79072805 1539sighandler(sig)
a0d0e21e 1540int sig;
79072805
LW
1541{
1542 dSP;
1543 GV *gv;
a0d0e21e 1544 HV *st;
79072805
LW
1545 SV *sv;
1546 CV *cv;
79072805 1547 AV *oldstack;
ff0cee69
PP
1548
1549 if (!psig_ptr[sig])
1550 die("Signal SIG%s received, but no signal handler set.\n",
1551 sig_name[sig]);
1552
88e89b8a 1553 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
a0d0e21e 1554 if (!cv || !CvROOT(cv)) {
79072805
LW
1555 if (dowarn)
1556 warn("SIG%s handler \"%s\" not defined.\n",
88e89b8a 1557 sig_name[sig], GvENAME(gv) );
79072805
LW
1558 return;
1559 }
1560
88e89b8a
PP
1561 oldstack = curstack;
1562 if (curstack != signalstack)
a0d0e21e 1563 AvFILL(signalstack) = 0;
88e89b8a 1564 SWITCHSTACK(curstack, signalstack);
79072805 1565
88e89b8a
PP
1566 if(psig_name[sig])
1567 sv = SvREFCNT_inc(psig_name[sig]);
1568 else {
ff0cee69
PP
1569 sv = sv_newmortal();
1570 sv_setpv(sv,sig_name[sig]);
88e89b8a 1571 }
a0d0e21e 1572 PUSHMARK(sp);
79072805 1573 PUSHs(sv);
79072805 1574 PUTBACK;
a0d0e21e
LW
1575
1576 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1577
1578 SWITCHSTACK(signalstack, oldstack);
4bf61a0f 1579
79072805
LW
1580 return;
1581}