Back to index

cell-binutils  2.17cvs20070401
pex-common.h
Go to the documentation of this file.
00001 /* Utilities to execute a program in a subprocess (possibly linked by pipes
00002    with other subprocesses), and wait for it.  Shared logic.
00003    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004
00004    Free Software Foundation, Inc.
00005 
00006 This file is part of the libiberty library.
00007 Libiberty is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Library General Public
00009 License as published by the Free Software Foundation; either
00010 version 2 of the License, or (at your option) any later version.
00011 
00012 Libiberty is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Library General Public License for more details.
00016 
00017 You should have received a copy of the GNU Library General Public
00018 License along with libiberty; see the file COPYING.LIB.  If not,
00019 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00020 Boston, MA 02110-1301, USA.  */
00021 
00022 #ifndef PEX_COMMON_H
00023 #define PEX_COMMON_H
00024 
00025 #include "config.h"
00026 #include "libiberty.h"
00027 #include <stdio.h>
00028 
00029 #define install_error_msg "installation problem, cannot exec `%s'"
00030 
00031 /* stdin file number.  */
00032 #define STDIN_FILE_NO 0
00033 
00034 /* stdout file number.  */
00035 #define STDOUT_FILE_NO 1
00036 
00037 /* stderr file number.  */
00038 #define STDERR_FILE_NO 2
00039 
00040 /* value of `pipe': port index for reading.  */
00041 #define READ_PORT 0
00042 
00043 /* value of `pipe': port index for writing.  */
00044 #define WRITE_PORT 1
00045 
00046 /* The structure used by pex_init and friends.  */
00047 
00048 struct pex_obj
00049 {
00050   /* Flags.  */
00051   int flags;
00052   /* Name of calling program, for error messages.  */
00053   const char *pname;
00054   /* Base name to use for temporary files.  */
00055   const char *tempbase;
00056   /* Pipe to use as stdin for next process.  */
00057   int next_input;
00058   /* File name to use as stdin for next process.  */
00059   char *next_input_name;
00060   /* Whether next_input_name was allocated using malloc.  */
00061   int next_input_name_allocated;
00062   /* If not -1, stderr pipe from the last process.  */
00063   int stderr_pipe;
00064   /* Number of child processes.  */
00065   int count;
00066   /* PIDs of child processes; array allocated using malloc.  */
00067   long *children;
00068   /* Exit statuses of child processes; array allocated using malloc.  */
00069   int *status;
00070   /* Time used by child processes; array allocated using malloc.  */
00071   struct pex_time *time;
00072   /* Number of children we have already waited for.  */
00073   int number_waited;
00074   /* FILE created by pex_input_file.  */
00075   FILE *input_file;
00076   /* FILE created by pex_read_output.  */
00077   FILE *read_output;
00078   /* FILE created by pex_read_err.  */
00079   FILE *read_err;
00080   /* Number of temporary files to remove.  */
00081   int remove_count;
00082   /* List of temporary files to remove; array allocated using malloc
00083      of strings allocated using malloc.  */
00084   char **remove;
00085   /* Pointers to system dependent functions.  */
00086   const struct pex_funcs *funcs;
00087   /* For use by system dependent code.  */
00088   void *sysdep;
00089 };
00090 
00091 /* Functions passed to pex_run_common.  */
00092 
00093 struct pex_funcs
00094 {
00095   /* Open file NAME for reading.  If BINARY is non-zero, open in
00096      binary mode.  Return >= 0 on success, -1 on error.  */
00097   int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
00098   /* Open file NAME for writing.  If BINARY is non-zero, open in
00099      binary mode.  Return >= 0 on success, -1 on error.  */
00100   int (*open_write) (struct pex_obj *, const char */* name */,
00101                      int /* binary */);
00102   /* Execute a child process.  FLAGS, EXECUTABLE, ARGV, ERR are from
00103      pex_run.  IN, OUT, ERRDES, TOCLOSE are all descriptors, from
00104      open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
00105      STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
00106      STD*_FILE_NO, they should be closed.  If the descriptor TOCLOSE
00107      is not -1, and the system supports pipes, TOCLOSE should be
00108      closed in the child process.  The function should handle the
00109      PEX_STDERR_TO_STDOUT flag.  Return >= 0 on success, or -1 on
00110      error and set *ERRMSG and *ERR.  */
00111   long (*exec_child) (struct pex_obj *, int /* flags */,
00112                       const char */* executable */, char * const * /* argv */,
00113                       char * const * /* env */,
00114                       int /* in */, int /* out */, int /* errdes */,
00115                     int /* toclose */, const char **/* errmsg */,
00116                     int */* err */);
00117   /* Close a descriptor.  Return 0 on success, -1 on error.  */
00118   int (*close) (struct pex_obj *, int);
00119   /* Wait for a child to complete, returning exit status in *STATUS
00120      and time in *TIME (if it is not null).  CHILD is from fork.  DONE
00121      is 1 if this is called via pex_free.  ERRMSG and ERR are as in
00122      fork.  Return 0 on success, -1 on error.  */
00123   int (*wait) (struct pex_obj *, long /* child */, int * /* status */,
00124                struct pex_time * /* time */, int /* done */,
00125                const char ** /* errmsg */, int * /* err */);
00126   /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
00127      descriptors in P[0] and P[1].  If BINARY is non-zero, open in
00128      binary mode.  Return 0 on success, -1 on error.  */
00129   int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
00130   /* Get a FILE pointer to read from a file descriptor (only called if
00131      PEX_USE_PIPES is set).  If BINARY is non-zero, open in binary
00132      mode.  Return pointer on success, NULL on error.  */
00133   FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
00134   /* Get a FILE pointer to write to the file descriptor FD (only
00135      called if PEX_USE_PIPES is set).  If BINARY is non-zero, open in
00136      binary mode.  Arrange for FD not to be inherited by the child
00137      processes.  Return pointer on success, NULL on error.  */
00138   FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
00139   /* Free any system dependent data associated with OBJ.  May be
00140      NULL if there is nothing to do.  */
00141   void (*cleanup) (struct pex_obj *);
00142 };
00143 
00144 extern struct pex_obj *pex_init_common (int, const char *, const char *,
00145                                    const struct pex_funcs *);
00146 
00147 #endif