pexecute.txh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. @c -*- mode: texinfo -*-
  2. @deftypefn Extension {struct pex_obj *} pex_init (int @var{flags}, @
  3. const char *@var{pname}, const char *@var{tempbase})
  4. Prepare to execute one or more programs, with standard output of each
  5. program fed to standard input of the next. This is a system
  6. independent interface to execute a pipeline.
  7. @var{flags} is a bitwise combination of the following:
  8. @table @code
  9. @vindex PEX_RECORD_TIMES
  10. @item PEX_RECORD_TIMES
  11. Record subprocess times if possible.
  12. @vindex PEX_USE_PIPES
  13. @item PEX_USE_PIPES
  14. Use pipes for communication between processes, if possible.
  15. @vindex PEX_SAVE_TEMPS
  16. @item PEX_SAVE_TEMPS
  17. Don't delete temporary files used for communication between
  18. processes.
  19. @end table
  20. @var{pname} is the name of program to be executed, used in error
  21. messages. @var{tempbase} is a base name to use for any required
  22. temporary files; it may be @code{NULL} to use a randomly chosen name.
  23. @end deftypefn
  24. @deftypefn Extension {const char *} pex_run (struct pex_obj *@var{obj}, @
  25. int @var{flags}, const char *@var{executable}, char * const *@var{argv}, @
  26. const char *@var{outname}, const char *@var{errname}, int *@var{err})
  27. Execute one program in a pipeline. On success this returns
  28. @code{NULL}. On failure it returns an error message, a statically
  29. allocated string.
  30. @var{obj} is returned by a previous call to @code{pex_init}.
  31. @var{flags} is a bitwise combination of the following:
  32. @table @code
  33. @vindex PEX_LAST
  34. @item PEX_LAST
  35. This must be set on the last program in the pipeline. In particular,
  36. it should be set when executing a single program. The standard output
  37. of the program will be sent to @var{outname}, or, if @var{outname} is
  38. @code{NULL}, to the standard output of the calling program. Do @emph{not}
  39. set this bit if you want to call @code{pex_read_output}
  40. (described below). After a call to @code{pex_run} with this bit set,
  41. @var{pex_run} may no longer be called with the same @var{obj}.
  42. @vindex PEX_SEARCH
  43. @item PEX_SEARCH
  44. Search for the program using the user's executable search path.
  45. @vindex PEX_SUFFIX
  46. @item PEX_SUFFIX
  47. @var{outname} is a suffix. See the description of @var{outname},
  48. below.
  49. @vindex PEX_STDERR_TO_STDOUT
  50. @item PEX_STDERR_TO_STDOUT
  51. Send the program's standard error to standard output, if possible.
  52. @vindex PEX_BINARY_INPUT
  53. @vindex PEX_BINARY_OUTPUT
  54. @vindex PEX_BINARY_ERROR
  55. @item PEX_BINARY_INPUT
  56. @itemx PEX_BINARY_OUTPUT
  57. @itemx PEX_BINARY_ERROR
  58. The standard input (output or error) of the program should be read (written) in
  59. binary mode rather than text mode. These flags are ignored on systems
  60. which do not distinguish binary mode and text mode, such as Unix. For
  61. proper behavior these flags should match appropriately---a call to
  62. @code{pex_run} using @code{PEX_BINARY_OUTPUT} should be followed by a
  63. call using @code{PEX_BINARY_INPUT}.
  64. @vindex PEX_STDERR_TO_PIPE
  65. @item PEX_STDERR_TO_PIPE
  66. Send the program's standard error to a pipe, if possible. This flag
  67. cannot be specified together with @code{PEX_STDERR_TO_STDOUT}. This
  68. flag can be specified only on the last program in pipeline.
  69. @end table
  70. @var{executable} is the program to execute. @var{argv} is the set of
  71. arguments to pass to the program; normally @code{@var{argv}[0]} will
  72. be a copy of @var{executable}.
  73. @var{outname} is used to set the name of the file to use for standard
  74. output. There are two cases in which no output file will be used:
  75. @enumerate
  76. @item
  77. if @code{PEX_LAST} is not set in @var{flags}, and @code{PEX_USE_PIPES}
  78. was set in the call to @code{pex_init}, and the system supports pipes
  79. @item
  80. if @code{PEX_LAST} is set in @var{flags}, and @var{outname} is
  81. @code{NULL}
  82. @end enumerate
  83. @noindent
  84. Otherwise the code will use a file to hold standard
  85. output. If @code{PEX_LAST} is not set, this file is considered to be
  86. a temporary file, and it will be removed when no longer needed, unless
  87. @code{PEX_SAVE_TEMPS} was set in the call to @code{pex_init}.
  88. There are two cases to consider when setting the name of the file to
  89. hold standard output.
  90. @enumerate
  91. @item
  92. @code{PEX_SUFFIX} is set in @var{flags}. In this case
  93. @var{outname} may not be @code{NULL}. If the @var{tempbase} parameter
  94. to @code{pex_init} was not @code{NULL}, then the output file name is
  95. the concatenation of @var{tempbase} and @var{outname}. If
  96. @var{tempbase} was @code{NULL}, then the output file name is a random
  97. file name ending in @var{outname}.
  98. @item
  99. @code{PEX_SUFFIX} was not set in @var{flags}. In this
  100. case, if @var{outname} is not @code{NULL}, it is used as the output
  101. file name. If @var{outname} is @code{NULL}, and @var{tempbase} was
  102. not NULL, the output file name is randomly chosen using
  103. @var{tempbase}. Otherwise the output file name is chosen completely
  104. at random.
  105. @end enumerate
  106. @var{errname} is the file name to use for standard error output. If
  107. it is @code{NULL}, standard error is the same as the caller's.
  108. Otherwise, standard error is written to the named file.
  109. On an error return, the code sets @code{*@var{err}} to an @code{errno}
  110. value, or to 0 if there is no relevant @code{errno}.
  111. @end deftypefn
  112. @deftypefn Extension {const char *} pex_run_in_environment (struct pex_obj *@var{obj}, @
  113. int @var{flags}, const char *@var{executable}, char * const *@var{argv}, @
  114. char * const *@var{env}, int @var{env_size}, const char *@var{outname}, @
  115. const char *@var{errname}, int *@var{err})
  116. Execute one program in a pipeline, permitting the environment for the
  117. program to be specified. Behaviour and parameters not listed below are
  118. as for @code{pex_run}.
  119. @var{env} is the environment for the child process, specified as an array of
  120. character pointers. Each element of the array should point to a string of the
  121. form @code{VAR=VALUE}, with the exception of the last element that must be
  122. @code{NULL}.
  123. @end deftypefn
  124. @deftypefn Extension {FILE *} pex_input_file (struct pex_obj *@var{obj}, @
  125. int @var{flags}, const char *@var{in_name})
  126. Return a stream for a temporary file to pass to the first program in
  127. the pipeline as input.
  128. The name of the input file is chosen according to the same rules
  129. @code{pex_run} uses to choose output file names, based on
  130. @var{in_name}, @var{obj} and the @code{PEX_SUFFIX} bit in @var{flags}.
  131. Don't call @code{fclose} on the returned stream; the first call to
  132. @code{pex_run} closes it automatically.
  133. If @var{flags} includes @code{PEX_BINARY_OUTPUT}, open the stream in
  134. binary mode; otherwise, open it in the default mode. Including
  135. @code{PEX_BINARY_OUTPUT} in @var{flags} has no effect on Unix.
  136. @end deftypefn
  137. @deftypefn Extension {FILE *} pex_input_pipe (struct pex_obj *@var{obj}, @
  138. int @var{binary})
  139. Return a stream @var{fp} for a pipe connected to the standard input of
  140. the first program in the pipeline; @var{fp} is opened for writing.
  141. You must have passed @code{PEX_USE_PIPES} to the @code{pex_init} call
  142. that returned @var{obj}.
  143. You must close @var{fp} using @code{fclose} yourself when you have
  144. finished writing data to the pipeline.
  145. The file descriptor underlying @var{fp} is marked not to be inherited
  146. by child processes.
  147. On systems that do not support pipes, this function returns
  148. @code{NULL}, and sets @code{errno} to @code{EINVAL}. If you would
  149. like to write code that is portable to all systems the @code{pex}
  150. functions support, consider using @code{pex_input_file} instead.
  151. There are two opportunities for deadlock using
  152. @code{pex_input_pipe}:
  153. @itemize @bullet
  154. @item
  155. Most systems' pipes can buffer only a fixed amount of data; a process
  156. that writes to a full pipe blocks. Thus, if you write to @file{fp}
  157. before starting the first process, you run the risk of blocking when
  158. there is no child process yet to read the data and allow you to
  159. continue. @code{pex_input_pipe} makes no promises about the
  160. size of the pipe's buffer, so if you need to write any data at all
  161. before starting the first process in the pipeline, consider using
  162. @code{pex_input_file} instead.
  163. @item
  164. Using @code{pex_input_pipe} and @code{pex_read_output} together
  165. may also cause deadlock. If the output pipe fills up, so that each
  166. program in the pipeline is waiting for the next to read more data, and
  167. you fill the input pipe by writing more data to @var{fp}, then there
  168. is no way to make progress: the only process that could read data from
  169. the output pipe is you, but you are blocked on the input pipe.
  170. @end itemize
  171. @end deftypefn
  172. @deftypefn Extension {FILE *} pex_read_output (struct pex_obj *@var{obj}, @
  173. int @var{binary})
  174. Returns a @code{FILE} pointer which may be used to read the standard
  175. output of the last program in the pipeline. When this is used,
  176. @code{PEX_LAST} should not be used in a call to @code{pex_run}. After
  177. this is called, @code{pex_run} may no longer be called with the same
  178. @var{obj}. @var{binary} should be non-zero if the file should be
  179. opened in binary mode. Don't call @code{fclose} on the returned file;
  180. it will be closed by @code{pex_free}.
  181. @end deftypefn
  182. @deftypefn Extension {FILE *} pex_read_err (struct pex_obj *@var{obj}, @
  183. int @var{binary})
  184. Returns a @code{FILE} pointer which may be used to read the standard
  185. error of the last program in the pipeline. When this is used,
  186. @code{PEX_LAST} should not be used in a call to @code{pex_run}. After
  187. this is called, @code{pex_run} may no longer be called with the same
  188. @var{obj}. @var{binary} should be non-zero if the file should be
  189. opened in binary mode. Don't call @code{fclose} on the returned file;
  190. it will be closed by @code{pex_free}.
  191. @end deftypefn
  192. @deftypefn Extension int pex_get_status (struct pex_obj *@var{obj}, @
  193. int @var{count}, int *@var{vector})
  194. Returns the exit status of all programs run using @var{obj}.
  195. @var{count} is the number of results expected. The results will be
  196. placed into @var{vector}. The results are in the order of the calls
  197. to @code{pex_run}. Returns 0 on error, 1 on success.
  198. @end deftypefn
  199. @deftypefn Extension int pex_get_times (struct pex_obj *@var{obj}, @
  200. int @var{count}, struct pex_time *@var{vector})
  201. Returns the process execution times of all programs run using
  202. @var{obj}. @var{count} is the number of results expected. The
  203. results will be placed into @var{vector}. The results are in the
  204. order of the calls to @code{pex_run}. Returns 0 on error, 1 on
  205. success.
  206. @code{struct pex_time} has the following fields of the type
  207. @code{unsigned long}: @code{user_seconds},
  208. @code{user_microseconds}, @code{system_seconds},
  209. @code{system_microseconds}. On systems which do not support reporting
  210. process times, all the fields will be set to @code{0}.
  211. @end deftypefn
  212. @deftypefn Extension void pex_free (struct pex_obj @var{obj})
  213. Clean up and free all data associated with @var{obj}. If you have not
  214. yet called @code{pex_get_times} or @code{pex_get_status}, this will
  215. try to kill the subprocesses.
  216. @end deftypefn
  217. @deftypefn Extension {const char *} pex_one (int @var{flags}, @
  218. const char *@var{executable}, char * const *@var{argv}, @
  219. const char *@var{pname}, const char *@var{outname}, const char *@var{errname}, @
  220. int *@var{status}, int *@var{err})
  221. An interface to permit the easy execution of a
  222. single program. The return value and most of the parameters are as
  223. for a call to @code{pex_run}. @var{flags} is restricted to a
  224. combination of @code{PEX_SEARCH}, @code{PEX_STDERR_TO_STDOUT}, and
  225. @code{PEX_BINARY_OUTPUT}. @var{outname} is interpreted as if
  226. @code{PEX_LAST} were set. On a successful return, @code{*@var{status}} will
  227. be set to the exit status of the program.
  228. @end deftypefn
  229. @deftypefn Extension int pexecute (const char *@var{program}, @
  230. char * const *@var{argv}, const char *@var{this_pname}, @
  231. const char *@var{temp_base}, char **@var{errmsg_fmt}, @
  232. char **@var{errmsg_arg}, int @var{flags})
  233. This is the old interface to execute one or more programs. It is
  234. still supported for compatibility purposes, but is no longer
  235. documented.
  236. @end deftypefn
  237. @deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
  238. Another part of the old execution interface.
  239. @end deftypefn