Frozen files must not undergo newline munging.  To support this, teach
m4_path_search whether a file is okay in text mode (normal m4 input)
or must be binary (a frozen file).

* src/m4.h (m4_path_search): Update prototype.
* src/path.c (m4_fopen, m4_path_search): Honor binary mode.
* src/builtin.c (m4_undivert, include): Update callers.
* src/freeze.c (reload_frozen_state): Likewise.
* src/m4.c (process_file): Likewise.
Reported by Juan Manuel Guerrero in
https://lists.gnu.org/archive/html/bug-m4/2023-01/msg00006.html
---
 src/builtin.c |  4 ++--
 src/freeze.c  |  2 +-
 src/m4.c      |  2 +-
 src/m4.h      |  2 +-
 src/path.c    | 20 +++++++++++---------
 5 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/builtin.c b/src/builtin.c
index 134607f6..cc3d1c42 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -1268,7 +1268,7 @@ m4_undivert (struct obstack *obs MAYBE_UNUSED, int argc, 
token_data **argv)
                     _("non-numeric argument to builtin `%s'"), ARG (0)));
         else
           {
-            fp = m4_path_search (ARG (i), NULL);
+            fp = m4_path_search (ARG (i), false, NULL);
             if (fp != NULL)
               {
                 insert_file (fp);
@@ -1382,7 +1382,7 @@ include (int argc, token_data **argv, bool silent)
   if (bad_argc (argv[0], argc, 2, 2))
     return;

-  fp = m4_path_search (ARG (1), &name);
+  fp = m4_path_search (ARG (1), false, &name);
   if (fp == NULL)
     {
       if (!silent)
diff --git a/src/freeze.c b/src/freeze.c
index 50b1966a..cfe5f9da 100644
--- a/src/freeze.c
+++ b/src/freeze.c
@@ -266,7 +266,7 @@ reload_frozen_state (const char *name)
     }                                                                   \
   while (0)

-  file = m4_path_search (name, NULL);
+  file = m4_path_search (name, !!O_BINARY, NULL);
   if (file == NULL)
     m4_failure (errno, _("cannot open %s"), name);
   current_file = name;
diff --git a/src/m4.c b/src/m4.c
index 14f4f753..52926139 100644
--- a/src/m4.c
+++ b/src/m4.c
@@ -376,7 +376,7 @@ process_file (const char *name)
   else
     {
       char *full_name;
-      FILE *fp = m4_path_search (name, &full_name);
+      FILE *fp = m4_path_search (name, false, &full_name);
       if (fp == NULL)
         {
           error (0, errno, _("cannot open `%s'"), name);
diff --git a/src/m4.h b/src/m4.h
index 9083a281..062200a1 100644
--- a/src/m4.h
+++ b/src/m4.h
@@ -458,7 +458,7 @@ extern const builtin *find_builtin_by_name (const char *);
 extern void include_init (void);
 extern void include_env_init (void);
 extern void add_include_directory (const char *);
-extern FILE *m4_path_search (const char *, char **);
+extern FILE *m4_path_search (const char *, bool, char **);
 
 /* File: eval.c  --- expression evaluation.  */

diff --git a/src/path.c b/src/path.c
index 3804cd57..af5e7cf0 100644
--- a/src/path.c
+++ b/src/path.c
@@ -106,11 +106,12 @@ add_include_directory (const char *dir)
 }

 /* Attempt to open FILE; if it opens, verify that it is not a
-   directory, and ensure it does not leak across execs.  */
+   directory, and ensure it does not leak across execs.  Use binary
+   mode instead of text if BINARY is set.  */
 static FILE *
-m4_fopen (const char *file)
+m4_fopen (const char *file, bool binary)
 {
-  FILE *fp = fopen (file, "re");
+  FILE *fp = fopen (file, binary ? "rbe" : "re");
   if (fp)
     {
       struct stat st;
@@ -126,12 +127,13 @@ m4_fopen (const char *file)
 }

 /* Search for FILE, first in `.', then according to -I options.  If
-   successful, return the open file, and if RESULT is not NULL, set
-   *RESULT to a malloc'd string that represents the file found with
-   respect to the current working directory.  */
+   successful, return the open file (in BINARY mode if requested), and
+   if RESULT is not NULL, set *RESULT to a malloc'd string that
+   represents the file found with respect to the current working
+   directory.  */

 FILE *
-m4_path_search (const char *file, char **result)
+m4_path_search (const char *file, bool binary, char **result)
 {
   FILE *fp;
   includes *incl;
@@ -149,7 +151,7 @@ m4_path_search (const char *file, char **result)
     }

   /* Look in current working directory first.  */
-  fp = m4_fopen (file);
+  fp = m4_fopen (file, binary);
   if (fp != NULL)
     {
       if (result)
@@ -170,7 +172,7 @@ m4_path_search (const char *file, char **result)
       xfprintf (stderr, "m4_path_search (%s) -- trying %s\n", file, name);
 #endif

-      fp = m4_fopen (name);
+      fp = m4_fopen (name, binary);
       if (fp != NULL)
         {
           if (debug_level & DEBUG_TRACE_PATH)
-- 
2.49.0


_______________________________________________
M4-patches mailing list
M4-patches@gnu.org
https://lists.gnu.org/mailman/listinfo/m4-patches

Reply via email to