This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [libiberty] support piping stderr


On Wednesday 31 January 2007 18:22, Ian Lance Taylor wrote:
> Vladimir Prus <vladimir@codesourcery.com> writes:
> 
> > +  if (errname != NULL && (flags & PEX_STDERR_TO_PIPE))
> 
> Write the test as
>     (flags & PEX_STDERR_TO_PIPE) != 0
> as in other parts of the file.

Is this a libiberty guideline? I can't find this in GNU coding standard.

> Note that PEX_STDERR_TO_PIPE will only work on Unix and Windows.
> This needs to be mentioned in the comments in libiberty.h.

Noted.

> In principle, given that we are restricted to Unix and Windows, there
> is no reason not to support PEX_STDERR_TO_PIPE for any process in the
> pipeline.  The children field of pex_obj could be promoted to an array
> of struct to record the stderr pipe descriptor number.  One could call
> something like pex_get_status to get all the file descriptors.  But I
> don't know if this facility would be useful.

I don't have any use case for it either, that's why it's not implemented.

> This should also be written as
>     ((flags & PEX_STDERR_TO_PIPE) != 0)
> If we retain the restriction that you can only use this on the last
> program, then you should also check PEX_LAST here and give an error if
> it is not set.

Are you sure? At least for pex_read_output it is explicitly documented
that PEX_LAST should *not* be used. Then, by analogy, it should
not be used for pex_read_err.

> 
> > +	  if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
> 
> You can't use PEX_BINARY_OUTPUT to decide whether stderr is in binary
> mode.  For the programs invoked by pex in a pipeline, it is natural
> for standard output to be binary while stderr is in text mode.  You
> need to introduce PEX_BINARY_ERROR.  And then it should be used in the
> existing errname == NULL case.

Changed.

> You need to update the documentation in libiberty/pexecute.txh.
> 
> 
> Please fix and resend.

Revised patch attached.

- Volodya

	include/
	* libiberty.h (PEX_STDERR_TO_PIPE): New define.
	(PEX_BINARY_ERROR): New define.
	(pex_read_err): New function.
	
	libiberty/
	* pex-common.h (struct pex_obj): New fields
	stderr_pipe and read_err.	
	* pex-common.c (pex_init_common): Initialize
	stderr_pipe.
	(pex_run_in_environment): Add error checking
	for PEX_STDERR_TO_PIPE.  Create a pipe
	for stderr if necessary.
	(pex_read_err): New.
	(pex_free): Close read_err.
	* pexecute.txh: Document changes.
	* functions.texi: Regenerated.
--- include/libiberty.h	(revision 3262)
+++ include/libiberty.h	(local)
@@ -400,6 +400,19 @@ extern struct pex_obj *pex_init (int fla
    PEX_BINARY_INPUT.  */
 #define PEX_BINARY_OUTPUT	0x20
 
+/* Capture stderr to a pipe.  The output can be read by
+   calling pex_read_err and reading from the returned
+   FILE object.  This flag may be specified only for
+   the last program in a pipeline.  
+
+   This flag is supported only on Unix and Windows.  */
+#define PEX_STDERR_TO_PIPE	0x40
+
+/* Capture stderr in binary mode.  This flag is ignored
+   on Unix.  */
+#define PEX_BINARY_ERROR	0x80
+
+
 /* Execute one program.  Returns NULL on success.  On error returns an
    error string (typically just the name of a system call); the error
    string is statically allocated.
@@ -521,6 +534,14 @@ extern FILE *pex_input_pipe (struct pex_
 
 extern FILE *pex_read_output (struct pex_obj *, int binary);
 
+/* Read the standard error of the last program to be executed.
+   pex_run can not be called after this.  BINARY should be non-zero if
+   the file should be opened in binary mode; this is ignored on Unix.
+   Returns NULL on error.  Don't call fclose on the returned FILE; it
+   will be closed by pex_free.  */
+
+extern FILE *pex_read_err (struct pex_obj *, int binary);
+
 /* Return exit status of all programs in VECTOR.  COUNT indicates the
    size of VECTOR.  The status codes in the vector are in the order of
    the calls to pex_run.  Returns 0 on error, 1 on success.  */
--- libiberty/functions.texi	(revision 3262)
+++ libiberty/functions.texi	(local)
@@ -650,7 +650,7 @@ Sets the first @var{count} bytes of @var
 
 @end deftypefn
 
-@c mkstemps.c:54
+@c mkstemps.c:58
 @deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
 
 Generate a unique temporary file name from @var{pattern}.
@@ -668,14 +668,14 @@ reading and writing.
 
 @end deftypefn
 
-@c pexecute.txh:244
+@c pexecute.txh:266
 @deftypefn Extension void pex_free (struct pex_obj @var{obj})
 
 Clean up and free all data associated with @var{obj}.
 
 @end deftypefn
 
-@c pexecute.txh:219
+@c pexecute.txh:241
 @deftypefn Extension int pex_get_status (struct pex_obj *@var{obj}, int @var{count}, int *@var{vector})
 
 Returns the exit status of all programs run using @var{obj}.
@@ -685,7 +685,7 @@ to @code{pex_run}.  Returns 0 on error, 
 
 @end deftypefn
 
-@c pexecute.txh:228
+@c pexecute.txh:250
 @deftypefn Extension int pex_get_times (struct pex_obj *@var{obj}, int @var{count}, struct pex_time *@var{vector})
 
 Returns the process execution times of all programs run using
@@ -734,7 +734,7 @@ temporary files; it may be @code{NULL} t
 
 @end deftypefn
 
-@c pexecute.txh:146
+@c pexecute.txh:155
 @deftypefn Extension {FILE *} pex_input_file (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{in_name})
 
 Return a stream for a temporary file to pass to the first program in
@@ -752,7 +752,7 @@ binary mode; otherwise, open it in the d
 @code{PEX_BINARY_OUTPUT} in @var{flags} has no effect on Unix.
 @end deftypefn
 
-@c pexecute.txh:163
+@c pexecute.txh:172
 @deftypefn Extension {FILE *} pex_input_pipe (struct pex_obj *@var{obj}, int @var{binary})
 
 Return a stream @var{fp} for a pipe connected to the standard input of
@@ -797,7 +797,7 @@ the output pipe is you, but you are bloc
 
 @end deftypefn
 
-@c pexecute.txh:250
+@c pexecute.txh:272
 @deftypefn Extension {const char *} pex_one (int @var{flags}, const char *@var{executable}, char * const *@var{argv}, const char *@var{pname}, const char *@var{outname}, const char *@var{errname}, int *@var{status}, int *@var{err})
 
 An interface to permit the easy execution of a
@@ -810,7 +810,20 @@ be set to the exit status of the program
 
 @end deftypefn
 
-@c pexecute.txh:207
+@c pexecute.txh:228
+@deftypefn Extension {FILE *} pex_read_err (struct pex_obj *@var{obj}, int @var{binary})
+
+Returns a @code{FILE} pointer which may be used to read the standard
+error of the last program in the pipeline.  When this is used,
+@code{PEX_LAST} should not be used in a call to @code{pex_run}.  After
+this is called, @code{pex_run} may no longer be called with the same
+@var{obj}.  @var{binary} should be non-zero if the file should be
+opened in binary mode.  Don't call @code{fclose} on the returned file;
+it will be closed by @code{pex_free}.
+
+@end deftypefn
+
+@c pexecute.txh:216
 @deftypefn Extension {FILE *} pex_read_output (struct pex_obj *@var{obj}, int @var{binary})
 
 Returns a @code{FILE} pointer which may be used to read the standard
@@ -861,14 +874,23 @@ Send the program's standard error to sta
 
 @vindex PEX_BINARY_INPUT
 @vindex PEX_BINARY_OUTPUT
+@vindex PEX_BINARY_ERROR
 @item PEX_BINARY_INPUT
 @itemx PEX_BINARY_OUTPUT
-The standard input (output) of the program should be read (written) in
+@itemx PEX_BINARY_ERROR
+The standard input (output or error) of the program should be read (written) in
 binary mode rather than text mode.  These flags are ignored on systems
 which do not distinguish binary mode and text mode, such as Unix.  For
 proper behavior these flags should match appropriately---a call to
 @code{pex_run} using @code{PEX_BINARY_OUTPUT} should be followed by a
 call using @code{PEX_BINARY_INPUT}.
+
+@vindex PEX_STDERR_TO_PIPE
+@item PEX_STDERR_TO_PIPE
+Send the program's standard error to a pipe, if possible.  This flag
+cannot be specified together with @code{PEX_STDERR_TO_STDOUT}.  This
+flag can be specified only on the last program in pipeline.
+
 @end table
 
 @var{executable} is the program to execute.  @var{argv} is the set of
@@ -924,7 +946,7 @@ value, or to 0 if there is no relevant @
 
 @end deftypefn
 
-@c pexecute.txh:133
+@c pexecute.txh:142
 @deftypefn Extension {const char *} pex_run_in_environment (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{executable}, char * const *@var{argv}, char * const *@var{env}, int @var{env_size}, const char *@var{outname}, const char *@var{errname}, int *@var{err})
 
 Execute one program in a pipeline, permitting the environment for the
@@ -938,8 +960,8 @@ form @code{VAR=VALUE}, with the exceptio
 
 @end deftypefn
 
-@c pexecute.txh:262
-@deftypefn Extension int pexecute (const char *@var{program}, char * const *@var{argv}, const char *@var{this_pname}, const char *@var{temp_base}, char **@var{errmsg_fmt}, char **@var{errmsg_arg}, int flags)
+@c pexecute.txh:284
+@deftypefn Extension int pexecute (const char *@var{program}, char * const *@var{argv}, const char *@var{this_pname}, const char *@var{temp_base}, char **@var{errmsg_fmt}, char **@var{errmsg_arg}, int @var{flags})
 
 This is the old interface to execute one or more programs.  It is
 still supported for compatibility purposes, but is no longer
@@ -966,7 +988,7 @@ name is unset/removed.
 
 @end deftypefn
 
-@c pexecute.txh:270
+@c pexecute.txh:292
 @deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
 
 Another part of the old execution interface.
--- libiberty/pex-common.c	(revision 3262)
+++ libiberty/pex-common.c	(local)
@@ -62,6 +62,7 @@ pex_init_common (int flags, const char *
   obj->next_input = STDIN_FILE_NO;
   obj->next_input_name = NULL;
   obj->next_input_name_allocated = 0;
+  obj->stderr_pipe = -1;
   obj->count = 0;
   obj->children = NULL;
   obj->status = NULL;
@@ -69,6 +70,7 @@ pex_init_common (int flags, const char *
   obj->number_waited = 0;
   obj->input_file = NULL;
   obj->read_output = NULL;
+  obj->read_err = NULL;
   obj->remove_count = 0;
   obj->remove = NULL;
   obj->funcs = funcs;
@@ -282,14 +284,43 @@ pex_run_in_environment (struct pex_obj *
 
   /* Set ERRDES.  */
 
+  if (errname != NULL && (flags & PEX_STDERR_TO_PIPE) != 0)
+    {
+      *err = 0;
+      errmsg = "both ERRNAME and PEX_STDERR_TO_PIPE specified.";
+      goto error_exit;
+    }
+
+  if (obj->stderr_pipe != -1)
+    {
+      *err = 0;
+      errmsg = "PEX_STDERR_TO_PIPE used in the middle of pipeline";
+      goto error_exit;
+    }
+
   if (errname == NULL)
-    errdes = STDERR_FILE_NO;
+    {
+      if (flags & PEX_STDERR_TO_PIPE)
+	{
+	  if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_ERROR) != 0) < 0)
+	    {
+	      *err = errno;
+	      errmsg = "pipe";
+	      goto error_exit;
+	    }
+	  
+	  errdes = p[WRITE_PORT];
+	  obj->stderr_pipe = p[READ_PORT];	  
+	}
+      else
+	{
+	  errdes = STDERR_FILE_NO;
+	}
+    }
   else
     {
-      /* We assume that stderr is in text mode--it certainly shouldn't
-	 be controlled by PEX_BINARY_OUTPUT.  If necessary, we can add
-	 a PEX_BINARY_STDERR flag.  */
-      errdes = obj->funcs->open_write (obj, errname, 0);
+      errdes = obj->funcs->open_write (obj, errname, 
+				       (flags & PEX_BINARY_ERROR) != 0);
       if (errdes < 0)
 	{
 	  *err = errno;
@@ -465,6 +496,17 @@ pex_read_output (struct pex_obj *obj, in
   return obj->read_output;
 }
 
+FILE *pex_read_err (struct pex_obj *obj, int binary)
+{
+  int o;
+  
+  o = obj->stderr_pipe;
+  if (o < 0 || o == STDIN_FILE_NO)
+    return NULL;
+  obj->read_err = obj->funcs->fdopenr (obj, o, binary);
+  return obj->read_err;    
+}
+
 /* Get the exit status and, if requested, the resource time for all
    the child processes.  Return 0 on failure, 1 on success.  */
 
@@ -578,6 +620,8 @@ pex_free (struct pex_obj *obj)
     free (obj->time);
   if (obj->read_output != NULL)
     fclose (obj->read_output);
+  if (obj->read_err != NULL)
+    fclose (obj->read_err);
 
   if (obj->remove_count > 0)
     {
--- libiberty/pex-common.h	(revision 3262)
+++ libiberty/pex-common.h	(local)
@@ -59,6 +59,8 @@ struct pex_obj
   char *next_input_name;
   /* Whether next_input_name was allocated using malloc.  */
   int next_input_name_allocated;
+  /* If not -1, stderr pipe from the last process.  */
+  int stderr_pipe;
   /* Number of child processes.  */
   int count;
   /* PIDs of child processes; array allocated using malloc.  */
@@ -73,6 +75,8 @@ struct pex_obj
   FILE *input_file;
   /* FILE created by pex_read_output.  */
   FILE *read_output;
+  /* FILE created by pex_read_err.  */
+  FILE *read_err;
   /* Number of temporary files to remove.  */
   int remove_count;
   /* List of temporary files to remove; array allocated using malloc
--- libiberty/pexecute.txh	(revision 3262)
+++ libiberty/pexecute.txh	(local)
@@ -67,14 +67,23 @@ Send the program's standard error to sta
 
 @vindex PEX_BINARY_INPUT
 @vindex PEX_BINARY_OUTPUT
+@vindex PEX_BINARY_ERROR
 @item PEX_BINARY_INPUT
 @itemx PEX_BINARY_OUTPUT
-The standard input (output) of the program should be read (written) in
+@itemx PEX_BINARY_ERROR
+The standard input (output or error) of the program should be read (written) in
 binary mode rather than text mode.  These flags are ignored on systems
 which do not distinguish binary mode and text mode, such as Unix.  For
 proper behavior these flags should match appropriately---a call to
 @code{pex_run} using @code{PEX_BINARY_OUTPUT} should be followed by a
 call using @code{PEX_BINARY_INPUT}.
+
+@vindex PEX_STDERR_TO_PIPE
+@item PEX_STDERR_TO_PIPE
+Send the program's standard error to a pipe, if possible.  This flag
+cannot be specified together with @code{PEX_STDERR_TO_STDOUT}.  This
+flag can be specified only on the last program in pipeline.
+
 @end table
 
 @var{executable} is the program to execute.  @var{argv} is the set of
@@ -216,6 +225,19 @@ it will be closed by @code{pex_free}.
 
 @end deftypefn
 
+@deftypefn Extension {FILE *} pex_read_err (struct pex_obj *@var{obj}, int @var{binary})
+
+Returns a @code{FILE} pointer which may be used to read the standard
+error of the last program in the pipeline.  When this is used,
+@code{PEX_LAST} should not be used in a call to @code{pex_run}.  After
+this is called, @code{pex_run} may no longer be called with the same
+@var{obj}.  @var{binary} should be non-zero if the file should be
+opened in binary mode.  Don't call @code{fclose} on the returned file;
+it will be closed by @code{pex_free}.
+
+@end deftypefn
+
+
 @deftypefn Extension int pex_get_status (struct pex_obj *@var{obj}, int @var{count}, int *@var{vector})
 
 Returns the exit status of all programs run using @var{obj}.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]