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