Process.h.in 17 KB

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