diff --git a/include/ChangeLog b/include/ChangeLog index 0e8b1500ca8..c494efc8fae 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,9 @@ +2007-01-31 Vladimir Prus + + * libiberty.h (PEX_STDERR_TO_PIPE): New define. + (PEX_BINARY_ERROR): New define. + (pex_read_err): New function. + 2007-01-29 Simon Baldwin * demangle.h: New cplus_demangle_print_callback, diff --git a/include/libiberty.h b/include/libiberty.h index 27291c98868..cc6cce3d220 100644 --- a/include/libiberty.h +++ b/include/libiberty.h @@ -400,6 +400,19 @@ extern struct pex_obj *pex_init (int flags, const char *pname, 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_obj *obj, int binary); 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. */ diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 33853243c71..4f23225cf72 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,17 @@ +2007-01-31 Vladimir Prus + + * 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. + 2007-01-31 Ben Elliston * strsignal.c (psignal): Change type of signo to int. diff --git a/libiberty/functions.texi b/libiberty/functions.texi index 45dcd4c086e..b791f23c1b4 100644 --- a/libiberty/functions.texi +++ b/libiberty/functions.texi @@ -650,7 +650,7 @@ Sets the first @var{count} bytes of @var{s} to the constant byte @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, 1 on success. @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} to use a randomly chosen name. @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 default mode. Including @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 blocked on the input pipe. @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 standard output, if possible. @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 @code{errno}. @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 exception of the last element that must be @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. diff --git a/libiberty/pex-common.c b/libiberty/pex-common.c index bb127f2ed77..7f355365eda 100644 --- a/libiberty/pex-common.c +++ b/libiberty/pex-common.c @@ -62,6 +62,7 @@ pex_init_common (int flags, const char *pname, const char *tempbase, 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 *pname, const char *tempbase, 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 *obj, int flags, const char *executable, /* 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,18 @@ pex_read_output (struct pex_obj *obj, int binary) 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 +621,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) { diff --git a/libiberty/pex-common.h b/libiberty/pex-common.h index 0e88c355ffc..8edc13b7650 100644 --- a/libiberty/pex-common.h +++ b/libiberty/pex-common.h @@ -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 diff --git a/libiberty/pexecute.txh b/libiberty/pexecute.txh index 0310d8f0858..b87c0e53a6d 100644 --- a/libiberty/pexecute.txh +++ b/libiberty/pexecute.txh @@ -67,14 +67,23 @@ Send the program's standard error to standard output, if possible. @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}.