diff mbox series

dumpfile cleanup

Message ID a5f184cd-23d1-d075-37c9-0f6ab578adeb@acm.org
State New
Headers show
Series dumpfile cleanup | expand

Commit Message

Nathan Sidwell April 26, 2018, noon UTC
I wanted to add '-' as a synonym for stdout in the dumping machinery.  But this 
bit of cleanup's needed first.  We check the special stdout/stderr names in 3 
different places -- that's crying out for a worker function.  We also check 
those streams by name before determining whether to fclose.  It seems more 
straight forwards to just compare to stdin & stdout themselves.

This patch does that cleanup, and the next patch will add the '-' functionality. 
  I've been using this on the modules branch for some time now.

ok for trunk?

nathan

Comments

Richard Biener April 26, 2018, 2:06 p.m. UTC | #1
On Thu, Apr 26, 2018 at 2:00 PM, Nathan Sidwell <nathan@acm.org> wrote:
> I wanted to add '-' as a synonym for stdout in the dumping machinery.  But
> this bit of cleanup's needed first.  We check the special stdout/stderr
> names in 3 different places -- that's crying out for a worker function.  We
> also check those streams by name before determining whether to fclose.  It
> seems more straight forwards to just compare to stdin & stdout themselves.
>
> This patch does that cleanup, and the next patch will add the '-'
> functionality.  I've been using this on the modules branch for some time
> now.
>
> ok for trunk?

OK.

Richard.

>
> nathan
> --
> Nathan Sidwell
diff mbox series

Patch

2018-04-26  Nathan Sidwell  <nathan@acm.org>

	* dumpfile.c (dump_open): New.
	(dump_open_alternate_stream, dump_start, dump_begin): Call it.
	(dump_finish): Detect stdio/stderr by value not name.

Index: dumpfile.c
===================================================================
--- dumpfile.c	(revision 259657)
+++ dumpfile.c	(working copy)
@@ -312,6 +312,27 @@  get_dump_file_name (struct dump_file_inf
   return concat (dump_base_name, dump_id, dfi->suffix, NULL);
 }
 
+/* Open a dump file called FILENAME.  Some filenames are special and
+   refer to the standard streams.  TRUNC indicates whether this is the
+   first open (so the file should be truncated, rather than appended).
+   An error message is emitted in the event of failure.  */
+
+static FILE *
+dump_open (const char *filename, bool trunc)
+{
+  if (strcmp ("stderr", filename) == 0)
+    return stderr;
+
+  if (strcmp ("stdout", filename) == 0)
+    return stdout;
+
+  FILE *stream = fopen (filename, trunc ? "w" : "a");
+
+  if (!stream)
+    error ("could not open dump file %qs: %m", filename);
+  return stream;
+}
+
 /* For a given DFI, open an alternate dump filename (which could also
    be a standard stream such as stdout/stderr). If the alternate dump
    file cannot be opened, return NULL.  */
@@ -319,22 +340,15 @@  get_dump_file_name (struct dump_file_inf
 static FILE *
 dump_open_alternate_stream (struct dump_file_info *dfi)
 {
-  FILE *stream ;
   if (!dfi->alt_filename)
     return NULL;
 
   if (dfi->alt_stream)
     return dfi->alt_stream;
 
-  stream = strcmp ("stderr", dfi->alt_filename) == 0
-    ? stderr
-    : strcmp ("stdout", dfi->alt_filename) == 0
-    ? stdout
-    : fopen (dfi->alt_filename, dfi->alt_state < 0 ? "w" : "a");
+  FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
 
-  if (!stream)
-    error ("could not open dump file %qs: %m", dfi->alt_filename);
-  else
+  if (stream)
     dfi->alt_state = 1;
 
   return stream;
@@ -515,14 +529,8 @@  dump_start (int phase, dump_flags_t *fla
   name = get_dump_file_name (phase);
   if (name)
     {
-      stream = strcmp ("stderr", name) == 0
-          ? stderr
-          : strcmp ("stdout", name) == 0
-          ? stdout
-          : fopen (name, dfi->pstate < 0 ? "w" : "a");
-      if (!stream)
-        error ("could not open dump file %qs: %m", name);
-      else
+      stream = dump_open (name, dfi->pstate < 0);
+      if (stream)
         {
           dfi->pstate = 1;
           count++;
@@ -562,13 +570,10 @@  dump_finish (int phase)
   if (phase < 0)
     return;
   dfi = get_dump_file_info (phase);
-  if (dfi->pstream && (!dfi->pfilename
-                       || (strcmp ("stderr", dfi->pfilename) != 0
-                           && strcmp ("stdout", dfi->pfilename) != 0)))
+  if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
     fclose (dfi->pstream);
 
-  if (dfi->alt_stream && strcmp ("stderr", dfi->alt_filename) != 0
-      && strcmp ("stdout", dfi->alt_filename) != 0)
+  if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
     fclose (dfi->alt_stream);
 
   dfi->alt_stream = NULL;
@@ -607,15 +612,8 @@  dump_begin (int phase, dump_flags_t *fla
     return NULL;
   dfi = get_dump_file_info (phase);
 
-  stream = strcmp ("stderr", name) == 0
-    ? stderr
-    : strcmp ("stdout", name) == 0
-    ? stdout
-    : fopen (name, dfi->pstate < 0 ? "w" : "a");
-
-  if (!stream)
-    error ("could not open dump file %qs: %m", name);
-  else
+  stream = dump_open (name, dfi->pstate < 0);
+  if (stream)
     dfi->pstate = 1;
   free (name);