[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

opening scripts in binmode (on Windows)



The following set of changes makes Perl open scripts on Windows 
in binmode.  This helps to enable processing binary content via
the DATA filehandle, and enables the ByteLoader to work.

I think we may want to do this on other DOSISH platforms as well.
I'll leave it to the respective platforms champions to send in
patches to enable it.


Sarathy
gsar@ActiveState.com
-----------------------------------8<-----------------------------------
Change 4739 by gsar@rake on 1999/12/30 05:44:21

	enable the PERL_BINMODE_SCRIPTS behavior by default on Windows
	to allow ByteLoader to work; the DATA filehandles continue to
	be left open in text mode for compatibility

Affected files ...

... //depot/perl/embed.h#149 edit
... //depot/perl/embed.pl#93 edit
... //depot/perl/objXSUB.h#90 edit
... //depot/perl/pod/perldelta.pod#125 edit
... //depot/perl/proto.h#183 edit
... //depot/perl/sv.c#182 edit
... //depot/perl/toke.c#176 edit
... //depot/perl/win32/Makefile#113 edit
... //depot/perl/win32/makefile.mk#122 edit
... //depot/perl/win32/win32.c#107 edit
... //depot/perl/win32/win32.h#56 edit

Differences ...

==== //depot/perl/embed.h#149 (text+w) ====
Index: perl/embed.h
--- perl/embed.h.~1~	Wed Jan  5 11:14:03 2000
+++ perl/embed.h	Wed Jan  5 11:14:03 2000
@@ -1077,8 +1077,8 @@
 #  if defined(CRIPPLED_CC)
 #define uni			S_uni
 #  endif
-#  if defined(WIN32)
-#define win32_textfilter	S_win32_textfilter
+#  if defined(PERL_CR_FILTER)
+#define cr_textfilter		S_cr_textfilter
 #  endif
 #endif
 #if defined(PERL_IN_UNIVERSAL_C) || defined(PERL_DECL_PROT)
@@ -2482,8 +2482,8 @@
 #  if defined(CRIPPLED_CC)
 #define uni(a,b)		S_uni(aTHX_ a,b)
 #  endif
-#  if defined(WIN32)
-#define win32_textfilter(a,b,c)	S_win32_textfilter(aTHX_ a,b,c)
+#  if defined(PERL_CR_FILTER)
+#define cr_textfilter(a,b,c)	S_cr_textfilter(aTHX_ a,b,c)
 #  endif
 #endif
 #if defined(PERL_IN_UNIVERSAL_C) || defined(PERL_DECL_PROT)
@@ -4830,9 +4830,9 @@
 #define S_uni			CPerlObj::S_uni
 #define uni			S_uni
 #  endif
-#  if defined(WIN32)
-#define S_win32_textfilter	CPerlObj::S_win32_textfilter
-#define win32_textfilter	S_win32_textfilter
+#  if defined(PERL_CR_FILTER)
+#define S_cr_textfilter		CPerlObj::S_cr_textfilter
+#define cr_textfilter		S_cr_textfilter
 #  endif
 #endif
 #if defined(PERL_IN_UNIVERSAL_C) || defined(PERL_DECL_PROT)

==== //depot/perl/embed.pl#93 (xtext) ====
Index: perl/embed.pl
--- perl/embed.pl.~1~	Wed Jan  5 11:14:03 2000
+++ perl/embed.pl	Wed Jan  5 11:14:03 2000
@@ -2169,8 +2169,8 @@
 #  if defined(CRIPPLED_CC)
 s	|int	|uni		|I32 f|char *s
 #  endif
-#  if defined(WIN32)
-s	|I32	|win32_textfilter	|int idx|SV *sv|int maxlen
+#  if defined(PERL_CR_FILTER)
+s	|I32	|cr_textfilter	|int idx|SV *sv|int maxlen
 #  endif
 #endif
 

==== //depot/perl/objXSUB.h#90 (text+w) ====
Index: perl/objXSUB.h
--- perl/objXSUB.h.~1~	Wed Jan  5 11:14:03 2000
+++ perl/objXSUB.h	Wed Jan  5 11:14:03 2000
@@ -3687,7 +3687,7 @@
 #if defined(PERL_IN_TOKE_C) || defined(PERL_DECL_PROT)
 #  if defined(CRIPPLED_CC)
 #  endif
-#  if defined(WIN32)
+#  if defined(PERL_CR_FILTER)
 #  endif
 #endif
 #if defined(PERL_IN_UNIVERSAL_C) || defined(PERL_DECL_PROT)

==== //depot/perl/pod/perldelta.pod#125 (text) ====
Index: perl/pod/perldelta.pod
--- perl/pod/perldelta.pod.~1~	Wed Jan  5 11:14:03 2000
+++ perl/pod/perldelta.pod	Wed Jan  5 11:14:03 2000
@@ -1043,6 +1043,10 @@
 Rudimentary support for building under command.com in Windows 95
 has been added.
 
+Scripts are read in binary mode by default to allow ByteLoader (and
+the filter mechanism in general) to work properly.  For compatibility,
+the DATA filehandle continues to be set to text mode.
+
 [TODO - GSAR]
 
 =head1 New tests

==== //depot/perl/proto.h#183 (text+w) ====
Index: perl/proto.h
--- perl/proto.h.~1~	Wed Jan  5 11:14:03 2000
+++ perl/proto.h	Wed Jan  5 11:14:03 2000
@@ -1095,8 +1095,8 @@
 #  if defined(CRIPPLED_CC)
 STATIC int	S_uni(pTHX_ I32 f, char *s);
 #  endif
-#  if defined(WIN32)
-STATIC I32	S_win32_textfilter(pTHX_ int idx, SV *sv, int maxlen);
+#  if defined(PERL_CR_FILTER)
+STATIC I32	S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen);
 #  endif
 #endif
 

==== //depot/perl/sv.c#182 (text) ====
Index: perl/sv.c
--- perl/sv.c.~1~	Wed Jan  5 11:14:03 2000
+++ perl/sv.c	Wed Jan  5 11:14:03 2000
@@ -4058,10 +4058,6 @@
 	}
     }
 
-#ifdef WIN32
-    win32_strip_return(sv);
-#endif
-
     return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
 }
 

==== //depot/perl/toke.c#176 (text) ====
Index: perl/toke.c
--- perl/toke.c.~1~	Wed Jan  5 11:14:03 2000
+++ perl/toke.c	Wed Jan  5 11:14:03 2000
@@ -304,15 +304,36 @@
  * utf16-to-utf8-reversed.
  */
 
-#ifdef WIN32
+#ifdef PERL_CR_FILTER
+static void
+strip_return(SV *sv)
+{
+    register char *s = SvPVX(sv);
+    register char *e = s + SvCUR(sv);
+    /* outer loop optimized to do nothing if there are no CR-LFs */
+    while (s < e) {
+	if (*s++ == '\r' && *s == '\n') {
+	    /* hit a CR-LF, need to copy the rest */
+	    register char *d = s - 1;
+	    *d++ = *s++;
+	    while (s < e) {
+		if (*s == '\r' && s[1] == '\n')
+		    s++;
+		*d++ = *s++;
+	    }
+	    SvCUR(sv) -= s - d;
+	    return;
+	}
+    }
+}
 
 STATIC I32
-S_win32_textfilter(pTHX_ int idx, SV *sv, int maxlen)
+S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
 {
- I32 count = FILTER_READ(idx+1, sv, maxlen);
- if (count > 0 && !maxlen)
-  win32_strip_return(sv);
- return count;
+    I32 count = FILTER_READ(idx+1, sv, maxlen);
+    if (count > 0 && !maxlen)
+	strip_return(sv);
+    return count;
 }
 #endif
 
@@ -1872,9 +1893,9 @@
 STATIC char *
 S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
 {
-#ifdef WIN32FILTER
+#ifdef PERL_CR_FILTER
     if (!PL_rsfp_filters) {
-	filter_add(win32_textfilter,NULL);
+	filter_add(S_cr_textfilter,NULL);
     }
 #endif
     if (PL_rsfp_filters) {
@@ -3785,6 +3806,26 @@
 		    IoTYPE(GvIOp(gv)) = '-';
 		else
 		    IoTYPE(GvIOp(gv)) = '<';
+#if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
+		/* if the script was opened in binmode, we need to revert
+		 * it to text mode for compatibility.
+		 * XXX this is a questionable hack at best. */
+		{
+		    Off_t loc = 0;
+		    if (IoTYPE(GvIOp(gv)) == '<') {
+			loc = PerlIO_tell(PL_rsfp);
+			(void)PerlIO_seek(PL_rsfp, 0L, 0);
+		    }
+		    if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
+#if defined(__BORLANDC__)
+			/* XXX see note in do_binmode() */
+			((FILE*)PL_rsfp)->flags |= _F_BIN;
+#endif
+			if (loc > 0)
+			    PerlIO_seek(PL_rsfp, loc, 0);
+		    }
+		}
+#endif
 		PL_rsfp = Nullfp;
 	    }
 	    goto fake_eof;

==== //depot/perl/win32/Makefile#113 (text) ====
Index: perl/win32/Makefile
--- perl/win32/Makefile.~1~	Wed Jan  5 11:14:03 2000
+++ perl/win32/Makefile	Wed Jan  5 11:14:03 2000
@@ -147,28 +147,36 @@
 CCLIBDIR	= $(CCHOME)\lib
 
 #
-# additional compiler flags can be specified here.
+# Additional compiler flags can be specified here.
+#
+
 #
-# Adding -DPERL_POLLUTE enables support for old symbols, at the expense of
-# extreme pollution. You most probably want this if you're compiling modules
-# from CPAN, or other such serious uses of this experimental perl release.
-# We don't enable this by default because we want the modules to get fixed
-# instead of clinging to shortcuts like this one.
+# This should normally be disabled.  Adding -DPERL_POLLUTE enables support
+# for old symbols by default, at the expense of extreme pollution.  You most
+# probably just want to build modules that won't compile with
+#         perl Makefile.PL POLLUTE=1
+# instead of enabling this.  Please report such modules to the respective
+# authors.
 #
 #BUILDOPT	= $(BUILDOPT) -DPERL_POLLUTE
 
 #
-# enable this to disable the File::Glob implementation of CORE::glob
+# This should normally be disabled.  Enabling it will disable the File::Glob
+# implementation of CORE::glob.
 #
 #BUILDOPT	= $(BUILDOPT) -DPERL_EXTERNAL_GLOB
 
-# Enabling this causes perl to do its own CR/LF conversions, and is required
-# if you want to be able to use the bytecode compiler and ByteLoader
-BUILDOPT	= $(BUILDOPT) -DUSE_BINMODE_SCRIPTS
+#
+# This should normally be disabled.  Enabling it causes perl to read scripts
+# in text mode (which is the 5.005 behavior) and will break ByteLoader.
+#BUILDOPT	= $(BUILDOPT) -DUSE_TEXTMODE_SCRIPTS
 
-# Enabling this runs a cloned toplevel interpreter (*EXPERIMENTAL*, fails tests)
+#
+# This should normally be disabled.  Enabling it runs a cloned toplevel
+# interpreter (*EXPERIMENTAL*, fails tests)
 #BUILDOPT	= $(BUILDOPT) -DTOP_CLONE
 
+#
 # specify semicolon-separated list of extra directories that modules will
 # look for libraries (spaces in path names need not be quoted)
 #

==== //depot/perl/win32/makefile.mk#122 (text) ====
Index: perl/win32/makefile.mk
--- perl/win32/makefile.mk.~1~	Wed Jan  5 11:14:03 2000
+++ perl/win32/makefile.mk	Wed Jan  5 11:14:03 2000
@@ -166,26 +166,33 @@
 CCLIBDIR	*= $(CCHOME)\lib
 
 #
-# additional compiler flags can be specified here.
+# Additional compiler flags can be specified here.
+#
+
 #
-# Adding -DPERL_POLLUTE enables support for old symbols, at the expense of
-# extreme pollution. You most probably want this if you're compiling modules
-# from CPAN, or other such serious uses of this experimental perl release.
-# We don't enable this by default because we want the modules to get fixed
-# instead of clinging to shortcuts like this one.
+# This should normally be disabled.  Adding -DPERL_POLLUTE enables support
+# for old symbols by default, at the expense of extreme pollution.  You most
+# probably just want to build modules that won't compile with
+#         perl Makefile.PL POLLUTE=1
+# instead of enabling this.  Please report such modules to the respective
+# authors.
 #
 #BUILDOPT	+= -DPERL_POLLUTE
 
 #
-# enable this to disable the File::Glob implementation of CORE::glob
+# This should normally be disabled.  Enabling it will disable the File::Glob
+# implementation of CORE::glob.
 #
 #BUILDOPT	+= -DPERL_EXTERNAL_GLOB
 
-# Enabling this causes perl to do its own CR/LF conversions, and is required
-# if you want to be able to use the bytecode compiler and ByteLoader
-BUILDOPT	+= -DUSE_BINMODE_SCRIPTS
+#
+# This should normally be disabled.  Enabling it causes perl to read scripts
+# in text mode (which is the 5.005 behavior) and will break ByteLoader.
+#BUILDOPT	+= -DUSE_TEXTMODE_SCRIPTS
 
-# Enabling this runs a cloned toplevel interpreter (*EXPERIMENTAL*, fails tests)
+#
+# This should normally be disabled.  Enabling it runs a cloned toplevel
+# interpreter (*EXPERIMENTAL*, fails tests)
 #BUILDOPT	+= -DTOP_CLONE
 
 #

==== //depot/perl/win32/win32.c#107 (text) ====
Index: perl/win32/win32.c
--- perl/win32/win32.c.~1~	Wed Jan  5 11:14:03 2000
+++ perl/win32/win32.c	Wed Jan  5 11:14:03 2000
@@ -3688,31 +3688,6 @@
     MALLOC_INIT;
 }
 
-#ifdef USE_BINMODE_SCRIPTS
-
-void
-win32_strip_return(SV *sv)
-{
- char *s = SvPVX(sv);
- char *e = s+SvCUR(sv);
- char *d = s;
- while (s < e)
-  {
-   if (*s == '\r' && s[1] == '\n')
-    {
-     *d++ = '\n';
-     s += 2;
-    }
-   else 
-    {
-     *d++ = *s++;
-    }   
-  }
- SvCUR_set(sv,d-SvPVX(sv)); 
-}
-
-#endif
-
 #ifdef USE_ITHREADS
 
 #  ifdef PERL_OBJECT

==== //depot/perl/win32/win32.h#56 (text) ====
Index: perl/win32/win32.h
--- perl/win32/win32.h.~1~	Wed Jan  5 11:14:03 2000
+++ perl/win32/win32.h	Wed Jan  5 11:14:03 2000
@@ -325,12 +325,10 @@
 #define PERL_CORE
 #endif
 
-#ifdef USE_BINMODE_SCRIPTS
-#define PERL_SCRIPT_MODE "rb"
-EXT void win32_strip_return(struct sv *sv);
+#ifdef USE_TEXTMODE_SCRIPTS
+#  define PERL_SCRIPT_MODE		"r"
 #else
-#define PERL_SCRIPT_MODE "r"
-#define win32_strip_return(sv) NOOP
+#  define PERL_SCRIPT_MODE		"rb"
 #endif
 
 /* 
End of Patch.

Change 4740 by gsar@auger on 1999/12/30 19:35:07

	leave DATA open in binmode if __END__ line doesn't have CRLF

Affected files ...

... //depot/perl/pod/perldelta.pod#126 edit
... //depot/perl/toke.c#177 edit

Differences ...

==== //depot/perl/pod/perldelta.pod#126 (text) ====
Index: perl/pod/perldelta.pod
--- perl/pod/perldelta.pod.~1~	Wed Jan  5 11:14:40 2000
+++ perl/pod/perldelta.pod	Wed Jan  5 11:14:40 2000
@@ -1045,7 +1045,10 @@
 
 Scripts are read in binary mode by default to allow ByteLoader (and
 the filter mechanism in general) to work properly.  For compatibility,
-the DATA filehandle continues to be set to text mode.
+the DATA filehandle will be set to text mode if a carriage return is
+detected at the end of the line containing the __END__ or __DATA__
+token; if not, the DATA filehandle will be left open in binary mode.
+Earlier versions always opened the DATA filehandle in text mode.
 
 [TODO - GSAR]
 

==== //depot/perl/toke.c#177 (text) ====
Index: perl/toke.c
--- perl/toke.c.~1~	Wed Jan  5 11:14:40 2000
+++ perl/toke.c	Wed Jan  5 11:14:40 2000
@@ -3808,8 +3808,10 @@
 		    IoTYPE(GvIOp(gv)) = '<';
 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
 		/* if the script was opened in binmode, we need to revert
-		 * it to text mode for compatibility.
+		 * it to text mode for compatibility; but only iff it has CRs
 		 * XXX this is a questionable hack at best. */
+		if (PL_bufend-PL_bufptr > 2
+		    && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
 		{
 		    Off_t loc = 0;
 		    if (IoTYPE(GvIOp(gv)) == '<') {
End of Patch.

Change 4741 by gsar@auger on 1999/12/30 19:36:21

	avoid CRLF in byteloadable files created by perlcc

Affected files ...

... //depot/perl/utils/perlcc.PL#14 edit

Differences ...

==== //depot/perl/utils/perlcc.PL#14 (text) ====
Index: perl/utils/perlcc.PL
--- perl/utils/perlcc.PL.~1~	Wed Jan  5 11:15:00 2000
+++ perl/utils/perlcc.PL	Wed Jan  5 11:15:00 2000
@@ -257,20 +257,19 @@
 
     local($") = " -I";
 
-    open(GENFILE, "> $generated_file") || die "Can't open $generated_file: $!";
-
     if ($backend eq "Bytecode")
     {
         require ByteLoader;
 
+	open(GENFILE, "> $generated_file") || die "Can't open $generated_file: $!";
+	binmode GENFILE;
         print GENFILE "#!$^X\n" if @_ == 3;
         print GENFILE "use ByteLoader $ByteLoader::VERSION;\n";
+	close(GENFILE);
 
 	$output_switch ="a";
     }
 
-    close(GENFILE);
-
     if (@_ == 3)                                   # compiling a program   
     {
         chmod $generated_file, 0777 & ~umask if $backend eq "Bytecode";
End of Patch.

Change 4752 by gsar@rake on 2000/01/04 01:19:20

	s/USE_TEXTMODE_SCRIPTS/PERL_TEXTMODE_SCRIPTS/g

Affected files ...

... //depot/perl/win32/Makefile#114 edit
... //depot/perl/win32/makefile.mk#123 edit
... //depot/perl/win32/win32.h#58 edit

Differences ...

==== //depot/perl/win32/Makefile#114 (text) ====
Index: perl/win32/Makefile
--- perl/win32/Makefile.~1~	Wed Jan  5 04:14:19 2000
+++ perl/win32/Makefile	Wed Jan  5 04:14:19 2000
@@ -169,7 +169,7 @@
 #
 # This should normally be disabled.  Enabling it causes perl to read scripts
 # in text mode (which is the 5.005 behavior) and will break ByteLoader.
-#BUILDOPT	= $(BUILDOPT) -DUSE_TEXTMODE_SCRIPTS
+#BUILDOPT	= $(BUILDOPT) -DPERL_TEXTMODE_SCRIPTS
 
 #
 # This should normally be disabled.  Enabling it runs a cloned toplevel

==== //depot/perl/win32/makefile.mk#123 (text) ====
Index: perl/win32/makefile.mk
--- perl/win32/makefile.mk.~1~	Wed Jan  5 04:14:19 2000
+++ perl/win32/makefile.mk	Wed Jan  5 04:14:19 2000
@@ -188,7 +188,7 @@
 #
 # This should normally be disabled.  Enabling it causes perl to read scripts
 # in text mode (which is the 5.005 behavior) and will break ByteLoader.
-#BUILDOPT	+= -DUSE_TEXTMODE_SCRIPTS
+#BUILDOPT	+= -DPERL_TEXTMODE_SCRIPTS
 
 #
 # This should normally be disabled.  Enabling it runs a cloned toplevel

==== //depot/perl/win32/win32.h#58 (text) ====
Index: perl/win32/win32.h
--- perl/win32/win32.h.~1~	Wed Jan  5 04:14:19 2000
+++ perl/win32/win32.h	Wed Jan  5 04:14:19 2000
@@ -330,7 +330,7 @@
 #define PERL_CORE
 #endif
 
-#ifdef USE_TEXTMODE_SCRIPTS
+#ifdef PERL_TEXTMODE_SCRIPTS
 #  define PERL_SCRIPT_MODE		"r"
 #else
 #  define PERL_SCRIPT_MODE		"rb"
End of Patch.


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]