Process.h.in 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_Process_h
  11. #define @KWSYS_NAMESPACE@_Process_h
  12. #include <@KWSYS_NAMESPACE@/Configure.h>
  13. /* Redefine all public interface symbol names to be in the proper
  14. namespace. These macros are used internally to kwsys only, and are
  15. not visible to user code. Use kwsysHeaderDump.pl to reproduce
  16. these macros after making changes to the interface. */
  17. #if !defined(KWSYS_NAMESPACE)
  18. # define kwsys_ns(x) @KWSYS_NAMESPACE@##x
  19. # define kwsysEXPORT @KWSYS_NAMESPACE@_EXPORT
  20. #endif
  21. #if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  22. # define kwsysProcess kwsys_ns(Process)
  23. # define kwsysProcess_s kwsys_ns(Process_s)
  24. # define kwsysProcess_New kwsys_ns(Process_New)
  25. # define kwsysProcess_Delete kwsys_ns(Process_Delete)
  26. # define kwsysProcess_SetCommand kwsys_ns(Process_SetCommand)
  27. # define kwsysProcess_AddCommand kwsys_ns(Process_AddCommand)
  28. # define kwsysProcess_SetTimeout kwsys_ns(Process_SetTimeout)
  29. # define kwsysProcess_SetWorkingDirectory kwsys_ns(Process_SetWorkingDirectory)
  30. # define kwsysProcess_SetPipeFile kwsys_ns(Process_SetPipeFile)
  31. # define kwsysProcess_SetPipeNative kwsys_ns(Process_SetPipeNative)
  32. # define kwsysProcess_SetPipeShared kwsys_ns(Process_SetPipeShared)
  33. # define kwsysProcess_Option_Detach kwsys_ns(Process_Option_Detach)
  34. # define kwsysProcess_Option_HideWindow kwsys_ns(Process_Option_HideWindow)
  35. # define kwsysProcess_Option_Verbatim kwsys_ns(Process_Option_Verbatim)
  36. # define kwsysProcess_GetOption kwsys_ns(Process_GetOption)
  37. # define kwsysProcess_SetOption kwsys_ns(Process_SetOption)
  38. # define kwsysProcess_Option_e kwsys_ns(Process_Option_e)
  39. # define kwsysProcess_State_Starting kwsys_ns(Process_State_Starting)
  40. # define kwsysProcess_State_Error kwsys_ns(Process_State_Error)
  41. # define kwsysProcess_State_Exception kwsys_ns(Process_State_Exception)
  42. # define kwsysProcess_State_Executing kwsys_ns(Process_State_Executing)
  43. # define kwsysProcess_State_Exited kwsys_ns(Process_State_Exited)
  44. # define kwsysProcess_State_Expired kwsys_ns(Process_State_Expired)
  45. # define kwsysProcess_State_Killed kwsys_ns(Process_State_Killed)
  46. # define kwsysProcess_State_Disowned kwsys_ns(Process_State_Disowned)
  47. # define kwsysProcess_GetState kwsys_ns(Process_GetState)
  48. # define kwsysProcess_State_e kwsys_ns(Process_State_e)
  49. # define kwsysProcess_Exception_None kwsys_ns(Process_Exception_None)
  50. # define kwsysProcess_Exception_Fault kwsys_ns(Process_Exception_Fault)
  51. # define kwsysProcess_Exception_Illegal kwsys_ns(Process_Exception_Illegal)
  52. # define kwsysProcess_Exception_Interrupt kwsys_ns(Process_Exception_Interrupt)
  53. # define kwsysProcess_Exception_Numerical kwsys_ns(Process_Exception_Numerical)
  54. # define kwsysProcess_Exception_Other kwsys_ns(Process_Exception_Other)
  55. # define kwsysProcess_GetExitException kwsys_ns(Process_GetExitException)
  56. # define kwsysProcess_Exception_e kwsys_ns(Process_Exception_e)
  57. # define kwsysProcess_GetExitCode kwsys_ns(Process_GetExitCode)
  58. # define kwsysProcess_GetExitValue kwsys_ns(Process_GetExitValue)
  59. # define kwsysProcess_GetErrorString kwsys_ns(Process_GetErrorString)
  60. # define kwsysProcess_GetExceptionString kwsys_ns(Process_GetExceptionString)
  61. # define kwsysProcess_Execute kwsys_ns(Process_Execute)
  62. # define kwsysProcess_Disown kwsys_ns(Process_Disown)
  63. # define kwsysProcess_WaitForData kwsys_ns(Process_WaitForData)
  64. # define kwsysProcess_Pipes_e kwsys_ns(Process_Pipes_e)
  65. # define kwsysProcess_Pipe_None kwsys_ns(Process_Pipe_None)
  66. # define kwsysProcess_Pipe_STDIN kwsys_ns(Process_Pipe_STDIN)
  67. # define kwsysProcess_Pipe_STDOUT kwsys_ns(Process_Pipe_STDOUT)
  68. # define kwsysProcess_Pipe_STDERR kwsys_ns(Process_Pipe_STDERR)
  69. # define kwsysProcess_Pipe_Timeout kwsys_ns(Process_Pipe_Timeout)
  70. # define kwsysProcess_Pipe_Handle kwsys_ns(Process_Pipe_Handle)
  71. # define kwsysProcess_WaitForExit kwsys_ns(Process_WaitForExit)
  72. # define kwsysProcess_Kill kwsys_ns(Process_Kill)
  73. #endif
  74. #if defined(__cplusplus)
  75. extern "C"
  76. {
  77. #endif
  78. /**
  79. * Process control data structure.
  80. */
  81. typedef struct kwsysProcess_s kwsysProcess;
  82. /* Platform-specific pipe handle type. */
  83. #if defined(_WIN32) && !defined(__CYGWIN__)
  84. typedef void* kwsysProcess_Pipe_Handle;
  85. #else
  86. typedef int kwsysProcess_Pipe_Handle;
  87. #endif
  88. /**
  89. * Create a new Process instance.
  90. */
  91. kwsysEXPORT kwsysProcess* kwsysProcess_New(void);
  92. /**
  93. * Delete an existing Process instance. If the instance is currently
  94. * executing a process, this blocks until the process terminates.
  95. */
  96. kwsysEXPORT void kwsysProcess_Delete(kwsysProcess* cp);
  97. /**
  98. * Set the command line to be executed. Argument is an array of
  99. * pointers to the command and each argument. The array must end with
  100. * a NULL pointer. Any previous command lines are removed. Returns
  101. * 1 for success and 0 otherwise.
  102. */
  103. kwsysEXPORT int kwsysProcess_SetCommand(kwsysProcess* cp,
  104. char const* const* command);
  105. /**
  106. * Add a command line to be executed. Argument is an array of
  107. * pointers to the command and each argument. The array must end with
  108. * a NULL pointer. If this is not the first command added, its
  109. * standard input will be connected to the standard output of the
  110. * previous command. Returns 1 for success and 0 otherwise.
  111. */
  112. kwsysEXPORT int kwsysProcess_AddCommand(kwsysProcess* cp,
  113. char const* const* command);
  114. /**
  115. * Set the timeout in seconds for the child process. The timeout
  116. * period begins when the child is executed. If the child has not
  117. * terminated when the timeout expires, it will be killed. A
  118. * non-positive (<= 0) value will disable the timeout.
  119. */
  120. kwsysEXPORT void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout);
  121. /**
  122. * Set the working directory for the child process. The working
  123. * directory can be absolute or relative to the current directory.
  124. * Returns 1 for success and 0 for failure.
  125. */
  126. kwsysEXPORT int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp,
  127. const char* dir);
  128. /**
  129. * Set the name of a file to be attached to the given pipe. Returns 1
  130. * for success and 0 for failure.
  131. */
  132. kwsysEXPORT int kwsysProcess_SetPipeFile(kwsysProcess* cp, int pipe,
  133. const char* file);
  134. /**
  135. * Set whether the given pipe in the child is shared with the parent
  136. * process. The default is no for Pipe_STDOUT and Pipe_STDERR and yes
  137. * for Pipe_STDIN.
  138. */
  139. kwsysEXPORT void kwsysProcess_SetPipeShared(kwsysProcess* cp, int pipe,
  140. int shared);
  141. /**
  142. * Specify a platform-specific native pipe for use as one of the child
  143. * interface pipes. The native pipe is specified by an array of two
  144. * descriptors or handles. The first entry in the array (index 0)
  145. * should be the read end of the pipe. The second entry in the array
  146. * (index 1) should be the write end of the pipe. If a null pointer
  147. * is given the option will be disabled.
  148. *
  149. * For Pipe_STDIN the native pipe is connected to the first child in
  150. * the pipeline as its stdin. After the children are created the
  151. * write end of the pipe will be closed in the child process and the
  152. * read end will be closed in the parent process.
  153. *
  154. * For Pipe_STDOUT and Pipe_STDERR the pipe is connected to the last
  155. * child as its stdout or stderr. After the children are created the
  156. * write end of the pipe will be closed in the parent process and the
  157. * read end will be closed in the child process.
  158. */
  159. kwsysEXPORT void kwsysProcess_SetPipeNative(kwsysProcess* cp, int pipe,
  160. kwsysProcess_Pipe_Handle p[2]);
  161. /**
  162. * Get/Set a possibly platform-specific option. Possible options are:
  163. *
  164. * kwsysProcess_Option_Detach = Whether to detach the process.
  165. * 0 = No (default)
  166. * 1 = Yes
  167. *
  168. * kwsysProcess_Option_HideWindow = Whether to hide window on Windows.
  169. * 0 = No (default)
  170. * 1 = Yes
  171. *
  172. * kwsysProcess_Option_Verbatim = Whether SetCommand and AddCommand
  173. * should treat the first argument
  174. * as a verbatim command line
  175. * and ignore the rest of the arguments.
  176. * 0 = No (default)
  177. * 1 = Yes
  178. */
  179. kwsysEXPORT int kwsysProcess_GetOption(kwsysProcess* cp, int optionId);
  180. kwsysEXPORT void kwsysProcess_SetOption(kwsysProcess* cp, int optionId,
  181. int value);
  182. enum kwsysProcess_Option_e
  183. {
  184. kwsysProcess_Option_HideWindow,
  185. kwsysProcess_Option_Detach,
  186. kwsysProcess_Option_Verbatim
  187. };
  188. /**
  189. * Get the current state of the Process instance. Possible states are:
  190. *
  191. * kwsysProcess_State_Starting = Execute has not yet been called.
  192. * kwsysProcess_State_Error = Error administrating the child process.
  193. * kwsysProcess_State_Exception = Child process exited abnormally.
  194. * kwsysProcess_State_Executing = Child process is currently running.
  195. * kwsysProcess_State_Exited = Child process exited normally.
  196. * kwsysProcess_State_Expired = Child process's timeout expired.
  197. * kwsysProcess_State_Killed = Child process terminated by Kill method.
  198. * kwsysProcess_State_Disowned = Child is no longer managed by this object.
  199. */
  200. kwsysEXPORT int kwsysProcess_GetState(kwsysProcess* cp);
  201. enum kwsysProcess_State_e
  202. {
  203. kwsysProcess_State_Starting,
  204. kwsysProcess_State_Error,
  205. kwsysProcess_State_Exception,
  206. kwsysProcess_State_Executing,
  207. kwsysProcess_State_Exited,
  208. kwsysProcess_State_Expired,
  209. kwsysProcess_State_Killed,
  210. kwsysProcess_State_Disowned
  211. };
  212. /**
  213. * When GetState returns "Exception", this method returns a
  214. * platform-independent description of the exceptional behavior that
  215. * caused the child to terminate abnormally. Possible exceptions are:
  216. *
  217. * kwsysProcess_Exception_None = No exceptional behavior occurred.
  218. * kwsysProcess_Exception_Fault = Child crashed with a memory fault.
  219. * kwsysProcess_Exception_Illegal = Child crashed with an illegal instruction.
  220. * kwsysProcess_Exception_Interrupt = Child was interrupted by user (Cntl-C/Break).
  221. * kwsysProcess_Exception_Numerical = Child crashed with a numerical exception.
  222. * kwsysProcess_Exception_Other = Child terminated for another reason.
  223. */
  224. kwsysEXPORT int kwsysProcess_GetExitException(kwsysProcess* cp);
  225. enum kwsysProcess_Exception_e
  226. {
  227. kwsysProcess_Exception_None,
  228. kwsysProcess_Exception_Fault,
  229. kwsysProcess_Exception_Illegal,
  230. kwsysProcess_Exception_Interrupt,
  231. kwsysProcess_Exception_Numerical,
  232. kwsysProcess_Exception_Other
  233. };
  234. /**
  235. * When GetState returns "Exited" or "Exception", this method returns
  236. * the platform-specific raw exit code of the process. UNIX platforms
  237. * should use WIFEXITED/WEXITSTATUS and WIFSIGNALED/WTERMSIG to access
  238. * this value. Windows users should compare the value to the various
  239. * EXCEPTION_* values.
  240. *
  241. * If GetState returns "Exited", use GetExitValue to get the
  242. * platform-independent child return value.
  243. */
  244. kwsysEXPORT int kwsysProcess_GetExitCode(kwsysProcess* cp);
  245. /**
  246. * When GetState returns "Exited", this method returns the child's
  247. * platform-independent exit code (such as the value returned by the
  248. * child's main).
  249. */
  250. kwsysEXPORT int kwsysProcess_GetExitValue(kwsysProcess* cp);
  251. /**
  252. * When GetState returns "Error", this method returns a string
  253. * describing the problem. Otherwise, it returns NULL.
  254. */
  255. kwsysEXPORT const char* kwsysProcess_GetErrorString(kwsysProcess* cp);
  256. /**
  257. * When GetState returns "Exception", this method returns a string
  258. * describing the problem. Otherwise, it returns NULL.
  259. */
  260. kwsysEXPORT const char* kwsysProcess_GetExceptionString(kwsysProcess* cp);
  261. /**
  262. * Start executing the child process.
  263. */
  264. kwsysEXPORT void kwsysProcess_Execute(kwsysProcess* cp);
  265. /**
  266. * Stop management of a detached child process. This closes any pipes
  267. * being read. If the child was not created with the
  268. * kwsysProcess_Option_Detach option, this method does nothing. This
  269. * is because disowning a non-detached process will cause the child
  270. * exit signal to be left unhandled until this process exits.
  271. */
  272. kwsysEXPORT void kwsysProcess_Disown(kwsysProcess* cp);
  273. /**
  274. * Block until data are available on a pipe, a timeout expires, or the
  275. * child process terminates. Arguments are as follows:
  276. *
  277. * data = If data are read, the pointer to which this points is
  278. * set to point to the data.
  279. * length = If data are read, the integer to which this points is
  280. * set to the length of the data read.
  281. * timeout = Specifies the maximum time this call may block. Upon
  282. * return after reading data, the time elapsed is subtracted
  283. * from the timeout value. If this timeout expires, the
  284. * value is set to 0. A NULL pointer passed for this argument
  285. * indicates no timeout for the call. A negative or zero
  286. * value passed for this argument may be used for polling
  287. * and will always return immediately.
  288. *
  289. * Return value will be one of:
  290. *
  291. * Pipe_None = No more data will be available from the child process,
  292. * ( == 0) or no process has been executed. WaitForExit should
  293. * be called to wait for the process to terminate.
  294. * Pipe_STDOUT = Data have been read from the child's stdout pipe.
  295. * Pipe_STDERR = Data have been read from the child's stderr pipe.
  296. * Pipe_Timeout = No data available within timeout specified for the
  297. * call. Time elapsed has been subtracted from timeout
  298. * argument.
  299. */
  300. kwsysEXPORT int kwsysProcess_WaitForData(kwsysProcess* cp, char** data,
  301. int* length, double* timeout);
  302. enum kwsysProcess_Pipes_e
  303. {
  304. kwsysProcess_Pipe_None,
  305. kwsysProcess_Pipe_STDIN,
  306. kwsysProcess_Pipe_STDOUT,
  307. kwsysProcess_Pipe_STDERR,
  308. kwsysProcess_Pipe_Timeout=255
  309. };
  310. /**
  311. * Block until the child process terminates or the given timeout
  312. * expires. If no process is running, returns immediatly. The
  313. * argument is:
  314. *
  315. * timeout = Specifies the maximum time this call may block. Upon
  316. * returning due to child termination, the elapsed time
  317. * is subtracted from the given value. A NULL pointer
  318. * passed for this argument indicates no timeout for the
  319. * call.
  320. *
  321. * Return value will be one of:
  322. *
  323. * 0 = Child did not terminate within timeout specified for
  324. * the call. Time elapsed has been subtracted from timeout
  325. * argument.
  326. * 1 = Child has terminated or was not running.
  327. */
  328. kwsysEXPORT int kwsysProcess_WaitForExit(kwsysProcess* cp, double* timeout);
  329. /**
  330. * Forcefully terminate the child process that is currently running.
  331. * The caller should call WaitForExit after this returns to wait for
  332. * the child to terminate.
  333. */
  334. kwsysEXPORT void kwsysProcess_Kill(kwsysProcess* cp);
  335. #if defined(__cplusplus)
  336. } /* extern "C" */
  337. #endif
  338. /* If we are building a kwsys .c or .cxx file, let it use these macros.
  339. Otherwise, undefine them to keep the namespace clean. */
  340. #if !defined(KWSYS_NAMESPACE)
  341. # undef kwsys_ns
  342. # undef kwsysEXPORT
  343. # if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  344. # undef kwsysProcess
  345. # undef kwsysProcess_s
  346. # undef kwsysProcess_New
  347. # undef kwsysProcess_Delete
  348. # undef kwsysProcess_SetCommand
  349. # undef kwsysProcess_AddCommand
  350. # undef kwsysProcess_SetTimeout
  351. # undef kwsysProcess_SetWorkingDirectory
  352. # undef kwsysProcess_SetPipeFile
  353. # undef kwsysProcess_SetPipeNative
  354. # undef kwsysProcess_SetPipeShared
  355. # undef kwsysProcess_Option_Detach
  356. # undef kwsysProcess_Option_HideWindow
  357. # undef kwsysProcess_Option_Verbatim
  358. # undef kwsysProcess_GetOption
  359. # undef kwsysProcess_SetOption
  360. # undef kwsysProcess_Option_e
  361. # undef kwsysProcess_State_Starting
  362. # undef kwsysProcess_State_Error
  363. # undef kwsysProcess_State_Exception
  364. # undef kwsysProcess_State_Executing
  365. # undef kwsysProcess_State_Exited
  366. # undef kwsysProcess_State_Expired
  367. # undef kwsysProcess_State_Killed
  368. # undef kwsysProcess_State_Disowned
  369. # undef kwsysProcess_GetState
  370. # undef kwsysProcess_State_e
  371. # undef kwsysProcess_Exception_None
  372. # undef kwsysProcess_Exception_Fault
  373. # undef kwsysProcess_Exception_Illegal
  374. # undef kwsysProcess_Exception_Interrupt
  375. # undef kwsysProcess_Exception_Numerical
  376. # undef kwsysProcess_Exception_Other
  377. # undef kwsysProcess_GetExitException
  378. # undef kwsysProcess_Exception_e
  379. # undef kwsysProcess_GetExitCode
  380. # undef kwsysProcess_GetExitValue
  381. # undef kwsysProcess_GetErrorString
  382. # undef kwsysProcess_GetExceptionString
  383. # undef kwsysProcess_Execute
  384. # undef kwsysProcess_Disown
  385. # undef kwsysProcess_WaitForData
  386. # undef kwsysProcess_Pipes_e
  387. # undef kwsysProcess_Pipe_None
  388. # undef kwsysProcess_Pipe_STDIN
  389. # undef kwsysProcess_Pipe_STDOUT
  390. # undef kwsysProcess_Pipe_STDERR
  391. # undef kwsysProcess_Pipe_Timeout
  392. # undef kwsysProcess_Pipe_Handle
  393. # undef kwsysProcess_WaitForExit
  394. # undef kwsysProcess_Kill
  395. # endif
  396. #endif
  397. #endif