groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 04/41: [preconv]: Test operands for seekability.


From: G. Branden Robinson
Subject: [groff] 04/41: [preconv]: Test operands for seekability.
Date: Sat, 5 Mar 2022 16:06:09 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit ecf7aeccbb4e5ecf54277117b503edf27dffbd9f
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Thu Mar 3 05:24:08 2022 +1100

    [preconv]: Test operands for seekability.
    
    Stop assuming that the default input stream, or an explicit '-' operand,
    is the only unseekable stream.  Check instead.
    
    * src/preproc/preconv/preconv.cpp (do_file): Add new Boolean
      `is_seekable`.  Test the input stream with `fseek()` per a suggestion
      from Ingo Schwarze.  Report unseekability in debug output and skip
      coding tag and uchardet tests altogether (precisely paralleling our
      description in the preconv(1) man page).  Also update debugging output
      to say "<standard input>" instead of "-".
    
    Fixes <https://savannah.gnu.org/bugs/?62131>.
    
    Also add editor aid comments and drop old style Emacs file-local
    variable setting.
---
 ChangeLog                       | 16 +++++++++++++++
 src/preproc/preconv/preconv.cpp | 45 ++++++++++++++++++++++++++++-------------
 2 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5ceff01e..e09733f7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2022-03-04  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       [preconv]: Stop assuming that the default input stream, or an
+       explicit '-' operand, is the only unseekable stream.  Check
+       instead.
+
+       * src/preproc/preconv/preconv.cpp (do_file): Add new Boolean
+       `is_seekable`.  Test the input stream with `fseek()` per a
+       suggestion from Ingo Schwarze.  Report unseekability in debug
+       output and skip coding tag and uchardet tests altogether
+       {precisely paralleling our description in the preconv(1) man
+       page}.  Also update debugging output to say "<standard input>"
+       instead of "-".
+
+       Fixes <https://savannah.gnu.org/bugs/?62131>.
+
 2022-03-04  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        [preconv]: Regression-test Savannah #62131.
diff --git a/src/preproc/preconv/preconv.cpp b/src/preproc/preconv/preconv.cpp
index b5b5a8cd..0e9ce28a 100644
--- a/src/preproc/preconv/preconv.cpp
+++ b/src/preproc/preconv/preconv.cpp
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /* Copyright (C) 2005-2020 Free Software Foundation, Inc.
      Written by Werner Lemberg (wl@gnu.org)
 
@@ -1070,7 +1069,8 @@ end:
 }
 
 // ---------------------------------------------------------
-// Handle an input file.  If filename is '-' handle stdin.
+// Handle an input file.  If `filename` is "-", read the
+// standard input stream.
 //
 // Return 1 on success, 0 otherwise.
 // ---------------------------------------------------------
@@ -1079,21 +1079,34 @@ do_file(const char *filename)
 {
   FILE *fp;
   string BOM, data;
+  bool is_seekable = false;
+  string reported_filename;
 
-  if (strcmp(filename, "-")) {
-    if (debug_flag)
-      fprintf(stderr, "file '%s':\n", filename);
+  // TODO: Consider moving some of this into a `quoted_file_name`
+  // function in libgroff.
+  if (strcmp(filename, "-") == 0) {
+    fp = stdin;
+    reported_filename = string("<standard input>");
+  }
+  else {
     fp = fopen(filename, FOPEN_RB);
-    if (!fp) {
-      error("can't open '%1': %2", filename, strerror(errno));
-      return 0;
-    }
+    reported_filename = "'" + string(filename) + "'";
+  }
+  if (!fp) {
+    error("can't open %1: %2", reported_filename.contents(),
+         strerror(errno));
+    return 0;
+  }
+  if (debug_flag)
+    fprintf(stderr, "processing %s\n", reported_filename.contents());
+  if (fseek(fp, 0L, SEEK_SET) == 0) {
+    is_seekable = true;
   }
   else {
+    SET_BINARY(fileno(fp));
     if (debug_flag)
-      fprintf(stderr, "standard input:\n");
-    SET_BINARY(fileno(stdin));
-    fp = stdin;
+      fprintf(stderr, "  stream is not seekable: %s\n",
+             strerror(errno));
   }
   const char *BOM_encoding = get_BOM(fp, BOM, data);
   // Determine the encoding.
@@ -1121,7 +1134,7 @@ do_file(const char *filename)
     if (!file_encoding) {
       if (debug_flag)
        fprintf(stderr, "  no coding tag\n");
-      if (strcmp(filename, "-"))
+      if (is_seekable)
          file_encoding = detect_file_encoding(fp);
       if (!file_encoding) {
         if (debug_flag)
@@ -1286,4 +1299,8 @@ main(int argc, char **argv)
   return nbad != 0;
 }
 
-/* end of preconv.cpp */
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:



reply via email to

[Prev in Thread] Current Thread [Next in Thread]