More URL "whitespacing".
[p5sagit/p5-mst-13.2.git] / perlio.c
index e68a212..1253696 100644 (file)
--- a/perlio.c
+++ b/perlio.c
@@ -1,10 +1,15 @@
 /*
- * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute
+ * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
  * under the terms of either the GNU General Public License or the
  * Artistic License, as specified in the README file.
  */
 
 /*
+ * Hour after hour for nearly three weary days he had jogged up and down,
+ * over passes, and through long dales, and across many streams.
+ */
+
+/*
  * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
  * at the dispatch tables, even when we do not need it for other reasons.
  * Invent a dSYS macro to abstract this out
@@ -187,6 +192,9 @@ PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
        if (fd >= 0) {
            char mode[8];
            int omode = fcntl(fd, F_GETFL);
+#ifdef DJGPP
+           omode = djgpp_get_stream_mode(f);
+#endif
            PerlIO_intmode2str(omode,mode,NULL);
            /* the r+ is a hack */
            return PerlIO_fdopen(fd, mode);
@@ -787,7 +795,8 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
                     * seen as an invalid separator character.
                     */
                    char q = ((*s == '\'') ? '"' : '\'');
-                   Perl_warn(aTHX_
+                   if (ckWARN(WARN_LAYER))
+                       Perl_warner(aTHX_ packWARN(WARN_LAYER),
                              "perlio: invalid separator character %c%c%c in layer specification list %s",
                              q, *s, q, s);
                    return -1;
@@ -822,7 +831,8 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
                             */
                        case '\0':
                            e--;
-                           Perl_warn(aTHX_
+                           if (ckWARN(WARN_LAYER))
+                               Perl_warner(aTHX_ packWARN(WARN_LAYER),
                                      "perlio: argument list not closed for layer \"%.*s\"",
                                      (int) (e - s), s);
                            return -1;
@@ -835,6 +845,7 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
                    }
                }
                if (e > s) {
+                   bool warn_layer = ckWARN(WARN_LAYER);
                    PerlIO_funcs *layer =
                        PerlIO_find_layer(aTHX_ s, llen, 1);
                    if (layer) {
@@ -844,7 +855,8 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
                                         &PL_sv_undef);
                    }
                    else {
-                       Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
+                       if (warn_layer)
+                           Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
                                  (int) llen, s);
                        return -1;
                    }
@@ -1072,24 +1084,73 @@ PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
     PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
                 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
                 (names) ? names : "(Null)");
-    /* Can't flush if switching encodings. */
-    if (!(names && memEQ(names, ":encoding(", 10))) {
-        PerlIO_flush(f);
+    if (names) {
+       /* Do not flush etc. if (e.g.) switching encodings.
+          if a pushed layer knows it needs to flush lower layers
+          (for example :unix which is never going to call them)
+          it can do the flush when it is pushed.
+        */
+       return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
+    }
+    else {
+       /* FIXME?: Looking down the layer stack seems wrong,
+          but is a way of reaching past (say) an encoding layer
+          to flip CRLF-ness of the layer(s) below
+        */
 #ifdef PERLIO_USING_CRLF
-       if (!names && (mode & O_BINARY)) {
-           PerlIO *top = f;
-           while (*top) {
-               if (PerlIOBase(top)->tab == &PerlIO_crlf) {
-                 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
-                 break;
+       /* Legacy binmode only has meaning if O_TEXT has a value distinct from
+          O_BINARY so we can look for it in mode.
+        */
+       if (!(mode & O_BINARY)) {
+           /* Text mode */
+           while (*f) {
+               /* Perhaps we should turn on bottom-most aware layer
+                  e.g. Ilya's idea that UNIX TTY could serve
+                */
+               if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
+                   if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
+                       /* Not in text mode - flush any pending stuff and flip it */
+                       PerlIO_flush(f);
+                       PerlIOBase(f)->flags |= PERLIO_F_CRLF;
+                   }
+                   /* Only need to turn it on in one layer so we are done */
+                   return TRUE;
                }
-               top = PerlIONext(top);
-               PerlIO_flush(top);
+               f = PerlIONext(f);
            }
+           /* Not finding a CRLF aware layer presumably means we are binary
+              which is not what was requested - so we failed
+              We _could_ push :crlf layer but so could caller
+            */
+           return FALSE;
        }
 #endif
+       /* Either asked for BINMODE or that is normal on this platform
+          see if any CRLF aware layers are present and turn off the flag
+          and possibly remove layer.
+        */
+       while (*f) {
+           if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
+               if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
+                   /* In text mode - flush any pending stuff and flip it */
+                   PerlIO_flush(f);
+                   PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
+#ifndef PERLIO_USING_CRLF
+                   /* CRLF is unusual case - if this is just the :crlf layer pop it */
+                   if (PerlIOBase(f)->tab == &PerlIO_crlf) {
+                       PerlIO_pop(aTHX_ f);
+                   }
+#endif
+                   /* Normal case is only one layer doing this, so exit on first
+                      abnormal case can always do multiple binmode calls
+                    */
+                   return TRUE;
+               }
+           }
+           f = PerlIONext(f);
+       }
+       return TRUE;
     }
-    return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
 }
 
 int
@@ -2102,6 +2163,8 @@ PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
     IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
     PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
     if (*PerlIONext(f)) {
+       /* We never call down so any pending stuff now */
+       PerlIO_flush(PerlIONext(f));
        s->fd = PerlIO_fileno(PerlIONext(f));
        /*
         * XXX could (or should) we retrieve the oflags from the open file
@@ -2333,8 +2396,11 @@ PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
        FILE *stdio =
            PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
                           PerlIOStdio_mode(mode, tmode));
-       if (stdio)
+       if (stdio) {
            s->stdio = stdio;
+           /* We never call down so any pending stuff now */
+           PerlIO_flush(PerlIONext(f));
+       }
        else
            return -1;
     }
@@ -3426,8 +3492,8 @@ PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
        PerlIO_get_base(f);
     if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
        PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
-       if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
-           STDCHAR *nl = b->ptr;
+       if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
+           STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
          scan:
            while (nl < b->end && *nl != 0xd)
                nl++;
@@ -3509,6 +3575,7 @@ PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
        ptr -= cnt;
     }
     else {
+#if 1
        /*
         * Test code - delete when it works ...
         */
@@ -3520,10 +3587,11 @@ PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
        chk -= cnt;
 
        if (ptr != chk ) {
-           Perl_warn(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
+           Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
                       " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
                       b->end, cnt);
        }
+#endif
     }
     if (c->nl) {
        if (ptr > c->nl) {