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