summaryrefslogtreecommitdiff
path: root/mg.c
diff options
context:
space:
mode:
authorCharles Bailey <bailey@newman.upenn.edu>2001-01-08 08:53:52 +0000
committerbailey <bailey@newman.upenn.edu>2001-01-08 08:53:52 +0000
commit0e06870bf080a38cda51c06c6612359afc2334e1 (patch)
tree763f11122a3b18bc443e808010b970428ab57432 /mg.c
parente3830a4ec012ee625f1b3bc63b5b18c656f377da (diff)
downloadperl-0e06870bf080a38cda51c06c6612359afc2334e1.tar.gz
Once again syncing after too long an absence
p4raw-id: //depot/vmsperl@8367
Diffstat (limited to 'mg.c')
-rw-r--r--mg.c176
1 files changed, 84 insertions, 92 deletions
diff --git a/mg.c b/mg.c
index bec0a82f72..99600a4fe7 100644
--- a/mg.c
+++ b/mg.c
@@ -1,6 +1,6 @@
/* mg.c
*
- * Copyright (c) 1991-2000, Larry Wall
+ * Copyright (c) 1991-2001, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
@@ -16,11 +16,6 @@
#define PERL_IN_MG_C
#include "perl.h"
-/* XXX If this causes problems, set i_unistd=undef in the hint file. */
-#ifdef I_UNISTD
-# include <unistd.h>
-#endif
-
#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
# ifndef NGROUPS
# define NGROUPS 32
@@ -44,7 +39,6 @@ struct magic_state {
STATIC void
S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
{
- dTHR;
MGS* mgs;
assert(SvMAGICAL(sv));
@@ -96,7 +90,6 @@ Do magic after a value is retrieved from the SV. See C<sv_magic>.
int
Perl_mg_get(pTHX_ SV *sv)
{
- dTHR;
I32 mgs_ix;
MAGIC* mg;
MAGIC** mgp;
@@ -139,7 +132,6 @@ Do magic after a value is assigned to the SV. See C<sv_magic>.
int
Perl_mg_set(pTHX_ SV *sv)
{
- dTHR;
I32 mgs_ix;
MAGIC* mg;
MAGIC* nextmg;
@@ -200,7 +192,7 @@ Perl_mg_size(pTHX_ SV *sv)
{
MAGIC* mg;
I32 len;
-
+
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
MGVTBL* vtbl = mg->mg_virtual;
if (vtbl && vtbl->svt_len) {
@@ -269,6 +261,8 @@ MAGIC*
Perl_mg_find(pTHX_ SV *sv, int type)
{
MAGIC* mg;
+ if (!sv)
+ return 0;
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
if (mg->mg_type == type)
return mg;
@@ -339,7 +333,6 @@ Perl_mg_free(pTHX_ SV *sv)
U32
Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register REGEXP *rx;
if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
@@ -348,14 +341,13 @@ Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
else /* @- */
return rx->lastparen;
}
-
+
return (U32)-1;
}
int
Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register I32 paren;
register I32 s;
register I32 i;
@@ -374,6 +366,11 @@ Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
i = t;
else /* @- */
i = s;
+
+ if (i > 0 && DO_UTF8(PL_reg_sv)) {
+ char *b = rx->subbeg;
+ i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
+ }
sv_setiv(sv,i);
}
}
@@ -383,7 +380,6 @@ Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
Perl_croak(aTHX_ PL_no_modify);
/* NOT REACHED */
return 0;
@@ -392,7 +388,6 @@ Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
U32
Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register I32 paren;
register I32 i;
register REGEXP *rx;
@@ -403,7 +398,7 @@ Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
case '5': case '6': case '7': case '8': case '9': case '&':
if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
- paren = atoi(mg->mg_ptr);
+ paren = atoi(mg->mg_ptr); /* $& is in [0] */
getparen:
if (paren <= rx->nparens &&
(s1 = rx->startp[paren]) != -1 &&
@@ -411,17 +406,15 @@ Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
{
i = t1 - s1;
getlen:
- if (i > 0 && (PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
- char *s = rx->subbeg + s1;
+ if (i > 0 && DO_UTF8(PL_reg_sv)) {
+ char *s = rx->subbeg + s1;
char *send = rx->subbeg + t1;
- i = 0;
- while (s < send) {
- s += UTF8SKIP(s);
- i++;
- }
+
+ i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
}
- if (i >= 0)
- return i;
+ if (i < 0)
+ Perl_croak(aTHX_ "panic: magic_len: %d", i);
+ return i;
}
}
return 0;
@@ -456,10 +449,6 @@ Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
}
}
return 0;
- case ',':
- return (STRLEN)PL_ofslen;
- case '\\':
- return (STRLEN)PL_orslen;
}
magic_get(sv,mg);
if (!SvPOK(sv) && SvNIOK(sv)) {
@@ -474,7 +463,6 @@ Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register I32 paren;
register char *s;
register I32 i;
@@ -498,7 +486,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
#ifdef MACOS_TRADITIONAL
{
char msg[256];
-
+
sv_setnv(sv,(double)gMacPerl_OSErr);
sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
}
@@ -563,15 +551,22 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
else
sv_setsv(sv, &PL_sv_undef);
break;
- case '\017': /* ^O */
- sv_setpv(sv, PL_osname);
+ case '\017': /* ^O & ^OPEN */
+ if (*(mg->mg_ptr+1) == '\0')
+ sv_setpv(sv, PL_osname);
+ else if (strEQ(mg->mg_ptr, "\017PEN")) {
+ if (!PL_compiling.cop_io)
+ sv_setsv(sv, &PL_sv_undef);
+ else {
+ sv_setsv(sv, PL_compiling.cop_io);
+ }
+ }
break;
case '\020': /* ^P */
sv_setiv(sv, (IV)PL_perldb);
break;
case '\023': /* ^S */
{
- dTHR;
if (PL_lex_state != LEX_NOTPARSING)
(void)SvOK_off(sv);
else if (PL_in_eval)
@@ -596,10 +591,10 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
}
else if (PL_compiling.cop_warnings == pWARN_ALL) {
sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
- }
+ }
else {
sv_setsv(sv, PL_compiling.cop_warnings);
- }
+ }
SvPOK_only(sv);
}
else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
@@ -614,7 +609,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
* Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
* XXX Does the new way break anything?
*/
- paren = atoi(mg->mg_ptr);
+ paren = atoi(mg->mg_ptr); /* $& is in [0] */
getparen:
if (paren <= rx->nparens &&
(s1 = rx->startp[paren]) != -1 &&
@@ -633,7 +628,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
PL_tainted = FALSE;
}
sv_setpvn(sv, s, i);
- if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
+ if (DO_UTF8(PL_reg_sv))
SvUTF8_on(sv);
else
SvUTF8_off(sv);
@@ -725,10 +720,8 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
break;
case ',':
- sv_setpvn(sv,PL_ofs,PL_ofslen);
break;
case '\\':
- sv_setpvn(sv,PL_ors,PL_orslen);
break;
case '#':
sv_setpv(sv,PL_ofmt);
@@ -895,7 +888,6 @@ Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
#if defined(VMS)
Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
#else
- dTHR;
if (PL_localizing) {
HE* entry;
STRLEN n_a;
@@ -936,12 +928,8 @@ Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
cur += len+1;
}
FreeEnvironmentStrings(envv);
-# else
-# ifdef __CYGWIN__
- I32 i;
- for (i = 0; environ[i]; i++)
- safesysfree(environ[i]);
# else
+#ifdef USE_ENVIRON_ARRAY
# ifndef PERL_USE_SAFE_PUTENV
I32 i;
@@ -951,10 +939,10 @@ Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
for (i = 0; environ[i]; i++)
safesysfree(environ[i]);
# endif /* PERL_USE_SAFE_PUTENV */
-# endif /* __CYGWIN__ */
environ[0] = Nullch;
+#endif /* USE_ENVIRON_ARRAY */
# endif /* WIN32 */
# endif /* PERL_IMPLICIT_SYS */
#endif /* VMS */
@@ -1009,7 +997,6 @@ Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register char *s;
I32 i;
SV** svp;
@@ -1126,7 +1113,7 @@ Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
}
return 0;
-}
+}
/* caller is responsible for stack switching/cleanup */
STATIC int
@@ -1137,7 +1124,7 @@ S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
PUSHMARK(SP);
EXTEND(SP, n);
PUSHs(SvTIED_obj(sv, mg));
- if (n > 1) {
+ if (n > 1) {
if (mg->mg_ptr) {
if (mg->mg_len >= 0)
PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
@@ -1205,7 +1192,7 @@ Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
U32
Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
-{
+{
dSP;
U32 retval = 0;
@@ -1267,12 +1254,11 @@ int
Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
{
return magic_methpack(sv,mg,"EXISTS");
-}
+}
int
Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
OP *o;
I32 i;
GV* gv;
@@ -1291,7 +1277,6 @@ Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
return 0;
}
@@ -1299,7 +1284,6 @@ Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
return 0;
}
@@ -1308,11 +1292,10 @@ int
Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
{
SV* lsv = LvTARG(sv);
-
+
if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
mg = mg_find(lsv, 'g');
if (mg && mg->mg_len >= 0) {
- dTHR;
I32 i = mg->mg_len;
if (DO_UTF8(lsv))
sv_pos_b2u(lsv, &i);
@@ -1331,10 +1314,9 @@ Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
SSize_t pos;
STRLEN len;
STRLEN ulen = 0;
- dTHR;
mg = 0;
-
+
if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
mg = mg_find(lsv, 'g');
if (!mg) {
@@ -1435,14 +1417,20 @@ Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
{
STRLEN len;
char *tmps = SvPV(sv,len);
- sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
+ if (DO_UTF8(sv)) {
+ sv_utf8_upgrade(LvTARG(sv));
+ sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
+ SvUTF8_on(LvTARG(sv));
+ }
+ else
+ sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
+
return 0;
}
int
Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
TAINT_IF((mg->mg_len & 1) ||
((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
return 0;
@@ -1451,7 +1439,6 @@ Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
if (PL_localizing) {
if (PL_localizing == 1)
mg->mg_len <<= 1;
@@ -1510,7 +1497,6 @@ Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
targ = AvARRAY(av)[LvTARGOFF(sv)];
}
if (targ && targ != &PL_sv_undef) {
- dTHR; /* just for SvREFCNT_dec */
/* somebody else defined it for us */
SvREFCNT_dec(LvTARG(sv));
LvTARG(sv) = SvREFCNT_inc(targ);
@@ -1541,7 +1527,6 @@ Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
void
Perl_vivify_defelem(pTHX_ SV *sv)
{
- dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
MAGIC *mg;
SV *value = Nullsv;
@@ -1665,7 +1650,6 @@ Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
register char *s;
I32 i;
STRLEN len;
@@ -1714,12 +1698,20 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
PL_inplace = Nullch;
break;
case '\017': /* ^O */
- if (PL_osname)
- Safefree(PL_osname);
- if (SvOK(sv))
- PL_osname = savepv(SvPV(sv,len));
- else
- PL_osname = Nullch;
+ if (*(mg->mg_ptr+1) == '\0') {
+ if (PL_osname)
+ Safefree(PL_osname);
+ if (SvOK(sv))
+ PL_osname = savepv(SvPV(sv,len));
+ else
+ PL_osname = Nullch;
+ }
+ else if (strEQ(mg->mg_ptr, "\017PEN")) {
+ if (!PL_compiling.cop_io)
+ PL_compiling.cop_io = newSVsv(sv);
+ else
+ sv_setsv(PL_compiling.cop_io,sv);
+ }
break;
case '\020': /* ^P */
PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
@@ -1737,7 +1729,7 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
if (*(mg->mg_ptr+1) == '\0') {
if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
- PL_dowarn = (PL_dowarn & ~G_WARN_ON)
+ PL_dowarn = (PL_dowarn & ~G_WARN_ON)
| (i ? G_WARN_ON : G_WARN_OFF) ;
}
}
@@ -1833,21 +1825,24 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
PL_rs = SvREFCNT_inc(PL_nrs);
break;
case '\\':
- if (PL_ors)
- Safefree(PL_ors);
+ if (PL_ors_sv)
+ SvREFCNT_dec(PL_ors_sv);
if (SvOK(sv) || SvGMAGICAL(sv)) {
- s = SvPV(sv,PL_orslen);
- PL_ors = savepvn(s,PL_orslen);
+ PL_ors_sv = newSVsv(sv);
}
else {
- PL_ors = Nullch;
- PL_orslen = 0;
+ PL_ors_sv = Nullsv;
}
break;
case ',':
- if (PL_ofs)
- Safefree(PL_ofs);
- PL_ofs = savepv(SvPV(sv, PL_ofslen));
+ if (PL_ofs_sv)
+ SvREFCNT_dec(PL_ofs_sv);
+ if (SvOK(sv) || SvGMAGICAL(sv)) {
+ PL_ofs_sv = newSVsv(sv);
+ }
+ else {
+ PL_ofs_sv = Nullsv;
+ }
break;
case '#':
if (PL_ofmt)
@@ -2043,7 +2038,7 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
if (PL_origargv[i] == s + 1
#ifdef OS2
|| PL_origargv[i] == s + 2
-#endif
+#endif
)
{
++s;
@@ -2056,7 +2051,7 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
if (PL_origenviron && (PL_origenviron[0] == s + 1
#ifdef OS2
|| (PL_origenviron[0] == s + 9 && (s += 8))
-#endif
+#endif
)) {
my_setenv("NoNe SuCh", Nullch);
/* force copy of environment */
@@ -2105,7 +2100,6 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
int
Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
{
- dTHR;
DEBUG_S(PerlIO_printf(Perl_debug_log,
"0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
PTR2UV(thr), PTR2UV(sv));)
@@ -2159,7 +2153,7 @@ Perl_sighandler(int sig)
#if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
PERL_SET_THX(aTHXo); /* fake TLS, see above */
#endif
-
+
if (PL_savestack_ix + 15 <= PL_savestack_max)
flags |= 1;
if (PL_markstack_ptr < PL_markstack_max - 2)
@@ -2180,7 +2174,7 @@ Perl_sighandler(int sig)
o_save_i = PL_savestack_ix;
SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
}
- if (flags & 4)
+ if (flags & 4)
PL_markstack_ptr++; /* Protect mark. */
if (flags & 8) {
PL_retstack_ix++;
@@ -2189,7 +2183,7 @@ Perl_sighandler(int sig)
if (flags & 16)
PL_scopestack_ix += 1;
/* sv_2cv is too complicated, try a simpler variant first: */
- if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
+ if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
|| SvTYPE(cv) != SVt_PVCV)
cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
@@ -2223,16 +2217,16 @@ Perl_sighandler(int sig)
cleanup:
if (flags & 1)
PL_savestack_ix -= 8; /* Unprotect save in progress. */
- if (flags & 4)
+ if (flags & 4)
PL_markstack_ptr--;
- if (flags & 8)
+ if (flags & 8)
PL_retstack_ix--;
if (flags & 16)
PL_scopestack_ix -= 1;
if (flags & 64)
SvREFCNT_dec(sv);
PL_op = myop; /* Apparently not needed... */
-
+
PL_Sv = tSv; /* Restore global temporaries. */
PL_Xpv = tXpv;
return;
@@ -2246,7 +2240,6 @@ cleanup:
static void
restore_magic(pTHXo_ void *p)
{
- dTHR;
MGS* mgs = SSPTR(PTR2IV(p), MGS*);
SV* sv = mgs->mgs_sv;
@@ -2288,7 +2281,6 @@ restore_magic(pTHXo_ void *p)
static void
unwind_handler_stack(pTHXo_ void *p)
{
- dTHR;
U32 flags = *(U32*)p;
if (flags & 1)